diff options
| author | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 15:51:40 +0100 |
|---|---|---|
| committer | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 15:51:40 +0100 |
| commit | df91c6624b69ee4e6c6eeca20ed68d331ed0e728 (patch) | |
| tree | 323c88b9df89bf8abaa2b98d10808474ab675dc2 /runtime/shims/dxgi_com.cpp | |
| parent | b40d8039a98f57b26bd05860febbabbe59b97873 (diff) | |
| parent | 6a6704df1592e1dc4a3215ddf1423a78adb52045 (diff) | |
Merge main into test/valu-int-misc (#1492)
Conflicted files: runtime/test/shaders/check.py, scripts/run_gpu_tests.py
AI-assisted.
Diffstat (limited to 'runtime/shims/dxgi_com.cpp')
| -rw-r--r-- | runtime/shims/dxgi_com.cpp | 63 |
1 files changed, 53 insertions, 10 deletions
diff --git a/runtime/shims/dxgi_com.cpp b/runtime/shims/dxgi_com.cpp index af3b96679..c4a7530c4 100644 --- a/runtime/shims/dxgi_com.cpp +++ b/runtime/shims/dxgi_com.cpp | |||
| @@ -22,6 +22,10 @@ void stub(const char* method) { | |||
| 22 | if (seen.insert(method).second) std::fprintf(stderr, "[rt] dxgi: unimplemented %s\n", method); | 22 | if (seen.insert(method).second) std::fprintf(stderr, "[rt] dxgi: unimplemented %s\n", method); |
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | bool valid_flip_buffer_count(UINT count) { | ||
| 26 | return count >= 2 && count <= DXGI_MAX_SWAP_CHAIN_BUFFERS; | ||
| 27 | } | ||
| 28 | |||
| 25 | VkFormat dxgi_to_vk_format(DXGI_FORMAT fmt) { | 29 | VkFormat dxgi_to_vk_format(DXGI_FORMAT fmt) { |
| 26 | switch (fmt) { | 30 | switch (fmt) { |
| 27 | case DXGI_FORMAT_B8G8R8A8_UNORM: return VK_FORMAT_B8G8R8A8_UNORM; | 31 | case DXGI_FORMAT_B8G8R8A8_UNORM: return VK_FORMAT_B8G8R8A8_UNORM; |
| @@ -128,14 +132,13 @@ public: | |||
| 128 | const VkExtent2D size = size_or_client(desc.Width, desc.Height, {256u, 256u}); | 132 | const VkExtent2D size = size_or_client(desc.Width, desc.Height, {256u, 256u}); |
| 129 | const std::uint32_t w = size.width; | 133 | const std::uint32_t w = size.width; |
| 130 | const std::uint32_t h = size.height; | 134 | const std::uint32_t h = size.height; |
| 131 | const std::uint32_t count = desc.BufferCount ? desc.BufferCount : 2u; | ||
| 132 | VkFormat vk_fmt = VK_FORMAT_UNDEFINED; | 135 | VkFormat vk_fmt = VK_FORMAT_UNDEFINED; |
| 133 | if (desc.Format != DXGI_FORMAT_UNKNOWN) { | 136 | if (desc.Format != DXGI_FORMAT_UNKNOWN) { |
| 134 | vk_fmt = dxgi_to_vk_format(desc.Format); | 137 | vk_fmt = dxgi_to_vk_format(desc.Format); |
| 135 | if (vk_fmt == VK_FORMAT_UNDEFINED) return false; | 138 | if (vk_fmt == VK_FORMAT_UNDEFINED) return false; |
| 136 | } | 139 | } |
| 137 | const bool allow_tearing = (flags_ & DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING) != 0; | 140 | const bool allow_tearing = (flags_ & DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING) != 0; |
| 138 | if (!swapchain_.create(hwnd, w, h, vk_fmt, count, allow_tearing)) return false; | 141 | if (!swapchain_.create(hwnd, w, h, vk_fmt, desc.BufferCount, allow_tearing)) return false; |
| 139 | if (flags_ & DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT) { | 142 | if (flags_ & DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT) { |
| 140 | // DXGI: waitable swapchains default to maximum frame latency 1. | 143 | // DXGI: waitable swapchains default to maximum frame latency 1. |
| 141 | max_frame_latency_.store(1, std::memory_order_relaxed); | 144 | max_frame_latency_.store(1, std::memory_order_relaxed); |
| @@ -234,11 +237,15 @@ public: | |||
| 234 | } | 237 | } |
| 235 | 238 | ||
| 236 | HRESULT STDMETHODCALLTYPE ResizeBuffers(UINT count, UINT width, UINT height, DXGI_FORMAT fmt, UINT flags) override { | 239 | HRESULT STDMETHODCALLTYPE ResizeBuffers(UINT count, UINT width, UINT height, DXGI_FORMAT fmt, UINT flags) override { |
| 240 | if (count != 0 && !valid_flip_buffer_count(count)) return E_INVALIDARG; | ||
| 237 | VkFormat vk_fmt = VK_FORMAT_UNDEFINED; | 241 | VkFormat vk_fmt = VK_FORMAT_UNDEFINED; |
| 238 | if (fmt != DXGI_FORMAT_UNKNOWN) { | 242 | if (fmt != DXGI_FORMAT_UNKNOWN) { |
| 239 | vk_fmt = dxgi_to_vk_format(fmt); | 243 | vk_fmt = dxgi_to_vk_format(fmt); |
| 240 | if (vk_fmt == VK_FORMAT_UNDEFINED) return DXGI_ERROR_UNSUPPORTED; | 244 | if (vk_fmt == VK_FORMAT_UNDEFINED) return DXGI_ERROR_UNSUPPORTED; |
| 241 | } | 245 | } |
| 246 | constexpr UINT creation_only_flags = DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT | | ||
| 247 | DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING; | ||
| 248 | if ((flags & creation_only_flags) != (flags_ & creation_only_flags)) return E_INVALIDARG; | ||
| 242 | // DXGI refuses to resize while the app still holds a back buffer reference. | 249 | // DXGI refuses to resize while the app still holds a back buffer reference. |
| 243 | for (auto* bb : buffers_) | 250 | for (auto* bb : buffers_) |
| 244 | if (bb->held_by_app()) return DXGI_ERROR_INVALID_CALL; | 251 | if (bb->held_by_app()) return DXGI_ERROR_INVALID_CALL; |
| @@ -246,10 +253,10 @@ public: | |||
| 246 | const VkExtent2D size = size_or_client(width, height, swapchain_.extent()); | 253 | const VkExtent2D size = size_or_client(width, height, swapchain_.extent()); |
| 247 | const std::uint32_t w = size.width; | 254 | const std::uint32_t w = size.width; |
| 248 | const std::uint32_t h = size.height; | 255 | const std::uint32_t h = size.height; |
| 249 | const UINT waitable_bit = flags_ & DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT; | 256 | const UINT next_flags = flags ? flags : flags_; |
| 250 | const UINT next_flags = (flags ? flags : flags_) | waitable_bit; | ||
| 251 | const bool allow_tearing = (next_flags & DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING) != 0; | 257 | const bool allow_tearing = (next_flags & DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING) != 0; |
| 252 | if (!swapchain_.resize(w, h, vk_fmt, count, allow_tearing)) return E_FAIL; | 258 | if (!swapchain_.resize(w, h, vk_fmt, count, allow_tearing)) return E_FAIL; |
| 259 | statistics_disjoint_.store(true, std::memory_order_relaxed); | ||
| 253 | flags_ = next_flags; | 260 | flags_ = next_flags; |
| 254 | register_buffers(); | 261 | register_buffers(); |
| 255 | std::uint32_t next = 0; | 262 | std::uint32_t next = 0; |
| @@ -259,8 +266,11 @@ public: | |||
| 259 | HRESULT STDMETHODCALLTYPE ResizeTarget(const DXGI_MODE_DESC*) override { return S_OK; } | 266 | HRESULT STDMETHODCALLTYPE ResizeTarget(const DXGI_MODE_DESC*) override { return S_OK; } |
| 260 | HRESULT STDMETHODCALLTYPE GetContainingOutput(IDXGIOutput**) override { return DXGI_ERROR_NOT_CURRENTLY_AVAILABLE; } | 267 | HRESULT STDMETHODCALLTYPE GetContainingOutput(IDXGIOutput**) override { return DXGI_ERROR_NOT_CURRENTLY_AVAILABLE; } |
| 261 | HRESULT STDMETHODCALLTYPE GetFrameStatistics(DXGI_FRAME_STATISTICS* stats) override { | 268 | HRESULT STDMETHODCALLTYPE GetFrameStatistics(DXGI_FRAME_STATISTICS* stats) override { |
| 262 | if (!stats) return E_INVALIDARG; | 269 | if (!stats) return DXGI_ERROR_INVALID_CALL; |
| 263 | std::memset(stats, 0, sizeof(*stats)); | 270 | std::memset(stats, 0, sizeof(*stats)); |
| 271 | // Native DXGI reports the discontinuity once, on the first valid query after creation or resize. | ||
| 272 | if (statistics_disjoint_.exchange(false, std::memory_order_relaxed)) | ||
| 273 | return DXGI_ERROR_FRAME_STATISTICS_DISJOINT; | ||
| 264 | const UINT count = present_count_.load(std::memory_order_relaxed); | 274 | const UINT count = present_count_.load(std::memory_order_relaxed); |
| 265 | stats->PresentCount = count; | 275 | stats->PresentCount = count; |
| 266 | stats->PresentRefreshCount = count; | 276 | stats->PresentRefreshCount = count; |
| @@ -425,6 +435,7 @@ private: | |||
| 425 | HANDLE waitable_ = nullptr; | 435 | HANDLE waitable_ = nullptr; |
| 426 | std::vector<BackBufferResource*> buffers_; | 436 | std::vector<BackBufferResource*> buffers_; |
| 427 | std::atomic<UINT> present_count_{0}; | 437 | std::atomic<UINT> present_count_{0}; |
| 438 | std::atomic<bool> statistics_disjoint_{true}; | ||
| 428 | std::atomic<LONGLONG> last_present_qpc_{0}; | 439 | std::atomic<LONGLONG> last_present_qpc_{0}; |
| 429 | std::atomic<UINT> max_frame_latency_{2}; | 440 | std::atomic<UINT> max_frame_latency_{2}; |
| 430 | DXGI_RGBA background_color_{0, 0, 0, 0}; | 441 | DXGI_RGBA background_color_{0, 0, 0, 0}; |
| @@ -777,6 +788,7 @@ public: | |||
| 777 | if (!out) return E_POINTER; | 788 | if (!out) return E_POINTER; |
| 778 | *out = nullptr; | 789 | *out = nullptr; |
| 779 | if (!hwnd || !desc) return E_INVALIDARG; | 790 | if (!hwnd || !desc) return E_INVALIDARG; |
| 791 | if (!valid_flip_buffer_count(desc->BufferCount)) return DXGI_ERROR_INVALID_CALL; | ||
| 780 | auto* sc = new SwapChain(this, device, context_, gipa_, instance_, physical_, gdpa_, queue_, queue_family_); | 792 | auto* sc = new SwapChain(this, device, context_, gipa_, instance_, physical_, gdpa_, queue_, queue_family_); |
| 781 | if (!sc->init(hwnd, *desc)) { | 793 | if (!sc->init(hwnd, *desc)) { |
| 782 | sc->Release(); | 794 | sc->Release(); |
| @@ -785,16 +797,46 @@ public: | |||
| 785 | *out = static_cast<IDXGISwapChain1*>(sc); | 797 | *out = static_cast<IDXGISwapChain1*>(sc); |
| 786 | return S_OK; | 798 | return S_OK; |
| 787 | } | 799 | } |
| 788 | HRESULT STDMETHODCALLTYPE CreateSwapChainForCoreWindow(IUnknown*, IUnknown*, const DXGI_SWAP_CHAIN_DESC1*, | 800 | HRESULT STDMETHODCALLTYPE CreateSwapChainForCoreWindow(IUnknown* device, IUnknown* window, |
| 789 | IDXGIOutput*, IDXGISwapChain1**) override { | 801 | const DXGI_SWAP_CHAIN_DESC1* desc, |
| 790 | return E_NOTIMPL; | 802 | IDXGIOutput* restrict_to_output, |
| 803 | IDXGISwapChain1** out) override { | ||
| 804 | if (!out) return E_POINTER; | ||
| 805 | *out = nullptr; | ||
| 806 | if (!device || !window || !desc) return E_INVALIDARG; | ||
| 807 | // UWP / Game Core Scarlett titles present to an ICoreWindow. Extract the underlying | ||
| 808 | // Win32 HWND via ICoreWindowInterop. | ||
| 809 | static constexpr GUID kIID_ICoreWindowInterop = { | ||
| 810 | 0x45D64A29, 0xA63E, 0x4CB6, {0xB4, 0x98, 0x57, 0x81, 0xD2, 0x98, 0xCB, 0x4F}}; | ||
| 811 | struct ICoreWindowInterop : public IUnknown { | ||
| 812 | virtual HRESULT STDMETHODCALLTYPE get_WindowHandle(HWND* hwnd) = 0; | ||
| 813 | virtual HRESULT STDMETHODCALLTYPE put_MessageHandled(unsigned char value) = 0; | ||
| 814 | }; | ||
| 815 | ICoreWindowInterop* interop = nullptr; | ||
| 816 | if (FAILED(window->QueryInterface(kIID_ICoreWindowInterop, reinterpret_cast<void**>(&interop))) || !interop) { | ||
| 817 | return DXGI_ERROR_INVALID_CALL; | ||
| 818 | } | ||
| 819 | HWND hwnd = nullptr; | ||
| 820 | const HRESULT hr = interop->get_WindowHandle(&hwnd); | ||
| 821 | interop->Release(); | ||
| 822 | if (FAILED(hr) || !hwnd) return DXGI_ERROR_INVALID_CALL; | ||
| 823 | return CreateSwapChainForHwnd(device, hwnd, desc, nullptr, restrict_to_output, out); | ||
| 791 | } | 824 | } |
| 792 | HRESULT STDMETHODCALLTYPE GetSharedResourceAdapterLuid(HANDLE, LUID*) override { return E_NOTIMPL; } | 825 | HRESULT STDMETHODCALLTYPE GetSharedResourceAdapterLuid(HANDLE, LUID*) override { return E_NOTIMPL; } |
| 793 | HRESULT STDMETHODCALLTYPE RegisterStereoStatusWindow(HWND, UINT, DWORD*) override { return E_NOTIMPL; } | 826 | HRESULT STDMETHODCALLTYPE RegisterStereoStatusWindow(HWND, UINT, DWORD*) override { return E_NOTIMPL; } |
| 794 | HRESULT STDMETHODCALLTYPE RegisterStereoStatusEvent(HANDLE, DWORD*) override { return E_NOTIMPL; } | 827 | HRESULT STDMETHODCALLTYPE RegisterStereoStatusEvent(HANDLE, DWORD*) override { return E_NOTIMPL; } |
| 795 | void STDMETHODCALLTYPE UnregisterStereoStatus(DWORD) override {} | 828 | void STDMETHODCALLTYPE UnregisterStereoStatus(DWORD) override {} |
| 796 | HRESULT STDMETHODCALLTYPE RegisterOcclusionStatusWindow(HWND, UINT, DWORD*) override { return E_NOTIMPL; } | 829 | HRESULT STDMETHODCALLTYPE RegisterOcclusionStatusWindow(HWND hwnd, UINT msg, DWORD* cookie) override { |
| 797 | HRESULT STDMETHODCALLTYPE RegisterOcclusionStatusEvent(HANDLE, DWORD*) override { return E_NOTIMPL; } | 830 | (void)msg; |
| 831 | if (!hwnd || !cookie) return DXGI_ERROR_INVALID_CALL; | ||
| 832 | *cookie = next_occlusion_cookie_.fetch_add(1, std::memory_order_relaxed); | ||
| 833 | return S_OK; | ||
| 834 | } | ||
| 835 | HRESULT STDMETHODCALLTYPE RegisterOcclusionStatusEvent(HANDLE event, DWORD* cookie) override { | ||
| 836 | if (!event || !cookie) return DXGI_ERROR_INVALID_CALL; | ||
| 837 | *cookie = next_occlusion_cookie_.fetch_add(1, std::memory_order_relaxed); | ||
| 838 | return S_OK; | ||
| 839 | } | ||
| 798 | void STDMETHODCALLTYPE UnregisterOcclusionStatus(DWORD) override {} | 840 | void STDMETHODCALLTYPE UnregisterOcclusionStatus(DWORD) override {} |
| 799 | HRESULT STDMETHODCALLTYPE CreateSwapChainForComposition(IUnknown*, const DXGI_SWAP_CHAIN_DESC1*, IDXGIOutput*, | 841 | HRESULT STDMETHODCALLTYPE CreateSwapChainForComposition(IUnknown*, const DXGI_SWAP_CHAIN_DESC1*, IDXGIOutput*, |
| 800 | IDXGISwapChain1**) override { | 842 | IDXGISwapChain1**) override { |
| @@ -880,6 +922,7 @@ private: | |||
| 880 | HWND assoc_hwnd_ = nullptr; | 922 | HWND assoc_hwnd_ = nullptr; |
| 881 | mutable std::once_flag tearing_once_; | 923 | mutable std::once_flag tearing_once_; |
| 882 | bool tearing_supported_ = false; | 924 | bool tearing_supported_ = false; |
| 925 | std::atomic<DWORD> next_occlusion_cookie_{1}; | ||
| 883 | }; | 926 | }; |
| 884 | 927 | ||
| 885 | } // namespace | 928 | } // namespace |