Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question [URP] Write custom StencilRef during GBufferPass?

Discussion in 'Universal Render Pipeline' started by sabojako, Aug 6, 2021.

  1. sabojako

    sabojako

    Joined:
    Jul 7, 2017
    Posts:
    48
    Hi,

    In URP, I am looking for a way to render a custom shader in the GBuffer pass but with a specific Stencil Ref (ID). UniversalMaterialTypes "Lit", "SimpleLit" and "Unlit" write to their respective stencil bits for lighting. But how are we supposed to input custom user flags in the other bits? This should be possible as StencilUsage mentions it:
    https://github.com/Unity-Technologi...ipelines.universal/Runtime/StencilUsage.cs#L7

    The GBufferPass is using context.DrawRenderers() with a "RenderStateBlock stateBlock" argument which completely overrides the RenderState without accounting for the StencilRef value in the Material.
    https://docs.unity3d.com/2021.2/Doc...ng.ScriptableRenderContext.DrawRenderers.html

    SRP Feature request:
    It would be great if the StencilRef from the Stencil block of shader files would be taken into account and combined with the RenderStateBlocks or if context.DrawRenderers() had an argument to allow that. AFAIK this is currently not possible.

    This combinaison definitely happens in the Standard(Legacy) Render Pipeline: When using the Deferred rendering path, "Deferred" shaders write to stencil 192 by default in the RenderDeferred.GBuffer pass. But actually custom "Deferred" shaders can write up to 255 (in the first 6 bits) if you add a Stencil{Ref [_StencilRef]} block in the shader file. By looking at the Frame Debugger, it seems the Standard render loop is able to pickup that material StencilReference and combine it with the GBuffer pass' StencilReference! Also StandardShaderGUI.cs does not manipulate StencilRef.

    In HDRP, StencilRef setup seems to happen in the Material Editors. They don't use RenderStateBlock overrides in their GBuffer rendering pass. Maybe it should be like that for URP too?
     
  2. sabojako

    sabojako

    Joined:
    Jul 7, 2017
    Posts:
    48
  3. CaffeinatedCoolaid

    CaffeinatedCoolaid

    Joined:
    May 10, 2021
    Posts:
    59
    I haven't dug too much into the deferred renderer yet, but a possible work around could be enable deph prepass and apply your stencil writes in the a depth only pass, then use a custom renderer feature to blit the the stencil data needed to a texture, and use that in your shader later.