diff options
| author | Gabriel Schneider <[email protected]> | 2026-09-25 21:48:16 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-09-25 21:48:16 -0300 |
| commit | 50f4ee716aeafa13118cbcfa0f5cb37f7d3940a4 (patch) | |
| tree | 359f5128d9a08098b92565dedb69341879427a04 /src/host/Host.zig | |
| parent | 9c33714591a2147f7e5879e77212ca9886d0c967 (diff) | |
| download | sketchbook-50f4ee716aeafa13118cbcfa0f5cb37f7d3940a4.tar.gz sketchbook-50f4ee716aeafa13118cbcfa0f5cb37f7d3940a4.zip | |
sketchbook: headless runs never open a window
The offscreen hints lost to the environment: with SDL_VIDEO_DRIVER=wayland
set, every headless test and script opened a real window. They are
overrides now. The offscreen driver has no Vulkan surface on every driver,
so a headless host's GPU renderer gets no window and draws into a
canvas-sized texture, which screenshots read.
Diffstat (limited to 'src/host/Host.zig')
| -rw-r--r-- | src/host/Host.zig | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/src/host/Host.zig b/src/host/Host.zig index 41597a6..81a5dc8 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -37,6 +37,9 @@ renderer: *c.SDL_Renderer, /// Null when the machine has no usable GPU driver. gpu: ?*c.SDL_GPUDevice, passes: ?Gpu = null, +/// Headless with a GPU: the renderer has no window and draws into this +/// canvas-sized texture, which screenshots read. +offscreen: ?*c.SDL_Texture = null, pointer_cursor: ?*c.SDL_Cursor = null, images: gfx.Images, audio: sound.Audio, @@ -70,9 +73,11 @@ screenshot: ?[:0]const u8 = null, pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std.process.Environ.Map, options: Options, linked: ?abi.Sketch) !void { if (options.headless) { - _ = c.SDL_SetHint(c.SDL_HINT_VIDEO_DRIVER, "offscreen"); - _ = c.SDL_SetHint(c.SDL_HINT_AUDIO_DRIVER, "dummy"); - _ = c.SDL_SetHint(c.SDL_HINT_RENDER_DRIVER, "software"); + // Overriding: a plain hint loses to the environment, and a user's + // SDL_VIDEO_DRIVER=wayland would open real windows during tests. + _ = c.SDL_SetHintWithPriority(c.SDL_HINT_VIDEO_DRIVER, "offscreen", c.SDL_HINT_OVERRIDE); + _ = c.SDL_SetHintWithPriority(c.SDL_HINT_AUDIO_DRIVER, "dummy", c.SDL_HINT_OVERRIDE); + _ = c.SDL_SetHintWithPriority(c.SDL_HINT_RENDER_DRIVER, "software", c.SDL_HINT_OVERRIDE); } if (!c.SDL_Init(c.SDL_INIT_VIDEO | c.SDL_INIT_EVENTS | c.SDL_INIT_AUDIO)) return sdlError("SDL_Init"); errdefer c.SDL_Quit(); @@ -91,9 +96,11 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std const window = c.SDL_CreateWindow(title.ptr, @intCast(info.width), @intCast(info.height), c.SDL_WINDOW_RESIZABLE) orelse return sdlError("SDL_CreateWindow"); errdefer c.SDL_DestroyWindow(window); // The 2D renderer runs on the GPU device that also runs sketches' GPU - // passes; without one, SDL picks whatever renderer works. + // passes; without one, SDL picks whatever renderer works. Headless, it + // gets no window (an offscreen window has no Vulkan surface everywhere) + // and draws into a texture instead (`setCanvas`). var gpu = c.SDL_CreateGPUDevice(c.SDL_GPU_SHADERFORMAT_SPIRV, false, null); - var renderer: ?*c.SDL_Renderer = if (gpu) |device| c.SDL_CreateGPURenderer(device, window) else null; + var renderer: ?*c.SDL_Renderer = if (gpu) |device| c.SDL_CreateGPURenderer(device, if (options.headless) null else window) else null; if (renderer == null) { if (gpu) |device| c.SDL_DestroyGPUDevice(device); gpu = null; @@ -151,6 +158,7 @@ pub fn deinit(self: *Host) void { if (self.passes) |*g| g.deinit(&self.images); self.images.deinit(); if (self.pointer_cursor) |cursor| c.SDL_DestroyCursor(cursor); + if (self.offscreen) |texture| c.SDL_DestroyTexture(texture); c.SDL_DestroyRenderer(self.renderer); if (self.gpu) |device| c.SDL_DestroyGPUDevice(device); c.SDL_DestroyWindow(self.window); @@ -378,7 +386,13 @@ fn adopt(self: *Host, sketch: abi.Sketch) !void { fn setCanvas(self: *Host, w: u32, h: u32) void { if (w == 0 or h == 0 or (self.canvas[0] == w and self.canvas[1] == h)) return; self.canvas = .{ w, h }; - _ = c.SDL_SetRenderLogicalPresentation(self.renderer, @intCast(w), @intCast(h), c.SDL_LOGICAL_PRESENTATION_LETTERBOX); + if (self.options.headless and self.gpu != null) { + if (self.offscreen) |texture| c.SDL_DestroyTexture(texture); + self.offscreen = c.SDL_CreateTexture(self.renderer, c.SDL_PIXELFORMAT_RGBA32, c.SDL_TEXTUREACCESS_TARGET, @intCast(w), @intCast(h)); + _ = c.SDL_SetRenderTarget(self.renderer, self.offscreen); + } else { + _ = c.SDL_SetRenderLogicalPresentation(self.renderer, @intCast(w), @intCast(h), c.SDL_LOGICAL_PRESENTATION_LETTERBOX); + } _ = c.SDL_SetWindowSize(self.window, @intCast(w), @intCast(h)); } |
