How to use Geometry Shaders With Instancing With Structured/Compute GPU Buffers


In Unity, a Compute Buffer is a buffer of memory that lives on the CPU which can hold arbitrary data.  That memory is then uploaded to the GPU when it is bound to a shader.  You can then use a Structured Buffer in your shader to access the data.  Compute Buffers/Structured Buffers are very powerful as they easily allow you to upload a list of arbitrary input data to the GPU in easy to use structures that can be utilized by a shader.

The following C# script and shader will demonstrate how to use a geometry shader with instancing.  We will submit a single draw call with 4 instanced points.  The shader will then index our uploaded compute buffer, via the current instance id, to pass along the individual point data to the geometry shader.  From there, the geometry shader will expand each point into a quad composed of 2 triangles.  The geometry shader will also procedurally generated uv coordinates for each new vertex so that the fragment shader can generate a vertical gradient.

Download the Sample Project

Share It!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.