|
12 | 12 | #include "CMeshManipulator.h"
|
13 | 13 | #include "os.h"
|
14 | 14 |
|
15 |
| -namespace nbl { namespace asset |
| 15 | +namespace nbl |
| 16 | +{ |
| 17 | +namespace asset |
16 | 18 | {
|
17 | 19 |
|
18 |
| -core::smart_refctd_ptr<asset::ICPUMeshBuffer> COverdrawMeshOptimizer::createOptimized(asset::ICPUMeshBuffer* _inbuffer, bool _createNew, float _threshold) |
| 20 | +void COverdrawMeshOptimizer::createOptimized(asset::ICPUMeshBuffer* _outbuffer, const asset::ICPUMeshBuffer* _inbuffer, float _threshold) |
19 | 21 | {
|
20 |
| - if (!_inbuffer) |
21 |
| - return nullptr; |
| 22 | + if (!_outbuffer || !_inbuffer) |
| 23 | + return; |
22 | 24 |
|
23 | 25 | const asset::E_INDEX_TYPE indexType = _inbuffer->getIndexType();
|
24 |
| - if (indexType == asset::EIT_UNKNOWN) |
25 |
| - return nullptr; |
26 |
| - |
27 |
| - const size_t indexSize = indexType == asset::EIT_16BIT ? 2u : 4u; |
28 |
| - |
29 |
| - auto outbuffer = _createNew ? IMeshManipulator::createMeshBufferDuplicate(_inbuffer) : core::smart_refctd_ptr<asset::ICPUMeshBuffer>(_inbuffer); |
| 26 | + const size_t indexSize = indexType == asset::EIT_16BIT ? sizeof(uint16_t):sizeof(uint32_t); |
30 | 27 |
|
31 |
| - void* const indices = outbuffer->getIndices(); |
32 |
| - if (!indices) |
| 28 | + const uint32_t idxCount = _inbuffer->getIndexCount(); |
| 29 | + const void* const inIndices = _inbuffer->getIndices(); |
| 30 | + if (idxCount==0u || indexType==asset::EIT_UNKNOWN || !inIndices) |
33 | 31 | {
|
34 | 32 | #ifdef _NBL_DEBUG
|
35 | 33 | os::Printer::log("Overdraw optimization: no index buffer -- mesh buffer left unchanged.");
|
36 | 34 | #endif
|
37 |
| - return outbuffer; |
| 35 | + return; |
38 | 36 | }
|
39 | 37 |
|
40 |
| - const size_t idxCount = outbuffer->getIndexCount(); |
41 |
| - void* indicesCopy = _NBL_ALIGNED_MALLOC(indexSize*idxCount,_NBL_SIMD_ALIGNMENT); |
42 |
| - memcpy(indicesCopy, indices, indexSize*idxCount); |
43 |
| - const size_t vertexCount = IMeshManipulator::upperBoundVertexID(outbuffer.get()); |
44 |
| - core::vector<core::vectorSIMDf> vertexPositions; |
| 38 | + void* const outIndices = _outbuffer->getIndices(); |
| 39 | + if (_outbuffer->getIndexCount()!=idxCount || _outbuffer->getIndexType()!=indexType || !outIndices) |
45 | 40 | {
|
46 |
| - core::vectorSIMDf pos; |
47 |
| - for (size_t i = 0; i < vertexCount; ++i) |
48 |
| - { |
49 |
| - outbuffer->getAttribute(pos, outbuffer->getPositionAttributeIx(), i); |
50 |
| - vertexPositions.push_back(pos); |
51 |
| - } |
| 41 | +#ifdef _NBL_DEBUG |
| 42 | + os::Printer::log("Overdraw optimization: output meshbuffer's index buffer does not match input -- mesh buffer left unchanged."); |
| 43 | +#endif |
| 44 | + return; |
52 | 45 | }
|
53 | 46 |
|
54 |
| - uint32_t* const hardClusters = (uint32_t*)_NBL_ALIGNED_MALLOC((idxCount/3) * 4,_NBL_SIMD_ALIGNMENT); |
| 47 | + void* indexCopy = nullptr; |
| 48 | + const uint16_t* inIndices16 = reinterpret_cast<const uint16_t*>(inIndices); |
| 49 | + const uint32_t* inIndices32 = reinterpret_cast<const uint32_t*>(inIndices); |
| 50 | + // TODO: more accure check for overlap |
| 51 | + if (_outbuffer->getIndexBufferBinding().buffer==_inbuffer->getIndexBufferBinding().buffer) |
| 52 | + { |
| 53 | + const size_t dataSize = indexSize*idxCount; |
| 54 | + indexCopy = _NBL_ALIGNED_MALLOC(dataSize,indexSize); |
| 55 | + memcpy(outIndices,inIndices,dataSize); |
| 56 | + inIndices16 = reinterpret_cast<const uint16_t*>(dataSize); |
| 57 | + inIndices32 = reinterpret_cast<const uint32_t*>(dataSize); |
| 58 | + } |
| 59 | + uint16_t* outIndices16 = reinterpret_cast<uint16_t*>(outIndices); |
| 60 | + uint32_t* outIndices32 = reinterpret_cast<uint32_t*>(outIndices); |
| 61 | + |
| 62 | + const uint32_t vertexCount = IMeshManipulator::upperBoundVertexID(_inbuffer); |
| 63 | + core::vector<core::vectorSIMDf> vertexPositions(vertexCount); |
| 64 | + for (uint32_t i=0u; i<vertexCount; ++i) |
| 65 | + _inbuffer->getAttribute(vertexPositions[i],_inbuffer->getPositionAttributeIx(),i); |
| 66 | + |
| 67 | + uint32_t* const hardClusters = reinterpret_cast<uint32_t*>(_NBL_ALIGNED_MALLOC((idxCount/3)*sizeof(uint32_t),_NBL_SIMD_ALIGNMENT)); |
55 | 68 | const size_t hardClusterCount = indexType == asset::EIT_16BIT ?
|
56 |
| - genHardBoundaries(hardClusters, (uint16_t*)indices, idxCount, vertexCount) : |
57 |
| - genHardBoundaries(hardClusters, (uint32_t*)indices, idxCount, vertexCount); |
| 69 | + genHardBoundaries(hardClusters, inIndices16, idxCount, vertexCount) : |
| 70 | + genHardBoundaries(hardClusters, inIndices32, idxCount, vertexCount); |
58 | 71 |
|
59 |
| - uint32_t* const softClusters = (uint32_t*)_NBL_ALIGNED_MALLOC((idxCount/3 + 1) * 4,_NBL_SIMD_ALIGNMENT); |
| 72 | + uint32_t* const softClusters = reinterpret_cast<uint32_t*>(_NBL_ALIGNED_MALLOC((idxCount/3+1)*sizeof(uint32_t),_NBL_SIMD_ALIGNMENT)); |
60 | 73 | const size_t softClusterCount = indexType == asset::EIT_16BIT ?
|
61 |
| - genSoftBoundaries(softClusters, (uint16_t*)indices, idxCount, vertexCount, hardClusters, hardClusterCount, _threshold) : |
62 |
| - genSoftBoundaries(softClusters, (uint32_t*)indices, idxCount, vertexCount, hardClusters, hardClusterCount, _threshold); |
| 74 | + genSoftBoundaries(softClusters, inIndices16, idxCount, vertexCount, hardClusters, hardClusterCount, _threshold) : |
| 75 | + genSoftBoundaries(softClusters, inIndices32, idxCount, vertexCount, hardClusters, hardClusterCount, _threshold); |
63 | 76 |
|
64 | 77 | ClusterSortData* sortedData = (ClusterSortData*)_NBL_ALIGNED_MALLOC(softClusterCount*sizeof(ClusterSortData),_NBL_SIMD_ALIGNMENT);
|
65 | 78 | if (indexType == asset::EIT_16BIT)
|
66 |
| - calcSortData(sortedData, (uint16_t*)indices, idxCount, vertexPositions, softClusters, softClusterCount); |
| 79 | + calcSortData(sortedData, inIndices16, idxCount, vertexPositions, softClusters, softClusterCount); |
67 | 80 | else
|
68 |
| - calcSortData(sortedData, (uint32_t*)indices, idxCount, vertexPositions, softClusters, softClusterCount); |
| 81 | + calcSortData(sortedData, inIndices32, idxCount, vertexPositions, softClusters, softClusterCount); |
69 | 82 |
|
70 |
| - std::stable_sort(sortedData, sortedData + softClusterCount, std::greater<ClusterSortData>()); |
| 83 | + std::stable_sort(sortedData, sortedData+softClusterCount, std::greater<ClusterSortData>()); |
71 | 84 |
|
72 |
| - for (size_t it = 0, jt = 0; it < softClusterCount; ++it) |
| 85 | + auto reorderIndices = [&](auto* out, const auto* in) |
73 | 86 | {
|
74 |
| - const uint32_t cluster = sortedData[it].cluster; |
| 87 | + for (size_t it = 0, jt = 0; it < softClusterCount; ++it) |
| 88 | + { |
| 89 | + const uint32_t cluster = sortedData[it].cluster; |
75 | 90 |
|
76 |
| - size_t start = softClusters[cluster]; |
77 |
| - size_t end = (cluster+1 < softClusterCount) ? softClusters[cluster+1] : idxCount/3; |
| 91 | + size_t start = softClusters[cluster]; |
| 92 | + size_t end = (cluster+1<softClusterCount) ? softClusters[cluster+1]:idxCount/3; |
78 | 93 |
|
79 |
| - if (indexType == asset::EIT_16BIT) |
80 |
| - { |
81 |
| - for (size_t i = start; i < end; ++i) |
82 |
| - { |
83 |
| - ((uint16_t*)indices)[jt++] = ((uint16_t*)indicesCopy)[3*i + 0]; |
84 |
| - ((uint16_t*)indices)[jt++] = ((uint16_t*)indicesCopy)[3*i + 1]; |
85 |
| - ((uint16_t*)indices)[jt++] = ((uint16_t*)indicesCopy)[3*i + 2]; |
86 |
| - } |
87 |
| - } |
88 |
| - else |
89 |
| - { |
90 | 94 | for (size_t i = start; i < end; ++i)
|
91 | 95 | {
|
92 |
| - ((uint32_t*)indices)[jt++] = ((uint32_t*)indicesCopy)[3*i + 0]; |
93 |
| - ((uint32_t*)indices)[jt++] = ((uint32_t*)indicesCopy)[3*i + 1]; |
94 |
| - ((uint32_t*)indices)[jt++] = ((uint32_t*)indicesCopy)[3*i + 2]; |
| 96 | + out[jt++] = in[3 * i + 0]; |
| 97 | + out[jt++] = in[3 * i + 1]; |
| 98 | + out[jt++] = in[3 * i + 2]; |
95 | 99 | }
|
96 | 100 | }
|
97 |
| - } |
| 101 | + }; |
| 102 | + if (indexType==asset::EIT_16BIT) |
| 103 | + reorderIndices(outIndices16,inIndices16); |
| 104 | + else |
| 105 | + reorderIndices(outIndices32,inIndices32); |
98 | 106 |
|
99 |
| - _NBL_ALIGNED_FREE(indicesCopy); |
| 107 | + if (indexCopy) |
| 108 | + _NBL_ALIGNED_FREE(indexCopy); |
100 | 109 | _NBL_ALIGNED_FREE(hardClusters);
|
101 | 110 | _NBL_ALIGNED_FREE(softClusters);
|
102 | 111 | _NBL_ALIGNED_FREE(sortedData);
|
103 |
| - |
104 |
| - return outbuffer; |
105 | 112 | }
|
106 | 113 |
|
107 | 114 | template<typename IdxT>
|
|
0 commit comments