Skip to content
systemcoding.dev
GPU

Declare a workgroup size with layout(local_size_x = 64) in

Set the local workgroup size of a GLSL compute shader with layout(local_size_x = 64) in, and compute the global index from gl_GlobalInvocationID.

1 min read
Summarize in AI

This is how a GLSL compute shader says how many invocations make up one workgroup. The declaration is required in a compute shader, and it is fixed at compile time, unlike an OpenCL local size passed at launch.

glsl
#version 430
layout(local_size_x = 64) in;

layout(std430, binding = 0) buffer Data { uint v[]; };

void main()
{
    uint i = gl_GlobalInvocationID.x;
    if (i < v.length())
        v[i] = i * 2u;
}

Unspecified dimensions default to 1, so this is a group of 64 by 1 by 1. For a 2D image you would write layout(local_size_x = 16, local_size_y = 16) in;. gl_GlobalInvocationID is gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID, so the first group covers indices 0 to 63, the second 64 to 127.

The size is bounded by GL_MAX_COMPUTE_WORK_GROUP_SIZE per dimension and by GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS for the product. OpenGL 4.3 guarantees the invocation limit is at least 1024, and the per-axis limits are at least 1024 for X and Y and 64 for Z, but real devices differ, so query them:

c
GLint inv; glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &inv);

A multiple of 32 or 64 matches the usual hardware group of lanes, but the best number comes from measuring. Use gl_WorkGroupSize inside the shader rather than repeating 64, so the constant lives in one place. Compute shaders need OpenGL 4.3 or GL_ARB_compute_shader; macOS stops at 4.1 and has none.

Share

More snippets

GPU

List platforms with clGetPlatformIDs

Call clGetPlatformIDs twice, once for the count and once for the handles, then print each platform name with clGetPlatformInfo in OpenCL 3.0 C.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.