Release cached Direct3D devices during adapter cleanup

This commit is contained in:
Mike Rodriguez committed 2026-12-01 20:31:37 -08:00
1 parent d7244969b2
commit 4c4658e214
2 files changed
+13 -76

No files matched your search

+7 -70
View File
@@ -301,8 +301,6 @@ const char* XorCursorSupportLevelToString(IDDCX_XOR_CURSOR_SUPPORT level) {
vector<unsigned char> Microsoft::IndirectDisp::IndirectDeviceContext::s_KnownMonitorEdid; //Changed to support static vector
std::map<LUID, std::shared_ptr<Direct3DDevice>, Microsoft::IndirectDisp::LuidComparator> Microsoft::IndirectDisp::IndirectDeviceContext::s_DeviceCache;
std::mutex Microsoft::IndirectDisp::IndirectDeviceContext::s_DeviceCacheMutex;
struct IndirectDeviceContextWrapper
{
@@ -3577,77 +3575,22 @@ int maincalc() {
std::shared_ptr<Direct3DDevice> IndirectDeviceContext::GetOrCreateDevice(LUID RenderAdapter)
{
std::shared_ptr<Direct3DDevice> Device;
stringstream logStream;
logStream << "GetOrCreateDevice called for LUID: " << RenderAdapter.HighPart << "-" << RenderAdapter.LowPart;
vddlog("d", logStream.str().c_str());
{
std::lock_guard<std::mutex> lock(s_DeviceCacheMutex);
logStream.str("");
logStream << "Device cache size: " << s_DeviceCache.size();
vddlog("d", logStream.str().c_str());
auto it = s_DeviceCache.find(RenderAdapter);
if (it != s_DeviceCache.end()) {
Device = it->second;
if (Device) {
logStream.str("");
logStream << "Reusing cached Direct3DDevice for LUID " << RenderAdapter.HighPart << "-" << RenderAdapter.LowPart;
vddlog("d", logStream.str().c_str());
return Device;
} else {
logStream.str("");
logStream << "Cached Direct3DDevice is null for LUID " << RenderAdapter.HighPart << "-" << RenderAdapter.LowPart << ", removing from cache";
vddlog("d", logStream.str().c_str());
s_DeviceCache.erase(it);
}
std::lock_guard<std::mutex> lock(m_DeviceCacheMutex);
auto it = m_DeviceCache.find(RenderAdapter);
if (it != m_DeviceCache.end()) {
return it->second;
}
}
logStream.str("");
logStream << "Creating new Direct3DDevice for LUID " << RenderAdapter.HighPart << "-" << RenderAdapter.LowPart;
vddlog("d", logStream.str().c_str());
Device = make_shared<Direct3DDevice>(RenderAdapter);
auto Device = make_shared<Direct3DDevice>(RenderAdapter);
if (FAILED(Device->Init())) {
vddlog("e", "Failed to initialize new Direct3DDevice");
return nullptr;
}
{
std::lock_guard<std::mutex> lock(s_DeviceCacheMutex);
s_DeviceCache[RenderAdapter] = Device;
logStream.str("");
logStream << "Created and cached new Direct3DDevice for LUID " << RenderAdapter.HighPart << "-" << RenderAdapter.LowPart << " (cache size now: " << s_DeviceCache.size() << ")";
vddlog("d", logStream.str().c_str());
}
return Device;
}
void IndirectDeviceContext::CleanupExpiredDevices()
{
std::lock_guard<std::mutex> lock(s_DeviceCacheMutex);
int removed = 0;
for (auto it = s_DeviceCache.begin(); it != s_DeviceCache.end();) {
// With shared_ptr cache, we only remove null devices (shouldn't happen)
if (!it->second) {
it = s_DeviceCache.erase(it);
removed++;
} else {
++it;
}
}
if (removed > 0) {
stringstream logStream;
logStream << "Cleaned up " << removed << " null Direct3DDevice references from cache";
vddlog("d", logStream.str().c_str());
}
std::lock_guard<std::mutex> lock(m_DeviceCacheMutex);
return m_DeviceCache.try_emplace(RenderAdapter, Device).first->second;
}
IndirectDeviceContext::IndirectDeviceContext(_In_ WDFDEVICE WdfDevice) :
@@ -3875,12 +3818,6 @@ void IndirectDeviceContext::CreateMonitor(unsigned int index) {
void IndirectDeviceContext::AssignSwapChain(IDDCX_MONITOR Monitor, IDDCX_SWAPCHAIN SwapChain, LUID RenderAdapter, HANDLE NewFrameEvent)
{
// Only cleanup expired devices periodically, not on every assignment
static int assignmentCount = 0;
if (++assignmentCount % 10 == 0) {
CleanupExpiredDevices();
}
auto Device = GetOrCreateDevice(RenderAdapter);
if (!Device)
{
+6 -6
View File
@@ -118,6 +118,12 @@ namespace Microsoft
void AssignSwapChain(IDDCX_MONITOR Monitor, IDDCX_SWAPCHAIN SwapChain, LUID RenderAdapter, HANDLE NewFrameEvent);
void UnassignSwapChain(IDDCX_MONITOR Monitor);
private:
// Keep Direct3D releases in WDF device cleanup, after the monitor workers stop.
std::mutex m_DeviceCacheMutex;
std::map<LUID, std::shared_ptr<Direct3DDevice>, LuidComparator> m_DeviceCache;
std::shared_ptr<Direct3DDevice> GetOrCreateDevice(LUID RenderAdapter);
protected:
WDFDEVICE m_WdfDevice;
@@ -131,12 +137,6 @@ namespace Microsoft
public:
static const DISPLAYCONFIG_VIDEO_SIGNAL_INFO s_KnownMonitorModes[];
static std::vector<BYTE> s_KnownMonitorEdid;
private:
static std::map<LUID, std::shared_ptr<Direct3DDevice>, LuidComparator> s_DeviceCache;
static std::mutex s_DeviceCacheMutex;
static std::shared_ptr<Direct3DDevice> GetOrCreateDevice(LUID RenderAdapter);
static void CleanupExpiredDevices();
};
}
}