Skip to content

Commit 27b9a44

Browse files
committed
Created CGPUMeshPackerV2
1 parent 1296f28 commit 27b9a44

File tree

5 files changed

+217
-145
lines changed

5 files changed

+217
-145
lines changed

include/nbl/asset/CCPUMeshPackerV1.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,12 @@ class CCPUMeshPackerV1 final : public IMeshPacker<ICPUMeshBuffer, MDIStructType>
9595
}
9696
};
9797

98-
struct PackerDataStore
98+
struct PackerDataStore : base_t::template PackerDataStoreCommon<ICPUBuffer>
9999
{
100-
//or output should look more like `return_type` from geometry creator?
101-
//TODO: add parameters of the
102-
core::smart_refctd_ptr<ICPUBuffer> MDIDataBuffer;
103100
SBufferBinding<ICPUBuffer> vertexBufferBindings[SVertexInputParams::MAX_ATTR_BUF_BINDING_COUNT] = {};
104101
SBufferBinding<ICPUBuffer> indexBuffer;
105102

106103
SVertexInputParams vertexInputParams;
107-
108-
inline bool isValid()
109-
{
110-
return this->MDIDataBuffer->getPointer() != nullptr;
111-
}
112104
};
113105

114106
template <typename Iterator>
@@ -132,8 +124,8 @@ class CCPUMeshPackerV1 final : public IMeshPacker<ICPUMeshBuffer, MDIStructType>
132124
//needs to be called before first `commit`
133125
void instantiateDataStorage();
134126

135-
template <typename Iterator>
136-
IMeshPackerBase::PackedMeshBufferData commit(const Iterator begin, const Iterator end, ReservedAllocationMeshBuffers& ramb);
127+
template <typename MeshBufferIterator>
128+
IMeshPackerBase::PackedMeshBufferData commit(const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd, ReservedAllocationMeshBuffers& ramb);
137129

138130
inline PackerDataStore& getPackerDataStore() { return m_output; };
139131

