GPUInstance的使用

本文详细介绍了Unity3D中GPUInstance的使用,包括GPUInstance的硬件特性、加速原理,适用场景,以及在Unity中的实现方法,如Shader支持、脚本端操作,并分析了软硬件支持和运行环境。内容涵盖了如何启用GPUInstance,如何处理实例化属性,以及可能出现的意外情况和优化效果。
摘要由CSDN通过智能技术生成

一.关于GPUInstance

1.用于渲染加速的硬件特性.

gpu硬件支持的一种特性,使用少量的渲染调用(DrawCall)渲染同一网格的多个副本.也就是说在渲染时,他只需要提交一个网格副本,一个材质球,然后在把这些模型对象中不同的属性(比如:位置,大小,旋转,颜色等)提取出来放到一个数组中.

2.底层逻辑,为什么能够加速渲染,提高性能?

  1. 针对同网格同材质的模型多个对象进行渲染时,有一下几种渲染方式:
    1. 原始模式,每个材质和模型都调用一次渲染,这样会导致的后果是,每次渲染一个对象CPU就会向GPU提交一次渲染相关的所有资源.很明显这里面有很多重复资源的提交造成了很大的浪费,也让渲染速度大大降低.
    2. 动态合批模式,这个很好,把最近相同的模型进行合并成一个模型,然后一次提交一次渲染,爽快利落,典型的空间换速度,也很划算,最大化的提高渲染效率.但是,当网格数量和网格的顶点数超过一定数量时,动态合批就不能在进行了因为这个时候就会拉低CPU的执行效率,以及内存空间的暴涨.所以动态合批有较多的限制.
    3. 静态合批模式,这个也很好,离线把模型全部给合并到一个网格(合并后的网格顶点数量有限制:65535),这样能够解决动态合批的问题,但是也有问题,会导致一下几个问题:
      1. 重复网格的副本会被复制很多份,导致资源包体和运行时内存都会有很大的增长.
      2. 一些不需要渲染的顶点也被提交给GPU,增大了GPU的顶点处理器的压力,.
      3. 在逻辑处理上,静态合批也有一个问题,就是合并完后的模型对象不能再被位移,旋转,缩放等处理了.
    4. 然后就是GPUInstance,他也是针对相同模型的网格和材质只提交一次,然后他组合这些模型对象的不同之处打成一个数组提交给GPU,这样既能够减少提交的次数,有不会让内存暴涨.CPU端也不会因为合并网格而各种计算,增加消耗.这样他的特点就出来了:
      1. 在CPU端进行模型裁剪,只合并看见的模型对象.这样提交给GPU渲染的实例会更少更有效.在某些场景下,这个相对于静态批处理来说优势很足.
      2. 只组织和提交模型对象之间的不同之处,内存的消耗也能够控制.这个相对于动静两种合批来说都是优势巨大.
      3. 不需要做网格的合并处理,这方面也CPU没有消耗.这个相对于动态合批来说优势巨大.
      4. 在CPU端的实例不同点的合并处理,需要一定的消耗,这个对于静态批处理来说没有优势.
      5. 在GPU端要增加一个索引和缓存的读取处理,这个也算是额外的消耗,这个对于静态批处理和动态批处理都没有优势.
      6. 因为一个批量渲染,只提交一个网格副本,所以对于网格的大小就没有了限制.适应范围更广泛,这个对于动态合批来说优势巨大.
  2. 综上几个渲染方式的对比之后,在针对这种大量的同网格,.同材质的模型时,数量越大性能提高程度就越明显.

二.适用场景

        1.不能被动态合批

                模型顶点数据(位置,法线,切线,UV等)总和不能超过2700个float.

        2.静态合批不划算或者不能静态核批

                模型为一簇簇的出现或者散布广泛

                模型对象位置,大小,旋转等都在变化.

            比如: 

                        草丛,树木丛

                        建筑装饰物件

                        陨石群

三.在U3D里实现

Shader的支持

基础使用

1).使用U3D内置的Shader

  1. 使用Unity自带的能够开启GpuInstance 的Shader(几乎都有,但是也有例外,比如Partcles的材质好像都没有.),创建一个材质球.
  2. 在材质球的Inspector中把Enable GPU Instancing的选项给勾选上. 

                    

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 支持风动画。 - 树木创建器支持风动画。 - 支持软遮挡树。 - 包括广告牌烘焙师和渲染器。
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 multi pass 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 (also supports VR platforms with both single pass and multi pass rendering modes). – Automatically configured custom shader support. – Supports Standard, Universal and HD Render Pipelines. – Complex hierarchies of prefabs instanced with a single click. – Multiple sub-meshes support. – LOD Groups and cross-fading support. (Cross-fading is supported on Standard Render Pipeline only) – Automatic 2D Billboard generation system (Standard RP only). – Shadows casting and receiving support for instances (frustum culled instances still can cast shadows). – Ability to use custom shadow distance per prototype and to choose the LOD to render shadows with. – Support for Floating Origin handling. – Multiple camera support. – Well documented API for procedural scenes and runtime modifications (examples included). – Ability to Remove instances inside bounds or colliders at runtime. – Ability to extend with custom Compute Shaders. – 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-Re
BETA RELEASE Download Demo Build (Windows PC)​ Crowd Animations is an extension for GPU Instancer, and requires it to work. Support Forum | Documentation | F.A.Q. Crowd Animations is an out of the box solution for using massive amounts of animated characters in your scenes with high performance. CA uses the GPU Instancer core and adds GPU skinning techniques on top of GPUI’s indirect instancing solution and GPU culling techniques. This results in a combination which will allow you to get the most out of GPU Instancing in Unity for your animated characters. ——————————— FEATURES ——————————— – Indirect GPU instancing with skinned meshes. – GPU frustum, occlusion and distance culling. – VR compatible. Works with both single pass and multipass rendering modes. – Supports Standard, LW, HD and Universal Render Pipelines. – Custom shader support (requires manual set-up). – Animation Blending (up to 4 animations). – Multiple skinned mesh renderers and submeshes support. – LOD Groups support (all LOD meshes must use the same rig). – Bone attachments. – Root Motion support. – Ability to use custom shadow distance per prototype and to choose the LOD to render shadows with. – Automatic detection of added and removed instances without any additional code. – Rigidbody and physics support. – Custom animation event system. – Easy to use interface. Crowd Animations supports two different animator workflows: Mecanim Animator: You can use the Unity Mecanim Animator to handle the state machine for animations as usual, and GPUI will read the animator states from the Mecanim Animator. This will let you use your existing Animators and scripts with Crowd Animations, and make it possible to use indirect GPU Instancing out of the box with all the GPUI features such as GPU frustum and occlusion culling. This workflow increases GPU performance while rendering skinned meshes, but it won’t be an ideal solution for projects that are CPU bound since the Mecanim Animator will still create an o
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值