Blender's GPU Accelerated UI, Decoupled from it's original codebase. Supports Rust, Python, JS, React, C & C++. Twice as good as GPUI
window hosts: switch an idle window to the GPU when the device is ready
nusaturnf74c454539e0
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex a7808dc..fa900f2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,7 +7,7 @@ All notable changes to **blenderui** (BUI). The format follows## [Unreleased]### Changed-- **Linux GPU start:** the OpenGL context and device are created on a worker thread, as on Direct3D 11. The first frames draw on the CPU.+- **Linux GPU start:** the OpenGL context and device are created on a worker thread, as on Direct3D 11. The first frames draw on the CPU. A window switches to the GPU when the device is ready, also when it is idle (Linux and Windows; `BUI_raster_gpu_pending`).- **Benchmarks on Linux:** `bindings/rust/bench/common/measure.rs` measures CPU time (`CLOCK_PROCESS_CPUTIME_ID`) and memory (`/proc/self/status`) on Linux, so `bui-bench`, `bench_frames` and `cargo test` build there; Windows is unchanged.### Fixeddiff --git a/gpu/include/GPU_raster.h b/gpu/include/GPU_raster.hindex bf310c4..4820485 100644--- a/gpu/include/GPU_raster.h+++ b/gpu/include/GPU_raster.h@@ -312,6 +312,9 @@ typedef struct BuiGpuX11Window {} BuiGpuX11Window;int BUI_raster_gpu_attach(BuiRaster *r, void *window_handle, int wait);int BUI_raster_gpu_poll(BuiRaster *r);+/* 1 while attach() still creates the device. A host polls until it is 0, so an idle window+ * also switches over. */+int BUI_raster_gpu_pending(const BuiRaster *r);int BUI_raster_is_gpu(const BuiRaster *r);/* Whether new windows use the GPU: BUI_gpu_set_default(), overridden by the* BUI_GPU environment variable (1/0). Off by default. */diff --git a/gpu/intern/gpu_raster.c b/gpu/intern/gpu_raster.cindex 55ca87e..c498a5b 100644--- a/gpu/intern/gpu_raster.c+++ b/gpu/intern/gpu_raster.c@@ -306,6 +306,11 @@ int BUI_raster_gpu_poll(BuiRaster *r)return sw;}+int BUI_raster_gpu_pending(const BuiRaster *r)+{+ return r->gpu_pending != NULL;+}+int BUI_raster_is_gpu(const BuiRaster *r){return r->gpu != NULL;diff --git a/kit/intern/kit_window_win32.c b/kit/intern/kit_window_win32.cindex 0acd7d0..03e0b78 100644--- a/kit/intern/kit_window_win32.c+++ b/kit/intern/kit_window_win32.c@@ -28,6 +28,7 @@# define ID_TIP 1# define ID_ANIM 2# define ID_WAKE 3 /* BK_TIMER_CUSTOM: custom nodes' frames */+# define ID_GPU 4 /* checks the GPU device until it is ready */# define ID_USER_BASE 1000struct BKWindow {@@ -323,6 +324,14 @@ static double refresh_ms(void)return ms;}+/* The device starts on a worker thread. The timer switches the window over when it is ready. */+static void gpu_attach(BKWindow *win)+{+ if (BUI_raster_gpu_attach(win->raster, win->hwnd, 0)) {+ SetTimer(win->hwnd, ID_GPU, (UINT)refresh_ms(), NULL);+ }+}+/* Milliseconds since the current animation frame began. */static double frame_ms(const BKWindow *win){@@ -630,6 +639,17 @@ static LRESULT CALLBACK wndproc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)KillTimer(hwnd, ID_WAKE);BK_timer(ctx, BK_TIMER_CUSTOM);}+ else if (wp == ID_GPU) {+ /* a GPU device that is ready switches an idle window over too */+ if (!BUI_raster_gpu_pending(win->raster)) {+ KillTimer(hwnd, ID_GPU);+ }+ else if (BUI_raster_gpu_poll(win->raster)) {+ KillTimer(hwnd, ID_GPU);+ win->full = 1;+ host_request(win);+ }+ }else if (wp >= ID_USER_BASE) {KillTimer(hwnd, wp);if (win->cb.on_timer) {@@ -701,7 +721,7 @@ BKWindow *BK_window_new(BKContext *ctx, const char *title, int width, int heightSetWindowLongPtrW(win->hwnd, GWLP_USERDATA, (LONG_PTR)win);g_open++;if (BUI_gpu_default()) {- BUI_raster_gpu_attach(win->raster, win->hwnd, 0);+ gpu_attach(win);}BKHost host = {win, host_request, host_set_cursor, host_warp,host_clipboard_get, host_clipboard_set, host_schedule, host_emit};@@ -791,7 +811,7 @@ BuiRaster *BK_window_raster(BKWindow *win)void BK_window_set_gpu(BKWindow *win, int on){if (win && on) {- BUI_raster_gpu_attach(win->raster, win->hwnd, 0);+ gpu_attach(win);host_request(win);}}diff --git a/kit/intern/kit_window_x11.c b/kit/intern/kit_window_x11.cindex 8e7cad2..9c585e4 100644--- a/kit/intern/kit_window_x11.c+++ b/kit/intern/kit_window_x11.c@@ -961,6 +961,9 @@ static double next_wait(void)if (win->render_pending) {return 0.0;}+ if (BUI_raster_gpu_pending(win->raster)) { /* check the device once per refresh */+ wait = wait < 0 ? refresh_ms() : BK_MIN(wait, refresh_ms());+ }for (int i = 0; i < MAX_TIMERS; i++) {if (win->timers[i].id != 0) {const double d = BK_MAX(0.0, win->timers[i].due - t);@@ -1013,6 +1016,13 @@ static void turn(double block_ms)handle_event(&ev);}run_timers();+ for (BKWindow *win = g_windows; win; win = win->next) {+ /* a GPU device that is ready switches an idle window over too */+ if (win->xid && BUI_raster_gpu_pending(win->raster) && BUI_raster_gpu_poll(win->raster)) {+ win->full = 1;+ win->render_pending = 1;+ }+ }int rendered = 0;for (BKWindow *win = g_windows; win; win = win->next) {if (win->xid && win->render_pending) {