Commit Graph
683 Commits
Author SHA1 Message Date
Teodor Tanasoaia 23b81da5cc [hlsl-out] polyfill float remainder operator (#7750) 2025-06-05 15:14:03 +02:00
Thierry Bergerandteoxoy 28af245d51 [wgsl-in,ir] Add support for parsing rust-style doc comments (#6364)
* [wgsl-in,ir] add support for parsing rust-style doc comments

* rename relevant items to `doc_comments` (or variations of it)

* address comments

* remove `next_until`

* rename `save_doc_comments` to `ignore_doc_comments`

* expand snapshot test and ignore blankspace when accumulating doc comments

* make tokenizer more straightforward

---------

Co-authored-by: teoxoy <28601907+teoxoy@users.noreply.github.com>
2025-06-05 15:13:11 +02:00
cryvosh bb46a7f046 [naga hlsl-out, glsl-out] Support atomicCompareExchangeWeak (#7658) 2025-06-02 13:36:44 +02:00
Andy Leiserson 6ead025a95 Remove flaky test_stack_size (#7739) 2025-05-30 22:12:31 -04:00
Andy Leiserson f34dfd90e0 [naga] Allow unreachable statements (#7718)
Allow unreachable statements after return/break/continue/discard.

Fixes #7536
2025-05-28 15:46:49 +00:00
Dmitry Zamkovandvalaphee 9c023e5e29 Implement subgroup quad ops (#7683)
* Rudimentary impl of quad ops, impl quad ops for spirv

* Impl quad swap for hlsl, msl and wgsl, finish spv front

* Cargo clippy & cargo fmt, impl valid for quad ops

* Enable quad feature

* Add missing feature to glsl

* Simplifying code by making `SubgroupQuadSwap` an instance of `SubgroupGather`

* Add `GroupNonUniformQuad` spv capability to Vulkan

* Adding GPU tests for quad operations

* Validate that broadcast operations use const invocation ids

* Added changelog entry

---------

Co-authored-by: valaphee <32491319+valaphee@users.noreply.github.com>
2025-05-26 09:32:01 +02:00
Dmitry ZamkovandAndreas Reich 44957709ff Vulkan support for SHADER_EARLY_DEPTH_TEST and fix to conservative depth optimizations (#7676)
Co-authored-by: Andreas Reich <r_andreas2@web.de>
2025-05-24 09:52:39 +00:00
Robert Bamler 8969965978 [naga] Vectorize [un]pack4x{I, U}8[Clamp] on msl
Implements more direct conversions between 32-bit integers and 4x8-bit
integer vectors using bit casting to/from `packed_[u]char4` when on
MSL 2.1+ (older versions of MSL don't seem to support these bit casts).

- `unpack4x{I, U}8(x)` becomes `[u]int4(as_type<packed_[u]char4>(x))`;
- `pack4x{I, U}8(x)` becomes `as_type<uint>(packed_[u]char4(x))`; and
- `pack4x{I, U}8Clamp(x)` becomes
  `as_type<uint>(packed_uchar4(metal::clamp(x, 0, 255)))`.

These bit casts match the WGSL spec for these functions because Metal
runs on little-endian machines.
2025-05-23 16:08:26 +02:00
Robert Bamler b32eb4a120 [naga] Vectorize [un]pack4x{I, U}8[Clamp] on spv
Emits vectorized SPIR-V code for the WGSL functions `unpack4xI8`,
`unpack4xU8`, `pack4xI8`, `pack4xU8`, `pack4xI8Clamp`, and
`pack4xU8Clamp` if `Capability::Int8` is available.

Exploits the following facts about SPIR-V ops:
- `SClamp`, `UClamp`, and `OpUConvert` accept vector arguments, in which
  case results are computed per component; and
- `OpBitcast` can cast between vectors and scalars, with a well-defined
  bit order that matches that required by the WGSL spec, see below.

WGSL spec for `pack4xI8` [1]:

> Component e[i] of the input is mapped to bits 8 x i through 8 x i + 7
> of the result.

SPIR-V spec for `OpBitcast` [2]:

> Within this mapping, any single component of `S` [remark: the type
> with fewer but wider components] (mapping to multiple components of
> `L` [remark: the type with more but narrower components]) maps its
> lower-ordered bits to the lower-numbered components of `L`.

[1] https://www.w3.org/TR/WGSL/#pack4xI8-builtin
[2] https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpBitcast
2025-05-23 16:08:26 +02:00
Robert Bamler d7e6a0e1fa Potentially optimize dot4{I,U}8Packed on Metal (#7653)
* Potentially optimize `dot4{I,U}8Packed` on Metal

This might allow the Metal compiler to emit faster code (but that's not
confirmed). See
<https://github.com/gpuweb/gpuweb/issues/2677#issuecomment-1713292226>
for the optimization. The limitation to Metal 2.1+ is discussed here:
<https://github.com/gfx-rs/wgpu/pull/7574#issuecomment-2835464472>.

* [naga] Factor out new part of `put_block` on msl

CI on test failed because the latest changes to `put_block` made its
stack too big. Factoring out the new code into a separate method fixes
this issue.
2025-05-21 18:47:59 +02: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
Zachary HarroldandConnor Fitzgerald 285fa48cec [naga]: Switch off of LazyLock to once_cell::racy::OnceBox (#7587)
* Switch off of `LazyLock` to a custom `RacyLock`

* Formatting

* Switch to `OnceBox` internally

* Clippy

* Simplify `trunc` usage

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

* Switch to `expect`

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

* Fix expectation conditions

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

* Update documentation

---------

Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
2025-04-30 11:12:04 -04:00
Andy Leiserson 850c3d4310 [naga] Write only the current entrypoint (#7626)
Changes the MSL and HLSL backends to support writing only a single entry
point, and uses them that way in wgpu-hal.

This is working towards a fix for #5885.

* Increase the limit in test_stack_size
2025-04-30 09:59:41 +02:00
Robert Bamler bb83976ddb Optimize dot4{I, U}8Packed for all spv versions
Emit optimized code for `dot4{I, U}8Packed` regardless of SPIR-V version
as long as the required capabilities are available. On SPIR-V < 1.6,
require the extension "SPV_KHR_integer_dot_product" for this. On
SPIR-V >= 1.6, don't require the extension because the corresponding
capabilities are part of SPIR-V >= 1.6 proper.
2025-04-28 16:37:45 +02:00
Robert Bamler 065d6546c4 Check for spv language version
When checking for capabilities in SPIR-V,
`capabilities_available == None` indicates that all capabilities are
available. However, some capabilities are not even defined for all
language versions, so we still need to check if the requested
capabilities even exist in the language version we're using.
2025-04-28 16:37:45 +02:00
Robert Bamler 5b20979e9b Use intrinsics for dot4{I, U}8Packed on spv 2025-04-28 16:37:45 +02:00
Robert Bamler fe05765602 Use intrinsics for dot4{I, U}8Packed in HLSL 2025-04-28 16:37:45 +02:00
Wouter de Bruijnandteoxoy 30b247a8d1 [naga glsl-out] Differentiate between support for std140 and std430 (#7579)
* [naga glsl-out] Differentiate between support for `std140` and `std430` layout, and emit `std140` in Uniforms when possible

* [naga glsl-out] Remove storage buffer std140 layout fallback, and error when we are unable to assign an explicit memory layout for uniform and storage globals

Co-authored-by: teoxoy <28601907+teoxoy@users.noreply.github.com>

---------

Co-authored-by: teoxoy <28601907+teoxoy@users.noreply.github.com>
2025-04-26 13:55:02 +02:00
Jim Blandy 5bc3c9da3c [naga] Constrain textureLoad array index args to i32 or u32.
In `naga::valid::expression`, require that
`Expression::ImageLoad::array_index` be either `i32` or `u32`, not
just an integral type of any size.
2025-04-24 13:45:12 -07:00
Jim Blandy 7c5f705580 [naga] Allow textureLoad's sample index arg to be unsigned.
Permit the `sample_index` argument of `textureLoad` overloads to be
either `i32` or `u32`.
2025-04-24 13:45:12 -07:00
Zachary Harrold 35f131ff10 Remove default features from petgraph (#7591) 2025-04-23 03:30:36 +00:00
Jim Blandy 5573c57393 [naga wgsl-in] Properly convert arguments to atomic operations. (#7573) 2025-04-21 12:27:02 -04:00
Zachary Harrold f79cf6ed23 Remove indexmap/std from wgsl-in (#7586) 2025-04-21 10:57:01 -04:00
Phena Ildanach d2ab981a76 [naga spv-in] Update supported capabilities list (#7575) 2025-04-18 21:26:45 +00:00
bf8cc43a15 Add stderr and termcolor features to Naga (#7482)
Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
Co-authored-by: Erich Gubler <erichdongubler@gmail.com>
2025-04-18 21:06:22 +00:00
Jim Blandy a9a3ea3a41 [naga wgsl-in] Convert textureStore values correctly.
Apply necessary automatic conversions to the `value` argument of
`textureStore`.
2025-04-18 12:56:51 -07:00
Jim Blandy de1b9a0899 [naga] Reduce indentation in ImageLoad expression validation.
In `naga::valid::expression::validate_expression`, use a let-else
statement so that the outermost control flow follows the success path,
and errors are close to the conditions that trigger them.
2025-04-18 12:56:51 -07:00
Jim Blandy 5304c3ca4a [naga wgsl-in] Apply automatic conversions to sampling arguments.
Properly apply WGSL's automatic conversions to the arguments
to texture sampling functions.

Introduce helper function `Lowerer::expression_with_leaf_scalar`.

Although this can't affect behavior, use
`Lowerer::expression_for_abstract` for the `image` and `sampler`
arguments, simply because we want to move away from
`Lower::expression`'s automatic concretization, and move towards
having callers say explicitly what sort of conversions they need.

Although this can't affect behavior, use
`Lowerer::expression_with_leaf_scalar` for the `offset` argument, so
that the code spells out that this requires an `i32` value, rather
than depending on blind concretization giving it that.

Continue to use `Lowerer::expression` for `gather` and `array_index`,
since those happen to behave correctly with blind concretization, and
can be cleaned up later.

Fixes #7427.
2025-04-18 11:56:34 -07:00
Jim Blandy 6cd6561884 [naga wgsl-in] Minor cleanup to Lowerer::texture_sample_helper.
Use deferred initialization instead of building tuples, so that match
arms can provide values by name, not by position within a tuple.
2025-04-18 11:56:34 -07:00
Erich Gubler c7330a8b49 style(naga): add whitespace b/w items in naga::error (#7558) 2025-04-17 00:47:33 -04:00
Erich Gubler 65c06c2b21 feat(naga): add pack4x{I,U}8Clamp built-ins 2025-04-16 15:07:11 -04:00
Erich Gubler 2aa8a830fb refactor(msl-out): Pack4x{I,U}8: make was_signed use `matches!(…) 2025-04-16 15:07:11 -04:00
Erich Gubler 97c30046bc refactor(glsl-out): Pack4x{I,U}8: make was_signed use `matches!(…) 2025-04-16 15:07:11 -04:00
Erich Gubler bb12f6287f refactor(naga): factor out write_arg 2025-04-16 15:07:11 -04:00
Jim Blandy fd4eb67877 [naga wgsl-in] Try out new use convention.
Change `naga::front::wgsl::lower` to `use` `ir` and `proc`,
and then qualify identifiers with that instead of `crate`.

This is strictly fiddling with `use` directives and name
qualification, with the concomitant formatting changes; there should
be no change to the module's behavior or meaning.
2025-04-16 08:25:14 -07: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
Andy Leiserson 09178f2400 [naga] Track globals used directly by a named expression (#7540) 2025-04-15 15:01:19 -04:00
Teodor Tanasoaia ffc920b9ae [hlsl-out] error on unimplemented non-struct push constants (#7545) 2025-04-15 17:12:28 +00:00
Teodor Tanasoaia 3805768cb4 [naga] mark readonly_and_readwrite_storage_textures & packed_4x8_integer_dot_product language extensions as implemented (#7543) 2025-04-15 09:30:56 -04: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
Vecvec 65c56fdee4 [spv-out] Prevent ray intersection functions overwriting each other. (#7497) 2025-04-11 11:18:45 +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 19429a1dc9 [naga] Refactor BlockContext type resolution methods
Change `resolve_type` and `resolve_type_impl` to return
`TypeResolution`s. Add a new method `resolve_type_inner` that returns a
`TypeInner` (i.e. what `resolve_type` used to do).
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
Jamie Nicol 08d3bb5196 [naga msl-out hlsl-out] Ensure wrapper functions for integer division and modulo avoid using minimum value literals
As we know that minimum value integer literals can cause problems for
some compilers. (See #7437)

Make the code which generates these functions call
msl::Writer::put_literal() and hlsl::Writer::write_literal()
respectively to output the minimum value integer literals instead of
just writing them directly, ensuring we only have to handle this
workaround in a single location (per backend).
2025-04-10 10:36:27 -04:00
Jamie Nicol 6dc3b7186c [naga hlsl-out] Emit most-negative integer value literals as 2nd-most-negative value minus one
As we did for MSL in #7437. eg `-2147483648` becomes `-2147483647 - 1`.

Neither FXC nor DXC currently have any issues parsing the most negative
value literals. However, we have been informed this is not guaranteed to
always be the case, so are making this change as a precaution.
2025-04-10 10:36:27 -04: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