Commit Graph
179 Commits
Author SHA1 Message Date
Erich Gubler 18f4ed66c0 chore: satisfy unused_parens lint (#8424) 2025-10-23 18:35:18 -04:00
SamandSelkoSays 6e76a98c99 [naga const-eval] LiteralVector and some demo builtins (#8223)
* [naga] Add `LiteralVector` and `match_literal_vector!`

Co-authored-by: SelkoSays <70065171+SelkoSays@users.noreply.github.com>
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>

* [naga] Implement builtins dot, length, distance, normalize in const using LiteralVector

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>

* Update snapshots

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
Co-authored-by: SelkoSays <70065171+SelkoSays@users.noreply.github.com>
2025-10-18 07:28:09 -04:00
Erich Gubler 5146b3f8ae fix(namer): normalize ":<>," characters to a snake case separator 2025-10-01 08:50:37 -04:00
Erich Gubler 90702156af fix(namer): escape, rather than strip, non-ASCII ident. characters
Escape non-ASCII identifier characters with `write!(…, "u{:04x}", …)`,
surrounding with `_` as appropriate. This solves (1) a debugging issue
where stripped characters would otherwise be invisible, and (2) failure
to re-validate that stripped identifiers didn't start with an ASCII
digit.

I've confirmed that this fixes [bug
1978197](https://bugzilla.mozilla.org/show_bug.cgi?id=1978197) on the
Firefox side.
2025-10-01 08:50:37 -04:00
Sam 084431b548 [naga] Implement atan2 in const (#8222)
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2025-09-24 11:23:25 -07:00
Kevin Reid 1c8d769b67 [naga] Introduce KeywordSet and CaseInsensitiveKeywordSet. (#8136) 2025-08-27 23:42:02 -04:00
Jamie Nicol 0448b46033 [naga] Reserve names for each plane and params buffer of external texture
Adds new `NameKey` variants `ExternalTextureGlobalVariable` and
`ExternalTextureFunctionArgument`, like their non-external-texture
cousins but additionally keyed by either being a specific plane index
or params buffer.

For each external texture global variable or function argument reserve
additional names for 3 planes and the params buffer. For Naga backends
which must represent external textures as multiple variables/arguments,
this will allow them to uniquely name each one.
2025-07-30 14:33:00 -07:00
Jamie NicolandJim Blandy 43a4d53107 [naga wgsl-in wgsl-out] WGSL support for texture_external texture type (#7822)
* [naga wgsl-in wgsl-out] WGSL support for texture_external texture type

Make wgsl-in correctly parse `texture_external` texture declarations,
and allow such textures to be used in `textureDimensions()`,
`textureSampleBaseClampToEdge()`, and `textureLoad()` function
calls. In IR these are represented by the `ImageClass::External` image
class, which is a 2D, non-multisampled, non-mipmapped, float-sampled
image.

Adds a new Capability `TEXTURE_EXTERNAL` and ensure validation rejects
shaders containing external textures if this capability flag is not
set. This capability is enabled for validation by wgpu devices which
support the `TEXTURE_EXTERNAL` feature (currently only when using the
noop backend), and by the Naga CLI when validating-only or when
outputting WGSL.

The WGSL backend can of course emit `ImageClass::External` images
directly as `texture_external` textures. Other backends are, for now,
unimplemented.

Lastly, we add a snapshot test covering all the valid uses of a
texture_external texture. These are:
  - As a global variable declaration
  - As an argument to the built-in functions `textureDimensions()`,
    `textureSampleBaseClampToEdge()`, and `textureLoad()`
  - As an argument to user-defined function declarations and calls.

We keep these in their own test so that we can control which targets
to run them against (currently WGSL and IR). When external textures
are supported by all Naga backends we can, if so inclined, integrate
these with existing texture tests.

* fixup! [naga wgsl-in wgsl-out] WGSL support for texture_external texture type

* fixup! [naga wgsl-in wgsl-out] WGSL support for texture_external texture type

---------

Co-authored-by: Jim Blandy <jimb@red-bean.com>
2025-07-22 14:38:32 -07:00
Andy Leiserson 942e59eabc [naga] Enforce a maximum size for any type (#7950)
* [naga] Enforce a maximum size for any type

Fixes #4339
Fixes #7383

* Doc fix and changelog entry
2025-07-21 13:52:50 +02:00
Andy Leiserson 9a596fa1dc Fix warnings from the nightly Rust compiler (#7964) 2025-07-17 16:08:06 -04:00
Connor Fitzgerald ff0de91ad7 Bump REPO_MSRV to 1.88 (#7960) 2025-07-17 13:00:40 -04:00
Phena Ildanach 1e031e7a02 [naga spv-in] Add support for Memory Barriers 2025-06-16 10:22:46 +02:00
Erich Gubler db3c35db90 fix(naga): properly impl. auto. type conv. for select 2025-06-11 21:55:02 +09:00
Erich Gubler 77def411c4 fix(const_eval): allow casts from AbstractInt to itself (#7657) 2025-06-11 09:06:20 +00:00
Zachary HarroldandConnor Fitzgerald f04391d916 [naga]: Add no_std polyfill for round_ties_even for f32 and f64 (#7585)
* Rely on `libm` for a `no_std` alternative to `round_ties_even`

Update comments around `no_std` CI task

* Update Cargo.toml

* Feedback

Co-Authored-By: Connor Fitzgerald <connorwadefitzgerald@gmail.com>

---------

Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
2025-05-08 09:59:19 -04:00
Erich Gubler 65c06c2b21 feat(naga): add pack4x{I,U}8Clamp built-ins 2025-04-16 15:07:11 -04:00
bernhlandErich Gubler 06ee718ddd [naga] Allow Scalar for MathFunction::Distance (#7530)
Co-authored-by: Erich Gubler <erichdongubler@gmail.com>
2025-04-16 08:24:32 -07:00
Connor Fitzgerald e45013645f [naga] Use racy::OnceBox for Namer::default (#7517) 2025-04-11 14:12:25 -04:00
Robert Bamler 7a66927460 Const eval of dot4I8Packed and dot4U8Packed
Prompted by rebasing on #7487.
2025-04-11 13:32:10 +02:00
Robert Bamler c7d0af156d Implement dot4I8Packed and dot4U8Packed
Closes #7481.

This implementation roughly follows approach 2 outlined in #7481, i.e.,
it adds a polyfill for the signed and unsigned dot product of packed
vectors for each platform. It doesn't use the specialized instructions
that are available for this operation on SPIR-V (with capability
DotProductInput4x8BitPacked).
2025-04-11 13:32:10 +02:00
Jim Blandy 22c4033cb3 [naga] Properly evaluate abs(most negative abstract int).
Have constant evaluator use `wrapping_abs` instead of `abs` when
applying `MathFunction::Abs` to AbstractInt values. WGSL says that
applying `abs` to the most negative AbstractInt value must return it
unchanged, which is what Rust's `i64::wrapping_abs` does.
2025-04-10 22:59:22 -04:00
Andy Leiserson 2a2c851c40 [naga] Two structs with the same members are not equivalent
Fixes #5796
2025-04-10 19:11:56 +02:00
Andy Leiserson 14b5838a00 [naga] Rename TypeInner::equivalent to non_struct_equivalent 2025-04-10 19:11:56 +02:00
Andy LeisersonandErich Gubler a0dbe5ebc6 refactor(msl-out): create a type for bounds check iter items
Co-Authored-By: Erich Gubler <erichdongubler@gmail.com>
2025-04-09 18:45:27 -04:00
Andy Leiserson a7afb56276 [msl-out] Fix ReadZeroSkipWrite bounds check mode for pointer arguments
Fixes #4541

--
Co-authored-by: Liam Murphy <liampm32@gmail.com>
Co-Authored-By: Erich Gubler <erichdongubler@gmail.com>
2025-04-09 18:45:27 -04:00
Andy Leiserson 587aea2da6 refactor(msl-out): extract bounds_check_iter helper
--
Co-authored-by: Liam Murphy <liampm32@gmail.com>
Co-Authored-By: Erich Gubler <erichdongubler@gmail.com>
2025-04-09 18:45:27 -04:00
Andy Leiserson be16f8ce18 Use ty.for_debug 2025-04-09 08:15:47 -07:00
Andy Leiserson d223bb582f [naga] Disallow logical operators && and || on vectors
Fixes #6856
2025-04-09 08:15:47 -07:00
Jim Blandy b5f6547934 [naga] Exhaustively match MathFunction in the constant evaluator. (#7487) 2025-04-07 16:54:15 -04:00
Jim Blandy 7da699608d [naga] Support MathFunction overloads correctly.
Define a new trait, `proc::builtins::OverloadSet`, for types that
represent a Naga IR builtin function's set of overloads. The
`OverloadSet` trait includes operations needed to validate calls,
choose automatic type conversions, and generate diagnostics.

Add a new function, `ir::MathFunction::overloads`, which returns the
given `MathFunction`'s set of overloads as an `impl OverloadSet`
value. Use this in the WGSL front end, the validator, and the
typifier.

To support `MathFunction::overloads`, provide two implementations
of `OverloadSet`:

- `List` is flexible but verbose.

- `Regular` is concise but more restrictive.

Some snapshot output is affected because `TypeResolution::Handle`
values turn into `TypeResolution::Value`, since the function database
constructs the return type directly.

To work around #7405, avoid offering abstract-typed overloads of some
functions.

This addresses #6443 for `MathFunction`, although that issue covers
other categories of operations as well.
2025-04-02 18:27:19 -04:00
Jim Blandy a8a49993c2 [naga] Add TypeInner::scalar_for_conversions helper function.
Define a new helper function on `naga::ir::TypeInner`,
`scalar_for_conversions`, that returns the leaf scalar of a type as
relevant for WGSL-style automatic conversions. Specifically, this
means that, unlike `TypeInner::scalar`, arrays are considered to have
leaf scalars if their elements do.
2025-04-02 18:27:19 -04:00
Jim Blandy e284e8055f [naga] Put Scalar::F16 constant in the right place. 2025-04-02 18:27:19 -04:00
Jim Blandy f11b3b98e3 [naga] Move TypeInner::automatically_converts_to into proc.
Make the associated function `TypeInner::automatically_converts_to`
available outside the WGSL front end by moving it into the
`proc::type_methods` module.
2025-04-02 18:27:19 -04:00
Andy Leiserson 2c777f79f0 [naga wgsl-in] vecN() constructors and let type conversions (#7367)
* Support `vecN()` constructors (fixes #7356)
* Apply automatic conversions to the initializer for `let` bindings
2025-04-02 15:52:40 +00:00
sagudev d937fabcd5 Revert: Workaround for some builtins not implemented as constant expression
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2025-04-02 07:53:13 -07:00
Andy Leiserson c7c79a0dc9 Disallow taking the address of a vector component (#7284) 2025-03-28 11:32:06 +01:00
Jamie Nicol 479173197e [naga hlsl-out msl-out spv-out] Avoid undefined behaviour when casting floats to integers
Currently we generate code to convert floating point values to integers
using constructor-style casts in HLSL, static_cast in MSL, and
OpConvertFToS/OpConvertFToU instructions in SPV. Unfortunately the
behaviour of these operations is undefined when the original value is
outside of the range of the target type.

This patch avoids undefined behaviour by first clamping the value to
be inside the target type's range, then performing the cast.
Additionally, we specifically clamp to the minimum and maximum values
that are exactly representable in both the original and the target
type, as per the WGSL spec[1]. Note that these may not be the same as
the minimum and maximum values of the target type.

We additionally must ensure we clamp in the same manner for
conversions during const evaluation. Lastly, although not part of the
WGSL spec, we do the same for casting from F64 and/or to I64 or U64.

[1] https://www.w3.org/TR/WGSL/#floating-point-conversion
2025-03-27 15:41:27 +00:00
Jamie Nicol e037f4bc48 [naga wgsl-in] Implement unary negation during const evaluation for Literal::F64 (#7431)
This not being implemented made it impossible to express negative f64
literals. This is now fixed, and we've added a test.
2025-03-26 08:18:52 -04:00
Jamie Nicol 7e0e1e2803 [naga wgsl-in] Clamp negative f16 values to 0 when converting to unsigned integers during const evaluation (#7421)
This avoids a panic due to f16::to_u32()/f16::to_u64() returning None
when the value of the f16 is <= -1.0. The correct behaviour when
converting from a floating point to an integer when the value is out
of range is to clamp to the nearest value that is representable in
both the source and destination type. ie zero for negative numbers.
2025-03-26 09:04:56 +00:00
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
Bruce Mitchener edd0ef80af Fix clippy::manual_repeat_n lints (#7394) 2025-03-22 16:18:34 +00: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
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
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
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 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
Connor Fitzgerald da7deb4381 Cache reserved keywords (#7338) 2025-03-18 14:26:45 -04: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
Jamie Nicol 6a61e62f55 [naga wgsl-in] Handle automatic type conversions for switch selector and case expressions (#7250)
This allows abstract-typed expressions to be used for some or all of
the switch selector and case selectors. If these are all not
convertible to the same concrete scalar integer type we return an
error. If all the selector expressions are abstract then they are
concretized to i32.

The note previously provided by the relevant error message, suggesting
adding or removing the `u` suffix from case values, has been
removed. While useful for simple literal values, it was comically
incorrect for more complex case expressions. The error message should
still be useful enough to allow the user to easily identify the
problem.
2025-03-07 03:50:36 +00:00
Kent Slaney 2fac7fa954 [naga] Correct override resolution in array lengths.
When the user provides values for a module's overrides, rather than
replacing override-sized array types with ordinary array types (which
could require adjusting type handles throughout the module), instead
edit all overrides to have initializers that are fully-evaluated
constant expressions. Then, change all backends to handle
override-sized arrays by retrieving their overrides' values.

For arrays whose sizes are override expressions, not simple references
to a specific override's value, let front ends built array types that
refer to anonymous overrides whose initializers are the necessary
expression.

This means that all arrays whose sizes are override expressions are
references to some `Override`. Remove `naga::PendingArraySize`, and
let `ArraySize::Pending` hold a `Handle<Override>` in all cases.

Expand `tests/gpu-tests/shader/array_size_overrides.rs` to include the
test case that motivated this approach.
2025-03-06 14:21:40 -08:00