Files
wgpu/naga
Jamie Nicol 3bbd97cf0d [naga wgsl-in] Allow explicit cast from AbstractFloat to integer types (#7377)
And from either AbstractFloat or AbstractInt to bool.

AbstractFloat to integer was presumably not implemented as *automatic*
conversion between those types is not allowed. However, explicit
conversions (for example `i32(1.0)`) are allowed, and are implemented
during const evaluation using the same code path in
ConstantEvaluator::cast().

The integer conversion constructors treat AbstractFloats as any other
floating point type, meaning we must follow the scalar floating point
to integral conversion algorithm [1]. This specifies:

    To convert a floating point scalar value X to an integer scalar
    type T:
    * If X is a NaN, the result is an indeterminate value in T.
    * If X is exactly representable in the target type T, then the
      result is that value.
    * Otherwise, the result is the value in T closest to truncate(X)
      and also exactly representable in the original floating point
      type.

Fortunately a rust cast satisfies all these conditions apart from the
result being exactly representable in the original floating point
type. However, as i32::MIN, i32::MAX, u32::MIN, and u32::MAX are all
representable by f64 (the underlying type of AbstractFloat), this is
not an issue.

For i64 and u64 using a rust cast will not meet that requirement, but
as these types are not present in the WGSL spec we are free to ignore
that.

[1] https://www.w3.org/TR/WGSL/#floating-point-conversion
2025-03-20 15:28:30 -04:00
..
2023-10-25 14:25:04 -04:00
2025-02-24 23:39:34 -05:00
2023-10-25 14:25:04 -04:00
2023-10-25 14:25:04 -04:00
2025-01-10 17:33:58 +00:00

Naga

Matrix Crates.io Docs.rs Build Status MSRV codecov.io

The shader translation library for the needs of wgpu.

Supported end-points

Front-end Status Feature Notes
SPIR-V (binary) ✅ spv-in
WGSL ✅ wgsl-in Fully validated
GLSL 🆗 glsl-in GLSL 440+ and Vulkan semantics only
Back-end Status Feature Notes
SPIR-V ✅ spv-out
WGSL 🆗 wgsl-out
Metal ✅ msl-out
HLSL ✅ hlsl-out Shader Model 5.0+ (DirectX 11+)
GLSL 🆗 glsl-out GLSL 330+ and GLSL ES 300+
AIR
DXIL/DXIR
DXBC
DOT (GraphViz) 🆗 dot-out Not a shading language

✅ = Primary support — 🆗 = Secondary support — 🚧 = Unsupported, but support in progress

Conversion tool

Naga can be used as a CLI, which allows testing the conversion of different code paths.

First, install naga-cli from crates.io or directly from GitHub.

# release version
cargo install naga-cli

# development version
cargo install naga-cli --git https://github.com/gfx-rs/wgpu.git

Then, you can run naga command.

naga my_shader.wgsl # validate only
naga my_shader.spv my_shader.txt # dump the IR module into a file
naga my_shader.spv my_shader.metal --flow-dir flow-dir # convert the SPV to Metal, also dump the SPIR-V flow graph to `flow-dir`
naga my_shader.wgsl my_shader.vert --profile es310 # convert the WGSL to GLSL vertex stage under ES 3.20 profile

As naga includes a default binary target, you can also use cargo run without installation. This is useful when you develop naga itself or investigate the behavior of naga at a specific commit (e.g. wgpu might pin a different version of naga than the HEAD of this repository).

cargo run my_shader.wgsl

Development workflow

The main instrument aiding the development is the good old cargo test --all-features --workspace, which will run the unit tests and also update all the snapshots. You'll see these changes in git before committing the code.

If working on a particular front-end or back-end, it may be convenient to enable the relevant features in Cargo.toml, e.g.

default = ["spv-out"] #TEMP!

This allows IDE basic checks to report errors there unless your IDE is sufficiently configurable already.

Finally, when changes to the snapshots are made, we should verify that the produced shaders are indeed valid for the target platforms they are compiled for:

cargo xtask validate spv # for Vulkan shaders, requires SPIRV-Tools installed
cargo xtask validate msl # for Metal shaders, requires XCode command-line tools installed
cargo xtask validate glsl # for OpenGL shaders, requires GLSLang installed
cargo xtask validate dot # for dot files, requires GraphViz installed
cargo xtask validate wgsl # for WGSL shaders
cargo xtask validate hlsl dxc # for HLSL shaders via DXC
cargo xtask validate hlsl fxc # for HLSL shaders via FXC