Skip to content

Commit 51a8f67

Browse files
committed
[naga] Reserve names for each plane and params buffer of external texture
Adds new `NameKey` variants `ExternalTextureGlobalVariable` and `ExternalTextureFunctionArgument`, like their non-external-texture cousins but additionally keyed by either being a specific plane index or params buffer. For each external texture global variable or function argument reserve additional names for 3 planes and the params buffer. For Naga backends which must represent external textures as multiple variables/arguments, this will allow them to uniquely name each one.
1 parent b25326f commit 51a8f67

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

naga/src/back/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,31 @@ impl FunctionCtx<'_> {
196196
}
197197
}
198198

199+
/// Helper method that generates a [`NameKey`](crate::proc::NameKey) for an external texture
200+
/// function argument.
201+
///
202+
/// # Panics
203+
/// - If the function arguments are less or equal to `arg`
204+
/// - If `self.ty` is not `FunctionType::Function`.
205+
pub const fn external_texture_argument_key(
206+
&self,
207+
arg: u32,
208+
external_texture_key: crate::proc::ExternalTextureNameKey,
209+
) -> crate::proc::NameKey {
210+
match self.ty {
211+
FunctionType::Function(handle) => {
212+
crate::proc::NameKey::ExternalTextureFunctionArgument(
213+
handle,
214+
arg,
215+
external_texture_key,
216+
)
217+
}
218+
FunctionType::EntryPoint(_) => {
219+
panic!("External textures cannot be used as arguments to entry points")
220+
}
221+
}
222+
}
223+
199224
/// Returns true if the given expression points to a fixed-function pipeline input.
200225
pub fn is_fixed_function_input(
201226
&self,

naga/src/proc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub use constant_evaluator::{
1818
pub use emitter::Emitter;
1919
pub use index::{BoundsCheckPolicies, BoundsCheckPolicy, IndexableLength, IndexableLengthError};
2020
pub use layouter::{Alignment, LayoutError, LayoutErrorInner, Layouter, TypeLayout};
21-
pub use namer::{EntryPointIndex, NameKey, Namer};
21+
pub use namer::{EntryPointIndex, ExternalTextureNameKey, NameKey, Namer};
2222
pub use overloads::{Conclusion, MissingSpecialType, OverloadSet, Rule};
2323
pub use terminator::ensure_block_returns;
2424
use thiserror::Error;

naga/src/proc/namer.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,40 @@ use crate::{arena::Handle, FastHashMap, FastHashSet};
1515
pub type EntryPointIndex = u16;
1616
const SEPARATOR: char = '_';
1717

18+
/// A component of a lowered external texture.
19+
///
20+
/// Whereas the WGSL backend implements [`ImageClass::External`]
21+
/// images directly, most other Naga backends lower them to a
22+
/// collection of ordinary textures that represent individual planes
23+
/// (as received from a video decoder, perhaps), together with a
24+
/// struct of parameters saying how they should be cropped, sampled,
25+
/// and color-converted.
26+
///
27+
/// This lowering means that individual globals and function
28+
/// parameters in Naga IR must be split out by the backends into
29+
/// collections of globals and parameters of simpler types.
30+
///
31+
/// A value of this enum serves as a name key for one specific
32+
/// component in the lowered representation of an external texture.
33+
/// That is, these keys are for variables/parameters that do not exist
34+
/// in the Naga IR, only in its lowered form.
35+
///
36+
/// [`ImageClass::External`]: crate::ir::ImageClass::External
37+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
38+
pub enum ExternalTextureNameKey {
39+
Plane(usize),
40+
Params,
41+
}
42+
43+
impl ExternalTextureNameKey {
44+
const ALL: &[(&str, ExternalTextureNameKey)] = &[
45+
("_plane0", ExternalTextureNameKey::Plane(0)),
46+
("_plane1", ExternalTextureNameKey::Plane(1)),
47+
("_plane2", ExternalTextureNameKey::Plane(2)),
48+
("_params", ExternalTextureNameKey::Params),
49+
];
50+
}
51+
1852
#[derive(Debug, Eq, Hash, PartialEq)]
1953
pub enum NameKey {
2054
Constant(Handle<crate::Constant>),
@@ -37,6 +71,17 @@ pub enum NameKey {
3771

3872
/// Entry point version of `FunctionOobLocal`.
3973
EntryPointOobLocal(EntryPointIndex, Handle<crate::Type>),
74+
75+
/// A global variable holding a component of a lowered external texture.
76+
///
77+
/// See [`ExternalTextureNameKey`] for details.
78+
ExternalTextureGlobalVariable(Handle<crate::GlobalVariable>, ExternalTextureNameKey),
79+
80+
/// A function argument holding a component of a lowered external
81+
/// texture.
82+
///
83+
/// See [`ExternalTextureNameKey`] for details.
84+
ExternalTextureFunctionArgument(Handle<crate::Function>, u32, ExternalTextureNameKey),
4085
}
4186

4287
/// This processor assigns names to all the things in a module
@@ -272,6 +317,27 @@ impl Namer {
272317
for (index, arg) in fun.arguments.iter().enumerate() {
273318
let name = self.call_or(&arg.name, "param");
274319
output.insert(NameKey::FunctionArgument(fun_handle, index as u32), name);
320+
321+
if matches!(
322+
module.types[arg.ty].inner,
323+
crate::TypeInner::Image {
324+
class: crate::ImageClass::External,
325+
..
326+
}
327+
) {
328+
let base = arg.name.as_deref().unwrap_or("param");
329+
for &(suffix, ext_key) in ExternalTextureNameKey::ALL {
330+
let name = self.call(&format!("{base}_{suffix}"));
331+
output.insert(
332+
NameKey::ExternalTextureFunctionArgument(
333+
fun_handle,
334+
index as u32,
335+
ext_key,
336+
),
337+
name,
338+
);
339+
}
340+
}
275341
}
276342
for (handle, var) in fun.local_variables.iter() {
277343
let name = self.call_or(&var.name, "local");
@@ -282,6 +348,23 @@ impl Namer {
282348
for (handle, var) in module.global_variables.iter() {
283349
let name = self.call_or(&var.name, "global");
284350
output.insert(NameKey::GlobalVariable(handle), name);
351+
352+
if matches!(
353+
module.types[var.ty].inner,
354+
crate::TypeInner::Image {
355+
class: crate::ImageClass::External,
356+
..
357+
}
358+
) {
359+
let base = var.name.as_deref().unwrap_or("global");
360+
for &(suffix, ext_key) in ExternalTextureNameKey::ALL {
361+
let name = self.call(&format!("{base}_{suffix}"));
362+
output.insert(
363+
NameKey::ExternalTextureGlobalVariable(handle, ext_key),
364+
name,
365+
);
366+
}
367+
}
285368
}
286369

287370
for (handle, constant) in module.constants.iter() {

0 commit comments

Comments
 (0)