Search Unity

Question How to deal with array properties with SRP Batcher

Discussion in 'Universal Render Pipeline' started by pw_prg_yinchao, Aug 27, 2021.

  1. pw_prg_yinchao

    pw_prg_yinchao

    Joined:
    Feb 14, 2020
    Posts:
    18
    I am writing a custom shader under URP with SRP Batcher enabled.

    I want to declare a vector array property, say "_Colors":
    Code (CSharp):
    1. half4 _Colors[8];
    and in order to make this shader SRP Batcher compatible:
    Code (CSharp):
    1. CBUFFER_START(UnityPerMaterial)
    2.     half4 _Colors[8];
    3. CBUFFER_END
    But now I have no idea to declare it in
    Properties
    block, because there is no
    Array
    type similar to
    Vector
    or
    Color
    . As a result, this shader will not be SRP Batcher compatible :(

    Besides, I have tried some ways and found something interesting:
    1. If a uniform variable in
    UnityPerMaterial
    CBuffer has a
    Array
    type, and a property declared in
    Properties
    block with same name, whatever its type is, SRP Batcher will feel satisfied. (tested by attachmented TestSRPBatcherCompatibilityWithArrayVariable.shader)
    2. If a uniform variable in
    UnityPerMaterial
    CBuffer has a
    Matrix
    type, and no property declared in
    Properties
    block with same name, SRP Batcher will feel satisfied. (tested by attachmented TestSRPBatcherCompatibilityWithArrayVariable.shader)
    All above are so confused to me.
    (All my attachmented shaders are tested under Unity 2019.4.29f1 with URP 7.4.1, on a Windows PC.)
     

    Attached Files:

    kgindemitsoulside likes this.
  2. kgindemitsoulside

    kgindemitsoulside

    Joined:
    Nov 5, 2018
    Posts:
    3
    Hey @pw_prg_yinchao, thanks for your workarounds to make the SRP Batcher happy!
    I tried the first workaround with declaring the colors array as a float property. And the drawcalls was afterwards batched.

    But, there was slightly different colors rendered. To fix the color rendering issue, I changed the line 3 from the first attachment to this one:

    [HideInInspector] _Colors("Colors", Float) = 0 // `Float` here is OK, but will not serialized as expected.

    The float needs just one 0 as initial value.
    Tested the changes on Android and iOS build with Unity 2021.1.2f1 version.
     
  3. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    501
    Hmm, from what I can tell having an array that's set as a scalar in the material properties doesn't actually work (Unity 2022.3.12f1) because the material is initialized with an array size of 1, and subsequent array size changes aren't allowed by Unity? I saw this bug report but there's no further information there, other than something is supposed to be fixed. How are people dealing with this exactly? I'm surprised there's so little info on this.
     
  4. forestrf

    forestrf

    Joined:
    Aug 28, 2010
    Posts:
    231
    This is quite painful as seems the only option is to not use the srp batcher in this cases, for no good reason.