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 pathDirectXMeshUtil.cpp
More file actions
515 lines (423 loc) · 14.5 KB
/
Copy pathDirectXMeshUtil.cpp
File metadata and controls
515 lines (423 loc) · 14.5 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
//-------------------------------------------------------------------------------------
// DirectXMeshUtil.cpp
//
// DirectX Mesh Geometry Library - Utilities
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// http://go.microsoft.com/fwlink/?LinkID=324981
//-------------------------------------------------------------------------------------
#include "DirectXMeshP.h"
using namespace DirectX;
#if defined(_XBOX_ONE) && defined(_TITLE)
static_assert(XBOX_DXGI_FORMAT_R10G10B10_SNORM_A2_UNORM == DXGI_FORMAT_R10G10B10_SNORM_A2_UNORM, "Xbox One XDK mismatch detected");
#endif
//=====================================================================================
// DXGI Format Utilities
//=====================================================================================
//-------------------------------------------------------------------------------------
// Returns bytes-per-element for a given DXGI format, or 0 on failure
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
size_t DirectX::BytesPerElement(DXGI_FORMAT fmt) noexcept
{
// This list only includes those formats that are valid for use by IB or VB
switch (static_cast<int>(fmt))
{
case DXGI_FORMAT_R32G32B32A32_FLOAT:
case DXGI_FORMAT_R32G32B32A32_UINT:
case DXGI_FORMAT_R32G32B32A32_SINT:
return 16;
case DXGI_FORMAT_R32G32B32_FLOAT:
case DXGI_FORMAT_R32G32B32_UINT:
case DXGI_FORMAT_R32G32B32_SINT:
return 12;
case DXGI_FORMAT_R16G16B16A16_FLOAT:
case DXGI_FORMAT_R16G16B16A16_UNORM:
case DXGI_FORMAT_R16G16B16A16_UINT:
case DXGI_FORMAT_R16G16B16A16_SNORM:
case DXGI_FORMAT_R16G16B16A16_SINT:
case DXGI_FORMAT_R32G32_FLOAT:
case DXGI_FORMAT_R32G32_UINT:
case DXGI_FORMAT_R32G32_SINT:
return 8;
case DXGI_FORMAT_R10G10B10A2_UNORM:
case DXGI_FORMAT_R10G10B10A2_UINT:
case DXGI_FORMAT_R11G11B10_FLOAT:
case DXGI_FORMAT_R8G8B8A8_UNORM:
case DXGI_FORMAT_R8G8B8A8_UINT:
case DXGI_FORMAT_R8G8B8A8_SNORM:
case DXGI_FORMAT_R8G8B8A8_SINT:
case DXGI_FORMAT_R16G16_FLOAT:
case DXGI_FORMAT_R16G16_UNORM:
case DXGI_FORMAT_R16G16_UINT:
case DXGI_FORMAT_R16G16_SNORM:
case DXGI_FORMAT_R16G16_SINT:
case DXGI_FORMAT_R32_FLOAT:
case DXGI_FORMAT_R32_UINT:
case DXGI_FORMAT_R32_SINT:
case DXGI_FORMAT_B8G8R8A8_UNORM:
case DXGI_FORMAT_B8G8R8X8_UNORM:
case XBOX_DXGI_FORMAT_R10G10B10_SNORM_A2_UNORM:
return 4;
case DXGI_FORMAT_R8G8_UNORM:
case DXGI_FORMAT_R8G8_UINT:
case DXGI_FORMAT_R8G8_SNORM:
case DXGI_FORMAT_R8G8_SINT:
case DXGI_FORMAT_R16_FLOAT:
case DXGI_FORMAT_R16_UNORM:
case DXGI_FORMAT_R16_UINT:
case DXGI_FORMAT_R16_SNORM:
case DXGI_FORMAT_R16_SINT:
case DXGI_FORMAT_B5G6R5_UNORM:
case DXGI_FORMAT_B5G5R5A1_UNORM:
return 2;
case DXGI_FORMAT_R8_UNORM:
case DXGI_FORMAT_R8_UINT:
case DXGI_FORMAT_R8_SNORM:
case DXGI_FORMAT_R8_SINT:
return 1;
case DXGI_FORMAT_B4G4R4A4_UNORM:
return 2;
default:
// No BC, sRGB, XRBias, SharedExp, Typeless, Depth, or Video formats
return 0;
}
}
//=====================================================================================
// Input Layout Descriptor Utilities
//=====================================================================================
//-------------------------------------------------------------------------------------
// Validates a Direct3D Input Layout
//-------------------------------------------------------------------------------------
#if defined(__d3d11_h__) || defined(__d3d11_x_h__)
_Use_decl_annotations_
bool DirectX::IsValid(const D3D11_INPUT_ELEMENT_DESC* vbDecl, size_t nDecl) noexcept
{
if (!vbDecl || !nDecl)
{
// Note that 0 is allowed by the runtime for degenerate cases, but is not defined for DirectXMesh
return false;
}
if (nDecl > D3D11_IA_VERTEX_INPUT_STRUCTURE_ELEMENT_COUNT)
{
// The upper-limit depends on feature level, so we assume highest value here
return false;
}
for (size_t j = 0; j < nDecl; ++j)
{
size_t bpe = BytesPerElement(vbDecl[j].Format);
if (!bpe)
{
// Not a valid DXGI format or it's not valid for VB usage
return false;
}
uint32_t alignment;
if (bpe == 1)
alignment = 1;
else if (bpe == 2)
alignment = 2;
else
alignment = 4;
if ((vbDecl[j].AlignedByteOffset != D3D11_APPEND_ALIGNED_ELEMENT)
&& (vbDecl[j].AlignedByteOffset % alignment) != 0)
{
// Invalid alignment for element
return false;
}
if (vbDecl[j].InputSlot >= D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT)
{
// The upper-limit depends on feature level, so we assume highest value here
return false;
}
switch (vbDecl[j].InputSlotClass)
{
case D3D11_INPUT_PER_VERTEX_DATA:
if (vbDecl[j].InstanceDataStepRate != 0)
{
return false;
}
break;
case D3D11_INPUT_PER_INSTANCE_DATA:
break;
default:
return false;
}
if (!vbDecl[j].SemanticName)
{
return false;
}
// Debug layer also checks for trailing digit in the semantic name, and checks
// for inconsistent semantic name/slot assignment.
}
return true;
}
#endif
#if defined(__d3d12_h__) || defined(__d3d12_x_h__) || defined(__XBOX_D3D12_X__)
bool DirectX::IsValid(const D3D12_INPUT_LAYOUT_DESC& vbDecl) noexcept
{
if (!vbDecl.pInputElementDescs || !vbDecl.NumElements)
{
// Note that 0 is allowed by the runtime for degenerate cases, but is not defined for DirectXMesh
return false;
}
if (vbDecl.NumElements > D3D12_IA_VERTEX_INPUT_STRUCTURE_ELEMENT_COUNT)
{
return false;
}
for (size_t j = 0; j < vbDecl.NumElements; ++j)
{
size_t bpe = BytesPerElement(vbDecl.pInputElementDescs[j].Format);
if (!bpe)
{
// Not a valid DXGI format or it's not valid for VB usage
return false;
}
uint32_t alignment;
if (bpe == 1)
alignment = 1;
else if (bpe == 2)
alignment = 2;
else
alignment = 4;
if ((vbDecl.pInputElementDescs[j].AlignedByteOffset != D3D12_APPEND_ALIGNED_ELEMENT)
&& (vbDecl.pInputElementDescs[j].AlignedByteOffset % alignment) != 0)
{
// Invalid alignment for element
return false;
}
if (vbDecl.pInputElementDescs[j].InputSlot >= D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT)
{
// The upper-limit depends on feature level, so we assume highest value here
return false;
}
switch (vbDecl.pInputElementDescs[j].InputSlotClass)
{
case D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA:
if (vbDecl.pInputElementDescs[j].InstanceDataStepRate != 0)
{
return false;
}
break;
case D3D12_INPUT_CLASSIFICATION_PER_INSTANCE_DATA:
break;
default:
return false;
}
if (!vbDecl.pInputElementDescs[j].SemanticName)
{
return false;
}
// Debug layer also checks for trailing digit in the semantic name, and checks
// for inconsistent semantic name/slot assignment.
}
return true;
}
#endif
//-------------------------------------------------------------------------------------
// Compute the offsets to each element, and total stride of each slot
//-------------------------------------------------------------------------------------
#if defined(__d3d11_h__) || defined(__d3d11_x_h__)
_Use_decl_annotations_
void DirectX::ComputeInputLayout(
const D3D11_INPUT_ELEMENT_DESC* vbDecl, size_t nDecl,
uint32_t* offsets, uint32_t* strides) noexcept
{
assert(IsValid(vbDecl, nDecl));
if (offsets)
memset(offsets, 0, sizeof(uint32_t) * nDecl);
if (strides)
memset(strides, 0, sizeof(uint32_t) * D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT);
uint32_t prevABO[D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT];
memset(prevABO, 0, sizeof(uint32_t) * D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT);
for (size_t j = 0; j < nDecl; ++j)
{
uint32_t slot = vbDecl[j].InputSlot;
if (slot >= D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT)
{
// ignore bad input slots
continue;
}
size_t bpe = BytesPerElement(vbDecl[j].Format);
if (!bpe)
{
// ignore invalid format
continue;
}
uint32_t alignment;
if (bpe == 1)
alignment = 1;
else if (bpe == 2)
alignment = 2;
else
alignment = 4;
uint32_t alignedByteOffset = vbDecl[j].AlignedByteOffset;
if (alignedByteOffset == D3D11_APPEND_ALIGNED_ELEMENT)
{
alignedByteOffset = prevABO[slot];
}
if (offsets)
{
offsets[j] = alignedByteOffset;
}
if (strides)
{
uint32_t istride = uint32_t(alignedByteOffset + bpe);
strides[slot] = std::max<uint32_t>(strides[slot], istride);
}
prevABO[slot] = uint32_t(alignedByteOffset + bpe + (bpe % alignment));
}
}
#endif
#if defined(__d3d12_h__) || defined(__d3d12_x_h__) || defined(__XBOX_D3D12_X__)
_Use_decl_annotations_
void DirectX::ComputeInputLayout(const D3D12_INPUT_LAYOUT_DESC& vbDecl,
uint32_t* offsets, uint32_t* strides) noexcept
{
assert(IsValid(vbDecl));
if (offsets)
memset(offsets, 0, sizeof(uint32_t) * vbDecl.NumElements);
if (strides)
memset(strides, 0, sizeof(uint32_t) * D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT);
uint32_t prevABO[D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT] = {};
for (size_t j = 0; j < vbDecl.NumElements; ++j)
{
uint32_t slot = vbDecl.pInputElementDescs[j].InputSlot;
if (slot >= D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT)
{
// ignore bad input slots
continue;
}
size_t bpe = BytesPerElement(vbDecl.pInputElementDescs[j].Format);
if (!bpe)
{
// ignore invalid format
continue;
}
uint32_t alignment;
if (bpe == 1)
alignment = 1;
else if (bpe == 2)
alignment = 2;
else
alignment = 4;
uint32_t alignedByteOffset = vbDecl.pInputElementDescs[j].AlignedByteOffset;
if (alignedByteOffset == D3D12_APPEND_ALIGNED_ELEMENT)
{
alignedByteOffset = prevABO[slot];
}
if (offsets)
{
offsets[j] = alignedByteOffset;
}
if (strides)
{
uint32_t istride = uint32_t(alignedByteOffset + bpe);
strides[slot] = std::max<uint32_t>(strides[slot], istride);
}
prevABO[slot] = uint32_t(alignedByteOffset + bpe + (bpe % alignment));
}
}
#endif
//=====================================================================================
// Attribute Utilities
//=====================================================================================
_Use_decl_annotations_
std::vector<std::pair<size_t, size_t>> DirectX::ComputeSubsets(const uint32_t* attributes, size_t nFaces)
{
std::vector<std::pair<size_t, size_t>> subsets;
if (!nFaces)
return subsets;
if (!attributes)
{
subsets.emplace_back(std::pair<size_t, size_t>(0u, nFaces));
return subsets;
}
uint32_t lastAttr = attributes[0];
size_t offset = 0;
size_t count = 1;
for (size_t j = 1; j < nFaces; ++j)
{
if (attributes[j] != lastAttr)
{
subsets.emplace_back(std::pair<size_t, size_t>(offset, count));
lastAttr = attributes[j];
offset = j;
count = 1;
}
else
{
count += 1;
}
}
if (count > 0)
{
subsets.emplace_back(std::pair<size_t, size_t>(offset, count));
}
return subsets;
}
//=====================================================================================
// Mesh Optimization Utilities
//=====================================================================================
namespace
{
template<class index_t>
void ComputeVertexCacheMissRateImpl(
_In_reads_(nFaces * 3) const index_t* indices, size_t nFaces, size_t nVerts, size_t cacheSize,
float& acmr, float& atvr)
{
acmr = -1.f;
atvr = -1.f;
if (!indices || !nFaces || !nVerts || !cacheSize)
return;
if ((uint64_t(nFaces) * 3) >= UINT32_MAX)
return;
if (nVerts >= index_t(-1))
return;
size_t misses = 0;
std::unique_ptr<uint32_t[]> fifo(new uint32_t[cacheSize]);
size_t tail = 0;
memset(fifo.get(), 0xff, sizeof(uint32_t) * cacheSize);
for (size_t j = 0; j < (nFaces * 3); ++j)
{
if (indices[j] == index_t(-1))
continue;
bool found = false;
for (size_t ptr = 0; ptr < cacheSize; ++ptr)
{
if (fifo[ptr] == indices[j])
{
found = true;
break;
}
}
if (!found)
{
++misses;
fifo[tail] = indices[j];
++tail;
if (tail == cacheSize) tail = 0;
}
}
// ideal is 0.5, individual tris have 3.0
acmr = float(misses) / float(nFaces);
// ideal is 1.0, worst case is 6.0
atvr = float(misses) / float(nVerts);
}
}
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
void DirectX::ComputeVertexCacheMissRate(
const uint16_t* indices, size_t nFaces, size_t nVerts, size_t cacheSize,
float& acmr, float& atvr)
{
ComputeVertexCacheMissRateImpl<uint16_t>(indices, nFaces, nVerts, cacheSize, acmr, atvr);
}
_Use_decl_annotations_
void DirectX::ComputeVertexCacheMissRate(
const uint32_t* indices, size_t nFaces, size_t nVerts, size_t cacheSize,
float& acmr, float& atvr)
{
ComputeVertexCacheMissRateImpl<uint32_t>(indices, nFaces, nVerts, cacheSize, acmr, atvr);
}
You can’t perform that action at this time.
