Skip to content

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.

Full uploads: one 64-bit hash sum tells a closed mesh (no per-vertex lists), cluster bounds computed while building the layout, packed vertices not read back from mapped memory

islandmeda953679e3394
diff --git a/src/core/UploadOrder.cpp b/src/core/UploadOrder.cpp
index b9594d5c..2f2309aa 100644
--- a/src/core/UploadOrder.cpp
+++ b/src/core/UploadOrder.cpp
@@ -72,12 +72,16 @@ void walkCluster(const Mesh& mesh, const UploadOrder& order, uint32_t c, F&& fn)
}
}
-// A 64-bit mix (splitmix64's finaliser) for comparing neighbour multisets as sums.
-uint64_t mix64(uint64_t x) {
- x += 0x9E3779B97F4A7C15ull;
- x = (x ^ (x >> 30)) * 0xBF58476D1CE4E5B9ull;
- x = (x ^ (x >> 27)) * 0x94D049BB133111EBull;
- return x ^ (x >> 31);
+// Two 64-bit mixes for the closed check (murmur3's finaliser with different constants).
+uint64_t mixA(uint64_t x) {
+ x ^= x >> 33;
+ x *= 0xFF51AFD7ED558CCDull;
+ return x ^ (x >> 33);
+}
+uint64_t mixB(uint64_t x) {
+ x ^= x >> 31;
+ x *= 0xC4CEB9FE1A85EC53ull;
+ return (x ^ (x >> 29)) | 1; // odd: w * b == 0 (mod 2^64) only when b == 0
}
// Sorts (key, value) pairs by key, keeping equal keys in order (LSD radix, 11 bits per pass).
@@ -180,29 +184,29 @@ void UploadOrder::build(const Mesh& mesh, const ClusterCallback& onCluster) {
toGpu_.resize(vertices_);
poolFor(vertices_, [&](size_t b, size_t e) { std::fill(toGpu_.begin() + std::ptrdiff_t(b), toGpu_.begin() + std::ptrdiff_t(e), kNone); },
1 << 16);
- std::vector<std::vector<std::pair<uint32_t, uint64_t>>> unbalanced(nc);
+ // Closed: every vertex's outgoing and incoming neighbours are the same multiset, i.e. the sum over its
+ // corners of mixA(next) - mixA(previous) is 0. Weighted by mixB of the vertex and summed over the mesh, that
+ // is 0 for a closed mesh and, for any other, anything (0 by a 2^-64 chance).
+ std::vector<uint64_t> balances(nc, 0);
// Each cluster's vertices in first-use order; a vertex's first slot is the lowest (an atomic min, so the
// result doesn't depend on which thread gets there first).
poolFor(nc, [&](size_t c0, size_t c1) {
for (size_t c = c0; c < c1; ++c) {
const Cluster& cl = clusters_[c];
thread_local std::vector<uint16_t> locals;
- thread_local std::vector<uint64_t> balance; // per local vertex: sum of hash(next) - hash(previous)
locals.clear();
- balance.assign(cl.vertexCount, 0);
- uint32_t corner[3], cornerLocal[3];
+ uint64_t balance = 0; // sum over corners a of mixB(a) * (mixA(next) - mixA(previous))
+ uint32_t corner[3];
int k = 0;
walkCluster(mesh, *this, uint32_t(c), [&](int local, uint32_t v, bool isNew) {
if (onCluster) locals.push_back(uint16_t(std::max(local, 0)));
- if (local >= 0) {
- corner[k] = v;
- cornerLocal[k] = uint32_t(local);
- }
+ corner[k] = v;
if (++k == 3) {
k = 0;
- if (local >= 0)
- for (int j = 0; j < 3; ++j)
- balance[cornerLocal[j]] += mix64(corner[(j + 1) % 3]) - mix64(corner[(j + 2) % 3]);
+ if (local >= 0) {
+ const uint64_t h0 = mixA(corner[0]), h1 = mixA(corner[1]), h2 = mixA(corner[2]);
+ balance += mixB(corner[0]) * (h1 - h2) + mixB(corner[1]) * (h2 - h0) + mixB(corner[2]) * (h0 - h1);
+ }
}
if (!isNew) return;
const uint32_t slot = cl.firstVertex + uint32_t(local);
@@ -213,9 +217,7 @@ void UploadOrder::build(const Mesh& mesh, const ClusterCallback& onCluster) {
}
});
if (onCluster) onCluster(uint32_t(c), locals.data());
- // Vertices whose edges don't balance within this cluster: settled once every cluster has added in.
- for (uint32_t l = 0; l < cl.vertexCount; ++l)
- if (balance[l] != 0) unbalanced[c].emplace_back(fromGpu_[cl.firstVertex + l], balance[l]);
+ balances[c] = balance;
const uint32_t padEnd = c + 1 < nc ? clusters_[c + 1].firstVertex : uint32_t(slots);
const uint32_t fill = cl.vertexCount ? fromGpu_[cl.firstVertex + cl.vertexCount - 1] : 0u;
std::fill(fromGpu_.begin() + cl.firstVertex + cl.vertexCount, fromGpu_.begin() + padEnd, fill);
@@ -241,24 +243,10 @@ void UploadOrder::build(const Mesh& mesh, const ClusterCallback& onCluster) {
std::vector<std::pair<uint32_t, uint32_t>>().swap(p);
}
radixSortByKey(bySlot, uint32_t(vertices_));
- // Closed: the unbalanced vertices (on seams, or open) sum to zero over all clusters.
- {
- std::vector<std::pair<uint32_t, uint64_t>> all;
- for (auto& u : unbalanced) {
- all.insert(all.end(), u.begin(), u.end());
- std::vector<std::pair<uint32_t, uint64_t>>().swap(u);
- }
- radixSortByKey(all, uint32_t(vertices_));
- closed_ = triangles_ > 0;
- for (size_t i = 0; i < all.size() && closed_;) {
- uint64_t sum = 0;
- size_t j = i;
- for (; j < all.size() && all[j].first == all[i].first; ++j) sum += all[j].second;
- closed_ = sum == 0;
- i = j;
- }
- }
for (const auto& e : bySlot) hasCopy_[e.first >> 6] |= uint64_t(1) << (e.first & 63);
+ uint64_t total = 0;
+ for (uint64_t b : balances) total += b;
+ closed_ = triangles_ > 0 && total == 0;
copies_ = std::move(bySlot);
marked_.assign((slots + kBlock - 1) / kBlock, 0);
blocks_.clear();
diff --git a/src/core/UploadOrder.h b/src/core/UploadOrder.h
index 84207bc3..f5f99c7c 100644
--- a/src/core/UploadOrder.h
+++ b/src/core/UploadOrder.h
@@ -49,8 +49,8 @@ public:
// a renderer a second pass to make them.
using ClusterCallback = std::function<void(uint32_t cluster, const uint16_t* localTriangles)>;
void build(const Mesh& mesh, const ClusterCallback& onCluster = {});
- // Every directed edge a->b of the mesh has as many b->a (found while building, from 64-bit hash sums of each
- // vertex's neighbours, so a false yes is a 2^-64 chance): the mesh is closed and consistently wound.
+ // Every directed edge a->b of the mesh has as many b->a (found while building, from a 64-bit hash sum, so a
+ // false yes is a 2^-64 chance): the mesh is closed and consistently wound.
bool closed() const { return closed_; }
// The number of clusters build() makes for `triangles` triangles.
static size_t clusterCount(size_t triangles) {
diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp
index a525876a..c4ae188a 100644
--- a/src/render/Renderer.cpp
+++ b/src/render/Renderer.cpp
@@ -915,9 +915,10 @@ void Renderer::uploadVertices(MeshSlot& s, const Mesh& mesh, bool full) {
uint64_t hp = 0, hn = 0;
for (uint32_t i = b; i < e; ++i) {
const uint32_t v = order[i];
- PackedVertex& pv = out[i - b];
+ PackedVertex pv; // packed here and stored once: `out` may be mapped GPU memory, slow to read back
inside &= packVertex(s.grid, mesh.positions[v], normals ? mesh.normals[v] : glm::vec3(0.0f, 0.0f, 1.0f),
curvatureAt(mesh, v), pv);
+ out[i - b] = pv;
hp = (hp ^ (uint64_t(pv.pos[0]) | uint64_t(pv.pos[1]) << 32)) * 0x100000001B3ull + i;
hn = (hn ^ pv.normal) * 0x100000001B3ull + i;
if ((i + 1) % UploadOrder::kBlock == 0 || i + 1 == e) {
@@ -953,7 +954,7 @@ void Renderer::uploadVertices(MeshSlot& s, const Mesh& mesh, bool full) {
// Culling bounds of the clusters movedRuns_ touches (every copy of a moved vertex is in a run, so every cluster
// with a moved triangle is), or of all.
-void Renderer::updateBounds(MeshSlot& s, const Mesh& mesh, bool all) {
+void Renderer::updateBounds(MeshSlot& s, const Mesh& mesh, bool all, bool computed) {
const auto clusters = s.order.clusters();
s.bounds.resize(clusters.size());
cullScratch_.clear();
@@ -969,6 +970,7 @@ void Renderer::updateBounds(MeshSlot& s, const Mesh& mesh, bool all) {
}
s.proxyStale.resize(clusters.size(), 1);
for (uint32_t c : cullScratch_) s.proxyStale[c] = 1;
+ if (computed) cullScratch_.clear();
poolFor(cullScratch_.size(), [&](size_t i0, size_t i1) {
for (size_t i = i0; i < i1; ++i) {
const uint32_t c = cullScratch_[i];
@@ -1251,21 +1253,23 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, boo
auto* indices = nt ? static_cast<uint16_t*>(gl::MapBufferRange(GL_ELEMENT_ARRAY_BUFFER, 0, GLsizeiptr(nt * 3 * sizeof(uint16_t)),
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT))
: nullptr;
- UploadOrder::ClusterCallback write;
- if (indices)
- write = [&](uint32_t c, const uint16_t* local) {
- const uint32_t count = s.order.clusterTriangles(c);
- uint16_t* out = indices + size_t(s.order.firstTriangle(c)) * 3;
- size_t drawn = count;
- if (!hidden) {
- std::copy_n(local, size_t(count) * 3, out);
- } else {
- drawn = 0;
- for (uint32_t t = 0; t < count; ++t)
- if (!mesh.hidden[s.order.triangle(c, t)]) std::copy_n(local + size_t(t) * 3, 3, out + 3 * drawn++);
- }
- s.drawCount[c] = GLsizei(drawn * 3);
- };
+ // The culling bounds too, while each cluster's triangles are at hand.
+ s.bounds.assign(UploadOrder::clusterCount(nt), ClusterBounds());
+ UploadOrder::ClusterCallback write = [&](uint32_t c, const uint16_t* local) {
+ s.bounds[c] = clusterBounds(mesh, s.order, c);
+ if (!indices) return;
+ const uint32_t count = s.order.clusterTriangles(c);
+ uint16_t* out = indices + size_t(s.order.firstTriangle(c)) * 3;
+ size_t drawn = count;
+ if (!hidden) {
+ std::copy_n(local, size_t(count) * 3, out);
+ } else {
+ drawn = 0;
+ for (uint32_t t = 0; t < count; ++t)
+ if (!mesh.hidden[s.order.triangle(c, t)]) std::copy_n(local + size_t(t) * 3, 3, out + 3 * drawn++);
+ }
+ s.drawCount[c] = GLsizei(drawn * 3);
+ };
s.order.build(mesh, write);
indicesDone = indices && gl::UnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
gl::BindVertexArray(0);
@@ -1279,7 +1283,7 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, boo
const std::span<const uint32_t> order = s.order.fromGpu();
if (full || ranged) {
uploadVertices(s, mesh, full);
- updateBounds(s, mesh, full);
+ updateBounds(s, mesh, full, full); // (a full upload computed them while building)
}
if (full) {
s.proxyAllocated = false; // sized for the old mesh
diff --git a/src/render/Renderer.h b/src/render/Renderer.h
index 89ff3134..0e8dfd0b 100644
--- a/src/render/Renderer.h
+++ b/src/render/Renderer.h
@@ -180,7 +180,8 @@ private:
static void freeSlot(MeshSlot& s);
void sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap = nullptr, bool wireframe = false);
void uploadVertices(MeshSlot& s, const Mesh& mesh, bool full);
- void updateBounds(MeshSlot& s, const Mesh& mesh, bool all);
+ // `computed`: the bounds are already current (only the totals are left).
+ void updateBounds(MeshSlot& s, const Mesh& mesh, bool all, bool computed = false);
void buildLines(MeshSlot& s, const Mesh& mesh);
void buildProxies(MeshSlot& s, const Mesh& mesh);
bool proxyUsed_ = false; // the last draw() drew motion proxies: sync keeps them current