Skip to content

Commit 3d5f697

Browse files
WebGPU: Replaced std::vector<Uint8> with std::unique_ptr<Uint8[]> in upload and dynamic memory managers to avoid zero-initialization overhead
1 parent 54b3d6d commit 3d5f697

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

Graphics/GraphicsEngineWebGPU/include/DynamicMemoryManagerWebGPU.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
/// Declaration of Diligent::DynamicMemoryManagerWebGPU class
3131

3232
#include <mutex>
33+
#include <memory>
3334
#include <vector>
3435

3536
#include "WebGPUObjectWrappers.hpp"
@@ -111,9 +112,9 @@ class DynamicMemoryManagerWebGPU
111112
size_t m_CurrentOffset = 0;
112113
WebGPUBufferWrapper m_wgpuBuffer;
113114

114-
std::mutex m_AvailablePagesMtx;
115-
std::vector<Page> m_AvailablePages;
116-
std::vector<Uint8> m_MappedData;
115+
std::mutex m_AvailablePagesMtx;
116+
std::vector<Page> m_AvailablePages;
117+
std::unique_ptr<Uint8[]> m_MappedData;
117118
};
118119

119120
} // namespace Diligent

Graphics/GraphicsEngineWebGPU/include/UploadMemoryManagerWebGPU.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
/// Declaration of Diligent::UploadMemoryManagerWebGPU class
3131

3232
#include <mutex>
33-
#include <vector>
33+
#include <memory>
3434
#include <atomic>
35+
#include <vector>
3536

3637
#include "WebGPUObjectWrappers.hpp"
3738
#include "BasicTypes.h"
@@ -83,13 +84,14 @@ class UploadMemoryManagerWebGPU
8384

8485
size_t GetSize() const
8586
{
86-
return m_Data.size();
87+
return m_DataSize;
8788
}
8889

8990
private:
9091
UploadMemoryManagerWebGPU* m_pMgr = nullptr;
9192
WebGPUBufferWrapper m_wgpuBuffer;
92-
std::vector<Uint8> m_Data;
93+
std::unique_ptr<Uint8[]> m_Data;
94+
size_t m_DataSize = 0;
9395
size_t m_CurrOffset = 0;
9496
};
9597

Graphics/GraphicsEngineWebGPU/src/DynamicMemoryManagerWebGPU.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ DynamicMemoryManagerWebGPU::DynamicMemoryManagerWebGPU(WGPUDevice wgpuDevice, si
133133
WGPUBufferUsage_Index |
134134
WGPUBufferUsage_Indirect;
135135
m_wgpuBuffer.Reset(wgpuDeviceCreateBuffer(wgpuDevice, &wgpuBufferDesc));
136-
m_MappedData.resize(BufferSize);
136+
m_MappedData.reset(new Uint8[BufferSize]);
137137

138138
LOG_INFO_MESSAGE("Created dynamic buffer: ", BufferSize >> 10, " KB");
139139
}

Graphics/GraphicsEngineWebGPU/src/UploadMemoryManagerWebGPU.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ namespace Diligent
3636

3737
UploadMemoryManagerWebGPU::Page::Page(UploadMemoryManagerWebGPU& Mgr, size_t Size) :
3838
m_pMgr{&Mgr},
39-
m_Data(Size)
39+
m_Data{new Uint8[Size]},
40+
m_DataSize{Size}
4041
{
4142
WGPUBufferDescriptor wgpuBufferDesc{};
4243
wgpuBufferDesc.label = GetWGPUStringView("Upload memory page");
@@ -58,10 +59,13 @@ UploadMemoryManagerWebGPU::Page::Page(Page&& RHS) noexcept :
5859
m_pMgr{RHS.m_pMgr},
5960
m_wgpuBuffer{std::move(RHS.m_wgpuBuffer)},
6061
m_Data{std::move(RHS.m_Data)},
62+
m_DataSize{RHS.m_DataSize},
6163
m_CurrOffset{RHS.m_CurrOffset}
6264
// clang-format on
6365
{
64-
RHS = Page{};
66+
RHS.m_pMgr = nullptr;
67+
RHS.m_DataSize = 0;
68+
RHS.m_CurrOffset = 0;
6569
}
6670

6771
UploadMemoryManagerWebGPU::Page& UploadMemoryManagerWebGPU::Page::operator=(Page&& RHS) noexcept
@@ -72,9 +76,11 @@ UploadMemoryManagerWebGPU::Page& UploadMemoryManagerWebGPU::Page::operator=(Page
7276
m_pMgr = RHS.m_pMgr;
7377
m_wgpuBuffer = std::move(RHS.m_wgpuBuffer);
7478
m_Data = std::move(RHS.m_Data);
79+
m_DataSize = RHS.m_DataSize;
7580
m_CurrOffset = RHS.m_CurrOffset;
7681

7782
RHS.m_pMgr = nullptr;
83+
RHS.m_DataSize = 0;
7884
RHS.m_CurrOffset = 0;
7985

8086
return *this;
@@ -91,7 +97,7 @@ UploadMemoryManagerWebGPU::Allocation UploadMemoryManagerWebGPU::Page::Allocate(
9197
Allocation Alloc;
9298
Alloc.Offset = AlignUp(m_CurrOffset, Alignment);
9399
Alloc.Size = AlignUp(Size, Alignment);
94-
if (Alloc.Offset + Alloc.Size <= m_Data.size())
100+
if (Alloc.Offset + Alloc.Size <= m_DataSize)
95101
{
96102
Alloc.wgpuBuffer = m_wgpuBuffer;
97103
Alloc.pData = &m_Data[Alloc.Offset];
@@ -105,7 +111,7 @@ void UploadMemoryManagerWebGPU::Page::FlushWrites(WGPUQueue wgpuQueue)
105111
{
106112
if (m_CurrOffset > 0)
107113
{
108-
wgpuQueueWriteBuffer(wgpuQueue, m_wgpuBuffer, 0, m_Data.data(), m_CurrOffset);
114+
wgpuQueueWriteBuffer(wgpuQueue, m_wgpuBuffer, 0, m_Data.get(), m_CurrOffset);
109115
}
110116
}
111117

0 commit comments

Comments
 (0)