Commit Graph
9055 Commits
Author SHA1 Message Date
Jim Blandy f0ff173f82 [naga] Implement constant evaluation for the cross builtin. (#7404)
Add support for `naga::ir::MathFunction::Cross` to
`naga::proc::constant_evaluator`.

In the tests:

- Change `naga/tests/in/wgsl/cross.wgsl` to use more interesting
  argument values. Rather than passing the same vector twice, which
  yields a cross product of zero, pass in the x and y unit vectors,
  whose cross product is the z unit vector. Update snapshot output.

- Replace `validation::bad_cross_builtin_args` with a new test,
  `builtin_cross_product_args`, that is written more in the style of
  the other tests in this module, and does not depend on the WGSL
  front end. Because this PR changes the behavior of the constant
  evaluator, this test stopped behaving as expected.

- In `wgsl_errors::check`, move a `panic!` out of a closure so that
  the `#[track_caller]` attribute works properly.
2025-03-24 10:55:22 +01:00
Christian Legnitto 10cd1ccd8c [spv_in] Finish emitter after function arguments (#7397)
Fixes https://github.com/gfx-rs/wgpu/issues/7358.
2025-03-24 10:36:30 +01:00
Bruce Mitchener ccaf9a84f8 Fix clippy::implicit_saturating_sub lints (#7395) 2025-03-22 13:17:34 -04:00
Bruce Mitchener edd0ef80af Fix clippy::manual_repeat_n lints (#7394) 2025-03-22 16:18:34 +00:00
Erich Gubler 1e172b2e14 test: repair multi-instance tests (#7388) 2025-03-21 13:25:31 -04:00
Connor Fitzgerald c37a005f63 Make target defaults align with "butterfly" target set (#7379) 2025-03-21 11:41:06 -04:00
Jamie NicolandErich Gubler 894d0369b5 [naga spv-out msl-out hlsl-out] Make infinite loop workaround count down instead of up (#7372)
[naga spv-out msl-out hlsl-out] Make infinite loop workaround count down instead of up

To avoid generating code containing infinite loops, and therefore
incurring the wrath of undefined behaviour, we insert a counter into
each loop that will break after 2^64 iterations. This was previously
implemented as two u32 variables counting up from zero.

We have been informed that this construct can cause certain Intel
drivers to hang. Instead, we must count down from u32::MAX. Counting
down is more fun, anyway.

Co-authored-by: Erich Gubler <erichdongubler@gmail.com>
2025-03-21 08:24:17 +00:00
Bruce Mitchener f89ede7cd8 [doc] Fix unbalanced backticks (#7385) 2025-03-21 03:03:37 +00:00
Bruce Mitchener b7e7d7ce7a Fix clippy::manual_contains lints (#7384) 2025-03-20 21:55:52 -04:00
Andy Leiserson ebf87526dd [doc] Correct the doc comment for Expression::FunctionArgument (#7382) 2025-03-20 19:29:44 -04:00
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
Connor Fitzgerald 79a5cd557c Remove vestigial snapshot outputs (#7380) 2025-03-20 15:21:18 -04:00
Connor Fitzgerald db4cb26858 Allow overriding instance flags in tests (#7374) 2025-03-20 11:35:54 -04:00
Jamie Nicol 187e1a6967 [naga wgsl-in] Reject vector constructors with incorrect number of components during const evaluation.
A vector constructor with multiple arguments is represented in IR as a
Compose expression. These can be nested, for example due to code such
as this: `vec4(1, vec3(2, vec2(3, 4)))`. The total number of
components of each argument must match the vector size, meaning
`vec4(1, 2, 3, 4, 5)` is invalid. The validation pass already catches
such occurences. However, validation runs after constant evaluation,
meaning this can cause issues if the expression is evaluated as part
of a const expression.

When applying an operation to a vector compose during const
evaluation, we typically "flatten" the vector using the function
`proc::flatten_compose()`. This silently truncates the list of
components to the size implied by the type, meaning invalid code is
accepted.

This patch validates that the total number of components in a Compose
expression, once flattened, matches the size implied by the type. We
do so once as each expression is registered to avoid needing to handle
this each time the expression is used in a const expression.

For nested compose expressions, the inner expressions will be registed
before the outer ones. This means we can trust that the size implied
by the inner expression's type is correct, as it will have already
been validated, and can therefore avoid recursing through each nested
expression when registering the outer expression.
2025-03-20 12:24:31 +00:00
Connor Fitzgerald 4687973c9a Use bytemuck instead of slice::from_raw_parts for transmutes (#7376) 2025-03-19 22:05:21 -04:00
c6286791fe feat: implement F16 support in shaders (#5701)
Co-authored-by: FL33TW00D <fleetwoodpersonal@gmail.com>
Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
Co-authored-by: ErichDonGubler <erichdongubler@gmail.com>
2025-03-19 16:26:50 +00:00
Kevin Reid b912232188 [naga] Make Level::next() public.
It doesn’t make sense to have `Level` public but not `Level::next()`.
2025-03-19 12:11:01 -04:00
Erich Gubler 47c72fa943 fix(wgsl-in,const-eval): use {float}::round_ties_even instead of polyfill for round 2025-03-19 11:21:57 -04:00
Jim Blandy 45b0df6adf [naga wgsl-in] Tweak logging in front::wgsl::lower::conversion. 2025-03-19 11:21:02 -04:00
Jim Blandy e82f52135b [naga wgsl-in] Use common type-formatting code.
Remove the `naga::front::wgsl::to_wgsl` module. Instead, use the
implementations of `naga::common::wgsl`'s `TryToWgsl` and
`TypeContext` traits.
2025-03-19 11:21:02 -04:00
Jim Blandy 3ec38a0a15 [naga wgsl] Add TryToWgsl::to_wgsl_for_diagnostics.
Add a new method to the `naga::common::wgsl::TryToWgsl` trait,
`to_wgsl_for_diagnostics`, which always returns a `String`, falling
back to the `Debug` representation of the type if necessary.

Provide a custom implementation for `Scalar` that shows abstract types
in a reasonable way.
2025-03-19 11:21:02 -04:00
Jim Blandy 62d49918fd [naga wgsl] Provide defaults for TypeContext non-WGSL methods.
Provide default implementations for the `write_non_wgsl_inner` and
`write_non_wgsl_scalar` methods of `naga::common::wgsl::TypeContext`.

We will be implementing this trait for various types in the WGSL front
end and the validator, and almost all of these applications will need
basically identical implementations of these methods.
2025-03-19 11:21:02 -04:00
Jim Blandy 13b8036b89 [naga wgsl] Add string creation methods to TypeContext.
Add new default methods, `type_to_string`, `type_inner_to_string`, and
`resolution_to_string` to the `naga::common::wgsl::TypeResolution`
trait, for generating WGSL source strings from these types.
2025-03-19 11:21:02 -04:00
Jim Blandy e17c0bdc17 [naga wgsl] Fix generics on naga::common::wgsl::TypeContext.
In `naga::common::wgsl::types`, move the type parameter `<W: Write>`
from the `TypeContext` trait itself to the individual methods of the
trait. It is very unlikely for Naga to need to be able to implement
`TypeContext`, but only for one particular kind of output stream.

The motivation for this change is that the old parameterization makes
it awkward to provide utility methods for generating `String`s on the
trait, which we do in subsequent commits. In order to write to a
`String`, such utility methods need `Self` to implement
`TypeContext<String>`, so you can add bounds to the methods like this:

    fn type_to_string(..., out: &mut W) -> ...
        where Self: TypeContext<String>
    {
        ... self.write(..., &mut string_buf)?;
    }

That will compile, but if you try to actually call it, Rust gets
confused. Remember, the above is not a method on
`TypeContext<String>`, it's a method on `TypeContext<W>` that uses
`TypeContext<String>` internally. So when you write
`ctx.type_to_string(...)`, Rust needs to decide whether `ctx`
implements `TypeContext<W>` for some completely unspecified `W`, and
asks for type annotations.

You could supply type annotations, but this would be basically
supplying some never-used type that implements `core::fmt::Write`.
Instead of

    ctx.type_to_string(handle)

you'd have to write

    TypeContext::<String>::type_to_string(ctx, handle)

which is dumb.

(I don't *think* this explanation belongs in the code, since it's an
explanation of a design *not* used, replaced by a design that's pretty
natural --- so I'll leave it here.)
2025-03-19 11:21:02 -04:00
Jim Blandy fc6d6c6483 [naga wgsl] Add write_type_resolution method to TypeContext.
Add a new default method, `write_type_resolution`, to the
`naga::common::wgsl::TypeResolution` trait, for writing
`TypeResolution` values as WGSL.
2025-03-19 11:21:02 -04:00
Jim Blandy 3b401670ae [naga wgsl-in] Clean up use directives in front::wgsl::error.
Put the `use` directives at the top of `front::wgsl::error` in the
usual order:
- identifiers defined by this crate itself
- identifiers defined  by other crates
- identifiers from the standard library

This commit should cause no change in behavior.
2025-03-19 11:21:02 -04:00
Jim Blandy 026628a739 [naga wgsl] Make formatting of non-WGSL types more flexible.
Extend the `naga::common::wgsl::TypeContext` trait with new methods,
`write_non_wgsl_inner` and `write_non_wgsl_scalar`, which are called
when `write_type`, `write_type_inner`, or `write_scalar` are presented
with Naga IR that cannot be expressed in WGSL. Delegating these cases
lets most uses of `unreachable!` be removed.

In the WGSL backend, implement `write_non_wgsl_inner` and
`write_non_wgsl_scalar` to call `unreachable!`, preserving existing
behavior.
2025-03-19 11:21:02 -04:00
Jim Blandy 2c4d2693f0 [naga wgsl] Move Scalar formatting into its own function.
In `naga::common::wgsl::types`, remove `unwrap_to_wgsl` from
`try_write_type_inner` and replace its uses with calls to a new trait
method, `write_scalar`, with a default definition. This should have no
effect on externally visible behavior, but it makes subsequent commits
easier to read.
2025-03-19 11:21:02 -04:00
Jim Blandy a79111922a [naga wgsl] Move guts of TypeInner formatting to a function.
Move the guts of the `naga::common::wgsl::TypeContext` trait's
`write_type_inner` method into a new function, `try_write_type_inner`,
private to the module.

This commit is only code motion; there should be no change to
behavior. Subsequent commits will introduce a private error type, with
the new function serving as a `try` block.
2025-03-19 11:21:02 -04:00
Jim Blandy 84cbf28ed9 [naga wgsl] Move ToWgsl and TryToWgsl into their own module.
In `naga::common::wgsl`, move the `ToWgsl` and `TryToWgsl` trait
definitions and impls into their own submodule, `to_wgsl`.

This change is only code motion; there should be no change in
behavior.
2025-03-19 11:21:02 -04:00
Jim Blandy bcb0f7ef3e [naga wgsl] Move diagnostics definitions into their own module.
In `naga::common::wgsl`, move `DisplayFilterableTriggeringRule` and
diagnostics-related impls into a new sub-module, `diagonostics`.

This change is only code motion; there should be no change in
behavior.
2025-03-19 11:21:02 -04:00
Jim Blandy b85a451dc9 [naga wgsl-in] Use String in error, not Box<str>.
Use `String` in variants of `naga::front::wgsl::Error`, not
`Box<str>`.

Delete test `naga::front::wgsl::error::test_error_size`. Since `Error`
is always returned boxed, its size should have no effect on `Result`
size.
2025-03-19 10:02:09 -04:00
Jim Blandy 5c9c6ea093 [naga wgsl-in] Box error values in Results.
Change the definition of `naga::front::wgsl::Result` to carry
a boxed `Error`, so that the size of `Error` does not affect
performance of success paths.
2025-03-19 10:02:09 -04:00
Jim Blandy 77fcca7522 [naga wgsl-in] Introduce a Result alias for the WGSL front end.
Introduce a new type alias, `naga::front::wgsl::Result`, that is
`core::result::Result` with the error type fixed to
`naga::front::wgsl::Error`. Use this alias where appropriate.

This commit should cause no changes in behavior or interface.
2025-03-19 10:02:09 -04:00
Connor Fitzgerald 1c69416c32 Rename package of custom_backend example 2025-03-19 09:45:53 -04:00
Connor Fitzgerald a1ee2c3d0d Add CI for custom_backend example 2025-03-19 09:45:53 -04:00
Connor Fitzgerald 516459efcd Fix custom_backend compilation error when compiled alone 2025-03-19 09:45:53 -04:00
Connor Fitzgerald c223d3544b Decouple custom_backend example from the workspace 2025-03-19 09:45:53 -04:00
Connor Fitzgerald f109a6966a Rename custom_backend example 2025-03-19 09:45:53 -04:00
Kevin Reid a48387756c Add a runnable example to the Naga crate documentation. 2025-03-18 17:06:04 -07:00
Kevin Reid ee78d61390 Move Naga IR types from naga/src/lib.rs to naga/src/ir/mod.rs.
The types remain available at their original paths by `pub use ir::*`.
This may or may not be a temporary measure for migration.

The only changes to the moved code are:
* Fixing doc links.
* Moving `pub use block::Block` to the top.
2025-03-18 17:06:04 -07:00
Erich Gubler 4db992a66d refactor(core): avoid manually reimplemented sort_by_key (#7366) 2025-03-18 17:43:04 -04:00
Connor Fitzgerald da7deb4381 Cache reserved keywords (#7338) 2025-03-18 14:26:45 -04:00
Connor Fitzgerald 5ac906645b Move missing snapshots 2025-03-18 10:26:31 -07:00
Andy Leiserson 8474132bd2 [tests] Test case for multiple bindings with different sizes (#7360)
* [tests] Test case for multiple bindings with different sizes (#7359)

* Fix clippy
2025-03-17 18:16:21 -04:00
Kevin Reid 3e4d24ea98 Remove dead code from naga::back. (#7350)
The `#![allow(dead_code)]` on the module was hiding dead code in the
child modules. I changed it to be conditional on actually having no
backends enabled. (Note that with #7349, there would actually be no
warnings and we could remove the `allow(dead_code)` entirely, but I
expect that state of affairs won’t necessarily persist.)
2025-03-17 08:23:51 -04:00
dependabot[bot] d55bb2956a chore(deps): bump crate-ci/typos from 1.30.1 to 1.30.2 (#7351)
Bumps [crate-ci/typos](https://github.com/crate-ci/typos) from 1.30.1 to 1.30.2.
- [Release notes](https://github.com/crate-ci/typos/releases)
- [Changelog](https://github.com/crate-ci/typos/blob/master/CHANGELOG.md)
- [Commits](https://github.com/crate-ci/typos/compare/v1.30.1...v1.30.2)

---
updated-dependencies:
- dependency-name: crate-ci/typos
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-17 02:58:05 -04:00
dependabot[bot] 2c8308650e chore(deps): bump the patch-updates group with 13 updates (#7352)
Bumps the patch-updates group with 13 updates:

| Package | From | To |
| --- | --- | --- |
| [libc](https://github.com/rust-lang/libc) | `0.2.170` | `0.2.171` |
| [once_cell](https://github.com/matklad/once_cell) | `1.20.3` | `1.21.1` |
| [tokio](https://github.com/tokio-rs/tokio) | `1.44.0` | `1.44.1` |
| [quote](https://github.com/dtolnay/quote) | `1.0.39` | `1.0.40` |
| [async-trait](https://github.com/dtolnay/async-trait) | `0.1.87` | `0.1.88` |
| [clap](https://github.com/clap-rs/clap) | `4.5.31` | `4.5.32` |
| [clap_builder](https://github.com/clap-rs/clap) | `4.5.31` | `4.5.32` |
| [clap_derive](https://github.com/clap-rs/clap) | `4.5.28` | `4.5.32` |
| [deno_terminal](https://github.com/denoland/deno_terminal) | `0.2.0` | `0.2.2` |
| [half](https://github.com/starkat99/half-rs) | `2.4.1` | `2.5.0` |
| [prettyplease](https://github.com/dtolnay/prettyplease) | `0.2.30` | `0.2.31` |
| [uuid](https://github.com/uuid-rs/uuid) | `1.15.1` | `1.16.0` |
| [winnow](https://github.com/winnow-rs/winnow) | `0.7.3` | `0.7.4` |


Updates `libc` from 0.2.170 to 0.2.171
- [Release notes](https://github.com/rust-lang/libc/releases)
- [Changelog](https://github.com/rust-lang/libc/blob/0.2.171/CHANGELOG.md)
- [Commits](https://github.com/rust-lang/libc/compare/0.2.170...0.2.171)

Updates `once_cell` from 1.20.3 to 1.21.1
- [Changelog](https://github.com/matklad/once_cell/blob/master/CHANGELOG.md)
- [Commits](https://github.com/matklad/once_cell/compare/v1.20.3...v1.21.1)

Updates `tokio` from 1.44.0 to 1.44.1
- [Release notes](https://github.com/tokio-rs/tokio/releases)
- [Commits](https://github.com/tokio-rs/tokio/compare/tokio-1.44.0...tokio-1.44.1)

Updates `quote` from 1.0.39 to 1.0.40
- [Release notes](https://github.com/dtolnay/quote/releases)
- [Commits](https://github.com/dtolnay/quote/compare/1.0.39...1.0.40)

Updates `async-trait` from 0.1.87 to 0.1.88
- [Release notes](https://github.com/dtolnay/async-trait/releases)
- [Commits](https://github.com/dtolnay/async-trait/compare/0.1.87...0.1.88)

Updates `clap` from 4.5.31 to 4.5.32
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](https://github.com/clap-rs/clap/compare/v4.5.31...clap_complete-v4.5.32)

Updates `clap_builder` from 4.5.31 to 4.5.32
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](https://github.com/clap-rs/clap/compare/v4.5.31...v4.5.32)

Updates `clap_derive` from 4.5.28 to 4.5.32
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](https://github.com/clap-rs/clap/compare/v4.5.28...v4.5.32)

Updates `deno_terminal` from 0.2.0 to 0.2.2
- [Release notes](https://github.com/denoland/deno_terminal/releases)
- [Commits](https://github.com/denoland/deno_terminal/compare/0.2.0...0.2.2)

Updates `half` from 2.4.1 to 2.5.0
- [Release notes](https://github.com/starkat99/half-rs/releases)
- [Changelog](https://github.com/starkat99/half-rs/blob/main/CHANGELOG.md)
- [Commits](https://github.com/starkat99/half-rs/compare/v2.4.1...v2.5.0)

Updates `prettyplease` from 0.2.30 to 0.2.31
- [Release notes](https://github.com/dtolnay/prettyplease/releases)
- [Commits](https://github.com/dtolnay/prettyplease/compare/0.2.30...0.2.31)

Updates `uuid` from 1.15.1 to 1.16.0
- [Release notes](https://github.com/uuid-rs/uuid/releases)
- [Commits](https://github.com/uuid-rs/uuid/compare/v1.15.1...v1.16.0)

Updates `winnow` from 0.7.3 to 0.7.4
- [Changelog](https://github.com/winnow-rs/winnow/blob/main/CHANGELOG.md)
- [Commits](https://github.com/winnow-rs/winnow/compare/v0.7.3...v0.7.4)

---
updated-dependencies:
- dependency-name: libc
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: once_cell
  dependency-type: direct:production
  update-type: version-update:semver-minor
  dependency-group: patch-updates
- dependency-name: tokio
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: quote
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: async-trait
  dependency-type: indirect
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: clap
  dependency-type: indirect
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: clap_builder
  dependency-type: indirect
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: clap_derive
  dependency-type: indirect
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: deno_terminal
  dependency-type: indirect
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: half
  dependency-type: indirect
  update-type: version-update:semver-minor
  dependency-group: patch-updates
- dependency-name: prettyplease
  dependency-type: indirect
  update-type: version-update:semver-patch
  dependency-group: patch-updates
- dependency-name: uuid
  dependency-type: indirect
  update-type: version-update:semver-minor
  dependency-group: patch-updates
- dependency-name: winnow
  dependency-type: indirect
  update-type: version-update:semver-patch
  dependency-group: patch-updates
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-17 02:56:50 -04:00
Jim Blandy 6011b0b797 [naga] Place the #[track_caller] attribute on some test utilities.
Place the `#[track_caller]` attribute on the `check` and
`validation_error` functions in the Naga `wgsl_errors` test module, so
that panics or assertion failures will point to the actual test,
not to the code in the utility function.
2025-03-16 16:56:04 -07:00
SupaMaggie70Incorporated e8ce3ae973 Add mesh shader stages to wgt::ShaderStages and naga::ShaderStage (#7292)
* Initial changes

* Fixed metal backend in wgpu-hal, ran tests
2025-03-15 05:34:20 +00:00