upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-22 05:45:58 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-22 05:45:58 -0500
commit4cfde416f85c60cafe5c2d7329f4c0e028006562 (patch)
treeddbeed5b59589d305595cc38bcf0314f81c5520f /runtime/src
parent70178d307cb87146a42414eefa19aa4ff0e0c40a (diff)
parentf85582e23d4ab9e19acbf694358f137d6b8531b1 (diff)
Merge main (f85582e) into runtime/compute-path
On top of the merge candidate 70178d3 (Ryx69/Leonida#6, which merged main at 426023a): the 219 commits main gained since. Three hunks conflicted, in two files; both follow the notes on #291 (ItsNotRexed 08:27 UTC, Vonsek 09:58 UTC on 2026-11-22). runtime/shims/d3d12_vulkan_device.cpp, device setup after the VulkanContext: - the branch's GPU address arena is created first, as before; - main's block that configures predication and the predicated byte copy (#1277) follows it; - one load_host_image_copy call and main's lazy D24 converter (enabled only where D24_UNORM_S8_UINT is not usable) replace the branch's unconditional enable_depth24, which was the same code before main changed it. runtime/src/vulkan_replay.h: - CopyBuffer: main's version. It already adds ResourceBinding::buffer_offset to both offsets, as the branch did, and adds the overflow and overlap checks and the predicated copy. With the arena, source and destination are often the same VkBuffer; the overlap check compares the adjusted ranges. - Predication: main's version. prepare() passes buffer_offset + the command's offset to prepare_predicate, so a default-heap buffer inside the arena is read at its own range; emit() captures the word and tests the capture's scratch slot, not the application's buffer. Everything else merged without conflicts. Nothing in main's 219 commits adds a caller that takes a buffer's Vulkan device address for its GPU VA. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/command_list.h3
-rw-r--r--runtime/src/depth24_convert.comp11
-rw-r--r--runtime/src/depth24_convert.inc127
-rw-r--r--runtime/src/gfx10_descriptors.h5
-rw-r--r--runtime/src/predicate_reduce.comp6
-rw-r--r--runtime/src/predicate_reduce_spv.h36
-rw-r--r--runtime/src/predicated_copy.comp11
-rw-r--r--runtime/src/predicated_copy_spv.h54
-rw-r--r--runtime/src/uav_clear.h55
-rw-r--r--runtime/src/vulkan_backend.h110
-rw-r--r--runtime/src/vulkan_predicate.h183
-rw-r--r--runtime/src/vulkan_predicated_copy.h154
-rw-r--r--runtime/src/vulkan_replay.h55
13 files changed, 711 insertions, 99 deletions
diff --git a/runtime/src/command_list.h b/runtime/src/command_list.h
index dff35d5b2..77a478a1a 100644
--- a/runtime/src/command_list.h
+++ b/runtime/src/command_list.h
@@ -642,7 +642,8 @@ public:
642 // Copies. The rules D3D12 states for the call itself are checked here; ranges against the resources are 642 // Copies. The rules D3D12 states for the call itself are checked here; ranges against the resources are
643 // checked at replay, when the resources are known. 643 // checked at replay, when the resources are known.
644 bool copy_buffer(const CopyBuffer& copy) { 644 bool copy_buffer(const CopyBuffer& copy) {
645 if (!copy.dst || !copy.src || !copy.size) return false; 645 if (!copy.dst || !copy.src) return false;
646 if (!copy.size) return !closed_; // Native D3D12 accepts a zero-byte CopyBufferRegion as a no-op.
646 return append(copy); 647 return append(copy);
647 } 648 }
648 649
diff --git a/runtime/src/depth24_convert.comp b/runtime/src/depth24_convert.comp
index eb4795a35..688f68d78 100644
--- a/runtime/src/depth24_convert.comp
+++ b/runtime/src/depth24_convert.comp
@@ -36,10 +36,11 @@ float depth24_to_float(uint d) {
36// The 24-bit UNORM of f as WARP and AMD's D3D12 drivers write it: the product with 2^24 - 1 is rounded to a float 36// The 24-bit UNORM of f as WARP and AMD's D3D12 drivers write it: the product with 2^24 - 1 is rounded to a float
37// first, then to the nearest integer with ties to even (NVIDIA's hardware breaks some ties the other way; see 37// first, then to the nearest integer with ties to even (NVIDIA's hardware breaks some ties the other way; see
38// depth24.h). NaN and negatives give 0, 1 and above the maximum. 38// depth24.h). NaN and negatives give 0, 1 and above the maximum.
39uint depth24_from_float(float f) { 39uint depth24_from_bits(uint bits) {
40 if (!(f > 0.0)) return 0u; 40 // In unsigned order, all negative encodings and positive NaNs lie above positive infinity.
41 if (f >= 1.0) return 0xffffffu; 41 if (bits > 0x7f800000u) return 0u;
42 return uint(roundEven(f * 16777215.0)); 42 if (bits >= 0x3f800000u) return 0xffffffu;
43 return uint(roundEven(uintBitsToFloat(bits) * 16777215.0));
43} 44}
44 45
45void main() { 46void main() {
@@ -48,5 +49,5 @@ void main() {
48 uint a = pc.app_base + g.z * pc.app_slice + g.y * pc.app_row + g.x; 49 uint a = pc.app_base + g.z * pc.app_slice + g.y * pc.app_row + g.x;
49 uint s = pc.scratch_base + (g.z * pc.height + g.y) * pc.width + g.x; 50 uint s = pc.scratch_base + (g.z * pc.height + g.y) * pc.width + g.x;
50 if (pc.to_float != 0u) scratch[s] = floatBitsToUint(depth24_to_float(app[a] & 0xffffffu)); 51 if (pc.to_float != 0u) scratch[s] = floatBitsToUint(depth24_to_float(app[a] & 0xffffffu));
51 else app[a] = depth24_from_float(uintBitsToFloat(scratch[s])); 52 else app[a] = depth24_from_bits(scratch[s]);
52} 53}
diff --git a/runtime/src/depth24_convert.inc b/runtime/src/depth24_convert.inc
index a237a5af4..9438af65b 100644
--- a/runtime/src/depth24_convert.inc
+++ b/runtime/src/depth24_convert.inc
@@ -27,17 +27,18 @@
270x00000000,0x00000023,0x00000000,0x00040047, 270x00000000,0x00000023,0x00000000,0x00040047,
280x000000a7,0x00000021,0x00000000,0x00040047, 280x000000a7,0x00000021,0x00000000,0x00040047,
290x000000a7,0x00000022,0x00000000,0x00040047, 290x000000a7,0x00000022,0x00000000,0x00040047,
300x000000bb,0x0000000b,0x00000019,0x00020013, 300x000000ba,0x0000000b,0x00000019,0x00020013,
310x00000002,0x00030021,0x00000003,0x00000002, 310x00000002,0x00030021,0x00000003,0x00000002,
320x00040015,0x00000006,0x00000020,0x00000000, 320x00040015,0x00000006,0x00000020,0x00000000,
330x00030016,0x00000008,0x00000020,0x0004002b, 330x00030016,0x00000008,0x00000020,0x0004002b,
340x00000006,0x00000013,0x00000000,0x00020014, 340x00000006,0x00000012,0x00000000,0x00020014,
350x00000014,0x0004002b,0x00000008,0x00000018, 350x00000013,0x0004002b,0x00000008,0x00000017,
360x00000000,0x00040015,0x0000001a,0x00000020, 360x00000000,0x00040015,0x00000019,0x00000020,
370x00000001,0x0004002b,0x0000001a,0x00000021, 370x00000001,0x0004002b,0x00000019,0x00000020,
380x00000017,0x0004002b,0x00000006,0x00000027, 380x00000017,0x0004002b,0x00000006,0x00000026,
390x00000001,0x0004002b,0x00000008,0x0000003c, 390x00000001,0x0004002b,0x00000006,0x00000035,
400x3f800000,0x0004002b,0x00000006,0x00000040, 400x7f800000,0x0004002b,0x00000006,0x0000003b,
410x3f800000,0x0004002b,0x00000006,0x0000003f,
410x00ffffff,0x0004002b,0x00000008,0x00000043, 420x00ffffff,0x0004002b,0x00000008,0x00000043,
420x4b7fffff,0x00040017,0x00000049,0x00000006, 430x4b7fffff,0x00040017,0x00000049,0x00000006,
430x00000003,0x00040020,0x0000004c,0x00000001, 440x00000003,0x00040020,0x0000004c,0x00000001,
@@ -47,15 +48,15 @@
470x00000006,0x00000006,0x00000006,0x00040020, 480x00000006,0x00000006,0x00000006,0x00040020,
480x00000052,0x00000009,0x00000051,0x0004003b, 490x00000052,0x00000009,0x00000051,0x0004003b,
490x00000052,0x00000053,0x00000009,0x0004002b, 500x00000052,0x00000053,0x00000009,0x0004002b,
500x0000001a,0x00000054,0x00000000,0x00040020, 510x00000019,0x00000054,0x00000000,0x00040020,
510x00000055,0x00000009,0x00000006,0x0004002b, 520x00000055,0x00000009,0x00000006,0x0004002b,
520x0000001a,0x0000005e,0x00000001,0x0004002b, 530x00000019,0x0000005e,0x00000001,0x0004002b,
530x0000001a,0x00000069,0x00000002,0x0004002b, 540x00000019,0x00000069,0x00000002,0x0004002b,
540x0000001a,0x00000072,0x00000004,0x0004002b, 550x00000019,0x00000072,0x00000004,0x0004002b,
550x0000001a,0x00000077,0x00000006,0x0004002b, 560x00000019,0x00000077,0x00000006,0x0004002b,
560x0000001a,0x0000007e,0x00000005,0x0004002b, 570x00000019,0x0000007e,0x00000005,0x0004002b,
570x0000001a,0x00000087,0x00000007,0x0004002b, 580x00000019,0x00000087,0x00000007,0x0004002b,
580x0000001a,0x00000099,0x00000003,0x0003001d, 590x00000019,0x00000099,0x00000003,0x0003001d,
590x0000009f,0x00000006,0x0003001e,0x000000a0, 600x0000009f,0x00000006,0x0003001e,0x000000a0,
600x0000009f,0x00040020,0x000000a1,0x0000000c, 610x0000009f,0x00040020,0x000000a1,0x0000000c,
610x000000a0,0x0004003b,0x000000a1,0x000000a2, 620x000000a0,0x0004003b,0x000000a1,0x000000a2,
@@ -64,43 +65,43 @@
640x000000a6,0x0000000c,0x000000a5,0x0004003b, 650x000000a6,0x0000000c,0x000000a5,0x0004003b,
650x000000a6,0x000000a7,0x0000000c,0x00040020, 660x000000a6,0x000000a7,0x0000000c,0x00040020,
660x000000a9,0x0000000c,0x00000006,0x0004002b, 670x000000a9,0x0000000c,0x00000006,0x0004002b,
670x00000006,0x000000ba,0x00000008,0x0006002c, 680x00000006,0x000000b9,0x00000008,0x0006002c,
680x00000049,0x000000bb,0x000000ba,0x000000ba, 690x00000049,0x000000ba,0x000000b9,0x000000b9,
690x00000027,0x0004002b,0x0000001a,0x00000109, 700x00000026,0x0004002b,0x00000019,0x00000109,
700x00000050,0x00050036,0x00000002,0x00000004, 710x00000050,0x00050036,0x00000002,0x00000004,
710x00000000,0x00000003,0x000200f8,0x00000005, 720x00000000,0x00000003,0x000200f8,0x00000005,
720x000300f7,0x000000bc,0x00000000,0x000300fb, 730x000300f7,0x000000bb,0x00000000,0x000300fb,
730x00000013,0x000000bd,0x000200f8,0x000000bd, 740x00000012,0x000000bc,0x000200f8,0x000000bc,
740x0004003d,0x00000049,0x0000004e,0x0000004d, 750x0004003d,0x00000049,0x0000004e,0x0000004d,
750x00050051,0x00000006,0x00000050,0x0000004e, 760x00050051,0x00000006,0x00000050,0x0000004e,
760x00000000,0x00050041,0x00000055,0x00000056, 770x00000000,0x00050041,0x00000055,0x00000056,
770x00000053,0x00000054,0x0004003d,0x00000006, 780x00000053,0x00000054,0x0004003d,0x00000006,
780x00000057,0x00000056,0x000500ae,0x00000014, 790x00000057,0x00000056,0x000500ae,0x00000013,
790x00000058,0x00000050,0x00000057,0x000400a8, 800x00000058,0x00000050,0x00000057,0x000400a8,
800x00000014,0x00000059,0x00000058,0x000300f7, 810x00000013,0x00000059,0x00000058,0x000300f7,
810x0000005b,0x00000000,0x000400fa,0x00000059, 820x0000005b,0x00000000,0x000400fa,0x00000059,
820x0000005a,0x0000005b,0x000200f8,0x0000005a, 830x0000005a,0x0000005b,0x000200f8,0x0000005a,
830x00050051,0x00000006,0x0000005d,0x0000004e, 840x00050051,0x00000006,0x0000005d,0x0000004e,
840x00000001,0x00050041,0x00000055,0x0000005f, 850x00000001,0x00050041,0x00000055,0x0000005f,
850x00000053,0x0000005e,0x0004003d,0x00000006, 860x00000053,0x0000005e,0x0004003d,0x00000006,
860x00000060,0x0000005f,0x000500ae,0x00000014, 870x00000060,0x0000005f,0x000500ae,0x00000013,
870x00000061,0x0000005d,0x00000060,0x000200f9, 880x00000061,0x0000005d,0x00000060,0x000200f9,
880x0000005b,0x000200f8,0x0000005b,0x000700f5, 890x0000005b,0x000200f8,0x0000005b,0x000700f5,
890x00000014,0x00000062,0x00000058,0x000000bd, 900x00000013,0x00000062,0x00000058,0x000000bc,
900x00000061,0x0000005a,0x000400a8,0x00000014, 910x00000061,0x0000005a,0x000400a8,0x00000013,
910x00000063,0x00000062,0x000300f7,0x00000065, 920x00000063,0x00000062,0x000300f7,0x00000065,
920x00000000,0x000400fa,0x00000063,0x00000064, 930x00000000,0x000400fa,0x00000063,0x00000064,
930x00000065,0x000200f8,0x00000064,0x00050051, 940x00000065,0x000200f8,0x00000064,0x00050051,
940x00000006,0x00000068,0x0000004e,0x00000002, 950x00000006,0x00000068,0x0000004e,0x00000002,
950x00050041,0x00000055,0x0000006a,0x00000053, 960x00050041,0x00000055,0x0000006a,0x00000053,
960x00000069,0x0004003d,0x00000006,0x0000006b, 970x00000069,0x0004003d,0x00000006,0x0000006b,
970x0000006a,0x000500ae,0x00000014,0x0000006c, 980x0000006a,0x000500ae,0x00000013,0x0000006c,
980x00000068,0x0000006b,0x000200f9,0x00000065, 990x00000068,0x0000006b,0x000200f9,0x00000065,
990x000200f8,0x00000065,0x000700f5,0x00000014, 1000x000200f8,0x00000065,0x000700f5,0x00000013,
1000x0000006d,0x00000062,0x0000005b,0x0000006c, 1010x0000006d,0x00000062,0x0000005b,0x0000006c,
1010x00000064,0x000300f7,0x0000006f,0x00000000, 1020x00000064,0x000300f7,0x0000006f,0x00000000,
1020x000400fa,0x0000006d,0x0000006e,0x0000006f, 1030x000400fa,0x0000006d,0x0000006e,0x0000006f,
1030x000200f8,0x0000006e,0x000200f9,0x000000bc, 1040x000200f8,0x0000006e,0x000200f9,0x000000bb,
1040x000200f8,0x0000006f,0x00050041,0x00000055, 1050x000200f8,0x0000006f,0x00050041,0x00000055,
1050x00000073,0x00000053,0x00000072,0x0004003d, 1060x00000073,0x00000053,0x00000072,0x0004003d,
1060x00000006,0x00000074,0x00000073,0x00050051, 1070x00000006,0x00000074,0x00000073,0x00050051,
@@ -130,63 +131,63 @@
1300x00000098,0x00000095,0x00000050,0x00050041, 1310x00000098,0x00000095,0x00000050,0x00050041,
1310x00000055,0x0000009a,0x00000053,0x00000099, 1320x00000055,0x0000009a,0x00000053,0x00000099,
1320x0004003d,0x00000006,0x0000009b,0x0000009a, 1330x0004003d,0x00000006,0x0000009b,0x0000009a,
1330x000500ab,0x00000014,0x0000009c,0x0000009b, 1340x000500ab,0x00000013,0x0000009c,0x0000009b,
1340x00000013,0x000300f7,0x0000009e,0x00000000, 1350x00000012,0x000300f7,0x0000009e,0x00000000,
1350x000400fa,0x0000009c,0x0000009d,0x000000b1, 1360x000400fa,0x0000009c,0x0000009d,0x000000b1,
1360x000200f8,0x0000009d,0x00060041,0x000000a9, 1370x000200f8,0x0000009d,0x00060041,0x000000a9,
1370x000000aa,0x000000a7,0x00000054,0x00000085, 1380x000000aa,0x000000a7,0x00000054,0x00000085,
1380x0004003d,0x00000006,0x000000ab,0x000000aa, 1390x0004003d,0x00000006,0x000000ab,0x000000aa,
1390x000500c7,0x00000006,0x000000ac,0x000000ab, 1400x000500c7,0x00000006,0x000000ac,0x000000ab,
1400x00000040,0x000300f7,0x000000e8,0x00000000, 1410x0000003f,0x000300f7,0x000000e8,0x00000000,
1410x000300fb,0x00000013,0x000000d2,0x000200f8, 1420x000300fb,0x00000012,0x000000d2,0x000200f8,
1420x000000d2,0x000500aa,0x00000014,0x000000d4, 1430x000000d2,0x000500aa,0x00000013,0x000000d4,
1430x000000ac,0x00000013,0x000300f7,0x000000d6, 1440x000000ac,0x00000012,0x000300f7,0x000000d6,
1440x00000000,0x000400fa,0x000000d4,0x000000d5, 1450x00000000,0x000400fa,0x000000d4,0x000000d5,
1450x000000d6,0x000200f8,0x000000d5,0x000200f9, 1460x000000d6,0x000200f8,0x000000d5,0x000200f9,
1460x000000e8,0x000200f8,0x000000d6,0x0006000c, 1470x000000e8,0x000200f8,0x000000d6,0x0006000c,
1470x0000001a,0x000000d8,0x00000001,0x0000004b, 1480x00000019,0x000000d8,0x00000001,0x0000004b,
1480x000000ac,0x00050082,0x0000001a,0x000000db, 1490x000000ac,0x00050082,0x00000019,0x000000db,
1490x00000021,0x000000d8,0x0004007c,0x00000006, 1500x00000020,0x000000d8,0x0004007c,0x00000006,
1500x000000dc,0x000000db,0x000500c4,0x00000006, 1510x000000dc,0x000000db,0x000500c4,0x00000006,
1510x000000dd,0x000000ac,0x000000dc,0x00050080, 1520x000000dd,0x000000ac,0x000000dc,0x00050080,
1520x00000006,0x000000df,0x000000dd,0x00000027, 1530x00000006,0x000000df,0x000000dd,0x00000026,
1530x00040070,0x00000008,0x000000e0,0x000000df, 1540x00040070,0x00000008,0x000000e0,0x000000df,
1540x00050080,0x0000001a,0x000000e3,0x000000d8, 1550x00050080,0x00000019,0x000000e3,0x000000d8,
1550x00000109,0x0004007c,0x00000006,0x000000e4, 1560x00000109,0x0004007c,0x00000006,0x000000e4,
1560x000000e3,0x000500c4,0x00000006,0x000000e5, 1570x000000e3,0x000500c4,0x00000006,0x000000e5,
1570x000000e4,0x00000021,0x0004007c,0x00000008, 1580x000000e4,0x00000020,0x0004007c,0x00000008,
1580x000000e6,0x000000e5,0x00050085,0x00000008, 1590x000000e6,0x000000e5,0x00050085,0x00000008,
1590x000000e7,0x000000e0,0x000000e6,0x000200f9, 1600x000000e7,0x000000e0,0x000000e6,0x000200f9,
1600x000000e8,0x000200f8,0x000000e8,0x000700f5, 1610x000000e8,0x000200f8,0x000000e8,0x000700f5,
1610x00000008,0x00000108,0x00000018,0x000000d5, 1620x00000008,0x00000108,0x00000017,0x000000d5,
1620x000000e7,0x000000d6,0x0004007c,0x00000006, 1630x000000e7,0x000000d6,0x0004007c,0x00000006,
1630x000000af,0x00000108,0x00060041,0x000000a9, 1640x000000af,0x00000108,0x00060041,0x000000a9,
1640x000000b0,0x000000a2,0x00000054,0x00000098, 1650x000000b0,0x000000a2,0x00000054,0x00000098,
1650x0003003e,0x000000b0,0x000000af,0x000200f9, 1660x0003003e,0x000000b0,0x000000af,0x000200f9,
1660x0000009e,0x000200f8,0x000000b1,0x00060041, 1670x0000009e,0x000200f8,0x000000b1,0x00060041,
1670x000000a9,0x000000b4,0x000000a2,0x00000054, 1680x000000a9,0x000000b5,0x000000a2,0x00000054,
1680x00000098,0x0004003d,0x00000006,0x000000b5, 1690x00000098,0x0004003d,0x00000006,0x000000b6,
1690x000000b4,0x0004007c,0x00000008,0x000000b6,
1700x000000b5,0x000300f7,0x000000fc,0x00000000, 1700x000000b5,0x000300f7,0x000000fc,0x00000000,
1710x000300fb,0x00000013,0x000000ee,0x000200f8, 1710x000300fb,0x00000012,0x000000ee,0x000200f8,
1720x000000ee,0x000500bd,0x00000014,0x000000f1, 1720x000000ee,0x000500ac,0x00000013,0x000000f0,
1730x000000b6,0x00000018,0x000300f7,0x000000f3, 1730x000000b6,0x00000035,0x000300f7,0x000000f2,
1740x00000000,0x000400fa,0x000000f1,0x000000f2, 1740x00000000,0x000400fa,0x000000f0,0x000000f1,
1750x000000f3,0x000200f8,0x000000f2,0x000200f9, 1750x000000f2,0x000200f8,0x000000f1,0x000200f9,
1760x000000fc,0x000200f8,0x000000f3,0x000500be, 1760x000000fc,0x000200f8,0x000000f2,0x000500ae,
1770x00000014,0x000000f5,0x000000b6,0x0000003c, 1770x00000013,0x000000f4,0x000000b6,0x0000003b,
1780x000300f7,0x000000f7,0x00000000,0x000400fa, 1780x000300f7,0x000000f6,0x00000000,0x000400fa,
1790x000000f5,0x000000f6,0x000000f7,0x000200f8, 1790x000000f4,0x000000f5,0x000000f6,0x000200f8,
1800x000000f6,0x000200f9,0x000000fc,0x000200f8, 1800x000000f5,0x000200f9,0x000000fc,0x000200f8,
1810x000000f7,0x00050085,0x00000008,0x000000f9, 1810x000000f6,0x0004007c,0x00000008,0x000000f8,
1820x000000b6,0x00000043,0x0006000c,0x00000008, 1820x000000b6,0x00050085,0x00000008,0x000000f9,
1830x000000f8,0x00000043,0x0006000c,0x00000008,
1830x000000fa,0x00000001,0x00000002,0x000000f9, 1840x000000fa,0x00000001,0x00000002,0x000000f9,
1840x0004006d,0x00000006,0x000000fb,0x000000fa, 1850x0004006d,0x00000006,0x000000fb,0x000000fa,
1850x000200f9,0x000000fc,0x000200f8,0x000000fc, 1860x000200f9,0x000000fc,0x000200f8,0x000000fc,
1860x000900f5,0x00000006,0x00000107,0x00000013, 1870x000900f5,0x00000006,0x00000107,0x00000012,
1870x000000f2,0x00000040,0x000000f6,0x000000fb, 1880x000000f1,0x0000003f,0x000000f5,0x000000fb,
1880x000000f7,0x00060041,0x000000a9,0x000000b9, 1890x000000f6,0x00060041,0x000000a9,0x000000b8,
1890x000000a7,0x00000054,0x00000085,0x0003003e, 1900x000000a7,0x00000054,0x00000085,0x0003003e,
1900x000000b9,0x00000107,0x000200f9,0x0000009e, 1910x000000b8,0x00000107,0x000200f9,0x0000009e,
1910x000200f8,0x0000009e,0x000200f9,0x000000bc, 1920x000200f8,0x0000009e,0x000200f9,0x000000bb,
1920x000200f8,0x000000bc,0x000100fd,0x00010038} 1930x000200f8,0x000000bb,0x000100fd,0x00010038}
diff --git a/runtime/src/gfx10_descriptors.h b/runtime/src/gfx10_descriptors.h
index 39f967460..ad3605d4e 100644
--- a/runtime/src/gfx10_descriptors.h
+++ b/runtime/src/gfx10_descriptors.h
@@ -556,7 +556,8 @@ inline constexpr VkSamplerReductionMode vulkan_reduction_mode(FilterMode mode) {
556// MIP_FILTER_NONE samples the base level only, which Vulkan expresses as nearest mip filtering with the LOD held 556// MIP_FILTER_NONE samples the base level only, which Vulkan expresses as nearest mip filtering with the LOD held
557// in [0, 0.25] (the Vulkan specification's note on samplers without mipmapping). An unnormalised sampler gets 557// in [0, 0.25] (the Vulkan specification's note on samplers without mipmapping). An unnormalised sampler gets
558// what Vulkan requires of one: no mips, no anisotropy, no comparison. 558// what Vulkan requires of one: no mips, no anisotropy, no comparison.
559inline VkSamplerCreateInfo vulkan_sampler(const Sampler& sampler) { 559// A decoded GFX10 bias has absolute value at most 32. When creating a device sampler, pass its smaller limit.
560inline VkSamplerCreateInfo vulkan_sampler(const Sampler& sampler, float max_lod_bias = 32.0f) {
560 static_assert(int(CompareFunction::never) == int(VK_COMPARE_OP_NEVER) && 561 static_assert(int(CompareFunction::never) == int(VK_COMPARE_OP_NEVER) &&
561 int(CompareFunction::less) == int(VK_COMPARE_OP_LESS) && 562 int(CompareFunction::less) == int(VK_COMPARE_OP_LESS) &&
562 int(CompareFunction::equal) == int(VK_COMPARE_OP_EQUAL) && 563 int(CompareFunction::equal) == int(VK_COMPARE_OP_EQUAL) &&
@@ -576,7 +577,7 @@ inline VkSamplerCreateInfo vulkan_sampler(const Sampler& sampler) {
576 info.addressModeU = vulkan_address_mode(sampler.address_u); 577 info.addressModeU = vulkan_address_mode(sampler.address_u);
577 info.addressModeV = vulkan_address_mode(sampler.address_v); 578 info.addressModeV = vulkan_address_mode(sampler.address_v);
578 info.addressModeW = vulkan_address_mode(sampler.address_w); 579 info.addressModeW = vulkan_address_mode(sampler.address_w);
579 info.mipLodBias = sampler.lod_bias; 580 info.mipLodBias = std::clamp(sampler.lod_bias, -max_lod_bias, max_lod_bias);
580 info.anisotropyEnable = sampler.max_anisotropy > 1 ? VK_TRUE : VK_FALSE; 581 info.anisotropyEnable = sampler.max_anisotropy > 1 ? VK_TRUE : VK_FALSE;
581 info.maxAnisotropy = float(std::max(sampler.max_anisotropy, 1u)); 582 info.maxAnisotropy = float(std::max(sampler.max_anisotropy, 1u));
582 info.compareEnable = (sampler.compare_never || sampler.compare != CompareFunction::never) ? VK_TRUE : VK_FALSE; 583 info.compareEnable = (sampler.compare_never || sampler.compare != CompareFunction::never) ? VK_TRUE : VK_FALSE;
diff --git a/runtime/src/predicate_reduce.comp b/runtime/src/predicate_reduce.comp
new file mode 100644
index 000000000..86b07d67f
--- /dev/null
+++ b/runtime/src/predicate_reduce.comp
@@ -0,0 +1,6 @@
1// Synthetic internal shader: one invocation captures a D3D12 64-bit predicate as a Vulkan condition.
2#version 450
3layout(local_size_x=1) in;
4layout(set=0,binding=0,std430) buffer Words { uint words[]; } data;
5layout(push_constant) uniform Index { uint base; } index;
6void main() { data.words[index.base+2] = data.words[index.base] | data.words[index.base+1]; }
diff --git a/runtime/src/predicate_reduce_spv.h b/runtime/src/predicate_reduce_spv.h
new file mode 100644
index 000000000..2d703597f
--- /dev/null
+++ b/runtime/src/predicate_reduce_spv.h
@@ -0,0 +1,36 @@
1// Generated with glslang 16.6.0:
2// glslang -V --target-env vulkan1.1 -Os --spirv-val predicate_reduce.comp -o predicate_reduce.spv
3#pragma once
4#include <cstdint>
5namespace leonida {
6inline constexpr std::uint32_t predicate_reduce_spirv[] = {
7 0x07230203u, 0x00010300u, 0x0008000bu, 0x00000024u, 0x00000000u, 0x00020011u, 0x00000001u, 0x0006000bu,
8 0x00000001u, 0x4c534c47u, 0x6474732eu, 0x3035342eu, 0x00000000u, 0x0003000eu, 0x00000000u, 0x00000001u,
9 0x0005000fu, 0x00000005u, 0x00000004u, 0x6e69616du, 0x00000000u, 0x00060010u, 0x00000004u, 0x00000011u,
10 0x00000001u, 0x00000001u, 0x00000001u, 0x00030003u, 0x00000002u, 0x000001c2u, 0x00040005u, 0x00000004u,
11 0x6e69616du, 0x00000000u, 0x00040005u, 0x00000008u, 0x64726f57u, 0x00000073u, 0x00050006u, 0x00000008u,
12 0x00000000u, 0x64726f77u, 0x00000073u, 0x00040005u, 0x0000000au, 0x61746164u, 0x00000000u, 0x00040005u,
13 0x0000000du, 0x65646e49u, 0x00000078u, 0x00050006u, 0x0000000du, 0x00000000u, 0x65736162u, 0x00000000u,
14 0x00040005u, 0x0000000fu, 0x65646e69u, 0x00000078u, 0x00040047u, 0x00000007u, 0x00000006u, 0x00000004u,
15 0x00030047u, 0x00000008u, 0x00000002u, 0x00050048u, 0x00000008u, 0x00000000u, 0x00000023u, 0x00000000u,
16 0x00040047u, 0x0000000au, 0x00000021u, 0x00000000u, 0x00040047u, 0x0000000au, 0x00000022u, 0x00000000u,
17 0x00030047u, 0x0000000du, 0x00000002u, 0x00050048u, 0x0000000du, 0x00000000u, 0x00000023u, 0x00000000u,
18 0x00040047u, 0x00000023u, 0x0000000bu, 0x00000019u, 0x00020013u, 0x00000002u, 0x00030021u, 0x00000003u,
19 0x00000002u, 0x00040015u, 0x00000006u, 0x00000020u, 0x00000000u, 0x0003001du, 0x00000007u, 0x00000006u,
20 0x0003001eu, 0x00000008u, 0x00000007u, 0x00040020u, 0x00000009u, 0x0000000cu, 0x00000008u, 0x0004003bu,
21 0x00000009u, 0x0000000au, 0x0000000cu, 0x00040015u, 0x0000000bu, 0x00000020u, 0x00000001u, 0x0004002bu,
22 0x0000000bu, 0x0000000cu, 0x00000000u, 0x0003001eu, 0x0000000du, 0x00000006u, 0x00040020u, 0x0000000eu,
23 0x00000009u, 0x0000000du, 0x0004003bu, 0x0000000eu, 0x0000000fu, 0x00000009u, 0x00040020u, 0x00000010u,
24 0x00000009u, 0x00000006u, 0x0004002bu, 0x00000006u, 0x00000013u, 0x00000002u, 0x00040020u, 0x00000017u,
25 0x0000000cu, 0x00000006u, 0x0004002bu, 0x00000006u, 0x0000001cu, 0x00000001u, 0x00040017u, 0x00000022u,
26 0x00000006u, 0x00000003u, 0x0006002cu, 0x00000022u, 0x00000023u, 0x0000001cu, 0x0000001cu, 0x0000001cu,
27 0x00050036u, 0x00000002u, 0x00000004u, 0x00000000u, 0x00000003u, 0x000200f8u, 0x00000005u, 0x00050041u,
28 0x00000010u, 0x00000011u, 0x0000000fu, 0x0000000cu, 0x0004003du, 0x00000006u, 0x00000012u, 0x00000011u,
29 0x00050080u, 0x00000006u, 0x00000014u, 0x00000012u, 0x00000013u, 0x00060041u, 0x00000017u, 0x00000018u,
30 0x0000000au, 0x0000000cu, 0x00000012u, 0x0004003du, 0x00000006u, 0x00000019u, 0x00000018u, 0x00050080u,
31 0x00000006u, 0x0000001du, 0x00000012u, 0x0000001cu, 0x00060041u, 0x00000017u, 0x0000001eu, 0x0000000au,
32 0x0000000cu, 0x0000001du, 0x0004003du, 0x00000006u, 0x0000001fu, 0x0000001eu, 0x000500c5u, 0x00000006u,
33 0x00000020u, 0x00000019u, 0x0000001fu, 0x00060041u, 0x00000017u, 0x00000021u, 0x0000000au, 0x0000000cu,
34 0x00000014u, 0x0003003eu, 0x00000021u, 0x00000020u, 0x000100fdu, 0x00010038u,
35};
36}
diff --git a/runtime/src/predicated_copy.comp b/runtime/src/predicated_copy.comp
new file mode 100644
index 000000000..0a105554d
--- /dev/null
+++ b/runtime/src/predicated_copy.comp
@@ -0,0 +1,11 @@
1// Synthetic byte-granular buffer copy, executed inside the captured conditional-rendering scope.
2#version 450
3#extension GL_EXT_shader_8bit_storage : require
4layout(local_size_x=64) in;
5layout(set=0,binding=0,std430) readonly buffer Source { uint8_t bytes[]; } source;
6layout(set=0,binding=1,std430) buffer Destination { uint8_t bytes[]; } destination;
7layout(push_constant) uniform Copy { uint source_offset; uint destination_offset; uint size; } copy;
8void main() {
9 uint i=gl_GlobalInvocationID.x;
10 if(i<copy.size) destination.bytes[copy.destination_offset+i]=source.bytes[copy.source_offset+i];
11}
diff --git a/runtime/src/predicated_copy_spv.h b/runtime/src/predicated_copy_spv.h
new file mode 100644
index 000000000..3cf9f30fe
--- /dev/null
+++ b/runtime/src/predicated_copy_spv.h
@@ -0,0 +1,54 @@
1// glslang 16.6.0: glslang -V --target-env vulkan1.2 -Os --spirv-val predicated_copy.comp -o predicated_copy.spv
2#pragma once
3#include <cstdint>
4namespace leonida { inline constexpr std::uint32_t predicated_copy_spirv[] = {
5 0x07230203u, 0x00010500u, 0x0008000bu, 0x00000037u, 0x00000000u, 0x00020011u, 0x00000001u, 0x00020011u,
6 0x00001160u, 0x0006000bu, 0x00000001u, 0x4c534c47u, 0x6474732eu, 0x3035342eu, 0x00000000u, 0x0003000eu,
7 0x00000000u, 0x00000001u, 0x0009000fu, 0x00000005u, 0x00000004u, 0x6e69616du, 0x00000000u, 0x0000000bu,
8 0x00000013u, 0x00000021u, 0x0000002bu, 0x00060010u, 0x00000004u, 0x00000011u, 0x00000040u, 0x00000001u,
9 0x00000001u, 0x00030003u, 0x00000002u, 0x000001c2u, 0x00080004u, 0x455f4c47u, 0x735f5458u, 0x65646168u,
10 0x62385f72u, 0x735f7469u, 0x61726f74u, 0x00006567u, 0x00040005u, 0x00000004u, 0x6e69616du, 0x00000000u,
11 0x00080005u, 0x0000000bu, 0x475f6c67u, 0x61626f6cu, 0x766e496cu, 0x7461636fu, 0x496e6f69u, 0x00000044u,
12 0x00040005u, 0x00000011u, 0x79706f43u, 0x00000000u, 0x00070006u, 0x00000011u, 0x00000000u, 0x72756f73u,
13 0x6f5f6563u, 0x65736666u, 0x00000074u, 0x00080006u, 0x00000011u, 0x00000001u, 0x74736564u, 0x74616e69u,
14 0x5f6e6f69u, 0x7366666fu, 0x00007465u, 0x00050006u, 0x00000011u, 0x00000002u, 0x657a6973u, 0x00000000u,
15 0x00040005u, 0x00000013u, 0x79706f63u, 0x00000000u, 0x00050005u, 0x0000001fu, 0x74736544u, 0x74616e69u,
16 0x006e6f69u, 0x00050006u, 0x0000001fu, 0x00000000u, 0x65747962u, 0x00000073u, 0x00050005u, 0x00000021u,
17 0x74736564u, 0x74616e69u, 0x006e6f69u, 0x00040005u, 0x00000029u, 0x72756f53u, 0x00006563u, 0x00050006u,
18 0x00000029u, 0x00000000u, 0x65747962u, 0x00000073u, 0x00040005u, 0x0000002bu, 0x72756f73u, 0x00006563u,
19 0x00040047u, 0x0000000bu, 0x0000000bu, 0x0000001cu, 0x00030047u, 0x00000011u, 0x00000002u, 0x00050048u,
20 0x00000011u, 0x00000000u, 0x00000023u, 0x00000000u, 0x00050048u, 0x00000011u, 0x00000001u, 0x00000023u,
21 0x00000004u, 0x00050048u, 0x00000011u, 0x00000002u, 0x00000023u, 0x00000008u, 0x00040047u, 0x0000001eu,
22 0x00000006u, 0x00000001u, 0x00030047u, 0x0000001fu, 0x00000002u, 0x00050048u, 0x0000001fu, 0x00000000u,
23 0x00000023u, 0x00000000u, 0x00040047u, 0x00000021u, 0x00000021u, 0x00000001u, 0x00040047u, 0x00000021u,
24 0x00000022u, 0x00000000u, 0x00040047u, 0x00000028u, 0x00000006u, 0x00000001u, 0x00030047u, 0x00000029u,
25 0x00000002u, 0x00040048u, 0x00000029u, 0x00000000u, 0x00000018u, 0x00050048u, 0x00000029u, 0x00000000u,
26 0x00000023u, 0x00000000u, 0x00030047u, 0x0000002bu, 0x00000018u, 0x00040047u, 0x0000002bu, 0x00000021u,
27 0x00000000u, 0x00040047u, 0x0000002bu, 0x00000022u, 0x00000000u, 0x00040047u, 0x00000036u, 0x0000000bu,
28 0x00000019u, 0x00020013u, 0x00000002u, 0x00030021u, 0x00000003u, 0x00000002u, 0x00040015u, 0x00000006u,
29 0x00000020u, 0x00000000u, 0x00040017u, 0x00000009u, 0x00000006u, 0x00000003u, 0x00040020u, 0x0000000au,
30 0x00000001u, 0x00000009u, 0x0004003bu, 0x0000000au, 0x0000000bu, 0x00000001u, 0x0004002bu, 0x00000006u,
31 0x0000000cu, 0x00000000u, 0x00040020u, 0x0000000du, 0x00000001u, 0x00000006u, 0x0005001eu, 0x00000011u,
32 0x00000006u, 0x00000006u, 0x00000006u, 0x00040020u, 0x00000012u, 0x00000009u, 0x00000011u, 0x0004003bu,
33 0x00000012u, 0x00000013u, 0x00000009u, 0x00040015u, 0x00000014u, 0x00000020u, 0x00000001u, 0x0004002bu,
34 0x00000014u, 0x00000015u, 0x00000002u, 0x00040020u, 0x00000016u, 0x00000009u, 0x00000006u, 0x00020014u,
35 0x00000019u, 0x00040015u, 0x0000001du, 0x00000008u, 0x00000000u, 0x0003001du, 0x0000001eu, 0x0000001du,
36 0x0003001eu, 0x0000001fu, 0x0000001eu, 0x00040020u, 0x00000020u, 0x0000000cu, 0x0000001fu, 0x0004003bu,
37 0x00000020u, 0x00000021u, 0x0000000cu, 0x0004002bu, 0x00000014u, 0x00000022u, 0x00000000u, 0x0004002bu,
38 0x00000014u, 0x00000023u, 0x00000001u, 0x0003001du, 0x00000028u, 0x0000001du, 0x0003001eu, 0x00000029u,
39 0x00000028u, 0x00040020u, 0x0000002au, 0x0000000cu, 0x00000029u, 0x0004003bu, 0x0000002au, 0x0000002bu,
40 0x0000000cu, 0x00040020u, 0x00000030u, 0x0000000cu, 0x0000001du, 0x0004002bu, 0x00000006u, 0x00000034u,
41 0x00000040u, 0x0004002bu, 0x00000006u, 0x00000035u, 0x00000001u, 0x0006002cu, 0x00000009u, 0x00000036u,
42 0x00000034u, 0x00000035u, 0x00000035u, 0x00050036u, 0x00000002u, 0x00000004u, 0x00000000u, 0x00000003u,
43 0x000200f8u, 0x00000005u, 0x00050041u, 0x0000000du, 0x0000000eu, 0x0000000bu, 0x0000000cu, 0x0004003du,
44 0x00000006u, 0x0000000fu, 0x0000000eu, 0x00050041u, 0x00000016u, 0x00000017u, 0x00000013u, 0x00000015u,
45 0x0004003du, 0x00000006u, 0x00000018u, 0x00000017u, 0x000500b0u, 0x00000019u, 0x0000001au, 0x0000000fu,
46 0x00000018u, 0x000300f7u, 0x0000001cu, 0x00000000u, 0x000400fau, 0x0000001au, 0x0000001bu, 0x0000001cu,
47 0x000200f8u, 0x0000001bu, 0x00050041u, 0x00000016u, 0x00000024u, 0x00000013u, 0x00000023u, 0x0004003du,
48 0x00000006u, 0x00000025u, 0x00000024u, 0x00050080u, 0x00000006u, 0x00000027u, 0x00000025u, 0x0000000fu,
49 0x00050041u, 0x00000016u, 0x0000002cu, 0x00000013u, 0x00000022u, 0x0004003du, 0x00000006u, 0x0000002du,
50 0x0000002cu, 0x00050080u, 0x00000006u, 0x0000002fu, 0x0000002du, 0x0000000fu, 0x00060041u, 0x00000030u,
51 0x00000031u, 0x0000002bu, 0x00000022u, 0x0000002fu, 0x0004003du, 0x0000001du, 0x00000032u, 0x00000031u,
52 0x00060041u, 0x00000030u, 0x00000033u, 0x00000021u, 0x00000022u, 0x00000027u, 0x0003003eu, 0x00000033u,
53 0x00000032u, 0x000200f9u, 0x0000001cu, 0x000200f8u, 0x0000001cu, 0x000100fdu, 0x00010038u,
54}; }
diff --git a/runtime/src/uav_clear.h b/runtime/src/uav_clear.h
index 73ec54dea..a27da8d7a 100644
--- a/runtime/src/uav_clear.h
+++ b/runtime/src/uav_clear.h
@@ -8,10 +8,13 @@
8// and ...Float, which the D3D12 methods keep): 8// and ...Float, which the D3D12 methods keep):
9// - Uint copies the low n bits of value i into channel i, where n is the channel's width, with no conversion, 9// - Uint copies the low n bits of value i into channel i, where n is the channel's width, with no conversion,
10// whatever the format's type: 0x3f800000 into an R32_FLOAT channel is 1.0, 0xff into an R8_UNORM one is 1.0. 10// whatever the format's type: 0x3f800000 into an R32_FLOAT channel is 1.0, 0xff into an R8_UNORM one is 1.0.
11// A raw or structured buffer view takes value 0 for every 32-bit word. 11// A raw or structured buffer view takes value 0 for every 32-bit word. R9G9B9E5_SHAREDEXP has no independent
12// per-channel integer bit layout and WARP clears it to 0 for any uint input.
12// - Float converts to the format: UNORM and SNORM clamp to [0, 1] / [-1, 1] and round to nearest (NaN is 0); 13// - Float converts to the format: UNORM and SNORM clamp to [0, 1] / [-1, 1] and round to nearest (NaN is 0);
13// 16-bit and the packed 11/10-bit floats round toward zero, a finite value too large becoming the largest finite 14// 16-bit and the packed 11/10-bit floats round toward zero, a finite value too large becoming the largest finite
14// (D3D11.3 3.2.2; WARP and NVIDIA agree), and the small floats have no sign: negative is 0. 15// (D3D11.3 3.2.2; WARP and NVIDIA agree), and the small floats have no sign: negative is 0.
16// R9G9B9E5_SHAREDEXP clamps negative/NaN to 0 and +Inf/overflow to 65408.0, shares the largest channel's
17// exponent and rounds mantissas half-up (bumping the exponent on 512), matching WARP.
15// D3D11 calls Float on an integer view invalid and D3D12 does not say; here the value is converted to the 18// D3D11 calls Float on an integer view invalid and D3D12 does not say; here the value is converted to the
16// integer (rounded toward zero, clamped, NaN is 0) so the result is defined. A raw or structured buffer view 19// integer (rounded toward zero, clamped, NaN is 0) so the result is defined. A raw or structured buffer view
17// takes value 0's bits. 20// takes value 0's bits.
@@ -35,7 +38,7 @@
35 38
36namespace leonida::uav_clear { 39namespace leonida::uav_clear {
37 40
38enum class Kind : std::uint8_t { unorm, snorm, uint, sint, sfloat, ufloat }; 41enum class Kind : std::uint8_t { unorm, snorm, uint, sint, sfloat, ufloat, shared_exp };
39 42
40// One channel: where its bits are in the element (little-endian, from bit 0 of byte 0) and how many. 43// One channel: where its bits are in the element (little-endian, from bit 0 of byte 0) and how many.
41struct Channel { 44struct Channel {
@@ -66,6 +69,7 @@ inline constexpr Layout layouts[] = {
66 {VK_FORMAT_A2B10G10R10_UNORM_PACK32, Kind::unorm, 4, {{{0, 10}, {10, 10}, {20, 10}, {30, 2}}}}, 69 {VK_FORMAT_A2B10G10R10_UNORM_PACK32, Kind::unorm, 4, {{{0, 10}, {10, 10}, {20, 10}, {30, 2}}}},
67 {VK_FORMAT_A2B10G10R10_UINT_PACK32, Kind::uint, 4, {{{0, 10}, {10, 10}, {20, 10}, {30, 2}}}}, 70 {VK_FORMAT_A2B10G10R10_UINT_PACK32, Kind::uint, 4, {{{0, 10}, {10, 10}, {20, 10}, {30, 2}}}},
68 {VK_FORMAT_B10G11R11_UFLOAT_PACK32, Kind::ufloat, 4, {{{0, 11}, {11, 11}, {22, 10}, {}}}}, 71 {VK_FORMAT_B10G11R11_UFLOAT_PACK32, Kind::ufloat, 4, {{{0, 11}, {11, 11}, {22, 10}, {}}}},
72 {VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, Kind::shared_exp, 4, {{{0, 9}, {9, 9}, {18, 9}, {27, 5}}}},
69 {VK_FORMAT_R8G8B8A8_UNORM, Kind::unorm, 4, {{{0, 8}, {8, 8}, {16, 8}, {24, 8}}}}, 73 {VK_FORMAT_R8G8B8A8_UNORM, Kind::unorm, 4, {{{0, 8}, {8, 8}, {16, 8}, {24, 8}}}},
70 {VK_FORMAT_R8G8B8A8_SNORM, Kind::snorm, 4, {{{0, 8}, {8, 8}, {16, 8}, {24, 8}}}}, 74 {VK_FORMAT_R8G8B8A8_SNORM, Kind::snorm, 4, {{{0, 8}, {8, 8}, {16, 8}, {24, 8}}}},
71 {VK_FORMAT_R8G8B8A8_UINT, Kind::uint, 4, {{{0, 8}, {8, 8}, {16, 8}, {24, 8}}}}, 75 {VK_FORMAT_R8G8B8A8_UINT, Kind::uint, 4, {{{0, 8}, {8, 8}, {16, 8}, {24, 8}}}},
@@ -184,6 +188,33 @@ inline std::uint32_t to_integer(float value, bool is_signed, unsigned bits) {
184 return std::uint32_t(std::clamp(std::trunc(double(value)), 0.0, std::ldexp(1.0, int(bits)) - 1)); 188 return std::uint32_t(std::clamp(std::trunc(double(value)), 0.0, std::ldexp(1.0, int(bits)) - 1));
185} 189}
186 190
191// Shared-exponent 9:9:9:5 unsigned float (EXT_texture_shared_exponent / D3D11.3).
192// Clamps negative and NaN to 0 and overflow/+Inf to 65408.0 ((511/512) * 2^16), shares the largest channel's
193// exponent, rounds mantissas half-up (floor(c / denom + 0.5)), and bumps the exponent if the largest mantissa
194// rounds to 512. Matches ClearUnorderedAccessViewFloat on WARP.
195inline std::uint32_t to_rgb9e5(float r, float g, float b) {
196 constexpr float max_value = 65408.0f;
197 const auto clamp_channel = [](float v) { return !(v > 0.0f) ? 0.0f : (v > max_value ? max_value : v); };
198 r = clamp_channel(r);
199 g = clamp_channel(g);
200 b = clamp_channel(b);
201 const float max_c = std::max(r, std::max(g, b));
202 if (max_c == 0.0f) return 0u;
203 int exp_raw = 0;
204 std::frexp(max_c, &exp_raw); // floor(log2(max_c)) == exp_raw - 1
205 int exp_shared = std::max(0, exp_raw + 15);
206 double denom = std::ldexp(1.0, exp_shared - 24); // 2^(exp_shared - 15 - 9)
207 const int max_m = static_cast<int>(std::floor(double(max_c) / denom + 0.5));
208 if (max_m == 512) {
209 ++exp_shared;
210 denom *= 2.0;
211 }
212 const auto rm = static_cast<std::uint32_t>(std::floor(double(r) / denom + 0.5));
213 const auto gm = static_cast<std::uint32_t>(std::floor(double(g) / denom + 0.5));
214 const auto bm = static_cast<std::uint32_t>(std::floor(double(b) / denom + 0.5));
215 return rm | (gm << 9) | (bm << 18) | (static_cast<std::uint32_t>(exp_shared) << 27);
216}
217
187} // namespace detail 218} // namespace detail
188 219
189// The element a clear writes into a view of `format`. `values` are the call's four UINTs, or the bits of its four 220// The element a clear writes into a view of `format`. `values` are the call's four UINTs, or the bits of its four
@@ -193,6 +224,14 @@ inline bool encode(VkFormat format, bool is_float, const std::array<std::uint32_
193 if (!l) return false; 224 if (!l) return false;
194 out = {}; 225 out = {};
195 out.size = l->bytes; 226 out.size = l->bytes;
227 if (l->kind == Kind::shared_exp) {
228 const std::uint32_t word = is_float ? detail::to_rgb9e5(std::bit_cast<float>(values[0]),
229 std::bit_cast<float>(values[1]),
230 std::bit_cast<float>(values[2]))
231 : 0u;
232 std::memcpy(out.bytes.data(), &word, 4);
233 return true;
234 }
196 for (unsigned i = 0; i < 4; ++i) { 235 for (unsigned i = 0; i < 4; ++i) {
197 const Channel c = l->rgba[i]; 236 const Channel c = l->rgba[i];
198 if (!c.bits) continue; 237 if (!c.bits) continue;
@@ -215,6 +254,7 @@ inline bool encode(VkFormat format, bool is_float, const std::array<std::uint32_
215 case Kind::sint: detail::put(out, c, detail::to_integer(v, true, c.bits)); break; 254 case Kind::sint: detail::put(out, c, detail::to_integer(v, true, c.bits)); break;
216 case Kind::sfloat: 255 case Kind::sfloat:
217 case Kind::ufloat: detail::put(out, c, detail::to_float_bits(v, *l, c.bits)); break; 256 case Kind::ufloat: detail::put(out, c, detail::to_float_bits(v, *l, c.bits)); break;
257 case Kind::shared_exp: break;
218 } 258 }
219 } 259 }
220 return true; 260 return true;
@@ -226,6 +266,16 @@ inline bool decode(VkFormat format, const Element& element, VkClearColorValue& o
226 const Layout* l = layout(format); 266 const Layout* l = layout(format);
227 if (!l || l->bytes != element.size) return false; 267 if (!l || l->bytes != element.size) return false;
228 out = {}; 268 out = {};
269 if (l->kind == Kind::shared_exp) {
270 std::uint32_t word = 0;
271 std::memcpy(&word, element.bytes.data(), 4);
272 const float scale = std::ldexp(1.0f, static_cast<int>(word >> 27) - 24);
273 out.float32[0] = float(word & 0x1ffu) * scale;
274 out.float32[1] = float((word >> 9) & 0x1ffu) * scale;
275 out.float32[2] = float((word >> 18) & 0x1ffu) * scale;
276 out.float32[3] = 1.0f;
277 return true;
278 }
229 for (unsigned i = 0; i < 4; ++i) { 279 for (unsigned i = 0; i < 4; ++i) {
230 const Channel c = l->rgba[i]; 280 const Channel c = l->rgba[i];
231 if (!c.bits) continue; 281 if (!c.bits) continue;
@@ -246,6 +296,7 @@ inline bool decode(VkFormat format, const Element& element, VkClearColorValue& o
246 } 296 }
247 case Kind::sfloat: 297 case Kind::sfloat:
248 case Kind::ufloat: out.float32[i] = detail::from_float_bits(raw, *l, c.bits); break; 298 case Kind::ufloat: out.float32[i] = detail::from_float_bits(raw, *l, c.bits); break;
299 case Kind::shared_exp: break;
249 } 300 }
250 } 301 }
251 return true; 302 return true;
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index fc4c666a4..8e43cdaa5 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -716,6 +716,17 @@ public:
716 VulkanContext& operator=(const VulkanContext&) = delete; 716 VulkanContext& operator=(const VulkanContext&) = delete;
717 717
718 bool loaded() const { return loaded_; } 718 bool loaded() const { return loaded_; }
719 void configure_predication(PFN_vkGetDeviceProcAddr get, const VkPhysicalDeviceMemoryProperties& memory,
720 VkDeviceSize max_range, std::uint32_t family) {
721 std::unique_lock lock(resource_mutex_);
722 predicate_family_ = family;
723 predicate_reducer_ = std::make_unique<PredicateReducer>(get, device_, memory, max_range);
724 }
725 // Configure before submitting work, only when storageBuffer8BitAccess is enabled on the device.
726 void configure_predicated_copy(PFN_vkGetDeviceProcAddr get, const VkPhysicalDeviceLimits& limits) {
727 std::unique_lock lock(resource_mutex_);
728 predicated_copier_ = std::make_unique<PredicatedBufferCopier>(get, device_, limits);
729 }
719 VkDevice device() const { return device_; } 730 VkDevice device() const { return device_; }
720 731
721 // Debug markers become labels from here on; call before any queue or list uses the context. Incomplete sets 732 // Debug markers become labels from here on; call before any queue or list uses the context. Incomplete sets
@@ -966,11 +977,12 @@ private:
966 info.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY; 977 info.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
967 info.format = r.format; 978 info.format = r.format;
968 info.subresourceRange = range; 979 info.subresourceRange = range;
969 // Reinterpreted RTVs need only color-attachment usage. Inheriting STORAGE usage from the resource 980 // Attachment views need only color-attachment usage. Inheriting STORAGE usage from a mutable/extended
970 // would incorrectly require it from an sRGB view, even though this view is never used as storage. 981 // resource (typeless RGBA8 sRGB views, or R32_UINT-backed R9G9B9E5 UAV images) would require unrelated usages.
971 VkImageViewUsageCreateInfo usage{VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO}; 982 VkImageViewUsageCreateInfo usage{VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO};
972 usage.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; 983 usage.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
973 if (r.rgba8_typeless) info.pNext = &usage; 984 if (r.rgba8_typeless || (range.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT && r.color_attachment))
985 info.pNext = &usage;
974 VkImageView view = VK_NULL_HANDLE; 986 VkImageView view = VK_NULL_HANDLE;
975 if (vk.vkCreateImageView(device_, &info, nullptr, &view) != VK_SUCCESS) return VK_NULL_HANDLE; 987 if (vk.vkCreateImageView(device_, &info, nullptr, &view) != VK_SUCCESS) return VK_NULL_HANDLE;
976 views_[key] = view; 988 views_[key] = view;
@@ -1002,6 +1014,9 @@ private:
1002 double timestamp_period_ = 0; 1014 double timestamp_period_ = 0;
1003 VkPhysicalDeviceMemoryProperties occlusion_resolve_memory_{}; 1015 VkPhysicalDeviceMemoryProperties occlusion_resolve_memory_{};
1004 ReplayFunctions replay_; 1016 ReplayFunctions replay_;
1017 std::unique_ptr<PredicateReducer> predicate_reducer_;
1018 std::unique_ptr<PredicatedBufferCopier> predicated_copier_;
1019 std::uint32_t predicate_family_ = UINT32_MAX;
1005 DebugLabelFunctions labels_; 1020 DebugLabelFunctions labels_;
1006}; 1021};
1007#ifdef _MSC_VER 1022#ifdef _MSC_VER
@@ -1078,7 +1093,7 @@ public:
1078 } 1093 }
1079 1094
1080 VulkanQueue(VulkanContext& context, VkQueue queue, std::uint32_t family, QueueKind kind = QueueKind::direct) 1095 VulkanQueue(VulkanContext& context, VkQueue queue, std::uint32_t family, QueueKind kind = QueueKind::direct)
1081 : context_(context), queue_(queue), kind_(kind) { 1096 : context_(context), queue_(queue), kind_(kind), family_(family) {
1082 auto& vk = context.vk; 1097 auto& vk = context.vk;
1083 VkCommandPoolCreateInfo pool{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO}; 1098 VkCommandPoolCreateInfo pool{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO};
1084 pool.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; 1099 pool.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
@@ -1117,18 +1132,67 @@ public:
1117 VkCommandBuffer command_buffer = VK_NULL_HANDLE; 1132 VkCommandBuffer command_buffer = VK_NULL_HANDLE;
1118 std::function<void(bool)> bindless_commit; 1133 std::function<void(bool)> bindless_commit;
1119 // Lock hierarchy: resource_mutex_ -> tracker_mutex_ -> submit_mutex_. 1134 // Lock hierarchy: resource_mutex_ -> tracker_mutex_ -> submit_mutex_.
1120 // Shared read-lock allows concurrent prepare() lookups; when depth24_ is active, tracker_mutex_ is also 1135 // Shared read-lock allows concurrent prepare() across queues; tracker_mutex_ is acquired lazily during
1121 // acquired before prepare() so Depth24Converter::prepare/collect/retire stay serialized per execution. 1136 // prepare() only if a list actually converts a depth24_as_float copy, and otherwise right before
1122 // Otherwise tracker_mutex_ is held from emit() through submit() so emit order == submit order across queues. 1137 // bindless_prepare_/emit() through submit() so bindless flushes, layout emits, and submissions stay in order.
1123 std::shared_lock resource_lock(context_.resource_mutex_); 1138 std::shared_lock resource_lock(context_.resource_mutex_);
1124 std::unique_lock tracker_lock(context_.tracker_mutex_, std::defer_lock); 1139 std::unique_lock tracker_lock(context_.tracker_mutex_, std::defer_lock);
1125 if (context_.depth24_) { 1140 // Declared after tracker_lock so any early return after depth24_->prepare() retires uncommitted scratch/pools
1126 tracker_lock.lock(); 1141 // with ticket 0 while tracker_lock is still held.
1127 context_.depth24_->collect(); 1142 struct Depth24Guard {
1128 } 1143 Depth24Converter* d24 = nullptr;
1144 VkSemaphore progress = VK_NULL_HANDLE;
1145 bool active = false;
1146 ~Depth24Guard() {
1147 if (active && d24) d24->retire(progress, 0);
1148 }
1149 } depth24_guard{context_.depth24_.get(), progress_, false};
1129 bindless_commit = context_.bindless_commit_; 1150 bindless_commit = context_.bindless_commit_;
1130 std::vector<PreparedList> prepared(lists.size()); 1151 std::vector<PreparedList> prepared(lists.size());
1131 ReplayFunctions replay = context_.replay_; 1152 ReplayFunctions replay = context_.replay_;
1153 std::uint64_t captures = 0;
1154 for (const auto* list : lists)
1155 for (const auto& command : list->commands())
1156 if (const auto* p = std::get_if<Predication>(&command); p && p->buffer) ++captures;
1157 std::unique_ptr<PredicateReducer::Batch> batch;
1158 std::unique_ptr<PredicatedBufferCopier::Batch> copy_batch;
1159 if (captures) {
1160 if (kind_ == QueueKind::copy || !context_.predicate_reducer_ || family_ != context_.predicate_family_)
1161 return ReplayResult::unsupported;
1162 // The helper's lazy pipeline is shared by queues. Serialize preparation with the tracker lock,
1163 // retaining main's resource -> tracker -> submit lock order through emit and submission.
1164 if (!tracker_lock.owns_lock()) tracker_lock.lock();
1165 batch = context_.predicate_reducer_->batch(captures);
1166 if (!batch) return ReplayResult::unsupported;
1167 replay.prepare_predicate = [&](VkBuffer buffer, VkDeviceSize offset, PredicateCapture& out) {
1168 return batch->prepare(buffer, offset, out);
1169 };
1170 replay.capture_predicate = [&](VkCommandBuffer cb, const PredicateCapture& p) {
1171 context_.predicate_reducer_->capture(cb, p);
1172 };
1173 if (context_.predicated_copier_) {
1174 copy_batch = context_.predicated_copier_->batch();
1175 replay.prepare_predicated_copy = [&](VkBuffer source, VkBuffer destination, VkDeviceSize src,
1176 VkDeviceSize dst, VkDeviceSize size,
1177 std::vector<PredicatedCopyDispatch>& out) {
1178 return copy_batch->prepare(source, destination, src, dst, size, out);
1179 };
1180 replay.emit_predicated_copy = [&](VkCommandBuffer cb, const std::vector<PredicatedCopyDispatch>& jobs) {
1181 context_.predicated_copier_->emit(cb, jobs);
1182 };
1183 }
1184 }
1185 if (context_.depth24_) {
1186 replay.depth24_prepare = [this, &tracker_lock, &depth24_guard](Depth24Job& job,
1187 std::vector<Depth24Job>& parts) {
1188 if (!tracker_lock.owns_lock()) tracker_lock.lock();
1189 if (!depth24_guard.active) {
1190 context_.depth24_->collect();
1191 depth24_guard.active = true;
1192 }
1193 return context_.depth24_->prepare(job, parts);
1194 };
1195 }
1132 const auto& occlusion_memory = context_.occlusion_resolve_memory_; 1196 const auto& occlusion_memory = context_.occlusion_resolve_memory_;
1133 const bool can_resolve_occlusion = occlusion_memory.memoryTypeCount != 0; 1197 const bool can_resolve_occlusion = occlusion_memory.memoryTypeCount != 0;
1134 if (can_resolve_occlusion) { 1198 if (can_resolve_occlusion) {
@@ -1191,21 +1255,26 @@ public:
1191 discard(); 1255 discard();
1192 return ReplayResult::unsupported; 1256 return ReplayResult::unsupported;
1193 } 1257 }
1258 if (!tracker_lock.owns_lock()) tracker_lock.lock();
1194 if (context_.bindless_prepare_ && !context_.bindless_prepare_(command_buffer)) { 1259 if (context_.bindless_prepare_ && !context_.bindless_prepare_(command_buffer)) {
1195 const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS; 1260 const bool ended = context_.vk.vkEndCommandBuffer(command_buffer) == VK_SUCCESS;
1196 resource_lock.unlock(); 1261 resource_lock.unlock();
1197 if (tracker_lock.owns_lock()) tracker_lock.unlock();
1198 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); 1262 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0);
1199 if (!submitted) discard(); 1263 if (!submitted) discard();
1264 if (depth24_guard.active) {
1265 context_.depth24_->retire(progress_, 0);
1266 depth24_guard.active = false;
1267 }
1268 tracker_lock.unlock();
1200 if (bindless_commit) bindless_commit(submitted); 1269 if (bindless_commit) bindless_commit(submitted);
1201 return ReplayResult::unsupported; 1270 return ReplayResult::unsupported;
1202 } 1271 }
1203 if (!tracker_lock.owns_lock()) tracker_lock.lock(); 1272 auto next_tracker = context_.tracker_;
1204 bool wrote_readback = false; 1273 bool wrote_readback = false;
1205 VkPipelineStageFlags2 readback_stages = 0; 1274 VkPipelineStageFlags2 readback_stages = 0;
1206 std::uint32_t open_labels = 0; // events may span the lists of one ExecuteCommandLists 1275 std::uint32_t open_labels = 0; // events may span the lists of one ExecuteCommandLists
1207 for (const auto& list : prepared) { 1276 for (const auto& list : prepared) {
1208 emit(list, command_buffer, replay, &context_.tracker_, &open_labels); 1277 emit(list, command_buffer, replay, &next_tracker, &open_labels);
1209 wrote_readback |= list.wrote_readback; 1278 wrote_readback |= list.wrote_readback;
1210 readback_stages |= list.readback_src_stages; 1279 readback_stages |= list.readback_src_stages;
1211 } 1280 }
@@ -1244,12 +1313,18 @@ public:
1244 resource_lock.unlock(); // release resource_mutex_ before vkQueueSubmit while keeping tracker_lock held 1313 resource_lock.unlock(); // release resource_mutex_ before vkQueueSubmit while keeping tracker_lock held
1245 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); 1314 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0);
1246 if (submitted) { 1315 if (submitted) {
1316 context_.tracker_ = std::move(next_tracker);
1317 slot->predicate = std::move(batch);
1318 slot->predicated_copy = std::move(copy_batch);
1247 queries.commit(); 1319 queries.commit();
1248 slot->segments = std::move(queries.retained); 1320 slot->segments = std::move(queries.retained);
1249 } else { 1321 } else {
1250 discard(); 1322 discard();
1251 } 1323 }
1252 if (context_.depth24_) context_.depth24_->retire(progress_, submitted ? submitted_ : 0); 1324 if (depth24_guard.active) {
1325 context_.depth24_->retire(progress_, submitted ? submitted_ : 0);
1326 depth24_guard.active = false;
1327 }
1253 tracker_lock.unlock(); 1328 tracker_lock.unlock();
1254 if (bindless_commit) bindless_commit(submitted); 1329 if (bindless_commit) bindless_commit(submitted);
1255 return submitted ? ReplayResult::success : ReplayResult::unsupported; 1330 return submitted ? ReplayResult::success : ReplayResult::unsupported;
@@ -1295,6 +1370,8 @@ private:
1295 VkCommandBuffer buffer; 1370 VkCommandBuffer buffer;
1296 std::uint64_t ticket; 1371 std::uint64_t ticket;
1297 std::vector<std::shared_ptr<const OcclusionSegmentPool>> segments; 1372 std::vector<std::shared_ptr<const OcclusionSegmentPool>> segments;
1373 std::unique_ptr<PredicatedBufferCopier::Batch> predicated_copy;
1374 std::unique_ptr<PredicateReducer::Batch> predicate; // retained until this slot completes
1298 }; 1375 };
1299 1376
1300 CommandSlot* acquire() { 1377 CommandSlot* acquire() {
@@ -1310,6 +1387,8 @@ private:
1310 } 1387 }
1311 if (reuse) { 1388 if (reuse) {
1312 if (context_.vk.vkResetCommandBuffer(reuse->buffer, 0) != VK_SUCCESS) return nullptr; 1389 if (context_.vk.vkResetCommandBuffer(reuse->buffer, 0) != VK_SUCCESS) return nullptr;
1390 reuse->predicate.reset();
1391 reuse->predicated_copy.reset();
1313 reuse->ticket = UINT64_MAX; // in use until submitted 1392 reuse->ticket = UINT64_MAX; // in use until submitted
1314 return reuse; 1393 return reuse;
1315 } 1394 }
@@ -1376,6 +1455,7 @@ private:
1376 VulkanContext& context_; 1455 VulkanContext& context_;
1377 VkQueue queue_; 1456 VkQueue queue_;
1378 QueueKind kind_ = QueueKind::direct; 1457 QueueKind kind_ = QueueKind::direct;
1458 std::uint32_t family_;
1379 VkCommandPool pool_ = VK_NULL_HANDLE; 1459 VkCommandPool pool_ = VK_NULL_HANDLE;
1380 VkSemaphore progress_ = VK_NULL_HANDLE; // our own timeline: which command buffers are done 1460 VkSemaphore progress_ = VK_NULL_HANDLE; // our own timeline: which command buffers are done
1381 std::uint64_t submitted_ = 0; 1461 std::uint64_t submitted_ = 0;
diff --git a/runtime/src/vulkan_predicate.h b/runtime/src/vulkan_predicate.h
new file mode 100644
index 000000000..cedb8af81
--- /dev/null
+++ b/runtime/src/vulkan_predicate.h
@@ -0,0 +1,183 @@
1// GPU snapshot of D3D12's 64-bit predicate. No host readback and no per-predicate queue wait.
2#pragma once
3#ifndef VK_NO_PROTOTYPES
4#define VK_NO_PROTOTYPES
5#endif
6#include <vulkan/vulkan.h>
7#include <cstdint>
8#include <memory>
9#include <initializer_list>
10#include "predicate_reduce_spv.h"
11
12namespace leonida {
13struct PredicateCapture {
14 VkBuffer source = VK_NULL_HANDLE;
15 VkDeviceSize source_offset = 0;
16 VkBuffer scratch = VK_NULL_HANDLE;
17 VkDeviceSize offset = 0; // two input words, then the reduced condition
18 VkDescriptorSet set = VK_NULL_HANDLE;
19};
20
21class PredicateReducer {
22public:
23 PredicateReducer(PFN_vkGetDeviceProcAddr get, VkDevice device, VkPhysicalDeviceMemoryProperties memory,
24 VkDeviceSize max_range) : get_(get), device_(device), memory_(memory), max_range_(max_range) {}
25 ~PredicateReducer() {
26 if (pipeline_) fn<PFN_vkDestroyPipeline>("vkDestroyPipeline")(device_, pipeline_, nullptr);
27 if (layout_) fn<PFN_vkDestroyPipelineLayout>("vkDestroyPipelineLayout")(device_, layout_, nullptr);
28 if (set_layout_) fn<PFN_vkDestroyDescriptorSetLayout>("vkDestroyDescriptorSetLayout")(device_, set_layout_, nullptr);
29 }
30 PredicateReducer(const PredicateReducer&) = delete;
31 PredicateReducer& operator=(const PredicateReducer&) = delete;
32
33 // Owned by a command-buffer slot until its timeline ticket completes (or a failed recording is reset).
34 struct Batch {
35 PredicateReducer& owner;
36 VkBuffer buffer = VK_NULL_HANDLE;
37 VkDeviceMemory memory = VK_NULL_HANDLE;
38 VkDescriptorPool pool = VK_NULL_HANDLE;
39 VkDescriptorSet set = VK_NULL_HANDLE;
40 VkDeviceSize bytes = 0;
41 std::uint32_t count = 0, next = 0;
42 explicit Batch(PredicateReducer& r) : owner(r) {}
43 ~Batch() {
44 if (pool) owner.fn<PFN_vkDestroyDescriptorPool>("vkDestroyDescriptorPool")(owner.device_, pool, nullptr);
45 if (buffer) owner.fn<PFN_vkDestroyBuffer>("vkDestroyBuffer")(owner.device_, buffer, nullptr);
46 if (memory) owner.fn<PFN_vkFreeMemory>("vkFreeMemory")(owner.device_, memory, nullptr);
47 }
48 Batch(const Batch&) = delete;
49 Batch& operator=(const Batch&) = delete;
50 bool prepare(VkBuffer source, VkDeviceSize offset, PredicateCapture& out) {
51 if (next >= count || !source) return false;
52 out = {source, offset, buffer, VkDeviceSize(next++) * 16, set};
53 return true;
54 }
55 };
56
57 std::unique_ptr<Batch> batch(std::uint64_t count) {
58 // One descriptor starts at offset zero; a push constant selects each aligned 16-byte slot.
59 if (!count || count > max_range_ / 16 || count > UINT32_MAX / 4 || !make_pipeline()) return {};
60 auto b = std::make_unique<Batch>(*this);
61 b->count = static_cast<std::uint32_t>(count);
62 b->bytes = count * 16;
63 VkBufferCreateInfo buffer{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
64 buffer.size = b->bytes;
65 buffer.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
66 VK_BUFFER_USAGE_CONDITIONAL_RENDERING_BIT_EXT;
67 if (fn<PFN_vkCreateBuffer>("vkCreateBuffer")(device_, &buffer, nullptr, &b->buffer) != VK_SUCCESS) return {};
68 VkMemoryRequirements requirements{};
69 fn<PFN_vkGetBufferMemoryRequirements>("vkGetBufferMemoryRequirements")(device_, b->buffer, &requirements);
70 std::uint32_t type = UINT32_MAX;
71 for (std::uint32_t i = 0; i < memory_.memoryTypeCount; ++i) {
72 if (!(requirements.memoryTypeBits & (1u << i))) continue;
73 if (type == UINT32_MAX) type = i;
74 if (memory_.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { type = i; break; }
75 }
76 if (type == UINT32_MAX) return {};
77 VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
78 allocation.allocationSize = requirements.size;
79 allocation.memoryTypeIndex = type;
80 if (fn<PFN_vkAllocateMemory>("vkAllocateMemory")(device_, &allocation, nullptr, &b->memory) != VK_SUCCESS ||
81 fn<PFN_vkBindBufferMemory>("vkBindBufferMemory")(device_, b->buffer, b->memory, 0) != VK_SUCCESS) return {};
82 const VkDescriptorPoolSize size{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1};
83 VkDescriptorPoolCreateInfo pool{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
84 pool.maxSets = 1; pool.poolSizeCount = 1; pool.pPoolSizes = &size;
85 if (fn<PFN_vkCreateDescriptorPool>("vkCreateDescriptorPool")(device_, &pool, nullptr, &b->pool) != VK_SUCCESS) return {};
86 VkDescriptorSetAllocateInfo sets{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
87 sets.descriptorPool = b->pool; sets.descriptorSetCount = 1; sets.pSetLayouts = &set_layout_;
88 if (fn<PFN_vkAllocateDescriptorSets>("vkAllocateDescriptorSets")(device_, &sets, &b->set) != VK_SUCCESS) return {};
89 const VkDescriptorBufferInfo info{b->buffer, 0, b->bytes};
90 VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
91 write.dstSet = b->set; write.dstBinding = 0; write.descriptorCount = 1;
92 write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; write.pBufferInfo = &info;
93 fn<PFN_vkUpdateDescriptorSets>("vkUpdateDescriptorSets")(device_, 1, &write, 0, nullptr);
94 return b;
95 }
96
97 void capture(VkCommandBuffer cb, const PredicateCapture& p) const {
98 // End an earlier conditional scope before entering this helper: dispatch is itself conditional.
99 // The descriptor spans the arena, so order all prior accesses before reusing it for another slot.
100 VkMemoryBarrier before{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
101 before.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT | VK_ACCESS_MEMORY_READ_BIT;
102 before.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
103 const auto barrier = fn<PFN_vkCmdPipelineBarrier>("vkCmdPipelineBarrier");
104 barrier(cb, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 1, &before, 0, nullptr, 0, nullptr);
105 const VkBufferCopy copy{p.source_offset, p.offset, 8};
106 fn<PFN_vkCmdCopyBuffer>("vkCmdCopyBuffer")(cb, p.source, p.scratch, 1, &copy);
107 // The application's PREDICATION state does not describe this internal transfer read. Preserve snapshot
108 // semantics when it subsequently transitions and overwrites the source in the same command list.
109 VkBufferMemoryBarrier source_done{VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER};
110 source_done.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
111 source_done.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
112 source_done.srcQueueFamilyIndex = source_done.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
113 source_done.buffer = p.source; source_done.offset = p.source_offset; source_done.size = 8;
114 barrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
115 0, 0, nullptr, 1, &source_done, 0, nullptr);
116 VkMemoryBarrier compute{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
117 compute.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
118 compute.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
119 barrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 1, &compute, 0, nullptr, 0, nullptr);
120 fn<PFN_vkCmdBindPipeline>("vkCmdBindPipeline")(cb, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_);
121 fn<PFN_vkCmdBindDescriptorSets>("vkCmdBindDescriptorSets")(cb, VK_PIPELINE_BIND_POINT_COMPUTE, layout_, 0, 1, &p.set, 0, nullptr);
122 const std::uint32_t index = static_cast<std::uint32_t>(p.offset / 4);
123 fn<PFN_vkCmdPushConstants>("vkCmdPushConstants")(cb, layout_, VK_SHADER_STAGE_COMPUTE_BIT, 0, 4, &index);
124 fn<PFN_vkCmdDispatch>("vkCmdDispatch")(cb, 1, 1, 1);
125 VkMemoryBarrier condition{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
126 condition.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
127 condition.dstAccessMask = VK_ACCESS_CONDITIONAL_RENDERING_READ_BIT_EXT;
128 barrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_CONDITIONAL_RENDERING_BIT_EXT,
129 0, 1, &condition, 0, nullptr, 0, nullptr);
130 }
131
132private:
133 template<class T> T fn(const char* name) const { return reinterpret_cast<T>(get_(device_, name)); }
134 bool make_pipeline() {
135 if (pipeline_) return true;
136 // No entry-point assumptions for partial/mock devices: fail before allocating or recording anything.
137 for (const char* name : {"vkCreateBuffer", "vkDestroyBuffer", "vkGetBufferMemoryRequirements", "vkAllocateMemory",
138 "vkFreeMemory", "vkBindBufferMemory", "vkCreateDescriptorPool", "vkDestroyDescriptorPool",
139 "vkAllocateDescriptorSets", "vkUpdateDescriptorSets", "vkCreateShaderModule", "vkDestroyShaderModule",
140 "vkCreateDescriptorSetLayout", "vkDestroyDescriptorSetLayout", "vkCreatePipelineLayout",
141 "vkDestroyPipelineLayout", "vkCreateComputePipelines", "vkDestroyPipeline", "vkCmdCopyBuffer",
142 "vkCmdPipelineBarrier", "vkCmdBindPipeline", "vkCmdBindDescriptorSets", "vkCmdPushConstants", "vkCmdDispatch"})
143 if (!get_(device_, name)) return false;
144 VkDescriptorSetLayout set = VK_NULL_HANDLE;
145 VkPipelineLayout layout = VK_NULL_HANDLE;
146 VkShaderModule shader = VK_NULL_HANDLE;
147 VkPipeline pipeline = VK_NULL_HANDLE;
148 const VkDescriptorSetLayoutBinding binding{0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
149 VkDescriptorSetLayoutCreateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
150 set_info.bindingCount = 1; set_info.pBindings = &binding;
151 bool ok = fn<PFN_vkCreateDescriptorSetLayout>("vkCreateDescriptorSetLayout")(device_, &set_info, nullptr, &set) == VK_SUCCESS;
152 const VkPushConstantRange push{VK_SHADER_STAGE_COMPUTE_BIT, 0, 4};
153 VkPipelineLayoutCreateInfo layout_info{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
154 layout_info.setLayoutCount = 1; layout_info.pSetLayouts = &set;
155 layout_info.pushConstantRangeCount = 1; layout_info.pPushConstantRanges = &push;
156 ok = ok && fn<PFN_vkCreatePipelineLayout>("vkCreatePipelineLayout")(device_, &layout_info, nullptr, &layout) == VK_SUCCESS;
157 VkShaderModuleCreateInfo module{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
158 module.codeSize = sizeof(predicate_reduce_spirv); module.pCode = predicate_reduce_spirv;
159 ok = ok && fn<PFN_vkCreateShaderModule>("vkCreateShaderModule")(device_, &module, nullptr, &shader) == VK_SUCCESS;
160 VkComputePipelineCreateInfo info{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO};
161 info.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO};
162 info.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; info.stage.module = shader; info.stage.pName = "main";
163 info.layout = layout;
164 ok = ok && fn<PFN_vkCreateComputePipelines>("vkCreateComputePipelines")(device_, VK_NULL_HANDLE, 1, &info, nullptr, &pipeline) == VK_SUCCESS;
165 if (shader) fn<PFN_vkDestroyShaderModule>("vkDestroyShaderModule")(device_, shader, nullptr);
166 if (!ok) {
167 if (pipeline) fn<PFN_vkDestroyPipeline>("vkDestroyPipeline")(device_, pipeline, nullptr);
168 if (layout) fn<PFN_vkDestroyPipelineLayout>("vkDestroyPipelineLayout")(device_, layout, nullptr);
169 if (set) fn<PFN_vkDestroyDescriptorSetLayout>("vkDestroyDescriptorSetLayout")(device_, set, nullptr);
170 return false;
171 }
172 set_layout_ = set; layout_ = layout; pipeline_ = pipeline;
173 return true;
174 }
175 PFN_vkGetDeviceProcAddr get_;
176 VkDevice device_;
177 VkPhysicalDeviceMemoryProperties memory_;
178 VkDeviceSize max_range_;
179 VkDescriptorSetLayout set_layout_ = VK_NULL_HANDLE;
180 VkPipelineLayout layout_ = VK_NULL_HANDLE;
181 VkPipeline pipeline_ = VK_NULL_HANDLE;
182};
183} // namespace leonida
diff --git a/runtime/src/vulkan_predicated_copy.h b/runtime/src/vulkan_predicated_copy.h
new file mode 100644
index 000000000..6a225ced7
--- /dev/null
+++ b/runtime/src/vulkan_predicated_copy.h
@@ -0,0 +1,154 @@
1// Byte-granular conditional CopyBufferRegion. Requires enabled storageBuffer8BitAccess, not shaderInt8.
2#pragma once
3#ifndef VK_NO_PROTOTYPES
4#define VK_NO_PROTOTYPES
5#endif
6#include <vulkan/vulkan.h>
7#include <algorithm>
8#include <cstdint>
9#include <memory>
10#include <initializer_list>
11#include <vector>
12#include "predicated_copy_spv.h"
13namespace leonida {
14struct PredicatedCopyDispatch {
15 VkDescriptorSet set = VK_NULL_HANDLE;
16 std::uint32_t source = 0, destination = 0, bytes = 0;
17};
18class PredicatedBufferCopier {
19public:
20 PredicatedBufferCopier(PFN_vkGetDeviceProcAddr get, VkDevice device, const VkPhysicalDeviceLimits& limits)
21 : get_(get), device_(device), limits_(limits) {}
22 ~PredicatedBufferCopier() {
23 if(pipeline_)fn<PFN_vkDestroyPipeline>("vkDestroyPipeline")(device_,pipeline_,nullptr);
24 if(layout_)fn<PFN_vkDestroyPipelineLayout>("vkDestroyPipelineLayout")(device_,layout_,nullptr);
25 if(set_layout_)fn<PFN_vkDestroyDescriptorSetLayout>("vkDestroyDescriptorSetLayout")(device_,set_layout_,nullptr);
26 }
27 PredicatedBufferCopier(const PredicatedBufferCopier&)=delete;
28 PredicatedBufferCopier& operator=(const PredicatedBufferCopier&)=delete;
29 struct Batch {
30 PredicatedBufferCopier& owner;
31 std::vector<VkDescriptorPool> pools;
32 explicit Batch(PredicatedBufferCopier& value):owner(value){}
33 ~Batch(){for(auto pool:pools)owner.fn<PFN_vkDestroyDescriptorPool>("vkDestroyDescriptorPool")(owner.device_,pool,nullptr);}
34 bool prepare(VkBuffer source,VkBuffer destination,VkDeviceSize src,VkDeviceSize dst,VkDeviceSize bytes,
35 std::vector<PredicatedCopyDispatch>& out) {
36 out.clear();
37 if(!bytes)return true;
38 if(!source||!destination||bytes>UINT64_MAX-src||bytes>UINT64_MAX-dst)return false;
39 if(source==destination && src<dst+bytes && dst<src+bytes)return false;
40 if(!owner.make_pipeline())return false;
41 // Align each descriptor base down, keeping byte prefixes in push constants. Split at the smaller
42 // remaining descriptor range or dispatch limit, so no uint32 shader index can wrap or overread.
43 struct Window{VkDescriptorBufferInfo source,destination;std::uint32_t src,dst,bytes;};
44 std::vector<Window> windows;
45 const auto alignment=std::max<VkDeviceSize>(1,owner.limits_.minStorageBufferOffsetAlignment);
46 const VkDeviceSize maximum=owner.limits_.maxStorageBufferRange;
47 const VkDeviceSize threads=VkDeviceSize(owner.limits_.maxComputeWorkGroupCount[0])*64;
48 for(VkDeviceSize done=0;done<bytes;){
49 const auto s=src+done,d=dst+done,sp=s%alignment,dp=d%alignment;
50 if(sp>=maximum||dp>=maximum||!threads)return false;
51 const auto n=std::min({bytes-done,maximum-sp,maximum-dp,threads});
52 if(!n||n>UINT32_MAX)return false;
53 windows.push_back({{source,s-sp,sp+n},{destination,d-dp,dp+n},
54 static_cast<std::uint32_t>(sp),static_cast<std::uint32_t>(dp),static_cast<std::uint32_t>(n)});
55 if(windows.size()>UINT32_MAX/2)return false;
56 done+=n;
57 }
58 const auto count=static_cast<std::uint32_t>(windows.size());
59 const VkDescriptorPoolSize size{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,count*2};
60 VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
61 pool_info.maxSets=count;pool_info.poolSizeCount=1;pool_info.pPoolSizes=&size;
62 VkDescriptorPool pool=VK_NULL_HANDLE;
63 if(owner.fn<PFN_vkCreateDescriptorPool>("vkCreateDescriptorPool")(owner.device_,&pool_info,nullptr,&pool)!=VK_SUCCESS)return false;
64 // The batch is retained with the command buffer until its timeline ticket completes.
65 pools.push_back(pool);
66 const std::vector<VkDescriptorSetLayout> layouts(count,owner.set_layout_);
67 std::vector<VkDescriptorSet> sets(count);
68 VkDescriptorSetAllocateInfo allocate{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
69 allocate.descriptorPool=pool;allocate.descriptorSetCount=count;allocate.pSetLayouts=layouts.data();
70 if(owner.fn<PFN_vkAllocateDescriptorSets>("vkAllocateDescriptorSets")(owner.device_,&allocate,sets.data())!=VK_SUCCESS)return false;
71 for(std::uint32_t i=0;i<count;++i){
72 VkWriteDescriptorSet writes[2]{};
73 const VkDescriptorBufferInfo* infos[]={&windows[i].source,&windows[i].destination};
74 for(std::uint32_t binding=0;binding<2;++binding){
75 auto& w=writes[binding];w.sType=VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;w.dstSet=sets[i];
76 w.dstBinding=binding;w.descriptorCount=1;w.descriptorType=VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;w.pBufferInfo=infos[binding];
77 }
78 owner.fn<PFN_vkUpdateDescriptorSets>("vkUpdateDescriptorSets")(owner.device_,2,writes,0,nullptr);
79 out.push_back({sets[i],windows[i].src,windows[i].dst,windows[i].bytes});
80 }
81 return true;
82 }
83 };
84 std::unique_ptr<Batch> batch(){return std::make_unique<Batch>(*this);}
85 void emit(VkCommandBuffer cb,const std::vector<PredicatedCopyDispatch>& jobs)const{
86 const auto barrier=fn<PFN_vkCmdPipelineBarrier>("vkCmdPipelineBarrier");
87 // Barriers are unconditional even when the dispatch is skipped by conditional rendering. They order
88 // transfer producers, dependent copies, and adjacent windows whose aligned descriptors may overlap.
89 for(const auto& job:jobs){
90 VkMemoryBarrier before{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
91 before.srcAccessMask=VK_ACCESS_MEMORY_WRITE_BIT|VK_ACCESS_MEMORY_READ_BIT;
92 before.dstAccessMask=VK_ACCESS_SHADER_READ_BIT|VK_ACCESS_SHADER_WRITE_BIT;
93 barrier(cb,VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,0,1,&before,0,nullptr,0,nullptr);
94 fn<PFN_vkCmdBindPipeline>("vkCmdBindPipeline")(cb,VK_PIPELINE_BIND_POINT_COMPUTE,pipeline_);
95 fn<PFN_vkCmdBindDescriptorSets>("vkCmdBindDescriptorSets")(cb,VK_PIPELINE_BIND_POINT_COMPUTE,layout_,0,1,&job.set,0,nullptr);
96 const std::uint32_t push[]={job.source,job.destination,job.bytes};
97 fn<PFN_vkCmdPushConstants>("vkCmdPushConstants")(cb,layout_,VK_SHADER_STAGE_COMPUTE_BIT,0,sizeof(push),push);
98 fn<PFN_vkCmdDispatch>("vkCmdDispatch")(cb,(job.bytes-1u)/64u+1u,1,1);
99 VkMemoryBarrier after{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
100 after.srcAccessMask=VK_ACCESS_SHADER_WRITE_BIT|VK_ACCESS_SHADER_READ_BIT;
101 after.dstAccessMask=VK_ACCESS_MEMORY_READ_BIT|VK_ACCESS_MEMORY_WRITE_BIT;
102 barrier(cb,VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,0,1,&after,0,nullptr,0,nullptr);
103 }
104 }
105private:
106 template<class T>T fn(const char* name)const{return reinterpret_cast<T>(get_(device_,name));}
107 bool make_pipeline() {
108 if (pipeline_) return true;
109 // No entry-point assumptions for partial/mock devices: fail before allocating or recording anything.
110 for (const char* name : {"vkCreateDescriptorPool", "vkDestroyDescriptorPool",
111 "vkAllocateDescriptorSets", "vkUpdateDescriptorSets", "vkCreateShaderModule", "vkDestroyShaderModule",
112 "vkCreateDescriptorSetLayout", "vkDestroyDescriptorSetLayout", "vkCreatePipelineLayout",
113 "vkDestroyPipelineLayout", "vkCreateComputePipelines", "vkDestroyPipeline",
114 "vkCmdPipelineBarrier", "vkCmdBindPipeline", "vkCmdBindDescriptorSets", "vkCmdPushConstants", "vkCmdDispatch"})
115 if (!get_(device_, name)) return false;
116 VkDescriptorSetLayout set = VK_NULL_HANDLE;
117 VkPipelineLayout layout = VK_NULL_HANDLE;
118 VkShaderModule shader = VK_NULL_HANDLE;
119 VkPipeline pipeline = VK_NULL_HANDLE;
120 const VkDescriptorSetLayoutBinding bindings[]={{0,VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,1,VK_SHADER_STAGE_COMPUTE_BIT,nullptr},{1,VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,1,VK_SHADER_STAGE_COMPUTE_BIT,nullptr}};
121 VkDescriptorSetLayoutCreateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
122 set_info.bindingCount = 2; set_info.pBindings = bindings;
123 bool ok = fn<PFN_vkCreateDescriptorSetLayout>("vkCreateDescriptorSetLayout")(device_, &set_info, nullptr, &set) == VK_SUCCESS;
124 const VkPushConstantRange push{VK_SHADER_STAGE_COMPUTE_BIT, 0, 12};
125 VkPipelineLayoutCreateInfo layout_info{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
126 layout_info.setLayoutCount = 1; layout_info.pSetLayouts = &set;
127 layout_info.pushConstantRangeCount = 1; layout_info.pPushConstantRanges = &push;
128 ok = ok && fn<PFN_vkCreatePipelineLayout>("vkCreatePipelineLayout")(device_, &layout_info, nullptr, &layout) == VK_SUCCESS;
129 VkShaderModuleCreateInfo module{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
130 module.codeSize = sizeof(predicated_copy_spirv); module.pCode = predicated_copy_spirv;
131 ok = ok && fn<PFN_vkCreateShaderModule>("vkCreateShaderModule")(device_, &module, nullptr, &shader) == VK_SUCCESS;
132 VkComputePipelineCreateInfo info{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO};
133 info.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO};
134 info.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; info.stage.module = shader; info.stage.pName = "main";
135 info.layout = layout;
136 ok = ok && fn<PFN_vkCreateComputePipelines>("vkCreateComputePipelines")(device_, VK_NULL_HANDLE, 1, &info, nullptr, &pipeline) == VK_SUCCESS;
137 if (shader) fn<PFN_vkDestroyShaderModule>("vkDestroyShaderModule")(device_, shader, nullptr);
138 if (!ok) {
139 if (pipeline) fn<PFN_vkDestroyPipeline>("vkDestroyPipeline")(device_, pipeline, nullptr);
140 if (layout) fn<PFN_vkDestroyPipelineLayout>("vkDestroyPipelineLayout")(device_, layout, nullptr);
141 if (set) fn<PFN_vkDestroyDescriptorSetLayout>("vkDestroyDescriptorSetLayout")(device_, set, nullptr);
142 return false;
143 }
144 set_layout_ = set; layout_ = layout; pipeline_ = pipeline;
145 return true;
146 }
147 PFN_vkGetDeviceProcAddr get_;
148 VkDevice device_;
149 VkPhysicalDeviceLimits limits_;
150 VkDescriptorSetLayout set_layout_=VK_NULL_HANDLE;
151 VkPipelineLayout layout_=VK_NULL_HANDLE;
152 VkPipeline pipeline_=VK_NULL_HANDLE;
153};
154}
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h
index f5d428e7a..8c34a1000 100644
--- a/runtime/src/vulkan_replay.h
+++ b/runtime/src/vulkan_replay.h
@@ -24,6 +24,8 @@
24// rectangles need color_attachment usage on the image (ResourceBinding::color_attachment); 24// rectangles need color_attachment usage on the image (ResourceBinding::color_attachment);
25// - query commands need the query functions, fill_buffer and query_pool; resolve destinations have TRANSFER_DST 25// - query commands need the query functions, fill_buffer and query_pool; resolve destinations have TRANSFER_DST
26// usage and binary occlusion resolves need resolve_binary_occlusion. 26// usage and binary occlusion resolves need resolve_binary_occlusion.
27// - predicated buffer copies need the byte-copy helper and buffers with STORAGE_BUFFER usage; helper descriptors
28// must remain alive through submission completion (VulkanQueue owns them).
27// A list that needs a missing function fails with ReplayResult::unsupported before anything is written. 29// A list that needs a missing function fails with ReplayResult::unsupported before anything is written.
28 30
29#include "command_list.h" 31#include "command_list.h"
@@ -31,6 +33,8 @@
31#include "uav_clear.h" 33#include "uav_clear.h"
32 34
33#include <vulkan/vulkan.h> 35#include <vulkan/vulkan.h>
36#include "vulkan_predicate.h"
37#include "vulkan_predicated_copy.h"
34 38
35#include <algorithm> 39#include <algorithm>
36#include <array> 40#include <array>
@@ -730,6 +734,12 @@ struct ReplayFunctions {
730 // Optional VK_KHR_acceleration_structure: BuildRaytracingAccelerationStructure. 734 // Optional VK_KHR_acceleration_structure: BuildRaytracingAccelerationStructure.
731 PFN_vkCmdBuildAccelerationStructuresKHR build_acceleration_structures = nullptr; 735 PFN_vkCmdBuildAccelerationStructuresKHR build_acceleration_structures = nullptr;
732 std::function<VkAccelerationStructureKHR(std::uint64_t)> resolve_acceleration_structure; 736 std::function<VkAccelerationStructureKHR(std::uint64_t)> resolve_acceleration_structure;
737 // Preparation only allocates a slot; capture records the GPU snapshot at the SetPredication command.
738 std::function<bool(VkBuffer, VkDeviceSize, PredicateCapture&)> prepare_predicate;
739 std::function<void(VkCommandBuffer, const PredicateCapture&)> capture_predicate;
740 std::function<bool(VkBuffer, VkBuffer, VkDeviceSize, VkDeviceSize, VkDeviceSize,
741 std::vector<PredicatedCopyDispatch>&)> prepare_predicated_copy;
742 std::function<void(VkCommandBuffer, const std::vector<PredicatedCopyDispatch>&)> emit_predicated_copy;
733 // Returns a 2D-array view of the range (caller-owned, alive until execution completes); rectangle clears. 743 // Returns a 2D-array view of the range (caller-owned, alive until execution completes); rectangle clears.
734 std::function<VkImageView(const ResourceBinding&, const VkImageSubresourceRange&)> image_view; 744 std::function<VkImageView(const ResourceBinding&, const VkImageSubresourceRange&)> image_view;
735 // Footprint copies of a depth24_as_float texture (depth24.h): `depth24_prepare` makes the scratch and descriptors 745 // Footprint copies of a depth24_as_float texture (depth24.h): `depth24_prepare` makes the scratch and descriptors
@@ -776,6 +786,8 @@ struct PreparedQuery {
776}; 786};
777 787
778struct Prepared { 788struct Prepared {
789 PredicateCapture predicate;
790 std::vector<PredicatedCopyDispatch> predicated_copy;
779 ResourceBinding binding; 791 ResourceBinding binding;
780 VkImageSubresourceRange range{}; // the barrier range, or the cleared view (cleared aspects only) 792 VkImageSubresourceRange range{}; // the barrier range, or the cleared view (cleared aspects only)
781 VkImageView view = VK_NULL_HANDLE; 793 VkImageView view = VK_NULL_HANDLE;
@@ -1794,13 +1806,14 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
1794 } else if (std::holds_alternative<CopyBuffer>(command) || std::holds_alternative<CopyTexture>(command) || 1806 } else if (std::holds_alternative<CopyBuffer>(command) || std::holds_alternative<CopyTexture>(command) ||
1795 std::holds_alternative<CopyResource>(command)) { 1807 std::holds_alternative<CopyResource>(command)) {
1796 if (!copies) return ReplayResult::unsupported; 1808 if (!copies) return ReplayResult::unsupported;
1797 if (predicated) ++out.unpredicated_copies; 1809 if (predicated && !std::holds_alternative<CopyBuffer>(command)) ++out.unpredicated_copies;
1798 const auto [dst_id, src_id] = copy_ids(command); 1810 const auto [dst_id, src_id] = copy_ids(command);
1799 const ResourceBinding* source = resolve(src_id); 1811 const ResourceBinding* source = resolve(src_id);
1800 if (!bind(dst_id) || !source) return ReplayResult::unresolved_resource; 1812 if (!bind(dst_id) || !source) return ReplayResult::unresolved_resource;
1801 if (p.binding.host_readable) { 1813 if (p.binding.host_readable) {
1802 out.wrote_readback = true; 1814 out.wrote_readback = true;
1803 out.readback_src_stages |= VK_PIPELINE_STAGE_2_COPY_BIT; 1815 out.readback_src_stages |= predicated && std::holds_alternative<CopyBuffer>(command)
1816 ? VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT : VK_PIPELINE_STAGE_2_COPY_BIT;
1804 } 1817 }
1805 p.source = *source; 1818 p.source = *source;
1806 ReplayResult result = ReplayResult::success; 1819 ReplayResult result = ReplayResult::success;
@@ -1809,8 +1822,19 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
1809 !detail::within(buffer_copy->dst_offset, buffer_copy->size, p.binding.size) || 1822 !detail::within(buffer_copy->dst_offset, buffer_copy->size, p.binding.size) ||
1810 !detail::within(buffer_copy->src_offset, buffer_copy->size, p.source.size)) 1823 !detail::within(buffer_copy->src_offset, buffer_copy->size, p.source.size))
1811 return ReplayResult::invalid_copy; 1824 return ReplayResult::invalid_copy;
1812 p.buffer_copies.push_back({p.source.buffer_offset + buffer_copy->src_offset, 1825 if (buffer_copy->src_offset > UINT64_MAX - p.source.buffer_offset ||
1813 p.binding.buffer_offset + buffer_copy->dst_offset, buffer_copy->size}); 1826 buffer_copy->dst_offset > UINT64_MAX - p.binding.buffer_offset) return ReplayResult::invalid_copy;
1827 const VkDeviceSize src = p.source.buffer_offset + buffer_copy->src_offset;
1828 const VkDeviceSize dst = p.binding.buffer_offset + buffer_copy->dst_offset;
1829 if (buffer_copy->size > UINT64_MAX - src || buffer_copy->size > UINT64_MAX - dst)
1830 return ReplayResult::invalid_copy;
1831 if (p.source.buffer == p.binding.buffer && src < dst + buffer_copy->size && dst < src + buffer_copy->size)
1832 return ReplayResult::invalid_copy;
1833 if (predicated) {
1834 if (statistics_active || !vk.prepare_predicated_copy || !vk.emit_predicated_copy ||
1835 !vk.prepare_predicated_copy(p.source.buffer, p.binding.buffer, src, dst, buffer_copy->size, p.predicated_copy))
1836 return ReplayResult::unsupported;
1837 } else p.buffer_copies.push_back({src, dst, buffer_copy->size});
1814 } else if (const auto* texture_copy = std::get_if<CopyTexture>(&command)) { 1838 } else if (const auto* texture_copy = std::get_if<CopyTexture>(&command)) {
1815 result = detail::prepare_texture_copy(*texture_copy, p); 1839 result = detail::prepare_texture_copy(*texture_copy, p);
1816 // A depth plane footprint of a D24 texture stored as D32: words of 24-bit UNORM, floats in the image. 1840 // A depth plane footprint of a D24 texture stored as D32: words of 24-bit UNORM, floats in the image.
@@ -1903,11 +1927,16 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
1903 predicated = predication->buffer != 0; 1927 predicated = predication->buffer != 0;
1904 if (!predicated) continue; 1928 if (!predicated) continue;
1905 if (!conditional) return ReplayResult::unsupported; 1929 if (!conditional) return ReplayResult::unsupported;
1930 // The internal dispatch must not appear in the application's pipeline-statistics result. Virtualising
1931 // that query is separate work; reject the combination before emitting any commands instead of lying.
1932 if (statistics_active) return ReplayResult::unsupported;
1906 if (!bind(predication->buffer)) return ReplayResult::unresolved_resource; 1933 if (!bind(predication->buffer)) return ReplayResult::unresolved_resource;
1907 // Vulkan reads one 32-bit word at a 4-byte aligned offset; D3D12's value is 64 bits at an 8-byte 1934 if (!p.binding.buffer || !detail::within(predication->offset, 8, p.binding.size) ||
1908 // aligned one, so its low word is read (docs/d3d12_vulkan.md section 4.7). 1935 predication->offset > UINT64_MAX - p.binding.buffer_offset)
1909 if (!p.binding.buffer || !detail::within(predication->offset, 4, p.binding.size))
1910 return ReplayResult::invalid_predication; 1936 return ReplayResult::invalid_predication;
1937 if (!vk.prepare_predicate || !vk.capture_predicate ||
1938 !vk.prepare_predicate(p.binding.buffer, p.binding.buffer_offset + predication->offset, p.predicate))
1939 return ReplayResult::unsupported;
1911 } else if (const QueryRange range = query_range(command); range.count) { 1940 } else if (const QueryRange range = query_range(command); range.count) {
1912 // BeginQuery, EndQuery and ResolveQueryData are not among D3D12's predicated calls, and Vulkan's 1941 // BeginQuery, EndQuery and ResolveQueryData are not among D3D12's predicated calls, and Vulkan's
1913 // conditional rendering leaves query commands and transfers alone, so a predicate changes nothing here. 1942 // conditional rendering leaves query commands and transfers alone, so a predicate changes nothing here.
@@ -2171,10 +2200,12 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
2171 if (const auto* predication = std::get_if<Predication>(&command)) { 2200 if (const auto* predication = std::get_if<Predication>(&command)) {
2172 // Conditional rendering cannot nest, so a new predicate ends the previous one first. 2201 // Conditional rendering cannot nest, so a new predicate ends the previous one first.
2173 if (predicate.buffer) vk.end_conditional_rendering(command_buffer); 2202 if (predicate.buffer) vk.end_conditional_rendering(command_buffer);
2174 predicate.buffer = p.binding.buffer; 2203 predicate.buffer = predication->buffer ? p.predicate.scratch : VK_NULL_HANDLE;
2175 if (predicate.buffer) { 2204 if (predicate.buffer) {
2176 // A default-heap buffer is a range of the GPU address arena's VkBuffer, starting at buffer_offset. 2205 // The word was captured from the resource at prepare's offset (buffer_offset + the command's, so a
2177 predicate.offset = p.binding.buffer_offset + predication->offset; 2206 // default-heap buffer inside the GPU address arena reads its own range); the test reads the capture.
2207 vk.capture_predicate(command_buffer, p.predicate);
2208 predicate.offset = p.predicate.offset + 8;
2178 // Vulkan discards the commands when the word is 0, which is EQUAL_ZERO; INVERTED gives NOT_EQUAL_ZERO. 2209 // Vulkan discards the commands when the word is 0, which is EQUAL_ZERO; INVERTED gives NOT_EQUAL_ZERO.
2179 predicate.flags = predication->op == d3d12_predication::not_equal_zero 2210 predicate.flags = predication->op == d3d12_predication::not_equal_zero
2180 ? VK_CONDITIONAL_RENDERING_INVERTED_BIT_EXT 2211 ? VK_CONDITIONAL_RENDERING_INVERTED_BIT_EXT
@@ -2410,7 +2441,9 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
2410 } else if (std::holds_alternative<CopyBuffer>(command) || std::holds_alternative<CopyTexture>(command) || 2441 } else if (std::holds_alternative<CopyBuffer>(command) || std::holds_alternative<CopyTexture>(command) ||
2411 std::holds_alternative<CopyResource>(command)) { 2442 std::holds_alternative<CopyResource>(command)) {
2412 const auto [dst_id, src_id] = copy_ids(command); 2443 const auto [dst_id, src_id] = copy_ids(command);
2413 if (!p.buffer_copies.empty()) { 2444 if (!p.predicated_copy.empty()) {
2445 vk.emit_predicated_copy(command_buffer, p.predicated_copy);
2446 } else if (!p.buffer_copies.empty()) {
2414 vk.copy_buffer(command_buffer, p.source.buffer, p.binding.buffer, std::uint32_t(p.buffer_copies.size()), 2447 vk.copy_buffer(command_buffer, p.source.buffer, p.binding.buffer, std::uint32_t(p.buffer_copies.size()),
2415 p.buffer_copies.data()); 2448 p.buffer_copies.data());
2416 } else if (!p.image_copies.empty()) { 2449 } else if (!p.image_copies.empty()) {