Skip to content

Commit cb8219f

Browse files
committed
toy sin and dot working
1 parent d6b32fa commit cb8219f

File tree

6 files changed

+415
-120
lines changed

6 files changed

+415
-120
lines changed

naga/src/common/wgsl.rs

+144-60
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,146 @@ impl StandardFilterableTriggeringRule {
6868
}
6969
}
7070

71+
impl crate::MathFunction {
72+
pub fn to_wgsl(self) -> &'static str {
73+
use crate::MathFunction as Mf;
74+
75+
match self {
76+
Mf::Abs => "abs",
77+
Mf::Min => "min",
78+
Mf::Max => "max",
79+
Mf::Clamp => "clamp",
80+
Mf::Saturate => "saturate",
81+
Mf::Cos => "cos",
82+
Mf::Cosh => "cosh",
83+
Mf::Sin => "sin",
84+
Mf::Sinh => "sinh",
85+
Mf::Tan => "tan",
86+
Mf::Tanh => "tanh",
87+
Mf::Acos => "acos",
88+
Mf::Asin => "asin",
89+
Mf::Atan => "atan",
90+
Mf::Atan2 => "atan2",
91+
Mf::Asinh => "asinh",
92+
Mf::Acosh => "acosh",
93+
Mf::Atanh => "atanh",
94+
Mf::Radians => "radians",
95+
Mf::Degrees => "degrees",
96+
Mf::Ceil => "ceil",
97+
Mf::Floor => "floor",
98+
Mf::Round => "round",
99+
Mf::Fract => "fract",
100+
Mf::Trunc => "trunc",
101+
Mf::Modf => "modf",
102+
Mf::Frexp => "frexp",
103+
Mf::Ldexp => "ldexp",
104+
Mf::Exp => "exp",
105+
Mf::Exp2 => "exp2",
106+
Mf::Log => "log",
107+
Mf::Log2 => "log2",
108+
Mf::Pow => "pow",
109+
Mf::Dot => "dot",
110+
Mf::Cross => "cross",
111+
Mf::Distance => "distance",
112+
Mf::Length => "length",
113+
Mf::Normalize => "normalize",
114+
Mf::FaceForward => "faceForward",
115+
Mf::Reflect => "reflect",
116+
Mf::Refract => "refract",
117+
Mf::Sign => "sign",
118+
Mf::Fma => "fma",
119+
Mf::Mix => "mix",
120+
Mf::Step => "step",
121+
Mf::SmoothStep => "smoothstep",
122+
Mf::Sqrt => "sqrt",
123+
Mf::InverseSqrt => "inverseSqrt",
124+
Mf::Transpose => "transpose",
125+
Mf::Determinant => "determinant",
126+
Mf::QuantizeToF16 => "quantizeToF16",
127+
Mf::CountTrailingZeros => "countTrailingZeros",
128+
Mf::CountLeadingZeros => "countLeadingZeros",
129+
Mf::CountOneBits => "countOneBits",
130+
Mf::ReverseBits => "reverseBits",
131+
Mf::ExtractBits => "extractBits",
132+
Mf::InsertBits => "insertBits",
133+
Mf::FirstTrailingBit => "firstTrailingBit",
134+
Mf::FirstLeadingBit => "firstLeadingBit",
135+
Mf::Pack4x8snorm => "pack4x8snorm",
136+
Mf::Pack4x8unorm => "pack4x8unorm",
137+
Mf::Pack2x16snorm => "pack2x16snorm",
138+
Mf::Pack2x16unorm => "pack2x16unorm",
139+
Mf::Pack2x16float => "pack2x16float",
140+
Mf::Pack4xI8 => "pack4xI8",
141+
Mf::Pack4xU8 => "pack4xU8",
142+
Mf::Unpack4x8snorm => "unpack4x8snorm",
143+
Mf::Unpack4x8unorm => "unpack4x8unorm",
144+
Mf::Unpack2x16snorm => "unpack2x16snorm",
145+
Mf::Unpack2x16unorm => "unpack2x16unorm",
146+
Mf::Unpack2x16float => "unpack2x16float",
147+
Mf::Unpack4xI8 => "unpack4xI8",
148+
Mf::Unpack4xU8 => "unpack4xU8",
149+
Mf::Inverse => "{matrix inverse}",
150+
Mf::Outer => "{vector outer product}",
151+
}
152+
}
153+
}
154+
155+
impl crate::BuiltIn {
156+
pub fn to_wgsl(self) -> &'static str {
157+
match self {
158+
crate::BuiltIn::Position { invariant: true } => "@position @invariant",
159+
crate::BuiltIn::Position { invariant: false } => "@position",
160+
crate::BuiltIn::ViewIndex => "view_index",
161+
crate::BuiltIn::BaseInstance => "{BaseInstance}",
162+
crate::BuiltIn::BaseVertex => "{BaseVertex}",
163+
crate::BuiltIn::ClipDistance => "{ClipDistance}",
164+
crate::BuiltIn::CullDistance => "{CullDistance}",
165+
crate::BuiltIn::InstanceIndex => "instance_index",
166+
crate::BuiltIn::PointSize => "{PointSize}",
167+
crate::BuiltIn::VertexIndex => "vertex_index",
168+
crate::BuiltIn::DrawID => "{DrawId}",
169+
crate::BuiltIn::FragDepth => "frag_depth",
170+
crate::BuiltIn::PointCoord => "{PointCoord}",
171+
crate::BuiltIn::FrontFacing => "front_facing",
172+
crate::BuiltIn::PrimitiveIndex => "primitive_index",
173+
crate::BuiltIn::SampleIndex => "sample_index",
174+
crate::BuiltIn::SampleMask => "sample_mask",
175+
crate::BuiltIn::GlobalInvocationId => "global_invocation_id",
176+
crate::BuiltIn::LocalInvocationId => "local_invocation_id",
177+
crate::BuiltIn::LocalInvocationIndex => "local_invocation_index",
178+
crate::BuiltIn::WorkGroupId => "workgroup_id",
179+
crate::BuiltIn::WorkGroupSize => "{WorkGroupSize}",
180+
crate::BuiltIn::NumWorkGroups => "num_workgroups",
181+
crate::BuiltIn::NumSubgroups => "num_subgroups",
182+
crate::BuiltIn::SubgroupId => "{SubgroupId}",
183+
crate::BuiltIn::SubgroupSize => "subgroup_size",
184+
crate::BuiltIn::SubgroupInvocationId => "subgroup_invocation_id",
185+
}
186+
}
187+
}
188+
189+
impl crate::Interpolation {
190+
pub fn to_wgsl(self) -> &'static str {
191+
match self {
192+
crate::Interpolation::Perspective => "perspective",
193+
crate::Interpolation::Linear => "linear",
194+
crate::Interpolation::Flat => "flat",
195+
}
196+
}
197+
}
198+
199+
impl crate::Sampling {
200+
pub fn to_wgsl(self) -> &'static str {
201+
match self {
202+
crate::Sampling::Center => "center",
203+
crate::Sampling::Centroid => "centroid",
204+
crate::Sampling::Sample => "sample",
205+
crate::Sampling::First => "first",
206+
crate::Sampling::Either => "either",
207+
}
208+
}
209+
}
210+
71211
pub struct Wgslish<T>(pub T);
72212

