Out of date keywords in Materials would break SRP batch

Suppose such case:

  1. There is a keyword _ENABLE_SOME_FEATURE in a shader;
  2. Create two materials from this shader and toggle the keyword on one of the materials;
  3. Remove the keyword from shader that would make the an out of date keyword remain in the previous material.
  4. Attemp to render these two materials with SRP batch on;

After that in the frame debugger you would see these two materials were not able to SRP batch with a reason: materials have different keywords.

  1. Open the material by a text editor locate the out of date keyword and remove it manually then render them again;

Now this time you could see them are successfully SRP batched.

Can the SRP batcher auto ignores the invalid keywords? For now I have to create a script myself to check hundreds of materials once a shader was changed to remove the invalid keywords.

2 Likes

The description Unity - Scripting API: Material.EnableKeyword says “Sets a shader keyword that is enabled on this material”. This doesn’t mean you can’t enable a keyword that doesn’t exist or doesn’t match the shader keywords.
Can you please report a bug?

However, in the frame debugger you could see the actual working keywords were same which means unity could determine that somehow I think.

This is “what it ended up using” and not “what was the input”. It’s complicated :slight_smile:
We have keyword system improvements in future plans.

That’s cool.

I found that still breaks the SRPbatch in 2020.3.38f1.
Is there any solutions?
…or maybe a script that i can check all my materials?

1 Like

Since I lost an entire day looking for a related bug, I will post it here:

If you Instantiate a material like so:

newMaterial = Instantiate(oldMaterial)

setting keywords on the newMaterial will not work!

Until you “refresh” the shader with

newMaterial.shader = newMaterial.shader

Bafflingly, if you do not do that, the newMaterial will report that it HAS the keyword, it will just not show up in game.

This is in Unity 2023 (and Unity 6)