(SRP) Scene Window breaking when selecting the main camera

I’m experimenting with the SRP and have pretty simple one written. So far things are working pretty well except for when I select the main camera then the Scene tab doesn’t draw correctly.

Not Selected:

Selected:

Note that the tabs are also not drawing for the editor and all the grid lines are missing. The main camera is also the standard built in one which is drawing in play mode and a build just fine.

Below is the code I’ve written so far:

using UnityEngine;
using UnityEngine.Rendering;

public class ExampleRenderPipelineInstance : RenderPipeline
{
#if UNITY_EDITOR
    private static readonly ShaderTagId[] LEGACY_SHADER_TAGS =
    {
        new ShaderTagId("Always"),
        new ShaderTagId("ForwardBase"),
        new ShaderTagId("PrepassBase"),
        new ShaderTagId("Vertex"),
        new ShaderTagId("VertexLMRGBM"),
        new ShaderTagId("VertexLM"),
    };

    private static Material _errorMaterial;
  
#endif

    private static ShaderTagId _standardDrawTag = new ShaderTagId("SRPDefaultUnlit");
  
    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        CommandBuffer cmd = CommandBufferPool.Get();
        cmd.ClearRenderTarget(true, true, Color.cyan * 0.25f);
      
        context.ExecuteCommandBuffer(cmd);
      
        cmd.Clear();
        CommandBufferPool.Release(cmd);
      
        for (int i = 0; i < cameras.Length; i++)
        {
            Camera camera = cameras[i];

#if UNITY_EDITOR
            if (camera.cameraType == CameraType.SceneView)
            {
                ScriptableRenderContext.EmitWorldGeometryForSceneView(camera);
            }
#endif
          
            camera.TryGetCullingParameters(out ScriptableCullingParameters cullingParams);
            CullingResults cullingResults = context.Cull(ref cullingParams);
          
            context.SetupCameraProperties(camera);
              
            ShaderTagId shaderTagId = _standardDrawTag;

            SortingSettings sortingSettings = new SortingSettings(camera);

            DrawingSettings drawingSettings = new DrawingSettings(shaderTagId, sortingSettings);
              
            FilteringSettings filteringSettings = FilteringSettings.defaultValue;
              
            context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings);
          
#if UNITY_EDITOR
            drawingSettings = new DrawingSettings(LEGACY_SHADER_TAGS[0], sortingSettings);

            if (_errorMaterial == null)
            {
                _errorMaterial = new Material(Shader.Find("Hidden/InternalErrorShader"));
            }

            drawingSettings.overrideMaterial = _errorMaterial;

            for (int j = 1; j < LEGACY_SHADER_TAGS.Length; j++)
            {
                drawingSettings.SetShaderPassName(j, LEGACY_SHADER_TAGS[j]);
            }
          
            filteringSettings = FilteringSettings.defaultValue;
            context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings);
          

            if (UnityEditor.Handles.ShouldRenderGizmos())
            {
                context.DrawGizmos(camera, GizmoSubset.PreImageEffects);
                context.DrawWireOverlay(camera);
                context.DrawGizmos(camera, GizmoSubset.PostImageEffects);
            }
#endif

        }
      
        context.Submit();
    }
}

Unity Version: 2020.3.30f1
SRP Core Version: 10.8.1

I’m sure I’m just missing some simple API call. Any help would be greatly appreciated :slight_smile:

Resolved by experimenting pretty quickly. Turns out when you click on a camera the sceneview and the preview camera are instead rendering to render textures and this SRP code isn’t respecting that. Below is what I’ve done to deal with that.

// Clearing the render target removed up here

for (int i = 0; i < cameras.Length; i++)
{
    Camera camera = cameras[i];
  
    if (camera.clearFlags == CameraClearFlags.Color)
    {
        CommandBuffer cmd = CommandBufferPool.Get();

        if (camera.targetTexture != null)
        {
            cmd.SetRenderTarget(camera.targetTexture);
        }
      
        cmd.ClearRenderTarget(true, true, camera.backgroundColor);
      
        context.ExecuteCommandBuffer(cmd);

        cmd.Clear();
        CommandBufferPool.Release(cmd);
    }
    // ...
}