-
Notifications
You must be signed in to change notification settings - Fork 59
Add WorkgroupSize builtin support #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
LegNeato
wants to merge
1
commit into
Rust-GPU:main
Choose a base branch
from
LegNeato:wgsize
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+298
−3
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#![crate_name = "workgroup_size"] | ||
|
||
// Tests that the WorkgroupSize builtin is correctly generated as a constant. | ||
|
||
// build-pass | ||
// compile-flags: -C llvm-args=--disassemble-globals | ||
// normalize-stderr-test "OpCapability VulkanMemoryModel\n" -> "" | ||
// normalize-stderr-test "OpSource .*\n" -> "" | ||
// normalize-stderr-test "OpExtension .SPV_KHR_vulkan_memory_model.\n" -> "" | ||
// normalize-stderr-test "OpMemoryModel Logical Vulkan" -> "OpMemoryModel Logical Simple" | ||
|
||
use spirv_std::glam::UVec3; | ||
use spirv_std::spirv; | ||
|
||
#[spirv(compute(threads(8, 4, 2)))] | ||
pub fn main(#[spirv(workgroup_size)] size: UVec3, #[spirv(local_invocation_id)] local_id: UVec3) { | ||
// The workgroup_size should be (8, 4, 2) | ||
// Using the size parameter ensures it's included in the generated SPIR-V | ||
let _total = size.x + size.y + size.z + local_id.x; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
OpCapability Shader | ||
OpCapability Float64 | ||
OpCapability Int64 | ||
OpCapability Int16 | ||
OpCapability Int8 | ||
OpCapability ShaderClockKHR | ||
OpExtension "SPV_KHR_shader_clock" | ||
OpMemoryModel Logical Simple | ||
OpEntryPoint GLCompute %1 "main" %2 | ||
OpExecutionMode %1 LocalSize 8 4 2 | ||
%3 = OpString "$OPSTRING_FILENAME/workgroup-size.rs" | ||
OpName %2 "local_id" | ||
OpName %4 "size" | ||
OpName %5 "workgroup_size::main" | ||
OpDecorate %2 BuiltIn LocalInvocationId | ||
OpDecorate %4 BuiltIn WorkgroupSize | ||
%6 = OpTypeInt 32 0 | ||
%7 = OpTypeVector %6 3 | ||
%8 = OpTypePointer Input %7 | ||
%9 = OpTypeVoid | ||
%10 = OpTypeFunction %9 | ||
%2 = OpVariable %8 Input | ||
%11 = OpTypeFunction %9 %7 %7 | ||
%12 = OpConstant %6 8 | ||
%13 = OpConstant %6 4 | ||
%14 = OpConstant %6 2 | ||
%4 = OpConstantComposite %7 %12 %13 %14 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// build-pass | ||
|
||
use spirv_std::glam::UVec3; | ||
use spirv_std::spirv; | ||
|
||
#[spirv(compute(threads(8, 4, 2)))] | ||
pub fn main(#[spirv(workgroup_size)] size: UVec3, #[spirv(local_invocation_id)] local_id: UVec3) { | ||
// The workgroup_size should be (8, 4, 2) | ||
assert!(size.x == 8); | ||
assert!(size.y == 4); | ||
assert!(size.z == 2); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
tests/difftests/tests/workgroup-size/workgroup-size-rust/Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "workgroup-size-rust" | ||
edition.workspace = true | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[lib] | ||
crate-type = ["dylib"] | ||
|
||
# Common deps | ||
[dependencies] | ||
|
||
# GPU deps | ||
spirv-std.workspace = true | ||
|
||
# CPU deps | ||
[target.'cfg(not(target_arch = "spirv"))'.dependencies] | ||
difftest.workspace = true |
30 changes: 30 additions & 0 deletions
30
tests/difftests/tests/workgroup-size/workgroup-size-rust/src/lib.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#![no_std] | ||
|
||
use spirv_std::glam::UVec3; | ||
use spirv_std::spirv; | ||
|
||
#[spirv(compute(threads(8, 4, 2)))] | ||
pub fn main_cs( | ||
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] output: &mut [u32], | ||
#[spirv(workgroup_size)] workgroup_size: UVec3, | ||
#[spirv(global_invocation_id)] global_id: UVec3, | ||
#[spirv(local_invocation_id)] local_id: UVec3, | ||
) { | ||
let idx = global_id.x as usize; | ||
|
||
if idx < output.len() { | ||
// Store a value that encodes the workgroup dimensions | ||
// This allows us to verify that the workgroup_size builtin is working correctly | ||
let encoded = (workgroup_size.x << 16) | (workgroup_size.y << 8) | workgroup_size.z; | ||
|
||
// Also encode the local invocation ID to show it's within the workgroup bounds | ||
let local_encoded = (local_id.x << 16) | (local_id.y << 8) | local_id.z; | ||
|
||
// Store: encoded workgroup size in even indices, local ID in odd indices | ||
if idx % 2 == 0 { | ||
output[idx] = encoded; // Should be (8 << 16) | (4 << 8) | 2 = 0x080402 | ||
} else { | ||
output[idx] = local_encoded; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/difftests/tests/workgroup-size/workgroup-size-rust/src/main.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use difftest::config::Config; | ||
use difftest::scaffold::compute::{RustComputeShader, WgpuComputeTest}; | ||
|
||
fn main() { | ||
// Load the config from the harness. | ||
let config = Config::from_path(std::env::args().nth(1).unwrap()).unwrap(); | ||
|
||
// Define test parameters, loading the rust shader from the current crate. | ||
// Dispatch 2x2x1 workgroups with workgroup size [8, 4, 2] | ||
// This gives us a total of 2*2*1 * 8*4*2 = 256 invocations | ||
let test = WgpuComputeTest::new(RustComputeShader::default(), [2, 2, 1], 1024); | ||
|
||
// Run the test and write the output to a file. | ||
test.run_test(&config).unwrap(); | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/difftests/tests/workgroup-size/workgroup-size-wgsl/Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "workgroup-size-wgsl" | ||
edition.workspace = true | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[[bin]] | ||
name = "workgroup-size-wgsl" | ||
path = "src/main.rs" | ||
|
||
[dependencies] | ||
difftest.workspace = true |
33 changes: 33 additions & 0 deletions
33
tests/difftests/tests/workgroup-size/workgroup-size-wgsl/shader.wgsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
@group(0) @binding(0) | ||
var<storage, read_write> output: array<u32>; | ||
|
||
// Define workgroup dimensions as named constants | ||
const WORKGROUP_SIZE_X: u32 = 8u; | ||
const WORKGROUP_SIZE_Y: u32 = 4u; | ||
const WORKGROUP_SIZE_Z: u32 = 2u; | ||
|
||
@compute @workgroup_size(WORKGROUP_SIZE_X, WORKGROUP_SIZE_Y, WORKGROUP_SIZE_Z) | ||
fn main_cs( | ||
@builtin(global_invocation_id) global_id: vec3<u32>, | ||
@builtin(local_invocation_id) local_id: vec3<u32> | ||
) { | ||
let idx = global_id.x; | ||
|
||
if (idx < arrayLength(&output)) { | ||
// Use the named constants to create the workgroup_size vector | ||
let workgroup_size = vec3<u32>(WORKGROUP_SIZE_X, WORKGROUP_SIZE_Y, WORKGROUP_SIZE_Z); | ||
|
||
// Store a value that encodes the workgroup dimensions | ||
let encoded = (workgroup_size.x << 16u) | (workgroup_size.y << 8u) | workgroup_size.z; | ||
|
||
// Also encode the local invocation ID | ||
let local_encoded = (local_id.x << 16u) | (local_id.y << 8u) | local_id.z; | ||
|
||
// Store: encoded workgroup size in even indices, local ID in odd indices | ||
if (idx % 2u == 0u) { | ||
output[idx] = encoded; // Should be (8 << 16) | (4 << 8) | 2 = 0x080402 | ||
} else { | ||
output[idx] = local_encoded; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/difftests/tests/workgroup-size/workgroup-size-wgsl/src/main.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use difftest::config::Config; | ||
use difftest::scaffold::compute::{WgpuComputeTest, WgslComputeShader}; | ||
|
||
fn main() { | ||
// Load the config from the harness. | ||
let config = Config::from_path(std::env::args().nth(1).unwrap()).unwrap(); | ||
|
||
// Define test parameters, loading the wgsl shader from the crate directory. | ||
// Dispatch 2x2x1 workgroups with workgroup size [8, 4, 2] | ||
// This gives us a total of 2*2*1 * 8*4*2 = 256 invocations | ||
let test = WgpuComputeTest::new(WgslComputeShader::default(), [2, 2, 1], 1024); | ||
|
||
// Run the test and write the output to a file. | ||
test.run_test(&config).unwrap(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specialization constants seem to be supported as well, at least according to specs. Is this a temporary restriction? I'd add a TODO if it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #298 (comment) for how they work :D