Fix local_invocation_id and local_invocation_index handling (#9099)

Co-authored-by: Andreas Reich <r_andreas2@web.de>
This commit is contained in:
Inner DaemonsandAndreas Reich authored and GitHub committed 2026-03-15 20:25:19 +00:00
1 parent e682ce3f29
commit 96b7872f77
19 files changed
+235 -35

No files matched your search

+1
View File
@@ -210,6 +210,7 @@ By @kpreid in [#9042](https://github.com/gfx-rs/wgpu/pull/9042).
- Renamed `EXPERIMENTAL_PASSTHROUGH_SHADERS` to `PASSTHROUGH_SHADERS` and made this no longer an experimental feature. by @inner-daemons in [#9054](https://github.com/gfx-rs/wgpu/pull/9054).
- BREAKING: End offsets in trace and `player` commands are now represented using `offset` + `size` instead. By @ErichDonGubler in [9073](https://github.com/gfx-rs/wgpu/pull/9073).
- Validate some uncaught cases where buffer transfer operations could overflow when computing an end offset. By @ErichDonGubler in [9073](https://github.com/gfx-rs/wgpu/pull/9073).
- Fix `local_invocation_id` and `local_invocation_index` being written multiple times in HLSL/MSL backends, and naming conflicts when users name variables `__local_invocation_id` or `__local_invocation_index`. By @inner-daemons in [#9099](https://github.com/gfx-rs/wgpu/pull/9099).
- Added internal labels to validation GPU objects and timestamp normalization code to improve clarity in graphics debuggers. By @szostid in [9094](https://github.com/gfx-rs/wgpu/pull/9094)
- Fix multi-planar texture copying. By @noituri [#9069](https://github.com/gfx-rs/wgpu/pull/9069)
+43 -17
View File
@@ -74,6 +74,7 @@ struct EntryPointBinding {
ty_name: String,
/// Members of generated structure
members: Vec<EpStructMember>,
local_invocation_index_name: Option<String>,
}
pub(super) struct EntryPointInterface {
@@ -619,11 +620,23 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
write!(self.out, "struct {struct_name}")?;
writeln!(self.out, " {{")?;
let mut local_invocation_index_name = None;
let mut subgroup_id_used = false;
for m in members.iter() {
// Sanity check that each IO member is a built-in or is assigned a
// location. Also see note about nesting in `write_ep_input_struct`.
debug_assert!(m.binding.is_some());
match m.binding {
Some(crate::Binding::BuiltIn(crate::BuiltIn::SubgroupId)) => {
subgroup_id_used = true;
}
Some(crate::Binding::BuiltIn(crate::BuiltIn::LocalInvocationIndex)) => {
local_invocation_index_name = Some(m.name.clone());
}
_ => (),
}
if is_subgroup_builtin_binding(&m.binding) {
continue;
}
@@ -636,17 +649,10 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
self.write_semantic(&m.binding, Some(shader_stage))?;
writeln!(self.out, ";")?;
}
if members.iter().any(|arg| {
matches!(
arg.binding,
Some(crate::Binding::BuiltIn(crate::BuiltIn::SubgroupId))
)
}) {
writeln!(
self.out,
"{}uint __local_invocation_index : SV_GroupIndex;",
back::INDENT
)?;
if subgroup_id_used && local_invocation_index_name.is_none() {
let name = self.namer.call("local_invocation_index");
writeln!(self.out, "{}uint {name} : SV_GroupIndex;", back::INDENT)?;
local_invocation_index_name = Some(name);
}
writeln!(self.out, "}};")?;
writeln!(self.out)?;
@@ -666,6 +672,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
arg_name: self.namer.call(struct_name.to_lowercase().as_str()),
ty_name: struct_name,
members,
local_invocation_index_name,
})
}
@@ -845,8 +852,10 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
Some(crate::Binding::BuiltIn(crate::BuiltIn::SubgroupId)) => {
write!(
self.out,
"{}.__local_invocation_index / WaveGetLaneCount()",
ep_input.arg_name
"{}.{} / WaveGetLaneCount()",
ep_input.arg_name,
// When writing SubgroupId, we always guarantee that local_invocation_index_name is written
ep_input.local_invocation_index_name.as_ref().unwrap()
)?;
}
_ => {
@@ -1593,6 +1602,8 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
let need_workgroup_variables_initialization =
self.need_workgroup_variables_initialization(func_ctx, module);
let needs_local_invocation_id_name = need_workgroup_variables_initialization;
let mut local_invocation_id_name = None;
// Write function arguments for non entry point functions
match func_ctx.ty {
back::FunctionType::Function(handle) => {
@@ -1620,6 +1631,12 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
let argument_name =
&self.names[&NameKey::EntryPointArgument(ep_index, index as u32)];
if arg.binding
== Some(crate::Binding::BuiltIn(crate::BuiltIn::LocalInvocationId))
{
local_invocation_id_name = Some(argument_name.clone());
}
write!(self.out, " {argument_name}")?;
if let TypeInner::Array { base, size, .. } = module.types[arg.ty].inner {
self.write_array_size(module, base, size)?;
@@ -1628,7 +1645,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
self.write_semantic(&arg.binding, Some((stage, Io::Input)))?;
}
}
if need_workgroup_variables_initialization {
if needs_local_invocation_id_name && local_invocation_id_name.is_none() {
if self
.entry_point_io
.get(&(ep_index as usize))
@@ -1639,7 +1656,9 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
{
write!(self.out, ", ")?;
}
write!(self.out, "uint3 __local_invocation_id : SV_GroupThreadID")?;
let var_name = self.namer.call("local_invocation_id");
write!(self.out, "uint3 {var_name} : SV_GroupThreadID")?;
local_invocation_id_name = Some(var_name);
}
}
}
@@ -1659,7 +1678,13 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
writeln!(self.out, "{{")?;
if need_workgroup_variables_initialization {
self.write_workgroup_variables_initialization(func_ctx, module)?;
self.write_workgroup_variables_initialization(
func_ctx,
module,
// need_workgroup_variables_initialization forces this to be written
// if the user doesn't specify it (so this must be Some())
local_invocation_id_name.unwrap(),
)?;
}
if let back::FunctionType::EntryPoint(index) = func_ctx.ty {
@@ -1810,12 +1835,13 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
&mut self,
func_ctx: &back::FunctionCtx,
module: &Module,
local_invocation_id_name: String,
) -> BackendResult {
let level = back::Level(1);
writeln!(
self.out,
"{level}if (all(__local_invocation_id == uint3(0u, 0u, 0u))) {{"
"{level}if (all({local_invocation_id_name} == uint3(0u, 0u, 0u))) {{"
)?;
let vars = module.global_variables.iter().filter(|&(handle, var)| {
+2
View File
@@ -343,6 +343,8 @@ const RESERVED: &[&str] = &[
"M_SQRT1_2",
// Naga utilities
"DefaultConstructible",
// Naga builtin names
"__local_invocation_id",
super::writer::FREXP_FUNCTION,
super::writer::MODF_FUNCTION,
super::writer::ABS_FUNCTION,
@@ -0,0 +1,15 @@
var<workgroup> wg_var: u32;
struct Input {
@builtin(local_invocation_id)
local_invocation_id: vec3<u32>,
@builtin(local_invocation_index)
local_invocation_index: u32,
}
@compute
@workgroup_size(1)
fn compute1(input: Input) {
wg_var = input.local_invocation_index * 2;
wg_var += input.local_invocation_id.x;
}
@@ -0,0 +1,27 @@
#version 310 es
precision highp float;
precision highp int;
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
struct Input {
uvec3 local_invocation_id;
uint local_invocation_index;
};
shared uint wg_var;
void main() {
if (gl_LocalInvocationID == uvec3(0u)) {
wg_var = 0u;
}
memoryBarrierShared();
barrier();
Input input_ = Input(gl_LocalInvocationID, gl_LocalInvocationIndex);
wg_var = (input_.local_invocation_index * 2u);
uint _e6 = wg_var;
wg_var = (_e6 + input_.local_invocation_id.x);
return;
}
@@ -4,7 +4,7 @@ static uint global_2 = (uint)0;
static uint global_3 = (uint)0;
struct ComputeInput_main {
uint __local_invocation_index : SV_GroupIndex;
uint local_invocation_index : SV_GroupIndex;
};
void function()
@@ -39,7 +39,7 @@ void function()
void main(ComputeInput_main computeinput_main)
{
uint param = (1u + WaveGetLaneCount() - 1u) / WaveGetLaneCount();
uint param_1 = computeinput_main.__local_invocation_index / WaveGetLaneCount();
uint param_1 = computeinput_main.local_invocation_index / WaveGetLaneCount();
uint param_2 = WaveGetLaneCount();
uint param_3 = WaveGetLaneIndex();
global = param;
@@ -0,0 +1,19 @@
struct Input {
uint3 local_invocation_id : SV_GroupThreadID;
uint local_invocation_index : SV_GroupIndex;
};
groupshared uint wg_var;
[numthreads(1, 1, 1)]
void compute1_(Input input, uint3 local_invocation_id : SV_GroupThreadID)
{
if (all(local_invocation_id == uint3(0u, 0u, 0u))) {
wg_var = (uint)0;
}
GroupMemoryBarrierWithGroupSync();
wg_var = (input.local_invocation_index * 2u);
uint _e6 = wg_var;
wg_var = (_e6 + input.local_invocation_id.x);
return;
}
@@ -0,0 +1,12 @@
(
vertex:[
],
fragment:[
],
compute:[
(
entry_point:"compute1_",
target_profile:"cs_5_1",
),
],
)
@@ -30,9 +30,9 @@ groupshared int64_t workgroup_atomic_arr[2];
groupshared Struct workgroup_struct;
[numthreads(2, 1, 1)]
void cs_main(uint3 id : SV_GroupThreadID, uint3 __local_invocation_id : SV_GroupThreadID)
void cs_main(uint3 id : SV_GroupThreadID)
{
if (all(__local_invocation_id == uint3(0u, 0u, 0u))) {
if (all(id == uint3(0u, 0u, 0u))) {
workgroup_atomic_scalar = (uint64_t)0;
workgroup_atomic_arr = (int64_t[2])0;
workgroup_struct = (Struct)0;
+2 -2
View File
@@ -21,9 +21,9 @@ groupshared int workgroup_atomic_arr[2];
groupshared Struct workgroup_struct;
[numthreads(2, 1, 1)]
void cs_main(uint3 id : SV_GroupThreadID, uint3 __local_invocation_id : SV_GroupThreadID)
void cs_main(uint3 id : SV_GroupThreadID)
{
if (all(__local_invocation_id == uint3(0u, 0u, 0u))) {
if (all(id == uint3(0u, 0u, 0u))) {
workgroup_atomic_scalar = (uint)0;
workgroup_atomic_arr = (int[2])0;
workgroup_struct = (Struct)0;
+2 -2
View File
@@ -110,9 +110,9 @@ uint NagaBufferLength(ByteAddressBuffer buffer)
}
[numthreads(1, 1, 1)]
void main(uint3 __local_invocation_id : SV_GroupThreadID)
void main(uint3 local_invocation_id : SV_GroupThreadID)
{
if (all(__local_invocation_id == uint3(0u, 0u, 0u))) {
if (all(local_invocation_id == uint3(0u, 0u, 0u))) {
wg = (float[10])0;
at_1 = (uint)0;
}
+2 -2
View File
@@ -75,9 +75,9 @@ FragmentOutput fragment(FragmentInput_fragment fragmentinput_fragment)
}
[numthreads(1, 1, 1)]
void compute(uint3 global_id : SV_DispatchThreadID, uint3 local_id : SV_GroupThreadID, uint local_index : SV_GroupIndex, uint3 wg_id : SV_GroupID, uint3 num_wgs : SV_GroupID, uint3 __local_invocation_id : SV_GroupThreadID)
void compute(uint3 global_id : SV_DispatchThreadID, uint3 local_id : SV_GroupThreadID, uint local_index : SV_GroupIndex, uint3 wg_id : SV_GroupID, uint3 num_wgs : SV_GroupID)
{
if (all(__local_invocation_id == uint3(0u, 0u, 0u))) {
if (all(local_id == uint3(0u, 0u, 0u))) {
output = (uint[1])0;
}
GroupMemoryBarrierWithGroupSync();
@@ -4,14 +4,14 @@ struct Structure {
};
struct ComputeInput_main {
uint __local_invocation_index : SV_GroupIndex;
uint local_invocation_index : SV_GroupIndex;
};
[numthreads(1, 1, 1)]
void main(ComputeInput_main computeinput_main)
{
Structure sizes = { (1u + WaveGetLaneCount() - 1u) / WaveGetLaneCount(), WaveGetLaneCount() };
uint subgroup_id = computeinput_main.__local_invocation_index / WaveGetLaneCount();
uint subgroup_id = computeinput_main.local_invocation_index / WaveGetLaneCount();
uint subgroup_invocation_id = WaveGetLaneIndex();
const uint4 _e7 = WaveActiveBallot(((subgroup_invocation_id & 1u) == 1u));
const uint4 _e8 = WaveActiveBallot(true);
@@ -8,9 +8,9 @@ groupshared int wg_signed;
groupshared AtomicStruct wg_struct;
[numthreads(64, 1, 1)]
void test_atomic_workgroup_uniform_load(uint3 workgroup_id : SV_GroupID, uint3 local_id : SV_GroupThreadID, uint3 __local_invocation_id : SV_GroupThreadID)
void test_atomic_workgroup_uniform_load(uint3 workgroup_id : SV_GroupID, uint3 local_id : SV_GroupThreadID)
{
if (all(__local_invocation_id == uint3(0u, 0u, 0u))) {
if (all(local_id == uint3(0u, 0u, 0u))) {
wg_scalar = (uint)0;
wg_signed = (int)0;
wg_struct = (AtomicStruct)0;
@@ -3,9 +3,9 @@ static const uint SIZE = 128u;
groupshared int arr_i32_[128];
[numthreads(4, 1, 1)]
void test_workgroupUniformLoad(uint3 workgroup_id : SV_GroupID, uint3 __local_invocation_id : SV_GroupThreadID)
void test_workgroupUniformLoad(uint3 workgroup_id : SV_GroupID, uint3 local_invocation_id : SV_GroupThreadID)
{
if (all(__local_invocation_id == uint3(0u, 0u, 0u))) {
if (all(local_invocation_id == uint3(0u, 0u, 0u))) {
arr_i32_ = (int[128])0;
}
GroupMemoryBarrierWithGroupSync();
@@ -8,9 +8,9 @@ groupshared WStruct w_mem;
RWByteAddressBuffer output : register(u0);
[numthreads(1, 1, 1)]
void main(uint3 __local_invocation_id : SV_GroupThreadID)
void main(uint3 local_invocation_id : SV_GroupThreadID)
{
if (all(__local_invocation_id == uint3(0u, 0u, 0u))) {
if (all(local_invocation_id == uint3(0u, 0u, 0u))) {
w_mem = (WStruct)0;
}
GroupMemoryBarrierWithGroupSync();
@@ -0,0 +1,28 @@
// language: metal1.0
#include <metal_stdlib>
#include <simd/simd.h>
using metal::uint;
struct Input {
metal::packed_uint3 local_invocation_id;
uint local_invocation_index;
};
struct compute1_Input {
};
kernel void compute1_(
metal::uint3 local_invocation_id [[thread_position_in_threadgroup]]
, uint local_invocation_index [[thread_index_in_threadgroup]]
, threadgroup uint& wg_var
) {
if (metal::all(local_invocation_id == metal::uint3(0u))) {
wg_var = {};
}
metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup);
const Input input = { local_invocation_id, local_invocation_index };
wg_var = input.local_invocation_index * 2u;
uint _e6 = wg_var;
wg_var = _e6 + input.local_invocation_id[0];
return;
}
@@ -0,0 +1,56 @@
; SPIR-V
; Version: 1.1
; Generator: rspirv
; Bound: 34
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %16 "compute1" %10 %13
OpExecutionMode %16 LocalSize 1 1 1
OpMemberDecorate %5 0 Offset 0
OpMemberDecorate %5 1 Offset 12
OpDecorate %10 BuiltIn LocalInvocationId
OpDecorate %13 BuiltIn LocalInvocationIndex
%2 = OpTypeVoid
%3 = OpTypeInt 32 0
%4 = OpTypeVector %3 3
%5 = OpTypeStruct %4 %3
%7 = OpTypePointer Workgroup %3
%6 = OpVariable %7 Workgroup
%11 = OpTypePointer Input %4
%10 = OpVariable %11 Input
%14 = OpTypePointer Input %3
%13 = OpVariable %14 Input
%17 = OpTypeFunction %2
%18 = OpConstant %3 2
%20 = OpConstantNull %3
%21 = OpConstant %3 0
%23 = OpTypeBool
%26 = OpConstant %3 264
%16 = OpFunction %2 None %17
%8 = OpLabel
%12 = OpLoad %4 %10
%15 = OpLoad %3 %13
%9 = OpCompositeConstruct %5 %12 %15
OpBranch %19
%19 = OpLabel
%22 = OpIEqual %23 %15 %21
OpSelectionMerge %24 None
OpBranchConditional %22 %25 %24
%25 = OpLabel
OpStore %6 %20
OpBranch %24
%24 = OpLabel
OpControlBarrier %18 %18 %26
OpBranch %27
%27 = OpLabel
%28 = OpCompositeExtract %3 %9 1
%29 = OpIMul %3 %28 %18
OpStore %6 %29
%30 = OpLoad %3 %6
%31 = OpCompositeExtract %4 %9 0
%32 = OpCompositeExtract %3 %31 0
%33 = OpIAdd %3 %30 %32
OpStore %6 %33
OpReturn
OpFunctionEnd
@@ -0,0 +1,14 @@
struct Input {
@builtin(local_invocation_id) local_invocation_id: vec3<u32>,
@builtin(local_invocation_index) local_invocation_index: u32,
}
var<workgroup> wg_var: u32;
@compute @workgroup_size(1, 1, 1)
fn compute1_(input: Input) {
wg_var = (input.local_invocation_index * 2u);
let _e6 = wg_var;
wg_var = (_e6 + input.local_invocation_id.x);
return;
}