fixed the bottleneck....... (mesh.makeViews incurred a very heavy penalty per triangle, currently naively caching the mesh views until I can be bothered to muck up the function headers)

This commit is contained in:
ecker 2025-09-15 17:28:13 -05:00
parent 1e548265ad
commit 16062f8135
4 changed files with 8 additions and 6 deletions

View File

@ -6,7 +6,7 @@
"metadata": {
"holdable": true,
"physics": {
"mass": 0,
"mass": 5,
"type": "bounding box"
// "type": "mesh"
}

View File

@ -348,7 +348,7 @@ namespace {
if ( bvh.nodes.empty() ) return;
// update leaf bounds
#pragma omp parallel for
//#pragma omp parallel for
for ( auto i = 0; i < bvh.nodes.size(); i++ ) {
auto& node = bvh.nodes[i];
if ( node.getCount() == 0 ) continue;

View File

@ -13,8 +13,8 @@ namespace {
uint32_t substeps = 0; // number of substeps per frame tick
uint32_t reserveCount = 32; // amount of elements to reserve for vectors used in this system, to-do: have it tie to a memory pool allocator
// increasing these make things lag for reasons I can imagine why (having to test more triangles over just more boxes)
uint32_t broadphaseBvhCapacity = 4; // number of bodies per leaf node
// increasing these make things lag for reasons I can imagine why
uint32_t broadphaseBvhCapacity = 1; // number of bodies per leaf node
uint32_t meshBvhCapacity = 1; // number of triangles per leaf node
// additionally flattens a BVH for linear iteration, rather than a recursive / stack-based traversal

View File

@ -92,7 +92,9 @@ namespace {
}
pod::TriangleWithNormal fetchTriangle( const uf::Mesh& mesh, size_t triID ) {
auto views = mesh.makeViews({"position", "normal"});
static thread_local uf::stl::unordered_map<const uf::Mesh*, uf::stl::vector<uf::Mesh::View>> cachedViews;
if ( cachedViews.count(&mesh) == 0 ) cachedViews[&mesh] = mesh.makeViews({"position"});
auto& views = cachedViews[&mesh];
UF_ASSERT(!views.empty());
// find which view contains this triangle index.
@ -110,7 +112,6 @@ namespace {
UF_ASSERT( view );
auto& positions = (*view)["position"];
auto& normals = (*view)["normal"];
auto& indices = (*view)["index"];
pod::TriangleWithNormal tri = { ::fetchTriangle( *view, indices, positions, triID ) };
@ -122,6 +123,7 @@ namespace {
// if body is a mesh, apply its transform to the triangles, else reorient the normal with respect to the body
pod::TriangleWithNormal fetchTriangle( const uf::Mesh& mesh, size_t triID, const pod::PhysicsBody& body, bool fast = false ) {
auto tri = ::fetchTriangle( mesh, triID );
auto transform = ::getTransform( body );
if ( body.collider.type == pod::ShapeType::MESH ) {