73213
impl Display for Wgslish<&crate::TypeInner> {
@@ -191,7 +331,7 @@ impl Display for Wgslish<crate::AddressSpace> {
191331
crate::AddressSpace::WorkGroup => "workgroup",
192332
crate::AddressSpace::Uniform => "uniform",
193333
crate::AddressSpace::Storage { access } => {
194-
return write!(f, "{access:?}");
334+
return write!(f, "storage, {access:?}");
195335
}
196336
crate::AddressSpace::Handle => "handle",
197337
crate::AddressSpace::PushConstant => "push_constant",
@@ -203,7 +343,7 @@ impl Display for Wgslish<crate::AddressSpace> {
203343
impl Display for Wgslish<&crate::Binding> {
204344
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
205345
match *self.0 {
206-
crate::Binding::BuiltIn(built_in) => Wgslish(built_in).fmt(f),
346+
crate::Binding::BuiltIn(built_in) => f.write_str(built_in.to_wgsl()),
207347
crate::Binding::Location {
208348
location,
209349
second_blend_source,
@@ -215,69 +355,13 @@ impl Display for Wgslish<&crate::Binding> {
215355
f.write_str(" @second_blend_source")?;
216356
}
217357
if let Some(interpolation) = interpolation {
218-
write!(f, " {}", Wgslish(interpolation))?;
358+
write!(f, " {}", interpolation.to_wgsl())?;
219359
}
220360
if let Some(sampling) = sampling {
221-
write!(f, " {}", Wgslish(sampling))?;
361+
write!(f, " {}", sampling.to_wgsl())?;
222362
}
223363
Ok(())
224364
}
225365
}
226366
}
227367
}
228-
229-
impl Display for Wgslish<crate::BuiltIn> {
230-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
231-
f.write_str(match self.0 {
232-
crate::BuiltIn::Position { invariant: true } => "@position @invariant",
233-
crate::BuiltIn::Position { invariant: false } => "@position",
234-
crate::BuiltIn::ViewIndex => "view_index",
235-
crate::BuiltIn::BaseInstance => "{BaseInstance}",
236-
crate::BuiltIn::BaseVertex => "{BaseVertex}",
237-
crate::BuiltIn::ClipDistance => "{ClipDistance}",
238-
crate::BuiltIn::CullDistance => "{CullDistance}",
239-
crate::BuiltIn::InstanceIndex => "instance_index",
240-
crate::BuiltIn::PointSize => "{PointSize}",
241-
crate::BuiltIn::VertexIndex => "vertex_index",
242-
crate::BuiltIn::DrawID => "{DrawId}",
243-
crate::BuiltIn::FragDepth => "frag_depth",
244-
crate::BuiltIn::PointCoord => "{PointCoord}",
245-
crate::BuiltIn::FrontFacing => "front_facing",
246-
crate::BuiltIn::PrimitiveIndex => "primitive_index",
247-
crate::BuiltIn::SampleIndex => "sample_index",
248-
crate::BuiltIn::SampleMask => "sample_mask",
249-
crate::BuiltIn::GlobalInvocationId => "global_invocation_id",
250-
crate::BuiltIn::LocalInvocationId => "local_invocation_id",
251-
crate::BuiltIn::LocalInvocationIndex => "local_invocation_index",
252-
crate::BuiltIn::WorkGroupId => "workgroup_id",
253-
crate::BuiltIn::WorkGroupSize => "{WorkGroupSize}",
254-
crate::BuiltIn::NumWorkGroups => "num_workgroups",
255-
crate::BuiltIn::NumSubgroups => "num_subgroups",
256-
crate::BuiltIn::SubgroupId => "{SubgroupId}",
257-
crate::BuiltIn::SubgroupSize => "subgroup_size",
258-
crate::BuiltIn::SubgroupInvocationId => "subgroup_invocation_id",
259-
})
260-
}
261-
}
262-
263-
impl Display for Wgslish<crate::Interpolation> {
264-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
265-
f.write_str(match self.0 {
266-
crate::Interpolation::Perspective => "perspective",
267-
crate::Interpolation::Linear => "linear",
268-
crate::Interpolation::Flat => "flat",
269-
})
270-
}
271-
}
272-
273-
impl Display for Wgslish<crate::Sampling> {
274-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
275-
f.write_str(match self.0 {
276-
crate::Sampling::Center => "center",
277-
crate::Sampling::Centroid => "centroid",
278-
crate::Sampling::Sample => "sample",
279-
crate::Sampling::First => "first",
280-
crate::Sampling::Either => "either",
281-
})
282-
}
283-
}

