/
D3D12MeshletGenerator.cpp
338 lines (281 loc) · 12.4 KB
/
D3D12MeshletGenerator.cpp
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
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#include "D3D12MeshletGenerator.h"
#include "Generation.h"
#include "Utilities.h"
using namespace DirectX;
namespace
{
inline XMVECTOR QuantizeSNorm(XMVECTOR value)
{
return (XMVectorClamp(value, g_XMNegativeOne, g_XMOne) * 0.5f + XMVectorReplicate(0.5f)) * 255.0f;
}
inline XMVECTOR QuantizeUNorm(XMVECTOR value)
{
return (XMVectorClamp(value, g_XMZero, g_XMOne)) * 255.0f;
}
}
namespace internal
{
template <typename T>
HRESULT ComputeMeshlets(
uint32_t maxVerts, uint32_t maxPrims,
const T* indices, uint32_t indexCount,
const Subset* indexSubsets, uint32_t subsetCount,
const XMFLOAT3* positions, uint32_t vertexCount,
std::vector<Subset>& meshletSubsets,
std::vector<Meshlet>& meshlets,
std::vector<uint8_t>& uniqueVertexIndices,
std::vector<PackedTriangle>& primitiveIndices);
template <typename T>
HRESULT ComputeCullData(
const XMFLOAT3* positions, uint32_t vertexCount,
const Meshlet* meshlets, uint32_t meshletCount,
const T* uniqueVertexIndices,
const PackedTriangle* primitiveIndices,
DWORD flags,
CullData* cullData
);
}
HRESULT ComputeMeshlets(
uint32_t maxVerts, uint32_t maxPrims,
const uint16_t* indices, uint32_t indexCount,
const Subset* indexSubsets, uint32_t subsetCount,
const XMFLOAT3* positions, uint32_t vertexCount,
std::vector<Subset>& meshletSubsets,
std::vector<Meshlet>& meshlets,
std::vector<uint8_t>& uniqueVertexIndices,
std::vector<PackedTriangle>& primitiveIndices)
{
return internal::ComputeMeshlets(maxVerts, maxPrims, indices, indexCount, indexSubsets, subsetCount, positions, vertexCount, meshletSubsets, meshlets, uniqueVertexIndices, primitiveIndices);
}
HRESULT ComputeMeshlets(
uint32_t maxVerts, uint32_t maxPrims,
const uint32_t* indices, uint32_t indexCount,
const Subset* indexSubsets, uint32_t subsetCount,
const XMFLOAT3* positions, uint32_t vertexCount,
std::vector<Subset>& meshletSubsets,
std::vector<Meshlet>& meshlets,
std::vector<uint8_t>& uniqueVertexIndices,
std::vector<PackedTriangle>& primitiveIndices)
{
return internal::ComputeMeshlets(maxVerts, maxPrims, indices, indexCount, indexSubsets, subsetCount, positions, vertexCount, meshletSubsets, meshlets, uniqueVertexIndices, primitiveIndices);
}
HRESULT ComputeMeshlets(
uint32_t maxVerts, uint32_t maxPrims,
const uint16_t* indices, uint32_t indexCount,
const XMFLOAT3* positions, uint32_t vertexCount,
std::vector<Subset>& meshletSubsets,
std::vector<Meshlet>& meshlets,
std::vector<uint8_t>& uniqueVertexIndices,
std::vector<PackedTriangle>& primitiveIndices)
{
Subset s = { 0, indexCount };
return internal::ComputeMeshlets(maxVerts, maxPrims, indices, indexCount, &s, 1u, positions, vertexCount, meshletSubsets, meshlets, uniqueVertexIndices, primitiveIndices);
}
HRESULT ComputeMeshlets(
uint32_t maxVerts, uint32_t maxPrims,
const uint32_t* indices, uint32_t indexCount,
const XMFLOAT3* positions, uint32_t vertexCount,
std::vector<Subset>& meshletSubsets,
std::vector<Meshlet>& meshlets,
std::vector<uint8_t>& uniqueVertexIndices,
std::vector<PackedTriangle>& primitiveIndices)
{
Subset s = { 0, indexCount };
return internal::ComputeMeshlets(maxVerts, maxPrims, indices, indexCount, &s, 1u, positions, vertexCount, meshletSubsets, meshlets, uniqueVertexIndices, primitiveIndices);
}
HRESULT ComputeCullData(
const XMFLOAT3* positions, uint32_t vertexCount,
const Meshlet* meshlets, uint32_t meshletCount,
const uint16_t* uniqueVertexIndices,
const PackedTriangle* primitiveIndices,
DWORD flags,
CullData* cullData
)
{
return internal::ComputeCullData(positions, vertexCount, meshlets, meshletCount, uniqueVertexIndices, primitiveIndices, flags, cullData);
}
HRESULT ComputeCullData(
const XMFLOAT3* positions, uint32_t vertexCount,
const Meshlet* meshlets, uint32_t meshletCount,
const uint32_t* uniqueVertexIndices,
const PackedTriangle* primitiveIndices,
DWORD flags,
CullData* cullData
)
{
return internal::ComputeCullData(positions, vertexCount, meshlets, meshletCount, uniqueVertexIndices, primitiveIndices, flags, cullData);
}
template <typename T>
HRESULT internal::ComputeMeshlets(
uint32_t maxVerts, uint32_t maxPrims,
const T* indices, uint32_t indexCount,
const Subset* indexSubsets, uint32_t subsetCount,
const DirectX::XMFLOAT3* positions, uint32_t vertexCount,
std::vector<Subset>& meshletSubsets,
std::vector<Meshlet>& meshlets,
std::vector<uint8_t>& uniqueVertexIndices,
std::vector<PackedTriangle>& primitiveIndices)
{
UNREFERENCED_PARAMETER(indexCount);
for (uint32_t i = 0; i < subsetCount; ++i)
{
Subset s = indexSubsets[i];
assert(s.Offset + s.Count <= indexCount);
std::vector<InlineMeshlet<T>> builtMeshlets;
Meshletize(maxVerts, maxPrims, indices + s.Offset, s.Count, positions, vertexCount, builtMeshlets);
Subset meshletSubset;
meshletSubset.Offset = static_cast<uint32_t>(meshlets.size());
meshletSubset.Count = static_cast<uint32_t>(builtMeshlets.size());
meshletSubsets.push_back(meshletSubset);
// Determine final unique vertex index and primitive index counts & offsets.
uint32_t startVertCount = static_cast<uint32_t>(uniqueVertexIndices.size()) / sizeof(T);
uint32_t startPrimCount = static_cast<uint32_t>(primitiveIndices.size());
uint32_t uniqueVertexIndexCount = startVertCount;
uint32_t primitiveIndexCount = startPrimCount;
// Resize the meshlet output array to hold the newly formed meshlets.
uint32_t meshletCount = static_cast<uint32_t>(meshlets.size());
meshlets.resize(meshletCount + builtMeshlets.size());
for (uint32_t j = 0, dest = meshletCount; j < static_cast<uint32_t>(builtMeshlets.size()); ++j, ++dest)
{
meshlets[dest].VertOffset = uniqueVertexIndexCount;
meshlets[dest].VertCount = static_cast<uint32_t>(builtMeshlets[j].UniqueVertexIndices.size());
uniqueVertexIndexCount += static_cast<uint32_t>(builtMeshlets[j].UniqueVertexIndices.size());
meshlets[dest].PrimOffset = primitiveIndexCount;
meshlets[dest].PrimCount = static_cast<uint32_t>(builtMeshlets[j].PrimitiveIndices.size());
primitiveIndexCount += static_cast<uint32_t>(builtMeshlets[j].PrimitiveIndices.size());
}
// Allocate space for the new data.
uniqueVertexIndices.resize(uniqueVertexIndexCount * sizeof(T));
primitiveIndices.resize(primitiveIndexCount);
// Copy data from the freshly built meshlets into the output buffers.
auto vertDest = reinterpret_cast<T*>(uniqueVertexIndices.data()) + startVertCount;
auto primDest = reinterpret_cast<uint32_t*>(primitiveIndices.data()) + startPrimCount;
for (uint32_t j = 0; j < static_cast<uint32_t>(builtMeshlets.size()); ++j)
{
std::memcpy(vertDest, builtMeshlets[j].UniqueVertexIndices.data(), builtMeshlets[j].UniqueVertexIndices.size() * sizeof(T));
std::memcpy(primDest, builtMeshlets[j].PrimitiveIndices.data(), builtMeshlets[j].PrimitiveIndices.size() * sizeof(uint32_t));
vertDest += builtMeshlets[j].UniqueVertexIndices.size();
primDest += builtMeshlets[j].PrimitiveIndices.size();
}
}
return S_OK;
}
//
// Strongly influenced by https://github.com/zeux/meshoptimizer - Thanks amigo!
//
template <typename T>
HRESULT internal::ComputeCullData(
const XMFLOAT3* positions, uint32_t vertexCount,
const Meshlet* meshlets, uint32_t meshletCount,
const T* uniqueVertexIndices,
const PackedTriangle* primitiveIndices,
DWORD flags,
CullData* cullData
)
{
UNREFERENCED_PARAMETER(vertexCount);
XMFLOAT3 vertices[256];
XMFLOAT3 normals[256];
for (uint32_t mi = 0; mi < meshletCount; ++mi)
{
auto& m = meshlets[mi];
auto& c = cullData[mi];
// Cache vertices
for (uint32_t i = 0; i < m.VertCount; ++i)
{
uint32_t vIndex = uniqueVertexIndices[m.VertOffset + i];
assert(vIndex < vertexCount);
vertices[i] = positions[vIndex];
}
// Generate primitive normals & cache
for (uint32_t i = 0; i < m.PrimCount; ++i)
{
auto primitive = primitiveIndices[m.PrimOffset + i];
XMVECTOR triangle[3]
{
XMLoadFloat3(&vertices[primitive.indices.i0]),
XMLoadFloat3(&vertices[primitive.indices.i1]),
XMLoadFloat3(&vertices[primitive.indices.i2]),
};
XMVECTOR p10 = triangle[1] - triangle[0];
XMVECTOR p20 = triangle[2] - triangle[0];
XMVECTOR n = XMVector3Normalize(XMVector3Cross(p10, p20));
XMStoreFloat3(&normals[i], (flags & CNORM_WIND_CW) != 0 ? -n : n);
}
// Calculate spatial bounds
XMVECTOR positionBounds = MinimumBoundingSphere(vertices, m.VertCount);
XMStoreFloat4(&c.BoundingSphere, positionBounds);
// Calculate the normal cone
// 1. Normalized center point of minimum bounding sphere of unit normals == conic axis
XMVECTOR normalBounds = MinimumBoundingSphere(normals, m.PrimCount);
// 2. Calculate dot product of all normals to conic axis, selecting minimum
XMVECTOR axis = XMVectorSetW(XMVector3Normalize(normalBounds), 0);
XMVECTOR minDot = g_XMOne;
for (uint32_t i = 0; i < m.PrimCount; ++i)
{
XMVECTOR dot = XMVector3Dot(axis, XMLoadFloat3(&normals[i]));
minDot = XMVectorMin(minDot, dot);
}
if (XMVector4Less(minDot, XMVectorReplicate(0.1f)))
{
// Degenerate cone
c.NormalCone[0] = 127;
c.NormalCone[1] = 127;
c.NormalCone[2] = 127;
c.NormalCone[3] = 255;
continue;
}
// Find the point on center-t*axis ray that lies in negative half-space of all triangles
float maxt = 0;
for (uint32_t i = 0; i < m.PrimCount; ++i)
{
auto primitive = primitiveIndices[m.PrimOffset + i];
uint32_t indices[3]
{
primitive.indices.i0,
primitive.indices.i1,
primitive.indices.i2,
};
XMVECTOR triangle[3]
{
XMLoadFloat3(&vertices[indices[0]]),
XMLoadFloat3(&vertices[indices[1]]),
XMLoadFloat3(&vertices[indices[2]]),
};
XMVECTOR c = positionBounds - triangle[0];
XMVECTOR n = XMLoadFloat3(&normals[i]);
float dc = XMVectorGetX(XMVector3Dot(c, n));
float dn = XMVectorGetX(XMVector3Dot(axis, n));
// dn should be larger than mindp cutoff above
assert(dn > 0.0f);
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 coneCutoff = XMVectorSqrt(g_XMOne - minDot * minDot);
// 3. Quantize to uint8
XMVECTOR quantized = QuantizeSNorm(axis);
c.NormalCone[0] = (uint8_t)XMVectorGetX(quantized);
c.NormalCone[1] = (uint8_t)XMVectorGetY(quantized);
c.NormalCone[2] = (uint8_t)XMVectorGetZ(quantized);
XMVECTOR error = ((quantized / 255.0f) * 2.0f - g_XMOne) - axis;
error = XMVectorSum(XMVectorAbs(error));
quantized = QuantizeUNorm(coneCutoff + error);
quantized = XMVectorMin(quantized + g_XMOne, XMVectorReplicate(255.0f));
c.NormalCone[3] = (uint8_t)XMVectorGetX(quantized);
}
return S_OK;
}