- Home /
What's the difference between using "[PerRendererData]" and "Enable GPU Instancing" in Unity shaders/materials?
I know that both of these options help optimize GPU performance when rendering many meshes with a few varying material properties -- for example, hundreds of objects that use the same material but should each be a different color.
However, each option seems to give different performance gains, and I haven't been able to find a clear explanation of how they differ or when to use each one.
In other words, when should I do this in my shader:
Properties {
[PerRendererData]_Color ("Color", Color) = (1,1,1,1)
}
vs. this:
UNITY_INSTANCING_CBUFFER_START(Props)
// put more per-instance properties here
UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color)
UNITY_INSTANCING_CBUFFER_END
...and why?
Running some basic tests in editor (rendering a few thousand cubes with the same material, then varying the color of each one via script), I found that [PerRendererData] reduced render thread time by about 30% (from ~9ms to ~6ms) while enabling GPU Instancing reduced render thread time by closer to 90% (from ~9ms to ~1ms).
So, my best guess is that:
- [PerRendererData] should be used when applying the same material with varying properties to many different objects that ALSO have different meshes (i.e. aren't all the exact same shape). It uses Material Property Blocks to take advantage of material instancing, but doesn't affect dynamic batching in any way.
- GPU Instanced Properties should be used when applying the same material with varying properties to many different objects that have the SAME mesh. It takes advantage of both material instancing and dynamic mesh batching.
Is this right, or are there other important distinctions / technical differences between these two? (Also, is there any documentation source that explains this?)
Since you already set up that test... have you tried setting the material properties via $$anonymous$$aterialPropertyBlock but without [PerRendererData] or did you just instantiate the material for your "unoptimized" test? I'm asking because adding [PerRendererData] does nothing to my shaders' disassemblies and I have also seen comments from Unity staff on forums that claimed that [PerRendererData] is functionally the same as [HideInInspector].
Answer by DavidSWu · Oct 31, 2018 at 08:40 AM
That sounds accurate to me.
My guess is that PerRendererData does not have anything to do with instancing, it just allows the rendering system to more rapidly chain together draw calls (semi batching) for related materials
I.e.
(Setup material)
DrawIndexed()
VSSetConstants()
DrawIndexed()
Your answer
Follow this Question
Related Questions
Every shader and quality is pink. URP 2 Answers
MaterialPropertyBlock ignored when rendering with replaced shaders? 0 Answers
Render problems (black, purple and blue effects) in old GPU and Unity3D 5.6 project on Unity 2018 0 Answers
Custom Shader with Canvas Renderer limit draw calls. 0 Answers
How to render material under UI? 1 Answer