@@ -195,10 +187,10 @@ CCPUMeshPackerV1<MDIStructType>::CCPUMeshPackerV1(const SVertexInputParams& preD
195187
m_perInstVtxSize = calcVertexSize(preDefinedLayout, E_VERTEX_INPUT_RATE::EVIR_PER_INSTANCE);
196188
if (m_perInstVtxSize)
197189
{
198-
m_perInsVtxBuffAlctrResSpc = _NBL_ALIGNED_MALLOC(core::GeneralpurposeAddressAllocator<uint32_t>::reserved_size(alignof(std::max_align_t), allocParams.perInstanceVertexBuffSupportedSize, allocParams.perInstanceVertexBufferMinAllocSize), _NBL_SIMD_ALIGNMENT);
190+
m_perInsVtxBuffAlctrResSpc = _NBL_ALIGNED_MALLOC(core::GeneralpurposeAddressAllocator<uint32_t>::reserved_size(alignof(std::max_align_t), allocParams.perInstanceVertexBuffSupportedSize / m_perInstVtxSize, allocParams.perInstanceVertexBufferMinAllocSize), _NBL_SIMD_ALIGNMENT);
199191
_NBL_DEBUG_BREAK_IF(m_perInsVtxBuffAlctrResSpc == nullptr);
200192
assert(m_perInsVtxBuffAlctrResSpc != nullptr);
201-
m_perInsVtxBuffAlctr = core::GeneralpurposeAddressAllocator<uint32_t>(m_perInsVtxBuffAlctrResSpc, 0u, 0u, alignof(std::max_align_t), allocParams.perInstanceVertexBuffSupportedSize, allocParams.perInstanceVertexBufferMinAllocSize);
193+
m_perInsVtxBuffAlctr = core::GeneralpurposeAddressAllocator<uint32_t>(m_perInsVtxBuffAlctrResSpc, 0u, 0u, alignof(std::max_align_t), allocParams.perInstanceVertexBuffSupportedSize / m_perInstVtxSize, allocParams.perInstanceVertexBufferMinAllocSize);
202194
}
203195

204196
initializeCommonAllocators(

include/nbl/asset/CCPUMeshPackerV2.h

Lines changed: 15 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -6,150 +6,44 @@
66
#define __NBL_ASSET_C_CPU_MESH_PACKER_V2_H_INCLUDED__
77

88
#include <nbl/asset/ICPUMesh.h>
9-
#include <nbl/asset/IMeshPacker.h>
9+
#include <nbl/asset/IMeshPackerV2.h>
1010

1111
namespace nbl
1212
{
1313
namespace asset
1414
{
1515

16-
// eventually I will rename it
1716
template <typename MDIStructType = DrawElementsIndirectCommand_t>
18-
class CCPUMeshPackerV2 final : public IMeshPacker<ICPUMeshBuffer, MDIStructType>
17+
class CCPUMeshPackerV2 final : public IMeshPackerV2<ICPUBuffer, ICPUMeshBuffer, MDIStructType>
1918
{
20-
using base_t = IMeshPacker<ICPUMeshBuffer, MDIStructType>;
19+
using base_t = IMeshPackerV2<ICPUBuffer, ICPUMeshBuffer, MDIStructType>;
2120
using Triangle = typename base_t::Triangle;
2221
using TriangleBatch = typename base_t::TriangleBatch;
2322

2423
public:
2524
using AllocationParams = IMeshPackerBase::AllocationParamsCommon;
26-
27-
struct AttribAllocParams
28-
{
29-
size_t offset = INVALID_ADDRESS;
30-
size_t size = 0ull;
31-
};
32-
33-
struct ReservedAllocationMeshBuffers
34-
{
35-
uint32_t mdiAllocationOffset;
36-
uint32_t mdiAllocationReservedSize;
37-
uint32_t indexAllocationOffset;
38-
uint32_t indexAllocationReservedSize;
39-
AttribAllocParams attribAllocParams[SVertexInputParams::MAX_VERTEX_ATTRIB_COUNT];
40-
41-
inline bool isValid()
42-
{
43-
return this->mdiAllocationOffset != core::GeneralpurposeAddressAllocator<uint32_t>::invalid_address;
44-
}
45-
};
46-
47-
template<typename BufferType>
48-
struct PackerDataStore
49-
{
50-
core::smart_refctd_ptr<BufferType> MDIDataBuffer;
51-
core::smart_refctd_ptr<BufferType> vertexBuffer;
52-
core::smart_refctd_ptr<BufferType> indexBuffer;
53-
54-
inline bool isValid()
55-
{
56-
return this->MDIDataBuffer->getPointer() != nullptr;
57-
}
58-
};
25+
using PackerDataStore = typename base_t::PackerDataStore;
26+
using ReservedAllocationMeshBuffers = typename base_t::ReservedAllocationMeshBuffers;
27+
using AttribAllocParams = typename base_t::AttribAllocParams;
5928

6029
public:
61-
CCPUMeshPackerV2(const AllocationParams& allocParams, uint16_t minTriangleCountPerMDIData = 256u, uint16_t maxTriangleCountPerMDIData = 1024u);
62-
63-
template <typename Iterator>
64-
bool alloc(ReservedAllocationMeshBuffers* rambOut, const Iterator begin, const Iterator end);
30+
CCPUMeshPackerV2(const AllocationParams& allocParams, uint16_t minTriangleCountPerMDIData = 256u, uint16_t maxTriangleCountPerMDIData = 1024u)
31+
:IMeshPackerV2<ICPUBuffer, ICPUMeshBuffer, MDIStructType>(allocParams, minTriangleCountPerMDIData, maxTriangleCountPerMDIData)
32+
{}
6533

66-
void instantiateDataStorage();
34+
template <typename MeshBufferIterator>
35+
bool commit(IMeshPackerBase::PackedMeshBufferData* pmbdOut, ReservedAllocationMeshBuffers* rambIn, const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd);
6736

68-
template <typename Iterator>
69-
bool commit(IMeshPackerBase::PackedMeshBufferData* pmbdOut, ReservedAllocationMeshBuffers* rambIn, const Iterator begin, const Iterator end);
70-
71-
PackerDataStore<video::IGPUBuffer> createGPUPackerDataStore(video::IVideoDriver* driver);
72-
inline PackerDataStore<ICPUBuffer> getCPUPackerDataStore() { return m_packerDataStore; };
73-
74-
private:
75-
76-
PackerDataStore<ICPUBuffer> m_packerDataStore;
77-
78-
const AllocationParams m_allocParams;
37+
inline PackerDataStore getPackerDataStore() { return m_packerDataStore; };
7938

8039
};
8140

8241
template <typename MDIStructType>
83-
CCPUMeshPackerV2<MDIStructType>::CCPUMeshPackerV2(const AllocationParams& allocParams, uint16_t minTriangleCountPerMDIData, uint16_t maxTriangleCountPerMDIData)
84-
:IMeshPacker<ICPUMeshBuffer, MDIStructType>(minTriangleCountPerMDIData, maxTriangleCountPerMDIData),
85-
m_allocParams(allocParams)
86-
{
87-
initializeCommonAllocators(allocParams);
88-
}
89-
90-
template <typename MDIStructType>
91-
template <typename Iterator>
92-
bool CCPUMeshPackerV2<MDIStructType>::alloc(ReservedAllocationMeshBuffers* rambOut, const Iterator begin, const Iterator end)
42+
template <typename MeshBufferIterator>
43+
bool CCPUMeshPackerV2<MDIStructType>::commit(IMeshPackerBase::PackedMeshBufferData* pmbdOut, ReservedAllocationMeshBuffers* rambIn, const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd)
9344
{
9445
size_t i = 0ull;
95-
for (auto it = begin; it != end; it++)
96-
{
97-
ReservedAllocationMeshBuffers& ramb = *(rambOut + i);
98-
const size_t vtxCnt = (*it)->calcVertexCount();
99-
const size_t idxCnt = (*it)->getIndexCount();
100-
101-
//allocate indices
102-
ramb.indexAllocationOffset = m_idxBuffAlctr.alloc_addr(idxCnt, 2u);
103-
if (ramb.indexAllocationOffset == INVALID_ADDRESS)
104-
return false;
105-
ramb.indexAllocationReservedSize = idxCnt * 2;
106-
107-
//allocate vertices
108-
const auto& mbVtxInputParams = (*it)->getPipeline()->getVertexInputParams();
109-
for (uint16_t attrBit = 0x0001, location = 0; location < SVertexInputParams::MAX_ATTR_BUF_BINDING_COUNT; attrBit <<= 1, location++)
110-
{
111-
if (!(attrBit & mbVtxInputParams.enabledAttribFlags))
112-
continue;
113-
114-
const uint32_t attribSize = asset::getTexelOrBlockBytesize(static_cast<E_FORMAT>(mbVtxInputParams.attributes[location].format));
115-
116-
ramb.attribAllocParams[location].offset = m_vtxBuffAlctr.alloc_addr(vtxCnt * attribSize, attribSize);
117-
118-
if (ramb.attribAllocParams[location].offset == INVALID_ADDRESS)
119-
return false;
120-
121-
ramb.attribAllocParams[location].size = vtxCnt * attribSize;
122-
}
123-
124-
//allocate MDI structs
125-
const uint32_t minIdxCntPerPatch = m_minTriangleCountPerMDIData * 3;
126-
size_t possibleMDIStructsNeededCnt = (idxCnt + minIdxCntPerPatch - 1) / minIdxCntPerPatch;
127-
128-
ramb.mdiAllocationOffset = m_MDIDataAlctr.alloc_addr(possibleMDIStructsNeededCnt, 1u);
129-
if (ramb.mdiAllocationOffset == INVALID_ADDRESS)
130-
return false;
131-
ramb.mdiAllocationReservedSize = possibleMDIStructsNeededCnt;
132-
133-
i++;
134-
}
135-
136-
return true;
137-
}
138-
139-
template <typename MDIStructType>
140-
void CCPUMeshPackerV2<MDIStructType>::instantiateDataStorage()
141-
{
142-
m_packerDataStore.MDIDataBuffer = core::make_smart_refctd_ptr<ICPUBuffer>(m_allocParams.MDIDataBuffSupportedCnt * sizeof(MDIStructType));
143-
m_packerDataStore.indexBuffer = core::make_smart_refctd_ptr<ICPUBuffer>(m_allocParams.indexBuffSupportedCnt * sizeof(uint16_t));
144-
m_packerDataStore.vertexBuffer = core::make_smart_refctd_ptr<ICPUBuffer>(m_allocParams.vertexBuffSupportedSize);
145-
}
146-
147-
template <typename MDIStructType>
148-
template <typename Iterator>
149-
bool CCPUMeshPackerV2<MDIStructType>::commit(IMeshPackerBase::PackedMeshBufferData* pmbdOut, ReservedAllocationMeshBuffers* rambIn, const Iterator begin, const Iterator end)
150-
{
151-
size_t i = 0ull;
152-
for (auto it = begin; it != end; it++)
46+
for (auto it = mbBegin; it != mbEnd; it++)
15347
{
15448
ReservedAllocationMeshBuffers& ramb = *(rambIn + i);
15549
IMeshPackerBase::PackedMeshBufferData& pmbd = *(pmbdOut + i);
@@ -202,17 +96,6 @@ bool CCPUMeshPackerV2<MDIStructType>::commit(IMeshPackerBase::PackedMeshBufferDa
20296
return true;
20397
}
20498

205-
template <typename MDIStructType>
206-
typename CCPUMeshPackerV2<MDIStructType>::PackerDataStore<video::IGPUBuffer> CCPUMeshPackerV2<MDIStructType>::createGPUPackerDataStore(video::IVideoDriver* driver)
207-
{
208-
PackerDataStore<video::IGPUBuffer> m_output;
209-
m_output.MDIDataBuffer = driver->createFilledDeviceLocalGPUBufferOnDedMem(m_packerDataStore.MDIDataBuffer->getSize(), m_packerDataStore.MDIDataBuffer->getPointer());
210-
m_output.vertexBuffer = driver->createFilledDeviceLocalGPUBufferOnDedMem(m_packerDataStore.vertexBuffer->getSize(), m_packerDataStore.vertexBuffer->getPointer());
211-
m_output.indexBuffer = driver->createFilledDeviceLocalGPUBufferOnDedMem(m_packerDataStore.indexBuffer->getSize(), m_packerDataStore.indexBuffer->getPointer());
212-
213-
return m_output;
214-
}
215-
21699
}
217100
}
218101

include/nbl/asset/IMeshPacker.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,19 @@ class IMeshPacker : public IMeshPackerBase
246246
}
247247

248248
protected:
249+
template <typename BufferType>
250+
struct PackerDataStoreCommon
251+
{
252+
static_assert(std::is_base_of<core::IBuffer, BufferType>::value);
253+
254+
core::smart_refctd_ptr<BufferType> MDIDataBuffer;
255+
256+
inline bool isValid()
257+
{
258+
return this->MDIDataBuffer->getPointer() != nullptr;
259+
}
260+
};
261+
249262
_NBL_STATIC_INLINE_CONSTEXPR uint32_t INVALID_ADDRESS = core::GeneralpurposeAddressAllocator<uint32_t>::invalid_address;
250263

251264
};

include/nbl/asset/IMeshPackerV2.h

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
5+
#ifndef __NBL_ASSET_I_MESH_PACKER_V2_H_INCLUDED__
6+
#define __NBL_ASSET_I_MESH_PACKER_V2_H_INCLUDED__
7+
8+
#include <nbl/asset/IMeshPacker.h>
9+
10+
namespace nbl
11+
{
12+
namespace asset
13+
{
14+
15+
template <typename BufferType, typename MeshBufferType, typename MDIStructType = DrawElementsIndirectCommand_t>
16+
class IMeshPackerV2 : public IMeshPacker<MeshBufferType, MDIStructType>
17+
{
18+
using base_t = IMeshPacker<MeshBufferType, MDIStructType>;
19+
using AllocationParams = IMeshPackerBase::AllocationParamsCommon;
20+
21+
public:
22+
struct AttribAllocParams
23+
{
24+
size_t offset = INVALID_ADDRESS;
25+
size_t size = 0ull;
26+
};
27+
28+
struct ReservedAllocationMeshBuffers
29+
{
30+
uint32_t mdiAllocationOffset;
31+
uint32_t mdiAllocationReservedSize;
32+
uint32_t indexAllocationOffset;
33+
uint32_t indexAllocationReservedSize;
34+
AttribAllocParams attribAllocParams[SVertexInputParams::MAX_VERTEX_ATTRIB_COUNT];
35+
36+
inline bool isValid()
37+
{
38+
return this->mdiAllocationOffset != core::GeneralpurposeAddressAllocator<uint32_t>::invalid_address;
39+
}
40+
};
41+
42+
struct PackerDataStore : base_t::template PackerDataStoreCommon<BufferType>
43+
{
44+
core::smart_refctd_ptr<BufferType> vertexBuffer;
45+
core::smart_refctd_ptr<BufferType> indexBuffer;
46+
};
47+
48+
protected:
49+
IMeshPackerV2(const AllocationParams& allocParams, uint16_t minTriangleCountPerMDIData, uint16_t maxTriangleCountPerMDIData)
50+
:base_t(minTriangleCountPerMDIData, maxTriangleCountPerMDIData),
51+
m_allocParams(allocParams)
52+
{
53+
initializeCommonAllocators(allocParams);
54+
};
55+
56+
public:
57+
template <typename MeshBufferIterator>
58+
bool alloc(ReservedAllocationMeshBuffers* rambOut, const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd);
59+
60+
void instantiateDataStorage();
61+
62+
inline PackerDataStore getPackerDataStore() { return m_packerDataStore; };
63+
64+
protected:
65+
PackerDataStore m_packerDataStore;
66+
const AllocationParams m_allocParams;
67+
68+
};
69+
70+
template <typename MeshBufferType, typename BufferType, typename MDIStructType>
71+
template <typename MeshBufferIterator>
72+
bool IMeshPackerV2<MeshBufferType, BufferType, MDIStructType>::alloc(ReservedAllocationMeshBuffers* rambOut, const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd)
73+
{
74+
size_t i = 0ull;
75+
for (auto it = mbBegin; it != mbEnd; it++)
76+
{
77+
ReservedAllocationMeshBuffers& ramb = *(rambOut + i);
78+
const size_t vtxCnt = (*it)->calcVertexCount();
79+
const size_t idxCnt = (*it)->getIndexCount();
80+
81+
//allocate indices
82+
ramb.indexAllocationOffset = m_idxBuffAlctr.alloc_addr(idxCnt, 2u);
83+
if (ramb.indexAllocationOffset == INVALID_ADDRESS)
84+
return false;
85+
ramb.indexAllocationReservedSize = idxCnt * 2;
86+
87+
//allocate vertices
88+
const auto& mbVtxInputParams = (*it)->getPipeline()->getVertexInputParams();
89+
for (uint16_t attrBit = 0x0001, location = 0; location < SVertexInputParams::MAX_ATTR_BUF_BINDING_COUNT; attrBit <<= 1, location++)
90+
{
91+
if (!(attrBit & mbVtxInputParams.enabledAttribFlags))
92+
continue;
93+
94+
const uint32_t attribSize = asset::getTexelOrBlockBytesize(static_cast<E_FORMAT>(mbVtxInputParams.attributes[location].format));
95+
96+
ramb.attribAllocParams[location].offset = m_vtxBuffAlctr.alloc_addr(vtxCnt * attribSize, attribSize);
97+
98+
if (ramb.attribAllocParams[location].offset == INVALID_ADDRESS)
99+
return false;
100+
101+
ramb.attribAllocParams[location].size = vtxCnt * attribSize;
102+
}
103+
104+
//allocate MDI structs
105+
const uint32_t minIdxCntPerPatch = m_minTriangleCountPerMDIData * 3;
106+
size_t possibleMDIStructsNeededCnt = (idxCnt + minIdxCntPerPatch - 1) / minIdxCntPerPatch;
107+
108+
ramb.mdiAllocationOffset = m_MDIDataAlctr.alloc_addr(possibleMDIStructsNeededCnt, 1u);
109+
if (ramb.mdiAllocationOffset == INVALID_ADDRESS)
110+
return false;
111+
ramb.mdiAllocationReservedSize = possibleMDIStructsNeededCnt;
112+
113+
i++;
114+
}
115+
116+
return true;
117+
}
118+
119+
template <typename BufferType, typename MeshBufferType, typename MDIStructType>
120+
void IMeshPackerV2<BufferType, MeshBufferType, MDIStructType>::instantiateDataStorage()
121+
{
122+
m_packerDataStore.MDIDataBuffer = core::make_smart_refctd_ptr<BufferType>(m_allocParams.MDIDataBuffSupportedCnt * sizeof(MDIStructType));
123+
m_packerDataStore.indexBuffer = core::make_smart_refctd_ptr<BufferType>(m_allocParams.indexBuffSupportedCnt * sizeof(uint16_t));
124+
m_packerDataStore.vertexBuffer = core::make_smart_refctd_ptr<BufferType>(m_allocParams.vertexBuffSupportedSize);
125+
}
126+
127+
}
128+
}
129+
130+
#endif

0 commit comments

Comments
 (0)