Skip to content

Commit f652a3c

Browse files
committed
[naga] Implement TryToWgsl for Scalar, and use as appropriate.
Replace `naga::back::wgsl::writer::scalar_kind_str` with a `TryToWgsl` implementation for `Scalar` in `common::wgsl`. Use this where needed in the WGSL backend.
1 parent e17bbbb commit f652a3c

File tree

2 files changed

+34
-47
lines changed

2 files changed

+34
-47
lines changed

naga/src/back/wgsl/writer.rs

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl<W: Write> Writer<W> {
423423
self.out,
424424
"vec{}<{}>",
425425
common::vector_size_str(size),
426-
scalar_kind_str(scalar),
426+
scalar.to_wgsl_if_implemented()?,
427427
)?,
428428
TypeInner::Sampler { comparison: false } => {
429429
write!(self.out, "sampler")?;
@@ -445,7 +445,7 @@ impl<W: Write> Writer<W> {
445445
Ic::Sampled { kind, multi } => (
446446
"",
447447
if multi { "multisampled_" } else { "" },
448-
scalar_kind_str(crate::Scalar { kind, width: 4 }),
448+
crate::Scalar { kind, width: 4 }.to_wgsl_if_implemented()?,
449449
"",
450450
),
451451
Ic::Depth { multi } => {
@@ -478,10 +478,10 @@ impl<W: Write> Writer<W> {
478478
}
479479
}
480480
TypeInner::Scalar(scalar) => {
481-
write!(self.out, "{}", scalar_kind_str(scalar))?;
481+
write!(self.out, "{}", scalar.to_wgsl_if_implemented()?)?;
482482
}
483483
TypeInner::Atomic(scalar) => {
484-
write!(self.out, "atomic<{}>", scalar_kind_str(scalar))?;
484+
write!(self.out, "atomic<{}>", scalar.to_wgsl_if_implemented()?)?;
485485
}
486486
TypeInner::Array {
487487
base,
@@ -533,7 +533,7 @@ impl<W: Write> Writer<W> {
533533
"mat{}x{}<{}>",
534534
common::vector_size_str(columns),
535535
common::vector_size_str(rows),
536-
scalar_kind_str(scalar)
536+
scalar.to_wgsl_if_implemented()?
537537
)?;
538538
}
539539
TypeInner::Pointer { base, space } => {
@@ -559,7 +559,12 @@ impl<W: Write> Writer<W> {
559559
} => {
560560
let (address, maybe_access) = address_space_str(space);
561561
if let Some(space) = address {
562-
write!(self.out, "ptr<{}, {}", space, scalar_kind_str(scalar))?;
562+
write!(
563+
self.out,
564+
"ptr<{}, {}",
565+
space,
566+
scalar.to_wgsl_if_implemented()?
567+
)?;
563568
if let Some(access) = maybe_access {
564569
write!(self.out, ", {access}")?;
565570
}
@@ -582,7 +587,7 @@ impl<W: Write> Writer<W> {
582587
"ptr<{}, vec{}<{}>",
583588
space,
584589
common::vector_size_str(size),
585-
scalar_kind_str(scalar)
590+
scalar.to_wgsl_if_implemented()?
586591
)?;
587592
if let Some(access) = maybe_access {
588593
write!(self.out, ", {access}")?;
@@ -1590,7 +1595,7 @@ impl<W: Write> Writer<W> {
15901595
kind,
15911596
width: convert.unwrap_or(scalar.width),
15921597
};
1593-
let scalar_kind_str = scalar_kind_str(scalar);
1598+
let scalar_kind_str = scalar.to_wgsl_if_implemented()?;
15941599
write!(
15951600
self.out,
15961601
"mat{}x{}<{}>",
@@ -1608,7 +1613,7 @@ impl<W: Write> Writer<W> {
16081613
width: convert.unwrap_or(width),
16091614
};
16101615
let vector_size_str = common::vector_size_str(size);
1611-
let scalar_kind_str = scalar_kind_str(scalar);
1616+
let scalar_kind_str = scalar.to_wgsl_if_implemented()?;
16121617
if convert.is_some() {
16131618
write!(self.out, "vec{vector_size_str}<{scalar_kind_str}>")?;
16141619
} else {
@@ -1620,7 +1625,7 @@ impl<W: Write> Writer<W> {
16201625
kind,
16211626
width: convert.unwrap_or(width),
16221627
};
1623-
let scalar_kind_str = scalar_kind_str(scalar);
1628+
let scalar_kind_str = scalar.to_wgsl_if_implemented()?;
16241629
if convert.is_some() {
16251630
write!(self.out, "{scalar_kind_str}")?
16261631
} else {
@@ -1883,43 +1888,6 @@ const fn image_dimension_str(dim: crate::ImageDimension) -> &'static str {
18831888
}
18841889
}
18851890

1886-
const fn scalar_kind_str(scalar: crate::Scalar) -> &'static str {
1887-
use crate::Scalar;
1888-
use crate::ScalarKind as Sk;
1889-
1890-
match scalar {
1891-
Scalar {
1892-
kind: Sk::Float,
1893-
width: 8,
1894-
} => "f64",
1895-
Scalar {
1896-
kind: Sk::Float,
1897-
width: 4,
1898-
} => "f32",
1899-
Scalar {
1900-
kind: Sk::Sint,
1901-
width: 4,
1902-
} => "i32",
1903-
Scalar {
1904-
kind: Sk::Uint,
1905-
width: 4,
1906-
} => "u32",
1907-
Scalar {
1908-
kind: Sk::Sint,
1909-
width: 8,
1910-
} => "i64",
1911-
Scalar {
1912-
kind: Sk::Uint,
1913-
width: 8,
1914-
} => "u64",
1915-
Scalar {
1916-
kind: Sk::Bool,
1917-
width: 1,
1918-
} => "bool",
1919-
_ => unreachable!(),
1920-
}
1921-
}
1922-
19231891
const fn address_space_str(
19241892
space: crate::AddressSpace,
19251893
) -> (Option<&'static str>, Option<&'static str>) {

naga/src/common/wgsl.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,22 @@ impl ToWgsl for crate::StorageFormat {
305305
}
306306
}
307307
}
308+
309+
impl TryToWgsl for crate::Scalar {
310+
const DESCRIPTION: &'static str = "scalar type";
311+
312+
fn try_to_wgsl(self) -> Option<&'static str> {
313+
use crate::Scalar;
314+
315+
Some(match self {
316+
Scalar::F64 => "f64",
317+
Scalar::F32 => "f32",
318+
Scalar::I32 => "i32",
319+
Scalar::U32 => "u32",
320+
Scalar::I64 => "i64",
321+
Scalar::U64 => "u64",
322+
Scalar::BOOL => "bool",
323+
_ => return None,
324+
})
325+
}
326+
}

0 commit comments

Comments
 (0)