Search Unity

Bug [WebGL] for loop in URP standard shaders kills Safari performance

Discussion in 'Universal Render Pipeline' started by matt_cauldron, Jan 18, 2022.

  1. matt_cauldron

    matt_cauldron

    Joined:
    Dec 17, 2021
    Posts:
    48
    Hi There,

    X-Posting from https://forum.unity.com/threads/pointlight-kills-safari-performance-2020-3-25-urp.1216254/
    and bug https://fogbugz.unity3d.com/default.asp?1391874_s87affehutkvq4jf

    The Simple Lit shader exhibits terrible performance in WebGL, but only on Safari.

    I noticed it only when using pixel (rather than vertex) lighting. Digging into the shader code in

    Lighting.hlsl
    Code (CSharp):
    1. half4 UniversalFragmentBlinnPhong(InputData inputData, half3 diffuse, half4 specularGloss, half smoothness, half3 emission, half alpha)
    2.  
    If I replace the for loop that iterates over the pixel lights
    Code (CSharp):
    1. for (uint lightIndex = 0u; lightIndex < pixelLightCount; ++lightIndex)
    2. {
    3. ...
    4. }
    With anything else (even just repeating the code 4x, for 4 pixel lights) the performance in safari becomes significantly better.

    My guess is however apple have implemented the for-loop for gles 3 is horribly imperformant.

    The question is, what is a performant way of re-implementing the for loop. Would using if statements

    e.g (pseduo code)
    Code (CSharp):
    1. col += numLights < 0 ? PixelLighting(Light0) : 0;
    2. col += numLights < 1 ? PixelLighting(Light1) : 0;
    cause too much branching logic and impact perf?

    Thanks in advance!