Skip to content

Commit 4c91d15

Browse files
committed
Created offset tables
1 parent af5e629 commit 4c91d15

File tree

8 files changed

+159
-71
lines changed

8 files changed

+159
-71
lines changed
6.66 KB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[LocalizedFileNames]
2+

examples_tests/50.MeshPacking/main.cpp

Lines changed: 106 additions & 58 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#version 460 core
22

3-
//layout(location = 0) in vec3 normal;
3+
layout(location = 0) in vec3 normal;
44
layout(location = 0) out vec4 color;
55

66
void main()
77
{
8-
//vec3 colorTmp = clamp(dot(vec3(0.0, 1.0, 0.0), normal), 0, 1) * vec3(1.0) + vec3(0.2);
9-
//color = vec4(colorTmp, 1.0);
8+
vec3 colorTmp = clamp(dot(vec3(0.0, 1.0, 0.0), normal), 0, 1) * vec3(1.0) + vec3(0.2);
9+
color = vec4(colorTmp, 1.0);
1010

11-
color = vec4(1.0);
11+
//color = vec4(1.0);
1212
}

include/nbl/asset/CCPUMeshPackerV2.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class CCPUMeshPackerV2 final : public IMeshPackerV2<ICPUBuffer, ICPUMeshBuffer,
2525
using PackerDataStore = typename base_t::PackerDataStore;
2626
using ReservedAllocationMeshBuffers = typename base_t::ReservedAllocationMeshBuffers;
2727
using AttribAllocParams = typename base_t::AttribAllocParams;
28+
using CombinedDataOffsetTable = typename base_t::CombinedDataOffsetTable;
2829

2930
public:
3031
CCPUMeshPackerV2(const AllocationParams& allocParams, uint16_t minTriangleCountPerMDIData = 256u, uint16_t maxTriangleCountPerMDIData = 1024u)
@@ -34,7 +35,7 @@ class CCPUMeshPackerV2 final : public IMeshPackerV2<ICPUBuffer, ICPUMeshBuffer,
3435
void instantiateDataStorage();
3536

3637
template <typename MeshBufferIterator>
37-
bool commit(IMeshPackerBase::PackedMeshBufferData* pmbdOut, ReservedAllocationMeshBuffers* rambIn, const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd);
38+
bool commit(IMeshPackerBase::PackedMeshBufferData* pmbdOut, CombinedDataOffsetTable* cdotOut, ReservedAllocationMeshBuffers* rambIn, const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd);
3839

3940
inline PackerDataStore getPackerDataStore() { return m_packerDataStore; };
4041

@@ -50,7 +51,7 @@ void CCPUMeshPackerV2<MDIStructType>::instantiateDataStorage()
5051

