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 "Serialization.hpp"
5using namespace Foundation;
6using namespace Core;
7using namespace Math;
8
9constexpr uint32_t kMeshletMaxVertices = 64; // max vertices per meshlet
10constexpr uint32_t kMeshletMaxTriangles = 96; // max triangles per meshlet (indices=3*triangles)
11
12#pragma pack(push,4)
13struct FVertex
14{
15 float3 position;
16 float3 normal;
17 float3 tangent;
19 float2 uv;
20};
21static_assert(sizeof(FVertex) == 48);
22
24{
25 uint16_t position[4]; // quantized FP16 [xyz] padding [w]
26 uint32_t tbn32; // packed tangent frame
27 uint16_t uv[2]; // quantized FP16 [uv]
28
29 static uint32_t PackTBN(const float3& normal, const float3& tangent, float bitangentSign);
30 static void UnpackTBN(uint32_t packed, float3& outNormal, float3& outTangent, float& outBitangentSign);
31
32 static FQVertex Pack(FVertex const& vertex);
33 static FVertex Unpack(FQVertex const& vertex);
34};
35static_assert(sizeof(FQVertex) == 16);
36#pragma pack(pop)
37
45{
46 uint16_t joints[4]{0, 0, 0, 0};
47 float weights[4]{1, 0, 0, 0};
48};
49static_assert(sizeof(FSkinBinding) == 24);
50
51struct FLODGroup // @ref clodGroup
52{
53 // DAG level the group was generated at
54 int depth;
55
56 // sphere bounds, in mesh coordinate space
57 float3 center;
58 float radius;
59
60 // combined simplification error, in mesh coordinate space
61 // FLT_MAX for terminal groups
62 float error;
63};
64static_assert(sizeof(FLODGroup) == 24);
65
66struct FMeshlet // @ref meshopt_Meshlet
67{
68 /* meshlet group */
69 /* ID of the @ref FLODGroup this meshlet belongs to in a hierarchy */
70 uint32_t group;
71 /* ID of the @ref FLODGroup (with more triangles) that produced this meshlet during simplification (parent). ~0u if
72 * original geometry */
73 uint32_t refined;
74
75 /* meshlet */
76 /* offsets within meshletVtx and meshletTri arrays with meshlet data */
77 uint32_t vtxOffset; // in vertices
78 uint32_t triOffset; // in indices (3*triangles)
79 /* number of vertices and triangles used in the meshlet; data is stored in consecutive range defined by offset and
80 * count */
81 uint32_t vtxCount;
82 uint32_t triCount;
83
84 /* bounds */
85 float4 centerRadius; // (x,y,z,r)
86 float4 coneAxisAngle; // (x,y,z,cos(half solid angle))
87 float3 coneApex; // (x,y,z)
88};
89static_assert(sizeof(FMeshlet) == 68);
90
96
98{
100 uint32_t vertexCount{0};
106 // Optional CPU skinning data: per-vertex @ref FSkinBinding (count == vertexCount when present)
107 // plus the index of the bound skeleton in the scene's skeleton table (-1 = not skinned).
108 // Skinned meshes carry no DAG/meshlets (they take the dynamic vertex/index path).
110 int32_t skeleton{-1};
111 // Optional morph-target POSITION deltas (target-major: target t, vertex v at t*vertexCount + v),
112 // the target count, and the index of the driving morph-weight track (-1 = not morph-animated).
113 // Morph-animated meshes also take the dynamic vertex/index path.
115 uint32_t morphTargetCount{0};
116 int32_t morphTrack{-1};
117
119 : lods(alloc)
120 {
121 }
122};
123
125{
126 Vector<FVertex> vertices; // Full precision, raw vertices. Used by importers.
127 Vector<FQVertex> verticesQuantized; // Quantized vertex data for GPU upload.
128 Vector<FSkinBinding> skin; // Optional CPU skin binding, parallel to @ref vertices (empty if not skinned).
129 Vector<float3> morphPositions; // Optional morph POSITION deltas, target-major (t*vertexCount + v).
130 uint32_t morphTargetCount{0};
131 struct LOD
132 {
133 Vector<uint32_t> indices; // Full precision triangle indices
134 LOD(Allocator* alloc) : indices(alloc) {}
135 };
137 struct DAG
138 {
139 Vector<FLODGroup> groups; // LOD groups with error bounds
140 Vector<FMeshlet> meshlets; // Meshlets built from all clusters
141 Vector<uint8_t> meshletTri; // Meshlet local triangle indices
142 Vector<uint32_t> meshletVtx; // Meshlet vertex indices into vertices/verticesQuantized
143 DAG(Allocator* alloc) : groups(alloc), meshlets(alloc), meshletTri(alloc), meshletVtx(alloc) {}
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 buildOrthonormalBasis(float3 n, float3& b1, float3& b2);
194float packUnitCircleSnorm(float2 v);
#define GLOBAL_ALLOC
Definition Allocator.hpp:225
float2 unpackUnitCircleSnorm(float v)
Definition Mesh.cpp:47
void buildOrthonormalBasis(float3 n, float3 &b1, float3 &b2)
Definition Mesh.cpp:13
float3 unpackUnitOctahedralSnorm(float2 v)
Definition Mesh.cpp:35
float2 packUnitOctahedralSnorm(float3 v)
Definition Mesh.cpp:28
float packUnitCircleSnorm(float2 v)
Definition Mesh.cpp:42
constexpr uint32_t kMeshletMaxTriangles
Definition Mesh.hpp:10
constexpr uint32_t kMeshletMaxVertices
Definition Mesh.hpp:9
General Purpose Allocator (GPA) interface.
Definition Allocator.hpp:24
std::vector< T, StlAllocator< T > > Vector
std::vector with explicit Foundation::Core::StlAllocator constructor
Definition Container.hpp:130
vec2 float2
Definition Math.hpp:27
vec3 float3
Definition Math.hpp:26
Definition Allocator.hpp:5
Definition Serialization.hpp:26
Definition Mesh.hpp:138
DAG(Allocator *alloc)
Definition Mesh.hpp:143
Vector< FMeshlet > meshlets
Definition Mesh.hpp:140
Vector< uint32_t > meshletVtx
Definition Mesh.hpp:142
Vector< FLODGroup > groups
Definition Mesh.hpp:139
Vector< uint8_t > meshletTri
Definition Mesh.hpp:141
Definition Mesh.hpp:132
LOD(Allocator *alloc)
Definition Mesh.hpp:134
Vector< uint32_t > indices
Definition Mesh.hpp:133
Definition Mesh.hpp:125
void Optimize()
Optimize vertex reuse with meshoptimizer.
Definition Mesh.cpp:324
struct FImportedMesh::DAG dag
bool EnsureRaw()
Prepares full-precision data for CPU access.
Definition Mesh.cpp:311
void Dequantize()
Dequantizes quantized vertex back into full precision representation Fills rawVertices with dequantiz...
Definition Mesh.cpp:117
bool IsQuantized() const
Definition Mesh.hpp:175
Vector< FQVertex > verticesQuantized
Definition Mesh.hpp:127
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:284
Vector< FVertex > vertices
Definition Mesh.hpp:126
void SimplifyLOD(int levels, float scale)
Creates N LOD levels, iteratively scaling down by 'scale' factor and populates lods index data.
Definition Mesh.cpp:201
Vector< float3 > morphPositions
Definition Mesh.hpp:129
Vector< LOD > lods
Definition Mesh.hpp:136
bool EnsureQuantized()
Prepares quantized GPU data buffers from possibly full-precision, or compressed data.
Definition Mesh.cpp:299
Vector< FSkinBinding > skin
Definition Mesh.hpp:128
bool IsRaw() const
Definition Mesh.hpp:180
void ClusterizeDAG()
Partitions the clusters of LOD levels into a DAG.
Definition Mesh.cpp:218
void Quantize()
Quantizes vertex data into more compact representation Fills quantizedVertices with quantized data fr...
Definition Mesh.cpp:111
uint32_t morphTargetCount
Definition Mesh.hpp:130
Definition Mesh.hpp:52
float radius
Definition Mesh.hpp:58
float error
Definition Mesh.hpp:62
float3 center
Definition Mesh.hpp:57
int depth
Definition Mesh.hpp:54
Definition Mesh.hpp:67
uint32_t refined
Definition Mesh.hpp:73
uint32_t triOffset
Definition Mesh.hpp:78
uint32_t vtxCount
Definition Mesh.hpp:81
float3 coneApex
Definition Mesh.hpp:87
uint32_t vtxOffset
Definition Mesh.hpp:77
uint32_t group
Definition Mesh.hpp:70
float4 coneAxisAngle
Definition Mesh.hpp:86
float4 centerRadius
Definition Mesh.hpp:85
uint32_t triCount
Definition Mesh.hpp:82
Definition Mesh.hpp:24
static FVertex Unpack(FQVertex const &vertex)
Definition Mesh.cpp:100
uint32_t tbn32
Definition Mesh.hpp:26
static void UnpackTBN(uint32_t packed, float3 &outNormal, float3 &outTangent, float &outBitangentSign)
Definition Mesh.cpp:74
static uint32_t PackTBN(const float3 &normal, const float3 &tangent, float bitangentSign)
Definition Mesh.cpp:57
uint16_t uv[2]
Definition Mesh.hpp:27
uint16_t position[4]
Definition Mesh.hpp:25
static FQVertex Pack(FVertex const &vertex)
Definition Mesh.cpp:89
Definition Mesh.hpp:92
uint32_t indexCount
Definition Mesh.hpp:94
FBlobRef indices
Definition Mesh.hpp:93
Definition Mesh.hpp:98
int32_t morphTrack
Definition Mesh.hpp:116
FBlobRef skinBinding
Definition Mesh.hpp:109
uint32_t vertexCount
Definition Mesh.hpp:100
FBlobRef morphPositions
Definition Mesh.hpp:114
FSerializedMesh(Allocator *alloc=GLOBAL_ALLOC)
Definition Mesh.hpp:118
uint32_t morphTargetCount
Definition Mesh.hpp:115
FBlobRef dagMeshlets
Definition Mesh.hpp:103
Vector< FSerializedMeshLOD > lods
Definition Mesh.hpp:101
int32_t skeleton
Definition Mesh.hpp:110
FBlobRef dagMeshletVtx
Definition Mesh.hpp:105
FBlobRef vertices
Definition Mesh.hpp:99
FBlobRef dagGroups
Definition Mesh.hpp:102
FBlobRef dagMeshletTri
Definition Mesh.hpp:104
Per-vertex skin binding for CPU skinning (parallel to a mesh's vertices).
Definition Mesh.hpp:45
uint16_t joints[4]
Definition Mesh.hpp:46
float weights[4]
Definition Mesh.hpp:47
Definition Mesh.hpp:14
float3 tangent
Definition Mesh.hpp:17
float2 uv
Definition Mesh.hpp:19
float3 position
Definition Mesh.hpp:15
float bitangentSign
Definition Mesh.hpp:18
float3 normal
Definition Mesh.hpp:16