diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index e6db3ab3fb..c0aaddfa8f 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -10990,6 +10990,8 @@ class PipelineState final Stage& gs_stage = stages[1]; Stage& ps_stage = stages[2]; const bool bary_fallback = ps_stage.entry && ps_stage.entry->bary_fallback_location.has_value(); + std::map original_raw_vs_outputs; + decltype(original_raw_vs_outputs)* raw_vs_outputs = nullptr; if (bary_fallback) { const int bary_loc = int(*ps_stage.entry->bary_fallback_location); if (mesh) { @@ -11021,8 +11023,10 @@ class PipelineState final if (vs_stage.entry && ps_stage.entry && !gs_stage.entry && (!ps_stage.entry->f16_halves.empty() || !vs_stage.entry->f16_halves.empty())) { std::string link_error; + if (bary_fallback && !ps_stage.entry->bary_fallback_per_vertex.empty()) + raw_vs_outputs = &original_raw_vs_outputs; if (!link_f16_halves(vs_stage.words, *vs_stage.entry, ps_stage.words, *ps_stage.entry, - &link_error, bary_fallback)) { + &link_error, bary_fallback, raw_vs_outputs)) { std::fprintf(stderr, "[rt] d3d12: %s\n", link_error.c_str()); return E_FAIL; } @@ -11124,7 +11128,7 @@ class PipelineState final if (!leonida::build_bary_fallback_gs(vs_stage.words, ps_stage.words, *ps_stage.entry->bary_fallback_location, ps_stage.entry->bary_fallback_per_vertex, words, &bary_info, - &bary_error)) { + &bary_error, raw_vs_outputs)) { std::fprintf(stderr, "[rt] d3d12: %s\n", bary_error.c_str()); return E_FAIL; } @@ -12172,10 +12176,11 @@ class PipelineState final bool link_f16_halves(std::vector& vs_words, const leonida::ShaderEntry& vs_entry, std::vector& ps_words, const leonida::ShaderEntry& ps_entry, - std::string* error, bool bary_fallback = false) const { + std::string* error, bool bary_fallback, + std::map* original_raw_vs_outputs) const { const std::uint32_t top_loc = bary_fallback ? device_.bary_f16_half_top_location : device_.f16_half_top_location; - return leonida::link_f16_halves(vs_words, vs_entry, ps_words, ps_entry, error, top_loc); + return leonida::link_f16_halves(vs_words, vs_entry, ps_words, ps_entry, error, top_loc, original_raw_vs_outputs); } public: @@ -12206,10 +12211,14 @@ class PipelineState final if (source.slot == 0) vs = i; if (source.slot == 2) ps = i; } + std::map original_raw_vs_outputs; + decltype(original_raw_vs_outputs)* raw_vs_outputs = nullptr; if (f16_linked_) { - if (vs == SIZE_MAX || ps == SIZE_MAX || - !link_f16_halves(words[vs], *sources_[vs].entry, words[ps], *sources_[ps].entry, &why, - bary_fallback_)) + if (vs == SIZE_MAX || ps == SIZE_MAX) return false; + if (bary_fallback_ && !sources_[ps].entry->bary_fallback_per_vertex.empty()) + raw_vs_outputs = &original_raw_vs_outputs; + if (!link_f16_halves(words[vs], *sources_[vs].entry, words[ps], *sources_[ps].entry, &why, + bary_fallback_, raw_vs_outputs)) return false; } for (std::size_t i = 0; i < sources_.size(); ++i) { @@ -12227,7 +12236,7 @@ class PipelineState final !leonida::build_bary_fallback_gs(words[vs], words[ps], *sources_[ps].entry->bary_fallback_location, sources_[ps].entry->bary_fallback_per_vertex, words[i], nullptr, - &why)) + &why, raw_vs_outputs)) return false; } else if (sources_[i].kind == StageSource::depth_only) { words[i] = leonida::depth24_depth_only_fragment(depth24_bias_); diff --git a/runtime/src/spirv_bary_fallback.h b/runtime/src/spirv_bary_fallback.h index 6cefed76f4..4d30053fe8 100644 --- a/runtime/src/spirv_bary_fallback.h +++ b/runtime/src/spirv_bary_fallback.h @@ -50,7 +50,8 @@ inline bool build_bary_fallback_gs(std::span vs_words, const std::map& per_vertex, std::vector& gs_words, BaryFallbackGsInfo* out_info = nullptr, - std::string* error = nullptr) { + std::string* error = nullptr, + const std::map* original_raw_vs_outputs = nullptr) { const auto fail = [&](const std::string& msg) { if (error) *error = msg; return false; @@ -187,11 +188,14 @@ inline bool build_bary_fallback_gs(std::span vs_words, if (clip_count && cull_count) return fail("bary_fallback: cannot forward mixed ClipDistance and CullDistance"); - std::map vs_out_loc_pointee; // loc -> pointee_ty + struct VsOutput { + std::uint32_t var_id, pointee_ty; + }; + std::map vs_outputs; for (const auto& [target, loc] : vs_loc_decs) { const auto it = vs_out_var_pointee.find(target); if (it == vs_out_var_pointee.end()) continue; - if (!vs_out_loc_pointee.emplace(loc, it->second).second) + if (!vs_outputs.emplace(loc, VsOutput{target, it->second}).second) return fail("bary_fallback: duplicate VS Output Location " + std::to_string(loc)); } @@ -252,6 +256,12 @@ inline bool build_bary_fallback_gs(std::span vs_words, } // Computed I/J pairs: the PS's Flat vec4[3] at `base` takes the three vertices' values of VS output `attr`. + // FP16 linking preserves the IDs of raw exports, but a decoded half can occupy a missing attribute's location. + const auto is_raw_vs_output = [&](std::uint32_t loc, std::uint32_t id) { + if (!original_raw_vs_outputs) return true; + const auto raw = original_raw_vs_outputs->find(loc); + return raw != original_raw_vs_outputs->end() && raw->second == id; + }; struct PerVertex { std::uint32_t attr = 0; std::uint32_t base = 0; @@ -284,8 +294,9 @@ inline bool build_bary_fallback_gs(std::span vs_words, } } PerVertex pv{attr, base, false}; - if (const auto vs_it = vs_out_loc_pointee.find(attr); vs_it != vs_out_loc_pointee.end()) { - if (!vs_v4float_tys.count(vs_it->second)) + if (const auto vs_it = vs_outputs.find(attr); + vs_it != vs_outputs.end() && is_raw_vs_output(attr, vs_it->second.var_id)) { + if (!vs_v4float_tys.count(vs_it->second.pointee_ty)) return fail("bary_fallback: VS Output at Location " + std::to_string(attr) + " is not vec4"); pv.forwarded = true; } @@ -305,9 +316,9 @@ inline bool build_bary_fallback_gs(std::span vs_words, if (!ps_v4float_tys.count(pt_ty)) return fail("bary_fallback: PS pass-through Input at Location " + std::to_string(loc) + " is not vec4"); pass_locs.push_back(loc); - const auto vs_it = vs_out_loc_pointee.find(loc); - if (vs_it != vs_out_loc_pointee.end()) { - if (!vs_v4float_tys.count(vs_it->second)) + const auto vs_it = vs_outputs.find(loc); + if (vs_it != vs_outputs.end()) { + if (!vs_v4float_tys.count(vs_it->second.pointee_ty)) return fail("bary_fallback: VS Output at Location " + std::to_string(loc) + " is not vec4"); forwarded_locs.push_back(loc); } else { diff --git a/runtime/src/spirv_f16_halves.h b/runtime/src/spirv_f16_halves.h index ea8d4f458e..5fbdfc55c3 100644 --- a/runtime/src/spirv_f16_halves.h +++ b/runtime/src/spirv_f16_halves.h @@ -248,14 +248,16 @@ inline constexpr std::uint32_t f16_half_top_location(std::uint32_t max_vertex_ou // // Returns true on success (including the no-op fast path when no patching is needed). On failure returns false and // writes a concise reason to `*error` if non-null; the caller's word vectors are only replaced on success. +// `original_raw_vs_outputs`, when requested, gets the original Location -> ID map excluding decoded FP16 halves. inline bool link_f16_halves(std::vector& vs_words, const ShaderEntry& vs_entry, std::vector& ps_words, const ShaderEntry& ps_entry, - std::string* error = nullptr, std::uint32_t top_location = 29) { + std::string* error = nullptr, std::uint32_t top_location = 29, + std::map* original_raw_vs_outputs = nullptr) { const auto fail = [&](const std::string& msg) { if (error) *error = msg; return false; }; - if (ps_entry.f16_halves.empty() && vs_entry.f16_halves.empty()) return true; + if (ps_entry.f16_halves.empty() && vs_entry.f16_halves.empty() && !original_raw_vs_outputs) return true; using namespace spirv_detail; Module vs_mod, ps_mod; @@ -313,7 +315,10 @@ inline bool link_f16_halves(std::vector& vs_words, const ShaderEn if (!already_linked) break; } } - if (already_linked) return true; + if (already_linked) { + if (original_raw_vs_outputs) *original_raw_vs_outputs = std::move(vs_raw_outputs); + return true; + } // Choose paired [lo, hi] locations for every param in ps_entry.f16_halves. // Keep PS's existing locations unchanged whenever none collides with VS/PS raw outputs/inputs or exceeds top_location. @@ -654,6 +659,7 @@ inline bool link_f16_halves(std::vector& vs_words, const ShaderEn vs_mod.insts = std::move(out_insts); vs_words = vs_mod.serialize(); if (ps_changed) ps_words = ps_mod.serialize(); + if (original_raw_vs_outputs) *original_raw_vs_outputs = std::move(vs_raw_outputs); return true; } diff --git a/runtime/test/spirv_bary_fallback_test.cpp b/runtime/test/spirv_bary_fallback_test.cpp index 5f9ca9a522..55970a2564 100644 --- a/runtime/test/spirv_bary_fallback_test.cpp +++ b/runtime/test/spirv_bary_fallback_test.cpp @@ -7,6 +7,7 @@ // 6. Clean refusals: mixed ClipDistance + CullDistance, ShaderLayer / ShaderViewportIndex, non-vec3 collision at bary_location. // 7. Computed I/J pairs (RT-BARY-FALLBACK-PER-VERTEX): a PS `Flat vec4[3]` Input named by `per_vertex` gets the // triangle's three values of the VS output it maps, with shared GS Inputs, zero-fill and clean refusals. +// 8. Neither a relocated nor a static FP16 half can supply a missing raw computed-I/J attribute. // Optional `--dump-dir ` writes each synthesized GS binary for external `spirv-val` inspection. // No GPU or game files required. #include "../src/spirv_bary_fallback.h" @@ -640,5 +641,47 @@ int main(int argc, char** argv) { CHECK(refused(build_ps({}, {}, 3, {}, {0}), 3, {{0, 1}})); } + // Relocating the two decoded halves to 13/14 must not make the VS appear to export raw attr14. + { + ShaderEntry vs_e, ps_e; + ps_e.f16_halves = {{0, {28, 29}}}; + auto vs = build_vs({.raw_locs = {0}}); + auto ps = build_ps({}, {}, 3, ps_e.f16_halves, {0, 4}); + PerVertexMap raw_vs_outputs; + std::vector gs; + std::string err; + CHECK(link_f16_halves(vs, vs_e, ps, ps_e, &err, 14, &raw_vs_outputs)); + CHECK(raw_vs_outputs.size() == 1 && raw_vs_outputs.count(0)); + CHECK(build_bary_fallback_gs(vs, ps, 3, PerVertexMap{{14, 0}, {0, 4}}, gs, + nullptr, &err, &raw_vs_outputs)); + GsInspection ins; + CHECK(verify_gs_module(gs, ins)); + // Keep both decoded halves and the genuine per-vertex attr0, but zero-fill the array for attr14. + CHECK((ins.in_locs == std::set{0, 13, 14})); + CHECK((ins.out_pv_locs == std::set{0, 4})); + CHECK(ins.out_pv_stores == 9 && ins.out_pv_null_stores == 3); + maybe_dump(dump_dir, "gs_per_vertex_relocated_half_alias", gs); + maybe_dump(dump_dir, "ps_per_vertex_relocated_half_alias", ps); + } + + // The same rule applies when the FP16 linker leaves unused static VS halves in place on its no-op path. + { + ShaderEntry vs_e; + vs_e.f16_halves = {{8, {12, 13}}}; + auto vs = build_vs({.raw_locs = {8}, .static_halves = vs_e.f16_halves}); + const auto original_vs = vs; + auto ps = build_ps({}, {}, 3, {}, {0}); + PerVertexMap raw_vs_outputs; + std::vector gs; + std::string err; + CHECK(link_f16_halves(vs, vs_e, ps, {}, &err, 14, &raw_vs_outputs)); + CHECK(vs == original_vs && raw_vs_outputs.size() == 1 && raw_vs_outputs.count(8)); + CHECK(build_bary_fallback_gs(vs, ps, 3, PerVertexMap{{13, 0}}, gs, nullptr, &err, &raw_vs_outputs)); + GsInspection ins; + CHECK(verify_gs_module(gs, ins)); + CHECK(ins.in_locs.empty() && ins.out_pv_stores == 0 && ins.out_pv_null_stores == 3); + maybe_dump(dump_dir, "gs_per_vertex_static_half_alias", gs); + } + return 0; }