Adding GPU Instancing to _BaseColor property in URP Lit Shader

I’m trying to allow the “_BaseColor” property of the standard URP Lit shader to accept an array of colors in a MaterialPropertyBlock. I’ve made sure the Material has “Enable GPU Instancing” on. Here is what I have:

MaterialPropertyBlock baseMPB = new MaterialPropertyBlock();
Vector4[] _colorsBase = new Vector4[myArray.Length];

for (int index = 0; index < myArray.Length; index++)
{
    // Some function that sets _colorsBase vectors
}

baseMPB.SetVectorArray("_BaseColor", _colorsBase);
Graphics.DrawMeshInstanced(myMesh, 0, myMaterial, myTransformsArray, myArray.Length, baseMPB);

However, this only uses the very first color in _colorsBase and does not change colors on subsequent meshes.

I’ve done quite a lot of looking and it seems according to this thread, Color is not setup to be instanced by default.

I don’t have too much experience with ShaderLab nor HLSL so it’s been difficult even trying to figure out where to get started with adding it in. It looks like I have to Copy+Paste both “Lit.Shader” and “LitInput.hlsl” and make changes on those. However, reading through these threads still have me very confused on how to start:

On top of that, taking a look inside LitInput.hlsl seems to include this snippet:

// NOTE: Do not ifdef the properties for dots instancing, but ifdef the actual usage.
// Otherwise you might break CPU-side as property constant-buffer offsets change per variant.
// NOTE: Dots instancing is orthogonal to the constant buffer above.
#ifdef UNITY_DOTS_INSTANCING_ENABLED
UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata)
    UNITY_DOTS_INSTANCED_PROP(float4, _BaseColor)
    ...
UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata)

#define _BaseColor              UNITY_ACCESS_DOTS_INSTANCED_PROP_WITH_DEFAULT(float4 , _BaseColor)
...
#endif

Which seems to indicate that there’s already been work done on instancing _BaseColor? Where should I even start for this? I don’t even know when the conversion between Color in Lit.shader and float4 in LitInput.hlsl occurs.

Alright I managed to figure it out with this tutorial: Draw Calls

For any newbies like me who didn’t really understand it, here is what I did:

I duplicated the lit.shader file. Then I tried to find where _BaseColor gets declared. As it turns out this code essentially means that the LitInput.hlsl and DepthOnlyPass.hlsl files are just copy+pasted into the “#include” part when compiled:

...
//--------------------------------------
// GPU Instancing
#pragma multi_compile_instancing
#pragma multi_compile _ DOTS_INSTANCING_ON

#include ".../LitInput.hlsl"
#include ".../DepthOnlyPass.hlsl"
ENDHLSL

As it turns out, _BaseColor gets defined in LitInput.hlsl. In particular this code:

CBUFFER_START(UnityPerMaterial)
float4 _BaseMap_ST;
float4 _DetailAlbedoMap_ST;
half4 _BaseColor;
half4 _SpecColor;
half4 _EmissionColor;
half _Cutoff;
half _Smoothness;
half _Metallic;
half _BumpScale;
half _Parallax;
half _OcclusionStrength;
half _ClearCoatMask;
half _ClearCoatSmoothness;
half _DetailAlbedoMapScale;
half _DetailNormalMapScale;
half _Surface;
CBUFFER_END

So what I did was I duplicated LitInput.hlsl and changed the path of the previous #include to point to my duplicate. I then deleted that “half4 _BaseColor” line, and added this snippet right below it:

...
CBUFFER_END

UNITY_INSTANCING_BUFFER_START(UnityPerMaterialInstanced)
UNITY_DEFINE_INSTANCED_PROP(half4, _BaseColor)
UNITY_INSTANCING_BUFFER_END(UnityPerMaterialInstanced)

Next, I looked for any _BaseColor usage in the .hlsl files used by the Lit.shader file, and replaced it with this:

UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterialInstanced, _BaseColor)

For Lit.shader, _BaseColor gets referenced in LitInput.hlsl, ShadowCasterPass.hlsl, and DepthOnlyPass.hlsl. So I duplicated those files, changed the #include path to point to my duplicates, and replaced the _BaseColor on all those files.

Applying this new shader to my original material now instances color properly. Of course I’m sure this would cause more issues if my use case wasn’t this simple (Literally just changing the color of the base lit shader), but this is how you do it since there doesn’t seem to be a guide for it without learning a bit of shader syntax.

1 Like