Skip to content

Commit 63c2bba

Browse files
committed
Finish Step 6): Add commit path in DeviceContextGLImpl (graphics + compute)
1 parent 4f7ffd7 commit 63c2bba

4 files changed

Lines changed: 79 additions & 13 deletions

File tree

Graphics/GraphicsEngineOpenGL/include/DeviceContextGLImpl.hpp

Lines changed: 2 additions & 2 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");
@@ -325,7 +325,7 @@ class DeviceContextGLImpl final : public DeviceContextBase<EngineGLImplTraits>
325325
__forceinline void PostDraw();
326326

327327
using TBindings = PipelineResourceSignatureGLImpl::TBindings;
328-
void BindProgramResources(Uint32 BindSRBMask);
328+
void BindProgramResources(Uint32 BindSRBMask, bool DynamicBuffersIntact = false, bool InlineConstantsIntact = false);
329329

330330
#ifdef DILIGENT_DEVELOPMENT
331331
void DvpValidateCommittedShaderResources();

Graphics/GraphicsEngineOpenGL/include/PipelineResourceSignatureGLImpl.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ class PipelineResourceSignatureGLImpl final : public PipelineResourceSignatureBa
141141
// Make the base class method visible
142142
using TPipelineResourceSignatureBase::CopyStaticResources;
143143

144+
// Updates inline constant buffers by mapping the shared dynamic UBOs and copying
145+
// data from the CPU-side staging buffer in the resource cache.
146+
void UpdateInlineConstantBuffers(const ShaderResourceCacheGL& ResourceCache, class GLContextState& CtxState) const;
147+
144148
bool HasInlineConstants() const
145149
{
146150
return m_NumInlineConstantBuffers != 0;

Graphics/GraphicsEngineOpenGL/src/DeviceContextGLImpl.cpp

Lines changed: 52 additions & 11 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");
@@ -698,7 +698,7 @@ void DeviceContextGLImpl::DvpValidateCommittedShaderResources()
698698
}
699699
#endif
700700

