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.

First stroke: the stroke's programs (predictive preview GHOST, draft FLAT) are compiled and drawn a frame after the model shows

islandmed8db5392e6221
diff --git a/changes/fix-first-stroke-stall.md b/changes/fix-first-stroke-stall.md
new file mode 100644
index 00000000..c2dc2dc0
--- /dev/null
+++ b/changes/fix-first-stroke-stall.md
@@ -0,0 +1,11 @@
+---
+fixed: The first stroke of a session no longer waits for the stroke's own shaders: the predictive preview and draft
+ stroke programs are compiled and drawn once a frame after the model shows (on Windows with NVIDIA the first stroke
+ froze one frame for 1-1.8 s on 15-28M meshes). SF_NO_STROKE_WARM=1 turns it off to compare.
+testers:
+ - Windows, NVIDIA: open a 15M+ model, wait a second, make one stroke; the view keeps moving with the brush (no
+ one-second freeze at the start).
+---
+Cause found in the bench JSONs: the stall is all in the "draw" split (drawView), not sculpt/upload/GPU time; the only
+GL work new on that frame is the GHOST / FLAT program variants and the first BufferSubData into the vertex buffer.
+The Windows gate (win.sh, PC frozen at commit time) is in REPORT.md: drop this fragment if it doesn't confirm.
diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cpp
index c1bc0541..e90c7c4e 100644
--- a/src/app/UiTest.cpp
+++ b/src/app/UiTest.cpp
@@ -1106,6 +1106,7 @@ double Demo::query(const std::string& name, bool& known) const {
app_.benchmarkDrawMs(0);
return double(app_.renderer_.coarseDrawnClusters());
}
+ if (name == "stroke_programs") return double(app_.renderer_.strokePrograms()); // compiled before any stroke (warmed)
if (name == "coarse_pending") return double(app_.renderer_.coarsePendingClusters()); // edited, levels not updated yet
if (name == "panel_drag_scrolls") return double(ui::DragScrolls()); // #100: drags that scrolled a panel
if (name == "panel_drag_scroll_y") return double(ui::DragScrollY()); // that panel's scroll after the last one
diff --git a/src/render/GL.h b/src/render/GL.h
index d0d69a5d..107052f7 100644
--- a/src/render/GL.h
+++ b/src/render/GL.h
@@ -132,6 +132,7 @@ constexpr GLbitfield GL_COLOR_BUFFER_BIT = 0x4000;
X(void, BindBuffer, GLenum, GLuint) \
X(void, BufferData, GLenum, GLsizeiptr, const void*, GLenum) \
X(void, BufferSubData, GLenum, GLintptr, GLsizeiptr, const void*) \
+ X(void, GetBufferSubData, GLenum, GLintptr, GLsizeiptr, void*) \
X(void, GenVertexArrays, GLsizei, GLuint*) \
X(void, DeleteVertexArrays, GLsizei, const GLuint*) \
X(void, BindVertexArray, GLuint) \
diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp
index d2e01566..043f57b1 100644
--- a/src/render/Renderer.cpp
+++ b/src/render/Renderer.cpp
@@ -3550,6 +3550,42 @@ void Renderer::drawSlot(const MeshSlot& s, bool active, bool flat, const RenderS
// The back faces left in clusters along the silhouette: the GPU skips them too when they can't show.
if (cullBackfaces_ || xrayCull_) gl::Enable(GL_CULL_FACE);
if (xrayCull_) gl::CullFace(xrayCull_);
+ // The first stroke's own GL work, done a frame after the Part first shows instead (the open's first frame stays as
+ // it was): its programs below (GHOST, draft FLAT) compile on first use, and drivers that compile only at the first
+ // draw (NVIDIA on Windows) stalled the first stroke of every session 1-1.8 s. Each draws one cluster here, under
+ // the main draw (same depth: it paints over it), and that cluster's vertices go up once as a stroke upload does.
+ // SF_NO_STROKE_WARM=1 leaves it out (to compare); SF_FRAME_LOG says what it cost.
+ static const bool noWarm = std::getenv("SF_NO_STROKE_WARM") != nullptr;
+ if (active && !noWarm && !thumbnailPass_ && !textured && !normalMapped && drawAlpha_ >= 1.0f && !xrayCull_ && !drawClusters_.empty() &&
+ s.strokeWarmed != features && s.strokeWarmFrames++ >= 1) {
+ s.strokeWarmed = features;
+ const auto warmStart = std::chrono::steady_clock::now();
+ const std::vector<uint32_t> one{drawClusters_.front()};
+ const auto range = s.order.clusters()[one[0]];
+ std::vector<PackedVertex> verts(range.vertexCount);
+ if (!verts.empty()) { // (BufferSubData of what is there: the stroke uploads' path, nothing changes)
+ const GLintptr at = GLintptr(size_t(range.firstVertex) * sizeof(PackedVertex));
+ const GLsizeiptr bytes = GLsizeiptr(verts.size() * sizeof(PackedVertex));
+ gl::BindBuffer(GL_ARRAY_BUFFER, s.vertexBuffer);
+ gl::GetBufferSubData(GL_ARRAY_BUFFER, at, bytes, verts.data());
+ gl::BufferSubData(GL_ARRAY_BUFFER, at, bytes, verts.data());
+ gl::BindBuffer(GL_ARRAY_BUFFER, 0);
+ }
+ const size_t drawn = lastDrawn_;
+ for (int k = 1; k < 4; ++k) {
+ if ((k & 2) && perTriangle) continue; // (draft strokes draw per-triangle data in full)
+ if (const MeshProgram* pg = meshProgram(features | ((k & 1) ? kGhost : 0) | ((k & 2) ? kFlat : 0))) {
+ bind(pg);
+ if (k & 1) gl::Uniform1i(pg->ghostGroups, 0); // (no dabs: the surface as it is)
+ drawClusters(s, one, GL_TRIANGLES, DrawPart::Triangles, -1);
+ }
+ }
+ bind(p);
+ lastDrawn_ = drawn;
+ if (static const bool log = std::getenv("SF_FRAME_LOG") != nullptr; log)
+ SDL_Log("stroke programs warmed in %.1f ms (features 0x%x)",
+ std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - warmStart).count(), unsigned(features));
+ }
// The predictive preview (render/Ghost.h): the clusters a ghost dab reaches draw with the GHOST program, the
// rest as usual (most of the mesh, which then doesn't pay for the preview).
// A draft stroke (Sculptor::draftStrokes) left the normals of the clusters it moved as they were: those draw
diff --git a/src/render/Renderer.h b/src/render/Renderer.h
index 6667440c..56592a89 100644
--- a/src/render/Renderer.h
+++ b/src/render/Renderer.h
@@ -256,6 +256,12 @@ public:
// are still to be updated (drawn in full until then).
size_t coarseDrawnClusters() const { return lastCoarseClusters_; }
size_t coarsePendingClusters() const;
+ // The stroke-only programs (predictive preview, draft normals) compiled so far: drawSlot warms them before a stroke.
+ int strokePrograms() const {
+ int n = 0;
+ for (int f = 0; f < 256; ++f) n += programs_[f].id && (f & (kGhost | kFlat)) ? 1 : 0;
+ return n;
+ }
// At rest (still 150 ms, no stroke) an over-budget mesh draws in full detail when one full frame fits the still-frame
// budget (100 ms, from the measured GPU time per triangle); else it stays coarse (coarsePreviewOnly: the info line
// says so). coarseStillWanted: the last frame drew coarse and full detail follows once the view has been still long
@@ -419,6 +425,9 @@ private:
// A mesh with a micro level (core/Micro.h) draws through this instead of the buffers above.
std::shared_ptr<MicroRenderer> micro;
size_t microDrawn = 0;
+ // The stroke-only programs (GHOST, draft FLAT) warmed for these features (drawSlot; -1: not yet), and
+ // the frames drawn while waiting (the open's first frame isn't made slower).
+ mutable int strokeWarmed = -1, strokeWarmFrames = 0;
};
MeshSlot& slot(uint32_t id); // created on first use
static void freeSlot(MeshSlot& s);
diff --git a/tests/ui/stroke_warm.sfui b/tests/ui/stroke_warm.sfui
new file mode 100644
index 00000000..42b4641e
--- /dev/null
+++ b/tests/ui/stroke_warm.sfui
@@ -0,0 +1,12 @@
+# The first stroke of a session draws with programs nothing drew before it (the predictive preview's GHOST, draft
+# strokes' FLAT normals); NVIDIA compiles them at their first draw and froze the first stroke 1-1.8 s on Windows.
+# They're compiled and drawn a frame after the model shows, so none is left for the stroke.
+run new_sphere subdivisions=4
+expect_value drawn_triangles > 0 # (the view drawn: the model shows)
+expect_value drawn_triangles > 0 # a frame later: warmed
+expect_value stroke_programs >= 3
+key f
+camera 0 0
+snapshot
+drag_path 0.4 0.5 0.6 0.5
+expect_value moved > 0