Skip to content

Commit acb245f

Browse files
committed
Checks some functions.
1 parent 1541898 commit acb245f

File tree

12 files changed

+1837
-687
lines changed

12 files changed

+1837
-687
lines changed

naga/src/common/wgsl.rs

Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,301 @@ impl StandardFilterableTriggeringRule {
6767
}
6868
}
6969
}
70+
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+
211+
pub struct Wgslish<T>(pub T);
212+
213+
impl Display for Wgslish<&crate::TypeInner> {
214+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
215+
match *self.0 {
216+
crate::TypeInner::Scalar(scalar) => Wgslish(scalar).fmt(f),
217+
crate::TypeInner::Vector { size, scalar } => {
218+
write!(f, "vec{}<{}>", size as u8, Wgslish(scalar))
219+
}
220+
crate::TypeInner::Matrix {
221+
columns,
222+
rows,
223+
scalar,
224+
} => {
225+
write!(
226+
f,
227+
"mat{}x{}<{}>",
228+
columns as u8,
229+
rows as u8,
230+
Wgslish(scalar)
231+
)
232+
}
233+
crate::TypeInner::Atomic(scalar) => {
234+
write!(f, "atomic<{}>", Wgslish(scalar))
235+
}
236+
crate::TypeInner::Pointer { base, space } => {
237+
write!(f, "ptr<{}, {base:?}>", Wgslish(space))
238+
}
239+
crate::TypeInner::ValuePointer {
240+
size,
241+
scalar,
242+
space,
243+
} => {
244+
write!(f, "ptr<{}, ", Wgslish(space))?;
245+
match size {
246+
Some(size) => write!(f, "vec{}<{}>", size as u8, Wgslish(scalar))?,
247+
None => Wgslish(scalar).fmt(f)?,
248+
}
249+
f.write_str(">")
250+
}
251+
crate::TypeInner::Array { base, size, stride } => {
252+
write!(f, "@stride({stride}) array<{base:?}")?;
253+
match size {
254+
crate::ArraySize::Constant(non_zero) => write!(f, ", {non_zero}>"),
255+
crate::ArraySize::Pending(pending_array_size) => match pending_array_size {
256+
crate::PendingArraySize::Expression(handle) => {
257+
write!(f, "expression {handle:?}")
258+
}
259+
crate::PendingArraySize::Override(handle) => {
260+
write!(f, "override {handle:?}")
261+
}
262+
},
263+
crate::ArraySize::Dynamic => f.write_str(">"),
264+
}
265+
}
266+
crate::TypeInner::Struct { ref members, span } => {
267+
write!(f, "@span({span}) struct {{ ")?;
268+
for (i, member) in members.iter().enumerate() {
269+
if i != 0 {
270+
f.write_str(", ")?;
271+
}
272+
write!(f, "@offset({}) ", member.offset)?;
273+
if let Some(ref binding) = member.binding {
274+
Wgslish(binding).fmt(f)?;
275+
}
276+
write!(
277+
f,
278+
"{}: {:?}",
279+
member.name.as_deref().unwrap_or("<anonymous>"),
280+
member.ty
281+
)?;
282+
}
283+
f.write_str("}")
284+
}
285+
crate::TypeInner::Image {
286+
dim: _,
287+
arrayed: _,
288+
class: _,
289+
} => todo!(),
290+
crate::TypeInner::Sampler { comparison: _ } => todo!(),
291+
crate::TypeInner::AccelerationStructure => todo!(),
292+
crate::TypeInner::RayQuery => todo!(),
293+
crate::TypeInner::BindingArray { base, size } => {
294+
write!(f, "array<{base:?}")?;
295+
match size {
296+
crate::ArraySize::Constant(non_zero) => write!(f, ", {non_zero}>"),
297+
crate::ArraySize::Pending(pending_array_size) => match pending_array_size {
298+
crate::PendingArraySize::Expression(handle) => {
299+
write!(f, "expression {handle:?}")
300+
}
301+
crate::PendingArraySize::Override(handle) => {
302+
write!(f, "override {handle:?}")
303+
}
304+
},
305+
crate::ArraySize::Dynamic => f.write_str(">"),
306+
}
307+
}
308+
}
309+
}
310+
}
311+
312+
impl Display for Wgslish<crate::Scalar> {
313+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
314+
let bits = self.0.width * 8;
315+
match self.0.kind {
316+
crate::ScalarKind::Sint => write!(f, "i{bits}"),
317+
crate::ScalarKind::Uint => write!(f, "u{bits}"),
318+
crate::ScalarKind::Float => write!(f, "f{bits}"),
319+
crate::ScalarKind::Bool => f.write_str("bool"),
320+
crate::ScalarKind::AbstractInt => f.write_str("{AbstractInt}"),
321+
crate::ScalarKind::AbstractFloat => f.write_str("{AbstractFloat}"),
322+
}
323+
}
324+
}
325+
326+
impl Display for Wgslish<crate::AddressSpace> {
327+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
328+
let wgsl = match self.0 {
329+
crate::AddressSpace::Function => "function",
330+
crate::AddressSpace::Private => "private",
331+
crate::AddressSpace::WorkGroup => "workgroup",
332+
crate::AddressSpace::Uniform => "uniform",
333+
crate::AddressSpace::Storage { access } => {
334+
return write!(f, "storage, {access:?}");
335+
}
336+
crate::AddressSpace::Handle => "handle",
337+
crate::AddressSpace::PushConstant => "push_constant",
338+
};
339+
f.write_str(wgsl)
340+
}
341+
}
342+
343+
impl Display for Wgslish<&crate::Binding> {
344+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
345+
match *self.0 {
346+
crate::Binding::BuiltIn(built_in) => f.write_str(built_in.to_wgsl()),
347+
crate::Binding::Location {
348+
location,
349+
second_blend_source,
350+
interpolation,
351+
sampling,
352+
} => {
353+
write!(f, "@location({location})")?;
354+
if second_blend_source {
355+
f.write_str(" @second_blend_source")?;
356+
}
357+
if let Some(interpolation) = interpolation {
358+
write!(f, " {}", interpolation.to_wgsl())?;
359+
}
360+
if let Some(sampling) = sampling {
361+
write!(f, " {}", sampling.to_wgsl())?;
362+
}
363+
Ok(())
364+
}
365+
}
366+
}
367+
}

0 commit comments

Comments
 (0)