Shader variants not ready when loading mesh from resource

I wrote a code that load parts of mesh at runtime from Resource using a simple Resource.Load.
I later set all the material properties by script according to a json file and everything works fine except shader keywords.
Shader keywords seem to only be working after 1 frame, if I try to set them immediately nothing happen.
Is it a bug or is there a way to wait for all the variants to be ready?
I tried Shader.WarmupAllShaders and nothing changed, the only solution I found was to defer the keyword setup in the next frame which is pretty ugly.

The code sets the keywords directly, and rendering takes into account the material keywords and the global keyword state, so there should be no delays at all.
Can you expand a bit? What leads you to think there’s a 1-frame delay that is tied to keyword setup?

Because if I call the EnableKeyword method right after loading the mesh it does nothing.
I am using the synchronous Resource.Load not the async one.
I don’t think it’s delayed by 1 frame, I do delay it to the next frame update as a workaround, the shader variant is probably loaded after some internal method.
I am trying to create a repro project but it seem to be working on that one, don’t know what is different, maybe something I am doing wrong.
One thing I forgot to say, is that after instantiating the material I do duplicate the material at runtime to avoid messing with my original materials.
I found something interesting using the debug inspector, the materials that don’t work(before doing the workaround) are lacking a keyword. See the image below. The two keywords are defined as the following code:

        [Toggle(AO_TEX)] _EnableAO_ ("Enable AO?", Float) = 1

Why does EnableKeyword only set EnableAO and not AO_TEX?
If I delay the method call it works properly.

7577650--938899--upload_2021-10-16_18-36-21.png

After some testing it seem if I call the method that enable the keywords and set the material properties two times in a row everything works, no need to wait for 1 frame. Could it be the material instancing is loading in asynchronous matter in the background?

Looks like a bug to me. May I ask you to report it?
Thanks! :slight_smile:

Yes, but I can’t isolate the bug and create a repro project, everything works normally in the repro project.
Is it ok if I don’t attach any project?

I found the issue, I was using EnableAO as the shader keyword while it was AO_TEX(which I need to rename cause it makes no sense… ) .
I don’t know why it is working on the second try, maybe because I am setting both the shader keyword and the float property at the same time so that I can have the editor toggle display the correct value, don’t know why this works on the second try, since my material setup create a new materials instance every time I call it, so it should behave identically.
Does the first call to set the float property of the toggle force the shader variant to be loaded but it’s still not available for it to use?