From 9f654c623533c0b58ebd6ee5ac2b9524bd5034e4 Mon Sep 17 00:00:00 2001 From: Jamie Nicol Date: Mon, 2 Jun 2025 17:21:23 +0100 Subject: [PATCH] [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. --- naga/src/back/wgsl/writer.rs | 20 +++- naga/src/compact/mod.rs | 12 ++ naga/src/front/type_gen.rs | 94 ++++++++++++++++ naga/src/front/wgsl/lower/mod.rs | 28 ++++- naga/src/ir/mod.rs | 22 ++++ naga/tests/out/ir/spv-fetch_depth.compact.ron | 1 + naga/tests/out/ir/spv-fetch_depth.ron | 1 + naga/tests/out/ir/spv-shadow.compact.ron | 1 + naga/tests/out/ir/spv-shadow.ron | 1 + .../out/ir/spv-spec-constants.compact.ron | 1 + naga/tests/out/ir/spv-spec-constants.ron | 1 + naga/tests/out/ir/wgsl-access.compact.ron | 1 + naga/tests/out/ir/wgsl-access.ron | 1 + naga/tests/out/ir/wgsl-collatz.compact.ron | 1 + naga/tests/out/ir/wgsl-collatz.ron | 1 + .../out/ir/wgsl-const_assert.compact.ron | 1 + naga/tests/out/ir/wgsl-const_assert.ron | 1 + .../out/ir/wgsl-diagnostic-filter.compact.ron | 1 + naga/tests/out/ir/wgsl-diagnostic-filter.ron | 1 + .../out/ir/wgsl-index-by-value.compact.ron | 1 + naga/tests/out/ir/wgsl-index-by-value.ron | 1 + .../tests/out/ir/wgsl-local-const.compact.ron | 1 + naga/tests/out/ir/wgsl-local-const.ron | 1 + naga/tests/out/ir/wgsl-must-use.compact.ron | 1 + naga/tests/out/ir/wgsl-must-use.ron | 1 + ...ides-atomicCompareExchangeWeak.compact.ron | 1 + ...sl-overrides-atomicCompareExchangeWeak.ron | 1 + .../ir/wgsl-overrides-ray-query.compact.ron | 1 + .../tests/out/ir/wgsl-overrides-ray-query.ron | 1 + naga/tests/out/ir/wgsl-overrides.compact.ron | 1 + naga/tests/out/ir/wgsl-overrides.ron | 1 + .../out/ir/wgsl-storage-textures.compact.ron | 1 + naga/tests/out/ir/wgsl-storage-textures.ron | 1 + ...l-template-list-trailing-comma.compact.ron | 1 + .../ir/wgsl-template-list-trailing-comma.ron | 1 + .../out/ir/wgsl-texture-external.compact.ron | 106 ++++++++++++++---- naga/tests/out/ir/wgsl-texture-external.ron | 106 ++++++++++++++---- .../ir/wgsl-types_with_comments.compact.ron | 1 + .../tests/out/ir/wgsl-types_with_comments.ron | 1 + wgpu-core/src/device/resource.rs | 13 ++- 40 files changed, 386 insertions(+), 47 deletions(-) diff --git a/naga/src/back/wgsl/writer.rs b/naga/src/back/wgsl/writer.rs index 58c1b734f..dd2696c17 100644 --- a/naga/src/back/wgsl/writer.rs +++ b/naga/src/back/wgsl/writer.rs @@ -109,12 +109,28 @@ impl Writer { self.required_polyfills.clear(); } - fn is_builtin_wgsl_struct(&self, module: &Module, handle: Handle) -> bool { + /// Determine if `ty` is the Naga IR presentation of a WGSL builtin type. + /// + /// Return true if `ty` refers to the Naga IR form of a WGSL builtin type + /// like `__atomic_compare_exchange_result`. + /// + /// Even though the module may use the type, the WGSL backend should avoid + /// emitting a definition for it, since it is [predeclared] in WGSL. + /// + /// This also covers types like [`NagaExternalTextureParams`], which other + /// backends use to lower WGSL constructs like external textures to their + /// implementations. WGSL can express these directly, so the types need not + /// be emitted. + /// + /// [predeclared]: https://www.w3.org/TR/WGSL/#predeclared + /// [`NagaExternalTextureParams`]: crate::ir::SpecialTypes::external_texture_params + fn is_builtin_wgsl_struct(&self, module: &Module, ty: Handle) -> bool { module .special_types .predeclared_types .values() - .any(|t| *t == handle) + .any(|t| *t == ty) + || Some(ty) == module.special_types.external_texture_params } pub fn write(&mut self, module: &Module, info: &valid::ModuleInfo) -> BackendResult { diff --git a/naga/src/compact/mod.rs b/naga/src/compact/mod.rs index fe4296be8..7fbb5c532 100644 --- a/naga/src/compact/mod.rs +++ b/naga/src/compact/mod.rs @@ -380,6 +380,7 @@ impl<'module> ModuleTracer<'module> { ref ray_intersection, ref ray_vertex_return, ref predeclared_types, + ref external_texture_params, } = *special_types; if let Some(ray_desc) = *ray_desc { @@ -391,6 +392,12 @@ impl<'module> ModuleTracer<'module> { if let Some(ray_vertex_return) = *ray_vertex_return { self.types_used.insert(ray_vertex_return); } + // The `external_texture_params` type is generated purely as a + // convenience to the backends. While it will never actually be used in + // the IR, it must be marked as used so that it survives compaction. + if let Some(external_texture_params) = *external_texture_params { + self.types_used.insert(external_texture_params); + } for (_, &handle) in predeclared_types { self.types_used.insert(handle); } @@ -532,6 +539,7 @@ impl ModuleMap { ref mut ray_intersection, ref mut ray_vertex_return, ref mut predeclared_types, + ref mut external_texture_params, } = *special; if let Some(ref mut ray_desc) = *ray_desc { @@ -545,6 +553,10 @@ impl ModuleMap { self.types.adjust(ray_vertex_return); } + if let Some(ref mut external_texture_params) = *external_texture_params { + self.types.adjust(external_texture_params); + } + for handle in predeclared_types.values_mut() { self.types.adjust(handle); } diff --git a/naga/src/front/type_gen.rs b/naga/src/front/type_gen.rs index 9a01b637d..d58561796 100644 --- a/naga/src/front/type_gen.rs +++ b/naga/src/front/type_gen.rs @@ -276,6 +276,100 @@ impl crate::Module { handle } + /// Generate [`SpecialTypes::external_texture_params`]. + /// + /// [`SpecialTypes::external_texture_params`]: crate::ir::SpecialTypes::external_texture_params + pub fn generate_external_texture_params_type(&mut self) -> Handle { + if let Some(handle) = self.special_types.external_texture_params { + return handle; + } + + let ty_u32 = self.types.insert( + crate::Type { + name: None, + inner: crate::TypeInner::Scalar(crate::Scalar::U32), + }, + Span::UNDEFINED, + ); + let ty_vec2u = self.types.insert( + crate::Type { + name: None, + inner: crate::TypeInner::Vector { + size: crate::VectorSize::Bi, + scalar: crate::Scalar::U32, + }, + }, + Span::UNDEFINED, + ); + let ty_mat3x2f = self.types.insert( + crate::Type { + name: None, + inner: crate::TypeInner::Matrix { + columns: crate::VectorSize::Tri, + rows: crate::VectorSize::Bi, + scalar: crate::Scalar::F32, + }, + }, + Span::UNDEFINED, + ); + let ty_mat4x4f = self.types.insert( + crate::Type { + name: None, + inner: crate::TypeInner::Matrix { + columns: crate::VectorSize::Quad, + rows: crate::VectorSize::Quad, + scalar: crate::Scalar::F32, + }, + }, + Span::UNDEFINED, + ); + + let handle = self.types.insert( + crate::Type { + name: Some("NagaExternalTextureParams".to_string()), + inner: crate::TypeInner::Struct { + members: vec![ + crate::StructMember { + name: Some("yuv_conversion_matrix".to_string()), + ty: ty_mat4x4f, + binding: None, + offset: 0, + }, + crate::StructMember { + name: Some("sample_transform".to_string()), + ty: ty_mat3x2f, + binding: None, + offset: 64, + }, + crate::StructMember { + name: Some("load_transform".to_string()), + ty: ty_mat3x2f, + binding: None, + offset: 88, + }, + crate::StructMember { + name: Some("size".to_string()), + ty: ty_vec2u, + binding: None, + offset: 112, + }, + crate::StructMember { + name: Some("num_planes".to_string()), + ty: ty_u32, + binding: None, + offset: 120, + }, + ], + span: 128, + }, + }, + Span::UNDEFINED, + ); + + self.special_types.external_texture_params = Some(handle); + handle + } + /// Populate this module's [`SpecialTypes::predeclared_types`] type and return the handle. /// /// [`SpecialTypes::predeclared_types`]: crate::SpecialTypes::predeclared_types diff --git a/naga/src/front/wgsl/lower/mod.rs b/naga/src/front/wgsl/lower/mod.rs index c8545ca65..3b6e7806c 100644 --- a/naga/src/front/wgsl/lower/mod.rs +++ b/naga/src/front/wgsl/lower/mod.rs @@ -3983,11 +3983,29 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { dim, arrayed, class, - } => ir::TypeInner::Image { - dim, - arrayed, - class, - }, + } => { + if class == crate::ImageClass::External { + // Other than the WGSL backend, every backend that supports + // external textures does so by lowering them to a set of + // ordinary textures and some parameters saying how to + // sample from them. We don't know which backend will + // consume the `Module` we're building, but in case it's not + // WGSL, populate `SpecialTypes::external_texture_params` + // with the type the backend will use for the parameter + // buffer. + // + // This is *not* the type we are lowering here: that's an + // ordinary `TypeInner::Image`. But the fact we are + // lowering a `texture_external` implies the backends may + // need `SpecialTypes::external_texture_params` too. + ctx.module.generate_external_texture_params_type(); + } + ir::TypeInner::Image { + dim, + arrayed, + class, + } + } ast::Type::Sampler { comparison } => ir::TypeInner::Sampler { comparison }, ast::Type::AccelerationStructure { vertex_return } => { ir::TypeInner::AccelerationStructure { vertex_return } diff --git a/naga/src/ir/mod.rs b/naga/src/ir/mod.rs index 010195484..1744ff92f 100644 --- a/naga/src/ir/mod.rs +++ b/naga/src/ir/mod.rs @@ -2346,6 +2346,28 @@ pub struct SpecialTypes { /// Call [`Module::generate_vertex_return_type`] pub ray_vertex_return: Option>, + /// Struct containing parameters required by some backends to emit code for + /// [`ImageClass::External`] textures. + /// + /// See `wgpu_core::device::resource::ExternalTextureParams` for the + /// documentation of each field. + /// + /// In WGSL, this type would be: + /// + /// ```ignore + /// struct NagaExternalTextureParams { // align size offset + /// yuv_conversion_matrix: mat4x4, // 16 64 0 + /// sample_transform: mat3x2, // 8 24 64 + /// load_transform: mat3x2, // 8 24 88 + /// size: vec2, // 8 8 112 + /// num_planes: u32, // 4 4 120 + /// } // whole struct: 16 128 + /// ``` + /// + /// Call [`Module::generate_external_texture_params_type`] to populate this + /// if needed and return the handle. + pub external_texture_params: Option>, + /// Types for predeclared wgsl types instantiated on demand. /// /// Call [`Module::generate_predeclared_type`] to populate this if diff --git a/naga/tests/out/ir/spv-fetch_depth.compact.ron b/naga/tests/out/ir/spv-fetch_depth.compact.ron index 024f91802..c2b7b9b5f 100644 --- a/naga/tests/out/ir/spv-fetch_depth.compact.ron +++ b/naga/tests/out/ir/spv-fetch_depth.compact.ron @@ -67,6 +67,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [ diff --git a/naga/tests/out/ir/spv-fetch_depth.ron b/naga/tests/out/ir/spv-fetch_depth.ron index 8daa29847..dd019d62a 100644 --- a/naga/tests/out/ir/spv-fetch_depth.ron +++ b/naga/tests/out/ir/spv-fetch_depth.ron @@ -130,6 +130,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [ diff --git a/naga/tests/out/ir/spv-shadow.compact.ron b/naga/tests/out/ir/spv-shadow.compact.ron index 7f4e6df5c..04547e651 100644 --- a/naga/tests/out/ir/spv-shadow.compact.ron +++ b/naga/tests/out/ir/spv-shadow.compact.ron @@ -155,6 +155,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [ diff --git a/naga/tests/out/ir/spv-shadow.ron b/naga/tests/out/ir/spv-shadow.ron index 35a9e48be..0babda31f 100644 --- a/naga/tests/out/ir/spv-shadow.ron +++ b/naga/tests/out/ir/spv-shadow.ron @@ -278,6 +278,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [ diff --git a/naga/tests/out/ir/spv-spec-constants.compact.ron b/naga/tests/out/ir/spv-spec-constants.compact.ron index 698a80725..a07dd0aca 100644 --- a/naga/tests/out/ir/spv-spec-constants.compact.ron +++ b/naga/tests/out/ir/spv-spec-constants.compact.ron @@ -171,6 +171,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [ diff --git a/naga/tests/out/ir/spv-spec-constants.ron b/naga/tests/out/ir/spv-spec-constants.ron index 5afd71ed6..643d3c730 100644 --- a/naga/tests/out/ir/spv-spec-constants.ron +++ b/naga/tests/out/ir/spv-spec-constants.ron @@ -262,6 +262,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [ diff --git a/naga/tests/out/ir/wgsl-access.compact.ron b/naga/tests/out/ir/wgsl-access.compact.ron index 5e15ff84c..fff8d0bcf 100644 --- a/naga/tests/out/ir/wgsl-access.compact.ron +++ b/naga/tests/out/ir/wgsl-access.compact.ron @@ -421,6 +421,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-access.ron b/naga/tests/out/ir/wgsl-access.ron index 5e15ff84c..fff8d0bcf 100644 --- a/naga/tests/out/ir/wgsl-access.ron +++ b/naga/tests/out/ir/wgsl-access.ron @@ -421,6 +421,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-collatz.compact.ron b/naga/tests/out/ir/wgsl-collatz.compact.ron index ce0d6d8c8..30168b262 100644 --- a/naga/tests/out/ir/wgsl-collatz.compact.ron +++ b/naga/tests/out/ir/wgsl-collatz.compact.ron @@ -44,6 +44,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-collatz.ron b/naga/tests/out/ir/wgsl-collatz.ron index ce0d6d8c8..30168b262 100644 --- a/naga/tests/out/ir/wgsl-collatz.ron +++ b/naga/tests/out/ir/wgsl-collatz.ron @@ -44,6 +44,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-const_assert.compact.ron b/naga/tests/out/ir/wgsl-const_assert.compact.ron index c10c9f97d..4d77a5749 100644 --- a/naga/tests/out/ir/wgsl-const_assert.compact.ron +++ b/naga/tests/out/ir/wgsl-const_assert.compact.ron @@ -4,6 +4,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-const_assert.ron b/naga/tests/out/ir/wgsl-const_assert.ron index c10c9f97d..4d77a5749 100644 --- a/naga/tests/out/ir/wgsl-const_assert.ron +++ b/naga/tests/out/ir/wgsl-const_assert.ron @@ -4,6 +4,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-diagnostic-filter.compact.ron b/naga/tests/out/ir/wgsl-diagnostic-filter.compact.ron index dc4d2defd..10f533f10 100644 --- a/naga/tests/out/ir/wgsl-diagnostic-filter.compact.ron +++ b/naga/tests/out/ir/wgsl-diagnostic-filter.compact.ron @@ -4,6 +4,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-diagnostic-filter.ron b/naga/tests/out/ir/wgsl-diagnostic-filter.ron index dc4d2defd..10f533f10 100644 --- a/naga/tests/out/ir/wgsl-diagnostic-filter.ron +++ b/naga/tests/out/ir/wgsl-diagnostic-filter.ron @@ -4,6 +4,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-index-by-value.compact.ron b/naga/tests/out/ir/wgsl-index-by-value.compact.ron index 8f3de4f3e..31a601054 100644 --- a/naga/tests/out/ir/wgsl-index-by-value.compact.ron +++ b/naga/tests/out/ir/wgsl-index-by-value.compact.ron @@ -81,6 +81,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-index-by-value.ron b/naga/tests/out/ir/wgsl-index-by-value.ron index 8f3de4f3e..31a601054 100644 --- a/naga/tests/out/ir/wgsl-index-by-value.ron +++ b/naga/tests/out/ir/wgsl-index-by-value.ron @@ -81,6 +81,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-local-const.compact.ron b/naga/tests/out/ir/wgsl-local-const.compact.ron index 8bce0bb00..b7deb55ac 100644 --- a/naga/tests/out/ir/wgsl-local-const.compact.ron +++ b/naga/tests/out/ir/wgsl-local-const.compact.ron @@ -26,6 +26,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [ diff --git a/naga/tests/out/ir/wgsl-local-const.ron b/naga/tests/out/ir/wgsl-local-const.ron index 8bce0bb00..b7deb55ac 100644 --- a/naga/tests/out/ir/wgsl-local-const.ron +++ b/naga/tests/out/ir/wgsl-local-const.ron @@ -26,6 +26,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [ diff --git a/naga/tests/out/ir/wgsl-must-use.compact.ron b/naga/tests/out/ir/wgsl-must-use.compact.ron index 0534e7363..4d148b906 100644 --- a/naga/tests/out/ir/wgsl-must-use.compact.ron +++ b/naga/tests/out/ir/wgsl-must-use.compact.ron @@ -12,6 +12,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-must-use.ron b/naga/tests/out/ir/wgsl-must-use.ron index 0534e7363..4d148b906 100644 --- a/naga/tests/out/ir/wgsl-must-use.ron +++ b/naga/tests/out/ir/wgsl-must-use.ron @@ -12,6 +12,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.compact.ron b/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.compact.ron index ac45d5bad..1e568ebbc 100644 --- a/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.compact.ron +++ b/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.compact.ron @@ -53,6 +53,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: { AtomicCompareExchangeWeakResult(( kind: Uint, diff --git a/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.ron b/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.ron index ac45d5bad..1e568ebbc 100644 --- a/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.ron +++ b/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.ron @@ -53,6 +53,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: { AtomicCompareExchangeWeakResult(( kind: Uint, diff --git a/naga/tests/out/ir/wgsl-overrides-ray-query.compact.ron b/naga/tests/out/ir/wgsl-overrides-ray-query.compact.ron index 0848ed2b7..649ba28d4 100644 --- a/naga/tests/out/ir/wgsl-overrides-ray-query.compact.ron +++ b/naga/tests/out/ir/wgsl-overrides-ray-query.compact.ron @@ -85,6 +85,7 @@ ray_desc: Some(5), ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-overrides-ray-query.ron b/naga/tests/out/ir/wgsl-overrides-ray-query.ron index 0848ed2b7..649ba28d4 100644 --- a/naga/tests/out/ir/wgsl-overrides-ray-query.ron +++ b/naga/tests/out/ir/wgsl-overrides-ray-query.ron @@ -85,6 +85,7 @@ ray_desc: Some(5), ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-overrides.compact.ron b/naga/tests/out/ir/wgsl-overrides.compact.ron index 4c40ef32d..eca515610 100644 --- a/naga/tests/out/ir/wgsl-overrides.compact.ron +++ b/naga/tests/out/ir/wgsl-overrides.compact.ron @@ -26,6 +26,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-overrides.ron b/naga/tests/out/ir/wgsl-overrides.ron index 4c40ef32d..eca515610 100644 --- a/naga/tests/out/ir/wgsl-overrides.ron +++ b/naga/tests/out/ir/wgsl-overrides.ron @@ -26,6 +26,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-storage-textures.compact.ron b/naga/tests/out/ir/wgsl-storage-textures.compact.ron index 4f1325da5..eb70e3bad 100644 --- a/naga/tests/out/ir/wgsl-storage-textures.compact.ron +++ b/naga/tests/out/ir/wgsl-storage-textures.compact.ron @@ -71,6 +71,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-storage-textures.ron b/naga/tests/out/ir/wgsl-storage-textures.ron index 4f1325da5..eb70e3bad 100644 --- a/naga/tests/out/ir/wgsl-storage-textures.ron +++ b/naga/tests/out/ir/wgsl-storage-textures.ron @@ -71,6 +71,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-template-list-trailing-comma.compact.ron b/naga/tests/out/ir/wgsl-template-list-trailing-comma.compact.ron index 67ec4ffb5..bfcd83f7f 100644 --- a/naga/tests/out/ir/wgsl-template-list-trailing-comma.compact.ron +++ b/naga/tests/out/ir/wgsl-template-list-trailing-comma.compact.ron @@ -28,6 +28,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-template-list-trailing-comma.ron b/naga/tests/out/ir/wgsl-template-list-trailing-comma.ron index 67ec4ffb5..bfcd83f7f 100644 --- a/naga/tests/out/ir/wgsl-template-list-trailing-comma.ron +++ b/naga/tests/out/ir/wgsl-template-list-trailing-comma.ron @@ -28,6 +28,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [], diff --git a/naga/tests/out/ir/wgsl-texture-external.compact.ron b/naga/tests/out/ir/wgsl-texture-external.compact.ron index 4c0876618..2bceb18a5 100644 --- a/naga/tests/out/ir/wgsl-texture-external.compact.ron +++ b/naga/tests/out/ir/wgsl-texture-external.compact.ron @@ -1,5 +1,82 @@ ( types: [ + ( + name: None, + inner: Scalar(( + kind: Uint, + width: 4, + )), + ), + ( + name: None, + inner: Vector( + size: Bi, + scalar: ( + kind: Uint, + width: 4, + ), + ), + ), + ( + name: None, + inner: Matrix( + columns: Tri, + rows: Bi, + scalar: ( + kind: Float, + width: 4, + ), + ), + ), + ( + name: None, + inner: Matrix( + columns: Quad, + rows: Quad, + scalar: ( + kind: Float, + width: 4, + ), + ), + ), + ( + name: Some("NagaExternalTextureParams"), + inner: Struct( + members: [ + ( + name: Some("yuv_conversion_matrix"), + ty: 3, + binding: None, + offset: 0, + ), + ( + name: Some("sample_transform"), + ty: 2, + binding: None, + offset: 64, + ), + ( + name: Some("load_transform"), + ty: 2, + binding: None, + offset: 88, + ), + ( + name: Some("size"), + ty: 1, + binding: None, + offset: 112, + ), + ( + name: Some("num_planes"), + ty: 0, + binding: None, + offset: 120, + ), + ], + span: 128, + ), + ), ( name: None, inner: Image( @@ -24,21 +101,12 @@ ), ), ), - ( - name: None, - inner: Vector( - size: Bi, - scalar: ( - kind: Uint, - width: 4, - ), - ), - ), ], special_types: ( ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: Some(4), predeclared_types: {}, ), constants: [], @@ -51,7 +119,7 @@ group: 0, binding: 0, )), - ty: 0, + ty: 5, init: None, ), ( @@ -61,7 +129,7 @@ group: 0, binding: 1, )), - ty: 1, + ty: 6, init: None, ), ], @@ -72,28 +140,28 @@ arguments: [ ( name: Some("t"), - ty: 0, + ty: 5, binding: None, ), ], result: Some(( - ty: 2, + ty: 7, binding: None, )), local_variables: [ ( name: Some("a"), - ty: 2, + ty: 7, init: None, ), ( name: Some("b"), - ty: 2, + ty: 7, init: None, ), ( name: Some("c"), - ty: 3, + ty: 1, init: None, ), ], @@ -217,7 +285,7 @@ name: Some("fragment_main"), arguments: [], result: Some(( - ty: 2, + ty: 7, binding: Some(Location( location: 0, interpolation: Some(Perspective), @@ -256,7 +324,7 @@ name: Some("vertex_main"), arguments: [], result: Some(( - ty: 2, + ty: 7, binding: Some(BuiltIn(Position( invariant: false, ))), diff --git a/naga/tests/out/ir/wgsl-texture-external.ron b/naga/tests/out/ir/wgsl-texture-external.ron index 4c0876618..2bceb18a5 100644 --- a/naga/tests/out/ir/wgsl-texture-external.ron +++ b/naga/tests/out/ir/wgsl-texture-external.ron @@ -1,5 +1,82 @@ ( types: [ + ( + name: None, + inner: Scalar(( + kind: Uint, + width: 4, + )), + ), + ( + name: None, + inner: Vector( + size: Bi, + scalar: ( + kind: Uint, + width: 4, + ), + ), + ), + ( + name: None, + inner: Matrix( + columns: Tri, + rows: Bi, + scalar: ( + kind: Float, + width: 4, + ), + ), + ), + ( + name: None, + inner: Matrix( + columns: Quad, + rows: Quad, + scalar: ( + kind: Float, + width: 4, + ), + ), + ), + ( + name: Some("NagaExternalTextureParams"), + inner: Struct( + members: [ + ( + name: Some("yuv_conversion_matrix"), + ty: 3, + binding: None, + offset: 0, + ), + ( + name: Some("sample_transform"), + ty: 2, + binding: None, + offset: 64, + ), + ( + name: Some("load_transform"), + ty: 2, + binding: None, + offset: 88, + ), + ( + name: Some("size"), + ty: 1, + binding: None, + offset: 112, + ), + ( + name: Some("num_planes"), + ty: 0, + binding: None, + offset: 120, + ), + ], + span: 128, + ), + ), ( name: None, inner: Image( @@ -24,21 +101,12 @@ ), ), ), - ( - name: None, - inner: Vector( - size: Bi, - scalar: ( - kind: Uint, - width: 4, - ), - ), - ), ], special_types: ( ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: Some(4), predeclared_types: {}, ), constants: [], @@ -51,7 +119,7 @@ group: 0, binding: 0, )), - ty: 0, + ty: 5, init: None, ), ( @@ -61,7 +129,7 @@ group: 0, binding: 1, )), - ty: 1, + ty: 6, init: None, ), ], @@ -72,28 +140,28 @@ arguments: [ ( name: Some("t"), - ty: 0, + ty: 5, binding: None, ), ], result: Some(( - ty: 2, + ty: 7, binding: None, )), local_variables: [ ( name: Some("a"), - ty: 2, + ty: 7, init: None, ), ( name: Some("b"), - ty: 2, + ty: 7, init: None, ), ( name: Some("c"), - ty: 3, + ty: 1, init: None, ), ], @@ -217,7 +285,7 @@ name: Some("fragment_main"), arguments: [], result: Some(( - ty: 2, + ty: 7, binding: Some(Location( location: 0, interpolation: Some(Perspective), @@ -256,7 +324,7 @@ name: Some("vertex_main"), arguments: [], result: Some(( - ty: 2, + ty: 7, binding: Some(BuiltIn(Position( invariant: false, ))), diff --git a/naga/tests/out/ir/wgsl-types_with_comments.compact.ron b/naga/tests/out/ir/wgsl-types_with_comments.compact.ron index dde1bd367..1139fab19 100644 --- a/naga/tests/out/ir/wgsl-types_with_comments.compact.ron +++ b/naga/tests/out/ir/wgsl-types_with_comments.compact.ron @@ -37,6 +37,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [ diff --git a/naga/tests/out/ir/wgsl-types_with_comments.ron b/naga/tests/out/ir/wgsl-types_with_comments.ron index 3a23c49c4..a1761c17c 100644 --- a/naga/tests/out/ir/wgsl-types_with_comments.ron +++ b/naga/tests/out/ir/wgsl-types_with_comments.ron @@ -62,6 +62,7 @@ ray_desc: None, ray_intersection: None, ray_vertex_return: None, + external_texture_params: None, predeclared_types: {}, ), constants: [ diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index ed03fce77..3f212f80e 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -85,9 +85,18 @@ pub struct ExternalTextureParams { /// to RGBA. /// This is ignored when `num_planes` is 1. pub yuv_conversion_matrix: [f32; 16], - /// 3x2 column-major matrix with which to multiply texture coordinates - /// prior to sampling from the external texture. + /// 3x2 column-major matrix with which to multiply normalized texture + /// coordinates prior to sampling from the external texture. This may + /// scale, translate, flip, and rotate in 90-degree increments, but the + /// result of transforming the rectangle (0,0)..(1,1) must be an + /// axis-aligned rectangle that falls within the bounds of (0,0)..(1,1). pub sample_transform: [f32; 6], + /// 3x2 column-major matrix with which to multiply unnormalized texture + /// coordinates prior to loading from the external texture. This may scale, + /// translate, flip, and rotate in 90-degree increments, but the result of + /// transforming the rectangle (0,0)..(texture_size - 1) must be an + /// axis-aligned rectangle that falls within the bounds of + /// (0,0)..(texture_size - 1). pub load_transform: [f32; 6], /// Size of the external texture. This value should be returned by size /// queries in shader code. Note that this may not match the dimensions of