diff options
| author | SuarezPM <SuarezPM@users.noreply.github.com> | 2026-11-22 19:04:22 -0300 |
|---|---|---|
| committer | SuarezPM <SuarezPM@users.noreply.github.com> | 2026-11-22 19:04:22 -0300 |
| commit | 73bee00a64b1cd70ee12865b17bf62172dcf1111 (patch) | |
| tree | 03a2b2b49ca8dbdf9aa38be1cdb6ad7a3c4aacaf /runtime/src/vulkan_present.h | |
| parent | 557a25ed3e06bf9b399ddebcea6abbd72f15e54e (diff) | |
| parent | 4c52ce27a6cf4af2bbf07a343ede39c5f1572b8d (diff) | |
Merge upstream/main into rt-shader-real-bvh
Keep both heap loaders: main's VKC_HEAP_INIT (VA 0) and this branch's VKC_HEAP_FILE (offset, auto-sized).
Diffstat (limited to 'runtime/src/vulkan_present.h')
| -rw-r--r-- | runtime/src/vulkan_present.h | 304 |
1 files changed, 294 insertions, 10 deletions
diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h index 9a73d2f82..fc6d1699e 100644 --- a/runtime/src/vulkan_present.h +++ b/runtime/src/vulkan_present.h | |||
| @@ -15,12 +15,18 @@ | |||
| 15 | // that follows the source (an sRGB source gets an sRGB swapchain, so the blit's decode and encode cancel; every | 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. | 16 | // other source gets a UNORM one), and an optional capture that copies each presented frame to host memory. |
| 17 | // | 17 | // |
| 18 | // Plane composition (PresentX with placement, a filter, HDR10 or a second plane): present_planes() runs a compute pass | ||
| 19 | // (present_compose.comp, vulkan_present_compose.h) over up to two textures into an RGBA16F image and copies that to the | ||
| 20 | // swapchain; with an HDR10 plane and a surface that lists an ST.2084 format the swapchain is created in it. The plain | ||
| 21 | // blit above is unchanged. | ||
| 22 | // | ||
| 18 | // Tearing on request (PresentX's ImmediateThresholdPercent): with VK_KHR_swapchain_maintenance1 (or the EXT original) | 23 | // Tearing on request (PresentX's ImmediateThresholdPercent): with VK_KHR_swapchain_maintenance1 (or the EXT original) |
| 19 | // the vsync swapchain is created with FIFO plus IMMEDIATE (FIFO_RELAXED when the surface cannot switch to IMMEDIATE), | 24 | // the vsync swapchain is created with FIFO plus IMMEDIATE (FIFO_RELAXED when the surface cannot switch to IMMEDIATE), |
| 20 | // and a present that asks for it is queued in that mode alone. Without the extension, or without either mode, a | 25 | // and a present that asks for it is queued in that mode alone. Without the extension, or without either mode, a |
| 21 | // request is ignored and the frame waits for the vblank. | 26 | // request is ignored and the frame waits for the vblank. |
| 22 | 27 | ||
| 23 | #include "vulkan_backend.h" | 28 | #include "vulkan_backend.h" |
| 29 | #include "vulkan_present_compose.h" | ||
| 24 | 30 | ||
| 25 | // Use the original EXT spellings. They are aliases of the promoted KHR types and are available in older headers. | 31 | // Use the original EXT spellings. They are aliases of the promoted KHR types and are available in older headers. |
| 26 | #if defined(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME) && defined(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME) | 32 | #if defined(VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME) && defined(VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME) |
| @@ -29,11 +35,13 @@ | |||
| 29 | 35 | ||
| 30 | #include <algorithm> | 36 | #include <algorithm> |
| 31 | #include <array> | 37 | #include <array> |
| 38 | #include <cmath> | ||
| 32 | #include <cstdint> | 39 | #include <cstdint> |
| 33 | #include <cstdio> | 40 | #include <cstdio> |
| 34 | #include <cstring> | 41 | #include <cstring> |
| 35 | #include <functional> | 42 | #include <functional> |
| 36 | #include <map> | 43 | #include <map> |
| 44 | #include <memory> | ||
| 37 | #include <vector> | 45 | #include <vector> |
| 38 | 46 | ||
| 39 | namespace leonida { | 47 | namespace leonida { |
| @@ -43,6 +51,28 @@ struct PresentRect { | |||
| 43 | std::int32_t left, top, right, bottom; | 51 | std::int32_t left, top, right, bottom; |
| 44 | }; | 52 | }; |
| 45 | 53 | ||
| 54 | // One plane of present_planes(): a colour texture, the part of it shown, and where in the output it goes. | ||
| 55 | struct PresentPlane { | ||
| 56 | ResourceId source{}; | ||
| 57 | PresentRect source_rect{}; // texels; used when has_source_rect | ||
| 58 | bool has_source_rect = false; | ||
| 59 | float dest[4] = {0, 0, 1, 1}; // left, top, right, bottom as fractions of the output (0..1) | ||
| 60 | bool point_filter = false; // nearest texel; otherwise bilinear inside the source rectangle | ||
| 61 | bool hdr10 = false; // the texture holds ST.2084 (PQ) Rec.2020 values; otherwise sRGB or G2.2 (PresentPlanes::sdr_eotf) Rec.709 ones | ||
| 62 | bool straight_alpha = false; // plane 1 only: alpha is not premultiplied into the colour | ||
| 63 | }; | ||
| 64 | struct PresentPlanes { | ||
| 65 | PresentPlane plane[2]; | ||
| 66 | std::uint32_t count = 1; | ||
| 67 | // What SDR white maps to in an HDR10 output, and what HDR10 is divided by for an SDR one (the white of the SDR output). | ||
| 68 | float sdr_white_nits = 203.0f; | ||
| 69 | // How SDR planes (and an SDR output) are encoded: piecewise sRGB (default; what Windows applies to SDR content in HDR | ||
| 70 | // mode, community-sourced, docs section 4.5) or a pure power of 2.2. | ||
| 71 | enum class SdrEotf : std::uint32_t { srgb = 0, gamma22 = 1 }; | ||
| 72 | SdrEotf sdr_eotf = SdrEotf::srgb; | ||
| 73 | float hdr_peak_nits = 1000.0f; // peak of the HDR10 content: the white point of the tone map on an SDR output | ||
| 74 | }; | ||
| 75 | |||
| 46 | namespace detail { | 76 | namespace detail { |
| 47 | inline bool is_srgb_format(VkFormat format) { | 77 | inline bool is_srgb_format(VkFormat format) { |
| 48 | switch (format) { | 78 | switch (format) { |
| @@ -140,7 +170,9 @@ public: | |||
| 140 | release(swapchain_); | 170 | release(swapchain_); |
| 141 | swapchain_ = VK_NULL_HANDLE; | 171 | swapchain_ = VK_NULL_HANDLE; |
| 142 | release_capture(); | 172 | release_capture(); |
| 173 | composer_.reset(); | ||
| 143 | for (Frame& frame : frames_) { | 174 | for (Frame& frame : frames_) { |
| 175 | drop_views(frame); | ||
| 144 | if (frame.done) vkDestroyFence_(device, frame.done, nullptr); | 176 | if (frame.done) vkDestroyFence_(device, frame.done, nullptr); |
| 145 | if (frame.acquired) vk.vkDestroySemaphore(device, frame.acquired, nullptr); | 177 | if (frame.acquired) vk.vkDestroySemaphore(device, frame.acquired, nullptr); |
| 146 | } | 178 | } |
| @@ -200,6 +232,49 @@ public: | |||
| 200 | return submit(&binding, source, vsync, source_rect, detail::is_srgb_format(binding.format), tear); | 232 | return submit(&binding, source, vsync, source_rect, detail::is_srgb_format(binding.format), tear); |
| 201 | } | 233 | } |
| 202 | 234 | ||
| 235 | // PresentX with planes (docs section 4.5): one or two textures, each scaled from its source rectangle to its | ||
| 236 | // destination rectangle of the window, plane 1 blended over plane 0, composed by a compute pass into an RGBA16F image | ||
| 237 | // that is then copied to the swapchain image. Colour spaces are converted to the swapchain's: when a plane is HDR10 and | ||
| 238 | // the surface lists an HDR10 (ST.2084) format the swapchain uses it (hdr_active()), else the HDR10 planes are | ||
| 239 | // tone mapped to SDR (shoulder above 0.8 of SDR white, then clipped). Planes are filtered and blended in linear light. The swapchain | ||
| 240 | // is always UNORM here; sRGB-format sources are | ||
| 241 | // encoded again by the pass. Sources must be single-sample, sampled (no DENY_SHADER_RESOURCE) float/UNORM/sRGB | ||
| 242 | // colour textures; others are refused (VK_ERROR_FORMAT_NOT_SUPPORTED). Rectangles outside their range give | ||
| 243 | // VK_ERROR_UNKNOWN. vsync and `tear` as for present(). | ||
| 244 | VkResult present_planes(const PresentPlanes& planes, bool vsync, bool tear = false) { | ||
| 245 | if (planes.count < 1 || planes.count > 2) return VK_ERROR_UNKNOWN; | ||
| 246 | if (!(planes.sdr_white_nits > 0.0f) || !std::isfinite(planes.sdr_white_nits)) return VK_ERROR_UNKNOWN; | ||
| 247 | if (!(planes.hdr_peak_nits > 0.0f) || !std::isfinite(planes.hdr_peak_nits)) return VK_ERROR_UNKNOWN; | ||
| 248 | for (std::uint32_t i = 0; i < planes.count; ++i) { | ||
| 249 | const PresentPlane& plane = planes.plane[i]; | ||
| 250 | ResourceBinding binding; | ||
| 251 | { | ||
| 252 | std::shared_lock lock(context_.resource_mutex_); | ||
| 253 | const ResourceBinding* found = context_.resolve(plane.source); | ||
| 254 | if (!found || !found->image) return VK_ERROR_UNKNOWN; | ||
| 255 | binding = *found; | ||
| 256 | } | ||
| 257 | if (binding.aspects != VK_IMAGE_ASPECT_COLOR_BIT || binding.samples > 1 || !binding.sampled || detail::blit_unsupported_format(binding.format) || | ||
| 258 | !(format_features(binding.format) & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) | ||
| 259 | return VK_ERROR_FORMAT_NOT_SUPPORTED; | ||
| 260 | const PresentRect& r = plane.source_rect; | ||
| 261 | if (plane.has_source_rect && (r.left < 0 || r.top < 0 || r.left >= r.right || r.top >= r.bottom || | ||
| 262 | std::uint32_t(r.right) > binding.width || std::uint32_t(r.bottom) > binding.height)) | ||
| 263 | return VK_ERROR_UNKNOWN; | ||
| 264 | const float* d = plane.dest; | ||
| 265 | if (!(d[0] >= 0.0f && d[0] < d[2] && d[2] <= 1.0f && d[1] >= 0.0f && d[1] < d[3] && d[3] <= 1.0f)) | ||
| 266 | return VK_ERROR_UNKNOWN; // (also false for NaN) | ||
| 267 | } | ||
| 268 | return submit(nullptr, ResourceId{}, vsync, nullptr, false, tear, &planes); | ||
| 269 | } | ||
| 270 | // The swapchain holds HDR10 (ST.2084) values: a plane asked for it and the surface has the format. | ||
| 271 | bool hdr_active() const { return hdr_active_; } | ||
| 272 | // Presents that went through the compose pass (present_planes); plain blits and blank frames are not counted. | ||
| 273 | std::uint64_t composed_presents() const { return composed_; } | ||
| 274 | // Tests, on surfaces without HDR10: compose as if the output were HDR10 but write the PQ values into the (8-bit, | ||
| 275 | // UNORM) swapchain, so the encoder can be checked on any GPU. Takes effect with the next present_planes(). | ||
| 276 | void force_hdr_encoding(bool on) { force_hdr_encoding_ = on; } | ||
| 277 | |||
| 203 | // Polled while a present waits for a swapchain image (every 50 ms): when it returns true the present gives up with | 278 | // Polled while a present waits for a swapchain image (every 50 ms): when it returns true the present gives up with |
| 204 | // VK_NOT_READY, so a caller that is being suspended is never held by a window that stopped taking frames. | 279 | // VK_NOT_READY, so a caller that is being suspended is never held by a window that stopped taking frames. |
| 205 | void set_abort(std::function<bool()> abort) { abort_ = std::move(abort); } | 280 | void set_abort(std::function<bool()> abort) { abort_ = std::move(abort); } |
| @@ -216,7 +291,7 @@ public: | |||
| 216 | if (!ok_) return false; | 291 | if (!ok_) return false; |
| 217 | capture_ = true; | 292 | capture_ = true; |
| 218 | stale_ = true; | 293 | stale_ = true; |
| 219 | if (!recreate(vsync_, swapchain_srgb_) || (swapchain_ && !capture_buffer_)) { | 294 | if (!recreate(vsync_, swapchain_srgb_, swapchain_hdr_req_) || (swapchain_ && !capture_buffer_)) { |
| 220 | capture_ = false; | 295 | capture_ = false; |
| 221 | return false; | 296 | return false; |
| 222 | } | 297 | } |
| @@ -236,22 +311,32 @@ public: | |||
| 236 | return true; | 311 | return true; |
| 237 | } | 312 | } |
| 238 | 313 | ||
| 239 | // Shared by present() and present_blank(). `binding` null: clear instead of blit. | 314 | // Shared by present(), present_blank() and present_planes(). `binding` null and `planes` null: clear instead of blit; |
| 315 | // `planes`: compose them (`binding`, `source`, `source_rect` unused). | ||
| 240 | VkResult submit(const ResourceBinding* binding, ResourceId source, bool vsync, const PresentRect* source_rect, | 316 | VkResult submit(const ResourceBinding* binding, ResourceId source, bool vsync, const PresentRect* source_rect, |
| 241 | bool srgb, bool tear = false) { | 317 | bool srgb, bool tear = false, const PresentPlanes* planes = nullptr) { |
| 242 | last_tore_ = false; | 318 | last_tore_ = false; |
| 243 | if (!ok_) return VK_ERROR_INITIALIZATION_FAILED; | 319 | if (!ok_) return VK_ERROR_INITIALIZATION_FAILED; |
| 244 | if (vsync != vsync_ || stale_ || srgb != swapchain_srgb_) { | 320 | // The swapchain's HDR10 format is only for composed planes that ask for it; a blit of an SDR source or a blank |
| 321 | // frame keeps whatever the swapchain has (black is black), except that a blit needs the SDR one. | ||
| 322 | bool want_hdr = swapchain_hdr_req_; | ||
| 323 | if (planes) | ||
| 324 | want_hdr = (planes->plane[0].hdr10) || (planes->count > 1 && planes->plane[1].hdr10); | ||
| 325 | else if (binding) | ||
| 326 | want_hdr = false; | ||
| 327 | if (vsync != vsync_ || stale_ || srgb != swapchain_srgb_ || want_hdr != swapchain_hdr_req_) { | ||
| 245 | // The swapchain's colour encoding is only recorded once the new swapchain exists, so a failed | 328 | // The swapchain's colour encoding is only recorded once the new swapchain exists, so a failed |
| 246 | // recreate is tried again at the next present. | 329 | // recreate is tried again at the next present. |
| 247 | if (!recreate(vsync, srgb)) return VK_ERROR_OUT_OF_DATE_KHR; | 330 | if (!recreate(vsync, srgb, want_hdr)) return VK_ERROR_OUT_OF_DATE_KHR; |
| 248 | } | 331 | } |
| 249 | if (!swapchain_) { // minimised: try again next frame | 332 | if (!swapchain_) { // minimised: try again next frame |
| 250 | stale_ = true; | 333 | stale_ = true; |
| 251 | return VK_SUCCESS; | 334 | return VK_SUCCESS; |
| 252 | } | 335 | } |
| 253 | if (binding && !presentable(source)) return VK_ERROR_UNKNOWN; // checked before an image is acquired for it | 336 | if (binding && !presentable(source)) return VK_ERROR_UNKNOWN; // checked before an image is acquired for it |
| 254 | if (binding && !(format_features(format_) & VK_FORMAT_FEATURE_BLIT_DST_BIT)) return VK_ERROR_FORMAT_NOT_SUPPORTED; | 337 | if ((binding || planes) && !(format_features(format_) & VK_FORMAT_FEATURE_BLIT_DST_BIT)) |
| 338 | return VK_ERROR_FORMAT_NOT_SUPPORTED; | ||
| 339 | if (planes && !prepare_compose()) return VK_ERROR_FORMAT_NOT_SUPPORTED; | ||
| 255 | auto& vk = context_.vk; | 340 | auto& vk = context_.vk; |
| 256 | const VkDevice device = context_.device(); | 341 | const VkDevice device = context_.device(); |
| 257 | Frame& frame = frames_[frame_index_ % frames_.size()]; | 342 | Frame& frame = frames_[frame_index_ % frames_.size()]; |
| @@ -261,18 +346,43 @@ public: | |||
| 261 | std::uint32_t image = 0; | 346 | std::uint32_t image = 0; |
| 262 | VkResult acquired = acquire(frame.acquired, &image); | 347 | VkResult acquired = acquire(frame.acquired, &image); |
| 263 | if (acquired == VK_ERROR_OUT_OF_DATE_KHR) { | 348 | if (acquired == VK_ERROR_OUT_OF_DATE_KHR) { |
| 264 | if (!recreate(vsync, swapchain_srgb_)) return acquired; | 349 | if (!recreate(vsync, swapchain_srgb_, swapchain_hdr_req_)) return acquired; |
| 265 | if (!swapchain_) return VK_SUCCESS; | 350 | if (!swapchain_) return VK_SUCCESS; |
| 266 | acquired = acquire(frame.acquired, &image); | 351 | acquired = acquire(frame.acquired, &image); |
| 267 | } | 352 | } |
| 268 | if (acquired != VK_SUCCESS && acquired != VK_SUBOPTIMAL_KHR) return acquired; // nothing was acquired | 353 | if (acquired != VK_SUCCESS && acquired != VK_SUBOPTIMAL_KHR) return acquired; // nothing was acquired |
| 269 | if (acquired == VK_SUBOPTIMAL_KHR) stale_ = true; | 354 | if (acquired == VK_SUBOPTIMAL_KHR) stale_ = true; |
| 355 | // An out-of-date retry above may have given the swapchain another extent: the compose target follows it. | ||
| 356 | if (planes && !prepare_compose()) return give_back(frame, VK_ERROR_FORMAT_NOT_SUPPORTED); | ||
| 270 | 357 | ||
| 271 | bool recorded = false; | 358 | bool recorded = false; |
| 272 | std::unique_lock tracker_lock(context_.tracker_mutex_, std::defer_lock); | 359 | std::unique_lock tracker_lock(context_.tracker_mutex_, std::defer_lock); |
| 273 | bool rollback_source = false; | 360 | bool rollback_source = false; |
| 274 | VkImageLayout prev_layout = VK_IMAGE_LAYOUT_UNDEFINED; | 361 | VkImageLayout prev_layout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 275 | if (binding) { | 362 | std::array<RollbackPlane, 2> plane_rollbacks{}; // present_planes(): the same, per plane |
| 363 | if (planes) { | ||
| 364 | std::array<ResourceBinding, 2> current{}; | ||
| 365 | bool found_all = true; | ||
| 366 | { | ||
| 367 | std::shared_lock resource_lock(context_.resource_mutex_); | ||
| 368 | for (std::uint32_t i = 0; i < planes->count; ++i) { | ||
| 369 | const ResourceBinding* found = context_.resolve(planes->plane[i].source); | ||
| 370 | if (found && found->image && found->aspects == VK_IMAGE_ASPECT_COLOR_BIT && found->sampled) | ||
| 371 | current[i] = *found; | ||
| 372 | else | ||
| 373 | found_all = false; | ||
| 374 | } | ||
| 375 | if (found_all) { | ||
| 376 | tracker_lock.lock(); | ||
| 377 | for (std::uint32_t i = 0; i < planes->count; ++i) { | ||
| 378 | plane_rollbacks[i].id = planes->plane[i].source; | ||
| 379 | plane_rollbacks[i].tracked = context_.tracker_.tracked(plane_rollbacks[i].id); | ||
| 380 | if (plane_rollbacks[i].tracked) plane_rollbacks[i].layout = context_.tracker_.layout(plane_rollbacks[i].id, 0); | ||
| 381 | } | ||
| 382 | recorded = record_planes(frame, std::uint32_t(frame_index_ % frames_.size()), current, *planes, image); | ||
| 383 | } | ||
| 384 | } | ||
| 385 | } else if (binding) { | ||
| 276 | // Lock hierarchy: resource_mutex_ -> tracker_mutex_ -> submit_mutex_ (vulkan_backend.h). | 386 | // Lock hierarchy: resource_mutex_ -> tracker_mutex_ -> submit_mutex_ (vulkan_backend.h). |
| 277 | // record() only touches subresource 0 (mip 0, layer 0, color) of `source`, so saving that one layout | 387 | // record() only touches subresource 0 (mip 0, layer 0, color) of `source`, so saving that one layout |
| 278 | // rolls back a failed submit in O(1) without copying the entire LayoutTracker map. | 388 | // rolls back a failed submit in O(1) without copying the entire LayoutTracker map. |
| @@ -292,6 +402,11 @@ public: | |||
| 292 | const auto rollback = [&] { | 402 | const auto rollback = [&] { |
| 293 | if (rollback_source) | 403 | if (rollback_source) |
| 294 | context_.tracker_.set(source, {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}, prev_layout); | 404 | context_.tracker_.set(source, {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}, prev_layout); |
| 405 | if (planes) | ||
| 406 | for (std::uint32_t i = 0; i < planes->count; ++i) | ||
| 407 | if (plane_rollbacks[i].tracked) | ||
| 408 | context_.tracker_.set(plane_rollbacks[i].id, {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}, | ||
| 409 | plane_rollbacks[i].layout); | ||
| 295 | }; | 410 | }; |
| 296 | if (!recorded) return give_back(frame, VK_ERROR_UNKNOWN); // released by another thread since presentable() | 411 | if (!recorded) return give_back(frame, VK_ERROR_UNKNOWN); // released by another thread since presentable() |
| 297 | if (const VkResult reset = vkResetFences_(device, 1, &frame.done); reset != VK_SUCCESS) { | 412 | if (const VkResult reset = vkResetFences_(device, 1, &frame.done); reset != VK_SUCCESS) { |
| @@ -342,6 +457,7 @@ public: | |||
| 342 | // is recreated next time, and the caller sees VK_SUCCESS, since its work was submitted. | 457 | // is recreated next time, and the caller sees VK_SUCCESS, since its work was submitted. |
| 343 | if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) { | 458 | if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) { |
| 344 | ++presented_; | 459 | ++presented_; |
| 460 | if (planes) ++composed_; | ||
| 345 | if (tearing) { | 461 | if (tearing) { |
| 346 | last_tore_ = true; | 462 | last_tore_ = true; |
| 347 | ++tear_presents_; | 463 | ++tear_presents_; |
| @@ -364,6 +480,7 @@ private: | |||
| 364 | VkCommandBuffer commands = VK_NULL_HANDLE; | 480 | VkCommandBuffer commands = VK_NULL_HANDLE; |
| 365 | VkFence done = VK_NULL_HANDLE; | 481 | VkFence done = VK_NULL_HANDLE; |
| 366 | VkSemaphore acquired = VK_NULL_HANDLE; | 482 | VkSemaphore acquired = VK_NULL_HANDLE; |
| 483 | std::array<VkImageView, 2> views{}; // the plane views of the last composed present, until the frame is reused | ||
| 367 | }; | 484 | }; |
| 368 | 485 | ||
| 369 | // Whether `source` is a colour texture the device knows, so present() can refuse it before acquiring an image. | 486 | // Whether `source` is a colour texture the device knows, so present() can refuse it before acquiring an image. |
| @@ -462,6 +579,141 @@ private: | |||
| 462 | vk.vkEndCommandBuffer(commands); | 579 | vk.vkEndCommandBuffer(commands); |
| 463 | } | 580 | } |
| 464 | 581 | ||
| 582 | struct RollbackPlane { | ||
| 583 | ResourceId id{}; | ||
| 584 | bool tracked = false; | ||
| 585 | VkImageLayout layout = VK_IMAGE_LAYOUT_UNDEFINED; | ||
| 586 | }; | ||
| 587 | |||
| 588 | void drop_views(Frame& frame) { | ||
| 589 | for (VkImageView& view : frame.views) | ||
| 590 | if (view) context_.vk.vkDestroyImageView(context_.device(), view, nullptr); | ||
| 591 | frame.views = {}; | ||
| 592 | } | ||
| 593 | |||
| 594 | // The compose pass and its target image, for the swapchain's current size. | ||
| 595 | bool prepare_compose() { | ||
| 596 | if (!composer_) { | ||
| 597 | VkPhysicalDeviceMemoryProperties memory{}; | ||
| 598 | if (!vkGetPhysicalDeviceMemoryProperties_) return false; | ||
| 599 | vkGetPhysicalDeviceMemoryProperties_(physical_, &memory); | ||
| 600 | composer_ = std::make_unique<PresentComposer>(context_, memory); | ||
| 601 | } | ||
| 602 | if (!composer_->ready()) return false; | ||
| 603 | if (composer_->target() && composer_->extent().width == extent_.width && | ||
| 604 | composer_->extent().height == extent_.height) | ||
| 605 | return true; | ||
| 606 | { | ||
| 607 | std::lock_guard lock(context_.submit_mutex()); | ||
| 608 | context_.vk.vkQueueWaitIdle(queue_); // the old target may still be in use | ||
| 609 | } | ||
| 610 | bool changed = false; | ||
| 611 | return composer_->ensure_target(extent_, changed); | ||
| 612 | } | ||
| 613 | |||
| 614 | // Records the composition of `planes` and its copy to swapchain image `image`. False, with nothing recorded and the | ||
| 615 | // tracker untouched, when a view cannot be made. The caller holds the resource and tracker locks. | ||
| 616 | bool record_planes(Frame& frame, std::uint32_t slot, const std::array<ResourceBinding, 2>& current, | ||
| 617 | const PresentPlanes& planes, std::uint32_t image) { | ||
| 618 | auto& vk = context_.vk; | ||
| 619 | const ReplayFunctions& fn = context_.replay_; | ||
| 620 | if (composer_->target() == VK_NULL_HANDLE || composer_->extent().width != extent_.width || | ||
| 621 | composer_->extent().height != extent_.height) | ||
| 622 | return false; // the target does not match the swapchain: nothing recorded | ||
| 623 | drop_views(frame); | ||
| 624 | for (std::uint32_t i = 0; i < planes.count; ++i) { | ||
| 625 | VkImageViewCreateInfo view_info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO}; | ||
| 626 | view_info.image = current[i].image; | ||
| 627 | view_info.viewType = VK_IMAGE_VIEW_TYPE_2D; | ||
| 628 | view_info.format = current[i].format; | ||
| 629 | view_info.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; | ||
| 630 | if (vk.vkCreateImageView(context_.device(), &view_info, nullptr, &frame.views[i]) != VK_SUCCESS) { | ||
| 631 | drop_views(frame); | ||
| 632 | return false; | ||
| 633 | } | ||
| 634 | } | ||
| 635 | if (planes.count == 1) frame.views[1] = VK_NULL_HANDLE; // the shader never reads it; plane 0's view is bound twice | ||
| 636 | |||
| 637 | VkCommandBuffer commands = frame.commands; | ||
| 638 | vk.vkResetCommandBuffer(commands, 0); | ||
| 639 | VkCommandBufferBeginInfo begin{VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; | ||
| 640 | begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; | ||
| 641 | vk.vkBeginCommandBuffer(commands, &begin); | ||
| 642 | |||
| 643 | constexpr VkAccessFlags any = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT; | ||
| 644 | const VkImageSubresourceRange range{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; | ||
| 645 | const bool same = planes.count > 1 && planes.plane[1].source == planes.plane[0].source; | ||
| 646 | std::array<LayoutPieces, 2> pieces; | ||
| 647 | std::array<VkImageLayout, 2> read_layout{}; | ||
| 648 | // Each distinct source: its PRESENT (common) layout, then readable by the compute pass. A never-written back buffer | ||
| 649 | // is zero-filled first, as any committed resource is. | ||
| 650 | for (std::uint32_t i = 0; i < planes.count; ++i) { | ||
| 651 | read_layout[i] = image_layout(current[i], VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); | ||
| 652 | if (i == 1 && same) continue; | ||
| 653 | const VkImageLayout common = image_layout(current[i], VK_IMAGE_LAYOUT_GENERAL); | ||
| 654 | pieces[i] = detail::current(&context_.tracker_, planes.plane[i].source, range, common); | ||
| 655 | detail::initialize(fn, commands, current[i], planes.plane[i].source, pieces[i], common, &context_.tracker_); | ||
| 656 | for (const auto& piece : pieces[i]) | ||
| 657 | detail::image_barrier(fn, commands, current[i], piece.range, piece.layout, read_layout[i], | ||
| 658 | VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, | ||
| 659 | VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT); | ||
| 660 | } | ||
| 661 | |||
| 662 | // The pass: destinations in output pixels, sources in texels. | ||
| 663 | PresentComposePush push{}; | ||
| 664 | const float out_w = float(extent_.width), out_h = float(extent_.height); | ||
| 665 | std::uint32_t* flags[2] = {&push.flags0, &push.flags1}; | ||
| 666 | float* dest[2] = {push.dest0, push.dest1}; | ||
| 667 | float* src[2] = {push.src0, push.src1}; | ||
| 668 | for (std::uint32_t i = 0; i < planes.count; ++i) { | ||
| 669 | const PresentPlane& plane = planes.plane[i]; | ||
| 670 | dest[i][0] = plane.dest[0] * out_w; | ||
| 671 | dest[i][1] = plane.dest[1] * out_h; | ||
| 672 | dest[i][2] = plane.dest[2] * out_w; | ||
| 673 | dest[i][3] = plane.dest[3] * out_h; | ||
| 674 | const PresentRect whole{0, 0, std::int32_t(current[i].width), std::int32_t(current[i].height)}; | ||
| 675 | const PresentRect& r = plane.has_source_rect ? plane.source_rect : whole; | ||
| 676 | src[i][0] = float(r.left); | ||
| 677 | src[i][1] = float(r.top); | ||
| 678 | src[i][2] = float(r.right); | ||
| 679 | src[i][3] = float(r.bottom); | ||
| 680 | *flags[i] = (plane.point_filter ? compose_flag::point : 0u) | | ||
| 681 | (detail::is_srgb_format(current[i].format) ? compose_flag::srgb_source : 0u) | | ||
| 682 | (plane.hdr10 ? compose_flag::hdr10 : 0u) | (plane.straight_alpha ? compose_flag::straight_alpha : 0u); | ||
| 683 | } | ||
| 684 | push.plane_count = planes.count; | ||
| 685 | push.out_hdr = (hdr_active_ || force_hdr_encoding_) ? 1u : 0u; | ||
| 686 | push.sdr_white = planes.sdr_white_nits; | ||
| 687 | push.sdr_eotf = std::uint32_t(planes.sdr_eotf); | ||
| 688 | push.hdr_peak = planes.hdr_peak_nits; | ||
| 689 | push.width = extent_.width; | ||
| 690 | push.height = extent_.height; | ||
| 691 | composer_->record(commands, slot, frame.views[0], read_layout[0], frame.views[1] ? frame.views[1] : frame.views[0], | ||
| 692 | read_layout[1] ? read_layout[1] : read_layout[0], push); | ||
| 693 | |||
| 694 | // The swapchain image, written by a copy of the composed image (same size: no scaling). | ||
| 695 | ResourceBinding target = target_of(image); | ||
| 696 | detail::image_barrier(fn, commands, target, range, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, | ||
| 697 | VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); | ||
| 698 | VkImageBlit blit{}; | ||
| 699 | blit.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; | ||
| 700 | blit.srcOffsets[1] = {std::int32_t(extent_.width), std::int32_t(extent_.height), 1}; | ||
| 701 | blit.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}; | ||
| 702 | blit.dstOffsets[1] = {std::int32_t(extent_.width), std::int32_t(extent_.height), 1}; | ||
| 703 | vkCmdBlitImage_(commands, composer_->target(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, images_[image], | ||
| 704 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, VK_FILTER_NEAREST); | ||
| 705 | finish_image(commands, image); | ||
| 706 | for (std::uint32_t i = 0; i < planes.count; ++i) { | ||
| 707 | if (i == 1 && same) continue; | ||
| 708 | for (const auto& piece : pieces[i]) | ||
| 709 | detail::image_barrier(fn, commands, current[i], piece.range, read_layout[i], piece.layout, | ||
| 710 | VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, any, | ||
| 711 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); | ||
| 712 | } | ||
| 713 | vk.vkEndCommandBuffer(commands); | ||
| 714 | return true; | ||
| 715 | } | ||
| 716 | |||
| 465 | // vkGetPhysicalDeviceFormatProperties, optimal tiling, cached per format. | 717 | // vkGetPhysicalDeviceFormatProperties, optimal tiling, cached per format. |
| 466 | VkFormatFeatureFlags format_features(VkFormat format) { | 718 | VkFormatFeatureFlags format_features(VkFormat format) { |
| 467 | const auto it = features_.find(format); | 719 | const auto it = features_.find(format); |
| @@ -595,7 +847,7 @@ private: | |||
| 595 | // (Re)creates the swapchain for the surface's current size. A zero-sized surface (minimised) leaves no | 847 | // (Re)creates the swapchain for the surface's current size. A zero-sized surface (minimised) leaves no |
| 596 | // swapchain and is retried on the next present. Keep the old swapchain until the surface queries succeed and | 848 | // swapchain and is retried on the next present. Keep the old swapchain until the surface queries succeed and |
| 597 | // the replacement can be attempted. An incomplete replacement stays stale so the next present tries again. | 849 | // the replacement can be attempted. An incomplete replacement stays stale so the next present tries again. |
| 598 | bool recreate(bool vsync, bool srgb) { | 850 | bool recreate(bool vsync, bool srgb, bool hdr = false) { |
| 599 | auto& vk = context_.vk; | 851 | auto& vk = context_.vk; |
| 600 | const VkDevice device = context_.device(); | 852 | const VkDevice device = context_.device(); |
| 601 | { | 853 | { |
| @@ -625,6 +877,8 @@ private: | |||
| 625 | swapchain_ = VK_NULL_HANDLE; | 877 | swapchain_ = VK_NULL_HANDLE; |
| 626 | vsync_ = vsync; | 878 | vsync_ = vsync; |
| 627 | swapchain_srgb_ = srgb; | 879 | swapchain_srgb_ = srgb; |
| 880 | swapchain_hdr_req_ = hdr; | ||
| 881 | hdr_active_ = false; | ||
| 628 | stale_ = false; | 882 | stale_ = false; |
| 629 | return true; | 883 | return true; |
| 630 | } | 884 | } |
| @@ -643,8 +897,28 @@ private: | |||
| 643 | // wants UNORM (the bytes go through as they are). BGRA before RGBA, as the surface lists them. | 897 | // wants UNORM (the bytes go through as they are). BGRA before RGBA, as the surface lists them. |
| 644 | VkSurfaceFormatKHR chosen = formats[0]; | 898 | VkSurfaceFormatKHR chosen = formats[0]; |
| 645 | bool found = false; | 899 | bool found = false; |
| 900 | bool chose_hdr = false; | ||
| 901 | if (hdr) { | ||
| 902 | // A plane in HDR10 (PQ, Rec.2020): the surface's ST.2084 format, which needs VK_EXT_swapchain_colorspace on | ||
| 903 | // the instance. Without it the composition converts to SDR. | ||
| 904 | for (const VkFormat wanted : {VK_FORMAT_A2B10G10R10_UNORM_PACK32, VK_FORMAT_A2R10G10B10_UNORM_PACK32}) { | ||
| 905 | for (const auto& f : formats) | ||
| 906 | if (f.format == wanted && f.colorSpace == VK_COLOR_SPACE_HDR10_ST2084_EXT) { | ||
| 907 | chosen = f; | ||
| 908 | found = chose_hdr = true; | ||
| 909 | break; | ||
| 910 | } | ||
| 911 | if (found) break; | ||
| 912 | } | ||
| 913 | if (!found && !warned_hdr_) { | ||
| 914 | warned_hdr_ = true; | ||
| 915 | std::fprintf(stderr, "[rt] present: the surface has no HDR10 (ST.2084) format; HDR10 planes are converted to " | ||
| 916 | "SDR by tone mapping\n"); | ||
| 917 | } | ||
| 918 | } | ||
| 646 | for (const VkFormat wanted : srgb ? std::array<VkFormat, 2>{VK_FORMAT_B8G8R8A8_SRGB, VK_FORMAT_R8G8B8A8_SRGB} | 919 | for (const VkFormat wanted : srgb ? std::array<VkFormat, 2>{VK_FORMAT_B8G8R8A8_SRGB, VK_FORMAT_R8G8B8A8_SRGB} |
| 647 | : std::array<VkFormat, 2>{VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM}) { | 920 | : std::array<VkFormat, 2>{VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM}) { |
| 921 | if (chose_hdr) break; | ||
| 648 | for (const auto& f : formats) | 922 | for (const auto& f : formats) |
| 649 | if (f.format == wanted && f.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { | 923 | if (f.format == wanted && f.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { |
| 650 | chosen = f; | 924 | chosen = f; |
| @@ -739,14 +1013,18 @@ private: | |||
| 739 | ready_ = std::move(ready); | 1013 | ready_ = std::move(ready); |
| 740 | format_ = chosen.format; | 1014 | format_ = chosen.format; |
| 741 | extent_ = extent; | 1015 | extent_ = extent; |
| 1016 | hdr_active_ = chose_hdr; | ||
| 742 | if (capture_) { | 1017 | if (capture_) { |
| 743 | // A capture that cannot be made fails the recreation with the swapchain in place and stale_ still set. | 1018 | // A capture that cannot be made fails the recreation with the swapchain in place and stale_ still set. |
| 744 | const bool four_bytes = format_ == VK_FORMAT_B8G8R8A8_UNORM || format_ == VK_FORMAT_R8G8B8A8_UNORM || | 1019 | const bool four_bytes = format_ == VK_FORMAT_B8G8R8A8_UNORM || format_ == VK_FORMAT_R8G8B8A8_UNORM || |
| 745 | format_ == VK_FORMAT_B8G8R8A8_SRGB || format_ == VK_FORMAT_R8G8B8A8_SRGB; | 1020 | format_ == VK_FORMAT_B8G8R8A8_SRGB || format_ == VK_FORMAT_R8G8B8A8_SRGB || |
| 1021 | format_ == VK_FORMAT_A2B10G10R10_UNORM_PACK32 || | ||
| 1022 | format_ == VK_FORMAT_A2R10G10B10_UNORM_PACK32; | ||
| 746 | if (!four_bytes || !make_capture_buffer()) return false; | 1023 | if (!four_bytes || !make_capture_buffer()) return false; |
| 747 | } | 1024 | } |
| 748 | vsync_ = vsync; | 1025 | vsync_ = vsync; |
| 749 | swapchain_srgb_ = srgb; | 1026 | swapchain_srgb_ = srgb; |
| 1027 | swapchain_hdr_req_ = hdr; | ||
| 750 | stale_ = false; | 1028 | stale_ = false; |
| 751 | return true; | 1029 | return true; |
| 752 | } | 1030 | } |
| @@ -836,6 +1114,12 @@ private: | |||
| 836 | bool stale_ = false; | 1114 | bool stale_ = false; |
| 837 | bool ok_ = false; | 1115 | bool ok_ = false; |
| 838 | bool swapchain_srgb_ = false; // the swapchain format follows the source being presented (set once it exists) | 1116 | bool swapchain_srgb_ = false; // the swapchain format follows the source being presented (set once it exists) |
| 1117 | bool swapchain_hdr_req_ = false; // the swapchain was built for an HDR10 request (it has the format only if hdr_active_) | ||
| 1118 | bool hdr_active_ = false; | ||
| 1119 | std::uint64_t composed_ = 0; | ||
| 1120 | bool force_hdr_encoding_ = false; | ||
| 1121 | bool warned_hdr_ = false; | ||
| 1122 | std::unique_ptr<PresentComposer> composer_; | ||
| 839 | std::function<bool()> abort_; | 1123 | std::function<bool()> abort_; |
| 840 | std::function<VkExtent2D()> extent_fn_; | 1124 | std::function<VkExtent2D()> extent_fn_; |
| 841 | std::map<VkFormat, VkFormatFeatureFlags> features_; | 1125 | std::map<VkFormat, VkFormatFeatureFlags> features_; |