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.

Faster full uploads: indices written into the mapped buffer while the layout is built, whole vertex arrays written mapped in parallel, the closed check made during the layout build; flat hash for the motion proxy

islandmede6778e8089d3
diff --git a/src/core/UploadOrder.cpp b/src/core/UploadOrder.cpp
index 054d67d9..b9594d5c 100644
--- a/src/core/UploadOrder.cpp
+++ b/src/core/UploadOrder.cpp
@@ -72,10 +72,19 @@ 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);
+}
+
// Sorts (key, value) pairs by key, keeping equal keys in order (LSD radix, 11 bits per pass).
-void radixSortByKey(std::vector<std::pair<uint32_t, uint32_t>>& items, uint32_t maxKey) {
+template <class V>
+void radixSortByKey(std::vector<std::pair<uint32_t, V>>& items, uint32_t maxKey) {
if (items.size() < 2) return;
- std::vector<std::pair<uint32_t, uint32_t>> tmp(items.size());
+ std::vector<std::pair<uint32_t, V>> tmp(items.size());
constexpr uint32_t kDigit = 11, kBuckets = 1u << kDigit;
const int bits = std::max(1, int(std::bit_width(maxKey)));
std::vector<uint32_t> count(kBuckets);
@@ -107,7 +116,7 @@ uint32_t UploadOrder::clusterOfSlot(uint32_t i) const {
return it == clusters_.begin() ? 0u : uint32_t(it - clusters_.begin() - 1);
}
-void UploadOrder::build(const Mesh& mesh) {
+void UploadOrder::build(const Mesh& mesh, const ClusterCallback& onCluster) {
triangles_ = mesh.triangleCount();
vertices_ = mesh.vertexCount();
// Chunks along a Morton curve of their centres (10 bits per axis over the mesh's bounds); a partial last
@@ -166,14 +175,35 @@ void UploadOrder::build(const Mesh& mesh) {
c.firstVertex = uint32_t(slots);
slots += (size_t(c.vertexCount) + kBlock - 1) & ~size_t(kBlock - 1);
}
- fromGpu_.assign(slots, 0);
- toGpu_.assign(vertices_, kNone);
+ // (Filled in parallel: these are tens of millions of entries.)
+ fromGpu_.resize(slots);
+ 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);
// 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];
+ 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);
+ }
+ 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 (!isNew) return;
const uint32_t slot = cl.firstVertex + uint32_t(local);
fromGpu_[slot] = v;
@@ -182,6 +212,10 @@ void UploadOrder::build(const Mesh& mesh) {
while (slot < cur && !first.compare_exchange_weak(cur, slot, std::memory_order_relaxed)) {
}
});
+ 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]);
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);
@@ -190,13 +224,40 @@ void UploadOrder::build(const Mesh& mesh) {
// The other copies, by vertex (a stable sort of the slot-ordered list, so slots stay increasing).
copies_.clear();
hasCopy_.assign((vertices_ + 63) / 64, 0);
+ std::vector<std::vector<std::pair<uint32_t, uint32_t>>> perCluster(nc);
+ poolFor(nc, [&](size_t c0, size_t c1) {
+ for (size_t c = c0; c < c1; ++c) {
+ const Cluster& cl = clusters_[c];
+ for (uint32_t i = cl.firstVertex; i < cl.firstVertex + cl.vertexCount; ++i)
+ if (toGpu_[fromGpu_[i]] != i) perCluster[c].emplace_back(fromGpu_[i], i);
+ }
+ }, 16);
+ size_t copyCount = 0;
+ for (const auto& p : perCluster) copyCount += p.size();
std::vector<std::pair<uint32_t, uint32_t>> bySlot;
- for (size_t c = 0; c < nc; ++c) {
- const Cluster& cl = clusters_[c];
- for (uint32_t i = cl.firstVertex; i < cl.firstVertex + cl.vertexCount; ++i)
- if (toGpu_[fromGpu_[i]] != i) bySlot.emplace_back(fromGpu_[i], i);
+ bySlot.reserve(copyCount);
+ for (auto& p : perCluster) {
+ bySlot.insert(bySlot.end(), p.begin(), p.end());
+ 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);
copies_ = std::move(bySlot);
marked_.assign((slots + kBlock - 1) / kBlock, 0);
diff --git a/src/core/UploadOrder.h b/src/core/UploadOrder.h
index bcdc4f01..84207bc3 100644
--- a/src/core/UploadOrder.h
+++ b/src/core/UploadOrder.h
@@ -23,6 +23,7 @@
#include "core/Mesh.h"
#include <cstdint>
+#include <functional>
#include <span>
#include <utility>
#include <vector>
@@ -43,8 +44,19 @@ public:
uint32_t triangleCount = 0;
};
- // Lays out `mesh` (its triangles and vertex count, and positions for the chunk order).
- void build(const Mesh& mesh);
+ // Lays out `mesh` (its triangles and vertex count, and positions for the chunk order). `onCluster`, when
+ // given, gets each cluster's localTriangles as they are made (in parallel, from any thread), which saves
+ // 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.
+ bool closed() const { return closed_; }
+ // The number of clusters build() makes for `triangles` triangles.
+ static size_t clusterCount(size_t triangles) {
+ const size_t chunks = (triangles + kChunkTriangles - 1) >> kChunkShift;
+ return (chunks + kClusterChunks - 1) / kClusterChunks;
+ }
// GPU vertex slots, including the padding that block-aligns each cluster.
size_t size() const { return fromGpu_.size(); }
// fromGpu()[i] = the mesh vertex GPU slot i holds (padding repeats a vertex of its cluster).
@@ -80,6 +92,7 @@ public:
private:
size_t triangles_ = 0, vertices_ = 0;
+ bool closed_ = false;
std::vector<Cluster> clusters_;
std::vector<uint32_t> chunks_; // chunk ids in cluster order (the mesh's partial last chunk, if any, last)
std::vector<uint32_t> toGpu_, fromGpu_;
diff --git a/src/render/Clusters.cpp b/src/render/Clusters.cpp
index dc4556c1..c708dc05 100644
--- a/src/render/Clusters.cpp
+++ b/src/render/Clusters.cpp
@@ -10,7 +10,6 @@
#include <cmath>
#include <cstring>
#include <map>
-#include <unordered_map>
#include <tuple>
#include <vector>
@@ -160,6 +159,39 @@ ClusterBounds clusterBounds(const Mesh& mesh, const UploadOrder& order, uint32_t
return b;
}
+namespace {
+
+// Cell -> its representative: an open-addressing table cleared by a stamp (a cluster has at most 3 vertices
+// per triangle, so 2^15 slots stay under half full).
+struct CellTable {
+ static constexpr uint32_t kBits = 15, kSize = 1u << kBits;
+ std::vector<uint64_t> keys = std::vector<uint64_t>(kSize);
+ std::vector<uint32_t> stamps = std::vector<uint32_t>(kSize, 0);
+ std::vector<uint16_t> reps = std::vector<uint16_t>(kSize);
+ std::vector<float> dists = std::vector<float>(kSize);
+ uint32_t stamp = 0;
+ void clear() {
+ if (++stamp == 0) {
+ std::fill(stamps.begin(), stamps.end(), 0);
+ stamp = 1;
+ }
+ }
+ uint32_t slot(uint64_t key, bool& found) {
+ for (uint32_t h = uint32_t((key * 0x9E3779B97F4A7C15ull) >> (64 - kBits));; h = (h + 1) & (kSize - 1)) {
+ if (stamps[h] != stamp) {
+ found = false;
+ return h;
+ }
+ if (keys[h] == key) {
+ found = true;
+ return h;
+ }
+ }
+ }
+};
+
+} // namespace
+
bool clusterProxy(const Mesh& mesh, const UploadOrder& order, uint32_t c, size_t maxTriangles, std::vector<uint16_t>& out,
ClusterBounds& bounds) {
out.clear();
@@ -190,28 +222,34 @@ bool clusterProxy(const Mesh& mesh, const UploadOrder& order, uint32_t c, size_t
if (!counted || !box.valid()) return false;
float cell = float(edges / double(counted)) * 2.5f;
if (!(cell > 0.0f)) return false;
- std::unordered_map<uint64_t, std::pair<uint16_t, float>> cells; // cell -> (representative, its distance to the centre)
+ thread_local CellTable cells;
+ thread_local std::vector<uint32_t> cellOfVertex;
+ cellOfVertex.resize(nl);
rep.resize(nl);
for (int attempt = 0; attempt < 4; ++attempt, cell *= 1.6f) {
cells.clear();
- auto cellOf = [&](const glm::vec3& p, glm::vec3& centre) {
- const glm::vec3 q = glm::floor((p - box.min) / cell);
- centre = box.min + (q + 0.5f) * cell;
- return uint64_t(uint32_t(q.x)) | (uint64_t(uint32_t(q.y)) << 21) | (uint64_t(uint32_t(q.z)) << 42);
- };
+ const float inv = 1.0f / cell;
for (uint32_t l = 0; l < nl; ++l) {
if (pinned[l]) continue;
- glm::vec3 centre;
const glm::vec3 p = pos(l);
- const uint64_t key = cellOf(p, centre);
+ const glm::vec3 q = glm::floor((p - box.min) * inv);
+ const glm::vec3 centre = box.min + (q + 0.5f) * cell;
+ const uint64_t key = uint64_t(uint32_t(q.x)) | (uint64_t(uint32_t(q.y)) << 21) | (uint64_t(uint32_t(q.z)) << 42);
const float d = glm::distance(p, centre);
- auto [it, added] = cells.emplace(key, std::make_pair(uint16_t(l), d));
- if (!added && d < it->second.second) it->second = {uint16_t(l), d};
- }
- for (uint32_t l = 0; l < nl; ++l) {
- glm::vec3 centre;
- rep[l] = pinned[l] ? uint16_t(l) : cells.at(cellOf(pos(l), centre)).first;
+ bool found = false;
+ const uint32_t h = cells.slot(key, found);
+ if (!found) {
+ cells.stamps[h] = cells.stamp;
+ cells.keys[h] = key;
+ cells.reps[h] = uint16_t(l);
+ cells.dists[h] = d;
+ } else if (d < cells.dists[h]) {
+ cells.reps[h] = uint16_t(l);
+ cells.dists[h] = d;
+ }
+ cellOfVertex[l] = h;
}
+ for (uint32_t l = 0; l < nl; ++l) rep[l] = pinned[l] ? uint16_t(l) : cells.reps[cellOfVertex[l]];
out.clear();
for (uint32_t i = 0; i < n; ++i) {
if (hidden && mesh.hidden[order.triangle(c, i)]) continue;
@@ -269,21 +307,29 @@ bool closedOriented(const Mesh& mesh) {
}
}
};
- // The vertices whose edges don't pair up.
+ // The vertices whose edges don't pair up: compared as sums of a 64-bit hash of the neighbour (no sorting;
+ // equal multisets give equal sums, and unequal ones colliding is a 2^-64 chance).
+ auto hash = [](uint64_t x) {
+ x += 0x9E3779B97F4A7C15ull;
+ x = (x ^ (x >> 30)) * 0xBF58476D1CE4E5B9ull;
+ x = (x ^ (x >> 27)) * 0x94D049BB133111EBull;
+ return x ^ (x >> 31);
+ };
constexpr size_t kBlock = 1 << 14;
std::vector<std::vector<uint32_t>> open((nv + kBlock - 1) / kBlock);
std::atomic<size_t> openCount{0};
const size_t limit = std::max<size_t>(64, nv / 100);
poolFor(nv, [&](size_t v0, size_t v1) {
- thread_local std::vector<uint32_t> outs, ins;
std::vector<uint32_t>& list = open[v0 / kBlock];
for (size_t a = v0; a < v1 && openCount.load(std::memory_order_relaxed) <= limit; ++a) {
- outs.clear();
- ins.clear();
- fan(uint32_t(a), outs, ins);
- std::sort(outs.begin(), outs.end());
- std::sort(ins.begin(), ins.end());
- if (outs != ins) {
+ uint64_t sum = 0;
+ for (uint32_t t : mesh.vertexTriangles(uint32_t(a))) {
+ if (t >= nt) continue;
+ const glm::uvec3& tri = mesh.triangles[t];
+ for (int c = 0; c < 3; ++c)
+ if (tri[c] == a) sum += hash(tri[(c + 1) % 3]) - hash(tri[(c + 2) % 3]);
+ }
+ if (sum != 0) {
list.push_back(uint32_t(a));
openCount.fetch_add(1, std::memory_order_relaxed);
}
diff --git a/src/render/GL.h b/src/render/GL.h
index e9f9698f..d10b24aa 100644
--- a/src/render/GL.h
+++ b/src/render/GL.h
@@ -94,6 +94,8 @@ constexpr GLenum GL_UNPACK_ROW_LENGTH = 0x0CF2;
constexpr GLenum GL_TEXTURE3 = 0x84C3;
constexpr GLenum GL_TEXTURE4 = 0x84C4;
constexpr GLenum GL_POINTS = 0x0000;
+constexpr GLbitfield GL_MAP_WRITE_BIT = 0x0002;
+constexpr GLbitfield GL_MAP_INVALIDATE_BUFFER_BIT = 0x0008;
constexpr GLenum GL_UNSIGNED_SHORT = 0x1403;
constexpr GLenum GL_CULL_FACE = 0x0B44;
constexpr GLenum GL_R16UI = 0x8234;
@@ -179,6 +181,8 @@ constexpr GLbitfield GL_COLOR_BUFFER_BIT = 0x4000;
X(void, VertexAttrib3f, GLuint, GLfloat, GLfloat, GLfloat) \
X(void, VertexAttrib4f, GLuint, GLfloat, GLfloat, GLfloat, GLfloat) \
X(void, DrawElementsBaseVertex, GLenum, GLsizei, GLenum, const void*, GLint) \
+ X(void*, MapBufferRange, GLenum, GLintptr, GLsizeiptr, GLbitfield) \
+ X(GLboolean, UnmapBuffer, GLenum) \
X(void, MultiDrawElementsBaseVertex, GLenum, const GLsizei*, GLenum, const void* const*, GLsizei, const GLint*)
namespace gl {
diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp
index f4719dde..856b1266 100644
--- a/src/render/Renderer.cpp
+++ b/src/render/Renderer.cpp
@@ -787,6 +787,15 @@ void uploadGathered(std::vector<uint8_t>& staging, GLuint buffer, size_t element
}
pieces.clear();
};
+ // A whole array: written straight into the mapped buffer, in parallel (no staging copy).
+ if (allocate && runs.size() == 1 && runs[0].first == 0 && runs[0].second == count && count > 0) {
+ if (auto* dst = static_cast<uint8_t*>(gl::MapBufferRange(GL_ARRAY_BUFFER, 0, GLsizeiptr(count * elementSize),
+ GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT))) {
+ poolFor(count, [&](size_t b, size_t e) { fill(uint32_t(b), uint32_t(e), dst + b * elementSize); }, kPiece);
+ if (gl::UnmapBuffer(GL_ARRAY_BUFFER)) return;
+ // (The data was lost, which drivers may do on a mode switch: sent the usual way below.)
+ }
+ }
size_t used = 0;
staging.resize(size_t(std::min<size_t>(kBatch, count)) * elementSize);
for (const auto& [b, e] : runs)
@@ -927,20 +936,14 @@ void Renderer::updateBounds(MeshSlot& s, const Mesh& mesh, bool all) {
}
}
-// Index buffer without hidden triangles (each cluster's drawn triangles first in its part of the buffer), the
-// first drawn triangle of each cluster, and the polygroup id and UVs of every drawn triangle in draw order
-// (looked up by that plus gl_PrimitiveID). The wireframe's edges are rebuilt on next need.
-void Renderer::uploadFaces(MeshSlot& s, const Mesh& mesh, bool wantUvs) {
+// The index buffer: each cluster's drawn triangles (hidden ones left out) first in its part of the buffer.
+void Renderer::uploadIndices(MeshSlot& s, const Mesh& mesh) {
const size_t nt = mesh.triangleCount();
const bool hidden = mesh.hidden.size() == nt && nt > 0;
- const bool groups = mesh.polygroups.size() == nt && nt > 0;
- const auto clusters = s.order.clusters();
- const size_t nc = clusters.size();
+ const size_t nc = s.order.clusters().size();
s.drawCount.assign(nc, 0);
- s.primBase.assign(nc, 0);
gl::BindVertexArray(s.vao);
gl::BufferData(GL_ELEMENT_ARRAY_BUFFER, GLsizeiptr(nt * 3 * sizeof(uint16_t)), nullptr, GL_STATIC_DRAW);
- s.bytes[1] = nt * 3 * sizeof(uint16_t);
constexpr size_t kChunk = 64; // clusters per upload
std::vector<uint16_t> staging;
for (size_t c0 = 0; c0 < nc; c0 += kChunk) {
@@ -975,6 +978,20 @@ void Renderer::uploadFaces(MeshSlot& s, const Mesh& mesh, bool wantUvs) {
staging.data());
}
gl::BindVertexArray(0);
+}
+
+// The index buffer (unless `indicesDone`), the first drawn triangle of each cluster, and the polygroup id and
+// UVs of every drawn triangle in draw order (looked up by that plus gl_PrimitiveID). The wireframe's edges and
+// motion proxies are rebuilt on next need.
+void Renderer::uploadFaces(MeshSlot& s, const Mesh& mesh, bool wantUvs, bool indicesDone) {
+ const size_t nt = mesh.triangleCount();
+ const bool hidden = mesh.hidden.size() == nt && nt > 0;
+ const bool groups = mesh.polygroups.size() == nt && nt > 0;
+ const auto clusters = s.order.clusters();
+ const size_t nc = clusters.size();
+ s.primBase.assign(nc, 0);
+ s.bytes[1] = nt * 3 * sizeof(uint16_t);
+ if (!indicesDone) uploadIndices(s, mesh);
size_t drawn = 0;
for (size_t c = 0; c < nc; ++c) {
s.primBase[c] = uint32_t(drawn);
@@ -1173,8 +1190,36 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, boo
const bool full = s.fresh || mesh.topologyDirty || nv != s.vertexCapacity;
// The GPU keeps the vertices in the slot's cluster layout (core/UploadOrder.h), so a dab re-sends a few
// short ranges holding what it changed, gathered from the mesh arrays.
+ bool indicesDone = false;
if (full) {
- s.order.build(mesh);
+ // The index buffer is written while the layout is made (mapped: the build's threads write each
+ // cluster's drawn triangles straight into it).
+ const size_t nt = mesh.triangleCount();
+ const bool hidden = mesh.hidden.size() == nt && nt > 0;
+ s.drawCount.assign(UploadOrder::clusterCount(nt), 0);
+ gl::BindVertexArray(s.vao);
+ gl::BufferData(GL_ELEMENT_ARRAY_BUFFER, GLsizeiptr(nt * 3 * sizeof(uint16_t)), nullptr, GL_STATIC_DRAW);
+ 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);
+ };
+ s.order.build(mesh, write);
+ indicesDone = indices && gl::UnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
+ gl::BindVertexArray(0);
s.grid = PositionGrid::around(meshBounds(mesh));
dirtyRuns_.assign(1, {0u, uint32_t(s.order.size())});
} else {
@@ -1190,8 +1235,9 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, boo
if (full) {
s.proxyAllocated = false; // sized for the old mesh
s.proxyCount.clear();
- s.closed = closedOriented(mesh);
- uploadFaces(s, mesh, wantUvs);
+ // (closedOriented also accepts unwelded seams, the same point twice, which the layout's check doesn't.)
+ s.closed = s.order.closed() || closedOriented(mesh);
+ uploadFaces(s, mesh, wantUvs, indicesDone);
s.vertexCapacity = nv;
// A mesh too big for the GPU's memory isn't drawn (rather than taking the driver down); the next full
// upload (e.g. after Lower Res or a decimation) tries again.
diff --git a/src/render/Renderer.h b/src/render/Renderer.h
index ff260443..02a99e80 100644
--- a/src/render/Renderer.h
+++ b/src/render/Renderer.h
@@ -183,7 +183,10 @@ private:
void buildProxies(MeshSlot& s, const Mesh& mesh);
bool proxyUsed_ = false; // the last draw() drew motion proxies: sync keeps them current
void syncNormalMap(MeshSlot& s, const Mesh& mesh, const NormalMapView* normalMap);
- void uploadFaces(MeshSlot& s, const Mesh& mesh, bool wantUvs);
+ // `indicesDone`: the index buffer and drawCount are already written (a full upload writes them while
+ // building the layout).
+ void uploadFaces(MeshSlot& s, const Mesh& mesh, bool wantUvs, bool indicesDone = false);
+ void uploadIndices(MeshSlot& s, const Mesh& mesh);
std::vector<std::pair<uint32_t, uint32_t>> dirtyRuns_; // scratch for sync: UploadOrder::dirtyRuns
std::vector<uint8_t> staging_; // scratch for sync: gathered vertex data
void syncTexture(MeshSlot& s, Mesh& mesh);
diff --git a/tests/test_render_clusters.cpp b/tests/test_render_clusters.cpp
index c4e6b259..07d862a4 100644
--- a/tests/test_render_clusters.cpp
+++ b/tests/test_render_clusters.cpp
@@ -191,6 +191,16 @@ TEST(closed_oriented_meshes) {
holed.rebuildTopology();
CHECK(!closedOriented(holed));
CHECK(!closedOriented(Mesh()));
+ // The layout's own check (made while building) agrees, unwelded seams aside.
+ UploadOrder order;
+ for (const Mesh& m : {makeIcosphere(3), makeCube(5), makeBenchSphere(200000)}) {
+ order.build(m);
+ CHECK(order.closed());
+ }
+ for (const Mesh& m : {makePlane(4), flipped, holed}) {
+ order.build(m);
+ CHECK(!order.closed());
+ }
// An unwelded seam (two vertices at one point, each with half the triangles) is still closed; a real
// crack is not.
Mesh seam = makeIcosphere(2);