diff options
| author | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 01:55:17 +0100 |
|---|---|---|
| committer | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 05:28:06 +0100 |
| commit | 4d62f48a5cd7f0133cccb83f55a763b587e64019 (patch) | |
| tree | 4328b5bf677208acc1fd93e543ba91d10bf6f104 /runtime/src/vulkan_present.h | |
| parent | 5ccbd36f3947feb39dc2095f071e82f9baa73022 (diff) | |
presenter: source rectangle, blank present, sRGB swapchain format, frame capture
VulkanPresenter::present takes an optional source rectangle (stretched to the
window), present_blank clears the swapchain image, the swapchain format follows
the source (an sRGB source gets an sRGB swapchain, every other a UNORM one), and
enable_capture/read_back copy each presented frame to host memory for tests.
Integer, block-compressed and multisampled sources are refused. WindowPresenter
forwards all of it. Also renames the device-extension counters in
create_vulkan_device_standalone that the merge left shadowing each other (C4456).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/vulkan_present.h')
| -rw-r--r-- | runtime/src/vulkan_present.h | 308 |
1 files changed, 275 insertions, 33 deletions
diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h index 23b26ff5a..0ac9f1be7 100644 --- a/runtime/src/vulkan_present.h +++ b/runtime/src/vulkan_present.h | |||
| @@ -9,16 +9,54 @@ | |||
| 9 | // Needs VK_KHR_swapchain on the device and a surface from the caller (which this object then owns). The swapchain | 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 | 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. | 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. | ||
| 12 | 17 | ||
| 13 | #include "vulkan_backend.h" | 18 | #include "vulkan_backend.h" |
| 14 | 19 | ||
| 15 | #include <algorithm> | 20 | #include <algorithm> |
| 16 | #include <array> | 21 | #include <array> |
| 17 | #include <cstdint> | 22 | #include <cstdint> |
| 23 | #include <cstdio> | ||
| 24 | #include <cstring> | ||
| 18 | #include <vector> | 25 | #include <vector> |
| 19 | 26 | ||
| 20 | namespace leonida { | 27 | namespace leonida { |
| 21 | 28 | ||
| 29 | // A sub-rectangle of a back buffer in texels; left/top inclusive, right/bottom exclusive (D3D12_RECT's convention). | ||
| 30 | struct PresentRect { | ||
| 31 | std::int32_t left, top, right, bottom; | ||
| 32 | }; | ||
| 33 | |||
| 34 | namespace detail { | ||
| 35 | inline bool is_srgb_format(VkFormat format) { | ||
| 36 | switch (format) { | ||
| 37 | case VK_FORMAT_R8_SRGB: case VK_FORMAT_R8G8_SRGB: case VK_FORMAT_R8G8B8_SRGB: case VK_FORMAT_B8G8R8_SRGB: | ||
| 38 | case VK_FORMAT_R8G8B8A8_SRGB: case VK_FORMAT_B8G8R8A8_SRGB: case VK_FORMAT_A8B8G8R8_SRGB_PACK32: | ||
| 39 | return true; | ||
| 40 | default: | ||
| 41 | return false; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | // What vkCmdBlitImage cannot read: integer and block-compressed colour formats. | ||
| 45 | inline bool blit_unsupported_format(VkFormat format) { | ||
| 46 | switch (format) { | ||
| 47 | case VK_FORMAT_R8_UINT: case VK_FORMAT_R8_SINT: case VK_FORMAT_R8G8_UINT: case VK_FORMAT_R8G8_SINT: | ||
| 48 | case VK_FORMAT_R8G8B8A8_UINT: case VK_FORMAT_R8G8B8A8_SINT: case VK_FORMAT_R16_UINT: case VK_FORMAT_R16_SINT: | ||
| 49 | case VK_FORMAT_R16G16_UINT: case VK_FORMAT_R16G16_SINT: case VK_FORMAT_R16G16B16A16_UINT: | ||
| 50 | case VK_FORMAT_R16G16B16A16_SINT: case VK_FORMAT_R32_UINT: case VK_FORMAT_R32_SINT: case VK_FORMAT_R32G32_UINT: | ||
| 51 | case VK_FORMAT_R32G32_SINT: case VK_FORMAT_R32G32B32A32_UINT: case VK_FORMAT_R32G32B32A32_SINT: | ||
| 52 | case VK_FORMAT_A2B10G10R10_UINT_PACK32: case VK_FORMAT_UNDEFINED: | ||
| 53 | return true; | ||
| 54 | default: | ||
| 55 | return format >= VK_FORMAT_BC1_RGB_UNORM_BLOCK && format <= VK_FORMAT_BC7_SRGB_BLOCK; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } // namespace detail | ||
| 59 | |||
| 22 | class VulkanPresenter { | 60 | class VulkanPresenter { |
| 23 | public: | 61 | public: |
| 24 | VulkanPresenter(VulkanContext& context, PFN_vkGetInstanceProcAddr get_instance, VkInstance instance, | 62 | VulkanPresenter(VulkanContext& context, PFN_vkGetInstanceProcAddr get_instance, VkInstance instance, |
| @@ -31,6 +69,7 @@ public: | |||
| 31 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceSurfacePresentModesKHR) | 69 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceSurfacePresentModesKHR) |
| 32 | LEONIDA_INSTANCE_FN(vkDestroySurfaceKHR) | 70 | LEONIDA_INSTANCE_FN(vkDestroySurfaceKHR) |
| 33 | LEONIDA_INSTANCE_FN(vkGetDeviceProcAddr) | 71 | LEONIDA_INSTANCE_FN(vkGetDeviceProcAddr) |
| 72 | LEONIDA_INSTANCE_FN(vkGetPhysicalDeviceMemoryProperties) | ||
| 34 | #undef LEONIDA_INSTANCE_FN | 73 | #undef LEONIDA_INSTANCE_FN |
| 35 | if (!vkGetDeviceProcAddr_ || !vkDestroySurfaceKHR_) return; | 74 | if (!vkGetDeviceProcAddr_ || !vkDestroySurfaceKHR_) return; |
| 36 | const VkDevice device = context.device(); | 75 | const VkDevice device = context.device(); |
| @@ -41,6 +80,7 @@ public: | |||
| 41 | LEONIDA_DEVICE_FN(vkAcquireNextImageKHR) | 80 | LEONIDA_DEVICE_FN(vkAcquireNextImageKHR) |
| 42 | LEONIDA_DEVICE_FN(vkQueuePresentKHR) | 81 | LEONIDA_DEVICE_FN(vkQueuePresentKHR) |
| 43 | LEONIDA_DEVICE_FN(vkCmdBlitImage) | 82 | LEONIDA_DEVICE_FN(vkCmdBlitImage) |
| 83 | LEONIDA_DEVICE_FN(vkCmdClearColorImage) | ||
| 44 | LEONIDA_DEVICE_FN(vkCreateFence) | 84 | LEONIDA_DEVICE_FN(vkCreateFence) |
| 45 | LEONIDA_DEVICE_FN(vkDestroyFence) | 85 | LEONIDA_DEVICE_FN(vkDestroyFence) |
| 46 | LEONIDA_DEVICE_FN(vkWaitForFences) | 86 | LEONIDA_DEVICE_FN(vkWaitForFences) |
| @@ -82,6 +122,7 @@ public: | |||
| 82 | } | 122 | } |
| 83 | release(swapchain_); | 123 | release(swapchain_); |
| 84 | swapchain_ = VK_NULL_HANDLE; | 124 | swapchain_ = VK_NULL_HANDLE; |
| 125 | release_capture(); | ||
| 85 | for (Frame& frame : frames_) { | 126 | for (Frame& frame : frames_) { |
| 86 | if (frame.done) vkDestroyFence_(device, frame.done, nullptr); | 127 | if (frame.done) vkDestroyFence_(device, frame.done, nullptr); |
| 87 | if (frame.acquired) vk.vkDestroySemaphore(device, frame.acquired, nullptr); | 128 | if (frame.acquired) vk.vkDestroySemaphore(device, frame.acquired, nullptr); |
| @@ -99,6 +140,10 @@ public: | |||
| 99 | std::uint64_t presented() const { return presented_; } | 140 | std::uint64_t presented() const { return presented_; } |
| 100 | 141 | ||
| 101 | // Blits the colour texture `source` (mip 0, layer 0, in D3D12's PRESENT state) to the window and presents it. | 142 | // Blits the colour texture `source` (mip 0, layer 0, in D3D12's PRESENT state) to the window and presents it. |
| 143 | // `source_rect`, when given, limits the blit to that part of the texture (stretched to the whole window); it | ||
| 144 | // must be non-empty and inside the texture (VK_ERROR_UNKNOWN otherwise). Integer, block-compressed and | ||
| 145 | // multisampled sources are refused (VK_ERROR_FORMAT_NOT_SUPPORTED); the other formats are converted by the blit. | ||
| 146 | // The blit filters bilinearly, so with a rectangle the outermost texels blend with their neighbours outside it. | ||
| 102 | // vsync: FIFO; otherwise MAILBOX or IMMEDIATE when the surface has them. VK_SUCCESS also covers a skipped | 147 | // vsync: FIFO; otherwise MAILBOX or IMMEDIATE when the surface has them. VK_SUCCESS also covers a skipped |
| 103 | // present (minimised window) and one the surface reported out of date (the swapchain is recreated next time); | 148 | // present (minimised window) and one the surface reported out of date (the swapchain is recreated next time); |
| 104 | // VK_ERROR_OUT_OF_DATE_KHR means recreating the swapchain failed, and the next present tries again. | 149 | // VK_ERROR_OUT_OF_DATE_KHR means recreating the swapchain failed, and the next present tries again. |
| @@ -107,16 +152,66 @@ public: | |||
| 107 | // wait on a fence nothing will signal: an empty batch takes the acquire semaphore and signals the frame's fence, | 152 | // wait on a fence nothing will signal: an empty batch takes the acquire semaphore and signals the frame's fence, |
| 108 | // and the swapchain is recreated next time, which gives back the image that was never presented. If even that | 153 | // and the swapchain is recreated next time, which gives back the image that was never presented. If even that |
| 109 | // batch fails, the presenter stops (ok() is false) and its owner has to make a new one. | 154 | // batch fails, the presenter stops (ok() is false) and its owner has to make a new one. |
| 110 | VkResult present(ResourceId source, bool vsync) { | 155 | VkResult present(ResourceId source, bool vsync, const PresentRect* source_rect = nullptr) { |
| 156 | ResourceBinding binding; | ||
| 157 | { | ||
| 158 | std::lock_guard lock(context_.mutex_); | ||
| 159 | const ResourceBinding* found = context_.resolve(source); | ||
| 160 | if (!found || !found->image || found->aspects != VK_IMAGE_ASPECT_COLOR_BIT) return VK_ERROR_UNKNOWN; | ||
| 161 | binding = *found; | ||
| 162 | } | ||
| 163 | if (binding.samples > 1 || detail::blit_unsupported_format(binding.format)) | ||
| 164 | return VK_ERROR_FORMAT_NOT_SUPPORTED; | ||
| 165 | if (source_rect && (source_rect->left < 0 || source_rect->top < 0 || source_rect->left >= source_rect->right || | ||
| 166 | source_rect->top >= source_rect->bottom || | ||
| 167 | std::uint32_t(source_rect->right) > binding.width || | ||
| 168 | std::uint32_t(source_rect->bottom) > binding.height)) | ||
| 169 | return VK_ERROR_UNKNOWN; | ||
| 170 | return submit(&binding, source, vsync, source_rect, detail::is_srgb_format(binding.format)); | ||
| 171 | } | ||
| 172 | |||
| 173 | // Presents a black frame: the swapchain image cleared, nothing read. PresentX with no planes. | ||
| 174 | VkResult present_blank(bool vsync) { return submit(nullptr, ResourceId{}, vsync, nullptr, want_srgb_); } | ||
| 175 | |||
| 176 | // From now on every presented frame is also copied to host memory (the swapchain gets TRANSFER_SRC usage), for | ||
| 177 | // read_back(). False when the surface cannot do it or its format is not 8 bits per channel with 4 channels. | ||
| 178 | // Call before the first present. | ||
| 179 | bool enable_capture() { | ||
| 180 | if (!ok_) return false; | ||
| 181 | capture_ = true; | ||
| 182 | stale_ = true; | ||
| 183 | if (!recreate(vsync_) || (swapchain_ && !capture_buffer_)) { | ||
| 184 | capture_ = false; | ||
| 185 | return false; | ||
| 186 | } | ||
| 187 | return true; | ||
| 188 | } | ||
| 189 | |||
| 190 | // The last presented frame as the swapchain stores it (4 bytes a texel, rows tightly packed, format() order). | ||
| 191 | // Waits for the frame to finish. False when nothing was presented since enable_capture(). | ||
| 192 | bool read_back(std::vector<std::uint8_t>& pixels, VkExtent2D* extent, VkFormat* format) { | ||
| 193 | if (!capture_ || !capture_ptr_ || !captured_) return false; | ||
| 194 | const Frame& frame = frames_[(frame_index_ + frames_.size() - 1) % frames_.size()]; | ||
| 195 | vkWaitForFences_(context_.device(), 1, &frame.done, VK_TRUE, UINT64_MAX); | ||
| 196 | pixels.assign(capture_ptr_, capture_ptr_ + std::size_t(captured_extent_.width) * captured_extent_.height * 4); | ||
| 197 | if (extent) *extent = captured_extent_; | ||
| 198 | if (format) *format = captured_format_; | ||
| 199 | return true; | ||
| 200 | } | ||
| 201 | |||
| 202 | // Shared by present() and present_blank(). `binding` null: clear instead of blit. | ||
| 203 | VkResult submit(const ResourceBinding* binding, ResourceId source, bool vsync, const PresentRect* source_rect, | ||
| 204 | bool srgb) { | ||
| 111 | if (!ok_) return VK_ERROR_INITIALIZATION_FAILED; | 205 | if (!ok_) return VK_ERROR_INITIALIZATION_FAILED; |
| 112 | if (vsync != vsync_ || stale_) { | 206 | if (vsync != vsync_ || stale_ || srgb != want_srgb_) { |
| 207 | want_srgb_ = srgb; | ||
| 113 | if (!recreate(vsync)) return VK_ERROR_OUT_OF_DATE_KHR; | 208 | if (!recreate(vsync)) return VK_ERROR_OUT_OF_DATE_KHR; |
| 114 | } | 209 | } |
| 115 | if (!swapchain_) { // minimised: try again next frame | 210 | if (!swapchain_) { // minimised: try again next frame |
| 116 | stale_ = true; | 211 | stale_ = true; |
| 117 | return VK_SUCCESS; | 212 | return VK_SUCCESS; |
| 118 | } | 213 | } |
| 119 | if (!presentable(source)) return VK_ERROR_UNKNOWN; // checked before an image is acquired for it | 214 | if (binding && !presentable(source)) return VK_ERROR_UNKNOWN; // checked before an image is acquired for it |
| 120 | auto& vk = context_.vk; | 215 | auto& vk = context_.vk; |
| 121 | const VkDevice device = context_.device(); | 216 | const VkDevice device = context_.device(); |
| 122 | Frame& frame = frames_[frame_index_ % frames_.size()]; | 217 | Frame& frame = frames_[frame_index_ % frames_.size()]; |
| @@ -134,26 +229,29 @@ public: | |||
| 134 | if (acquired == VK_SUBOPTIMAL_KHR) stale_ = true; | 229 | if (acquired == VK_SUBOPTIMAL_KHR) stale_ = true; |
| 135 | 230 | ||
| 136 | bool recorded = false; | 231 | bool recorded = false; |
| 137 | { | 232 | if (binding) { |
| 138 | // The back buffer's layout is tracked like any other texture; this holds the context while it is read. | 233 | // The back buffer's layout is tracked like any other texture; this holds the context while it is read. |
| 139 | std::lock_guard lock(context_.mutex_); | 234 | std::lock_guard lock(context_.mutex_); |
| 140 | const ResourceBinding* binding = context_.resolve(source); | 235 | const ResourceBinding* current = context_.resolve(source); |
| 141 | if (binding && binding->image && binding->aspects == VK_IMAGE_ASPECT_COLOR_BIT) { | 236 | if (current && current->image && current->aspects == VK_IMAGE_ASPECT_COLOR_BIT) { |
| 142 | record(frame.commands, *binding, source, image); | 237 | record(frame.commands, *current, source, image, source_rect); |
| 143 | recorded = true; | 238 | recorded = true; |
| 144 | } | 239 | } |
| 240 | } else { | ||
| 241 | record_blank(frame.commands, image); | ||
| 242 | recorded = true; | ||
| 145 | } | 243 | } |
| 146 | if (!recorded) return give_back(frame, VK_ERROR_UNKNOWN); // released by another thread since presentable() | 244 | if (!recorded) return give_back(frame, VK_ERROR_UNKNOWN); // released by another thread since presentable() |
| 147 | if (const VkResult reset = vkResetFences_(device, 1, &frame.done); reset != VK_SUCCESS) return stop(reset); | 245 | if (const VkResult reset = vkResetFences_(device, 1, &frame.done); reset != VK_SUCCESS) return stop(reset); |
| 148 | const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; | 246 | const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 149 | VkSubmitInfo submit{VK_STRUCTURE_TYPE_SUBMIT_INFO}; | 247 | VkSubmitInfo submit_info{VK_STRUCTURE_TYPE_SUBMIT_INFO}; |
| 150 | submit.waitSemaphoreCount = 1; | 248 | submit_info.waitSemaphoreCount = 1; |
| 151 | submit.pWaitSemaphores = &frame.acquired; | 249 | submit_info.pWaitSemaphores = &frame.acquired; |
| 152 | submit.pWaitDstStageMask = &wait_stage; | 250 | submit_info.pWaitDstStageMask = &wait_stage; |
| 153 | submit.commandBufferCount = 1; | 251 | submit_info.commandBufferCount = 1; |
| 154 | submit.pCommandBuffers = &frame.commands; | 252 | submit_info.pCommandBuffers = &frame.commands; |
| 155 | submit.signalSemaphoreCount = 1; | 253 | submit_info.signalSemaphoreCount = 1; |
| 156 | submit.pSignalSemaphores = &ready_[image]; // one per image: reused only once that image comes back | 254 | submit_info.pSignalSemaphores = &ready_[image]; // one per image: reused only once that image comes back |
| 157 | VkPresentInfoKHR present{VK_STRUCTURE_TYPE_PRESENT_INFO_KHR}; | 255 | VkPresentInfoKHR present{VK_STRUCTURE_TYPE_PRESENT_INFO_KHR}; |
| 158 | present.waitSemaphoreCount = 1; | 256 | present.waitSemaphoreCount = 1; |
| 159 | present.pWaitSemaphores = &ready_[image]; | 257 | present.pWaitSemaphores = &ready_[image]; |
| @@ -163,7 +261,7 @@ public: | |||
| 163 | VkResult submitted, result = VK_SUCCESS; | 261 | VkResult submitted, result = VK_SUCCESS; |
| 164 | { | 262 | { |
| 165 | std::lock_guard lock(context_.submit_mutex()); | 263 | std::lock_guard lock(context_.submit_mutex()); |
| 166 | submitted = vk.vkQueueSubmit(queue_, 1, &submit, frame.done); | 264 | submitted = vk.vkQueueSubmit(queue_, 1, &submit_info, frame.done); |
| 167 | if (submitted == VK_SUCCESS) result = vkQueuePresentKHR_(queue_, &present); | 265 | if (submitted == VK_SUCCESS) result = vkQueuePresentKHR_(queue_, &present); |
| 168 | } | 266 | } |
| 169 | if (submitted != VK_SUCCESS) return give_back(frame, submitted); | 267 | if (submitted != VK_SUCCESS) return give_back(frame, submitted); |
| @@ -171,6 +269,11 @@ public: | |||
| 171 | // Only a present the engine took counts (SUBOPTIMAL is shown too). OUT_OF_DATE was not shown: the swapchain | 269 | // Only a present the engine took counts (SUBOPTIMAL is shown too). OUT_OF_DATE was not shown: the swapchain |
| 172 | // is recreated next time, and the caller sees VK_SUCCESS, since its work was submitted. | 270 | // is recreated next time, and the caller sees VK_SUCCESS, since its work was submitted. |
| 173 | if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) ++presented_; | 271 | if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) ++presented_; |
| 272 | if (capture_ && capture_ptr_) { | ||
| 273 | captured_ = true; | ||
| 274 | captured_extent_ = extent_; | ||
| 275 | captured_format_ = format_; | ||
| 276 | } | ||
| 174 | if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) { | 277 | if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) { |
| 175 | stale_ = true; | 278 | stale_ = true; |
| 176 | result = VK_SUCCESS; | 279 | result = VK_SUCCESS; |
| @@ -199,20 +302,29 @@ private: | |||
| 199 | const VkDevice device = context_.device(); | 302 | const VkDevice device = context_.device(); |
| 200 | if (vkResetFences_(device, 1, &frame.done) != VK_SUCCESS) return stop(failure); | 303 | if (vkResetFences_(device, 1, &frame.done) != VK_SUCCESS) return stop(failure); |
| 201 | const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; | 304 | const VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; |
| 202 | VkSubmitInfo submit{VK_STRUCTURE_TYPE_SUBMIT_INFO}; | 305 | VkSubmitInfo submit_info{VK_STRUCTURE_TYPE_SUBMIT_INFO}; |
| 203 | submit.waitSemaphoreCount = 1; | 306 | submit_info.waitSemaphoreCount = 1; |
| 204 | submit.pWaitSemaphores = &frame.acquired; | 307 | submit_info.pWaitSemaphores = &frame.acquired; |
| 205 | submit.pWaitDstStageMask = &wait_stage; | 308 | submit_info.pWaitDstStageMask = &wait_stage; |
| 206 | VkResult submitted; | 309 | VkResult submitted; |
| 207 | { | 310 | { |
| 208 | std::lock_guard lock(context_.submit_mutex()); | 311 | std::lock_guard lock(context_.submit_mutex()); |
| 209 | submitted = context_.vk.vkQueueSubmit(queue_, 1, &submit, frame.done); | 312 | submitted = context_.vk.vkQueueSubmit(queue_, 1, &submit_info, frame.done); |
| 210 | } | 313 | } |
| 211 | if (submitted != VK_SUCCESS) return stop(failure); | 314 | if (submitted != VK_SUCCESS) return stop(failure); |
| 212 | stale_ = true; | 315 | stale_ = true; |
| 213 | return failure; | 316 | return failure; |
| 214 | } | 317 | } |
| 215 | 318 | ||
| 319 | // The swapchain image as a binding, for the barrier helpers. | ||
| 320 | ResourceBinding target_of(std::uint32_t image) const { | ||
| 321 | ResourceBinding target{}; | ||
| 322 | target.image = images_[image]; | ||
| 323 | target.format = format_; | ||
| 324 | target.aspects = VK_IMAGE_ASPECT_COLOR_BIT; | ||
| 325 | return target; | ||
| 326 | } | ||
| 327 | |||
| 216 | // The frame's fence or acquire semaphore can no longer be trusted: no more presents (ok() is false), and the | 328 | // The frame's fence or acquire semaphore can no longer be trusted: no more presents (ok() is false), and the |
| 217 | // owner makes a new presenter. Returns `failure`. | 329 | // owner makes a new presenter. Returns `failure`. |
| 218 | VkResult stop(VkResult failure) { | 330 | VkResult stop(VkResult failure) { |
| @@ -220,7 +332,8 @@ private: | |||
| 220 | return failure; | 332 | return failure; |
| 221 | } | 333 | } |
| 222 | 334 | ||
| 223 | void record(VkCommandBuffer commands, const ResourceBinding& binding, ResourceId source, std::uint32_t image) { | 335 | void record(VkCommandBuffer commands, const ResourceBinding& binding, ResourceId source, std::uint32_t image, |
| 336 | const PresentRect* source_rect) { | ||
| 224 | auto& vk = context_.vk; | 337 | auto& vk = context_.vk; |
| 225 | const ReplayFunctions& fn = context_.replay_; | 338 | const ReplayFunctions& fn = context_.replay_; |
| 226 | vk.vkResetCommandBuffer(commands, 0); | 339 | vk.vkResetCommandBuffer(commands, 0); |
| @@ -249,20 +362,21 @@ private: | |||
| 249 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TRANSFER_BIT, | 362 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 250 | VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); | 363 | VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); |
| 251 | 364 | ||
| 252 | const std::int32_t sw = std::int32_t(binding.width), sh = std::int32_t(binding.height); | 365 | const PresentRect whole{0, 0, std::int32_t(binding.width), std::int32_t(binding.height)}; |
| 366 | const PresentRect& src = source_rect ? *source_rect : whole; | ||
| 367 | const std::int32_t sw = src.right - src.left, sh = src.bottom - src.top; | ||
| 253 | const std::int32_t dw = std::int32_t(extent_.width), dh = std::int32_t(extent_.height); | 368 | const std::int32_t dw = std::int32_t(extent_.width), dh = std::int32_t(extent_.height); |
| 254 | VkImageBlit blit{}; | 369 | VkImageBlit blit{}; |
| 255 | blit.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; | 370 | blit.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; |
| 256 | blit.srcOffsets[1] = {sw, sh, 1}; | 371 | blit.srcOffsets[0] = {src.left, src.top, 0}; |
| 372 | blit.srcOffsets[1] = {src.right, src.bottom, 1}; | ||
| 257 | blit.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; | 373 | blit.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; |
| 258 | blit.dstOffsets[1] = {dw, dh, 1}; | 374 | blit.dstOffsets[1] = {dw, dh, 1}; |
| 259 | vkCmdBlitImage_(commands, binding.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, images_[image], | 375 | vkCmdBlitImage_(commands, binding.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, images_[image], |
| 260 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, | 376 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, |
| 261 | sw == dw && sh == dh ? VK_FILTER_NEAREST : VK_FILTER_LINEAR); | 377 | sw == dw && sh == dh ? VK_FILTER_NEAREST : VK_FILTER_LINEAR); |
| 262 | 378 | ||
| 263 | detail::image_barrier(fn, commands, target, range, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, | 379 | finish_image(commands, image); |
| 264 | VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_ACCESS_TRANSFER_WRITE_BIT, | ||
| 265 | VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT); | ||
| 266 | for (const auto& piece : pieces) | 380 | for (const auto& piece : pieces) |
| 267 | detail::image_barrier(fn, commands, binding, piece.range, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, | 381 | detail::image_barrier(fn, commands, binding, piece.range, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 268 | piece.layout, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, any, | 382 | piece.layout, VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, any, |
| @@ -270,6 +384,95 @@ private: | |||
| 270 | vk.vkEndCommandBuffer(commands); | 384 | vk.vkEndCommandBuffer(commands); |
| 271 | } | 385 | } |
| 272 | 386 | ||
| 387 | // Clears the swapchain image to opaque black. | ||
| 388 | void record_blank(VkCommandBuffer commands, std::uint32_t image) { | ||
| 389 | auto& vk = context_.vk; | ||
| 390 | const ReplayFunctions& fn = context_.replay_; | ||
| 391 | vk.vkResetCommandBuffer(commands, 0); | ||
| 392 | VkCommandBufferBeginInfo begin{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; | ||
| 393 | begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; | ||
| 394 | vk.vkBeginCommandBuffer(commands, &begin); | ||
| 395 | const VkImageSubresourceRange range{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; | ||
| 396 | detail::image_barrier(fn, commands, target_of(image), range, VK_IMAGE_LAYOUT_UNDEFINED, | ||
| 397 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TRANSFER_BIT, | ||
| 398 | VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); | ||
| 399 | const VkClearColorValue black{{0.0f, 0.0f, 0.0f, 1.0f}}; | ||
| 400 | vkCmdClearColorImage_(commands, images_[image], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &black, 1, &range); | ||
| 401 | finish_image(commands, image); | ||
| 402 | vk.vkEndCommandBuffer(commands); | ||
| 403 | } | ||
| 404 | |||
| 405 | // The swapchain image is written (TRANSFER_DST): optionally copy it to the capture buffer, then hand it to | ||
| 406 | // the presentation engine. | ||
| 407 | void finish_image(VkCommandBuffer commands, std::uint32_t image) { | ||
| 408 | const ReplayFunctions& fn = context_.replay_; | ||
| 409 | const VkImageSubresourceRange range{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; | ||
| 410 | VkImageLayout layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; | ||
| 411 | VkAccessFlags access = VK_ACCESS_TRANSFER_WRITE_BIT; | ||
| 412 | if (capture_ && capture_buffer_) { | ||
| 413 | detail::image_barrier(fn, commands, target_of(image), range, layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, | ||
| 414 | access, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_READ_BIT, | ||
| 415 | VK_PIPELINE_STAGE_TRANSFER_BIT); | ||
| 416 | VkBufferImageCopy copy{}; | ||
| 417 | copy.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; | ||
| 418 | copy.imageExtent = {extent_.width, extent_.height, 1}; | ||
| 419 | fn.copy_image_to_buffer(commands, images_[image], VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, capture_buffer_, 1, | ||
| 420 | ©); | ||
| 421 | layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; | ||
| 422 | access = VK_ACCESS_TRANSFER_READ_BIT; | ||
| 423 | } | ||
| 424 | detail::image_barrier(fn, commands, target_of(image), range, layout, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, access, | ||
| 425 | VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT); | ||
| 426 | } | ||
| 427 | |||
| 428 | // (Re)creates the capture buffer for the current swapchain size: host visible, coherent, persistently mapped. | ||
| 429 | bool make_capture_buffer() { | ||
| 430 | release_capture(); | ||
| 431 | auto& vk = context_.vk; | ||
| 432 | const VkDevice device = context_.device(); | ||
| 433 | capture_size_ = VkDeviceSize(extent_.width) * extent_.height * 4; | ||
| 434 | VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; | ||
| 435 | info.size = capture_size_; | ||
| 436 | info.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT; | ||
| 437 | info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; | ||
| 438 | if (vk.vkCreateBuffer(device, &info, nullptr, &capture_buffer_) != VK_SUCCESS) return false; | ||
| 439 | VkMemoryRequirements requirements{}; | ||
| 440 | vk.vkGetBufferMemoryRequirements(device, capture_buffer_, &requirements); | ||
| 441 | VkPhysicalDeviceMemoryProperties memory{}; | ||
| 442 | vkGetPhysicalDeviceMemoryProperties_(physical_, &memory); | ||
| 443 | constexpr VkMemoryPropertyFlags wanted = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; | ||
| 444 | std::uint32_t type = UINT32_MAX; | ||
| 445 | for (std::uint32_t i = 0; i < memory.memoryTypeCount; ++i) | ||
| 446 | if ((requirements.memoryTypeBits & (1u << i)) && (memory.memoryTypes[i].propertyFlags & wanted) == wanted) { | ||
| 447 | type = i; | ||
| 448 | break; | ||
| 449 | } | ||
| 450 | VkMemoryAllocateInfo allocate{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; | ||
| 451 | allocate.allocationSize = requirements.size; | ||
| 452 | allocate.memoryTypeIndex = type; | ||
| 453 | void* mapped = nullptr; | ||
| 454 | if (type == UINT32_MAX || vk.vkAllocateMemory(device, &allocate, nullptr, &capture_memory_) != VK_SUCCESS || | ||
| 455 | vk.vkBindBufferMemory(device, capture_buffer_, capture_memory_, 0) != VK_SUCCESS || | ||
| 456 | vk.vkMapMemory(device, capture_memory_, 0, VK_WHOLE_SIZE, 0, &mapped) != VK_SUCCESS) { | ||
| 457 | release_capture(); | ||
| 458 | return false; | ||
| 459 | } | ||
| 460 | capture_ptr_ = static_cast<std::uint8_t*>(mapped); | ||
| 461 | captured_ = false; | ||
| 462 | return true; | ||
| 463 | } | ||
| 464 | |||
| 465 | void release_capture() { | ||
| 466 | auto& vk = context_.vk; | ||
| 467 | const VkDevice device = context_.device(); | ||
| 468 | if (capture_memory_) vk.vkFreeMemory(device, capture_memory_, nullptr); // unmaps | ||
| 469 | if (capture_buffer_) vk.vkDestroyBuffer(device, capture_buffer_, nullptr); | ||
| 470 | capture_memory_ = VK_NULL_HANDLE; | ||
| 471 | capture_buffer_ = VK_NULL_HANDLE; | ||
| 472 | capture_ptr_ = nullptr; | ||
| 473 | captured_ = false; | ||
| 474 | } | ||
| 475 | |||
| 273 | // (Re)creates the swapchain for the surface's current size. A zero-sized surface (minimised) leaves no | 476 | // (Re)creates the swapchain for the surface's current size. A zero-sized surface (minimised) leaves no |
| 274 | // swapchain and is retried on the next present. Nothing is published until every piece exists: on any failure | 477 | // swapchain and is retried on the next present. Nothing is published until every piece exists: on any failure |
| 275 | // the presenter is left with no swapchain and stale_ set, so the next present tries again, and no image is ever | 478 | // the presenter is left with no swapchain and stale_ set, so the next present tries again, and no image is ever |
| @@ -302,13 +505,30 @@ private: | |||
| 302 | std::vector<VkSurfaceFormatKHR> formats(count); | 505 | std::vector<VkSurfaceFormatKHR> formats(count); |
| 303 | vkGetPhysicalDeviceSurfaceFormatsKHR_(physical_, surface_, &count, formats.data()); | 506 | vkGetPhysicalDeviceSurfaceFormatsKHR_(physical_, surface_, &count, formats.data()); |
| 304 | if (formats.empty()) return false; | 507 | if (formats.empty()) return false; |
| 508 | // An sRGB source wants an sRGB swapchain (the blit decodes and encodes, which cancel); any other source | ||
| 509 | // wants UNORM (the bytes go through as they are). BGRA before RGBA, as the surface lists them. | ||
| 305 | VkSurfaceFormatKHR chosen = formats[0]; | 510 | VkSurfaceFormatKHR chosen = formats[0]; |
| 306 | for (const auto& f : formats) | 511 | bool found = false; |
| 307 | if ((f.format == VK_FORMAT_B8G8R8A8_UNORM || f.format == VK_FORMAT_R8G8B8A8_UNORM) && | 512 | for (const VkFormat wanted : want_srgb_ ? std::array<VkFormat, 2>{VK_FORMAT_B8G8R8A8_SRGB, VK_FORMAT_R8G8B8A8_SRGB} |
| 308 | f.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { | 513 | : std::array<VkFormat, 2>{VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM}) { |
| 309 | chosen = f; | 514 | for (const auto& f : formats) |
| 310 | break; | 515 | if (f.format == wanted && f.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { |
| 311 | } | 516 | chosen = f; |
| 517 | found = true; | ||
| 518 | break; | ||
| 519 | } | ||
| 520 | if (found) break; | ||
| 521 | } | ||
| 522 | if (!found && want_srgb_) { | ||
| 523 | if (!warned_srgb_) std::fprintf(stderr, "[rt] present: the surface has no sRGB 8-bit format; an sRGB back buffer is shown darker\n"); | ||
| 524 | warned_srgb_ = true; | ||
| 525 | for (const auto& f : formats) | ||
| 526 | if ((f.format == VK_FORMAT_B8G8R8A8_UNORM || f.format == VK_FORMAT_R8G8B8A8_UNORM) && | ||
| 527 | f.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { | ||
| 528 | chosen = f; | ||
| 529 | break; | ||
| 530 | } | ||
| 531 | } | ||
| 312 | vkGetPhysicalDeviceSurfacePresentModesKHR_(physical_, surface_, &count, nullptr); | 532 | vkGetPhysicalDeviceSurfacePresentModesKHR_(physical_, surface_, &count, nullptr); |
| 313 | std::vector<VkPresentModeKHR> modes(count); | 533 | std::vector<VkPresentModeKHR> modes(count); |
| 314 | vkGetPhysicalDeviceSurfacePresentModesKHR_(physical_, surface_, &count, modes.data()); | 534 | vkGetPhysicalDeviceSurfacePresentModesKHR_(physical_, surface_, &count, modes.data()); |
| @@ -330,6 +550,10 @@ private: | |||
| 330 | info.imageExtent = caps.currentExtent; | 550 | info.imageExtent = caps.currentExtent; |
| 331 | info.imageArrayLayers = 1; | 551 | info.imageArrayLayers = 1; |
| 332 | info.imageUsage = VK_IMAGE_USAGE_TRANSFER_DST_BIT; | 552 | info.imageUsage = VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 553 | if (capture_) { | ||
| 554 | if (!(caps.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT)) return false; | ||
| 555 | info.imageUsage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; | ||
| 556 | } | ||
| 333 | info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; | 557 | info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 334 | info.preTransform = caps.currentTransform; | 558 | info.preTransform = caps.currentTransform; |
| 335 | info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; | 559 | info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; |
| @@ -364,6 +588,12 @@ private: | |||
| 364 | ready_ = std::move(ready); | 588 | ready_ = std::move(ready); |
| 365 | format_ = chosen.format; | 589 | format_ = chosen.format; |
| 366 | extent_ = caps.currentExtent; | 590 | extent_ = caps.currentExtent; |
| 591 | if (capture_) { | ||
| 592 | // A capture that cannot be made fails the recreation with the swapchain in place and stale_ still set. | ||
| 593 | const bool four_bytes = format_ == VK_FORMAT_B8G8R8A8_UNORM || format_ == VK_FORMAT_R8G8B8A8_UNORM || | ||
| 594 | format_ == VK_FORMAT_B8G8R8A8_SRGB || format_ == VK_FORMAT_R8G8B8A8_SRGB; | ||
| 595 | if (!four_bytes || !make_capture_buffer()) return false; | ||
| 596 | } | ||
| 367 | vsync_ = vsync; | 597 | vsync_ = vsync; |
| 368 | stale_ = false; | 598 | stale_ = false; |
| 369 | return true; | 599 | return true; |
| @@ -396,6 +626,16 @@ private: | |||
| 396 | bool vsync_ = true; | 626 | bool vsync_ = true; |
| 397 | bool stale_ = false; | 627 | bool stale_ = false; |
| 398 | bool ok_ = false; | 628 | bool ok_ = false; |
| 629 | bool want_srgb_ = false; // the swapchain format follows the source being presented | ||
| 630 | bool warned_srgb_ = false; | ||
| 631 | bool capture_ = false; | ||
| 632 | VkBuffer capture_buffer_ = VK_NULL_HANDLE; | ||
| 633 | VkDeviceMemory capture_memory_ = VK_NULL_HANDLE; | ||
| 634 | VkDeviceSize capture_size_ = 0; | ||
| 635 | std::uint8_t* capture_ptr_ = nullptr; | ||
| 636 | bool captured_ = false; | ||
| 637 | VkExtent2D captured_extent_{}; | ||
| 638 | VkFormat captured_format_ = VK_FORMAT_UNDEFINED; | ||
| 399 | 639 | ||
| 400 | PFN_vkGetPhysicalDeviceSurfaceSupportKHR vkGetPhysicalDeviceSurfaceSupportKHR_ = nullptr; | 640 | PFN_vkGetPhysicalDeviceSurfaceSupportKHR vkGetPhysicalDeviceSurfaceSupportKHR_ = nullptr; |
| 401 | PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR vkGetPhysicalDeviceSurfaceCapabilitiesKHR_ = nullptr; | 641 | PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR vkGetPhysicalDeviceSurfaceCapabilitiesKHR_ = nullptr; |
| @@ -409,6 +649,8 @@ private: | |||
| 409 | PFN_vkAcquireNextImageKHR vkAcquireNextImageKHR_ = nullptr; | 649 | PFN_vkAcquireNextImageKHR vkAcquireNextImageKHR_ = nullptr; |
| 410 | PFN_vkQueuePresentKHR vkQueuePresentKHR_ = nullptr; | 650 | PFN_vkQueuePresentKHR vkQueuePresentKHR_ = nullptr; |
| 411 | PFN_vkCmdBlitImage vkCmdBlitImage_ = nullptr; | 651 | PFN_vkCmdBlitImage vkCmdBlitImage_ = nullptr; |
| 652 | PFN_vkCmdClearColorImage vkCmdClearColorImage_ = nullptr; | ||
| 653 | PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties_ = nullptr; | ||
| 412 | PFN_vkCreateFence vkCreateFence_ = nullptr; | 654 | PFN_vkCreateFence vkCreateFence_ = nullptr; |
| 413 | PFN_vkDestroyFence vkDestroyFence_ = nullptr; | 655 | PFN_vkDestroyFence vkDestroyFence_ = nullptr; |
| 414 | PFN_vkWaitForFences vkWaitForFences_ = nullptr; | 656 | PFN_vkWaitForFences vkWaitForFences_ = nullptr; |