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.

Frames overlap CPU and GPU: GPU frame times read through fences, one frame in flight

islandmed5c07579d2b81
diff --git a/src/app/App.cpp b/src/app/App.cpp
index 7c8240b6..c81cc969 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -322,6 +322,8 @@ void App::shutdown() {
spaceMouseClose();
shutdownHardwareProfile(); // its timer queries need the GL context
+ if (lastFrameFence_) gl::DeleteSync(lastFrameFence_);
+ lastFrameFence_ = nullptr;
if (refTexture_) gl::DeleteTextures(1, &refTexture_);
gallery_.reset(); // its preview textures need the GL context
thumbs_.clear();
@@ -1191,7 +1193,15 @@ int App::run() {
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
hardwareProfileFrameEnd();
if (proof_) proofBeforeSwap(fbW, fbH); // Help > Benchmark: timing, video capture, the scripted pointer
+ // CPU and GPU overlap: the GPU draws this frame while the CPU builds the next. At most one frame in flight
+ // (the input lag doesn't grow): the last frame's GPU work is done before this one is shown.
+ if (lastFrameFence_) {
+ gl::ClientWaitSync(lastFrameFence_, GL_SYNC_FLUSH_COMMANDS_BIT, 100'000'000ull); // (100 ms: never stuck)
+ gl::DeleteSync(lastFrameFence_);
+ }
SDL_GL_SwapWindow(window_);
+ lastFrameFence_ = gl::FenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
+ gl::Flush(); // (to the GPU now: an offscreen swap sends nothing)
mark();
if (phases == 7) // (Help > Benchmark splits each phase's frames by these)
for (int k = 0; k < 6; ++k) frameSplitMs_[k] = phase[k + 1] - phase[k];
diff --git a/src/app/App.h b/src/app/App.h
index 9f50e15f..25c0b4f9 100644
--- a/src/app/App.h
+++ b/src/app/App.h
@@ -2374,6 +2374,7 @@ private:
// The last drawn frame's time by part (run()'s marks): events, dabs, UI, ghost + scene sync (the upload), draw,
// UI draw + swap (where the frame waits for the GPU).
double frameSplitMs_[6] = {};
+ GLsync lastFrameFence_ = nullptr; // the last shown frame's GPU work is done
// GPU time of frames (GL_TIME_ELAPSED, a few frames late) since Help > Benchmark last took them.
std::vector<double> proofGpuMs_;
// The view's resolution while it moves (experimental, docs/design/perf-moonshots.md §0): 0.75 on the simulated
diff --git a/src/app/Calibration.cpp b/src/app/Calibration.cpp
index 07bc42ea..ef69641a 100644
--- a/src/app/Calibration.cpp
+++ b/src/app/Calibration.cpp
@@ -478,7 +478,11 @@ bool GpuFrameTimer::available() {
void GpuFrameTimer::begin() {
if (!available() || active_ >= 0) return;
- if (pending_[next_]) pending_[next_] = false; // never read in time: dropped
+ if (pending_[next_]) { // never read in time: dropped
+ pending_[next_] = false;
+ if (fences_[next_]) gl::DeleteSync(fences_[next_]);
+ fences_[next_] = nullptr;
+ }
static const GlExtra e = loadExtra();
e.BeginQuery(kGL_TIME_ELAPSED, queries_[next_]);
active_ = next_;
@@ -488,6 +492,7 @@ void GpuFrameTimer::end(uint64_t frame) {
if (active_ < 0) return;
static const GlExtra e = loadExtra();
e.EndQuery(kGL_TIME_ELAPSED);
+ fences_[active_] = gl::FenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
frames_[active_] = frame;
pending_[active_] = true;
next_ = (active_ + 1) % kSlots;
@@ -502,9 +507,16 @@ bool GpuFrameTimer::poll(uint64_t& frame, double& ms) {
for (int k = 0; k < kSlots; ++k) {
const int i = (next_ + k) % kSlots;
if (!pending_[i] || i == active_) continue;
- uint64_t ready = 0;
- e.GetQueryObjectui64v(queries_[i], kGL_QUERY_RESULT_AVAILABLE, &ready);
- if (!ready) return false; // later ones aren't either
+ if (fences_[i]) { // done on the GPU? (a zero timeout: never waits)
+ const GLenum r = gl::ClientWaitSync(fences_[i], 0, 0);
+ if (r == GL_TIMEOUT_EXPIRED) return false; // later ones aren't either
+ gl::DeleteSync(fences_[i]);
+ fences_[i] = nullptr;
+ } else { // (no fence: the driver's word)
+ uint64_t ready = 0;
+ e.GetQueryObjectui64v(queries_[i], kGL_QUERY_RESULT_AVAILABLE, &ready);
+ if (!ready) return false;
+ }
uint64_t ns = 0;
e.GetQueryObjectui64v(queries_[i], kGL_QUERY_RESULT, &ns);
pending_[i] = false;
@@ -520,6 +532,10 @@ void GpuFrameTimer::release() {
static const GlExtra e = loadExtra();
if (active_ >= 0) e.EndQuery(kGL_TIME_ELAPSED);
e.DeleteQueries(kSlots, queries_);
+ for (auto*& f : fences_) {
+ if (f) gl::DeleteSync(f);
+ f = nullptr;
+ }
}
ok_ = false;
loaded_ = false;
diff --git a/src/app/Calibration.h b/src/app/Calibration.h
index e65e09cd..ef743d51 100644
--- a/src/app/Calibration.h
+++ b/src/app/Calibration.h
@@ -13,6 +13,7 @@
#pragma once
#include "core/HardwareProfile.h"
+#include "render/GL.h"
#include <string>
@@ -46,7 +47,9 @@ private:
};
// GPU time of each frame through GL timer queries (GL_TIME_ELAPSED), read a few frames later so the CPU never
-// waits for the GPU. Unavailable (available() false) where the driver has no timer queries.
+// waits for the GPU: a fence after each query says when it is done (asking the query itself, even only whether it
+// is available, waited for the whole frame's GPU work on Mesa's Apple driver: CPU and GPU took turns). Unavailable
+// (available() false) where the driver has no timer queries.
class GpuFrameTimer {
public:
bool available(); // loads the entry points on first use (needs the context)
@@ -58,6 +61,7 @@ public:
private:
static constexpr int kSlots = 4;
unsigned queries_[kSlots] = {};
+ GLsync fences_[kSlots] = {}; // signalled when the slot's query is done
uint64_t frames_[kSlots] = {};
bool pending_[kSlots] = {};
int next_ = 0, active_ = -1;
diff --git a/src/render/GL.h b/src/render/GL.h
index d0d69a5d..2fb9600a 100644
--- a/src/render/GL.h
+++ b/src/render/GL.h
@@ -126,6 +126,7 @@ constexpr GLbitfield GL_COLOR_BUFFER_BIT = 0x4000;
X(void, PolygonOffset, GLfloat, GLfloat) \
X(const GLubyte*, GetString, GLenum) \
X(void, Finish, void) \
+ X(void, Flush, void) \
X(GLenum, GetError, void) \
X(void, GenBuffers, GLsizei, GLuint*) \
X(void, DeleteBuffers, GLsizei, const GLuint*) \