Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question on the efficiency/optimisation of the MaterialPropertyBlock class

Discussion in 'Shaders' started by stebongo, Dec 21, 2018.

  1. stebongo

    stebongo

    Joined:
    Dec 18, 2016
    Posts:
    21
    So I recently discovered the [PerRendererData] attribute and the MaterialPropertyBlock class, which the way I saw it looks like a godsend to graphics optimisation. I know in generality, less materials can mean mean more performant, and [PerRendererData] allows a whole lot of different objects to use the same material even if looking very different. Now my question is, is this actually more performant? If I'm using 50 different objects with the same shader, should I give them all one material and change the texture/colour/glossiness etc. through script and [PerRendererData] or does this only apply in certain circumstances or for the ease of the developer, to avoid making hundreds of individual materials?

    Thanks!
     
    Last edited: Dec 21, 2018
    theforgot3n1 likes this.
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    This does nothing. It's a legacy thing related to sprite rendering which today only hides that property in the inspector. Material property blocks can be used with or without that and it'll have zero impact on performance.

    In the general sense, yes. More specifically it's fewer set pass calls. Each material requires a new set pass which for the narrow view of this thread can be thought of as "Hey GPU, you're rendering with these material property values now". Using a material property block does not avoid this as the material properties are still changing, so there's little to no impact on the GPU performance in using multiple materials vs property blocks.

    On the CPU side there's some memory benefits to using property blocks as only those properties that are changing have to be stored in memory vs. storing a complete copy of all of the material's settings. This does come at the cost of a little bit of CPU performance, but it's mostly inconsequential and likely less than swapping out a full material most of the time. If you're going to be changing values frequently, then a property block is definitely faster. It's potentially faster to have multiple materials if the modified values aren't ever changed after their initial modification. But there's a big asterisk for both of those.

    If you're changing the property block often, but are modifying the properties from multiple scripts (or from an animation, which also uses property blocks) then you may need to use GetPropertyBlock() to not stomp on existing settings. That can end up being slower than modifying the material itself.

    If you're using instancing then you have to use property blocks to modify the instanced properties, otherwise multiple materials will never be instanced even if all of their settings are identical.


    So; TLDR: Yes, use property blocks. No, don't use [PerRendererData] unless you don't want the value visible in the editor. Avoiding multiple materials is good for memory usage, and avoids potential memory leaks from not destroying them properly.
     
  3. IndaACN

    IndaACN

    Joined:
    Mar 6, 2017
    Posts:
    3
    Hey @bgolus .

    There seems to be an issue with the latest versions of Unity where there are a ridiculous amount of shader variants when building for Android. In the Graphics tab it says there are a couple hundred variants, but at build time its like 46k+. I haven't dug too deep into this issue yet, but with your knowledge I hope to get straight to the point if you are able to help. We are getting super long build times and was wondering if using Material Property Blocks would help with this? Do you have any other insights into this issue and what might help ease the wait on build times?

    Thanks.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Material property blocks won’t have any affect either way.