Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Shaders' started by JacobK, Aug 27, 2021.

  1. JacobK

    JacobK

    Joined:
    Jun 18, 2013
    Posts:
    20
    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
    CBUFFER_START(UnityPerMaterial)
    (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!
     
    adamgryu likes this.
  2. iside2000s_unity

    iside2000s_unity

    Joined:
    Oct 13, 2021
    Posts:
    1
    Last edited: Jan 1, 2022
  3. WonkeeKim

    WonkeeKim

    Joined:
    Apr 8, 2013
    Posts:
    19
    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.
     
  4. adamgryu

    adamgryu

    Joined:
    Mar 1, 2014
    Posts:
    186
    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.
     
    Deepscorn likes this.