Skip to content
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

Add specialization constants via #[spirv(spec_constant(id = 123))] x: u32 entry-point inputs. #1081

Merged
merged 2 commits into from
Jul 21, 2023

Conversation

eddyb
Copy link
Contributor

@eddyb eddyb commented Jul 19, 2023

Quoting some of the documentation I added to docs/src/attributes.md:

Entry point inputs also allow access to SPIR-V "specialization constants",
which are each associated with an user-specified numeric "ID" (SPIR-V SpecId),
used to override them later ("specializing" the shader):

If a "specialization constant" is not overriden, it falls back to its default
value, which is either user-specified (via default = ...), or 0 otherwise.

While only "specialization constants" of type u32 are currently supported, it's
always possible to manually create values of other types, from one or more u32s.

Example:

#[spirv(vertex)]
fn main(
    // Default is implicitly `0`, if not specified.
    #[spirv(spec_constant(id = 1))] no_default: u32,

    // IDs don't need to be sequential or obey any order.
    #[spirv(spec_constant(id = 9000, default = 123))] default_123: u32,

    // Assembling a larger value out of multiple `u32` is also possible.
    #[spirv(spec_constant(id = 100))] x_u64_lo: u32,
    #[spirv(spec_constant(id = 101))] x_u64_hi: u32,
) {
    let x_u64 = ((x_u64_hi as u64) << 32) | (x_u64_lo as u64);
}

There's not much else to it, quite bare-bones, but I wanted to have something for @VZout before the release.

@eddyb
Copy link
Contributor Author

eddyb commented Jul 20, 2023

To test this for myself, I made it so that in example-runner-ash, you can now NumPad +/- to control sky-shader's "sun intensity", through a new specialization constant (and yes, it will rebuild the pipeline, and require recompiling the shaders in the driver, etc., every single time).

low "sun intensity" high "sun intensity"
image image

@eddyb eddyb enabled auto-merge (rebase) July 20, 2023 18:43
@eddyb eddyb requested review from repi, VZout and fu5ha and removed request for fu5ha and VZout July 21, 2023 08:09
Copy link
Member

@fu5ha fu5ha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool :D

@eddyb eddyb merged commit 4c6cf0d into EmbarkStudios:main Jul 21, 2023
7 checks passed
@eddyb eddyb deleted the spec-const branch July 21, 2023 11:25
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.

2 participants