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

[naga wgsl-in] Add support for unsigned types when calling textureLoad with the level parameter. #7058

Conversation

ygdrasil-io
Copy link
Contributor

@ygdrasil-io ygdrasil-io commented Feb 5, 2025

Connections
Fix #5433

Description
In WSGL, the Naga frontend returns an error when using a u32 as the level parameter in textureLoad, although this is acceptable according to the specification at https://www.w3.org/TR/WGSL/#textureload.

Testing
This issue was tested by updating the image.wsgl file, which is executed as part of the unit test suite. The test includes a call to textureLoad with a u32 parameter.

Checklist

  • Run cargo fmt.
  • Run taplo format.
  • Run cargo clippy. If applicable, add:
    • --target wasm32-unknown-unknown
  • Run cargo xtask test to run tests.
  • Add change to CHANGELOG.md. See simple instructions inside file.

@ygdrasil-io ygdrasil-io changed the title Refactor image level index type validation logic. [naga wgsl-in] Refactor image level type validation logic when calling textureLoad. Feb 5, 2025
@ygdrasil-io ygdrasil-io changed the title [naga wgsl-in] Refactor image level type validation logic when calling textureLoad. [naga wgsl-in] Add support for unsigned types when calling textureLoad with the level parameter. Feb 5, 2025
@ygdrasil-io
Copy link
Contributor Author

FYI To test I used cargo test --all-features --workspace because cargo xtask test return

cargo xtask test
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
     Running `xtask/target/debug/xtask test`
[INFO  xtask::test] Generating .gpuconfig file based on gpus on the system
   Compiling ...
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 6.85s
     Running `target/debug/wgpu-info --json -o .gpuconfig`
[INFO  xtask::test] Found 1 gpu
[INFO  xtask::test] Running cargo tests
error: no such command: `nextest`

        Did you mean `test`?

        View all installed commands with `cargo --list`
        Find a package to install `nextest` with `cargo search cargo-nextest`
Error: Tests failed

Caused by:
    command exited with non-zero code `cargo nextest run --benches --tests --no-fail-fast --all-features --retries 0`: 101

@ygdrasil-io ygdrasil-io marked this pull request as ready for review February 5, 2025 01:58
@ErichDonGubler
Copy link
Member

ErichDonGubler commented Feb 5, 2025

@ygdrasil-io: Sounds like you don't have cargo-nextest installed; you can find instructions to do so here. Our diagnostic ought to be better, though.

@ygdrasil-io
Copy link
Contributor Author

The texelFetch function in GLSL does not support unsigned integers. Should we cast the value to an integer?
https://registry.khronos.org/OpenGL-Refpages/gl4/html/texelFetch.xhtml

@ygdrasil-io
Copy link
Contributor Author

@cwfitzgerald suggested doing casting on matrix, so I implemented it with commit 546497b.

Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

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

Nice, looks generally good, a few comments

@@ -37,6 +37,7 @@ void main(uint3 local_id : SV_GroupThreadID)
uint2 dim = NagaRWDimensions2D(image_storage_src);
int2 itc = (int2((dim * local_id.xy)) % int2(10, 20));
uint4 value1_ = image_mipmapped_src.Load(int3(itc, int(local_id.z)));
uint4 value1_2_ = image_mipmapped_src.Load(int3(itc, uint(local_id.z)));
Copy link
Member

Choose a reason for hiding this comment

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

I feel like there's an implicit conversion here, and we should also wrap the argument in int as well, like we do glsl.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is done. I also did some refactoring on wgpu/naga/src/back/hlsl/writer.rs to break up the monolithic function write_expr.

@cwfitzgerald cwfitzgerald assigned cwfitzgerald and unassigned teoxoy Feb 12, 2025
@ygdrasil-io ygdrasil-io force-pushed the fix-wgsl-textureLoad-level-parameter-checking branch from eb51db5 to dcc3a54 Compare February 16, 2025 15:22
Copy link
Member

@cwfitzgerald cwfitzgerald 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 we're g2g after jimb's comment is addressed.

@ygdrasil-io
Copy link
Contributor Author

I think we're g2g after jimb's comment is addressed.

this is fixed

Copy link
Member

@cwfitzgerald cwfitzgerald left a comment

Choose a reason for hiding this comment

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

Thanks!

@cwfitzgerald
Copy link
Member

cwfitzgerald commented Feb 24, 2025

Got a conflict on image.spvasm - if you merge trunk then regenerate the files, this should resolve the conflict. Then ping me

Improve the validation logic for image level indices by matching against specific scalar types (Sint or Uint). This change ensures accurate error handling for invalid index types.
Moved HLSL image load logic into a dedicated `write_image_load` function to improve modularity and readability. Additionally, added explicit type casting for certain scalar values to ensure correctness in generated HLSL code. Updated tests to reflect these changes and added related utility updates.
Update parameter types in `write_expr` for consistency and add `#[allow(clippy::too_many_arguments)]` to suppress linter warnings. This improves readability and maintains compatibility with existing code conventions.
@ygdrasil-io ygdrasil-io force-pushed the fix-wgsl-textureLoad-level-parameter-checking branch from c94b7b4 to 00dbdf8 Compare February 24, 2025 21:00
@cwfitzgerald cwfitzgerald enabled auto-merge (squash) February 24, 2025 21:01
@cwfitzgerald cwfitzgerald merged commit 92a1702 into gfx-rs:trunk Feb 24, 2025
34 checks passed
@ygdrasil-io ygdrasil-io deleted the fix-wgsl-textureLoad-level-parameter-checking branch February 24, 2025 21:13
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.

Shader validation fail using textureLoad with unexpected type from naga
5 participants