Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

LegNeato
Copy link
Collaborator

Implement #[spirv(workgroup_size)] builtin that provides workgroup dimensions as a constant to compute shaders. The constant is created from the LocalSize execution mode values.

See https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#BuiltIn and https://github.com/KhronosGroup/SPIRV-Guide/blob/main/chapters/local_size_and_workgroup_size.md

Implement #[spirv(workgroup_size)] builtin that provides workgroup dimensions
as a constant to compute shaders. The constant is created from the LocalSize
execution mode values.

See https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#BuiltIn
and https://github.com/KhronosGroup/SPIRV-Guide/blob/main/chapters/local_size_and_workgroup_size.md
None => {
self.tcx.dcx().span_err(
attrs.builtin.as_ref().unwrap().span,
"WorkgroupSize builtin requires LocalSize execution mode to be set (e.g., #[spirv(compute(threads(x, y, z)))])",
Copy link
Contributor

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.

Copy link
Member

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

@Firestar99
Copy link
Member

Firestar99 commented Jun 27, 2025

I think it's fine the way it is, but I do have some design feedback for potential future iterations:

I'd much rather want the workgroup size be a constant, similar to what I'm doing rn:

pub const LIGHTING_WG_SIZE: u32 = 64;

const_assert_eq!(LIGHTING_WG_SIZE, 64);
#[bindless(compute(threads(64)))]
pub fn lighting_cs(...) { ... }

This allows me to access the workgroup size from the CPU code as well to compute the workgroup dimensions:

let groups = [
	(image_size.x + LIGHTING_WG_SIZE - 1) / LIGHTING_WG_SIZE,
	image_size.y,
	1,
];

And allows you to declare shared memory as a multiple of the workgroup size:

pub const DIRECTIONAL_SHADOWS_WG_SIZE: u32 = 64;
const SHARED_SIZE: usize = DIRECTIONAL_SHADOWS_WG_SIZE as usize * 2;

const_assert_eq!(DIRECTIONAL_SHADOWS_WG_SIZE, 64);
#[bindless(compute(threads(64)))]
pub fn directional_shadows(
    #[spirv(workgroup)] shared: &[f32; SHARED_SIZE],
) { ... }

Instead of this PR, I'd much rather have the proc macro accept a const expr instead of a literal, so we can do this:

pub const LIGHTING_WG_SIZE: u32 = 64;

#[bindless(compute(threads(LIGHTING_WG_SIZE)))]
pub fn lighting_cs(...) { ... }

Not sure how hard this is to implement.

@LegNeato
Copy link
Collaborator Author

I tried to do the rust const route for the past week, the problem is that rustc doesn't support constants in attrs currently so I have to do a bunch of hacky stuff with resolution to make it work. I have it working for most cases, but not all. Not sure if it is better to support it with sharp edge cases or not support it at all.

@LegNeato
Copy link
Collaborator Author

I'll clean up what I have and we can compare and contrast. At the very least we'll need to document as people coming from glsl will expect something like workgroupsize attr to exist.

@Firestar99
Copy link
Member

Firestar99 commented Jun 27, 2025

@LegNeato I wonder if we actually need to parse out the actual value within that macro....

Currently we're emitting this, for which we clearly need to parse the literals:

     OpEntryPoint GLCompute %3 "lighting_cs" %bla %bla2 %bla3
     OpExecutionMode %3 LocalSize 64 1 1
%3 = OpFunction %void None %443

But with Vulkan1.2 we get the new fancy LocalSizeId instead of LocalSize, so we can do this:

%4 = OpTypeInt 32 0
     OpDecorate %5 SpecId 123
%5 = OpSpecConstant %4 64
%6 = OpConstant %4 1
     OpEntryPoint GLCompute %3 "lighting_cs" %bla %bla2 %bla3
     OpExecutionMode %3 LocalSizeId %5 %6 %6
%3 = OpFunction %void None %443

Note how at the point we're writing the OpExecutionMode we don't actually write any literaly, just references to constants. Why couldn't they be references to actual constants in rust code? And if we have number literals, we can still parse and emit them as constants. It would also open up a path towards workgroup size being a spec constant, see %5.

We probably should move this design discussion to an issue, I just want to talk about it now in case this has any ramifications for this PR as well.

@LegNeato LegNeato marked this pull request as draft June 27, 2025 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants