From 3d029133cdd5cc8dc3196a0fbbb459240db2556c Mon Sep 17 00:00:00 2001 From: assiduous Date: Wed, 19 Oct 2022 17:00:58 -0700 Subject: [PATCH] Render state cache: added option to log cache events --- .../interface/RenderStateCache.h | 16 ++++++++ .../GraphicsTools/src/RenderStateCache.cpp | 37 +++++++++++++++++-- .../src/RenderStateCacheTest.cpp | 3 +- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/Graphics/GraphicsTools/interface/RenderStateCache.h b/Graphics/GraphicsTools/interface/RenderStateCache.h index d886a2433..3d9b8a346 100644 --- a/Graphics/GraphicsTools/interface/RenderStateCache.h +++ b/Graphics/GraphicsTools/interface/RenderStateCache.h @@ -38,6 +38,22 @@ struct RenderStateCacheCreateInfo { /// A pointer to the render device, must not be null. IRenderDevice* pDevice DEFAULT_INITIALIZER(nullptr); + + /// Whether to log the cache usage events such as + /// if the object was found in the cache or not. + bool EnableLogging DEFAULT_INITIALIZER(false); + +#if DILIGENT_CPP_INTERFACE + constexpr RenderStateCacheCreateInfo() noexcept + {} + + constexpr explicit RenderStateCacheCreateInfo( + IRenderDevice* _pDevice, + bool _EnableLogging = RenderStateCacheCreateInfo{}.EnableLogging) noexcept : + pDevice{_pDevice}, + EnableLogging{_EnableLogging} + {} +#endif }; typedef struct RenderStateCacheCreateInfo RenderStateCacheCreateInfo; diff --git a/Graphics/GraphicsTools/src/RenderStateCache.cpp b/Graphics/GraphicsTools/src/RenderStateCache.cpp index 235b9bdba..29c5c6012 100644 --- a/Graphics/GraphicsTools/src/RenderStateCache.cpp +++ b/Graphics/GraphicsTools/src/RenderStateCache.cpp @@ -175,6 +175,7 @@ class RenderStateCacheImpl final : public ObjectBase private: RefCntAutoPtr m_pDevice; const RENDER_DEVICE_TYPE m_DeviceType; + const bool m_EnableLogging; RefCntAutoPtr m_pSerializationDevice; RefCntAutoPtr m_pArchiver; RefCntAutoPtr m_pDearchiver; @@ -189,8 +190,11 @@ class RenderStateCacheImpl final : public ObjectBase RenderStateCacheImpl::RenderStateCacheImpl(IReferenceCounters* pRefCounters, const RenderStateCacheCreateInfo& CreateInfo) : TBase{pRefCounters}, - m_pDevice{CreateInfo.pDevice}, - m_DeviceType{CreateInfo.pDevice != nullptr ? CreateInfo.pDevice->GetDeviceInfo().Type : RENDER_DEVICE_TYPE_UNDEFINED} + // clang-format off + m_pDevice {CreateInfo.pDevice}, + m_DeviceType {CreateInfo.pDevice != nullptr ? CreateInfo.pDevice->GetDeviceInfo().Type : RENDER_DEVICE_TYPE_UNDEFINED}, + m_EnableLogging{CreateInfo.EnableLogging} +// clang-format on { if (CreateInfo.pDevice == nullptr) LOG_ERROR_AND_THROW("CreateInfo.pDevice must not be null"); @@ -227,6 +231,15 @@ RenderStateCacheImpl::RenderStateCacheImpl(IReferenceCounters* pRe LOG_ERROR_AND_THROW("Failed to create dearchiver"); } +#define RENDER_STATE_CACHE_LOG(...) \ + do \ + { \ + if (m_EnableLogging) \ + { \ + LOG_INFO_MESSAGE("Render state cache: ", __VA_ARGS__); \ + } \ + } while (false) + bool RenderStateCacheImpl::CreateShader(const ShaderCreateInfo& ShaderCI, IShader** ppShader) { @@ -252,6 +265,7 @@ bool RenderStateCacheImpl::CreateShader(const ShaderCreateInfo& ShaderCI, if (auto pShader = it->second.Lock()) { *ppShader = pShader.Detach(); + RENDER_STATE_CACHE_LOG("Reusing existing shader '", (ShaderCI.Desc.Name ? ShaderCI.Desc.Name : ""), "'."); return true; } else @@ -303,7 +317,10 @@ bool RenderStateCacheImpl::CreateShader(const ShaderCreateInfo& ShaderCI, UnpackInfo.pUserData = Callback; m_pDearchiver->UnpackShader(UnpackInfo, ppShader); if (*ppShader != nullptr) + { + RENDER_STATE_CACHE_LOG("Found shader '", HashStr, "'."); return true; + } } // Next, try to find the shader in the archiver @@ -317,7 +334,12 @@ bool RenderStateCacheImpl::CreateShader(const ShaderCreateInfo& ShaderCI, ArchiveInfo.DeviceFlags = static_cast(1 << m_DeviceType); m_pSerializationDevice->CreateShader(ArchiveShaderCI, ArchiveInfo, &pArchivedShader); if (pArchivedShader) - m_pArchiver->AddShader(pArchivedShader); + { + if (m_pArchiver->AddShader(pArchivedShader)) + RENDER_STATE_CACHE_LOG("Added shader '", HashStr, "'."); + else + LOG_ERROR_MESSAGE("Failed to archive shader '", HashStr, "'."); + } } if (pArchivedShader) @@ -570,6 +592,7 @@ bool RenderStateCacheImpl::CreatePipelineState(const CreateInfoType& PSOCreateIn if (auto pPSO = it->second.Lock()) { *ppPipelineState = pPSO.Detach(); + RENDER_STATE_CACHE_LOG("Reusing existing PSO '", (PSOCreateInfo.PSODesc.Name ? PSOCreateInfo.PSODesc.Name : ""), "'."); return true; } else @@ -612,7 +635,10 @@ bool RenderStateCacheImpl::CreatePipelineState(const CreateInfoType& PSOCreateIn } if (FoundInCache) + { + RENDER_STATE_CACHE_LOG("Found PSO '", HashStr, "'."); return true; + } if (m_pArchiver->GetPipelineState(PSOCreateInfo.PSODesc.PipelineType, HashStr.c_str()) != nullptr) return true; @@ -630,7 +656,10 @@ bool RenderStateCacheImpl::CreatePipelineState(const CreateInfoType& PSOCreateIn if (pSerializedPSO) { - m_pArchiver->AddPipelineState(pSerializedPSO); + if (m_pArchiver->AddPipelineState(pSerializedPSO)) + RENDER_STATE_CACHE_LOG("Added PSO '", HashStr, "'."); + else + LOG_ERROR_MESSAGE("Failed to archive PSO '", HashStr, "'."); } } catch (...) diff --git a/Tests/DiligentCoreAPITest/src/RenderStateCacheTest.cpp b/Tests/DiligentCoreAPITest/src/RenderStateCacheTest.cpp index 6d43c4d1e..ee4250964 100644 --- a/Tests/DiligentCoreAPITest/src/RenderStateCacheTest.cpp +++ b/Tests/DiligentCoreAPITest/src/RenderStateCacheTest.cpp @@ -182,8 +182,7 @@ void VerifyComputePSO(IPipelineState* pPSO, bool UseSignature = false) RefCntAutoPtr CreateCache(IRenderDevice* pDevice, IDataBlob* pCacheData = nullptr) { - RenderStateCacheCreateInfo CacheCI; - CacheCI.pDevice = pDevice; + RenderStateCacheCreateInfo CacheCI{pDevice, true}; RefCntAutoPtr pCache; CreateRenderStateCache(CacheCI, &pCache);