From 00605f7a109845a99f16621b267305a8b995dddc Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 11:27:39 -0300 Subject: sketchbook: shapes antialiased up to 3840x2160; SDL's Vulkan backend no longer defragments (it lost a frame's first draws) Co-Authored-By: Claude Fable 5.2 --- README.md | 8 +++++++- build.zig | 13 +++++++++++++ src/host/gfx.zig | 7 ++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 181493b..123b2bc 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,8 @@ shapes blend alike, and shapes keep their order among `fill`, `image`, `text` and the clip. Both antialias: the canvas does, and native draws 4x4 samples a pixel (with a GPU renderer: runs of consecutive shapes go into a supersampled layer, composited when another command comes) and -averages them. Edges come out within a few levels of the web's, and a +averages them; 2x2 on a canvas or target over 1920x1080, none over +3840x2160. Edges come out within a few levels of the web's, and a 1 px line on a pixel row stays full black on both. `sk.draw` makes them, at two levels, neither with state of its own: @@ -467,6 +468,11 @@ stb_vorbis): WAV, FLAC and Ogg Vorbis, chosen by content. fails the exit. SDL's Vulkan backend leaked when `vkAllocateMemory` failed and it fell back to another memory type (it happens with many headless hosts at once); the build compiles that file with the fix. + It also never defragments memory there: SDL moves textures to new ones + inside whichever command buffer is submitted next, and a sketch's GPU + pass is submitted while the frame's 2D commands are still being + recorded, so the canvas lost what was drawn before the pass and showed + the previous frame under the rest (after a large texture was freed). ## The web host diff --git a/build.zig b/build.zig index b980c91..e9c3277 100644 --- a/build.zig +++ b/build.zig @@ -302,6 +302,16 @@ fn fixSdlThreadCleanup(b: *Build, sdl: *Build.Module, source: Build.LazyPath) vo /// another memory type. Many headless hosts at once (parallel `e2e`) run /// small heaps out, and the leak check failed random scripts. The file is /// compiled from a copy with the two frees added. +/// +/// The copy also never defragments memory. A defragmentation runs inside +/// whichever command buffer is submitted next, moves the textures of a +/// fragmented allocation to new ones and re-points their handles, and the +/// host submits a sketch's GPU passes while the 2D renderer's command +/// buffer for the frame is still being recorded: what that had drawn so +/// far went to the old texture and the rest to the copy, which shows the +/// previous frame under it (seen after a large texture was freed, at +/// 1920x1080 and up). Freed memory is still reused; it is only not +/// compacted. fn fixSdlVulkanAllocationLeak(b: *Build, sdl: *Build.Module, source: Build.LazyPath) void { const file = "src/gpu/vulkan/SDL_gpu_vulkan.c"; var found = false; @@ -333,6 +343,9 @@ fn fixSdlVulkanAllocationLeak(b: *Build, sdl: *Build.Module, source: Build.LazyP "SDL_gpu_vulkan.c", " // Uh oh, we couldn't allocate, time to clean up\n SDL_free(allocation->freeRegions);\n", " // Uh oh, we couldn't allocate, time to clean up\n SDL_free(allocation->freeRegions);\n SDL_free(allocation->usedRegions);\n SDL_DestroyMutex(allocation->memoryLock);\n", + "SDL_gpu_vulkan.c", + " VULKAN_INTERNAL_MarkAllocationsForDefrag(renderer);\n", + " (void)VULKAN_INTERNAL_MarkAllocationsForDefrag;\n", }); // The copy's relative includes (`../SDL_sysgpu.h`) resolve from the // original directory. diff --git a/src/host/gfx.zig b/src/host/gfx.zig index 3affa6e..0466f27 100644 --- a/src/host/gfx.zig +++ b/src/host/gfx.zig @@ -180,9 +180,10 @@ pub const Painter = struct { dirty: [4]u32 = .{ 0, 0, 0, 0 }, }; - /// 16 samples a pixel, unless that takes more than 16 Mi of them; 4, - /// then; else none. - const max_samples = 16 << 20; + /// 16 samples a pixel, unless that takes more than a 3840x2160 canvas + /// does at 4 (what 1920x1080 takes at 16: a 127 MiB layer); 4, then; + /// else none. + const max_samples = 3840 * 2160 * 4; fn factorFor(size: [2]u32) u32 { const pixels = @as(u64, size[0]) * size[1]; -- cgit v1.3