forked from nvpro-samples/gl_vk_meshlet_cadscene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvmeshlet_packbasic.hpp
702 lines (580 loc) · 21.7 KB
/
nvmeshlet_packbasic.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
* Copyright (c) 2017-2022, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2017-2022 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _NV_MESHLET_PACKBASIC_H__
#define _NV_MESHLET_PACKBASIC_H__
#include "nvmeshlet_builder.hpp"
namespace NVMeshlet {
static const uint32_t PACKBASIC_ALIGN = 16;
// how many indices are fetched per thread, 8 or 4
static const uint32_t PACKBASIC_PRIMITIVE_INDICES_PER_FETCH = 8;
typedef uint32_t PackBasicType;
struct MeshletPackBasicDesc
{
//
// Bitfield layout :
//
// Field.X | Bits | Content
// ------------|:----:|----------------------------------------------
// bboxMinX | 8 | bounding box coord relative to object bbox
// bboxMinY | 8 | UNORM8
// bboxMinZ | 8 |
// vertexMax | 8 | number of vertex indices - 1 in the meshlet
// ------------|:----:|----------------------------------------------
// Field.Y | |
// ------------|:----:|----------------------------------------------
// bboxMaxX | 8 | bounding box coord relative to object bbox
// bboxMaxY | 8 | UNORM8
// bboxMaxZ | 8 |
// primMax | 8 | number of primitives - 1 in the meshlet
// ------------|:----:|----------------------------------------------
// Field.Z | |
// ------------|:----:|----------------------------------------------
// coneOctX | 8 | octant coordinate for cone normal, SNORM8
// coneOctY | 8 | octant coordinate for cone normal, SNORM8
// coneAngle | 8 | -sin(cone.angle), SNORM8
// vertexPack | 8 | vertex indices per 32 bits (1 or 2)
// ------------|:----:|----------------------------------------------
// Field.W | |
// ------------|:----:|----------------------------------------------
// packOffset | 32 | index buffer value of the first vertex
//
// Note : the bitfield is not expanded in the struct due to differences in how
// GPU & CPU compilers pack bit-fields and endian-ness.
union
{
#if !defined(NDEBUG) && defined(_MSC_VER)
struct
{
// warning, not portable
unsigned bboxMinX : 8;
unsigned bboxMinY : 8;
unsigned bboxMinZ : 8;
unsigned vertexMax : 8;
unsigned bboxMaxX : 8;
unsigned bboxMaxY : 8;
unsigned bboxMaxZ : 8;
unsigned primMax : 8;
signed coneOctX : 8;
signed coneOctY : 8;
signed coneAngle : 8;
unsigned vertexPack : 8;
unsigned packOffset : 32;
} _debug;
#endif
struct
{
uint32_t fieldX;
uint32_t fieldY;
uint32_t fieldZ;
uint32_t fieldW;
};
};
[[nodiscard]] uint32_t getNumVertices() const { return unpack(fieldX, 8, 24) + 1; }
void setNumVertices(uint32_t num)
{
assert(num <= MAX_VERTEX_COUNT_LIMIT);
fieldX |= pack(num - 1, 8, 24);
}
[[nodiscard]] uint32_t getNumPrims() const { return unpack(fieldY, 8, 24) + 1; }
void setNumPrims(uint32_t num)
{
assert(num <= MAX_PRIMITIVE_COUNT_LIMIT);
fieldY |= pack(num - 1, 8, 24);
}
[[nodiscard]] uint32_t getNumVertexPack() const { return unpack(fieldZ, 8, 24); }
void setNumVertexPack(uint32_t num) { fieldZ |= pack(num, 8, 24); }
[[nodiscard]] uint32_t getPackOffset() const { return fieldW; }
void setPackOffset(uint32_t index) { fieldW = index; }
[[nodiscard]] uint32_t getVertexStart() const { return 0; }
[[nodiscard]] uint32_t getVertexSize() const
{
uint32_t vertexDiv = getNumVertexPack();
uint32_t vertexElems = ((getNumVertices() + vertexDiv - 1) / vertexDiv);
return vertexElems;
}
[[nodiscard]] uint32_t getPrimStart() const { return (getVertexStart() + getVertexSize() + 1) & (~1u); }
[[nodiscard]] uint32_t getPrimSize() const
{
uint32_t primDiv = 4;
uint32_t primElems = ((getNumPrims() * 3 + PACKBASIC_PRIMITIVE_INDICES_PER_FETCH - 1) / primDiv);
return primElems;
}
// positions are relative to object's bbox treated as UNORM
void setBBox(uint8_t const bboxMin[3], uint8_t const bboxMax[3])
{
fieldX |= pack(bboxMin[0], 8, 0) | pack(bboxMin[1], 8, 8) | pack(bboxMin[2], 8, 16);
fieldY |= pack(bboxMax[0], 8, 0) | pack(bboxMax[1], 8, 8) | pack(bboxMax[2], 8, 16);
}
void getBBox(uint8_t bboxMin[3], uint8_t bboxMax[3]) const
{
bboxMin[0] = unpack(fieldX, 8, 0);
bboxMin[0] = unpack(fieldX, 8, 8);
bboxMin[0] = unpack(fieldX, 8, 16);
bboxMax[0] = unpack(fieldY, 8, 0);
bboxMax[0] = unpack(fieldY, 8, 8);
bboxMax[0] = unpack(fieldY, 8, 16);
}
// uses octant encoding for cone Normal
// positive angle means the cluster cannot be backface-culled
// numbers are treated as SNORM
void setCone(int8_t coneOctX, int8_t coneOctY, int8_t minusSinAngle)
{
fieldZ |= pack(coneOctX, 8, 0);
fieldZ |= pack(coneOctY, 8, 8);
fieldZ |= pack(minusSinAngle, 8, 16);
}
void getCone(int8_t& coneOctX, int8_t& coneOctY, int8_t& minusSinAngle) const
{
coneOctX = static_cast<int8_t>(unpack(fieldZ, 8, 0));
coneOctY = static_cast<int8_t>(unpack(fieldZ, 8, 8));
minusSinAngle = static_cast<int8_t>(unpack(fieldZ, 8, 16));
}
MeshletPackBasicDesc()
{
fieldX = 0;
fieldY = 0;
fieldZ = 0;
fieldW = 0;
}
};
struct MeshletPackBasic
{
// variable size
//
// aligned to PACKBASIC_ALIGN bytes
// - first sequence is either 16 or 32 bit indices per vertex
// (vertexPack is 2 or 1) respectively
// - second sequence aligned to 8 bytes, primitive many 8 bit values
//
//
// { u32[numVertices/vertexPack ...], padding..., u8[(numPrimitives) * 3 ...] }
union
{
uint32_t data32[1];
uint16_t data16[1];
uint8_t data8[1];
};
inline void setVertexIndex(uint32_t PACKED_SIZE, uint32_t vertex, uint32_t vertexPack, uint32_t indexValue)
{
#if 1
(void)PACKED_SIZE;
if(vertexPack == 1)
{
data32[vertex] = indexValue;
}
else
{
data16[vertex] = indexValue;
}
#else
uint32_t idx = vertex / vertexPack;
uint32_t shift = vertex % vertexPack;
assert(idx < PACKED_SIZE);
data32[idx] |= indexValue << (shift * 16);
#endif
}
[[nodiscard]] inline uint32_t getVertexIndex(uint32_t vertex, uint32_t vertexPack) const
{
#if 1
return (vertexPack == 1) ? data32[vertex] : data16[vertex];
#else
uint32_t idx = vertex / vertexPack;
uint32_t shift = vertex & (vertexPack - 1);
uint32_t bits = vertexPack == 2 ? 16 : 0;
uint32_t indexValue = data32[idx];
indexValue <<= ((1 - shift) * bits);
indexValue >>= (bits);
return indexValue;
#endif
}
inline void setPrimIndices(uint32_t PACKED_SIZE, uint32_t prim, uint32_t primStart, const uint8_t indices[3])
{
uint32_t idx = primStart * 4 + prim * 3;
assert(idx < PACKED_SIZE * 4);
data8[idx + 0] = indices[0];
data8[idx + 1] = indices[1];
data8[idx + 2] = indices[2];
}
inline void getPrimIndices(uint32_t prim, uint32_t primStart, uint8_t indices[3]) const
{
uint32_t idx = primStart * 4 + prim * 3;
indices[0] = data8[idx + 0];
indices[1] = data8[idx + 1];
indices[2] = data8[idx + 2];
}
};
class PackBasicBuilder
{
public:
//////////////////////////////////////////////////////////////////////////
// Builder output
// The provided builder functions operate on one triangle mesh at a time
// and generate these outputs.
struct MeshletGeometry
{
std::vector<PackBasicType> meshletPacks;
std::vector<MeshletPackBasicDesc> meshletDescriptors;
std::vector<MeshletBbox> meshletBboxes;
};
//////////////////////////////////////////////////////////////////////////
// Builder configuration
private:
// might want to template these instead of using MAX
uint32_t m_maxVertexCount;
uint32_t m_maxPrimitiveCount;
bool m_separateBboxes;
// due to hw allocation granuarlity, good values are
// vertex count = 32 or 64
// primitive count = 40, 84 or 126
// maximizes the fit into gl_PrimitiveIndices[128 * N - 4]
public:
void setup(uint32_t maxVertexCount, uint32_t maxPrimitiveCount, bool separateBboxes = false)
{
assert(maxPrimitiveCount <= MAX_PRIMITIVE_COUNT_LIMIT);
assert(maxVertexCount <= MAX_VERTEX_COUNT_LIMIT);
m_maxVertexCount = maxVertexCount;
m_maxPrimitiveCount = maxPrimitiveCount;
m_separateBboxes = separateBboxes;
{
uint32_t indices = maxPrimitiveCount * 3;
// align to PRIMITIVE_INDICES_PER_FETCH
uint32_t indicesFit = (indices / PACKBASIC_PRIMITIVE_INDICES_PER_FETCH) * PACKBASIC_PRIMITIVE_INDICES_PER_FETCH;
uint32_t numTrisFit = indicesFit / 3;
assert(numTrisFit > 0);
m_maxPrimitiveCount = numTrisFit;
}
}
//////////////////////////////////////////////////////////////////////////
// generate meshlets
private:
static void addMeshlet(MeshletGeometry& geometry, const PrimitiveCache& cache)
{
uint32_t packOffset = uint32_t(geometry.meshletPacks.size());
uint32_t vertexPack = cache.numVertexAllBits <= 16 ? 2 : 1;
MeshletPackBasicDesc meshlet;
meshlet.setNumPrims(cache.numPrims);
meshlet.setNumVertices(cache.numVertices);
meshlet.setNumVertexPack(vertexPack);
meshlet.setPackOffset(packOffset);
uint32_t vertexStart = meshlet.getVertexStart();
uint32_t vertexSize = meshlet.getVertexSize();
uint32_t primStart = meshlet.getPrimStart();
uint32_t primSize = meshlet.getPrimSize();
uint32_t packedSize = std::max(vertexStart + vertexSize, primStart + primSize);
packedSize = alignedSize(packedSize, PACKBASIC_ALIGN);
geometry.meshletPacks.resize(geometry.meshletPacks.size() + packedSize, 0);
geometry.meshletDescriptors.push_back(meshlet);
auto* pack = (MeshletPackBasic*)&geometry.meshletPacks[packOffset];
{
for(uint32_t v = 0; v < cache.numVertices; v++)
{
pack->setVertexIndex(packedSize, v, vertexPack, cache.vertices[v]);
}
uint32_t primStartLoc = meshlet.getPrimStart();
for(uint32_t p = 0; p < cache.numPrims; p++)
{
pack->setPrimIndices(packedSize, p, primStartLoc, cache.primitives[p]);
}
}
}
public:
// Returns the number of successfully processed indices.
// If the returned number is lower than provided input, use the number
// as starting offset and create a new geometry description.
template <class VertexIndexType>
uint32_t buildMeshlets(MeshletGeometry& geometry, const uint32_t numIndices, const VertexIndexType* NV_RESTRICT indices) const
{
assert(m_maxPrimitiveCount <= MAX_PRIMITIVE_COUNT_LIMIT);
assert(m_maxVertexCount <= MAX_VERTEX_COUNT_LIMIT);
PrimitiveCache cache;
cache.maxPrimitiveSize = m_maxPrimitiveCount;
cache.maxVertexSize = m_maxVertexCount;
cache.reset();
for(uint32_t i = 0; i < numIndices / 3; i++)
{
if(cache.cannotInsertBlock(indices[i * 3 + 0], indices[i * 3 + 1], indices[i * 3 + 2]))
{
// finish old and reset
addMeshlet(geometry, cache);
cache.reset();
}
cache.insert(indices[i * 3 + 0], indices[i * 3 + 1], indices[i * 3 + 2]);
}
if(!cache.empty())
{
addMeshlet(geometry, cache);
}
return numIndices;
}
static void padTaskMeshlets(MeshletGeometry& geometry)
{
if(geometry.meshletDescriptors.empty())
return;
}
//////////////////////////////////////////////////////////////////////////
// generate early culling per meshlet
public:
// bbox and cone angle
void buildMeshletEarlyCulling(MeshletGeometry& geometry,
const float objectBboxMin[3],
const float objectBboxMax[3],
const float* NV_RESTRICT positions,
const size_t positionStride) const
{
assert((positionStride % sizeof(float)) == 0);
size_t positionMul = positionStride / sizeof(float);
vec objectBboxExtent = vec(objectBboxMax) - vec(objectBboxMin);
if(m_separateBboxes)
{
geometry.meshletBboxes.resize(geometry.meshletDescriptors.size());
}
for(size_t i = 0; i < geometry.meshletDescriptors.size(); i++)
{
MeshletPackBasicDesc& meshlet = geometry.meshletDescriptors[i];
const auto* pack = (const MeshletPackBasic*)&geometry.meshletPacks[meshlet.getPackOffset()];
uint32_t primCount = meshlet.getNumPrims();
uint32_t primStart = meshlet.getPrimStart();
uint32_t vertexCount = meshlet.getNumVertices();
uint32_t vertexPack = meshlet.getNumVertexPack();
vec bboxMin = vec(FLT_MAX);
vec bboxMax = vec(-FLT_MAX);
vec avgNormal = vec(0.0f);
vec triNormals[MAX_PRIMITIVE_COUNT_LIMIT];
// skip unset
if(vertexCount == 1)
continue;
for(uint32_t p = 0; p < primCount; p++)
{
uint8_t indices[3];
uint32_t idxA;
uint32_t idxB;
uint32_t idxC;
pack->getPrimIndices(p, primStart, indices);
idxA = pack->getVertexIndex(indices[0], vertexPack);
idxB = pack->getVertexIndex(indices[1], vertexPack);
idxC = pack->getVertexIndex(indices[2], vertexPack);
vec posA = vec(&positions[idxA * positionMul]);
vec posB = vec(&positions[idxB * positionMul]);
vec posC = vec(&positions[idxC * positionMul]);
{
// bbox
bboxMin = vec_min(bboxMin, posA);
bboxMin = vec_min(bboxMin, posB);
bboxMin = vec_min(bboxMin, posC);
bboxMax = vec_max(bboxMax, posA);
bboxMax = vec_max(bboxMax, posB);
bboxMax = vec_max(bboxMax, posC);
}
{
// cone
vec cross = vec_cross(posB - posA, posC - posA);
float length = vec_length(cross);
vec normal;
if(length > FLT_EPSILON)
{
normal = cross * (1.0f / length);
}
else
{
normal = cross;
}
avgNormal = avgNormal + normal;
triNormals[p] = normal;
}
}
if(m_separateBboxes)
{
geometry.meshletBboxes[i].bboxMin[0] = bboxMin.x;
geometry.meshletBboxes[i].bboxMin[1] = bboxMin.y;
geometry.meshletBboxes[i].bboxMin[2] = bboxMin.z;
geometry.meshletBboxes[i].bboxMax[0] = bboxMax.x;
geometry.meshletBboxes[i].bboxMax[1] = bboxMax.y;
geometry.meshletBboxes[i].bboxMax[2] = bboxMax.z;
}
{
// bbox
// truncate min relative to object min
bboxMin = bboxMin - vec(objectBboxMin);
bboxMax = bboxMax - vec(objectBboxMin);
bboxMin = bboxMin / objectBboxExtent;
bboxMax = bboxMax / objectBboxExtent;
// snap to grid
const int gridBits = 8;
const int gridLast = (1 << gridBits) - 1;
uint8_t gridMin[3];
uint8_t gridMax[3];
gridMin[0] = std::max(0, std::min(int(truncf(bboxMin.x * float(gridLast))), gridLast - 1));
gridMin[1] = std::max(0, std::min(int(truncf(bboxMin.y * float(gridLast))), gridLast - 1));
gridMin[2] = std::max(0, std::min(int(truncf(bboxMin.z * float(gridLast))), gridLast - 1));
gridMax[0] = std::max(0, std::min(int(ceilf(bboxMax.x * float(gridLast))), gridLast));
gridMax[1] = std::max(0, std::min(int(ceilf(bboxMax.y * float(gridLast))), gridLast));
gridMax[2] = std::max(0, std::min(int(ceilf(bboxMax.z * float(gridLast))), gridLast));
meshlet.setBBox(gridMin, gridMax);
}
{
// potential improvement, instead of average maybe use
// http://www.cs.technion.ac.il/~cggc/files/gallery-pdfs/Barequet-1.pdf
float len = vec_length(avgNormal);
if(len > FLT_EPSILON)
{
avgNormal = avgNormal / len;
}
else
{
avgNormal = vec(0.0f);
}
vec packed = float32x3_to_octn_precise(avgNormal, 16);
auto coneX = static_cast<int8_t>(std::min(127, std::max(-127, int32_t(packed.x * 127.0f))));
auto coneY = static_cast<int8_t>(std::min(127, std::max(-127, int32_t(packed.y * 127.0f))));
// post quantization normal
avgNormal = oct_to_float32x3(vec(float(coneX) / 127.0f, float(coneY) / 127.0f, 0.0f));
float mindot = 1.0f;
for(unsigned int p = 0; p < primCount; p++)
{
mindot = std::min(mindot, vec_dot(triNormals[p], avgNormal));
}
// apply safety delta due to quantization
mindot -= 1.0f / 127.0f;
mindot = std::max(-1.0f, mindot);
// positive value for cluster not being backface cullable (normals > 90°)
int8_t coneAngle = 127;
if(mindot > 0)
{
// otherwise store -sin(cone angle)
// we test against dot product (cosine) so this is equivalent to cos(cone angle + 90°)
float angle = -sinf(acosf(mindot));
coneAngle = static_cast<int8_t>(std::max(-127, std::min(127, int32_t(angle * 127.0f))));
}
meshlet.setCone(coneX, coneY, coneAngle);
}
}
}
//////////////////////////////////////////////////////////////////////////
template <class VertexIndexType>
StatusCode errorCheck(const MeshletGeometry& geometry,
uint32_t minVertex,
uint32_t maxVertex,
uint32_t numIndices,
const VertexIndexType* NV_RESTRICT indices) const
{
uint32_t compareTris = 0;
for(size_t i = 0; i < geometry.meshletDescriptors.size(); i++)
{
const MeshletPackBasicDesc& meshlet = geometry.meshletDescriptors[i];
const MeshletPackBasic* pack = (const MeshletPackBasic*)&geometry.meshletPacks[meshlet.getPackOffset()];
uint32_t primCount = meshlet.getNumPrims();
uint32_t primStart = meshlet.getPrimStart();
uint32_t vertexCount = meshlet.getNumVertices();
uint32_t vertexPack = meshlet.getNumVertexPack();
// skip unset
if(vertexCount == 1)
continue;
for(uint32_t p = 0; p < primCount; p++)
{
uint8_t blockIndices[3];
pack->getPrimIndices(p, primStart, blockIndices);
if(blockIndices[0] >= m_maxVertexCount || blockIndices[1] >= m_maxVertexCount || blockIndices[2] >= m_maxVertexCount)
{
return STATUS_PRIM_OUT_OF_BOUNDS;
}
uint32_t idxA = pack->getVertexIndex(blockIndices[0], vertexPack);
uint32_t idxB = pack->getVertexIndex(blockIndices[1], vertexPack);
uint32_t idxC = pack->getVertexIndex(blockIndices[2], vertexPack);
if(idxA < minVertex || idxA > maxVertex || idxB < minVertex || idxB > maxVertex || idxC < minVertex || idxC > maxVertex)
{
return STATUS_VERTEX_OUT_OF_BOUNDS;
}
uint32_t refA = 0;
uint32_t refB = 0;
uint32_t refC = 0;
while(refA == refB || refA == refC || refB == refC)
{
if(compareTris * 3 + 2 >= numIndices)
{
return STATUS_MISMATCH_INDICES;
}
refA = indices[compareTris * 3 + 0];
refB = indices[compareTris * 3 + 1];
refC = indices[compareTris * 3 + 2];
compareTris++;
}
if(refA != idxA || refB != idxB || refC != idxC)
{
return STATUS_MISMATCH_INDICES;
}
}
}
return STATUS_NO_ERROR;
}
void appendStats(const MeshletGeometry& geometry, Stats& stats) const
{
if(geometry.meshletDescriptors.empty())
{
return;
}
stats.meshletsStored += geometry.meshletDescriptors.size();
double primloadAvg = 0;
double primloadVar = 0;
double vertexloadAvg = 0;
double vertexloadVar = 0;
size_t meshletsTotal = 0;
for(auto meshlet : geometry.meshletDescriptors)
{
uint32_t primCount = meshlet.getNumPrims();
uint32_t vertexCount = meshlet.getNumVertices();
if(vertexCount == 1)
{
continue;
}
meshletsTotal++;
stats.vertexTotal += vertexCount;
stats.primTotal += primCount;
primloadAvg += double(primCount) / double(m_maxPrimitiveCount);
vertexloadAvg += double(vertexCount) / double(m_maxVertexCount);
int8_t coneX;
int8_t coneY;
int8_t coneAngle;
meshlet.getCone(coneX, coneY, coneAngle);
stats.backfaceTotal += coneAngle < 0 ? 1 : 0;
}
stats.meshletsTotal += meshletsTotal;
double statsNum = meshletsTotal ? double(meshletsTotal) : 1.0;
primloadAvg /= statsNum;
vertexloadAvg /= statsNum;
for(auto meshlet : geometry.meshletDescriptors)
{
uint32_t primCount = meshlet.getNumPrims();
uint32_t vertexCount = meshlet.getNumVertices();
double diff;
diff = primloadAvg - ((double(primCount) / double(m_maxPrimitiveCount)));
primloadVar += diff * diff;
diff = vertexloadAvg - ((double(vertexCount) / double(m_maxVertexCount)));
vertexloadVar += diff * diff;
}
primloadVar /= statsNum;
vertexloadVar /= statsNum;
stats.primloadAvg += primloadAvg;
stats.primloadVar += primloadVar;
stats.vertexloadAvg += vertexloadAvg;
stats.vertexloadVar += vertexloadVar;
stats.appended += 1.0;
}
};
} // namespace NVMeshlet
#endif