Search Unity

A question regarding CommandBuffer to extend the forward renderer pipeline

Discussion in 'General Graphics' started by geroppo, Sep 21, 2017.

  1. geroppo

    geroppo

    Joined:
    Dec 24, 2012
    Posts:
    140
    Hi, I've been trying to add a second render target during opaque rendering in forward mode, but it seems like the target gets cleared when rendering opaque objects. I'm doing the following:
    Code (CSharp):
    1. CommandBuffer command = new CommandBuffer();
    2.         RenderTargetIdentifier rt_id = new RenderTargetIdentifier(my_render_texture);      
    3.         RenderTargetIdentifier active_id = new RenderTargetIdentifier(BuiltinRenderTextureType.CameraTarget);
    4.         command.SetRenderTarget(new RenderTargetIdentifier[]{active_id, rt_id}, active_id);
    5.         Camera.main.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, command);
    6.  
    What I'm trying to accomplish is to generate a few extra textures (like glow map and a few masks) during the geometry pass. I could do an extra pass, but at this point I'm just wondering if it's possible to do it in one using command buffers. When I output the contents of my_render_texture using the OnRenderImage method, the contents come out black, so I assume that the RT is not being binded correctly, or it's being removed when the opaque drawing phase starts.

    This is the shader I'm using to write to multiple RT
    Code (CSharp):
    1. struct frag_out
    2. {
    3.      fixed4 color_buffer : SV_Target0;
    4.      fixed4 mask : SV_Target1;
    5. };
    6.  
    7. frag_out fragment_shader(v2f i)
    8. {
    9.     frag_out fo;
    10.     fo.color_buffer = float4(1,0,0,0);
    11.     fo.mask = float4(0,1,0,0);
    12.     return fo;
    13. }
    14.  
    The color buffer comes out ok...but I'm guessing it's because the render targets got "reset" before starting to render opaque geometry. Also the render texture that I'm using has the same format that the currently active one.