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.
C++ 3d3d-sculptingcppdear-imguidigital-sculptingfbxgame-developmentgaussian-splattinggltfimage-to-3dmcpopenglopenusdsculptingtext-to-3dunreal-engine
Warm UI tests: sculptforge --ui-serve runs UI test scripts one after another in one window and GL context
islandmed67caecba4bfb
diff --git a/CMakeLists.txt b/CMakeLists.txtindex ea9f2a18..a5a06268 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -311,6 +311,7 @@ if(SCULPTFORGE_BUILD_APP)src/app/AsyncStroke.cppsrc/app/Demo.cppsrc/app/UiTest.cpp+ src/app/UiServe.cppsrc/app/Actions.cppsrc/app/BlenderKeymap.cppsrc/app/Ui.cpp@@ -702,11 +703,36 @@ if(SCULPTFORGE_BUILD_TESTS)# UI tests: each tests/ui/*.sfui script drives the real app headless (needs an# OpenGL 3.3 context; CI runs them on Linux under Xvfb with Mesa). Label "ui".if(SCULPTFORGE_BUILD_APP)+ # Warm UI tests (tools/test/warm_ui.py): the fixture ui_warm starts a pool of `sculptforge --ui-serve` servers+ # (one window and GL context each, made once) and each ui_* script test is a thin client of it, with the same+ # output and exit code as a cold run. SF_UI_COLD=1 when running ctest runs them cold, as does+ # -DSCULPTFORGE_UI_WARM=OFF; a script with a `# cold` line always runs cold. Not yet on Windows (cold there).+ option(SCULPTFORGE_UI_WARM "Run UI test scripts in a pool of warm servers" ON)+ set(SF_UI_WARM_DIR ${CMAKE_CURRENT_BINARY_DIR}/ui-warm)+ if(SCULPTFORGE_UI_WARM AND Python3_Interpreter_FOUND AND NOT WIN32)+ sculptforge_runner(SF_UI_WARM_APP sculptforge)+ set(SF_UI_WARM_PY ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/test/warm_ui.py)+ add_test(NAME ui_warm_start COMMAND ${SF_UI_WARM_PY} start --dir ${SF_UI_WARM_DIR} --app ${SF_UI_WARM_APP})+ add_test(NAME ui_warm_stop COMMAND ${SF_UI_WARM_PY} stop --dir ${SF_UI_WARM_DIR})+ set_tests_properties(ui_warm_start PROPERTIES FIXTURES_SETUP ui_warm LABELS ui TIMEOUT 60)+ set_tests_properties(ui_warm_stop PROPERTIES FIXTURES_CLEANUP ui_warm LABELS ui TIMEOUT 400)+ endif()+ # sculptforge_ui_test(NAME SCRIPT [ARGS...]): ui_NAME runs tests/ui/SCRIPT.sfui, screenshots in ui-shots/NAME.+ function(sculptforge_ui_test name script)+ set(args --ui-test ${CMAKE_CURRENT_SOURCE_DIR}/tests/ui/${script}.sfui ${ARGN}+ --ui-out ${CMAKE_CURRENT_BINARY_DIR}/ui-shots/${name})+ if(TEST ui_warm_start)+ add_test(NAME ui_${name} COMMAND ${SF_UI_WARM_PY} client --dir ${SF_UI_WARM_DIR} --app ${SF_UI_WARM_APP} -- ${args})+ set_tests_properties(ui_${name} PROPERTIES FIXTURES_REQUIRED ui_warm)+ else()+ add_test(NAME ui_${name} COMMAND sculptforge ${args})+ endif()+ set_tests_properties(ui_${name} PROPERTIES LABELS ui TIMEOUT 300)+ endfunction()file(GLOB SCULPTFORGE_UI_TESTS CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tests/ui/*.sfui)foreach(script ${SCULPTFORGE_UI_TESTS})get_filename_component(name ${script} NAME_WE)- add_test(NAME ui_${name} COMMAND sculptforge --ui-test ${script} --ui-out ${CMAKE_CURRENT_BINARY_DIR}/ui-shots/${name})- set_tests_properties(ui_${name} PROPERTIES LABELS ui TIMEOUT 300)+ sculptforge_ui_test(${name} ${name})endforeach()# Splat training (also started by a phone scan) runs the fake Brush / COLMAP / ffmpeg.foreach(t ui_splat_train ui_phone_link)@@ -720,43 +746,27 @@ if(SCULPTFORGE_BUILD_TESTS)set_tests_properties(ui_hardware_profile PROPERTIES ENVIRONMENT "SF_HW_PROFILE_IN_TESTS=1")endif()# The shell layout again on a 1920x1080 window (docs/ui-design.md §2.3).- add_test(NAME ui_shell_layout_1080 COMMAND sculptforge --ui-test ${CMAKE_CURRENT_SOURCE_DIR}/tests/ui/shell_layout.sfui- --size 1920x1080 --ui-out ${CMAKE_CURRENT_BINARY_DIR}/ui-shots/shell_layout_1080)- set_tests_properties(ui_shell_layout_1080 PROPERTIES LABELS ui TIMEOUT 300)- add_test(NAME ui_help_console_1080 COMMAND sculptforge --ui-test ${CMAKE_CURRENT_SOURCE_DIR}/tests/ui/help_console.sfui- --size 1920x1080 --ui-out ${CMAKE_CURRENT_BINARY_DIR}/ui-shots/help_console_1080)- set_tests_properties(ui_help_console_1080 PROPERTIES LABELS ui TIMEOUT 300)+ sculptforge_ui_test(shell_layout_1080 shell_layout --size 1920x1080)+ sculptforge_ui_test(help_console_1080 help_console --size 1920x1080)# The S / U / I / O slider on a UI scaled 1.5x (Windows at 150%, #9): it must still open under the cursor.- add_test(NAME ui_size_popup_scaled COMMAND sculptforge --ui-test ${CMAKE_CURRENT_SOURCE_DIR}/tests/ui/size_popup.sfui- --size 2400x1350 --ui-out ${CMAKE_CURRENT_BINARY_DIR}/ui-shots/size_popup_scaled)- set_tests_properties(ui_size_popup_scaled PROPERTIES LABELS ui TIMEOUT 300)+ sculptforge_ui_test(size_popup_scaled size_popup --size 2400x1350)# The stroke popup on a UI scaled 1.5x: it opens under the cursor and its items are where they're drawn.- add_test(NAME ui_stroke_popup_scaled COMMAND sculptforge --ui-test ${CMAKE_CURRENT_SOURCE_DIR}/tests/ui/stroke_popup.sfui- --size 2400x1350 --ui-out ${CMAKE_CURRENT_BINARY_DIR}/ui-shots/stroke_popup_scaled)- set_tests_properties(ui_stroke_popup_scaled PROPERTIES LABELS ui TIMEOUT 300)+ sculptforge_ui_test(stroke_popup_scaled stroke_popup --size 2400x1350)# UI scale (#87) at 1600x900, where Automatic in a test is exactly 100 %; label "dpi": release.sh step check runs them before the tag.- add_test(NAME ui_ui_scale_900 COMMAND sculptforge --ui-test ${CMAKE_CURRENT_SOURCE_DIR}/tests/ui/ui_scale.sfui- --size 1600x900 --ui-out ${CMAKE_CURRENT_BINARY_DIR}/ui-shots/ui_scale_900)+ sculptforge_ui_test(ui_scale_900 ui_scale --size 1600x900)set_tests_properties(ui_ui_scale_900 PROPERTIES LABELS "ui;dpi" TIMEOUT 300)if(TEST ui_ui_scale)set_tests_properties(ui_ui_scale PROPERTIES LABELS "ui;dpi")endif()# The Library on a 1280x720 screen (#80): its close button stays on screen.- add_test(NAME ui_library_small_screen_720 COMMAND sculptforge --ui-test ${CMAKE_CURRENT_SOURCE_DIR}/tests/ui/library_small_screen.sfui- --size 1280x720 --ui-out ${CMAKE_CURRENT_BINARY_DIR}/ui-shots/library_small_screen_720)- set_tests_properties(ui_library_small_screen_720 PROPERTIES LABELS ui TIMEOUT 300)+ sculptforge_ui_test(library_small_screen_720 library_small_screen --size 1280x720)# #107: every look's right tray on a 1280x720 screen; #105: a Windows laptop at 150-200 % (2560x1600 at 200 % is# 1280x800 logical px), nothing past the window's edges.- add_test(NAME ui_right_tray_looks_720 COMMAND sculptforge --ui-test ${CMAKE_CURRENT_SOURCE_DIR}/tests/ui/right_tray_looks.sfui- --size 1280x720 --ui-out ${CMAKE_CURRENT_BINARY_DIR}/ui-shots/right_tray_looks_720)- add_test(NAME ui_windows_dpi_2560 COMMAND sculptforge --ui-test ${CMAKE_CURRENT_SOURCE_DIR}/tests/ui/windows_dpi.sfui- --size 2560x1600 --ui-out ${CMAKE_CURRENT_BINARY_DIR}/ui-shots/windows_dpi_2560)- add_test(NAME ui_windows_dpi_2160 COMMAND sculptforge --ui-test ${CMAKE_CURRENT_SOURCE_DIR}/tests/ui/windows_dpi.sfui- --size 2160x1350 --ui-out ${CMAKE_CURRENT_BINARY_DIR}/ui-shots/windows_dpi_2160)+ sculptforge_ui_test(right_tray_looks_720 right_tray_looks --size 1280x720)+ sculptforge_ui_test(windows_dpi_2560 windows_dpi --size 2560x1600)+ sculptforge_ui_test(windows_dpi_2160 windows_dpi --size 2160x1350)set_tests_properties(ui_right_tray_looks_720 ui_windows_dpi_2560 ui_windows_dpi_2160 PROPERTIES LABELS "ui;dpi" TIMEOUT 300)- add_test(NAME ui_user_guide_1080 COMMAND sculptforge --ui-test ${CMAKE_CURRENT_SOURCE_DIR}/tests/ui/user_guide.sfui- --size 1920x1080 --ui-out ${CMAKE_CURRENT_BINARY_DIR}/ui-shots/user_guide_1080)- set_tests_properties(ui_user_guide_1080 PROPERTIES LABELS ui TIMEOUT 300)+ sculptforge_ui_test(user_guide_1080 user_guide --size 1920x1080)# A crash leaves a note in the log file (#16). Needs no window.add_test(NAME crash_log COMMAND ${CMAKE_COMMAND} -DAPP=$<TARGET_FILE:sculptforge>-DDIR=${CMAKE_CURRENT_BINARY_DIR}/crash-log-test -P ${CMAKE_CURRENT_SOURCE_DIR}/tests/crash_log.cmake)diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex a6a1aa2e..965f3d70 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -71,7 +71,15 @@ ctest --test-dir build -L ui #The step reference is at the top of `src/app/UiTest.cpp`. Tests run headless on simulated time, so mosttake under a second. They use a throwaway settings folder and never touch your own. A failing step stopsthe script and saves `FAIL-line-N.png`, and clicking a missing item lists similarly named items. Clicks-scroll panels as needed, and clicking an item a person couldn't reach (off screen) fails. CI runs the UI+scroll panels as needed, and clicking an item a person couldn't reach (off screen) fails.++Under ctest (Linux, macOS) the scripts run warm: the `ui_warm` fixture starts a few `sculptforge --ui-serve`+servers that make their window and OpenGL context once and then run one script after another, each in a fresh+app with fresh settings (`tools/test/warm_ui.py`; `src/app/UiServe.h` lists what is reset between scripts). Output+and exit codes are those of a cold run; a server that crashes reruns its script cold. `SF_UI_COLD=1 ctest ...` runs+every script in its own process, as does a `# cold` line in a script that changes something the reset can't put+back. `tools/test/warm_ui.py suite --build build --repeat 3 --shuffle` runs the suite outside ctest and+`warm_ui.py compare` lists verdicts and screenshots that differ between two such runs. CI runs the UItests on Linux under Xvfb and on Windows with Mesa's software OpenGL, and uploads the screenshots as anartifact. CI compiles through ccache (see the comment at the top of `.github/workflows/ci.yml`). Releases: see[docs/releasing.md](docs/releasing.md).diff --git a/src/ai/Http.cpp b/src/ai/Http.cppindex ffb2b43d..097ab450 100644--- a/src/ai/Http.cpp+++ b/src/ai/Http.cpp@@ -22,6 +22,7 @@ std::atomic<uint64_t> gRequests{0};}uint64_t httpRequestCount() { return gRequests; }+void resetHttpRequestCount() { gRequests = 0; }HttpResponse httpRequest(const std::string& method, const std::string& url, const std::vector<std::string>& headers,const std::string& body, int timeoutSeconds) {diff --git a/src/ai/Http.h b/src/ai/Http.hindex 8607c3ff..e731d13d 100644--- a/src/ai/Http.h+++ b/src/ai/Http.h@@ -41,6 +41,7 @@ std::string multipartBody(const std::vector<MultipartField>& fields, std::string// Every request this process started (tests: the AI switch sends nothing while it is off).uint64_t httpRequestCount();+void resetHttpRequestCount(); // (the warm UI test server, between scripts: app/UiServe.h)inline HttpResponse httpPostJson(const std::string& url, const std::vector<std::string>& headers, const std::string& body,int timeoutSeconds = 120) {diff --git a/src/app/App.cpp b/src/app/App.cppindex b8d5f790..38d2455f 100644--- a/src/app/App.cpp+++ b/src/app/App.cpp@@ -16,6 +16,7 @@#include "app/Shell.h"#include "app/Theme.h"#include "app/Ui.h"+#include "app/UiServe.h"#include <IconsLucide.h>@@ -92,11 +93,26 @@ bool ctrlDown(SDL_Keymod mod) { return (mod & (SDL_KMOD_CTRL | SDL_KMOD_GUI)) !=} // namespace+namespace {+// SF_STARTUP_TIMES=1: how long each start-up step took (stderr), to keep start-up and UI tests quick.+struct StartupTimer {+ bool on = std::getenv("SF_STARTUP_TIMES") != nullptr;+ std::chrono::steady_clock::time_point last = std::chrono::steady_clock::now();+ void step(const char* what) {+ if (!on) return;+ const auto now = std::chrono::steady_clock::now();+ std::fprintf(stderr, "startup %-22s %7.1f ms\n", what, std::chrono::duration<double, std::milli>(now - last).count());+ last = now;+ }+};+} // namespace+App::App() = default;App::~App() = default;bool App::init(const AppOptions& options) {options_ = options;+ StartupTimer timer;const bool recording = !options.recordPath.empty() || !options.uiTest.empty();const bool headless = !options.renderPath.empty() || recording || !options.timelapsePath.empty();// UI tests run in a hidden window that never has focus; SDL drops joystick input without focus otherwise, and@@ -107,7 +123,8 @@ bool App::init(const AppOptions& options) {// Full screen on one monitor stays up when the pointer clicks on another (Windows multi-monitor, Discord).SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");if (!headless && options.demo.empty()) applyCachedThreadCount(); // the hardware profile's threads, before any brush work- if (!SDL_Init(SDL_INIT_VIDEO)) {+ if (sharedWindow_) syncSdlEnvironment(); // (what SDL_Init would have read)+ else if (!SDL_Init(SDL_INIT_VIDEO)) {SDL_Log("SDL_Init failed: %s", SDL_GetError());return false;}@@ -117,16 +134,7 @@ bool App::init(const AppOptions& options) {else SDL_Log("SDL joystick init failed (no SpaceMouse): %s", SDL_GetError());}- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);- SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);- SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);- // No multisampling in the window: the 3D view antialiases in its own target (Renderer::drawView), so it can- // draw with fewer samples while a huge scene moves (Preferences > Performance).- SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);- SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);+ glWindowAttributes();const SDL_WindowFlags flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY |(headless ? SDL_WINDOW_HIDDEN : 0) |@@ -135,7 +143,12 @@ bool App::init(const AppOptions& options) {rememberWindow_ = !sized && !headless && options.demo.empty() && !options.fullscreen;const WindowState saved = rememberWindow_ ? loadWindowState() : WindowState{};const int winW = sized ? options.width : saved.w, winH = sized ? options.height : saved.h;- window_ = SDL_CreateWindow("SculptForge", winW, winH, flags);+ if (sharedWindow_) { // --ui-serve: the server's window, at this script's size+ window_ = sharedWindow_;+ SDL_SetWindowSize(window_, winW, winH);+ SDL_SyncWindow(window_);+ } else+ window_ = SDL_CreateWindow("SculptForge", winW, winH, flags);if (!window_) {SDL_Log("SDL_CreateWindow failed: %s", SDL_GetError());return false;@@ -162,15 +175,17 @@ bool App::init(const AppOptions& options) {}}}- glContext_ = SDL_GL_CreateContext(window_);+ glContext_ = sharedContext_ ? sharedContext_ : SDL_GL_CreateContext(window_);if (!glContext_) {SDL_Log("OpenGL 3.3 context creation failed: %s", SDL_GetError());return false;}SDL_GL_MakeCurrent(window_, glContext_);SDL_GL_SetSwapInterval(1);+ timer.step("window, GL context");- if (const char* missing = gl::load([](const char* n) { return reinterpret_cast<gl::GenericProc>(SDL_GL_GetProcAddress(n)); })) {+ if (const char* missing = sharedContext_ ? nullptr // (loaded by the server)+ : gl::load([](const char* n) { return reinterpret_cast<gl::GenericProc>(SDL_GL_GetProcAddress(n)); })) {SDL_Log("Missing OpenGL function: %s", missing);return false;}@@ -182,6 +197,7 @@ bool App::init(const AppOptions& options) {SDL_Log("%s", err.c_str());return false;}+ timer.step("renderer");renderer_.setEnvironmentWake([] { wakeMainLoop(); }); // (PBR view: a new environment's lighting is ready)// Fill layers ask for Mesh Maps: the app bakes them on a worker thread (PanelFill.cpp, updateMeshMaps).setMeshMapRequester([this](Document&) { meshMapsWanted_ = true; });@@ -211,6 +227,7 @@ bool App::init(const AppOptions& options) {initUiConfig(headless || !options.demo.empty()); // ui.json (layout, shortcuts, UI scale); never for tests and videosuiScale_ = targetUiScale(); // the style follows in applyLook belowbuildUiFonts(uiScale_);+ timer.step("ImGui, settings, fonts");if (recording) {ImGui::GetCurrentContext()->TestEngineHookItems = true; // lets the demo find buttons by labelio.IniFilename = nullptr; // fixed, reproducible layout@@ -221,13 +238,16 @@ bool App::init(const AppOptions& options) {for (int i = 0; i < int(FileAction::Count); ++i) dialogRequests_[i] = {this, FileAction(i)};initBrushLibrary(); // before a project can bring its own brushes+ timer.step("look, brush library");initProject(); // before opening anything, so it lands in the recent filesinitActions(); // the default shortcuts and every command, for keys, help and the command paletteinitBrushSettings(); // each brush's own size, intensity, stroke and alpha (#78)loadBrushCurves(); // each brush's own pressure curve (Precision.cpp)initPhoneActions(); // File > Capture from phone (PanelPhone.cpp)initLibraryBrowser();+ timer.step("project, actions, library");runCommand("new_sphere");+ timer.step("new sphere");if (!options.openPath.empty()) openPath(options.openPath);if (!options.script.empty()) {std::vector<ScriptStatement> statements;@@ -256,6 +276,7 @@ bool App::init(const AppOptions& options) {ai::defaultGeneratorCommand(std::string(base) + cliName).c_str());}initHardwareProfile(); // the cached hardware profile, or calibration over the first frames (PanelPerformanceProfile.cpp)+ timer.step("hardware profile");return true;}@@ -280,11 +301,33 @@ void App::shutdown() {c = nullptr;}renderer_.shutdown();+ if (sharedWindow_) { // --ui-serve: the window, its context and SDL stay for the next script+ wintab_.close();+ return;+ }if (glContext_) SDL_GL_DestroyContext(glContext_);if (window_) SDL_DestroyWindow(window_);SDL_Quit();}+void App::glWindowAttributes() {+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);+ SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);+ SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);+ // No multisampling in the window: the 3D view antialiases in its own target (Renderer::drawView), so it can+ // draw with fewer samples while a huge scene moves (Preferences > Performance).+ SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);+ SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);+}++void App::shareWindow(SDL_Window* window, SDL_GLContext context) {+ sharedWindow_ = window;+ sharedContext_ = context;+}+int App::renderHeadless() {const int frames = std::max(1, options_.turntable);camera_.frame(options_.hideMesh && !doc_.splats.empty() ? doc_.splats.bounds(0.9f) : sceneBounds());@@ -332,7 +375,21 @@ int App::runUiTest(int w, int h, int fps) {const float dt = 1.0f / float(fps);int frames = 0;bool finalFrame = false;+ std::vector<std::future<void>> shotWrites;+ const bool timed = std::getenv("SF_STARTUP_TIMES") != nullptr; // the first frames' times (StartupTimer)+ double frameMs[4] = {}; // input and polls, ImGui backends' new frame, the UI, the scene+ auto lap = [last = std::chrono::steady_clock::now()](double& into) mutable {+ const auto now = std::chrono::steady_clock::now();+ into += std::chrono::duration<double, std::milli>(now - last).count();+ last = now;+ };while (frames < fps * 60 * 30) {+ if (timed && (frames == 1 || frames == 5 || frames == 20)) {+ std::fprintf(stderr, "frames to %2d: polls %.1f, new frame %.1f, UI %.1f, scene %.1f ms\n", frames, frameMs[0],+ frameMs[1], frameMs[2], frameMs[3]);+ frameMs[0] = frameMs[1] = frameMs[2] = frameMs[3] = 0.0;+ }+ lap(frameMs[3]);SDL_Event e;while (SDL_PollEvent(&e)) // the script provides all input (its SpaceMouse is a virtual joystick)if (e.type >= SDL_EVENT_JOYSTICK_AXIS_MOTION && e.type < SDL_EVENT_GAMEPAD_AXIS_MOTION) spaceMouseEvent(e);@@ -361,8 +418,10 @@ int App::runUiTest(int w, int h, int fps) {hardwareProfileFrameBegin(); // (only with SF_HW_PROFILE_IN_TESTS=1: tests use fixed settings)Demo::beginFrame();if (uiScaleDirty_) applyUiScale(); // (Preferences, a display-scale change: #87)+ lap(frameMs[0]);ImGui_ImplOpenGL3_NewFrame();ImGui_ImplSDL3_NewFrame();+ lap(frameMs[1]);keepImGuiOnPen();ImGuiIO& io = ImGui::GetIO();io.DisplaySize = ImVec2(float(w), float(h));@@ -375,6 +434,7 @@ int App::runUiTest(int w, int h, int fps) {if (ImGuiContext* g = ImGui::GetCurrentContext()) demo_->setFrameText(g->LogBuffer.c_str());ImGui::LogFinish();ImGui::Render();+ lap(frameMs[2]);updateMouseCursor();buildGhost(); // (the stroke preview, as run() draws it)@@ -401,9 +461,18 @@ int App::runUiTest(int w, int h, int fps) {img.rgba = rgba;for (size_t i = 3; i < img.rgba.size(); i += 4) img.rgba[i] = 255;demo_->setLastShot(img.rgba, w, h);- if (!img.savePng(shot, err)) SDL_Log("%s", err.c_str());- else SDL_Log("Screenshot %s", shot.c_str());- }+ // The PNG compresses on another thread while the script goes on (encoding was a fifth of the UI tests' time).+ if (shotWrites.size() >= 4) {+ shotWrites.front().get();+ shotWrites.erase(shotWrites.begin());+ }+ shotWrites.push_back(std::async(std::launch::async, [img = std::move(img), shot] {+ std::string error;+ if (!img.savePng(shot, error)) SDL_Log("%s", error.c_str());+ }));+ SDL_Log("Screenshot %s", shot.c_str());+ }+ for (auto& write : shotWrites) write.get(); // every screenshot on disk before the verdictfor (const auto& f : demo_->failures()) std::fprintf(stderr, "FAIL %s\n", f.c_str());if (demo_->failures().empty())std::printf("PASS %s (%d frames)\n", options_.uiTest.c_str(), frames);diff --git a/src/app/App.h b/src/app/App.hindex 5996c37a..293c38a7 100644--- a/src/app/App.h+++ b/src/app/App.h@@ -134,6 +134,10 @@ public:UiLayout& layout() { return layout_; }ThumbnailCache& thumbnails() { return thumbs_; }void shutdown();+ // The warm UI test server (sculptforge --ui-serve, UiServe.cpp): every App it makes draws in this one hidden+ // window and GL context, made once, instead of its own; shutdown() leaves them (and SDL) for the next script.+ static void shareWindow(SDL_Window* window, SDL_GLContext context);+ static void glWindowAttributes(); // OpenGL 3.3 core, before a window is madevoid requestUiScale(); // Preferences > Interface > UI scale changed: rebuilt live before the next frame (#87)enum class FileAction { OpenObj, SaveObj, ExportGlb, ExportLods, SaveBrush, LoadBrush, OpenImage, ExportUsd, SaveSplats, ExportUnreal, SaveProject, AppendPart, ComfyWorkflow, LoadLitSphere, InsertMesh, Count };@@ -236,6 +240,8 @@ private:SDL_Window* window_ = nullptr;SDL_GLContext glContext_ = nullptr;+ static inline SDL_Window* sharedWindow_ = nullptr; // shareWindow+ static inline SDL_GLContext sharedContext_ = nullptr;bool running_ = true;std::string iniPath_;diff --git a/src/app/Config.cpp b/src/app/Config.cppindex 5421a9cb..eba4f755 100644--- a/src/app/Config.cpp+++ b/src/app/Config.cpp@@ -9,6 +9,7 @@#include "app/Config.h"#include "app/App.h"+#include "app/UiServe.h"#include "app/CrashLog.h"#include "app/LookSchemes.h"#include "app/Palettes.h"@@ -1169,6 +1170,16 @@ void App::drawHelpConsoleUi() {ui::DrawHelp(*this); // last: on top of everything}+namespace {+char g_controlsFilter[96] = ""; // the Controls window's filter field+char g_hotkeysFilter[64] = ""; // Preferences > Hotkeys' filter field+} // namespace++void resetConfigGlobals() {+ g_controlsFilter[0] = g_hotkeysFilter[0] = 0;+ for (auto& r : g_requests) r = {};+}+void App::drawControlsWindow() {const float s = uiScale_;ImGui::SetNextWindowSize(ImVec2(560.0f * s, 640.0f * s), ImGuiCond_FirstUseEver);@@ -1183,7 +1194,7 @@ void App::drawControlsWindow() {drawInputStyleRow(ImGui::GetContentRegionAvail().x * 0.6f);ImGui::SameLine();ui::ActionButton(*this, "input.import_blender", ImVec2(-FLT_MIN, 0), ui::Look::IconLabel);- static char filter[96] = "";+ char (&filter)[96] = g_controlsFilter;ImGui::SetNextItemWidth(-FLT_MIN);ImGui::InputTextWithHint("##controls_filter", ICON_LC_SEARCH " Filter shortcuts and gestures", filter, sizeof filter);const std::string f = lower(filter);@@ -1395,7 +1406,7 @@ void drawInterfaceSection(App& app) {}void drawHotkeysSection(App& app) {- static char filter[64] = "";+ char (&filter)[64] = g_hotkeysFilter;ImGui::SetNextItemWidth(-FLT_MIN);ImGui::InputTextWithHint("##hotkeys_filter", tr(ICON_LC_SEARCH " Filter"), filter, sizeof filter);const std::string f = lower(filter);diff --git a/src/app/CrashLog.cpp b/src/app/CrashLog.cppindex 4c49cd55..4a4fcbb7 100644--- a/src/app/CrashLog.cpp+++ b/src/app/CrashLog.cpp@@ -53,6 +53,7 @@ SDL_LogOutputFunction g_sdlDefault = nullptr;void* g_sdlDefaultData = nullptr;// The last command, as a C string the crash handlers can read without allocating.char g_last[256] = "(none yet)";+std::string g_lastLogged; // action(): the last one writtenstd::atomic<bool> g_crashing{false};std::filesystem::path fsPath(const std::string& utf8) {@@ -208,6 +209,8 @@ void SDLCALL onSdlLog(void*, int category, SDL_LogPriority priority, const char*void start(const std::string& folder) {std::lock_guard lock(g_mutex);if (g_file) return;+ g_previousCrashed = false; // (the warm UI test server starts a log for every script)+ g_lastLogged.clear();std::string dir = folder;if (dir.empty()) {if (char* pref = SDL_GetPrefPath("SculptForge", "SculptForge")) {@@ -289,10 +292,9 @@ void note(const std::string& line) {void action(const std::string& what) {std::lock_guard lock(g_mutex);// Commands that run on every slider step would bury the rest: a repeat of the last one isn't written again.- static std::string last;std::snprintf(g_last, sizeof g_last, "%s", what.c_str());- if (what == last) return;- last = what;+ if (what == g_lastLogged) return;+ g_lastLogged = what;writeLocked(what);}diff --git a/src/app/Demo.cpp b/src/app/Demo.cppindex e249a761..d1b622d4 100644--- a/src/app/Demo.cpp+++ b/src/app/Demo.cpp@@ -3,6 +3,7 @@#include "app/Demo.h"#include "app/App.h"+#include "app/UiServe.h"#include "app/Ui.h"#include <imgui_internal.h>@@ -132,6 +133,8 @@ glm::vec3 onSphere(float latDeg, float lonDeg, float r = 1.0f) {} // namespace+void resetDemoGlobals() { items() = {}; }+void Demo::beginFrame() {items().byLabel.clear();items().offscreen.clear();diff --git a/src/app/Pickers.cpp b/src/app/Pickers.cppindex 726ac697..9e01f1d8 100644--- a/src/app/Pickers.cpp+++ b/src/app/Pickers.cpp@@ -7,6 +7,7 @@#include "app/Pickers.h"#include "app/App.h"+#include "app/UiServe.h"#include "app/AssetLibrary.h"#include "app/Ui.h"#include "core/BrushLibrary.h"@@ -260,6 +261,8 @@ const BrushAlpha& builtinAlpha(int i) {return all[size_t(std::clamp(i, 0, int(all.size()) - 1))];}+char paletteFilter[64] = ""; // the brush palette's filter field+} // namespace// ---------------------------------------------------------------------------@@ -619,7 +622,6 @@ void drawBrushGrid(App& app, GridMode mode) {const float tile = 64.0f * app.uiScale_;const std::vector<BrushItem> items = PickerUi::brushItems(app, true);- static char paletteFilter[64] = "";if (!popup) {ImGui::SetNextItemWidth(-SortMenuWidth());ImGui::InputTextWithHint("##brushfilter", ICON_LC_SEARCH " Filter brushes", paletteFilter,@@ -1533,4 +1535,10 @@ void App::drawColorWidget(float width) {ImGui::EndGroup();}+void resetPickerGlobals() {+ glow() = {};+ st() = {};+ paletteFilter[0] = 0;+}+} // namespace sfdiff --git a/src/app/Ui.cpp b/src/app/Ui.cppindex 80e29a91..92f151ed 100644--- a/src/app/Ui.cpp+++ b/src/app/Ui.cpp@@ -3,6 +3,7 @@#include "app/Ui.h"#include "app/App.h"+#include "app/UiServe.h"#include "app/I18n.h"#include "app/Shell.h"#include "app/Theme.h"@@ -708,6 +709,25 @@ int g_dragScrolls = 0;float g_dragScrollY = 0.0f;} // namespace+void resetUiGlobals() {+ g_help = {};+ g_shown.clear();+ g_popupInfo = true;+ g_nextShelf = ShelfNone, g_nextIndex = -1;+ g_anims.clear();+ g_reduceMotion = g_animating = g_animatingLast = false;+ g_seen.clear();+ g_rings.clear();+ g_noRing.clear();+ g_seenFrame = -1;+ g_ringsShown = g_liftsShown = g_liftsCounting = 0;+ g_dragScrollWindow = 0;+ g_dragScrollButton = ImGuiMouseButton_Left;+ g_dragScrollMoved = false;+ g_dragScrolls = 0;+ g_dragScrollY = 0.0f;+}+void DragScrollPanels() {ImGuiContext& g = *GImGui;const ImGuiIO& io = g.IO;diff --git a/src/app/UiServe.cpp b/src/app/UiServe.cppnew file mode 100644index 00000000..e7e43f6c--- /dev/null+++ b/src/app/UiServe.cpp@@ -0,0 +1,300 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// sculptforge --ui-serve: see UiServe.h.+#include "app/UiServe.h"+#include "app/App.h"+#include "app/I18n.h"+#include "ai/AiPolicy.h"+#include "ai/Http.h"+#include "core/Bake.h"+#include "core/LayerStack.h"+#include "core/LitSphere.h"+#include "core/Macro.h"+#include "core/MemoryGuard.h"+#include "core/Pack.h"+#include "core/ThreadPool.h"+#include "core/TierConfig.h"+#include "render/GL.h"+#include "render/Renderer.h"++#include <SDL3/SDL.h>++#include <chrono>+#include <cstdio>+#include <cstdlib>+#include <fcntl.h>+#include <filesystem>+#include <map>+#include <string>+#include <vector>++#if defined(_WIN32)+#include <io.h>+#define dup _dup+#define dup2 _dup2+#define close _close+#define fileno _fileno+#define fdopen _fdopen+#else+#include <unistd.h>+extern char** environ;+#endif++namespace sf {++namespace {++std::map<std::string, std::string> environment() {+ std::map<std::string, std::string> env;+#if defined(_WIN32)+ char** list = _environ;+#else+ char** list = environ;+#endif+ for (char** e = list; e && *e; ++e) {+ const std::string kv = *e;+ const size_t eq = kv.find('=', 1); // (Windows keeps "=C:=C:\..." entries)+ if (eq != std::string::npos) env[kv.substr(0, eq)] = kv.substr(eq + 1);+ }+ return env;+}++void setEnv(const std::string& name, const std::string& value) {+#if defined(_WIN32)+ _putenv_s(name.c_str(), value.c_str());+#else+ setenv(name.c_str(), value.c_str(), 1);+#endif+}++void unsetEnv(const std::string& name) {+#if defined(_WIN32)+ _putenv_s(name.c_str(), "");+#else+ unsetenv(name.c_str());+#endif+}++// The environment as it was at `saved` (scripts' `env`, the request's -e, isolateSettings' XDG folders).+void restoreEnvironment(const std::map<std::string, std::string>& saved) {+ for (const auto& [name, value] : environment()) {+ const auto it = saved.find(name);+ if (it == saved.end()) unsetEnv(name);+ else if (it->second != value) setEnv(name, it->second);+ }+ for (const auto& [name, value] : saved)+ if (!std::getenv(name.c_str())) setEnv(name, value);+}++// After each script: can this context still draw offscreen? A script that makes the GPU drop the device (WSL's+// d3d12: "Removing Device") leaves the server unable to run any other; it then quits and is started again.+bool glHealthy() {+ while (gl::GetError() != GL_NO_ERROR) {+ }+ GLuint fbo = 0, rb = 0;+ gl::GenFramebuffers(1, &fbo);+ gl::GenRenderbuffers(1, &rb);+ gl::BindRenderbuffer(GL_RENDERBUFFER, rb);+ gl::RenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, 4, 4);+ gl::BindFramebuffer(GL_FRAMEBUFFER, fbo);+ gl::FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rb);+ const bool complete = gl::CheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;+ gl::BindFramebuffer(GL_FRAMEBUFFER, 0);+ gl::DeleteFramebuffers(1, &fbo);+ gl::DeleteRenderbuffers(1, &rb);+ return complete && gl::GetError() == GL_NO_ERROR;+}++// Globals a run leaves set, back to a new process's values. App::init sets the rest again (the AI policy, the+// packs and macro folders, the bake listener and mesh map requester point at the App, hardware tier...).+void resetGlobals() {+ setBakeListener({});+ setMeshMapRequester({});+ setCurrentTierConfig(TierConfig{});+ setAvailableMemoryOverride(0);+ setLanguage(Language::English);+ setPacksSafeMode(false);+ setPacksRoot("");+ setUserMacroDir("");+ ai::setAiForcedOff(false);+ ai::setAiEnabled(true);+ ai::resetHttpRequestCount();+ clearCustomLitSpheres();+ int joysticks = 0; // a script's virtual SpaceMouse (UiTest.cpp), which a process's end would take away+ if (SDL_JoystickID* ids = SDL_GetJoysticks(&joysticks)) {+ for (int i = 0; i < joysticks; ++i)+ if (SDL_IsJoystickVirtual(ids[i])) SDL_DetachVirtualJoystick(ids[i]);+ SDL_free(ids);+ }+ resetConfigGlobals();+ resetDemoGlobals();+ resetPickerGlobals();+ ui::resetUiGlobals();+ SDL_FlushEvents(SDL_EVENT_FIRST, SDL_EVENT_LAST);+ SDL_ClearClipboardData(); // a new process owns no clipboard (CMakeLists: clipboard scripts run serially)+ std::srand(1);+}++// A file descriptor programs the scripts start (fake tools, a QRemesh worker) don't inherit.+int privateDup(int fd) {+ const int copy = dup(fd);+#if !defined(_WIN32)+ if (copy >= 0) fcntl(copy, F_SETFD, FD_CLOEXEC);+#endif+ return copy;+}++bool readLine(FILE* in, std::string& line) {+ line.clear();+ for (int c; (c = std::fgetc(in)) != EOF;) {+ if (c == '\n') return true;+ line += char(c);+ }+ return !line.empty();+}++std::vector<std::string> splitTabs(const std::string& line) {+ std::vector<std::string> out;+ size_t at = 0;+ for (;;) {+ const size_t tab = line.find('\t', at);+ out.push_back(line.substr(at, tab == std::string::npos ? std::string::npos : tab - at));+ if (tab == std::string::npos) return out;+ at = tab + 1;+ }+}++} // namespace++void syncSdlEnvironment() {+ SDL_Environment* sdl = SDL_GetEnvironment();+ const auto now = environment();+ if (char** vars = SDL_GetEnvironmentVariables(sdl)) {+ for (char** v = vars; *v; ++v) {+ const std::string kv = *v;+ const size_t eq = kv.find('=', 1);+ if (eq != std::string::npos && !now.count(kv.substr(0, eq))) SDL_UnsetEnvironmentVariable(sdl, kv.substr(0, eq).c_str());+ }+ SDL_free(vars);+ }+ for (const auto& [name, value] : now) SDL_SetEnvironmentVariable(sdl, name.c_str(), value.c_str(), true);+}++int runUiServer(int argc, char** argv, int (*runOne)(int argc, char** argv)) {+ int width = 1600, height = 960;+ for (int i = 1; i + 1 < argc; ++i) {+ const std::string a = argv[i];+ if (a == "--size") std::sscanf(argv[++i], "%dx%d", &width, &height);+ // Servers side by side share the CPU: each pool spinning on every thread would only slow them all down.+ // (Results don't depend on the thread count: core/ThreadPool.h.)+ else if (a == "--threads") ThreadPool::setSharedThreadCount(std::atoi(argv[++i]));+ }+ // As App::init sets them for a UI test, before SDL starts.+ SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");+ SDL_SetHint(SDL_HINT_PEN_MOUSE_EVENTS, "0");+ SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");+ if (!SDL_Init(SDL_INIT_VIDEO)) {+ std::fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());+ return 1;+ }+ App::glWindowAttributes();+ SDL_Window* window = SDL_CreateWindow("SculptForge", width, height,+ SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_HIDDEN);+ SDL_GLContext context = window ? SDL_GL_CreateContext(window) : nullptr;+ if (!context) {+ std::fprintf(stderr, "No window or OpenGL 3.3 context: %s\n", SDL_GetError());+ return 1;+ }+ SDL_GL_MakeCurrent(window, context);+ if (const char* missing = gl::load([](const char* n) { return reinterpret_cast<gl::GenericProc>(SDL_GL_GetProcAddress(n)); })) {+ std::fprintf(stderr, "Missing OpenGL function: %s\n", missing);+ return 1;+ }+ App::shareWindow(window, context);+ Renderer::keepPrograms(true); // shaders compile once, not for every script++ // Requests come on the standard input we were given and answers go to its standard output, on private copies:+ // the scripts' programs get an empty input, and each script's own output goes to its log.+ std::fflush(stdout);+ const int answerFd = privateDup(fileno(stdout)), savedErr = privateDup(fileno(stderr)), requestFd = privateDup(fileno(stdin));+ FILE* answers = fdopen(answerFd, "w");+ FILE* requests = fdopen(requestFd, "r");+ if (!answers || !requests) return 1;+#if defined(_WIN32)+ const int empty = _open("NUL", _O_RDONLY);+#else+ const int empty = open("/dev/null", O_RDONLY);+#endif+ if (empty >= 0) {+ dup2(empty, fileno(stdin));+ close(empty);+ }+ const auto savedEnv = environment();+ std::error_code ec;+ const auto savedDir = std::filesystem::current_path(ec);+ std::fprintf(answers, "ready\n");+ std::fflush(answers);++ std::string line;+ while (readLine(requests, line)) {+ if (!line.empty() && line.back() == '\r') line.pop_back();+ const auto f = splitTabs(line);+ if (f[0] == "quit") break;+ if (f[0] != "run" || f.size() < 3) {+ std::fprintf(answers, "error\tunknown request\n");+ std::fflush(answers);+ continue;+ }+ std::vector<std::string> args{"sculptforge"};+ for (size_t i = 2; i < f.size(); ++i) {+ if (f[i] == "-e" && i + 1 < f.size()) {+ const std::string& kv = f[++i];+ const size_t eq = kv.find('=');+ if (eq != std::string::npos) setEnv(kv.substr(0, eq), kv.substr(eq + 1));+ } else if (f[i] == "-C" && i + 1 < f.size()) {+ std::filesystem::current_path(std::filesystem::path(std::u8string(f[i + 1].begin(), f[i + 1].end())), ec);+ ++i;+ } else+ args.push_back(f[i]);+ }+ std::vector<char*> argvOne;+ for (auto& a : args) argvOne.push_back(a.data());+ argvOne.push_back(nullptr);++ resetGlobals();+ std::fflush(stdout);+ std::fflush(stderr);+#if defined(_WIN32)+ const int log = _open(f[1].c_str(), _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY, 0600);+#else+ const int log = open(f[1].c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);+#endif+ if (log >= 0) {+ dup2(log, fileno(stdout));+ dup2(log, fileno(stderr));+ close(log);+ }+ const auto start = std::chrono::steady_clock::now();+ const int code = runOne(int(args.size()), argvOne.data());+ const double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count();+ std::fflush(stdout);+ std::fflush(stderr);+ dup2(savedErr, fileno(stdout)); // (not our answers: stray output from a straggling thread goes to stderr)+ dup2(savedErr, fileno(stderr));+ restoreEnvironment(savedEnv);+ std::filesystem::current_path(savedDir, ec);+ const bool healthy = glHealthy();+ std::fprintf(answers, "done\t%d\t%.0f%s\n", code, ms, healthy ? "" : "\tlost");+ std::fflush(answers);+ if (!healthy) break;+ }+ App::shareWindow(nullptr, nullptr);+ Renderer::keepPrograms(false);+ SDL_GL_DestroyContext(context);+ SDL_DestroyWindow(window);+ SDL_Quit();+ return 0;+}++} // namespace sfdiff --git a/src/app/UiServe.h b/src/app/UiServe.hnew file mode 100644index 00000000..26ad80cd--- /dev/null+++ b/src/app/UiServe.h@@ -0,0 +1,34 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// The warm UI test server: sculptforge --ui-serve [--size WxH] [--threads N] makes its hidden window, OpenGL context and SDL once,+// then runs UI test scripts one after another, each a whole run of the app (main's own code, a new App, its own+// isolated settings folder) in that window. Starting a process, SDL, a window and a GL context for every script is+// most of a short UI test's time. tools/test/warm_ui.py keeps a pool of them for ctest.+//+// Requests come on standard input only (no port, no socket: nothing another user or program can reach), one per+// line, fields separated by tabs:+// run <TAB> LOG [<TAB> -e <TAB> NAME=VALUE]... [<TAB> -C <TAB> DIR] <TAB> ARGS... e.g. --ui-test x.sfui --ui-out d+// quit+// The script's standard output and error go to LOG; the answer, on standard output, is "done <TAB> EXIT <TAB> MS".+// The server starts with "ready". Between scripts it puts back what a script may change outside its App: the+// environment, the working directory, the clipboard, queued events, rand()'s seed and the globals below. A script+// that changes something else for good says `# cold` and runs in a process of its own (warm_ui.py).+#pragma once++namespace sf {++int runUiServer(int argc, char** argv, int (*runOne)(int argc, char** argv));+// SDL keeps a copy of the environment, made when it starts (SDL_getenv, SDL_GetPrefPath's XDG folders): App::init+// makes the server's copy what a new process's SDL_Init would see now (isolateSettings' folders, the test's -e).+void syncSdlEnvironment();++// What the app keeps outside its App between runs, put back as a new process has it.+void resetConfigGlobals(); // Config.cpp: filter fields, pending file dialogs+void resetDemoGlobals(); // Demo.cpp: the items drawn last frame+void resetPickerGlobals(); // Pickers.cpp: recent brushes, the palette filter+namespace ui {+void resetUiGlobals(); // Ui.cpp: hover rings, animations, drag scrolling+}++} // namespace sfdiff --git a/src/app/main.cpp b/src/app/main.cppindex 6092afdc..2c586496 100644--- a/src/app/main.cpp+++ b/src/app/main.cpp@@ -4,6 +4,7 @@#include "app/Bench.h"#include "app/Calibration.h"#include "app/CrashLog.h"+#include "app/UiServe.h"#include "ai/AiPolicy.h"#include "core/FocusDetail.h"#include "core/Pack.h"@@ -15,6 +16,7 @@#include <cstdio>#include <cstdlib>#include <filesystem>+#include <memory>#include <stdexcept>#include <string>@@ -46,6 +48,7 @@ void usage() {" --supersample N Render recordings N times larger and downsample (sharper, allows zoom-ins)\n"" --ui-test FILE.sfui Run a UI test script headless and exit (0 = pass); settings are isolated\n"" --ui-out DIR Where UI tests save screenshots\n"+ " --ui-serve [--threads N] Warm UI test server: one window, then one test per stdin line (tools/test/warm_ui.py)\n"" --fullscreen Start filling the screen (F11 toggles)\n"" --no-ai Start with every AI feature off and locked (as SF_AI=off)\n"" --no-packs Safe mode: start without any installed pack (Library > Packs)\n"@@ -147,7 +150,8 @@ int finishIsolated(int code) {std::abort();}-int main(int argc, char** argv) {+// One run of the app: the whole of main, also each script of the warm UI test server (--ui-serve, UiServe.cpp).+static int runMain(int argc, char** argv) {sf::AppOptions options;std::string logDir, crashTest;bool sizeGiven = false;@@ -221,6 +225,7 @@ int main(int argc, char** argv) {return 1;}}+ g_isolatedDir.clear(); // (the warm server's last script's folder)if (automatedRun()) isolateSettings();if (bench.run) return finishIsolated(sf::runAppBenchmark(bench));if (calibrate) return finishIsolated(sf::runCalibrateCommandLine(calibrateQuick, calibrateSave));@@ -249,14 +254,25 @@ int main(int argc, char** argv) {}sf::crashlog::start(logDir); // after isolateSettings: a UI test logs into its own folderif (!crashTest.empty()) crashOnPurpose(crashTest);- sf::App app;- if (!app.init(options)) {- app.shutdown();+ auto app = std::make_unique<sf::App>();+ if (!app->init(options)) {+ app->shutdown();sf::crashlog::stop();return 1;}- int code = app.run();- app.shutdown();+ const auto runStart = std::chrono::steady_clock::now();+ int code = app->run();+ const auto runEnd = std::chrono::steady_clock::now();+ app->shutdown();+ app.reset();+ if (std::getenv("SF_STARTUP_TIMES"))+ std::fprintf(stderr, "run %.1f ms, shutdown %.1f ms\n", std::chrono::duration<double, std::milli>(runEnd - runStart).count(),+ std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - runEnd).count());sf::crashlog::stop();return finishIsolated(code);}++int main(int argc, char** argv) {+ if (argc >= 2 && std::string(argv[1]) == "--ui-serve") return sf::runUiServer(argc - 1, argv + 1, runMain);+ return runMain(argc, argv);+}diff --git a/src/core/LitSphere.cpp b/src/core/LitSphere.cppindex 0d9edc8e..19d3c2c2 100644--- a/src/core/LitSphere.cpp+++ b/src/core/LitSphere.cpp@@ -224,6 +224,10 @@ bool isLitSphereListed(int index) {return (index >= 0 && index < kStyleCount) || (isCustomLitSphere(index) && !customs()[size_t(index - kStyleCount)].removed);}const CustomLitSphere* customLitSphere(int index) { return isCustomLitSphere(index) ? &customs()[size_t(index - kStyleCount)] : nullptr; }+void clearCustomLitSpheres() {+ customs().clear();+ g_revision = 1;+}uint64_t litsphereRevision() { return g_revision; }std::string litsphereKey(int index) {diff --git a/src/core/LitSphere.h b/src/core/LitSphere.hindex 611fd5cc..1424376b 100644--- a/src/core/LitSphere.h+++ b/src/core/LitSphere.h@@ -54,6 +54,7 @@ bool isCustomLitSphere(int index); // also true for a removed one (its index sta// Shown in the material lists: a built-in, or a custom Lit Sphere that wasn't removed.bool isLitSphereListed(int index);const CustomLitSphere* customLitSphere(int index); // null for built-ins and bad indices+void clearCustomLitSpheres(); // none loaded, as at start (the warm UI test server, between scripts)// Changes whenever a custom Lit Sphere is added or its pixels replaced (renderers upload again).uint64_t litsphereRevision();// Name plus, for custom Lit Spheres, the pixel hash: a cache key that changes with the image.diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cppindex 546a9c2a..b615ba8d 100644--- a/src/render/Renderer.cpp+++ b/src/render/Renderer.cpp@@ -20,6 +20,7 @@#include <bit>#include <chrono>#include <thread>+#include <unordered_map>#include <cstdlib>#include <limits>#include <vector>@@ -774,8 +775,33 @@ GLuint compile(GLenum type, const char* src, std::string& error, const char* prereturn s;}+// Renderer::keepPrograms: every program linked, by its sources, kept for the next Renderer in the same GL context.+bool g_keepPrograms = false;+std::unordered_map<std::string, GLuint> g_keptPrograms;+int g_keptVertexIdBase = -1; // checkVertexIdBase's answer (-1: not asked yet)+std::vector<GLuint> g_keptLitSpheres; // the built-in lit spheres' textures++GLuint linkNew(const char* vs, const char* fs, std::string& error, const char* fsPrefix, const char* gs);+// `fsPrefix` goes before the fragment source (the #version line and defines); `gs` is an optional geometry shader.GLuint link(const char* vs, const char* fs, std::string& error, const char* fsPrefix = nullptr, const char* gs = nullptr) {+ if (!g_keepPrograms) return linkNew(vs, fs, error, fsPrefix, gs);+ std::string key = std::string(vs) + '\x1f' + (fsPrefix ? fsPrefix : "") + '\x1f' + fs + '\x1f' + (gs ? gs : "");+ if (const auto it = g_keptPrograms.find(key); it != g_keptPrograms.end()) return it->second;+ const GLuint p = linkNew(vs, fs, error, fsPrefix, gs);+ if (p) g_keptPrograms.emplace(std::move(key), p);+ return p;+}++void deleteProgram(GLuint p) {+ if (!p) return;+ if (g_keepPrograms)+ for (const auto& [key, kept] : g_keptPrograms)+ if (kept == p) return;+ gl::DeleteProgram(p);+}++GLuint linkNew(const char* vs, const char* fs, std::string& error, const char* fsPrefix, const char* gs) {GLuint v = compile(GL_VERTEX_SHADER, vs, error);if (!v) return 0;GLuint f = compile(GL_FRAGMENT_SHADER, fs, error, fsPrefix);@@ -824,6 +850,17 @@ glm::vec3 facegroupColor(uint32_t id) {return val * glm::mix(glm::vec3(1.0f), rgb, sat);}+void Renderer::keepPrograms(bool keep) {+ g_keepPrograms = keep;+ if (!keep) {+ for (const auto& [key, p] : g_keptPrograms) gl::DeleteProgram(p);+ g_keptPrograms.clear();+ g_keptVertexIdBase = -1;+ if (!g_keptLitSpheres.empty()) gl::DeleteTextures(GLsizei(g_keptLitSpheres.size()), g_keptLitSpheres.data());+ g_keptLitSpheres.clear();+ }+}+bool Renderer::init(std::string& error) {for (int f : {0, kPaint}) // the common ones up front, so a broken driver fails here rather than mid-sessionif (!meshProgram(f)) {@@ -850,17 +887,25 @@ bool Renderer::init(std::string& error) {gl::GenVertexArrays(1, &emptyVao_);- // Built-in lit spheres, generated once (256 KB each); custom ones upload in syncLit Spheres.- litsphereTextures_.assign(size_t(builtinLitSphereCount()), 0);- litsphereHashes_.assign(size_t(builtinLitSphereCount()), 0);- gl::GenTextures(GLsizei(litsphereTextures_.size()), litsphereTextures_.data());- for (int i = 0; i < builtinLitSphereCount(); ++i) {- constexpr int kSize = 256;- uploadLitSphere(litsphereTextures_[size_t(i)], kSize, generateLitSphere(i, kSize));+ // Built-in lit spheres, generated once (256 KB each, together on the thread pool) and uploaded (with keepPrograms,+ // kept for the next Renderer: an upload is slow on some drivers, WSL's d3d12); custom ones upload in syncLitSpheres.+ if (g_keepPrograms && !g_keptLitSpheres.empty()) litsphereTextures_ = g_keptLitSpheres;+ else {+ constexpr int kLitSphereSize = 256;+ std::vector<std::vector<uint8_t>> pixels(static_cast<size_t>(builtinLitSphereCount()));+ poolFor(pixels.size(), [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) pixels[i] = generateLitSphere(int(i), kLitSphereSize);+ }, 1, 2);+ litsphereTextures_.assign(pixels.size(), 0);+ gl::GenTextures(GLsizei(litsphereTextures_.size()), litsphereTextures_.data());+ for (size_t i = 0; i < pixels.size(); ++i) uploadLitSphere(litsphereTextures_[i], kLitSphereSize, pixels[i]);+ if (g_keepPrograms) g_keptLitSpheres = litsphereTextures_;}+ litsphereHashes_.assign(size_t(builtinLitSphereCount()), 0);syncLitSpheres();// SF_NO_VERTEXID_BASE=1 takes the other path, to check it on drivers that don't need it.- vertexIdHasBase_ = checkVertexIdBase() && !std::getenv("SF_NO_VERTEXID_BASE");+ if (g_keptVertexIdBase < 0 || !g_keepPrograms) g_keptVertexIdBase = checkVertexIdBase() ? 1 : 0;+ vertexIdHasBase_ = g_keptVertexIdBase == 1 && !std::getenv("SF_NO_VERTEXID_BASE");return true;}@@ -978,7 +1023,7 @@ bool Renderer::checkVertexIdBase() {gl::BindFramebuffer(GL_FRAMEBUFFER, 0);gl::DeleteFramebuffers(1, &fbo);gl::DeleteRenderbuffers(1, &rb);- gl::DeleteProgram(prog);+ deleteProgram(prog);for (int i = 0; i < 16 && gl::GetError() != GL_NO_ERROR; ++i) {}return id == 1002;@@ -1109,23 +1154,24 @@ void Renderer::shutdown() {}slots_.clear();drawList_.clear();- if (!litsphereTextures_.empty()) gl::DeleteTextures(GLsizei(litsphereTextures_.size()), litsphereTextures_.data());+ for (GLuint& t : litsphereTextures_) // (not the kept built-ins: keepPrograms)+ if (std::find(g_keptLitSpheres.begin(), g_keptLitSpheres.end(), t) == g_keptLitSpheres.end()) gl::DeleteTextures(1, &t);litsphereTextures_.clear();litsphereHashes_.clear();litsphereRevision_ = 0;for (GLuint* t : {&envTexture_, &brdfTexture_})if (*t) gl::DeleteTextures(1, t);envIndex_ = envFailedIndex_ = -1;- if (envBgProgram_) gl::DeleteProgram(envBgProgram_);+ deleteProgram(envBgProgram_);envBgProgram_ = 0;gl::DeleteVertexArrays(1, &emptyVao_);for (MeshProgram& p : programs_) {- if (p.id) gl::DeleteProgram(p.id);+ deleteProgram(p.id);p = MeshProgram();}- gl::DeleteProgram(wireProgram_);- gl::DeleteProgram(bgProgram_);- gl::DeleteProgram(splatProgram_);+ deleteProgram(wireProgram_);+ deleteProgram(bgProgram_);+ deleteProgram(splatProgram_);gl::DeleteBuffers(1, &splatData_);gl::DeleteBuffers(1, &splatOrder_);gl::DeleteTextures(1, &splatTexture_);diff --git a/src/render/Renderer.h b/src/render/Renderer.hindex 6b6aa3ac..60119e2c 100644--- a/src/render/Renderer.h+++ b/src/render/Renderer.h@@ -142,6 +142,9 @@ class Renderer {public:bool init(std::string& error);void shutdown();+ // The warm UI test server (app/UiServe.h): shaders linked once and kept for the next Renderer in the same GL+ // context (and the driver check init makes); false frees them.+ static void keepPrograms(bool keep);// Makes `scene` what draw() shows, in order: uploads whatever each visible// mesh marked dirty and clears its dirty state (a new slot uploads// everything), and frees the slots of ids no longer listed.diff --git a/tools/test/warm_ui.py b/tools/test/warm_ui.pynew file mode 100644index 00000000..61569067--- /dev/null+++ b/tools/test/warm_ui.py@@ -0,0 +1,541 @@+#!/usr/bin/env python3+# SculptForge - open source digital sculpting+# SPDX-License-Identifier: GPL-3.0-or-later+"""Warm UI tests: a pool of `sculptforge --ui-serve` servers (src/app/UiServe.h) runs tests/ui/*.sfui scripts+without starting a process, SDL, a window and an OpenGL context for each one.++ warm_ui.py start --dir D --app APP start the pool in the background (ctest fixture ui_warm)+ warm_ui.py client --dir D --app APP -- ARGS... run one test through the pool, as `APP ARGS...` would run it+ cold: same output, same exit code (each ctest ui_* test)+ warm_ui.py stop --dir D stop the pool (ctest fixture cleanup)+ warm_ui.py suite --build B [--cold] [--repeat K] [--shuffle] [--json F]+ every ui test ctest knows, through a pool of its own (or cold), with+ wall time and screenshot hashes: to compare warm with cold+ warm_ui.py compare COLD.json WARM.json verdicts and screenshots of WARM's runs that differ from what every run+ in COLD agrees on++The pool talks to its servers over their standard input and output, and clients reach the pool through files in+D (made private to this user, 0700): no port, no socket. A server that crashes or hangs is restarted and its script+rerun cold, once. A script with a `# cold` line, or SF_UI_COLD=1 in the environment, always runs cold. Servers:+SF_UI_SERVERS or half the CPU threads.+"""+import argparse+import hashlib+import json+import os+import queue+import random+import re+import shutil+import signal+import subprocess+import sys+import threading+import time+import uuid++TIMEOUT = 290 # seconds a script may take (ctest gives a ui test 300)+IDLE_EXIT = 900 # a pool nobody used for this long stops (a ctest run that never reached the cleanup)+++def servers_wanted():+ n = os.environ.get("SF_UI_SERVERS")+ return max(1, int(n)) if n else max(1, (os.cpu_count() or 2) // 2)+++def script_of(args):+ return args[args.index("--ui-test") + 1] if "--ui-test" in args[:-1] else None+++def wants_cold(args):+ """Runs that stay cold: anything but a UI test script, scripts marked `# cold`, and SF_UI_COLD=1."""+ if os.environ.get("SF_UI_COLD", "0") not in ("", "0"):+ return True+ script = script_of(args)+ if not script:+ return True+ try:+ with open(script, encoding="utf-8") as f:+ return any(re.match(r"\s*#\s*cold\b", line) for line in f)+ except OSError:+ return True+++def run_cold(app, args, env, cwd, timeout=TIMEOUT):+ """(exit code, output) of one cold run, as ctest would see it."""+ try:+ p = subprocess.run([app] + args, env=env, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,+ stdin=subprocess.DEVNULL, timeout=timeout)+ return p.returncode, p.stdout.decode("utf-8", "replace")+ except subprocess.TimeoutExpired as e:+ return 124, (e.stdout or b"").decode("utf-8", "replace") + f"\nwarm_ui: cold run timed out after {timeout} s\n"+++class Server:+ """One `APP --ui-serve`; run() sends it a script and waits for the answer."""++ def __init__(self, app, logdir, index, threads):+ self.app, self.index, self.threads = app, index, threads+ self.errlog = os.path.join(logdir, f"server-{index}.log")+ self.proc = None+ self.lines = None+ self.runs = 0++ def start(self):+ self.lines = queue.Queue()+ err = open(self.errlog, "ab")+ self.proc = subprocess.Popen([self.app, "--ui-serve", "--threads", str(self.threads)], stdin=subprocess.PIPE, stdout=subprocess.PIPE,+ stderr=err, bufsize=0)+ err.close()+ self.runs = 0+ out, lines = self.proc.stdout, self.lines++ def read():+ for raw in iter(out.readline, b""):+ lines.put(raw.decode("utf-8", "replace").rstrip("\r\n"))+ lines.put(None) # the server is gone++ threading.Thread(target=read, daemon=True).start()+ if self._answer(120) == "ready":+ return True+ self.stop(kill=True)+ return False++ def start_retrying(self, tries=4):+ """start(), again after a pause when it fails: an OpenGL context can fail to come up while many others start+ (WSL's d3d12: "Could not find a valid EGL device")."""+ for i in range(tries):+ if self.start():+ return True+ time.sleep(0.5 + random.random() * (i + 1))+ return False++ def _answer(self, timeout):+ try:+ return self.lines.get(timeout=timeout)+ except queue.Empty:+ return None++ def stop(self, kill=False):+ if not self.proc:+ return+ try:+ if kill:+ self.proc.kill()+ else:+ self.proc.stdin.write(b"quit\n")+ self.proc.stdin.flush()+ self.proc.wait(timeout=10)+ except (OSError, subprocess.TimeoutExpired):+ self.proc.kill()+ self.proc = None++ def run(self, args, env_changes, cwd, log, timeout):+ """Exit code, or None when the server crashed, hung or lost its GPU device during a failed script (it is+ then gone)."""+ if not self.proc and not self.start_retrying():+ return None+ fields = ["run", log]+ for k, v in env_changes.items():+ fields += ["-e", f"{k}={v}"]+ fields += ["-C", cwd] + args+ if any("\t" in f or "\n" in f for f in fields):+ return None+ try:+ self.proc.stdin.write(("\t".join(fields) + "\n").encode("utf-8"))+ self.proc.stdin.flush()+ except OSError:+ self.stop(kill=True)+ return None+ answer = self._answer(timeout)+ if not answer or not answer.startswith("done\t"):+ self.stop(kill=True)+ return None+ self.runs += 1+ fields = answer.split("\t")+ code = int(fields[1])+ if "lost" in fields[3:]: # the GPU dropped its device: the server quit; a failure may be that, not the script+ self.stop(kill=True)+ return None if code != 0 else code+ return code+++class Pool:+ """N servers; run() takes a free one (blocking) and falls back to a cold run when a server fails."""++ def __init__(self, app, workdir, n):+ self.app, self.workdir = app, workdir+ os.makedirs(os.path.join(workdir, "logs"), exist_ok=True)+ self.free = queue.Queue()+ # The CPU threads shared out: each server's brush thread pool gets its part (at least 2).+ threads = max(2, (os.cpu_count() or 2) // n)+ self.servers = [Server(app, workdir, i, threads) for i in range(n)]+ for s in self.servers:+ self.free.put(s)+ self.base_env = dict(os.environ)+ self.cold_reruns = 0++ def warm_up(self):+ threads = [threading.Thread(target=s.start_retrying) for s in self.servers]+ for t in threads:+ t.start()+ for t in threads:+ t.join()++ def env_changes(self, env):+ return {k: v for k, v in env.items() if self.base_env.get(k) != v}++ def run(self, args, env, cwd, timeout=TIMEOUT):+ """(exit code, output, how): how is warm, cold (asked for) or rerun (a server crashed or hung)."""+ if wants_cold(args) or env.get("SF_UI_COLD", "0") not in ("", "0"):+ return run_cold(self.app, args, env, cwd, timeout) + ("cold",)+ server = self.free.get()+ log = os.path.join(self.workdir, "logs", uuid.uuid4().hex + ".log")+ try:+ code = server.run(args, self.env_changes(env), cwd, log, timeout)+ finally:+ self.free.put(server)+ output = ""+ try:+ with open(log, encoding="utf-8", errors="replace") as f:+ output = f.read()+ os.remove(log)+ except OSError:+ pass+ if code is not None:+ return code, output, "warm"+ self.cold_reruns += 1+ code, cold = run_cold(self.app, args, env, cwd, timeout)+ note = f"warm_ui: the warm server failed on {script_of(args)} (crash, timeout or lost GPU device); rerun cold:\n"+ return code, output + "\n" + note + cold, "rerun"++ def stop(self):+ for s in self.servers:+ s.stop()+++# --- the pool as a background process, for ctest -------------------------------------------------------------------++def private_dir(d):+ os.makedirs(d, mode=0o700, exist_ok=True)+ if os.name != "nt":+ os.chmod(d, 0o700)+ for sub in ("queue", "done"):+ os.makedirs(os.path.join(d, sub), mode=0o700, exist_ok=True)+++def pid_alive(pid):+ if os.name == "nt": # (os.kill would end it)+ import ctypes+ h = ctypes.windll.kernel32.OpenProcess(0x1000, False, pid) # PROCESS_QUERY_LIMITED_INFORMATION+ if not h:+ return False+ code = ctypes.c_ulong()+ ctypes.windll.kernel32.GetExitCodeProcess(h, ctypes.byref(code))+ ctypes.windll.kernel32.CloseHandle(h)+ return code.value == 259 # STILL_ACTIVE+ try:+ os.kill(pid, 0)+ return True+ except OSError:+ return False+++def pool_alive(d):+ try:+ with open(os.path.join(d, "pool.pid")) as f:+ return pid_alive(int(f.read()))+ except (OSError, ValueError):+ return False+++def cmd_start(a):+ if os.environ.get("SF_UI_COLD", "0") not in ("", "0"):+ print("SF_UI_COLD=1: UI tests run cold")+ return 0+ d = os.path.abspath(a.dir)+ cmd_stop(a, quiet=True)+ shutil.rmtree(d, ignore_errors=True)+ private_dir(d)+ log = open(os.path.join(d, "pool.log"), "ab")+ kw = {"start_new_session": True} if os.name != "nt" else {"creationflags": 0x00000008} # DETACHED_PROCESS+ subprocess.Popen([sys.executable, os.path.abspath(__file__), "serve", "--dir", d, "--app", a.app],+ stdin=subprocess.DEVNULL, stdout=log, stderr=log, close_fds=True, **kw)+ for _ in range(100): # until its pid file is there (the servers warm up while the first tests queue)+ if pool_alive(d):+ print(f"warm UI pool started: {servers_wanted()} servers ({d})")+ return 0+ time.sleep(0.05)+ print("warm UI pool did not start; UI tests run cold")+ return 0+++def cmd_serve(a):+ d = a.dir+ pool = Pool(a.app, d, servers_wanted())+ with open(os.path.join(d, "pool.pid.tmp"), "w") as f:+ f.write(str(os.getpid()))+ os.replace(os.path.join(d, "pool.pid.tmp"), os.path.join(d, "pool.pid"))+ threading.Thread(target=pool.warm_up, daemon=True).start()+ jobs = queue.Queue()+ taken = set()+ stop = threading.Event()++ def work():+ while not stop.is_set():+ try:+ name = jobs.get(timeout=0.2)+ except queue.Empty:+ continue+ path = os.path.join(d, "queue", name)+ try:+ with open(path, encoding="utf-8") as f:+ req = json.load(f)+ os.remove(path)+ code, output, how = pool.run(req["args"], req["env"], req["cwd"], req.get("timeout", TIMEOUT))+ except Exception as e: # a broken request: answer it rather than leave its client waiting+ code, output, how = 1, f"warm_ui: {e}\n", "error"+ tmp = os.path.join(d, "done", name + ".tmp")+ with open(tmp, "w", encoding="utf-8") as f:+ json.dump({"code": code, "output": output, "how": how}, f)+ os.replace(tmp, os.path.join(d, "done", name))++ workers = [threading.Thread(target=work, daemon=True) for _ in pool.servers]+ for w in workers:+ w.start()+ last = time.time()+ while not os.path.exists(os.path.join(d, "stop")) and time.time() - last < IDLE_EXIT:+ names = sorted(n for n in os.listdir(os.path.join(d, "queue")) if n.endswith(".json") and n not in taken)+ for n in names:+ taken.add(n)+ jobs.put(n)+ last = time.time()+ time.sleep(0.01)+ stop.set()+ for w in workers:+ w.join(timeout=TIMEOUT)+ pool.stop()+ try:+ os.remove(os.path.join(d, "pool.pid"))+ except OSError:+ pass+ print(f"warm UI pool stopped; {pool.cold_reruns} cold reruns")+ return 0+++def cmd_client(a):+ args = a.args[1:] if a.args[:1] == ["--"] else a.args+ d = os.path.abspath(a.dir)+ if wants_cold(args) or not pool_alive(d):+ code, output = run_cold(a.app, args, dict(os.environ), os.getcwd())+ sys.stdout.write(output)+ return code+ name = f"{time.time():.6f}-{os.getpid()}-{uuid.uuid4().hex[:6]}.json"+ tmp = os.path.join(d, "queue", name + ".tmp")+ with open(tmp, "w", encoding="utf-8") as f:+ json.dump({"args": args, "env": dict(os.environ), "cwd": os.getcwd(), "timeout": TIMEOUT}, f)+ os.replace(tmp, os.path.join(d, "queue", name))+ done = os.path.join(d, "done", name)+ checked = time.time()+ while not os.path.exists(done):+ time.sleep(0.02)+ if time.time() - checked > 5: # the pool died with our request: run it ourselves+ checked = time.time()+ if not pool_alive(d) and not os.path.exists(done):+ code, output = run_cold(a.app, args, dict(os.environ), os.getcwd())+ sys.stdout.write("warm_ui: the pool stopped; ran cold\n" + output)+ return code+ with open(done, encoding="utf-8") as f:+ res = json.load(f)+ os.remove(done)+ sys.stdout.write(res["output"])+ if res["how"] == "rerun":+ sys.stdout.write("warm_ui: [rerun cold]\n")+ return res["code"]+++def cmd_stop(a, quiet=False):+ d = os.path.abspath(a.dir)+ if not os.path.isdir(d):+ return 0+ open(os.path.join(d, "stop"), "w").close()+ for _ in range(600):+ if not pool_alive(d):+ break+ time.sleep(0.05)+ if not quiet:+ try:+ with open(os.path.join(d, "pool.log"), encoding="utf-8", errors="replace") as f:+ sys.stdout.write(f.read())+ except OSError:+ pass+ return 0+++# --- the whole UI suite, for comparing warm with cold ---------------------------------------------------------------++def ui_tests(build, label_regex):+ out = subprocess.run(["ctest", "--test-dir", build, "--show-only=json-v1", "-L", label_regex],+ capture_output=True, text=True, check=True).stdout+ tests = []+ for t in json.loads(out)["tests"]:+ props = {p["name"]: p["value"] for p in t.get("properties", [])}+ if props.get("DISABLED") or "command" not in t:+ continue+ cmd = t["command"]+ if "client" in cmd and "--" in cmd: # a warm client: the app and its arguments+ cmd = [cmd[cmd.index("--app") + 1]] + cmd[cmd.index("--") + 1:]+ env = dict(os.environ)+ for kv in props.get("ENVIRONMENT", []):+ k, _, v = kv.partition("=")+ env[k] = v+ tests.append({"name": t["name"], "app": cmd[0], "args": cmd[1:], "env": env,+ "cwd": props.get("WORKING_DIRECTORY", build)})+ return tests+++def shot_hashes(folder):+ hashes = {}+ for root, _, files in os.walk(folder):+ for n in files:+ if n.endswith(".png"):+ p = os.path.join(root, n)+ with open(p, "rb") as f:+ hashes[os.path.relpath(p, folder)] = hashlib.sha256(f.read()).hexdigest()[:16]+ return hashes+++def cmd_suite(a):+ build = os.path.abspath(a.build)+ tests = [t for t in ui_tests(build, a.label) if script_of(t["args"])]+ if a.regex:+ tests = [t for t in tests if re.search(a.regex, t["name"])]+ shots = os.path.abspath(a.shots or os.path.join(build, "ui-shots-" + ("cold" if a.cold else "warm")))+ for t in tests: # each run's screenshots in a folder of its own+ i = t["args"].index("--ui-out") + 1 if "--ui-out" in t["args"] else None+ if i:+ t["args"][i] = os.path.join(shots, os.path.basename(t["args"][i]))+ n = a.servers or servers_wanted()+ pool = None+ if not a.cold:+ pool = Pool(tests[0]["app"], os.path.join(build, "ui-warm-suite"), n)+ t0 = time.time()+ pool.warm_up()+ print(f"{n} servers ready in {time.time() - t0:.1f} s")+ results = {}+ for rep in range(a.repeat):+ order = list(tests)+ if a.shuffle:+ random.Random(a.seed + rep).shuffle(order)+ shutil.rmtree(shots, ignore_errors=True)+ work = queue.Queue()+ for t in order:+ work.put(t)+ lock = threading.Lock()+ rep_results = {}++ def go():+ while True:+ try:+ t = work.get_nowait()+ except queue.Empty:+ return+ s = time.time()+ if pool:+ code, out, how = pool.run(t["args"], t["env"], t["cwd"])+ else:+ code, out = run_cold(t["app"], t["args"], t["env"], t["cwd"])+ how = "cold"+ with lock:+ rep_results[t["name"]] = {"code": code, "seconds": round(time.time() - s, 2), "how": how,+ "fail": [l for l in out.splitlines() if l.startswith("FAIL")][:5]}+ if code != 0:+ rep_results[t["name"]]["tail"] = out.splitlines()[-12:]+ if a.verbose:+ print(f"--- {t['name']} ({how}) exit {code}:\n" + "\n".join(out.splitlines()[-12:]))++ t0 = time.time()+ threads = [threading.Thread(target=go) for _ in range(a.jobs or n)]+ for th in threads:+ th.start()+ for th in threads:+ th.join()+ wall = time.time() - t0+ failed = sorted(k for k, r in rep_results.items() if r["code"] != 0)+ print(f"run {rep + 1}: {len(order)} tests in {wall:.1f} s, {len(failed)} failed"+ + (f": {' '.join(failed)}" if failed else ""))+ results[f"run{rep + 1}"] = {"wall": round(wall, 1), "tests": rep_results, "shots": shot_hashes(shots),+ "failed": failed}+ if pool:+ print(f"cold reruns: {pool.cold_reruns}")+ pool.stop()+ if a.json:+ with open(a.json, "w") as f:+ json.dump(results, f, indent=1, sort_keys=True)+ return 0 if all(not r["failed"] for r in results.values()) else 1+++def cmd_compare(a):+ """The first file's runs are the reference (cold ones, say): a verdict or screenshot counts only where they+ all agree, since some screenshots differ from run to run anyway (GPU timing). Each run of the other files is+ checked against that."""+ def runs_of(path):+ with open(path) as f:+ return [(f"{os.path.basename(path)}:{k}", r) for k, r in sorted(json.load(f).items())]+ ref = runs_of(a.files[0])+ others = [r for path in a.files[1:] for r in runs_of(path)] or ref[1:]+ ref_runs = [r for _, r in ref]+ ok = lambda r, t: r["tests"].get(t, {}).get("code") == 0+ tests = set().union(*(r["tests"] for _, r in ref))+ stable_verdict = {t: ok(ref_runs[0], t) for t in tests if len({ok(r, t) for r in ref_runs}) == 1}+ shots = set().union(*(r["shots"] for r in ref_runs))+ stable_shot = {s: ref_runs[0]["shots"].get(s) for s in shots if len({r["shots"].get(s) for r in ref_runs}) == 1}+ print(f"reference {a.files[0]}: {len(ref)} runs ({', '.join(str(r['wall']) + ' s' for r in ref_runs)}); "+ f"{len(stable_verdict)} of {len(tests)} verdicts and {len(stable_shot)} of {len(shots)} screenshots the same in each")+ differ = 0+ for name, r in others:+ verdicts = sorted(t for t, v in stable_verdict.items() if ok(r, t) != v)+ diff_shots = sorted(s for s, h in stable_shot.items() if r["shots"].get(s) != h)+ differ += len(verdicts) + len(diff_shots)+ print(f"{name} ({r['wall']} s): {len(verdicts)} verdicts and {len(diff_shots)} stable screenshots differ")+ for t in verdicts:+ print(f" verdict {t}: {r['tests'].get(t, {}).get('code')} {r['tests'].get(t, {}).get('fail')}")+ for s in diff_shots:+ print(f" shot {s}")+ return 1 if differ else 0+++def main():+ p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)+ sub = p.add_subparsers(dest="cmd", required=True)+ for name in ("start", "serve", "client", "stop"):+ s = sub.add_parser(name)+ s.add_argument("--dir", required=True)+ if name != "stop":+ s.add_argument("--app", required=name != "stop")+ if name == "client":+ s.add_argument("args", nargs=argparse.REMAINDER)+ s = sub.add_parser("suite")+ s.add_argument("--build", required=True)+ s.add_argument("--label", default="^ui$")+ s.add_argument("--regex", help="only the tests whose name matches")+ s.add_argument("--cold", action="store_true", help="every test in a process of its own")+ s.add_argument("--servers", type=int, help="warm servers (default SF_UI_SERVERS or half the CPU threads)")+ s.add_argument("--jobs", type=int, help="tests at once (default: as many as servers)")+ s.add_argument("--repeat", type=int, default=1)+ s.add_argument("--shuffle", action="store_true")+ s.add_argument("--seed", type=int, default=1)+ s.add_argument("--shots", help="screenshot folder (default BUILD/ui-shots-warm or -cold)")+ s.add_argument("--json", help="write every run's results and screenshot hashes here")+ s.add_argument("--verbose", action="store_true", help="print the end of each failed test's output")+ s = sub.add_parser("compare")+ s.add_argument("files", nargs="+")+ a = p.parse_args()+ if os.name != "nt":+ signal.signal(signal.SIGTERM, lambda *_: sys.exit(1))+ return {"start": cmd_start, "serve": cmd_serve, "client": cmd_client, "stop": cmd_stop,+ "suite": cmd_suite, "compare": cmd_compare}[a.cmd](a)+++if __name__ == "__main__":+ sys.exit(main())