GPU Instancing

时间:2019-11-06
本文章向大家介绍GPU Instancing,主要包括GPU Instancing使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

作用:

  • 批渲染Mesh相同的那些物体,以降低DrawCall数
  • 这些物体可以有不同的参数,比如颜色与缩放

GPU Instancing与静态批处理,动态批处理的区别

  • 使用静态,动态批处理物体的材质的所有参数是相同的,因为使用Renderer.sharedMaterial修改参数,则所有物体都会受影响。而使用Renderer.material会生成新实例,没法进行批处理

  • 使用GPU Instancing的同一类物体的材质对象相同,但可以在代码中通过接口设置不同的参数,但仍会被批渲染。

使用条件:

  • 兼容的平台及API

  • 相同的Mesh与Material

  • 不支持SkinnedMeshRenderer

  • Shader支持GPU Instancing

     
     

GPU Instancing支持的平台:

  • DirectX 11 and DirectX 12 on Windows
  • OpenGL Core 4.1+/ES3.0+ on Windows, macOS,
  • Linux, iOS and Android
  • Metal on macOS and iOS
  • Vulkan on Windows and Android
  • PlayStation 4 and Xbox One
  • WebGL (requires WebGL 2.0 API)

Shader Target Levels

注意:

  • 使用multiple per-instance属性时,不用在MaterialPropertyBlocks中填充所有属性

  • 如果一个instance缺少了属性,则会使用材质的默认值,没有默认值则使用0

  • 不要将非instanced的属性写入MaterialPropertyBlocks中,否则会造成instancing关闭,会创建出不同的材质

  • 静态批渲染优先级比GPU Instancing高

  • 有些情况会导致instancing失败

    • 材质改变
    • 深度排序

    如果想强制使用instancing绘制这些物体,就得使用Graphics.DrawMeshInstanced

Graphics.DrawMeshInstanced

  • 提交的批处理的实例数量不能超过最大值500
  • 当使用OpenGL,Metal的graphics tools时,最大值500还要除以4
  • 建议使用预先分配的instancing数组,具体参见Automatic Memory Management
  • API

Graphics.DrawMeshInstancedIndirect

  • 用来读取instancing draw calls的参数
  • API

param instancing_options

  • maccount: batchSize

    • 尽量接近实际的instance数量,以减少Shader编译时间
    • 使用opengl或metal最大数量将除以4
  • force_same_maxcount_for_gl

    • 强制opengl或metal中最大instance数量不除以4
  • assumeuniformscaling
    指定所有的缩放都为统一缩放

  • lodfade

  • procedual:FunctionName

注意

  • Surface Shader默认开启了instancing,如果要关闭则定义#param noinstancing

  • Unity默认剔除未使用的的Instancing,如要全部使用则需要在Graphics Setting中设置选项Instancing Variants

  • 使用Graphics.DrawMeshInstanced时,需要勾选材质中的选项Enable Instancing。使用Graphics.DrawMeshInstancedIndirect则不需要勾选。此时PROCEDURAL_INSTANCING_ON不被stripping影响

  • Instanced draw calls在Frame Debugger中表现为Draw Mesh(instanced)

  • 虽然不需要总是定义逐instance的属性,但是还是要设置instance ID。因为世界矩阵需要这个参数才能正常工作。Surface会自动添加,但是自定义的顶点片元Shader需要手动添加UNITY_SETUP_INSTANCE_ID

  • 在前向渲染中,不支持多光照,只有base pass才会生效,参考Forward RenderingPass Tags

  • 使用了lightmaps,不同的光或Reflection Probes时instancing不会生效

  • 使用了多个pass的shader中,只有第一个pass才能使用instancing,因为对于一个物体,之后所有的pass会一同渲染,造成材质变化。

  • 所有宏命令都在UnityInstancing.cginc

https://docs.unity3d.com/560/Documentation/Manual/GPUInstancing.html



作者:晓龙酱
链接:https://www.jianshu.com/p/1492611bb432
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

原文地址:https://www.cnblogs.com/yptianma/p/11806251.html