|
6 | 6 | #define __NBL_ASSET_C_CPU_MESH_PACKER_V2_H_INCLUDED__
|
7 | 7 |
|
8 | 8 | #include <nbl/asset/ICPUMesh.h>
|
9 |
| -#include <nbl/asset/IMeshPacker.h> |
| 9 | +#include <nbl/asset/IMeshPackerV2.h> |
10 | 10 |
|
11 | 11 | namespace nbl
|
12 | 12 | {
|
13 | 13 | namespace asset
|
14 | 14 | {
|
15 | 15 |
|
16 |
| -// eventually I will rename it |
17 | 16 | template <typename MDIStructType = DrawElementsIndirectCommand_t>
|
18 |
| -class CCPUMeshPackerV2 final : public IMeshPacker<ICPUMeshBuffer, MDIStructType> |
| 17 | +class CCPUMeshPackerV2 final : public IMeshPackerV2<ICPUBuffer, ICPUMeshBuffer, MDIStructType> |
19 | 18 | {
|
20 |
| - using base_t = IMeshPacker<ICPUMeshBuffer, MDIStructType>; |
| 19 | + using base_t = IMeshPackerV2<ICPUBuffer, ICPUMeshBuffer, MDIStructType>; |
21 | 20 | using Triangle = typename base_t::Triangle;
|
22 | 21 | using TriangleBatch = typename base_t::TriangleBatch;
|
23 | 22 |
|
24 | 23 | public:
|
25 | 24 | 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; |
59 | 28 |
|
60 | 29 | 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 | + {} |
65 | 33 |
|
66 |
| - void instantiateDataStorage(); |
| 34 | + template <typename MeshBufferIterator> |
| 35 | + bool commit(IMeshPackerBase::PackedMeshBufferData* pmbdOut, ReservedAllocationMeshBuffers* rambIn, const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd); |
67 | 36 |
|
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; }; |
79 | 38 |
|
80 | 39 | };
|
81 | 40 |
|
82 | 41 | 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) |
93 | 44 | {
|
94 | 45 | 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++) |
153 | 47 | {
|
154 | 48 | ReservedAllocationMeshBuffers& ramb = *(rambIn + i);
|
155 | 49 | IMeshPackerBase::PackedMeshBufferData& pmbd = *(pmbdOut + i);
|
@@ -202,17 +96,6 @@ bool CCPUMeshPackerV2<MDIStructType>::commit(IMeshPackerBase::PackedMeshBufferDa
|
202 | 96 | return true;
|
203 | 97 | }
|
204 | 98 |
|
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 |
| - |
216 | 99 | }
|
217 | 100 | }
|
218 | 101 |
|
|
0 commit comments