Search Unity

Graphics.DrawProceduralIndirect has no lighting? (built-in deferred) [solved]

Discussion in 'General Graphics' started by GuitarBro, Feb 4, 2021.

  1. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Simple question, hopefully simple answer: is Graphics.DrawProceduralIndirect capable of rendering into the deferred gbuffer so it can receive lighting? If so, how do you go about doing that? If not, how can I get a shaded mesh generated on the GPU without transferring data back to the CPU?

    I already have a working script/shaders that generates a mesh on the GPU which I want to render without CPU readback as it contains many vertices. Only problem is that it lacks lighting at the moment which makes it less than ideal. If possible it would be great to access SV_Target0-3 so I can do some custom logic with the unused gbuffer channels. :)
     
  2. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Even inspecting the frame debugger, it appears to be rendering in RenderForwardOpaque.Render despite the "Queue" = "Geometry" "RenderType" = "Opaque" in the shader.

    Does it have something to do with the fact that I'm calling it in LateUpdate()?
     
  3. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    (Bump)

    Nobody has any idea about this?
     
  4. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
  5. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
  6. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
  7. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
  8. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Is this a surface shader or a plain shader? A surface shader should automatically be deferred by default. Plain shaders need to have that set up manually in their render queue. Check the docs and the source for the standard shader.
     
  9. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Thanks, @KokkuHub . It is a fragment shader. Using the same fragment shader (ignoring the buffer containing the vertices) on a normal mesh, it is lit correctly though. I can post my shader source later today.
     
  10. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Here's the relevant shader code I'm using:
    Code (CSharp):
    1. struct VertexOutput {
    2.     float3 positionWS : TEXCOORD0;
    3.     float3 normalWS : TEXCOORD1;
    4.     float3 uvTint : TEXCOORD2;
    5.     float4 positionCS : SV_POSITION;
    6. };
    7.  
    8. void frag(VertexOutput input,
    9.                 out half4 outGBuffer0 : SV_Target0,
    10.                 out half4 outGBuffer1 : SV_Target1,
    11.                 out half4 outGBuffer2 : SV_Target2,
    12.                 out half4 outEmission : SV_Target3)
    13.             {
    14.                 fixed4 c = tex2D(_Albedo, input.uvTint) * _Tint;
    15.  
    16.                 half3 wn = input.normalWS;
    17.  
    18.                 half3 c_diff, c_spec;
    19.                 fixed refl10;
    20.                 c_diff = DiffuseAndSpecularFromMetallic(
    21.                     c, 0,
    22.                     c_spec, refl10
    23.                 );
    24.  
    25.  
    26.                 UnityStandardData data;
    27.                 data.diffuseColor = c_diff;
    28.                 data.occlusion = lerp(input.uvTint.y, 1, 0.5);
    29.                 data.specularColor = c_spec;
    30.                 data.smoothness = (1 - _Roughness);
    31.                 data.normalWorld = wn;
    32.                 UnityStandardDataToGbuffer(data, outGBuffer0, outGBuffer1, outGBuffer2);
    33.                 outGBuffer2.a = 0.25;
    34.  
    35.                 // Calculate ambient lighting and output to the emission buffer.
    36.                 half3 sh = 1;// ShadeSHPerPixel(wn, 0, /*IN.ambient,*/ input.position);
    37.                 outEmission = half4(sh * c_diff, 1);
    38.             }
     
  11. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    GuitarBro likes this.
  12. GuitarBro

    GuitarBro

    Joined:
    Oct 9, 2014
    Posts:
    180
    Ah ha, that did the trick! Can't believe I missed that somehow. Much appreciated.
     
    Neto_Kokku likes this.