Foundation
Loading...
Searching...
No Matches
Mesh.hpp
Go to the documentation of this file.
1#pragma once
2#include <Core/Container.hpp>
3#include <Math/Math.hpp>
4#include "Metadata.hpp"
5#include "Precompute.hpp"
6#include "Serialization.hpp"
7using namespace Foundation;
8using namespace Core;
9using namespace Math;
10
11constexpr uint32_t kMeshletMaxVertices = 64; // max vertices per meshlet
12constexpr uint32_t kMeshletMaxTriangles = 96; // max triangles per meshlet (indices=3*triangles)
13
14#pragma pack(push,4)
23static_assert(sizeof(FVertex) == 48);
24
26{
27 uint16_t position[4]; // quantized FP16 [xyz] padding [w]
28 uint32_t tbn32; // packed tangent frame
29 uint16_t uv[2]; // quantized FP16 [uv]
30
31 static uint32_t PackTBN(const float3& normal, const float3& tangent, float bitangentSign);
32 static void UnpackTBN(uint32_t packed, float3& outNormal, float3& outTangent, float& outBitangentSign);
33
34 static FQVertex Pack(FVertex const& vertex);
35 static FVertex Unpack(FQVertex const& vertex);
36};
37static_assert(sizeof(FQVertex) == 16);
38#pragma pack(pop)
39
40
41
42struct FLODGroup // @ref clodGroup
43{
44 // DAG level the group was generated at
45 int depth;
46
47 // sphere bounds, in mesh coordinate space
49 float radius;
50
51 // combined simplification error, in mesh coordinate space
52 // FLT_MAX for terminal groups
53 float error;
54};
55static_assert(sizeof(FLODGroup) == 24);
56
57struct FMeshlet // @ref meshopt_Meshlet
58{
59 /* meshlet group */
60 /* ID of the @ref FLODGroup this meshlet belongs to in a hierarchy */
61 uint32_t group;
62 /* ID of the @ref FLODGroup (with more triangles) that produced this meshlet during simplification (parent). ~0u if
63 * original geometry */
64 uint32_t refined;
65
66 /* meshlet */
67 /* offsets within meshletVtx and meshletTri arrays with meshlet data */
68 uint32_t vtxOffset; // in vertices
69 uint32_t triOffset; // in indices (3*triangles)
70 /* number of vertices and triangles used in the meshlet; data is stored in consecutive range defined by offset and
71 * count */
72 uint32_t vtxCount;
73 uint32_t triCount;
74
75 /* bounds */
76 float4 centerRadius; // (x,y,z,r)
77 float4 coneAxisAngle; // (x,y,z,cos(half solid angle))
78 float3 coneApex; // (x,y,z)
79};
80static_assert(sizeof(FMeshlet) == 68);
81
83{
84 uint32_t meshletIndex;
85 uint32_t aliasOffset;
86 uint32_t triCount;
87 float area;
89 float radius;
90};
91static_assert(sizeof(FEmissiveMeshlet) == 32);
92
98
119
121{
122 Vector<FVertex> vertices; // Full precision, raw vertices. Used by importers.
123 Vector<FQVertex> verticesQuantized; // Quantized vertex data for GPU upload.
124 struct LOD
125 {
126 Vector<uint32_t> indices; // Full precision triangle indices
127 LOD(Allocator* alloc) : indices(alloc) {}
128 };
130 struct DAG
131 {
132 Vector<FLODGroup> groups; // LOD groups with error bounds
133 Vector<FMeshlet> meshlets; // Meshlets built from all clusters
134 Vector<uint8_t> meshletTri; // Meshlet local triangle indices
135 Vector<uint32_t> meshletVtx; // Meshlet vertex indices into vertices/verticesQuantized
139 DAG(Allocator* alloc) :
140 groups(alloc), meshlets(alloc), meshletTri(alloc), meshletVtx(alloc),
142 {
143 }
145
146 FImportedMesh(Allocator* alloc);
150 void Optimize();
155 void SimplifyLOD(int levels, float scale);
159 void ClusterizeDAG();
164 void Quantize();
169 void Dequantize();
174 bool EnsureQuantized();
175 [[nodiscard]] bool IsQuantized() const { return !verticesQuantized.empty(); }
179 bool EnsureRaw();
180 [[nodiscard]] bool IsRaw() const { return !vertices.empty(); }
187 [[nodiscard]] size_t CalculateQuantizedBound(bool lod, bool dag) const;
188};
189
190/* -- Math Exports -- */
191void CoordinateSystem(float3 n, float3& b1, float3& b2);
196
#define GLOBAL_ALLOC
Definition Allocator.hpp:240
float3 UnpackUnitOctahedralSnorm(float2 v)
Definition Mesh.cpp:35
float PackUnitCircleSnorm(float2 v)
Definition Mesh.cpp:42
float2 UnpackUnitCircleSnorm(float v)
Definition Mesh.cpp:47
void RecomputeNormals(Span< FVertex > verts, Span< const uint32_t > indices)
Recomputes smooth per-vertex normals from positions over an indexed triangle list.
Definition Mesh.cpp:53
float2 PackUnitOctahedralSnorm(float3 v)
Definition Mesh.cpp:28
constexpr uint32_t kMeshletMaxTriangles
Definition Mesh.hpp:12
void CoordinateSystem(float3 n, float3 &b1, float3 &b2)
Definition Mesh.cpp:13
constexpr uint32_t kMeshletMaxVertices
Definition Mesh.hpp:11
Allocator interface (noexcept)
Definition Allocator.hpp:29
std::vector< T, StlAllocator< T > > Vector
std::vector with explicit Foundation::Core::StlAllocator constructor
Definition Container.hpp:149
std::span< T > Span
Alias for std::span
Definition Container.hpp:62
vec2 float2
Definition Math.hpp:27
vec3 float3
Definition Math.hpp:26
vec4 float4
Definition Math.hpp:25
Definition Allocator.hpp:6
Definition Serialization.hpp:27
Definition Mesh.hpp:83
float radius
Definition Mesh.hpp:89
float3 center
Definition Mesh.hpp:88
uint32_t aliasOffset
Definition Mesh.hpp:85
uint32_t meshletIndex
Definition Mesh.hpp:84
float area
Definition Mesh.hpp:87
uint32_t triCount
Definition Mesh.hpp:86
Definition Mesh.hpp:131
Vector< GSAlias > emissiveAliases
Definition Mesh.hpp:137
DAG(Allocator *alloc)
Definition Mesh.hpp:139
Vector< FEmissiveMeshlet > emissiveMeshlets
Definition Mesh.hpp:136
Vector< FMeshlet > meshlets
Definition Mesh.hpp:133
Vector< uint32_t > emissivePrimitiveMap
Definition Mesh.hpp:138
Vector< uint32_t > meshletVtx
Definition Mesh.hpp:135
Vector< FLODGroup > groups
Definition Mesh.hpp:132
Vector< uint8_t > meshletTri
Definition Mesh.hpp:134
Definition Mesh.hpp:125
LOD(Allocator *alloc)
Definition Mesh.hpp:127
Vector< uint32_t > indices
Definition Mesh.hpp:126
Definition Mesh.hpp:121
void Optimize()
Optimize vertex reuse with meshoptimizer.
Definition Mesh.cpp:439
struct FImportedMesh::DAG dag
bool EnsureRaw()
Prepares full-precision data for CPU access.
Definition Mesh.cpp:426
void Dequantize()
Dequantizes quantized vertex back into full precision representation Fills rawVertices with dequantiz...
Definition Mesh.cpp:139
bool IsQuantized() const
Definition Mesh.hpp:175
Vector< FQVertex > verticesQuantized
Definition Mesh.hpp:123
size_t CalculateQuantizedBound(bool lod, bool dag) const
Returns a lower bound estimate of the size of the quantized mesh data. The size is conservative in th...
Definition Mesh.cpp:399
Vector< FVertex > vertices
Definition Mesh.hpp:122
void SimplifyLOD(int levels, float scale)
Creates N LOD levels, iteratively scaling down by 'scale' factor and populates lods index data.
Definition Mesh.cpp:223
Vector< LOD > lods
Definition Mesh.hpp:129
bool EnsureQuantized()
Prepares quantized GPU data buffers from possibly full-precision, or compressed data.
Definition Mesh.cpp:414
bool IsRaw() const
Definition Mesh.hpp:180
void ClusterizeDAG()
Partitions the clusters of LOD levels into a DAG.
Definition Mesh.cpp:241
void Quantize()
Quantizes vertex data into more compact representation Fills quantizedVertices with quantized data fr...
Definition Mesh.cpp:133
Definition Mesh.hpp:43
float radius
Definition Mesh.hpp:49
float error
Definition Mesh.hpp:53
float3 center
Definition Mesh.hpp:48
int depth
Definition Mesh.hpp:45
Definition Mesh.hpp:58
uint32_t refined
Definition Mesh.hpp:64
uint32_t triOffset
Definition Mesh.hpp:69
uint32_t vtxCount
Definition Mesh.hpp:72
float3 coneApex
Definition Mesh.hpp:78
uint32_t vtxOffset
Definition Mesh.hpp:68
uint32_t group
Definition Mesh.hpp:61
float4 coneAxisAngle
Definition Mesh.hpp:77
float4 centerRadius
Definition Mesh.hpp:76
uint32_t triCount
Definition Mesh.hpp:73
Definition Mesh.hpp:26
static FVertex Unpack(FQVertex const &vertex)
Definition Mesh.cpp:122
uint32_t tbn32
Definition Mesh.hpp:28
static void UnpackTBN(uint32_t packed, float3 &outNormal, float3 &outTangent, float &outBitangentSign)
Definition Mesh.cpp:96
static uint32_t PackTBN(const float3 &normal, const float3 &tangent, float bitangentSign)
Definition Mesh.cpp:79
uint16_t uv[2]
Definition Mesh.hpp:29
uint16_t position[4]
Definition Mesh.hpp:27
static FQVertex Pack(FVertex const &vertex)
Definition Mesh.cpp:111
Definition Metadata.hpp:14
Definition Mesh.hpp:94
uint32_t indexCount
Definition Mesh.hpp:96
FBlobRef indices
Definition Mesh.hpp:95
Definition Mesh.hpp:100
FBlobRef emissivePrimitiveMap
Definition Mesh.hpp:112
FSerializedBounds bounds
Definition Mesh.hpp:102
uint32_t vertexCount
Definition Mesh.hpp:104
FSerializedMesh(Allocator *alloc=GLOBAL_ALLOC)
Definition Mesh.hpp:114
FBlobRef emissiveMeshlets
Definition Mesh.hpp:110
FBlobRef dagMeshlets
Definition Mesh.hpp:107
Vector< FSerializedMeshLOD > lods
Definition Mesh.hpp:105
FBlobRef dagMeshletVtx
Definition Mesh.hpp:109
FBlobRef vertices
Definition Mesh.hpp:103
FBlobRef emissiveAliases
Definition Mesh.hpp:111
FBlobRef dagGroups
Definition Mesh.hpp:106
FBlobRef dagMeshletTri
Definition Mesh.hpp:108
Definition Mesh.hpp:16
float3 tangent
Definition Mesh.hpp:19
float2 uv
Definition Mesh.hpp:21
float3 position
Definition Mesh.hpp:17
float bitangentSign
Definition Mesh.hpp:20
float3 normal
Definition Mesh.hpp:18
128-bit identifier. Two flavors, both well-formed per RFC 9562:
Definition UUID.hpp:35