SpiecsEngine
 
Loading...
Searching...
No Matches

◆ AppendMeshlets()

void Spices::MeshProcessor::AppendMeshlets ( MeshPack * meshPack,
uint32_t lod,
const SpicesShader::Sphere & clusterBoundSphere,
const std::vector< glm::uvec3 > & primVertices )
staticprivate

Create and Append Meshlets to MeshPack use given indices.

Parameters
[in]meshPackMeshPack.
[in]lodcurrent lod level.
[in]clusterBoundSphereCluster's Bound Sphere.
[in]primVerticesPrimVertices Buffer.

normal cone weight set 0.5f; Get Const variable.

Init meshopt variable.

Pack Sparse Inputs.

Build Meshlets.

Adjust meshopt variable.

Optimize meshlets and compute meshlet bound and cone.

Layout map for primpoints.

Fill in data back to meshpack variable.

Fill in Lod data.

normal cone weight set 0.5f; Get Const variable.

Init meshopt variable.

Pack Sparse Inputs.

Build Meshlets.

Adjust meshopt variable.

Optimize meshlets and compute meshlet bound and cone.

Layout map for primpoints.

Fill in data back to meshpack variable.

Fill in Lod data.

Definition at line 154 of file MeshProcessor.cpp.

160 {
162
167 const float coneWeight = 0.5f;
168 const uint32_t primLocationsOffset = meshPack->m_MeshResource.primitiveLocations.attributes->size();
169 const uint32_t primVerticesOffset = meshPack->m_MeshResource.primitiveVertices.attributes->size();
170 const uint32_t meshletsOffset = meshPack->m_MeshResource.meshlets.attributes->size();
171
175 size_t max_meshlets = meshopt_buildMeshletsBound(primVertices.size() * 3, SpicesShader::MESHLET_NVERTICES, SpicesShader::MESHLET_NPRIMITIVES);
176 std::vector<meshopt_Meshlet> meshoptlets(max_meshlets);
177 std::vector<unsigned int> meshlet_vertices(max_meshlets * SpicesShader::MESHLET_NVERTICES);
178 std::vector<unsigned char> meshlet_triangles(max_meshlets * SpicesShader::MESHLET_NPRIMITIVES * 3);
179
183 std::vector<glm::vec3> packPoints;
184 std::vector<glm::uvec3> packPrimPoints;
185 std::unordered_map<uint32_t, uint32_t> primVerticesMapReverse;
186 PackPrimVerticesFromSparseInputs(meshPack, primVertices, packPoints, packPrimPoints, primVerticesMapReverse);
187
191 size_t nMeshlet = meshopt_buildMeshlets(
192 meshoptlets.data() ,
193 meshlet_vertices.data() ,
194 meshlet_triangles.data() ,
195 &packPrimPoints[0].x ,
196 packPrimPoints.size() * 3 ,
197 &packPoints[0].x ,
198 packPoints.size() ,
199 sizeof(glm::vec3) ,
200 SpicesShader::MESHLET_NVERTICES ,
201 SpicesShader::MESHLET_NPRIMITIVES ,
202 coneWeight
203 );
204
208 const meshopt_Meshlet& last = meshoptlets[nMeshlet - 1];
209 meshoptlets .resize(nMeshlet);
210 meshlet_vertices .resize(last.vertex_offset + last.vertex_count);
211 meshlet_triangles.resize(last.triangle_offset + (last.triangle_count * 3 + 3) & ~3);
212
216 uint32_t nPrimitives = 0;
217 for (size_t i = 0; i < nMeshlet; ++i)
218 {
219 meshopt_optimizeMeshlet(
220 &meshlet_vertices[meshoptlets[i].vertex_offset] ,
221 &meshlet_triangles[meshoptlets[i].triangle_offset] ,
222 meshoptlets[i].triangle_count, meshoptlets[i].vertex_count
223 );
224
225 const meshopt_Meshlet& m = meshoptlets[i];
226 meshopt_Bounds bounds = meshopt_computeMeshletBounds(
227 &meshlet_vertices[m.vertex_offset] ,
228 &meshlet_triangles[m.triangle_offset] ,
229 m.triangle_count ,
230 &packPoints[0].x,
231 packPoints.size(),
232 sizeof(glm::vec3)
233 );
234
235 Meshlet meshlet;
236 meshlet.FromMeshopt(meshoptlets[i], bounds);
237 meshlet.primitiveOffset = nPrimitives;
238
239 meshlet.vertexOffset += primLocationsOffset;
240 meshlet.primitiveOffset += primVerticesOffset;
241 meshlet.lod = lod;
242
243 meshlet.clusterBoundSphere = clusterBoundSphere;
244
245 meshPack->m_MeshResource.meshlets.attributes->push_back(std::move(meshlet));
246
247 nPrimitives += m.triangle_count;
248 }
249
253 std::unordered_map<glm::uvec3, uint32_t> inPrimPointsLayoutMap;
254 auto& vertices = *meshPack->m_MeshResource.vertices.attributes;
255 for (auto& primVertex : primVertices)
256 {
257 inPrimPointsLayoutMap[{ vertices[primVertex.x].x, vertices[primVertex.y].x, vertices[primVertex.z].x }] = inPrimPointsLayoutMap.size();
258 }
259
263 const Meshlet& lastm = (*meshPack->m_MeshResource.meshlets.attributes)[meshletsOffset + nMeshlet - 1];
264 meshPack->m_MeshResource.primitivePoints .attributes->resize(lastm.primitiveOffset + lastm.nPrimitives);
265 meshPack->m_MeshResource.primitiveVertices .attributes->resize(lastm.primitiveOffset + lastm.nPrimitives);
266 meshPack->m_MeshResource.primitiveLocations .attributes->resize(lastm.primitiveOffset + lastm.nPrimitives);
267
268 for (uint32_t i = 0; i < nMeshlet; i++)
269 {
270 const meshopt_Meshlet& m = meshoptlets[i];
271 const Meshlet& ml = (*meshPack->m_MeshResource.meshlets.attributes)[meshletsOffset + i];
272
273 for (uint32_t j = 0; j < m.triangle_count; j++)
274 {
275 uint32_t a = (uint32_t)meshlet_triangles[m.triangle_offset + 3 * j + 0] + m.vertex_offset;
276 uint32_t b = (uint32_t)meshlet_triangles[m.triangle_offset + 3 * j + 1] + m.vertex_offset;
277 uint32_t c = (uint32_t)meshlet_triangles[m.triangle_offset + 3 * j + 2] + m.vertex_offset;
278
279 uint32_t& x = vertices[primVerticesMapReverse[meshlet_vertices[a]]].x;
280 uint32_t& y = vertices[primVerticesMapReverse[meshlet_vertices[b]]].x;
281 uint32_t& z = vertices[primVerticesMapReverse[meshlet_vertices[c]]].x;
282
283 (*meshPack->m_MeshResource.primitivePoints.attributes)[ml.primitiveOffset + j] = { x, y, z };
284
285 (*meshPack->m_MeshResource.primitiveLocations.attributes)[ml.primitiveOffset + j].x = a + primLocationsOffset;
286 (*meshPack->m_MeshResource.primitiveLocations.attributes)[ml.primitiveOffset + j].y = b + primLocationsOffset;
287 (*meshPack->m_MeshResource.primitiveLocations.attributes)[ml.primitiveOffset + j].z = c + primLocationsOffset;
288
289 (*meshPack->m_MeshResource.primitiveVertices.attributes)[ml.primitiveOffset + j] = primVertices[inPrimPointsLayoutMap[{ x, y, z }]];
290 }
291 }
292
296 uint32_t nLods = meshPack->m_MeshResource.lods.attributes->size();
297
298 if (nLods == lod + 1)
299 {
300 Lod& lodRef = (*meshPack->m_MeshResource.lods.attributes)[lod];
301
302 lodRef.nPrimitives += lastm.primitiveOffset + lastm.nPrimitives - primVerticesOffset;
303 lodRef.nMeshlets += nMeshlet;
304 }
305 else
306 {
307 Lod lodData;
308 lodData.primVertexOffset = primVerticesOffset;
309 lodData.nPrimitives = lastm.primitiveOffset + lastm.nPrimitives - primVerticesOffset;
310 lodData.nMeshlets = nMeshlet;
311 lodData.meshletOffset = meshletsOffset;
312
313 meshPack->m_MeshResource.lods.attributes->push_back(std::move(lodData));
314 }
315 }
#define SPICES_PROFILE_ZONE
static bool PackPrimVerticesFromSparseInputs(MeshPack *meshPack, const std::vector< glm::uvec3 > primVertices, std::vector< glm::vec3 > &packPoints, std::vector< glm::uvec3 > &packPrimPoints, std::unordered_map< uint32_t, uint32_t > &primVerticesMapReverse)
Pack PrimVertices from Sparse PrimVertices Inputs.

References Spices::Lod::meshletOffset, Spices::Lod::nPrimitives, and Spices::Lod::primVertexOffset.