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// Per-vertex binding stored separately from the render vertex format.
42{
43 uint16_t joints[4]{0, 0, 0, 0};
44 float weights[4]{1, 0, 0, 0};
45};
46static_assert(sizeof(FSkinBinding) == 24);
47
48struct FLODGroup // @ref clodGroup
49{
50 // DAG level the group was generated at
51 int depth;
52
53 // sphere bounds, in mesh coordinate space
55 float radius;
56
57 // combined simplification error, in mesh coordinate space
58 // FLT_MAX for terminal groups
59 float error;
60};
61static_assert(sizeof(FLODGroup) == 24);
62
63struct FMeshlet // @ref meshopt_Meshlet
64{
65 /* meshlet group */
66 /* ID of the @ref FLODGroup this meshlet belongs to in a hierarchy */
67 uint32_t group;
68 /* ID of the @ref FLODGroup (with more triangles) that produced this meshlet during simplification (parent). ~0u if
69 * original geometry */
70 uint32_t refined;
71
72 /* meshlet */
73 /* offsets within meshletVtx and meshletTri arrays with meshlet data */
74 uint32_t vtxOffset; // in vertices
75 uint32_t triOffset; // in indices (3*triangles)
76 /* number of vertices and triangles used in the meshlet; data is stored in consecutive range defined by offset and
77 * count */
78 uint32_t vtxCount;
79 uint32_t triCount;
80
81 /* bounds */
82 float4 centerRadius; // (x,y,z,r)
83 float4 coneAxisAngle; // (x,y,z,cos(half solid angle))
84 float3 coneApex; // (x,y,z)
85};
86static_assert(sizeof(FMeshlet) == 68);
87
89{
90 uint32_t meshletIndex;
91 uint32_t aliasOffset;
92 uint32_t triCount;
93 float area;
95 float radius;
96};
97static_assert(sizeof(FEmissiveMeshlet) == 32);
98
100{
102 uint32_t indexCount{0};
103};
104
127
129{
130 Vector<FVertex> vertices; // Full precision, raw vertices. Used by importers.
131 Vector<FQVertex> verticesQuantized; // Quantized vertex data for GPU upload.
132 Vector<FSkinBinding> skin; // Per-vertex skin binding; empty when rigid. Parallel to @ref vertices.
133 struct LOD
134 {
135 Vector<uint32_t> indices; // Full precision triangle indices
136 LOD(Allocator* alloc) : indices(alloc) {}
137 };
139 struct DAG
140 {
141 Vector<FLODGroup> groups; // LOD groups with error bounds
142 Vector<FMeshlet> meshlets; // Meshlets built from all clusters
143 Vector<uint8_t> meshletTri; // Meshlet local triangle indices
144 Vector<uint32_t> meshletVtx; // Meshlet vertex indices into vertices/verticesQuantized
148 DAG(Allocator* alloc) :
149 groups(alloc), meshlets(alloc), meshletTri(alloc), meshletVtx(alloc),
151 {
152 }
154
155 FImportedMesh(Allocator* alloc);
159 void Optimize();
164 void SimplifyLOD(int levels, float scale);
168 void ClusterizeDAG();
173 void Quantize();
178 void Dequantize();
183 bool EnsureQuantized();
184 [[nodiscard]] bool IsQuantized() const { return !verticesQuantized.empty(); }
188 bool EnsureRaw();
189 [[nodiscard]] bool IsRaw() const { return !vertices.empty(); }
196 [[nodiscard]] size_t CalculateQuantizedBound(bool lod, bool dag) const;
197};
198
199/* -- Math Exports -- */
200void CoordinateSystem(float3 n, float3& b1, float3& b2);
205
#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:89
float radius
Definition Mesh.hpp:95
float3 center
Definition Mesh.hpp:94
uint32_t aliasOffset
Definition Mesh.hpp:91
uint32_t meshletIndex
Definition Mesh.hpp:90
float area
Definition Mesh.hpp:93
uint32_t triCount
Definition Mesh.hpp:92
Definition Mesh.hpp:140
Vector< GSAlias > emissiveAliases
Definition Mesh.hpp:146
DAG(Allocator *alloc)
Definition Mesh.hpp:148
Vector< FEmissiveMeshlet > emissiveMeshlets
Definition Mesh.hpp:145
Vector< FMeshlet > meshlets
Definition Mesh.hpp:142
Vector< uint32_t > emissivePrimitiveMap
Definition Mesh.hpp:147
Vector< uint32_t > meshletVtx
Definition Mesh.hpp:144
Vector< FLODGroup > groups
Definition Mesh.hpp:141
Vector< uint8_t > meshletTri
Definition Mesh.hpp:143
Definition Mesh.hpp:134
LOD(Allocator *alloc)
Definition Mesh.hpp:136
Vector< uint32_t > indices
Definition Mesh.hpp:135
Definition Mesh.hpp:129
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:184
Vector< FQVertex > verticesQuantized
Definition Mesh.hpp:131
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:130
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:138
bool EnsureQuantized()
Prepares quantized GPU data buffers from possibly full-precision, or compressed data.
Definition Mesh.cpp:414
Vector< FSkinBinding > skin
Definition Mesh.hpp:132
bool IsRaw() const
Definition Mesh.hpp:189
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:49
float radius
Definition Mesh.hpp:55
float error
Definition Mesh.hpp:59
float3 center
Definition Mesh.hpp:54
int depth
Definition Mesh.hpp:51
Definition Mesh.hpp:64
uint32_t refined
Definition Mesh.hpp:70
uint32_t triOffset
Definition Mesh.hpp:75
uint32_t vtxCount
Definition Mesh.hpp:78
float3 coneApex
Definition Mesh.hpp:84
uint32_t vtxOffset
Definition Mesh.hpp:74
uint32_t group
Definition Mesh.hpp:67
float4 coneAxisAngle
Definition Mesh.hpp:83
float4 centerRadius
Definition Mesh.hpp:82
uint32_t triCount
Definition Mesh.hpp:79
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:100
uint32_t indexCount
Definition Mesh.hpp:102
FBlobRef indices
Definition Mesh.hpp:101
Definition Mesh.hpp:106
FBlobRef emissivePrimitiveMap
Definition Mesh.hpp:118
FSerializedBounds bounds
Definition Mesh.hpp:108
FBlobRef skinBinding
Definition Mesh.hpp:119
FUUID skeleton
Definition Mesh.hpp:120
uint32_t vertexCount
Definition Mesh.hpp:110
FSerializedMesh(Allocator *alloc=GLOBAL_ALLOC)
Definition Mesh.hpp:122
FBlobRef emissiveMeshlets
Definition Mesh.hpp:116
FBlobRef dagMeshlets
Definition Mesh.hpp:113
Vector< FSerializedMeshLOD > lods
Definition Mesh.hpp:111
FBlobRef dagMeshletVtx
Definition Mesh.hpp:115
FBlobRef vertices
Definition Mesh.hpp:109
FBlobRef emissiveAliases
Definition Mesh.hpp:117
FBlobRef dagGroups
Definition Mesh.hpp:112
FBlobRef dagMeshletTri
Definition Mesh.hpp:114
Definition Mesh.hpp:42
uint16_t joints[4]
Definition Mesh.hpp:43
float weights[4]
Definition Mesh.hpp:44
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