Skip to content

Commit

Permalink
Render state cache: added option to log cache events
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 20, 2022
1 parent 3e92794 commit 3d02913
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
16 changes: 16 additions & 0 deletions Graphics/GraphicsTools/interface/RenderStateCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
37 changes: 33 additions & 4 deletions Graphics/GraphicsTools/src/RenderStateCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class RenderStateCacheImpl final : public ObjectBase<IRenderStateCache>
private:
RefCntAutoPtr<IRenderDevice> m_pDevice;
const RENDER_DEVICE_TYPE m_DeviceType;
const bool m_EnableLogging;
RefCntAutoPtr<ISerializationDevice> m_pSerializationDevice;
RefCntAutoPtr<IArchiver> m_pArchiver;
RefCntAutoPtr<IDearchiver> m_pDearchiver;
Expand All @@ -189,8 +190,11 @@ class RenderStateCacheImpl final : public ObjectBase<IRenderStateCache>
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");
Expand Down Expand Up @@ -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)
{
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -317,7 +334,12 @@ bool RenderStateCacheImpl::CreateShader(const ShaderCreateInfo& ShaderCI,
ArchiveInfo.DeviceFlags = static_cast<ARCHIVE_DEVICE_DATA_FLAGS>(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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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 (...)
Expand Down
3 changes: 1 addition & 2 deletions Tests/DiligentCoreAPITest/src/RenderStateCacheTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ void VerifyComputePSO(IPipelineState* pPSO, bool UseSignature = false)

RefCntAutoPtr<IRenderStateCache> CreateCache(IRenderDevice* pDevice, IDataBlob* pCacheData = nullptr)
{
RenderStateCacheCreateInfo CacheCI;
CacheCI.pDevice = pDevice;
RenderStateCacheCreateInfo CacheCI{pDevice, true};

RefCntAutoPtr<IRenderStateCache> pCache;
CreateRenderStateCache(CacheCI, &pCache);
Expand Down

0 comments on commit 3d02913

Please sign in to comment.