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

WGSL: const declarations in function scope cannot be used as const expressions #6720

Open
ErichDonGubler opened this issue Dec 13, 2024 · 2 comments
Labels
area: naga front-end lang: WGSL WebGPU Shading Language naga Shader Translator type: bug Something isn't working

Comments

@ErichDonGubler
Copy link
Member

Description

const-declared values in function scope (i.e., "local" const mySize = 2 inside a fn declaration) cannot be used in contexts where values must be constant-evaluatable, like array sizes.

Repro steps

  1. Observe that while this passes validation in WGSL:

    const myArraySize = 2;
    @compute @workgroup_size(1, 1)
    fn main() {
    	var myArray: array<f32, myArraySize>;
    }

    …this does not:

    @compute @workgroup_size(1, 1)
    fn main() {
    	const myArraySize = 2; // Hello, I moved here!
    	var myArray: array<f32, myArraySize>;
    }

    …which returns an error from naga-cli:

    error: this operation is not supported in a const context
      ┌─ ../in.wgsl:4:26
      │
    4 │     var myArray: array<f32, myArraySize>;
      │                             ^^^^^^^^^^^ operation not supported here
    

Expected vs observed behavior

I expect that values bound to const declarations should be usable as a const expression in the same contexts, independent of where they are declared (lexical scoping notwithstanding).

@ErichDonGubler ErichDonGubler added the type: bug Something isn't working label Dec 13, 2024
@cwfitzgerald cwfitzgerald added naga Shader Translator area: naga front-end lang: WGSL WebGPU Shading Language labels Dec 13, 2024
@sagudev
Copy link
Contributor

sagudev commented Dec 13, 2024

Indeed, I will check whats wrong.

@sagudev
Copy link
Contributor

sagudev commented Dec 13, 2024

The problem is that we pass global ctx when resolving type:

v.ty.map(|ast| self.resolve_ast_type(ast, &mut ctx.as_global()))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: naga front-end lang: WGSL WebGPU Shading Language naga Shader Translator type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants