Skip to content

Commit 2a86179

Browse files
ResourceCacheD3D12: implement SetInlineConstants (#672)
1 parent 1c02a41 commit 2a86179

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

Graphics/GraphicsEngine/include/ShaderResourceVariableBase.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ bool VerifyResourceBinding(const char* ExpectedResourceTypeName,
165165
const IDeviceObject* pCachedObject, // Object already set in the cache
166166
const char* SignatureName)
167167
{
168+
if (ResDesc.ResourceType == SHADER_RESOURCE_TYPE_INLINE_CONSTANTS)
169+
{
170+
RESOURCE_VALIDATION_FAILURE("Cannot bind resources to inline constants variable '", GetShaderResourcePrintName(ResDesc, BindInfo.ArrayIndex), '\'');
171+
return false;
172+
}
173+
168174
if (BindInfo.pObject != nullptr && pResourceImpl == nullptr)
169175
{
170176
std::stringstream ss;
@@ -602,6 +608,34 @@ bool VerifyDynamicBufferOffset(const PipelineResourceDesc& ResDesc,
602608
}
603609

604610

611+
inline bool VerifyInlineConstants(const PipelineResourceDesc& ResDesc,
612+
const void* pConstants,
613+
Uint32 FirstConstant,
614+
Uint32 NumConstants)
615+
{
616+
bool ParamsOK = true;
617+
if (ResDesc.ResourceType != SHADER_RESOURCE_TYPE_INLINE_CONSTANTS)
618+
{
619+
RESOURCE_VALIDATION_FAILURE("SetInlineConstants() is only allowed for inline constant variables.");
620+
ParamsOK = false;
621+
}
622+
623+
if (pConstants == nullptr)
624+
{
625+
RESOURCE_VALIDATION_FAILURE("Pointer to inline constants is null.");
626+
ParamsOK = false;
627+
}
628+
629+
if (FirstConstant + NumConstants > ResDesc.ArraySize)
630+
{
631+
RESOURCE_VALIDATION_FAILURE("Inline constant range (", FirstConstant, " .. ", FirstConstant + NumConstants - 1,
632+
") is out of bounds for variable '", ResDesc.Name, "' of size ", ResDesc.ArraySize, " constants.");
633+
ParamsOK = false;
634+
}
635+
636+
return ParamsOK;
637+
}
638+
605639
#undef RESOURCE_VALIDATION_FAILURE
606640

607641
template <typename ShaderVectorType>

Graphics/GraphicsEngineD3D12/include/ShaderResourceCacheD3D12.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ class ShaderResourceCacheD3D12 : public ShaderResourceCacheBase
281281
Uint32 OffsetFromTableStart,
282282
Uint32 BufferDynamicOffset);
283283

284+
void SetInlineConstants(Uint32 RootIndex,
285+
const void* pConstants,
286+
Uint32 FirstConstant,
287+
Uint32 NumConstants);
288+
284289
const RootTable& GetRootTable(Uint32 RootIndex) const
285290
{
286291
VERIFY_EXPR(RootIndex < m_NumTables);

Graphics/GraphicsEngineD3D12/src/ShaderResourceCacheD3D12.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,22 @@ void ShaderResourceCacheD3D12::SetBufferDynamicOffset(Uint32 RootIndex,
386386
Res.BufferDynamicOffset = BufferDynamicOffset;
387387
}
388388

389+
void ShaderResourceCacheD3D12::SetInlineConstants(Uint32 RootIndex,
390+
const void* pConstants,
391+
Uint32 FirstConstant,
392+
Uint32 NumConstants)
393+
{
394+
RootTable& Tbl = GetRootTable(RootIndex);
395+
Resource& Res = Tbl.GetResource(0);
396+
VERIFY_EXPR(Res.Type == SHADER_RESOURCE_TYPE_INLINE_CONSTANTS);
397+
VERIFY(Res.IsNull(), "There should be no resource bound for root constants as they contain raw data.");
398+
VERIFY(Res.CPUDescriptorHandle.ptr != 0, "Resources used to store root constants must have valid pointer to the data.");
399+
VERIFY(FirstConstant + NumConstants <= Res.BufferRangeSize,
400+
"Too many constants (", FirstConstant + NumConstants, ") for the allocated space (", Res.BufferRangeSize, ")");
401+
Uint32* pDstConstants = reinterpret_cast<Uint32*>(Res.CPUDescriptorHandle.ptr);
402+
memcpy(pDstConstants + FirstConstant, pConstants, NumConstants * sizeof(Uint32));
403+
}
404+
389405
const ShaderResourceCacheD3D12::Resource& ShaderResourceCacheD3D12::CopyResource(ID3D12Device* pd3d12Device,
390406
Uint32 RootIndex,
391407
Uint32 OffsetFromTableStart,

Graphics/GraphicsEngineD3D12/src/ShaderVariableManagerD3D12.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,19 @@ void ShaderVariableManagerD3D12::SetInlineConstants(Uint32 ResIndex,
678678
Uint32 FirstConstant,
679679
Uint32 NumConstants)
680680
{
681-
UNSUPPORTED("Not implemented yet");
681+
const ResourceAttribs& Attribs = m_pSignature->GetResourceAttribs(ResIndex);
682+
const ResourceCacheContentType CacheType = m_ResourceCache.GetContentType();
683+
const Uint32 RootIndex = Attribs.RootIndex(CacheType);
684+
VERIFY_EXPR(Attribs.OffsetFromTableStart(CacheType) == 0);
685+
686+
#ifdef DILIGENT_DEVELOPMENT
687+
{
688+
const PipelineResourceDesc& ResDesc = m_pSignature->GetResourceDesc(ResIndex);
689+
VerifyInlineConstants(ResDesc, pConstants, FirstConstant, NumConstants);
690+
}
691+
#endif
692+
693+
m_ResourceCache.SetInlineConstants(RootIndex, pConstants, FirstConstant, NumConstants);
682694
}
683695

684696
IDeviceObject* ShaderVariableManagerD3D12::Get(Uint32 ArrayIndex,

0 commit comments

Comments
 (0)