Search Unity

iPhone app needs platform specific shader variants preloaded to avoid hitches on first playthrough

Discussion in 'Shaders' started by ElStarken, Feb 11, 2021.

  1. ElStarken

    ElStarken

    Joined:
    Dec 11, 2020
    Posts:
    4
    We're getting significant hitches on the first playthrough of our iPhone app, and the XCode profiler indicates it's happening with Metal shader compilation.

    I've saved out Shader Variant Collections in the Editor and preload and warm them up, but the editor is on a Mac, whereas the issue is occurring on the target iPhone platform, which I imagine has a very different GPU, etc. So our preloaded and warmed up variants aren't making any difference.

    I've also tried matching all graphics settings regardless of performance, to try and make the phone use the Mac generated shader variants, but still it compiles its own at runtime.

    Is there any way to save out all current compiled shader variants at runtime, on the iPhone? This seems to be the only way I can preserve and then preload the collection the target platform needs.
     
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,456
  3. ElStarken

    ElStarken

    Joined:
    Dec 11, 2020
    Posts:
    4
    Yes, I have the VariantCollection preloaded and call WarmUp(), but the collection was saved out after a playthrough on a Mac. Apparently they aren't the same ones generated and used on an iPhone (which makes sense) because I can see in the profiler that it makes all new ones anyway.

    So I'm hoping to save out the ShaderVariantCollection that gets generated on the phone, but I don't see anything accessible in the Unity interface to do that without an editor.

    (And from what I've read, WarmUpAllShaders() isn't quite going to cut it, but I will give that a try as well. Thanks...)
     
  4. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    In theory, running the editor with the build target set as your target platform should use the same variants as your player will use, barring any features being locked behind UNITY_EDITOR checks.
     
  5. ElStarken

    ElStarken

    Joined:
    Dec 11, 2020
    Posts:
    4
    Even with a completely different GPU? Yeah, I'm still digging into all the shader settings, starting to play around with it.

    I matched up all the Quality and Graphics settings to force them to match regardless of performance. Still wouldn't use the preloaded/prewarmed shader variants.
     
  6. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    The GPU has no bearing on what variants are compiled. The variant collection does not store actual compiled shader binaries, it merely stores a list of variants for each shader much like a material does when it's used in a scene. It's main purpose is to allow you to ensure variants which are only enabled programmatically are actually compiled into your builds.

    When you assign a material to a renderer, and set its properties in the inspector, Unity will detect the variants needed by its shader based on that material. But if you have shader keywords that are only ever enabled via code or shaders which are only assigned via Shader.Find(), those are not going to be automatically picked up.

    Question: are you using asset bundles (or addressables)?
     
  7. ElStarken

    ElStarken

    Joined:
    Dec 11, 2020
    Posts:
    4
    Interesting, thanks for the input.

    No, we aren't using Addressables or Asset Bundles. Possibly in the near future though.
     
  8. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    It sounds like a bug then.