GPU Instancing详解

GPU Instancing的定义

  •  一种Draw call的优化方案,使用一个Draw call就能渲染具有多个相同材质的网格对象。而这些网格的每个copy称为一个实例(Instancing)。此技术在一个场景中对于需要绘制多个相同对象来说是一个行之有效办法,例如树木   灌木丛  或者大量 重复性物体。

Unity中的表现:

  • GPU Instancing 在同一个Draw call中渲染完全相同的网格。可以通过添加变量来减少重复的外观,每个实例可以具有不同的属性,例如颜色或缩放。在Frame Debugger中如有Draw calls显示多个实例时会显示 Darw Mesh(Instanced)。

注意问题

  1. 在SRP Batcher下 非要在SRP下使用GPU instance的话那可以使用Graphics.DrawMeshInstanced,用C#代码中绘制出网格体。
  2. SkinedMeshRender不直接支持GPU Instance技术,仅支持MeshRender。如果希望支持SkinedMeshRender可以使用官方的一项方案Animator Instancing。GitHub - Unity-Technologies/Animation-Instancing: This technique is designed to instance Characters(SkinnedMeshRender).This technique is designed to instance Characters(SkinnedMeshRender). - GitHub - Unity-Technologies/Animation-Instancing: This technique is designed to instance Characters(SkinnedMeshRender).https://github.com/Unity-Technologies/Animation-Instancing
  3. GPU Instancing可以受光照影响,也可以支持光照烘培。

如何在自定义Shader中使用GPU Instancing

Shader "Unlit/MyGPUInstance"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            //第一步: sharder 增加变体使用shader可以支持instance  
            #pragma multi_compile_instancing

            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;

                //第二步:instancID 加入顶点着色器输入结构 
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
                //第三步:instancID加入顶点着色器输出结构
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                //第四步:instanceid在顶点的相关设置  
                UNITY_SETUP_INSTANCE_ID(v);
                //第五步:传递 instanceid 顶点到片元
                UNITY_TRANSFER_INSTANCE_ID(v, o);

                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                //第六步:instanceid在片元的相关设置
                UNITY_SETUP_INSTANCE_ID(i);
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}

其他的一些问题:

  1. URP渲染管线中,会发现GPU Instancing没有生效,原因是UPR默认开启SRP Batch,你可以选择关闭它。
  2. 如果已经GPU Instancing的物体 需要修改实例的一些属性,需要使用MaterialPropertyBlock,也就是通过C#代码来控制,这样可以不破坏GPU Instancing
  3. 如上所说,SRP Bacth和GUP Instancing 在URP中只能支持其中一个而SRP Batcher优先级最高,对于写草和树的Shader,比较建议直接支持GPU Instancing,而不支持SRP Batcher, 在移动端上效率相对SRP Batcher更好。