naga/src/front/wgsl/error.rs

+53
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,18 @@ pub(crate) enum Error<'a> {
260260
expected: Range<u32>,
261261
found: u32,
262262
},
263+
WrongArgumentType {
264+
function: String,
265+
call_span: Span,
266+
arg_span: Span,
267+
arg_index: u32,
268+
found: String,
269+
allowed: Vec<String>,
270+
},
271+
AmbiguousCall {
272+
call_span: Span,
273+
alternatives: Vec<String>,
274+
},
263275
FunctionReturnsVoid(Span),
264276
FunctionMustUseUnused(Span),
265277
FunctionMustUseReturnsVoid(Span, Span),
@@ -809,6 +821,47 @@ impl<'a> Error<'a> {
809821
labels: vec![(span, "wrong number of arguments".into())],
810822
notes: vec![],
811823
},
824+
Error::WrongArgumentType {
825+
ref function,
826+
call_span,
827+
arg_span,
828+
arg_index,
829+
ref found,
830+
ref allowed,
831+
} => {
832+
let message = format!(
833+
"This call to `{function}` cannot accept a value of type `{found}` for argument #{}",
834+
arg_index + 1,
835+
);
836+
let labels = vec![
837+
(call_span, "The arguments to this function call have incorrect types".into()),
838+
(arg_span, format!(
839+
"This argument has type `{found}`",
840+
).into())
841+
];
842+
843+
let mut notes = vec![];
844+
if arg_index > 0 {
845+
notes.push("Given the types of the preceding arguments,".into());
846+
notes.push(format!("the following types are allowed for argument #{}:", arg_index + 1));
847+
} else {
848+
notes.push("The following types are allowed for the first argument:".to_string());
849+
};
850+
notes.extend(allowed.iter().map(|ty| format!("allowed type: {ty}")));
851+
852+
ParseError { message, labels, notes }
853+
},
854+
Error::AmbiguousCall { call_span, ref alternatives } => {
855+
let message = "Function call is ambiguous: more than one overload could apply".into();
856+
let labels = vec![
857+
(call_span, "More than one overload of this function could apply to these arguments".into()),
858+
];
859+
let mut notes = vec![
860+
"All of the following overloads could apply, but no one overload is clearly preferable:".into()
861+
];
862+
notes.extend(alternatives.iter().map(|alt| format!("possible overload: {alt}")));
863+
ParseError { message, labels, notes }
864+
},
812865
Error::FunctionReturnsVoid(span) => ParseError {
813866
message: "function does not return any value".to_string(),
814867
labels: vec![(span, "".into())],

naga/src/front/wgsl/lower/conversion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl<'source> super::ExpressionContext<'source, '_, '_> {
5959
self.convert_leaf_scalar(expr, expr_span, goal_scalar)
6060
}
6161

62-
/// Try to convert `expr`'s leaf scalar to `goal` using automatic conversions.
62+
/// Try to convert `expr`'s leaf scalar to `goal_scalar` using automatic conversions.
6363
///
6464
/// If no conversions are necessary, return `expr` unchanged.
6565
///

0 commit comments

Comments
 (0)