diff options
| author | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 11:58:17 +0100 |
|---|---|---|
| committer | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 11:58:17 +0100 |
| commit | 17ef3d0cfbb0d9dfa6c10e2229032c959008fa05 (patch) | |
| tree | 2bd55dd353f65325c22aa42153e0436435801f62 /runtime/src | |
| parent | db7538e8f0b37075c13ed2f05a30499bdb463781 (diff) | |
| parent | c2967a5e60060e4068fb992196d8f4b494a5046c (diff) | |
chore: integrate upstream after the explicit-gather revert
Keep the corrected texture descriptor while bringing the branch onto the maintainer's post-revert tree.
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/depth24.h | 9 | ||||
| -rw-r--r-- | runtime/src/depth24_convert.comp | 22 | ||||
| -rw-r--r-- | runtime/src/depth24_convert.inc | 318 | ||||
| -rw-r--r-- | runtime/src/frame_pacer.h | 374 | ||||
| -rw-r--r-- | runtime/src/main.cpp | 6 | ||||
| -rw-r--r-- | runtime/src/vulkan_backend.h | 1 | ||||
| -rw-r--r-- | runtime/src/vulkan_depth24.h | 32 | ||||
| -rw-r--r-- | runtime/src/vulkan_present.h | 700 | ||||
| -rw-r--r-- | runtime/src/vulkan_replay.h | 148 |
9 files changed, 1448 insertions, 162 deletions
diff --git a/runtime/src/depth24.h b/runtime/src/depth24.h index ced51bcb1..b9100297b 100644 --- a/runtime/src/depth24.h +++ b/runtime/src/depth24.h | |||
| @@ -50,8 +50,15 @@ inline float depth24_clear_value(float value) { return depth24_to_float(depth24_ | |||
| 50 | // One copy between a footprint (words of 24-bit depth) and the D32 aspect of a texture, as the compute pass sees it. | 50 | // One copy between a footprint (words of 24-bit depth) and the D32 aspect of a texture, as the compute pass sees it. |
| 51 | // The pass converts between the application's buffer and a tight scratch buffer of floats; a copy command moves the | 51 | // The pass converts between the application's buffer and a tight scratch buffer of floats; a copy command moves the |
| 52 | // scratch to or from the image. vulkan_depth24.h fills the scratch and descriptor fields in `prepare`. | 52 | // scratch to or from the image. vulkan_depth24.h fills the scratch and descriptor fields in `prepare`. |
| 53 | // | ||
| 54 | // The same pass, scratch and job repack 96-bit R32G32B32 texels (`rgb96`, D3D-62): the footprint then has three | ||
| 55 | // words a texel and the scratch four (the layer's R32G32B32A32 image), copied as they are. The row and slice | ||
| 56 | // distances stay in words; only the texel's width differs (app_texel_words, scratch_texel_words). | ||
| 53 | struct Depth24Job { | 57 | struct Depth24Job { |
| 54 | bool to_float = false; // an upload (D24 words to floats); false: a readback (floats to words) | 58 | bool to_float = false; // an upload (D24 words to floats); false: a readback (floats to words) |
| 59 | bool rgb96 = false; // 96-bit texels: `to_float` is only the direction (true: the upload) | ||
| 60 | std::uint32_t app_texel_words() const { return rgb96 ? 3 : 1; } | ||
| 61 | std::uint32_t scratch_texel_words() const { return rgb96 ? 4 : 1; } | ||
| 55 | VkBuffer app = VK_NULL_HANDLE; // the application's buffer | 62 | VkBuffer app = VK_NULL_HANDLE; // the application's buffer |
| 56 | std::uint64_t app_offset = 0; // bytes, a multiple of 4 | 63 | std::uint64_t app_offset = 0; // bytes, a multiple of 4 |
| 57 | std::uint64_t app_size = 0; // bytes in the buffer | 64 | std::uint64_t app_size = 0; // bytes in the buffer |
| @@ -59,7 +66,7 @@ struct Depth24Job { | |||
| 59 | std::uint32_t app_row_words = 0; // words between rows and between slices of the footprint | 66 | std::uint32_t app_row_words = 0; // words between rows and between slices of the footprint |
| 60 | std::uint32_t app_slice_words = 0; | 67 | std::uint32_t app_slice_words = 0; |
| 61 | // Set by prepare: | 68 | // Set by prepare: |
| 62 | VkBuffer scratch = VK_NULL_HANDLE; // width * height * slices floats, tightly packed | 69 | VkBuffer scratch = VK_NULL_HANDLE; // width * height * slices texels (floats, or four words), tightly packed |
| 63 | VkDeviceSize scratch_offset = 0; | 70 | VkDeviceSize scratch_offset = 0; |
| 64 | VkDescriptorSet set = VK_NULL_HANDLE; | 71 | VkDescriptorSet set = VK_NULL_HANDLE; |
| 65 | std::uint32_t app_base_words = 0; // the region's first word in the descriptor's window | 72 | std::uint32_t app_base_words = 0; // the region's first word in the descriptor's window |
diff --git a/runtime/src/depth24_convert.comp b/runtime/src/depth24_convert.comp index 688f68d78..822a67459 100644 --- a/runtime/src/depth24_convert.comp +++ b/runtime/src/depth24_convert.comp | |||
| @@ -7,6 +7,11 @@ | |||
| 7 | // NaN and negatives 0, 1 and above 16777215), the unspecified top byte written as 0 | 7 | // NaN and negatives 0, 1 and above 16777215), the unspecified top byte written as 0 |
| 8 | // The upload direction is integer arithmetic (no float division, whose rounding is the GPU's choice); the readback | 8 | // The upload direction is integer arithmetic (no float division, whose rounding is the GPU's choice); the readback |
| 9 | // uses one float multiply, which Vulkan requires to be correctly rounded, and roundEven. | 9 | // uses one float multiply, which Vulkan requires to be correctly rounded, and roundEven. |
| 10 | // | ||
| 11 | // The same pass repacks 96-bit R32G32B32 texels, which the layer holds in R32G32B32A32 images (D3D-62; `to_float` | ||
| 12 | // 2 and 3). A footprint texel is then three words and a scratch texel four, and the words are copied as they are: | ||
| 13 | // to_float == 3 (upload): scratch[s .. s+2] = app[a .. a+2], scratch[s+3] = 0 (never read: views read alpha as one) | ||
| 14 | // to_float == 2 (readback): app[a .. a+2] = scratch[s .. s+2] | ||
| 10 | #version 450 | 15 | #version 450 |
| 11 | 16 | ||
| 12 | layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; | 17 | layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; |
| @@ -16,7 +21,7 @@ layout(set = 0, binding = 1) buffer Scratch { uint scratch[]; }; | |||
| 16 | 21 | ||
| 17 | layout(push_constant) uniform Push { | 22 | layout(push_constant) uniform Push { |
| 18 | uint width, height, slices; | 23 | uint width, height, slices; |
| 19 | uint to_float; | 24 | uint to_float; // 0 and 1: D24 words and floats; 2 and 3: 96-bit texels of 3 and 4 words (see above) |
| 20 | uint app_base; // first word of the region in `app`, within the bound window | 25 | uint app_base; // first word of the region in `app`, within the bound window |
| 21 | uint app_row; // words between rows and between slices of the app buffer | 26 | uint app_row; // words between rows and between slices of the app buffer |
| 22 | uint app_slice; | 27 | uint app_slice; |
| @@ -46,6 +51,21 @@ uint depth24_from_bits(uint bits) { | |||
| 46 | void main() { | 51 | void main() { |
| 47 | uvec3 g = gl_GlobalInvocationID; | 52 | uvec3 g = gl_GlobalInvocationID; |
| 48 | if (g.x >= pc.width || g.y >= pc.height || g.z >= pc.slices) return; | 53 | if (g.x >= pc.width || g.y >= pc.height || g.z >= pc.slices) return; |
| 54 | if (pc.to_float >= 2u) { | ||
| 55 | uint a = pc.app_base + g.z * pc.app_slice + g.y * pc.app_row + g.x * 3u; | ||
| 56 | uint s = pc.scratch_base + ((g.z * pc.height + g.y) * pc.width + g.x) * 4u; | ||
| 57 | if (pc.to_float == 3u) { | ||
| 58 | scratch[s] = app[a]; | ||
| 59 | scratch[s + 1u] = app[a + 1u]; | ||
| 60 | scratch[s + 2u] = app[a + 2u]; | ||
| 61 | scratch[s + 3u] = 0u; | ||
| 62 | } else { | ||
| 63 | app[a] = scratch[s]; | ||
| 64 | app[a + 1u] = scratch[s + 1u]; | ||
| 65 | app[a + 2u] = scratch[s + 2u]; | ||
| 66 | } | ||
| 67 | return; | ||
| 68 | } | ||
| 49 | uint a = pc.app_base + g.z * pc.app_slice + g.y * pc.app_row + g.x; | 69 | uint a = pc.app_base + g.z * pc.app_slice + g.y * pc.app_row + g.x; |
| 50 | uint s = pc.scratch_base + (g.z * pc.height + g.y) * pc.width + g.x; | 70 | uint s = pc.scratch_base + (g.z * pc.height + g.y) * pc.width + g.x; |
| 51 | if (pc.to_float != 0u) scratch[s] = floatBitsToUint(depth24_to_float(app[a] & 0xffffffu)); | 71 | if (pc.to_float != 0u) scratch[s] = floatBitsToUint(depth24_to_float(app[a] & 0xffffffu)); |
diff --git a/runtime/src/depth24_convert.inc b/runtime/src/depth24_convert.inc index 9438af65b..155b6b22f 100644 --- a/runtime/src/depth24_convert.inc +++ b/runtime/src/depth24_convert.inc | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | {0x07230203,0x00010300,0x000d000b,0x0000010a, | 1 | {0x07230203,0x00010300,0x000d000b,0x0000016d, |
| 2 | 0x00000000,0x00020011,0x00000001,0x0006000b, | 2 | 0x00000000,0x00020011,0x00000001,0x0006000b, |
| 3 | 0x00000001,0x4c534c47,0x6474732e,0x3035342e, | 3 | 0x00000001,0x4c534c47,0x6474732e,0x3035342e, |
| 4 | 0x00000000,0x0003000e,0x00000000,0x00000001, | 4 | 0x00000000,0x0003000e,0x00000000,0x00000001, |
| @@ -17,17 +17,17 @@ | |||
| 17 | 0x00000014,0x00050048,0x00000051,0x00000006, | 17 | 0x00000014,0x00050048,0x00000051,0x00000006, |
| 18 | 0x00000023,0x00000018,0x00050048,0x00000051, | 18 | 0x00000023,0x00000018,0x00050048,0x00000051, |
| 19 | 0x00000007,0x00000023,0x0000001c,0x00040047, | 19 | 0x00000007,0x00000023,0x0000001c,0x00040047, |
| 20 | 0x0000009f,0x00000006,0x00000004,0x00030047, | 20 | 0x000000a8,0x00000006,0x00000004,0x00030047, |
| 21 | 0x000000a0,0x00000002,0x00050048,0x000000a0, | 21 | 0x000000a9,0x00000002,0x00050048,0x000000a9, |
| 22 | 0x00000000,0x00000023,0x00000000,0x00040047, | 22 | 0x00000000,0x00000023,0x00000000,0x00040047, |
| 23 | 0x000000a2,0x00000021,0x00000001,0x00040047, | 23 | 0x000000ab,0x00000021,0x00000001,0x00040047, |
| 24 | 0x000000a2,0x00000022,0x00000000,0x00040047, | 24 | 0x000000ab,0x00000022,0x00000000,0x00040047, |
| 25 | 0x000000a4,0x00000006,0x00000004,0x00030047, | 25 | 0x000000ad,0x00000006,0x00000004,0x00030047, |
| 26 | 0x000000a5,0x00000002,0x00050048,0x000000a5, | 26 | 0x000000ae,0x00000002,0x00050048,0x000000ae, |
| 27 | 0x00000000,0x00000023,0x00000000,0x00040047, | 27 | 0x00000000,0x00000023,0x00000000,0x00040047, |
| 28 | 0x000000a7,0x00000021,0x00000000,0x00040047, | 28 | 0x000000b0,0x00000021,0x00000000,0x00040047, |
| 29 | 0x000000a7,0x00000022,0x00000000,0x00040047, | 29 | 0x000000b0,0x00000022,0x00000000,0x00040047, |
| 30 | 0x000000ba,0x0000000b,0x00000019,0x00020013, | 30 | 0x00000117,0x0000000b,0x00000019,0x00020013, |
| 31 | 0x00000002,0x00030021,0x00000003,0x00000002, | 31 | 0x00000002,0x00030021,0x00000003,0x00000002, |
| 32 | 0x00040015,0x00000006,0x00000020,0x00000000, | 32 | 0x00040015,0x00000006,0x00000020,0x00000000, |
| 33 | 0x00030016,0x00000008,0x00000020,0x0004002b, | 33 | 0x00030016,0x00000008,0x00000020,0x0004002b, |
| @@ -51,27 +51,30 @@ | |||
| 51 | 0x00000019,0x00000054,0x00000000,0x00040020, | 51 | 0x00000019,0x00000054,0x00000000,0x00040020, |
| 52 | 0x00000055,0x00000009,0x00000006,0x0004002b, | 52 | 0x00000055,0x00000009,0x00000006,0x0004002b, |
| 53 | 0x00000019,0x0000005e,0x00000001,0x0004002b, | 53 | 0x00000019,0x0000005e,0x00000001,0x0004002b, |
| 54 | 0x00000006,0x00000066,0x00000002,0x0004002b, | ||
| 54 | 0x00000019,0x00000069,0x00000002,0x0004002b, | 55 | 0x00000019,0x00000069,0x00000002,0x0004002b, |
| 55 | 0x00000019,0x00000072,0x00000004,0x0004002b, | 56 | 0x00000019,0x00000071,0x00000003,0x0004002b, |
| 56 | 0x00000019,0x00000077,0x00000006,0x0004002b, | 57 | 0x00000019,0x00000078,0x00000004,0x0004002b, |
| 57 | 0x00000019,0x0000007e,0x00000005,0x0004002b, | 58 | 0x00000019,0x0000007d,0x00000006,0x0004002b, |
| 58 | 0x00000019,0x00000087,0x00000007,0x0004002b, | 59 | 0x00000019,0x00000084,0x00000005,0x0004002b, |
| 59 | 0x00000019,0x00000099,0x00000003,0x0003001d, | 60 | 0x00000006,0x0000008b,0x00000003,0x0004002b, |
| 60 | 0x0000009f,0x00000006,0x0003001e,0x000000a0, | 61 | 0x00000019,0x0000008f,0x00000007,0x0004002b, |
| 61 | 0x0000009f,0x00040020,0x000000a1,0x0000000c, | 62 | 0x00000006,0x000000a0,0x00000004,0x0003001d, |
| 62 | 0x000000a0,0x0004003b,0x000000a1,0x000000a2, | 63 | 0x000000a8,0x00000006,0x0003001e,0x000000a9, |
| 63 | 0x0000000c,0x0003001d,0x000000a4,0x00000006, | 64 | 0x000000a8,0x00040020,0x000000aa,0x0000000c, |
| 64 | 0x0003001e,0x000000a5,0x000000a4,0x00040020, | 65 | 0x000000a9,0x0004003b,0x000000aa,0x000000ab, |
| 65 | 0x000000a6,0x0000000c,0x000000a5,0x0004003b, | 66 | 0x0000000c,0x0003001d,0x000000ad,0x00000006, |
| 66 | 0x000000a6,0x000000a7,0x0000000c,0x00040020, | 67 | 0x0003001e,0x000000ae,0x000000ad,0x00040020, |
| 67 | 0x000000a9,0x0000000c,0x00000006,0x0004002b, | 68 | 0x000000af,0x0000000c,0x000000ae,0x0004003b, |
| 68 | 0x00000006,0x000000b9,0x00000008,0x0006002c, | 69 | 0x000000af,0x000000b0,0x0000000c,0x00040020, |
| 69 | 0x00000049,0x000000ba,0x000000b9,0x000000b9, | 70 | 0x000000b2,0x0000000c,0x00000006,0x0004002b, |
| 70 | 0x00000026,0x0004002b,0x00000019,0x00000109, | 71 | 0x00000006,0x00000116,0x00000008,0x0006002c, |
| 72 | 0x00000049,0x00000117,0x00000116,0x00000116, | ||
| 73 | 0x00000026,0x0004002b,0x00000019,0x0000016c, | ||
| 71 | 0x00000050,0x00050036,0x00000002,0x00000004, | 74 | 0x00000050,0x00050036,0x00000002,0x00000004, |
| 72 | 0x00000000,0x00000003,0x000200f8,0x00000005, | 75 | 0x00000000,0x00000003,0x000200f8,0x00000005, |
| 73 | 0x000300f7,0x000000bb,0x00000000,0x000300fb, | 76 | 0x000300f7,0x00000118,0x00000000,0x000300fb, |
| 74 | 0x00000012,0x000000bc,0x000200f8,0x000000bc, | 77 | 0x00000012,0x00000119,0x000200f8,0x00000119, |
| 75 | 0x0004003d,0x00000049,0x0000004e,0x0000004d, | 78 | 0x0004003d,0x00000049,0x0000004e,0x0000004d, |
| 76 | 0x00050051,0x00000006,0x00000050,0x0000004e, | 79 | 0x00050051,0x00000006,0x00000050,0x0000004e, |
| 77 | 0x00000000,0x00050041,0x00000055,0x00000056, | 80 | 0x00000000,0x00050041,0x00000055,0x00000056, |
| @@ -87,7 +90,7 @@ | |||
| 87 | 0x00000060,0x0000005f,0x000500ae,0x00000013, | 90 | 0x00000060,0x0000005f,0x000500ae,0x00000013, |
| 88 | 0x00000061,0x0000005d,0x00000060,0x000200f9, | 91 | 0x00000061,0x0000005d,0x00000060,0x000200f9, |
| 89 | 0x0000005b,0x000200f8,0x0000005b,0x000700f5, | 92 | 0x0000005b,0x000200f8,0x0000005b,0x000700f5, |
| 90 | 0x00000013,0x00000062,0x00000058,0x000000bc, | 93 | 0x00000013,0x00000062,0x00000058,0x00000119, |
| 91 | 0x00000061,0x0000005a,0x000400a8,0x00000013, | 94 | 0x00000061,0x0000005a,0x000400a8,0x00000013, |
| 92 | 0x00000063,0x00000062,0x000300f7,0x00000065, | 95 | 0x00000063,0x00000062,0x000300f7,0x00000065, |
| 93 | 0x00000000,0x000400fa,0x00000063,0x00000064, | 96 | 0x00000000,0x000400fa,0x00000063,0x00000064, |
| @@ -101,93 +104,174 @@ | |||
| 101 | 0x0000006d,0x00000062,0x0000005b,0x0000006c, | 104 | 0x0000006d,0x00000062,0x0000005b,0x0000006c, |
| 102 | 0x00000064,0x000300f7,0x0000006f,0x00000000, | 105 | 0x00000064,0x000300f7,0x0000006f,0x00000000, |
| 103 | 0x000400fa,0x0000006d,0x0000006e,0x0000006f, | 106 | 0x000400fa,0x0000006d,0x0000006e,0x0000006f, |
| 104 | 0x000200f8,0x0000006e,0x000200f9,0x000000bb, | 107 | 0x000200f8,0x0000006e,0x000200f9,0x00000118, |
| 105 | 0x000200f8,0x0000006f,0x00050041,0x00000055, | 108 | 0x000200f8,0x0000006f,0x00050041,0x00000055, |
| 106 | 0x00000073,0x00000053,0x00000072,0x0004003d, | 109 | 0x00000072,0x00000053,0x00000071,0x0004003d, |
| 107 | 0x00000006,0x00000074,0x00000073,0x00050051, | 110 | 0x00000006,0x00000073,0x00000072,0x000500ae, |
| 108 | 0x00000006,0x00000076,0x0000004e,0x00000002, | 111 | 0x00000013,0x00000074,0x00000073,0x00000066, |
| 109 | 0x00050041,0x00000055,0x00000078,0x00000053, | 112 | 0x000300f7,0x00000076,0x00000000,0x000400fa, |
| 110 | 0x00000077,0x0004003d,0x00000006,0x00000079, | 113 | 0x00000074,0x00000075,0x00000076,0x000200f8, |
| 111 | 0x00000078,0x00050084,0x00000006,0x0000007a, | 114 | 0x00000075,0x00050041,0x00000055,0x00000079, |
| 112 | 0x00000076,0x00000079,0x00050080,0x00000006, | 115 | 0x00000053,0x00000078,0x0004003d,0x00000006, |
| 113 | 0x0000007b,0x00000074,0x0000007a,0x00050051, | 116 | 0x0000007a,0x00000079,0x00050051,0x00000006, |
| 114 | 0x00000006,0x0000007d,0x0000004e,0x00000001, | 117 | 0x0000007c,0x0000004e,0x00000002,0x00050041, |
| 115 | 0x00050041,0x00000055,0x0000007f,0x00000053, | 118 | 0x00000055,0x0000007e,0x00000053,0x0000007d, |
| 116 | 0x0000007e,0x0004003d,0x00000006,0x00000080, | 119 | 0x0004003d,0x00000006,0x0000007f,0x0000007e, |
| 117 | 0x0000007f,0x00050084,0x00000006,0x00000081, | 120 | 0x00050084,0x00000006,0x00000080,0x0000007c, |
| 118 | 0x0000007d,0x00000080,0x00050080,0x00000006, | 121 | 0x0000007f,0x00050080,0x00000006,0x00000081, |
| 119 | 0x00000082,0x0000007b,0x00000081,0x00050080, | 122 | 0x0000007a,0x00000080,0x00050051,0x00000006, |
| 120 | 0x00000006,0x00000085,0x00000082,0x00000050, | 123 | 0x00000083,0x0000004e,0x00000001,0x00050041, |
| 121 | 0x00050041,0x00000055,0x00000088,0x00000053, | 124 | 0x00000055,0x00000085,0x00000053,0x00000084, |
| 122 | 0x00000087,0x0004003d,0x00000006,0x00000089, | 125 | 0x0004003d,0x00000006,0x00000086,0x00000085, |
| 123 | 0x00000088,0x00050041,0x00000055,0x0000008c, | 126 | 0x00050084,0x00000006,0x00000087,0x00000083, |
| 127 | 0x00000086,0x00050080,0x00000006,0x00000088, | ||
| 128 | 0x00000081,0x00000087,0x00050084,0x00000006, | ||
| 129 | 0x0000008c,0x00000050,0x0000008b,0x00050080, | ||
| 130 | 0x00000006,0x0000008d,0x00000088,0x0000008c, | ||
| 131 | 0x00050041,0x00000055,0x00000090,0x00000053, | ||
| 132 | 0x0000008f,0x0004003d,0x00000006,0x00000091, | ||
| 133 | 0x00000090,0x00050041,0x00000055,0x00000094, | ||
| 124 | 0x00000053,0x0000005e,0x0004003d,0x00000006, | 134 | 0x00000053,0x0000005e,0x0004003d,0x00000006, |
| 125 | 0x0000008d,0x0000008c,0x00050084,0x00000006, | 135 | 0x00000095,0x00000094,0x00050084,0x00000006, |
| 126 | 0x0000008e,0x00000076,0x0000008d,0x00050080, | 136 | 0x00000096,0x0000007c,0x00000095,0x00050080, |
| 127 | 0x00000006,0x00000091,0x0000008e,0x0000007d, | 137 | 0x00000006,0x00000099,0x00000096,0x00000083, |
| 128 | 0x00050084,0x00000006,0x00000094,0x00000091, | 138 | 0x00050084,0x00000006,0x0000009c,0x00000099, |
| 129 | 0x00000057,0x00050080,0x00000006,0x00000095, | 139 | 0x00000057,0x00050080,0x00000006,0x0000009f, |
| 130 | 0x00000089,0x00000094,0x00050080,0x00000006, | 140 | 0x0000009c,0x00000050,0x00050084,0x00000006, |
| 131 | 0x00000098,0x00000095,0x00000050,0x00050041, | 141 | 0x000000a1,0x0000009f,0x000000a0,0x00050080, |
| 132 | 0x00000055,0x0000009a,0x00000053,0x00000099, | 142 | 0x00000006,0x000000a2,0x00000091,0x000000a1, |
| 133 | 0x0004003d,0x00000006,0x0000009b,0x0000009a, | 143 | 0x000500aa,0x00000013,0x000000a5,0x00000073, |
| 134 | 0x000500ab,0x00000013,0x0000009c,0x0000009b, | 144 | 0x0000008b,0x000300f7,0x000000a7,0x00000000, |
| 135 | 0x00000012,0x000300f7,0x0000009e,0x00000000, | 145 | 0x000400fa,0x000000a5,0x000000a6,0x000000c7, |
| 136 | 0x000400fa,0x0000009c,0x0000009d,0x000000b1, | 146 | 0x000200f8,0x000000a6,0x00060041,0x000000b2, |
| 137 | 0x000200f8,0x0000009d,0x00060041,0x000000a9, | 147 | 0x000000b3,0x000000b0,0x00000054,0x0000008d, |
| 138 | 0x000000aa,0x000000a7,0x00000054,0x00000085, | 148 | 0x0004003d,0x00000006,0x000000b4,0x000000b3, |
| 139 | 0x0004003d,0x00000006,0x000000ab,0x000000aa, | 149 | 0x00060041,0x000000b2,0x000000b5,0x000000ab, |
| 140 | 0x000500c7,0x00000006,0x000000ac,0x000000ab, | 150 | 0x00000054,0x000000a2,0x0003003e,0x000000b5, |
| 141 | 0x0000003f,0x000300f7,0x000000e8,0x00000000, | 151 | 0x000000b4,0x00050080,0x00000006,0x000000b7, |
| 142 | 0x000300fb,0x00000012,0x000000d2,0x000200f8, | 152 | 0x000000a2,0x00000026,0x00050080,0x00000006, |
| 143 | 0x000000d2,0x000500aa,0x00000013,0x000000d4, | 153 | 0x000000b9,0x0000008d,0x00000026,0x00060041, |
| 144 | 0x000000ac,0x00000012,0x000300f7,0x000000d6, | 154 | 0x000000b2,0x000000ba,0x000000b0,0x00000054, |
| 145 | 0x00000000,0x000400fa,0x000000d4,0x000000d5, | 155 | 0x000000b9,0x0004003d,0x00000006,0x000000bb, |
| 146 | 0x000000d6,0x000200f8,0x000000d5,0x000200f9, | 156 | 0x000000ba,0x00060041,0x000000b2,0x000000bc, |
| 147 | 0x000000e8,0x000200f8,0x000000d6,0x0006000c, | 157 | 0x000000ab,0x00000054,0x000000b7,0x0003003e, |
| 148 | 0x00000019,0x000000d8,0x00000001,0x0000004b, | 158 | 0x000000bc,0x000000bb,0x00050080,0x00000006, |
| 149 | 0x000000ac,0x00050082,0x00000019,0x000000db, | 159 | 0x000000be,0x000000a2,0x00000066,0x00050080, |
| 150 | 0x00000020,0x000000d8,0x0004007c,0x00000006, | 160 | 0x00000006,0x000000c0,0x0000008d,0x00000066, |
| 151 | 0x000000dc,0x000000db,0x000500c4,0x00000006, | 161 | 0x00060041,0x000000b2,0x000000c1,0x000000b0, |
| 152 | 0x000000dd,0x000000ac,0x000000dc,0x00050080, | 162 | 0x00000054,0x000000c0,0x0004003d,0x00000006, |
| 153 | 0x00000006,0x000000df,0x000000dd,0x00000026, | 163 | 0x000000c2,0x000000c1,0x00060041,0x000000b2, |
| 154 | 0x00040070,0x00000008,0x000000e0,0x000000df, | 164 | 0x000000c3,0x000000ab,0x00000054,0x000000be, |
| 155 | 0x00050080,0x00000019,0x000000e3,0x000000d8, | 165 | 0x0003003e,0x000000c3,0x000000c2,0x00050080, |
| 156 | 0x00000109,0x0004007c,0x00000006,0x000000e4, | 166 | 0x00000006,0x000000c5,0x000000a2,0x0000008b, |
| 157 | 0x000000e3,0x000500c4,0x00000006,0x000000e5, | 167 | 0x00060041,0x000000b2,0x000000c6,0x000000ab, |
| 158 | 0x000000e4,0x00000020,0x0004007c,0x00000008, | 168 | 0x00000054,0x000000c5,0x0003003e,0x000000c6, |
| 159 | 0x000000e6,0x000000e5,0x00050085,0x00000008, | 169 | 0x00000012,0x000200f9,0x000000a7,0x000200f8, |
| 160 | 0x000000e7,0x000000e0,0x000000e6,0x000200f9, | 170 | 0x000000c7,0x00060041,0x000000b2,0x000000ca, |
| 161 | 0x000000e8,0x000200f8,0x000000e8,0x000700f5, | 171 | 0x000000ab,0x00000054,0x000000a2,0x0004003d, |
| 162 | 0x00000008,0x00000108,0x00000017,0x000000d5, | 172 | 0x00000006,0x000000cb,0x000000ca,0x00060041, |
| 163 | 0x000000e7,0x000000d6,0x0004007c,0x00000006, | 173 | 0x000000b2,0x000000cc,0x000000b0,0x00000054, |
| 164 | 0x000000af,0x00000108,0x00060041,0x000000a9, | 174 | 0x0000008d,0x0003003e,0x000000cc,0x000000cb, |
| 165 | 0x000000b0,0x000000a2,0x00000054,0x00000098, | 175 | 0x00050080,0x00000006,0x000000ce,0x0000008d, |
| 166 | 0x0003003e,0x000000b0,0x000000af,0x000200f9, | 176 | 0x00000026,0x00050080,0x00000006,0x000000d0, |
| 167 | 0x0000009e,0x000200f8,0x000000b1,0x00060041, | 177 | 0x000000a2,0x00000026,0x00060041,0x000000b2, |
| 168 | 0x000000a9,0x000000b5,0x000000a2,0x00000054, | 178 | 0x000000d1,0x000000ab,0x00000054,0x000000d0, |
| 169 | 0x00000098,0x0004003d,0x00000006,0x000000b6, | 179 | 0x0004003d,0x00000006,0x000000d2,0x000000d1, |
| 170 | 0x000000b5,0x000300f7,0x000000fc,0x00000000, | 180 | 0x00060041,0x000000b2,0x000000d3,0x000000b0, |
| 171 | 0x000300fb,0x00000012,0x000000ee,0x000200f8, | 181 | 0x00000054,0x000000ce,0x0003003e,0x000000d3, |
| 172 | 0x000000ee,0x000500ac,0x00000013,0x000000f0, | 182 | 0x000000d2,0x00050080,0x00000006,0x000000d5, |
| 173 | 0x000000b6,0x00000035,0x000300f7,0x000000f2, | 183 | 0x0000008d,0x00000066,0x00050080,0x00000006, |
| 174 | 0x00000000,0x000400fa,0x000000f0,0x000000f1, | 184 | 0x000000d7,0x000000a2,0x00000066,0x00060041, |
| 175 | 0x000000f2,0x000200f8,0x000000f1,0x000200f9, | 185 | 0x000000b2,0x000000d8,0x000000ab,0x00000054, |
| 176 | 0x000000fc,0x000200f8,0x000000f2,0x000500ae, | 186 | 0x000000d7,0x0004003d,0x00000006,0x000000d9, |
| 177 | 0x00000013,0x000000f4,0x000000b6,0x0000003b, | 187 | 0x000000d8,0x00060041,0x000000b2,0x000000da, |
| 178 | 0x000300f7,0x000000f6,0x00000000,0x000400fa, | 188 | 0x000000b0,0x00000054,0x000000d5,0x0003003e, |
| 179 | 0x000000f4,0x000000f5,0x000000f6,0x000200f8, | 189 | 0x000000da,0x000000d9,0x000200f9,0x000000a7, |
| 180 | 0x000000f5,0x000200f9,0x000000fc,0x000200f8, | 190 | 0x000200f8,0x000000a7,0x000200f9,0x00000118, |
| 181 | 0x000000f6,0x0004007c,0x00000008,0x000000f8, | 191 | 0x000200f8,0x00000076,0x00050041,0x00000055, |
| 182 | 0x000000b6,0x00050085,0x00000008,0x000000f9, | 192 | 0x000000dd,0x00000053,0x00000078,0x0004003d, |
| 183 | 0x000000f8,0x00000043,0x0006000c,0x00000008, | 193 | 0x00000006,0x000000de,0x000000dd,0x00050051, |
| 184 | 0x000000fa,0x00000001,0x00000002,0x000000f9, | 194 | 0x00000006,0x000000e0,0x0000004e,0x00000002, |
| 185 | 0x0004006d,0x00000006,0x000000fb,0x000000fa, | 195 | 0x00050041,0x00000055,0x000000e1,0x00000053, |
| 186 | 0x000200f9,0x000000fc,0x000200f8,0x000000fc, | 196 | 0x0000007d,0x0004003d,0x00000006,0x000000e2, |
| 187 | 0x000900f5,0x00000006,0x00000107,0x00000012, | 197 | 0x000000e1,0x00050084,0x00000006,0x000000e3, |
| 188 | 0x000000f1,0x0000003f,0x000000f5,0x000000fb, | 198 | 0x000000e0,0x000000e2,0x00050080,0x00000006, |
| 189 | 0x000000f6,0x00060041,0x000000a9,0x000000b8, | 199 | 0x000000e4,0x000000de,0x000000e3,0x00050051, |
| 190 | 0x000000a7,0x00000054,0x00000085,0x0003003e, | 200 | 0x00000006,0x000000e6,0x0000004e,0x00000001, |
| 191 | 0x000000b8,0x00000107,0x000200f9,0x0000009e, | 201 | 0x00050041,0x00000055,0x000000e7,0x00000053, |
| 192 | 0x000200f8,0x0000009e,0x000200f9,0x000000bb, | 202 | 0x00000084,0x0004003d,0x00000006,0x000000e8, |
| 193 | 0x000200f8,0x000000bb,0x000100fd,0x00010038} | 203 | 0x000000e7,0x00050084,0x00000006,0x000000e9, |
| 204 | 0x000000e6,0x000000e8,0x00050080,0x00000006, | ||
| 205 | 0x000000ea,0x000000e4,0x000000e9,0x00050080, | ||
| 206 | 0x00000006,0x000000ed,0x000000ea,0x00000050, | ||
| 207 | 0x00050041,0x00000055,0x000000ef,0x00000053, | ||
| 208 | 0x0000008f,0x0004003d,0x00000006,0x000000f0, | ||
| 209 | 0x000000ef,0x00050041,0x00000055,0x000000f3, | ||
| 210 | 0x00000053,0x0000005e,0x0004003d,0x00000006, | ||
| 211 | 0x000000f4,0x000000f3,0x00050084,0x00000006, | ||
| 212 | 0x000000f5,0x000000e0,0x000000f4,0x00050080, | ||
| 213 | 0x00000006,0x000000f8,0x000000f5,0x000000e6, | ||
| 214 | 0x00050084,0x00000006,0x000000fb,0x000000f8, | ||
| 215 | 0x00000057,0x00050080,0x00000006,0x000000fc, | ||
| 216 | 0x000000f0,0x000000fb,0x00050080,0x00000006, | ||
| 217 | 0x000000ff,0x000000fc,0x00000050,0x000500ab, | ||
| 218 | 0x00000013,0x00000102,0x00000073,0x00000012, | ||
| 219 | 0x000300f7,0x00000104,0x00000000,0x000400fa, | ||
| 220 | 0x00000102,0x00000103,0x0000010e,0x000200f8, | ||
| 221 | 0x00000103,0x00060041,0x000000b2,0x00000107, | ||
| 222 | 0x000000b0,0x00000054,0x000000ed,0x0004003d, | ||
| 223 | 0x00000006,0x00000108,0x00000107,0x000500c7, | ||
| 224 | 0x00000006,0x00000109,0x00000108,0x0000003f, | ||
| 225 | 0x000300f7,0x00000145,0x00000000,0x000300fb, | ||
| 226 | 0x00000012,0x0000012f,0x000200f8,0x0000012f, | ||
| 227 | 0x000500aa,0x00000013,0x00000131,0x00000109, | ||
| 228 | 0x00000012,0x000300f7,0x00000133,0x00000000, | ||
| 229 | 0x000400fa,0x00000131,0x00000132,0x00000133, | ||
| 230 | 0x000200f8,0x00000132,0x000200f9,0x00000145, | ||
| 231 | 0x000200f8,0x00000133,0x0006000c,0x00000019, | ||
| 232 | 0x00000135,0x00000001,0x0000004b,0x00000109, | ||
| 233 | 0x00050082,0x00000019,0x00000138,0x00000020, | ||
| 234 | 0x00000135,0x0004007c,0x00000006,0x00000139, | ||
| 235 | 0x00000138,0x000500c4,0x00000006,0x0000013a, | ||
| 236 | 0x00000109,0x00000139,0x00050080,0x00000006, | ||
| 237 | 0x0000013c,0x0000013a,0x00000026,0x00040070, | ||
| 238 | 0x00000008,0x0000013d,0x0000013c,0x00050080, | ||
| 239 | 0x00000019,0x00000140,0x00000135,0x0000016c, | ||
| 240 | 0x0004007c,0x00000006,0x00000141,0x00000140, | ||
| 241 | 0x000500c4,0x00000006,0x00000142,0x00000141, | ||
| 242 | 0x00000020,0x0004007c,0x00000008,0x00000143, | ||
| 243 | 0x00000142,0x00050085,0x00000008,0x00000144, | ||
| 244 | 0x0000013d,0x00000143,0x000200f9,0x00000145, | ||
| 245 | 0x000200f8,0x00000145,0x000700f5,0x00000008, | ||
| 246 | 0x0000016b,0x00000017,0x00000132,0x00000144, | ||
| 247 | 0x00000133,0x0004007c,0x00000006,0x0000010c, | ||
| 248 | 0x0000016b,0x00060041,0x000000b2,0x0000010d, | ||
| 249 | 0x000000ab,0x00000054,0x000000ff,0x0003003e, | ||
| 250 | 0x0000010d,0x0000010c,0x000200f9,0x00000104, | ||
| 251 | 0x000200f8,0x0000010e,0x00060041,0x000000b2, | ||
| 252 | 0x00000112,0x000000ab,0x00000054,0x000000ff, | ||
| 253 | 0x0004003d,0x00000006,0x00000113,0x00000112, | ||
| 254 | 0x000300f7,0x00000159,0x00000000,0x000300fb, | ||
| 255 | 0x00000012,0x0000014b,0x000200f8,0x0000014b, | ||
| 256 | 0x000500ac,0x00000013,0x0000014d,0x00000113, | ||
| 257 | 0x00000035,0x000300f7,0x0000014f,0x00000000, | ||
| 258 | 0x000400fa,0x0000014d,0x0000014e,0x0000014f, | ||
| 259 | 0x000200f8,0x0000014e,0x000200f9,0x00000159, | ||
| 260 | 0x000200f8,0x0000014f,0x000500ae,0x00000013, | ||
| 261 | 0x00000151,0x00000113,0x0000003b,0x000300f7, | ||
| 262 | 0x00000153,0x00000000,0x000400fa,0x00000151, | ||
| 263 | 0x00000152,0x00000153,0x000200f8,0x00000152, | ||
| 264 | 0x000200f9,0x00000159,0x000200f8,0x00000153, | ||
| 265 | 0x0004007c,0x00000008,0x00000155,0x00000113, | ||
| 266 | 0x00050085,0x00000008,0x00000156,0x00000155, | ||
| 267 | 0x00000043,0x0006000c,0x00000008,0x00000157, | ||
| 268 | 0x00000001,0x00000002,0x00000156,0x0004006d, | ||
| 269 | 0x00000006,0x00000158,0x00000157,0x000200f9, | ||
| 270 | 0x00000159,0x000200f8,0x00000159,0x000900f5, | ||
| 271 | 0x00000006,0x0000016a,0x00000012,0x0000014e, | ||
| 272 | 0x0000003f,0x00000152,0x00000158,0x00000153, | ||
| 273 | 0x00060041,0x000000b2,0x00000115,0x000000b0, | ||
| 274 | 0x00000054,0x000000ed,0x0003003e,0x00000115, | ||
| 275 | 0x0000016a,0x000200f9,0x00000104,0x000200f8, | ||
| 276 | 0x00000104,0x000200f9,0x00000118,0x000200f8, | ||
| 277 | 0x00000118,0x000100fd,0x00010038} | ||
diff --git a/runtime/src/frame_pacer.h b/runtime/src/frame_pacer.h new file mode 100644 index 000000000..9c35c5c96 --- /dev/null +++ b/runtime/src/frame_pacer.h | |||
| @@ -0,0 +1,374 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | // Frame pacing for the Xbox presentation path (docs/d3d12_vulkan.md section 4.5): what SetFrameIntervalX, | ||
| 4 | // ScheduleFrameEventX, WaitFrameEventX and GetFrameStatisticsX do, and the token check and flip bookkeeping that | ||
| 5 | // PresentX needs. No Vulkan and no Windows headers: time comes from an injectable FrameClock, so the tests drive it | ||
| 6 | // with a fake clock and the real runtime uses a steady high-resolution timer (a host vblank source can replace it). | ||
| 7 | // | ||
| 8 | // What is known and what is ours. From the public GDKX samples (section 4.5): the call order, the argument counts, that | ||
| 9 | // the interval call takes an allowed frame latency (buffer count - 1 in one sample, 1 in another), that the origin | ||
| 10 | // event is scheduled once and waited on every frame, that the wait yields a token that PresentX carries, and that | ||
| 11 | // frame events must be registered again after ResumeX. Everything below the call shapes is our own model, chosen to | ||
| 12 | // be deterministic and to behave like the samples expect; none of it is measured on the console: | ||
| 13 | // * An interval is a period in microseconds (the real enum values are unmeasured). | ||
| 14 | // * Vblanks happen at epoch + n * period (n = 1, 2, ...), the epoch being the time of SetFrameIntervalX. The | ||
| 15 | // first origin after ScheduleFrameEventX is the next vblank. The origin event of vblank n is signalled | ||
| 16 | // offset microseconds after it (ScheduleFrameEventX's offset). | ||
| 17 | // * A frame is "in flight" from PresentX until the vblank that shows it. Frames show one per vblank, in order. | ||
| 18 | // WaitFrameEventX does not return while `latency` frames are in flight. | ||
| 19 | // * A wait that comes late (after the next origin already passed) returns at once with the newest passed origin; | ||
| 20 | // the missed origins are skipped, not queued. | ||
| 21 | // * Tokens are 1, 2, 3, ... (0 is the null token), one per successful wait, never reused, across suspend/resume. | ||
| 22 | |||
| 23 | #include <algorithm> | ||
| 24 | #include <atomic> | ||
| 25 | #include <chrono> | ||
| 26 | #include <condition_variable> | ||
| 27 | #include <cstdint> | ||
| 28 | #include <deque> | ||
| 29 | #include <functional> | ||
| 30 | #include <limits> | ||
| 31 | #include <mutex> | ||
| 32 | |||
| 33 | namespace leonida { | ||
| 34 | |||
| 35 | // HRESULT values as plain numbers, so this header stands alone. | ||
| 36 | namespace pacer_hr { | ||
| 37 | inline constexpr std::int32_t ok = 0; | ||
| 38 | inline constexpr std::int32_t false_ = 1; // S_FALSE | ||
| 39 | inline constexpr std::int32_t invalid_arg = std::int32_t(0x80070057); // E_INVALIDARG | ||
| 40 | inline constexpr std::int32_t invalid_state = std::int32_t(0x8007139F); // HRESULT_FROM_WIN32(ERROR_INVALID_STATE) | ||
| 41 | inline constexpr std::int32_t timeout = std::int32_t(0x80070102); // HRESULT_FROM_WIN32(WAIT_TIMEOUT) | ||
| 42 | } // namespace pacer_hr | ||
| 43 | |||
| 44 | inline constexpr std::uint64_t no_deadline = std::numeric_limits<std::uint64_t>::max(); | ||
| 45 | inline constexpr std::uint32_t infinite_timeout_ms = 0xFFFFFFFFu; // INFINITE | ||
| 46 | |||
| 47 | class FrameClock { | ||
| 48 | public: | ||
| 49 | virtual ~FrameClock() = default; | ||
| 50 | virtual std::uint64_t now_us() = 0; | ||
| 51 | // Called with `lock` held on the pacer's mutex. Returns when `cv` is notified, `deadline_us` (in now_us() time; | ||
| 52 | // no_deadline for none) has passed, or spuriously; the caller re-checks its condition. | ||
| 53 | virtual void wait(std::unique_lock<std::mutex>& lock, std::condition_variable& cv, std::uint64_t deadline_us) = 0; | ||
| 54 | // The pacer gives a function that takes its mutex and notifies; a fake clock calls it when time moves. | ||
| 55 | virtual void set_wake(std::function<void()> wake) { (void)wake; } | ||
| 56 | }; | ||
| 57 | |||
| 58 | // Real time: std::chrono::steady_clock (QueryPerformanceCounter on Windows). | ||
| 59 | class SteadyFrameClock final : public FrameClock { | ||
| 60 | public: | ||
| 61 | std::uint64_t now_us() override { | ||
| 62 | return std::uint64_t(std::chrono::duration_cast<std::chrono::microseconds>( | ||
| 63 | std::chrono::steady_clock::now() - start_).count()); | ||
| 64 | } | ||
| 65 | void wait(std::unique_lock<std::mutex>& lock, std::condition_variable& cv, std::uint64_t deadline_us) override { | ||
| 66 | if (deadline_us == no_deadline) cv.wait(lock); | ||
| 67 | else cv.wait_until(lock, start_ + std::chrono::microseconds(deadline_us)); | ||
| 68 | } | ||
| 69 | |||
| 70 | private: | ||
| 71 | std::chrono::steady_clock::time_point start_ = std::chrono::steady_clock::now(); | ||
| 72 | }; | ||
| 73 | |||
| 74 | // Time that only moves when the test says so. A pacer waiting on it blocks until advance_us() is called. | ||
| 75 | class ManualFrameClock final : public FrameClock { | ||
| 76 | public: | ||
| 77 | std::uint64_t now_us() override { return now_.load(); } | ||
| 78 | void wait(std::unique_lock<std::mutex>& lock, std::condition_variable& cv, std::uint64_t) override { cv.wait(lock); } | ||
| 79 | void set_wake(std::function<void()> wake) override { | ||
| 80 | std::lock_guard guard(wake_mutex_); | ||
| 81 | wake_ = std::move(wake); | ||
| 82 | } | ||
| 83 | void advance_us(std::uint64_t microseconds) { | ||
| 84 | now_ += microseconds; | ||
| 85 | // Called under the lock, so a pacer that is being destroyed (set_wake({})) is never woken after it is gone. | ||
| 86 | std::lock_guard guard(wake_mutex_); | ||
| 87 | if (wake_) wake_(); | ||
| 88 | } | ||
| 89 | |||
| 90 | private: | ||
| 91 | std::atomic<std::uint64_t> now_{0}; | ||
| 92 | std::mutex wake_mutex_; | ||
| 93 | std::function<void()> wake_; | ||
| 94 | }; | ||
| 95 | |||
| 96 | // Provisional definitions of ours: the real constants and layouts of the console API are unmeasured. | ||
| 97 | inline constexpr std::uint32_t frame_event_origin = 0; | ||
| 98 | // Statistics types (bit mask). Interval and render entries are never produced here. | ||
| 99 | inline constexpr std::uint32_t stat_frame_interval = 0x01; | ||
| 100 | inline constexpr std::uint32_t stat_frame_event = 0x02; | ||
| 101 | inline constexpr std::uint32_t stat_render = 0x04; | ||
| 102 | inline constexpr std::uint32_t stat_present = 0x08; | ||
| 103 | inline constexpr std::uint32_t stat_display = 0x10; | ||
| 104 | |||
| 105 | struct FrameStatistic { | ||
| 106 | std::uint32_t type = 0; // one stat_* bit | ||
| 107 | std::uint32_t plane_index = 0; | ||
| 108 | std::uint64_t token = 0; | ||
| 109 | std::uint64_t time_us = 0; // pacer clock | ||
| 110 | float percent_scanned = 0.0f; // display entries: where the scan was when the frame came up (always 0 here) | ||
| 111 | }; | ||
| 112 | |||
| 113 | class FramePacer { | ||
| 114 | public: | ||
| 115 | explicit FramePacer(FrameClock& clock) : clock_(clock) { | ||
| 116 | clock_.set_wake([this] { | ||
| 117 | { std::lock_guard guard(mutex_); } | ||
| 118 | cv_.notify_all(); | ||
| 119 | }); | ||
| 120 | } | ||
| 121 | ~FramePacer() { | ||
| 122 | clock_.set_wake({}); | ||
| 123 | { | ||
| 124 | std::lock_guard guard(mutex_); | ||
| 125 | shutdown_ = true; | ||
| 126 | } | ||
| 127 | cv_.notify_all(); | ||
| 128 | std::unique_lock lock(mutex_); // waiters leave before the members go | ||
| 129 | cv_.wait(lock, [this] { return waiters_ == 0; }); | ||
| 130 | } | ||
| 131 | FramePacer(const FramePacer&) = delete; | ||
| 132 | FramePacer& operator=(const FramePacer&) = delete; | ||
| 133 | |||
| 134 | // SetFrameIntervalX(output, interval, latency, flags). `period_us` is the interval as a period; `latency` is | ||
| 135 | // the number of frames allowed in flight (0 counts as 1). The vblank grid is re-based: its epoch is now. A | ||
| 136 | // scheduled origin event stays armed (with its offset, kept below the new period) and its next origin is one | ||
| 137 | // new period from now; waiters blocked on the old grid pick the new one up. | ||
| 138 | std::int32_t set_frame_interval(std::uint32_t period_us, std::uint32_t latency, std::uint32_t flags) { | ||
| 139 | (void)flags; | ||
| 140 | if (period_us == 0) return pacer_hr::invalid_arg; | ||
| 141 | std::lock_guard guard(mutex_); | ||
| 142 | if (suspended_) return pacer_hr::invalid_state; | ||
| 143 | period_us_ = period_us; | ||
| 144 | latency_ = std::max(latency, 1u); | ||
| 145 | epoch_us_ = clock_.now_us(); | ||
| 146 | interval_set_ = true; | ||
| 147 | offset_us_ = std::min(offset_us_, period_us - 1); | ||
| 148 | last_origin_ = 0; // origin 0 is the epoch itself, which has passed | ||
| 149 | cv_.notify_all(); | ||
| 150 | return pacer_hr::ok; | ||
| 151 | } | ||
| 152 | |||
| 153 | // ScheduleFrameEventX(event, offset, ..., flags). Only the origin event exists here. | ||
| 154 | std::int32_t schedule_frame_event(std::uint32_t event, std::uint32_t offset_us, std::uint32_t flags) { | ||
| 155 | (void)flags; | ||
| 156 | if (event != frame_event_origin) return pacer_hr::invalid_arg; | ||
| 157 | std::lock_guard guard(mutex_); | ||
| 158 | if (suspended_ || !interval_set_) return pacer_hr::invalid_state; | ||
| 159 | if (offset_us >= period_us_) return pacer_hr::invalid_arg; | ||
| 160 | offset_us_ = offset_us; | ||
| 161 | armed_ = true; | ||
| 162 | // The first origin is the next one that has not come yet: origin n is due at epoch + n * period + offset. | ||
| 163 | const std::uint64_t now = clock_.now_us(); | ||
| 164 | last_origin_ = now >= epoch_us_ + offset_us_ ? std::int64_t((now - epoch_us_ - offset_us_) / period_us_) : 0; | ||
| 165 | cv_.notify_all(); | ||
| 166 | return pacer_hr::ok; | ||
| 167 | } | ||
| 168 | |||
| 169 | // WaitFrameEventX(event, timeout, ..., flags, &token). Blocks until the next origin, and while `latency` frames | ||
| 170 | // are in flight. pacer_hr::timeout when `timeout_ms` passes first (infinite_timeout_ms: never); the token is left | ||
| 171 | // alone then. invalid_state when nothing is registered, the pacer is suspended, or one of those happens while waiting. | ||
| 172 | std::int32_t wait_frame_event(std::uint32_t event, std::uint32_t timeout_ms, std::uint32_t flags, | ||
| 173 | std::uint64_t* token) { | ||
| 174 | (void)flags; | ||
| 175 | if (event != frame_event_origin || !token) return pacer_hr::invalid_arg; | ||
| 176 | std::unique_lock lock(mutex_); | ||
| 177 | ++waiters_; | ||
| 178 | const std::int32_t result = wait_locked(lock, timeout_ms, token); | ||
| 179 | if (--waiters_ == 0) cv_.notify_all(); | ||
| 180 | return result; | ||
| 181 | } | ||
| 182 | |||
| 183 | // GetFrameStatisticsX(token, type_mask, &count, array). `count` is the room in `out` on entry and the number of | ||
| 184 | // entries written on return. S_OK when every producible requested type was written (event, present, display), | ||
| 185 | // S_FALSE with count 0 or fewer when they are not (there yet, or the token is unknown or has aged out of the | ||
| 186 | // last 64). With a null `out`, count only receives how many entries are available. | ||
| 187 | std::int32_t get_frame_statistics(std::uint64_t token, std::uint32_t type_mask, std::uint32_t* count, | ||
| 188 | FrameStatistic* out) { | ||
| 189 | if (!count) return pacer_hr::invalid_arg; | ||
| 190 | std::lock_guard guard(mutex_); | ||
| 191 | retire_locked(clock_.now_us()); | ||
| 192 | const Record* record = nullptr; | ||
| 193 | for (const Record& r : records_) | ||
| 194 | if (r.token == token) record = &r; | ||
| 195 | FrameStatistic found[3]; | ||
| 196 | std::uint32_t n = 0, expected = 0; | ||
| 197 | auto add = [&](std::uint32_t type, bool present, std::uint64_t time) { | ||
| 198 | if (!(type_mask & type)) return; | ||
| 199 | ++expected; | ||
| 200 | if (!record || !present) return; | ||
| 201 | found[n++] = FrameStatistic{type, 0, token, time, 0.0f}; | ||
| 202 | }; | ||
| 203 | if (record) { | ||
| 204 | add(stat_frame_event, true, record->origin_us); | ||
| 205 | add(stat_present, record->presented, record->present_us); | ||
| 206 | add(stat_display, record->displayed, record->display_us); | ||
| 207 | } else { | ||
| 208 | expected = ((type_mask & stat_frame_event) ? 1u : 0u) + ((type_mask & stat_present) ? 1u : 0u) + | ||
| 209 | ((type_mask & stat_display) ? 1u : 0u); | ||
| 210 | } | ||
| 211 | if (!out) { | ||
| 212 | const std::uint32_t available = n; | ||
| 213 | *count = available; | ||
| 214 | return available && available == expected ? pacer_hr::ok : pacer_hr::false_; | ||
| 215 | } | ||
| 216 | const std::uint32_t written = std::min(n, *count); | ||
| 217 | for (std::uint32_t i = 0; i < written; ++i) out[i] = found[i]; | ||
| 218 | *count = written; | ||
| 219 | return written && written == expected ? pacer_hr::ok : pacer_hr::false_; | ||
| 220 | } | ||
| 221 | |||
| 222 | // Queue side. check_present_token before the frame is presented, commit_present_token after it was queued. | ||
| 223 | // What real hardware does with a null, repeated or out-of-order token is unmeasured, and a title that presents | ||
| 224 | // with Token = 0 (a loading screen), re-presents with one token, or presents from a render thread out of order | ||
| 225 | // must not freeze. So the only refusal is a token that was never handed out (invalid_arg, which is a bug in the | ||
| 226 | // caller); null, stale (older than the last presented) and repeated tokens return ok and say which kind they | ||
| 227 | // were in `issue`, so the caller can log it. invalid_state while suspended. | ||
| 228 | enum class TokenIssue { none, null_token, stale, repeated }; | ||
| 229 | std::int32_t check_present_token(std::uint64_t token, TokenIssue* issue = nullptr) { | ||
| 230 | std::lock_guard guard(mutex_); | ||
| 231 | if (issue) *issue = TokenIssue::none; | ||
| 232 | if (suspended_) return pacer_hr::invalid_state; | ||
| 233 | if (token > issued_) return pacer_hr::invalid_arg; | ||
| 234 | if (issue) { | ||
| 235 | if (token == 0) *issue = TokenIssue::null_token; | ||
| 236 | else if (token == last_presented_) *issue = TokenIssue::repeated; | ||
| 237 | else if (token < last_presented_) *issue = TokenIssue::stale; | ||
| 238 | } | ||
| 239 | return pacer_hr::ok; | ||
| 240 | } | ||
| 241 | void commit_present_token(std::uint64_t token, std::uint32_t plane_count) { | ||
| 242 | (void)plane_count; | ||
| 243 | std::lock_guard guard(mutex_); | ||
| 244 | const std::uint64_t now = clock_.now_us(); | ||
| 245 | retire_locked(now); | ||
| 246 | last_presented_ = std::max(last_presented_, token); | ||
| 247 | if (!interval_set_) { // not paced: shows at once | ||
| 248 | for (Record& r : records_) | ||
| 249 | if (r.token == token) { r.presented = r.displayed = true; r.present_us = r.display_us = now; } | ||
| 250 | return; | ||
| 251 | } | ||
| 252 | // The vblank after now, and not before the one after the previous queued frame. | ||
| 253 | const std::uint64_t since = now > epoch_us_ ? now - epoch_us_ : 0; | ||
| 254 | std::uint64_t flip = epoch_us_ + (since / period_us_ + 1) * period_us_; | ||
| 255 | if (!pending_.empty()) flip = std::max(flip, pending_.back().flip_us + period_us_); | ||
| 256 | pending_.push_back(Pending{token, flip}); | ||
| 257 | for (Record& r : records_) | ||
| 258 | if (r.token == token) { r.presented = true; r.present_us = now; } | ||
| 259 | cv_.notify_all(); | ||
| 260 | } | ||
| 261 | // A blank present (no planes) carries no token: it is only counted as a frame to show. | ||
| 262 | void note_untokened_present() { | ||
| 263 | std::lock_guard guard(mutex_); | ||
| 264 | if (!interval_set_) return; | ||
| 265 | const std::uint64_t now = clock_.now_us(); | ||
| 266 | retire_locked(now); | ||
| 267 | const std::uint64_t since = now > epoch_us_ ? now - epoch_us_ : 0; | ||
| 268 | std::uint64_t flip = epoch_us_ + (since / period_us_ + 1) * period_us_; | ||
| 269 | if (!pending_.empty()) flip = std::max(flip, pending_.back().flip_us + period_us_); | ||
| 270 | pending_.push_back(Pending{0, flip}); | ||
| 271 | cv_.notify_all(); | ||
| 272 | } | ||
| 273 | |||
| 274 | // SuspendX: presentation stops, the registration and the frames in flight are dropped, waiters fail. | ||
| 275 | void suspend() { | ||
| 276 | std::lock_guard guard(mutex_); | ||
| 277 | suspended_ = true; | ||
| 278 | interval_set_ = armed_ = false; | ||
| 279 | pending_.clear(); | ||
| 280 | cv_.notify_all(); | ||
| 281 | } | ||
| 282 | // ResumeX: presents work again, but the interval and the origin event must be set up again. | ||
| 283 | void resume() { | ||
| 284 | std::lock_guard guard(mutex_); | ||
| 285 | suspended_ = false; | ||
| 286 | interval_set_ = armed_ = false; | ||
| 287 | cv_.notify_all(); | ||
| 288 | } | ||
| 289 | |||
| 290 | bool suspended() const { std::lock_guard guard(mutex_); return suspended_; } | ||
| 291 | std::uint64_t tokens_issued() const { std::lock_guard guard(mutex_); return issued_; } | ||
| 292 | std::uint32_t frames_in_flight() { | ||
| 293 | std::lock_guard guard(mutex_); | ||
| 294 | retire_locked(clock_.now_us()); | ||
| 295 | return std::uint32_t(pending_.size()); | ||
| 296 | } | ||
| 297 | std::uint32_t period_us() const { std::lock_guard guard(mutex_); return period_us_; } | ||
| 298 | bool registered() const { std::lock_guard guard(mutex_); return interval_set_ && armed_; } | ||
| 299 | |||
| 300 | private: | ||
| 301 | struct Pending { | ||
| 302 | std::uint64_t token; | ||
| 303 | std::uint64_t flip_us; | ||
| 304 | }; | ||
| 305 | struct Record { | ||
| 306 | std::uint64_t token = 0, origin_us = 0, present_us = 0, display_us = 0; | ||
| 307 | bool presented = false, displayed = false; | ||
| 308 | }; | ||
| 309 | |||
| 310 | // Frames whose vblank has come are shown. | ||
| 311 | void retire_locked(std::uint64_t now) { | ||
| 312 | while (!pending_.empty() && pending_.front().flip_us <= now) { | ||
| 313 | for (Record& r : records_) | ||
| 314 | if (r.token == pending_.front().token && pending_.front().token) { | ||
| 315 | r.displayed = true; | ||
| 316 | r.display_us = pending_.front().flip_us; | ||
| 317 | } | ||
| 318 | pending_.pop_front(); | ||
| 319 | } | ||
| 320 | } | ||
| 321 | |||
| 322 | std::int32_t wait_locked(std::unique_lock<std::mutex>& lock, std::uint32_t timeout_ms, std::uint64_t* token) { | ||
| 323 | const std::uint64_t start = clock_.now_us(); | ||
| 324 | const std::uint64_t give_up = timeout_ms == infinite_timeout_ms ? no_deadline | ||
| 325 | : start + std::uint64_t(timeout_ms) * 1000; | ||
| 326 | for (;;) { | ||
| 327 | if (shutdown_ || suspended_ || !interval_set_ || !armed_) return pacer_hr::invalid_state; | ||
| 328 | const std::uint64_t now = clock_.now_us(); | ||
| 329 | retire_locked(now); | ||
| 330 | const std::uint64_t first = epoch_us_ + offset_us_; // origin 0 | ||
| 331 | const std::int64_t next = last_origin_ + 1; | ||
| 332 | const std::uint64_t next_us = first + std::uint64_t(next) * period_us_; | ||
| 333 | std::uint64_t wake = next_us; | ||
| 334 | if (now >= next_us) { | ||
| 335 | if (pending_.size() < latency_) { | ||
| 336 | const std::int64_t newest = std::int64_t((now - first) / period_us_); | ||
| 337 | const std::int64_t origin = std::max(next, newest); | ||
| 338 | last_origin_ = origin; | ||
| 339 | *token = ++issued_; | ||
| 340 | Record record; | ||
| 341 | record.token = *token; | ||
| 342 | record.origin_us = first + std::uint64_t(origin) * period_us_; | ||
| 343 | records_.push_back(record); | ||
| 344 | if (records_.size() > 64) records_.pop_front(); | ||
| 345 | return pacer_hr::ok; | ||
| 346 | } | ||
| 347 | wake = pending_.front().flip_us; // blocked by the latency: until the oldest frame is shown | ||
| 348 | } | ||
| 349 | if (give_up != no_deadline && now >= give_up) return pacer_hr::timeout; | ||
| 350 | clock_.wait(lock, cv_, give_up == no_deadline ? wake : std::min(wake, give_up)); | ||
| 351 | } | ||
| 352 | } | ||
| 353 | |||
| 354 | FrameClock& clock_; | ||
| 355 | mutable std::mutex mutex_; | ||
| 356 | std::condition_variable cv_; | ||
| 357 | bool shutdown_ = false; | ||
| 358 | std::uint32_t waiters_ = 0; | ||
| 359 | |||
| 360 | bool suspended_ = false; | ||
| 361 | bool interval_set_ = false; | ||
| 362 | bool armed_ = false; | ||
| 363 | std::uint32_t period_us_ = 0; | ||
| 364 | std::uint32_t latency_ = 1; | ||
| 365 | std::uint32_t offset_us_ = 0; | ||
| 366 | std::uint64_t epoch_us_ = 0; | ||
| 367 | std::int64_t last_origin_ = 0; // origin 0 is the epoch itself, which has passed | ||
| 368 | std::uint64_t issued_ = 0; | ||
| 369 | std::uint64_t last_presented_ = 0; | ||
| 370 | std::deque<Pending> pending_; | ||
| 371 | std::deque<Record> records_; | ||
| 372 | }; | ||
| 373 | |||
| 374 | } // namespace leonida | ||
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index c0248e819..9ceccf53a 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp | |||
| @@ -49,11 +49,11 @@ int usage() { | |||
| 49 | int wmain(int argc, wchar_t** argv) { | 49 | int wmain(int argc, wchar_t** argv) { |
| 50 | std::filesystem::path image, game_dir; | 50 | std::filesystem::path image, game_dir; |
| 51 | bool dry_run = false, init = false, run = false, trace_d3d12 = false; | 51 | bool dry_run = false, init = false, run = false, trace_d3d12 = false; |
| 52 | std::vector<std::string> game_args; | 52 | std::vector<std::wstring> game_args; // wide: path::string() throws on a character the code page lacks (RT-84) |
| 53 | for (int i = 1; i < argc; ++i) { | 53 | for (int i = 1; i < argc; ++i) { |
| 54 | std::wstring a = argv[i]; | 54 | std::wstring a = argv[i]; |
| 55 | if (a == L"--") { | 55 | if (a == L"--") { |
| 56 | for (++i; i < argc; ++i) game_args.push_back(std::filesystem::path(argv[i]).string()); | 56 | for (++i; i < argc; ++i) game_args.push_back(argv[i]); |
| 57 | break; | 57 | break; |
| 58 | } | 58 | } |
| 59 | if (a == L"--dry-run") dry_run = true; | 59 | if (a == L"--dry-run") dry_run = true; |
| @@ -104,7 +104,7 @@ int wmain(int argc, wchar_t** argv) { | |||
| 104 | std::fprintf(stderr, "error: %s has no entry point\n", main_module.name.c_str()); | 104 | std::fprintf(stderr, "error: %s has no entry point\n", main_module.name.c_str()); |
| 105 | return 1; | 105 | return 1; |
| 106 | } | 106 | } |
| 107 | rt::process::set_command_line(image.filename().string(), game_args); | 107 | rt::process::set_command_line(image.filename().wstring(), game_args); |
| 108 | std::printf("running %s\n", main_module.name.c_str()); | 108 | std::printf("running %s\n", main_module.name.c_str()); |
| 109 | std::fflush(stdout); | 109 | std::fflush(stdout); |
| 110 | const int code = rt::process::run_entry(main_module.base + main_module.entry_rva); | 110 | const int code = rt::process::run_entry(main_module.base + main_module.entry_rva); |
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 8a222c86f..0bb6e54f2 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -891,6 +891,7 @@ public: | |||
| 891 | 891 | ||
| 892 | private: | 892 | private: |
| 893 | friend class VulkanQueue; | 893 | friend class VulkanQueue; |
| 894 | friend class VulkanPresenter; | ||
| 894 | 895 | ||
| 895 | const ResourceBinding* resolve(ResourceId id) const { | 896 | const ResourceBinding* resolve(ResourceId id) const { |
| 896 | const auto it = resources_.find(id); | 897 | const auto it = resources_.find(id); |
diff --git a/runtime/src/vulkan_depth24.h b/runtime/src/vulkan_depth24.h index da57d45b4..1bc9c8ba5 100644 --- a/runtime/src/vulkan_depth24.h +++ b/runtime/src/vulkan_depth24.h | |||
| @@ -1,3 +1,6 @@ | |||
| 1 | // Also the repack of 96-bit R32G32B32 texels, held in R32G32B32A32 images (Depth24Job::rgb96, D3D-62): the same | ||
| 2 | // scratch, parts and lifetime, with texels of three words in the footprint and four in the scratch. | ||
| 3 | // | ||
| 1 | // The compute pass behind D24_UNORM_S8_UINT on a GPU without D24 (depth24.h, depth24_convert.comp). A copy between a | 4 | // The compute pass behind D24_UNORM_S8_UINT on a GPU without D24 (depth24.h, depth24_convert.comp). A copy between a |
| 2 | // footprint and the depth aspect of such a texture goes through a scratch buffer of floats: an upload converts the | 5 | // footprint and the depth aspect of such a texture goes through a scratch buffer of floats: an upload converts the |
| 3 | // footprint's words into the scratch and a copy command moves the scratch into the image; a readback copies the image | 6 | // footprint's words into the scratch and a copy command moves the scratch into the image; a readback copies the image |
| @@ -54,22 +57,26 @@ public: | |||
| 54 | // larger, so no copy is too big for the device. `whole.scratch` and `whole.scratch_offset` tell the copy where | 57 | // larger, so no copy is too big for the device. `whole.scratch` and `whole.scratch_offset` tell the copy where |
| 55 | // the floats are. | 58 | // the floats are. |
| 56 | bool prepare(Depth24Job& whole, std::vector<Depth24Job>& parts) { | 59 | bool prepare(Depth24Job& whole, std::vector<Depth24Job>& parts) { |
| 60 | // A row of the footprint is `aw` words wide and a row of the scratch `sw` (a texel is one word of each for | ||
| 61 | // D24, three and four for 96-bit texels). | ||
| 62 | const std::uint64_t aw = std::uint64_t(whole.width) * whole.app_texel_words(); | ||
| 63 | const std::uint64_t sw = std::uint64_t(whole.width) * whole.scratch_texel_words(); | ||
| 57 | if (!loaded_ || !make_pipeline() || !whole.app || !whole.width || !whole.height || !whole.slices || | 64 | if (!loaded_ || !make_pipeline() || !whole.app || !whole.width || !whole.height || !whole.slices || |
| 58 | whole.app_offset % 4 || whole.app_row_words < whole.width || | 65 | whole.app_offset % 4 || whole.app_row_words < aw || |
| 59 | whole.app_slice_words < std::uint64_t(whole.app_row_words) * (whole.height - 1) + whole.width) | 66 | whole.app_slice_words < std::uint64_t(whole.app_row_words) * (whole.height - 1) + aw) |
| 60 | return false; | 67 | return false; |
| 61 | const std::uint64_t w = whole.width, h = whole.height, s = whole.slices; | 68 | const std::uint64_t h = whole.height, s = whole.slices; |
| 62 | const std::uint64_t span_words = (s - 1) * whole.app_slice_words + (h - 1) * whole.app_row_words + w; | 69 | const std::uint64_t span_words = (s - 1) * whole.app_slice_words + (h - 1) * whole.app_row_words + aw; |
| 63 | if (whole.app_offset + span_words * 4 > whole.app_size) return false; | 70 | if (whole.app_offset + span_words * 4 > whole.app_size) return false; |
| 64 | if (!whole.scratch && !scratch(w * h * s * 4, whole.scratch, whole.scratch_offset)) return false; | 71 | if (!whole.scratch && !scratch(sw * h * s * 4, whole.scratch, whole.scratch_offset)) return false; |
| 65 | 72 | ||
| 66 | // A window starts at a multiple of 256 (the largest minStorageBufferOffsetAlignment), which costs up to 252 bytes. | 73 | // A window starts at a multiple of 256 (the largest minStorageBufferOffsetAlignment), which costs up to 252 bytes. |
| 67 | constexpr std::uint64_t slack = 256; | 74 | constexpr std::uint64_t slack = 256; |
| 68 | const std::uint64_t limit = max_range_; | 75 | const std::uint64_t limit = max_range_; |
| 69 | const auto app_bytes = [&](std::uint64_t slices, std::uint64_t rows) { | 76 | const auto app_bytes = [&](std::uint64_t slices, std::uint64_t rows) { |
| 70 | return ((slices - 1) * whole.app_slice_words + (rows - 1) * whole.app_row_words + w) * 4 + slack; | 77 | return ((slices - 1) * whole.app_slice_words + (rows - 1) * whole.app_row_words + aw) * 4 + slack; |
| 71 | }; | 78 | }; |
| 72 | const auto scratch_bytes = [&](std::uint64_t slices, std::uint64_t rows) { return slices * rows * w * 4 + slack; }; | 79 | const auto scratch_bytes = [&](std::uint64_t slices, std::uint64_t rows) { return slices * rows * sw * 4 + slack; }; |
| 73 | std::uint64_t slices_per_part = 0; // 0: a slice alone is too big | 80 | std::uint64_t slices_per_part = 0; // 0: a slice alone is too big |
| 74 | while (slices_per_part < s && app_bytes(slices_per_part + 1, h) <= limit && scratch_bytes(slices_per_part + 1, h) <= limit) | 81 | while (slices_per_part < s && app_bytes(slices_per_part + 1, h) <= limit && scratch_bytes(slices_per_part + 1, h) <= limit) |
| 75 | ++slices_per_part; | 82 | ++slices_per_part; |
| @@ -89,7 +96,7 @@ public: | |||
| 89 | part.slices = std::uint32_t(n); | 96 | part.slices = std::uint32_t(n); |
| 90 | part.height = std::uint32_t(rows); | 97 | part.height = std::uint32_t(rows); |
| 91 | part.app_offset = whole.app_offset + (z * whole.app_slice_words + y * whole.app_row_words) * 4; | 98 | part.app_offset = whole.app_offset + (z * whole.app_slice_words + y * whole.app_row_words) * 4; |
| 92 | part.scratch_offset = whole.scratch_offset + ((z * h + y) * w) * 4; | 99 | part.scratch_offset = whole.scratch_offset + ((z * h + y) * sw) * 4; |
| 93 | if (!make_part(part, whole.app_size)) return false; | 100 | if (!make_part(part, whole.app_size)) return false; |
| 94 | parts.push_back(part); | 101 | parts.push_back(part); |
| 95 | } | 102 | } |
| @@ -103,8 +110,8 @@ public: | |||
| 103 | void run(VkCommandBuffer command_buffer, const Depth24Job& job) const { | 110 | void run(VkCommandBuffer command_buffer, const Depth24Job& job) const { |
| 104 | struct Push { | 111 | struct Push { |
| 105 | std::uint32_t width, height, slices, to_float, app_base, app_row, app_slice, scratch_base; | 112 | std::uint32_t width, height, slices, to_float, app_base, app_row, app_slice, scratch_base; |
| 106 | } push{job.width, job.height, job.slices, job.to_float ? 1u : 0u, job.app_base_words, job.app_row_words, | 113 | } push{job.width, job.height, job.slices, (job.to_float ? 1u : 0u) | (job.rgb96 ? 2u : 0u), job.app_base_words, |
| 107 | job.app_slice_words, job.scratch_base_words}; | 114 | job.app_row_words, job.app_slice_words, job.scratch_base_words}; |
| 108 | vk_.vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_); | 115 | vk_.vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_); |
| 109 | vk_.vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_layout_, 0, 1, &job.set, 0, | 116 | vk_.vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_layout_, 0, 1, &job.set, 0, |
| 110 | nullptr); | 117 | nullptr); |
| @@ -238,10 +245,11 @@ private: | |||
| 238 | bool make_part(Depth24Job& part, std::uint64_t app_size) { | 245 | bool make_part(Depth24Job& part, std::uint64_t app_size) { |
| 239 | constexpr std::uint64_t alignment = 256; | 246 | constexpr std::uint64_t alignment = 256; |
| 240 | const std::uint64_t span = ((std::uint64_t(part.slices) - 1) * part.app_slice_words + | 247 | const std::uint64_t span = ((std::uint64_t(part.slices) - 1) * part.app_slice_words + |
| 241 | (std::uint64_t(part.height) - 1) * part.app_row_words + part.width) * 4; | 248 | (std::uint64_t(part.height) - 1) * part.app_row_words + |
| 249 | std::uint64_t(part.width) * part.app_texel_words()) * 4; | ||
| 242 | const std::uint64_t window = part.app_offset / alignment * alignment; | 250 | const std::uint64_t window = part.app_offset / alignment * alignment; |
| 243 | const std::uint64_t range = part.app_offset - window + span; | 251 | const std::uint64_t range = part.app_offset - window + span; |
| 244 | const std::uint64_t tight = std::uint64_t(part.width) * part.height * part.slices * 4; | 252 | const std::uint64_t tight = std::uint64_t(part.width) * part.scratch_texel_words() * part.height * part.slices * 4; |
| 245 | const std::uint64_t scratch_window = part.scratch_offset / alignment * alignment; | 253 | const std::uint64_t scratch_window = part.scratch_offset / alignment * alignment; |
| 246 | const std::uint64_t scratch_range = part.scratch_offset - scratch_window + tight; | 254 | const std::uint64_t scratch_range = part.scratch_offset - scratch_window + tight; |
| 247 | if (window + range > app_size) return false; | 255 | if (window + range > app_size) return false; |
diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h new file mode 100644 index 000000000..98a8d93c9 --- /dev/null +++ b/runtime/src/vulkan_present.h | |||
| @@ -0,0 +1,700 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | // Puts a D3D12 back buffer on screen through a Vulkan swapchain, for whichever front end the game turns out to use | ||
| 4 | // (DXGI or the console's PresentX; docs/d3d12_vulkan.md question 6). The back buffer is an ordinary texture in the | ||
| 5 | // D3D12 PRESENT (common) state; present() blits it to the acquired swapchain image, scaling if the window size | ||
| 6 | // differs, and queues the present. The application's own work reaches the same VkQueue first, so every draw it | ||
| 7 | // submitted before present() is in the frame. | ||
| 8 | // | ||
| 9 | // Needs VK_KHR_swapchain on the device and a surface from the caller (which this object then owns). The swapchain | ||
| 10 | // is recreated when the window changes size or the surface reports it out of date; a minimised window skips | ||
| 11 | // presents. Two frames are in flight; present() waits for the older one before reusing its command buffer. | ||
| 12 | // | ||
| 13 | // Beyond the plain blit (the Xbox PresentX path, docs/d3d12_vulkan.md section 4.5): a source rectangle (only that part | ||
| 14 | // of the back buffer is scaled to the window), a blank present (the window cleared to black), a swapchain format | ||
| 15 | // that follows the source (an sRGB source gets an sRGB swapchain, so the blit's decode and encode cancel; every | ||
| 16 | // other source gets a UNORM one), and an optional capture that copies each presented frame to host memory. | ||
| 17 | |||
| 18 | #include "vulkan_backend.h" | ||
| 19 | |||
| 20 | #include <algorithm> | ||
| 21 | #include <array> | ||
| 22 | #include <cstdint> | ||
| 23 | #include <cstdio> | ||
| 24 | #include <cstring> | ||
| 25 | #include <functional> | ||
| 26 | #include <map> | ||
| 27 | #include <vector> | ||
| 28 | |||
| 29 | namespace leonida { | ||
| 30 | |||
| 31 | // A sub-rectangle of a back buffer in texels; left/top inclusive, right/bottom exclusive (D3D12_RECT's convention). | ||
| 32 | struct PresentRect { | ||
| 33 | std::int32_t left, top, right, bottom; | ||
| 34 | }; | ||
| 35 | |||
| 36 | namespace detail { | ||
| 37 | inline bool is_srgb_format(VkFormat format) { | ||
| 38 | switch (format) { | ||
| 39 | case VK_FORMAT_R8_SRGB: case VK_FORMAT_R8G8_SRGB: case VK_FORMAT_R8G8B8_SRGB: case VK_FORMAT_B8G8R8_SRGB: | ||
| 40 | case VK_FORMAT_R8G8B8A8_SRGB: case VK_FORMAT_B8G8R8A8_SRGB: case VK_FORMAT_A8B8G8R8_SRGB_PACK32: | ||
| 41 | return true; | ||
| 42 | default: | ||
| 43 | return false; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | // What vkCmdBlitImage cannot read: integer and block-compressed colour formats. | ||
| 47 | inline bool blit_unsupported_format(VkFormat format) { | ||
| 48 | switch (format) { | ||
| 49 | case VK_FORMAT_R8_UINT: case VK_FORMAT_R8_SINT: case VK_FORMAT_R8G8_UINT: case VK_FORMAT_R8G8_SINT: | ||
| 50 | case VK_FORMAT_R8G8B8A8_UINT: case VK_FORMAT_R8G8B8A8_SINT: case VK_FORMAT_R16_UINT: case VK_FORMAT_R16_SINT: | ||
| 51 | case VK_FORMAT_R16G16_UINT: case VK_FORMAT_R16G16_SINT: case VK_FORMAT_R16G16B16A16_UINT: | ||
| 52 | case VK_FORMAT_R16G16B16A16_SINT: case VK_FORMAT_R32_UINT: case VK_FORMAT_R32_SINT: case VK_FORMAT_R32G32_UINT: | ||
| 53 | case VK_FORMAT_R32G32_SINT: case VK_FORMAT_R32G32B32A32_UINT: case VK_FORMAT_R32G32B32A32_SINT: | ||
| 54 | case VK_FORMAT_A2B10G10R10_UINT_PACK32: case VK_FORMAT_UNDEFINED: | ||
| 55 | return true; | ||
| 56 | default: | ||
| 57 | return format >= VK_FORMAT_BC1_RGB_UNORM_BLOCK && format <= VK_FORMAT_BC7_SRGB_BLOCK; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } // namespace detail | ||
| 61 | |||
| 62 | class VulkanPresenter { | ||
| 63 | public: | ||
| 64 | VulkanPresenter(VulkanContext& context, PFN_vkGetInstanceProcAddr get_instance, VkInstance instance, | ||
| 65 | VkPhysicalDevice physical, VkQueue queue, std::uint32_t family, VkSurfaceKHR surface) | ||
| 66 | : context_(context), instance_(instance), physical_(physical), queue_(queue), surface_(surface) { | ||
| 67 | #define LEONIDA_INSTANCE_FN(name) name##_ = reinterpret_cast<PFN_##name>(get_instance(instance, #name)); | ||
| 68 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceSurfaceSupportKHR) | ||
| 69 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceSurfaceCapabilitiesKHR) | ||
| 70 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceSurfaceFormatsKHR) | ||
| 71 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceSurfacePresentModesKHR) | ||
| 72 | LEONIDA_INSTANCE_FN(vkDestroySurfaceKHR) | ||
| 73 | LEONIDA_INSTANCE_FN(vkGetDeviceProcAddr) | ||
| 74 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceMemoryProperties) | ||
| 75 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceFormatProperties) | ||
| 76 | #undef LEONIDA_INSTANCE_FN | ||
| 77 | if (!vkGetDeviceProcAddr_ || !vkDestroySurfaceKHR_) return; | ||
| 78 | const VkDevice device = context.device(); | ||
| 79 | #define LEONIDA_DEVICE_FN(name) name##_ = reinterpret_cast<PFN_##name>(vkGetDeviceProcAddr_(device, #name)); | ||
| 80 | LEONIDA_DEVICE_FN(vkCreateSwapchainKHR) | ||
| 81 | LEONIDA_DEVICE_FN(vkDestroySwapchainKHR) | ||
| 82 | LEONIDA_DEVICE_FN(vkGetSwapchainImagesKHR) | ||
| 83 | LEONIDA_DEVICE_FN(vkAcquireNextImageKHR) | ||
| 84 | LEONIDA_DEVICE_FN(vkQueuePresentKHR) | ||
| 85 | LEONIDA_DEVICE_FN(vkCmdBlitImage) | ||
| 86 | LEONIDA_DEVICE_FN(vkCmdClearColorImage) | ||
| 87 | LEONIDA_DEVICE_FN(vkCreateFence) | ||
| 88 | LEONIDA_DEVICE_FN(vkDestroyFence) | ||
| 89 | LEONIDA_DEVICE_FN(vkWaitForFences) | ||
| 90 | LEONIDA_DEVICE_FN(vkResetFences) | ||
| 91 | #undef LEONIDA_DEVICE_FN | ||
| 92 | if (!vkCreateSwapchainKHR_ || !vkQueuePresentKHR_ || !vkCmdBlitImage_ || !vkCreateFence_) return; | ||
| 93 | |||
| 94 | VkBool32 supported = VK_FALSE; | ||
| 95 | vkGetPhysicalDeviceSurfaceSupportKHR_(physical, family, surface, &supported); | ||
| 96 | if (!supported) return; | ||
| 97 | |||
| 98 | auto& vk = context.vk; | ||
| 99 | VkCommandPoolCreateInfo pool{VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO}; | ||
| 100 | pool.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; | ||
| 101 | pool.queueFamilyIndex = family; | ||
| 102 | if (vk.vkCreateCommandPool(device, &pool, nullptr, &pool_) != VK_SUCCESS) return; | ||
| 103 | for (Frame& frame : frames_) { | ||
| 104 | VkCommandBufferAllocateInfo allocate{VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO}; | ||
| 105 | allocate.commandPool = pool_; | ||
| 106 | allocate.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; | ||
| 107 | allocate.commandBufferCount = 1; | ||
| 108 | VkFenceCreateInfo fence{VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; | ||
| 109 | fence.flags = VK_FENCE_CREATE_SIGNALED_BIT; | ||
| 110 | VkSemaphoreCreateInfo semaphore{VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO}; | ||
| 111 | if (vk.vkAllocateCommandBuffers(device, &allocate, &frame.commands) != VK_SUCCESS || | ||
| 112 | vkCreateFence_(device, &fence, nullptr, &frame.done) != VK_SUCCESS || | ||
| 113 | vk.vkCreateSemaphore(device, &semaphore, nullptr, &frame.acquired) != VK_SUCCESS) | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | ok_ = recreate(true, false); | ||
| 117 | } | ||
| 118 | |||
| 119 | ~VulkanPresenter() { | ||
| 120 | auto& vk = context_.vk; | ||
| 121 | const VkDevice device = context_.device(); | ||
| 122 | { | ||
| 123 | std::lock_guard lock(context_.submit_mutex()); | ||
| 124 | vk.vkQueueWaitIdle(queue_); | ||
| 125 | } | ||
| 126 | release(swapchain_); | ||
| 127 | swapchain_ = VK_NULL_HANDLE; | ||
| 128 | release_capture(); | ||
| 129 | for (Frame& frame : frames_) { | ||
| 130 | if (frame.done) vkDestroyFence_(device, frame.done, nullptr); | ||
| 131 | if (frame.acquired) vk.vkDestroySemaphore(device, frame.acquired, nullptr); | ||
| 132 | } | ||
| 133 | if (pool_) vk.vkDestroyCommandPool(device, pool_, nullptr); | ||
| 134 | if (surface_ && vkDestroySurfaceKHR_) vkDestroySurfaceKHR_(instance_, surface_, nullptr); | ||
| 135 | } | ||
| 136 | |||
| 137 | VulkanPresenter(const VulkanPresenter&) = delete; | ||
| 138 | VulkanPresenter& operator=(const VulkanPresenter&) = delete; | ||
| 139 | |||
| 140 | bool ok() const { return ok_; } | ||
| 141 | VkExtent2D extent() const { return extent_; } | ||
| 142 | VkFormat format() const { return format_; } | ||
| 143 | std::uint64_t presented() const { return presented_; } | ||
| 144 | |||
| 145 | // Blits the colour texture `source` (mip 0, layer 0, in D3D12's PRESENT state) to the window and presents it. | ||
| 146 | // `source_rect`, when given, limits the blit to that part of the texture (stretched to the whole window); it | ||
| 147 | // must be non-empty and inside the texture (VK_ERROR_UNKNOWN otherwise). Integer, block-compressed and | ||
| 148 | // multisampled sources are refused (VK_ERROR_FORMAT_NOT_SUPPORTED); the other formats are converted by the blit. | ||
| 149 | // The blit filters bilinearly, so with a rectangle the outermost texels blend with their neighbours outside it. | ||
| 150 | // vsync: FIFO; otherwise MAILBOX or IMMEDIATE when the surface has them. VK_SUCCESS also covers a skipped | ||
| 151 | // present (minimised window) and one the surface reported out of date (the swapchain is recreated next time); | ||
| 152 | // VK_ERROR_OUT_OF_DATE_KHR means recreating the swapchain failed, and the next present tries again. | ||
| 153 | // | ||
| 154 | // A failure after an image was acquired (the blit's submission) is recovered here, so the next present does not | ||
| 155 | // wait on a fence nothing will signal: an empty batch takes the acquire semaphore and signals the frame's fence, | ||
| 156 | // and the swapchain is recreated next time, which gives back the image that was never presented. If even that | ||
| 157 | // batch fails, the presenter stops (ok() is false) and its owner has to make a new one. | ||
| 158 | VkResult present(ResourceId source, bool vsync, const PresentRect* source_rect = nullptr) { | ||
| 159 | ResourceBinding binding; | ||
| 160 | { | ||
| 161 | std::shared_lock lock(context_.resource_mutex_); // the resource table (vulkan_backend.h) | ||
| 162 | const ResourceBinding* found = context_.resolve(source); | ||
| 163 | if (!found || !found->image || found->aspects != VK_IMAGE_ASPECT_COLOR_BIT) return VK_ERROR_UNKNOWN; | ||
| 164 | binding = *found; | ||
| 165 | } | ||
| 166 | // Multisampled sources (no resolve into a temporary yet), integer formats (a UNORM swapchain cannot take them) | ||
| 167 | // and anything the GPU cannot blit from are refused. | ||
| 168 | if (binding.samples > 1 || detail::blit_unsupported_format(binding.format) || | ||
| 169 | !(format_features(binding.format) & VK_FORMAT_FEATURE_BLIT_SRC_BIT)) | ||
| 170 | return VK_ERROR_FORMAT_NOT_SUPPORTED; | ||
| 171 | if (source_rect && (source_rect->left < 0 || source_rect->top < 0 || source_rect->left >= source_rect->right || | ||
| 172 | source_rect->top >= source_rect->bottom || | ||
| 173 | std::uint32_t(source_rect->right) > binding.width || | ||
| 174 | std::uint32_t(source_rect->bottom) > binding.height)) | ||
| 175 | return VK_ERROR_UNKNOWN; | ||
| 176 | return submit(&binding, source, vsync, source_rect, detail::is_srgb_format(binding.format)); | ||
| 177 | } | ||
| 178 | |||
| 179 | // Polled while a present waits for a swapchain image (every 50 ms): when it returns true the present gives up with | ||
| 180 | // VK_NOT_READY, so a caller that is being suspended is never held by a window that stopped taking frames. | ||
| 181 | void set_abort(std::function<bool()> abort) { abort_ = std::move(abort); } | ||
| 182 | |||
| 183 | // Presents a black frame: the swapchain image cleared, nothing read. PresentX with no planes. | ||
| 184 | VkResult present_blank(bool vsync) { return submit(nullptr, ResourceId{}, vsync, nullptr, swapchain_srgb_); } | ||
| 185 | |||
| 186 | // From now on every presented frame is also copied to host memory (the swapchain gets TRANSFER_SRC usage), for | ||
| 187 | // read_back(). False when the surface cannot do it or its format is not 8 bits per channel with 4 channels. | ||
| 188 | // Call before the first present. | ||
| 189 | bool enable_capture() { | ||
| 190 | if (!ok_) return false; | ||
| 191 | capture_ = true; | ||
| 192 | stale_ = true; | ||
| 193 | if (!recreate(vsync_, swapchain_srgb_) || (swapchain_ && !capture_buffer_)) { | ||
| 194 | capture_ = false; | ||
| 195 | return false; | ||
| 196 | } | ||
| 197 | return true; | ||
| 198 | } | ||
| 199 | |||
| 200 | // The last presented frame as the swapchain stores it (4 bytes a texel, rows tightly packed, format() order). | ||
| 201 | // Waits for the frame to finish. False when nothing was presented since enable_capture(). | ||
| 202 | bool read_back(std::vector<std::uint8_t>& pixels, VkExtent2D* extent, VkFormat* format) { | ||
| 203 | if (!capture_ || !capture_ptr_ || !captured_) return false; | ||
| 204 | const Frame& frame = frames_[(frame_index_ + frames_.size() - 1) % frames_.size()]; | ||
| 205 | vkWaitForFences_(context_.device(), 1, &frame.done, VK_TRUE, UINT64_MAX); | ||
| 206 | pixels.assign(capture_ptr_, capture_ptr_ + std::size_t(captured_extent_.width) * captured_extent_.height * 4); | ||
| 207 | if (extent) *extent = captured_extent_; | ||
| 208 | if (format) *format = captured_format_; | ||
| 209 | return true; | ||
| 210 | } | ||
| 211 | |||
| 212 | // Shared by present() and present_blank(). `binding` null: clear instead of blit. | ||
| 213 | VkResult submit(const ResourceBinding* binding, ResourceId source, bool vsync, const PresentRect* source_rect, | ||
| 214 | bool srgb) { | ||
| 215 | if (!ok_) return VK_ERROR_INITIALIZATION_FAILED; | ||
| 216 | if (vsync != vsync_ || stale_ || srgb != swapchain_srgb_) { | ||
| 217 | // The swapchain's colour encoding is only recorded once the new swapchain exists, so a failed | ||
| 218 | // recreate is tried again at the next present. | ||
| 219 | if (!recreate(vsync, srgb)) return VK_ERROR_OUT_OF_DATE_KHR; | ||
| 220 | } | ||
| 221 | if (!swapchain_) { // minimised: try again next frame | ||
| 222 | stale_ = true; | ||
| 223 | return VK_SUCCESS; | ||
| 224 | } | ||
| 225 | if (binding && !presentable(source)) return VK_ERROR_UNKNOWN; // checked before an image is acquired for it | ||
| 226 | if (binding && !(format_features(format_) & VK_FORMAT_FEATURE_BLIT_DST_BIT)) return VK_ERROR_FORMAT_NOT_SUPPORTED; | ||
| 227 | auto& vk = context_.vk; | ||
| 228 | const VkDevice device = context_.device(); | ||
| 229 | Frame& frame = frames_[frame_index_ % frames_.size()]; | ||
| 230 | if (const VkResult waited = vkWaitForFences_(device, 1, &frame.done, VK_TRUE, UINT64_MAX); waited != VK_SUCCESS) | ||
| 231 | return stop(waited); | ||
| 232 | |||
| 233 | std::uint32_t image = 0; | ||
| 234 | VkResult acquired = acquire(frame.acquired, &image); | ||
| 235 | if (acquired == VK_ERROR_OUT_OF_DATE_KHR) { | ||
| 236 | if (!recreate(vsync, swapchain_srgb_)) return acquired; | ||
| 237 | if (!swapchain_) return VK_SUCCESS; | ||
| 238 | acquired = acquire(frame.acquired, &image); | ||
| 239 | } | ||
| 240 | if (acquired != VK_SUCCESS && acquired != VK_SUBOPTIMAL_KHR) return acquired; // nothing was acquired | ||
| 241 | if (acquired == VK_SUBOPTIMAL_KHR) stale_ = true; | ||
| 242 | |||
| 243 | bool recorded = false; | ||
| 244 | if (binding) { | ||
| 245 | // The back buffer's layout is tracked like any other texture; this holds the context while it is read. | ||
| 246 | std::shared_lock lock(context_.resource_mutex_); // the resource table (vulkan_backend.h) | ||
| 247 | const ResourceBinding* current = context_.resolve(source); | ||
| 248 | if (current && current->image && current->aspects == VK_IMAGE_ASPECT_COLOR_BIT) { | ||
| 249 | record(frame.commands, *current, source, image, source_rect); | ||
| 250 | recorded = true; | ||
| 251 | } | ||
| 252 | } else { | ||
| 253 | record_blank(frame.commands, image); | ||
| 254 | recorded = true; | ||
| 255 | } | ||
| 256 | if (!recorded) return give_back(frame, VK_ERROR_UNKNOWN); // released by another thread since presentable() | ||
| 257 | if (const VkResult reset = vkResetFences_(device, 1, &frame.done); reset != VK_SUCCESS) return stop(reset); | ||
| 258 | const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; | ||
| 259 | VkSubmitInfo submit_info{VK_STRUCTURE_TYPE_SUBMIT_INFO}; | ||
| 260 | submit_info.waitSemaphoreCount = 1; | ||
| 261 | submit_info.pWaitSemaphores = &frame.acquired; | ||
| 262 | submit_info.pWaitDstStageMask = &wait_stage; | ||
| 263 | submit_info.commandBufferCount = 1; | ||
| 264 | submit_info.pCommandBuffers = &frame.commands; | ||
| 265 | submit_info.signalSemaphoreCount = 1; | ||
| 266 | submit_info.pSignalSemaphores = &ready_[image]; // one per image: reused only once that image comes back | ||
| 267 | VkPresentInfoKHR present{VK_STRUCTURE_TYPE_PRESENT_INFO_KHR}; | ||
| 268 | present.waitSemaphoreCount = 1; | ||
| 269 | present.pWaitSemaphores = &ready_[image]; | ||
| 270 | present.swapchainCount = 1; | ||
| 271 | present.pSwapchains = &swapchain_; | ||
| 272 | present.pImageIndices = ℑ | ||
| 273 | VkResult submitted, result = VK_SUCCESS; | ||
| 274 | { | ||
| 275 | std::lock_guard lock(context_.submit_mutex()); | ||
| 276 | submitted = vk.vkQueueSubmit(queue_, 1, &submit_info, frame.done); | ||
| 277 | if (submitted == VK_SUCCESS) result = vkQueuePresentKHR_(queue_, &present); | ||
| 278 | } | ||
| 279 | if (submitted != VK_SUCCESS) return give_back(frame, submitted); | ||
| 280 | ++frame_index_; | ||
| 281 | // Only a present the engine took counts (SUBOPTIMAL is shown too). OUT_OF_DATE was not shown: the swapchain | ||
| 282 | // is recreated next time, and the caller sees VK_SUCCESS, since its work was submitted. | ||
| 283 | if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) ++presented_; | ||
| 284 | if (capture_ && capture_ptr_) { | ||
| 285 | captured_ = true; | ||
| 286 | captured_extent_ = extent_; | ||
| 287 | captured_format_ = format_; | ||
| 288 | } | ||
| 289 | if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) { | ||
| 290 | stale_ = true; | ||
| 291 | result = VK_SUCCESS; | ||
| 292 | } | ||
| 293 | return result; | ||
| 294 | } | ||
| 295 | |||
| 296 | private: | ||
| 297 | struct Frame { | ||
| 298 | VkCommandBuffer commands = VK_NULL_HANDLE; | ||
| 299 | VkFence done = VK_NULL_HANDLE; | ||
| 300 | VkSemaphore acquired = VK_NULL_HANDLE; | ||
| 301 | }; | ||
| 302 | |||
| 303 | // Whether `source` is a colour texture the device knows, so present() can refuse it before acquiring an image. | ||
| 304 | bool presentable(ResourceId source) { | ||
| 305 | std::shared_lock lock(context_.resource_mutex_); // the resource table (vulkan_backend.h) | ||
| 306 | const ResourceBinding* binding = context_.resolve(source); | ||
| 307 | return binding && binding->image && binding->aspects == VK_IMAGE_ASPECT_COLOR_BIT; | ||
| 308 | } | ||
| 309 | |||
| 310 | // An image was acquired into `frame` but its blit was not submitted. An empty batch waits on the acquire | ||
| 311 | // semaphore and signals the frame's fence, so both are usable again; the swapchain is recreated on the next | ||
| 312 | // present, which gives back the image that was never presented. Returns `failure`, the error to report. | ||
| 313 | VkResult give_back(Frame& frame, VkResult failure) { | ||
| 314 | const VkDevice device = context_.device(); | ||
| 315 | if (vkResetFences_(device, 1, &frame.done) != VK_SUCCESS) return stop(failure); | ||
| 316 | const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; | ||
| 317 | VkSubmitInfo submit_info{VK_STRUCTURE_TYPE_SUBMIT_INFO}; | ||
| 318 | submit_info.waitSemaphoreCount = 1; | ||
| 319 | submit_info.pWaitSemaphores = &frame.acquired; | ||
| 320 | submit_info.pWaitDstStageMask = &wait_stage; | ||
| 321 | VkResult submitted; | ||
| 322 | { | ||
| 323 | std::lock_guard lock(context_.submit_mutex()); | ||
| 324 | submitted = context_.vk.vkQueueSubmit(queue_, 1, &submit_info, frame.done); | ||
| 325 | } | ||
| 326 | if (submitted != VK_SUCCESS) return stop(failure); | ||
| 327 | stale_ = true; | ||
| 328 | return failure; | ||
| 329 | } | ||
| 330 | |||
| 331 | // The swapchain image as a binding, for the barrier helpers. | ||
| 332 | ResourceBinding target_of(std::uint32_t image) const { | ||
| 333 | ResourceBinding target{}; | ||
| 334 | target.image = images_[image]; | ||
| 335 | target.format = format_; | ||
| 336 | target.aspects = VK_IMAGE_ASPECT_COLOR_BIT; | ||
| 337 | return target; | ||
| 338 | } | ||
| 339 | |||
| 340 | // The frame's fence or acquire semaphore can no longer be trusted: no more presents (ok() is false), and the | ||
| 341 | // owner makes a new presenter. Returns `failure`. | ||
| 342 | VkResult stop(VkResult failure) { | ||
| 343 | ok_ = false; | ||
| 344 | return failure; | ||
| 345 | } | ||
| 346 | |||
| 347 | void record(VkCommandBuffer commands, const ResourceBinding& binding, ResourceId source, std::uint32_t image, | ||
| 348 | const PresentRect* source_rect) { | ||
| 349 | auto& vk = context_.vk; | ||
| 350 | const ReplayFunctions& fn = context_.replay_; | ||
| 351 | vk.vkResetCommandBuffer(commands, 0); | ||
| 352 | VkCommandBufferBeginInfo begin{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; | ||
| 353 | begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; | ||
| 354 | vk.vkBeginCommandBuffer(commands, &begin); | ||
| 355 | |||
| 356 | constexpr VkAccessFlags any = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT; | ||
| 357 | const VkImageSubresourceRange range{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; | ||
| 358 | // PRESENT is D3D12's common state: GENERAL for an ordinary texture. A never-written back buffer is | ||
| 359 | // zero-filled first, as any committed resource is. | ||
| 360 | const VkImageLayout common = image_layout(binding, VK_IMAGE_LAYOUT_GENERAL); | ||
| 361 | auto pieces = detail::current(&context_.tracker_, source, range, common); | ||
| 362 | detail::initialize(fn, commands, binding, source, pieces, common, &context_.tracker_); | ||
| 363 | for (const auto& piece : pieces) | ||
| 364 | detail::image_barrier(fn, commands, binding, piece.range, piece.layout, | ||
| 365 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_ACCESS_MEMORY_WRITE_BIT, | ||
| 366 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_ACCESS_TRANSFER_READ_BIT, | ||
| 367 | VK_PIPELINE_STAGE_TRANSFER_BIT); | ||
| 368 | ResourceBinding target{}; // the swapchain image, for the barrier helpers | ||
| 369 | target.image = images_[image]; | ||
| 370 | target.format = format_; | ||
| 371 | target.aspects = VK_IMAGE_ASPECT_COLOR_BIT; | ||
| 372 | // The acquire semaphore is waited at the transfer stage, so the swapchain image's barrier starts there. | ||
| 373 | detail::image_barrier(fn, commands, target, range, VK_IMAGE_LAYOUT_UNDEFINED, | ||
| 374 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TRANSFER_BIT, | ||
| 375 | VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); | ||
| 376 | |||
| 377 | const PresentRect whole{0, 0, std::int32_t(binding.width), std::int32_t(binding.height)}; | ||
| 378 | const PresentRect& src = source_rect ? *source_rect : whole; | ||
| 379 | const std::int32_t sw = src.right - src.left, sh = src.bottom - src.top; | ||
| 380 | const std::int32_t dw = std::int32_t(extent_.width), dh = std::int32_t(extent_.height); | ||
| 381 | VkImageBlit blit{}; | ||
| 382 | blit.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; | ||
| 383 | blit.srcOffsets[0] = {src.left, src.top, 0}; | ||
| 384 | blit.srcOffsets[1] = {src.right, src.bottom, 1}; | ||
| 385 | blit.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; | ||
| 386 | blit.dstOffsets[1] = {dw, dh, 1}; | ||
| 387 | vkCmdBlitImage_(commands, binding.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, images_[image], | ||
| 388 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, | ||
| 389 | sw == dw && sh == dh ? VK_FILTER_NEAREST : blit_filter(binding.format)); | ||
| 390 | |||
| 391 | finish_image(commands, image); | ||
| 392 | for (const auto& piece : pieces) | ||
| 393 | detail::image_barrier(fn, commands, binding, piece.range, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, | ||
| 394 | piece.layout, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, any, | ||
| 395 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); | ||
| 396 | vk.vkEndCommandBuffer(commands); | ||
| 397 | } | ||
| 398 | |||
| 399 | // vkGetPhysicalDeviceFormatProperties, optimal tiling, cached per format. | ||
| 400 | VkFormatFeatureFlags format_features(VkFormat format) { | ||
| 401 | const auto it = features_.find(format); | ||
| 402 | if (it != features_.end()) return it->second; | ||
| 403 | VkFormatProperties properties{}; | ||
| 404 | if (vkGetPhysicalDeviceFormatProperties_) vkGetPhysicalDeviceFormatProperties_(physical_, format, &properties); | ||
| 405 | return features_[format] = properties.optimalTilingFeatures; | ||
| 406 | } | ||
| 407 | // Scaling blits filter bilinearly when the source format allows it, else take the nearest texel. | ||
| 408 | VkFilter blit_filter(VkFormat source) { | ||
| 409 | return (format_features(source) & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT) ? VK_FILTER_LINEAR | ||
| 410 | : VK_FILTER_NEAREST; | ||
| 411 | } | ||
| 412 | |||
| 413 | VkResult acquire(VkSemaphore semaphore, std::uint32_t* image) { | ||
| 414 | for (;;) { | ||
| 415 | const VkResult result = vkAcquireNextImageKHR_(context_.device(), swapchain_, 50'000'000, semaphore, | ||
| 416 | VK_NULL_HANDLE, image); | ||
| 417 | if (result != VK_TIMEOUT && result != VK_NOT_READY) return result; | ||
| 418 | if (abort_ && abort_()) return VK_NOT_READY; | ||
| 419 | } | ||
| 420 | } | ||
| 421 | |||
| 422 | // Clears the swapchain image to opaque black. | ||
| 423 | void record_blank(VkCommandBuffer commands, std::uint32_t image) { | ||
| 424 | auto& vk = context_.vk; | ||
| 425 | const ReplayFunctions& fn = context_.replay_; | ||
| 426 | vk.vkResetCommandBuffer(commands, 0); | ||
| 427 | VkCommandBufferBeginInfo begin{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; | ||
| 428 | begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; | ||
| 429 | vk.vkBeginCommandBuffer(commands, &begin); | ||
| 430 | const VkImageSubresourceRange range{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; | ||
| 431 | detail::image_barrier(fn, commands, target_of(image), range, VK_IMAGE_LAYOUT_UNDEFINED, | ||
| 432 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TRANSFER_BIT, | ||
| 433 | VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); | ||
| 434 | const VkClearColorValue black{{0.0f, 0.0f, 0.0f, 1.0f}}; | ||
| 435 | vkCmdClearColorImage_(commands, images_[image], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &black, 1, &range); | ||
| 436 | finish_image(commands, image); | ||
| 437 | vk.vkEndCommandBuffer(commands); | ||
| 438 | } | ||
| 439 | |||
| 440 | // The swapchain image is written (TRANSFER_DST): optionally copy it to the capture buffer, then hand it to | ||
| 441 | // the presentation engine. | ||
| 442 | void finish_image(VkCommandBuffer commands, std::uint32_t image) { | ||
| 443 | const ReplayFunctions& fn = context_.replay_; | ||
| 444 | const VkImageSubresourceRange range{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; | ||
| 445 | VkImageLayout layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; | ||
| 446 | VkAccessFlags access = VK_ACCESS_TRANSFER_WRITE_BIT; | ||
| 447 | if (capture_ && capture_buffer_) { | ||
| 448 | detail::image_barrier(fn, commands, target_of(image), range, layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, | ||
| 449 | access, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT, | ||
| 450 | VK_PIPELINE_STAGE_TRANSFER_BIT); | ||
| 451 | VkBufferImageCopy copy{}; | ||
| 452 | copy.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; | ||
| 453 | copy.imageExtent = {extent_.width, extent_.height, 1}; | ||
| 454 | fn.copy_image_to_buffer(commands, images_[image], VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, capture_buffer_, 1, | ||
| 455 | ©); | ||
| 456 | layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; | ||
| 457 | access = VK_ACCESS_TRANSFER_READ_BIT; | ||
| 458 | } | ||
| 459 | detail::image_barrier(fn, commands, target_of(image), range, layout, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, access, | ||
| 460 | VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT); | ||
| 461 | } | ||
| 462 | |||
| 463 | // (Re)creates the capture buffer for the current swapchain size: host visible, coherent, persistently mapped. | ||
| 464 | bool make_capture_buffer() { | ||
| 465 | release_capture(); | ||
| 466 | auto& vk = context_.vk; | ||
| 467 | const VkDevice device = context_.device(); | ||
| 468 | capture_size_ = VkDeviceSize(extent_.width) * extent_.height * 4; | ||
| 469 | VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; | ||
| 470 | info.size = capture_size_; | ||
| 471 | info.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT; | ||
| 472 | info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; | ||
| 473 | if (vk.vkCreateBuffer(device, &info, nullptr, &capture_buffer_) != VK_SUCCESS) return false; | ||
| 474 | VkMemoryRequirements requirements{}; | ||
| 475 | vk.vkGetBufferMemoryRequirements(device, capture_buffer_, &requirements); | ||
| 476 | VkPhysicalDeviceMemoryProperties memory{}; | ||
| 477 | vkGetPhysicalDeviceMemoryProperties_(physical_, &memory); | ||
| 478 | constexpr VkMemoryPropertyFlags wanted = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; | ||
| 479 | std::uint32_t type = UINT32_MAX; | ||
| 480 | for (std::uint32_t i = 0; i < memory.memoryTypeCount; ++i) | ||
| 481 | if ((requirements.memoryTypeBits & (1u << i)) && (memory.memoryTypes[i].propertyFlags & wanted) == wanted) { | ||
| 482 | type = i; | ||
| 483 | break; | ||
| 484 | } | ||
| 485 | VkMemoryAllocateInfo allocate{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; | ||
| 486 | allocate.allocationSize = requirements.size; | ||
| 487 | allocate.memoryTypeIndex = type; | ||
| 488 | void* mapped = nullptr; | ||
| 489 | if (type == UINT32_MAX || vk.vkAllocateMemory(device, &allocate, nullptr, &capture_memory_) != VK_SUCCESS || | ||
| 490 | vk.vkBindBufferMemory(device, capture_buffer_, capture_memory_, 0) != VK_SUCCESS || | ||
| 491 | vk.vkMapMemory(device, capture_memory_, 0, VK_WHOLE_SIZE, 0, &mapped) != VK_SUCCESS) { | ||
| 492 | release_capture(); | ||
| 493 | return false; | ||
| 494 | } | ||
| 495 | capture_ptr_ = static_cast<std::uint8_t*>(mapped); | ||
| 496 | captured_ = false; | ||
| 497 | return true; | ||
| 498 | } | ||
| 499 | |||
| 500 | void release_capture() { | ||
| 501 | auto& vk = context_.vk; | ||
| 502 | const VkDevice device = context_.device(); | ||
| 503 | if (capture_memory_) vk.vkFreeMemory(device, capture_memory_, nullptr); // unmaps | ||
| 504 | if (capture_buffer_) vk.vkDestroyBuffer(device, capture_buffer_, nullptr); | ||
| 505 | capture_memory_ = VK_NULL_HANDLE; | ||
| 506 | capture_buffer_ = VK_NULL_HANDLE; | ||
| 507 | capture_ptr_ = nullptr; | ||
| 508 | captured_ = false; | ||
| 509 | } | ||
| 510 | |||
| 511 | // (Re)creates the swapchain for the surface's current size. A zero-sized surface (minimised) leaves no | ||
| 512 | // swapchain and is retried on the next present. Nothing is published until every piece exists: on any failure | ||
| 513 | // the presenter is left with no swapchain and stale_ set, so the next present tries again, and no image is ever | ||
| 514 | // acquired without its per-image semaphore. | ||
| 515 | bool recreate(bool vsync, bool srgb) { | ||
| 516 | auto& vk = context_.vk; | ||
| 517 | const VkDevice device = context_.device(); | ||
| 518 | { | ||
| 519 | std::lock_guard lock(context_.submit_mutex()); | ||
| 520 | vk.vkQueueWaitIdle(queue_); // the old swapchain's images and semaphores may still be in use | ||
| 521 | } | ||
| 522 | stale_ = true; // until this succeeds | ||
| 523 | VkSurfaceCapabilitiesKHR caps{}; | ||
| 524 | if (vkGetPhysicalDeviceSurfaceCapabilitiesKHR_(physical_, surface_, &caps) != VK_SUCCESS) return false; | ||
| 525 | VkSwapchainKHR old = swapchain_; | ||
| 526 | swapchain_ = VK_NULL_HANDLE; | ||
| 527 | if (caps.currentExtent.width == 0 || caps.currentExtent.height == 0) { | ||
| 528 | release(old); | ||
| 529 | vsync_ = vsync; | ||
| 530 | swapchain_srgb_ = srgb; | ||
| 531 | stale_ = false; | ||
| 532 | return true; | ||
| 533 | } | ||
| 534 | if (!(caps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT)) { | ||
| 535 | release(old); | ||
| 536 | return false; | ||
| 537 | } | ||
| 538 | |||
| 539 | std::uint32_t count = 0; | ||
| 540 | vkGetPhysicalDeviceSurfaceFormatsKHR_(physical_, surface_, &count, nullptr); | ||
| 541 | std::vector<VkSurfaceFormatKHR> formats(count); | ||
| 542 | vkGetPhysicalDeviceSurfaceFormatsKHR_(physical_, surface_, &count, formats.data()); | ||
| 543 | if (formats.empty()) return false; | ||
| 544 | // An sRGB source wants an sRGB swapchain (the blit decodes and encodes, which cancel); any other source | ||
| 545 | // wants UNORM (the bytes go through as they are). BGRA before RGBA, as the surface lists them. | ||
| 546 | VkSurfaceFormatKHR chosen = formats[0]; | ||
| 547 | bool found = false; | ||
| 548 | for (const VkFormat wanted : srgb ? std::array<VkFormat, 2>{VK_FORMAT_B8G8R8A8_SRGB, VK_FORMAT_R8G8B8A8_SRGB} | ||
| 549 | : std::array<VkFormat, 2>{VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM}) { | ||
| 550 | for (const auto& f : formats) | ||
| 551 | if (f.format == wanted && f.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { | ||
| 552 | chosen = f; | ||
| 553 | found = true; | ||
| 554 | break; | ||
| 555 | } | ||
| 556 | if (found) break; | ||
| 557 | } | ||
| 558 | if (!found && srgb) { | ||
| 559 | if (!warned_srgb_) std::fprintf(stderr, "[rt] present: the surface has no sRGB 8-bit format; an sRGB back buffer is shown darker\n"); | ||
| 560 | warned_srgb_ = true; | ||
| 561 | for (const auto& f : formats) | ||
| 562 | if ((f.format == VK_FORMAT_B8G8R8A8_UNORM || f.format == VK_FORMAT_R8G8B8A8_UNORM) && | ||
| 563 | f.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { | ||
| 564 | chosen = f; | ||
| 565 | break; | ||
| 566 | } | ||
| 567 | } | ||
| 568 | vkGetPhysicalDeviceSurfacePresentModesKHR_(physical_, surface_, &count, nullptr); | ||
| 569 | std::vector<VkPresentModeKHR> modes(count); | ||
| 570 | vkGetPhysicalDeviceSurfacePresentModesKHR_(physical_, surface_, &count, modes.data()); | ||
| 571 | VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR; // always available | ||
| 572 | if (!vsync) { | ||
| 573 | for (const VkPresentModeKHR wanted : {VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_IMMEDIATE_KHR}) | ||
| 574 | if (std::find(modes.begin(), modes.end(), wanted) != modes.end()) { | ||
| 575 | mode = wanted; | ||
| 576 | break; | ||
| 577 | } | ||
| 578 | } | ||
| 579 | |||
| 580 | VkSwapchainCreateInfoKHR info{VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR}; | ||
| 581 | info.surface = surface_; | ||
| 582 | info.minImageCount = std::max(caps.minImageCount + 1, 2u); | ||
| 583 | if (caps.maxImageCount) info.minImageCount = std::min(info.minImageCount, caps.maxImageCount); | ||
| 584 | info.imageFormat = chosen.format; | ||
| 585 | info.imageColorSpace = chosen.colorSpace; | ||
| 586 | info.imageExtent = caps.currentExtent; | ||
| 587 | info.imageArrayLayers = 1; | ||
| 588 | info.imageUsage = VK_IMAGE_USAGE_TRANSFER_DST_BIT; | ||
| 589 | if (capture_) { | ||
| 590 | if (!(caps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT)) return false; | ||
| 591 | info.imageUsage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; | ||
| 592 | } | ||
| 593 | info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; | ||
| 594 | info.preTransform = caps.currentTransform; | ||
| 595 | info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; | ||
| 596 | if (!(caps.supportedCompositeAlpha & info.compositeAlpha)) info.compositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR; | ||
| 597 | info.presentMode = mode; | ||
| 598 | info.clipped = VK_TRUE; | ||
| 599 | info.oldSwapchain = old; | ||
| 600 | VkSwapchainKHR fresh = VK_NULL_HANDLE; | ||
| 601 | const VkResult created = vkCreateSwapchainKHR_(device, &info, nullptr, &fresh); | ||
| 602 | release(old); // retired by the creation attempt, whether or not it succeeded | ||
| 603 | if (created != VK_SUCCESS) return false; | ||
| 604 | |||
| 605 | std::vector<VkImage> images; | ||
| 606 | std::vector<VkSemaphore> ready; | ||
| 607 | const auto discard = [&] { | ||
| 608 | for (VkSemaphore semaphore : ready) vk.vkDestroySemaphore(device, semaphore, nullptr); | ||
| 609 | vkDestroySwapchainKHR_(device, fresh, nullptr); | ||
| 610 | return false; | ||
| 611 | }; | ||
| 612 | if (vkGetSwapchainImagesKHR_(device, fresh, &count, nullptr) != VK_SUCCESS || !count) return discard(); | ||
| 613 | images.resize(count); | ||
| 614 | if (vkGetSwapchainImagesKHR_(device, fresh, &count, images.data()) != VK_SUCCESS) return discard(); | ||
| 615 | images.resize(count); | ||
| 616 | for (std::uint32_t i = 0; i < count; ++i) { | ||
| 617 | VkSemaphoreCreateInfo semaphore_info{VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO}; | ||
| 618 | VkSemaphore semaphore = VK_NULL_HANDLE; | ||
| 619 | if (vk.vkCreateSemaphore(device, &semaphore_info, nullptr, &semaphore) != VK_SUCCESS) return discard(); | ||
| 620 | ready.push_back(semaphore); | ||
| 621 | } | ||
| 622 | swapchain_ = fresh; | ||
| 623 | images_ = std::move(images); | ||
| 624 | ready_ = std::move(ready); | ||
| 625 | format_ = chosen.format; | ||
| 626 | extent_ = caps.currentExtent; | ||
| 627 | if (capture_) { | ||
| 628 | // A capture that cannot be made fails the recreation with the swapchain in place and stale_ still set. | ||
| 629 | const bool four_bytes = format_ == VK_FORMAT_B8G8R8A8_UNORM || format_ == VK_FORMAT_R8G8B8A8_UNORM || | ||
| 630 | format_ == VK_FORMAT_B8G8R8A8_SRGB || format_ == VK_FORMAT_R8G8B8A8_SRGB; | ||
| 631 | if (!four_bytes || !make_capture_buffer()) return false; | ||
| 632 | } | ||
| 633 | vsync_ = vsync; | ||
| 634 | swapchain_srgb_ = srgb; | ||
| 635 | stale_ = false; | ||
| 636 | return true; | ||
| 637 | } | ||
| 638 | |||
| 639 | // Destroys a swapchain that is being replaced (or the last one), with the per-image semaphores that went with it. | ||
| 640 | void release(VkSwapchainKHR swapchain) { | ||
| 641 | const VkDevice device = context_.device(); | ||
| 642 | for (VkSemaphore semaphore : ready_) | ||
| 643 | if (semaphore) context_.vk.vkDestroySemaphore(device, semaphore, nullptr); | ||
| 644 | ready_.clear(); | ||
| 645 | images_.clear(); | ||
| 646 | if (swapchain) vkDestroySwapchainKHR_(device, swapchain, nullptr); | ||
| 647 | } | ||
| 648 | |||
| 649 | VulkanContext& context_; | ||
| 650 | VkInstance instance_; | ||
| 651 | VkPhysicalDevice physical_; | ||
| 652 | VkQueue queue_; | ||
| 653 | VkSurfaceKHR surface_; | ||
| 654 | VkSwapchainKHR swapchain_ = VK_NULL_HANDLE; | ||
| 655 | VkFormat format_ = VK_FORMAT_UNDEFINED; | ||
| 656 | VkExtent2D extent_{}; | ||
| 657 | std::vector<VkImage> images_; | ||
| 658 | std::vector<VkSemaphore> ready_; // per swapchain image: blit done, present may start | ||
| 659 | std::array<Frame, 2> frames_{}; | ||
| 660 | VkCommandPool pool_ = VK_NULL_HANDLE; | ||
| 661 | std::uint64_t frame_index_ = 0; | ||
| 662 | std::uint64_t presented_ = 0; | ||
| 663 | bool vsync_ = true; | ||
| 664 | bool stale_ = false; | ||
| 665 | bool ok_ = false; | ||
| 666 | bool swapchain_srgb_ = false; // the swapchain format follows the source being presented (set once it exists) | ||
| 667 | std::function<bool()> abort_; | ||
| 668 | std::map<VkFormat, VkFormatFeatureFlags> features_; | ||
| 669 | bool warned_srgb_ = false; | ||
| 670 | bool capture_ = false; | ||
| 671 | VkBuffer capture_buffer_ = VK_NULL_HANDLE; | ||
| 672 | VkDeviceMemory capture_memory_ = VK_NULL_HANDLE; | ||
| 673 | VkDeviceSize capture_size_ = 0; | ||
| 674 | std::uint8_t* capture_ptr_ = nullptr; | ||
| 675 | bool captured_ = false; | ||
| 676 | VkExtent2D captured_extent_{}; | ||
| 677 | VkFormat captured_format_ = VK_FORMAT_UNDEFINED; | ||
| 678 | |||
| 679 | PFN_vkGetPhysicalDeviceSurfaceSupportKHR vkGetPhysicalDeviceSurfaceSupportKHR_ = nullptr; | ||
| 680 | PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR vkGetPhysicalDeviceSurfaceCapabilitiesKHR_ = nullptr; | ||
| 681 | PFN_vkGetPhysicalDeviceSurfaceFormatsKHR vkGetPhysicalDeviceSurfaceFormatsKHR_ = nullptr; | ||
| 682 | PFN_vkGetPhysicalDeviceSurfacePresentModesKHR vkGetPhysicalDeviceSurfacePresentModesKHR_ = nullptr; | ||
| 683 | PFN_vkDestroySurfaceKHR vkDestroySurfaceKHR_ = nullptr; | ||
| 684 | PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr_ = nullptr; | ||
| 685 | PFN_vkCreateSwapchainKHR vkCreateSwapchainKHR_ = nullptr; | ||
| 686 | PFN_vkDestroySwapchainKHR vkDestroySwapchainKHR_ = nullptr; | ||
| 687 | PFN_vkGetSwapchainImagesKHR vkGetSwapchainImagesKHR_ = nullptr; | ||
| 688 | PFN_vkAcquireNextImageKHR vkAcquireNextImageKHR_ = nullptr; | ||
| 689 | PFN_vkQueuePresentKHR vkQueuePresentKHR_ = nullptr; | ||
| 690 | PFN_vkCmdBlitImage vkCmdBlitImage_ = nullptr; | ||
| 691 | PFN_vkCmdClearColorImage vkCmdClearColorImage_ = nullptr; | ||
| 692 | PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties_ = nullptr; | ||
| 693 | PFN_vkGetPhysicalDeviceFormatProperties vkGetPhysicalDeviceFormatProperties_ = nullptr; | ||
| 694 | PFN_vkCreateFence vkCreateFence_ = nullptr; | ||
| 695 | PFN_vkDestroyFence vkDestroyFence_ = nullptr; | ||
| 696 | PFN_vkWaitForFences vkWaitForFences_ = nullptr; | ||
| 697 | PFN_vkResetFences vkResetFences_ = nullptr; | ||
| 698 | }; | ||
| 699 | |||
| 700 | } // namespace leonida | ||
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 6df4beba5..436eb4e93 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h | |||
| @@ -79,6 +79,9 @@ struct ResourceBinding { | |||
| 79 | // (depth24.h) and clears quantise to 24 bits. | 79 | // (depth24.h) and clears quantise to 24 bits. |
| 80 | bool depth24_as_float = false; | 80 | bool depth24_as_float = false; |
| 81 | bool depth24 = false; // a D24 family format, native or not: depth clears are quantised to 24 bits first | 81 | bool depth24 = false; // a D24 family format, native or not: depth clears are quantised to 24 bits first |
| 82 | // A D3D12 R32G32B32 texture (12 bytes a texel) held in an R32G32B32A32 image (16): footprints keep D3D12's 12 | ||
| 83 | // bytes, and copies between it and a buffer are repacked by the depth24 pass (Depth24Job::rgb96). | ||
| 84 | bool rgb96 = false; | ||
| 82 | bool color_attachment = false; // has COLOR_ATTACHMENT usage: unordered-access rectangle clears need it | 85 | bool color_attachment = false; // has COLOR_ATTACHMENT usage: unordered-access rectangle clears need it |
| 83 | bool host_readable = false; // D3D12_HEAP_TYPE_READBACK (or CUSTOM write-back): needs host-read flush | 86 | bool host_readable = false; // D3D12_HEAP_TYPE_READBACK (or CUSTOM write-back): needs host-read flush |
| 84 | }; | 87 | }; |
| @@ -116,8 +119,9 @@ inline VulkanAccess vulkan_access(std::uint32_t states, bool presentable, bool c | |||
| 116 | constexpr VkPipelineStageFlags fragment_tests = | 119 | constexpr VkPipelineStageFlags fragment_tests = |
| 117 | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; | 120 | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; |
| 118 | static constexpr Bit bits[] = { | 121 | static constexpr Bit bits[] = { |
| 122 | // A root CBV can be loaded through a shader buffer device address. | ||
| 119 | {s::vertex_and_constant_buffer, VK_IMAGE_LAYOUT_GENERAL, | 123 | {s::vertex_and_constant_buffer, VK_IMAGE_LAYOUT_GENERAL, |
| 120 | VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT | VK_ACCESS_UNIFORM_READ_BIT, | 124 | VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT | VK_ACCESS_UNIFORM_READ_BIT | VK_ACCESS_SHADER_READ_BIT, |
| 121 | VK_PIPELINE_STAGE_VERTEX_INPUT_BIT | shader_stages}, | 125 | VK_PIPELINE_STAGE_VERTEX_INPUT_BIT | shader_stages}, |
| 122 | {s::index_buffer, VK_IMAGE_LAYOUT_GENERAL, VK_ACCESS_INDEX_READ_BIT, VK_PIPELINE_STAGE_VERTEX_INPUT_BIT}, | 126 | {s::index_buffer, VK_IMAGE_LAYOUT_GENERAL, VK_ACCESS_INDEX_READ_BIT, VK_PIPELINE_STAGE_VERTEX_INPUT_BIT}, |
| 123 | {s::render_target, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, | 127 | {s::render_target, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| @@ -230,7 +234,7 @@ inline VkAccessFlags2 vulkan_barrier_access(std::uint32_t access) { | |||
| 230 | }; | 234 | }; |
| 231 | static constexpr Bit bits[] = { | 235 | static constexpr Bit bits[] = { |
| 232 | {a::vertex_buffer, VK_ACCESS_2_VERTEX_ATTRIBUTE_READ_BIT}, | 236 | {a::vertex_buffer, VK_ACCESS_2_VERTEX_ATTRIBUTE_READ_BIT}, |
| 233 | {a::constant_buffer, VK_ACCESS_2_UNIFORM_READ_BIT}, | 237 | {a::constant_buffer, VK_ACCESS_2_UNIFORM_READ_BIT | VK_ACCESS_2_SHADER_STORAGE_READ_BIT}, |
| 234 | {a::index_buffer, VK_ACCESS_2_INDEX_READ_BIT}, | 238 | {a::index_buffer, VK_ACCESS_2_INDEX_READ_BIT}, |
| 235 | {a::render_target, VK_ACCESS_2_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT}, | 239 | {a::render_target, VK_ACCESS_2_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT}, |
| 236 | {a::unordered_access, VK_ACCESS_2_SHADER_READ_BIT | VK_ACCESS_2_SHADER_WRITE_BIT}, | 240 | {a::unordered_access, VK_ACCESS_2_SHADER_READ_BIT | VK_ACCESS_2_SHADER_WRITE_BIT}, |
| @@ -259,7 +263,7 @@ inline VkAccessFlags2 vulkan_barrier_access(std::uint32_t access) { | |||
| 259 | return out; | 263 | return out; |
| 260 | } | 264 | } |
| 261 | 265 | ||
| 262 | // For one VkAccessFlagBits2 that vulkan_barrier_access produces: the stages Vulkan lets perform it (the "supported | 266 | // For accesses that vulkan_barrier_access produces: the stages Vulkan lets perform them (the "supported |
| 263 | // access types" table of the synchronization2 chapter, with the meta stages that contain them), and the narrowest | 267 | // access types" table of the synchronization2 chapter, with the meta stages that contain them), and the narrowest |
| 264 | // stage to add when a barrier's stages have none of those. MEMORY_READ/WRITE are valid in every stage. | 268 | // stage to add when a barrier's stages have none of those. MEMORY_READ/WRITE are valid in every stage. |
| 265 | struct AccessStages { | 269 | struct AccessStages { |
| @@ -288,7 +292,7 @@ inline constexpr AccessStages access_stages[] = { | |||
| 288 | VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT | VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, | 292 | VK_PIPELINE_STAGE_2_ALL_GRAPHICS_BIT | VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, |
| 289 | VK_PIPELINE_STAGE_2_VERTEX_ATTRIBUTE_INPUT_BIT}, | 293 | VK_PIPELINE_STAGE_2_VERTEX_ATTRIBUTE_INPUT_BIT}, |
| 290 | // Shader accesses: D3D12 does not say which shader stage, so every one that can run (all shading). | 294 | // Shader accesses: D3D12 does not say which shader stage, so every one that can run (all shading). |
| 291 | {VK_ACCESS_2_UNIFORM_READ_BIT, shader_stages2, | 295 | {VK_ACCESS_2_UNIFORM_READ_BIT | VK_ACCESS_2_SHADER_STORAGE_READ_BIT, shader_stages2, |
| 292 | VK_PIPELINE_STAGE_2_PRE_RASTERIZATION_SHADERS_BIT | VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT | | 296 | VK_PIPELINE_STAGE_2_PRE_RASTERIZATION_SHADERS_BIT | VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT | |
| 293 | VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT}, | 297 | VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT}, |
| 294 | {VK_ACCESS_2_SHADER_READ_BIT, shader_stages2, | 298 | {VK_ACCESS_2_SHADER_READ_BIT, shader_stages2, |
| @@ -458,6 +462,14 @@ inline BlockFormat block_format(VkFormat format, VkImageAspectFlags aspect) { | |||
| 458 | } | 462 | } |
| 459 | } | 463 | } |
| 460 | 464 | ||
| 465 | // The same for a resource as D3D12 lays it out, which for an rgb96 texture is not its image's layout. | ||
| 466 | inline constexpr std::uint32_t rgb96_bytes = 12; | ||
| 467 | inline BlockFormat copy_block_format(const ResourceBinding& resource, VkImageAspectFlags aspect) { | ||
| 468 | BlockFormat format = block_format(resource.format, aspect); | ||
| 469 | if (resource.rgb96 && format.bytes) format.bytes = rgb96_bytes; | ||
| 470 | return format; | ||
| 471 | } | ||
| 472 | |||
| 461 | inline bool two_planes(const ResourceBinding& resource) { | 473 | inline bool two_planes(const ResourceBinding& resource) { |
| 462 | return (resource.aspects & depth_stencil_aspects) == depth_stencil_aspects; | 474 | return (resource.aspects & depth_stencil_aspects) == depth_stencil_aspects; |
| 463 | } | 475 | } |
| @@ -704,6 +716,7 @@ enum class ReplayResult { | |||
| 704 | invalid_copy, | 716 | invalid_copy, |
| 705 | invalid_predication, | 717 | invalid_predication, |
| 706 | invalid_query, | 718 | invalid_query, |
| 719 | invalid_indirect_arguments, | ||
| 707 | unsupported | 720 | unsupported |
| 708 | }; | 721 | }; |
| 709 | 722 | ||
| @@ -748,6 +761,9 @@ struct Prepared { | |||
| 748 | VkResolveModeFlagBits resolve_mode = VK_RESOLVE_MODE_NONE; | 761 | VkResolveModeFlagBits resolve_mode = VK_RESOLVE_MODE_NONE; |
| 749 | VkImageView resolve_view = VK_NULL_HANDLE; | 762 | VkImageView resolve_view = VK_NULL_HANDLE; |
| 750 | bool has_depth24 = false; // a depth plane copy of a depth24_as_float texture: convert through depth24 | 763 | bool has_depth24 = false; // a depth plane copy of a depth24_as_float texture: convert through depth24 |
| 764 | // (or a copy of an rgb96 texture: repacked by the same pass) | ||
| 765 | std::uint32_t rgb96_row_words = 0; // an rgb96 footprint: words between its rows and between its slices | ||
| 766 | std::uint32_t rgb96_slice_words = 0; | ||
| 751 | Depth24Job depth24; // the whole copy: where its scratch floats are | 767 | Depth24Job depth24; // the whole copy: where its scratch floats are |
| 752 | std::vector<Depth24Job> depth24_parts; // the dispatches that convert it | 768 | std::vector<Depth24Job> depth24_parts; // the dispatches that convert it |
| 753 | std::vector<VkImageSubresourceRange> discards; // DiscardResource: the subresources to discard | 769 | std::vector<VkImageSubresourceRange> discards; // DiscardResource: the subresources to discard |
| @@ -945,6 +961,21 @@ inline bool within(std::uint64_t offset, std::uint64_t size, std::uint64_t total | |||
| 945 | return offset <= total && size <= total - offset; | 961 | return offset <= total && size <= total - offset; |
| 946 | } | 962 | } |
| 947 | 963 | ||
| 964 | inline VkDeviceSize indirect_record_size(IndirectKind kind) { | ||
| 965 | switch (kind) { | ||
| 966 | case IndirectKind::draw: return sizeof(VkDrawIndirectCommand); | ||
| 967 | case IndirectKind::draw_indexed: return sizeof(VkDrawIndexedIndirectCommand); | ||
| 968 | case IndirectKind::dispatch: return sizeof(VkDispatchIndirectCommand); | ||
| 969 | } | ||
| 970 | return 0; | ||
| 971 | } | ||
| 972 | |||
| 973 | inline bool indirect_range(const ResourceBinding& binding, VkDeviceSize offset, VkDeviceSize bytes) { | ||
| 974 | return (binding.buffer_offset | offset) % sizeof(std::uint32_t) == 0 && | ||
| 975 | within(offset, bytes, binding.size) && | ||
| 976 | within(binding.buffer_offset, offset + bytes, UINT64_MAX); | ||
| 977 | } | ||
| 978 | |||
| 948 | // The bytes of a buffer UAV clear, and whether the transfer commands can write them. Elements of up to 4 bytes | 979 | // The bytes of a buffer UAV clear, and whether the transfer commands can write them. Elements of up to 4 bytes |
| 949 | // repeat in every aligned word, so vkCmdFillBuffer writes the aligned middle and copies from it the unaligned ends | 980 | // repeat in every aligned word, so vkCmdFillBuffer writes the aligned middle and copies from it the unaligned ends |
| 950 | // (vkCmdCopyBuffer has no alignment rule); that needs one whole word in the middle. Wider elements are written | 981 | // (vkCmdCopyBuffer has no alignment rule); that needs one whole word in the middle. Wider elements are written |
| @@ -1041,7 +1072,7 @@ inline ReplayResult prepare_texture_copy(const CopyTexture& copy, Prepared& p) { | |||
| 1041 | !copy_subresource(p.source, copy.src.subresource, src)) | 1072 | !copy_subresource(p.source, copy.src.subresource, src)) |
| 1042 | return ReplayResult::invalid_subresource; | 1073 | return ReplayResult::invalid_subresource; |
| 1043 | if (dst.aspectMask != src.aspectMask || p.binding.volume != p.source.volume || | 1074 | if (dst.aspectMask != src.aspectMask || p.binding.volume != p.source.volume || |
| 1044 | p.binding.samples != p.source.samples) | 1075 | p.binding.samples != p.source.samples || p.binding.rgb96 != p.source.rgb96) |
| 1045 | return ReplayResult::invalid_copy; | 1076 | return ReplayResult::invalid_copy; |
| 1046 | const VkExtent2D from = mip_extent(p.source, src.baseMipLevel), to = mip_extent(p.binding, dst.baseMipLevel); | 1077 | const VkExtent2D from = mip_extent(p.source, src.baseMipLevel), to = mip_extent(p.binding, dst.baseMipLevel); |
| 1047 | const auto from_depth = std::max(1u, p.source.depth >> src.baseMipLevel); | 1078 | const auto from_depth = std::max(1u, p.source.depth >> src.baseMipLevel); |
| @@ -1122,8 +1153,10 @@ inline ReplayResult prepare_texture_copy(const CopyTexture& copy, Prepared& p) { | |||
| 1122 | VkImageSubresourceRange range{}; | 1153 | VkImageSubresourceRange range{}; |
| 1123 | if (!copy_subresource(image, texture.subresource, range)) return ReplayResult::invalid_subresource; | 1154 | if (!copy_subresource(image, texture.subresource, range)) return ReplayResult::invalid_subresource; |
| 1124 | if (image.samples != 1) return ReplayResult::invalid_copy; // multisampled textures have no footprint | 1155 | if (image.samples != 1) return ReplayResult::invalid_copy; // multisampled textures have no footprint |
| 1125 | const BlockFormat format = block_format(image.format, range.aspectMask); | 1156 | const BlockFormat format = copy_block_format(image, range.aspectMask); |
| 1126 | if (!format.bytes || placed.row_pitch % format.bytes) return ReplayResult::unsupported; | 1157 | // A 12-byte texel does not divide D3D12's 256-byte pitches; an rgb96 copy hands Vulkan the scratch buffer of |
| 1158 | // the repack, never this pitch, and the pass reads the footprint in words. | ||
| 1159 | if (!format.bytes || placed.row_pitch % (image.rgb96 ? 4 : format.bytes)) return ReplayResult::unsupported; | ||
| 1127 | const VkExtent2D mip = mip_extent(image, range.baseMipLevel); | 1160 | const VkExtent2D mip = mip_extent(image, range.baseMipLevel); |
| 1128 | const auto mip_depth = std::max(1u, image.depth >> range.baseMipLevel); | 1161 | const auto mip_depth = std::max(1u, image.depth >> range.baseMipLevel); |
| 1129 | // The box is in the source's coordinates: the footprint for an upload, the texture for a readback. | 1162 | // The box is in the source's coordinates: the footprint for an upload, the texture for a readback. |
| @@ -1153,7 +1186,9 @@ inline ReplayResult prepare_texture_copy(const CopyTexture& copy, Prepared& p) { | |||
| 1153 | std::uint64_t(placed_x / format.block) * format.bytes; | 1186 | std::uint64_t(placed_x / format.block) * format.bytes; |
| 1154 | if (!within(slice_start, in_slice, buffer.size)) return ReplayResult::invalid_copy; | 1187 | if (!within(slice_start, in_slice, buffer.size)) return ReplayResult::invalid_copy; |
| 1155 | const auto offset = slice_start + in_slice; | 1188 | const auto offset = slice_start + in_slice; |
| 1156 | const std::uint32_t alignment = (range.aspectMask & depth_stencil_aspects) ? 4 : format.bytes; | 1189 | // Vulkan wants a buffer offset that is a multiple of the texel size (VUID-vkCmdCopyBufferToImage-dstImage-07975), |
| 1190 | // which D3D12's 512-byte placement does not give a 12-byte texel: one more reason an rgb96 copy is repacked. | ||
| 1191 | const std::uint32_t alignment = (range.aspectMask & depth_stencil_aspects) || image.rgb96 ? 4 : format.bytes; | ||
| 1157 | if (offset % alignment) return ReplayResult::unsupported; | 1192 | if (offset % alignment) return ReplayResult::unsupported; |
| 1158 | const std::uint64_t rows = (std::uint64_t(h) + format.block - 1) / format.block; | 1193 | const std::uint64_t rows = (std::uint64_t(h) + format.block - 1) / format.block; |
| 1159 | const std::uint64_t row_bytes = (std::uint64_t(w) + format.block - 1) / format.block * format.bytes; | 1194 | const std::uint64_t row_bytes = (std::uint64_t(w) + format.block - 1) / format.block * format.bytes; |
| @@ -1168,6 +1203,13 @@ inline ReplayResult prepare_texture_copy(const CopyTexture& copy, Prepared& p) { | |||
| 1168 | region.imageSubresource = {range.aspectMask, range.baseMipLevel, range.baseArrayLayer, 1}; | 1203 | region.imageSubresource = {range.aspectMask, range.baseMipLevel, range.baseArrayLayer, 1}; |
| 1169 | region.imageOffset = {std::int32_t(texture_x), std::int32_t(texture_y), std::int32_t(texture_z)}; | 1204 | region.imageOffset = {std::int32_t(texture_x), std::int32_t(texture_y), std::int32_t(texture_z)}; |
| 1170 | region.imageExtent = {w, h, depth}; | 1205 | region.imageExtent = {w, h, depth}; |
| 1206 | if (image.rgb96) { // the repack's scratch replaces this buffer in the copy; these are for the pass | ||
| 1207 | if (placed.row_pitch / 4 > UINT32_MAX || slice_pitch / 4 > UINT32_MAX) return ReplayResult::unsupported; | ||
| 1208 | p.rgb96_row_words = std::uint32_t(placed.row_pitch / 4); | ||
| 1209 | p.rgb96_slice_words = std::uint32_t(slice_pitch / 4); | ||
| 1210 | region.bufferRowLength = 0; | ||
| 1211 | region.bufferImageHeight = 0; | ||
| 1212 | } | ||
| 1171 | p.buffer_image_copies.push_back(region); | 1213 | p.buffer_image_copies.push_back(region); |
| 1172 | (upload ? p.range : p.source_range) = range; | 1214 | (upload ? p.range : p.source_range) = range; |
| 1173 | return ReplayResult::success; | 1215 | return ReplayResult::success; |
| @@ -1192,6 +1234,7 @@ inline ReplayResult prepare_resource_copy(Prepared& p) { | |||
| 1192 | if (!dst.image || !src.image || !same_shape || | 1234 | if (!dst.image || !src.image || !same_shape || |
| 1193 | dst.depth != src.depth || dst.volume != src.volume || dst.mip_levels != src.mip_levels || | 1235 | dst.depth != src.depth || dst.volume != src.volume || dst.mip_levels != src.mip_levels || |
| 1194 | dst.array_layers != src.array_layers || dst.aspects != src.aspects || dst.samples != src.samples || | 1236 | dst.array_layers != src.array_layers || dst.aspects != src.aspects || dst.samples != src.samples || |
| 1237 | dst.rgb96 != src.rgb96 || | ||
| 1195 | block_format(dst.format, VK_IMAGE_ASPECT_COLOR_BIT).bytes != block_format(src.format, VK_IMAGE_ASPECT_COLOR_BIT).bytes) | 1238 | block_format(dst.format, VK_IMAGE_ASPECT_COLOR_BIT).bytes != block_format(src.format, VK_IMAGE_ASPECT_COLOR_BIT).bytes) |
| 1196 | return ReplayResult::invalid_copy; | 1239 | return ReplayResult::invalid_copy; |
| 1197 | for (const VkImageAspectFlags aspect : {VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_ASPECT_DEPTH_BIT, VK_IMAGE_ASPECT_STENCIL_BIT}) { | 1240 | for (const VkImageAspectFlags aspect : {VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_ASPECT_DEPTH_BIT, VK_IMAGE_ASPECT_STENCIL_BIT}) { |
| @@ -1634,6 +1677,23 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 1634 | job.app_slice_words = job.app_row_words * (region.bufferImageHeight ? region.bufferImageHeight : job.height); | 1677 | job.app_slice_words = job.app_row_words * (region.bufferImageHeight ? region.bufferImageHeight : job.height); |
| 1635 | if (!vk.depth24_prepare || !vk.depth24_run || !vk.depth24_prepare(job, p.depth24_parts)) return ReplayResult::unsupported; | 1678 | if (!vk.depth24_prepare || !vk.depth24_run || !vk.depth24_prepare(job, p.depth24_parts)) return ReplayResult::unsupported; |
| 1636 | p.has_depth24 = true; | 1679 | p.has_depth24 = true; |
| 1680 | } else if (result == ReplayResult::success && image.rgb96 && !p.buffer_image_copies.empty()) { | ||
| 1681 | // A footprint of a 96-bit texture: 12-byte texels, 16 in the image. The same pass and scratch. | ||
| 1682 | const VkBufferImageCopy& region = p.buffer_image_copies[0]; | ||
| 1683 | const ResourceBinding& buffer = upload ? p.source : p.binding; | ||
| 1684 | Depth24Job& job = p.depth24; | ||
| 1685 | job.rgb96 = true; | ||
| 1686 | job.to_float = upload; | ||
| 1687 | job.app = buffer.buffer; | ||
| 1688 | job.app_offset = region.bufferOffset; | ||
| 1689 | job.app_size = buffer.size; | ||
| 1690 | job.width = region.imageExtent.width; | ||
| 1691 | job.height = region.imageExtent.height; | ||
| 1692 | job.slices = region.imageExtent.depth; | ||
| 1693 | job.app_row_words = p.rgb96_row_words; | ||
| 1694 | job.app_slice_words = p.rgb96_slice_words; | ||
| 1695 | if (!vk.depth24_prepare || !vk.depth24_run || !vk.depth24_prepare(job, p.depth24_parts)) return ReplayResult::unsupported; | ||
| 1696 | p.has_depth24 = true; | ||
| 1637 | } | 1697 | } |
| 1638 | } else { | 1698 | } else { |
| 1639 | result = detail::prepare_resource_copy(p); | 1699 | result = detail::prepare_resource_copy(p); |
| @@ -1714,8 +1774,8 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 1714 | !vk.prepare_predicate(p.binding.buffer, p.binding.buffer_offset + predication->offset, p.predicate)) | 1774 | !vk.prepare_predicate(p.binding.buffer, p.binding.buffer_offset + predication->offset, p.predicate)) |
| 1715 | return ReplayResult::unsupported; | 1775 | return ReplayResult::unsupported; |
| 1716 | } else if (const QueryRange range = query_range(command); range.count) { | 1776 | } else if (const QueryRange range = query_range(command); range.count) { |
| 1717 | // BeginQuery, EndQuery and ResolveQueryData are not among D3D12's predicated calls, and Vulkan's | 1777 | // BeginQuery, EndQuery and ResolveQueryData are not among D3D12's predicated calls, so a predicate does |
| 1718 | // conditional rendering leaves query commands and transfers alone, so a predicate changes nothing here. | 1778 | // not suppress them (emit_query suspends conditional rendering around ResolveQueryData). |
| 1719 | if (!queries) return ReplayResult::unsupported; | 1779 | if (!queries) return ReplayResult::unsupported; |
| 1720 | QueryPool* heap = vk.query_pool(range.heap); | 1780 | QueryPool* heap = vk.query_pool(range.heap); |
| 1721 | if (!heap) return ReplayResult::unresolved_resource; | 1781 | if (!heap) return ReplayResult::unresolved_resource; |
| @@ -1758,9 +1818,17 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun | |||
| 1758 | out.queries.push_back({i, heap, statistics_active}); | 1818 | out.queries.push_back({i, heap, statistics_active}); |
| 1759 | } else if (const auto* ind = std::get_if<ExecuteIndirect>(&command)) { | 1819 | } else if (const auto* ind = std::get_if<ExecuteIndirect>(&command)) { |
| 1760 | if (!bind(ind->argument_buffer) || !p.binding.buffer) return ReplayResult::unresolved_resource; | 1820 | if (!bind(ind->argument_buffer) || !p.binding.buffer) return ReplayResult::unresolved_resource; |
| 1821 | const VkDeviceSize record_size = detail::indirect_record_size(ind->kind); | ||
| 1822 | if (!record_size || ind->stride < record_size || ind->stride % sizeof(std::uint32_t)) | ||
| 1823 | return ReplayResult::invalid_indirect_arguments; | ||
| 1824 | const VkDeviceSize bytes = VkDeviceSize(ind->max_count - 1) * ind->stride + record_size; | ||
| 1825 | if (!detail::indirect_range(p.binding, ind->argument_offset, bytes)) | ||
| 1826 | return ReplayResult::invalid_indirect_arguments; | ||
| 1761 | if (ind->count_buffer) { | 1827 | if (ind->count_buffer) { |
| 1762 | const ResourceBinding* cnt = resolve(ind->count_buffer); | 1828 | const ResourceBinding* cnt = resolve(ind->count_buffer); |
| 1763 | if (!cnt || !cnt->buffer) return ReplayResult::unresolved_resource; | 1829 | if (!cnt || !cnt->buffer) return ReplayResult::unresolved_resource; |
| 1830 | if (!detail::indirect_range(*cnt, ind->count_offset, sizeof(std::uint32_t))) | ||
| 1831 | return ReplayResult::invalid_indirect_arguments; | ||
| 1764 | p.source = *cnt; | 1832 | p.source = *cnt; |
| 1765 | if ((ind->kind == IndirectKind::draw && !vk.draw_indirect_count) || | 1833 | if ((ind->kind == IndirectKind::draw && !vk.draw_indirect_count) || |
| 1766 | (ind->kind == IndirectKind::draw_indexed && !vk.draw_indexed_indirect_count) || | 1834 | (ind->kind == IndirectKind::draw_indexed && !vk.draw_indexed_indirect_count) || |
| @@ -1908,16 +1976,16 @@ inline void emit_query(const PreparedList& list, const detail::PreparedQuery& qu | |||
| 1908 | const bool binary = resolve.type == d3d12_query::binary_occlusion; | 1976 | const bool binary = resolve.type == d3d12_query::binary_occlusion; |
| 1909 | const auto size = query_result_size(resolve.type); | 1977 | const auto size = query_result_size(resolve.type); |
| 1910 | const auto dst = list.commands[query.command].binding.buffer; | 1978 | const auto dst = list.commands[query.command].binding.buffer; |
| 1911 | bool suspended = false; | 1979 | // ResolveQueryData is unconditional in D3D12. Suspend conditional rendering for the whole resolve rather |
| 1980 | // than only before helper dispatches: on Intel's Windows Vulkan driver, vkCmdFillBuffer (used for un-ended | ||
| 1981 | // queries) is suppressed inside an active conditional rendering scope after PredicateReducer::capture's | ||
| 1982 | // compute dispatch (D3D-148). | ||
| 1983 | const bool suspend = predicate.buffer && !query.resolves.empty(); | ||
| 1984 | if (suspend) vk.end_conditional_rendering(cb); | ||
| 1912 | for (const auto& run : query.resolves) { | 1985 | for (const auto& run : query.resolves) { |
| 1913 | if (!run.pool) { | 1986 | if (!run.pool) { |
| 1914 | vk.fill_buffer(cb, dst, run.offset, VkDeviceSize(run.count) * size, 0); | 1987 | vk.fill_buffer(cb, dst, run.offset, VkDeviceSize(run.count) * size, 0); |
| 1915 | } else if (run.sum || binary) { | 1988 | } else if (run.sum || binary) { |
| 1916 | // ResolveQueryData is unconditional even when the helper uses a dispatch. | ||
| 1917 | if (predicate.buffer && !suspended) { | ||
| 1918 | vk.end_conditional_rendering(cb); | ||
| 1919 | suspended = true; | ||
| 1920 | } | ||
| 1921 | const auto& resolve_occlusion = run.sum ? vk.sum_occlusion : vk.resolve_binary_occlusion; | 1989 | const auto& resolve_occlusion = run.sum ? vk.sum_occlusion : vk.resolve_binary_occlusion; |
| 1922 | resolve_occlusion(cb, run.pool, run.first, run.count, dst, run.offset); | 1990 | resolve_occlusion(cb, run.pool, run.first, run.count, dst, run.offset); |
| 1923 | } else { | 1991 | } else { |
| @@ -1925,7 +1993,7 @@ inline void emit_query(const PreparedList& list, const detail::PreparedQuery& qu | |||
| 1925 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); | 1993 | VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); |
| 1926 | } | 1994 | } |
| 1927 | } | 1995 | } |
| 1928 | if (suspended) vk.begin_conditional_rendering(cb, &predicate); | 1996 | if (suspend) vk.begin_conditional_rendering(cb, &predicate); |
| 1929 | } | 1997 | } |
| 1930 | } | 1998 | } |
| 1931 | 1999 | ||
| @@ -2219,8 +2287,27 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 2219 | const VkImageLayout dst_layout = detail::copy_layout( | 2287 | const VkImageLayout dst_layout = detail::copy_layout( |
| 2220 | vk, command_buffer, p.binding, dst_id, detail::planes_for_layout(p.binding, p.range, separate), | 2288 | vk, command_buffer, p.binding, dst_id, detail::planes_for_layout(p.binding, p.range, separate), |
| 2221 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT, tracker); | 2289 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT, tracker); |
| 2222 | vk.copy_image(command_buffer, p.source.image, src_layout, p.binding.image, dst_layout, | 2290 | const auto count = std::uint32_t(p.image_copies.size()); |
| 2223 | std::uint32_t(p.image_copies.size()), p.image_copies.data()); | 2291 | const auto depth_stencil = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; |
| 2292 | if (std::holds_alternative<CopyResource>(command) && | ||
| 2293 | (p.binding.aspects & depth_stencil) == depth_stencil) { | ||
| 2294 | // prepare_resource_copy groups regions by aspect. On RX 5700 XT (Windows driver | ||
| 2295 | // 32.0.21045.5002), copying both groups in one command corrupts stencil in multi-mip | ||
| 2296 | // arrays, even with validation clean. Order the aspect writes explicitly; a barrier | ||
| 2297 | // before or after the bulk copy does not fix it. Keep all mips/layers batched per aspect. | ||
| 2298 | const auto depth_count = p.source.mip_levels; | ||
| 2299 | vk.copy_image(command_buffer, p.source.image, src_layout, p.binding.image, dst_layout, | ||
| 2300 | depth_count, p.image_copies.data()); | ||
| 2301 | detail::memory_barrier(vk, command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, | ||
| 2302 | VK_PIPELINE_STAGE_TRANSFER_BIT, | ||
| 2303 | VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT, | ||
| 2304 | VK_PIPELINE_STAGE_TRANSFER_BIT); | ||
| 2305 | vk.copy_image(command_buffer, p.source.image, src_layout, p.binding.image, dst_layout, | ||
| 2306 | count - depth_count, p.image_copies.data() + depth_count); | ||
| 2307 | } else { | ||
| 2308 | vk.copy_image(command_buffer, p.source.image, src_layout, p.binding.image, dst_layout, | ||
| 2309 | count, p.image_copies.data()); | ||
| 2310 | } | ||
| 2224 | } else if (p.binding.image) { | 2311 | } else if (p.binding.image) { |
| 2225 | VkBuffer source = p.source.buffer; | 2312 | VkBuffer source = p.source.buffer; |
| 2226 | std::vector<VkBufferImageCopy> converted; | 2313 | std::vector<VkBufferImageCopy> converted; |
| @@ -2368,7 +2455,12 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 2368 | else if (write->mode == write_immediate_mode::marker_in) | 2455 | else if (write->mode == write_immediate_mode::marker_in) |
| 2369 | detail::memory_barrier(vk, command_buffer, 0, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, | 2456 | detail::memory_barrier(vk, command_buffer, 0, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, |
| 2370 | VK_PIPELINE_STAGE_TRANSFER_BIT); | 2457 | VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 2458 | // WriteBufferImmediate is unconditional on hardware D3D12 (measured on Intel and NVIDIA D3D12; not in | ||
| 2459 | // SetPredication's predicated list). Suspend conditional rendering around vkCmdUpdateBuffer because | ||
| 2460 | // Intel's Windows Vulkan driver suppresses it after PredicateReducer::capture's compute dispatch. | ||
| 2461 | if (predicate.buffer) vk.end_conditional_rendering(command_buffer); | ||
| 2371 | vk.update_buffer(command_buffer, p.write_buffer, p.write_offset, 4, &write->value); | 2462 | vk.update_buffer(command_buffer, p.write_buffer, p.write_offset, 4, &write->value); |
| 2463 | if (predicate.buffer) vk.begin_conditional_rendering(command_buffer, &predicate); | ||
| 2372 | } else if (std::holds_alternative<AtomicCopyBuffer>(command)) { | 2464 | } else if (std::holds_alternative<AtomicCopyBuffer>(command)) { |
| 2373 | // Every earlier write, to the dependent resources among others, finishes and is visible before the copy. | 2465 | // Every earlier write, to the dependent resources among others, finishes and is visible before the copy. |
| 2374 | // Vulkan promises no atomicity for a transfer; that a naturally aligned 4- or 8-byte copy is one write | 2466 | // Vulkan promises no atomicity for a transfer; that a naturally aligned 4- or 8-byte copy is one write |
| @@ -2378,25 +2470,25 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c | |||
| 2378 | VK_PIPELINE_STAGE_TRANSFER_BIT); | 2470 | VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 2379 | vk.copy_buffer(command_buffer, p.source.buffer, p.binding.buffer, 1, p.buffer_copies.data()); | 2471 | vk.copy_buffer(command_buffer, p.source.buffer, p.binding.buffer, 1, p.buffer_copies.data()); |
| 2380 | } else if (const auto* ind = std::get_if<ExecuteIndirect>(&command)) { | 2472 | } else if (const auto* ind = std::get_if<ExecuteIndirect>(&command)) { |
| 2473 | const VkDeviceSize arguments = p.binding.buffer_offset + ind->argument_offset; | ||
| 2381 | if (ind->count_buffer) { | 2474 | if (ind->count_buffer) { |
| 2475 | const VkDeviceSize count = p.source.buffer_offset + ind->count_offset; | ||
| 2382 | if (ind->kind == IndirectKind::draw) { | 2476 | if (ind->kind == IndirectKind::draw) { |
| 2383 | vk.draw_indirect_count(command_buffer, p.binding.buffer, ind->argument_offset, p.source.buffer, | 2477 | vk.draw_indirect_count(command_buffer, p.binding.buffer, arguments, p.source.buffer, |
| 2384 | ind->count_offset, ind->max_count, ind->stride); | 2478 | count, ind->max_count, ind->stride); |
| 2385 | } else if (ind->kind == IndirectKind::draw_indexed) { | 2479 | } else if (ind->kind == IndirectKind::draw_indexed) { |
| 2386 | vk.draw_indexed_indirect_count(command_buffer, p.binding.buffer, ind->argument_offset, | 2480 | vk.draw_indexed_indirect_count(command_buffer, p.binding.buffer, arguments, |
| 2387 | p.source.buffer, ind->count_offset, ind->max_count, ind->stride); | 2481 | p.source.buffer, count, ind->max_count, ind->stride); |
| 2388 | } | 2482 | } |
| 2389 | } else { | 2483 | } else { |
| 2390 | if (ind->kind == IndirectKind::draw) { | 2484 | if (ind->kind == IndirectKind::draw) { |
| 2391 | vk.draw_indirect(command_buffer, p.binding.buffer, ind->argument_offset, ind->max_count, | 2485 | vk.draw_indirect(command_buffer, p.binding.buffer, arguments, ind->max_count, ind->stride); |
| 2392 | ind->stride); | ||
| 2393 | } else if (ind->kind == IndirectKind::draw_indexed) { | 2486 | } else if (ind->kind == IndirectKind::draw_indexed) { |
| 2394 | vk.draw_indexed_indirect(command_buffer, p.binding.buffer, ind->argument_offset, ind->max_count, | 2487 | vk.draw_indexed_indirect(command_buffer, p.binding.buffer, arguments, ind->max_count, ind->stride); |
| 2395 | ind->stride); | ||
| 2396 | } else if (ind->kind == IndirectKind::dispatch) { | 2488 | } else if (ind->kind == IndirectKind::dispatch) { |
| 2397 | for (std::uint32_t k = 0; k < ind->max_count; ++k) { | 2489 | for (std::uint32_t k = 0; k < ind->max_count; ++k) { |
| 2398 | vk.dispatch_indirect(command_buffer, p.binding.buffer, | 2490 | vk.dispatch_indirect(command_buffer, p.binding.buffer, |
| 2399 | ind->argument_offset + std::uint64_t(k) * ind->stride); | 2491 | arguments + VkDeviceSize(k) * ind->stride); |
| 2400 | } | 2492 | } |
| 2401 | } | 2493 | } |
| 2402 | } | 2494 | } |