Unity gpu instancing что это

GPU Instancing

Introduction

You can use GPU instancing to draw many identical objects with only a few draw calls. There are some restrictions which you need to bear in mind:

Adding instancing to your objects

There is a Standard Surface Shader available that supports instancing. Add one to your project by selecting Shader > Standard Surface Shader (Instanced).

Unity gpu instancing что это. Смотреть фото Unity gpu instancing что это. Смотреть картинку Unity gpu instancing что это. Картинка про Unity gpu instancing что это. Фото Unity gpu instancing что этоAdding the Standard Instanced Shader

Apply this shader to your GameObject’s Material. In your Material’s Inspector window, click the Shader drop-down, roll over the Instanced field and choose your instanced shader from the list:

Unity gpu instancing что это. Смотреть фото Unity gpu instancing что это. Смотреть картинку Unity gpu instancing что это. Картинка про Unity gpu instancing что это. Фото Unity gpu instancing что этоAssigning the Standard Instanced Shader to a Material

Adding per-instance data

Even though the instanced objects are sharing the same mesh and material, you can set shader properties on a per-object basis using the MaterialPropertyBlock API. In the example below, each object is assigned a random color value using the _Color property:

Adding instancing to your own shaders

Let’s take a simple unlit shader and make it capable of instancing:

Added code

Addition:Function:
#pragma multi_compile_instancingmulti_compile_instancing generates a shader with two variants: one with built-in keyword INSTANCING_ON defined (allowing instancing), the other with nothing defined. This allows the shader to fall back to a non-instanced version if instancing isn’t supported on the GPU.
UNITY_INSTANCE_IDThis is used in the vertex shader input/output structure to define an instance ID. See SV_InstanceID for more information.
UNITY_INSTANCING_CBUFFER_START(name) / UNITY_INSTANCING_CBUFFER_ENDEvery per-instance property must be defined in a specially named constant buffer. Use this pair of macros to wrap the properties you want to be made unique to each instance.
UNITY_DEFINE_INSTANCED_PROP(float4, color)This defines a per-instance shader property with a type and a name. In this example the color property is unique.
UNITY_SETUP_INSTANCE_ID(v);This makes the instance ID accessible to shader functions. It must be used at the very beginning of a vertex shader, and is optional for fragment shaders.
UNITY_TRANSFER_INSTANCE_ID(v, o);This copies the instance ID from the input structure to the output structure in the vertex shader. This is only necessary if you need to access per-instance data in fragment shader.
UNITY_ACCESS_INSTANCED_PROP(color)This accesses a per-instance shader property. It uses instance ID to index into instance data array.

Note: As long as material properties are instanced, renderers can always be rendered instanced, even if you put different instanced properties into different renderers. Normal “non-instanced” properties cannot be batched, so do not put them in the MaterialPropertyBlock; instead, create different materials for them.

A note regarding UnityObjectToClipPos

UnityObjectToClipPos(v.vertex) is always preferred where mul(UNITY_MATRIX_MVP,v.vertex) would otherwise be used. While you can continue to use UNITY_MATRIX_MVP as normal in instanced shaders, UnityObjectToClipPos is the most efficient way of transforming vertex positions from object space into clip space.

In instanced shaders, UNITY_MATRIX_MVP (among other built-in matrices) is transparently modified to include an extra matrix multiply. Specifically, it is expanded to mul(UNITY_MATRIX_VP, unity_ObjectToWorld). unity_ObjectToWorld is expanded to unity_ObjectToWorldArray[unity_InstanceID]). UnityObjectToClipPos is optimized to perform 2 matrix-vector multiplications simultaneously, and is therefore more efficient than performing the multiplication manually as the shader compiler will not automatically perform this optimization.

Источник

GPU instancing

Introduction

You can use GPU instancing to draw many identical objects with only a few draw calls. There are some restrictions that you need to bear in mind:

Adding instancing to your objects

A Standard Surface Shader that supports instancing is available in the Unity Editor. Add one to your project by selecting Shader > Standard Surface Shader (Instanced).

Unity gpu instancing что это. Смотреть фото Unity gpu instancing что это. Смотреть картинку Unity gpu instancing что это. Картинка про Unity gpu instancing что это. Фото Unity gpu instancing что этоAdding the Standard Instanced Shader

