Blender's GPU Accelerated UI, Decoupled from it's original codebase. Supports Rust, Python, JS, React, C & C++. Twice as good as GPUI
video bench
Preetham Pemmasani631118c3d074
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 79efe3a..1bf4f3e 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -107,6 +107,9 @@ if(BUI_BUILD_TESTS)if(WIN32) # the thumbnail-grid benchmark (bench/images/image_grid.c; not a test)add_executable(bui_image_bench bench/images/image_grid.c)target_link_libraries(bui_image_bench PRIVATE bui::kit psapi)+ # players at once, CPU and GPU (bench/video/video_bench.c; not a test)+ add_executable(bui_video_bench bench/video/video_bench.c)+ target_link_libraries(bui_video_bench PRIVATE bui::kit psapi)endif()endif()diff --git a/bench/video/make_clips.sh b/bench/video/make_clips.shnew file mode 100644index 0000000..c7ccdfe--- /dev/null+++ b/bench/video/make_clips.sh@@ -0,0 +1,13 @@+#!/bin/sh+# Test clips for bui_video_bench (H.264 High, yuv420p, 30 fps, 12 s, a keyframe+# a second): 1080p and 4K. Generated where you run the benchmark, never+# committed. Usage: make_clips.sh <out dir> (needs ffmpeg with libx264)+set -e+out=${1:-.}+mkdir -p "$out"+for size in 1920x1080 3840x2160; do+ ffmpeg -v error -y -f lavfi -i "testsrc2=size=$size:rate=30:duration=12" \+ -c:v libx264 -preset veryfast -crf 23 -pix_fmt yuv420p -g 30 -movflags +faststart \+ "$out/clip_$size.mp4"+done+ls -l "$out"diff --git a/bench/video/video_bench.c b/bench/video/video_bench.cnew file mode 100644index 0000000..fb4f840--- /dev/null+++ b/bench/video/video_bench.c@@ -0,0 +1,257 @@+/* Video benchmark (website: Video > Performance).+ *+ * bui_video_bench <clip> <count> <cell_w> <cell_h> [gpu] [seconds]+ *+ * <count> players of <clip> (looping, muted autoplay) on a canvas, each in a+ * <cell_w> x <cell_h> box (object-fit: contain), laid out in rows in a+ * headless window, driven like a window loop: the context's frame timer+ * (BKHost.schedule), decoded frames arriving, renders. CPU raster or, with+ * "gpu", the GPU renderer (Direct3D 11 offscreen). Prints JSON:+ * - first_frame_ms: from BK_video_new to every player showing a frame+ * - render p50 / p99 (ms) while playing+ * - cpu_percent: process CPU time (all threads: UI, decode workers, the OS+ * decoder's threads) over wall time, of one core+ * - frames shown per second (all players) and dropped frames: frames the+ * clips' rate wanted in the measured time that never showed+ * - working set, MEM allocations per rendered frame+ * - off screen (players no longer drawn) for 3 s: decodes, renders, CPU ms+ * - MEM blocks left after BK_video_free + BK_image_exit+ * Clips are generated by bench/video/make_clips.sh (ffmpeg), never committed.+ *+ * SPDX-License-Identifier: GPL-2.0-or-later */++#include <stdio.h>+#include <stdlib.h>+#include <string.h>++#define WIN32_LEAN_AND_MEAN+#include <windows.h>+#include <psapi.h>++#include "BUI_kit.h"+#include "GPU_raster.h"+#include "MEM_guardedalloc.h"++#define MAXV 16+#define BK_MIN(a, b) ((a) < (b) ? (a) : (b))++static double now_ms(void)+{+ LARGE_INTEGER f, c;+ QueryPerformanceFrequency(&f);+ QueryPerformanceCounter(&c);+ return (double)c.QuadPart * 1000.0 / (double)f.QuadPart;+}++static double cpu_ms(void)+{+ FILETIME c, e, k, u;+ GetProcessTimes(GetCurrentProcess(), &c, &e, &k, &u);+ const ULONGLONG kk = ((ULONGLONG)k.dwHighDateTime << 32) | k.dwLowDateTime;+ const ULONGLONG uu = ((ULONGLONG)u.dwHighDateTime << 32) | u.dwLowDateTime;+ return (double)(kk + uu) / 1e4;+}++static size_t working_set(void)+{+ PROCESS_MEMORY_COUNTERS pmc;+ GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));+ return pmc.WorkingSetSize;+}++static int cmp(const void *a, const void *b)+{+ const double x = *(const double *)a, y = *(const double *)b;+ return x < y ? -1 : x > y;+}++static struct {+ double due;+ int timer;+} g_timer;++static void schedule(void *data, int ms, int timer)+{+ (void)data;+ g_timer.due = now_ms() + ms;+ g_timer.timer = timer;+}++typedef struct Loop {+ BKContext *ctx;+ BuiRaster *r;+ int w, h;+ BKVideo *v[MAXV];+ int n;+ double last_t[MAXV];+ int shown; /* frame changes seen (all players) */+ int renders;+ double *ft;+ int nft, cap;+} Loop;++static void loop_for(Loop *L, double ms)+{+ const double end = now_ms() + ms;+ while (now_ms() < end) {+ double wait = end - now_ms();+ if (g_timer.due > 0.0 && g_timer.due - now_ms() < wait) {+ wait = g_timer.due - now_ms();+ }+ if (wait > 1.0) {+ Sleep(1);+ }+ if (g_timer.due > 0.0 && now_ms() >= g_timer.due) {+ g_timer.due = 0.0;+ BK_timer(L->ctx, g_timer.timer);+ }+ BK_image_poll();+ if (BK_needs_render(L->ctx)) {+ const double a = now_ms();+ BK_render(L->ctx, L->r, L->w, L->h, 0);+ if (L->nft < L->cap) {+ L->ft[L->nft++] = now_ms() - a;+ }+ L->renders++;+ for (int i = 0; i < L->n; i++) {+ const double t = BK_video_time(L->v[i]);+ L->shown += t != L->last_t[i];+ L->last_t[i] = t;+ }+ }+ }+}++static void show(Loop *L, int cw, int ch, int cols)+{+ for (int i = 0; i < L->n; i++) {+ const float x = (float)(i % cols * cw), y = (float)(i / cols * ch);+ const float coords[4] = {x, y, x + cw, y + ch};+ BKCanvasItem it;+ memset(&it, 0, sizeof(it));+ it.id = i + 1;+ it.type = BK_ITEM_IMAGE;+ it.coords = coords;+ it.ncoords = 4;+ it.fit = BK_FIT_CONTAIN;+ it.video = L->v[i];+ BK_canvas_put_item(BK_context_root(L->ctx), i, &it);+ }+ BK_canvas_truncate(BK_context_root(L->ctx), L->n);+}++static int all_loaded(Loop *L)+{+ for (int i = 0; i < L->n; i++) {+ if (BK_video_state(L->v[i]) == BK_VIDEO_LOADING) {+ return 0;+ }+ }+ return 1;+}++int main(int argc, char **argv)+{+ if (argc < 5) {+ fprintf(stderr, "usage: bui_video_bench <clip> <count> <cell_w> <cell_h> [gpu] [seconds]\n");+ return 2;+ }+ const char *clip = argv[1];+ const int n = BK_MIN(MAXV, atoi(argv[2])), cw = atoi(argv[3]), ch = atoi(argv[4]);+ const int gpu = argc > 5 && strcmp(argv[5], "gpu") == 0;+ const double secs = argc > 6 ? atof(argv[6]) : 8.0;+ const int cols = n <= 1 ? 1 : n <= 4 ? 2 : 3;+ Loop L = {0};+ L.n = n;+ L.w = cw * cols;+ L.h = ch * ((n + cols - 1) / cols);+ L.cap = 200000;+ L.ft = (double *)malloc(sizeof(double) * (size_t)L.cap);+ const unsigned blocks0 = MEM_get_memory_blocks_in_use();+ BKNode *canvas = BK_node_new("canvas");+ L.ctx = BK_context_new(canvas);+ BKHost host = {0};+ host.schedule = schedule;+ BK_context_set_host(L.ctx, &host);+ L.r = BUI_raster_create(L.w, L.h);+ if (gpu && !BUI_raster_gpu_attach(L.r, NULL, 1)) {+ fprintf(stderr, "no GPU renderer\n");+ return 1;+ }+ const size_t ws0 = working_set();++ /* start-up: players created, drawn, until every one shows a frame */+ const double t0 = now_ms();+ for (int i = 0; i < n; i++) {+ L.v[i] = BK_video_new(clip);+ BK_video_set_loop(L.v[i], 1);+ }+ show(&L, cw, ch, cols);+ BK_render(L.ctx, L.r, L.w, L.h, 1);+ while (!all_loaded(&L) && now_ms() - t0 < 20000.0) {+ loop_for(&L, 1.0);+ }+ const double first = now_ms() - t0;+ if (BK_video_state(L.v[0]) != BK_VIDEO_PLAYING) {+ fprintf(stderr, "the clip didn't play (backends: %s)\n", BK_video_backends());+ return 1;+ }+ int vw = 0, vh = 0;+ BK_video_size(L.v[0], &vw, &vh);+ loop_for(&L, 1000.0); /* warm up */++ /* playing */+ L.nft = L.renders = L.shown = 0;+ const size_t alloc0 = MEM_get_allocations_total();+ const double c0 = cpu_ms(), w0 = now_ms();+ double tv0[MAXV];+ for (int i = 0; i < n; i++) {+ tv0[i] = BK_video_time(L.v[i]);+ }+ loop_for(&L, secs * 1000.0);+ const double wall = now_ms() - w0, cpu = cpu_ms() - c0;+ const size_t allocs = MEM_get_allocations_total() - alloc0;+ const size_t ws = working_set();+ qsort(L.ft, (size_t)L.nft, sizeof(double), cmp);+ const double p50 = L.nft ? L.ft[L.nft / 2] : 0, p99 = L.nft ? L.ft[(int)(L.nft * 0.99)] : 0;+ const int shown = L.shown, renders = L.renders;+ (void)tv0;+ /* the clips are 30 fps: what the measured wall time wanted */+ const double wanted = wall / 1000.0 * 30.0 * n;++ /* off screen */+ BK_canvas_truncate(canvas, 0);+ BK_render(L.ctx, L.r, L.w, L.h, 1);+ loop_for(&L, 300.0); /* steps in flight land */+ BKImageStats st;+ BK_image_stats(&st);+ const int dec0 = st.decoded;+ L.renders = 0;+ const double oc0 = cpu_ms();+ loop_for(&L, 3000.0);+ const double off_cpu = cpu_ms() - oc0;+ BK_image_stats(&st);+ const int off_dec = st.decoded - dec0, off_renders = L.renders;++ for (int i = 0; i < n; i++) {+ BK_video_free(L.v[i]);+ }+ BK_context_free(L.ctx);+ BK_node_free(canvas);+ BUI_raster_free(L.r);+ BK_image_exit();+ printf("{\"clip\": \"%dx%d\", \"players\": %d, \"cell\": \"%dx%d\", \"renderer\": \"%s\", "+ "\"backend\": \"%s\", \"first_frame_ms\": %.0f, \"seconds\": %.1f, "+ "\"render_p50_ms\": %.2f, \"render_p99_ms\": %.2f, \"cpu_percent\": %.1f, "+ "\"frames_shown_per_s\": %.1f, \"dropped_frames\": %.0f, \"dropped_percent\": %.1f, "+ "\"working_set_mb\": %.1f, \"working_set_added_mb\": %.1f, "+ "\"mem_allocs_per_render\": %.2f, \"offscreen_3s_decodes\": %d, "+ "\"offscreen_3s_renders\": %d, \"offscreen_3s_cpu_ms\": %.0f, \"mem_blocks_leaked\": %d}\n",+ vw, vh, n, cw, ch, gpu ? "gpu" : "cpu", BK_video_backends(), first, wall / 1000.0, p50,+ p99, cpu / wall * 100.0, shown / (wall / 1000.0), wanted > shown ? wanted - shown : 0.0,+ wanted > shown ? (wanted - shown) / wanted * 100.0 : 0.0, ws / 1048576.0,+ (double)(ws - ws0) / 1048576.0, renders ? (double)allocs / renders : 0.0, off_dec,+ off_renders, off_cpu, (int)MEM_get_memory_blocks_in_use() - (int)blocks0);+ free(L.ft);+ return 0;+}