diff options
| author | totonii <totonicontato@gmail.com> | 2026-11-22 06:37:36 -0300 |
|---|---|---|
| committer | totonii <totonicontato@gmail.com> | 2026-11-22 06:59:07 -0300 |
| commit | 571cad4d8a2e1a6bc6b48c7900019f1ff31998e4 (patch) | |
| tree | 08906a3a120e3d9d0b260ae1b692a4e5f1270423 /runtime/shims/dxgi_com.cpp | |
| parent | c0ac5fd6518b3ba9fa66765b8b07099990dadd74 (diff) | |
fix(dxgi): share video-memory reservation state across adapter handles per LUID
Diffstat (limited to 'runtime/shims/dxgi_com.cpp')
| -rw-r--r-- | runtime/shims/dxgi_com.cpp | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/runtime/shims/dxgi_com.cpp b/runtime/shims/dxgi_com.cpp index 08cd5a3fd..c4afcb5f6 100644 --- a/runtime/shims/dxgi_com.cpp +++ b/runtime/shims/dxgi_com.cpp | |||
| @@ -4,9 +4,12 @@ | |||
| 4 | #include <atomic> | 4 | #include <atomic> |
| 5 | #include <cstdio> | 5 | #include <cstdio> |
| 6 | #include <cstring> | 6 | #include <cstring> |
| 7 | #include <map> | ||
| 8 | #include <memory> | ||
| 7 | #include <mutex> | 9 | #include <mutex> |
| 8 | #include <set> | 10 | #include <set> |
| 9 | #include <string> | 11 | #include <string> |
| 12 | #include <tuple> | ||
| 10 | #include <vector> | 13 | #include <vector> |
| 11 | 14 | ||
| 12 | namespace rt::dxgi { | 15 | namespace rt::dxgi { |
| @@ -346,6 +349,26 @@ private: | |||
| 346 | std::atomic<UINT> max_frame_latency_{2}; | 349 | std::atomic<UINT> max_frame_latency_{2}; |
| 347 | }; | 350 | }; |
| 348 | 351 | ||
| 352 | struct ReservationState { | ||
| 353 | std::atomic<UINT64> local{0}; | ||
| 354 | std::atomic<UINT64> non_local{0}; | ||
| 355 | }; | ||
| 356 | |||
| 357 | // Video-memory reservations are scoped per physical adapter in the process (matching native DXGI, | ||
| 358 | // where separate IDXGIAdapter3 handles opened for the same LUID observe each other's reservations). | ||
| 359 | std::shared_ptr<ReservationState> adapter_reservation_state(LUID luid, VkPhysicalDevice physical) { | ||
| 360 | static std::mutex mutex; | ||
| 361 | static std::map<std::tuple<std::uint32_t, std::int32_t, std::uintptr_t>, std::shared_ptr<ReservationState>> table; | ||
| 362 | const bool has_luid = (luid.LowPart != 0 || luid.HighPart != 0); | ||
| 363 | const auto key = | ||
| 364 | has_luid ? std::make_tuple(std::uint32_t(luid.LowPart), std::int32_t(luid.HighPart), std::uintptr_t(0)) | ||
| 365 | : std::make_tuple(std::uint32_t(0), std::int32_t(0), reinterpret_cast<std::uintptr_t>(physical)); | ||
| 366 | std::lock_guard lock(mutex); | ||
| 367 | auto& entry = table[key]; | ||
| 368 | if (!entry) entry = std::make_shared<ReservationState>(); | ||
| 369 | return entry; | ||
| 370 | } | ||
| 371 | |||
| 349 | // Vulkan-backed IDXGIAdapter3 reporting the physical device's name, IDs, LUID (matching | 372 | // Vulkan-backed IDXGIAdapter3 reporting the physical device's name, IDs, LUID (matching |
| 350 | // ID3D12Device::GetAdapterLuid), memory heap sizes, and video memory budget info. | 373 | // ID3D12Device::GetAdapterLuid), memory heap sizes, and video memory budget info. |
| 351 | class Adapter final : public IDXGIAdapter3 { | 374 | class Adapter final : public IDXGIAdapter3 { |
| @@ -354,6 +377,7 @@ public: | |||
| 354 | : parent_(parent), gipa_(gipa), instance_(instance), physical_(physical) { | 377 | : parent_(parent), gipa_(gipa), instance_(instance), physical_(physical) { |
| 355 | if (parent_) parent_->AddRef(); | 378 | if (parent_) parent_->AddRef(); |
| 356 | populate_desc(); | 379 | populate_desc(); |
| 380 | reservations_ = adapter_reservation_state(desc_.AdapterLuid, physical_); | ||
| 357 | } | 381 | } |
| 358 | 382 | ||
| 359 | ~Adapter() { | 383 | ~Adapter() { |
| @@ -475,19 +499,19 @@ public: | |||
| 475 | info->Budget = want_local ? desc_.DedicatedVideoMemory : desc_.SharedSystemMemory; | 499 | info->Budget = want_local ? desc_.DedicatedVideoMemory : desc_.SharedSystemMemory; |
| 476 | } | 500 | } |
| 477 | info->AvailableForReservation = info->Budget / 2; | 501 | info->AvailableForReservation = info->Budget / 2; |
| 478 | info->CurrentReservation = want_local ? local_reservation_.load(std::memory_order_relaxed) | 502 | info->CurrentReservation = want_local ? reservations_->local.load(std::memory_order_relaxed) |
| 479 | : non_local_reservation_.load(std::memory_order_relaxed); | 503 | : reservations_->non_local.load(std::memory_order_relaxed); |
| 480 | return S_OK; | 504 | return S_OK; |
| 481 | } | 505 | } |
| 482 | HRESULT STDMETHODCALLTYPE SetVideoMemoryReservation(UINT node_index, DXGI_MEMORY_SEGMENT_GROUP group, | 506 | HRESULT STDMETHODCALLTYPE SetVideoMemoryReservation(UINT node_index, DXGI_MEMORY_SEGMENT_GROUP group, |
| 483 | UINT64 reservation) override { | 507 | UINT64 reservation) override { |
| 484 | if (node_index != 0) return E_INVALIDARG; | 508 | if (node_index != 0) return E_INVALIDARG; |
| 485 | if (group == DXGI_MEMORY_SEGMENT_GROUP_LOCAL) { | 509 | if (group == DXGI_MEMORY_SEGMENT_GROUP_LOCAL) { |
| 486 | local_reservation_.store(reservation, std::memory_order_relaxed); | 510 | reservations_->local.store(reservation, std::memory_order_relaxed); |
| 487 | return S_OK; | 511 | return S_OK; |
| 488 | } | 512 | } |
| 489 | if (group == DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL) { | 513 | if (group == DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL) { |
| 490 | non_local_reservation_.store(reservation, std::memory_order_relaxed); | 514 | reservations_->non_local.store(reservation, std::memory_order_relaxed); |
| 491 | return S_OK; | 515 | return S_OK; |
| 492 | } | 516 | } |
| 493 | return E_INVALIDARG; | 517 | return E_INVALIDARG; |
| @@ -571,8 +595,7 @@ private: | |||
| 571 | VkPhysicalDevice physical_ = VK_NULL_HANDLE; | 595 | VkPhysicalDevice physical_ = VK_NULL_HANDLE; |
| 572 | DXGI_ADAPTER_DESC2 desc_{}; | 596 | DXGI_ADAPTER_DESC2 desc_{}; |
| 573 | bool has_memory_budget_ = false; | 597 | bool has_memory_budget_ = false; |
| 574 | std::atomic<UINT64> local_reservation_{0}; | 598 | std::shared_ptr<ReservationState> reservations_; |
| 575 | std::atomic<UINT64> non_local_reservation_{0}; | ||
| 576 | }; | 599 | }; |
| 577 | 600 | ||
| 578 | class Factory final : public IDXGIFactory4 { | 601 | class Factory final : public IDXGIFactory4 { |