877 Commits

Author SHA1 Message Date
Connor Fitzgerald
3e153fb8ec Label no-op supporting tests 2025-08-01 01:54:01 -04:00
Connor Fitzgerald
df40889e1b Prepare for miri 2025-08-01 01:54:01 -04:00
Jamie Nicol
59f815a9b9 [naga] Have validation reject shaders containing binding arrays of external textures
For simplicity's sake our initial implementation of external textures
will not support binding arrays of external textures. We should
therefore reject any shaders which use them during validation.

Their implementation will be tracked in #8027.

naga/src/valid/type.rs JJ: JJ: Lines starting with "JJ:" (like this
one) will be removed.
2025-07-30 14:33:00 -07:00
Jamie Nicol
e1ccb6632c [naga hlsl-out] Implement external texture support
This adds HLSL backend support for `ImageClass::External` (ie WGSL's
`external_texture` texture type).

For each external texture global variable in the IR, we declare 3
`Texture2D` globals as well as a `cbuffer` for the params. The
additional bindings required by these are found in the newly added
`external_texture_binding_map`. Unique names for each can be obtained
using `NameKey::ExternalTextureGlobalVariable`.

For functions that contain ImageQuery::Size, ImageLoad, or ImageSample
expressions for external textures, ensure we have generated wrapper
functions for those expressions. When emitting code for the
expressions themselves, simply insert a call to the wrapper function.

For size queries, we return the value provided in the params
struct. If that value is [0, 0] then we query the size of the plane 0
texture and return that.

For load and sample, we sample the textures based on the number of
planes specified in the params struct. If there is more than one plane
we additionally perform YUV to RGB conversion using the provided
matrix.

Unfortunately HLSL does not allow structs to contain textures, meaning
we are unable to wrap the 3 textures and params struct variables in a
single variable that can be passed around.

For our wrapper functions we therefore ensure they take the three
textures and the params as consecutive arguments. Likewise, when
declaring user-defined functions with external texture arguments, we
expand the single external texture argument into 4 consecutive
arguments. (Using NameKey::ExternalTextureFunctionArgument to ensure
unique names for each.)

Thankfully external textures can only be used as either global
variables or function arguments. This means we only have to handle the
`Expression::GlobalVariable` and `Expression::FunctionArgument` cases
of `write_expr()`. Since in both cases we know the external texture
can only be an argument to either a user-defined function or one of
our wrapper functions, we can simply emit the names of the variables
for each three textures and the params struct in a comma-separated
list.
2025-07-30 14:33:00 -07: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 Nicol
9f654c6235 [naga] Generate special type for external texture params buffer
During wgsl lowering, if we encounter an external texture type then
generate the `ExternalTextureParams` struct. This will be required by
most Naga backends to implement external textures.

This type is not actually used by wgsl-in or the IR. However,
generating it in Naga IR ensures tricky details such as member
alignment are handled for us.