5152
template <typename MDIStructType>
5253
template <typename MeshBufferIterator>
53-
bool CCPUMeshPackerV2<MDIStructType>::commit(IMeshPackerBase::PackedMeshBufferData* pmbdOut, ReservedAllocationMeshBuffers* rambIn, const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd)
54+
bool CCPUMeshPackerV2<MDIStructType>::commit(IMeshPackerBase::PackedMeshBufferData* pmbdOut, CombinedDataOffsetTable* cdotOut, ReservedAllocationMeshBuffers* rambIn, const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd)
5455
{
5556
size_t i = 0ull;
5657
for (auto it = mbBegin; it != mbEnd; it++)
@@ -66,6 +67,7 @@ bool CCPUMeshPackerV2<MDIStructType>::commit(IMeshPackerBase::PackedMeshBufferDa
6667
core::vector<TriangleBatch> triangleBatches = constructTriangleBatches(*it);
6768

6869
size_t batchFirstIdx = ramb.indexAllocationOffset;
70+
size_t verticesAddedCnt = 0u;
6971

7072
for (TriangleBatch& batch : triangleBatches)
7173
{
@@ -80,10 +82,19 @@ bool CCPUMeshPackerV2<MDIStructType>::commit(IMeshPackerBase::PackedMeshBufferDa
8082
if (ramb.attribAllocParams[location].offset == INVALID_ADDRESS)
8183
return false;
8284

83-
uint8_t* dstAttrPtr = static_cast<uint8_t*>(m_packerDataStore.vertexBuffer->getPointer()) + ramb.attribAllocParams[location].offset;
84-
deinterleaveAndCopyAttribute(*it, location, usedVertices, dstAttrPtr, true);
85+
//should I cashe it?
86+
const uint32_t attribSize = asset::getTexelOrBlockBytesize(static_cast<E_FORMAT>(mbVtxInputParams.attributes[location].format));
87+
const uint32_t currBatchOffset = verticesAddedCnt * attribSize;
88+
89+
uint8_t* dstAttrPtr = static_cast<uint8_t*>(m_packerDataStore.vertexBuffer->getPointer()) + ramb.attribAllocParams[location].offset + currBatchOffset;
90+
deinterleaveAndCopyAttribute(*it, location, usedVertices, dstAttrPtr);
91+
92+
cdotOut->attribOffset[location] = ramb.attribAllocParams[location].offset / attribSize + verticesAddedCnt;
8593
}
8694

95+
verticesAddedCnt += usedVertices.size();
96+
cdotOut++;
97+
8798
//construct mdi data
8899
MDIStructType MDIData;
89100
MDIData.count = batch.triangles.size() * 3u;

include/nbl/asset/IMeshPackerV2.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class IMeshPackerV2 : public IMeshPacker<MeshBufferType, MDIStructType>
4141
}
4242
};
4343

44+
struct CombinedDataOffsetTable
45+
{
46+
size_t attribOffset[SVertexInputParams::MAX_VERTEX_ATTRIB_COUNT];
47+
};
48+
4449
struct PackerDataStore : base_t::template PackerDataStoreCommon<BufferType>
4550
{
4651
core::smart_refctd_ptr<BufferType> vertexBuffer;
@@ -59,6 +64,9 @@ class IMeshPackerV2 : public IMeshPacker<MeshBufferType, MDIStructType>
5964
template <typename MeshBufferIterator>
6065
bool alloc(ReservedAllocationMeshBuffers* rambOut, const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd);
6166

67+
template <typename MeshBufferIterator>
68+
uint32_t calcDataTableNeededSize(const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd);
69+
6270
inline PackerDataStore getPackerDataStore() { return m_packerDataStore; };
6371

6472
protected:
@@ -76,7 +84,7 @@ bool IMeshPackerV2<MeshBufferType, BufferType, MDIStructType>::alloc(ReservedAll
7684
{
7785
ReservedAllocationMeshBuffers& ramb = *(rambOut + i);
7886
const size_t idxCnt = (*it)->getIndexCount();
79-
const size_t maxVtxCnt = (idxCnt + 1u) / 2u;
87+
const size_t maxVtxCnt = IMeshManipulator::upperBoundVertexID(*it); //ahsdfjkasdfgasdklfhasdf TODO: deal with vertex duplication, same for v1
8088

8189
//allocate indices
8290
ramb.indexAllocationOffset = m_idxBuffAlctr.alloc_addr(idxCnt, 1u);
@@ -116,6 +124,23 @@ bool IMeshPackerV2<MeshBufferType, BufferType, MDIStructType>::alloc(ReservedAll
116124
return true;
117125
}
118126

127+
template <typename MeshBufferType, typename BufferType, typename MDIStructType>
128+
template <typename MeshBufferIterator>
129+
uint32_t IMeshPackerV2<MeshBufferType, BufferType, MDIStructType>::calcDataTableNeededSize(const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd)
130+
{
131+
uint32_t acc = 0u;
132+
for (auto mbIt = mbBegin; mbIt != mbEnd; mbIt++)
133+
{
134+
const size_t idxCnt = (*mbIt)->getIndexCount();
135+
const uint32_t triCnt = idxCnt / 3;
136+
assert(idxCnt % 3 == 0);
137+
138+
acc += calcBatchCount(triCnt);
139+
}
140+
141+
return acc;
142+
}
143+
119144
}
120145
}
121146

include/nbl/asset/utils/CCPUMeshPackerV1.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ IMeshPackerBase::PackedMeshBufferData CCPUMeshPackerV1<MDIStructType>::commit(co
473473
const size_t attrSize = asset::getTexelOrBlockBytesize(static_cast<E_FORMAT>(attrib.format));
474474
dstAttrPtr += (ramb.vertexAllocationOffset + verticesAddedToUnifiedBufferCnt) * attrSize;
475475

476-
deinterleaveAndCopyAttribute(*it, location, usedVertices, dstAttrPtr, false);
476+
deinterleaveAndCopyAttribute(*it, location, usedVertices, dstAttrPtr);
477477
}
478478

479479
verticesAddedToUnifiedBufferCnt += usedVertices.size();

include/nbl/asset/utils/IMeshPacker.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ class IMeshPacker : public IMeshPackerBase
125125
return size;
126126
}
127127

128+
inline constexpr uint32_t calcBatchCount(uint32_t triCnt) { return (triCnt + m_maxTriangleCountPerMDIData - 1) / m_maxTriangleCountPerMDIData; }
129+
128130
struct Triangle
129131
{
130132
uint32_t oldIndices[3];
@@ -139,9 +141,9 @@ class IMeshPacker : public IMeshPackerBase
139141
{
140142
const size_t idxCnt = meshBuffer->getIndexCount();
141143
const uint32_t triCnt = idxCnt / 3;
142-
_NBL_DEBUG_BREAK_IF(idxCnt % 3 != 0);
144+
assert(idxCnt % 3 == 0);
143145

144-
const uint32_t batchCount = (triCnt + m_maxTriangleCountPerMDIData - 1) / m_maxTriangleCountPerMDIData;
146+
const uint32_t batchCount = calcBatchCount(triCnt);
145147

146148
core::vector<TriangleBatch> output(batchCount);
147149

@@ -228,7 +230,7 @@ class IMeshPacker : public IMeshPackerBase
228230
return usedVertices;
229231
}
230232

231-
void deinterleaveAndCopyAttribute(MeshBufferType* meshBuffer, uint16_t attrLocation, const core::unordered_map<uint32_t, uint16_t>& usedVertices, uint8_t* dstAttrPtr, bool roundUpAttrSizeToPoT /*TODO*/)
233+
void deinterleaveAndCopyAttribute(MeshBufferType* meshBuffer, uint16_t attrLocation, const core::unordered_map<uint32_t, uint16_t>& usedVertices, uint8_t* dstAttrPtr)
232234
{
233235
uint8_t* srcAttrPtr = meshBuffer->getAttribPointer(attrLocation);
234236
SVertexInputParams& mbVtxInputParams = meshBuffer->getPipeline()->getVertexInputParams();

0 commit comments

Comments
 (0)