Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Resolved 2022.2, URP, in builds, same shader variants compiling multiple times

Discussion in '2022.2 Beta' started by adamgolden, Oct 19, 2022.

  1. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    Repro: You have a material in your scene and the shader is appearing in builds as expected (i.e. it's not pink or missing). You then have 100 (or however many) different prefabs which may be loaded in dynamically (each an Addressable), and in each prefab a renderer has either that material or another which is using the same shader.

    When playing your game, as each one of these prefabs becomes visible for the first time, the same variant gets recompiled (meaning your game freezes for a moment, unless it's running in Play Mode in the Editor). In the repro above, each variant would be compiled 100 times!

    Here's a screenshot of the new issue, filtered to a single shader, from output of a real project. This shows just how much work the engine is now doing at runtime handling 1 shader and 2 variants. There should only be (and previously would only be) 2 lines of output for this, not 20:

    shader_compiling_of_same_variants_multiple_times.jpg

    Diagnosing: Does not occur in Play Mode - builds only. Running your game in the editor is perfectly smooth. In builds, there's stuttering/freezing when new content first appears. Looking into connected profiler you see shader compilation being the cause, but those shaders are [or previously would be] already compiled by Shader Variant Collection warmup or by having been rendered in the Scene prior to loading of the new content.

    To confirm (or rule out) that this is happening in your project, create a development build, making sure Project Settings->Graphics->Log Shader Compilation is enabled.

    Previously, while material references would still be different (i.e. "MyMaterial" in "PrefabA" != "MyMaterial" in "PrefabB".. if we're talking about prefabs being loaded in.. which might also be solvable by giving each and every material an Addressable id - unconfirmed theory), the engine previously would still only compile each shader variant once, whenever it was first needed for rendering.

    I'm using 2022.2.0b11 (though this has been an issue for me since 0b10), URP 14.0.3 and Addressables 1.20.5. Unfortunately, the code of "14.0.3" has changed since it first came out back in May but the versioning has not (reported here), and recently with upgrade to engine version 0b10, there were suddenly errors thrown from URP related to function signature changes (IIRC), so I re-downloaded "14.0.3" from Package Manager again which resolved them. However, as a result, I don't know whether what caused the issue at hand is the engine code or render pipeline code, but 0b10 (with whatever 14.0.3 code was latest when 0b10 was released) is the first time I encountered this. Also, if it's possible that for example Addressables "1.20.5" has been multiple versions released at different times while still being "1.20.5", in theory that package could contain the changes that caused this.. or potentially any other package not being versioned which has been having multiple updates.

    Anyway, the solution turned out to be making each and every shader an Addressable.. which is a clean approach to managing them anyway I guess and probably what would be officially recommended, so I think the issue could end up resolved with a "Will Not Fix" status.
     
    dtaddis and paulatwarp like this.