diff options
| author | Coen <coen@graafstal.com> | 2026-11-23 23:18:39 +0100 |
|---|---|---|
| committer | Coen <coen@graafstal.com> | 2026-11-23 23:18:39 +0100 |
| commit | c48a663a82e1db7fb9d8a1d1573145e5dcf1ee0a (patch) | |
| tree | 2d8a2dc0c060efa70584ad4e5426abc7801d1612 /runtime/src | |
| parent | 5d74c272363f2703265d44e6aaab1a1df8ddfaf3 (diff) | |
d3d12/spirv_f16_halves: fix linker fast-path, Private decorations, and device location budget
Fixes four bugs in --f16-interp-fallback per-pipeline linking:
1. link_f16_halves() no longer skips patching on its fast path when static
VS/PS half locations exceed top_location or when an unused VS static half
collides with a raw PS input location.
2. Demoting an unused VS static half Output variable to Private now strips
all interface-only decorations (NoPerspective, Flat, Patch, Centroid,
Sample, Invariant, Location, Component), avoiding invalid SPIR-V.
3. keep_ps_locs now preserves raw PS input locations (ps_raw_locs) when
picking relocation slots.
4. CreateGraphicsPipelineState now runs link_f16_halves() when only VS
declares f16_halves and derives top_location from the physical device's
min(maxVertexOutputComponents, maxFragmentInputComponents) instead of
hardcoding 29.
Adds regression tests (cases 6-10) and interface-only decoration validation
in runtime/test/spirv_f16_halves_test.cpp.
AI-assisted (Antigravity).
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/spirv_f16_halves.h | 81 |
1 files changed, 62 insertions, 19 deletions
diff --git a/runtime/src/spirv_f16_halves.h b/runtime/src/spirv_f16_halves.h index 5dffb64ed..ea8d4f458 100644 --- a/runtime/src/spirv_f16_halves.h +++ b/runtime/src/spirv_f16_halves.h | |||
| @@ -85,6 +85,22 @@ constexpr std::uint32_t kStoragePrivate = 6; | |||
| 85 | constexpr std::uint32_t kDecorationLocation = 30; | 85 | constexpr std::uint32_t kDecorationLocation = 30; |
| 86 | constexpr std::uint32_t kGlslUnpackHalf2x16 = 62; | 86 | constexpr std::uint32_t kGlslUnpackHalf2x16 = 62; |
| 87 | 87 | ||
| 88 | inline constexpr bool is_interface_only_decoration(std::uint32_t dec) { | ||
| 89 | switch (dec) { | ||
| 90 | case 13: // NoPerspective | ||
| 91 | case 14: // Flat | ||
| 92 | case 15: // Patch | ||
| 93 | case 16: // Centroid | ||
| 94 | case 17: // Sample | ||
| 95 | case 18: // Invariant | ||
| 96 | case kDecorationLocation: | ||
| 97 | case 31: // Component | ||
| 98 | return true; | ||
| 99 | default: | ||
| 100 | return false; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 88 | struct Inst { | 104 | struct Inst { |
| 89 | std::uint16_t opcode = 0; | 105 | std::uint16_t opcode = 0; |
| 90 | std::vector<std::uint32_t> words; | 106 | std::vector<std::uint32_t> words; |
| @@ -217,6 +233,15 @@ inline std::map<std::uint32_t, std::uint32_t> collect_located_vars(const Module& | |||
| 217 | 233 | ||
| 218 | } // namespace spirv_detail | 234 | } // namespace spirv_detail |
| 219 | 235 | ||
| 236 | // Highest usable vec4 Location (capped at 29, MoltenVK's 124-component limit) for a device's stage interface limits. | ||
| 237 | // Reserves 4 components for BuiltIn Position (so 128 -> 29, 124 -> 29, 64 -> 14). | ||
| 238 | inline constexpr std::uint32_t f16_half_top_location(std::uint32_t max_vertex_output_components, | ||
| 239 | std::uint32_t max_fragment_input_components) { | ||
| 240 | const std::uint32_t limit = std::min(max_vertex_output_components, max_fragment_input_components); | ||
| 241 | if (limit < 8) return 0; | ||
| 242 | return std::min<std::uint32_t>(29u, limit / 4u - 2u); | ||
| 243 | } | ||
| 244 | |||
| 220 | // Specializes/patches `vs_words` and (if needed) `ps_words` so that every entry in `ps_entry.f16_halves` is | 245 | // Specializes/patches `vs_words` and (if needed) `ps_words` so that every entry in `ps_entry.f16_halves` is |
| 221 | // exported by the VS at the exact smooth location the PS reads, within `0..top_location` (default 29, MoltenVK's | 246 | // exported by the VS at the exact smooth location the PS reads, within `0..top_location` (default 29, MoltenVK's |
| 222 | // 124-component vertex output limit counting gl_Position). | 247 | // 124-component vertex output limit counting gl_Position). |
| @@ -230,18 +255,7 @@ inline bool link_f16_halves(std::vector<std::uint32_t>& vs_words, const ShaderEn | |||
| 230 | if (error) *error = msg; | 255 | if (error) *error = msg; |
| 231 | return false; | 256 | return false; |
| 232 | }; | 257 | }; |
| 233 | if (ps_entry.f16_halves.empty()) return true; | 258 | if (ps_entry.f16_halves.empty() && vs_entry.f16_halves.empty()) return true; |
| 234 | |||
| 235 | // Fast path: every PS half pair is already exported at the exact same locations by VS. | ||
| 236 | bool already_linked = true; | ||
| 237 | for (const auto& [param, ps_pair] : ps_entry.f16_halves) { | ||
| 238 | const auto it = vs_entry.f16_halves.find(param); | ||
| 239 | if (it == vs_entry.f16_halves.end() || it->second != ps_pair) { | ||
| 240 | already_linked = false; | ||
| 241 | break; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | if (already_linked) return true; | ||
| 245 | 259 | ||
| 246 | using namespace spirv_detail; | 260 | using namespace spirv_detail; |
| 247 | Module vs_mod, ps_mod; | 261 | Module vs_mod, ps_mod; |
| @@ -276,13 +290,39 @@ inline bool link_f16_halves(std::vector<std::uint32_t>& vs_words, const ShaderEn | |||
| 276 | if (!ps_static_half_locs.count(loc)) ps_raw_locs.insert(loc); | 290 | if (!ps_static_half_locs.count(loc)) ps_raw_locs.insert(loc); |
| 277 | } | 291 | } |
| 278 | 292 | ||
| 293 | // Fast path: every PS half pair is already exported at the exact same locations by VS, all static halves fit | ||
| 294 | // within 0..top_location, and no unused VS static half collides with a raw PS input. | ||
| 295 | bool already_linked = true; | ||
| 296 | for (const auto& [param, ps_pair] : ps_entry.f16_halves) { | ||
| 297 | const auto it = vs_entry.f16_halves.find(param); | ||
| 298 | if (it == vs_entry.f16_halves.end() || it->second != ps_pair || ps_pair[0] > top_location || | ||
| 299 | ps_pair[1] > top_location) { | ||
| 300 | already_linked = false; | ||
| 301 | break; | ||
| 302 | } | ||
| 303 | } | ||
| 304 | if (already_linked) { | ||
| 305 | for (const auto& [param, vs_pair] : vs_entry.f16_halves) { | ||
| 306 | const bool used_by_ps = ps_entry.f16_halves.count(param) != 0; | ||
| 307 | for (std::uint32_t loc : vs_pair) { | ||
| 308 | if (loc > top_location || (!used_by_ps && ps_raw_locs.count(loc))) { | ||
| 309 | already_linked = false; | ||
| 310 | break; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | if (!already_linked) break; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | if (already_linked) return true; | ||
| 317 | |||
| 279 | // Choose paired [lo, hi] locations for every param in ps_entry.f16_halves. | 318 | // Choose paired [lo, hi] locations for every param in ps_entry.f16_halves. |
| 280 | // Keep PS's existing locations unchanged whenever none collides with VS raw outputs or exceeds top_location. | 319 | // Keep PS's existing locations unchanged whenever none collides with VS/PS raw outputs/inputs or exceeds top_location. |
| 281 | bool keep_ps_locs = true; | 320 | bool keep_ps_locs = true; |
| 282 | std::set<std::uint32_t> seen_ps_halves; | 321 | std::set<std::uint32_t> seen_ps_halves; |
| 283 | for (const auto& [param, pair] : ps_entry.f16_halves) { | 322 | for (const auto& [param, pair] : ps_entry.f16_halves) { |
| 284 | for (std::uint32_t loc : pair) { | 323 | for (std::uint32_t loc : pair) { |
| 285 | if (loc > top_location || vs_raw_locs.count(loc) || !seen_ps_halves.insert(loc).second) { | 324 | if (loc > top_location || vs_raw_locs.count(loc) || ps_raw_locs.count(loc) || |
| 325 | !seen_ps_halves.insert(loc).second) { | ||
| 286 | keep_ps_locs = false; | 326 | keep_ps_locs = false; |
| 287 | break; | 327 | break; |
| 288 | } | 328 | } |
| @@ -542,12 +582,15 @@ inline bool link_f16_halves(std::vector<std::uint32_t>& vs_words, const ShaderEn | |||
| 542 | continue; | 582 | continue; |
| 543 | } | 583 | } |
| 544 | 584 | ||
| 545 | // Update or remove OpDecorate Location on static VS f16_halves outputs. | 585 | // Update Location or strip interface-only decorations on static VS f16_halves outputs. |
| 546 | if (inst.opcode == kOpDecorate && inst.words.size() >= 4 && inst.words[2] == kDecorationLocation) { | 586 | if (inst.opcode == kOpDecorate && inst.words.size() >= 3) { |
| 547 | const std::uint32_t target = inst.words[1]; | 587 | const std::uint32_t target = inst.words[1]; |
| 548 | if (demote_vars.count(target)) continue; // drop Location decoration from demoted Private variable | 588 | const std::uint32_t dec = inst.words[2]; |
| 549 | const auto it = vs_var_new_loc.find(target); | 589 | if (demote_vars.count(target) && is_interface_only_decoration(dec)) continue; |
| 550 | if (it != vs_var_new_loc.end()) inst.words[3] = it->second; | 590 | if (dec == kDecorationLocation && inst.words.size() >= 4) { |
| 591 | const auto it = vs_var_new_loc.find(target); | ||
| 592 | if (it != vs_var_new_loc.end()) inst.words[3] = it->second; | ||
| 593 | } | ||
| 551 | out_insts.push_back(std::move(inst)); | 594 | out_insts.push_back(std::move(inst)); |
| 552 | continue; | 595 | continue; |
| 553 | } | 596 | } |