wgsl-out must ensure it does *not* generate code for this type, as it
handles external textures natively.
2025-07-30 14:33:00 -07:00
Jim Blandy
dbe64a769b
[naga hlsl-out] Fix odd name for a Handle<GlobalVariable>. (#7996) 2025-07-23 16:48:49 -04:00
Jim Blandy
2fcd41377c
[naga] Make Naga tests build when no features are enabled. (#7989) 2025-07-23 12:09:45 -04:00
Jim Blandy
d9a6b4fa4a [naga, wgpu-core] Add some docs for Naga validator creation. 2025-07-23 08:51:13 -07:00
Jamie Nicol
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
Teodor Tanasoaia
33b9f86cc4
Fix detection of local_invocation_id for zero initialization of workgroup memory (#7962) 2025-07-17 12:47:59 -04:00
SupaMaggie70Incorporated
dd50e56c59
Update rspirv version (#7945) 2025-07-15 00:51:31 -04:00
Andy Leiserson
1b4eca97cf [naga hlsl-out] Factor out some repetitive code 2025-07-11 16:55:46 -07:00
Andy Leiserson
bfa7ee8de5 [naga hlsl-out] Handle additional cases of Cx2 matrices
Fixes #4423
2025-07-11 16:55:46 -07:00
Andy Leiserson
0c629bb3c2
[naga] cli support for process_overrides compaction (#7792) 2025-06-26 14:12:18 -07:00
Zachary Harrold
4c39227510
Allow Naga spv-in and spv-out in no_std (#7760)
Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
2025-06-26 16:45:47 -04:00
Jamie Nicol
55a2c3095d [naga] Support textureSampleBaseClampToEdge() for texture2d
Adds a new flag to the IR indicating when image sample coordinates are
to be clamped. Adds wgsl-in support for parsing and lowering to
IR. Validation ensures this flag is only used when sampling a 2D
non-arrayed sampled texture, without offset, gather, or depth
comparison. This matches the WGSL requirements, with the exception of
supporting `texture_external` textures, which will follow in a later
patch.

SPIRV, HLSL, and Metal backends are supported so far, with GLSL left
for a follow up. (In GLSL the texture will simply be sampled without
the coordinates being clamped.)

It may seem unfortunate to have to handle this separately for each
backend, and indeed it would have been possible to implement this simply
in the WGSL frontend. However, future patches will add support for using
textureSampleBaseClampToEdge() with external textures, which will
actually have to be handled by each backend. This patch is laying the
groundwork for that.
2025-06-17 14:27:18 -07:00
Jamie Nicol
d811258424 [naga msl-out] Add padding to end of structs if required 2025-06-17 20:20:25 +01:00
Jim Blandy
48eae68156 [naga spv-in] Lay out entry point output structs correctly.
In the SPIR-V front end, when generating Naga IR `Struct` types to
represent a SPIR-V entry point's `Output` variables, instead of saying
"0xFFFF, // shouldn't matter", follow the usual rules in assigning
struct member offsets and computing an overall
size (`TypeInner::Struct::span`) for the resulting struct type.
2025-06-17 09:56:57 -07:00
Andy Leiserson
45ebc73610 [naga] Always compact the module in process_overrides
Fixes #7638
2025-06-16 16:42:02 -07:00
Vecvec
03775c54fe
Prevent naga crashing on an aliased ray query. (#7759) 2025-06-16 08:45:39 +00:00
Dmitry Zamkov
bbb7cc79ef
Implement clip-distances extension for GL and Vulkan backends (#7730)
* Basic implementation of `clip_distances` for Vulkan and GL backends

* Added GPU test for `clip-distances`

* Update feature array size

* Add changelog entry

* Validate `clip_distances` array size

* Check for `clip_distances` enable directive

* Consolidate code for generating `enable` directives in WGSL backend and add `clip_distances`.
2025-06-16 10:33:31 +02:00
Phena Ildanach
486a77d682 [naga glsl-out] Split writes for memory/control barriers 2025-06-16 10:22:46 +02:00
Phena Ildanach
dd273fd7e2 [naga spv] Split workgroup and subgroup memory semantics in Control Barriers 2025-06-16 10:22:46 +02:00
Phena Ildanach
1e031e7a02 [naga spv-in] Add support for Memory Barriers 2025-06-16 10:22:46 +02:00
Erich Gubler
f96ac55aa4
fix(naga): don't panic on f16s in pipeline constants (#7801) 2025-06-13 10:41:19 -07:00
Andy Leiserson
82fa8e2a94 [naga] Remove non-essential override references via compaction
Adds a mode to compaction that removes unused functions, global
variables, and named types and overrides. This mode is used
everywhere except the compaction at the end of lowering, where
it is important to preserve unused items for type checking and
other validation of the module.

Pruning all but the active entry point and then compacting makes
`process_overrides` tolerant of missing values for overrides that are
not used by the active entry point.

Fixes #5885
2025-06-12 14:22:18 +09:00
Andy Leiserson
096f1f1f6d [naga] Remove the compact feature 2025-06-12 14:22:18 +09:00
Andy Leiserson
611a2bbede [naga] Create a helper for adjust_doc_comments 2025-06-12 14:22:18 +09:00
atlv
645354a528
Make naga span methods take path as generic AsRef Path (#7643)
Co-authored-by: Erich Gubler <erichdongubler@gmail.com>
Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
2025-06-12 01:55:25 +00:00
Erich Gubler
9659838a1b
refactor(naga): resolve!(…): de-dupe $expr usage (#7787) 2025-06-11 11:33:37 -04:00
Erich Gubler
db3c35db90 fix(naga): properly impl. auto. type conv. for select 2025-06-11 21:55:02 +09:00
Erich Gubler
3c0803d1cc refactor(wgsl-in): make Scalar::concretize pub(in crate::front::wgsl) 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
Nico Burns
630905134b
Make strum a dev-only dependency (#7776) 2025-06-11 04:32:11 -04:00
Teodor Tanasoaia
23b81da5cc
[hlsl-out] polyfill float remainder operator (#7750) 2025-06-05 15:14:03 +02:00
Thierry Berger
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 Zamkov
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 Zamkov
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
Andy Leiserson
0997b99429 [naga] Make additional test code reachable from entrypoints 2025-05-22 13:01:15 -07:00
Andy Leiserson
25636e274b [naga] Ensure globals in wgsl snapshot tests are reachable from an entry point
Convert tabs to spaces in access.wgsl
2025-05-22 13:01:15 -07: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