Skip to content

Conversation

@06wj
Copy link
Contributor

@06wj 06wj commented Nov 28, 2025

Connections
No dependencies on other PRs. Please add related issues if any.

Description
This PR adds support for GLSL specialization constants (constant_id layout qualifier) in the Naga GLSL frontend and enables the WGSL backend to output override declarations.

  1. GLSL Frontend
  • Parse layout(constant_id = N) qualifier for const declarations
  • Convert these to Naga Override instead of Constant
  • Handle Override expressions in the constant evaluator
  1. WGSL Backend
  • Remove the "pipeline constants not supported" error
  • Add write_override() method to output @id(N) override declarations
  • Support Expression::Override in expression writers
  • Implement write_override in TypeContext
  1. Naming
  • Add NameKey::Override variant for override naming

Testing

  • Add spec-constant.frag test case with various specialization constant types

Squash or Rebase?

This branch contains a single functional commit and can be rebased directly.

Checklist

  • Run cargo fmt.
  • Run taplo format.
  • Run cargo clippy --tests. If applicable, add:
    • --target wasm32-unknown-unknown
  • Run cargo xtask test to run tests.
  • If this contains user-facing changes, add a CHANGELOG.md entry.

@06wj 06wj force-pushed the glsl-constant-id-support branch from ef924a2 to 35ab6a0 Compare November 28, 2025 09:14
@06wj 06wj force-pushed the glsl-constant-id-support branch 2 times, most recently from 916411a to 291adc7 Compare December 8, 2025 06:09
Behavior::Glsl(_) => {
unreachable!()
// GLSL specialization constants (constant_id) become Override expressions
Behavior::Glsl(GlslRestrictions::Const | GlslRestrictions::Runtime(_)) => {
Copy link
Member

Choose a reason for hiding this comment

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

So, GL_KHR_vulkan_glsl allows specialization constants to be used in constants:

layout(constant_id = 18) const int scX = 1;
layout(constant_id = 19) const int scZ = 1;
const ivec3 scVec = ivec3(scX, 1, scZ);  // partially specialized vector

But does this work, would you be able to add a test for this?

I think even if we allow overrides in GlslRestrictions::Const here it won't since the validator will complain that constants use overrides in their initializers.

The GLSL frontend would have to emit overrides for all constants that (indirectly) use specialization constants.

We can of course put this off and create an issue for it, but I was just wondering.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! I've added the test case and you're right - it doesn't work correctly. The generated WGSL hardcodes the constant_id values (18, 19) instead of referencing the overrides.

const scVec: vec3<i32> = vec3<i32>(18i, 1i, 19i); 

Fixing this would require the GLSL frontend to track transitive dependencies on specialization constants. I think we should keep this PR focused on direct specialization constant usage (which works) and create a separate issue for this case.

Sound good?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that sounds fine but it seems it's misbehaving rather than it being a validation issue, can you try doing this for the const path so that we get an error instead (for now):

Behavior::Glsl(GlslRestrictions::Const(_)) => {
    Err(ConstantEvaluatorError::OverrideExpr)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

image Added the error message.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

Copy link
Member

@teoxoy teoxoy left a comment

Choose a reason for hiding this comment

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

I think this looks good! Just left a comment.

@06wj 06wj force-pushed the glsl-constant-id-support branch 2 times, most recently from c78617b to 7f3c041 Compare December 16, 2025 07:12
@06wj 06wj force-pushed the glsl-constant-id-support branch from 7f3c041 to 12e47af Compare December 16, 2025 07:18
@teoxoy teoxoy merged commit e4cfcf4 into gfx-rs:trunk Dec 16, 2025
47 checks passed
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