From a1f169d2b7389f06fd48bab0eab38de2806f1f23 Mon Sep 17 00:00:00 2001 From: Royoo <3230853833@qq.com> Date: Fri, 27 Nov 2026 23:45:16 +0800 Subject: [PATCH] Preserve local menu shader and settings texture fixes --- src/direct_compute_cache.cpp | 13 +++-- src/gfx1013_direct_compute.inl | 4 +- src/gfx1013_direct_graphics.inl | 48 ++++++++++++++++--- src/startup_stubs/d3d12_compute_dispatch.inl | 9 ++-- ...native_graphics_interface_library_check.py | 16 +++++++ tests/native_indexed_pixel_texture_check.py | 9 +++- 6 files changed, 83 insertions(+), 16 deletions(-) diff --git a/src/direct_compute_cache.cpp b/src/direct_compute_cache.cpp index f1e67681f2..ecd67d12a5 100644 --- a/src/direct_compute_cache.cpp +++ b/src/direct_compute_cache.cpp @@ -682,6 +682,7 @@ DirectGraphicsPair prepare_direct_graphics(std::string_view a, std::string_view std::map interfaces; std::optional first_stage; bool exact_pair = reflected; + bool exact_pixel_code = false; Bytes entry; const bool fixed_entry = pixel_entry.size() == 12 && pixel_entry.substr(0,4) == "PSI1"; if (fixed_entry && !reflected) @@ -695,6 +696,7 @@ DirectGraphicsPair prepare_direct_graphics(std::string_view a, std::string_view auto matched = match_graphics_stage(raw[i],companions[i],exact); exact_pair &= exact; auto stage = matched.stage; + if (stage == GpuStage::ps) exact_pixel_code = exact; if (i == 0) first_stage = stage; if (!interfaces.emplace(stage, std::move(matched)).second) throw UnsupportedComputeShader("duplicate reflected graphics stage"); @@ -777,8 +779,12 @@ DirectGraphicsPair prepare_direct_graphics(std::string_view a, std::string_view channels[r.index / 4] |= 1u << r.first; } for (const auto &attr : attrs) { + // A specialized raw PS can read more components of the same PARAM + // than its library variant. The raw VS/PS link above has already + // checked every consumed component against a matching VS export. + const bool linked_raw_row = fixed_entry && !exact_pixel_code && attr.row == attr.location; if (attr.usage != 6 || attr.location >= channels.size() || attr.flags != 2 || - (channels[attr.location] & ~attr.mask) || !attr.mask) + (!linked_raw_row && (channels[attr.location] & ~attr.mask)) || !attr.mask) throw UnsupportedComputeShader("raw and reflected interpolation channels disagree at location " + std::to_string(attr.location) + " (usage " + std::to_string(attr.usage) + ", flags " + std::to_string(attr.flags) + ", reflected mask " + std::to_string(attr.mask) + @@ -917,8 +923,9 @@ std::optional format_compute_runtime_manifest(const NativeComputeSh uav_lines << "uav " << b.shader_register << " table " << b.root_parameter << ' ' << b.byte_offset << '\n'; } else if ((b.kind == ComputeViewKind::texture_uav || b.kind == ComputeViewKind::half4_texture_uav) && - (b.image_dimension == 1 || b.image_dimension == 5) && b.array_count == 1) { - uav_lines << "uav " << b.shader_register << (b.image_dimension == 5 ? " tex2da" : " tex2d") + (b.image_dimension == 1 || b.image_dimension == 5 || + (b.kind == ComputeViewKind::texture_uav && b.image_dimension == 2)) && b.array_count == 1) { + uav_lines << "uav " << b.shader_register << (b.image_dimension == 5 ? " tex2da" : b.image_dimension == 2 ? " tex3d" : " tex2d") << " float table " << b.root_parameter << ' ' << b.byte_offset << " channels 4\n"; } else { return std::nullopt; diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index 70147b5c71..a51b1e5e4d 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -53,7 +53,7 @@ struct DirectCompute { unsigned graphics_pc = 0; bool perspective_center_pair = false; bool exact_pixel_entry = false; - bool helper_result_unobserved = false; + std::function helper_result_unobserved; unsigned pixel_entry_vgprs = 0; std::function untouched_pixel_register; // P1/P2 consume the entry IJ pair symbolically. After an exact-mask @@ -123,7 +123,7 @@ struct DirectCompute { (!graphics_registers.count(r) || (guarded_ij.count(r) && guarded_ij.at(r).first == graphics_predicate))) { write(r, {}); guarded_ij.emplace(r, std::make_pair(graphics_predicate, value)); - } else if (exact_pixel_entry && helper_result_unobserved && pixel_entry_vgprs && r >= 256 + pixel_entry_vgprs && + } else if (exact_pixel_entry && helper_result_unobserved(r) && pixel_entry_vgprs && r >= 256 + pixel_entry_vgprs && !graphics_registers.count(r) && untouched_pixel_register(r)) { // Only covered lanes can observe this result. In particular no // subsequent implicit LOD or quad operation can inspect helpers. diff --git a/src/gfx1013_direct_graphics.inl b/src/gfx1013_direct_graphics.inl index 137b5cc9ab..2762795257 100644 --- a/src/gfx1013_direct_graphics.inl +++ b/src/gfx1013_direct_graphics.inl @@ -957,7 +957,7 @@ struct GraphicsMaskRegions { bool sampled = false; for (; end < code.size(); ++end) { const auto &i = code[end]; - if (i.enc == Encoding::Image && i.op >= 0x20 && i.op <= 0x3f) sampled = true; + if (i.enc == Encoding::Image && (i.op == 0 || (i.op >= 0x20 && i.op <= 0x3f))) sampled = true; else if (i.name != "s_waitcnt" && i.name != "s_nop") break; } if (!sampled || end == code.size() || !restore_mask(code[end]) || code[end].src[0] == start.src[0]) continue; @@ -1604,6 +1604,40 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = return false; }; const auto last_lane_observer = std::find_if_not(program.code.rbegin(), program.code.rend(), lane_local); + // A later lane observer matters only if the new value can reach it. + // Conservatively record every potential touch up to the final observer, + // including inputs to ordinary instructions that could propagate the value. + std::array last_helper_touch{}; + if (pixel && straight && last_lane_observer != program.code.rend()) { + for (const auto &op : program.code) { + if (op.pc > last_lane_observer->pc) break; + auto touch = [&](unsigned r) { + if (r >= 256 && r < 512) last_helper_touch[r-256] = op.pc; + }; + if (op.enc == Encoding::Sopp || op.enc == Encoding::Smem || + op.enc == Encoding::Sop1 || op.enc == Encoding::Sop2) continue; + if (op.enc == Encoding::Interp) { + touch(256 + (op.a & 255)); + touch(256 + ((op.a >> 18) & 255)); + } else if (op.name == "v_readfirstlane_b32" || op.name == "v_cvt_i32_f32" || + op.name == "v_mov_b32" || op.name == "v_and_b32") { + for (auto r : op.src) touch(r); + touch(op.dst); + } else if (op.enc == Encoding::Image && !(op.a & 6) && + ((op.op == 32 && ((op.a >> 3) & 7) == 1) || + (op.op == 0 && ((op.a >> 3) & 7) == 5))) { + // Plain 2D sample or 2D-array load; no NSA address list. + unsigned count = op.op == 32 ? 2 : 3; + for (unsigned j = 0; j < count; ++j) touch(256 + (op.b & 255) + j); + for (unsigned j = 0; j < 4; ++j) touch(256 + op.dst + j); + } else { + last_helper_touch.fill(op.pc); + } + } + } + alu.helper_result_unobserved = [&](unsigned r) { + return straight && r >= 256 && r < 512 && alu.graphics_pc >= last_helper_touch[r-256]; + }; bool whole_quad = false; bool exact_after_wqm = false; bool exact_sample_region = false; @@ -1744,8 +1778,7 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = i.name.find("_b64") != Name::npos || i.enc == Encoding::Vopc ? 2 : 1; if (i.dst < mask_reg + 2 && mask_reg < i.dst + width) discard_lane.clear(); } - alu.helper_result_unobserved = straight && - (last_lane_observer == program.code.rend() || pc >= last_lane_observer->pc); + if (auto it = regions.expired.find(pc); it != regions.expired.end()) for (auto reg : it->second) alu.write(reg, {}); if (regions.vertex_anchors.count(pc) || regions.captures.count(pc)) { @@ -1951,7 +1984,7 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = i.enc != Encoding::Sop1 && i.enc != Encoding::Sop2 && i.enc != Encoding::Sopk && i.enc != Encoding::Export && !(i.name == "v_mov_b32" && i.dst >= 256 && !i.dpp_control && !i.subword) && - !(i.enc==Encoding::Image && i.op>=0x20 && i.op<=0x3f)) + !(i.enc==Encoding::Image && (i.op==0 || (i.op>=0x20 && i.op<=0x3f)))) refuse(pc,"unsupported exact-mask operation after whole-quad entry"); if ((i.name == "s_wqm_b32" || i.name == "s_wqm_b64") && i.dst == 126 && i.src[0] == 126) { if (!stage.wave32 && i.name == "s_wqm_b32") @@ -2174,7 +2207,8 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = break; // A scheduled FP32 FMA accesses exactly three scalar-width // sources and one destination. It cannot change EXEC. - if (j.name == "v_fma_f32" && j.dst != i.dst) + if ((j.name == "v_fma_f32" || j.name == "v_fma_mixlo_f16" || + j.name == "v_fma_mixhi_f16" || j.name == "v_mul_f32" || j.name == "s_waitcnt") && j.dst != i.dst) continue; if (j.dst == i.dst && j.name == (low ? "v_fma_mixhi_f16" : "v_fma_mixlo_f16") && std::find(i.src.begin(), i.src.end(), i.dst) == i.src.end()) @@ -2202,7 +2236,9 @@ NativeComputeShader lower_graphics(GuestShaderStage &stage, unsigned first_srv = } alu.arithmetic(i, state); if (heap_range) heap_ranges[alu.read(i.dst)]=*heap_range; - if (uniform_vertex || (pixel && i.name == "v_med3_f32" && alu.graphics_predicate.empty() && + const bool uniform_conversion = pixel && straight && alu.graphics_predicate.empty() && + i.name == "v_cvt_i32_f32" && !i.dpp_control && !i.subword && i.src[0] <= 105; + if (uniform_vertex || uniform_conversion || (pixel && i.name == "v_med3_f32" && alu.graphics_predicate.empty() && std::all_of(i.src.begin(), i.src.end(), scalar_operand))) { auto value = alu.read(i.dst); if (alu.ir.dependencies[value].none()) uniform_vector_values.insert(value); diff --git a/src/startup_stubs/d3d12_compute_dispatch.inl b/src/startup_stubs/d3d12_compute_dispatch.inl index 1b8c706d13..53e5c3bcdb 100644 --- a/src/startup_stubs/d3d12_compute_dispatch.inl +++ b/src/startup_stubs/d3d12_compute_dispatch.inl @@ -258,13 +258,16 @@ bool translate_compute_natively(ComputeTranslation& tr) for (const auto& view : program.bindings) { const bool packed_x = view.kind == x2w::ComputeViewKind::packed_d16_x_uav; const bool half_typed = packed_x || view.kind == x2w::ComputeViewKind::half_buffer_uav; - const bool output = view.kind == x2w::ComputeViewKind::raw_buffer_uav || half_typed; + const bool texture_input = view.kind == x2w::ComputeViewKind::texture_srv && view.image_dimension == 1; + const bool volume = view.kind == x2w::ComputeViewKind::texture_uav && view.image_dimension == 2; + const bool output = view.kind == x2w::ComputeViewKind::raw_buffer_uav || half_typed || volume; auto& slots = output ? tr.uavs : tr.srvs; - if ((!output && view.kind != x2w::ComputeViewKind::raw_vector_srv && !x2w::accepts_cbv_window(view.kind)) || view.array_count != 1 || + if ((!output && !texture_input && view.kind != x2w::ComputeViewKind::raw_vector_srv && !x2w::accepts_cbv_window(view.kind)) || view.array_count != 1 || view.root_parameter >= 16 || view.shader_register != slots.size()) throw std::runtime_error("view kind " + std::to_string(static_cast(view.kind)) + " is not bound for a natively translated program"); - slots.push_back({static_cast(view.root_parameter), static_cast(view.byte_offset)}); + slots.push_back({static_cast(view.root_parameter), static_cast(view.byte_offset), volume || texture_input}); + if (volume) slots.back().uav_dimension = D3D12_UAV_DIMENSION_TEXTURE3D; slots.back().half_typed_bytes = half_typed ? view.required_bytes : 0; slots.back().packed_d16_x = packed_x; if (x2w::accepts_cbv_window(view.kind)) diff --git a/tests/native_graphics_interface_library_check.py b/tests/native_graphics_interface_library_check.py index 206449002a..d5475858ca 100644 --- a/tests/native_graphics_interface_library_check.py +++ b/tests/native_graphics_interface_library_check.py @@ -80,6 +80,22 @@ def interpolate(attr, channel, op, dst, src): publish(folder, code, words(*vp), root, slot57_inputs=(2, count)) batch(folder, base / f"linked-{attr}-{count}", expected=error) + # The library PS consumes x only. A fixed variant also reads y, + # which is valid only when the actual VS exports that channel. + narrow = [(6, row, 1, row, 0, 0, row, 2) for row in range(2)] + (fixed / "smooth.cdxil").write_bytes(patched_pixel( + len(template), template, input_address=2, attributes=narrow)) + code = words(*template, interpolate(0, 1, 0, 5, 0), interpolate(0, 1, 1, 5, 1), + 0xf800180f, 0x05040504, end) + for mask in (15, 1): + producer = vp.copy() + producer[3] = (producer[3] & ~15) | mask + (fixed / "varying.cdxil").write_bytes(vertex(program=producer)) + run("--index-graphics-interfaces", fixed, cache) + folder = base / f"raw-channels-{mask}" + publish(folder, code, words(*producer), root, slot57_inputs=(2, 1)) + batch(folder, base / f"raw-linked-{mask}", expected=None if mask == 15 else "graphics stages disagree") + # An exact VS writes a four-component image through the second # guest slot. The serialized root covers just the first slot. # The FSSM offset, descriptor width, class and SGPR establish the diff --git a/tests/native_indexed_pixel_texture_check.py b/tests/native_indexed_pixel_texture_check.py index 51504e971d..68382ebd74 100644 --- a/tests/native_indexed_pixel_texture_check.py +++ b/tests/native_indexed_pixel_texture_check.py @@ -22,8 +22,13 @@ def texture_program(original, mask, opposite, base_offset=32): # Explicit words of the fixture's point sampler (2 bits set at bit 54). sampler = [0xbeac0380, 0xbead03ff, 0x00c00000, 0xbeae0380, 0xbeaf0380] - # Define untouched halves of the fixture's packed export VGPRs. - initial = [0x7e000280 | (dst << 17) for dst in (47, 80, 81)] + # Interleave a different destination between the two writes defining v80. + # Only v47 and v81 need initial upper halves for their partial exports. + initial = [0x7e000280 | (dst << 17) for dst in (47, 81)] + lo = source.index(0xcc214000 | 80) + high, other_low = source[lo+2:lo+4], source[lo+4:lo+6] + assert high[0] == 0xcc224000 | 80 and other_low[0] == 0xcc214000 | 81 + source[lo+2:lo+6] = other_low + high def and_mask(dst, src): return 0x87000000 | (dst << 16) | ((128 + mask) << 8) | src