Search Unity

CommandBuffer rendering to the camera and a render target

Discussion in 'General Graphics' started by NoiseFloorDev, Aug 17, 2017.

  1. NoiseFloorDev

    NoiseFloorDev

    Joined:
    May 13, 2017
    Posts:
    104
    I'm trying to make an object render both to the camera and to a render target, so I get the target in isolation for effects while also rendering it normally to the camera in a single pass. I'm using forward rendering, the object is hidden from normal rendering with the camera mask, and my shader has two color outputs.

    I create a temporary texture, clear it, then set the render target to the camera and the temporary:

    Code (csharp):
    1.  
    2.         RenderTextureDescriptor renderTextureDescriptor = new RenderTextureDescriptor(width, height, RenderTextureFormat.Default);
    3.         commandBuffer.GetTemporaryRT(textureId, renderTextureDescriptor, FilterMode.Bilinear);
    4.  
    5.         commandBuffer.SetRenderTarget(textureId);
    6.         commandBuffer.ClearRenderTarget(false, true, Color.black);
    7.  
    8.         RenderTargetIdentifier[] colorBuffers = new RenderTargetIdentifier[2] {
    9.             BuiltinRenderTextureType.CameraTarget,
    10.             textureId,
    11.         };
    12.  
    13.         commandBuffer.SetRenderTarget(colorBuffers, BuiltinRenderTextureType.CameraTarget);
    14.         commandBuffer.DrawRenderer(objectRenderer, objectRenderer.sharedMaterial);
    15.         Camera.main.AddCommandBuffer(CameraEvent.AfterForwardOpaque, commandBuffer);
    16.  

    However, the mesh isn't drawn into the camera. The frame debugger shows the second render target as receiving the output of COLOR0, when it should be getting COLOR1. If I remove the first SetRenderTarget/ClearRenderTarget call (so it doesn't clear the temp), the mesh is now drawn into the camera, but not into the temp.

    It seems like what's happening is the SetRenderTarget(color[], depth) call is failing silently and leaving it on the previous render target, but there's no clue of why. The only way I can get any output at all is if I render only to two temporary textures (with depth pointing at one of them).

    The camera does have its own render texture, since MSAA is on (this is visible in the frame debugger).

    I've tried both BuiltinRenderTextureType.CameraTarget and Depth to the depth argument (for some reason there's no SetRenderTarget that takes an array of color targets and leaves depth alone), which didn't help.

    I can get correct results if I render manually with Camera.SetTargetBuffers and Camera.Render, but not with command buffers. Any clues?
     
  2. NoiseFloorDev

    NoiseFloorDev

    Joined:
    May 13, 2017
    Posts:
    104
    Yow. If I set my render target's msaaSamples to match the camera, it works. If it doesn't match, it just silently breaks.
     
  3. Lyje

    Lyje

    Joined:
    Mar 24, 2013
    Posts:
    139
    Also just ran into this (specifically, MSAA not matching camera = no drawing). Any progress?
     
  4. NoiseFloorDev

    NoiseFloorDev

    Joined:
    May 13, 2017
    Posts:
    104
    I think I just made sure msaaSamples does match the camera.
     
unityunity