701-
void DeviceContextGLImpl::BindProgramResources(Uint32 BindSRBMask)
701+
void DeviceContextGLImpl::BindProgramResources(Uint32 BindSRBMask, bool DynamicBuffersIntact, bool InlineConstantsIntact)
702702
{
703703
VERIFY_EXPR(BindSRBMask != 0);
704704
//if (m_CommittedResourcesTentativeBarriers != 0)
@@ -721,16 +721,55 @@ void DeviceContextGLImpl::BindProgramResources(Uint32 BindSRBMask)
721721

722722
const ShaderResourceCacheImplType* pResourceCache = m_BindInfo.ResourceCaches[sign];
723723
DEV_CHECK_ERR(pResourceCache != nullptr, "Resource cache at index ", sign, " is null");
724-
if (m_BindInfo.StaleSRBMask & SignBit)
724+
725+
const bool SRBStale = (m_BindInfo.StaleSRBMask & SignBit) != 0;
726+
if (SRBStale)
727+
{
725728
pResourceCache->BindResources(GetContextState(), BaseBindings, m_BoundWritableTextures, m_BoundWritableBuffers);
729+
}
726730
else
727731
{
728-
VERIFY((m_BindInfo.DynamicSRBMask & SignBit) != 0,
729-
"When bit in StaleSRBMask is not set, the same bit in DynamicSRBMask must be set. Check GetCommitMask().");
730-
DEV_CHECK_ERR(pResourceCache->HasDynamicResources(),
731-
"Bit in DynamicSRBMask is set, but the cache does not contain dynamic resources. This may indicate that resources "
732-
"in the cache have changed, but the SRB has not been committed before the draw/dispatch command.");
733-
pResourceCache->BindDynamicBuffers(GetContextState(), BaseBindings);
732+
VERIFY(((m_BindInfo.DynamicSRBMask | m_BindInfo.InlineConstantsSRBMask) & SignBit) != 0,
733+
"When bit in StaleSRBMask is not set, the same bit in either DynamicSRBMask or InlineConstantsSRBMask must be set. Check GetCommitMask().");
734+
735+
if ((m_BindInfo.DynamicSRBMask & SignBit) != 0)
736+
{
737+
DEV_CHECK_ERR(pResourceCache->HasDynamicResources(),
738+
"Bit in DynamicSRBMask is set, but the cache does not contain dynamic resources. This may indicate that resources "
739+
"in the cache have changed, but the SRB has not been committed before the draw/dispatch command.");
740+
if (!DynamicBuffersIntact)
741+
{
742+
pResourceCache->BindDynamicBuffers(GetContextState(), BaseBindings);
743+
}
744+
}
745+
}
746+
747+
// Update inline constant buffers if needed
748+
if ((m_BindInfo.InlineConstantsSRBMask & SignBit) != 0)
749+
{
750+
VERIFY(pResourceCache->HasInlineConstants(),
751+
"Shader resource cache does not contain inline constants, but the corresponding bit in InlineConstantsSRBMask is set. "
752+
"This may be a bug because inline constants flag in the cache never changes after SRB creation, "
753+
"while m_BindInfo.InlineConstantsSRBMask is initialized when SRB is committed.");
754+
// Always update inline constant buffers if the SRB is stale
755+
if (SRBStale || !InlineConstantsIntact)
756+
{
757+
if (PipelineResourceSignatureGLImpl* pSign = m_pPipelineState->GetResourceSignature(sign))
758+
{
759+
pSign->UpdateInlineConstantBuffers(*pResourceCache, GetContextState());
760+
}
761+
else
762+
{
763+
UNEXPECTED("Pipeline resource signature is null for signature index ", sign);
764+
}
765+
}
766+
}
767+
else
768+
{
769+
VERIFY(!pResourceCache->HasInlineConstants(),
770+
"Shader resource cache contains inline constants, but the corresponding bit in InlineConstantsSRBMask is not set. "
771+
"This may be a bug because inline constants flag in the cache never changes after SRB creation, "
772+
"while m_BindInfo.InlineConstantsSRBMask is initialized when SRB is committed.");
734773
}
735774
}
736775
m_BindInfo.StaleSRBMask &= ~m_BindInfo.ActiveSRBMask;
@@ -797,9 +836,11 @@ void DeviceContextGLImpl::PrepareForDraw(DRAW_FLAGS Flags, bool IsIndexed, GLenu
797836
// The program might have changed since the last SetPipelineState call if a shader was
798837
// created after the call (ShaderResourcesGL needs to bind a program to load uniforms).
799838
m_pPipelineState->CommitProgram(m_ContextState);
800-
if (Uint32 BindSRBMask = m_BindInfo.GetCommitMask(Flags & DRAW_FLAG_DYNAMIC_RESOURCE_BUFFERS_INTACT, Flags & DRAW_FLAG_INLINE_CONSTANTS_INTACT))
839+
const bool DynamicBuffersIntact = (Flags & DRAW_FLAG_DYNAMIC_RESOURCE_BUFFERS_INTACT) != 0;
840+
const bool InlineConstantsIntact = (Flags & DRAW_FLAG_INLINE_CONSTANTS_INTACT) != 0;
841+
if (Uint32 BindSRBMask = m_BindInfo.GetCommitMask(DynamicBuffersIntact, InlineConstantsIntact))
801842
{
802-
BindProgramResources(BindSRBMask);
843+
BindProgramResources(BindSRBMask, DynamicBuffersIntact, InlineConstantsIntact);
803844
}
804845

805846
#ifdef DILIGENT_DEVELOPMENT

Graphics/GraphicsEngineOpenGL/src/PipelineResourceSignatureGLImpl.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include <functional>
3434

3535
#include "RenderDeviceGLImpl.hpp"
36+
#include "BufferGLImpl.hpp"
37+
#include "GLContextState.hpp"
3638

3739
namespace Diligent
3840
{
@@ -576,6 +578,25 @@ void PipelineResourceSignatureGLImpl::CopyStaticResources(ShaderResourceCacheGL&
576578
#endif
577579
}
578580

581+
void PipelineResourceSignatureGLImpl::UpdateInlineConstantBuffers(const ShaderResourceCacheGL& ResourceCache, GLContextState& CtxState) const
582+
{
583+
for (Uint32 i = 0; i < m_NumInlineConstantBuffers; ++i)
584+
{
585+
const InlineConstantBufferAttribsGL& InlineCBAttr = GetInlineConstantBuffer(i);
586+
VERIFY_EXPR(InlineCBAttr.pBuffer);
587+
588+
const ShaderResourceCacheGL::CachedUB& InlineCB = ResourceCache.GetConstUB(InlineCBAttr.CacheOffset);
589+
VERIFY(InlineCBAttr.NumConstants * sizeof(Uint32) == InlineCB.RangeSize, "Inline constant buffer size mismatch");
590+
VERIFY(InlineCB.pInlineConstantData != nullptr, "Inline constant data pointer is null");
591+
592+
// Map the shared buffer and copy the data
593+
PVoid pMappedData = nullptr;
594+
InlineCBAttr.pBuffer->Map(CtxState, MAP_WRITE, MAP_FLAG_DISCARD, pMappedData);
595+
memcpy(pMappedData, InlineCB.pInlineConstantData, InlineCBAttr.NumConstants * sizeof(Uint32));
596+
InlineCBAttr.pBuffer->Unmap(CtxState);
597+
}
598+
}
599+
579600
void PipelineResourceSignatureGLImpl::InitSRBResourceCache(ShaderResourceCacheGL& ResourceCache)
580601
{
581602
ResourceCache.Initialize(m_BindingCount, m_SRBMemAllocator.GetResourceCacheDataAllocator(0), m_DynamicUBOMask, m_DynamicSSBOMask, m_TotalInlineConstants);

0 commit comments

Comments
 (0)