Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. We are looking for your feedback about Templates! Tell us about your experiences by taking our survey.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Does anybody know if [PerRendererData] in custom shaders adds to draw counts?

Discussion in 'General Graphics' started by Reverend-Speed, Jan 11, 2021.

  1. Reverend-Speed

    Reverend-Speed

    Joined:
    Mar 28, 2011
    Posts:
    214
    Apologies, I'm off to bed and I had this nagging question - does using [PerRendererData] in my shaders cause materials with different [PerRendererData] to count as separate materials, and thus require further draw counts?

    Any info on this would be really helpful. Meanwhile, I'm bushed, going to sleep. Thanks in advance.

    --Rev
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,775
    [PerRenderData]
    hides the property from the inspector. It’s the same as
    [HideInInspector]
    . Apart from that, it does nothing. It may have at one point in the past, but it doesn’t anymore.

    Whether or not changing that a material property means an additional set pass call / draw count is kind of a different question. Two objects with the exact same material may require separate draws anyway, so having different material settings doesn’t “add” anything extra. But it will prevent batching. However if the shader is an instanced shader, both objects are using the same mesh, you’re modifying the properties with a MaterialPropertyBlock, and the property being modified is an instanced property... then it might not add any extra draws. Or it might because something else is breaking batching.
     
    AcidArrow likes this.
  3. Reverend-Speed

    Reverend-Speed

    Joined:
    Mar 28, 2011
    Posts:
    214
    Thank you, @bgolus , always appreciate your knowledge on these issues. Facilitating batching is my goal, as you surmise, and, yeah, 'MaterialPropertyBlock' is what I should have referenced instead of [PerRenderData] - it's been a while since I've looked at this. Granted something outside changing the values of the instanced property might prevent batching, but I just want to reduce additional draws while changing the appearance of a shader on different surfaces (for example, making wallpaper look different by changing colour values, alpha and clipping for a repeating pattern, etc), on walls in different rooms.

    Sounds a little unpredictable, however. Will just have to build some tests.

    Thank you!
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,775
    For batching, you cannot change material properties. Full stop. If you want per-mesh values, you need to bake that into the vertex data: vertex colors or in additional UV sets.
     
  5. Reverend-Speed

    Reverend-Speed

    Joined:
    Mar 28, 2011
    Posts:
    214
    Ah, that's good to know, even if it's 'bad' news.

    Just to clarify on per-mesh values, are you suggesting that the same shader applied to different meshes with, say, different vertex colour across the mesh could use the vertex colour to tint the shader - yet retain batching across the two different meshes (otherwise identical) using the same shader? I'm guessing no, but I want to be clear on this.

    Someone I know was investigating instanced definitions for something similar... eg:
    Code (CSharp):
    1. UNITY_INSTANCING_CBUFFER_START(Props)
    2.             UNITY_DEFINE_INSTANCED_PROP(float, _Index)
    3.             UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color)
    4.  
    5. UNITY_INSTANCING_CBUFFER_END
    ...but I think I need to take that up with them!

    Thanks again, @bgolus !
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,775
    You got it. That’s how particles and sprites work.
     
  7. Reverend-Speed

    Reverend-Speed

    Joined:
    Mar 28, 2011
    Posts:
    214
    Excellent! Thank you! It's interesting to think that particles and sprites are still using vertex colour while using this was removed from the Standard shader... Some vertex alpha as standard would be quite handy at times (flame exhausts, etc).

    Actually - while I have your attention, @bgolus , one more question on batching, if you'd be so kind...!

    If I had an outdoors scene with multiple objects all using a small range of materials (eg. one for wooden objects, one for brick walls, one for concrete floors etc.), I'd expect the objects using the same materials to batch as a single drawcall (other things breaking batching aside).

    However, if I was to give them all a second material slot using the same material (a triplanar shader with, say, animated normal map / specular puddles on surfaces facing upwards and animated normal map / specular rivulets of water running downwards on all vertical surfaces (leaving alpha transitions and angles aside for the moment)), my guess would be the second material would batch across the meshes as one drawcall (other things breaking batching aside), with the individual primary materials across the meshes batching as their own drawcall per mesh (other things breaking batching aside).

    Does that scan?

    (I have a hankering for a really filthy rain scene and I've been looking at how a multitude of games do this (Wolfenstein New Order, Remember Me, Heavy Rain, Metal Gear Solid 2, Gears of War 1, Left 4 Dead 2, Arkham Knight and others). There's a long way to go, but having a nice rain material (with material property blocks running off a central manager) that I can just slap on top of geometry would be a great start.)
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,775
    It should work out that way, yeah. I’d say try and see though. More materials on an mesh than it was imported using is ... somewhat unsupported behavior. I mean it works, but there are bugs they’ve never bothered to fix with that setup. Mainly a mesh with more than one material index on import will only draw the first material index’s sub mesh for any materials over that. So this technique won’t work for meshes that already have multiple materials. But for this case, if you’ve already merged it all, it “should” work.

    However I think I ran into some issues with this on statically batched meshes, but I can’t remember exactly what.
     
  9. Reverend-Speed

    Reverend-Speed

    Joined:
    Mar 28, 2011
    Posts:
    214
    That's great to hear. I usually only run one material per mesh, sooo... hopefully it'll work. Thanks, @bgolus .
     
unityunity