> For the complete documentation index, see [llms.txt](https://ipgames.gitbook.io/htrace-wsgi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ipgames.gitbook.io/htrace-wsgi/quick-start-manual/gpu-instancing-and-manual-mesh-rendering.md).

# GPU Instancing & Manual Mesh Rendering

GPU instancing is a [draw call optimization](https://docs.unity3d.com/Manual/optimizing-draw-calls.html) 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.

{% hint style="info" %}
Enabling instancing on native materials does not affect regular rendering for the main camera, as the SRP Batcher is typically prioritized over GPU instancing
{% endhint %}

## Automatic Instancing

HTrace WSGI can take advantage of automatic GPU instancing in the following three cases:

* **Terrain** with "Draw Instanced" option enabled (currently available only in Editor, not in Builds)\
  ![](https://877355991-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjAg5otUNTCfeLUhM6dT%2Fuploads%2F0ZCMIYHXdqsuQiyiLJ2j%2Fimage.png?alt=media\&token=e9eae39c-d67c-4955-bc5e-270ac67ef0bb)\ <br>

* **Terrain Details** with "Use GPU Instancing" option enabled\
  ![](https://877355991-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjAg5otUNTCfeLUhM6dT%2Fuploads%2FeXDzMoPl58ytJjZcMrKX%2Fimage.png?alt=media\&token=6dcca361-3cc2-4e68-b7ce-eb1bc7a74834)

* **GameObjects** and **Trees** with materials that have GPU Instancing enabled![](https://877355991-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjAg5otUNTCfeLUhM6dT%2Fuploads%2F8d5nbNRfnZuJOzDsEsq5%2Fimage.png?alt=media\&token=14edf419-16bd-4d15-99a5-681c5bbebdb7)

## RenderX Functions with Native Shaders

HTrace WSGI supports manual (programmatic) mesh rendering & instancing with native HDRP materials through the following functions:

* [Graphics.RenderMesh()](https://docs.unity3d.com/ScriptReference/Graphics.RenderMesh.html)
* [Graphics.RenderMeshInstanced()](https://docs.unity3d.com/ScriptReference/Graphics.RenderMeshInstanced.html)

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

<figure><img src="https://877355991-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjAg5otUNTCfeLUhM6dT%2Fuploads%2Fp3JTpydJjlepJlhVyp8u%2Fimage.png?alt=media&amp;token=6aea641e-e1ee-4d23-8885-43085a76a163" alt="" width="563"><figcaption><p>Voxelization Debug view</p></figcaption></figure>

Another, more extensive example can be found in the [Example Project section](#example-project) below.

## RenderX Functions with Custom Shaders

HTrace supports manual (programmatic) mesh rendering & instancing with custom shaders through the following functions:

* [Graphics.RenderMeshIndirect()](https://docs.unity3d.com/ScriptReference/Graphics.RenderMeshIndirect.html)
* [Graphics.RenderMeshPrimitives()](https://docs.unity3d.com/ScriptReference/Graphics.RenderMeshPrimitives.html)
* [Graphics.RenderPrimitive()](https://ipgames.gitbook.io/htrace-wsgi/quick-start-manual/https:/docs.unity3d.com/ScriptReference/Graphics.RenderPrimitives.html)
* [Graphics.RenderPrimitivesIndexed()](https://docs.unity3d.com/ScriptReference/Graphics.RenderPrimitivesIndexed.html)
* [Graphics.RenderPrimitivesIndexedIndirect()](https://docs.unity3d.com/ScriptReference/Graphics.RenderPrimitivesIndexedIndirect.html)
* [Graphics.RenderPrimitivesIndirect()](https://docs.unity3d.com/ScriptReference/Graphics.RenderPrimitivesIndirect.html)

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](#example-project) below.

## Example Project

{% file src="/files/REVzNi6iishNWKRIyfo3" %}

To set this package up, follow these steps:

{% stepper %}
{% step %}
Download the .unitypackage file
{% endstep %}

{% step %}
Import it into the project that has HTraceWSGI package installed
{% endstep %}

{% step %}
Open `ManualMeshRenderingAndGPUInstancing_Scene` scene in Assets → HTraceWSGI → Examples → ManualMeshRenderingAndGPUInstancing
{% endstep %}

{% step %}
Add HTraceWSGI to the scene following [Quick-Start manual](/htrace-wsgi/quick-start-manual.md)
{% endstep %}

{% step %}
Enter Play mode
{% endstep %}
{% endstepper %}

If done correctly, the following output will be rendered:

<figure><img src="https://877355991-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjAg5otUNTCfeLUhM6dT%2Fuploads%2FuAaEySEH4txzUzWTaUNs%2Fimage.png?alt=media&amp;token=c459aaf5-208a-43fa-a6e2-bab2cb90c238" alt=""><figcaption></figcaption></figure>

The **Instancing Test** script contains examples for all `RenderX` functions described in the previous sections. To navigate between them use the `Type` dropdown:

<figure><img src="https://877355991-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfjAg5otUNTCfeLUhM6dT%2Fuploads%2FtLRrgtQslxirDNAyA0r4%2Fimage.png?alt=media&amp;token=56c07787-6113-4a8f-97bc-8dc7086c0335" alt="" width="482"><figcaption></figcaption></figure>

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.&#x20;

{% hint style="info" %}
`HTraceVoxelization` is a universal shader tag that can be used in any custom shader to add object to voxelization.
{% endhint %}
