Repository navigation
Expand file tree
/
Copy pathMeshAsset.cpp
More file actions
509 lines (467 loc) · 16.4 KB
/
Copy pathMeshAsset.cpp
File metadata and controls
509 lines (467 loc) · 16.4 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
#include "MeshAsset.h"
#include <deki/LogSystem.h>
#include <deki/providers/FileSystem.h>
#include <deki/providers/IFileSystem.h>
#include "Mesh3DBackend.h" // NextRevision
#include "deki-rendering/PixelFormat.h"
#include <algorithm>
#include <cstdint>
#include <cstring>
namespace Deki3D
{
namespace
{
/// The vertex layout an attribute mask describes. Offsets follow the order
/// the asset compiler writes: position, normal, uv, colour, each present only
/// when its bit is set.
VertexLayout LayoutFor(uint16_t attributes, uint16_t stride)
{
VertexLayout layout;
layout.stride = stride;
int16_t offset = 0;
layout.positionOffset = offset;
offset += static_cast<int16_t>(sizeof(float) * 3);
if (attributes & MeshAttributeNormal)
{
layout.normalOffset = offset;
offset += static_cast<int16_t>(sizeof(float) * 3);
}
if (attributes & MeshAttributeUV)
{
layout.uvOffset = offset;
offset += static_cast<int16_t>(sizeof(float) * 2);
}
if (attributes & MeshAttributeColor)
{
layout.colorOffset = offset;
offset += static_cast<int16_t>(sizeof(uint32_t));
}
return layout;
}
/// The bytes one vertex with these attributes needs (LayoutFor's offsets).
uint32_t VertexBytesFor(uint16_t attributes)
{
uint32_t bytes = sizeof(float) * 3;
if (attributes & MeshAttributeNormal)
{
bytes += sizeof(float) * 3;
}
if (attributes & MeshAttributeUV)
{
bytes += sizeof(float) * 2;
}
if (attributes & MeshAttributeColor)
{
bytes += sizeof(uint32_t);
}
return bytes;
}
/// The exponent of a power of two, or -1 when the value is not one.
int ShiftOf(uint16_t value)
{
if (value == 0 || (value & (value - 1)) != 0)
{
return -1;
}
int shift = 0;
while ((1u << shift) < value)
{
++shift;
}
return shift;
}
} // namespace
/// Where a load reads the file from, front to back. Every field goes through
/// Take, which refuses to run off the end: devices have little memory
/// protection, so a truncated asset must fail cleanly instead of reading
/// whatever follows it.
class MeshSource
{
public:
virtual ~MeshSource() = default;
virtual bool Take(void* out, size_t bytes) = 0;
};
namespace
{
/// A blob already in memory.
class MemorySource : public MeshSource
{
public:
MemorySource(const uint8_t* data, size_t size)
: m_Data(data),
m_Size(size)
{
}
bool Take(void* out, size_t bytes) override
{
if (bytes > m_Size - m_Offset)
{
return false;
}
std::memcpy(out, m_Data + m_Offset, bytes);
m_Offset += bytes;
return true;
}
private:
const uint8_t* m_Data;
size_t m_Size;
size_t m_Offset = 0;
};
/// A file, read a part at a time into wherever the part belongs.
class FileSource : public MeshSource
{
public:
explicit FileSource(const char* path)
: m_Fs(Deki::FileSystem::GetFileSystemForPath(path))
{
if (m_Fs)
{
m_File = m_Fs->OpenFile(path, Deki::IFileSystem::OpenMode::ReadBinary);
}
if (m_File)
{
const long size = m_Fs->GetFileSize(m_File);
m_Left = size > 0 ? static_cast<size_t>(size) : 0;
}
}
~FileSource() override
{
if (m_File)
{
m_Fs->CloseFile(m_File);
}
}
bool Open() const { return m_File != nullptr; }
bool Take(void* out, size_t bytes) override
{
if (!m_File || bytes > m_Left)
{
return false;
}
if (bytes > 0 && m_Fs->ReadFile(m_File, out, bytes) != bytes)
{
return false;
}
m_Left -= bytes;
return true;
}
private:
Deki::IFileSystem* m_Fs;
Deki::IFileSystem::FileHandle m_File = nullptr;
size_t m_Left = 0;
};
} // namespace
bool MeshAsset::LoadFromMemory(const uint8_t* data, size_t size)
{
Clear();
if (!data)
{
return false;
}
MemorySource source(data, size);
return Load(source);
}
bool MeshAsset::LoadFromFile(const char* path)
{
Clear();
FileSource source(path);
if (!source.Open())
{
DEKI_LOG_WARNING("MeshAsset: cannot open %s", path ? path : "(null)");
return false;
}
return Load(source);
}
bool MeshAsset::Load(MeshSource& cursor)
{
MeshFileHeader header{};
if (!cursor.Take(&header, kMeshFileHeaderV4Bytes))
{
return false;
}
if (std::memcmp(header.magic, "DMSH", 4) != 0)
{
DEKI_LOG_WARNING("MeshAsset: not a mesh (bad magic)");
return false;
}
if (header.version != kMeshFileVersion && header.version != 4)
{
DEKI_LOG_WARNING("MeshAsset: version %u, this build reads 4 and %u; rebuild the asset",
static_cast<unsigned>(header.version), static_cast<unsigned>(kMeshFileVersion));
return false;
}
header.flags = 0;
if (header.version >= 5 && !cursor.Take(&header.flags, sizeof(header.flags)))
{
return false;
}
const uint8_t indexBytes = (header.flags & MeshFileIndex32) ? 4 : 2;
if (header.vertexCount == 0 || header.indexCount == 0 || header.vertexStride == 0)
{
return false;
}
// The stride must hold the attributes it claims and keep its floats 4-byte
// aligned: a short one reads past the buffer, and unaligned float loads
// trap on Xtensa.
if (header.vertexStride < VertexBytesFor(header.attributes) || header.vertexStride % 4 != 0)
{
return Fail("its vertex stride does not fit its attributes");
}
// In 64 bits: vertexCount * stride can wrap on a 32-bit board, giving a
// small buffer that indices then read past.
const uint64_t vertexBytes64 = static_cast<uint64_t>(header.vertexCount) * header.vertexStride;
if (vertexBytes64 > SIZE_MAX / 2)
{
return Fail("its vertex buffer is larger than this device can address");
}
const size_t vertexBytes = static_cast<size_t>(vertexBytes64);
if (!m_Vertices.Allocate(vertexBytes, Deki::Memory::External))
{
return Fail("no room for the vertex buffer");
}
if (!cursor.Take(m_Vertices.Data(), vertexBytes))
{
return Fail("truncated vertex buffer");
}
if (static_cast<uint64_t>(header.indexCount) * indexBytes > SIZE_MAX / 2)
{
return Fail("its index buffer is larger than this device can address");
}
if (!m_Indices.Allocate(static_cast<size_t>(header.indexCount) * indexBytes, Deki::Memory::External))
{
return Fail("no room for the index buffer");
}
if (!cursor.Take(m_Indices.Data(), m_Indices.Bytes()))
{
return Fail("truncated index buffer");
}
if (!m_Submeshes.Allocate(header.submeshCount > 0 ? header.submeshCount : 1, Deki::Memory::Internal))
{
return Fail("no room for the submesh table");
}
if (header.submeshCount > 0)
{
for (uint16_t i = 0; i < header.submeshCount; ++i)
{
MeshFileSubmesh onDisk{};
if (!cursor.Take(&onDisk, sizeof(onDisk)))
{
return Fail("truncated submesh table");
}
Submesh3D sub;
sub.firstIndex = onDisk.firstIndex;
sub.indexCount = onDisk.indexCount;
sub.materialIndex = onDisk.materialIndex;
m_Submeshes.Data()[i] = sub;
}
}
else
{
Submesh3D all;
all.firstIndex = 0;
all.indexCount = header.indexCount;
all.materialIndex = 0;
m_Submeshes.Data()[0] = all;
}
Deki::Buffer<MeshFileMaterial> fileMaterials;
if (!fileMaterials.Allocate(header.materialCount, Deki::Memory::Internal))
{
return Fail("no room for the material table");
}
for (uint16_t i = 0; i < header.materialCount; ++i)
{
if (!cursor.Take(&fileMaterials.Data()[i], sizeof(MeshFileMaterial)))
{
return Fail("truncated material table");
}
}
Deki::Buffer<MeshFileTexture> fileTextures;
if (!fileTextures.Allocate(header.textureCount, Deki::Memory::Internal))
{
return Fail("no room for the texture table");
}
for (uint16_t i = 0; i < header.textureCount; ++i)
{
if (!cursor.Take(&fileTextures.Data()[i], sizeof(MeshFileTexture)))
{
return Fail("truncated texture table");
}
}
if (header.texturePixelBytes > 0)
{
if (!m_TexturePixels.Allocate(header.texturePixelBytes, Deki::Memory::External))
{
return Fail("no room for the texture pixels");
}
if (!cursor.Take(m_TexturePixels.Data(), header.texturePixelBytes))
{
return Fail("truncated texture pixels");
}
}
// Everything is read; now check that every reference points inside it.
m_View.indices = m_Indices.Data();
m_View.indexBytes = indexBytes;
for (uint32_t i = 0; i < header.indexCount; ++i)
{
if (m_View.IndexAt(i) >= header.vertexCount)
{
return Fail("an index points past the vertex buffer");
}
}
// Each submesh's bounds, for the software rasteriser to skip a part out
// of view: worked out here rather than stored, so a mesh file of any
// version has them. One pass over the indices.
const VertexLayout layout = LayoutFor(header.attributes, header.vertexStride);
for (size_t i = 0; i < m_Submeshes.Count(); ++i)
{
Submesh3D& sub = m_Submeshes.Data()[i];
if (static_cast<uint64_t>(sub.firstIndex) + sub.indexCount > header.indexCount)
{
return Fail("a submesh runs past the index buffer");
}
sub.hasBounds = false;
if (layout.positionOffset < 0 || sub.indexCount == 0)
{
continue;
}
for (uint32_t k = sub.firstIndex; k < sub.firstIndex + sub.indexCount; ++k)
{
const uint8_t* vertex =
static_cast<const uint8_t*>(m_Vertices.Data()) + static_cast<size_t>(m_View.IndexAt(k)) * layout.stride;
float position[3];
std::memcpy(position, vertex + layout.positionOffset, sizeof(position));
const Deki::Vector3 at(position[0], position[1], position[2]);
if (!sub.hasBounds)
{
sub.boundsMin = sub.boundsMax = at;
sub.hasBounds = true;
continue;
}
sub.boundsMin = Deki::Vector3(std::min(sub.boundsMin.x, at.x), std::min(sub.boundsMin.y, at.y),
std::min(sub.boundsMin.z, at.z));
sub.boundsMax = Deki::Vector3(std::max(sub.boundsMax.x, at.x), std::max(sub.boundsMax.y, at.y),
std::max(sub.boundsMax.z, at.z));
}
}
if (!m_Textures.Allocate(header.textureCount, Deki::Memory::Internal))
{
return Fail("no room for the texture table");
}
for (uint16_t ti = 0; ti < header.textureCount; ++ti)
{
const MeshFileTexture& t = fileTextures.Data()[ti];
Texture3D view;
const int wShift = ShiftOf(t.width);
const int hShift = ShiftOf(t.height);
const bool knownFormat = t.format <= static_cast<uint8_t>(TexelFormat::ALPHA8);
const TexelFormat format = knownFormat ? static_cast<TexelFormat>(t.format) : TexelFormat::RGB565;
const uint64_t bytes = static_cast<uint64_t>(t.width) * t.height * TexelBytes(format);
// 64-bit, so offset + size cannot wrap past the check; and aligned for
// the 2- and 4-byte texel reads (Raster3D reads through uint16_t*).
const uint64_t texel = TexelBytes(format);
const bool aligned = (texel == 2 || texel == 4) ? (t.byteOffset % texel) == 0 : true;
const bool fits = aligned && static_cast<uint64_t>(t.byteOffset) + bytes <= m_TexturePixels.Count();
if (knownFormat && wShift >= 0 && hShift >= 0 && bytes > 0 && fits)
{
// The file keeps RGB565 low byte first; draw from memory's order,
// high byte first on a big-endian board. Once per pixel run, even
// if two textures share it: a second swap would undo the first.
if (format == TexelFormat::RGB565 || format == TexelFormat::RGB565A8)
{
bool converted = false;
for (uint16_t prev = 0; prev < ti && !converted; ++prev)
{
const MeshFileTexture& p = fileTextures.Data()[prev];
converted = p.byteOffset == t.byteOffset && m_Textures.Data()[prev].Valid() &&
(m_Textures.Data()[prev].format == TexelFormat::RGB565 ||
m_Textures.Data()[prev].format == TexelFormat::RGB565A8);
}
if (!converted)
{
DekiPixel::FileToMemory565(m_TexturePixels.Data() + t.byteOffset,
static_cast<size_t>(t.width) * t.height, static_cast<size_t>(texel));
}
}
view.pixels = m_TexturePixels.Data() + t.byteOffset;
view.palette = nullptr;
view.format = format;
view.hasAlpha = format == TexelFormat::RGB565 || format == TexelFormat::RGB565A8 ||
format == TexelFormat::RGBA8888 || format == TexelFormat::ALPHA8;
const uint8_t filter = t.flags & MeshTextureFilterMask;
view.filter = filter <= static_cast<uint8_t>(Deki::TextureFilter::Trilinear)
? static_cast<Deki::TextureFilter>(filter)
: Deki::TextureFilter::Point;
view.width = t.width;
view.height = t.height;
view.widthShift = static_cast<uint8_t>(wShift);
view.heightShift = static_cast<uint8_t>(hShift);
}
else
{
// Left invalid instead of rejecting the file: one bad texture
// costs only its material's texturing, not the whole model.
DEKI_LOG_WARNING("MeshAsset: texture %ux%u (format %u) at %u is unusable; that material goes untextured",
static_cast<unsigned>(t.width), static_cast<unsigned>(t.height),
static_cast<unsigned>(t.format), static_cast<unsigned>(t.byteOffset));
}
if (view.Valid())
{
view.revision = NextRevision();
}
m_Textures.Data()[ti] = view;
}
// A model with no materials of its own still draws, with one default.
if (!m_Materials.Allocate(header.materialCount ? header.materialCount : 1, Deki::Memory::Internal))
{
return Fail("no room for the material table");
}
m_Materials.Data()[0] = Material3D{}; // Buffer memory is zeroed, not constructed
for (uint16_t mi = 0; mi < header.materialCount; ++mi)
{
const MeshFileMaterial& m = fileMaterials.Data()[mi];
Material3D material;
material.tint = m.tint;
material.doubleSided = (m.flags & MeshMaterialDoubleSided) != 0;
material.alphaTest = (m.flags & MeshMaterialAlphaTest) != 0;
if (m.textureIndex >= 0 && static_cast<size_t>(m.textureIndex) < m_Textures.Count() &&
m_Textures.Data()[m.textureIndex].Valid())
{
material.texture = &m_Textures.Data()[m.textureIndex];
}
m_Materials.Data()[mi] = material;
}
for (size_t i = 0; i < m_Submeshes.Count(); ++i)
{
if (m_Submeshes.Data()[i].materialIndex >= m_Materials.Count())
{
return Fail("a submesh names a material that is not there");
}
}
m_View.layout = LayoutFor(header.attributes, header.vertexStride);
m_View.vertexCount = header.vertexCount;
m_View.indexCount = header.indexCount;
m_View.boundsMin = Deki::Vector3(header.boundsMin[0], header.boundsMin[1], header.boundsMin[2]);
m_View.boundsMax = Deki::Vector3(header.boundsMax[0], header.boundsMax[1], header.boundsMax[2]);
m_View.vertices = m_Vertices.Data();
m_View.submeshes = m_Submeshes.Data();
m_View.submeshCount = static_cast<uint16_t>(m_Submeshes.Count());
m_View.revision = NextRevision();
return true;
}
void MeshAsset::Clear()
{
m_Vertices.Reset();
m_Indices.Reset();
m_Submeshes.Reset();
m_TexturePixels.Reset();
m_Textures.Reset();
m_Materials.Reset();
m_View = Mesh3D{};
}
bool MeshAsset::Fail(const char* why)
{
DEKI_LOG_WARNING("MeshAsset: %s", why);
Clear();
return false;
}
} // namespace Deki3D