Skip to content

Commit

Permalink
Add specialization constants via `#[spirv(spec_constant(id = 123))] x…
Browse files Browse the repository at this point in the history
…: u32` entry-point inputs.
  • Loading branch information
eddyb committed Jul 19, 2023
1 parent 4252427 commit 6fd2341
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 67 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added ⭐
- [PR#1081](https://github.com/EmbarkStudios/rust-gpu/pull/1081) added the ability
to access SPIR-V specialization constants (`OpSpecConstant`) via entry-point
inputs declared as `#[spirv(spec_constant(id = ..., default = ...))] x: u32`
(see also [the `#[spirv(spec_constant)]` attribute documentation](docs/src/attributes.md#specialization-constants))
- [PR#1080](https://github.com/EmbarkStudios/rust-gpu/pull/1080) added `debugPrintf`-based
panic reporting, with the desired behavior selected via `spirv_builder::ShaderPanicStrategy`
panic reporting, with the desired behavior selected via `spirv_builder::ShaderPanicStrategy`
(see its documentation for more details about each available panic handling strategy)

### Changed 🛠
Expand Down
17 changes: 16 additions & 1 deletion crates/rustc_codegen_spirv/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ pub enum IntrinsicType {
Matrix,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct SpecConstant {
pub id: u32,
pub default: Option<u32>,
}

// NOTE(eddyb) when adding new `#[spirv(...)]` attributes, the tests found inside
// `tests/ui/spirv-attr` should be updated (and new ones added if necessary).
#[derive(Debug, Clone)]
Expand All @@ -87,6 +93,7 @@ pub enum SpirvAttribute {
Flat,
Invariant,
InputAttachmentIndex(u32),
SpecConstant(SpecConstant),

// `fn`/closure attributes:
BufferLoadIntrinsic,
Expand Down Expand Up @@ -121,6 +128,7 @@ pub struct AggregatedSpirvAttributes {
pub flat: Option<Spanned<()>>,
pub invariant: Option<Spanned<()>>,
pub input_attachment_index: Option<Spanned<u32>>,
pub spec_constant: Option<Spanned<SpecConstant>>,

// `fn`/closure attributes:
pub buffer_load_intrinsic: Option<Spanned<()>>,
Expand Down Expand Up @@ -211,6 +219,12 @@ impl AggregatedSpirvAttributes {
span,
"#[spirv(attachment_index)]",
),
SpecConstant(value) => try_insert(
&mut self.spec_constant,
value,
span,
"#[spirv(spec_constant)]",
),
BufferLoadIntrinsic => try_insert(
&mut self.buffer_load_intrinsic,
(),
Expand Down Expand Up @@ -300,7 +314,8 @@ impl CheckSpirvAttrVisitor<'_> {
| SpirvAttribute::Binding(_)
| SpirvAttribute::Flat
| SpirvAttribute::Invariant
| SpirvAttribute::InputAttachmentIndex(_) => match target {
| SpirvAttribute::InputAttachmentIndex(_)
| SpirvAttribute::SpecConstant(_) => match target {
Target::Param => {
let parent_hir_id = self.tcx.hir().parent_id(hir_id);
let parent_is_entry_point =
Expand Down
Loading

0 comments on commit 6fd2341

Please sign in to comment.