Skip to content

Commit 101ea38

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 0b8f716 commit 101ea38

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ use crate::{arena::Handle, FastHashMap, FastHashSet};
1515
pub type EntryPointIndex = u16;
1616
const SEPARATOR: char = '_';
1717

18+
/// Some backends may represent an external texture as multiple global variables
19+
/// or function arguments. This serves as a key for each of those parts.
20+
#[derive(Debug, Eq, Hash, PartialEq)]
21+
pub enum ExternalTextureNameKey {
22+
Plane(usize),
23+
Params,
24+
}
25+
1826
#[derive(Debug, Eq, Hash, PartialEq)]
1927
pub enum NameKey {
2028
Constant(Handle<crate::Constant>),
@@ -37,6 +45,9 @@ pub enum NameKey {
3745

3846
/// Entry point version of `FunctionOobLocal`.
3947
EntryPointOobLocal(EntryPointIndex, Handle<crate::Type>),
48+
49+
ExternalTextureGlobalVariable(Handle<crate::GlobalVariable>, ExternalTextureNameKey),
50+
ExternalTextureFunctionArgument(Handle<crate::Function>, u32, ExternalTextureNameKey),
4051
}
4152

4253
/// This processor assigns names to all the things in a module
@@ -272,6 +283,36 @@ impl Namer {
272283
for (index, arg) in fun.arguments.iter().enumerate() {
273284
let name = self.call_or(&arg.name, "param");
274285
output.insert(NameKey::FunctionArgument(fun_handle, index as u32), name);
286+
287+
if matches!(
288+
module.types[arg.ty].inner,
289+
crate::TypeInner::Image {
290+
class: crate::ImageClass::External,
291+
..
292+
}
293+
) {
294+
let base = arg.name.as_ref().map_or("param", |s| s.as_str());
295+
for i in 0..3 {
296+
let name = self.call(&format!("{base}_plane{i}"));
297+
output.insert(
298+
NameKey::ExternalTextureFunctionArgument(
299+
fun_handle,
300+
index as u32,
301+
ExternalTextureNameKey::Plane(i),
302+
),
303+
name,
304+
);
305+
}
306+
let name = self.call(&format!("{base}_params"));
307+
output.insert(
308+
NameKey::ExternalTextureFunctionArgument(
309+
fun_handle,
310+
index as u32,
311+
ExternalTextureNameKey::Params,
312+
),
313+
name,
314+
);
315+
}
275316
}
276317
for (handle, var) in fun.local_variables.iter() {
277318
let name = self.call_or(&var.name, "local");
@@ -282,6 +323,31 @@ impl Namer {
282323
for (handle, var) in module.global_variables.iter() {
283324
let name = self.call_or(&var.name, "global");
284325
output.insert(NameKey::GlobalVariable(handle), name);
326+
327+
if matches!(
328+
module.types[var.ty].inner,
329+
crate::TypeInner::Image {
330+
class: crate::ImageClass::External,
331+
..
332+
}
333+
) {
334+
let base = var.name.as_ref().map_or("global", |s| s.as_str());
335+
for i in 0..3 {
336+
let name = self.call(&format!("{base}_plane{i}"));
337+
output.insert(
338+
NameKey::ExternalTextureGlobalVariable(
339+
handle,
340+
ExternalTextureNameKey::Plane(i),
341+
),
342+
name,
343+
);
344+
}
345+
let name = self.call(&format!("{base}_params"));
346+
output.insert(
347+
NameKey::ExternalTextureGlobalVariable(handle, ExternalTextureNameKey::Params),
348+
name,
349+
);
350+
}
285351
}
286352

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

0 commit comments

Comments
 (0)