Skip to content

Commit 5789746

Browse files
committed
[naga wgsl-in] Implement abstract types for consts, constructors.
1 parent bda1c9e commit 5789746

22 files changed

+1539
-739
lines changed

CHANGELOG.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,44 @@ Passing an owned value `window` to `Surface` will return a `Surface<'static>`. S
102102
- Introduce a new `Scalar` struct type for use in Naga's IR, and update all frontend, middle, and backend code appropriately. By @jimblandy in [#4673](https://github.com/gfx-rs/wgpu/pull/4673).
103103
- Add more metal keywords. By @fornwall in [#4707](https://github.com/gfx-rs/wgpu/pull/4707).
104104

105-
- Implement WGSL abstract types (by @jimblandy):
106-
- Add a new `naga::Literal` variant, `I64`, for signed 64-bit literals. [#4711](https://github.com/gfx-rs/wgpu/pull/4711)
105+
- Add partial support for WGSL abstract types (@jimblandy in [#4743](https://github.com/gfx-rs/wgpu/pull/4743)).
106+
107+
Abstract types make numeric literals easier to use, by
108+
automatically converting literals and other constant expressions
109+
from abstract numeric types to concrete types when safe and
110+
necessary. For example, to build a vector of floating-point
111+
numbers, Naga previously made you write:
112+
113+
vec3<f32>(1.0, 2.0, 3.0)
114+
115+
With this change, you can now simply write:
116+
117+
vec3<f32>(1, 2, 3)
118+
119+
Even though the literals are abstract integers, Naga recognizes
120+
that it is safe and necessary to convert them to `f32` values in
121+
order to build the vector. You can also use abstract values as
122+
initializers for global constants, like this:
123+
124+
const unit_x: vec2<f32> = vec2(1, 0);
125+
126+
The literals `1` and `0` are abstract integers, and the expression
127+
`vec2(1, 0)` is an abstract vector. However, Naga recognizes that
128+
it can convert that to the concrete type `vec2<f32>` to satisfy
129+
the given type of `unit_x`.
130+
131+
The WGSL specification permits abstract integers and
132+
floating-point values in almost all contexts, but Naga's support
133+
for this is still incomplete. Many WGSL operators and builtin
134+
functions are specified to produce abstract results when applied
135+
to abstract inputs, but for now Naga simply concretizes them all
136+
before applying the operation. We will expand Naga's abstract type
137+
support in subsequent pull requests.
138+
139+
As part of this work, the public types `naga::ScalarKind` and
140+
`naga::Literal` now have new variants, `AbstractInt` and `AbstractFloat`.
141+
142+
- Add a new `naga::Literal` variant, `I64`, for signed 64-bit literals. [#4711](https://github.com/gfx-rs/wgpu/pull/4711)
107143

108144
- Emit and init `struct` member padding always. By @ErichDonGubler in [#4701](https://github.com/gfx-rs/wgpu/pull/4701).
109145

naga/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ deserialize = ["serde", "bitflags/serde", "indexmap/serde"]
3131
arbitrary = ["dep:arbitrary", "bitflags/arbitrary", "indexmap/arbitrary"]
3232
spv-in = ["petgraph", "spirv"]
3333
spv-out = ["spirv"]
34-
wgsl-in = ["hexf-parse", "unicode-xid"]
34+
wgsl-in = ["hexf-parse", "unicode-xid", "compact"]
3535
wgsl-out = []
3636
hlsl-out = []
3737
compact = []

naga/src/front/wgsl/error.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ pub enum Error<'a> {
251251
ExpectedPositiveArrayLength(Span),
252252
MissingWorkgroupSize(Span),
253253
ConstantEvaluatorError(ConstantEvaluatorError, Span),
254+
AutoConversion {
255+
dest_span: Span,
256+
dest_type: String,
257+
source_span: Span,
258+
source_type: String,
259+
},
254260
}
255261

256262
impl<'a> Error<'a> {
@@ -712,6 +718,20 @@ impl<'a> Error<'a> {
712718
)],
713719
notes: vec![],
714720
},
721+
Error::AutoConversion { dest_span, ref dest_type, source_span, ref source_type } => ParseError {
722+
message: format!("automatic conversions cannot convert `{source_type}` to `{dest_type}`"),
723+
labels: vec![
724+
(
725+
dest_span,
726+
format!("a value of type {dest_type} is required here").into(),
727+
),
728+
(
729+
source_span,
730+
format!("this expression has type {source_type}").into(),
731+
)
732+
],
733+
notes: vec![],
734+
}
715735
}
716736
}
717737
}

0 commit comments

Comments
 (0)