Apply this Shader to your GameObject’s Material. In your Material’s Inspector window, click the Shader drop-down, roll over the Instanced field, and choose your instanced Shader from the list:

Unity gpu instancing что это. Смотреть фото Unity gpu instancing что это. Смотреть картинку Unity gpu instancing что это. Картинка про Unity gpu instancing что это. Фото Unity gpu instancing что этоAssigning the Standard Instanced Shader to a Material

Adding per-instance data

Even though the instanced GameObjects are sharing the same Mesh and Material, you can set Shader properties on a per-object basis using the MaterialPropertyBlock API. In the example below, each GameObject is assigned a random color value using the _Color property:

Adding instancing to your own shaders

The following example takes a simple unlit Shader and makes it capable of instancing:

Added code

AdditionFunction
#pragma multi_compile_instancingmulti_compile_instancing generates a Shader with two variants: one with built-in keyword INSTANCING_ON defined (allowing instancing), the other with nothing defined. This allows the Shader to fall back to a non-instanced version if instancing isn’t supported on the GPU.
UNITY_INSTANCE_IDThis is used in the vertex Shader input/output structure to define an instance ID. See SV_InstanceID for more information.
UNITY_INSTANCING_CBUFFER_START(name) / UNITY_INSTANCING_CBUFFER_ENDEvery per-instance property must be defined in a specially named constant buffer. Use this pair of macros to wrap the properties you want to be made unique to each instance.
UNITY_DEFINE_INSTANCED_PROP(float4, color)This defines a per-instance Shader property with a type and a name. In this example, the _color property is unique.
UNITY_SETUP_INSTANCE_ID(v);This makes the instance ID accessible to Shader functions. It must be used at the very beginning of a vertex Shader, and is optional for fragment Shaders.
UNITY_TRANSFER_INSTANCE_ID(v, o);This copies the instance ID from the input structure to the output structure in the vertex Shader. This is only necessary if you need to access per-instance data in the fragment Shader.
UNITY_ACCESS_INSTANCED_PROP(color)This accesses a per-instance Shader property. It uses an instance ID to index into the instance data array.

A note regarding UnityObjectToClipPos

UnityObjectToClipPos(v.vertex) is always preferred where mul(UNITY_MATRIX_MVP,v.vertex) would otherwise be used. While you can continue to use UNITY_MATRIX_MVP as normal in instanced Shaders, UnityObjectToClipPos is the most efficient way of transforming vertex positions from object space into clip space.

UnityObjectToClipPos is optimized to perform two matrix-vector multiplications simultaneously, and is therefore more efficient than performing the multiplication manually, because the Shader compiler does not automatically perform this optimization.

Modifying multi-pass Shaders to work with instancing

For vertex and fragment Shaders, Unity needs to change the way vertex transformations are calculated in multi-pass scenarios (for example, in the ForwardAdd pass) to avoid z-fighting artifacts against the base/first passes due to floating point errors in matrix calculation. To do this, add #pragma force_concat_matrix to the Shader.

Specifically, the vertex transformation in the ForwardAdd pass is calculated by multiplying the M (model) matrix with the VP (view and projection) matrix instead of using a CPU-precomputed MVP matrix.

This is not necessary for surface Shaders, because the correct calculation is automatically substituted.

Batching priority

Static batching takes priority over instancing. If a GameObject is marked for static batching and is successfully batched, instancing is disabled even if its Renderer uses an instancing Shader. When this happens, a warning box appears in the Inspector suggesting that the Static Batching flag be unchecked in the Player Settings.

Instancing takes priority over dynamic batching. If Meshes can be instanced, dynamic batching is disabled.

Further notes

Is something described here not working as you expect it to? It might be a Known Issue. Please check with the Issue Tracker at issuetracker.unity3d.com.

Copyright © 2017 Unity Technologies. Publication 5.5-001G 2017-03-29

Источник

GPU instancing

Introduction

Use GPU Instancing to draw (or render) multiple copies of the same Mesh at once, using a small number of draw calls. It is useful for drawing objects such as buildings, trees and grass, or other things that appear repeatedly in a Scene.

GPU Instancing only renders identical Meshes with each draw call, but each instance can have different parameters (for example, color or scale) to add variation and reduce the appearance of repetition.

GPU Instancing can reduce the number of draw calls used per Scene. This significantly improves the rendering performance of your project.

