Skip to content

Commit 1c4be13

Browse files
ResourceSignatureD3D12: copy inline constants from static resources (#672)
1 parent 69d2f73 commit 1c4be13

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

Graphics/GraphicsEngineD3D12/src/PipelineResourceSignatureD3D12Impl.cpp

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -443,27 +443,47 @@ void PipelineResourceSignatureD3D12Impl::CopyStaticResources(ShaderResourceCache
443443

444444
Uint32 SrcCacheOffset = Attr.OffsetFromTableStart(SrcCacheType);
445445
Uint32 DstCacheOffset = Attr.OffsetFromTableStart(DstCacheType);
446-
for (Uint32 ArrInd = 0; ArrInd < ResDesc.ArraySize; ++ArrInd, ++SrcCacheOffset, ++DstCacheOffset)
446+
if (ResDesc.Flags & PIPELINE_RESOURCE_FLAG_INLINE_CONSTANTS)
447447
{
448-
const ShaderResourceCacheD3D12::Resource& SrcRes = SrcRootTable.GetResource(SrcCacheOffset);
449-
if (!SrcRes.pObject)
450-
{
451-
if (DstCacheType == ResourceCacheContentType::SRB)
452-
LOG_ERROR_MESSAGE("No resource is assigned to static shader variable '", GetShaderResourcePrintName(ResDesc, ArrInd), "' in pipeline resource signature '", m_Desc.Name, "'.");
453-
continue;
454-
}
448+
VERIFY(ResDesc.ResourceType == SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, "Only constant buffers can be marked as INLINE_CONSTANTS.");
455449

450+
const ShaderResourceCacheD3D12::Resource& SrcRes = SrcRootTable.GetResource(SrcCacheOffset);
456451
const ShaderResourceCacheD3D12::Resource& DstRes = DstRootTable.GetResource(DstCacheOffset);
457-
if (DstRes.pObject != SrcRes.pObject)
458-
{
459-
DEV_CHECK_ERR(DstRes.pObject == nullptr, "Static resource has already been initialized, and the new resource does not match previously assigned resource.");
460-
DstResourceCache.CopyResource(d3d12Device, DstRootIndex, DstCacheOffset, SrcRes);
461-
}
462-
else
452+
VERIFY(SrcRes.CPUDescriptorHandle.ptr != 0, "Inline constant resource must have valid CPU descriptor handle.");
453+
VERIFY(DstRes.CPUDescriptorHandle.ptr != 0, "Inline constant resource must have valid CPU descriptor handle.");
454+
455+
// For inline constants, array size is the number of 4-byte constant values
456+
const Uint32 NumConstantValues = ResDesc.ArraySize;
457+
// Copy the actual constant values.
458+
// For inline constants, CPUDescriptorHandle.ptr stores the pointer to the constant values buffer.
459+
memcpy(reinterpret_cast<void*>(DstRes.CPUDescriptorHandle.ptr),
460+
reinterpret_cast<const void*>(SrcRes.CPUDescriptorHandle.ptr),
461+
NumConstantValues * sizeof(Uint32));
462+
}
463+
else
464+
{
465+
for (Uint32 ArrInd = 0; ArrInd < ResDesc.ArraySize; ++ArrInd, ++SrcCacheOffset, ++DstCacheOffset)
463466
{
464-
VERIFY_EXPR(DstRes.pObject == SrcRes.pObject);
465-
VERIFY_EXPR(DstRes.Type == SrcRes.Type);
466-
VERIFY_EXPR(DstRes.CPUDescriptorHandle.ptr == SrcRes.CPUDescriptorHandle.ptr);
467+
const ShaderResourceCacheD3D12::Resource& SrcRes = SrcRootTable.GetResource(SrcCacheOffset);
468+
if (!SrcRes.pObject)
469+
{
470+
if (DstCacheType == ResourceCacheContentType::SRB)
471+
LOG_ERROR_MESSAGE("No resource is assigned to static shader variable '", GetShaderResourcePrintName(ResDesc, ArrInd), "' in pipeline resource signature '", m_Desc.Name, "'.");
472+
continue;
473+
}
474+
475+
const ShaderResourceCacheD3D12::Resource& DstRes = DstRootTable.GetResource(DstCacheOffset);
476+
if (DstRes.pObject != SrcRes.pObject)
477+
{
478+
DEV_CHECK_ERR(DstRes.pObject == nullptr, "Static resource has already been initialized, and the new resource does not match previously assigned resource.");
479+
DstResourceCache.CopyResource(d3d12Device, DstRootIndex, DstCacheOffset, SrcRes);
480+
}
481+
else
482+
{
483+
VERIFY_EXPR(DstRes.pObject == SrcRes.pObject);
484+
VERIFY_EXPR(DstRes.Type == SrcRes.Type);
485+
VERIFY_EXPR(DstRes.CPUDescriptorHandle.ptr == SrcRes.CPUDescriptorHandle.ptr);
486+
}
467487
}
468488
}
469489
}

Graphics/GraphicsEngineD3D12/src/ShaderResourceCacheD3D12.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ void ShaderResourceCacheD3D12::Initialize(IMemoryAllocator& Me
172172
{
173173
Resource& Res = GetRootTable(InlineConstInfo.RootIndex).GetResource(InlineConstInfo.OffsetFromTableStart);
174174
Res.CPUDescriptorHandle.ptr = reinterpret_cast<SIZE_T>(pCurrInlineConstValueStorage);
175+
// Note that normally resource type is set when a resource is bound to the cache.
176+
// For inline constants, we set it here during tinitialization.
177+
Res.Type = SHADER_RESOURCE_TYPE_CONSTANT_BUFFER;
178+
// Store the number of constant values in BufferRangeSize
179+
Res.BufferRangeSize = InlineConstInfo.NumValues;
175180
pCurrInlineConstValueStorage += InlineConstInfo.NumValues;
176181
}
177182
VERIFY_EXPR(pCurrInlineConstValueStorage == GetInlineConstantStorage() + TotalInlineConstantValues);
@@ -262,12 +267,19 @@ void ShaderResourceCacheD3D12::Initialize(IMemoryAllocator& MemAllocator,
262267
VERIFY_EXPR(RootConsts.TableOffsetInGroupAllocation == RootParameter::InvalidTableOffsetInGroupAllocation);
263268
VERIFY_EXPR(RootConsts.d3d12RootParam.ParameterType == D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS);
264269

265-
GetResource(ResIdx).CPUDescriptorHandle.ptr = reinterpret_cast<SIZE_T>(pCurrInlineConstValueStorage);
270+
Resource& Res = GetResource(ResIdx);
271+
Res.CPUDescriptorHandle.ptr = reinterpret_cast<SIZE_T>(pCurrInlineConstValueStorage);
272+
// Note that normally resource type is set when a resource is bound to the cache.
273+
// For inline constants, we set it here during tinitialization.
274+
Res.Type = SHADER_RESOURCE_TYPE_CONSTANT_BUFFER;
275+
// Store the number of constant values in BufferRangeSize
276+
Res.BufferRangeSize = RootConsts.d3d12RootParam.Constants.Num32BitValues;
277+
266278
pCurrInlineConstValueStorage += RootConsts.d3d12RootParam.Constants.Num32BitValues;
267279

268280
new (&GetRootTable(RootConsts.RootIndex)) RootTable{
269281
1,
270-
&GetResource(ResIdx),
282+
&Res,
271283
false //IsRootView
272284
};
273285
++ResIdx;

0 commit comments

Comments
 (0)