Skip to content

Commit

Permalink
subgroup: Add subgroup invocation id and subgroup size builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
exrook committed Oct 1, 2023
1 parent 4f2a204 commit a80fa00
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4171,6 +4171,9 @@ const fn glsl_built_in(
Bi::WorkGroupId => "gl_WorkGroupID",
Bi::WorkGroupSize => "gl_WorkGroupSize",
Bi::NumWorkGroups => "gl_NumWorkGroups",
// subgroup
Bi::SubgroupInvocationId => "gl_SubgroupInvocationID",
Bi::SubgroupSize => "gl_SubgroupSize",
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/back/hlsl/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,12 @@ impl crate::BuiltIn {
// to this field will get replaced with references to `SPECIAL_CBUF_VAR`
// in `Writer::write_expr`.
Self::NumWorkGroups => "SV_GroupID",
Self::BaseInstance | Self::BaseVertex | Self::WorkGroupSize => {
return Err(Error::Unimplemented(format!("builtin {self:?}")))
}

Self::SubgroupInvocationId
| Self::SubgroupSize
| Self::BaseInstance
| Self::BaseVertex
| Self::WorkGroupSize => return Err(Error::Unimplemented(format!("builtin {self:?}"))),
Self::PointSize | Self::ViewIndex | Self::PointCoord => {
return Err(Error::Custom(format!("Unsupported builtin {self:?}")))
}
Expand Down
3 changes: 3 additions & 0 deletions src/back/msl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@ impl ResolvedBinding {
Bi::WorkGroupId => "threadgroup_position_in_grid",
Bi::WorkGroupSize => "dispatch_threads_per_threadgroup",
Bi::NumWorkGroups => "threadgroups_per_grid",
// subgroup
Bi::SubgroupInvocationId => "simdgroup_index_in_threadgroup",
Bi::SubgroupSize => "simdgroups_per_threadgroup",
Bi::CullDistance | Bi::ViewIndex => {
return Err(Error::UnsupportedBuiltIn(built_in))
}
Expand Down
18 changes: 18 additions & 0 deletions src/back/spv/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,24 @@ impl Writer {
Bi::WorkGroupId => BuiltIn::WorkgroupId,
Bi::WorkGroupSize => BuiltIn::WorkgroupSize,
Bi::NumWorkGroups => BuiltIn::NumWorkgroups,
// Subgroup
Bi::SubgroupInvocationId => {
self.require_any(
"`subgroup_invocation_id` built-in",
&[spirv::Capability::GroupNonUniform],
)?;
BuiltIn::SubgroupLocalInvocationId
}
Bi::SubgroupSize => {
self.require_any(
"`subgroup_invocation_id` built-in",
&[
spirv::Capability::GroupNonUniform,
spirv::Capability::SubgroupBallotKHR,
],
)?;
BuiltIn::SubgroupSize
}
};

self.decorate(id, Decoration::BuiltIn, &[built_in as u32]);
Expand Down
2 changes: 2 additions & 0 deletions src/back/wgsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,8 @@ fn builtin_str(built_in: crate::BuiltIn) -> Result<&'static str, Error> {
Bi::SampleMask => "sample_mask",
Bi::PrimitiveIndex => "primitive_index",
Bi::ViewIndex => "view_index",
Bi::SubgroupInvocationId => "subgroup_invocation_id",
Bi::SubgroupSize => "subgroup_size",
Bi::BaseInstance
| Bi::BaseVertex
| Bi::ClipDistance
Expand Down
3 changes: 3 additions & 0 deletions src/front/wgsl/parse/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub fn map_built_in(word: &str, span: Span) -> Result<crate::BuiltIn, Error<'_>>
"local_invocation_index" => crate::BuiltIn::LocalInvocationIndex,
"workgroup_id" => crate::BuiltIn::WorkGroupId,
"num_workgroups" => crate::BuiltIn::NumWorkGroups,
// subgroup
"subgroup_invocation_id" => crate::BuiltIn::SubgroupInvocationId,
"subgroup_size" => crate::BuiltIn::SubgroupSize,
_ => return Err(Error::UnknownBuiltin(span)),
})
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ pub enum BuiltIn {
WorkGroupId,
WorkGroupSize,
NumWorkGroups,
// subgroup
SubgroupInvocationId,
SubgroupSize,
}

/// Number of bytes per scalar.
Expand Down
11 changes: 11 additions & 0 deletions src/valid/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,17 @@ impl VaryingContext<'_> {
width,
},
),
Bi::SubgroupInvocationId | Bi::SubgroupSize => (
match self.stage {
St::Compute | St::Fragment => !self.output,
St::Vertex => false,
},
*ty_inner
== Ti::Scalar {
kind: Sk::Uint,
width,
},
),
};

if !visible {
Expand Down

0 comments on commit a80fa00

Please sign in to comment.