diff options
| author | totonii <totonicontato@gmail.com> | 2026-11-22 07:00:57 -0300 |
|---|---|---|
| committer | totonii <totonicontato@gmail.com> | 2026-11-22 07:00:57 -0300 |
| commit | ede8c96f3d863e9a97289f378aaadc8b2ecd60f7 (patch) | |
| tree | ef0bebcf5cfe3533c3963161c0fe014b9a270135 /runtime/shims/dxgi_com.cpp | |
| parent | 571cad4d8a2e1a6bc6b48c7900019f1ff31998e4 (diff) | |
fix(dxgi): use primary DEVICE_LOCAL heap to avoid double-counting BAR1 aperture
Diffstat (limited to 'runtime/shims/dxgi_com.cpp')
| -rw-r--r-- | runtime/shims/dxgi_com.cpp | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/runtime/shims/dxgi_com.cpp b/runtime/shims/dxgi_com.cpp index c4afcb5f6..72b6ef5e8 100644 --- a/runtime/shims/dxgi_com.cpp +++ b/runtime/shims/dxgi_com.cpp | |||
| @@ -369,6 +369,23 @@ std::shared_ptr<ReservationState> adapter_reservation_state(LUID luid, VkPhysica | |||
| 369 | return entry; | 369 | return entry; |
| 370 | } | 370 | } |
| 371 | 371 | ||
| 372 | // On discrete GPUs without Resizable BAR, Vulkan exposes a second smaller DEVICE_LOCAL heap for the | ||
| 373 | // CPU-visible BAR1 aperture (typically 256 MiB) that aliases the same physical VRAM as the primary heap. | ||
| 374 | // Pick the largest DEVICE_LOCAL heap so DedicatedVideoMemory and LOCAL budget/usage match Windows DXGI | ||
| 375 | // instead of double-counting BAR1. | ||
| 376 | std::uint32_t primary_device_local_heap(const VkPhysicalDeviceMemoryProperties& mem) { | ||
| 377 | std::uint32_t best = UINT32_MAX; | ||
| 378 | VkDeviceSize best_size = 0; | ||
| 379 | for (std::uint32_t i = 0; i < mem.memoryHeapCount; ++i) { | ||
| 380 | if ((mem.memoryHeaps[i].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) == 0) continue; | ||
| 381 | if (best == UINT32_MAX || mem.memoryHeaps[i].size > best_size) { | ||
| 382 | best = i; | ||
| 383 | best_size = mem.memoryHeaps[i].size; | ||
| 384 | } | ||
| 385 | } | ||
| 386 | return best; | ||
| 387 | } | ||
| 388 | |||
| 372 | // Vulkan-backed IDXGIAdapter3 reporting the physical device's name, IDs, LUID (matching | 389 | // Vulkan-backed IDXGIAdapter3 reporting the physical device's name, IDs, LUID (matching |
| 373 | // ID3D12Device::GetAdapterLuid), memory heap sizes, and video memory budget info. | 390 | // ID3D12Device::GetAdapterLuid), memory heap sizes, and video memory budget info. |
| 374 | class Adapter final : public IDXGIAdapter3 { | 391 | class Adapter final : public IDXGIAdapter3 { |
| @@ -486,11 +503,18 @@ public: | |||
| 486 | VkPhysicalDeviceMemoryProperties2 mem2{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2, &budget}; | 503 | VkPhysicalDeviceMemoryProperties2 mem2{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2, &budget}; |
| 487 | get_mem2(physical_, &mem2); | 504 | get_mem2(physical_, &mem2); |
| 488 | const auto& mem = mem2.memoryProperties; | 505 | const auto& mem = mem2.memoryProperties; |
| 489 | for (std::uint32_t i = 0; i < mem.memoryHeapCount; ++i) { | 506 | if (want_local) { |
| 490 | const bool is_local = (mem.memoryHeaps[i].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0; | 507 | const std::uint32_t local_heap = primary_device_local_heap(mem); |
| 491 | if (is_local == want_local) { | 508 | if (local_heap != UINT32_MAX) { |
| 492 | info->Budget += budget.heapBudget[i]; | 509 | info->Budget = budget.heapBudget[local_heap]; |
| 493 | info->CurrentUsage += budget.heapUsage[i]; | 510 | info->CurrentUsage = budget.heapUsage[local_heap]; |
| 511 | } | ||
| 512 | } else { | ||
| 513 | for (std::uint32_t i = 0; i < mem.memoryHeapCount; ++i) { | ||
| 514 | if ((mem.memoryHeaps[i].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) == 0) { | ||
| 515 | info->Budget += budget.heapBudget[i]; | ||
| 516 | info->CurrentUsage += budget.heapUsage[i]; | ||
| 517 | } | ||
| 494 | } | 518 | } |
| 495 | } | 519 | } |
| 496 | } | 520 | } |
| @@ -559,12 +583,11 @@ private: | |||
| 559 | gipa_(instance_, "vkGetPhysicalDeviceMemoryProperties"))) { | 583 | gipa_(instance_, "vkGetPhysicalDeviceMemoryProperties"))) { |
| 560 | VkPhysicalDeviceMemoryProperties mem{}; | 584 | VkPhysicalDeviceMemoryProperties mem{}; |
| 561 | get_mem(physical_, &mem); | 585 | get_mem(physical_, &mem); |
| 562 | std::uint64_t dedicated = 0; | 586 | const std::uint32_t local_heap = primary_device_local_heap(mem); |
| 587 | const std::uint64_t dedicated = (local_heap != UINT32_MAX) ? mem.memoryHeaps[local_heap].size : 0; | ||
| 563 | std::uint64_t shared = 0; | 588 | std::uint64_t shared = 0; |
| 564 | for (std::uint32_t i = 0; i < mem.memoryHeapCount; ++i) { | 589 | for (std::uint32_t i = 0; i < mem.memoryHeapCount; ++i) { |
| 565 | if (mem.memoryHeaps[i].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) | 590 | if ((mem.memoryHeaps[i].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) == 0) |
| 566 | dedicated += mem.memoryHeaps[i].size; | ||
| 567 | else | ||
| 568 | shared += mem.memoryHeaps[i].size; | 591 | shared += mem.memoryHeaps[i].size; |
| 569 | } | 592 | } |
| 570 | desc_.DedicatedVideoMemory = static_cast<SIZE_T>(dedicated); | 593 | desc_.DedicatedVideoMemory = static_cast<SIZE_T>(dedicated); |