Group grids¶
What it does¶
A compute dispatch (cmdDispatch) or mesh draw (cmdDrawMesh) can launch at most 65,535 groups
per dimension on every backend (kMaxGroupsPerDimension). <RI/Helpers/Grid.hpp> folds a larger group count
into rows so it fits within that limit. RI::Helpers::groupGrid(count, groupSize) computes the
number of groups needed for count items, with groupSize items per group, and returns a
RI::DispatchIndirectArgs struct:
- If the group count is at most 65,535, it returns
{groups, 1, 1}(including{0, 1, 1}whencountis 0). - If the group count is larger than 65,535, it folds the groups into rows of 65,535 and returns
{65535, rows, 1}, up to 65,535 rows.
If groupSize is 0, or if the group count exceeds 65,535 x 65,535 groups, groupGrid reports a
programmer error.
In the shader, eqLinearGroup from <equine/grid.hlsli> recovers the 1D group index from
SV_GroupID. When the group count is above 65,535 and not a multiple of 65,535, the last row
launches extra groups past the end, so the shader must compare the item or group index against the
count and return early. Because groupGrid returns RI::DispatchIndirectArgs, you can pass its
fields to cmdDispatch or cmdDrawMesh, or write the struct directly into an indirect argument
buffer.
groupGrid runs on the CPU only and keeps no state.
Example¶
const RI::DispatchIndirectArgs grid = RI::Helpers::groupGrid(itemCount, 64);
RI::Helpers::cmdSetRootData(cmd, Root{.count = itemCount});
RI::cmdDispatch(cmd, grid.x, grid.y, grid.z);
#include <equine/grid.hlsli>
#include <equine/root.hlsli>
[numthreads(64, 1, 1)]
void main(uint3 group : SV_GroupID, uint3 thread : SV_GroupThreadID) {
const uint item = eqLinearGroup(group) * 64 + thread.x;
if (item >= root.count) {
return;
}
// ...
}
Mesh draws (cmdDrawMesh) also limit the total x * y * z group count to kMaxMeshGroups
(2^22). Because a folded grid pads the last row to a multiple of 65,535, the padded total
65,535 * rows must stay at or below kMaxMeshGroups.
How to roll your own¶
When the group count is above 65,535, divide it by 65,535 and round up for y, then dispatch
{65535, rows, 1}. In the shader, compute the linear group index as group.x + group.y * 65535.