Skip to content

Commit 9088984

Browse files
committed
builtin: prototype with local_invocation_id
1 parent 2232fcb commit 9088984

File tree

6 files changed

+150
-0
lines changed

6 files changed

+150
-0
lines changed

crates/spirv-std/src/builtin.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//! Symbols to query SPIR-V read-only global built-ins
2+
3+
/// compute shader built-ins
4+
pub mod compute {
5+
#[cfg(target_arch = "spirv")]
6+
use core::arch::asm;
7+
use glam::UVec3;
8+
9+
/// GLSL: `gl_LocalInvocationID()`
10+
/// WGSL: `local_invocation_id`
11+
#[doc(alias = "gl_LocalInvocationID")]
12+
#[inline]
13+
#[gpu_only]
14+
pub fn local_invocation_id() -> UVec3 {
15+
unsafe {
16+
let result = UVec3::default();
17+
asm! {
18+
"%builtin = OpVariable typeof{result} Input",
19+
"OpDecorate %builtin BuiltIn LocalInvocationId",
20+
"%result = OpLoad typeof*{result} %builtin",
21+
"OpStore {result} %result",
22+
result = in(reg) &result,
23+
}
24+
result
25+
}
26+
}
27+
}

crates/spirv-std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ pub use macros::spirv;
9191
pub use macros::{debug_printf, debug_printfln};
9292

