Skip to content

Commit e6144b8

Browse files
Fixed mapped buffer validation in device context
1 parent 08ef2ea commit e6144b8

3 files changed

Lines changed: 31 additions & 15 deletions

File tree

Graphics/GraphicsEngine/include/DeviceContextBase.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,9 @@ class DeviceContextBase : public ObjectBase<typename EngineImplTraits::DeviceCon
716716
struct DbgMappedBufferInfo
717717
{
718718
MAP_TYPE MapType;
719+
USAGE Usage;
720+
721+
RefCntWeakPtr<IBuffer> pBuffer;
719722
};
720723
std::unordered_map<IBuffer*, DbgMappedBufferInfo> m_DbgMappedBuffers;
721724
#endif
@@ -1799,7 +1802,7 @@ inline void DeviceContextBase<ImplementationTraits>::MapBuffer(
17991802
#ifdef DILIGENT_DEBUG
18001803
{
18011804
VERIFY(m_DbgMappedBuffers.find(pBuffer) == m_DbgMappedBuffers.end(), "Buffer '", BuffDesc.Name, "' has already been mapped");
1802-
m_DbgMappedBuffers[pBuffer] = DbgMappedBufferInfo{MapType};
1805+
m_DbgMappedBuffers[pBuffer] = DbgMappedBufferInfo{MapType, BuffDesc.Usage, RefCntWeakPtr<IBuffer>{pBuffer}};
18031806
}
18041807
#endif
18051808

@@ -1852,9 +1855,16 @@ inline void DeviceContextBase<ImplementationTraits>::UnmapBuffer(IBuffer* pBuffe
18521855
#ifdef DILIGENT_DEBUG
18531856
{
18541857
auto MappedBufferIt = m_DbgMappedBuffers.find(pBuffer);
1855-
VERIFY(MappedBufferIt != m_DbgMappedBuffers.end(), "Buffer '", pBuffer->GetDesc().Name, "' has not been mapped.");
1856-
VERIFY(MappedBufferIt->second.MapType == MapType, "MapType (", MapType, ") does not match the map type that was used to map the buffer ", MappedBufferIt->second.MapType);
1857-
m_DbgMappedBuffers.erase(MappedBufferIt);
1858+
if (MappedBufferIt != m_DbgMappedBuffers.end())
1859+
{
1860+
VERIFY(MappedBufferIt->second.MapType == MapType, "MapType (", MapType,
1861+
") does not match the map type that was used to map the buffer ", MappedBufferIt->second.MapType);
1862+
m_DbgMappedBuffers.erase(MappedBufferIt);
1863+
}
1864+
else
1865+
{
1866+
UNEXPECTED("Buffer '", pBuffer->GetDesc().Name, "' has not been mapped.");
1867+
}
18581868
}
18591869
#endif
18601870
}

Graphics/GraphicsEngineD3D12/src/DeviceContextD3D12Impl.cpp

Lines changed: 10 additions & 7 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");
@@ -1105,14 +1105,17 @@ void DeviceContextD3D12Impl::Flush()
11051105
void DeviceContextD3D12Impl::FinishFrame()
11061106
{
11071107
#ifdef DILIGENT_DEBUG
1108-
for (const auto& MappedBuffIt : m_DbgMappedBuffers)
1108+
for (auto& MappedBuffIt : m_DbgMappedBuffers)
11091109
{
1110-
const BufferDesc& BuffDesc = MappedBuffIt.first->GetDesc();
1111-
if (BuffDesc.Usage == USAGE_DYNAMIC)
1110+
if (MappedBuffIt.second.Usage == USAGE_DYNAMIC)
11121111
{
1113-
LOG_WARNING_MESSAGE("Dynamic buffer '", BuffDesc.Name,
1114-
"' is still mapped when finishing the frame. The contents of the buffer and "
1115-
"mapped address will become invalid");
1112+
if (RefCntAutoPtr<IBuffer> pBuffer = MappedBuffIt.second.pBuffer.Lock())
1113+
{
1114+
const BufferDesc& BuffDesc = pBuffer->GetDesc();
1115+
LOG_WARNING_MESSAGE("Dynamic buffer '", BuffDesc.Name,
1116+
"' is still mapped when finishing the frame. The contents of the buffer and "
1117+
"mapped address will become invalid");
1118+
}
11161119
}
11171120
}
11181121
#endif

Graphics/GraphicsEngineVulkan/src/DeviceContextVkImpl.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,12 +1480,15 @@ void DeviceContextVkImpl::ClearRenderTarget(ITextureView* pView, const void* RGB
14801480
void DeviceContextVkImpl::FinishFrame()
14811481
{
14821482
#ifdef DILIGENT_DEBUG
1483-
for (const auto& MappedBuffIt : m_DbgMappedBuffers)
1483+
for (auto& MappedBuffIt : m_DbgMappedBuffers)
14841484
{
1485-
const BufferDesc& BuffDesc = MappedBuffIt.first->GetDesc();
1486-
if (BuffDesc.Usage == USAGE_DYNAMIC)
1485+
if (MappedBuffIt.second.Usage == USAGE_DYNAMIC)
14871486
{
1488-
LOG_WARNING_MESSAGE("Dynamic buffer '", BuffDesc.Name, "' is still mapped when finishing the frame. The contents of the buffer and mapped address will become invalid");
1487+
if (RefCntAutoPtr<IBuffer> pBuffer = MappedBuffIt.second.pBuffer.Lock())
1488+
{
1489+
const BufferDesc& BuffDesc = pBuffer->GetDesc();
1490+
LOG_WARNING_MESSAGE("Dynamic buffer '", BuffDesc.Name, "' is still mapped when finishing the frame. The contents of the buffer and mapped address will become invalid");
1491+
}
14891492
}
14901493
}
14911494
#endif

0 commit comments

Comments
 (0)