Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

CommandBuffer access to targetTexture on CameraEvent.AfterEverything

Discussion in 'General Graphics' started by sbsmith, Jul 29, 2019.

  1. sbsmith

    sbsmith

    Joined:
    Feb 7, 2013
    Posts:
    126
    Unity 2018.3.8

    I have a series of cameras whose output is accumulated into a texture.

    The cameras use the default render target and I extract a portion of their render output into the desired texture using a command buffer that runs on CameraEvent.AfterEverything.

    I assign the texture to the command buffer like this:

    Code (CSharp):
    1. m_segmentAccumulate.SetGlobalTexture( Uniforms._MainTex, BuiltinRenderTextureType.CurrentActive );
    This worked great for a while (on desktops at least) but then I decided that I wanted to be able to reduce their render cost by having them render at a smaller resolution.

    To achieve this, I tried using RenderTexture.GetTemporary to assign a targetTexture to the camera.

    Now the shader running on my accumulation command buffer is having its _MainTex set to UnityDefault2D instead of the expected TempBuffer.

    I have tried changing the RenderTargetIdentifier to BuiltinRenderTextureType.CameraTarget, BuiltinRenderTextureType.RenderTexture, and BuiltinRenderTextureType.BindableTexture with the same results.

    If I just assign the RenderTexture to the buffer directly, it shows up as a black TempBuffer.

    Code (CSharp):
    1. m_segmentAccumulate.SetGlobalTexture( Uniforms._MainTex, m_segment.CameraTarget );
    (where CameraTarget is the camera.targetTexture).

    To make things stranger, this problem only happens to the last accumulated camera. So if I have three accumulated cameras, BuiltinRenderTextureType.CurrentActive will work for the first two of three. Note that they are all the exact same code.

    It is also worth noting that if I run my game on mobile without using the renderTextures, so the way that worked perfectly on desktops, my accumulation shader will have its input texture set to the default instead of the desired texture.
     
  2. sbsmith

    sbsmith

    Joined:
    Feb 7, 2013
    Posts:
    126
    I did some more tests and noticed that if I use a permanent RenderTexture, instead of a temporary one, that I can read from it, but then the frame debugger cleared the accumulation texture right after.

    That got me thinking: The commands that I think are supposed to happen at the end of my camera render are actually happening at the beginning of the next camera render. However, when I checked the queue for my shader it was set to Overlay, so I'm not sure why that would be happening.
     
  3. sbsmith

    sbsmith

    Joined:
    Feb 7, 2013
    Posts:
    126
    It looks like there was a bug in my code where camera depth was being set in the pre-cull of another camera. Making sure that all of this happens in LateUpdate seems to have fixed the issues I was seeing. I am now able to access the buffers that I expect to be able to at the time I expect to be able to.