9393
pub mod arch;
94+
pub mod builtin;
9495
pub mod byte_addressable_buffer;
9596
pub mod debug_printf;
9697
pub mod float;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// build-pass
2+
// compile-flags: -C llvm-args=--disassemble
3+
// normalize-stderr-test "OpLine .*\n" -> ""
4+
// normalize-stderr-test "OpSource .*\n" -> ""
5+
// normalize-stderr-test "%\d+ = OpString .*\n" -> ""
6+
// normalize-stderr-test "OpCapability VulkanMemoryModel\n" -> ""
7+
// normalize-stderr-test "OpMemoryModel Logical Vulkan" -> "OpMemoryModel Logical Simple"
8+
// normalize-stderr-test "; .*\n" -> ""
9+
// ignore-spv1.0
10+
// ignore-spv1.1
11+
// ignore-spv1.2
12+
// ignore-spv1.3
13+
// ignore-vulkan1.0
14+
// ignore-vulkan1.1
15+
16+
use spirv_std::glam::*;
17+
use spirv_std::spirv;
18+
19+
#[spirv(compute(threads(1)))]
20+
pub fn compute(
21+
#[spirv(descriptor_set = 0, binding = 0, storage_buffer)] out: &mut u32,
22+
// #[spirv(global_invocation_id)] global_invocation_id: UVec3,
23+
// #[spirv(local_invocation_id)] local_invocation_id: UVec3,
24+
// #[spirv(subgroup_local_invocation_id)] subgroup_local_invocation_id: u32,
25+
// #[spirv(num_subgroups)] num_subgroups: u32,
26+
// #[spirv(num_workgroups)] num_workgroups: UVec3,
27+
// #[spirv(subgroup_id)] subgroup_id: u32,
28+
// #[spirv(workgroup_id)] workgroup_id: UVec3,
29+
) {
30+
let local_invocation_id = spirv_std::builtin::compute::local_invocation_id();
31+
*out = local_invocation_id.x;
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
OpCapability Shader
2+
OpMemoryModel Logical Simple
3+
OpEntryPoint GLCompute %1 "compute" %2 %3
4+
OpExecutionMode %1 LocalSize 1 1 1
5+
OpDecorate %6 Block
6+
OpMemberDecorate %6 0 Offset 0
7+
OpDecorate %2 Binding 0
8+
OpDecorate %2 DescriptorSet 0
9+
OpDecorate %3 BuiltIn LocalInvocationId
10+
%7 = OpTypeInt 32 0
11+
%6 = OpTypeStruct %7
12+
%8 = OpTypePointer StorageBuffer %6
13+
%9 = OpTypeVoid
14+
%10 = OpTypeFunction %9
15+
%11 = OpTypePointer StorageBuffer %7
16+
%2 = OpVariable %8 StorageBuffer
17+
%12 = OpConstant %7 0
18+
%13 = OpTypeVector %7 3
19+
%14 = OpTypePointer Input %13
20+
%3 = OpVariable %14 Input
21+
%1 = OpFunction %9 None %10
22+
%15 = OpLabel
23+
%16 = OpInBoundsAccessChain %11 %2 %12
24+
%17 = OpLoad %13 %3
25+
%18 = OpCompositeExtract %7 %17 0
26+
OpStore %16 %18
27+
OpNoLine
28+
OpReturn
29+
OpFunctionEnd
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// build-pass
2+
// compile-flags: -C llvm-args=--disassemble
3+
// normalize-stderr-test "OpLine .*\n" -> ""
4+
// normalize-stderr-test "OpSource .*\n" -> ""
5+
// normalize-stderr-test "%\d+ = OpString .*\n" -> ""
6+
// normalize-stderr-test "OpCapability VulkanMemoryModel\n" -> ""
7+
// normalize-stderr-test "OpMemoryModel Logical Vulkan" -> "OpMemoryModel Logical Simple"
8+
// normalize-stderr-test "; .*\n" -> ""
9+
// ignore-spv1.0
10+
// ignore-spv1.1
11+
// ignore-spv1.2
12+
// ignore-spv1.3
13+
// ignore-vulkan1.0
14+
// ignore-vulkan1.1
15+
16+
use spirv_std::glam::*;
17+
use spirv_std::spirv;
18+
19+
#[spirv(compute(threads(1)))]
20+
pub fn compute(
21+
#[spirv(descriptor_set = 0, binding = 0, storage_buffer)] out: &mut u32,
22+
// #[spirv(global_invocation_id)] global_invocation_id: UVec3,
23+
#[spirv(local_invocation_id)] local_invocation_id: UVec3,
24+
// #[spirv(subgroup_local_invocation_id)] subgroup_local_invocation_id: u32,
25+
// #[spirv(num_subgroups)] num_subgroups: u32,
26+
// #[spirv(num_workgroups)] num_workgroups: UVec3,
27+
// #[spirv(subgroup_id)] subgroup_id: u32,
28+
// #[spirv(workgroup_id)] workgroup_id: UVec3,
29+
) {
30+
*out = local_invocation_id.x;
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
OpCapability Shader
2+
OpMemoryModel Logical Simple
3+
OpEntryPoint GLCompute %1 "compute" %2 %3
4+
OpExecutionMode %1 LocalSize 1 1 1
5+
OpName %3 "local_invocation_id"
6+
OpDecorate %5 Block
7+
OpMemberDecorate %5 0 Offset 0
8+
OpDecorate %2 Binding 0
9+
OpDecorate %2 DescriptorSet 0
10+
OpDecorate %3 BuiltIn LocalInvocationId
11+
%6 = OpTypeInt 32 0
12+
%5 = OpTypeStruct %6
13+
%7 = OpTypePointer StorageBuffer %5
14+
%8 = OpTypeVector %6 3
15+
%9 = OpTypePointer Input %8
16+
%10 = OpTypeVoid
17+
%11 = OpTypeFunction %10
18+
%12 = OpTypePointer StorageBuffer %6
19+
%2 = OpVariable %7 StorageBuffer
20+
%13 = OpConstant %6 0
21+
%3 = OpVariable %9 Input
22+
%1 = OpFunction %10 None %11
23+
%14 = OpLabel
24+
%15 = OpInBoundsAccessChain %12 %2 %13
25+
%16 = OpLoad %8 %3
26+
%17 = OpCompositeExtract %6 %16 0
27+
OpStore %15 %17
28+
OpNoLine
29+
OpReturn
30+
OpFunctionEnd

0 commit comments

Comments
 (0)