[URP] Write custom StencilRef during GBufferPass?

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:

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.

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?

Poking related devs :slight_smile: @kaychang @phil_lira

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.