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

Question Diffirent CBUFFER for subshader

Discussion in 'Shaders' started by Camarent, Apr 7, 2021.

  1. Camarent

    Camarent

    Joined:
    Feb 19, 2014
    Posts:
    168
    Hi!

    I am working on a project that contains HDRP and URP in one Unity project. So I was super excited to hear that shadergraph can support multiple targets for one shader but since they announced it contains one major bug that prevents you to actually use it.

    If you add HDRP and URP target then switch graphics to use URP, your shaders will be not compatible with SRP Batcher. It happens because HDRP subshaders and URP subshaders have different CBUFFER size.

    Here is little example from shadergraph generation.
    HDRP CBUFFER
    Code (CSharp):
    1. CBUFFER_START(UnityPerMaterial)
    2.         float4 _EmissionColor;
    3.         float _UseShadowThreshold;
    4.         float4 _DoubleSidedConstants;
    5.         float _BlendMode;
    6.         float _EnableBlendModePreserveSpecularLighting;
    7.         float _RayTracing;
    8.         float _RefractionModel;
    9.         float4 _BaseColor;
    10.         float4 _BaseColorMap_TexelSize;
    11.         float4 _MaskMap_TexelSize;
    12.         float _Metallic;
    13.         float _Smoothness;
    14.         float _SmoothnessRemapMin;
    15.         float _SmoothnessRemapMax;
    16.         float4 _NormalMap_TexelSize;
    17.         float _NormalStrength;
    18.         float4 _EmissionMap_TexelSize;
    19.         float _Transparency;
    20.         float _DitherOffset;
    21.         float _DitherSize;
    22.         float _AlphaClippingThreshold;
    23. CBUFFER_END
    URP CBUFFER from same shader
    Code (CSharp):
    1.  CBUFFER_START(UnityPerMaterial)
    2.         float4 _BaseColor;
    3.         float4 _BaseColorMap_TexelSize;
    4.         float4 _MaskMap_TexelSize;
    5.         float _Metallic;
    6.         float _Smoothness;
    7.         float _SmoothnessRemapMin;
    8.         float _SmoothnessRemapMax;
    9.         float4 _NormalMap_TexelSize;
    10.         float _NormalStrength;
    11.         float4 _EmissionMap_TexelSize;
    12.         float4 _EmissionColor;
    13.         float _Transparency;
    14.         float _DitherOffset;
    15.         float _DitherSize;
    16.         float _AlphaClippingThreshold;
    17. CBUFFER_END
    HDRP subshader goes first so Unity check other cbuffer size by HDRP. That is why HDRP works with SRP Batcher and URP is not.

    For now I decided to manually copy HDRP CBUFFER to all URP subshaders.
    Can someone recommend other way to work with different CBUFFER sizes in subshaders? Is it even possible? I think that the main reason why this bug exist for more then half of year because Unity do now want add redundant properties to URP but this is just my guess.

    I actually have another thread with confirmed issue about this problem in shadergraph if someone interested:
    https://forum.unity.com/threads/cro...-urp-breaks-srp-batcher.1041535/#post-6959990
    https://issuetracker.unity3d.com/is...61.1969219334.1617786597-910572688.1606392700