From 7bb8cbcbaad9e62fe44d39db3882826b5d61b950 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 12 May 2024 15:23:58 +1000 Subject: [PATCH] D3D12Device: Fix pipeline cache load error after device change --- src/util/d3d12_device.cpp | 41 +++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/util/d3d12_device.cpp b/src/util/d3d12_device.cpp index e57cb7188..e10b2de04 100644 --- a/src/util/d3d12_device.cpp +++ b/src/util/d3d12_device.cpp @@ -287,21 +287,42 @@ void D3D12Device::DestroyDevice() bool D3D12Device::ReadPipelineCache(const std::string& filename) { - std::optional> data; + std::optional> data = FileSystem::ReadBinaryFile(filename.c_str()); - auto fp = FileSystem::OpenManagedCFile(filename.c_str(), "rb"); - if (fp) - data = FileSystem::ReadBinaryFile(fp.get()); - - const HRESULT hr = + HRESULT hr = m_device->CreatePipelineLibrary(data.has_value() ? data->data() : nullptr, data.has_value() ? data->size() : 0, IID_PPV_ARGS(m_pipeline_library.ReleaseAndGetAddressOf())); + if (SUCCEEDED(hr)) + { + if (data.has_value()) + s_pipeline_cache_data = std::move(data.value()); + + return true; + } + + // Try without the cache data. + if (data.has_value()) + { + Log_WarningFmt("CreatePipelineLibrary() failed, trying without cache data. Error: {}", + Error::CreateHResult(hr).GetDescription()); + + hr = m_device->CreatePipelineLibrary(nullptr, 0, IID_PPV_ARGS(m_pipeline_library.ReleaseAndGetAddressOf())); + if (SUCCEEDED(hr)) + { + // Delete cache file, it's no longer relevant. + Log_InfoFmt("Deleting pipeline cache file {}", filename); + FileSystem::DeleteFile(filename.c_str()); + } + } + if (FAILED(hr)) - Log_WarningPrintf("CreatePipelineLibrary() failed with HRESULT %08X, pipeline caching will not be available.", hr); - else if (data.has_value()) - s_pipeline_cache_data = std::move(data.value()); + { + Log_WarningFmt("CreatePipelineLibrary() failed, pipeline caching will not be available. Error: {}", + Error::CreateHResult(hr).GetDescription()); + return false; + } - return SUCCEEDED(hr); + return true; } bool D3D12Device::GetPipelineCacheData(DynamicHeapArray* data)