URP Custom Shader: How to UsePass DepthOnly/Meta/ShadowCaster without breaking SRP batching?

Hiya, when writing custom shaders for URP, we’re including the meta pass from the URP lit shader using
UsePass "Universal Render Pipeline/Lit/Meta"

This works fine, but to work with the SRP batcher, but our shader has a custom

(variables)
CBUFFER_END```
block across all passes

This causes the meta pass (and other passes from UsePass to have a seperate UnityPerMaterial cbuffer), and SRP Batcher then reports "**UnityPerMaterial CBuffer inconsistent size include a SubShader (Meta)**"

I've tried moving the cbuffer to a HLSLINCLUDE block but that didn't fix the issue - is there a correct way to handle this without having a declare a custom meta (etc) pass for each shader?

Thanks!
1 Like

in the Litinput.hlsl file
https://github.com/Unity-Technologies/Graphics/blob/master/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl
there is a NOTE brfore the CBUFFER block: // NOTE: Do not ifdef the properties here as SRP batcher can not handle different layouts.

SRP batcher requires the same CBUFFER size, so I think you can’t use UsePass with SRP batcher unless both has same properties. I think you need to hard copy and paste the lit shader codes and replace the LitInput.hlsl with yours.

This is really unfortunate. I’ve been using a custom lighting model with all custom shaders, and if I’m not able to use UsePass this will make shader maintenance even more difficult.

The error “UnityPerMaterial CBuffer inconsistent size include a SubShader (Meta)” is a bit vague - I assumed it meant we were not allowed to have different CBuffer sizes for different keyword variants of the same pass. However, it seems that all passes (Shadows, DepthOnly, Meta etc) need to have the same CBUFFER? This seems to make it harder to reuse shader code.

On that note, are there any good resources for how to write custom shaders with many variants in a maintainable way? The amount of boilerplate that needs to be done to support various engine features has been difficult.

1 Like