Commit Graph

136 Commits

Author SHA1 Message Date
teoxoy
207747cab5 [hlsl-out] add support for restricting indexing to avoid OOB accesses 2024-10-23 16:51:44 +02:00
Erich Gubler
36fab5ce3d fix(msl-out): use namer for <fun>{Input,Output} structs 2024-10-22 10:14:26 -04:00
Erich Gubler
2302b5fac6 test: add regr. cvg. for conflicting idents. in shader I/O and locals 2024-10-22 10:14:26 -04:00
Jasper St. Pierre
1b2ef8612d
naga: Add support for textureQueryLevels to GLSL parser (#6415) 2024-10-18 10:07:58 +02:00
Xiaopeng Li
74ef445bca
ADD metal namespace for uint4 (#6417) 2024-10-17 11:45:29 +02:00
Schell Carl Scivally
76b1605090
[spv-in] sampling from depth textures returns a scalar and is splatted (#6384) 2024-10-11 20:24:16 +02:00
Jim Blandy
1047fa57f0 [naga] Move array-by-value snapshot tests to index-by-value.wgsl. 2024-10-11 08:27:15 -07:00
Jim Blandy
ed3006ccc6 [naga spv-out] Spill arrays and matrices for runtime indexing.
Improve handling of `Access` expressions whose base is an array or
matrix (not a pointer to such), and whose index is not known at
compile time. SPIR-V does not have instructions that can do this
directly, so spill such values to temporary variables, and perform the
accesses using `OpAccessChain` instructions applied to the
temporaries.

When performing chains of accesses like `a[i].x[j]`, do not reify
intermediate values; generate a single `OpAccessChain` for the entire
thing.

Remove special cases for arrays; the same code now handles arrays and
matrices.

Update validation to permit dynamic indexing of matrices.

For details, see the comments on the new tracking structures in
`naga:🔙:spv::Function`.

Add snapshot test `index-by-value.wgsl`.

Fixes #6358.
Fixes #4337.
Alternative to #6362.
2024-10-11 08:27:15 -07:00
Jim Blandy
f9075fc4b8 [naga] Test access to a member/element through a pointer. 2024-10-11 08:27:15 -07:00
Ronny Chan
73764fdc6a
[naga/wgsl-out]: polyfill inverse function (#6385) 2024-10-11 15:56:12 +02:00
Asher Jingkong Chen
bf33e481f3
[naga msl-out] Implement atomicCompareExchangeWeak for MSL backend (#6265) 2024-10-10 12:45:24 +02:00
Jim Blandy
3693da27f9 [naga spv-out] Bounds-check runtime-sized array access correctly.
Do not neglect to apply bounds checks to indexing operations on
runtime-sized arrays, even when they are accessed via an `AccessIndex`
instruction.

Before this commit, `BlockContext::write_expression_pointer` would not
apply bounds checks to `OpAccessChain` indices provided by an
`AccessIndex` instruction, apparently with the rationale that any
out-of-bounds accesses should have been reported by constant
evaluation.

While it is true that the `index` operand of an `AccessIndex`
expression is known at compile time, and that the WGSL constant
evaluation rules require accesses that can be statically determined to
be out-of-bounds to be shader creation or pipeline creation time
errors, accesses to runtime-sized arrays don't follow this pattern:
even if the index is known, the length with which it must be compared
is not.

Fixes #4441.
2024-10-08 11:53:15 -07:00
Jim Blandy
7283185305 [naga] Extend snapshot tests for bounds checks.
Extend the snapshot tests for bounds checking to cover the case where
a runtime-sized array is indexed by a constant.
2024-10-08 11:53:15 -07:00
Jim Blandy
3d85781f05 [naga spv-out] Consolidate code to find index values.
Let the SPIR-V backend use `GuardedIndex::try_resolve_to_constant`,
rather than writing out its definition in `write_restricted_index` and
`write_index_comparison`.

Call `try_resolve_to_constant` in one place, in `write_bounds_check`,
and simply pass the `GuardedIndex` into subroutines.

Reduce `write_restricted_index` and `write_index_comparison` to case
analysis and code generation.

Note that this commit does have a benign effect on SPIR-V snapshot
output for programs like this:

    let one_i = 1i;
    var vec0 = vec3<i32>();
    vec0[one_i] = 1;

The value indexing `vec0` here is an `i32`, but after this commit, the
operand to `OpAccessChain` becomes a `u32` constant (with the same
value).

This is because `write_bounds_check` now calls
`try_resolve_to_constant` itself, rather than deferring this work to
its callees, so it may return `BoundsCheckResult::KnownInBounds` even
when the `Unchecked` policy is in force. This directs the caller,
`write_expression_pointer`, to treat the `OpAccessChain` operand as a
fresh `u32` constant, rather than simply passing through the original
`i32` expression.
2024-10-08 11:53:15 -07:00
Jim Blandy
e432980a73 [naga spv-out] Don't emit unreachable blocks that jump into loops.
When generating SPIR-V, avoid generating unreachable blocks following
statements like `break`, `return`, and so on that cause non-local
exits. These unreachable blocks can cause SPIR-V validation to fail.

Fixes #6220.
2024-10-03 21:07:18 -07:00
sagudev
2d82054ae4 [wgsl-in, spv-out] Allow dynamic indexing of arrays by value.
Bring https://github.com/gfx-rs/naga/pull/723 back from the dead.

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
Co-authored-by: Dzmitry Malyshau <kvarkus@gmail.com>
Co-authored-by: Jim Blandy <jimb@red-bean.com>
2024-10-02 18:07:02 -07:00
Jim Blandy
98c4d6f42e
[naga] Permit only structs as binding array elements. (#6333)
Require `T` to be a struct in `binding_array<T, ...>`; do not permit
arrays.

In #5428, the validator was changed to accept binding array types that
the SPIR-V backend couldn't properly emit. Specifically, the validator
was changed to accept `binding_array<array<T>>`, but the SPIR-V
backend wasn't changed to wrap the binding array elements in a SPIR-V
struct type, as Vulkan requires. So the type would be accepted by the
validator, and then rejected by the backend.
2024-09-27 17:00:21 -07:00
Samson
866be693d6
[naga] Handle phony statements properly by treating them as named expressions (#6328)
* [naga wgsl-in] phony assignments add named expressions

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

* [naga wgsl-out] write out _naga_phony as phony

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

* Add test

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

* use statement span

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

* every phony has same name

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

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2024-09-27 14:52:53 -07:00
Jim Blandy
3fda684eb9 [naga msl-out] Defeat the MSL compiler's infinite loop analysis.
See the comments in the code for details.

This patch emits the definition of the macro only when the first loop
is encountered. This does make that first loop's code look a bit odd:
it would be more natural to define the macro at the top of the
file. (See the modified files in `naga/tests/out/msl`.)

Rejected alternatives:

- We could emit the macro definition unconditionally at the top of the
  file. But this changes every MSL snapshot output file, whereas only
  eight of them actually contain loops.

- We could have the validator flag modules that contain loops. But the
  changes end up being not small, and spread across the validator, so
  this seems disproportionate. If we had other consumers of this
  information, it might make sense.

- We could change the MSL backend to allow text to be generated out of
  order, so that we can decide whether to define the macro after we've
  generated all the function bodies. But at the moment this seems like
  unnecessary complexity, although it might be worth doing in the
  future if we had additional uses for it - say, to conditionally emit
  helper function definitions.

Fixes #4972.
2024-09-18 11:01:51 -04:00
Schell Carl Scivally
fc85e4f970
spv-in parse more atomic ops (#5824)
* add parsing for spirv::Op::AtomicLoad and spirv::Op::AtomicStore

* spv-in parse AtomicExchange and AtomicCompareExchange

* add atomic i decrement

* bookend atomic store statement with emmitter.finish/emitter.start to suppress a double load expression

bookend atomic result expressions with emitter.finish/start to prevent double defs

* add atomic iadd, isub, smin, umin, smax, umax, and, or, xor

* parse atomic flag test and set, parse atomic flag clear

* remove atomic compare exchange work

* changelog

* moved spirv tests into front/spv/mod.rs

* feature gate atomic spv tests because they require wgsl-[in,out]

* BlockContext::get_contained_global_variable returns Result

* Generate spans covering the entire instruction.

Granted, there is pre-existing code in the SPIR-V front end that gets
this wrong, but:

It doesn't make sense to read `self.data_offset`, and then immediately
pass that to `self.span_from_with_op`. The point of that function is
to make the span cover the entire instruction, operands included.

* Move `From` implementation into spv front end

* doc comments, minor cleanups

* remove parsing of OpAtomicFlagClear and OpAtomicFlagTestAndSet

* sync atomic spvasm files

---------

Co-authored-by: Jim Blandy <jimb@red-bean.com>
2024-09-18 05:39:36 +00:00
Dzmitry Malyshau
eb18854b46 spv-out: configure source language in debug info 2024-09-13 11:37:23 -07:00
Jim Blandy
c87717b814
[naga] Document snapshots::Targets::IR and ANALYSIS. (#6231) 2024-09-06 16:59:20 -04:00
Jim Blandy
aeac0f29fe
[naga] Make tests compile without the WGSL front end. (#6217) 2024-09-05 10:18:20 +02:00
Erich Gubler
2aa69e1bb2 test(wgsl): note link b/w cross and bad_cross_builtin_args tests 2024-09-04 15:08:56 -04:00
Erich Gubler
fcbec57d84 test(wgsl): test explicit center with {perspective,linear} interpolation 2024-09-04 15:08:56 -04:00
Samson
4e9a2a5003
[naga wgsl] Impl const_assert (#6198)
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2024-09-02 17:37:04 +00:00
Samson
ace2e201cf
[naga wgsl-in] allow global vars without explicit type (#6199)
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2024-09-02 18:27:52 +02:00
Samson
105cb9db31
[naga wgsl-in] Proper singular generic in vec and matrix (#6189)
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2024-09-02 14:04:51 +02:00
Samson
585f4a1036
[naga wgsl] all swizzle components must be either color or dimension (#6187) 2024-08-31 11:08:34 +02:00
Erich Gubler
04618b36a8
feat(wgsl): add first and either sampling for @interpolate(flat, …) (#6181) 2024-08-30 15:08:00 +00:00
Samson
34bb9e4ceb
[naga wgsl] Implement local const declarations (#6156) 2024-08-30 11:55:03 +02:00
Erich Gubler
7164f3eb4e fix(wgsl): narrow accepted args. of cross to vec3<$float> 2024-08-28 13:26:54 -04:00
Erich Gubler
327b92e92b test(wgsl): check that vec3 args. are accepted in cross built-in 2024-08-28 13:26:54 -04:00
vero
3178ffb0d7
Fix hlsl image type conversion (#6123) 2024-08-19 10:24:40 +02:00
teoxoy
ccd6d2ca48 remove BoundsCheckPolicies.image_store 2024-07-26 10:44:22 -07:00
Erich Gubler
a220fcfc57 feat(const_eval): impl. firstTrailingBit 2024-07-24 09:12:23 -04:00
Erich Gubler
5b44baa8c8 feat(const_eval): impl. firstLeadingBit 2024-07-24 09:12:23 -04:00
Imbris
6d7975eb3b [naga hlsl-out glsl-out] Work around backend loop/switch bugs.
Introduce a new module, `naga:🔙:continue_forward`, containing
shared code for rendering Naga `Continue` statements as backend
`break` statements and assignments to introduced `bool` locals.
See the module's documentation for details.

- [hlsl-out] Transform degenerate single body switches into `do-while`
  loops. Properly render `Continue` statements enclosed by
  `Switch` statements enclosed by `Loop` statements.

- [glsl-out] Transform degenerate single body switches into `do-while`
  loops.

Improve `naga xtask validate spv` error message.

Fixes #4485.
Fixes #4514.
2024-07-23 18:12:19 -07:00
Dzmitry Malyshau
1b4e8ada63 spv-out: fix acceleration structure in a function argument 2024-07-15 10:05:51 +02:00
JMS55
17fcb19425
[naga, hal] miscellaneous fixes for Atomic64 support (#5952)
In `naga:🔙hlsl`:

- Generate calls to `Interlocked{op}64` when necessary. not
  `Interlocked{op}`.

- Make atomic operations that do not produce a value emit their
  operands properly.
  
In the Naga snapshot tests:

- Adapt `atomicOps-int64-min-max.wgsl` to include cases that
  cover non-trivial atomic operation operand emitting.

In `wgpu_hal::vulkan::adapter`:

- When retrieving physical device features, be sure to include
  the `PhysicalDeviceShaderAtomicInt64Features` extending struct
  in the chain whenever the `VK_KHR_shader_atomic_int64` extension
  is available.

- Request both `shader_{buffer,shared}_int64_atomics` in the
  `PhysicalDeviceShaderAtomicInt64Features` extending struct when either of
  `wgpu_types::Features::SHADER_INT64_ATOMIC_{ALL_OPS,MIN_MAX}` is requested.

---------

Co-authored-by: Jim Blandy <jimb@red-bean.com>
2024-07-13 19:17:59 -07:00
Andreas Reich
89c3baf34e Add cfg_alias indirection to Naga's x_out features 2024-07-09 09:53:39 +02:00
Imbris
3a6814770a
Allow unconsumed inputs in fragment shaders (#5531)
* Allow unconsumed inputs in fragment shaders by removing them from vertex
outputs when generating HLSL.

Fixes https://github.com/gfx-rs/wgpu/issues/3748

* Add naga:🔙:hlsl::FragmentEntryPoint for providing information
  about the fragment entry point when generating vertex entry points via
  naga:🔙:hlsl::Writer::write. Vertex outputs not consumed by the
  fragment entry point are omitted in the final output struct.
* Add naga snapshot test for this new feature,
* Remove Features::SHADER_UNUSED_VERTEX_OUTPUT,
  StageError::InputNotConsumed, and associated validation logic.
* Make wgpu dx12 backend pass fragment shader info when generating
  vertex HLSL.
* Add wgpu regression test for allowing unconsumed inputs.

* Address review

* Add note that nesting structs for the inter-stage interface can't
  happen.
* Remove new TODO notes (some addressed and some transferred to an issue
  https://github.com/gfx-rs/wgpu/issues/5577)
* Changed issue that regression test refers to 3748 -> 5553
* Add debug_assert that binding.is_some() in hlsl writer
* Fix typos caught in CI

Also, fix compiling snapshot test when hlsl-out feature is not enabled.
2024-07-04 09:08:46 +02:00
Schell Carl Scivally
6405dcf611 [naga spv-in] Adjust types of globals used by atomic instructions.
To support atomic instructions in the SPIR-V front end, observe which
global variables the input accesses using atomic instructions, and
adjust their types from ordinary scalars to atomic values.

See comments in `naga::front::atomic_upgrade`.
2024-06-21 21:51:25 -07:00
Jim Blandy
d6c4d5c5c3 [naga] Manually implement serde traits for NonMaxU32.
When the appropriate features are enabled, manually implement
`serde::Serialize` and `serde::Deserialize`, such that the serialized
form of `NonMaxU32::new(n).unwrap()` is the same as that of `n`.

This eliminates the last trace of 1-based indices from Naga's snapshot
tests, and aligns `std::fmt::Debug` with the serialized form.
2024-06-21 10:09:03 +02:00
Jim Blandy
9f498fd571 [naga] Use new NonMaxU32 type for Handle indices.
Define a new type, `NonMaxU32`, that can represent any `u32` value
except `u32::MAX`, defined such that `Option<NonMaxU32>` is still a
32-bit value.

Change `Handle` to use `NonMaxU32`. Adjust all code that works
directly with handle indices to assume zero-based indices, not
one-based indices.
2024-06-21 10:09:03 +02:00
Jim Blandy
92287c30b5 [naga hlsl-out] Use Baked for baked expressions.
Make the HLSL backend more like other backends by using `back::Baked`
to generate names for baked expression identifiers. This removes the
final uses of `Handle::index` from the HLSL backend.

This is separated out from the previous commit because it changes lots
of snapshot tests, whereas the previous commit has no effect on Naga's
output.
2024-06-21 09:56:40 +02:00
Teodor Tanasoaia
00f5c57b86
update IR snapshots (#5793) 2024-06-10 18:41:07 -04:00
Vecvec
73401ede25
Fix double decoration if a binding array contains a struct with a runtime array (#5776) 2024-06-10 11:20:33 +02:00
Jim Blandy
1ad1c4ae77 [naga] Add snapshot test output omitted from #5702. 2024-06-10 10:22:51 +02:00
Atlas Dostal
abba12ae4e Add support for 64 bit integer atomic operations in shaders.
Add the following flags to `wgpu_types::Features`:

- `SHADER_INT64_ATOMIC_ALL_OPS` enables all atomic operations on `atomic<i64>` and
  `atomic<u64>` values.

- `SHADER_INT64_ATOMIC_MIN_MAX` is a subset of the above, enabling only
  `AtomicFunction::Min` and `AtomicFunction::Max` operations on `atomic<i64>` and
  `atomic<u64>` values in the `Storage` address space. These are the only 64-bit
  atomic operations available on Metal as of 3.1.

Add corresponding flags to `naga::valid::Capabilities`. These are supported by the
WGSL front end, and all Naga backends.

Platform support:

- On Direct3d 12, in `D3D12_FEATURE_DATA_D3D12_OPTIONS9`, if
  `AtomicInt64OnTypedResourceSupported` and `AtomicInt64OnGroupSharedSupported` are
  both available, then both wgpu features described above are available.

- On Metal, `SHADER_INT64_ATOMIC_MIN_MAX` is available on Apple9 hardware, and on
  hardware that advertises both Apple8 and Mac2 support. This also requires Metal
  Shading Language 2.4 or later. Metal does not yet support the more general
  `SHADER_INT64_ATOMIC_ALL_OPS`.

- On Vulkan, if the `VK_KHR_shader_atomic_int64` extension is available with both the
  `shader_buffer_int64_atomics` and `shader_shared_int64_atomics` features, then both
  wgpu features described above are available.
2024-06-08 18:36:26 -07:00