Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  3. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Flickering depth clear output in SRP

Discussion in 'Graphics Experimental Previews' started by Mourdraug, Sep 6, 2023.

  1. Mourdraug

    Mourdraug

    Joined:
    Jun 23, 2013
    Posts:
    3
    I'm trying to write my own deferred pipeline following multiple sources, but primarily this one: https://docs.unity3d.com/ScriptReference/Rendering.ScriptableRenderContext.BeginRenderPass.html
    All the other attachments seem to be working fine(save for some scaling and being upside down issues) but my depth attachment behaves very weirdly (see the video below). I recorded it with a call to:
    depth.ConfigureClear(new Color(0, 0, 0, 0), 1, 0);
    before I added that it flickered red for some reason. What confuses me most is why it tries to clear it with color at all since I passed its index as depth in BeginRenderPass.


    Code (CSharp):
    1. public void Render(ScriptableRenderContext context, Camera camera)
    2.     {
    3.         this.context = context;
    4.         this.camera = camera;
    5.  
    6.         PrepareBuffer();
    7.         PrepareForSceneWindow();
    8.         if (!Cull())
    9.         {
    10.             return;
    11.         }
    12.  
    13.         Setup();
    14.  
    15.         int width = camera.scaledPixelWidth;
    16.         int height = camera.scaledPixelHeight;
    17.         int aa = 1;
    18.         RenderTargetIdentifier target = BuiltinRenderTextureType.CameraTarget;
    19.  
    20.         if (camera.targetTexture)
    21.         {
    22.             width = camera.targetTexture.width;
    23.             height = camera.targetTexture.height;
    24.             target = camera.targetTexture;
    25.             aa = camera.targetTexture.antiAliasing;
    26.         }
    27.  
    28.         var colorFormat = QualitySettings.activeColorSpace == ColorSpace.Linear ? GraphicsFormat.R8G8B8A8_SRGB : GraphicsFormat.R8G8B8A8_UNorm;
    29.  
    30.         var midtone = new AttachmentDescriptor(colorFormat);
    31.         var highlight = new AttachmentDescriptor(colorFormat);
    32.         var shadow = new AttachmentDescriptor(colorFormat);
    33.         var normal = new AttachmentDescriptor(GraphicsFormat.R8G8B8A8_UNorm);
    34.         var emission = new AttachmentDescriptor(GraphicsFormat.B10G11R11_UFloatPack32);
    35.         var depth = new AttachmentDescriptor(RenderTextureFormat.Depth);
    36.  
    37.         midtone.ConfigureClear(Color.black, 1, 0);
    38.         highlight.ConfigureClear(Color.black, 1, 0);
    39.         shadow.ConfigureClear(Color.black, 1, 0);
    40.         normal.ConfigureClear(Color.black, 1, 0);
    41.         emission.ConfigureClear(Color.black, 1, 0);
    42.         depth.ConfigureClear(new Color(0, 0, 0, 0), 1, 0);
    43.  
    44.         midtone.ConfigureTarget(target, false, true);
    45.  
    46.  
    47.         const int depthIndex = 0, midtoneIndex = 1, highlightIndex = 2, shadowIndex = 3, normalIndex = 4, emissionIndex = 5;
    48.  
    49.         var attachments = new NativeArray<AttachmentDescriptor>(6, Allocator.Temp);
    50.         attachments[depthIndex] = depth;
    51.         attachments[midtoneIndex] = midtone;
    52.         attachments[highlightIndex] = highlight;
    53.         attachments[shadowIndex] = shadow;
    54.         attachments[normalIndex] = normal;
    55.         attachments[emissionIndex] = emission;
    56.         using (context.BeginScopedRenderPass(width, height, aa, attachments, depthIndex))
    57.         {