From b33ca9cbf8941698d90a110dd54349b90b03e581 Mon Sep 17 00:00:00 2001 From: JB2WIN Date: Sun, 29 Nov 2026 19:23:45 +0100 Subject: [PATCH 1/4] native: read 2D table texture width and base level from entry words Compute programs read an image descriptor's width (word 1 bits 30-31 and word 2 & 0x3fff), base level (word 3 bits 12-15) and a word-1 null check with scalar ALU ops. For a 2D table texture SRV the program samples, the direct translator now replaces those four reads with entry words (udsrc imgwidth / imgbase) that the stand-in fills from the bound view, and the null check with a constant. Other descriptor word reads keep their refusal. Refs #3224, #3023 AI-assisted. Co-Authored-By: Claude Opus 5.5 --- docs/startup-runtime.md | 18 ++ src/direct_compute_cache.cpp | 18 +- src/gfx1013_compute_native.h | 3 + src/gfx1013_direct_compute.inl | 119 +++++++ src/native_compute_pipeline.cpp | 2 + src/startup_stubs/d3d12_compute_dispatch.inl | 48 ++- ...tartup_compute_dispatch_image_fields.cmake | 17 + tests/native_compute_manifest_reasons.cpp | 47 ++- ...tup_compute_dispatch_image_fields_check.py | 305 ++++++++++++++++++ tests/startup_stream_consumer.cpp | 41 ++- 10 files changed, 609 insertions(+), 9 deletions(-) create mode 100644 tests/cmake/startup_compute_dispatch_image_fields.cmake create mode 100644 tests/startup_compute_dispatch_image_fields_check.py diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 040ae3e4b2..172df0a473 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -1100,6 +1100,24 @@ rebuilds stale native outputs and leaves Python publications in place. The stand absent manifest on subsequent dispatches on the ManifestRetry schedule (1 s, doubling up to 32 s), so a running process can pick up a rebuilt output (by the code path; no test covers the reload). Compute request keys stay at `29cs`, and revision-28 archives are still read. +A compute program may read the width, base mip and emptiness of a 2D texture descriptor from a table +slot that it also samples with an image op: `s_bfe_u32` word 1 `0x2001e` (width low bits), `s_and_b32` word 2 +`0x3fff` (width high bits), `s_bfe_u32` word 3 `0x4000c` (base level) and `s_cmp_eq/lg_i32/u32` word 1 against +inline 0. The bit positions follow the public AMD PAL `sq_img_rsrc_t` (gfx10 member for words 1 and 2, common +member for the base level). The translator replaces these reads with entry words (`udsrc imgwidth ` holds +width - 1, `udsrc imgbase ` the base mip, after the stride words) and the null check with a constant, +since the dispatch runs only with a resolved input texture. At Dispatch the stand-in fills the words from the +bound view: the resource's mip-0 width, and the described view's MostDetailedMip (0 for a view without +description). Only a 2D `texture_srv` is accepted (`image field words from a view that is not one 2D texture SRV` +otherwise). 96-bit and block-compressed formats are refused (`an image field source has a format whose +descriptor extent is not its width`). The runtime cannot see a padded view that PAL would program and assumes the +unpadded mip-0 extent. A read is replaced only when its word sits at the expected offset of a 32-byte aligned +descriptor record. Any other read of an image descriptor word keeps its refusal, and `NativeComputePipeline` +does not bind these words. Check with `ctest --preset dev -R startup_compute_dispatch_image_fields` +([`tests/startup_compute_dispatch_image_fields_check.py`](../tests/startup_compute_dispatch_image_fields_check.py)). +On WARP the translated module cannot run (wave32), so that test substitutes an HLSL program for the published +`.cs.cso` and covers the manifest, the reader and the fill there; the translation itself runs on hardware. + Check with `ctest --preset dev -R '^startup_compute_dispatch_native_(warp|hardware)$' -V` ([`tests/startup_compute_dispatch_native_check.py`](../tests/startup_compute_dispatch_native_check.py); [route, controls and results](experiments/startup-compute-native-dispatch.md)). diff --git a/src/direct_compute_cache.cpp b/src/direct_compute_cache.cpp index 83af0a61b2..e96cc9ba1a 100644 --- a/src/direct_compute_cache.cpp +++ b/src/direct_compute_cache.cpp @@ -104,6 +104,11 @@ Bytes encode(const NativeComputeShader &program) { word(out, static_cast(program.stride_srvs.size())); for (unsigned srv : program.stride_srvs) word(out, srv); + word(out, static_cast(program.image_fields.size())); + for (const auto &[srv, field] : program.image_fields) { + word(out, srv); + word(out, field); + } word(out, static_cast(program.samplers.size())); for (const auto &s : program.samplers) { for (auto n : s.words) @@ -157,6 +162,11 @@ NativeComputeShader decode(std::string_view bytes) { n = in.count(32); for (unsigned j = 0; j < n; ++j) shader.stride_srvs.push_back(in.count(31)); + n = in.count(32); + for (unsigned j = 0; j < n; ++j) { + const unsigned srv = in.count(31); + shader.image_fields.emplace_back(srv, in.count(1)); + } n = in.count(2048); for (unsigned j = 0; j < n; ++j) { ComputeSampler sampler{}; @@ -936,7 +946,7 @@ std::optional format_compute_runtime_manifest(const NativeComputeSh return refuse("more than 8 root constants in one root parameter"); ud_count += block.count; } - ud_count += static_cast(program.stride_srvs.size()); + ud_count += static_cast(program.stride_srvs.size() + program.image_fields.size()); if (ud_count > 32) return refuse("more than 32 user data words"); @@ -1068,6 +1078,12 @@ std::optional format_compute_runtime_manifest(const NativeComputeSh return refuse("stride words from a view that is not one raw vector SRV"); out << "udsrc " << ud_index++ << " stride " << srv << '\n'; } + for (const auto &[srv, field] : program.image_fields) { + if (srv >= srv_count || sorted[srv].kind != ComputeViewKind::texture_srv || sorted[srv].array_count != 1 || + sorted[srv].image_dimension != 1 || field > 1) + return refuse("image field words from a view that is not one 2D texture SRV"); + out << "udsrc " << ud_index++ << (field ? " imgbase " : " imgwidth ") << srv << '\n'; + } out << srv_lines.str() << uav_lines.str() << sampler_lines.str() << "native " << native_compute_output_revision << '\n'; return out.str(); diff --git a/src/gfx1013_compute_native.h b/src/gfx1013_compute_native.h index 136b2fc754..09ac35ef1f 100644 --- a/src/gfx1013_compute_native.h +++ b/src/gfx1013_compute_native.h @@ -138,6 +138,9 @@ struct NativeComputeShader { std::vector constants; // Additional b0 words, after root constants, read from these bound SRVs. std::vector stride_srvs; + // After the stride words: {SRV register, field} of the sampled 2D texture whose descriptor dimension field is + // read. Field 0 is width - 1, field 1 the most detailed mip of the view. + std::vector> image_fields; std::string hlsl, dxil_ir; std::vector bindings; std::vector samplers; diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index 32e2b0a8c3..3516cfaffd 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -88,6 +88,10 @@ struct DirectCompute { unsigned mode_bits = 0xf0; std::vector scc_live; std::map,string> descriptor_strides; + // Dimension fields of a 2D texture descriptor in a table slot, read by scalar ops (see image_field_read). The + // runtime fills one entry word per field from the bound view: width - 1 and the most detailed mip. + struct ImageFieldWords { bool width = false, base = false; string width_word, base_word; }; + std::map,ImageFieldWords> image_fields; unsigned label_number = 0; bool pixel_stage = false; bool pixel_heap_enabled = false; @@ -343,6 +347,8 @@ struct DirectCompute { } void initialize_constants(const GraphicsConstantLayout *linked = nullptr) { unsigned words = static_cast(descriptor_strides.size()); + for (const auto &entry : image_fields) + words += unsigned(entry.second.width) + unsigned(entry.second.base); for (const auto &block : analysis.result.constants) words += block.count; if (words) { @@ -366,6 +372,10 @@ struct DirectCompute { } for (auto &entry : descriptor_strides) entry.second = ir.binary("and",word_at(pos++),"16383"); + for (auto &entry : image_fields) { + if (entry.second.width) entry.second.width_word = word_at(pos++); + if (entry.second.base) entry.second.base_word = word_at(pos++); + } } } // Refuses a GPU-selected heap read that the contract does not cover. raw_buffer, typed_dword and raw_and_typed_dword @@ -3172,6 +3182,92 @@ struct DirectCompute { } return scc_live[program.at.at(definition.next)]; } + // The scalar reads of a texture descriptor's dimension fields that the runtime answers from the bound view: + // s_bfe_u32 word1 0x2001e (width_lo), s_and_b32 word2 0x3fff (width_hi), s_bfe_u32 word3 0x4000c (base level) and + // s_cmp_{eq,lg}_{i32,u32} word1, 0 (null check). Layout: AMD PAL gfx10_sq_ko_reg.h sq_img_rsrc_t (public, MIT). + // slot is (root parameter, byte offset of descriptor word 0); the caller decides whether it names an image slot. + enum class ImageFieldOp { width_lo, width_hi, base, null_eq, null_lg }; + struct ImageFieldRead { std::pair slot; ImageFieldOp op; }; + static std::optional image_field_read(const Inst &i, const State &s) { + using T = Symbol::Type; + if ((i.enc != Encoding::Sop2 && i.enc != Encoding::Sopc) || (i.enc == Encoding::Sop2 && i.dst > 105)) + return {}; + const auto a = source(i, i.src[0], s), b = source(i, i.src[1], s); + const auto word = [](const Symbol &v, unsigned w) { + return v.type == T::descriptor && v.value != compute_resource_heap && v.offset % 32 == 4 * w; + }; + const auto at = [](const Symbol &v, unsigned w) { + return std::make_pair(unsigned(v.value), unsigned(v.offset - 4 * w)); + }; + if (i.name == "s_bfe_u32" && b.type == T::constant) { + if (word(a, 1) && b.value == 0x2001e) return ImageFieldRead{at(a, 1), ImageFieldOp::width_lo}; + if (word(a, 3) && b.value == 0x4000c) return ImageFieldRead{at(a, 3), ImageFieldOp::base}; + } + if (i.name == "s_and_b32") { + if (word(a, 2) && b.type == T::constant && b.value == 0x3fff) + return ImageFieldRead{at(a, 2), ImageFieldOp::width_hi}; + if (word(b, 2) && a.type == T::constant && a.value == 0x3fff) + return ImageFieldRead{at(b, 2), ImageFieldOp::width_hi}; + } + if (i.enc == Encoding::Sopc && i.src[1] == 128 && word(a, 1)) { + if (i.name == "s_cmp_eq_i32" || i.name == "s_cmp_eq_u32") + return ImageFieldRead{at(a, 1), ImageFieldOp::null_eq}; + if (i.name == "s_cmp_lg_i32" || i.name == "s_cmp_lg_u32") + return ImageFieldRead{at(a, 1), ImageFieldOp::null_lg}; + } + return {}; + } + // Keeps a field read only when its slot is the base of a sampled image op of this program and no image store or + // atomic names it. Everything else keeps the numeric-SGPR refusal. + void prepare_image_fields() { + const auto &p = analysis.program; + std::set> sampled, written; + for (size_t n = 0; n < p.code.size(); ++n) { + const auto &i = p.code[n]; + if (!p.reached[n] || i.enc != Encoding::Image) continue; + const auto &symbol = p.before[n].at(((i.b >> 16) & 31) * 4); + if (symbol.type != Symbol::Type::descriptor || symbol.value == compute_resource_heap) continue; + const std::pair key{symbol.value, symbol.offset}; + (i.op == 8 || is_image_atomic(i) ? written : sampled).insert(key); + } + for (size_t n = 0; n < p.code.size(); ++n) { + if (!p.reached[n]) continue; + const auto field = image_field_read(p.code[n], p.before[n]); + if (!field || !sampled.count(field->slot) || written.count(field->slot)) continue; + auto &entry = image_fields[field->slot]; + if (field->op == ImageFieldOp::width_lo || field->op == ImageFieldOp::width_hi) entry.width = true; + if (field->op == ImageFieldOp::base) entry.base = true; + } + if (image_fields.empty()) return; + size_t words = descriptor_strides.size(); + for (const auto &entry : image_fields) + words += size_t(entry.second.width) + size_t(entry.second.base); + for (const auto &block : analysis.result.constants) words += block.count; + if (words > 32) + refuse(p.code.front().pc, "more than 32 entry words with image fields"); + } + // After lowering: names the sampled-texture binding behind each slot, in the order initialize_constants laid out + // the words (width, then base, per slot). + void bind_image_fields() { + auto &r = analysis.result; + for (const auto &[slot, entry] : image_fields) { + const ComputeViewBinding *found = nullptr; + unsigned matches = 0; + for (const auto &b : r.bindings) + if (b.kind == ComputeViewKind::texture_srv && b.root_parameter == slot.first && + b.byte_offset == slot.second) { + found = &b; + ++matches; + } + if (!matches) + refuse(analysis.program.code.front().pc, "an image descriptor field has no sampled texture"); + if (matches > 1) + refuse(analysis.program.code.front().pc, + "an image descriptor field names a slot with more than one texture binding"); + if (entry.width) r.image_fields.emplace_back(found->shader_register, 0u); + if (entry.base) r.image_fields.emplace_back(found->shader_register, 1u); + } + } void prepare_descriptor_strides() { const auto &p = analysis.program; const auto &layout = analysis.layout; @@ -3300,6 +3396,27 @@ struct DirectCompute { } void arithmetic(const Inst &i, const State &state, const string *permuted = nullptr, bool compute = false) { auto n = i.name; + if (compute && !image_fields.empty()) { + const auto field = image_field_read(i, state); + const auto entry = field ? image_fields.find(field->slot) : image_fields.end(); + if (entry != image_fields.end()) { + string result; + switch (field->op) { + case ImageFieldOp::width_lo: result = ir.binary("and", entry->second.width_word, "3"); break; + case ImageFieldOp::width_hi: + result = ir.binary("and", ir.binary("lshr", entry->second.width_word, "2"), "16383"); + break; + case ImageFieldOp::base: result = ir.binary("and", entry->second.base_word, "15"); break; + // Word 1 holds the 9-bit format at bit 20, nonzero for any texture; the runtime refuses a dispatch + // whose texture is not resolved. + case ImageFieldOp::null_eq: write(128, "0"); return; + case ImageFieldOp::null_lg: write(128, "1"); return; + } + write(i.dst, result); + write(128, ir.value("zext i1 " + cmp("ne", result, "0") + " to i32")); + return; + } + } if (compute) { if (const auto offset = heap_offset_add(i, state)) { const auto base = i.src[1 - *offset]; @@ -5083,6 +5200,7 @@ struct DirectCompute { if (uses_lds) ir.globals << "@lds = internal addrspace(3) global [" << lds_words << " x i32] undef, align 4\n"; prepare_descriptor_strides(); + prepare_image_fields(); const auto &p = analysis.program; mode_bits = 0xf0; bool mode_write = false, conditional = false; @@ -5210,6 +5328,7 @@ struct DirectCompute { else arithmetic(i, p.before[k], nullptr, true); } + bind_image_fields(); auto &r = analysis.result; r.instruction_count = unsigned(p.code.size()); const unsigned rank_words = !virtual_waves && x * y * z > 64 ? x * y * z : 0; diff --git a/src/native_compute_pipeline.cpp b/src/native_compute_pipeline.cpp index f0f51b1299..b3acfe92d9 100644 --- a/src/native_compute_pipeline.cpp +++ b/src/native_compute_pipeline.cpp @@ -49,6 +49,8 @@ NativeComputePipeline create_native_compute_pipeline(ID3D12Device *device, Shade } pipeline.uav_count = counts[1]; pipeline.constants = std::move(program.constants); + if (!program.image_fields.empty()) + throw std::runtime_error("image descriptor field words are not bound by NativeComputePipeline"); pipeline.stride_srvs = std::move(program.stride_srvs); for (const auto &block : pipeline.constants) pipeline.constant_words += block.count; diff --git a/src/startup_stubs/d3d12_compute_dispatch.inl b/src/startup_stubs/d3d12_compute_dispatch.inl index 9356b70f3f..498f385ff1 100644 --- a/src/startup_stubs/d3d12_compute_dispatch.inl +++ b/src/startup_stubs/d3d12_compute_dispatch.inl @@ -32,7 +32,9 @@ struct ComputeTranslation { // constant (parameter and word) that indexes its 32-byte record in the list's view heap, or, with word_param // set, the table buffer (word_param, word_byte) whose word at byte at + root constant (a, b) * times (a < 0: // at alone) is the index. An entry value that is not a root constant names input or output a by its source. - enum Source { root_constant, input_stride, input_word3, output_word3, output_stride, output_records }; + // input_image_width is width - 1 and input_image_base the most detailed mip of the input texture's view. + enum Source { root_constant, input_stride, input_word3, input_image_width, input_image_base, output_word3, output_stride, + output_records }; struct Slot { int a, b; bool texture, heap; @@ -325,8 +327,9 @@ const char* load_compute_translation(ComputeTranslation& tr) const auto whole = [&] { return used == static_cast(line.size()); }; // The manifest names of the Source values after root_constant, in their order. const auto source = [&] { - static const char* const names[] = {"stride", "descword3", "uav_descword3", "uav_stride", "uav_numrecords"}; - for (int s = 0; s < 5; ++s) + static const char* const names[] = {"stride", "descword3", "imgwidth", "imgbase", "uav_descword3", "uav_stride", + "uav_numrecords"}; + for (int s = 0; s < 7; ++s) if (!std::strcmp(name, names[s])) return static_cast(s + 1); return ComputeTranslation::root_constant; }; @@ -442,9 +445,13 @@ const char* load_compute_translation(ComputeTranslation& tr) return "entry value, input or output count"; for (const ComputeTranslation::Slot& ud : tr.uds) { const auto& slots = ud.source >= ComputeTranslation::output_word3 ? tr.uavs : tr.srvs; + const bool image_field = ud.source == ComputeTranslation::input_image_width || + ud.source == ComputeTranslation::input_image_base; if (ud.source != ComputeTranslation::root_constant && - (ud.a < 0 || static_cast(ud.a) >= slots.size() || slots[ud.a].texture || slots[ud.a].root)) - return "an entry value names a descriptor that is not a buffer input or output"; + (ud.a < 0 || static_cast(ud.a) >= slots.size() || slots[ud.a].root || + (image_field ? !slots[ud.a].texture : slots[ud.a].texture))) + return image_field ? "an image field entry value names a view that is not a texture input" + : "an entry value names a descriptor that is not a buffer input or output"; } x2w_startup::ComPtr code; if (!cache_path(path, L"", tr.key, L".cs.cso") || FAILED(D3DReadFileToBlob(path, &code))) return "no compiled program"; @@ -520,6 +527,18 @@ bool translate_compute_natively(ComputeTranslation& tr) slot.source = ComputeTranslation::input_stride; tr.uds.push_back(slot); } + for (const auto& [srv, field] : program.image_fields) { + const bool present = std::any_of(program.bindings.begin(), program.bindings.end(), [srv = srv](const auto& view) { + return view.kind == x2w::ComputeViewKind::texture_srv && view.shader_register == srv && + view.image_dimension == 1 && view.array_count == 1; + }); + if (!present) throw std::runtime_error("image field source is not a bound texture SRV"); + ComputeTranslation::Slot slot{}; + slot.a = static_cast(srv); + slot.source = field ? ComputeTranslation::input_image_base : ComputeTranslation::input_image_width; + tr.uds.push_back(slot); + } + if (tr.uds.size() > 32) throw std::runtime_error("more than 32 entry values"); std::sort(program.bindings.begin(), program.bindings.end(), [](const auto& a, const auto& b) { return a.shader_register < b.shader_register; }); for (const auto& view : program.bindings) { @@ -789,6 +808,7 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch& // An indexed atomic UAV: the {stride, count} root constants bound at parameter 2 + its UAV index. UINT atomic_stride = 0, atomic_count = 0; bool atomic = false; + DXGI_FORMAT title_format = DXGI_FORMAT_UNKNOWN; // an input texture: the view's format as the title recorded it }; static_assert(sizeof(D3D12_SHADER_RESOURCE_VIEW_DESC) >= ViewRecord::srv_desc_bytes); static_assert(sizeof(D3D12_UNORDERED_ACCESS_VIEW_DESC) >= ViewRecord::uav_desc_bytes); @@ -902,6 +922,7 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch& D3D12_SHADER_RESOURCE_VIEW_DESC desc{}; const bool described = record.desc_bytes >= ViewRecord::srv_desc_bytes; if (described) std::memcpy(&desc, record.desc, ViewRecord::srv_desc_bytes); + const DXGI_FORMAT title_format = desc.Format ? desc.Format : resource.Format; if (desc.Format) desc.Format = desktop_format(desc.Format); words[3] = static_cast(desc.Format ? desc.Format : resource.Format) | static_cast(desc.ViewDimension) << 16; if (!known || record.slot != 18) reason = "an input texture entry is not the token of a shader resource view"; @@ -946,6 +967,7 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch& views[i] = {texture, 0, 0, true, true, described}; views[i].desc.srv = desc; views[i].wrapper = wrapper; + views[i].title_format = title_format; continue; } D3D12_UNORDERED_ACCESS_VIEW_DESC desc{}; @@ -1263,9 +1285,25 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch& } values[i] = word[3]; break; + case ComputeTranslation::input_image_width: + case ComputeTranslation::input_image_base: { + // The descriptor holds the image extent of mip 0 in texels and the view's first mip (AMD PAL, public). + // 96-bit and block-compressed formats program another extent, and a padded view cannot be seen here. + const auto fmt = static_cast(view.title_format); + if (!view.texture || !view.resource || tr->srvs[ud.a].srv_dimension != D3D12_SRV_DIMENSION_TEXTURE2D) + reason = "an image field source is not an input texture"; + else if ((fmt >= 5 && fmt <= 8) || (fmt >= 70 && fmt <= 84) || (fmt >= 94 && fmt <= 99)) + reason = "an image field source has a format whose descriptor extent is not its width"; + else if (ud.source == ComputeTranslation::input_image_width) + values[i] = static_cast(view.resource->GetDesc().Width - 1); + else + values[i] = view.described ? view.desc.srv.Texture2D.MostDetailedMip : 0; + break; + } case ComputeTranslation::output_word3: values[i] = view.words[3]; break; case ComputeTranslation::output_stride: values[i] = view.words[1] >> 16; break; // unmasked: the program checks the swizzle bits case ComputeTranslation::output_records: values[i] = view.words[2]; break; + default: reason = "an entry value source is unknown"; break; } } // A texture the title filled in memory is brought up to date first. An upload recorded before a later one diff --git a/tests/cmake/startup_compute_dispatch_image_fields.cmake b/tests/cmake/startup_compute_dispatch_image_fields.cmake new file mode 100644 index 0000000000..de7f1e5ba0 --- /dev/null +++ b/tests/cmake/startup_compute_dispatch_image_fields.cmake @@ -0,0 +1,17 @@ +if(TARGET compile_dxil AND EXISTS "${INTAKE_PYTHON}" + AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll") + add_test(NAME startup_compute_dispatch_image_fields_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_dispatch_image_fields_check.py" compile + "$" "${DXC_BIN_DIR}") + set_tests_properties(startup_compute_dispatch_image_fields_controls PROPERTIES TIMEOUT 120) +endif() +if(TARGET compile_dxil AND TARGET startup_stream_consumer AND TARGET offscreen_check AND EXISTS "${INTAKE_PYTHON}" + AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll") + foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_compute_dispatch_image_fields_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_dispatch_image_fields_check.py" + "$" "$" "$" + "${DXC_BIN_DIR}" ${adapter}) + set_tests_properties(startup_compute_dispatch_image_fields_${adapter} PROPERTIES TIMEOUT 180 SKIP_RETURN_CODE 77) + endforeach() +endif() diff --git a/tests/native_compute_manifest_reasons.cpp b/tests/native_compute_manifest_reasons.cpp index 858f61dab5..8aec24a5c1 100644 --- a/tests/native_compute_manifest_reasons.cpp +++ b/tests/native_compute_manifest_reasons.cpp @@ -118,6 +118,33 @@ int main() { }}, {"comparison sampler", [](auto &p) { p.samplers.push_back({{0, 0, 0, 0}, true, true}); }}, {"stride words from a view that is not one raw vector SRV", [](auto &p) { p.stride_srvs = {0}; }}, + {"image field words from a view that is not one 2D texture SRV", [](auto &p) { p.image_fields = {{0, 0}}; }}, + {"image field words from a view that is not one 2D texture SRV", [](auto &p) { // 1D + p.bindings[0].kind = K::texture_srv; + p.bindings[0].image_dimension = 0; + p.image_fields = {{0, 1}}; + }}, + {"image field words from a view that is not one 2D texture SRV", [](auto &p) { // 2D array dimension + p.bindings[0].kind = K::texture_srv; + p.bindings[0].image_dimension = 5; + p.image_fields = {{0, 0}}; + }}, + {"image field words from a view that is not one 2D texture SRV", [](auto &p) { // no such SRV + p.bindings[0].kind = K::texture_srv; + p.bindings[0].image_dimension = 1; + p.image_fields = {{1, 0}}; + }}, + {"image field words from a view that is not one 2D texture SRV", [](auto &p) { // unknown field + p.bindings[0].kind = K::texture_srv; + p.bindings[0].image_dimension = 1; + p.image_fields = {{0, 2}}; + }}, + {"more than 32 user data words", [](auto &p) { + p.constants.assign(3, {0, 0, 8}); + p.bindings[0].kind = K::texture_srv; + p.bindings[0].image_dimension = 1; + p.image_fields.assign(9, {0, 0}); + }}, }; for (const auto &c : cases) { auto program = base(); @@ -194,6 +221,24 @@ int main() { std::printf(" | got: %s\n", manifest ? manifest->c_str() : reason.c_str()); failures += !ok; } - std::printf("%d of %zu manifest reasons wrong\n", failures, std::size(cases) + std::size(published) + 3); + // Image field words follow the root constants: `udsrc N imgwidth S` and `udsrc N imgbase S`, S the SRV index. + { + auto program = base(); + program.bindings[0].kind = K::texture_srv; + program.bindings[0].image_dimension = 1; + program.image_fields = {{0, 0}, {0, 1}}; + reason = "unset"; + const auto manifest = format_compute_runtime_manifest(program, &reason); + const std::string wanted = "x2w-manifest 2\nstage cs\ngroup 8 4 1\nud 4\nudsrc 0 rootconst 0 0\n" + "udsrc 1 rootconst 0 1\nudsrc 2 imgwidth 0\nudsrc 3 imgbase 0\n" + "srv 0 tex2d table 1 64\nuav 0 table 1 32\nnative " + + std::to_string(native_compute_output_revision) + "\n"; + const bool ok = manifest && *manifest == wanted && reason == "unset"; + std::printf("%s publishes udsrc imgwidth and imgbase\n", ok ? "ok " : "FAIL"); + if (!ok) + std::printf(" | got: %s\n", manifest ? manifest->c_str() : reason.c_str()); + failures += !ok; + } + std::printf("%d of %zu manifest reasons wrong\n", failures, std::size(cases) + std::size(published) + 4); return failures ? 1 : 0; } diff --git a/tests/startup_compute_dispatch_image_fields_check.py b/tests/startup_compute_dispatch_image_fields_check.py new file mode 100644 index 0000000000..9a107fbe2d --- /dev/null +++ b/tests/startup_compute_dispatch_image_fields_check.py @@ -0,0 +1,305 @@ +"""A published native compute program reading the width, base mip and emptiness of a 2D texture descriptor from a table. + +usage: startup_compute_dispatch_image_fields_check.py + startup_compute_dispatch_image_fields_check.py compile + startup_compute_dispatch_image_fields_check.py binary + +The program loads the eight words of the table descriptor at byte 64 (a texture SRV) and the output UAV at byte 32, then +reads the fields of the texture's V# with scalar instructions: width - 1 as (word 2 & 0x3fff) << 2 | bfe(word 1, 0x2001e) +(PAL gfx10 sq_img_rsrc_t: width_lo at word 1 bits 31:30, width_hi at word 2 bits 13:0), the base mip as +bfe(word 3, 0x4000c) (bits 15:12), and an emptiness test s_cmp_eq_u32 word 1, 0 that branches over a store of 0xdeadbeef. It +stores width, base and the texel (1, 1) of the view's mip 0 (image_load) to words 0, 1 and 3 of the raw UAV. The translator +replaces those descriptor reads with entry words the runtime fills from the bound view, so the published manifest lists +`udsrc 2 imgwidth 0` and `udsrc 3 imgbase 0` after `ud 4` (two root constants come first), and the input as +`srv 0 tex2d table 1 64`. +The consumer (dispatch-texdim-fields) creates a committed R32_FLOAT 8x4 texture with three mips (texel (x, y) of mip m = +100 m + x + y width_m) and a TEXTURE2D view of it at table byte 64 with MostDetailedMip 1, MipLevels 2. Expected output: +word 0 = 8 (the mip-0 width, not the view's 4), word 1 = 1, word 2 = the sentinel (the emptiness branch is taken, so the +store does not run), word 3 = 105.0; every other word keeps the sentinel. With the view created without a description +(-undescribed) the view starts at mip 0: width 8, base 0, texel 9.0. + +Controls (translation, through compile_dxil): `s_and_b32 ..., 0x3ff`, `s_bfe_u32 ..., 0x90014` (the format field of word 1) +and a program without the image op each keep the refusal of an unmodelled descriptor word read. +Control (run, hardware): with s_cbranch_scc1 for scc0 the store runs and word 2 is 0xdeadbeef, which shows that the +store is live and that the null check is the translator's constant. +Exit 77 when the adapter is not available. On hardware without wave32 the run fails (the translated module needs it). + +The translator's DXIL is [WaveSize(32)], which WARP cannot create. On warp the published manifest is kept and the program's +.cs.cso is replaced by IMAGE_HLSL below, the same program in HLSL for cs_6_0 reading its entry words from b0: that +run covers the manifest lines, the reader and the runtime's fill from the bound view on WARP, not the translator (hardware +covers it). +""" +from pathlib import Path +import struct +import subprocess +import sys +from tempfile import TemporaryDirectory + +from native_gather_lod_check import render +from startup_compute_dispatch_check import dispatch, lines, prepare_case +from startup_compute_translate_check import state_a, write_request +from startup_gfx2hlsl_compute_check import SENTINEL, TGID_X, TGID_Y, TGID_Z, USER7, WORDS, root_blob + +GROUP = (8, 1, 1) +STATE = state_a(GROUP, USER7 | TGID_X | TGID_Y | TGID_Z) +MODE = 'dispatch-texdim-fields' +MARKER = 0xdeadbeef +INPUT_LINE = 'srv 0 tex2d table 1 64' +OUTPUT_LINE = 'uav 0 table 1 32' +MANIFEST_FIELDS = ['ud 4', 'udsrc 2 imgwidth 0', 'udsrc 3 imgbase 0'] + +# (assembly, machine words recorded from llvm-mc 15 for gfx1013). `binary ` assembles every line again. +PROGRAM = [ + ('s_mov_b32 s20, s6', '060394be'), + ('s_mov_b32 s21, 0', '800395be'), + ('s_load_dwordx8 s[24:31], s[20:21], 0x40', '0a060cf4400000fa'), + ('s_load_dwordx4 s[32:35], s[20:21], 0x20', '0a0808f4200000fa'), + ('v_mov_b32 v1, 0', '8002027e'), + ('s_waitcnt lgkmcnt(0)', '7fc08cbf'), + ('s_bfe_u32 s36, s25, 0x2001e', '19ffa4931e000200'), + ('s_and_b32 s37, s26, 0x3fff', '1aff2587ff3f0000'), + ('s_lshl_b32 s37, s37, 2', '2582258f'), + ('s_or_b32 s36, s36, s37', '24252488'), + ('s_add_u32 s36, s36, 1', '24812480'), + ('s_bfe_u32 s38, s27, 0x4000c', '1bffa6930c000400'), + ('v_mov_b32 v2, s36', '2402047e'), + ('v_mov_b32 v3, s38', '2602067e'), + ('buffer_store_dword v2, v1, s[32:35], 0 offen', '001070e001020880'), + ('buffer_store_dword v3, v1, s[32:35], 0 offen offset:4', '041070e001030880'), + ('s_cmp_eq_u32 s25, 0', '198006bf'), + ('s_cbranch_scc0 4', '040084bf'), # over the next two instructions (4 dwords) + ('v_mov_b32 v4, 0xdeadbeef', 'ff02087eefbeadde'), + ('buffer_store_dword v4, v1, s[32:35], 0 offen offset:8', '081070e001040880'), + ('v_mov_b32 v6, 1', '81020c7e'), + ('v_mov_b32 v7, 1', '81020e7e'), + ('image_load v5, v[6:7], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D', '080100f006050600'), + ('s_waitcnt vmcnt(0)', '703f8cbf'), + ('buffer_store_dword v5, v1, s[32:35], 0 offen offset:12', '0c1070e001050880'), + ('s_endpgm', '000081bf'), +] +# Mutants: line -> (replacement assembly, replacement words) +MUTANTS = { + 'scc1': ('s_cbranch_scc0 4', ('s_cbranch_scc1 4', '040085bf')), + 'mask': ('s_and_b32 s37, s26, 0x3fff', ('s_and_b32 s37, s26, 0x3ff', '1aff2587ff030000')), + 'format': ('s_bfe_u32 s36, s25, 0x2001e', ('s_bfe_u32 s36, s25, 0x90014', '19ffa49314000900')), + 'no-image': ('image_load v5, v[6:7], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D', None), +} +# What each translation control must say (the existing refusal of a numeric read of a descriptor word). +REFUSALS = {'mask': 'unbound numeric SGPR s26', 'format': 'unbound numeric SGPR s25', + 'no-image': 'unbound numeric SGPR s25'} + + +def program(mutant=None): + out = b'' + for line, words in PROGRAM: + if mutant and MUTANTS[mutant][0] == line: + replacement = MUTANTS[mutant][1] + words = replacement[1] if replacement else '' + out += bytes.fromhex(words) + return out + + +def bits(value): + return struct.unpack(' r0 : register(t0); +RWByteAddressBuffer w0 : register(u0); +[numthreads(8, 1, 1)] void main() +{ + const uint lo = field.z & 3, hi = (field.z >> 2) & 0x3fff; + w0.Store(0, ((hi << 2) | lo) + 1); + w0.Store(4, field.w & 15); + w0.Store(12, asuint(r0.Load(int3(1, 1, 0)))); +} +""" + + +def expected(width, base, texel, marker=SENTINEL): + words = [SENTINEL] * WORDS + words[:4] = [width, base, marker, bits(texel)] + return words + + +def assemble(llvm_mc, source): + from startup_flat_param_check import assemble as run + return run(llvm_mc, source) + + +def check_binary(llvm_mc): + for line, words in PROGRAM: + built = assemble(llvm_mc, line + '\n').hex() + assert built == words, f'{line}: assembled {built}, recorded {words}' + for name, (_, replacement) in MUTANTS.items(): + if replacement: + built = assemble(llvm_mc, replacement[0] + '\n').hex() + assert built == replacement[1], f'{name}: {replacement[0]}: assembled {built}, recorded {replacement[1]}' + print('startup_compute_dispatch_image_fields binary: fixture words match llvm-mc') + + +def publish(compile_dxil, sdk, cache, key, expect_failure=False): + result = subprocess.run([compile_dxil, sdk, cache.parent / 'native', 'gfx1013-direct-cs@0', + '--requests', cache, cache], capture_output=True, text=True, timeout=60) + manifest = cache / f'{key}.txt' + if expect_failure: + assert result.returncode != 0 and not manifest.exists(), (result.stdout, result.stderr) + return result + assert result.returncode == 0 and manifest.is_file(), (result.stdout, result.stderr) + text = manifest.read_text() + assert not (cache / f'{key}.cs.hlsl').exists(), 'expected native publication' + return manifest, text + + +def check_manifest(text): + found = text.splitlines() + for line in (INPUT_LINE, OUTPUT_LINE, *MANIFEST_FIELDS): + assert found.count(line) == 1, (line, text) + assert found.index('udsrc 2 imgwidth 0') + 1 == found.index('udsrc 3 imgbase 0') + + +def check_compiler(compile_dxil, sdk): + with TemporaryDirectory() as directory: + cache = Path(directory) / 'cache' + cache.mkdir() + key = write_request(cache, program(), STATE, root=root_blob(table_range=True)) + _, text = publish(compile_dxil, sdk, cache, f'{key:016x}') + check_manifest(text) + for name, reason in REFUSALS.items(): + mutant = Path(directory) / name + mutant.mkdir() + work = mutant / 'cache' + work.mkdir() + key = write_request(work, program(name), STATE, root=root_blob(table_range=True)) + result = publish(compile_dxil, sdk, work, f'{key:016x}', expect_failure=True) + assert reason in result.stderr + result.stdout, (name, reason, result.stdout, result.stderr) + # The scc1 mutant is a program the translator accepts; only its run tells it from the original. + mutant = Path(directory) / 'scc1' + mutant.mkdir() + work = mutant / 'cache' + work.mkdir() + key = write_request(work, program('scc1'), STATE, root=root_blob(table_range=True)) + _, text = publish(compile_dxil, sdk, work, f'{key:016x}') + check_manifest(text) + print('startup_compute_dispatch_image_fields compile: PASS') + + +def hlsl_program(sdk, cache, key): + source = cache / 'image.hlsl' + source.write_text(IMAGE_HLSL) + result = subprocess.run([Path(sdk) / 'dxc.exe', '-T', 'cs_6_0', '-E', 'main', '-Fo', cache / f'{key}.cs.cso', source], + capture_output=True, text=True, timeout=60) + assert result.returncode == 0, result.stdout + result.stderr + + +def check_words(got, want, label): + first = next(((i, a, b) for i, (a, b) in enumerate(zip(got, want)) if a != b), None) + if first is not None: + word, actual, wanted = first + raise AssertionError(f'{label}: word {word} expected {wanted:08x}, read {actual:08x} ' + f'({sum(a != b for a, b in zip(got, want))} words differ)') + + +def check(compile_dxil, consumer, sdk, work, adapter): + cache, env = prepare_case(work, adapter) + env.pop('X2W_NATIVE_SHADERS', None) + env.pop('X2W_NATIVE_SHADERS_ONLY', None) + (work / 'program').write_bytes(program()) + (work / 'state').write_bytes(STATE) + untouched = [SENTINEL] * WORDS + want = expected(8, 1, 105.0) + assert want != untouched and want[2] == SENTINEL + + got, result = dispatch(work, consumer, env, MODE, probe=True) + check_words(got, untouched, 'before translation') + assert len(lines(result, 'Dispatch(3, 2, 2) refused: no translation')) == 1, result.stderr[-3000:] + [request] = cache.glob('req_*.c67') + key = request.stem.removeprefix('req_') + manifest, text = publish(compile_dxil, sdk, cache, key) + check_manifest(text) + if adapter == 'warp': + hlsl_program(sdk, cache, key) + + def clean(result, label): + errors = [line for line in lines(result, 'x2w-d3d12-debug ') if ' sev=0 ' in line or ' sev=1 ' in line] + assert not errors, (label, errors[0]) + + got, result = dispatch(work, consumer, env, MODE) + check_words(got, want, 'width, base, empty check and texel') + assert len(lines(result, 'Dispatch(3, 2, 2) translated')) == 1 and not lines(result, 'refused'), result.stderr[-3000:] + clean(result, MODE) + layers = lines(result, 'x2w-d3d12 debug layer: ') + assert layers and '0x00000000' in layers[0], ('debug layer not enabled', layers) + print(f'{adapter}: width 8 (mip 0 of the texture, not the view\'s 4), base 1, the empty-check store skipped and ' + f'texel 105.0 match; debug layer: enabled, no error message', flush=True) + + # A view without a description starts at mip 0: base 0 and the texel (1, 1) of mip 0. + got, result = dispatch(work, consumer, env, MODE + '-undescribed') + check_words(got, expected(8, 0, 9.0), 'undescribed view') + assert len(lines(result, 'Dispatch(3, 2, 2) translated')) == 1, result.stderr[-3000:] + clean(result, MODE + '-undescribed') + + # The entry words are the manifest's: an image source over a buffer slot is not a texture input and must be refused + # when the manifest is read, and the output stays untouched. + original = text + manifest.write_bytes(original.replace('udsrc 2 imgwidth 0', 'udsrc 2 stride 0').encode()) + got, result = dispatch(work, consumer, env, MODE) + check_words(got, untouched, 'manifest rewritten to stride') + assert not lines(result, 'Dispatch(3, 2, 2) translated'), result.stderr[-3000:] + manifest.write_bytes(original.replace('udsrc 2 imgwidth 0', 'udsrc 2 imgwidth 1').encode()) + got, result = dispatch(work, consumer, env, MODE) + check_words(got, untouched, 'manifest names a missing SRV') + assert not lines(result, 'Dispatch(3, 2, 2) translated'), result.stderr[-3000:] + manifest.write_bytes(original.encode()) + got, result = dispatch(work, consumer, env, MODE) + check_words(got, want, 'restored manifest') + + if adapter == 'hardware': + # With the branch inverted the store runs: the sentinel word is written, so the skipped store above was the null + # check's constant and not a dead store. + (work / 'program').write_bytes(program('scc1')) + got, result = dispatch(work, consumer, env, MODE) + # The request is keyed by the program, so a new key needs its own publication. + if lines(result, 'refused: no translation'): + [request] = [p for p in cache.glob('req_*.c67') if p.stem.removeprefix('req_') != key] + publish(compile_dxil, sdk, cache, request.stem.removeprefix('req_')) + got, result = dispatch(work, consumer, env, MODE) + check_words(got, expected(8, 1, 105.0, MARKER), 'branch inverted') + print('hardware: with s_cbranch_scc1 the store runs and word 2 is 0xdeadbeef', flush=True) + + # The in-process route (X2W_NATIVE_SHADERS) fills the same entry words from the program's own bindings. + native = work / 'inprocess' + native.mkdir() + _, env = prepare_case(native, adapter) + env.update(X2W_NATIVE_SHADERS=sdk, X2W_NATIVE_SHADERS_ONLY='1') + (native / 'program').write_bytes(program()) + (native / 'state').write_bytes(STATE) + got, result = dispatch(native, consumer, env, MODE) + check_words(got, want, 'in-process translation') + assert len(lines(result, 'translated natively: 4 entry values, 1 inputs, 1 outputs')) == 1, result.stderr[-3000:] + assert len(lines(result, 'Dispatch(3, 2, 2) translated')) == 1, result.stderr[-3000:] + clean(result, 'in-process translation') + print('hardware: the in-process translation fills the same words (4 entry values, 1 input, 1 output)', flush=True) + print(f'{adapter}: the manifest rewritten to a buffer source or a missing SRV is refused with the output untouched', + flush=True) + + +def main(compile_dxil, consumer, renderer, sdk, adapter): + if adapter == 'hardware': + wave = render(renderer, adapter, '--shader-wave32') + assert wave.returncode == 0, wave.stdout + wave.stderr + with TemporaryDirectory() as directory: + check(compile_dxil, consumer, sdk, Path(directory) / adapter, adapter) + print(f'startup_compute_dispatch_image_fields: PASS on {adapter}') + + +if __name__ == '__main__': + if len(sys.argv) == 4 and sys.argv[1] == 'compile': + check_compiler(*sys.argv[2:]) + elif len(sys.argv) == 3 and sys.argv[1] == 'binary': + check_binary(sys.argv[2]) + elif len(sys.argv) == 6 and sys.argv[5] in ('warp', 'hardware'): + main(*sys.argv[1:]) + else: + sys.exit(__doc__) diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index 4c7a864970..a5aa2f9ed7 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -428,7 +428,7 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin } if (mode == "dispatch-records32" || typed_output_offset || input_offset || shared_uav || mode == "dispatch-srv-token-typed" || mode == "dispatch-records32-input-null" || - mode.rfind("dispatch-cube", 0) == 0) { // output at byte 32, none at byte 16 + mode.rfind("dispatch-cube", 0) == 0 || mode.rfind("dispatch-texdim-fields", 0) == 0) { // output at byte 32, none at byte 16 std::memset(reinterpret_cast(cpu + sizeof(descriptors[0])), 0, sizeof(descriptors[1])); std::memcpy(reinterpret_cast(cpu + 32), descriptors[1], sizeof(descriptors[1])); } @@ -594,7 +594,44 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin // Byte 16 is not a descriptor of the heap: the token's words are copied there, as a title copies descriptors. if (mode == "dispatch-output-texture-token") std::memcpy(reinterpret_cast(cpu + 16), reinterpret_cast(cpu + 64), 16); } - if (mode.rfind("dispatch-texdim-", 0) == 0) { + if (mode.rfind("dispatch-texdim-fields", 0) == 0) { + // dispatch-texdim-fields[-undescribed]: records of 32 bytes (the output at byte 32, as dispatch-records32); the sampled + // input is a committed R32_FLOAT 2D texture of 8x4 texels with three mips (8x4, 4x2, 2x1), each uploaded by its own + // copy, texel (x, y) of mip m holding 100 m + x + y width_m. Its SRV at table byte 64 is a TEXTURE2D view of mip 1 with + // MipLevels 2, so the view's texel (1, 1) is 100 + 1 + 1 * 4 = 105. -undescribed creates the view without a description + // (the view then starts at mip 0, where texel (1, 1) is 9). + const std::string control = mode.substr(22); + if (!control.empty() && control != "-undescribed") return 2; + constexpr DXGI_FORMAT format = DXGI_FORMAT_R32_FLOAT; + constexpr UINT mips = 3, pitch = D3D12_TEXTURE_DATA_PITCH_ALIGNMENT, mip_bytes = pitch * 4; + const D3D12_RESOURCE_DESC texture_desc{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, 8, 4, 1, mips, format, {1, 0}}; + const D3D12_RESOURCE_DESC upload_desc{D3D12_RESOURCE_DIMENSION_BUFFER, 0, 8192, 1, 1, 1, DXGI_FORMAT_UNKNOWN, {1, 0}, + D3D12_TEXTURE_LAYOUT_ROW_MAJOR}; + void* const texture = make_committed(device, D3D12_HEAP_TYPE_DEFAULT, texture_desc, D3D12_RESOURCE_STATE_COMMON); + void* const upload = make_committed(device, D3D12_HEAP_TYPE_UPLOAD, upload_desc, D3D12_RESOURCE_STATE_GENERIC_READ); + void* texel = nullptr; + if (!texture || !upload || FAILED(static_cast(invoke(upload, 8, 0, 0, word(&texel)))) || !texel) return 80; + std::memset(texel, 0, 8192); + for (UINT mip = 0; mip < mips; ++mip) + for (UINT y = 0; y < (4u >> mip > 1 ? 4u >> mip : 1u); ++y) + for (UINT x = 0; x < (8u >> mip); ++x) { + const float value = static_cast(100 * mip + x + y * (8u >> mip)); + std::memcpy(static_cast(texel) + mip * mip_bytes + y * pitch + x * 4, &value, sizeof(value)); + } + invoke(upload, 9, 0, 0); + for (UINT mip = 0; mip < mips; ++mip) { + D3D12_TEXTURE_COPY_LOCATION image{static_cast(texture), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX}; + image.SubresourceIndex = mip; + D3D12_TEXTURE_COPY_LOCATION source{static_cast(upload), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT}; + source.PlacedFootprint = {UINT64{mip} * mip_bytes, {format, 8u >> mip, 4u >> mip > 1 ? 4u >> mip : 1u, 1, pitch}}; + invoke(list, 16, word(&image), 0, 0, 0, word(&source), 0); + } + D3D12_SHADER_RESOURCE_VIEW_DESC view{format, D3D12_SRV_DIMENSION_TEXTURE2D, D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING}; + view.Texture2D.MostDetailedMip = 1; + view.Texture2D.MipLevels = 2; + invoke(device, 18, word(texture), control == "-undescribed" ? 0 : word(&view), cpu + 64); + } + if (mode.rfind("dispatch-texdim-", 0) == 0 && mode.rfind("dispatch-texdim-fields", 0) != 0) { // dispatch-texdim-<1d|2da|3d>[-mismatch|-undescribed|-clamped]: the sampled input is a described R32_FLOAT texture // of that dimension in the SRV at table byte 64, texel (x + y * 4 + slice * 16) holding 0.25 * (index + 1): // 1d is 8 texels wide, 2da is 4x4 with six slices (the view starts at slice 1 and spans four), 3d is 4x4x4. From e37bd1ad1e094bb19547a661b6d3f412c2ee60d5 Mon Sep 17 00:00:00 2001 From: JB2WIN Date: Sun, 29 Nov 2026 22:13:32 +0100 Subject: [PATCH 2/4] tools: list the image field and entry value refusals in the triage reasons Review of #3500 (Leyukaka): startup_triage_run_check found five new source reasons missing. AI-assisted. Co-Authored-By: Claude Opus 5.5 --- tools/startup/triage_run.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/startup/triage_run.py b/tools/startup/triage_run.py index a606a6a9d8..5f11738f50 100644 --- a/tools/startup/triage_run.py +++ b/tools/startup/triage_run.py @@ -116,11 +116,15 @@ 'an MTBUF byte window requires identity DST_SEL', 'an MTBUF byte window requires identity component mapping', 'an entry value names a descriptor that is not a buffer input or output', + 'an entry value source is unknown', 'an entry value that is not a root constant or a buffer descriptor word', 'an entry value was not set since the compute root', 'an exact-width image store requires a matching output texture channel count', 'an exact-width image store requires typed UAV store support', 'an image atomic requires an R32_UINT UAV', + 'an image field entry value names a view that is not a texture input', + 'an image field source has a format whose descriptor extent is not its width', + 'an image field source is not an input texture', 'an indexed atomic output needs a slot-19 token view', 'an indexed atomic output view has an unsupported stride or range', 'an input 1D texture has no recorded view description', @@ -221,6 +225,7 @@ 'native translation from another compiler revision; rebuild it', 'native translation without a revision; rebuild it', 'no compiled program', + 'more than 32 entry values', 'no descriptor heap on this device', 'no translation', 'not a compute manifest', From b04c1a7f3d908f66ce28d8edbfcf89c60494ce77 Mon Sep 17 00:00:00 2001 From: JB2WIN Date: Sun, 29 Nov 2026 22:37:33 +0100 Subject: [PATCH 3/4] tests: cover s_cmp_lg_u32 and the SCC of replaced image field reads The image field fixture now also assembles s_cmp_lg_u32 word 1, 0 with a branch over a store, and two field reads (base, width_hi) each followed by s_cbranch_scc0 over a store, so the SCC the translator writes for a replaced read is consumed. Base 0 on the undescribed view covers the SCC 0 path. A mutant now replaces only the first line it names, since s_cbranch_scc0 4 occurs twice. AI-assisted. Co-Authored-By: Claude Opus 5.5 --- ...tup_compute_dispatch_image_fields_check.py | 42 +++++++++++++++---- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/tests/startup_compute_dispatch_image_fields_check.py b/tests/startup_compute_dispatch_image_fields_check.py index 9a107fbe2d..0bfb468fa2 100644 --- a/tests/startup_compute_dispatch_image_fields_check.py +++ b/tests/startup_compute_dispatch_image_fields_check.py @@ -8,24 +8,30 @@ reads the fields of the texture's V# with scalar instructions: width - 1 as (word 2 & 0x3fff) << 2 | bfe(word 1, 0x2001e) (PAL gfx10 sq_img_rsrc_t: width_lo at word 1 bits 31:30, width_hi at word 2 bits 13:0), the base mip as bfe(word 3, 0x4000c) (bits 15:12), and an emptiness test s_cmp_eq_u32 word 1, 0 that branches over a store of 0xdeadbeef. It -stores width, base and the texel (1, 1) of the view's mip 0 (image_load) to words 0, 1 and 3 of the raw UAV. The translator +stores width, base and the texel (1, 1) of the view's mip 0 (image_load) to words 0, 1 and 3 of the raw UAV. A second +test s_cmp_lg_u32 word 1, 0 branches over a store of 0x600df00d to word 4 when the view is empty, and two more field reads +(the base and the width_hi mask) are each followed by s_cbranch_scc0 over a store of base to word 5 and width to word 6, +so the SCC each replaced read writes (its result != 0) is consumed. The base read is seen with both SCC values (base 1 +and, on the undescribed view, base 0); the width_hi read only with SCC 1, since both views have width 8. The translator replaces those descriptor reads with entry words the runtime fills from the bound view, so the published manifest lists `udsrc 2 imgwidth 0` and `udsrc 3 imgbase 0` after `ud 4` (two root constants come first), and the input as `srv 0 tex2d table 1 64`. The consumer (dispatch-texdim-fields) creates a committed R32_FLOAT 8x4 texture with three mips (texel (x, y) of mip m = 100 m + x + y width_m) and a TEXTURE2D view of it at table byte 64 with MostDetailedMip 1, MipLevels 2. Expected output: word 0 = 8 (the mip-0 width, not the view's 4), word 1 = 1, word 2 = the sentinel (the emptiness branch is taken, so the -store does not run), word 3 = 105.0; every other word keeps the sentinel. With the view created without a description -(-undescribed) the view starts at mip 0: width 8, base 0, texel 9.0. +store does not run), word 3 = 105.0, word 4 = 0x600df00d, word 5 = 1, word 6 = 8; every other word keeps the sentinel. +With the view created without a description (-undescribed) the view starts at mip 0: width 8, base 0, texel 9.0, and +word 5 keeps the sentinel (base 0 sets SCC to 0, so its store is skipped). Controls (translation, through compile_dxil): `s_and_b32 ..., 0x3ff`, `s_bfe_u32 ..., 0x90014` (the format field of word 1) and a program without the image op each keep the refusal of an unmodelled descriptor word read. -Control (run, hardware): with s_cbranch_scc1 for scc0 the store runs and word 2 is 0xdeadbeef, which shows that the +Control (run, hardware): with s_cbranch_scc1 for the first (emptiness) s_cbranch_scc0 the store runs and word 2 is 0xdeadbeef, which shows that the store is live and that the null check is the translator's constant. Exit 77 when the adapter is not available. On hardware without wave32 the run fails (the translated module needs it). The translator's DXIL is [WaveSize(32)], which WARP cannot create. On warp the published manifest is kept and the program's -.cs.cso is replaced by IMAGE_HLSL below, the same program in HLSL for cs_6_0 reading its entry words from b0: that +.cs.cso is replaced by IMAGE_HLSL below, the same program in HLSL for cs_6_0 reading its entry words from b0, with +both emptiness branches resolved for a non-empty view (the store of word 2 dropped, that of word 4 unconditional): that run covers the manifest lines, the reader and the runtime's fill from the bound view on WARP, not the translator (hardware covers it). """ @@ -70,6 +76,17 @@ ('s_cbranch_scc0 4', '040084bf'), # over the next two instructions (4 dwords) ('v_mov_b32 v4, 0xdeadbeef', 'ff02087eefbeadde'), ('buffer_store_dword v4, v1, s[32:35], 0 offen offset:8', '081070e001040880'), + ('s_cmp_lg_u32 s25, 0', '198007bf'), + ('s_cbranch_scc0 4', '040084bf'), # not taken: the view is not empty, so the store of 0x600df00d runs + ('v_mov_b32 v8, 0x600df00d', 'ff02107e0df00d60'), + ('buffer_store_dword v8, v1, s[32:35], 0 offen offset:16', '101070e001080880'), + # The SCC of a replaced field read is its result != 0: base 0 skips the store of word 5, width 8 runs that of word 6. + ('s_bfe_u32 s39, s27, 0x4000c', '1bffa7930c000400'), + ('s_cbranch_scc0 2', '020084bf'), + ('buffer_store_dword v3, v1, s[32:35], 0 offen offset:20', '141070e001030880'), + ('s_and_b32 s40, s26, 0x3fff', '1aff2887ff3f0000'), + ('s_cbranch_scc0 2', '020084bf'), + ('buffer_store_dword v2, v1, s[32:35], 0 offen offset:24', '181070e001020880'), ('v_mov_b32 v6, 1', '81020c7e'), ('v_mov_b32 v7, 1', '81020e7e'), ('image_load v5, v[6:7], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D', '080100f006050600'), @@ -91,11 +108,15 @@ def program(mutant=None): out = b'' + pending = mutant for line, words in PROGRAM: - if mutant and MUTANTS[mutant][0] == line: - replacement = MUTANTS[mutant][1] + # A mutant replaces the first line it names; `s_cbranch_scc0 4` occurs twice. + if pending and MUTANTS[pending][0] == line: + replacement = MUTANTS[pending][1] words = replacement[1] if replacement else '' + pending = None out += bytes.fromhex(words) + assert pending is None, mutant return out @@ -113,13 +134,18 @@ def bits(value): w0.Store(0, ((hi << 2) | lo) + 1); w0.Store(4, field.w & 15); w0.Store(12, asuint(r0.Load(int3(1, 1, 0)))); + w0.Store(16, 0x600df00d); + if ((field.w & 15) != 0) w0.Store(20, field.w & 15); + if (hi != 0) w0.Store(24, ((hi << 2) | lo) + 1); } """ +LG_MARKER = 0x600df00d def expected(width, base, texel, marker=SENTINEL): words = [SENTINEL] * WORDS - words[:4] = [width, base, marker, bits(texel)] + # Words 4-6: the s_cmp_lg_u32 store, then base and width stored only when the field read's SCC (result != 0) is 1. + words[:7] = [width, base, marker, bits(texel), LG_MARKER, base or SENTINEL, width if ((width - 1) >> 2) & 0x3fff else SENTINEL] return words From 2902e500895ac2cd82bfa1c38e4048c42ca3eed5 Mon Sep 17 00:00:00 2001 From: JB2WIN Date: Sun, 29 Nov 2026 23:25:38 +0100 Subject: [PATCH 4/4] tests: assemble the _i32 forms of the image emptiness tests s_cmp_eq_i32 and s_cmp_lg_i32 word 1, 0 each branch over a store that runs for a non-empty view (words 7 and 8). AI-assisted. Co-Authored-By: Claude Opus 5.5 --- ...tup_compute_dispatch_image_fields_check.py | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tests/startup_compute_dispatch_image_fields_check.py b/tests/startup_compute_dispatch_image_fields_check.py index 0bfb468fa2..4fd1ecefd9 100644 --- a/tests/startup_compute_dispatch_image_fields_check.py +++ b/tests/startup_compute_dispatch_image_fields_check.py @@ -12,14 +12,17 @@ test s_cmp_lg_u32 word 1, 0 branches over a store of 0x600df00d to word 4 when the view is empty, and two more field reads (the base and the width_hi mask) are each followed by s_cbranch_scc0 over a store of base to word 5 and width to word 6, so the SCC each replaced read writes (its result != 0) is consumed. The base read is seen with both SCC values (base 1 -and, on the undescribed view, base 0); the width_hi read only with SCC 1, since both views have width 8. The translator +and, on the undescribed view, base 0); the width_hi read only with SCC 1, since both views have width 8. Last, +s_cmp_eq_i32 and s_cmp_lg_i32 word 1, 0 each branch over a store of 0x600df00d (words 7 and 8) that runs for a +non-empty view. The translator replaces those descriptor reads with entry words the runtime fills from the bound view, so the published manifest lists `udsrc 2 imgwidth 0` and `udsrc 3 imgbase 0` after `ud 4` (two root constants come first), and the input as `srv 0 tex2d table 1 64`. The consumer (dispatch-texdim-fields) creates a committed R32_FLOAT 8x4 texture with three mips (texel (x, y) of mip m = 100 m + x + y width_m) and a TEXTURE2D view of it at table byte 64 with MostDetailedMip 1, MipLevels 2. Expected output: word 0 = 8 (the mip-0 width, not the view's 4), word 1 = 1, word 2 = the sentinel (the emptiness branch is taken, so the -store does not run), word 3 = 105.0, word 4 = 0x600df00d, word 5 = 1, word 6 = 8; every other word keeps the sentinel. +store does not run), word 3 = 105.0, word 4 = 0x600df00d, word 5 = 1, word 6 = 8, words 7 and 8 = 0x600df00d; every +other word keeps the sentinel. With the view created without a description (-undescribed) the view starts at mip 0: width 8, base 0, texel 9.0, and word 5 keeps the sentinel (base 0 sets SCC to 0, so its store is skipped). @@ -87,6 +90,13 @@ ('s_and_b32 s40, s26, 0x3fff', '1aff2887ff3f0000'), ('s_cbranch_scc0 2', '020084bf'), ('buffer_store_dword v2, v1, s[32:35], 0 offen offset:24', '181070e001020880'), + # The _i32 forms of both emptiness tests: neither branch is taken, so words 7 and 8 get 0x600df00d. + ('s_cmp_eq_i32 s25, 0', '198000bf'), + ('s_cbranch_scc1 2', '020085bf'), + ('buffer_store_dword v8, v1, s[32:35], 0 offen offset:28', '1c1070e001080880'), + ('s_cmp_lg_i32 s25, 0', '198001bf'), + ('s_cbranch_scc0 2', '020084bf'), + ('buffer_store_dword v8, v1, s[32:35], 0 offen offset:32', '201070e001080880'), ('v_mov_b32 v6, 1', '81020c7e'), ('v_mov_b32 v7, 1', '81020e7e'), ('image_load v5, v[6:7], s[24:31] dmask:0x1 dim:SQ_RSRC_IMG_2D', '080100f006050600'), @@ -137,6 +147,8 @@ def bits(value): w0.Store(16, 0x600df00d); if ((field.w & 15) != 0) w0.Store(20, field.w & 15); if (hi != 0) w0.Store(24, ((hi << 2) | lo) + 1); + w0.Store(28, 0x600df00d); + w0.Store(32, 0x600df00d); } """ LG_MARKER = 0x600df00d @@ -145,7 +157,9 @@ def bits(value): def expected(width, base, texel, marker=SENTINEL): words = [SENTINEL] * WORDS # Words 4-6: the s_cmp_lg_u32 store, then base and width stored only when the field read's SCC (result != 0) is 1. - words[:7] = [width, base, marker, bits(texel), LG_MARKER, base or SENTINEL, width if ((width - 1) >> 2) & 0x3fff else SENTINEL] + # Words 7 and 8: the stores after s_cmp_eq_i32 and s_cmp_lg_i32 on a non-empty view. + words[:9] = [width, base, marker, bits(texel), LG_MARKER, base or SENTINEL, + width if ((width - 1) >> 2) & 0x3fff else SENTINEL, LG_MARKER, LG_MARKER] return words