Skip to content

[naga hlsl-out] HLSL implemention of external textures #7826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions naga-cli/src/bin/naga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,9 @@ fn run() -> anyhow::Result<()> {
use naga::valid::Capabilities as C;
let missing = match Path::new(path).extension().and_then(|ex| ex.to_str()) {
Some("wgsl") => C::CLIP_DISTANCE | C::CULL_DISTANCE,
Some("metal") => C::CULL_DISTANCE,
_ => C::empty(),
Some("metal") => C::CULL_DISTANCE | C::TEXTURE_EXTERNAL,
Some("hlsl") => C::empty(),
_ => C::TEXTURE_EXTERNAL,
};
caps & !missing
});
Expand Down
3 changes: 2 additions & 1 deletion naga/src/back/glsl/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ impl<W> Writer<'_, W> {
_ => {}
},
ImageClass::Sampled { multi: false, .. }
| ImageClass::Depth { multi: false } => {}
| ImageClass::Depth { multi: false }
| ImageClass::External => {}
}
}
_ => {}
Expand Down
5 changes: 5 additions & 0 deletions naga/src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,7 @@ impl<'a, W: Write> Writer<'a, W> {
Ic::Depth { multi: true } => ("sampler", float, "MS", ""),
Ic::Depth { multi: false } => ("sampler", float, "", "Shadow"),
Ic::Storage { format, .. } => ("image", format.into(), "", ""),
Ic::External => unimplemented!(),
};

let precision = if self.options.version.is_es() {
Expand Down Expand Up @@ -3302,6 +3303,7 @@ impl<'a, W: Write> Writer<'a, W> {
write!(self.out, "imageSize(")?;
self.write_expr(image, ctx)?;
}
ImageClass::External => unimplemented!(),
}
write!(self.out, ")")?;
if components != 1 || self.options.version.is_es() {
Expand All @@ -3317,6 +3319,7 @@ impl<'a, W: Write> Writer<'a, W> {
let fun_name = match class {
ImageClass::Sampled { .. } | ImageClass::Depth { .. } => "textureSize",
ImageClass::Storage { .. } => "imageSize",
ImageClass::External => unimplemented!(),
};
write!(self.out, "{fun_name}(")?;
self.write_expr(image, ctx)?;
Expand All @@ -3336,6 +3339,7 @@ impl<'a, W: Write> Writer<'a, W> {
"textureSamples"
}
ImageClass::Storage { .. } => "imageSamples",
ImageClass::External => unimplemented!(),
};
write!(self.out, "{fun_name}(")?;
self.write_expr(image, ctx)?;
Expand Down Expand Up @@ -4618,6 +4622,7 @@ impl<'a, W: Write> Writer<'a, W> {
"WGSL `textureLoad` from depth textures is not supported in GLSL".to_string(),
))
}
crate::ImageClass::External => unimplemented!(),
};

// openGL es doesn't have 1D images so we need workaround it
Expand Down
466 changes: 378 additions & 88 deletions naga/src/back/hlsl/help.rs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions naga/src/back/hlsl/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ pub const RESERVED: &[&str] = &[
super::writer::INSERT_BITS_FUNCTION,
super::writer::SAMPLER_HEAP_VAR,
super::writer::COMPARISON_SAMPLER_HEAP_VAR,
super::writer::SAMPLE_EXTERNAL_TEXTURE_FUNCTION,
super::writer::ABS_FUNCTION,
super::writer::DIV_FUNCTION,
super::writer::MOD_FUNCTION,
Expand All @@ -834,6 +835,7 @@ pub const RESERVED: &[&str] = &[
super::writer::F2U32_FUNCTION,
super::writer::F2I64_FUNCTION,
super::writer::F2U64_FUNCTION,
super::writer::IMAGE_LOAD_EXTERNAL_FUNCTION,
super::writer::IMAGE_SAMPLE_BASE_CLAMP_TO_EDGE_FUNCTION,
];

Expand Down
65 changes: 65 additions & 0 deletions naga/src/back/hlsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,41 @@ where

pub type DynamicStorageBufferOffsetsTargets = alloc::collections::BTreeMap<u32, OffsetsBindTarget>;

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
pub struct ExternalTextureBindTarget {
pub planes: [BindTarget; 3],
pub params: BindTarget,
}

#[cfg(any(feature = "serialize", feature = "deserialize"))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
struct ExternalTextureBindingMapSerialization {
resource_binding: crate::ResourceBinding,
bind_target: ExternalTextureBindTarget,
}

#[cfg(feature = "deserialize")]
fn deserialize_external_texture_binding_map<'de, D>(
deserializer: D,
) -> Result<ExternalTextureBindingMap, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::Deserialize;

let vec = Vec::<ExternalTextureBindingMapSerialization>::deserialize(deserializer)?;
let mut map = ExternalTextureBindingMap::default();
for item in vec {
map.insert(item.resource_binding, item.bind_target);
}
Ok(map)
}
pub type ExternalTextureBindingMap =
alloc::collections::BTreeMap<crate::ResourceBinding, ExternalTextureBindTarget>;

/// Shorthand result used internally by the backend
type BackendResult = Result<(), Error>;

Expand Down Expand Up @@ -376,6 +411,11 @@ pub struct Options {
serde(deserialize_with = "deserialize_storage_buffer_offsets")
)]
pub dynamic_storage_buffer_offsets_targets: DynamicStorageBufferOffsetsTargets,
#[cfg_attr(
feature = "deserialize",
serde(deserialize_with = "deserialize_external_texture_binding_map")
)]
pub external_texture_binding_map: ExternalTextureBindingMap,
/// Should workgroup variables be zero initialized (by polyfilling)?
pub zero_initialize_workgroup_memory: bool,
/// Should we restrict indexing of vectors, matrices and arrays?
Expand All @@ -396,6 +436,7 @@ impl Default for Options {
sampler_buffer_binding_map: alloc::collections::BTreeMap::default(),
push_constants_target: None,
dynamic_storage_buffer_offsets_targets: alloc::collections::BTreeMap::new(),
external_texture_binding_map: ExternalTextureBindingMap::default(),
zero_initialize_workgroup_memory: true,
restrict_indexing: true,
force_loop_bounding: true,
Expand All @@ -420,6 +461,29 @@ impl Options {
None => Err(EntryPointError::MissingBinding(*res_binding)),
}
}

fn resolve_external_texture_resource_binding(
&self,
res_binding: &crate::ResourceBinding,
) -> Result<ExternalTextureBindTarget, EntryPointError> {
match self.external_texture_binding_map.get(res_binding) {
Some(target) => Ok(*target),
None if self.fake_missing_bindings => {
let fake = BindTarget {
space: res_binding.group as u8,
register: res_binding.binding,
binding_array_size: None,
dynamic_storage_buffer_offsets_index: None,
restrict_indexing: false,
};
Ok(ExternalTextureBindTarget {
planes: [fake, fake, fake],
params: fake,
})
}
None => Err(EntryPointError::MissingBinding(*res_binding)),
}
}
}

/// Reflection info for entry point names.
Expand Down Expand Up @@ -474,6 +538,7 @@ enum WrappedType {
ArrayLength(help::WrappedArrayLength),
ImageSample(help::WrappedImageSample),
ImageQuery(help::WrappedImageQuery),
ImageLoad(help::WrappedImageLoad),
ImageLoadScalar(crate::Scalar),
Constructor(help::WrappedConstructor),
StructMatrixAccess(help::WrappedStructMatrixAccess),
Expand Down
Loading