You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
builtins with a 3D variant should all have 1D variants (global_invocation_index, workgroup_index) (WGSL proposed language change) #5154
Some builtins have both 3D and 1D variants, e.g., local_invocation_id and local_invocation_index. The latter is a linearized function of the former. Both are useful. They relieve the programmer from manually computing one from the other (usually 3D -> 1D), a computation that is unfortunately (in my experience) error-prone.
Two builtins have 3D but not 1D variants. I would like to see two 1D variants added as builtins:
global_invocation_index (from global_invocation_id). I should add here that from a teaching perspective, this is a very useful builtin as it allows a simpler WGSL 101 kernel (“one million threads each fetch one u32, add 1 to it, and write it back”). (But of course we shouldn’t add builtins for strictly pedagogical reasons.)
workgroup_index (from workgroup_id)
I expect these could be easily polyfilled, correctly, once, by the compiler team, and not many times, sometimes incorrectly, by WGSL programmers. :)
This was previously requested in #4087 with a few +1s there.
One concern here is that for a large enough dispatch, the linearized global invocation index could overflow the maximum u32 value. We could define the builtins as wrapping around in those cases, but that feels like a major trip hazard.
I would expect this would be a rare occurrence—such a grid would also have (far) more invocations than addressable memory locations—but I would submit a reasonable compromise would be that global_invocation_index return the low 32 bits of the infinite-precision global_invocation_index (wraparound), and there could also be a global_invocation_index_high that returns bits 63:32 of the infinite-precision global_invocation_index.
From internal discussion today, @jrprice recalls an earlier WGSL meeting where this option for dealing with overflow was considered. It follows similar flow to validation of workgroupCount{X|Y|Z} <= this.device.limits.maxComputeWorkgroupsPerDimension:
For dispatchWorkgroups:
allow the builtins
at command recording time, the implementation knows (a) the dispatch size, and (b) the workgroup size (reflected out of the compiled shader). If the shader statically uses the builtins and the dispatch would overflow the u32, then fail the dispatch and invalidate the compute command encoder.
This is in line with the dispatchWorkgroups validation that occurs in the device timeline.
From Google-internal discussion today, the idea carries over to indirect dispatch.
The same check occurs, but on the queue timeline; in the overflow case 0 workgroups will be dispatched. (No error is returned to the application).
JB: Today I’m just looking for a consensus on whether these are desirable and implementable. I want to either close the issue or ask for a PR.
JB: Perhaps we could provide linear versions of everything but the global invocation index?
DN: I think that’s a mug’s game. I’ve worked on platforms that supported 2billion invocations in a workgroup (in some configurations).
JB: In issue JP raises issue of overflows for example the global invocation id when flattened, since you can have 64k workgroups along each axis then in fantasy case have 2^48 range and dont' have 64 bit types. Letting thing overflow could be source of confusion giving folks back a vec2 to simulate 64bit doesn't seem better then them doing the math. Maybe global invocation id doesn't make sense to flatten but others we could provide flattened indices for.
DN: Personally I think it's cheesy to not support everywhere. Could conceive of a device with a large workgroup size so think separation between global and local is artificially. James recalled a discussion where we could do validation at the time you're encoding the dispatch to say if statically using builtin and at dispatch know workgroup size from shader and the number of workgroups we can do the math/validation in device timeline and error as one of the validation steps. You are not allowed to launch because the launch is too big
JB: Right fix shader to use id
DN: Yea, weird is we have indirect dispatch. We checked there is validation where we check the values in the buffer and if it's out of range then we zero out the dispatch so we can do the same kind of validation at queue timeline. If the shader will overflow then you get no invocations, same failure mode. Can make it safe and offer all of them.
My unofficial summary of the minutes: Yes, we should provide linearized versions of these builtin values. It is feasible to require validation sufficient to avoid overflows. Next step is to draft spec language.
JB: Discussion on issue is mostly editorial/bike shedding. Got approval from Oguz and from dneto so, just needs approvals from Mozilla/Apple and then can land. Will add Mozillas approval. Just need thumbs up from Apple. Will ping him on the issue.
overridex:u32;
overridey:u32;
overridez:u32;
@compute@workgroup_size(x,y,z)
fnmain(@builtin(workgroup_id) wgid:vec3u,
@builtin(num_workgroups) numwgs:vec3u,
@builtin(global_invocation_id) gid:vec3u) {
letworkgroup_index=wgid.x+ (wgid.y*numwgs.x) + (wgid.z*numwgs.x*numwgs.y);
letglobal_invocation_index=gid.x+ (gid.y*x*numwgs.x) + (gid.z*x*numwgs.x*y*numwgs.y);
// workgroup_index and global_invocation_index can be used as linear ranges now.
}
Can be replaced with:
overridex:u32;
overridey:u32;
overridez:u32;
@compute@workgroup_size(x,y,z)
fnmain(@builtin(workgroup_index) wgindex:u32,
@builtin(global_invocation_index) gindex:u32) {
// wgindex is equivalent to workgroup_index in the first shader. // gindex is equivalent to global_invocation_index in the first shader.
}
Activity
jimblandy commentedon Apr 29, 2025
This seems reasonable on the face. I'd like to hear thoughts from others with a lot of experience writing shaders.
cc @magcius @mighdoll @greggman @toji
jrprice commentedon Apr 29, 2025
This was previously requested in #4087 with a few +1s there.
One concern here is that for a large enough dispatch, the linearized global invocation index could overflow the maximum
u32value. We could define the builtins as wrapping around in those cases, but that feels like a major trip hazard.jowens commentedon Apr 29, 2025
I would expect this would be a rare occurrence—such a grid would also have (far) more invocations than addressable memory locations—but I would submit a reasonable compromise would be that
global_invocation_indexreturn the low 32 bits of the infinite-precisionglobal_invocation_index(wraparound), and there could also be aglobal_invocation_index_highthat returns bits 63:32 of the infinite-precisionglobal_invocation_index.dneto0 commentedon Feb 3, 2026
From internal discussion today, @jrprice recalls an earlier WGSL meeting where this option for dealing with overflow was considered. It follows similar flow to validation of
workgroupCount{X|Y|Z} <= this.device.limits.maxComputeWorkgroupsPerDimension:For
dispatchWorkgroups:This is in line with the
dispatchWorkgroupsvalidation that occurs in the device timeline.From Google-internal discussion today, the idea carries over to indirect dispatch.
The same check occurs, but on the queue timeline; in the overflow case 0 workgroups will be dispatched. (No error is returned to the application).
jimblandy commentedon Feb 3, 2026
minutes from WGSL committee meeting 2026-2-3
jimblandy commentedon Feb 3, 2026
My unofficial summary of the minutes: Yes, we should provide linearized versions of these builtin values. It is feasible to require validation sufficient to avoid overflows. Next step is to draft spec language.
jimblandy commentedon Mar 10, 2026
minutes from WGSL committee meeting 2026-3-10
Add linear_indexing language feature (gpuweb#5554)
alan-baker commentedon Mar 24, 2026
For posterity, the following code:
Can be replaced with:
b2: v148/webgpu-linear-indexing-feature contract + page ids
b2: v148/webgpu-linear-indexing-feature contract + page ids
b2: v148/webgpu-linear-indexing-feature contract + page ids