Skip to content
Navigation Menu
{{ message }}
forked from microsoft/DirectXMesh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectXMeshletGenerator.cpp
More file actions
886 lines (740 loc) · 30.8 KB
/
Copy pathDirectXMeshletGenerator.cpp
File metadata and controls
886 lines (740 loc) · 30.8 KB
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
//-------------------------------------------------------------------------------------
// DirectXMeshletGenerator.cpp
//
// DirectX Mesh Geometry Library - Meshlet Computation
//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//
// http://go.microsoft.com/fwlink/?LinkID=324981
//-------------------------------------------------------------------------------------
#include "DirectXMeshP.h"
using namespace DirectX;
using namespace DirectX::PackedVector;
static_assert(sizeof(MeshletTriangle) == sizeof(uint32_t), "MeshletTriangle packing is incorrect");
namespace
{
//---------------------------------------------------------------------------------
// Helper class which manages a fixed-size array like a vector.
//---------------------------------------------------------------------------------
template <typename T, size_t N>
class StaticVector
{
public:
StaticVector() noexcept
: m_data{}, m_size(0)
{ }
~StaticVector() = default;
StaticVector(StaticVector&&) = default;
StaticVector& operator= (StaticVector&&) = default;
StaticVector(StaticVector const&) = default;
StaticVector& operator= (StaticVector const&) = default;
void push_back(const T& value) noexcept
{
assert(m_size < N);
m_data[m_size++] = value;
}
void push_back(T&& value) noexcept
{
assert(m_size < N);
m_data[m_size++] = std::move(value);
}
template <typename... Args>
void emplace_back(Args&&... args) noexcept
{
assert(m_size < N);
m_data[m_size++] = T(std::forward<Args>(args)...);
}
size_t size() const noexcept { return m_size; }
bool empty() const noexcept { return m_size == 0; }
T* data() noexcept { return m_data.data(); }
const T* data() const noexcept { return m_data.data(); }
T& operator[](size_t index) noexcept { assert(index < m_size); return m_data[index]; }
const T& operator[](size_t index) const noexcept { assert(index < m_size); return m_data[index]; }
private:
std::array<T, N> m_data;
size_t m_size;
};
//---------------------------------------------------------------------------------
// Helper struct which maintains the working state of a new meshlet
//---------------------------------------------------------------------------------
template <typename T>
struct InlineMeshlet
{
StaticVector<T, MESHLET_MAXIMUM_SIZE> UniqueVertexIndices;
StaticVector<MeshletTriangle, MESHLET_MAXIMUM_SIZE> PrimitiveIndices;
};
//---------------------------------------------------------------------------------
// Computes normal vector from the points of a triangle
//---------------------------------------------------------------------------------
inline XMVECTOR XM_CALLCONV ComputeNormal(_In_reads_(3) const XMFLOAT3* tri) noexcept
{
XMVECTOR p0 = XMLoadFloat3(&tri[0]);
XMVECTOR p1 = XMLoadFloat3(&tri[1]);
XMVECTOR p2 = XMLoadFloat3(&tri[2]);
XMVECTOR v01 = XMVectorSubtract(p0, p1);
XMVECTOR v02 = XMVectorSubtract(p0, p2);
return XMVector3Normalize(XMVector3Cross(v01, v02));
}
//---------------------------------------------------------------------------------
// Computes number of triangle vertices already exist in the meshlet
//---------------------------------------------------------------------------------
template <typename T>
uint8_t ComputeReuse(const InlineMeshlet<T>& meshlet, _In_reads_(3) const T* triIndices) noexcept
{
uint8_t count = 0;
for (size_t i = 0; i < meshlet.UniqueVertexIndices.size(); ++i)
{
for (size_t j = 0; j < 3u; ++j)
{
if (meshlet.UniqueVertexIndices[i] == triIndices[j])
{
assert(count < 255);
_Analysis_assume_(count < 255);
++count;
}
}
}
return count;
}
//---------------------------------------------------------------------------------
// Computes a candidacy score based on spatial locality, orientational coherence,
// and vertex re-use within a meshlet.
//---------------------------------------------------------------------------------
template <typename T>
float XM_CALLCONV ComputeScore(
const InlineMeshlet<T>& meshlet,
FXMVECTOR sphere,
FXMVECTOR normal,
_In_reads_(3) const T* triIndices,
_In_reads_(3) const XMFLOAT3* triVerts) noexcept
{
// Configurable weighted sum parameters
constexpr float c_wtReuse = 0.334f;
constexpr float c_wtLocation = 0.333f;
constexpr float c_wtOrientation = 1.0f - (c_wtReuse + c_wtLocation);
// Vertex reuse -
uint8_t reuse = ComputeReuse(meshlet, triIndices);
float scrReuse = 1.0f - (float(reuse) / 3.0f);
// Distance from center point - log falloff to preserve normalization where it needs it
float maxSq = 0;
for (size_t i = 0; i < 3u; ++i)
{
XMVECTOR pos = XMLoadFloat3(&triVerts[i]);
XMVECTOR v = XMVectorSubtract(sphere, pos);
float distSq = XMVectorGetX(XMVector3Dot(v, v));
maxSq = std::max(maxSq, distSq);
}
float r = XMVectorGetW(sphere);
float r2 = r * r;
float scrLocation = std::max(0.0f, log2f(maxSq / (r2 + FLT_EPSILON) + FLT_EPSILON));
// Angle between normal and meshlet cone axis - cosine falloff
XMVECTOR n = ComputeNormal(triVerts);
float d = XMVectorGetX(XMVector3Dot(n, normal));
float scrOrientation = (1.0f - d) * 0.5f;
// Weighted sum of scores
return c_wtReuse * scrReuse + c_wtLocation * scrLocation + c_wtOrientation * scrOrientation;
}
//---------------------------------------------------------------------------------
// Attempts to add a candidate triangle to a meshlet
//---------------------------------------------------------------------------------
template <typename T>
bool TryAddToMeshlet(
size_t maxVerts,
size_t maxPrims,
_In_reads_(3) const T* tri,
InlineMeshlet<T>& meshlet)
{
// Cull degenerate triangle and return success
// newCount calculation will break if such triangle is passed
if (tri[0] == tri[1] || tri[1] == tri[2] || tri[0] == tri[2])
return true;
// Are we already full of vertices?
if (meshlet.UniqueVertexIndices.size() >= maxVerts)
return false;
// Are we full, or can we store an additional primitive?
if (meshlet.PrimitiveIndices.size() >= maxPrims)
return false;
uint32_t indices[3] = { uint32_t(-1), uint32_t(-1), uint32_t(-1) };
uint8_t newCount = 3;
for (size_t i = 0; i < meshlet.UniqueVertexIndices.size(); ++i)
{
for (size_t j = 0; j < 3; ++j)
{
if (meshlet.UniqueVertexIndices[i] == tri[j])
{
indices[j] = static_cast<uint32_t>(i);
--newCount;
}
}
}
// Will this triangle fit?
if (meshlet.UniqueVertexIndices.size() + newCount > maxVerts)
return false;
// Add unique vertex indices to unique vertex index list
for (size_t j = 0; j < 3; ++j)
{
if (indices[j] == uint32_t(-1))
{
indices[j] = static_cast<uint32_t>(meshlet.UniqueVertexIndices.size());
meshlet.UniqueVertexIndices.push_back(tri[j]);
}
}
// Add the new primitive
MeshletTriangle mtri = { indices[0], indices[1], indices[2] };
meshlet.PrimitiveIndices.emplace_back(mtri);
return true;
}
//---------------------------------------------------------------------------------
// Determines whether a meshlet contains the maximum number of vertices/primitives
//---------------------------------------------------------------------------------
template <typename T>
inline bool IsMeshletFull(size_t maxVerts, size_t maxPrims, const InlineMeshlet<T>& meshlet) noexcept
{
assert(meshlet.UniqueVertexIndices.size() <= maxVerts);
assert(meshlet.PrimitiveIndices.size() <= maxPrims);
return meshlet.UniqueVertexIndices.size() >= maxVerts || meshlet.PrimitiveIndices.size() >= maxPrims;
}
//---------------------------------------------------------------------------------
// Meshletize a contiguous list of primitives
//---------------------------------------------------------------------------------
template <typename T>
HRESULT Meshletize(
size_t maxVerts,
size_t maxPrims,
_In_reads_(nFaces * 3) const T* indices,
size_t nFaces,
_In_reads_(nVerts) const XMFLOAT3* positions,
size_t nVerts,
const std::pair<size_t, size_t>& subset,
_In_reads_(nFaces * 3) const uint32_t* adjacency,
std::vector<InlineMeshlet<T>>& meshlets)
{
if (!indices || !positions || !adjacency)
return E_INVALIDARG;
if (subset.first + subset.second > nFaces)
return E_UNEXPECTED;
meshlets.clear();
// Bitmask of all triangles in mesh to determine whether a specific one has been added
std::vector<bool> checklist;
checklist.resize(subset.second);
// Cache to maintain scores for each candidate triangle
std::vector<std::pair<uint32_t, float>> candidates;
std::unordered_set<uint32_t> candidateCheck;
// Positions and normals of the current primitive
std::vector<XMFLOAT3> vertices;
std::vector<XMFLOAT3> normals;
// Seed the candidate list with the first triangle of the subset
const uint32_t startIndex = static_cast<uint32_t>(subset.first);
const uint32_t endIndex = static_cast<uint32_t>(subset.first + subset.second);
uint32_t triIndex = static_cast<uint32_t>(subset.first);
candidates.push_back(std::make_pair(triIndex, 0.0f));
candidateCheck.insert(triIndex);
// Continue adding triangles until triangle list is exhausted.
InlineMeshlet<T>* curr = nullptr;
while (!candidates.empty())
{
uint32_t index = candidates.back().first;
candidates.pop_back();
T tri[3] =
{
indices[index * 3],
indices[index * 3 + 1],
indices[index * 3 + 2],
};
if (tri[0] >= nVerts ||
tri[1] >= nVerts ||
tri[2] >= nVerts)
{
return E_UNEXPECTED;
}
// Create a new meshlet if necessary
if (curr == nullptr)
{
vertices.clear();
normals.clear();
meshlets.emplace_back();
curr = &meshlets.back();
}
// Try to add triangle to meshlet
if (TryAddToMeshlet(maxVerts, maxPrims, tri, *curr))
{
// Success! Mark as added.
checklist[index - startIndex] = true;
// Add positions & normal to list
XMFLOAT3 points[3] =
{
positions[tri[0]],
positions[tri[1]],
positions[tri[2]],
};
vertices.push_back(points[0]);
vertices.push_back(points[1]);
vertices.push_back(points[2]);
normals.emplace_back();
XMStoreFloat3(&normals.back(), ComputeNormal(points));
// Compute new bounding sphere & normal axis
BoundingSphere positionBounds, normalBounds;
BoundingSphere::CreateFromPoints(positionBounds, vertices.size(), vertices.data(), sizeof(XMFLOAT3));
BoundingSphere::CreateFromPoints(normalBounds, normals.size(), normals.data(), sizeof(XMFLOAT3));
XMVECTOR psphere = XMLoadFloat4(reinterpret_cast<XMFLOAT4*>(&positionBounds));
XMVECTOR normal = XMVector3Normalize(XMLoadFloat4(reinterpret_cast<XMFLOAT4*>(&normalBounds)));
// Find and add all applicable adjacent triangles to candidate list
const uint32_t adjIndex = index * 3;
uint32_t adj[3] =
{
adjacency[adjIndex],
adjacency[adjIndex + 1],
adjacency[adjIndex + 2],
};
for (size_t i = 0; i < 3u; ++i)
{
// Invalid triangle in adjacency slot
if (adj[i] == uint32_t(-1))
continue;
// Primitive is outside the subset
if (adj[i] < subset.first || adj[i] > endIndex)
continue;
// Already processed triangle
if (checklist[adj[i] - startIndex])
continue;
// Triangle already in the candidate list
if (candidateCheck.count(adj[i]))
continue;
candidates.push_back(std::make_pair(adj[i], FLT_MAX));
candidateCheck.insert(adj[i]);
}
// Re-score remaining candidate triangles
for (size_t i = 0; i < candidates.size(); ++i)
{
uint32_t candidate = candidates[i].first;
T triIndices[3] =
{
indices[candidate * 3],
indices[candidate * 3 + 1],
indices[candidate * 3 + 2],
};
if (triIndices[0] >= nVerts ||
triIndices[1] >= nVerts ||
triIndices[2] >= nVerts)
{
return E_UNEXPECTED;
}
XMFLOAT3 triVerts[3] =
{
positions[triIndices[0]],
positions[triIndices[1]],
positions[triIndices[2]],
};
candidates[i].second = ComputeScore(*curr, psphere, normal, triIndices, triVerts);
}
// Determine whether we need to move to the next meshlet.
if (IsMeshletFull(maxVerts, maxPrims, *curr))
{
candidateCheck.clear();
curr = nullptr;
// Discard candidates - one of our existing candidates as the next meshlet seed.
if (!candidates.empty())
{
candidates[0] = candidates.back();
candidates.resize(1);
candidateCheck.insert(candidates[0].first);
}
}
else
{
// Sort in reverse order to use vector as a queue with pop_back
std::stable_sort(candidates.begin(), candidates.end(), [](auto& a, auto& b) { return a.second > b.second; });
}
}
else
{
// Ran out of candidates while attempting to fill the last bits of a meshlet.
if (candidates.empty())
{
candidateCheck.clear();
curr = nullptr;
}
}
// Ran out of candidates; add a new seed candidate to start the next meshlet.
if (candidates.empty())
{
while (triIndex < endIndex && checklist[triIndex - startIndex])
++triIndex;
if (triIndex == endIndex)
break;
candidates.push_back(std::make_pair(triIndex, 0.0f));
candidateCheck.insert(triIndex);
}
}
return S_OK;
}
//---------------------------------------------------------------------------------
// Builds meshlets for a list of index subsets and organizes their data into
// corresponding output buffers.
//---------------------------------------------------------------------------------
template <typename T>
HRESULT ComputeMeshletsInternal(
_In_reads_(nFaces * 3) const T* indices,
size_t nFaces,
_In_reads_(nVerts) const XMFLOAT3* positions,
size_t nVerts,
_In_reads_(nSubsets) const std::pair<size_t, size_t>* subsets,
size_t nSubsets,
_In_reads_opt_(nFaces * 3) const uint32_t* adjacency,
std::vector<Meshlet>& meshlets,
std::vector<uint8_t>& uniqueVertexIB,
std::vector<MeshletTriangle>& primitiveIndices,
_Out_writes_(nSubsets) std::pair<size_t, size_t>* meshletSubsets,
size_t maxVerts,
size_t maxPrims)
{
if (!indices || !positions || !subsets || !meshletSubsets)
return E_INVALIDARG;
// Validate the meshlet vertex & primitive sizes
if (maxVerts < MESHLET_MINIMUM_SIZE || maxVerts > MESHLET_MAXIMUM_SIZE)
return E_INVALIDARG;
if (maxPrims < MESHLET_MINIMUM_SIZE || maxPrims > MESHLET_MAXIMUM_SIZE)
return E_INVALIDARG;
if (nFaces == 0 || nVerts == 0 || nSubsets == 0)
return E_INVALIDARG;
// Auto-generate adjacency data if not provided.
std::unique_ptr<uint32_t[]> generatedAdj;
if (!adjacency)
{
generatedAdj.reset(new (std::nothrow) uint32_t[nFaces * 3]);
if (!generatedAdj)
return E_OUTOFMEMORY;
HRESULT hr = GenerateAdjacencyAndPointReps(indices, nFaces, positions, nVerts, 0.0f, nullptr, generatedAdj.get());
if (FAILED(hr))
{
return hr;
}
adjacency = generatedAdj.get();
}
// Now start generating meshlets
for (size_t i = 0; i < nSubsets; ++i)
{
auto& s = subsets[i];
if ((s.first + s.second) > nFaces)
{
return E_UNEXPECTED;
}
std::vector<InlineMeshlet<T>> newMeshlets;
HRESULT hr = Meshletize(maxVerts, maxPrims, indices, nFaces, positions, nVerts, s, adjacency, newMeshlets);
if (FAILED(hr))
{
return hr;
}
meshletSubsets[i] = std::make_pair(meshlets.size(), newMeshlets.size());
// Determine final unique vertex index and primitive index counts & offsets.
size_t startVertCount = uniqueVertexIB.size() / sizeof(T);
size_t startPrimCount = primitiveIndices.size();
size_t uniqueVertexIndexCount = startVertCount;
size_t primitiveIndexCount = startPrimCount;
// Resize the meshlet output array to hold the newly formed meshlets.
size_t meshletCount = meshlets.size();
meshlets.resize(meshletCount + newMeshlets.size());
Meshlet* dest = &meshlets[meshletCount];
for (auto& m : newMeshlets)
{
dest->VertOffset = static_cast<uint32_t>(uniqueVertexIndexCount);
dest->VertCount = static_cast<uint32_t>(m.UniqueVertexIndices.size());
dest->PrimOffset = static_cast<uint32_t>(primitiveIndexCount);
dest->PrimCount = static_cast<uint32_t>(m.PrimitiveIndices.size());
uniqueVertexIndexCount += m.UniqueVertexIndices.size();
primitiveIndexCount += m.PrimitiveIndices.size();
++dest;
}
// Allocate space for the new data.
uniqueVertexIB.resize(uniqueVertexIndexCount * sizeof(T));
primitiveIndices.resize(primitiveIndexCount);
// Copy data from the freshly built meshlets into the output buffers.
auto vertDest = reinterpret_cast<T*>(uniqueVertexIB.data()) + startVertCount;
auto primDest = reinterpret_cast<uint32_t*>(primitiveIndices.data()) + startPrimCount;
for (auto& m : newMeshlets)
{
memcpy(vertDest, m.UniqueVertexIndices.data(), m.UniqueVertexIndices.size() * sizeof(T));
memcpy(primDest, m.PrimitiveIndices.data(), m.PrimitiveIndices.size() * sizeof(uint32_t));
vertDest += m.UniqueVertexIndices.size();
primDest += m.PrimitiveIndices.size();
}
}
return S_OK;
}
//---------------------------------------------------------------------------------
// Generates culling data for a contiguous list of meshlets.
// Influenced by implementation in https://github.com/zeux/meshoptimizer
//---------------------------------------------------------------------------------
template <typename T>
HRESULT ComputeCullDataInternal(
_In_reads_(nVerts) const XMFLOAT3* positions,
size_t nVerts,
_In_reads_(nMeshlets) const Meshlet* meshlets,
size_t nMeshlets,
_In_reads_(nVertIndices) const T* uniqueVertexIndices,
size_t nVertIndices,
_In_reads_(nPrimIndices) const MeshletTriangle* primitiveIndices,
size_t nPrimIndices,
_Out_writes_(nMeshlets) CullData* cullData,
MESHLET_FLAGS flags) noexcept
{
// Input validation
if (!positions || !meshlets || !uniqueVertexIndices || !primitiveIndices || !cullData)
return E_INVALIDARG;
if (nVerts == 0 || nMeshlets == 0 || nVertIndices == 0 || nPrimIndices == 0)
return E_INVALIDARG;
// Cache for meshlet vertices & normals
XMFLOAT3 vertices[MESHLET_MAXIMUM_SIZE] = {};
XMFLOAT3 normals[MESHLET_MAXIMUM_SIZE] = {};
for (size_t mi = 0; mi < nMeshlets; ++mi)
{
auto& m = meshlets[mi];
auto& c = cullData[mi];
// Cache vertices
for (size_t i = 0; i < m.VertCount; ++i)
{
if (m.VertOffset + i >= nVertIndices)
{
return E_UNEXPECTED;
}
uint32_t vIndex = uniqueVertexIndices[m.VertOffset + i];
if (vIndex >= nVerts)
{
return E_UNEXPECTED;
}
vertices[i] = positions[vIndex];
}
// Generate primitive normals & cache
for (size_t i = 0; i < m.PrimCount; ++i)
{
if (m.PrimOffset + i >= nPrimIndices)
{
return E_UNEXPECTED;
}
auto primitive = primitiveIndices[m.PrimOffset + i];
XMFLOAT3 triangle[3]
{
vertices[primitive.i0],
vertices[primitive.i1],
vertices[primitive.i2],
};
XMVECTOR n = ComputeNormal(triangle);
n = (flags & MESHLET_WIND_CW) != 0 ? XMVectorNegate(n) : n;
XMStoreFloat3(&normals[i], n);
}
// Calculate spatial bounds
BoundingSphere::CreateFromPoints(c.BoundingSphere, m.VertCount, vertices, sizeof(XMFLOAT3));
XMVECTOR positionBounds = XMLoadFloat4(reinterpret_cast<XMFLOAT4*>(&c.BoundingSphere));
// Calculate the normal cone
// 1. Normalized center point of minimum bounding sphere of unit normals == conic axis
BoundingSphere nsphere;
BoundingSphere::CreateFromPoints(nsphere, m.PrimCount, normals, sizeof(XMFLOAT3));
// 2. Calculate dot product of all normals to conic axis, selecting minimum
XMVECTOR normalBounds = XMLoadFloat4(reinterpret_cast<XMFLOAT4*>(&nsphere));
XMVECTOR axis = XMVectorSetW(XMVector3Normalize(normalBounds), 0);
XMVECTOR minDot = g_XMOne;
for (size_t i = 0; i < m.PrimCount; ++i)
{
XMVECTOR dot = XMVector3Dot(axis, XMLoadFloat3(&normals[i]));
minDot = XMVectorMin(minDot, dot);
}
if (XMVector4Less(minDot, XMVectorReplicate(0.1f)))
{
XMStoreUByteN4(&c.NormalCone, XMVectorSet(0, 0, 0, 1)); // Degenerate cone
continue;
}
// Find the point on center-t*axis ray that lies in negative half-space of all triangles
float maxt = 0;
for (size_t i = 0; i < m.PrimCount; ++i)
{
if (m.PrimOffset + i >= nPrimIndices)
{
return E_UNEXPECTED;
}
auto primitive = primitiveIndices[m.PrimOffset + i];
XMVECTOR p0 = XMLoadFloat3(&vertices[primitive.i0]);
XMVECTOR center = XMVectorSubtract(positionBounds, p0);
XMVECTOR normal = XMLoadFloat3(&normals[i]);
float dc = XMVectorGetX(XMVector3Dot(center, normal));
float dn = XMVectorGetX(XMVector3Dot(axis, normal));
// dn should be larger than mindp cutoff above
if (dn <= 0.0f)
{
return E_UNEXPECTED;
}
float t = dc / dn;
maxt = (t > maxt) ? t : maxt;
}
// cone apex should be in the negative half-space of all cluster triangles by construction
c.ApexOffset = maxt;
// cos(a) for normal cone is minDot; we need to add 90 degrees on both sides and invert the cone
// which gives us -cos(a+90) = -(-sin(a)) = sin(a) = sqrt(1 - cos^2(a))
XMVECTOR minDotSq = XMVectorMultiply(minDot, minDot);
XMVECTOR coneCutoff = XMVectorSqrt(XMVectorSubtract(g_XMOne, minDotSq));
// Quantize normal vector to uint8
XMBYTEN4 snquant;
XMStoreByteN4(&snquant, axis);
c.NormalCone.x = uint8_t(int16_t(snquant.x) + 128);
c.NormalCone.y = uint8_t(int16_t(snquant.y) + 128);
c.NormalCone.z = uint8_t(int16_t(snquant.z) + 128);
// Calculate error bias from quantization
XMVECTOR dequant = XMLoadByteN4(&snquant);
XMVECTOR error = XMVectorSum(XMVectorAbs(XMVectorSubtract(dequant, axis)));
// Add error bias to cone cutoff
coneCutoff = XMVectorAdd(coneCutoff, error);
// Quantize normal cone spread to uint8 and bias upward
XMUBYTEN4 nquant;
XMStoreUByteN4(&nquant, coneCutoff);
c.NormalCone.w = nquant.x;
}
return S_OK;
}
}
//=====================================================================================
// Entry-points
//=====================================================================================
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT DirectX::ComputeMeshlets(
const uint16_t* indices,
size_t nFaces,
const XMFLOAT3* positions,
size_t nVerts,
const uint32_t* adjacency,
std::vector<Meshlet>& meshlets,
std::vector<uint8_t>& uniqueVertexIB,
std::vector<MeshletTriangle>& primitiveIndices,
size_t maxVerts,
size_t maxPrims)
{
std::pair<size_t, size_t> s = { 0, nFaces };
std::pair<size_t, size_t> subset;
return ComputeMeshletsInternal<uint16_t>(
indices, nFaces,
positions, nVerts,
&s, 1u,
adjacency,
meshlets,
uniqueVertexIB, primitiveIndices,
&subset,
maxVerts, maxPrims);
}
_Use_decl_annotations_
HRESULT DirectX::ComputeMeshlets(
const uint32_t* indices,
size_t nFaces,
const XMFLOAT3* positions,
size_t nVerts,
const uint32_t* adjacency,
std::vector<Meshlet>& meshlets,
std::vector<uint8_t>& uniqueVertexIB,
std::vector<MeshletTriangle>& primitiveIndices,
size_t maxVerts,
size_t maxPrims)
{
std::pair<size_t, size_t> s = { 0, nFaces };
std::pair<size_t, size_t> subset;
return ComputeMeshletsInternal<uint32_t>(
indices, nFaces,
positions, nVerts,
&s, 1u,
adjacency,
meshlets,
uniqueVertexIB, primitiveIndices,
&subset,
maxVerts, maxPrims);
}
_Use_decl_annotations_
HRESULT DirectX::ComputeMeshlets(
const uint16_t* indices,
size_t nFaces,
const XMFLOAT3* positions,
size_t nVerts,
const std::pair<size_t, size_t>* subsets,
size_t nSubsets,
const uint32_t* adjacency,
std::vector<Meshlet>& meshlets,
std::vector<uint8_t>& uniqueVertexIB,
std::vector<MeshletTriangle>& primitiveIndices,
std::pair<size_t, size_t>* meshletSubsets,
size_t maxVerts,
size_t maxPrims)
{
return ComputeMeshletsInternal<uint16_t>(
indices, nFaces,
positions, nVerts,
subsets, nSubsets,
adjacency,
meshlets,
uniqueVertexIB, primitiveIndices, meshletSubsets,
maxVerts, maxPrims);
}
_Use_decl_annotations_
HRESULT DirectX::ComputeMeshlets(
const uint32_t* indices,
size_t nFaces,
const XMFLOAT3* positions,
size_t nVerts,
const std::pair<size_t, size_t>* subsets,
size_t nSubsets,
const uint32_t* adjacency,
std::vector<Meshlet>& meshlets,
std::vector<uint8_t>& uniqueVertexIB,
std::vector<MeshletTriangle>& primitiveIndices,
std::pair<size_t, size_t>* meshletSubsets,
size_t maxVerts,
size_t maxPrims)
{
return ComputeMeshletsInternal<uint32_t>(
indices, nFaces,
positions, nVerts,
subsets, nSubsets,
adjacency,
meshlets,
uniqueVertexIB,
primitiveIndices,
meshletSubsets,
maxVerts, maxPrims);
}
_Use_decl_annotations_
HRESULT DirectX::ComputeCullData(
const XMFLOAT3* positions,
size_t nVerts,
const Meshlet* meshlets,
size_t nMeshlets,
const uint16_t* uniqueVertexIndices,
size_t nVertIndices,
const MeshletTriangle* primitiveIndices,
size_t nPrimIndices,
CullData* cullData,
MESHLET_FLAGS flags) noexcept
{
return ComputeCullDataInternal<uint16_t>(
positions, nVerts,
meshlets, nMeshlets,
uniqueVertexIndices, nVertIndices,
primitiveIndices, nPrimIndices,
cullData, flags);
}
_Use_decl_annotations_
HRESULT DirectX::ComputeCullData(
const XMFLOAT3* positions,
size_t nVerts,
const Meshlet* meshlets,
size_t nMeshlets,
const uint32_t* uniqueVertexIndices,
size_t nVertIndices,
const MeshletTriangle* primitiveIndices,
size_t nPrimIndices,
CullData* cullData,
MESHLET_FLAGS flags) noexcept
{
return ComputeCullDataInternal<uint32_t>(
positions, nVerts,
meshlets, nMeshlets,
uniqueVertexIndices, nVertIndices,
primitiveIndices, nPrimIndices,
cullData,
flags);
}
You can’t perform that action at this time.
