Skip to content

Commit 1b68eba

Browse files
DeviceContextD3D11: update inline constant buffers (#672)
1 parent cb316d7 commit 1b68eba

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
@@ -460,17 +460,50 @@ void DeviceContextD3D11Impl::BindShaderResources(Uint32 BindSRBMask)
460460
else
461461
{
462462
// Bind constant buffers with dynamic offsets. In Direct3D11 only those buffers are counted as dynamic.
463-
VERIFY((m_BindInfo.DynamicSRBMask & SignBit) != 0,
464-
"When bit in StaleSRBMask is not set, the same bit in DynamicSRBMask must be set. Check GetCommitMask().");
465-
DEV_CHECK_ERR(pResourceCache->HasDynamicResources(),
466-
"Bit in DynamicSRBMask is set, but the cache does not contain dynamic resources. This may indicate that resources "
467-
"in the cache have changed, but the SRB has not been committed before the draw/dispatch command.");
468-
if (pResourceCache->GetUAVCount(PSInd) > 0)
463+
VERIFY(((m_BindInfo.DynamicSRBMask | m_BindInfo.InlineConstantsSRBMask) & SignBit) != 0,
464+
"When bit in StaleSRBMask is not set, the same bit in either DynamicSRBMask or InlineConstantsSRBMask must be set. Check GetCommitMask().");
465+
466+
if ((m_BindInfo.DynamicSRBMask & SignBit) != 0)
467+
{
468+
DEV_CHECK_ERR(pResourceCache->HasDynamicResources(),
469+
"Shader resource cache does not contain dynamic resources, but the corresponding bit in DynamicSRBMask is set. "
470+
"This may indicate that resources in the cache have changed, but the SRB has not been committed before the draw/dispatch command.");
471+
if (pResourceCache->GetUAVCount(PSInd) > 0)
472+
{
473+
if (PsUavBindMode != PixelShaderUAVBindMode::Bind)
474+
PsUavBindMode = PixelShaderUAVBindMode::Keep;
475+
}
476+
BindDynamicCBs(*pResourceCache, BaseBindings);
477+
}
478+
else
479+
{
480+
DEV_CHECK_ERR(!pResourceCache->HasDynamicResources(),
481+
"Shader resource cache contains dynamic resources, but the corresponding bit in DynamicSRBMask is not set. "
482+
"This may indicate that resources in the cache have changed, but the SRB has not been committed before the draw/dispatch command.");
483+
}
484+
}
485+
486+
if ((m_BindInfo.InlineConstantsSRBMask & SignBit) != 0)
487+
{
488+
VERIFY(pResourceCache->HasInlineConstants(),
489+
"Shader resource cache does not contain inline constants, but the corresponding bit in InlineConstantsSRBMask is set. "
490+
"This may be a bug because root constants mask in the cache never changes after SRB creation, "
491+
"while m_BindInfo.InlineConstantsSRBMask is initialized when SRB is committed.");
492+
if (PipelineResourceSignatureD3D11Impl* pSign = m_pPipelineState->GetResourceSignature(SignIdx))
469493
{
470-
if (PsUavBindMode != PixelShaderUAVBindMode::Bind)
471-
PsUavBindMode = PixelShaderUAVBindMode::Keep;
494+
pSign->UpdateInlineConstantBuffers(*pResourceCache, m_pd3d11DeviceContext);
472495
}
473-
BindDynamicCBs(*pResourceCache, BaseBindings);
496+
else
497+
{
498+
UNEXPECTED("Pipeline resource signature is null for signature index ", SignIdx);
499+
}
500+
}
501+
else
502+
{
503+
VERIFY(!pResourceCache->HasInlineConstants(),
504+
"Shader resource cache contains inline constants, but the corresponding bit in InlineConstantsSRBMask is not set. "
505+
"This may be a bug because root constants mask in the cache never changes after SRB creation, "
506+
"while m_BindInfo.InlineConstantsSRBMask is initialized when SRB is committed.");
474507
}
475508
}
476509

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)