Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved SG and performance questions

Discussion in 'Shader Graph' started by lifeisabeach, Aug 12, 2021.

  1. lifeisabeach

    lifeisabeach

    Joined:
    Apr 26, 2020
    Posts:
    47
    I have an SG shader with many properties that I'm setting by code.
    I'm trying to optimize it, and am wondering about some performance considerations.

    If anyone could give insight into them, I'd appreciate it.
    1. Does exposing properties, or not, impact shader performance?
    2. I have some 'boolean' properties that I toggle via script (via SetFloat(), and using 'MaterialPropertyBlock' to set them on the material at once); there are dozens of them; does changing them from 'boolean' to 'float' change anything performance-wise?
    3. Most of those dozens of 'booleans' are in a subgraph; this subgraph is set to 'Float' precision (as is the main graph); there is no calculation done on the subgraph, only toggles on/off; does changing this subgraph to 'Half' precision help with performance?
    4. I could break those 'toggled sections' into separate shader and separate materials and add those multiple materials to my GameObject as needed, instead of having a single shader/single material. But all of them would need to have transparency. My assumption is that a single material with transparency is better for performance than multiple materials with transparency, especially since I'm targeting mobile? Am I correct in my assumption?
    Any other thoughts on this are welcome too. Thanks!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,236
    No.

    Yes and no. If you’re using these it’s inputs into a Branch node for any version of Shader Graph since 7.3, using a bool might be slightly faster. Older versions of Shader Graph using a float is slightly faster. At least for rendering the shaders themselves. But the difference is negligible. There’s also probably some minor performance difference in the render thread on the CPU when using Boolean values, but I’d be surprised if you could actually measure it.

    The precision setting is irrelevant for Boolean values, but any values being passed through the sub graph may be affected. It can improve performance, but only if the values that interact with that sub graph get used with other values of the same precision.

    For mobile it might be better to use keywords instead of Boolean values and Branch nodes. It’s almost always better to use a single more expensive shader than multiple materials to produce the same result. There are some situations where it can be cheaper to use multiple passes, but that’s mainly for doing processing on images, like blurs or other cases where you’re sampling a texture many times in a single shader.
     
  3. lifeisabeach

    lifeisabeach

    Joined:
    Apr 26, 2020
    Posts:
    47
    Thank you for the great response @bgolus ! That explained a lot, it all makes sense, really helpful. I will look into keywords, thanks for pointing that out; I haven't used them so I wasn't even considering them but it looks like it is useful exactly to my use case of 'Creating shaders with features that you can turn on or off for each Material instance'. Cool, will definitely try it out, thanks again.