Adding instancing to your Materials

To enable GPU Instancing on Materials, select your Material in the Project window, and in the Inspector, tick the Enable Instancing checkbox.

Unity gpu instancing что это. Смотреть фото Unity gpu instancing что это. Смотреть картинку Unity gpu instancing что это. Картинка про Unity gpu instancing что это. Фото Unity gpu instancing что этоThe Enable Instancing checkbox as it appears in the Material Inspector window

Unity only displays this checkbox if the Material Shader supports GPU Instancing. This includes Standard, StandardSpecular and all surface Shaders. See documentation on standard Shaders for more information.

The screenshots below show the same Scene with multiple GameObjects; in the top image GPU Instancing is enabled, in the bottom image it is not. Note the difference in FPS, Batches and Saved by batching.

Unity gpu instancing что это. Смотреть фото Unity gpu instancing что это. Смотреть картинку Unity gpu instancing что это. Картинка про Unity gpu instancing что это. Фото Unity gpu instancing что этоWith GPU Instancing: A simple Scene that includes multiple identical GameObjects that have GPU Instancing enabled Unity gpu instancing что это. Смотреть фото Unity gpu instancing что это. Смотреть картинку Unity gpu instancing что это. Картинка про Unity gpu instancing что это. Фото Unity gpu instancing что этоNo GPU Instancing: A simple Scene that includes multiple identical GameObjects that do not have GPU Instancing enabled.

When you use GPU instancing, the following restrictions apply:

Unity automatically picks MeshRenderer components and Graphics.DrawMesh calls for instancing. Note that SkinnedMeshRenderer is not supported.

Unity only batches GameObjects that share the same Mesh and the same Material in a single GPU instancing draw call. Use a small number of Meshes and Materials for better instancing efficiency. To create variations, modify your shader scripts to add per-instance data (see next section to learn more about this).

You can also use the calls Graphics.DrawMeshInstanced and Graphics.DrawMeshInstancedIndirect. to perform GPU Instancing from your scripts.

GPU Instancing is available on the following platforms and APIs:

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)

Adding per-instance data

By default, Unity only batches instances of GameObjects with different Transforms in each instanced draw call. To add more variance to your instanced GameObjects, modify your Shader to add per-instance properties such as Material color.

The example below demonstrates how to create an instanced Shader with different colour values for each instance.

When you declare _Color as an instanced property, Unity takes all _Color GameObjects that share a Mesh and Material and includes them in a single draw call.

For these changes to take effect, you must enable GPU Instancing. To do this, select your Shader in the Project window, and in the Inspector, tick the Enable Instancing checkbox.

Unity gpu instancing что это. Смотреть фото Unity gpu instancing что это. Смотреть картинку Unity gpu instancing что это. Картинка про Unity gpu instancing что это. Фото Unity gpu instancing что этоThe Enable Instancing checkbox as shown in the Shader Inspector window

Adding instancing to vertex and fragment Shaders

The following example takes a simple unlit Shader and makes it capable of instancing with different colors:

Shader modifications

AdditionFunction
#pragma multi_compile_instancingUse this to instruct Unity to generate instancing variants. It is not necessary for surface Shaders.
UNITY_VERTEX_INPUT_INSTANCE_IDUse this in the vertex Shader input/output structure to define an instance ID. See SV_InstanceID for more information.
UNITY_INSTANCING_CBUFFER_START(name) / UNITY_INSTANCING_CBUFFER_EN DEvery per-instance property must be defined in a specially named constant buffer. Use this pair of macros to wrap the properties you want to be made unique to each instance.
UNITY_DEFINE_INSTANCED_PROP(float4, _Color)Use this to define a per-instance Shader property with a type and a name. In this example, the _Color property is unique.
UNITY_SETUP_INSTANCE_ID(v);Use this to make the instance ID accessible to Shader functions. It must be used at the very beginning of a vertex Shader, and is optional for fragment Shaders.
UNITY_TRANSFER_INSTANCE_ID(v, o);Use this to copy the instance ID from the input structure to the output structure in the vertex Shader. This is only necessary if you need to access per-instance data in the fragment Shader.
UNITY_ACCESS_INSTANCED_PROP(color)Use this to access a per-instance Shader property. It uses an instance ID to index into the instance data array.

Notes:

Advanced GPU instancing tips

Batching priority

When batching, Unity prioritizes Static batching over instancing. If you mark one of your GameObjects for static batching, and Unity successfully batches it, Unity disables instancing on that GameObject, even if its Renderer uses an instancing Shader. When this happens, the Inspector window displays a warning message suggesting that you disable Static Batching. To do this, open the Player Settings (Edit > Project Settings > Player), open Other Settings, and under the Rendering section, untick the Static Batching checkbox.

Unity prioritizes instancing over dynamic batching. If Unity can instance a Mesh, it disables dynamic batching for that Mesh.

Graphics.DrawMeshInstanced

Some factors can prevent GameObjects from being instanced together automatically. These factors include Material changes and depth sorting. Use Graphics.DrawMeshInstanced to force Unity to draw these objects using GPU instancing. Like Graphics.DrawMesh, this function draws Meshes for one frame without creating unnecessary GameObjects.

Do not submit batches of instances that exceed the maxcount specified in your Shader script (500 by default). When using graphics tools from OpenGL or Metal, Unity divides the maxcount by 4, and uses the result as the maximum number of batches you can submit. The recommended practice for drawing arbitrary number of instances is to maintain a pool of pre-allocated 500-sized arrays (and MaterialPropertyBlocks if needed) and reuse these arrays as much as possible. See documentation on Automatic Memory Management for more information about object pooling.

Graphics.DrawMeshInstancedIndirect

Use DrawMeshInstancedIndirect in a script to read the parameters of instancing draw calls, including the number of instances, from a compute buffer. This is useful if you want to populate all of the instance data from the GPU, and the CPU does not know the number of instances to draw (for example, when performing GPU culling). See API documentation on Graphics.DrawMeshInstancedIndirect for a detailed explanation and code examples.

pragma instancing_options

The #pragma instancing_options directive can use the following switches:

SwitchFunction
maxcount: batchSizeUse this to specify the maximum number of instances to draw in one instanced draw call. By default this value is 500. When using OpenGL or Metal, Unity divides the maxcount by 4, and uses the result as the maximum number of batches you can submit. Make this value as small as possible to match the amount of instances you want to draw. For example, to draw a maximum of 1000 instances, add maxcount: 1000 to your shader script. Larger values increase Shader compilation time, and can reduce GPU performance.
force_same_maxcount_for_glUse this to force Unity to stop dividing the maxcount by 4 on graphics tools from OpenGL or Metal.
assumeuniformscalingUse this to instruct Unity to assume that all the instances have uniform scalings (the same scale for all X, Y and Z axes).
lodfadeUse this to make LOD fade values instanceable. This is useful for SpeedTree and other LOD techniques that use the LOD fading feature.
procedural:FunctionNameUse this to instruct Unity to generate an additional variant for use with Graphics.DrawMeshInstancedIndirect.
At the beginning of the vertex Shader stage, Unity calls the function specified after the colon. To set up the instance data manually, add per-instance data to this function in the same way you would normally add per-instance data to a Shader. Unity also calls this function at the beginning of a fragment Shader if any of the fetched instance properties are included in the fragment Shader.

UnityObjectToClipPos

The console window (menu: Window > Console) displays performance warnings if there are still places where UNITY_MATRIX_MVP (along with UNITY_MATRIX_MV ) is used.

Further notes

Surface Shaders have instancing variants generated by default, unless you specify noinstancing in the #pragma surface directive. Standard and StandardSpecular Shaders are already modified to have instancing support, but with no per-instance properties defined other than the transforms. Unity ignores uses of #pragma multi_compile_instancing in a surface Shader.

Unity strips instancing variants if GPU Instancing is not enabled on any GameObject in the Scene. To override the stripping behaviour, open the Graphics Settings (menu: Edit > Project Settings > Graphics), navigate to the Shader stripping section and change the Instancing Variants.

Instanced draw calls appear in the Frame Debugger as Draw Mesh (instanced).

You don’t always need to define per-instance properties. However, setting up an instance ID is mandatory, because world matrices need it to function correctly. Surface shaders automatically set up an instance ID. You must set up the instance ID for Custom Vertex and Fragment shaders manually. To do this, use UNITY_SETUP_INSTANCE_ID at the beginning of the Shader.

