Skip to content

Commit 133b327

Browse files
DeviceContextD3D11: update inline constant buffers (#672)
1 parent 7f05671 commit 133b327

File tree

3 files changed

+77
-25
lines changed

3 files changed

+77
-25
lines changed

Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ class ShaderResourceCacheD3D11 : public ShaderResourceCacheBase
348348
template <D3D11_RESOURCE_RANGE>
349349
__forceinline Uint32 GetResourceCount(Uint32 ShaderInd) const;
350350

351-
bool IsInitialized() const { return m_IsInitialized; }
351+
bool IsInitialized() const { return m_Flags & FLAG_IS_INITIALIZED; }
352352

353353
ResourceCacheContentType GetContentType() const { return m_ContentType; }
354354

@@ -422,7 +422,7 @@ class ShaderResourceCacheD3D11 : public ShaderResourceCacheBase
422422

423423
bool HasInlineConstants() const
424424
{
425-
return false;
425+
return m_Flags & FLAG_HAS_INLINE_CONSTANTS;
426426
}
427427

428428
#ifdef DILIGENT_DEBUG
@@ -504,7 +504,14 @@ class ShaderResourceCacheD3D11 : public ShaderResourceCacheBase
504504

505505
std::array<OffsetType, MaxOffsets> m_Offsets = {};
506506

507-
bool m_IsInitialized = false;
507+
enum FLAGS : Uint8
508+
{
509+
FLAG_NONE = 0,
510+
FLAG_IS_INITIALIZED = 1u << 0u,
511+
FLAG_HAS_INLINE_CONSTANTS = 1u << 1u,
512+
};
513+
DECLARE_FRIEND_FLAG_ENUM_OPERATORS(FLAGS)
514+
FLAGS m_Flags = FLAG_NONE;
508515

509516
// Indicates what types of resources are stored in the cache
510517
const ResourceCacheContentType m_ContentType;
@@ -518,6 +525,7 @@ class ShaderResourceCacheD3D11 : public ShaderResourceCacheBase
518525

519526
std::unique_ptr<Uint8, STDDeleter<Uint8, IMemoryAllocator>> m_pResourceData;
520527
};
528+
DEFINE_FLAG_ENUM_OPERATORS(ShaderResourceCacheD3D11::FLAGS)
521529

522530
template <>
523531
struct ShaderResourceCacheD3D11::CachedResourceTraits<D3D11_RESOURCE_RANGE_CBV>

Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -457,17 +457,50 @@ void DeviceContextD3D11Impl::BindShaderResources(Uint32 BindSRBMask)
457457
else
458458
{
459459
// Bind constant buffers with dynamic offsets. In Direct3D11 only those buffers are counted as dynamic.
460-
VERIFY((m_BindInfo.DynamicSRBMask & SignBit) != 0,
461-
"When bit in StaleSRBMask is not set, the same bit in DynamicSRBMask must be set. Check GetCommitMask().");
462-
DEV_CHECK_ERR(pResourceCache->HasDynamicResources(),
463-
"Bit in DynamicSRBMask is set, but the cache does not contain dynamic resources. This may indicate that resources "
464-
"in the cache have changed, but the SRB has not been committed before the draw/dispatch command.");
465-
if (pResourceCache->GetUAVCount(PSInd) > 0)
460+
VERIFY(((m_BindInfo.DynamicSRBMask | m_BindInfo.InlineConstantsSRBMask) & SignBit) != 0,
461+
"When bit in StaleSRBMask is not set, the same bit in either DynamicSRBMask or InlineConstantsSRBMask must be set. Check GetCommitMask().");
462+
463+
if ((m_BindInfo.DynamicSRBMask & SignBit) != 0)
464+
{
465+
DEV_CHECK_ERR(pResourceCache->HasDynamicResources(),
466+
"Shader resource cache does not contain dynamic resources, but the corresponding bit in DynamicSRBMask is set. "
467+
"This may indicate that resources in the cache have changed, but the SRB has not been committed before the draw/dispatch command.");
468+
if (pResourceCache->GetUAVCount(PSInd) > 0)
469+
{
470+
if (PsUavBindMode != PixelShaderUAVBindMode::Bind)
471+
PsUavBindMode = PixelShaderUAVBindMode::Keep;
472+
}
473+
BindDynamicCBs(*pResourceCache, BaseBindings);
474+
}
475+
else
476+
{
477+
DEV_CHECK_ERR(!pResourceCache->HasDynamicResources(),
478+
"Shader resource cache contains dynamic resources, but the corresponding bit in DynamicSRBMask is not set. "
479+
"This may indicate that resources in the cache have changed, but the SRB has not been committed before the draw/dispatch command.");
480+
}
481+
}
482+
483+
if ((m_BindInfo.InlineConstantsSRBMask & SignBit) != 0)
484+
{
485+
VERIFY(pResourceCache->HasInlineConstants(),
486+
"Shader resource cache does not contain inline constants, but the corresponding bit in InlineConstantsSRBMask is set. "
487+
"This may be a bug because inline constants flag in the cache never changes after SRB creation, "
488+
"while m_BindInfo.InlineConstantsSRBMask is initialized when SRB is committed.");
489+
if (PipelineResourceSignatureD3D11Impl* pSign = m_pPipelineState->GetResourceSignature(SignIdx))
466490
{
467-
if (PsUavBindMode != PixelShaderUAVBindMode::Bind)
468-
PsUavBindMode = PixelShaderUAVBindMode::Keep;
491+
pSign->UpdateInlineConstantBuffers(*pResourceCache, m_pd3d11DeviceContext);
469492
}
470-
BindDynamicCBs(*pResourceCache, BaseBindings);
493+
else
494+
{
495+
UNEXPECTED("Pipeline resource signature is null for signature index ", SignIdx);
496+
}
497+
}
498+
else
499+
{
500+
VERIFY(!pResourceCache->HasInlineConstants(),
501+
"Shader resource cache contains inline constants, but the corresponding bit in InlineConstantsSRBMask is not set. "
502+
"This may be a bug because inline constants flag in the cache never changes after SRB creation, "
503+
"while m_BindInfo.InlineConstantsSRBMask is initialized when SRB is committed.");
471504
}
472505
}
473506

Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,17 @@ void ShaderResourceCacheD3D11::Initialize(const D3D11ShaderResourceCounters&
164164
}
165165
};
166166

