Skip to content

Commit 7a4c2ca

Browse files
PipelineResourceSignatureBase: add CreateInlineConstantBuffer method
1 parent beb0fa6 commit 7a4c2ca

3 files changed

Lines changed: 27 additions & 41 deletions

File tree

Graphics/GraphicsEngine/include/PipelineResourceSignatureBase.hpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2025 Diligent Graphics LLC
2+
* Copyright 2019-2026 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -343,6 +343,9 @@ class PipelineResourceSignatureBase : public DeviceObjectBase<typename EngineImp
343343
// Sampler implementation type (SamplerD3D12Impl, SamplerVkImpl, etc.)
344344
using SamplerImplType = typename EngineImplTraits::SamplerImplType;
345345

346+
// Buffer implementation type (BufferD3D12Impl, BufferVkImpl, etc.)
347+
using BufferImplType = typename EngineImplTraits::BufferImplType;
348+
346349
// Pipeline resource signature implementation type (PipelineResourceSignatureD3D12Impl, PipelineResourceSignatureVkImpl, etc.)
347350
using PipelineResourceSignatureImplType = typename EngineImplTraits::PipelineResourceSignatureImplType;
348351

@@ -1061,6 +1064,22 @@ class PipelineResourceSignatureBase : public DeviceObjectBase<typename EngineImp
10611064
}
10621065
}
10631066

1067+
RefCntAutoPtr<BufferImplType> CreateInlineConstantBuffer(const char* ResName, Uint32 NumConstants) const
1068+
{
1069+
RefCntAutoPtr<IBuffer> pBuffer;
1070+
if (this->m_pDevice)
1071+
{
1072+
std::string Name = this->m_Desc.Name;
1073+
Name += " - ";
1074+
Name += ResName;
1075+
BufferDesc CBDesc{Name.c_str(), NumConstants * sizeof(Uint32), BIND_UNIFORM_BUFFER, USAGE_DYNAMIC, CPU_ACCESS_WRITE};
1076+
1077+
this->m_pDevice->CreateBuffer(CBDesc, nullptr, &pBuffer);
1078+
VERIFY_EXPR(pBuffer);
1079+
}
1080+
return RefCntAutoPtr<BufferImplType>{pBuffer.RawPtr<BufferImplType>()};
1081+
}
1082+
10641083
protected:
10651084
std::unique_ptr<void, STDDeleterRawMem<void>> m_pRawMemory;
10661085

Graphics/GraphicsEngineD3D11/src/PipelineResourceSignatureD3D11Impl.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,18 +261,7 @@ void PipelineResourceSignatureD3D11Impl::CreateLayout(const bool IsSerialized)
261261
// which will allow skipping buffer update if the inline constants are not changed.
262262
// However, this will increase memory consumption as each SRB will have its own copy of the inline CB.
263263
// Besides, inline constants are expected to change frequently, so skipping updates is unlikely.
264-
if (m_pDevice)
265-
{
266-
std::string Name = m_Desc.Name;
267-
Name += " - ";
268-
Name += ResDesc.Name;
269-
BufferDesc CBDesc{Name.c_str(), ResDesc.ArraySize * sizeof(Uint32), BIND_UNIFORM_BUFFER, USAGE_DYNAMIC, CPU_ACCESS_WRITE};
270-
271-
RefCntAutoPtr<IBuffer> pBuffer;
272-
m_pDevice->CreateBuffer(CBDesc, nullptr, &pBuffer);
273-
VERIFY_EXPR(pBuffer);
274-
InlineCBAttribs.pBuffer = pBuffer.RawPtr<BufferD3D11Impl>();
275-
}
264+
InlineCBAttribs.pBuffer = CreateInlineConstantBuffer(ResDesc.Name, ResDesc.ArraySize);
276265
}
277266
}
278267
else

Graphics/GraphicsEngineVulkan/src/PipelineResourceSignatureVkImpl.cpp

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ void PipelineResourceSignatureVkImpl::CreateSetLayouts(const bool IsSerialized)
210210
}
211211

212212
// Initialize static resource cache (now that we know the inline constant size)
213-
if (GetNumStaticResStages() > 0 && StaticResourceCount > 0)
213+
if (StaticResourceCount > 0)
214214
{
215+
VERIFY_EXPR(GetNumStaticResStages() > 0);
215216
m_pStaticResCache->InitializeSets(GetRawAllocator(), 1, &StaticResourceCount, TotalStaticInlineConstants);
216217
}
217218

@@ -403,26 +404,10 @@ void PipelineResourceSignatureVkImpl::CreateSetLayouts(const bool IsSerialized)
403404
InlineCBAttribs.BindingIndex = pAttribs->BindingIndex;
404405
InlineCBAttribs.NumConstants = ResDesc.ArraySize; // For inline constants, ArraySize is the number of 32-bit constants
405406

406-
// Create a shared buffer in the Signature for all inline constants
407+
// Create a shared buffer in the Signature for all inline constants.
407408
// All SRBs will reference this same buffer (similar to D3D11 backend)
408-
// Push constant selection is handled at PSO creation time
409-
if (m_pDevice)
410-
{
411-
std::string Name = m_Desc.Name;
412-
Name += " - ";
413-
Name += ResDesc.Name;
414-
BufferDesc CBDesc;
415-
CBDesc.Name = Name.c_str();
416-
CBDesc.Size = ResDesc.ArraySize * sizeof(Uint32);
417-
CBDesc.Usage = USAGE_DYNAMIC;
418-
CBDesc.BindFlags = BIND_UNIFORM_BUFFER;
419-
CBDesc.CPUAccessFlags = CPU_ACCESS_WRITE;
420-
421-
RefCntAutoPtr<IBuffer> pBuffer;
422-
m_pDevice->CreateBuffer(CBDesc, nullptr, &pBuffer);
423-
VERIFY_EXPR(pBuffer);
424-
InlineCBAttribs.pBuffer = RefCntAutoPtr<BufferVkImpl>{pBuffer, IID_BufferVk};
425-
}
409+
// Push constant selection is handled at PSO creation time.
410+
InlineCBAttribs.pBuffer = CreateInlineConstantBuffer(ResDesc.Name, ResDesc.ArraySize);
426411
}
427412
}
428413
VERIFY_EXPR(InlineConstantBufferIdx == m_NumInlineConstantBufferAttribs);
@@ -577,14 +562,7 @@ void PipelineResourceSignatureVkImpl::Destruct()
577562

578563
// Release shared inline constant buffers before base class Destruct
579564
// Each InlineConstantBufferAttribsVk::pBuffer holds a RefCntAutoPtr to the shared buffer
580-
if (m_InlineConstantBufferAttribs)
581-
{
582-
for (Uint32 i = 0; i < m_NumInlineConstantBufferAttribs; ++i)
583-
{
584-
m_InlineConstantBufferAttribs[i].pBuffer.Release();
585-
}
586-
m_InlineConstantBufferAttribs.reset();
587-
}
565+
m_InlineConstantBufferAttribs.reset();
588566
m_NumInlineConstantBufferAttribs = 0;
589567

590568
TPipelineResourceSignatureBase::Destruct();

0 commit comments

Comments
 (0)