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.

Coarse LOD: a slow frame no longer leaves a dense mesh on its coarse level at rest (#151)

islandmed6f3ac877f4d4
diff --git a/changes/fix-paint-layer-top-subd.md b/changes/fix-paint-layer-top-subd.md
new file mode 100644
index 00000000..4caf61ab
--- /dev/null
+++ b/changes/fix-paint-layer-top-subd.md
@@ -0,0 +1,16 @@
+---
+whats_new: **Full detail after a new paint layer**: on dense meshes, adding a paint layer no longer leaves the top
+ subdivision level looking unsubdivided until a restart.
+fixed: Adding a paint layer on a dense top subdivision level (4M+ triangles) could leave the model drawn at its coarse
+ preview level at rest, looking like level 1, through level changes and project reloads until a restart (#151, thanks
+ @fxrtst). The GPU time used to decide whether full detail fits a still frame no longer counts the CPU work of the
+ frame that added the layer, and one slow frame can no longer tip it.
+testers:
+ - Windows: a 4M+ triangle mesh with 3 subdivision levels, on the top level open Tool > Paint Layers > New. Let the
+ view rest: full detail (wrinkles sharp), not the blurred preview; the status bar doesn't say "Preview detail".
+ - Lower Level, Higher Level, and reopen the project from the Library: full detail at rest each time.
+---
+Cause: the frame governor's GPU timer brackets the whole frame, so the ~400 ms the CPU spent on the add (paint stack,
+colour upload, every cluster's coarse levels) counted as 3D-view GPU time over the 4.28M triangles drawn in full that
+frame. The per-triangle estimate (renderer-wide, so it outlived reloads) then put a full still frame over its 100 ms
+budget ("Preview detail"), and coarse frames never measure the full mesh to bring it down again.
diff --git a/src/app/App.cpp b/src/app/App.cpp
index d07cbc89..9937f90a 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -1198,6 +1198,7 @@ int App::run() {
renderSettings_.resolutionScale = float(uiPrefs_.viewResolution) / 100.0f; // (#80; only the window's view)
if (perf_.movingNow) // while the view moves, the floor tier draws fewer pixels; full resolution when it stops
renderSettings_.resolutionScale = std::min(renderSettings_.resolutionScale, motionResolution());
+ hardwareProfileDrawStart();
perf_.samplesUsed = renderer_.drawView(camera_, fbW, fbH, renderSettings_, canvasViewport(fbW, fbH), samples);
if (snapshotEnv && *snapshotEnv && (sm.moving || sm.releasing)) {
// Three points of the stroke, and the end (the frame the last dab reached the screen).
diff --git a/src/app/App.h b/src/app/App.h
index f4c643c6..61836279 100644
--- a/src/app/App.h
+++ b/src/app/App.h
@@ -1175,6 +1175,7 @@ private:
void initHardwareProfile();
void shutdownHardwareProfile();
void hardwareProfileFrameBegin();
+ void hardwareProfileDrawStart(); // the 3D view's draw begins (the CPU's time before it isn't the triangles')
void hardwareProfileFrameEnd(); // before the swap: feeds the governor, runs a calibration step
void drawHardwareProfileOverlay(); // calibration progress over the first frames (Esc skips)
bool hardwareCalibrating() const; // a calibration is running (proof mode waits for it)
diff --git a/src/app/PanelPerformanceProfile.cpp b/src/app/PanelPerformanceProfile.cpp
index 73add9f8..70aec96b 100644
--- a/src/app/PanelPerformanceProfile.cpp
+++ b/src/app/PanelPerformanceProfile.cpp
@@ -47,10 +47,11 @@ struct HwProfileUi {
FrameGovernor governor;
GpuFrameTimer timer;
uint64_t frame = 0;
- double frameStartMs = 0.0;
+ double frameStartMs = 0.0, drawStartMs = -1.0; // (drawStartMs: the 3D view's draw began; -1: not this frame)
struct Pending {
uint64_t frame;
FrameSample sample;
+ double cpuBeforeDrawMs; // the GPU waited through it inside the frame's timer query
};
std::deque<Pending> pending; // frames waiting for their GPU time
uint64_t revision = 0; // performanceSettingsRevision() when last applied
@@ -240,9 +241,14 @@ void App::hardwareProfileFrameBegin() {
activeUntil_ = std::max(activeUntil_, double(SDL_GetTicksNS()) * 1e-9 + 0.3); // keep the frames coming
}
hw.frameStartMs = nowMs();
+ hw.drawStartMs = -1.0;
if (!hw.calibrator) hw.timer.begin();
}
+void App::hardwareProfileDrawStart() {
+ if (hwProfile_) hwProfile_->drawStartMs = nowMs();
+}
+
void App::hardwareProfileFrameEnd() {
if (!hwProfile_) return;
HwProfileUi& hw = *hwProfile_;
@@ -258,17 +264,18 @@ void App::hardwareProfileFrameEnd() {
hw.lastCpuMs = sample.cpuMs;
bool changed = false;
if (hw.timer.available()) {
- hw.pending.push_back({frame, sample});
+ hw.pending.push_back({frame, sample, hw.drawStartMs >= 0.0 ? hw.drawStartMs - hw.frameStartMs : sample.cpuMs});
uint64_t doneFrame = 0;
double gpuMs = 0.0;
while (hw.timer.poll(doneFrame, gpuMs)) {
while (!hw.pending.empty() && hw.pending.front().frame < doneFrame) hw.pending.pop_front(); // lost
if (!hw.pending.empty() && hw.pending.front().frame == doneFrame) {
FrameSample s = hw.pending.front().sample;
+ const double cpuBeforeDrawMs = hw.pending.front().cpuBeforeDrawMs;
hw.pending.pop_front();
s.gpuMs = gpuMs;
hw.lastGpuMs = gpuMs;
- renderer_.noteGpuFrame(gpuMs, s.drawnTriangles);
+ renderer_.noteGpuFrame(gpuMs, s.drawnTriangles, cpuBeforeDrawMs);
if (proof_ && proofGpuMs_.size() < 4096) proofGpuMs_.push_back(gpuMs);
changed = hw.governor.addFrame(s) || changed;
}
diff --git a/src/render/Clusters.cpp b/src/render/Clusters.cpp
index 41959460..d0e6f9af 100644
--- a/src/render/Clusters.cpp
+++ b/src/render/Clusters.cpp
@@ -576,6 +576,18 @@ bool coarseAttributesMatch(const CoarseLevels& L, const Mesh& mesh) {
return (nv > 0 && mesh.colors.size() == nv) == !L.colors.empty() && (nv > 0 && mesh.mask.size() == nv) == !L.mask.empty();
}
+double nextGpuMsPerTriangle(double estimate, double gpuMs, double cpuBeforeDrawMs, uint64_t triangles) {
+ const double drawMs = gpuMs - std::max(cpuBeforeDrawMs, 0.0);
+ if (!(drawMs > 0.2) || triangles < 500000) return estimate; // (small frames are mostly fixed costs)
+ double v = drawMs / double(triangles);
+ // (A frame whose GPU time can't hold those triangles didn't draw them: UI tests draw the 3D view only for their
+ // shots, the timer measures every frame. More than 1e11 triangles a second is no measurement.)
+ if (v < 1e-8) return estimate;
+ if (!(estimate > 0.0)) return v;
+ v = std::min(v, 4.0 * estimate);
+ return 0.8 * estimate + 0.2 * v;
+}
+
void coarseAttributes(CoarseLevels& L, const Mesh& mesh, std::vector<uint32_t> cells) {
const size_t nv = mesh.vertexCount(), count = L.positions.size();
if (L.levels.empty() || L.vertexCell.size() != nv) return;
diff --git a/src/render/Clusters.h b/src/render/Clusters.h
index 2e6f86d6..6cdc571c 100644
--- a/src/render/Clusters.h
+++ b/src/render/Clusters.h
@@ -122,6 +122,14 @@ void coarseAttributes(CoarseLevels& levels, const Mesh& mesh, std::vector<uint32
// Whether the cells' colours and mask are there exactly where the mesh has them.
bool coarseAttributesMatch(const CoarseLevels& levels, const Mesh& mesh);
+// The measured GPU time per drawn triangle (ms; 0 = none yet), a moving average of whole-frame timer queries, after
+// one more frame: `gpuMs` for `triangles`, of which `cpuBeforeDrawMs` the CPU spent before the 3D view's first draw
+// (the UI, an action, the upload: the GPU waits through it inside the query). Frames too small to say, and samples
+// over four times the estimate (a first draw linking its shaders, a stall) move it by at most that much: one slow
+// frame never decides that a mesh's full detail can't be drawn at rest (#151: a paint layer added on a 4M mesh left
+// it on its coarse level until a restart, as coarse frames never measure the full mesh again).
+double nextGpuMsPerTriangle(double estimate, double gpuMs, double cpuBeforeDrawMs, uint64_t triangles);
+
// 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). Wound outwards (a positive signed volume: the sum of ClusterBounds::volume), every
// back face then lies behind a front face from outside. (Strictly: where no part of the surface is turned
diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp
index d2e01566..3958bb10 100644
--- a/src/render/Renderer.cpp
+++ b/src/render/Renderer.cpp
@@ -3351,13 +3351,8 @@ void Renderer::uploadCoarseAttributes(MeshSlot& s) {
gl::BindBuffer(GL_ARRAY_BUFFER, s.coarseVertices); // (the points' attributes are set up against it)
}
-void Renderer::noteGpuFrame(double gpuMs, uint64_t drawnTriangles) {
- if (!(gpuMs > 0.2) || drawnTriangles < 500000) return; // (small frames are mostly fixed costs)
- const double v = gpuMs / double(drawnTriangles);
- // (A frame whose GPU time can't hold those triangles didn't draw them: UI tests draw the 3D view only for their
- // shots, the timer measures every frame. More than 1e11 triangles a second is no measurement.)
- if (v < 1e-8) return;
- gpuMsPerTriangle_ = gpuMsPerTriangle_ > 0.0 ? 0.8 * gpuMsPerTriangle_ + 0.2 * v : v;
+void Renderer::noteGpuFrame(double gpuMs, uint64_t drawnTriangles, double cpuBeforeDrawMs) {
+ gpuMsPerTriangle_ = nextGpuMsPerTriangle(gpuMsPerTriangle_, gpuMs, cpuBeforeDrawMs, drawnTriangles);
}
size_t Renderer::coarsePendingClusters() const {
diff --git a/src/render/Renderer.h b/src/render/Renderer.h
index 6667440c..0d5ffa14 100644
--- a/src/render/Renderer.h
+++ b/src/render/Renderer.h
@@ -262,7 +262,9 @@ public:
// enough (the app draws until then).
bool coarsePreviewOnly() const { return previewOnly_; }
bool coarseStillWanted() const { return stillWanted_; }
- void noteGpuFrame(double gpuMs, uint64_t drawnTriangles); // (the frame governor's GPU timer)
+ // The frame governor's GPU timer: the whole frame's GPU time, the CPU's before the 3D view drew (render/Clusters.h:
+ // nextGpuMsPerTriangle)
+ void noteGpuFrame(double gpuMs, uint64_t drawnTriangles, double cpuBeforeDrawMs = 0.0);
void setTriangleRate(double millionsPerSecond) { triangleRateM_ = millionsPerSecond; } // (calibration; 0 = unknown)
void setStroking(bool on) { stroking_ = on; } // a stroke runs: sync leaves the edited clusters' levels until it ends
// The last syncScene left motion proxies to build (a few milliseconds of them a frame; the rest draw in full).
diff --git a/tests/test_render_clusters.cpp b/tests/test_render_clusters.cpp
index 47412f52..c96551f2 100644
--- a/tests/test_render_clusters.cpp
+++ b/tests/test_render_clusters.cpp
@@ -513,3 +513,31 @@ TEST(coarse_levels_carry_colour_and_mask) {
updateCoarseLevels(L, m, order, {0u});
CHECK(L.colors.empty() && L.mask.empty());
}
+
+TEST(coarse_still_frame_estimate_survives_a_slow_frame) {
+ // #151: a paint layer added on a 4.28M-triangle top level left the mesh on its coarse level at rest until a
+ // restart. The frame that added it spent ~400 ms on the CPU (the layer, the upload, the coarse levels) while the
+ // GPU timer, which brackets the whole frame, waited: 400 ms over 4.28M triangles put the full-detail still frame
+ // past its 100 ms budget, and coarse frames never measure the full mesh again.
+ const uint64_t top = 4279936;
+ double e = 0.0;
+ for (int i = 0; i < 10; ++i) e = nextGpuMsPerTriangle(e, 6.0, 0.5, top); // a 3080 Ti class GPU: ~6 ms a frame
+ const double settled = e;
+ CHECK(double(top) * settled < 10.0);
+ // The slow frame, a few times over (the add, then the coarse levels catching up): the CPU's part isn't counted.
+ for (int i = 0; i < 4; ++i) e = nextGpuMsPerTriangle(e, 420.0, 410.0, top);
+ CHECK(double(top) * e < 100.0);
+ CHECK(e < 1.5 * settled);
+ // A spike inside the draw itself (a first draw linking its shaders): one frame moves it by at most 1.6 times.
+ e = nextGpuMsPerTriangle(settled, 2000.0, 0.5, top);
+ CHECK(e <= 1.6 * settled * 1.0001);
+ CHECK(double(top) * e < 100.0);
+ // A GPU that really is that slow still gets there after a few frames.
+ e = settled;
+ for (int i = 0; i < 30; ++i) e = nextGpuMsPerTriangle(e, 300.0, 0.5, top);
+ CHECK(double(top) * e > 100.0);
+ // Frames too small to say, or with no GPU time left after the CPU's, leave it alone.
+ CHECK(nextGpuMsPerTriangle(settled, 50.0, 0.5, 100000) == settled);
+ CHECK(nextGpuMsPerTriangle(settled, 50.0, 60.0, top) == settled);
+ CHECK(nextGpuMsPerTriangle(0.0, 6.0, 0.5, top) > 0.0);
+}