How to deal with array properties with SRP Batcher

I am writing a custom shader under URP with SRP Batcher enabled.

I want to declare a vector array property, say “_Colors”:

half4 _Colors[8];

and in order to make this shader SRP Batcher compatible:

CBUFFER_START(UnityPerMaterial)
    half4 _Colors[8];
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 :frowning:

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.)

7452779–914441–TestSRPBatcherCompatibilityWithArrayVariable.shader (1.63 KB)
7452779–914444–TestSRPBatcherCompatibilityWithMatrixVariable.shader (1.51 KB)

1 Like

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.

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.

This is quite painful as seems the only option is to not use the srp batcher in this cases, for no good reason.