upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorCoen <coen@graafstal.com>2026-11-23 23:18:39 +0100
committerCoen <coen@graafstal.com>2026-11-23 23:18:39 +0100
commitc48a663a82e1db7fb9d8a1d1573145e5dcf1ee0a (patch)
tree2d8a2dc0c060efa70584ad4e5426abc7801d1612 /runtime
parent5d74c272363f2703265d44e6aaab1a1df8ddfaf3 (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')
-rw-r--r--runtime/shims/d3d12_vulkan_device.cpp8
-rw-r--r--runtime/src/spirv_f16_halves.h81
-rw-r--r--runtime/test/spirv_f16_halves_test.cpp132
3 files changed, 196 insertions, 25 deletions
diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp
index 3b9958b35..b9719a77e 100644
--- a/runtime/shims/d3d12_vulkan_device.cpp
+++ b/runtime/shims/d3d12_vulkan_device.cpp
@@ -629,6 +629,7 @@ struct DeviceState {
629 629
630 // Cached physical-device no-attachment sample counts for D3D12_OPTIONS19. 630 // Cached physical-device no-attachment sample counts for D3D12_OPTIONS19.
631 UINT no_output_sample_counts = 0; 631 UINT no_output_sample_counts = 0;
632 std::uint32_t f16_half_top_location = 29;
632 float max_sampler_lod_bias = 0; 633 float max_sampler_lod_bias = 0;
633 D3D12_CONSERVATIVE_RASTERIZATION_TIER conservative_raster_tier = D3D12_CONSERVATIVE_RASTERIZATION_TIER_NOT_SUPPORTED; 634 D3D12_CONSERVATIVE_RASTERIZATION_TIER conservative_raster_tier = D3D12_CONSERVATIVE_RASTERIZATION_TIER_NOT_SUPPORTED;
634 bool conservative_post_depth_coverage = false; 635 bool conservative_post_depth_coverage = false;
@@ -3378,10 +3379,11 @@ public:
3378 if (vs_stage.entry && ps_stage.entry && !ps_stage.entry->f16_halves.empty() && gs_stage.entry) { 3379 if (vs_stage.entry && ps_stage.entry && !ps_stage.entry->f16_halves.empty() && gs_stage.entry) {
3379 log_once("--f16-interp-fallback halves are linked VS->PS only; this PSO has a geometry shader", 0); 3380 log_once("--f16-interp-fallback halves are linked VS->PS only; this PSO has a geometry shader", 0);
3380 } 3381 }
3381 if (vs_stage.entry && ps_stage.entry && !gs_stage.entry && !ps_stage.entry->f16_halves.empty()) { 3382 if (vs_stage.entry && ps_stage.entry && !gs_stage.entry &&
3383 (!ps_stage.entry->f16_halves.empty() || !vs_stage.entry->f16_halves.empty())) {
3382 std::string link_error; 3384 std::string link_error;
3383 if (!leonida::link_f16_halves(vs_stage.words, *vs_stage.entry, ps_stage.words, *ps_stage.entry, 3385 if (!leonida::link_f16_halves(vs_stage.words, *vs_stage.entry, ps_stage.words, *ps_stage.entry,
3384 &link_error)) { 3386 &link_error, device_.f16_half_top_location)) {
3385 std::fprintf(stderr, "[rt] d3d12: %s\n", link_error.c_str()); 3387 std::fprintf(stderr, "[rt] d3d12: %s\n", link_error.c_str());
3386 return E_FAIL; 3388 return E_FAIL;
3387 } 3389 }
@@ -4867,6 +4869,8 @@ public:
4867 VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT | VK_SAMPLE_COUNT_8_BIT | VK_SAMPLE_COUNT_16_BIT; 4869 VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT | VK_SAMPLE_COUNT_8_BIT | VK_SAMPLE_COUNT_16_BIT;
4868 state_.no_output_sample_counts = 4870 state_.no_output_sample_counts =
4869 static_cast<UINT>(props.limits.framebufferNoAttachmentsSampleCounts & kD3D12SampleCounts); 4871 static_cast<UINT>(props.limits.framebufferNoAttachmentsSampleCounts & kD3D12SampleCounts);
4872 state_.f16_half_top_location = leonida::f16_half_top_location(
4873 props.limits.maxVertexOutputComponents, props.limits.maxFragmentInputComponents);
4870 } 4874 }
4871 if (const auto get_props2 = reinterpret_cast<PFN_vkGetPhysicalDeviceProperties2>( 4875 if (const auto get_props2 = reinterpret_cast<PFN_vkGetPhysicalDeviceProperties2>(
4872 info.get_instance_proc(info.instance, "vkGetPhysicalDeviceProperties2"))) { 4876 info.get_instance_proc(info.instance, "vkGetPhysicalDeviceProperties2"))) {
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;
85constexpr std::uint32_t kDecorationLocation = 30; 85constexpr std::uint32_t kDecorationLocation = 30;
86constexpr std::uint32_t kGlslUnpackHalf2x16 = 62; 86constexpr std::uint32_t kGlslUnpackHalf2x16 = 62;
87 87
88inline 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
88struct Inst { 104struct 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).
238inline 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 }
diff --git a/runtime/test/spirv_f16_halves_test.cpp b/runtime/test/spirv_f16_halves_test.cpp
index 29bb37113..61604f14a 100644
--- a/runtime/test/spirv_f16_halves_test.cpp
+++ b/runtime/test/spirv_f16_halves_test.cpp
@@ -178,12 +178,19 @@ bool verify_spirv_structure(const std::vector<std::uint32_t>& words, std::uint32
178 } 178 }
179 } 179 }
180 for (const Inst& inst : mod.insts) { 180 for (const Inst& inst : mod.insts) {
181 if (inst.opcode == kOpDecorate && inst.words.size() >= 4 && inst.words[2] == kDecorationLocation) { 181 if (inst.opcode == kOpDecorate && inst.words.size() >= 3) {
182 const auto it = var_storage.find(inst.words[1]); 182 const auto it = var_storage.find(inst.words[1]);
183 if (it != var_storage.end() && it->second == storage_class) { 183 const std::uint32_t dec = inst.words[2];
184 if (!out_loc_to_var.emplace(inst.words[3], inst.words[1]).second) return false; // duplicate Location 184 if (dec == kDecorationLocation && inst.words.size() >= 4) {
185 if (it != var_storage.end() && it->second == storage_class) {
186 if (!out_loc_to_var.emplace(inst.words[3], inst.words[1]).second) return false; // duplicate Location
187 }
188 }
189 if (it != var_storage.end() && it->second == kStoragePrivate &&
190 (dec == 13 || dec == 14 || dec == 15 || dec == 16 || dec == 17 || dec == 18 ||
191 dec == kDecorationLocation || dec == 31)) {
192 return false; // Private cannot carry interface-only decorations (Flat/NoPerspective/Location/Component/...)
185 } 193 }
186 if (it != var_storage.end() && it->second == kStoragePrivate) return false; // Private cannot have Location
187 } 194 }
188 } 195 }
189 return true; 196 return true;
@@ -295,6 +302,123 @@ int main() {
295 CHECK(vs_w == vs_orig && ps_w == ps_orig); 302 CHECK(vs_w == vs_orig && ps_w == ps_orig);
296 } 303 }
297 304
305 // 6. Fast-path top_location enforcement: matching VS/PS static halves above top_location must be relocated (or
306 // rejected when they do not fit), not silently passed through unchanged.
307 {
308 ShaderEntry vs_e, ps_e;
309 vs_e.f16_halves = {{0, {28, 29}}, {1, {26, 27}}};
310 ps_e.f16_halves = {{0, {28, 29}}};
311 auto vs_w = build_vs(0x00010500u, 2, vs_e.f16_halves);
312 auto ps_w = build_ps(0x00010500u, {0, 1}, ps_e.f16_halves);
313 std::string err;
314 // 64-component Vulkan minimum limit -> top_location = 14; both VS and PS must relocate param 0 into <= 14
315 // and demote unused param 1 (at 26, 27) to Private.
316 CHECK(link_f16_halves(vs_w, vs_e, ps_w, ps_e, &err, 14));
317 std::map<std::uint32_t, std::uint32_t> vs_locs, ps_locs;
318 int vs_unpacks = 0, vs_privs = 0, ps_unpacks = 0, ps_privs = 0;
319 std::set<std::uint32_t> vs_ep, ps_ep;
320 CHECK(verify_spirv_structure(vs_w, kStorageOutput, vs_locs, vs_unpacks, vs_privs, vs_ep));
321 CHECK(verify_spirv_structure(ps_w, kStorageInput, ps_locs, ps_unpacks, ps_privs, ps_ep));
322 CHECK(vs_privs == 2);
323 CHECK(vs_locs.rbegin()->first <= 14);
324 CHECK(ps_locs.rbegin()->first <= 14);
325 CHECK(vs_locs.count(13) == 1 && vs_locs.count(14) == 1);
326 CHECK(ps_locs.count(13) == 1 && ps_locs.count(14) == 1);
327 }
328
329 // 7. Empty ps_entry.f16_halves with out-of-budget VS static halves: unused VS halves > top_location must be
330 // demoted to Private instead of returning early and failing pipeline creation on a <124-component device.
331 {
332 ShaderEntry vs_e, ps_e;
333 vs_e.f16_halves = {{0, {28, 29}}};
334 auto vs_w = build_vs(0x00010500u, 2, vs_e.f16_halves);
335 auto ps_w = build_ps(0x00010500u, {0, 1}, {});
336 const auto ps_orig = ps_w;
337 std::string err;
338 CHECK(link_f16_halves(vs_w, vs_e, ps_w, ps_e, &err, 14));
339 CHECK(ps_w == ps_orig);
340 std::map<std::uint32_t, std::uint32_t> vs_locs;
341 int unpacks = 0, privs = 0;
342 std::set<std::uint32_t> ep_iface;
343 CHECK(verify_spirv_structure(vs_w, kStorageOutput, vs_locs, unpacks, privs, ep_iface));
344 CHECK(privs == 2);
345 CHECK(vs_locs.size() == 2 && vs_locs.rbegin()->first == 1);
346 }
347
348 // 8. Unused VS static half colliding with a raw PS input: fast path must not leave the unused VS half at a
349 // Location the PS reads as a raw input.
350 {
351 ShaderEntry vs_e, ps_e;
352 vs_e.f16_halves = {{0, {28, 29}}, {1, {26, 27}}};
353 ps_e.f16_halves = {{0, {28, 29}}};
354 auto vs_w = build_vs(0x00010500u, 2, vs_e.f16_halves);
355 auto ps_w = build_ps(0x00010500u, {0, 1, 26}, ps_e.f16_halves);
356 std::string err;
357 CHECK(link_f16_halves(vs_w, vs_e, ps_w, ps_e, &err, 29));
358 std::map<std::uint32_t, std::uint32_t> vs_locs;
359 int unpacks = 0, privs = 0;
360 std::set<std::uint32_t> ep_iface;
361 CHECK(verify_spirv_structure(vs_w, kStorageOutput, vs_locs, unpacks, privs, ep_iface));
362 CHECK(privs == 2);
363 CHECK(!vs_locs.count(26) && !vs_locs.count(27));
364 CHECK(vs_locs.count(28) == 1 && vs_locs.count(29) == 1);
365 }
366
367 // 9. Demoting a static VS half variable strips non-Location interface-only decorations (e.g. Flat, Component)
368 // so the resulting Private variable remains valid SPIR-V.
369 {
370 ShaderEntry vs_e, ps_e;
371 vs_e.f16_halves = {{0, {28, 29}}, {1, {26, 27}}};
372 ps_e.f16_halves = {{0, {26, 27}}}; // forces param 1 (IDs 14, 15) to be demoted to Private
373 auto vs_w = build_vs(0x00010500u, 2, vs_e.f16_halves);
374 // Inject OpDecorate 14 Flat and OpDecorate 15 Component 0 right after OpDecorate 15 Location 27.
375 Module m;
376 CHECK(m.parse(vs_w));
377 std::vector<Inst> patched;
378 for (const Inst& inst : m.insts) {
379 patched.push_back(inst);
380 if (inst.opcode == kOpDecorate && inst.words.size() >= 4 && inst.words[2] == kDecorationLocation &&
381 inst.words[3] == 27) {
382 patched.push_back(make_inst(kOpDecorate, {inst.words[1] - 1, 14})); // Flat
383 patched.push_back(make_inst(kOpDecorate, {inst.words[1], 31, 0})); // Component 0
384 }
385 }
386 m.insts = std::move(patched);
387 vs_w = m.serialize();
388
389 auto ps_w = build_ps(0x00010500u, {0, 1}, ps_e.f16_halves);
390 std::string err;
391 CHECK(link_f16_halves(vs_w, vs_e, ps_w, ps_e, &err));
392 std::map<std::uint32_t, std::uint32_t> vs_locs;
393 int unpacks = 0, privs = 0;
394 std::set<std::uint32_t> ep_iface;
395 CHECK(verify_spirv_structure(vs_w, kStorageOutput, vs_locs, unpacks, privs, ep_iface));
396 CHECK(privs == 2);
397 }
398
399 // 10. Device-limit helper f16_half_top_location() and out-of-budget fast-path rejection.
400 {
401 CHECK(f16_half_top_location(128, 128) == 29);
402 CHECK(f16_half_top_location(124, 128) == 29);
403 CHECK(f16_half_top_location(120, 120) == 28);
404 CHECK(f16_half_top_location(64, 64) == 14);
405 CHECK(f16_half_top_location(64, 128) == 14);
406 CHECK(f16_half_top_location(4, 64) == 0);
407
408 // Matching VS/PS static halves at [28, 29], but 14 raw VS outputs (0..13) leave only location 14 free (< 2):
409 // must fail cleanly instead of taking the fast path with out-of-range locations.
410 ShaderEntry vs_e, ps_e;
411 vs_e.f16_halves = {{0, {28, 29}}};
412 ps_e.f16_halves = {{0, {28, 29}}};
413 auto vs_w = build_vs(0x00010500u, 14, vs_e.f16_halves);
414 auto ps_w = build_ps(0x00010500u, {0}, ps_e.f16_halves);
415 const auto vs_orig = vs_w, ps_orig = ps_w;
416 std::string err;
417 CHECK(!link_f16_halves(vs_w, vs_e, ps_w, ps_e, &err, 14));
418 CHECK(!err.empty());
419 CHECK(vs_w == vs_orig && ps_w == ps_orig);
420 }
421
298 std::printf("spirv_f16_halves_test: all checks passed\n"); 422 std::printf("spirv_f16_halves_test: all checks passed\n");
299 return 0; 423 return 0;
300} 424}