8842 Commits

Author SHA1 Message Date
Connor Fitzgerald
7e119968ce
Properly Deal with Timeouts (#7030) 2025-02-14 23:19:51 +00:00
Jim Blandy
f90f19c7e8 [naga spv-out] Factor out wrapped divide/module generation.
Move the code to generate the definition of an overflow-safe
divide/modulo SPIR-V function into its own Rust function, to reduce
indentation and clarify influences. This commit isn't intended to
cause any change in behavior.
2025-02-14 15:17:40 -08:00
Jamie Nicol
cb9666c6a7 [naga hlsl-out] Document the need for wrapper functions for integer division, modulo, abs(), and unary negation
Explain we need the wrapper functions not just to avoid undefined
behaviour, but also to ensure we adhere to the WGSL spec. Additionally
link to issue #7109 in cases where our workaround needs follow-up work
for non-32-bit integer types.
2025-02-14 15:17:40 -08:00
Jamie Nicol
b3b40c03d4 [naga msl-out] Document the need for wrapper functions for integer division, modulo, abs(), and unary negation
Explain we need the wrapper functions not just to avoid undefined
behaviour (or unspecified in the case of division), but also to ensure
we adhere to the WGSL spec.
2025-02-14 15:17:40 -08:00
Jim Blandy
96de35aac1 [hlsl-out] Clarify comment on HLSL floating-point divide by zero.
Replace the link to the resolved WGSL spec issue about floating-point
division by zero (gpuweb/gpuweb#2798) with links to the Direct3D 11
functional specification (which Direct3D 12 inherits) and the DXIL
specification, explaining that HLSL does what WGSL wants here.
2025-02-14 15:17:40 -08:00
Jim Blandy
bafeee6680 [hlsl-out] Clean up repetition in write_mapped_math_functions.
Since every `match` arm ends up looking up the type of the operation's
first argument, just do that once. This avoids a repetitive lookup for
`Abs`.
2025-02-14 15:17:40 -08:00
Jim Blandy
a25098f5ec [naga hlsl-out] Use Scalar associated constants in patterns. 2025-02-14 15:17:40 -08:00
Jamie Nicol
6cf4b710b8 [naga spv-out] Avoid undefined behaviour for integer division and modulo
Integer division or modulo is undefined behaviour in SPIR-V when the
divisor is zero, or when the dividend is the most negative number
representable by the result type and the divisor is negative one.

This patch makes us avoid this undefined behaviour and instead ensures
we adhere to the WGSL spec in these cases: for divisions the
expression evaluates to the value of the dividend, and for modulos the
expression evaluates to zero.

Similarily to how we handle these cases for the MSL and HLSL backends,
prior to emitting each function we emit code for any helper functions
required by that function's expressions. In this case that is helper
functions for integer division and modulo. Then, when emitting the
actual function's body, if we encounter an expression which needs
wrapped we instead emit a function call to the helper.
2025-02-14 15:17:40 -08:00
Jamie Nicol
b6186ba332 [naga hlsl-out] Avoid undefined behaviour for integer division, modulo, negation, and abs
Emit helper functions for MathFunction::Abs and UnaryOperator::Negate
with a signed integer scalar or vector operand. And for
BinaryOperator::Divide and BinaryOperator::Modulo with signed or
unsigned integer scalar or vector operands.

Abs and Negate are written to avoid signed integer overflow when the
operand equals INT_MIN. This is achieved by bitcasting the value to
unsigned, using the negation operator, then bitcasting the result back
to signed. As HLSL's bitcast functions asint() and asuint() only work
for 32-bit types, we only use this workaround in such cases.

Division and Modulo avoid undefined behaviour for INT_MIN / -1 and
divide-by-zero by using 1 for the divisor instead. Additionally we
avoid undefined behaviour when using the modulo operator on operands
of mixed signedness by using the equation from the WGSL spec, using
division, subtraction and multiplication, rather than HLSL's modulus
operator.
2025-02-14 15:17:40 -08:00
Jamie Nicol
88f6e9b8f8 [naga hlsl-out] Avoid undefined behaviour for signed integer addition, subtraction, and multiplication
Though not explicitly specified one way or the other, we have been
informed by DirectX engineers that signed integer overflow may be
undefined behaviour in some cases. To avoid this, we therefore bitcast
signed operands to unsigned prior to performing addition, subtraction,
or multiplication, then bitcast the result back to signed. As signed
types are represented as two's complement, this gives the correct
result whilst avoid any potential undefined behaviour.

Unfortunately HLSL's bitcast functions asint() and asuint() only work
for the 32-bit int and uint types. We therefore only apply this
workaround for 32-bit signed arithmetic. Support for other bit widths
could be added in the future, but extra care must be taken when
converting from unsigned to signed to avoid undefined or
implemented-defined behaviour.
2025-02-14 15:17:40 -08:00
Jamie Nicol
30363ef493 [naga hlsl-out] Wrap int literals in constructor-style cast
There is no literal suffix in HLSL for the integer type. We can,
however, wrap integer literals in a constructor style cast. This
avoids ambiguity when passing literals to overloaded functions, which
we'll make use of in the subsequent patch in this PR.
2025-02-14 15:17:40 -08:00
Jamie Nicol
8807c83363 [naga msl-out] Avoid undefined behaviour for integer division, modulo, negation, and abs
Adds infrastructure to the MSL backend for emitting helper functions,
based upon the existing HLSL backend equivalent. Emit helper functions
for MathFunction::Abs and UnaryOperator::Negate with a signed integer
scalar or vector operand. And for BinaryOperator::Divide and
BinaryOperator::Modulo with signed or unsigned integer scalar or
vector operands.

Abs and Negate are written to avoid signed integer overflow when the
operand equals INT_MIN. This is achieved by bitcasting the value to
unsigned and using the negation operator, then bitcasting the result
back to signed.

Division and Modulo avoid undefined bevaviour for INT_MIN / -1 and
divide-by-zero by using 1 for the divisor instead. Additionally we
avoid undefined behaviour when using the modulo operator when either
or both operands are negative by using the equation from the WGSL
spec, using division, subtraction and multiplication, rather than
MSL's modulus operator.

Lastly, as the usage of the wrapper function for unary integer
negation makes the negation_avoids_prefix_decrement() testcase less
interesting, we extend it to additionally test negating floats.
2025-02-14 15:17:40 -08:00
Connor Fitzgerald
106b709798
Remove HashMaps from wgpu API (#7133)
* Remove HashMaps from Surface API

* Fix Comments and Errors
2025-02-14 14:36:22 -05:00
Jamie Nicol
4bb09e1d3c
[naga wgsl-in] Ensure constant evaluation correctly handles Composes of vector ZeroValues (#7138) 2025-02-14 13:18:12 -05:00
Erich Gubler
eea3dde67e fix: use separate pub consts for FeaturesWebGPU 2025-02-14 12:18:41 -05:00
Erich Gubler
b05e9705bf fix: add cbindgen:ignore on const with the same ident. 2025-02-14 12:18:41 -05:00
Erich Gubler
7510d7b929 fix: add cbindgen:ignore on conflicting monomorphizations 2025-02-14 12:18:41 -05:00
Erich Gubler
a7a5784156 refactor: use match instead of if let for Features::from_bits 2025-02-14 12:18:41 -05:00
Erich Gubler
8ecafe2750 refactor: use match, instead of if, for Features::from_name 2025-02-14 12:18:41 -05:00
Erich Gubler
e8e09d059f fix: use same visibility for Features fields as itself 2025-02-14 12:18:41 -05:00
Erich Gubler
26eded137a fix: allow(missing_docs) on Features fields 2025-02-14 12:18:41 -05:00
Erich Gubler
36ad109d7a style: fmt. bitflags_array! macro a bit
Most important points:

- Make macro repetitions easy to parse as a human.
- Try to follow `rustfmt` a bit more closely.
2025-02-14 12:18:41 -05:00
Bruce Mitchener
3710de8e5b
Fix clippy::doc_overindented_list_items lints (#7139) 2025-02-14 16:36:41 +00:00
Teodor Tanasoaia
ef0e6f782a
deduplicate vertex buffer size validation (#7124) 2025-02-14 10:33:28 +01:00
Bruce Mitchener
bae0e70e8d
Fix clippy::unneeded_struct_pattern lints (#7136) 2025-02-14 01:32:21 -05:00
Connor Fitzgerald
ab38df24ba
Improve cargo-nextext Configuration (#7131) 2025-02-13 18:55:23 -05:00
Connor Fitzgerald
03a01df3cb
Speed Up Benchmarks in Test (#7129) 2025-02-13 18:48:13 -05:00
Kevin Reid
2f50426b35 Remove unnecessary chunk size field from StagingBelt. 2025-02-13 12:46:29 -05:00
Kevin Reid
ec6c7225da Remove unnecessary Arc from StagingBelt.
`Buffer` now implements `Clone` on its own.
2025-02-13 12:46:29 -05:00
Kevin Reid
fd896695d5 Expose StagingBelt allocation directly.
This allows using `StagingBelt` for copying to textures or any other
operation where copying to an existing buffer is not the desired outcome.

One unfortunate feature of the API is that `allocate()` returns the entire
buffer and an offset, so applications could accidentally touch parts of
the belt buffer outside the intended allocation. It might make more sense
to return `wgpu::BufferSlice`, but that struct cannot be used in
operations like `copy_buffer_to_texture` and does not have getters, so it
is not currently suitable for that purpose.

I also moved `Exclusive` into a module so that its unsafe-to-access field
is properly private, and made various improvements to the `StagingBelt`
documentation, such as acknowledging that `write_buffer_with()` exists.
2025-02-13 12:46:29 -05:00
Bruce Mitchener
7fe8e3818a
wgpu-types: Improve formatting of docs (#7125) 2025-02-13 12:25:21 -05:00
Connor Fitzgerald
c39d37fc36
Fix Send/Sync of RequestDeviceErrorKind (#7122) 2025-02-13 08:46:50 -05:00
Kevin Reid
c4b25b8794
Duplicate BufferSlice methods onto Buffer and vice versa. (#7123)
This allows users to skip creation of `BufferSlice` if they have no use
for it, and brings `wgpu` closer to the WebGPU API without removing any
Rust convenience. New functions:

* `BufferSlice::slice()`
* `Buffer::map_async()`
* `Buffer::get_mapped_range()`
* `Buffer::get_mapped_range_mut()`
* `Buffer::get_mapped_range()`
2025-02-13 14:40:09 +01:00
Leo Kettmeir
f0bf789f58
refactor(deno): use object wrap (#7113) 2025-02-13 04:17:00 -08:00
Kevin Reid
0f111cb27c Add a test for the noop backend. 2025-02-12 22:31:47 -05:00
Kevin Reid
118a985ccd Allow creating Noop backend through the normal mechanisms. 2025-02-12 22:31:47 -05:00
Kevin Reid
7293a13a97 Extend the capabilities of the Noop backend.
This will allow using it in tests of wgpu resource management code
that does not actually require a backend.

* `enumerate_adapters()` returns an adapter instead of failing.
* `open()` returns a device instead of failing.
* `create_buffer()` allocates actual memory.
* `map_buffer()` actually provides access to that memory.
* `clear_buffer()` actually clears the buffer.
* `copy_buffer_to_buffer()` actually copies data.
* Fences actually work (trivially, because all operations are
  synchronous).

Future work could include implementing texture copies,
timestamp queries, and the clearing part of render passes.
2025-02-12 22:31:47 -05:00
Kevin Reid
0143b4aaa0 Rename Empty backend to Noop.
This is in preparation for making it a more substantial test stub,
able to execute nontrivial code, and usable from the safe `wgpu` API.
2025-02-12 22:31:47 -05:00
richerfu
ff907736ef
feat(gles): support gles backend on openharmony (#7085)
* fix(gles): fix gles backend crash on openharmony

* docs: add changelog

* ci: add OpenHarmony to CI

* Update .github/workflows/ci.yml

---------

Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
2025-02-13 03:01:50 +00:00
Adrian Wielgosik
0f5d575ef3
Lower max_color_attachments limit for GL to 4 (#6994)
Co-authored-by: Andreas Reich <r_andreas2@web.de>
2025-02-13 00:37:34 +00:00
Amr Bashir
2f607d3e64
fix(dx12): map composite alpha mode (#7117)
* fix(dx12): map composite alpha mode

closes #7108

* changelog entry

* add composite modes based on surface target

* fix missing `in` in change log entry
2025-02-12 23:02:50 +00:00
Daniel McNab
97704099de
Document Queue::write_buffer's allocation on native APIs (#7114)
Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
2025-02-12 20:20:35 +00:00
Vecvec
5af9e30009
Fix HLSL single scalar loads (#7104) 2025-02-12 18:05:32 +00:00
Jim Blandy
0dd6a1cfe2 [naga] Retain unnamed overrides' types during compaction.
Ensure that unnamed overrides' types are retained, even when the
overrides are used only by global expressions. Add a test case.

Fixes #7072.
2025-02-12 09:19:56 -08:00
Jim Blandy
c934184c9e [naga] Retain unnamed constants' types during compaction.
Ensure that unnamed constants' types are retained, even when the
constants are used only by global expressions. Add a test case.

The old code marked all named constants as used; then processed
function bodies; and then marked the types of all used constants as
used. This ensured that, as long as a constant was used by a function,
its type would be retained, but didn't address the case where a
constant is used only by a global expression.
2025-02-12 09:19:56 -08:00
Dzmitry Malyshau
189c97c88a
Implement ray query candidate intersection generation and confirmation (#7047) 2025-02-12 16:23:29 +01:00
Jamie Nicol
b477c66a9d [naga wgsl-in] Do not attempt automatic type conversion for non-abstract types
Attempting to convert a non-abstract type will always fail, which can
result in unrelated errors being misreported as type conversion
errors. For example, in #7035 we made it so that abstract types can be
used as return values. This changed the final testcase in the
invalid_functions() test to fail due to failing to convert a u32 to an
atomic<u32>, rather than with a NonConstructibleReturnType error.

This patch makes it so that we do not attempt to convert non-abstract
types in the first place. This avoids the AutoConversion error for
that testcase, meaning the NonConstructibleReturnType is once again
reported.

Various callsites of try_automatic_conversions() have been updated to
ensure they still return an InitializationTypeMismatch error if the
types are mismatched, even if try_automatic_conversions() succeeds (eg
when conversion was not attempted due to the type being concrete). To
reduce code duplication these callsites were all adapted to use the
existing type_and_init() helper function.

Lastly, a couple of tests that expected to result in a AutoConversion
error have been adjusted to ensure this still occurs, by ensuring the
type used is abstract.
2025-02-12 15:10:57 +00:00
Jamie Nicol
a546e60d6d
[naga wgsl-in] Ensure correct array stride when converting types during const evaluation (#7112)
When converting the underlying scalar type of an array during const
evaluation, we currently use the resulting base type's size as the array
stride for the resulting type. For certain types, this may not match the
required alignment and will therefore result in a validation error.

For example, `array<vec3<f32>, N>` should have a stride of 16. But if
declared with an abstract initializer, eg `array(vec3(0.0))` we will
incorrectly determine the stride to be 12.

To solve this, we use the proc::Layouter struct to determine the
required array stride during const evaluation. To avoid repeating
layouting work, we reuse the Lowerer's layouter, passing it through the
various *Contexts through to the ConstantEvaluator.
2025-02-12 15:46:13 +01:00
Vecvec
3a4a40aae4
[wgpu-hal] Blas compaction (#7101) 2025-02-12 13:19:07 +00:00
Shaye Garg
d7a42933a5
[naga] Error on duplicate fields in structs (#7088)
* error in wgsl-in

* changelog

* add test
2025-02-12 03:27:55 +00:00