GPU Instancer is an out of the box solution to display extreme numbers of objects on screen with high performance. With a few mouse clicks, you can instance your prefabs, Unity terrain details and trees. GPU Instancer provides user friendly tools to allow everyone to use Indirect GPU Instancing without having to go through the deep learning curve of Compute Shaders and GPU infrastructure. Also, an API with extensive documentation is provided to manage runtime changes. --------------------------------- Features --------------------------------- - Out of the box solution for complex GPU Instancing. - VR compatible. Works with both single pass and multipass rendering modes. - Mobile compatible. Works with both iOS and Android. - Easy to use interface. - Tens of thousands of objects rendered lightning fast in a single draw call. - GPU frustum culling. - GPU occlusion culling (non-VR platforms only). - Automatically configured custom shader support - Complex hierarchies of prefabs instanced with a single click. - Multiple sub-meshes support. - LOD Groups and cross-fading support (with animation or fade transition width). - Automatic 2D Billboard generation system (auto-added as last LOD). - Shadows casting and receiving support for instances (frustum culled instances still can cast shadows). - Unity 5.6 support. - Well documented API for procedural scenes and runtime modifications (examples included). - Example scenes that showcase GPU Instancer capabilities. Prefab Instancing Features: - Ability to automatically instance prefabs at your scene that you distribute with your favorite prefab painting tool. - Automatically Add-Remove prefab instances without any aditional code. - Automatic detection and updating of transform position, rotation and scale changes. - Full or area localized rigidbody and physics support. - Add-Remove-Update prefab instances with or without instantiating GameObjects (examples included). - Instance based material variations through API (similar to Material Property Blocks). - Enabling and disabling instancing at runtime per instance basis. - API to manage instanced prefabs at runtime. - Includes mobile demo scene with custom controllers. Detail Instancing Features: - Dense grass fields and vegetation with very high frame rates. - Included vegetation shader with wind, shadows, AO, billboarding and various other properties. - Support for custom shaders and materials. - Cross quadding support: automatically turns grass textures to crossed quads. - Ability to paint prefabs with custom materials on Unity terrain (with Unity terrain tools). - Ability to use prefabs with LOD Groups on Unity terrain. - Further performance improvements with automatic spatial partitioning. - API to manage instanced terrain detail prototypes at runtime (examples included). - Editor GPU Instancing simulation. Tree Instancing Features [BETA]: - Dense forests with very high frame rates. - Speed Tree support with wind animations. - Included billboard baker and renderers. - Custom vertex color wind animation support for Soft Occlusion Tree shaders. Third Party Integrations: - Gaia integration. - Map Magic integration. Planned Features: - Tree Creator support with wind animations (a limited version is currently available). - Support for animation baking and skinned mesh renderers. Requirements: - DirectX 11 or DirectX 12 and Shader Model 5.0 GPU (Windows, Windows Store) - Metal (macOS, iOS) - OpenGL Core 4.3 (Windows, Linux) - Vulkan (Android, Windows, Linux) - OpenGL ES 3.1 (Android 8.0 Oreo or later) - Modern Consoles (PS4, Xbox One) To provide the fastest possible performance, GPU Instancer utilizes Indirect GPU Instancing using Unity's DrawMeshInstancedIndirect method and Compute Shaders. GPU Instancing results in magnitudes of performance improvement over static batching and mesh combining. Also, other available solutions for GPU Instancing (including Unity's material option and the DrawMeshInstanced method) fail short on limited buffer sizes and therefore result in more draw calls and less performance. By using the indirect method GPU Instancer aims to provide the ultimate solution for this, and increases performance considerably while rendering the same mesh multiple times. For more Information: Getting Started API Documentation
GPU Instancer 是一款开箱即用的工具,可显示屏幕上对象的精确数量,性能表现强劲。只需鼠标点击几下,你就可以实例化你的预设、Unity 地形细节和树木。 GPU Instancer 可提供方便操作的工具,让你省去费力学习计算着色器和 GPU 基础架构的麻烦,轻松使用间接 GPU 实例化。同时,我们还提供带有详细文档的 API,用来管理运行时期更改。 --------------------------------- 功能特色 --------------------------------- - 适用于复杂 GPU 实例化的开箱即用型解决方案。 - 兼容 VR。适用于单通道和多通道渲染模式。 - 兼容移动端。适用于 iOS 和 Android。 - 易于使用的界面。 - 数以万计的对象仅需绘制调用一次,即可快速渲染。 - GPU 视锥体剔除。 - GPU 遮挡剔除(还支持具有单通道和多通道渲染模式的 VR 平台)。 - 支持自动配置的自定义着色器。 - 支持标准、通用和高清渲染管线。 - 一键即可将层次复杂的预制件进行实例化。 - 支持多个子网格。 - 支持 LOD 组和交叉渐变。(交叉渐变仅在标准渲染管线中支持) - 自动 2D 广告牌生成系统(仅限标准 RP)。 - 阴影投射和获得实例的支持(视锥剔除实例仍然可以投射阴影)。 - 能够按原型使用自定义阴影距离,以及选择用于渲染阴影的 LOD。 - 支持浮动原点处理。 - 支持多个视角。 - 记录良好的 API,可用于过程场景和运行时修改(包括示例)。 - 能够在运行时移除边界或碰撞体内的实例。 - 能够使用自定义计算着色器进行扩展。 - 展示 GPU 实例化器功能的示例场景。 预制件实例化功能: - 能够用你喜欢的预制件涂装工具在分配的场景中自动实例化预制件。 - 自动添加-移除预制件实例,无需任何附加代码。 - 自动检测和更新变换位置、旋转和缩放变化。 - 支持全部或区域定位的刚体和物理。 - 支持嵌套式预制件(Unity 2018.3 及更高版本)。 - 带有或不带实例化游戏对象的添加-移除-更新预制件实例(包括示例)。 - 经由 API 基于实例的材质变化(由于缓冲区限制,Android 不支持) - 在运行时基于实例启用和禁用实例化。 - 在运行时管理实例化预制件的 API。 - 包括带有自定义控制器的移动演示场景。 详细的实例化功能: - 高帧频的茂密草地和植被。 - 包括具有风、阴影、AO、广告牌和各种其他属性的植被着色器。 - 支持自定义着色器和材质。 - 支持交叉四边形:自动将草纹理转变成交叉四边形。 - 能够使用 Unity 地形上的自定义材质对预制件进行涂刷(使用 Unity 地形工具)。 - 能够在 Unity 地形上将预制件与 LOD 组一起使用。 - 通过自动空间分区进一步提高性能。 - 在运行时管理实例化地形详细信息原型的 API(包括示例)。 - 编辑器 GPU 实例化模拟。 树木实例化功能: - 高帧率的茂密森林。 - SpeedTree 支持风动画。 - SpeedTree 8 支持风动画。 - 树木创建器支持风动画。 - 支持软遮挡树。 - 包括广告牌烘焙师和渲染器。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值