How to Speed Up a Compute Shader with Groupshared Memory
Groupshared memory in a compute shader is a small, fast memory that all threads in one thread group share. If those threads need the same data, read it from GPU memory once, put it in groupshared memory, and let every thread read it from there.
That one change made the Thermal Erosion 3 node in Vista, our procedural terrain generator for Unity, about 1.8x faster. The terrain it makes did not change. This post explains why it works, then shows the real code.

The problem: the same data is read many times
The terrain is a heightmap: a grid of cells, each with a height. It is stored in GPU memory, which is large but slow to read.
Erosion updates one cell at a time. To update a cell, the shader reads the height of that cell and its 8 neighbors, compares them, and moves a little soil downhill. It repeats this thousands of times.
The GPU runs one thread per cell. Threads run in groups of 64, covering an 8 x 8 area. Each thread reads 9 cells, so one group does 64 * 9 = 576 reads. But neighboring threads read almost the same cells. The whole group only touches a 10 x 10 area: 100 different cells. Most of the 576 reads fetch a value another thread in the group already fetched.
The math for each cell is tiny. The time goes into waiting for reads. A shader like this is called memory bound: it gets faster when it reads less data, not when it does less math.

The fix: groupshared memory in a compute shader
Each thread group gets a small block of fast memory, declared with groupshared in HLSL. Reading it takes a few GPU cycles. Reading GPU memory can take hundreds.
Only threads in the same group can see that block. It starts empty: nothing is in it until your code writes it.
So the shader works in three steps:
Load. The threads copy the group's 100 cells from GPU memory into groupshared memory, once.
Wait. Every thread waits until the copy is finished. This wait is called a barrier.
Compute. Each thread reads its 9 cells from groupshared memory instead of GPU memory.
One honest note: the GPU cache already saves some repeated reads. So you will not get the full 5.7x (576 down to 100). Expect around 1.3x to 2x on the parts you change. Groupshared memory makes the saving certain on every GPU, instead of depending on the cache.
Splitting 100 cells over 64 threads
There are 100 cells to load and 64 threads to load them. Each thread uses its index, 0 to 63, to pick cells:
Round 1: thread 0 loads cell 0, thread 1 loads cell 1, up to thread 63 loading cell 63.
Round 2: thread 0 loads cell 64, thread 1 loads cell 65, up to thread 35 loading cell 99. Threads 36 to 63 have nothing left, so they stop.
Every cell is loaded exactly once, and threads do not need to talk to each other. This is called a strided loop:
for (uint cell = threadIndex; cell < 100; cell += 64)
Note that during the load, a thread often copies a cell that another thread will use. That is fine. After the wait, each thread goes back to its own cell and reads its 9 neighbors.
The real code
This is the load step from Vista's ThermalErosion3.compute. It stores only the height, one float per cell, so the whole block is 400 bytes.
#define GROUP_SIZE 8
#define TILE_SIZE (GROUP_SIZE + 2) // 8x8 group plus a 1 cell border
groupshared float gs_HeightCM[TILE_SIZE * TILE_SIZE]; // 100 heights
#define TILE_H(x, z) gs_HeightCM[(z) * TILE_SIZE + (x)]
void LoadHeightTile(uint2 groupId, uint2 localId)
{
uint2 tileOriginPx = groupId * GROUP_SIZE + (uint2)MARGIN - 1; // top left of the 10x10 area
uint flatLocalIndex = localId.y * GROUP_SIZE + localId.x; // thread index, 0..63
for (uint slot = flatLocalIndex; slot < TILE_SIZE * TILE_SIZE; slot += GROUP_SIZE * GROUP_SIZE)
{
uint2 texel = tileOriginPx + uint2(slot % TILE_SIZE, slot / TILE_SIZE);
float4 data = _WorldDataCM[texel];
gs_HeightCM[slot] = data.r + data.g; // keep the height only
}
}Then each thread loads, waits, and computes:
LoadHeightTile(groupId.xz, groupThreadId.xz); // 1. load
GroupMemoryBarrierWithGroupSync(); // 2. wait
uint tx = groupThreadId.x + 1; // 3. compute: this thread's cell
uint tz = groupThreadId.z + 1;
float heightLeft = TILE_H(tx - 1, tz); // read from groupshared memoryPitfalls
Skipping the wait. A thread may read a cell that is not loaded yet. The result looks like random noise.
Storing more than you need. Keep only the values you read. Less groupshared memory per group lets more groups run at once.
Reading past the edge of the texture. Vista's texture has an empty border, so the outer ring of cells is always safe to read. Without one, clamp the coordinates.
The result
We changed the two height steps first, then the soil transport step. Measured on a desktop GPU, 2048 x 2048 terrain, 1000 iterations, median times:

Stage | Time |
|---|---|
Before groupshared | 2943 ms |
Height steps changed | 1876 ms |
Transport step changed too | 1643 ms |
About 1.8x faster overall, only from reading less memory.
In Vista
Thermal Erosion 3 shipped in Vista 3.2 with this change, and Hydraulic Erosion 2 uses the same approach. Both nodes are in every edition, including the free Vista Personal. They also spread a long simulation over several frames, so the editor stays responsive while it runs.
FAQ
When does groupshared memory make a compute shader faster? When threads in a group read the same data, such as a blur or erosion that reads neighbor cells.
Does groupshared memory always make a compute shader faster? No. If threads do not read the same data, the extra load and wait only add work.
Do I need a barrier after loading groupshared memory? Yes. Call GroupMemoryBarrierWithGroupSync() after the load, or some threads will read cells that are not loaded yet.
Try it
Find a compute shader where neighboring threads read the same cells, add the load and the wait, and time it before and after. To see these erosion nodes shape a terrain, try Vista.



Comments