upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/dxgi_com.cpp
diff options
context:
space:
mode:
authorlines9686-rgb <282495677+lines9686-rgb@users.noreply.github.com>2026-11-23 20:58:52 +0300
committerlines9686-rgb <282495677+lines9686-rgb@users.noreply.github.com>2026-11-24 07:16:33 +0300
commita3090aab2d29bb859a87bfb22dc38a994629054a (patch)
tree47ae3d4a8d7cb8ad2ada744f809936a76dd24876 /runtime/shims/dxgi_com.cpp
parentece4ba5b31ebc6f345c8e29eae1261f2f780057a (diff)
dxgi: a failed ResizeBuffers leaves the swap chain without buffers until the next one succeeds, as on the host
Measured against the host runtime (RTX 3060 Ti, NVIDIA 610.88, and WARP): once ResizeBuffers accepts its arguments the old buffers are gone. If the new ones can't be made it fails, and the swap chain stays without buffers. GetDesc1 already reports the requested size and count, GetBuffer and Present are DXGI_ERROR_INVALID_CALL, and the next successful ResizeBuffers restores everything. A side above 16384 is E_INVALIDARG on the hardware device. A call refused for its arguments (a back buffer still held) changes nothing. The layer answered E_FAIL and kept describing the old buffers. It now answers E_INVALIDARG for a side above 16384 before calling Vulkan (it used to pass the extent to vkCreateImage, past the device's limits) and E_OUTOFMEMORY when Vulkan fails. It reports the requested description, refuses Present, and recovers on the next ResizeBuffers. dxgi_swapchain_test compares 15 steps of that with the host's default adapter. dxgi_swapchain_count_test gets failure injection on its fake Vulkan table: whether the offscreen ring or vkCreateSwapchainKHR fails (the latter retires the old swapchain anyway), on the offscreen and on the direct path, the next resize builds a working swapchain. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/dxgi_com.cpp')
-rw-r--r--runtime/shims/dxgi_com.cpp60
1 files changed, 49 insertions, 11 deletions
diff --git a/runtime/shims/dxgi_com.cpp b/runtime/shims/dxgi_com.cpp
index ce40348f8..a65a1cb6d 100644
--- a/runtime/shims/dxgi_com.cpp
+++ b/runtime/shims/dxgi_com.cpp
@@ -305,6 +305,7 @@ public:
305 if ((flags & DXGI_PRESENT_ALLOW_TEARING) && 305 if ((flags & DXGI_PRESENT_ALLOW_TEARING) &&
306 (sync != 0 || !(flags_ & DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING))) 306 (sync != 0 || !(flags_ & DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING)))
307 return DXGI_ERROR_INVALID_CALL; 307 return DXGI_ERROR_INVALID_CALL;
308 if (lost_) return DXGI_ERROR_INVALID_CALL; // host-measured: no buffers after a failed ResizeBuffers
308 if (flags & DXGI_PRESENT_TEST) return S_OK; // only asks whether a present would succeed 309 if (flags & DXGI_PRESENT_TEST) return S_OK; // only asks whether a present would succeed
309 const std::uint32_t cur = swapchain_.current_back_buffer_index(); 310 const std::uint32_t cur = swapchain_.current_back_buffer_index();
310 if (!swapchain_.present(cur)) return E_FAIL; 311 if (!swapchain_.present(cur)) return E_FAIL;
@@ -334,12 +335,12 @@ public:
334 HRESULT STDMETHODCALLTYPE GetDesc(DXGI_SWAP_CHAIN_DESC* desc) override { 335 HRESULT STDMETHODCALLTYPE GetDesc(DXGI_SWAP_CHAIN_DESC* desc) override {
335 if (!desc) return E_INVALIDARG; 336 if (!desc) return E_INVALIDARG;
336 std::memset(desc, 0, sizeof(*desc)); 337 std::memset(desc, 0, sizeof(*desc));
337 desc->BufferDesc.Width = swapchain_.extent().width; 338 desc->BufferDesc.Width = lost_ ? lost_width_ : swapchain_.extent().width;
338 desc->BufferDesc.Height = swapchain_.extent().height; 339 desc->BufferDesc.Height = lost_ ? lost_height_ : swapchain_.extent().height;
339 desc->BufferDesc.Format = vk_to_dxgi_format(swapchain_.format()); 340 desc->BufferDesc.Format = current_format();
340 desc->SampleDesc.Count = 1; 341 desc->SampleDesc.Count = 1;
341 desc->BufferUsage = buffer_usage_; 342 desc->BufferUsage = buffer_usage_;
342 desc->BufferCount = swapchain_.image_count(); 343 desc->BufferCount = lost_ ? lost_count_ : swapchain_.image_count();
343 desc->OutputWindow = hwnd_; 344 desc->OutputWindow = hwnd_;
344 desc->Windowed = TRUE; 345 desc->Windowed = TRUE;
345 desc->SwapEffect = swap_effect_; 346 desc->SwapEffect = swap_effect_;
@@ -347,6 +348,19 @@ public:
347 return S_OK; 348 return S_OK;
348 } 349 }
349 350
351 // The swap chain's format as GetDesc reports it.
352 DXGI_FORMAT current_format() const { return lost_ ? lost_format_ : vk_to_dxgi_format(swapchain_.format()); }
353
354 // A failed ResizeBuffers (see resize_buffers): no buffers until the next successful one, and the description
355 // reports what was asked for.
356 void lose_buffers(std::uint32_t width, std::uint32_t height, UINT count, DXGI_FORMAT format) {
357 lost_ = true;
358 lost_width_ = width;
359 lost_height_ = height;
360 lost_count_ = count;
361 lost_format_ = format;
362 }
363
350 HRESULT STDMETHODCALLTYPE ResizeBuffers(UINT count, UINT width, UINT height, DXGI_FORMAT fmt, UINT flags) override { 364 HRESULT STDMETHODCALLTYPE ResizeBuffers(UINT count, UINT width, UINT height, DXGI_FORMAT fmt, UINT flags) override {
351 return resize_buffers(count, width, height, fmt, flags, nullptr, nullptr); 365 return resize_buffers(count, width, height, fmt, flags, nullptr, nullptr);
352 } 366 }
@@ -374,18 +388,37 @@ public:
374 // DXGI refuses to resize while the app still holds a back buffer reference. 388 // DXGI refuses to resize while the app still holds a back buffer reference.
375 for (auto* bb : buffers_) 389 for (auto* bb : buffers_)
376 if (bb->held_by_app()) return DXGI_ERROR_INVALID_CALL; 390 if (bb->held_by_app()) return DXGI_ERROR_INVALID_CALL;
377 const std::size_t target_count = count ? count : buffers_.size(); 391 const std::size_t target_count = count ? count : (lost_ ? lost_count_ : buffers_.size());
378 if (node_masks && !std::all_of(node_masks, node_masks + target_count, [](UINT mask) { return mask <= 1; })) 392 if (node_masks && !std::all_of(node_masks, node_masks + target_count, [](UINT mask) { return mask <= 1; }))
379 return DXGI_ERROR_INVALID_CALL; 393 return DXGI_ERROR_INVALID_CALL;
380 if (present_queues && !std::all_of(present_queues, present_queues + target_count, is_direct_present_queue)) 394 if (present_queues && !std::all_of(present_queues, present_queues + target_count, is_direct_present_queue))
381 return DXGI_ERROR_INVALID_CALL; 395 return DXGI_ERROR_INVALID_CALL;
382 unregister_buffers(); 396 unregister_buffers();
383 const VkExtent2D size = size_or_client(width, height, swapchain_.extent()); 397 const VkExtent2D size = size_or_client(width, height, lost_ ? VkExtent2D{lost_width_, lost_height_}
398 : swapchain_.extent());
384 const std::uint32_t w = size.width; 399 const std::uint32_t w = size.width;
385 const std::uint32_t h = size.height; 400 const std::uint32_t h = size.height;
386 const UINT next_flags = flags ? flags : flags_; 401 const UINT next_flags = flags ? flags : flags_;
387 const bool allow_tearing = (next_flags & DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING) != 0; 402 const bool allow_tearing = (next_flags & DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING) != 0;
388 if (!swapchain_.resize(w, h, vk_fmt, count, allow_tearing)) return E_FAIL; 403 // Host-measured (RTX 3060 Ti / NVIDIA 610.88 and WARP): once the arguments are accepted, the old buffers are
404 // gone. When the new ones can't be made, ResizeBuffers fails and the swap chain stays without buffers: its
405 // description already has the new size and count, GetBuffer and Present are DXGI_ERROR_INVALID_CALL, and the
406 // next successful ResizeBuffers brings it back. A side above 16384 is E_INVALIDARG on the hardware device
407 // (WARP takes 16385 and answers E_OUTOFMEMORY at 32768); a Vulkan failure here is E_OUTOFMEMORY.
408 // The size check also keeps vkCreateImage from an extent past the device's limits.
409 const DXGI_FORMAT next_format = fmt != DXGI_FORMAT_UNKNOWN ? fmt : current_format();
410 if (w > D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION || h > D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION) {
411 lose_buffers(w, h, static_cast<UINT>(target_count), next_format);
412 return E_INVALIDARG;
413 }
414 // Format UNKNOWN keeps the swap chain's format, which after a failed ResizeBuffers is the one it asked for.
415 const VkFormat resize_format = vk_fmt != VK_FORMAT_UNDEFINED ? vk_fmt
416 : lost_ ? dxgi_to_vk_format(lost_format_) : VK_FORMAT_UNDEFINED;
417 if (!swapchain_.resize(w, h, resize_format, static_cast<std::uint32_t>(target_count), allow_tearing)) {
418 lose_buffers(w, h, static_cast<UINT>(target_count), next_format);
419 return E_OUTOFMEMORY;
420 }
421 lost_ = false;
389 source_size_ = {w, h}; // host-measured: ResizeBuffers resets the source region to the new back buffers 422 source_size_ = {w, h}; // host-measured: ResizeBuffers resets the source region to the new back buffers
390 statistics_disjoint_.store(true, std::memory_order_relaxed); 423 statistics_disjoint_.store(true, std::memory_order_relaxed);
391 flags_ = next_flags; 424 flags_ = next_flags;
@@ -462,12 +495,12 @@ public:
462 HRESULT STDMETHODCALLTYPE GetDesc1(DXGI_SWAP_CHAIN_DESC1* desc) override { 495 HRESULT STDMETHODCALLTYPE GetDesc1(DXGI_SWAP_CHAIN_DESC1* desc) override {
463 if (!desc) return E_INVALIDARG; 496 if (!desc) return E_INVALIDARG;
464 std::memset(desc, 0, sizeof(*desc)); 497 std::memset(desc, 0, sizeof(*desc));
465 desc->Width = swapchain_.extent().width; 498 desc->Width = lost_ ? lost_width_ : swapchain_.extent().width;
466 desc->Height = swapchain_.extent().height; 499 desc->Height = lost_ ? lost_height_ : swapchain_.extent().height;
467 desc->Format = vk_to_dxgi_format(swapchain_.format()); 500 desc->Format = current_format();
468 desc->SampleDesc.Count = 1; 501 desc->SampleDesc.Count = 1;
469 desc->BufferUsage = buffer_usage_; 502 desc->BufferUsage = buffer_usage_;
470 desc->BufferCount = swapchain_.image_count(); 503 desc->BufferCount = lost_ ? lost_count_ : swapchain_.image_count();
471 desc->Scaling = DXGI_SCALING_STRETCH; 504 desc->Scaling = DXGI_SCALING_STRETCH;
472 desc->SwapEffect = swap_effect_; 505 desc->SwapEffect = swap_effect_;
473 desc->AlphaMode = DXGI_ALPHA_MODE_IGNORE; 506 desc->AlphaMode = DXGI_ALPHA_MODE_IGNORE;
@@ -642,6 +675,11 @@ private:
642 DXGI_USAGE buffer_usage_ = DXGI_USAGE_RENDER_TARGET_OUTPUT; 675 DXGI_USAGE buffer_usage_ = DXGI_USAGE_RENDER_TARGET_OUTPUT;
643 HANDLE waitable_ = nullptr; 676 HANDLE waitable_ = nullptr;
644 std::vector<BackBufferResource*> buffers_; 677 std::vector<BackBufferResource*> buffers_;
678 bool lost_ = false; // after a failed ResizeBuffers: no buffers, the description below
679 std::uint32_t lost_width_ = 0;
680 std::uint32_t lost_height_ = 0;
681 UINT lost_count_ = 0;
682 DXGI_FORMAT lost_format_ = DXGI_FORMAT_UNKNOWN;
645 std::atomic<UINT> present_count_{0}; 683 std::atomic<UINT> present_count_{0};
646 std::atomic<bool> statistics_disjoint_{true}; 684 std::atomic<bool> statistics_disjoint_{true};
647 std::atomic<LONGLONG> last_present_qpc_{0}; 685 std::atomic<LONGLONG> last_present_qpc_{0};