Skip to content

Commit 4e2f0bd

Browse files
committed
Finish step 3: Extend ShaderResourceCacheGL for inline constant staging.
1 parent 22b9811 commit 4e2f0bd

2 files changed

Lines changed: 85 additions & 13 deletions

File tree

Graphics/GraphicsEngineOpenGL/include/ShaderResourceCacheGL.hpp

Lines changed: 74 additions & 8 deletions
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");
@@ -40,10 +40,10 @@ namespace Diligent
4040

4141
// All resources are stored in the continuous memory using the following layout:
4242
//
43-
// | Cached UBs | Cached Textures | Cached Images | Cached Storage Blocks |
44-
// |----------------------------------------------------|--------------------------|---------------------------|
45-
// | 0 | 1 | ... | UBCount-1 | 0 | 1 | ...| SmpCount-1 | 0 | 1 | ... | ImgCount-1 | 0 | 1 | ... | SBOCount-1 |
46-
// -----------------------------------------------------------------------------------------------------------
43+
// | Cached UBs | Cached Textures | Cached Images | Cached Storage Blocks | Inline Constant Data |
44+
// |----------------------------------------------------|--------------------------|---------------------------|-------------------------|
45+
// | 0 | 1 | ... | UBCount-1 | 0 | 1 | ...| SmpCount-1 | 0 | 1 | ... | ImgCount-1 | 0 | 1 | ... | SBOCount-1 | Uint32[] (tail) |
46+
// --------------------------------------------------------------------------------------------------------------------------------------
4747
//
4848
class ShaderResourceCacheGL : public ShaderResourceCacheBase
4949
{
@@ -71,13 +71,27 @@ class ShaderResourceCacheGL : public ShaderResourceCacheBase
7171
Uint32 RangeSize = 0;
7272
Uint32 DynamicOffset = 0;
7373

74+
// Pointer to inline constant data
75+
void* pInlineConstantData = nullptr;
76+
7477
// In OpenGL dynamic buffers are only those that are not bound as a whole and
7578
// can use a dynamic offset, irrespective of the variable type or whether the
7679
// buffer is USAGE_DYNAMIC or not.
7780
bool IsDynamic() const
7881
{
7982
return pBuffer && RangeSize < pBuffer->GetDesc().Size;
8083
}
84+
85+
void SetInlineConstants(const void* pSrcConstants, Uint32 FirstConstant, Uint32 NumConstants)
86+
{
87+
VERIFY(pSrcConstants != nullptr, "Source constant data pointer is null");
88+
VERIFY(FirstConstant + NumConstants <= RangeSize / sizeof(Uint32),
89+
"Too many constants (", FirstConstant + NumConstants, ") for the allocated space (", RangeSize / sizeof(Uint32), ")");
90+
VERIFY(pInlineConstantData != nullptr, "Inline constant data pointer is null");
91+
memcpy(reinterpret_cast<Uint8*>(pInlineConstantData) + FirstConstant * sizeof(Uint32),
92+
pSrcConstants,
93+
NumConstants * sizeof(Uint32));
94+
}
8195
};
8296

8397
/// Describes a resource bound to a sampler or an image slot
@@ -142,9 +156,9 @@ class ShaderResourceCacheGL : public ShaderResourceCacheBase
142156
};
143157

144158
using TResourceCount = std::array<Uint16, 4>; // same as PipelineResourceSignatureGLImpl::TBindings.
145-
static size_t GetRequiredMemorySize(const TResourceCount& ResCount);
159+
static size_t GetRequiredMemorySize(const TResourceCount& ResCount, Uint32 TotalInlineConstants = 0);
146160

147-
void Initialize(const TResourceCount& Count, IMemoryAllocator& MemAllocator, Uint64 DynamicUBOSlotMask, Uint64 DynamicSSBOSlotMask);
161+
void Initialize(const TResourceCount& Count, IMemoryAllocator& MemAllocator, Uint64 DynamicUBOSlotMask, Uint64 DynamicSSBOSlotMask, Uint32 TotalInlineConstants = 0);
148162

