-
Notifications
You must be signed in to change notification settings - Fork 2
/
process.cpp
408 lines (339 loc) · 13 KB
/
process.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
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
//-------------------------------------------------------------------------------------
// process.cpp
//
// Copyright (c) Microsoft Corporation.
//-------------------------------------------------------------------------------------
#include "directxtest.h"
#include "UVAtlas.h"
#include "DirectXMesh.h"
#include "TestHelpers.h"
#include "WaveFrontReader.h"
using namespace DirectX;
struct TestMedia
{
const wchar_t * fname;
};
static const TestMedia g_TestMedia16[] =
{
// filename
{ MESH_MEDIA_PATH L"cup._obj", },
{ MESH_MEDIA_PATH L"teapot._obj", },
#ifndef BUILD_BVT_ONLY
{ MESH_MEDIA_PATH L"SuperSimpleRunner._obj", },
{ MESH_MEDIA_PATH L"shuttle._obj", },
{ MESH_MEDIA_PATH L"player_ship_a._obj", },
{ MESH_MEDIA_PATH L"FSEngineGeo._obj", },
{ MESH_MEDIA_PATH L"sphere.vbo", },
{ MESH_MEDIA_PATH L"cylinder.vbo", },
{ MESH_MEDIA_PATH L"torus.vbo", },
#endif
};
#ifndef BUILD_BVT_ONLY
static const TestMedia g_TestMedia32[] =
{
// filename
{ MESH_MEDIA_PATH L"Head_Big_Ears._obj", },
{ MESH_MEDIA_PATH L"John40k._obj", },
};
#endif
//-------------------------------------------------------------------------------------
static inline bool IsValidFacePartition( const uint32_t* facePart, size_t nFaces, size_t nCharts )
{
if ( !facePart )
return false;
for( size_t j = 0; j < nFaces; ++j )
{
if ( facePart[j] >= nCharts )
return false;
}
return true;
}
//-------------------------------------------------------------------------------------
static inline bool VerifyVertices( const XMFLOAT3* pos, size_t nVerts, const UVAtlasVertex* verts, const uint32_t* remap, size_t nTotalVerts )
{
if ( !pos || !verts || !remap )
return false;
auto vptr = verts;
for( size_t j = 0; j < nTotalVerts; ++j, ++vptr )
{
size_t oldn = remap[j];
if ( oldn >= nVerts )
return false;
XMVECTOR v1 = XMLoadFloat3( &pos[ oldn ] );
XMVECTOR v2 = XMLoadFloat3( &vptr->pos );
if ( !XMVector3NearEqual( v1, v2, g_XMEpsilon ) )
return false;
}
return true;
}
//-------------------------------------------------------------------------------------
static HRESULT __cdecl UVAtlasCallback( float fPercentDone )
{
UNREFERENCED_PARAMETER(fPercentDone);
static ULONGLONG s_lastTick = 0;
ULONGLONG tick = GetTickCount64();
if ( ( tick - s_lastTick ) > 1000 )
{
print(".");
s_lastTick = tick;
}
return S_OK;
}
//-------------------------------------------------------------------------------------
// MeshProcess (16-bit)
bool Test08()
{
bool success = true;
size_t ncount = 0;
size_t npass = 0;
for( size_t index=0; index < std::size(g_TestMedia16); ++index )
{
wchar_t szPath[MAX_PATH] = {};
DWORD ret = ExpandEnvironmentStringsW( g_TestMedia16[index].fname, szPath, MAX_PATH );
if ( !ret || ret > MAX_PATH )
{
printe( "ERROR: ExpandEnvironmentStrings FAILED\n" );
return false;
}
#ifdef _DEBUG
OutputDebugStringW(szPath);
OutputDebugStringA("\n");
#endif
wchar_t ext[_MAX_EXT];
_wsplitpath_s( szPath, nullptr, 0, nullptr, 0, nullptr, 0, ext, _MAX_EXT );
++ncount;
std::unique_ptr<DX::WaveFrontReader<uint16_t>> mesh(new DX::WaveFrontReader<uint16_t>());
print( "*" );
HRESULT hr;
if ( _wcsicmp( ext, L".vbo" ) == 0 )
{
hr = mesh->LoadVBO( szPath );
}
else
{
hr = mesh->Load( szPath );
}
if ( FAILED(hr) )
{
success = false;
printe( "ERROR: Failed loading mesh data (%08X):\n%S\n", static_cast<unsigned int>(hr), szPath );
continue;
}
size_t nFaces = mesh->indices.size() / 3;
size_t nVerts = mesh->vertices.size();
#ifdef _DEBUG
char output[ 256 ] = {};
sprintf_s( output, "INFO: %zu verts, %zu faces\n", nVerts, nFaces );
OutputDebugStringA( output );
#endif
std::wstring msgs;
hr = Validate( mesh->indices.data(), nFaces, nVerts, nullptr, VALIDATE_DEFAULT, &msgs );
if ( FAILED(hr) )
{
success = false;
printe( "ERROR: Failed Validate mesh data (%08X):\n%S\n%S\n", static_cast<unsigned int>(hr), szPath, msgs.c_str() );
continue;
}
#ifdef _DEBUG
hr = Validate( mesh->indices.data(), nFaces, nVerts, nullptr, VALIDATE_DEGENERATE, &msgs );
if ( FAILED(hr) )
{
OutputDebugStringW( msgs.c_str() );
}
#endif
std::unique_ptr<XMFLOAT3[]> pos( new XMFLOAT3[ nVerts ] );
for( size_t j = 0; j < nVerts; ++j )
pos[ j ] = mesh->vertices[ j ].position;
std::unique_ptr<uint32_t[]> adj( new uint32_t[ mesh->indices.size() ] );
memset( adj.get(), 0xff, sizeof(uint32_t) * mesh->indices.size() );
hr = GenerateAdjacencyAndPointReps( mesh->indices.data(), nFaces, pos.get(), nVerts, 0.f, nullptr, adj.get() );
if ( FAILED(hr) )
{
success = false;
printe("ERROR: failed GenerateAdjacencyAndPointReps (%08X)\n:%S\n", static_cast<unsigned int>(hr), szPath );
continue;
}
std::vector<UVAtlasVertex> vb;
std::vector<uint8_t> ib;
std::vector<uint32_t> facePart;
std::vector<uint32_t> remap;
float maxStretch = 0.f;
size_t numCharts = 0;
hr = UVAtlasCreate( pos.get(), nVerts, mesh->indices.data(), DXGI_FORMAT_R16_UINT, nFaces,
0, 0.f, 512, 512, 1.f,
adj.get(), nullptr, nullptr, UVAtlasCallback, UVATLAS_DEFAULT_CALLBACK_FREQUENCY,
UVATLAS_DEFAULT, vb, ib, &facePart, &remap, &maxStretch, &numCharts );
if (FAILED(hr))
{
printe( "\nERROR: create atlas failed (%08X)\n%S\n", static_cast<unsigned int>(hr), szPath );
success = false;
}
else if ( vb.size() < nVerts
|| (ib.size() / (sizeof(uint16_t)*3)) != nFaces
|| facePart.size() != nFaces
|| remap.size() != vb.size()
|| !numCharts )
{
printe( "\nERROR: Unexpected results from create atlas:\n%S\n\tverts %zu\n\tfaces %zu (%zu bytes)\n\tface partitions %zu\n\tremap array %zu\n\tmaxStretch %f\n\tnumCharts %zu\n",
szPath, vb.size(), nFaces, ib.size(), facePart.size(), remap.size(), maxStretch, numCharts );
success = false;
}
else if ( !IsValidVertexRemap( reinterpret_cast<const uint16_t*>( ib.data() ), nFaces, remap.data(), vb.size(), true ) )
{
printe( "\nERROR: Vertex remap invalid from create atlas:\n%S\n", szPath );
success = false;
}
else if ( !IsValidFacePartition( facePart.data(), nFaces, numCharts ) )
{
printe( "\nERROR: Face partition invalid from create atlas:\n%S\n", szPath );
success = false;
}
else if ( !VerifyVertices( pos.get(), nVerts, vb.data(), remap.data(), vb.size() ) )
{
printe( "\nERROR: Vertex data doesn't match remap:\n%S\n", szPath );
success = false;
}
else
{
hr = Validate( reinterpret_cast<const uint16_t*>( ib.data() ), nFaces, vb.size(), nullptr, VALIDATE_DEFAULT, &msgs );
if ( FAILED(hr) )
{
printe( "\nERROR: Invalid index buffer from create atlas (%08X):\n%S\n%S\n", static_cast<unsigned int>(hr), szPath, msgs.c_str() );
success = false;
}
}
++npass;
}
print("\n%zu meshes tested, %zu meshes passed ", ncount, npass );
return success;
}
//-------------------------------------------------------------------------------------
// MeshProcess (32-bit)
#ifndef BUILD_BVT_ONLY
bool Test11()
{
bool success = true;
size_t ncount = 0;
size_t npass = 0;
for (size_t index = 0; index < std::size(g_TestMedia32); ++index)
{
wchar_t szPath[MAX_PATH] = {};
DWORD ret = ExpandEnvironmentStringsW(g_TestMedia32[index].fname, szPath, MAX_PATH);
if (!ret || ret > MAX_PATH)
{
printe("ERROR: ExpandEnvironmentStrings FAILED\n");
return false;
}
#ifdef _DEBUG
OutputDebugStringW(szPath);
OutputDebugStringA("\n");
#endif
wchar_t ext[_MAX_EXT];
_wsplitpath_s(szPath, nullptr, 0, nullptr, 0, nullptr, 0, ext, _MAX_EXT);
++ncount;
std::unique_ptr<DX::WaveFrontReader<uint32_t>> mesh(new DX::WaveFrontReader<uint32_t>());
print("*");
HRESULT hr;
if (_wcsicmp(ext, L".vbo") == 0)
{
hr = mesh->LoadVBO(szPath);
}
else
{
hr = mesh->Load(szPath);
}
if (FAILED(hr))
{
success = false;
printe("ERROR: Failed loading mesh data (%08X):\n%S\n", static_cast<unsigned int>(hr), szPath);
continue;
}
size_t nFaces = mesh->indices.size() / 3;
size_t nVerts = mesh->vertices.size();
#ifdef _DEBUG
char output[256] = {};
sprintf_s(output, "INFO: %zu verts, %zu faces\n", nVerts, nFaces);
OutputDebugStringA(output);
#endif
std::wstring msgs;
hr = Validate(mesh->indices.data(), nFaces, nVerts, nullptr, VALIDATE_DEFAULT, &msgs);
if (FAILED(hr))
{
success = false;
printe("ERROR: Failed Validate mesh data (%08X):\n%S\n%S\n", static_cast<unsigned int>(hr), szPath, msgs.c_str());
continue;
}
#ifdef _DEBUG
hr = Validate(mesh->indices.data(), nFaces, nVerts, nullptr, VALIDATE_DEGENERATE, &msgs);
if (FAILED(hr))
{
OutputDebugStringW(msgs.c_str());
}
#endif
std::unique_ptr<XMFLOAT3[]> pos(new XMFLOAT3[nVerts]);
for (size_t j = 0; j < nVerts; ++j)
pos[j] = mesh->vertices[j].position;
std::unique_ptr<uint32_t[]> adj(new uint32_t[mesh->indices.size()]);
memset(adj.get(), 0xff, sizeof(uint32_t) * mesh->indices.size());
hr = GenerateAdjacencyAndPointReps(mesh->indices.data(), nFaces, pos.get(), nVerts, 0.f, nullptr, adj.get());
if (FAILED(hr))
{
success = false;
printe("ERROR: failed GenerateAdjacencyAndPointReps (%08X)\n:%S\n", static_cast<unsigned int>(hr), szPath);
continue;
}
std::vector<UVAtlasVertex> vb;
std::vector<uint8_t> ib;
std::vector<uint32_t> facePart;
std::vector<uint32_t> remap;
float maxStretch = 0.f;
size_t numCharts = 0;
hr = UVAtlasCreate(pos.get(), nVerts, mesh->indices.data(), DXGI_FORMAT_R32_UINT, nFaces,
0, 0.f, 512, 512, 1.f,
adj.get(), nullptr, nullptr, UVAtlasCallback, UVATLAS_DEFAULT_CALLBACK_FREQUENCY,
UVATLAS_DEFAULT, vb, ib, &facePart, &remap, &maxStretch, &numCharts);
if (FAILED(hr))
{
printe("\nERROR: create atlas failed (%08X)\n%S\n", static_cast<unsigned int>(hr), szPath);
success = false;
}
else if (vb.size() < nVerts
|| (ib.size() / (sizeof(uint32_t) * 3)) != nFaces
|| facePart.size() != nFaces
|| remap.size() != vb.size()
|| !numCharts)
{
printe("\nERROR: Unexpected results from create atlas:\n%S\n\tverts %zu\n\tfaces %zu (%zu bytes)\n\tface partitions %zu\n\tremap array %zu\n\tmaxStretch %f\n\tnumCharts %zu\n",
szPath, vb.size(), nFaces, ib.size(), facePart.size(), remap.size(), maxStretch, numCharts);
success = false;
}
else if (!IsValidVertexRemap(reinterpret_cast<const uint32_t*>(ib.data()), nFaces, remap.data(), vb.size(), true))
{
printe("\nERROR: Vertex remap invalid from create atlas:\n%S\n", szPath);
success = false;
}
else if (!IsValidFacePartition(facePart.data(), nFaces, numCharts))
{
printe("\nERROR: Face partition invalid from create atlas:\n%S\n", szPath);
success = false;
}
else if (!VerifyVertices(pos.get(), nVerts, vb.data(), remap.data(), vb.size()))
{
printe("\nERROR: Vertex data doesn't match remap:\n%S\n", szPath);
success = false;
}
else
{
hr = Validate(reinterpret_cast<const uint32_t*>(ib.data()), nFaces, vb.size(), nullptr, VALIDATE_DEFAULT, &msgs);
if (FAILED(hr))
{
printe("\nERROR: Invalid index buffer from create atlas (%08X):\n%S\n%S\n", static_cast<unsigned int>(hr), szPath, msgs.c_str());
success = false;
}
}
++npass;
}
print("\n%zu meshes tested, %zu meshes passed ", ncount, npass);
return success;
}
#endif