Let only sampling decide a texture's binding class
AIR declares each texture argument's access, and reflection reports a write-capable one as a storage image at `STORAGE_TEXTURE_BINDING_BASE + n`, which is where a consumer writes its descriptor. The emitter chose the binding class from what the BODY does instead, and a size query counted as a sampled-image use. So a `texture2d<float, write>` whose body only calls `get_width()` bound at `TEXTURE_BINDING_BASE + n`: the consumer wrote its descriptor somewhere the shader does not read. Only sampling can decide this -- `OpSampledImage` is what requires `Sampled` 1. Everything else AIR does to a texture has a form for either class: `read_texture` is `OpImageRead` on a storage image and `OpImageFetch` on a sampled one, size and sample-count queries have an opcode each, `get_num_mip_levels` folds to one level on a storage image, and `is_null_texture` folds to a constant. The rule took two pieces before, and the gap was between them: `texture_dims` skips the read and query uses of a texture the body WRITES, and `storage_texel_read_operands` restored the ones a texel read had otherwise disqualified. A texture AIR declares write-capable but the body never writes fell through both. `sampled_binding_required_operands` replaces the pair with the one question that matters, and `storage_texel_read_operands` is gone. `air.gather_*` is not in the set, preserving what this classification has always done: a gathered AND written texture would need one descriptor to be both `Sampled` 1 and `Sampled` 2, the read-write-with-format case `write_texture_dims` documents as out of scope, and adding it converts those shaders into a write-lowering failure instead of resolving anything. I tried it: one corpus module regressed that way. Whether a gathered storage image reaches a correct lowering is unaudited and stated as such. Verified over 2880 corpus sources: bytes identical, reflection identical, failure count unchanged at 204. The gap needs a texture AIR declares writable that the body never writes, which no corpus shader does. New `native_write_declared_texture_binds_as_storage_even_if_the_body_only_queries_it` checks the module against the binding reflection reports, and is verified as a detector. `native_kernel_helper_wrapped_write_texture_query_resolves_loaded_image` was pinning the old behavior with an incidental `OpImageQuerySizeLod` assertion; its subject is that the query resolves to the wrapped texture, so it now expects the LOD-less form a storage image takes. Gate: fmt, clippy -D warnings, warnings-denied all-features rustdoc, cargo test -p metal2vulkan (debug and release), cargo test -p metal2vulkan-validation.
This commit is contained in:
1 parent
a0995c4b3e
commit
b8fcc4cd44
4 files changed
+99
-21
No files matched your search
@@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Fixed
|
||||
|
||||
- A texture AIR declares write-capable binds as a storage image even when the shader body only
|
||||
queries its size. The binding class came from what the body did, and a size query counted as a
|
||||
sampled-image use, so such a texture bound in the sampled-texture band while reflection reported
|
||||
it in the storage-texture band -- a consumer wrote its descriptor where the shader does not read
|
||||
it. Only sampling decides the class now; every other AIR texture operation has a form for either.
|
||||
- Scratch files under a caller-supplied `tmp` are named per process and per call, so two callers
|
||||
sharing one directory no longer overwrite and delete each other's input to `spirv-val` or
|
||||
`llvm-dis`. The shared name surfaced as a validation failure in a module that was fine.
|
||||
|
||||
@@ -206,6 +206,65 @@ fn runtime_storage_image_atomic_requires_scalar_format_and_host_support() {
|
||||
tools::spirv_val_bytes(&spv, &tmp).expect("atomic specialization spirv-val");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_write_declared_texture_binds_as_storage_even_if_the_body_only_queries_it() {
|
||||
// AIR declares `dst` write-capable, so a consumer allocates it a storage-image descriptor at
|
||||
// `STORAGE_TEXTURE_BINDING_BASE + n` -- reflection says so, from the type name. The binding
|
||||
// class the emitter chose came instead from what the BODY does, and a size query used to count
|
||||
// as a sampled-image use, so this shader bound `dst` at `TEXTURE_BINDING_BASE + n`: the
|
||||
// consumer wrote its descriptor where the shader does not read it.
|
||||
//
|
||||
// Only sampling can decide this. A size query has an opcode for each binding class, so it
|
||||
// decides nothing.
|
||||
let ll = r#"
|
||||
target triple = "spirv-unknown-vulkan1.2"
|
||||
define void @k(ptr addrspace(1) %dst, ptr addrspace(1) %out) {
|
||||
entry:
|
||||
%w = call i32 @air.get_width_texture_2d(ptr addrspace(1) %dst, i32 0)
|
||||
store i32 %w, ptr addrspace(1) %out, align 4
|
||||
ret void
|
||||
}
|
||||
declare i32 @air.get_width_texture_2d(ptr addrspace(1), i32)
|
||||
!air.kernel = !{!0}
|
||||
!0 = !{ptr @k, !1, !2}
|
||||
!1 = !{}
|
||||
!2 = !{!3, !4}
|
||||
!3 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.write", !"air.arg_type_name", !"texture2d<float, write>", !"air.arg_name", !"dst"}
|
||||
!4 = !{i32 1, !"air.buffer", !"air.buffer_size", i32 4, !"air.location_index", i32 0, i32 1, !"air.write", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"uint", !"air.arg_name", !"out"}
|
||||
"#;
|
||||
let tmp = std::env::temp_dir().join(format!(
|
||||
"metal2vulkan_write_declared_query_only_{}",
|
||||
std::process::id()
|
||||
));
|
||||
let _ = std::fs::create_dir_all(&tmp);
|
||||
let (spv, reflection) = crate::translate_sanitized_native_reflected(
|
||||
ll,
|
||||
Stage::Kernel,
|
||||
&tmp,
|
||||
passes::TransformOptions::default(),
|
||||
)
|
||||
.expect("translate");
|
||||
let asm = disassemble(&spv).expect("disassemble");
|
||||
assert!(
|
||||
asm.contains("2D 0 0 0 2"),
|
||||
"a write-declared texture binds as a storage image:\n{asm}"
|
||||
);
|
||||
let reflected = reflection
|
||||
.binding_at(crate::reflect::ResourceKind::StorageImage, 0)
|
||||
.and_then(|resource| resource.descriptor)
|
||||
.expect("reflection reports it as a storage image");
|
||||
assert_eq!(
|
||||
reflected.binding,
|
||||
crate::reflect::STORAGE_TEXTURE_BINDING_BASE
|
||||
);
|
||||
assert!(
|
||||
asm.contains(&format!("Binding {}", reflected.binding)),
|
||||
"the module has to decorate the binding reflection reports:\n{asm}"
|
||||
);
|
||||
tools::spirv_val_bytes(&spv, &tmp).expect("spirv-val");
|
||||
let _ = std::fs::remove_dir_all(&tmp);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_buffer_texture_size_query_uses_query_size_without_lod() {
|
||||
// SPIR-V allows `OpImageQuerySizeLod` only on a 1D/2D/3D/Cube image with `MS` 0 and a `Sampled`
|
||||
@@ -4220,7 +4279,9 @@ declare i32 @air.get_width_texture_2d(ptr addrspace(1), i32)
|
||||
let _ = std::fs::create_dir_all(&tmp);
|
||||
let spv = crate::translate_sanitized_native(ll, Stage::Kernel, &tmp).expect("translate");
|
||||
let asm = disassemble(&spv).expect("disassemble");
|
||||
assert!(asm.contains("OpImageQuerySizeLod"), "{asm}");
|
||||
// The subject is that the query resolves to the wrapped texture at all. AIR declares `dst`
|
||||
// write-capable, so it binds as a storage image and the query takes the LOD-less form.
|
||||
assert!(asm.contains("OpImageQuerySize "), "{asm}");
|
||||
assert!(!asm.contains("OpFunctionCall"), "{asm}");
|
||||
if std::process::Command::new("spirv-val")
|
||||
.arg("--version")
|
||||
|
||||
@@ -626,13 +626,34 @@ fn write_texture_operands(
|
||||
out
|
||||
}
|
||||
|
||||
/// Metadata-declared storage textures used by direct texel reads, with any parameter also consumed
|
||||
/// by a sampled-image operation or query removed. This distinguishes `read_texture` (legal as
|
||||
/// `OpImageRead`) from the other calls that make `texture_dims` require a Sampled=1 binding.
|
||||
pub(in crate::passes) fn storage_texel_read_operands(ctx: &Ctx, entry_idx: usize) -> HashSet<Word> {
|
||||
/// Textures this module cannot bind as a storage image, because something samples through them.
|
||||
///
|
||||
/// `OpSampledImage` requires an image whose `Sampled` operand is 1. Everything else AIR does to a
|
||||
/// texture has a form for either binding: `read_texture` is `OpImageRead` on a storage image and
|
||||
/// `OpImageFetch` on a sampled one, size and sample-count queries have an opcode each (see
|
||||
/// `image_size_query_op`), `get_num_mip_levels` folds to one level on a storage image, and
|
||||
/// `is_null_texture` folds to a constant.
|
||||
///
|
||||
/// So a query does not decide the binding class, and this is the only rule that does. It used to
|
||||
/// take two: `texture_dims` collects every use including queries but skips the read and query uses
|
||||
/// of a texture the body WRITES, and `storage_texel_read_operands` restored the ones a texel read
|
||||
/// had otherwise disqualified. A texture that AIR declares write-capable but the body never writes
|
||||
/// fell through both -- a `texture2d<float, write>` that is only size-queried bound as a sampled
|
||||
/// image at `TEXTURE_BINDING_BASE + n` while reflection reported a storage image at
|
||||
/// `STORAGE_TEXTURE_BINDING_BASE + n`, so the consumer wrote its descriptor where the shader does
|
||||
/// not read it.
|
||||
///
|
||||
/// `air.gather_*` is not here, which preserves what this classification has always done. A gathered
|
||||
/// AND written texture would need one descriptor to be both `Sampled` 1 and `Sampled` 2 -- the
|
||||
/// read-write-with-format case `write_texture_dims` documents as out of scope -- so putting it here
|
||||
/// converts those shaders into a write-lowering failure instead of resolving anything. Whether a
|
||||
/// gathered storage image reaches a correct lowering is unaudited.
|
||||
pub(in crate::passes) fn sampled_binding_required_operands(
|
||||
ctx: &Ctx,
|
||||
entry_idx: usize,
|
||||
) -> HashSet<Word> {
|
||||
let names = air_names(&ctx.module);
|
||||
let mut reads = HashSet::new();
|
||||
let mut sampled_or_queried = HashSet::new();
|
||||
let mut required = HashSet::new();
|
||||
for instruction in ctx.module.functions[entry_idx]
|
||||
.blocks
|
||||
.iter()
|
||||
@@ -649,20 +670,11 @@ pub(in crate::passes) fn storage_texel_read_operands(ctx: &Ctx, entry_idx: usize
|
||||
let Some(Operand::IdRef(texture)) = instruction.operands.get(1) else {
|
||||
continue;
|
||||
};
|
||||
if name.starts_with("air.read_texture") || name.starts_with("air.read_depth") {
|
||||
reads.insert(*texture);
|
||||
} else if name.starts_with("air.sample_texture")
|
||||
|| name.starts_with("air.sample_depth")
|
||||
|| name.starts_with("air.is_null_texture")
|
||||
|| is_size_query(name)
|
||||
|| name.starts_with("air.get_num_mip_levels_")
|
||||
|| name.starts_with("air.get_num_samples_texture")
|
||||
{
|
||||
sampled_or_queried.insert(*texture);
|
||||
if name.starts_with("air.sample_texture") || name.starts_with("air.sample_depth") {
|
||||
required.insert(*texture);
|
||||
}
|
||||
}
|
||||
reads.retain(|texture| !sampled_or_queried.contains(texture));
|
||||
reads
|
||||
required
|
||||
}
|
||||
|
||||
fn is_size_query(name: &str) -> bool {
|
||||
|
||||
@@ -308,10 +308,10 @@ pub(super) fn build_stage_input(
|
||||
wtex_candidates.entry(pid).or_insert(shape);
|
||||
}
|
||||
}
|
||||
let storage_texel_reads = storage_texel_read_operands(ctx, entry_idx);
|
||||
let sampled_required = sampled_binding_required_operands(ctx, entry_idx);
|
||||
let wtex_dims: HashMap<Word, (Dim, bool, ImageFormat, ImageComp)> = wtex_candidates
|
||||
.into_iter()
|
||||
.filter(|(pid, _)| !tex_dims.contains_key(pid) || storage_texel_reads.contains(pid))
|
||||
.filter(|(pid, _)| !sampled_required.contains(pid))
|
||||
.collect();
|
||||
|
||||
// Plan a binding for each parameter, allocating descriptor bindings and locations in role order.
|
||||
|
||||
Reference in new issue
Block a user