149163
void SetUniformBuffer(Uint32 CacheOffset, RefCntAutoPtr<BufferGLImpl>&& pBuff, Uint64 BaseOffset, Uint64 RangeSize)
150164
{
@@ -347,7 +361,56 @@ class ShaderResourceCacheGL : public ShaderResourceCacheBase
347361

348362
bool HasInlineConstants() const
349363
{
350-
return false;
364+
return m_HasInlineConstants;
365+
}
366+
367+
void InitInlineConstantBuffer(Uint32 CacheOffset,
368+
RefCntAutoPtr<BufferGLImpl>&& pBuffer,
369+
Uint32 NumConstants,
370+
void* pInlineConstantData)
371+
{
372+
VERIFY_EXPR(pBuffer);
373+
VERIFY_EXPR(pInlineConstantData);
374+
375+
CachedUB& UB = GetUB(CacheOffset);
376+
UB.pBuffer = std::move(pBuffer);
377+
UB.BaseOffset = 0;
378+
UB.RangeSize = NumConstants * sizeof(Uint32);
379+
UB.DynamicOffset = 0;
380+
UB.pInlineConstantData = pInlineConstantData;
381+
382+
UpdateRevision();
383+
}
384+
385+
void SetInlineConstants(Uint32 CacheOffset,
386+
const void* pConstants,
387+
Uint32 FirstConstant,
388+
Uint32 NumConstants)
389+
{
390+
VERIFY(CacheOffset < GetUBCount(), "Cache offset is out of range");
391+
CachedUB& UB = GetUB(CacheOffset);
392+
UB.SetInlineConstants(pConstants, FirstConstant, NumConstants);
393+
UpdateRevision();
394+
}
395+
396+
void CopyInlineConstants(const ShaderResourceCacheGL& SrcCache,
397+
Uint32 CacheOffset,
398+
Uint32 NumConstants)
399+
{
400+
VERIFY(CacheOffset < GetUBCount(), "Destination index is out of range");
401+
VERIFY(CacheOffset < SrcCache.GetUBCount(), "Source index is out of range");
402+
403+
const CachedUB& SrcUB = SrcCache.GetConstUB(CacheOffset);
404+
CachedUB& DstUB = GetUB(CacheOffset);
405+
406+
VERIFY(SrcUB.pInlineConstantData != nullptr, "Source inline constant data is null");
407+
VERIFY(DstUB.pInlineConstantData != nullptr, "Destination inline constant data is null");
408+
VERIFY(SrcUB.RangeSize == NumConstants * sizeof(Uint32), "Source inline constant buffer size mismatch");
409+
VERIFY(DstUB.RangeSize == NumConstants * sizeof(Uint32), "Destination inline constant buffer size mismatch");
410+
411+
memcpy(DstUB.pInlineConstantData,
412+
SrcUB.pInlineConstantData,
413+
NumConstants * sizeof(Uint32));
351414
}
352415

353416
#ifdef DILIGENT_DEBUG
@@ -397,6 +460,9 @@ class ShaderResourceCacheGL : public ShaderResourceCacheBase
397460
// Indicates what types of resources are stored in the cache
398461
const ResourceCacheContentType m_ContentType;
399462

463+
// Indicates whether this cache has inline constants
464+
bool m_HasInlineConstants = false;
465+
400466
#ifdef DILIGENT_DEVELOPMENT
401467
bool m_bStaticResourcesInitialized = false;
402468
#endif

Graphics/GraphicsEngineOpenGL/src/ShaderResourceCacheGL.cpp

Lines changed: 11 additions & 5 deletions
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");
@@ -35,7 +35,7 @@
3535
namespace Diligent
3636
{
3737

38-
size_t ShaderResourceCacheGL::GetRequiredMemorySize(const TResourceCount& ResCount)
38+
size_t ShaderResourceCacheGL::GetRequiredMemorySize(const TResourceCount& ResCount, Uint32 TotalInlineConstants)
3939
{
4040
static_assert(std::is_same<TResourceCount, PipelineResourceSignatureGLImpl::TBindings>::value,
4141
"ShaderResourceCacheGL::TResourceCount must be the same type as PipelineResourceSignatureGLImpl::TBindings");
@@ -46,14 +46,19 @@ size_t ShaderResourceCacheGL::GetRequiredMemorySize(const TResourceCount& ResCou
4646
sizeof(CachedResourceView) * ResCount[BINDING_RANGE_IMAGE] +
4747
sizeof(CachedSSBO) * ResCount[BINDING_RANGE_STORAGE_BUFFER];
4848
// clang-format on
49+
50+
// Add space for inline constant data at the tail
51+
MemSize += TotalInlineConstants * sizeof(Uint32);
52+
4953
VERIFY(MemSize < InvalidResourceOffset, "Memory size exceed the maximum allowed size.");
5054
return MemSize;
5155
}
5256

53-
void ShaderResourceCacheGL::Initialize(const TResourceCount& ResCount, IMemoryAllocator& MemAllocator, Uint64 DynamicUBOSlotMask, Uint64 DynamicSSBOSlotMask)
57+
void ShaderResourceCacheGL::Initialize(const TResourceCount& ResCount, IMemoryAllocator& MemAllocator, Uint64 DynamicUBOSlotMask, Uint64 DynamicSSBOSlotMask, Uint32 TotalInlineConstants)
5458
{
5559
m_DynamicUBOSlotMask = DynamicUBOSlotMask;
5660
m_DynamicSSBOSlotMask = DynamicSSBOSlotMask;
61+
m_HasInlineConstants = (TotalInlineConstants > 0);
5762

5863
VERIFY(!m_pResourceData, "Cache has already been initialized");
5964

@@ -69,9 +74,10 @@ void ShaderResourceCacheGL::Initialize(const TResourceCount& ResCount, IMemoryAl
6974
VERIFY_EXPR(GetSSBOCount() == static_cast<Uint32>(ResCount[BINDING_RANGE_STORAGE_BUFFER]));
7075
// clang-format on
7176

72-
size_t BufferSize = m_MemoryEndOffset;
77+
// Inline constant data tail is after m_MemoryEndOffset
78+
size_t BufferSize = m_MemoryEndOffset + TotalInlineConstants * sizeof(Uint32);
7379

74-
VERIFY_EXPR(BufferSize == GetRequiredMemorySize(ResCount));
80+
VERIFY_EXPR(BufferSize == GetRequiredMemorySize(ResCount, TotalInlineConstants));
7581

7682
if (BufferSize > 0)
7783
{

0 commit comments

Comments
 (0)