When using forward rendering, Unity cannot efficiently instance objects that are affected by multiple lights. Only the base pass can make effective use of instancing, not the added passes. For more information about lighting passes, see documentation on Forward Rendering and Pass Tags

Objects that use lightmaps, or are affected by different light or Reflection Probes, can’t be instanced.

If you have more than two passes for multi-pass Shaders, only the first passes can be instanced. This is because Unity forces the later passes to be rendered together for each object, forcing Material changes.

All the Shader macros used in the above examples are defined in UnityInstancing.cginc. Find this file in the following directory: [Unity installation folder]\Editor\Data\CGIncludes.

Enable instancing checkbox guidance, DrawMeshInstancedIndirect, #pragma multi-compile added in 5.6

Источник

GPU instancing

Introduction

Use GPU Instancing to draw (or render) multiple copies of the same Mesh at once, using a small number of draw calls. It is useful for drawing objects such as buildings, trees and grass, or other things that appear repeatedly in a Scene.

GPU Instancing only renders identical Meshes with each draw call, but each instance can have different parameters (for example, color or scale) to add variation and reduce the appearance of repetition.

GPU Instancing can reduce the number of draw calls used per Scene. This significantly improves the rendering performance of your project.

Adding instancing to your Materials

To enable GPU Instancing on Materials, select your Material in the Project window, and in the Inspector, tick the Enable Instancing checkbox.

Unity gpu instancing что это. Смотреть фото Unity gpu instancing что это. Смотреть картинку Unity gpu instancing что это. Картинка про Unity gpu instancing что это. Фото Unity gpu instancing что этоThe Enable Instancing checkbox as it appears in the Material Inspector window

Unity only displays this checkbox if the Material Shader supports GPU Instancing. This includes Standard, StandardSpecular and all surface Shaders. See documentation on standard Shaders for more information.

The screenshots below show the same Scene with multiple GameObjects; in the top image GPU Instancing is enabled, in the bottom image it is not. Note the difference in FPS, Batches and Saved by batching.

Unity gpu instancing что это. Смотреть фото Unity gpu instancing что это. Смотреть картинку Unity gpu instancing что это. Картинка про Unity gpu instancing что это. Фото Unity gpu instancing что этоWith GPU Instancing: A simple Scene that includes multiple identical GameObjects that have GPU Instancing enabled Unity gpu instancing что это. Смотреть фото Unity gpu instancing что это. Смотреть картинку Unity gpu instancing что это. Картинка про Unity gpu instancing что это. Фото Unity gpu instancing что этоNo GPU Instancing: A simple Scene that includes multiple identical GameObjects that do not have GPU Instancing enabled.

When you use GPU instancing, the following restrictions apply:

Unity automatically picks MeshRenderer components and Graphics.DrawMesh calls for instancing. Note that SkinnedMeshRenderer is not supported.

Unity only batches GameObjects that share the same Mesh and the same Material in a single GPU instancing draw call. Use a small number of Meshes and Materials for better instancing efficiency. To create variations, modify your shader scripts to add per-instance data (see next section to learn more about this).

You can also use the calls Graphics.DrawMeshInstanced and Graphics.DrawMeshInstancedIndirect to perform GPU Instancing from your scripts.

GPU Instancing is available on the following platforms and APIs:

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, Linux and Android

PlayStation 4 and Xbox One

WebGL (requires WebGL 2.0 API)

Adding per-instance data

By default, Unity only batches instances of GameObjects with different Transforms in each instanced draw call. To add more variance to your instanced GameObjects, modify your Shader to add per-instance properties such as Material color.

The example below demonstrates how to create an instanced Shader with different colour values for each instance.

When you declare _Color as an instanced property, Unity will gather _Color values from the MaterialPropertyBlock objects set on GameObjects and put them in a single draw call.

Note that in normal cases (where an instancing shader is not used, or _Color is not a per-instance property), draw call batches are broken due to different values in the MaterialPropertyBlock.

For these changes to take effect, you must enable GPU Instancing. To do this, select your Shader in the Project window, and in the Inspector, tick the Enable Instancing checkbox.

Unity gpu instancing что это. Смотреть фото Unity gpu instancing что это. Смотреть картинку Unity gpu instancing что это. Картинка про Unity gpu instancing что это. Фото Unity gpu instancing что этоThe Enable Instancing checkbox as shown in the Shader Inspector window

