Skip to content

Commit 611c3c9

Browse files
committed
fixup! [naga] Generate special type for external texture params buffer
1 parent 2084c01 commit 611c3c9

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

naga/src/back/wgsl/writer.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,28 @@ impl<W: Write> Writer<W> {
109109
self.required_polyfills.clear();
110110
}
111111

112-
fn is_builtin_wgsl_struct(&self, module: &Module, handle: Handle<crate::Type>) -> bool {
112+
/// Determine if `ty` is the Naga IR presentation of a WGSL builtin type.
113+
///
114+
/// Return true if `ty` refers to the Naga IR form of a WGSL builtin type
115+
/// like `__atomic_compare_exchange_result`.
116+
///
117+
/// Even though the module may use the type, the WGSL backend should avoid
118+
/// emitting a definition for it, since it is [predeclared] in WGSL.
119+
///
120+
/// This also covers types like [`NagaExternalTextureParams`], which other
121+
/// backends use to lower WGSL constructs like external textures to their
122+
/// implementations. WGSL can express these directly, so the types need not
123+
/// be emitted.
124+
///
125+
/// [predeclared]: https://www.w3.org/TR/WGSL/#predeclared
126+
/// [`NagaExternalTextureParams`]: crate::ir::SpecialTypes::external_texture_params
127+
fn is_builtin_wgsl_struct(&self, module: &Module, ty: Handle<crate::Type>) -> bool {
113128
module
114129
.special_types
115130
.predeclared_types
116131
.values()
117-
.any(|t| *t == handle)
118-
|| Some(handle) == module.special_types.external_texture_params
132+
.any(|t| *t == ty)
133+
|| Some(ty) == module.special_types.external_texture_params
119134
}
120135

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

naga/src/front/type_gen.rs

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

279+
/// Generate [`SpecialTypes::external_texture_params`].
280+
///
281+
/// [`SpecialTypes::external_texture_params`]: crate::ir::SpecialTypes::external_texture_params
279282
pub fn generate_external_texture_params_type(&mut self) -> Handle<crate::Type> {
280283
if let Some(handle) = self.special_types.external_texture_params {
281284
return handle;

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3985,11 +3985,19 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
39853985
class,
39863986
} => {
39873987
if class == crate::ImageClass::External {
3988-
// Generate the type for the params buffer that will be
3989-
// used by most backends to implement external textures.
3990-
// This is *not* the type we are resolving, but the fact we
3991-
// are resolving a `texture_external` implies the backends
3992-
// will need the params buffer too.
3988+
// Other than the WGSL backend, every backend that supports
3989+
// external textures does so by lowering them to a set of
3990+
// ordinary textures and some parameters saying how to
3991+
// sample from them. We don't know which backend will
3992+
// consume the `Module` we're building, but in case it's not
3993+
// WGSL, populate `SpecialTypes::external_texture_params`
3994+
// with the type the backend will use for the parameter
3995+
// buffer.
3996+
//
3997+
// This is *not* the type we are lowering here: that's an
3998+
// ordinary `TypeInne::Image`. But the fact we are lowering
3999+
// a `texture_external` implies the backends may need
4000+
// `SpecialTypes::external_texture_params` too.
39934001
ctx.module.generate_external_texture_params_type();
39944002
}
39954003
ir::TypeInner::Image {

naga/src/ir/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,6 +2349,18 @@ pub struct SpecialTypes {
23492349
/// Struct containing parameters required by some backends to emit code for
23502350
/// [`ImageClass::External`] textures.
23512351
///
2352+
/// In WGSL, this type would be:
2353+
///
2354+
/// ```ignore
2355+
/// struct NagaExternalTextureParams { // align size offset
2356+
/// yuv_conversion_matrix: mat4x4<f32>, // 16 64 0
2357+
/// sample_transform: mat3x2<f32>, // 8 24 64
2358+
/// load_transform: mat3x2<f32>, // 8 24 88
2359+
/// size: vec2<u32>, // 8 8 112
2360+
/// num_planes: u32, // 4 4 120
2361+
/// } // whole struct: 16 128
2362+
/// ```
2363+
///
23522364
/// Call [`Module::generate_external_texture_params_type`] to populate this
23532365
/// if needed and return the handle.
23542366
pub external_texture_params: Option<Handle<Type>>,

0 commit comments

Comments
 (0)