167-
ProcessInlineCBs([&BufferSize](const InlineConstantBufferAttribsD3D11& InlineCBAttr) {
168-
BufferSize += InlineCBAttr.NumConstants * sizeof(Uint32);
167+
Uint32 TotalInlineConstants = 0;
168+
ProcessInlineCBs([&TotalInlineConstants](const InlineConstantBufferAttribsD3D11& InlineCBAttr) {
169+
TotalInlineConstants += InlineCBAttr.NumConstants;
169170
});
170171

172+
if (TotalInlineConstants > 0)
173+
{
174+
m_Flags |= FLAG_HAS_INLINE_CONSTANTS;
175+
BufferSize += TotalInlineConstants * sizeof(Uint32);
176+
}
177+
171178
if (BufferSize > 0)
172179
{
173180
m_pResourceData = decltype(m_pResourceData){
@@ -186,16 +193,20 @@ void ShaderResourceCacheD3D11::Initialize(const D3D11ShaderResourceCounters&
186193
ConstructResources<D3D11_RESOURCE_RANGE_UAV>(ShaderInd);
187194
}
188195

189-
Uint32* pInlineCBData = reinterpret_cast<Uint32*>(m_pResourceData.get() + MemOffset);
190-
// Initialize inline constant buffers.
191-
ProcessInlineCBs([&pInlineCBData, this](const InlineConstantBufferAttribsD3D11& InlineCBAttr) {
192-
VERIFY_EXPR(InlineCBAttr.NumConstants > 0);
193-
VERIFY_EXPR(InlineCBAttr.pBuffer != nullptr);
194-
InitInlineConstantBuffer(InlineCBAttr.BindPoints, InlineCBAttr.pBuffer, InlineCBAttr.NumConstants, pInlineCBData);
195-
pInlineCBData += InlineCBAttr.NumConstants;
196-
});
196+
if (TotalInlineConstants > 0)
197+
{
198+
Uint32* pInlineCBData = reinterpret_cast<Uint32*>(m_pResourceData.get() + MemOffset);
199+
// Initialize inline constant buffers.
200+
ProcessInlineCBs([&pInlineCBData, this](const InlineConstantBufferAttribsD3D11& InlineCBAttr) {
201+
VERIFY_EXPR(InlineCBAttr.NumConstants > 0);
202+
VERIFY_EXPR(InlineCBAttr.pBuffer != nullptr);
203+
InitInlineConstantBuffer(InlineCBAttr.BindPoints, InlineCBAttr.pBuffer, InlineCBAttr.NumConstants, pInlineCBData);
204+
pInlineCBData += InlineCBAttr.NumConstants;
205+
});
206+
VERIFY_EXPR(pInlineCBData == reinterpret_cast<Uint32*>(m_pResourceData.get() + BufferSize));
207+
}
197208

198-
m_IsInitialized = true;
209+
m_Flags |= FLAG_IS_INITIALIZED;
199210
}
200211

201212
ShaderResourceCacheD3D11::~ShaderResourceCacheD3D11()
@@ -210,8 +221,8 @@ ShaderResourceCacheD3D11::~ShaderResourceCacheD3D11()
210221
DestructResources<D3D11_RESOURCE_RANGE_SAMPLER>(ShaderInd);
211222
DestructResources<D3D11_RESOURCE_RANGE_UAV>(ShaderInd);
212223
}
213-
m_Offsets = {};
214-
m_IsInitialized = false;
224+
m_Offsets = {};
225+
m_Flags = FLAG_NONE;
215226

216227
m_pResourceData.reset();
217228
}

0 commit comments

Comments
 (0)