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.

Motion proxy: while a big scene moves (Fast Navigation), clusters draw a closed vertex-clustered proxy with a fifth of the triangles

islandmedd1c105c7b9d7
diff --git a/src/app/App.cpp b/src/app/App.cpp
index 653c4b44..e0339058 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -582,7 +582,10 @@ int App::run() {
const auto syncStart = std::chrono::steady_clock::now();
syncScene();
notePerformanceFrame(std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - syncStart).count());
- perf_.samplesUsed = renderer_.drawView(camera_, fbW, fbH, renderSettings_, canvasViewport(fbW, fbH), viewSamples());
+ const int samples = viewSamples();
+ renderSettings_.motionProxy = perf_.proxyNow; // big meshes draw their lighter proxy while the view moves
+ perf_.samplesUsed = renderer_.drawView(camera_, fbW, fbH, renderSettings_, canvasViewport(fbW, fbH), samples);
+ renderSettings_.motionProxy = false; // (other renders, e.g. project thumbnails, draw in full)
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window_);
tickProject(dt);
diff --git a/src/app/App.h b/src/app/App.h
index 49846ab8..06f55b6a 100644
--- a/src/app/App.h
+++ b/src/app/App.h
@@ -437,6 +437,7 @@ private:
glm::mat4 lastView{0.0f};
int framesStill = 0; // frames since the view last moved
bool fastNow = false; // this frame draws without antialiasing (fast navigation)
+ bool proxyNow = false; // this frame draws big meshes' motion proxy (fast navigation)
int samplesWanted = 0; // viewSamples() for the last frame
int samplesUsed = 0; // what the renderer managed last frame (fewer when out of memory)
double strokeSyncMs = 0.0; // upload time summed over the open stroke's frames
diff --git a/src/app/Bench.cpp b/src/app/Bench.cpp
index 5104538c..57688b85 100644
--- a/src/app/Bench.cpp
+++ b/src/app/Bench.cpp
@@ -73,13 +73,15 @@ int runAppBenchmark(BenchCommandLine cl) {
gl::Finish();
return msSince(t);
};
- gpu.orbitFrameMs = [&](Mesh& m, int samples) {
+ size_t orbitDrawn = 0; // triangles the last orbit frame drew
+ gpu.orbitFrameMs = [&](Mesh& m, int samples, bool moving) {
int w = 0, h = 0;
SDL_GetWindowSizeInPixels(window, &w, &h);
Camera camera;
camera.aspect = float(w) / float(std::max(h, 1));
camera.frame(m.bounds());
RenderSettings settings;
+ settings.motionProxy = moving;
renderer.syncScene(scene(m));
const int warmup = 5, frames = 120;
Clock::time_point t;
@@ -89,14 +91,18 @@ int runAppBenchmark(BenchCommandLine cl) {
t = Clock::now();
}
SDL_PumpEvents();
+ if (moving) renderer.syncScene(scene(m)); // as the app does every frame (builds the proxy once)
camera.yaw = float(i) * 6.2831853f / float(frames);
renderer.drawView(camera, w, h, settings, {}, samples);
SDL_GL_SwapWindow(window);
}
gl::Finish();
const double ms = msSince(t) / frames;
+ orbitDrawn = renderer.lastDrawnTriangles();
+ settings.motionProxy = false; // the view settles (else the next upload would build a proxy too)
// SF_BENCH_SHOT=prefix: one frame as the window shows it, read back, to check the view draws right.
if (const char* shot = std::getenv("SF_BENCH_SHOT")) {
+ settings.motionProxy = moving;
renderer.drawView(camera, w, h, settings, {}, samples);
Image img;
img.width = w;
@@ -110,12 +116,32 @@ int runAppBenchmark(BenchCommandLine cl) {
img.rgba = std::move(flipped);
for (size_t i = 3; i < img.rgba.size(); i += 4) img.rgba[i] = 255;
std::string err;
- img.savePng(std::string(shot) + "-" + std::to_string(m.triangleCount()) + "-" + std::to_string(samples) + "x.png", err);
+ img.savePng(std::string(shot) + "-" + std::to_string(m.triangleCount()) + "-" + std::to_string(samples) + (moving ? "x-moving" : "x") + ".png", err);
+ settings.motionProxy = false;
}
+ renderer.drawView(camera, w, h, settings, {}, samples);
+ gl::Finish();
return ms;
};
+ gpu.motionSetupMs = [&](Mesh& m) {
+ int w = 0, h = 0;
+ SDL_GetWindowSizeInPixels(window, &w, &h);
+ Camera camera;
+ camera.aspect = float(w) / float(std::max(h, 1));
+ camera.frame(m.bounds());
+ RenderSettings settings;
+ settings.motionProxy = true;
+ m.topologyDirty = true; // everything new, as after opening or dividing
+ renderer.syncScene(scene(m));
+ renderer.drawView(camera, w, h, settings, {}, 1); // the first moving frame asks for the proxy
+ gl::Finish();
+ const auto t = Clock::now();
+ renderer.syncScene(scene(m));
+ gl::Finish();
+ return msSince(t);
+ };
gpu.gpuBytes = [&] { return renderer.gpuBytes(); };
- gpu.drawnTriangles = [&] { return renderer.lastDrawnTriangles(); };
+ gpu.drawnTriangles = [&] { return orbitDrawn; };
code = runBenchCommandLine(cl, "## With the GPU (sculptforge --bench)\n\n", &gpu);
renderer.shutdown();
}
diff --git a/src/app/PanelPerformance.cpp b/src/app/PanelPerformance.cpp
index 0f4e1a31..12609854 100644
--- a/src/app/PanelPerformance.cpp
+++ b/src/app/PanelPerformance.cpp
@@ -102,7 +102,8 @@ int App::viewSamples() {
size_t triangles = 0;
for (int i = 0; i < doc_.subtoolCount(); ++i)
if (doc_.subtools[size_t(i)].visible) triangles += doc_.subtoolMesh(i).triangleCount();
- perf_.fastNow = aa > 1 && uiPrefs_.fastNavigation && moving && triangles >= fastNavigationTriangles();
+ perf_.proxyNow = uiPrefs_.fastNavigation && moving && triangles >= fastNavigationTriangles();
+ perf_.fastNow = aa > 1 && perf_.proxyNow;
perf_.samplesWanted = perf_.fastNow ? 1 : aa;
return perf_.samplesWanted;
}
diff --git a/src/core/Benchmark.cpp b/src/core/Benchmark.cpp
index 49abd2e4..26557afc 100644
--- a/src/core/Benchmark.cpp
+++ b/src/core/Benchmark.cpp
@@ -387,15 +387,27 @@ std::string runBenchmark(const BenchOptions& o, const BenchGpu* gpu) {
}
if (gpu && gpu->orbitFrameMs) {
log(" orbit");
- for (int samples : {4, 1}) {
- const double f = gpu->orbitFrameMs(m, samples);
- md << "| Orbit: frame time, " << (samples == 1 ? "Fast Navigation (no antialiasing)" : "4x antialiasing") << " | "
- << fmt("%.1f", f) << " (" << fmt("%.0f", 1000.0 / std::max(f, 1e-3)) << " fps) |\n";
- }
- if (gpu->drawnTriangles) {
- const double d = double(gpu->drawnTriangles());
- md << "| Orbit: triangles drawn after culling | " << millions(d) << " (" << fmt("%.0f", 100.0 * d / nt) << "%) |\n";
+ if (gpu->motionSetupMs)
+ md << "| Motion proxy build (first move after an upload) | " << fmt("%.0f", gpu->motionSetupMs(m)) << " |\n";
+ struct Orbit {
+ int samples;
+ bool moving;
+ const char* name;
+ };
+ for (const Orbit& o : {Orbit{4, false, "4x antialiasing"}, Orbit{1, false, "no antialiasing"},
+ Orbit{1, true, "Fast Navigation (no antialiasing, motion proxy)"}}) {
+ const double f = gpu->orbitFrameMs(m, o.samples, o.moving);
+ md << "| Orbit: frame time, " << o.name << " | " << fmt("%.1f", f) << " (" << fmt("%.0f", 1000.0 / std::max(f, 1e-3))
+ << " fps) |\n";
+ if (gpu->drawnTriangles && o.samples == 1) {
+ const double d = double(gpu->drawnTriangles());
+ md << "| Orbit: triangles drawn" << (o.moving ? " (motion proxy)" : "") << " | " << millions(d) << " ("
+ << fmt("%.0f", 100.0 * d / nt) << "%) |\n";
+ }
}
+ if (gpu->gpuBytes)
+ md << "| GPU memory with the motion proxy | " << fmt("%.0f MB", double(gpu->gpuBytes()) / 1e6) << " = "
+ << fmt("%.1f", double(gpu->gpuBytes()) / nt) << " B/triangle |\n";
}
md << "\n";
if (o.partial) o.partial(md.str());
diff --git a/src/core/Benchmark.h b/src/core/Benchmark.h
index d520378d..8d8168bd 100644
--- a/src/core/Benchmark.h
+++ b/src/core/Benchmark.h
@@ -25,8 +25,11 @@ struct BenchGpu {
// Uploads what the mesh marked dirty and waits for the GPU; returns milliseconds. First call: everything.
std::function<double(Mesh&)> upload;
// Average milliseconds per frame over a turn around the mesh (already uploaded), drawn with `samples`
- // antialiasing samples as the app's view draws (1 = Fast Navigation while the view moves).
- std::function<double(Mesh&, int samples)> orbitFrameMs;
+ // antialiasing samples as the app's view draws; `moving`: as Fast Navigation draws while the view moves
+ // (the renderer's motion proxy, if it has one; the app also drops to 1 sample).
+ std::function<double(Mesh&, int samples, bool moving)> orbitFrameMs;
+ // Milliseconds to build what moving the view needs first (the motion proxy) for the uploaded mesh (optional).
+ std::function<double(Mesh&)> motionSetupMs;
std::function<size_t()> gpuBytes; // GPU memory the renderer holds for the mesh (optional)
std::function<size_t()> drawnTriangles; // triangles the last orbit frame drew, after culling (optional)
std::string description; // GPU / driver, for the report
diff --git a/src/core/UploadOrder.h b/src/core/UploadOrder.h
index 6c3ea488..bcdc4f01 100644
--- a/src/core/UploadOrder.h
+++ b/src/core/UploadOrder.h
@@ -51,6 +51,8 @@ public:
std::span<const uint32_t> fromGpu() const { return fromGpu_; }
// The first GPU slot holding mesh vertex v (kNone for vertices no triangle uses).
uint32_t toGpu(uint32_t v) const { return toGpu_[v]; }
+ // v has a copy in more than one cluster (it is on a seam between clusters).
+ bool shared(uint32_t v) const { return v < vertices_ && ((hasCopy_[v >> 6] >> (v & 63)) & 1); }
// Every GPU slot holding v, in increasing order (one for most vertices).
void slotsOf(uint32_t v, std::vector<uint32_t>& out) const;
diff --git a/src/render/Clusters.cpp b/src/render/Clusters.cpp
index 6297b4f0..dc4556c1 100644
--- a/src/render/Clusters.cpp
+++ b/src/render/Clusters.cpp
@@ -10,6 +10,7 @@
#include <cmath>
#include <cstring>
#include <map>
+#include <unordered_map>
#include <tuple>
#include <vector>
@@ -159,6 +160,100 @@ ClusterBounds clusterBounds(const Mesh& mesh, const UploadOrder& order, uint32_t
return b;
}
+bool clusterProxy(const Mesh& mesh, const UploadOrder& order, uint32_t c, size_t maxTriangles, std::vector<uint16_t>& out,
+ ClusterBounds& bounds) {
+ out.clear();
+ const uint32_t n = order.clusterTriangles(c);
+ const UploadOrder::Cluster& cl = order.clusters()[c];
+ const auto fromGpu = order.fromGpu();
+ thread_local std::vector<uint16_t> local, rep;
+ thread_local std::vector<uint8_t> pinned;
+ local.resize(size_t(n) * 3);
+ order.localTriangles(mesh, c, local.data());
+ const uint32_t nl = cl.vertexCount;
+ pinned.assign(nl, 0);
+ AABB box;
+ for (uint32_t l = 0; l < nl; ++l) {
+ const uint32_t v = fromGpu[cl.firstVertex + l];
+ pinned[l] = order.shared(v);
+ box.expand(mesh.positions[v]);
+ }
+ auto pos = [&](uint32_t l) { return mesh.positions[fromGpu[cl.firstVertex + l]]; };
+ const bool hidden = mesh.hidden.size() == mesh.triangleCount();
+ double edges = 0.0;
+ size_t counted = 0;
+ for (uint32_t i = 0; i < n; i += 7) { // a sample is plenty
+ const uint16_t* t = local.data() + size_t(i) * 3;
+ edges += glm::distance(pos(t[0]), pos(t[1])) + glm::distance(pos(t[1]), pos(t[2]));
+ counted += 2;
+ }
+ 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)
+ 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);
+ };
+ 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 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;
+ }
+ out.clear();
+ for (uint32_t i = 0; i < n; ++i) {
+ if (hidden && mesh.hidden[order.triangle(c, i)]) continue;
+ const uint16_t* t = local.data() + size_t(i) * 3;
+ const uint16_t a = rep[t[0]], b = rep[t[1]], d = rep[t[2]];
+ if (a == b || b == d || a == d) continue;
+ out.insert(out.end(), {a, b, d});
+ }
+ if (out.size() / 3 <= maxTriangles) break;
+ }
+ if (out.size() / 3 > maxTriangles) {
+ out.clear();
+ return false;
+ }
+ // The proxy's own normal cone (its triangles lean differently from the cluster's).
+ glm::vec3 sum(0.0f);
+ float minDot = 1.0f;
+ bool faces = false;
+ thread_local std::vector<glm::vec3> normals;
+ normals.resize(out.size() / 3);
+ for (size_t i = 0; i < normals.size(); ++i) {
+ const glm::vec3 p0 = pos(out[i * 3]);
+ const glm::vec3 cr = glm::cross(pos(out[i * 3 + 1]) - p0, pos(out[i * 3 + 2]) - p0);
+ const float l2 = glm::dot(cr, cr);
+ normals[i] = l2 > 0.0f && std::isfinite(l2) ? cr / std::sqrt(l2) : glm::vec3(0.0f);
+ sum += normals[i];
+ }
+ const float sl = glm::length(sum);
+ for (const glm::vec3& nn : normals)
+ if (nn != glm::vec3(0.0f) && sl > 1e-6f) {
+ minDot = std::min(minDot, glm::dot(nn, sum / sl));
+ faces = true;
+ }
+ bounds.axis = glm::vec3(0.0f, 0.0f, 1.0f);
+ bounds.coneAngle = -1.0f;
+ if (faces && minDot > 0.0f) {
+ bounds.axis = sum / sl;
+ bounds.coneAngle = std::acos(std::min(minDot, 1.0f));
+ }
+ return true;
+}
+
bool closedOriented(const Mesh& mesh) {
const size_t nv = mesh.vertexCount(), nt = mesh.triangleCount();
if (nt == 0) return false;
diff --git a/src/render/Clusters.h b/src/render/Clusters.h
index 72e43572..07187dc2 100644
--- a/src/render/Clusters.h
+++ b/src/render/Clusters.h
@@ -59,6 +59,16 @@ struct ClusterBounds {
};
ClusterBounds clusterBounds(const Mesh& mesh, const UploadOrder& order, uint32_t cluster);
+// The motion proxy of cluster c (drawn while the view moves): its triangles with the vertices snapped to one
+// per cell of a grid a few edges wide (the vertex nearest the cell's centre), the degenerate ones dropped, as
+// indices into the cluster's vertex range like localTriangles. Vertices on seams between clusters stay, so
+// the proxies of neighbouring clusters still meet: the proxy of a closed mesh is closed (a simplicial map of
+// it). `hidden` triangles are left out. At most maxTriangles (a coarser grid until it fits); false (and
+// nothing) when it doesn't fit even then. `bounds` gets the proxy's normal cone (centre and radius as the
+// cluster's, whose vertices it uses).
+bool clusterProxy(const Mesh& mesh, const UploadOrder& order, uint32_t c, size_t maxTriangles, std::vector<uint16_t>& out,
+ ClusterBounds& bounds);
+
// Every directed edge a->b has as many b->a: the mesh is the closed, consistently wound boundary of a volume
// (it may intersect itself). Then from outside, every back face lies behind a front face.
bool closedOriented(const Mesh& mesh);
diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp
index a3396c98..f4719dde 100644
--- a/src/render/Renderer.cpp
+++ b/src/render/Renderer.cpp
@@ -162,6 +162,8 @@ uniform int uMatcap; // 1: shade with the matcap texture instead of the
uniform sampler2D uMatcapTex; // sRGB-encoded lit sphere (core/Matcap.h)
uniform vec2 uMatcapCavity; // crease darkening, ridge lift (sf::matcapCavity)
uniform int uDim; // 1: an inactive SubTool, shown a little darker and greyer
+uniform int uTwoSided; // 1: back faces shade with the normal turned towards the viewer; 0: the motion proxy,
+ // whose folds face backwards on the outside of the surface
out vec4 fragColor;
vec4 finish(vec3 c) {
if (uDim == 1) c = mix(c, vec3(dot(c, vec3(0.2126, 0.7152, 0.0722))), 0.25) * 0.72;
@@ -204,7 +206,7 @@ void main() {
vec3 B = (vTangent.w < 0.0 ? -1.0 : 1.0) * cross(n, T);
n = normalize(tn.x * T + tn.y * B + tn.z * n);
#endif
- if (!gl_FrontFacing) n = -n;
+ if (!gl_FrontFacing && uTwoSided == 1) n = -n;
vec2 ndc = (gl_FragCoord.xy - uViewport.xy) / uViewport.zw * 2.0 - 1.0;
vec3 v = normalize(vec3(-ndc / uProjScale, 1.0));
if (uMatcap == 1) {
@@ -501,6 +503,7 @@ const Renderer::MeshProgram* Renderer::meshProgram(int features) {
p.primBase = at("uPrimBase");
p.hasPaint = at("uHasPaint");
p.normalFlipY = at("uNormalFlipY");
+ p.twoSided = at("uTwoSided");
// Texture units: groups 1, matcap 2, UVs 3, painted texture 4, normal map 5, tangents 6, cluster table 7.
gl::UseProgram(p.id);
gl::Uniform1i(at("uGroupIds"), 1);
@@ -578,7 +581,9 @@ Renderer::MeshSlot& Renderer::slot(uint32_t id) {
gl::GenTextures(1, &s.primTexture);
gl::GenBuffers(1, &s.uvData);
gl::GenTextures(1, &s.uvTexture);
- for (GLuint vao : {s.vao, s.lineVao}) {
+ gl::GenVertexArrays(1, &s.proxyVao);
+ gl::GenBuffers(1, &s.proxyBuffer);
+ for (GLuint vao : {s.vao, s.lineVao, s.proxyVao}) {
gl::BindVertexArray(vao);
gl::BindBuffer(GL_ARRAY_BUFFER, s.vertexBuffer);
gl::EnableVertexAttribArray(0);
@@ -594,7 +599,7 @@ Renderer::MeshSlot& Renderer::slot(uint32_t id) {
gl::VertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, 4, nullptr);
gl::BindBuffer(GL_ARRAY_BUFFER, s.maskBuffer);
gl::VertexAttribPointer(3, 1, GL_UNSIGNED_BYTE, GL_TRUE, 1, nullptr);
- gl::BindBuffer(GL_ELEMENT_ARRAY_BUFFER, s.indexBuffer);
+ gl::BindBuffer(GL_ELEMENT_ARRAY_BUFFER, vao == s.proxyVao ? s.proxyBuffer : s.indexBuffer);
}
gl::BindVertexArray(0);
gl::BindBuffer(GL_ARRAY_BUFFER, 0);
@@ -602,7 +607,8 @@ Renderer::MeshSlot& Renderer::slot(uint32_t id) {
}
void Renderer::freeSlot(MeshSlot& s) {
- for (GLuint* b : {&s.vertexBuffer, &s.indexBuffer, &s.lineBuffer, &s.colorBuffer, &s.maskBuffer, &s.groupData, &s.primData, &s.uvData})
+ for (GLuint* b : {&s.vertexBuffer, &s.indexBuffer, &s.lineBuffer, &s.proxyBuffer, &s.colorBuffer, &s.maskBuffer, &s.groupData,
+ &s.primData, &s.uvData})
gl::DeleteBuffers(1, b);
gl::DeleteTextures(1, &s.groupTexture);
gl::DeleteTextures(1, &s.primTexture);
@@ -613,6 +619,7 @@ void Renderer::freeSlot(MeshSlot& s) {
if (s.tangentTexture) gl::DeleteTextures(1, &s.tangentTexture);
gl::DeleteVertexArrays(1, &s.vao);
gl::DeleteVertexArrays(1, &s.lineVao);
+ gl::DeleteVertexArrays(1, &s.proxyVao);
s = MeshSlot();
}
@@ -620,6 +627,7 @@ size_t Renderer::gpuBytes() const {
size_t total = 0;
for (const MeshSlot& s : slots_) {
for (size_t b : s.bytes) total += b;
+ if (s.proxyAllocated) total += (s.order.triangleCount() / 4) * 3 * sizeof(uint16_t);
if (s.paintTexture) total += size_t(s.textureW) * size_t(s.textureH) * 4 * 4 / 3; // with mipmaps
}
return total;
@@ -903,6 +911,8 @@ void Renderer::updateBounds(MeshSlot& s, const Mesh& mesh, bool all) {
if (cullScratch_.empty() || c > cullScratch_.back()) cullScratch_.push_back(c);
}
}
+ s.proxyStale.resize(clusters.size(), 1);
+ for (uint32_t c : cullScratch_) s.proxyStale[c] = 1;
poolFor(cullScratch_.size(), [&](size_t i0, size_t i1) {
for (size_t i = i0; i < i1; ++i) {
const uint32_t c = cullScratch_[i];
@@ -1053,6 +1063,7 @@ void Renderer::uploadFaces(MeshSlot& s, const Mesh& mesh, bool wantUvs) {
}
s.hasUvs = textured;
s.linesValid = false;
+ s.proxyStale.assign(nc, 1);
if (textured) s.drawnOrder = std::move(drawnTris); // the normal map's tangents follow it
else std::vector<uint32_t>().swap(s.drawnOrder);
if (visibleUvs_.capacity() > (size_t(1) << 16)) std::vector<glm::vec2>().swap(visibleUvs_);
@@ -1115,6 +1126,44 @@ void Renderer::buildLines(MeshSlot& s, const Mesh& mesh) {
s.linesValid = true;
}
+// The motion proxies of the clusters that changed since they were built (all of them at first), each in its
+// own part of the proxy buffer.
+void Renderer::buildProxies(MeshSlot& s, const Mesh& mesh) {
+ const size_t nc = s.order.clusters().size();
+ s.proxyStale.resize(nc, 1);
+ s.proxyCount.resize(nc, 0);
+ s.proxyBounds.resize(nc);
+ cullScratch_.clear();
+ for (uint32_t c = 0; c < nc; ++c)
+ if (s.proxyStale[c]) cullScratch_.push_back(c);
+ if (cullScratch_.empty()) return;
+ if (!s.proxyAllocated) {
+ gl::BindVertexArray(s.proxyVao);
+ gl::BufferData(GL_ELEMENT_ARRAY_BUFFER, GLsizeiptr((s.order.triangleCount() / 4) * 3 * sizeof(uint16_t)), nullptr, GL_STATIC_DRAW);
+ gl::BindVertexArray(0);
+ s.proxyAllocated = true;
+ }
+ std::vector<std::vector<uint16_t>> built(cullScratch_.size());
+ poolFor(cullScratch_.size(), [&](size_t i0, size_t i1) {
+ for (size_t i = i0; i < i1; ++i) {
+ const uint32_t c = cullScratch_[i];
+ ClusterBounds b = s.bounds[c];
+ if (!clusterProxy(mesh, s.order, c, s.order.clusterTriangles(c) / 4, built[i], b)) built[i].clear();
+ s.proxyBounds[c] = b;
+ }
+ }, 1, 2);
+ gl::BindVertexArray(s.proxyVao);
+ for (size_t i = 0; i < cullScratch_.size(); ++i) {
+ const uint32_t c = cullScratch_[i];
+ s.proxyCount[c] = uint32_t(built[i].size());
+ s.proxyStale[c] = 0;
+ if (!built[i].empty())
+ gl::BufferSubData(GL_ELEMENT_ARRAY_BUFFER, GLintptr(size_t(s.order.firstTriangle(c) / 4) * 3 * sizeof(uint16_t)),
+ GLsizeiptr(built[i].size() * sizeof(uint16_t)), built[i].data());
+ }
+ gl::BindVertexArray(0);
+}
+
void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, bool wireframe) {
const size_t nv = mesh.vertexCount();
if (normalMap && (!normalMap->rgb || !normalMap->tangents || normalMap->tangents->size() != mesh.uvs.size() ||
@@ -1139,6 +1188,8 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, boo
updateBounds(s, mesh, full);
}
if (full) {
+ s.proxyAllocated = false; // sized for the old mesh
+ s.proxyCount.clear();
s.closed = closedOriented(mesh);
uploadFaces(s, mesh, wantUvs);
s.vertexCapacity = nv;
@@ -1163,6 +1214,7 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, boo
}
if (!full && (mesh.facesDirty || wantUvs != s.hasUvs)) uploadFaces(s, mesh, wantUvs);
if (wireframe && !s.linesValid && !s.outOfMemory) buildLines(s, mesh);
+ if (proxyUsed_ && !s.outOfMemory) buildProxies(s, mesh);
syncTexture(s, mesh);
syncNormalMap(s, mesh, normalMap);
@@ -1180,9 +1232,11 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, boo
gl::BufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
}
if (present != uploaded) {
- gl::BindVertexArray(s.vao);
- if (present) gl::EnableVertexAttribArray(location);
- else gl::DisableVertexAttribArray(location);
+ for (GLuint vao : {s.vao, s.proxyVao}) {
+ gl::BindVertexArray(vao);
+ if (present) gl::EnableVertexAttribArray(location);
+ else gl::DisableVertexAttribArray(location);
+ }
gl::BindVertexArray(0);
}
uploaded = present;
@@ -1495,10 +1549,8 @@ void Renderer::draw(const Camera& camera, int targetWidth, int targetHeight, con
gl::Enable(GL_POLYGON_OFFSET_FILL);
gl::PolygonOffset(1.0f, 1.0f);
for (const DrawItem& item : drawList_)
- if (const MeshSlot* s = findSlot(item.id); s && s->drawnTriangles > 0) {
- cull(*s, frameCull_);
- drawSlot(*s, item.active, settings);
- }
+ if (const MeshSlot* s = findSlot(item.id); s && s->drawnTriangles > 0) drawSlot(*s, item.active, settings);
+ proxyUsed_ = settings.motionProxy;
gl::Disable(GL_POLYGON_OFFSET_FILL);
for (GLenum unit : {kTexture7, kTexture6, GL_TEXTURE3, GL_TEXTURE1}) {
gl::ActiveTexture(unit);
@@ -1526,12 +1578,12 @@ void Renderer::draw(const Camera& camera, int targetWidth, int targetHeight, con
if (s->linesValid) {
gl::Uniform1f(gl::GetUniformLocation(wireProgram_, "uAlpha"), 0.5775f); // 1 - (1 - 0.35)^2
gl::BindVertexArray(s->lineVao);
- drawClusters(*s, GL_LINES, true, -1);
+ drawClusters(*s, drawClusters_, GL_LINES, Part::Lines, -1);
} else { // edges not built (a mesh not synced for the wireframe): outline the triangles
gl::Uniform1f(gl::GetUniformLocation(wireProgram_, "uAlpha"), 0.35f);
gl::PolygonMode(GL_FRONT_AND_BACK, GL_LINE);
gl::BindVertexArray(s->vao);
- drawClusters(*s, GL_TRIANGLES, false, -1);
+ drawClusters(*s, drawClusters_, GL_TRIANGLES, Part::Triangles, -1);
gl::PolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
}
@@ -1546,8 +1598,10 @@ void Renderer::draw(const Camera& camera, int targetWidth, int targetHeight, con
// The clusters of `s` inside the view, less those facing away when that can't open a hole: the mesh is closed
// and wound consistently (closedOriented) with nothing hidden, the eye is outside its bounds, and no cluster
// reaches into the near plane's slab (a cut-open front would show the back faces behind it).
-void Renderer::cull(const MeshSlot& s, const CullView& frame) {
+void Renderer::cull(const MeshSlot& s, const CullView& frame, bool proxy) {
drawClusters_.clear();
+ proxyClusters_.clear();
+ proxy = proxy && s.proxyCount.size() == s.drawCount.size() && s.proxyBounds.size() == s.drawCount.size();
CullView v = frame;
const glm::vec3 e = v.eye;
v.backfaces = s.closed && !s.anyHidden && s.box.valid() &&
@@ -1567,38 +1621,53 @@ void Renderer::cull(const MeshSlot& s, const CullView& frame) {
for (uint32_t c = 0; c < nc; ++c) {
if (s.drawCount[c] == 0) continue;
const ClusterBounds& b = s.bounds[c];
- if (!insideFrustum(v, b) || facingAway(v, b)) continue;
- drawClusters_.push_back(c);
+ if (!insideFrustum(v, b)) continue;
+ if (proxy && s.proxyCount[c] > 0 && !s.proxyStale[c]) {
+ if (!facingAway(v, s.proxyBounds[c])) proxyClusters_.push_back(c);
+ } else if (!facingAway(v, b)) {
+ drawClusters_.push_back(c);
+ }
}
}
-// Draws drawClusters_ of `s` (its VAO bound): triangles, or the wireframe's edges with `lines`. One multi-draw,
-// or with per-triangle data on a driver whose gl_VertexID lacks the base vertex, one draw per cluster with its
-// first drawn triangle in `primBaseLocation`.
-void Renderer::drawClusters(const MeshSlot& s, GLenum mode, bool lines, GLint primBaseLocation) {
+// Draws `clusters` of `s` (the VAO for `part` bound): their triangles, the wireframe's edges, or their motion
+// proxies. One multi-draw, or with per-triangle data on a driver whose gl_VertexID lacks the base vertex, one
+// draw per cluster with its first drawn triangle in `primBaseLocation`.
+void Renderer::drawClusters(const MeshSlot& s, const std::vector<uint32_t>& clusters, GLenum mode, Part part, GLint primBaseLocation) {
drawCounts_.clear();
drawOffsets_.clear();
drawBases_.clear();
- const auto clusters = s.order.clusters();
+ const auto layout = s.order.clusters();
size_t indices = 0;
- for (uint32_t c : drawClusters_) {
- const GLsizei count = lines ? GLsizei(s.lineCount[c]) : s.drawCount[c];
- if (count == 0) continue;
- const size_t first = lines ? size_t(s.lineFirst[c]) : size_t(s.order.firstTriangle(c)) * 3;
+ for (uint32_t c : clusters) {
+ GLsizei count = 0;
+ size_t first = 0;
+ switch (part) {
+ case Part::Triangles:
+ count = s.drawCount[c];
+ first = size_t(s.order.firstTriangle(c)) * 3;
+ break;
+ case Part::Lines:
+ count = GLsizei(s.lineCount[c]);
+ first = s.lineFirst[c];
+ break;
+ case Part::Proxy:
+ count = GLsizei(s.proxyCount[c]);
+ first = size_t(s.order.firstTriangle(c) / 4) * 3;
+ break;
+ }
drawCounts_.push_back(count);
drawOffsets_.push_back(reinterpret_cast<const void*>(first * sizeof(uint16_t)));
- drawBases_.push_back(GLint(clusters[c].firstVertex));
+ drawBases_.push_back(GLint(layout[c].firstVertex));
indices += size_t(count);
}
if (drawCounts_.empty()) return;
- if (!lines) lastDrawn_ += indices / 3;
+ if (part != Part::Lines) lastDrawn_ += indices / 3;
if (primBaseLocation >= 0 && !vertexIdHasBase_) {
- size_t i = 0;
- for (uint32_t c : drawClusters_) {
- if (s.drawCount[c] == 0) continue;
- gl::Uniform1i(primBaseLocation, GLint(s.primBase[c]));
+ for (size_t i = 0; i < clusters.size(); ++i) {
+ if (drawCounts_[i] == 0) continue;
+ gl::Uniform1i(primBaseLocation, GLint(s.primBase[clusters[i]]));
gl::DrawElementsBaseVertex(mode, drawCounts_[i], GL_UNSIGNED_SHORT, drawOffsets_[i], drawBases_[i]);
- ++i;
}
return;
}
@@ -1652,13 +1721,25 @@ void Renderer::drawSlot(const MeshSlot& s, bool active, const RenderSettings& se
gl::Uniform1i(p->showMask, active && settings.showMask && s.hasMask ? 1 : 0);
gl::Uniform1i(p->groups, groups);
gl::Uniform1i(p->dim, !active && settings.dimInactive ? 1 : 0);
+ gl::Uniform1i(p->twoSided, 1);
gl::BindVertexArray(s.vao);
// Constant values for the attribute arrays that are disabled.
if (!s.hasColors) gl::VertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);
if (!s.hasMask) gl::VertexAttrib1f(3, 0.0f);
+ // While the view moves, the motion proxy where it is built (not with per-triangle data, which follows the
+ // full triangles).
+ const bool perTriangle = features & (kPrim | kTextured | kNormalMap);
+ cull(s, frameCull_, settings.motionProxy && !perTriangle);
// The back faces left in clusters along the silhouette: the GPU skips them too when they can't show.
if (cullBackfaces_) gl::Enable(GL_CULL_FACE);
- drawClusters(s, GL_TRIANGLES, false, (features & (kPrim | kTextured | kNormalMap)) ? p->primBase : -1);
+ drawClusters(s, drawClusters_, GL_TRIANGLES, Part::Triangles, perTriangle ? p->primBase : -1);
+ if (!proxyClusters_.empty()) {
+ gl::BindVertexArray(s.proxyVao);
+ if (!s.hasColors) gl::VertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);
+ if (!s.hasMask) gl::VertexAttrib1f(3, 0.0f);
+ gl::Uniform1i(p->twoSided, 0);
+ drawClusters(s, proxyClusters_, GL_TRIANGLES, Part::Proxy, -1);
+ }
gl::Disable(GL_CULL_FACE);
}
diff --git a/src/render/Renderer.h b/src/render/Renderer.h
index 8c0eed3b..ff260443 100644
--- a/src/render/Renderer.h
+++ b/src/render/Renderer.h
@@ -27,6 +27,7 @@ struct RenderSettings {
bool showSplats = true;
float splatOpacity = 1.0f; // global fade for using a capture as reference
bool dimInactive = true; // SubTools other than the active one render slightly darker (ZBrush style)
+ bool motionProxy = false; // the view is moving (Fast Navigation): draw big meshes' lighter proxy (render/Clusters.h)
};
// A baked tangent-space normal map to preview on a mesh (core/Bake.h): RGB8 rows bottom-up, with the
@@ -162,6 +163,12 @@ private:
std::vector<uint32_t> primBase; // per cluster: its first drawn triangle
std::vector<uint32_t> lineFirst, lineCount; // per cluster: its edges in lineBuffer (in indices)
bool linesValid = false;
+ // Motion proxy (clusterProxy): per cluster up to a quarter of its triangles, at (firstTriangle / 4) * 3.
+ GLuint proxyVao = 0, proxyBuffer = 0;
+ std::vector<uint32_t> proxyCount; // indices; 0: none (the cluster draws in full)
+ std::vector<uint8_t> proxyStale; // to rebuild before use
+ std::vector<ClusterBounds> proxyBounds; // the proxy's normal cone
+ bool proxyAllocated = false;
std::vector<uint32_t> drawnOrder; // the mesh triangle of each drawn triangle (meshes with UVs only)
size_t drawnTriangles = 0;
size_t vertexCapacity = 0;
@@ -173,15 +180,20 @@ private:
void uploadVertices(MeshSlot& s, const Mesh& mesh, bool full);
void updateBounds(MeshSlot& s, const Mesh& mesh, bool all);
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
void syncNormalMap(MeshSlot& s, const Mesh& mesh, const NormalMapView* normalMap);
void uploadFaces(MeshSlot& s, const Mesh& mesh, bool wantUvs);
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);
- // The clusters of `s` to draw this frame (into drawCounts_ / drawOffsets_ / drawBases_ / drawClusters_).
- void cull(const MeshSlot& s, const CullView& view);
+ // The clusters of `s` to draw this frame: in full (drawClusters_), and with `proxy` as their motion proxy
+ // where it is built (proxyClusters_).
+ void cull(const MeshSlot& s, const CullView& view, bool proxy = false);
void drawSlot(const MeshSlot& s, bool active, const RenderSettings& settings);
- void drawClusters(const MeshSlot& s, GLenum mode, bool lines, GLint primBaseLocation);
+ enum class Part { Triangles, Lines, Proxy };
+ void drawClusters(const MeshSlot& s, const std::vector<uint32_t>& clusters, GLenum mode, Part part, GLint primBaseLocation);
+ std::vector<uint32_t> proxyClusters_;
std::vector<GLsizei> drawCounts_;
std::vector<const void*> drawOffsets_;
std::vector<GLint> drawBases_;
@@ -198,7 +210,7 @@ private:
GLuint id = 0;
GLint view = -1, proj = -1, gridOrigin = -1, gridStep = -1, viewport = -1, projScale = -1, baseColor = -1, paint = -1,
showMask = -1, groups = -1, matcap = -1, matcapCavity = -1, dim = -1, primBase = -1, hasPaint = -1,
- normalFlipY = -1;
+ normalFlipY = -1, twoSided = -1;
};
static constexpr int kPaint = 1, kPrim = 2, kTextured = 4, kNormalMap = 8;
MeshProgram programs_[16];
diff --git a/tests/test_render_clusters.cpp b/tests/test_render_clusters.cpp
index dcaff6f6..c4e6b259 100644
--- a/tests/test_render_clusters.cpp
+++ b/tests/test_render_clusters.cpp
@@ -15,6 +15,7 @@
#include <glm/gtc/matrix_transform.hpp>
#include <cmath>
+#include <map>
#include <random>
using namespace sf;
@@ -284,3 +285,51 @@ TEST(cluster_frustum_culling) {
b.center = glm::vec3(0.0f);
CHECK(!insideFrustum(cv, b));
}
+
+TEST(motion_proxy_is_closed_and_light) {
+ // The proxies of all clusters of a closed mesh, mapped back to mesh vertices, make a closed surface (no
+ // holes while the view moves), each at most a quarter of its cluster.
+ Mesh m = makeBenchSphere(300000);
+ for (size_t v = 0; v < m.vertexCount(); ++v) { // some sculpted detail
+ const glm::vec3 p = m.positions[v];
+ m.positions[v] = p * (1.0f + 0.02f * std::sin(p.x * 31.0f) * std::sin(p.z * 29.0f));
+ }
+ m.recomputeAllNormals();
+ UploadOrder order;
+ order.build(m);
+ std::map<std::pair<uint32_t, uint32_t>, int> edges; // directed edge -> count, minus the reverse's
+ std::vector<uint16_t> proxy;
+ size_t total = 0;
+ bool light = true, built = true, inRange = true;
+ for (uint32_t c = 0; c < order.clusters().size(); ++c) {
+ ClusterBounds b = clusterBounds(m, order, c);
+ const size_t cap = order.clusterTriangles(c) / 4;
+ built = built && clusterProxy(m, order, c, cap, proxy, b);
+ light = light && proxy.size() / 3 <= cap && proxy.size() % 3 == 0;
+ total += proxy.size() / 3;
+ const auto& cl = order.clusters()[c];
+ for (size_t i = 0; i < proxy.size(); i += 3)
+ for (int k = 0; k < 3; ++k) {
+ inRange = inRange && proxy[i + size_t(k)] < cl.vertexCount;
+ const uint32_t a = order.fromGpu()[cl.firstVertex + proxy[i + size_t(k)]];
+ const uint32_t e = order.fromGpu()[cl.firstVertex + proxy[i + size_t((k + 1) % 3)]];
+ ++edges[{a, e}];
+ --edges[{e, a}];
+ }
+ }
+ bool closed = true;
+ for (const auto& [e, n] : edges) closed = closed && n == 0;
+ std::printf(" motion proxy: %zu of %zu triangles\n", total, m.triangleCount());
+ CHECK(built);
+ CHECK(light);
+ CHECK(inRange);
+ CHECK(closed);
+ CHECK(total * 4 < m.triangleCount());
+ // Hidden triangles stay out of the proxy too.
+ Mesh h = makeBenchSphere(20000);
+ h.ensureHidden();
+ for (size_t t = 0; t < h.triangleCount(); ++t) h.hidden[t] = 1;
+ order.build(h);
+ ClusterBounds b;
+ CHECK(clusterProxy(h, order, 0, order.clusterTriangles(0), proxy, b) && proxy.empty());
+}