Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::uint32_t, std::uint32_t> 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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -12172,10 +12176,11 @@ class PipelineState final

bool link_f16_halves(std::vector<std::uint32_t>& vs_words, const leonida::ShaderEntry& vs_entry,
std::vector<std::uint32_t>& ps_words, const leonida::ShaderEntry& ps_entry,
std::string* error, bool bary_fallback = false) const {
std::string* error, bool bary_fallback,
std::map<std::uint32_t, std::uint32_t>* 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:
Expand Down Expand Up @@ -12206,10 +12211,14 @@ class PipelineState final
if (source.slot == 0) vs = i;
if (source.slot == 2) ps = i;
}
std::map<std::uint32_t, std::uint32_t> 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) {
Expand All @@ -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_);
Expand Down
27 changes: 19 additions & 8 deletions runtime/src/spirv_bary_fallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ inline bool build_bary_fallback_gs(std::span<const std::uint32_t> vs_words,
const std::map<std::uint32_t, std::uint32_t>& per_vertex,
std::vector<std::uint32_t>& gs_words,
BaryFallbackGsInfo* out_info = nullptr,
std::string* error = nullptr) {
std::string* error = nullptr,
const std::map<std::uint32_t, std::uint32_t>* original_raw_vs_outputs = nullptr) {
const auto fail = [&](const std::string& msg) {
if (error) *error = msg;
return false;
Expand Down Expand Up @@ -187,11 +188,14 @@ inline bool build_bary_fallback_gs(std::span<const std::uint32_t> vs_words,
if (clip_count && cull_count)
return fail("bary_fallback: cannot forward mixed ClipDistance and CullDistance");

std::map<std::uint32_t, std::uint32_t> vs_out_loc_pointee; // loc -> pointee_ty
struct VsOutput {
std::uint32_t var_id, pointee_ty;
};
std::map<std::uint32_t, VsOutput> 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));
}

Expand Down Expand Up @@ -252,6 +256,12 @@ inline bool build_bary_fallback_gs(std::span<const std::uint32_t> 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;
Expand Down Expand Up @@ -284,8 +294,9 @@ inline bool build_bary_fallback_gs(std::span<const std::uint32_t> 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<f32>");
pv.forwarded = true;
}
Expand All @@ -305,9 +316,9 @@ inline bool build_bary_fallback_gs(std::span<const std::uint32_t> 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<f32>");
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<f32>");
forwarded_locs.push_back(loc);
} else {
Expand Down
12 changes: 9 additions & 3 deletions runtime/src/spirv_f16_halves.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::uint32_t>& vs_words, const ShaderEntry& vs_entry,
std::vector<std::uint32_t>& 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<std::uint32_t, std::uint32_t>* 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;
Expand Down Expand Up @@ -313,7 +315,10 @@ inline bool link_f16_halves(std::vector<std::uint32_t>& 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.
Expand Down Expand Up @@ -654,6 +659,7 @@ inline bool link_f16_halves(std::vector<std::uint32_t>& 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;
}

Expand Down
43 changes: 43 additions & 0 deletions runtime/test/spirv_bary_fallback_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <dir>` writes each synthesized GS binary for external `spirv-val` inspection.
// No GPU or game files required.
#include "../src/spirv_bary_fallback.h"
Expand Down Expand Up @@ -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<std::uint32_t> 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<std::uint32_t>{0, 13, 14}));
CHECK((ins.out_pv_locs == std::set<std::uint32_t>{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<std::uint32_t> 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;
}