|
4 | 4 | #include "../util/util_log.h" |
5 | 5 |
|
6 | 6 | namespace dxvk { |
7 | | - std::unordered_map<IUnknown*, NvapiD3dLowLatencyDevice> NvapiD3dLowLatencyDevice::m_lowLatencyDeviceMap = {}; |
| 7 | + std::unordered_map<IUnknown*, std::shared_ptr<NvapiD3dLowLatencyDevice>> NvapiD3dLowLatencyDevice::m_lowLatencyDeviceMap = {}; |
8 | 8 | std::mutex NvapiD3dLowLatencyDevice::m_mutex = {}; |
9 | 9 |
|
10 | 10 | void NvapiD3dLowLatencyDevice::Reset() { |
11 | 11 | std::scoped_lock lock{m_mutex}; |
12 | 12 | m_lowLatencyDeviceMap.clear(); |
13 | 13 | } |
14 | 14 |
|
| 15 | + static Com<ID3DLowLatencyDevice> GetD3DLowLatencyDevice(IUnknown* device) { |
| 16 | + Com<ID3DLowLatencyDevice> d3dLowLatencyDevice; |
| 17 | + |
| 18 | + if (SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&d3dLowLatencyDevice)))) |
| 19 | + return d3dLowLatencyDevice; |
| 20 | + |
| 21 | + if (Com<ID3D11DeviceChild> d3d11DeviceChild; SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&d3d11DeviceChild)))) { |
| 22 | + Com<ID3D11Device> d3d11Device; |
| 23 | + d3d11DeviceChild->GetDevice(&d3d11Device); |
| 24 | + if (SUCCEEDED(d3d11Device->QueryInterface(IID_PPV_ARGS(&d3dLowLatencyDevice)))) |
| 25 | + return d3dLowLatencyDevice; |
| 26 | + } |
| 27 | + |
| 28 | + if (Com<ID3D12DeviceChild> d3d12DeviceChild; SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&d3d12DeviceChild)))) { |
| 29 | + if (SUCCEEDED(d3d12DeviceChild->GetDevice(IID_PPV_ARGS(&d3dLowLatencyDevice)))) |
| 30 | + return d3dLowLatencyDevice; |
| 31 | + } |
| 32 | + |
| 33 | + return nullptr; |
| 34 | + } |
| 35 | + |
15 | 36 | NvapiD3dLowLatencyDevice* NvapiD3dLowLatencyDevice::GetOrCreate(IUnknown* device) { |
16 | 37 | std::scoped_lock lock{m_mutex}; |
17 | 38 |
|
18 | 39 | if (auto lowLatencyDevice = Get(device)) |
19 | 40 | return lowLatencyDevice; |
20 | 41 |
|
21 | | - Com<ID3DLowLatencyDevice> d3dLowLatencyDevice; |
22 | | - if (FAILED(device->QueryInterface(IID_PPV_ARGS(&d3dLowLatencyDevice)))) |
| 42 | + auto d3dLowLatencyDevice = GetD3DLowLatencyDevice(device); |
| 43 | + if (d3dLowLatencyDevice == nullptr) |
23 | 44 | return nullptr; |
24 | 45 |
|
25 | | - auto [it, inserted] = m_lowLatencyDeviceMap.emplace( |
26 | | - std::piecewise_construct, |
27 | | - std::forward_as_tuple(device), |
28 | | - std::forward_as_tuple(d3dLowLatencyDevice.ptr())); |
| 46 | + // Look for a cache entry where NvapiD3dLowLatencyDevice's m_d3dLowLatencyDevice matches the one we found |
| 47 | + auto itF = std::find_if(m_lowLatencyDeviceMap.begin(), m_lowLatencyDeviceMap.end(), |
| 48 | + [&d3dLowLatencyDevice](auto& item) { return item.second->m_d3dLowLatencyDevice == d3dLowLatencyDevice.ptr(); }); |
| 49 | + |
| 50 | + auto [itI, inserted] = itF == m_lowLatencyDeviceMap.end() |
| 51 | + ? m_lowLatencyDeviceMap.emplace(device, std::make_shared<NvapiD3dLowLatencyDevice>(d3dLowLatencyDevice.ptr())) |
| 52 | + : m_lowLatencyDeviceMap.emplace(device, itF->second); |
29 | 53 |
|
30 | 54 | if (!inserted) |
31 | 55 | return nullptr; |
32 | 56 |
|
33 | | - return &it->second; |
34 | | - } |
35 | | - |
36 | | - NvapiD3dLowLatencyDevice* NvapiD3dLowLatencyDevice::GetOrCreate(ID3D12CommandQueue* commandQueue) { |
37 | | - // some games like The Cycle: Frontier (868270) pass ID3D11DeviceContext to NvAPI_D3D12_SetAsyncFrameMarker, |
38 | | - // so let's not trust the caller and handle this somewhat gracefully instead of just crashing |
39 | | - if (Com<ID3D11DeviceChild> d3d11DeviceChild; SUCCEEDED(commandQueue->QueryInterface(IID_PPV_ARGS(&d3d11DeviceChild)))) { |
40 | | - Com<ID3D11Device> d3d11Device; |
41 | | - d3d11DeviceChild->GetDevice(&d3d11Device); |
42 | | - return GetOrCreate(d3d11Device.ptr()); |
43 | | - } |
44 | | - |
45 | | - Com<ID3D12Device> d3d12Device; |
46 | | - if (SUCCEEDED(commandQueue->GetDevice(IID_PPV_ARGS(&d3d12Device)))) |
47 | | - return GetOrCreate(static_cast<IUnknown*>(d3d12Device.ptr())); |
48 | | - |
49 | | - return nullptr; |
| 57 | + return itI->second.get(); |
50 | 58 | } |
51 | 59 |
|
52 | 60 | NvapiD3dLowLatencyDevice* NvapiD3dLowLatencyDevice::Get(IUnknown* device) { |
53 | 61 | auto it = m_lowLatencyDeviceMap.find(device); |
54 | | - return it == m_lowLatencyDeviceMap.end() ? nullptr : &it->second; |
| 62 | + return it == m_lowLatencyDeviceMap.end() ? nullptr : it->second.get(); |
55 | 63 | } |
56 | 64 |
|
57 | 65 | std::optional<uint32_t> NvapiD3dLowLatencyDevice::ToMarkerType(NV_LATENCY_MARKER_TYPE markerType) { |
|
0 commit comments