Adding instancing to vertex and fragment Shaders

The following example takes a simple unlit Shader and makes it capable of instancing with different colors:

Shader modifications

СложениеФункция:
#pragma multi_compile_instancingUse this to instruct Unity to generate instancing variants. It is not necessary for surface Shaders.
UNITY_VERTEX_INPUT_INSTANCE_IDUse this in the vertex Shader input/output structure to define an instance ID. See SV_InstanceID for more information.
UNITY_INSTANCING_BUFFER_START(name) / UNITY_INSTANCING_BUFFER_END(name)Every per-instance property must be defined in a specially named constant buffer. Use this pair of macros to wrap the properties you want to be made unique to each instance.
UNITY_DEFINE_INSTANCED_PROP(float4, _Color)Use this to define a per-instance Shader property with a type and a name. In this example, the _Color property is unique.
UNITY_SETUP_INSTANCE_ID(v);Use this to make the instance ID accessible to Shader functions. It must be used at the very beginning of a vertex Shader, and is optional for fragment Shaders.
UNITY_TRANSFER_INSTANCE_ID(v, o);Use this to copy the instance ID from the input structure to the output structure in the vertex Shader. This is only necessary if you need to access per-instance data in the fragment Shader.
UNITY_ACCESS_INSTANCED_PROP(arrayName, color)Use this to access a per-instance Shader property declared in an instancing constant buffer. It uses an instance ID to index into the instance data array. The arrayName in the macro must match the one in UNITY_INSTANCING_BUFFER_END(name) macro.

Notes:

Advanced GPU instancing tips

Batching priority

When batching, Unity prioritizes Static batching over instancing. If you mark one of your GameObjects for static batching, and Unity successfully batches it, Unity disables instancing on that GameObject, even if its Renderer uses an instancing Shader. When this happens, the Inspector window displays a warning message suggesting that you disable Static Batching. To do this, open the Player settings (Edit > Project Settings, then select the Player category), open Other Settings for your platform, and under the Rendering section, disable the Static Batching setting.

Unity prioritizes instancing over dynamic batching. If Unity can instance a Mesh, it disables dynamic batching for that Mesh.

Graphics.DrawMeshInstanced

Some factors can prevent GameObjects from being instanced together automatically. These factors include Material changes and depth sorting. Use Graphics.DrawMeshInstanced to force Unity to draw these objects using GPU instancing. Like Graphics.DrawMesh, this function draws Meshes for one frame without creating unnecessary GameObjects.

Graphics.DrawMeshInstancedIndirect

Use DrawMeshInstancedIndirect in a script to read the parameters of instancing draw calls, including the number of instances, from a compute buffer. This is useful if you want to populate all of the instance data from the GPU, and the CPU does not know the number of instances to draw (for example, when performing GPU culling). See API documentation on Graphics.DrawMeshInstancedIndirect for a detailed explanation and code examples.

Global Illumination support

Since Unity 2018.1, Global Illumination (GI) rendering is supported by GPU Instancing in the form of light probes, occlusion probes (in Shadowmask mode) and lightmap STs. Standard shaders and surface shaders have GI support automatically enabled.

Dynamic renderers affected by light probes and occlusion probes baked in the scene, and static renderers baked to the same lightmap texture, can be automatically batched together using GPU Instancing by Forward and Deferred render loop.

For Graphics.DrawMeshInstanced, you can enable light probe and occlusion probe rendering by setting the LightProbeUsage argument to CustomProvided and providing a MaterialPropertyBlock with probe data copied in. See API documentation on LightProbes.CalculateInterpolatedLightAndOcclusionProbes for a detailed explanation and code examples.

Global Illumination and GPU Instancing

GPU Instancing supports Global Illumination (GI) rendering in Unity. Each GPU instance can support GI coming from either different Light Probes, one lightmap (but multiple atlas regions in that lightmap), or one Light Probe Proxy Volume component (baked for the space volume containing all the instances). Standard shaders and surface shaders come with this support enabled.

You can use GPU Instancing to automatically batch dynamic Mesh Renderers affected by baked Light Probes (including their occlusion data), or static Mesh Renderers baked to the same lightmap Texture, via a Forward and Deferred render loop. See documentation on the Rendering pipeline for more information.

