[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.
This commit is contained in:
Jamie Nicol authored and Jim Blandy committed 2025-07-30 14:33:00 -07:00
1 parent 36ae215e9c
commit 9f654c6235
40 files changed
+386 -47

No files matched your search

+18 -2
View File
@@ -109,12 +109,28 @@ impl<W: Write> Writer<W> {
self.required_polyfills.clear();
}
fn is_builtin_wgsl_struct(&self, module: &Module, handle: Handle<crate::Type>) -> 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<crate::Type>) -> 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 {
+12
View File
@@ -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);
}
+94
View File
@@ -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<crate::Type> {
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
+23 -5
View File
@@ -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 }
+22
View File
@@ -2346,6 +2346,28 @@ pub struct SpecialTypes {
/// Call [`Module::generate_vertex_return_type`]
pub ray_vertex_return: Option<Handle<Type>>,
/// 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<f32>, // 16 64 0
/// sample_transform: mat3x2<f32>, // 8 24 64
/// load_transform: mat3x2<f32>, // 8 24 88
/// size: vec2<u32>, // 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<Handle<Type>>,
/// Types for predeclared wgsl types instantiated on demand.
///
/// Call [`Module::generate_predeclared_type`] to populate this if
@@ -67,6 +67,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [
+1
View File
@@ -130,6 +130,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [
+1
View File
@@ -155,6 +155,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [
+1
View File
@@ -278,6 +278,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [
@@ -171,6 +171,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [
+1
View File
@@ -262,6 +262,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [
@@ -421,6 +421,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
+1
View File
@@ -421,6 +421,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
@@ -44,6 +44,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
+1
View File
@@ -44,6 +44,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
@@ -4,6 +4,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
+1
View File
@@ -4,6 +4,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
@@ -4,6 +4,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
@@ -4,6 +4,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
@@ -81,6 +81,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
@@ -81,6 +81,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
@@ -26,6 +26,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [
+1
View File
@@ -26,6 +26,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [
@@ -12,6 +12,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
+1
View File
@@ -12,6 +12,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
@@ -53,6 +53,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {
AtomicCompareExchangeWeakResult((
kind: Uint,
@@ -53,6 +53,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {
AtomicCompareExchangeWeakResult((
kind: Uint,
@@ -85,6 +85,7 @@
ray_desc: Some(5),
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
@@ -85,6 +85,7 @@
ray_desc: Some(5),
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
@@ -26,6 +26,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
+1
View File
@@ -26,6 +26,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
@@ -71,6 +71,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
@@ -71,6 +71,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
@@ -28,6 +28,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
@@ -28,6 +28,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [],
@@ -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,
))),
+87 -19
View File
@@ -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,
))),
@@ -37,6 +37,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [
@@ -62,6 +62,7 @@
ray_desc: None,
ray_intersection: None,
ray_vertex_return: None,
external_texture_params: None,
predeclared_types: {},
),
constants: [
+11 -2
View File
@@ -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