Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.
C++ 3d3d-sculptingcppdear-imguidigital-sculptingfbxgame-developmentgaussian-splattinggltfimage-to-3dmcpopenglopenusdsculptingtext-to-3dunreal-engine
Small dabs stay on one thread: the vertex query by the triangles under its tasks, set listing by chunks
islandmed2a7a47dbcfd5
diff --git a/src/core/BVH.cpp b/src/core/BVH.cppindex ee507cc6..9ecfac34 100644--- a/src/core/BVH.cpp+++ b/src/core/BVH.cpp@@ -259,11 +259,12 @@ void BVH::queryVertices(const Mesh& mesh, const glm::vec3& center, float radius,// The units of parallel work: the subtrees the sphere touches, split breadth first until there are enough// of them (the same ones for any thread count).- constexpr size_t kTasks = 128;+ constexpr size_t kTasks = 128, kSerialTriangles = 16384;std::vector<uint32_t>& tasks = queryTasks_;tasks.clear();if (sphereBox(nodes_[0].box, center, r2)) tasks.push_back(0);- for (bool split = true; split && tasks.size() < kTasks;) {+ int depth = 0; // of every inner node in `tasks` (median splits: each holds about nt >> depth triangles)+ for (bool split = true; split && tasks.size() < kTasks; ++depth) {split = false;queryNext_.clear();for (uint32_t ni : tasks) {@@ -323,6 +324,13 @@ void BVH::queryVertices(const Mesh& mesh, const glm::vec3& center, float radius,};// The set doesn't depend on which thread found what, take() lists it in index order, and the leaves are// joined in task order.+ size_t work = 0; // triangles under the tasks, roughly+ for (uint32_t ni : tasks) work += nodes_[ni].count > 0 ? nodes_[ni].count : mesh.triangleCount() >> depth;+ if (work < kSerialTriangles) { // waking threads would cost more+ for (uint32_t root : tasks) walk(root, stack_, queryLeaves_, vertBits_);+ vertBits_.take(out);+ return;+ }if (queryParts_.size() < tasks.size()) queryParts_.resize(tasks.size());vertBits_.insertParallel(tasks.size(), 1, [&](size_t b, size_t e, IndexBits& found) {std::vector<uint32_t> nodeStack;diff --git a/src/core/IndexBits.cpp b/src/core/IndexBits.cppindex ae1035a3..25bc278a 100644--- a/src/core/IndexBits.cpp+++ b/src/core/IndexBits.cpp@@ -78,7 +78,7 @@ void IndexBits::absorb(std::span<IndexBits> parts) {}}}- }, 8);+ }, 32);for (IndexBits& p : parts) std::fill(p.summary_.begin(), p.summary_.end(), 0);}@@ -87,7 +87,7 @@ void IndexBits::take(std::vector<uint32_t>& out, const std::function<void(size_tconst size_t nc = chunks_.size();if (nc == 0) return;// Count each chunk's members, then write them at their offsets (blocks of chunks, so small sets run inline).- constexpr size_t kBlock = 8;+ constexpr size_t kBlock = 32; // chunks per job: small sets run inlinecounts_.resize(nc + 1);poolFor(nc, [&](size_t b, size_t e) {for (size_t k = b; k < e; ++k) {