For Graphics.DrawMeshInstanced, you can enable the rendering of Light Probes (including their occlusion data) by setting the LightProbeUsage argument to CustomProvided and providing a MaterialPropertyBlock with probe data copied in. See API documentation on LightProbes.CalculateInterpolatedLightAndOcclusionProbes for a detailed explanation and code examples.

Shader warming-up

Since Unity 2017.3, you need to warm up shaders to use instancing on OpenGL if you want absolutely smooth rendering when the shader renders for the first time. If you warm up shaders for instancing on a platform that doesn’t require shader warm up, nothing will happen.

#pragma instancing_options

The #pragma instancing_options directive can use the following switches:

SwitchФункция:
forcemaxcount:batchSize and maxcount:batchSizeOn most platforms, Unity automatically calculates the instancing data array size by dividing the maximum constant buffer size on the target device with the size of the structure containing all per-instance properties. Generally you don’t need to worry about the batch size. However, on some platforms (Vulkan, Xbox One and Switch), a fixed array size is still required. You can specify the batch size for those platforms by using maxcount option. The option is completely ignored on the other platforms. If you really want to force a batch size for all platforms, use forcemaxcount (for example, when you know you will only issue draws with 256 instanced sprites via DrawMeshInstanced). The default value for the two options is 500.
assumeuniformscalingUse this to instruct Unity to assume that all the instances have uniform scalings (the same scale for all X, Y and Z axes).
nolodfadeUse this to prevent Unity from applying GPU Instancing to LOD fade values.
nolightprobeUse this to prevent Unity from applying GPU Instancing to Light Probe values (including their occlusion data). This is useful for performance if you are absolutely sure that there are no GameObjects using both GPU Instancing and Light Probes.
nolightmapUse this to prevent Unity from applying GPU Instancing to Lightmap ST (atlas information) values. This is useful for performance if you are absolutely sure that there are no GameObjects using both GPU Instancing and lightmaps.
procedural:FunctionNameUse this to instruct Unity to generate an additional variant for use with Graphics.DrawMeshInstancedIndirect.
At the beginning of the vertex Shader stage, Unity calls the function specified after the colon. To set up the instance data manually, add per-instance data to this function in the same way you would normally add per-instance data to a Shader. Unity also calls this function at the beginning of a fragment Shader if any of the fetched instance properties are included in the fragment Shader.

UnityObjectToClipPos

The console window (menu: Window > General > Console) displays performance warnings if there are still places where UNITY_MATRIX_MVP (along with UNITY_MATRIX_MV ) is used.

Further notes

Surface Shaders have instancing variants generated by default, unless you specify noinstancing in the #pragma surface directive. Standard and StandardSpecular Shaders are already modified to have instancing support, but with no per-instance properties defined other than the transforms. Unity ignores uses of #pragma multi_compile_instancing in a surface Shader.

Unity strips instancing variants if GPU Instancing is not enabled on any GameObject in the Scene. To override the stripping behaviour, open the Graphics settings (menu: Edit > Project Settings, then select the Graphics category), navigate to the Shader stripping section and change the Instancing Variants.

Instanced draw calls appear in the Frame Debugger as Draw Mesh (instanced).

You don’t always need to define per-instance properties. However, setting up an instance ID is mandatory, because world matrices need it to function correctly. Surface shaders automatically set up an instance ID. You must set up the instance ID for Custom Vertex and Fragment shaders manually. To do this, use UNITY_SETUP_INSTANCE_ID at the beginning of the Shader.

When using forward rendering, Unity cannot efficiently instance objects that are affected by multiple lights. Only the base pass can make effective use of instancing, not the added passes. For more information about lighting passes, see documentation on Forward Rendering and Pass Tags

If you have more than two passes for multi-pass Shaders, only the first passes can be instanced. This is because Unity forces the later passes to be rendered together for each object, forcing Material changes.

All the Shader macros used in the above examples are defined in UnityInstancing.cginc. Find this file in the following directory: [Unity installation folder]\Editor\Data\CGIncludes.

2017–10–24 Page amended with editorial review

Enable instancing checkbox guidance, DrawMeshInstancedIndirect, #pragma multi-compile added in 5.6

Shader warm up for GPU instancing added in 2017.3 NewIn20173

Global Illumination (GI) support in GPU instancing added in 2018.1 NewIn20181

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *