1use crate::proc::GlobalCtx;
4use crate::Handle;
5
6impl crate::proc::TypeResolution {
7 pub fn to_wgsl(&self, gctx: &GlobalCtx) -> String {
8 match *self {
9 crate::proc::TypeResolution::Handle(handle) => handle.to_wgsl(gctx),
10 crate::proc::TypeResolution::Value(ref inner) => inner.to_wgsl(gctx),
11 }
12 }
13}
14
15impl Handle<crate::Type> {
16 pub fn to_wgsl(self, gctx: &GlobalCtx) -> String {
20 let ty = &gctx.types[self];
21 match ty.name {
22 Some(ref name) => name.clone(),
23 None => ty.inner.to_wgsl(gctx),
24 }
25 }
26}
27
28impl crate::TypeInner {
29 pub fn to_wgsl(&self, gctx: &GlobalCtx) -> String {
37 use crate::TypeInner as Ti;
38
39 match *self {
40 Ti::Scalar(scalar) => scalar.to_wgsl(),
41 Ti::Vector { size, scalar } => {
42 format!("vec{}<{}>", size as u32, scalar.to_wgsl())
43 }
44 Ti::Matrix {
45 columns,
46 rows,
47 scalar,
48 } => {
49 format!(
50 "mat{}x{}<{}>",
51 columns as u32,
52 rows as u32,
53 scalar.to_wgsl(),
54 )
55 }
56 Ti::Atomic(scalar) => {
57 format!("atomic<{}>", scalar.to_wgsl())
58 }
59 Ti::Pointer { base, .. } => {
60 let name = base.to_wgsl(gctx);
61 format!("ptr<{name}>")
62 }
63 Ti::ValuePointer { scalar, .. } => {
64 format!("ptr<{}>", scalar.to_wgsl())
65 }
66 Ti::Array { base, size, .. } => {
67 let base = base.to_wgsl(gctx);
68 match size {
69 crate::ArraySize::Constant(size) => format!("array<{base}, {size}>"),
70 crate::ArraySize::Dynamic => format!("array<{base}>"),
71 }
72 }
73 Ti::Struct { .. } => {
74 "struct".to_string()
76 }
77 Ti::Image {
78 dim,
79 arrayed,
80 class,
81 } => {
82 let dim_suffix = match dim {
83 crate::ImageDimension::D1 => "_1d",
84 crate::ImageDimension::D2 => "_2d",
85 crate::ImageDimension::D3 => "_3d",
86 crate::ImageDimension::Cube => "_cube",
87 };
88 let array_suffix = if arrayed { "_array" } else { "" };
89
90 let class_suffix = match class {
91 crate::ImageClass::Sampled { multi: true, .. } => "_multisampled",
92 crate::ImageClass::Depth { multi: false } => "_depth",
93 crate::ImageClass::Depth { multi: true } => "_depth_multisampled",
94 crate::ImageClass::Sampled { multi: false, .. }
95 | crate::ImageClass::Storage { .. } => "",
96 };
97
98 let type_in_brackets = match class {
99 crate::ImageClass::Sampled { kind, .. } => {
100 let element_type = crate::Scalar { kind, width: 4 }.to_wgsl();
104 format!("<{element_type}>")
105 }
106 crate::ImageClass::Depth { multi: _ } => String::new(),
107 crate::ImageClass::Storage { format, access } => {
108 if access.contains(crate::StorageAccess::STORE) {
109 format!("<{},write>", format.to_wgsl())
110 } else {
111 format!("<{}>", format.to_wgsl())
112 }
113 }
114 };
115
116 format!("texture{class_suffix}{dim_suffix}{array_suffix}{type_in_brackets}")
117 }
118 Ti::Sampler { .. } => "sampler".to_string(),
119 Ti::AccelerationStructure => "acceleration_structure".to_string(),
120 Ti::RayQuery => "ray_query".to_string(),
121 Ti::BindingArray { base, size, .. } => {
122 let member_type = &gctx.types[base];
123 let base = member_type.name.as_deref().unwrap_or("unknown");
124 match size {
125 crate::ArraySize::Constant(size) => format!("binding_array<{base}, {size}>"),
126 crate::ArraySize::Dynamic => format!("binding_array<{base}>"),
127 }
128 }
129 }
130 }
131}
132
133impl crate::Scalar {
134 pub fn to_wgsl(self) -> String {
138 let prefix = match self.kind {
139 crate::ScalarKind::Sint => "i",
140 crate::ScalarKind::Uint => "u",
141 crate::ScalarKind::Float => "f",
142 crate::ScalarKind::Bool => return "bool".to_string(),
143 crate::ScalarKind::AbstractInt => return "{AbstractInt}".to_string(),
144 crate::ScalarKind::AbstractFloat => return "{AbstractFloat}".to_string(),
145 };
146 format!("{}{}", prefix, self.width * 8)
147 }
148}
149
150impl crate::StorageFormat {
151 pub const fn to_wgsl(self) -> &'static str {
152 use crate::StorageFormat as Sf;
153 match self {
154 Sf::R8Unorm => "r8unorm",
155 Sf::R8Snorm => "r8snorm",
156 Sf::R8Uint => "r8uint",
157 Sf::R8Sint => "r8sint",
158 Sf::R16Uint => "r16uint",
159 Sf::R16Sint => "r16sint",
160 Sf::R16Float => "r16float",
161 Sf::Rg8Unorm => "rg8unorm",
162 Sf::Rg8Snorm => "rg8snorm",
163 Sf::Rg8Uint => "rg8uint",
164 Sf::Rg8Sint => "rg8sint",
165 Sf::R32Uint => "r32uint",
166 Sf::R32Sint => "r32sint",
167 Sf::R32Float => "r32float",
168 Sf::Rg16Uint => "rg16uint",
169 Sf::Rg16Sint => "rg16sint",
170 Sf::Rg16Float => "rg16float",
171 Sf::Rgba8Unorm => "rgba8unorm",
172 Sf::Rgba8Snorm => "rgba8snorm",
173 Sf::Rgba8Uint => "rgba8uint",
174 Sf::Rgba8Sint => "rgba8sint",
175 Sf::Bgra8Unorm => "bgra8unorm",
176 Sf::Rgb10a2Uint => "rgb10a2uint",
177 Sf::Rgb10a2Unorm => "rgb10a2unorm",
178 Sf::Rg11b10Ufloat => "rg11b10float",
179 Sf::Rg32Uint => "rg32uint",
180 Sf::Rg32Sint => "rg32sint",
181 Sf::Rg32Float => "rg32float",
182 Sf::Rgba16Uint => "rgba16uint",
183 Sf::Rgba16Sint => "rgba16sint",
184 Sf::Rgba16Float => "rgba16float",
185 Sf::Rgba32Uint => "rgba32uint",
186 Sf::Rgba32Sint => "rgba32sint",
187 Sf::Rgba32Float => "rgba32float",
188 Sf::R16Unorm => "r16unorm",
189 Sf::R16Snorm => "r16snorm",
190 Sf::Rg16Unorm => "rg16unorm",
191 Sf::Rg16Snorm => "rg16snorm",
192 Sf::Rgba16Unorm => "rgba16unorm",
193 Sf::Rgba16Snorm => "rgba16snorm",
194 }
195 }
196}
197
198mod tests {
199 #[test]
200 fn to_wgsl() {
201 use std::num::NonZeroU32;
202
203 let mut types = crate::UniqueArena::new();
204
205 let mytype1 = types.insert(
206 crate::Type {
207 name: Some("MyType1".to_string()),
208 inner: crate::TypeInner::Struct {
209 members: vec![],
210 span: 0,
211 },
212 },
213 Default::default(),
214 );
215 let mytype2 = types.insert(
216 crate::Type {
217 name: Some("MyType2".to_string()),
218 inner: crate::TypeInner::Struct {
219 members: vec![],
220 span: 0,
221 },
222 },
223 Default::default(),
224 );
225
226 let gctx = crate::proc::GlobalCtx {
227 types: &types,
228 constants: &crate::Arena::new(),
229 overrides: &crate::Arena::new(),
230 global_expressions: &crate::Arena::new(),
231 };
232 let array = crate::TypeInner::Array {
233 base: mytype1,
234 stride: 4,
235 size: crate::ArraySize::Constant(unsafe { NonZeroU32::new_unchecked(32) }),
236 };
237 assert_eq!(array.to_wgsl(&gctx), "array<MyType1, 32>");
238
239 let mat = crate::TypeInner::Matrix {
240 rows: crate::VectorSize::Quad,
241 columns: crate::VectorSize::Bi,
242 scalar: crate::Scalar::F64,
243 };
244 assert_eq!(mat.to_wgsl(&gctx), "mat2x4<f64>");
245
246 let ptr = crate::TypeInner::Pointer {
247 base: mytype2,
248 space: crate::AddressSpace::Storage {
249 access: crate::StorageAccess::default(),
250 },
251 };
252 assert_eq!(ptr.to_wgsl(&gctx), "ptr<MyType2>");
253
254 let img1 = crate::TypeInner::Image {
255 dim: crate::ImageDimension::D2,
256 arrayed: false,
257 class: crate::ImageClass::Sampled {
258 kind: crate::ScalarKind::Float,
259 multi: true,
260 },
261 };
262 assert_eq!(img1.to_wgsl(&gctx), "texture_multisampled_2d<f32>");
263
264 let img2 = crate::TypeInner::Image {
265 dim: crate::ImageDimension::Cube,
266 arrayed: true,
267 class: crate::ImageClass::Depth { multi: false },
268 };
269 assert_eq!(img2.to_wgsl(&gctx), "texture_depth_cube_array");
270
271 let img3 = crate::TypeInner::Image {
272 dim: crate::ImageDimension::D2,
273 arrayed: false,
274 class: crate::ImageClass::Depth { multi: true },
275 };
276 assert_eq!(img3.to_wgsl(&gctx), "texture_depth_multisampled_2d");
277
278 let array = crate::TypeInner::BindingArray {
279 base: mytype1,
280 size: crate::ArraySize::Constant(unsafe { NonZeroU32::new_unchecked(32) }),
281 };
282 assert_eq!(array.to_wgsl(&gctx), "binding_array<MyType1, 32>");
283 }
284}