GPU Instancing & Manual Mesh Rendering
Last updated
Last updated
GPU instancing is a draw call optimization method that renders multiple copies of a mesh with the same material in a single draw call. It is advisable to use instancing whenever possible, because it can significantly improve the performance of voxelization.
Enabling instancing on native materials does not affect regular rendering for the main camera, as the SRP Batcher is typically prioritized over GPU instancing
HTrace can take advantage of automatic GPU instancing in the following three cases:
Terrain with "Draw Instanced" option enabled
Terrain Details with "Use GPU Instancing" option enabled
GameObjects and Trees with materials that have GPU Instancing enabled
HTrace supports manual (programmatic) mesh rendering & instancing with native HDRP materials through the following functions:
Rendering with these functions doesn't require custom shaders; it can be done with native shaders (e.g., HDRP Lit
), which are automatically handled by voxelization. To take advantage of GPU instancing in this case, make sure that the material used for rendering has GPU Instancing enabled.
An example of this rendering can be found in the Cornell Box sample scene included with the HTrace package. To view it, enter Play mode and enable the 'Constant Mode Only' GameObject in the scene hierarchy. This will spawn a rotating array of blue spheres rendered and animated using the RenderMeshInstanced()
function with a standard HDRP Lit material
Another, more extensive example can be found in the Example Project section below.
HTrace supports manual (programmatic) mesh rendering & instancing with custom shaders through the following functions:
Rendering with these functions requires custom shaders. Please, refer to Unity's documentation to find more about how to set them up. HTrace Voxelization pass must be added to such shaders in order to include them into voxelization, more details on it can be found in the Example Project section below.
To set this package up, follow these steps:
Download the .unitypackage file
Import it into the project that has HTrace package installed
Open ManualMeshRenderingAndGPUInstancing_Scene
scene in Assets → H-Trace → Examples → ManualMeshRenderingAndGPUInstancing
Add HTrace to the scene following Quick-Start manual
Enter Play mode
If done correctly, the following output will be rendered:
The Instancing Test script contains examples for all RenderX
functions described in the previous sections. To navigate between them use the Type
dropdown:
All types of RenderX
functions listed in this dropdown are compatible with voxelization, and their effects can be observed in the Voxelization Debug modes. For more details, refer to the ManualMeshRenderingAndGPUInstancing.cs
script and the corresponding shaders.
As mentioned in the previous sections, the Render Mesh
and Render Mesh Instanced
functions dispatch draw calls using native shaders (HDRP Lit
in this example), while other functions rely on custom shaders available in the Shaders folder.
Note that each of these custom shaders contains an HTrace Voxelization pass with a specific tag: Tags { "LightMode" = "HTraceVoxelization" }
, allowing objects to be included into voxelization.
HTraceVoxelization
is a universal shader tag that can be used in any custom shader to add object to voxelization.