diff options
| author | Nolan Cotter <186326284+NolanCotter@users.noreply.github.com> | 2026-11-22 20:39:59 -0700 |
|---|---|---|
| committer | Nolan Cotter <186326284+NolanCotter@users.noreply.github.com> | 2026-11-22 20:39:59 -0700 |
| commit | fa5f26db8eafd2cc27f627b68c0a5a5949bcf2ff (patch) | |
| tree | fc57e238be829295fc216dff89a6d93e64c16b50 /runtime/shims/dxgi_com.cpp | |
| parent | 9b693ecd8a2f90799f6c47f8f06cf062feb9820c (diff) | |
| parent | 7b7d73f890f9fbbf5967bbb694390bbc99e33ba6 (diff) | |
Merge origin/main: DXGI device fields coexist with the console-ABI device state
Diffstat (limited to 'runtime/shims/dxgi_com.cpp')
| -rw-r--r-- | runtime/shims/dxgi_com.cpp | 169 |
1 files changed, 159 insertions, 10 deletions
diff --git a/runtime/shims/dxgi_com.cpp b/runtime/shims/dxgi_com.cpp index 734824e07..54aa57991 100644 --- a/runtime/shims/dxgi_com.cpp +++ b/runtime/shims/dxgi_com.cpp | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | 3 | ||
| 4 | #include <algorithm> | 4 | #include <algorithm> |
| 5 | #include <atomic> | 5 | #include <atomic> |
| 6 | #include <cstdint> | ||
| 6 | #include <cstdio> | 7 | #include <cstdio> |
| 7 | #include <cstring> | 8 | #include <cstring> |
| 8 | #include <map> | 9 | #include <map> |
| @@ -26,6 +27,90 @@ void stub(const char* method) { | |||
| 26 | if (seen.insert(method).second) std::fprintf(stderr, "[rt] dxgi: unimplemented %s\n", method); | 27 | if (seen.insert(method).second) std::fprintf(stderr, "[rt] dxgi: unimplemented %s\n", method); |
| 27 | } | 28 | } |
| 28 | 29 | ||
| 30 | // IDXGIObject private data, shared by the factory, adapter and swapchain. SetPrivateData keeps a copy of the bytes; | ||
| 31 | // SetPrivateDataInterface holds one reference until the entry is replaced, removed or the owner dies (Microsoft: | ||
| 32 | // IDXGIObject::SetPrivateDataInterface). Zero bytes remove the entry. GetPrivateData gives an interface back with a | ||
| 33 | // reference added, and a buffer too small for the entry gets DXGI_ERROR_MORE_DATA and the size to retry with. | ||
| 34 | class PrivateDataStore { | ||
| 35 | public: | ||
| 36 | PrivateDataStore() = default; | ||
| 37 | PrivateDataStore(const PrivateDataStore&) = delete; | ||
| 38 | PrivateDataStore& operator=(const PrivateDataStore&) = delete; | ||
| 39 | ~PrivateDataStore() { | ||
| 40 | for (auto& entry : data_) | ||
| 41 | if (entry.object) entry.object->Release(); | ||
| 42 | } | ||
| 43 | |||
| 44 | HRESULT get(REFGUID guid, UINT* size, void* data) { | ||
| 45 | if (!size) return E_INVALIDARG; | ||
| 46 | std::lock_guard lock(mutex_); | ||
| 47 | for (const auto& entry : data_) { | ||
| 48 | if (entry.key != guid) continue; | ||
| 49 | const UINT bytes = entry.object ? UINT(sizeof(IUnknown*)) : UINT(entry.bytes.size()); | ||
| 50 | if (data && *size < bytes) { | ||
| 51 | *size = bytes; | ||
| 52 | return DXGI_ERROR_MORE_DATA; | ||
| 53 | } | ||
| 54 | if (data) { | ||
| 55 | if (entry.object) { | ||
| 56 | entry.object->AddRef(); | ||
| 57 | std::memcpy(data, &entry.object, sizeof(IUnknown*)); | ||
| 58 | } else { | ||
| 59 | std::memcpy(data, entry.bytes.data(), bytes); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | *size = bytes; | ||
| 63 | return S_OK; | ||
| 64 | } | ||
| 65 | *size = 0; | ||
| 66 | return DXGI_ERROR_NOT_FOUND; | ||
| 67 | } | ||
| 68 | |||
| 69 | HRESULT set(REFGUID guid, UINT size, const void* data) { | ||
| 70 | if (size && !data) return E_INVALIDARG; | ||
| 71 | Entry entry{guid, {}, nullptr}; | ||
| 72 | if (size) { | ||
| 73 | const auto* bytes = static_cast<const std::uint8_t*>(data); | ||
| 74 | entry.bytes.assign(bytes, bytes + size); | ||
| 75 | } | ||
| 76 | store(std::move(entry), size != 0); | ||
| 77 | return S_OK; | ||
| 78 | } | ||
| 79 | |||
| 80 | HRESULT set_interface(REFGUID guid, const IUnknown* object) { | ||
| 81 | IUnknown* held = const_cast<IUnknown*>(object); | ||
| 82 | if (held) held->AddRef(); | ||
| 83 | store(Entry{guid, {}, held}, held != nullptr); | ||
| 84 | return S_OK; | ||
| 85 | } | ||
| 86 | |||
| 87 | private: | ||
| 88 | struct Entry { | ||
| 89 | GUID key; | ||
| 90 | std::vector<std::uint8_t> bytes; | ||
| 91 | IUnknown* object; | ||
| 92 | }; | ||
| 93 | |||
| 94 | // The replaced interface is released after the lock is dropped, since its Release may run arbitrary code. | ||
| 95 | void store(Entry entry, bool keep) { | ||
| 96 | IUnknown* released = nullptr; | ||
| 97 | { | ||
| 98 | std::lock_guard lock(mutex_); | ||
| 99 | for (auto it = data_.begin(); it != data_.end(); ++it) { | ||
| 100 | if (it->key != entry.key) continue; | ||
| 101 | released = it->object; | ||
| 102 | data_.erase(it); | ||
| 103 | break; | ||
| 104 | } | ||
| 105 | if (keep) data_.push_back(std::move(entry)); | ||
| 106 | } | ||
| 107 | if (released) released->Release(); | ||
| 108 | } | ||
| 109 | |||
| 110 | std::mutex mutex_; | ||
| 111 | std::vector<Entry> data_; | ||
| 112 | }; | ||
| 113 | |||
| 29 | bool valid_flip_buffer_count(UINT count) { | 114 | bool valid_flip_buffer_count(UINT count) { |
| 30 | return count >= 2 && count <= DXGI_MAX_SWAP_CHAIN_BUFFERS; | 115 | return count >= 2 && count <= DXGI_MAX_SWAP_CHAIN_BUFFERS; |
| 31 | } | 116 | } |
| @@ -177,9 +262,15 @@ public: | |||
| 177 | } | 262 | } |
| 178 | 263 | ||
| 179 | // IDXGIObject | 264 | // IDXGIObject |
| 180 | HRESULT STDMETHODCALLTYPE SetPrivateData(REFGUID, UINT, const void*) override { return S_OK; } | 265 | HRESULT STDMETHODCALLTYPE SetPrivateData(REFGUID guid, UINT size, const void* data) override { |
| 181 | HRESULT STDMETHODCALLTYPE SetPrivateDataInterface(REFGUID, const IUnknown*) override { return S_OK; } | 266 | return private_data_.set(guid, size, data); |
| 182 | HRESULT STDMETHODCALLTYPE GetPrivateData(REFGUID, UINT*, void*) override { return DXGI_ERROR_NOT_FOUND; } | 267 | } |
| 268 | HRESULT STDMETHODCALLTYPE SetPrivateDataInterface(REFGUID guid, const IUnknown* object) override { | ||
| 269 | return private_data_.set_interface(guid, object); | ||
| 270 | } | ||
| 271 | HRESULT STDMETHODCALLTYPE GetPrivateData(REFGUID guid, UINT* size, void* data) override { | ||
| 272 | return private_data_.get(guid, size, data); | ||
| 273 | } | ||
| 183 | HRESULT STDMETHODCALLTYPE GetParent(REFIID riid, void** out) override { | 274 | HRESULT STDMETHODCALLTYPE GetParent(REFIID riid, void** out) override { |
| 184 | if (!out) return E_POINTER; | 275 | if (!out) return E_POINTER; |
| 185 | *out = nullptr; | 276 | *out = nullptr; |
| @@ -481,6 +572,7 @@ private: | |||
| 481 | } | 572 | } |
| 482 | 573 | ||
| 483 | std::atomic<ULONG> refs_{1}; | 574 | std::atomic<ULONG> refs_{1}; |
| 575 | PrivateDataStore private_data_; | ||
| 484 | IDXGIFactory4* parent_ = nullptr; | 576 | IDXGIFactory4* parent_ = nullptr; |
| 485 | IUnknown* device_ = nullptr; | 577 | IUnknown* device_ = nullptr; |
| 486 | leonida::VulkanContext& context_; | 578 | leonida::VulkanContext& context_; |
| @@ -573,9 +665,15 @@ public: | |||
| 573 | } | 665 | } |
| 574 | 666 | ||
| 575 | // IDXGIObject | 667 | // IDXGIObject |
| 576 | HRESULT STDMETHODCALLTYPE SetPrivateData(REFGUID, UINT, const void*) override { return S_OK; } | 668 | HRESULT STDMETHODCALLTYPE SetPrivateData(REFGUID guid, UINT size, const void* data) override { |
| 577 | HRESULT STDMETHODCALLTYPE SetPrivateDataInterface(REFGUID, const IUnknown*) override { return S_OK; } | 669 | return private_data_.set(guid, size, data); |
| 578 | HRESULT STDMETHODCALLTYPE GetPrivateData(REFGUID, UINT*, void*) override { return DXGI_ERROR_NOT_FOUND; } | 670 | } |
| 671 | HRESULT STDMETHODCALLTYPE SetPrivateDataInterface(REFGUID guid, const IUnknown* object) override { | ||
| 672 | return private_data_.set_interface(guid, object); | ||
| 673 | } | ||
| 674 | HRESULT STDMETHODCALLTYPE GetPrivateData(REFGUID guid, UINT* size, void* data) override { | ||
| 675 | return private_data_.get(guid, size, data); | ||
| 676 | } | ||
| 579 | HRESULT STDMETHODCALLTYPE GetParent(REFIID riid, void** out) override { | 677 | HRESULT STDMETHODCALLTYPE GetParent(REFIID riid, void** out) override { |
| 580 | if (!out) return E_POINTER; | 678 | if (!out) return E_POINTER; |
| 581 | *out = nullptr; | 679 | *out = nullptr; |
| @@ -767,6 +865,7 @@ private: | |||
| 767 | 865 | ||
| 768 | std::atomic<DWORD> next_budget_cookie_{1}; | 866 | std::atomic<DWORD> next_budget_cookie_{1}; |
| 769 | std::atomic<ULONG> refs_{1}; | 867 | std::atomic<ULONG> refs_{1}; |
| 868 | PrivateDataStore private_data_; | ||
| 770 | IDXGIFactory4* parent_ = nullptr; | 869 | IDXGIFactory4* parent_ = nullptr; |
| 771 | PFN_vkGetInstanceProcAddr gipa_ = nullptr; | 870 | PFN_vkGetInstanceProcAddr gipa_ = nullptr; |
| 772 | VkInstance instance_ = VK_NULL_HANDLE; | 871 | VkInstance instance_ = VK_NULL_HANDLE; |
| @@ -805,9 +904,15 @@ public: | |||
| 805 | } | 904 | } |
| 806 | 905 | ||
| 807 | // IDXGIObject | 906 | // IDXGIObject |
| 808 | HRESULT STDMETHODCALLTYPE SetPrivateData(REFGUID, UINT, const void*) override { return S_OK; } | 907 | HRESULT STDMETHODCALLTYPE SetPrivateData(REFGUID guid, UINT size, const void* data) override { |
| 809 | HRESULT STDMETHODCALLTYPE SetPrivateDataInterface(REFGUID, const IUnknown*) override { return S_OK; } | 908 | return private_data_.set(guid, size, data); |
| 810 | HRESULT STDMETHODCALLTYPE GetPrivateData(REFGUID, UINT*, void*) override { return DXGI_ERROR_NOT_FOUND; } | 909 | } |
| 910 | HRESULT STDMETHODCALLTYPE SetPrivateDataInterface(REFGUID guid, const IUnknown* object) override { | ||
| 911 | return private_data_.set_interface(guid, object); | ||
| 912 | } | ||
| 913 | HRESULT STDMETHODCALLTYPE GetPrivateData(REFGUID guid, UINT* size, void* data) override { | ||
| 914 | return private_data_.get(guid, size, data); | ||
| 915 | } | ||
| 811 | HRESULT STDMETHODCALLTYPE GetParent(REFIID, void** out) override { | 916 | HRESULT STDMETHODCALLTYPE GetParent(REFIID, void** out) override { |
| 812 | if (!out) return E_POINTER; | 917 | if (!out) return E_POINTER; |
| 813 | *out = nullptr; | 918 | *out = nullptr; |
| @@ -883,6 +988,49 @@ public: | |||
| 883 | HRESULT STDMETHODCALLTYPE CreateSwapChainForHwnd(IUnknown* device, HWND hwnd, const DXGI_SWAP_CHAIN_DESC1* desc, | 988 | HRESULT STDMETHODCALLTYPE CreateSwapChainForHwnd(IUnknown* device, HWND hwnd, const DXGI_SWAP_CHAIN_DESC1* desc, |
| 884 | const DXGI_SWAP_CHAIN_FULLSCREEN_DESC*, IDXGIOutput* restrict_to_output, | 989 | const DXGI_SWAP_CHAIN_FULLSCREEN_DESC*, IDXGIOutput* restrict_to_output, |
| 885 | IDXGISwapChain1** out) override { | 990 | IDXGISwapChain1** out) override { |
| 991 | if (out) *out = nullptr; | ||
| 992 | if (const HRESULT hr = check_hwnd_swap_chain(device, hwnd, desc, out); FAILED(hr)) return hr; | ||
| 993 | return create_swap_chain(device, hwnd, desc, restrict_to_output, out); | ||
| 994 | } | ||
| 995 | |||
| 996 | // What the host runtime's DXGI refuses for a D3D12 swap chain on a window (dxgi_swapchain_test compares these, | ||
| 997 | // RTX 5070 / 621.14): a null queue, window, description or output, an ID3D12Device in place of the queue, the | ||
| 998 | // bitblt effects, a scaling or alpha mode a window can't present, a flag outside the ones a D3D12 window swap chain | ||
| 999 | // takes (MODE_SWITCH, DISPLAY_ONLY, FRAME_LATENCY_WAITABLE_OBJECT, ALLOW_TEARING, the holographic restriction and | ||
| 1000 | // 0x2000), a shared, read-only, discard-on-present or unordered-access buffer usage, MSAA, a buffer count outside | ||
| 1001 | // 2..16 and a format the swap chain can't take are DXGI_ERROR_INVALID_CALL; a side above 16384 is E_INVALIDARG, | ||
| 1002 | // only when nothing else is wrong. | ||
| 1003 | static HRESULT check_hwnd_swap_chain(IUnknown* device, HWND hwnd, const DXGI_SWAP_CHAIN_DESC1* desc, | ||
| 1004 | IDXGISwapChain1** out) { | ||
| 1005 | if (!device || !hwnd || !desc || !out) return DXGI_ERROR_INVALID_CALL; | ||
| 1006 | ID3D12CommandQueue* queue = nullptr; | ||
| 1007 | if (FAILED(device->QueryInterface(IID_PPV_ARGS(&queue))) || !queue) return DXGI_ERROR_INVALID_CALL; | ||
| 1008 | queue->Release(); | ||
| 1009 | if (desc->SwapEffect != DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL && desc->SwapEffect != DXGI_SWAP_EFFECT_FLIP_DISCARD) | ||
| 1010 | return DXGI_ERROR_INVALID_CALL; | ||
| 1011 | if (desc->Scaling != DXGI_SCALING_STRETCH && desc->Scaling != DXGI_SCALING_NONE) return DXGI_ERROR_INVALID_CALL; | ||
| 1012 | if (desc->AlphaMode != DXGI_ALPHA_MODE_UNSPECIFIED && desc->AlphaMode != DXGI_ALPHA_MODE_IGNORE) | ||
| 1013 | return DXGI_ERROR_INVALID_CALL; | ||
| 1014 | constexpr UINT kWindowFlags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH | DXGI_SWAP_CHAIN_FLAG_DISPLAY_ONLY | | ||
| 1015 | DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT | | ||
| 1016 | DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING | 0x1000u | 0x2000u; | ||
| 1017 | if (desc->Flags & ~kWindowFlags) return DXGI_ERROR_INVALID_CALL; | ||
| 1018 | constexpr DXGI_USAGE kRefusedUsage = | ||
| 1019 | DXGI_USAGE_SHARED | DXGI_USAGE_READ_ONLY | DXGI_USAGE_DISCARD_ON_PRESENT | DXGI_USAGE_UNORDERED_ACCESS; | ||
| 1020 | if (desc->BufferUsage & kRefusedUsage) return DXGI_ERROR_INVALID_CALL; | ||
| 1021 | // The host checks these before the size: MSAA 4 or one buffer at width 16385 is still INVALID_CALL. | ||
| 1022 | if (desc->SampleDesc.Count != 1 || desc->SampleDesc.Quality != 0) return DXGI_ERROR_INVALID_CALL; | ||
| 1023 | if (!valid_flip_buffer_count(desc->BufferCount)) return DXGI_ERROR_INVALID_CALL; | ||
| 1024 | if (dxgi_to_vk_format(desc->Format) == VK_FORMAT_UNDEFINED) return DXGI_ERROR_INVALID_CALL; | ||
| 1025 | if (desc->Width > D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION || desc->Height > D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION) | ||
| 1026 | return E_INVALIDARG; | ||
| 1027 | return S_OK; | ||
| 1028 | } | ||
| 1029 | |||
| 1030 | // The swap chain itself, for a window or (through its HWND) a CoreWindow. The CoreWindow path keeps only these | ||
| 1031 | // checks: the host's rules for it (ASPECT_RATIO_STRETCH, for one) can't be compared without a real CoreWindow. | ||
| 1032 | HRESULT create_swap_chain(IUnknown* device, HWND hwnd, const DXGI_SWAP_CHAIN_DESC1* desc, | ||
| 1033 | IDXGIOutput* restrict_to_output, IDXGISwapChain1** out) { | ||
| 886 | if (!out) return E_POINTER; | 1034 | if (!out) return E_POINTER; |
| 887 | *out = nullptr; | 1035 | *out = nullptr; |
| 888 | if (!hwnd || !desc) return E_INVALIDARG; | 1036 | if (!hwnd || !desc) return E_INVALIDARG; |
| @@ -922,7 +1070,7 @@ public: | |||
| 922 | const HRESULT hr = interop->get_WindowHandle(&hwnd); | 1070 | const HRESULT hr = interop->get_WindowHandle(&hwnd); |
| 923 | interop->Release(); | 1071 | interop->Release(); |
| 924 | if (FAILED(hr) || !hwnd) return DXGI_ERROR_INVALID_CALL; | 1072 | if (FAILED(hr) || !hwnd) return DXGI_ERROR_INVALID_CALL; |
| 925 | return CreateSwapChainForHwnd(device, hwnd, desc, nullptr, restrict_to_output, out); | 1073 | return create_swap_chain(device, hwnd, desc, restrict_to_output, out); |
| 926 | } | 1074 | } |
| 927 | HRESULT STDMETHODCALLTYPE GetSharedResourceAdapterLuid(HANDLE, LUID*) override { return E_NOTIMPL; } | 1075 | HRESULT STDMETHODCALLTYPE GetSharedResourceAdapterLuid(HANDLE, LUID*) override { return E_NOTIMPL; } |
| 928 | HRESULT STDMETHODCALLTYPE RegisterStereoStatusWindow(HWND, UINT, DWORD*) override { return E_NOTIMPL; } | 1076 | HRESULT STDMETHODCALLTYPE RegisterStereoStatusWindow(HWND, UINT, DWORD*) override { return E_NOTIMPL; } |
| @@ -1047,6 +1195,7 @@ private: | |||
| 1047 | } | 1195 | } |
| 1048 | 1196 | ||
| 1049 | std::atomic<ULONG> refs_{1}; | 1197 | std::atomic<ULONG> refs_{1}; |
| 1198 | PrivateDataStore private_data_; | ||
| 1050 | leonida::VulkanContext& context_; | 1199 | leonida::VulkanContext& context_; |
| 1051 | PFN_vkGetInstanceProcAddr gipa_; | 1200 | PFN_vkGetInstanceProcAddr gipa_; |
| 1052 | VkInstance instance_; | 1201 | VkInstance instance_; |