Skip to content

Commit 0b8f716

Browse files
committed
[naga] Generate special type for external texture params buffer
During wgsl lowering, if we encounter an external texture type then generate the `ExternalTextureParams` struct. This will be required by most Naga backends to implement external textures. This type is not actually used by wgsl-in or the IR. However, generating it in Naga IR ensures tricky details such as member alignment are handled for us. wgsl-out must ensure it does *not* generate code for this type, as it handles external textures natively.
1 parent 5827783 commit 0b8f716

39 files changed

+309
-41
lines changed

naga/src/back/wgsl/writer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ impl<W: Write> Writer<W> {
115115
.predeclared_types
116116
.values()
117117
.any(|t| *t == handle)
118+
|| Some(handle) == module.special_types.external_texture_params
118119
}
119120

120121
pub fn write(&mut self, module: &Module, info: &valid::ModuleInfo) -> BackendResult {

naga/src/compact/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ impl<'module> ModuleTracer<'module> {
380380
ref ray_intersection,
381381
ref ray_vertex_return,
382382
ref predeclared_types,
383+
ref external_texture_params,
383384
} = *special_types;
384385

385386
if let Some(ray_desc) = *ray_desc {
@@ -391,6 +392,9 @@ impl<'module> ModuleTracer<'module> {
391392
if let Some(ray_vertex_return) = *ray_vertex_return {
392393
self.types_used.insert(ray_vertex_return);
393394
}
395+
if let Some(external_texture_params) = *external_texture_params {
396+
self.types_used.insert(external_texture_params);
397+
}
394398
for (_, &handle) in predeclared_types {
395399
self.types_used.insert(handle);
396400
}
@@ -532,6 +536,7 @@ impl ModuleMap {
532536
ref mut ray_intersection,
533537
ref mut ray_vertex_return,
534538
ref mut predeclared_types,
539+
ref mut external_texture_params,
535540
} = *special;
536541

537542
if let Some(ref mut ray_desc) = *ray_desc {
@@ -545,6 +550,10 @@ impl ModuleMap {
545550
self.types.adjust(ray_vertex_return);
546551
}
547552

553+
if let Some(ref mut external_texture_params) = *external_texture_params {
554+
self.types.adjust(external_texture_params);
555+
}
556+
548557
for handle in predeclared_types.values_mut() {
549558
self.types.adjust(handle);
550559
}

naga/src/front/type_gen.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,91 @@ impl crate::Module {
276276
handle
277277
}
278278

279+
pub fn generate_external_texture_params_type(&mut self) -> Handle<crate::Type> {
280+
if let Some(handle) = self.special_types.external_texture_params {
281+
return handle;
282+
}
283+
284+
let ty_u32 = self.types.insert(
285+
crate::Type {
286+
name: None,
287+
inner: crate::TypeInner::Scalar(crate::Scalar::U32),
288+
},
289+
Span::UNDEFINED,
290+
);
291+
let ty_vec2u = self.types.insert(
292+
crate::Type {
293+
name: None,
294+
inner: crate::TypeInner::Vector {
295+
size: crate::VectorSize::Bi,
296+
scalar: crate::Scalar::U32,
297+
},
298+
},
299+
Span::UNDEFINED,
300+
);
301+
let ty_mat3x2f = self.types.insert(
302+
crate::Type {
303+
name: None,
304+
inner: crate::TypeInner::Matrix {
305+
columns: crate::VectorSize::Tri,
306+
rows: crate::VectorSize::Bi,
307+
scalar: crate::Scalar::F32,
308+
},
309+
},
310+
Span::UNDEFINED,
311+
);
312+
let ty_mat4x4f = self.types.insert(
313+
crate::Type {
314+
name: None,
315+
inner: crate::TypeInner::Matrix {
316+
columns: crate::VectorSize::Quad,
317+
rows: crate::VectorSize::Quad,
318+
scalar: crate::Scalar::F32,
319+
},
320+
},
321+
Span::UNDEFINED,
322+
);
323+
324+
let handle = self.types.insert(
325+
crate::Type {
326+
name: Some("NagaExternalTextureParams".to_string()),
327+
inner: crate::TypeInner::Struct {
328+
members: vec![
329+
crate::StructMember {
330+
name: Some("yuv_conversion_matrix".to_string()),
331+
ty: ty_mat4x4f,
332+
binding: None,
333+
offset: 0,
334+
},
335+
crate::StructMember {
336+
name: Some("sample_transform".to_string()),
337+
ty: ty_mat3x2f,
338+
binding: None,
339+
offset: 64,
340+
},
341+
crate::StructMember {
342+
name: Some("size".to_string()),
343+
ty: ty_vec2u,
344+
binding: None,
345+
offset: 88,
346+
},
347+
crate::StructMember {
348+
name: Some("num_planes".to_string()),
349+
ty: ty_u32,
350+
binding: None,
351+
offset: 96,
352+
},
353+
],
354+
span: 112,
355+
},
356+
},
357+
Span::UNDEFINED,
358+
);
359+
360+
self.special_types.external_texture_params = Some(handle);
361+
handle
362+
}
363+
279364
/// Populate this module's [`SpecialTypes::predeclared_types`] type and return the handle.
280365
///
281366
/// [`SpecialTypes::predeclared_types`]: crate::SpecialTypes::predeclared_types

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3950,11 +3950,21 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
39503950
dim,
39513951
arrayed,
39523952
class,
3953-
} => ir::TypeInner::Image {
3954-
dim,
3955-
arrayed,
3956-
class,
3957-
},
3953+
} => {
3954+
if class == crate::ImageClass::External {
3955+
// Generate the type for the params buffer that will be
3956+
// used by most backends to implement external textures.
3957+
// This is *not* the type we are resolving, but the fact we
3958+
// are resolving a `texture_external` implies the backends
3959+
// will need the params buffer too.
3960+
ctx.module.generate_external_texture_params_type();
3961+
}
3962+
ir::TypeInner::Image {
3963+
dim,
3964+
arrayed,
3965+
class,
3966+
}
3967+
}
39583968
ast::Type::Sampler { comparison } => ir::TypeInner::Sampler { comparison },
39593969
ast::Type::AccelerationStructure { vertex_return } => {
39603970
ir::TypeInner::AccelerationStructure { vertex_return }

naga/src/ir/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,13 @@ pub struct SpecialTypes {
23342334
/// Call [`Module::generate_vertex_return_type`]
23352335
pub ray_vertex_return: Option<Handle<Type>>,
23362336

2337+
/// Struct containing parameters required by some backends to emit code for
2338+
/// [`ImageClass::External`] textures.
2339+
///
2340+
/// Call [`Module::generate_external_texture_params_type`] to populate this
2341+
/// if needed and return the handle.
2342+
pub external_texture_params: Option<Handle<Type>>,
2343+
23372344
/// Types for predeclared wgsl types instantiated on demand.
23382345
///
23392346
/// Call [`Module::generate_predeclared_type`] to populate this if

naga/tests/out/ir/spv-fetch_depth.compact.ron

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
ray_desc: None,
6868
ray_intersection: None,
6969
ray_vertex_return: None,
70+
external_texture_params: None,
7071
predeclared_types: {},
7172
),
7273
constants: [

naga/tests/out/ir/spv-fetch_depth.ron

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
ray_desc: None,
131131
ray_intersection: None,
132132
ray_vertex_return: None,
133+
external_texture_params: None,
133134
predeclared_types: {},
134135
),
135136
constants: [

naga/tests/out/ir/spv-shadow.compact.ron

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
ray_desc: None,
156156
ray_intersection: None,
157157
ray_vertex_return: None,
158+
external_texture_params: None,
158159
predeclared_types: {},
159160
),
160161
constants: [

naga/tests/out/ir/spv-shadow.ron

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@
278278
ray_desc: None,
279279
ray_intersection: None,
280280
ray_vertex_return: None,
281+
external_texture_params: None,
281282
predeclared_types: {},
282283
),
283284
constants: [

naga/tests/out/ir/spv-spec-constants.compact.ron

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
ray_desc: None,
172172
ray_intersection: None,
173173
ray_vertex_return: None,
174+
external_texture_params: None,
174175
predeclared_types: {},
175176
),
176177
constants: [

0 commit comments

Comments
 (0)