diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-28 21:07:13 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-12 13:22:02 -0300 |
| commit | 590ba7b9b579b97e34b51ab7e3460ebc85713343 (patch) | |
| tree | 6a73a7570ff4f79f41deff078455877b9bc6571b /src/host | |
| parent | bc8e6eb1bfef0c5e9b3c40fc1e4fd5a1659e2b03 (diff) | |
| download | sketchbook-590ba7b9b579b97e34b51ab7e3460ebc85713343.tar.gz sketchbook-590ba7b9b579b97e34b51ab7e3460ebc85713343.zip | |
sketchbook: Zig 0.17, a Store on disk, requests, a scope (waveform, spectrum, spectrogram), audio codecs and fonts, web host
Diffstat (limited to 'src/host')
| -rw-r--r-- | src/host/Console.zig | 4 | ||||
| -rw-r--r-- | src/host/Host.zig | 198 | ||||
| -rw-r--r-- | src/host/audio.zig | 94 | ||||
| -rw-r--r-- | src/host/fields.zig | 2 | ||||
| -rw-r--r-- | src/host/gfx.zig | 24 | ||||
| -rw-r--r-- | src/host/gpu.zig | 311 | ||||
| -rw-r--r-- | src/host/library.zig | 10 | ||||
| -rw-r--r-- | src/host/memory.zig | 393 | ||||
| -rw-r--r-- | src/host/root.zig | 11 | ||||
| -rw-r--r-- | src/host/script.zig | 33 |
10 files changed, 960 insertions, 120 deletions
diff --git a/src/host/Console.zig b/src/host/Console.zig index e6be2f0..b22facb 100644 --- a/src/host/Console.zig +++ b/src/host/Console.zig @@ -95,7 +95,7 @@ fn complete(self: *Console, extra: []const []const u8) void { first = candidate; common = candidate.len; } - const match = std.fmt.bufPrint(matches[used..], "{s} ", .{candidate}) catch continue; + const match = std.mem.print(matches[used..], "{s} ", .{candidate}) catch continue; used += match.len; count += 1; }; @@ -120,7 +120,7 @@ pub fn log(self: *Console, message: []const u8) void { pub fn print(self: *Console, comptime fmt: []const u8, args: anytype) void { var buf: [512]u8 = undefined; - self.log(std.fmt.bufPrint(&buf, fmt, args) catch "(message too long)"); + self.log(std.mem.print(&buf, fmt, args) catch "(message too long)"); } /// Runs one line. Host commands first; anything else goes to the sketch. diff --git a/src/host/Host.zig b/src/host/Host.zig index d511099..7ebe54a 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -25,6 +25,8 @@ pub const Options = struct { headless: bool = false, /// Forget the state file's contents: scripts start from a known place. fresh: bool = false, + /// Time every GPU command alone and print the times at exit. + profile_gpu: bool = false, }; gpa: std.mem.Allocator, @@ -45,6 +47,10 @@ images: gfx.Images, audio: sound.Audio, state: memory.StateFile, session: memory.Session, +store: memory.StoreFile, +/// Images and buffers the sketch made (`newImage`, `decodeImage`, +/// `newBuffer`): Session holds their handles, so they go when it resets. +made: struct { images: std.ArrayList(abi.Image) = .empty, buffers: std.ArrayList(abi.Buffer) = .empty } = .{}, recordings: []align(memory.page) u8, timeline: Timeline, console: Console = .{}, @@ -71,6 +77,7 @@ started: bool = true, last_now: u64 = 0, /// Saved after the next frame is drawn (scripts). screenshot: ?[:0]const u8 = null, +dialog: Dialog = .{}, 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) { @@ -93,7 +100,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std const info = sketch.info(); var title_buf: [256]u8 = undefined; - const title = std.fmt.bufPrintZ(&title_buf, "{s}", .{info.name}) catch "sketch"; + const title = std.mem.printSentinel(&title_buf, "{s}", .{info.name}, 0) catch "sketch"; 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 @@ -117,8 +124,14 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std var state = try memory.StateFile.open(gpa, options.state); errdefer state.close(); var inputs_buf: [std.fs.max_path_bytes]u8 = undefined; - const recordings = try mapRecordings(try std.fmt.bufPrint(&inputs_buf, "{s}.inputs", .{options.state})); + const recordings = try mapRecordings(try std.mem.print(&inputs_buf, "{s}.inputs", .{options.state})); errdefer std.posix.munmap(recordings); + var store_buf: [std.fs.max_path_bytes]u8 = undefined; + var store = memory.StoreFile.open(gpa, try std.mem.print(&store_buf, "{s}.store", .{options.state}), options.fresh) catch |err| { + if (err == error.StoreInUse) std.log.err("{s}.store is open in another process", .{options.state}); + return err; + }; + errdefer store.close(); self.* = .{ .gpa = gpa, @@ -132,6 +145,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std .audio = .{ .gpa = gpa }, .state = state, .session = .init(gpa), + .store = store, .recordings = recordings, .timeline = undefined, .sketch = sketch, @@ -141,6 +155,9 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std }; self.timeline = .{ .bank = .{ .state = &self.state, .inputs = recordings } }; if (gpu) |device| self.passes = .init(gpa, device); + if (options.profile_gpu) if (self.passes) |*g| { + g.profile = .init(gpa); + }; self.audio.open() catch |err| std.log.warn("audio unavailable: {t}", .{err}); if (options.library) |path| if (linked == null) { self.watch = Watch.init(path) catch null; @@ -156,6 +173,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std } pub fn deinit(self: *Host) void { + self.dialog.deinit(); if (self.watch) |w| w.deinit(); self.audio.deinit(); if (self.passes) |*g| g.deinit(&self.images); @@ -173,6 +191,9 @@ pub fn deinit(self: *Host) void { self.files.deinit(self.gpa); std.posix.munmap(self.recordings); self.session.deinit(self.gpa); + self.store.close(); + self.made.images.deinit(self.gpa); + self.made.buffers.deinit(self.gpa); self.state.close(); self.pending.deinit(self.gpa); self.frame_arena.deinit(); @@ -242,16 +263,30 @@ fn draw(self: *Host, now: u64) !void { self.input.console = self.console.visible; self.input.width = @floatFromInt(self.canvas[0]); self.input.height = @floatFromInt(self.canvas[1]); + // Headless runs have no window to fill: the canvas is all the room. + self.input.window_width = self.input.width; + self.input.window_height = self.input.height; + if (!self.options.headless) { + var w: c_int = 0; + var h: c_int = 0; + if (c.SDL_GetWindowSize(self.window, &w, &h) and w > 0 and h > 0) { + self.input.window_width = @floatFromInt(w); + self.input.window_height = @floatFromInt(h); + } + } self.input.streams = self.audio.streamStatus(); self.input.sample_rate = sound.sample_rate; + self.input.microphone = self.audio.record(arena); + self.input.downloads = if (self.passes) |*g| try g.collect(arena) else &.{}; self.input.frame += 1; const ctx = self.context(); - self.sketch.draw(&ctx, self.live().ptr, self.session.bytes.ptr); + self.sketch.draw(&ctx, self.live().ptr); _ = c.SDL_SetRenderClipRect(self.renderer, null); _ = c.SDL_SetRenderDrawColor(self.renderer, 0, 0, 0, 255); _ = c.SDL_RenderClear(self.renderer); self.audio.beginFrame(); + var lines: std.ArrayList([]const u8) = .empty; for (self.commands.items) |command| switch (command) { .canvas => |size| self.setCanvas(size.w, size.h), .cursor => |kind| self.cursor = kind, @@ -259,9 +294,16 @@ fn draw(self: *Host, now: u64) !void { .audio => |a| self.audio.push(a.key, a.samples, a.clear), .quit => self.quit = true, .gpu => |pass| if (self.passes) |*g| g.run(&self.images, pass) catch |err| self.print("gpu pass: {t} ({s})", .{ err, c.SDL_GetError() }), + .upload => |u| if (self.passes) |*g| g.write(u.buffer, u.offset, u.bytes) catch |err| self.print("gpu upload: {t} ({s})", .{ err, c.SDL_GetError() }), + .compute => |d| if (self.passes) |*g| g.compute(d) catch |err| self.print("gpu compute: {t} ({s})", .{ err, c.SDL_GetError() }), + .microphone => self.audio.listen(), + .download => |d| if (self.passes) |*g| g.download(d) catch |err| self.print("gpu download: {t} ({s})", .{ err, c.SDL_GetError() }), + .open_file => |o| self.openFile(o.command, o.pattern), + .run => |line| try lines.append(arena, line), else => gfx.draw(self.renderer, &self.images, command), }; self.audio.endFrame(); + for (lines.items) |line| self.runLine(line, now) catch {}; _ = c.SDL_SetRenderClipRect(self.renderer, null); var extra: std.ArrayList(abi.Command) = .empty; @@ -284,12 +326,72 @@ fn draw(self: *Host, now: u64) !void { if (self.passes) |*g| g.endFrame(); } +/// A file dialog: shown on request, its answer run as a console line from +/// the main loop. SDL may answer on another thread. +const Dialog = struct { + mutex: ?*c.SDL_Mutex = null, + /// Shown and not yet answered (main thread only). + showing: bool = false, + command: [64]u8 = undefined, + command_len: usize = 0, + /// The filter SDL reads until it answers. + pattern: [128:0]u8 = @splat(0), + filter: c.SDL_DialogFileFilter = undefined, + /// "<command> <path>" once answered; empty when cancelled. + answer: [std.fs.max_path_bytes + 64]u8 = undefined, + answer_len: ?usize = null, + + fn deinit(self: *Dialog) void { + if (self.mutex) |m| c.SDL_DestroyMutex(m); + self.mutex = null; + } + + fn callback(userdata: ?*anyopaque, files: [*c]const [*c]const u8, _: c_int) callconv(.c) void { + const self: *Dialog = @ptrCast(@alignCast(userdata orelse return)); + const m = self.mutex orelse return; + c.SDL_LockMutex(m); + defer c.SDL_UnlockMutex(m); + const first = if (files != null and files[0] != null) std.mem.span(files[0]) else ""; + const line = if (first.len == 0) "" else std.mem.print(&self.answer, "{s} {s}", .{ self.command[0..self.command_len], first }) catch ""; + self.answer_len = line.len; + } + + /// The answer's console line, once; empty when cancelled. + fn take(self: *Dialog, out: []u8) ?[]const u8 { + const m = self.mutex orelse return null; + c.SDL_LockMutex(m); + defer c.SDL_UnlockMutex(m); + const len = self.answer_len orelse return null; + self.answer_len = null; + self.showing = false; + @memcpy(out[0..len], self.answer[0..len]); + return out[0..len]; + } +}; + +fn openFile(self: *Host, command: []const u8, pattern: []const u8) void { + const d = &self.dialog; + if (self.options.headless or d.showing) return; + if (command.len > d.command.len or pattern.len > d.pattern.len) return self.print("open_file: command or pattern too long", .{}); + if (d.mutex == null) d.mutex = c.SDL_CreateMutex() orelse return self.print("open_file: {s}", .{c.SDL_GetError()}); + @memcpy(d.command[0..command.len], command); + d.command_len = command.len; + @memset(&d.pattern, 0); + @memcpy(d.pattern[0..pattern.len], pattern); + d.filter = .{ .name = "Files", .pattern = &d.pattern }; + d.showing = true; + c.SDL_ShowOpenFileDialog(Dialog.callback, d, self.window, if (pattern.len > 0) &d.filter else null, @intFromBool(pattern.len > 0), null, false); +} + fn context(self: *Host) abi.Context { return .{ .host = .{ .ctx = self, .vtable = &api }, .input = &self.input, - .arena = self.frame_arena.allocator(), - .gpa = self.session.heap.allocator(), + .scratch = self.frame_arena.allocator(), + .session = self.session.heap.allocator(), + .store = self.store.allocator(), + .session_root = self.session.bytes.ptr, + .store_root = self.store.root(), .commands = &self.commands, }; } @@ -298,7 +400,7 @@ fn context(self: *Host) abi.Context { /// console; while it is open it owns the keyboard, and pointer events over it /// become a cancel, so a half-made click never lands behind it. pub fn route(self: *Host, e: abi.Event) !void { - if (e.kind == .key_down and e.code == @intFromEnum(abi.Key.grave)) { + if (e.kind == .key_down and e.code == @backingInt(abi.Key.grave)) { if (e.flags & abi.Event.repeat != 0) return; self.console.visible = !self.console.visible; return self.pending.append(self.gpa, .{ .kind = .cancel, .time = e.time }); @@ -316,7 +418,7 @@ pub fn route(self: *Host, e: abi.Event) !void { return; }, .key_up => return, - .mouse_move, .mouse_down, .mouse_up, .wheel => if (e.y >= @as(f32, @floatFromInt(self.canvas[1])) - Console.panel_height) { + .mouse_move, .mouse_down, .mouse_up, .wheel, .wheel_horizontal => if (e.y >= @as(f32, @floatFromInt(self.canvas[1])) - Console.panel_height) { return self.pending.append(self.gpa, .{ .kind = .cancel, .time = e.time }); }, else => {}, @@ -339,7 +441,7 @@ pub fn runLine(self: *Host, line: []const u8, now: u64) !void { pub fn forward(self: *Host, _: *Console, line: []const u8, now: u64) abi.Outcome { self.input.now = now; const ctx = self.context(); - return self.sketch.command(&ctx, self.live().ptr, self.session.bytes.ptr, &line); + return self.sketch.command(&ctx, self.live().ptr, &line); } /// Fresh state: declared defaults, then the sketch's `init`. @@ -373,6 +475,8 @@ fn adopt(self: *Host, sketch: abi.Sketch) !void { if (info.abi != abi.hash) return error.AbiMismatch; const adoption = try self.state.adopt(self.gpa, info.state, sketch.defaults); const session_reset = try self.session.adopt(self.gpa, info.session, sketch.defaults); + if (session_reset) self.releaseMade(); + const stored = try self.store.adopt(self.gpa, info.store, sketch.defaults); _ = self.info_arena.reset(.retain_capacity); const arena = self.info_arena.allocator(); self.name = try arena.dupe(u8, info.name); @@ -388,6 +492,33 @@ fn adopt(self: *Host, sketch: abi.Sketch) !void { } if (!self.state.used(0)) self.resetState(); if (session_reset and self.reloads > 0) self.print("session layout changed: session reset", .{}); + switch (stored) { + .unchanged, .created => {}, + .migrated => |m| self.print("store layout changed: kept {d} fields, reset {d} to defaults", .{ m.kept, m.reset }), + } +} + +/// Frees what the old Session made: its handles are gone with it. +fn releaseMade(self: *Host) void { + for (self.made.images.items) |image| { + if (self.passes) |*g| g.forget(&self.images, image); + self.images.free(image); + } + if (self.passes) |*g| for (self.made.buffers.items) |buffer| g.freeBuffer(buffer); + self.made.images.clearRetainingCapacity(); + self.made.buffers.clearRetainingCapacity(); +} + +fn remember(self: *Host, comptime kind: enum { images, buffers }, handle: anytype) @TypeOf(handle) { + if (handle != .none) @field(self.made, @tagName(kind)).append(self.gpa, handle) catch {}; + return handle; +} + +fn forget(list: anytype, handle: anytype) void { + for (list.items, 0..) |h, i| if (h == handle) { + _ = list.swapRemove(i); + return; + }; } fn setCanvas(self: *Host, w: u32, h: u32) void { @@ -421,7 +552,7 @@ fn applyCursor(self: *Host) void { pub fn print(self: *Host, comptime fmt: []const u8, args: anytype) void { var buf: [512]u8 = undefined; - const message = std.fmt.bufPrint(&buf, fmt, args) catch "(message too long)"; + const message = std.mem.print(&buf, fmt, args) catch "(message too long)"; self.console.log(message); // Headless runs are tests: they report through their own results. if (!self.options.headless) std.debug.print("{s}\n", .{message}); @@ -436,18 +567,21 @@ pub fn poll(self: *Host) !void { switch (event.type) { c.SDL_EVENT_QUIT => self.quit = true, c.SDL_EVENT_WINDOW_FOCUS_LOST => try self.route(.{ .kind = .cancel, .time = time }), - c.SDL_EVENT_MOUSE_MOTION => try self.route(.{ .kind = .mouse_move, .time = time, .x = event.motion.x, .y = event.motion.y }), + c.SDL_EVENT_MOUSE_MOTION => try self.route(.{ .kind = .mouse_move, .time = time, .x = event.motion.x, .y = event.motion.y, .flags = held() }), c.SDL_EVENT_MOUSE_BUTTON_DOWN, c.SDL_EVENT_MOUSE_BUTTON_UP => try self.route(.{ .kind = if (event.type == c.SDL_EVENT_MOUSE_BUTTON_DOWN) .mouse_down else .mouse_up, .time = time, .x = event.button.x, .y = event.button.y, .code = event.button.button, + .flags = held(), }), c.SDL_EVENT_MOUSE_WHEEL => { - const delta = event.wheel.y * @as(f32, if (event.wheel.direction == c.SDL_MOUSEWHEEL_FLIPPED) -1 else 1); - if (!std.math.isFinite(delta) or delta == 0) continue; - try self.route(.{ .kind = .wheel, .time = time, .x = event.wheel.mouse_x, .y = event.wheel.mouse_y, .value = @bitCast(delta) }); + const direction: f32 = if (event.wheel.direction == c.SDL_MOUSEWHEEL_FLIPPED) -1 else 1; + const dx = event.wheel.x * direction; + const dy = event.wheel.y * direction; + if (std.math.isFinite(dy) and dy != 0) try self.route(.{ .kind = .wheel, .time = time, .x = event.wheel.mouse_x, .y = event.wheel.mouse_y, .value = @bitCast(dy), .flags = held() }); + if (std.math.isFinite(dx) and dx != 0) try self.route(.{ .kind = .wheel_horizontal, .time = time, .x = event.wheel.mouse_x, .y = event.wheel.mouse_y, .value = @bitCast(dx), .flags = held() }); }, c.SDL_EVENT_KEY_DOWN, c.SDL_EVENT_KEY_UP => try self.route(.{ .kind = if (event.type == c.SDL_EVENT_KEY_DOWN) .key_down else .key_up, @@ -463,6 +597,16 @@ pub fn poll(self: *Host) !void { else => {}, } } + var buf: [@sizeOf(@FieldType(Dialog, "answer"))]u8 = undefined; + if (self.dialog.take(&buf)) |line| if (line.len > 0) { + self.console.print("> {s}", .{line}); + self.runLine(line, c.SDL_GetTicksNS()) catch {}; + }; +} + +/// The modifiers held now, for a pointer event's `flags`. +fn held() u8 { + return abi.Event.withModifiers(modifiers(c.SDL_GetModState())); } fn modifiers(mod: c.SDL_Keymod) u32 { @@ -475,7 +619,7 @@ fn modifiers(mod: c.SDL_Keymod) u32 { } fn consoleKey(e: abi.Event) ?Console.Key { - const key: abi.Key = @enumFromInt(@as(u8, @truncate(e.code))); + const key: abi.Key = @fromBackingInt(@intCast(@as(u8, @truncate(e.code)))); if (key == .u and e.value & abi.Key.Mod.ctrl != 0) return .clear; if (e.flags & abi.Event.repeat != 0 and key.isEnter()) return null; if (key.isEnter()) return .submit; @@ -509,9 +653,12 @@ const api: abi.Host.VTable = .{ .newImage = apiNewImage, .freeImage = apiFreeImage, .gpu = apiGpu, + .newBuffer = apiNewBuffer, + .freeBuffer = apiFreeBuffer, .imageSize = apiImageSize, .loadSound = apiLoadSound, .decodeSound = apiDecodeSound, + .samples = apiSamples, .readFile = apiReadFile, .env = apiEnv, .clock = apiClock, @@ -527,19 +674,38 @@ fn apiLoadImage(ctx: *anyopaque, path: []const u8) abi.Image { return hostOf(ctx).images.load(path); } fn apiDecodeImage(ctx: *anyopaque, png: []const u8) abi.Image { - return hostOf(ctx).images.decode(png); + const self = hostOf(ctx); + return self.remember(.images, self.images.decode(png)); } fn apiNewImage(ctx: *anyopaque, w: u32, h: u32) abi.Image { - return hostOf(ctx).images.new(w, h); + const self = hostOf(ctx); + return self.remember(.images, self.images.new(w, h)); } fn apiFreeImage(ctx: *anyopaque, image: abi.Image) void { const self = hostOf(ctx); + forget(&self.made.images, image); if (self.passes) |*g| g.forget(&self.images, image); self.images.free(image); } fn apiGpu(ctx: *anyopaque) bool { return hostOf(ctx).gpu != null; } +fn apiNewBuffer(ctx: *anyopaque, size: u32) abi.Buffer { + const self = hostOf(ctx); + const g = &(self.passes orelse return .none); + return self.remember(.buffers, g.newBuffer(size) catch |err| blk: { + self.print("gpu buffer: {t} ({s})", .{ err, c.SDL_GetError() }); + break :blk .none; + }); +} +fn apiFreeBuffer(ctx: *anyopaque, buffer: abi.Buffer) void { + const self = hostOf(ctx); + forget(&self.made.buffers, buffer); + if (self.passes) |*g| g.freeBuffer(buffer); +} +fn apiSamples(ctx: *anyopaque, audio: abi.Audio) []const f32 { + return hostOf(ctx).audio.samplesOf(audio); +} fn apiImageSize(ctx: *anyopaque, image: abi.Image) [2]u32 { return hostOf(ctx).images.size(image); } diff --git a/src/host/audio.zig b/src/host/audio.zig index 87332b1..aebb909 100644 --- a/src/host/audio.zig +++ b/src/host/audio.zig @@ -1,9 +1,11 @@ -//! Sound output: decoded clips played by reconciled voices, plus raw streams -//! the sketch fills itself. The audio thread only ever sees SDL's own copies -//! of the samples; sketch code and memory never run on it. +//! Sound: decoded clips played by reconciled voices, raw streams the sketch +//! fills itself, and the microphone, open while declared. The audio thread +//! only ever sees SDL's own copies of the samples; sketch code and memory +//! never run on it. const std = @import("std"); const c = @import("c"); const abi = @import("abi"); +const memory = @import("memory.zig"); pub const sample_rate = 48_000; const spec: c.SDL_AudioSpec = .{ .format = c.SDL_AUDIO_F32, .channels = 1, .freq = sample_rate }; @@ -21,6 +23,11 @@ pub const Audio = struct { streams: std.ArrayList(Stream) = .empty, status: std.ArrayList(abi.Stream) = .empty, scope: Scope = .{}, + /// The default recording device, bound to this stream while declared. + microphone: ?*c.SDL_AudioStream = null, + listening: bool = false, + /// Set when opening failed, so it is tried once per declaration. + deaf: bool = false, const Playing = struct { voice: abi.Voice, cursor: usize = 0, done: bool = false, seen: bool = true }; const Stream = struct { key: u32, out: *c.SDL_AudioStream, pushed: u64 = 0 }; @@ -34,6 +41,7 @@ pub const Audio = struct { } pub fn deinit(self: *Audio) void { + if (self.microphone) |m| c.SDL_DestroyAudioStream(m); self.scope.deinit(); for (self.streams.items) |s| c.SDL_DestroyAudioStream(s.out); if (self.voices_out) |s| c.SDL_DestroyAudioStream(s); @@ -70,7 +78,7 @@ pub const Audio = struct { /// `.none` when `wav` does not decode. pub fn decode(self: *Audio, wav: []const u8) abi.Audio { - return self.add(decodeWav(self.gpa, wav) catch return .none); + return self.add(decodeBytes(self.gpa, wav) catch return .none); } fn add(self: *Audio, samples: []f32) abi.Audio { @@ -78,17 +86,52 @@ pub const Audio = struct { self.gpa.free(samples); return .none; }; - return @enumFromInt(self.clips.items.len); + return @fromBackingInt(@intCast(self.clips.items.len)); } - fn samplesOf(self: *Audio, audio: abi.Audio) []const f32 { - const index = @intFromEnum(audio); + pub fn samplesOf(self: *Audio, audio: abi.Audio) []const f32 { + const index = @backingInt(audio); if (index == 0 or index > self.clips.items.len) return &.{}; return self.clips.items[index - 1]; } pub fn beginFrame(self: *Audio) void { for (self.voices.items) |*v| v.seen = false; + self.listening = false; + } + + /// The microphone was declared this frame. + pub fn listen(self: *Audio) void { + self.listening = true; + } + + /// What the microphone heard since the last call, into `arena`. + pub fn record(self: *Audio, arena: std.mem.Allocator) []const f32 { + const m = self.microphone orelse return &.{}; + const available = c.SDL_GetAudioStreamAvailable(m); + if (available <= 0) return &.{}; + const samples = arena.alloc(f32, @as(usize, @intCast(available)) / @sizeOf(f32)) catch return &.{}; + const got = c.SDL_GetAudioStreamData(m, samples.ptr, @intCast(samples.len * @sizeOf(f32))); + if (got <= 0) return &.{}; + return samples[0 .. @as(usize, @intCast(got)) / @sizeOf(f32)]; + } + + /// Opens or closes the microphone to match this frame's declaration. + fn reconcileMicrophone(self: *Audio) void { + if (!self.listening) { + if (self.microphone) |m| c.SDL_DestroyAudioStream(m); + self.microphone = null; + self.deaf = false; + return; + } + if (self.microphone != null or self.deaf) return; + const m = c.SDL_OpenAudioDeviceStream(c.SDL_AUDIO_DEVICE_DEFAULT_RECORDING, &spec, null, null) orelse { + std.log.warn("microphone: {s}", .{c.SDL_GetError()}); + self.deaf = true; + return; + }; + if (!c.SDL_ResumeAudioStreamDevice(m)) std.log.warn("microphone: {s}", .{c.SDL_GetError()}); + self.microphone = m; } /// Reconciles one declared voice with what is playing. @@ -128,8 +171,10 @@ pub const Audio = struct { return &self.streams.items[self.streams.items.len - 1]; } - /// Drops undeclared voices and keeps the voice mix queued ahead. + /// Drops undeclared voices, keeps the voice mix queued ahead, and opens + /// or closes the microphone. pub fn endFrame(self: *Audio) void { + self.reconcileMicrophone(); var i: usize = 0; while (i < self.voices.items.len) { if (self.voices.items[i].seen) i += 1 else _ = self.voices.swapRemove(i); @@ -183,28 +228,23 @@ pub const Audio = struct { } }; -fn decodeWav(gpa: std.mem.Allocator, wav: []const u8) ![]f32 { - const io = c.SDL_IOFromConstMem(wav.ptr, wav.len) orelse return error.OpenWav; - var from: c.SDL_AudioSpec = undefined; - var data: [*c]u8 = null; - var len: u32 = 0; - if (!c.SDL_LoadWAV_IO(io, true, &from, &data, &len)) return error.DecodeWav; - defer c.SDL_free(data); - return convert(gpa, &from, data[0..len]); +/// WAV, FLAC or Ogg Vorbis, by content (see `vendor/codecs.h`). +fn decodeBytes(gpa: std.mem.Allocator, bytes: []const u8) ![]f32 { + var channels: u32 = 0; + var rate: u32 = 0; + var frames: u64 = 0; + const raw = c.sketchbook_decode_audio(bytes.ptr, bytes.len, &channels, &rate, &frames) orelse return error.DecodeAudio; + defer c.sketchbook_free_audio(raw); + if (rate == 0 or rate > 192000 or channels == 0 or channels > 8 or frames == 0 or frames > @as(u64, rate) * 600) return error.UnsupportedSoundFormat; + const from: c.SDL_AudioSpec = .{ .format = c.SDL_AUDIO_F32, .channels = @intCast(channels), .freq = @intCast(rate) }; + return convert(gpa, &from, std.mem.sliceAsBytes(raw[0..@intCast(frames * channels)])); } fn decodeFile(gpa: std.mem.Allocator, path: []const u8) ![]f32 { - var buf: [std.fs.max_path_bytes]u8 = undefined; - const z = try std.fmt.bufPrintZ(&buf, "{s}", .{path}); - var info: c.SF_INFO = std.mem.zeroes(c.SF_INFO); - const file = c.sf_open(z.ptr, c.SFM_READ, &info) orelse return error.OpenSoundFile; - defer _ = c.sf_close(file); - if (info.samplerate <= 0 or info.samplerate > 192000 or info.channels <= 0 or info.channels > 8 or info.frames <= 0 or info.frames > @as(i64, info.samplerate) * 600) return error.UnsupportedSoundFormat; - const raw = try gpa.alloc(f32, @intCast(info.frames * info.channels)); - defer gpa.free(raw); - if (c.sf_readf_float(file, raw.ptr, info.frames) != info.frames) return error.TruncatedAudio; - const from: c.SDL_AudioSpec = .{ .format = c.SDL_AUDIO_F32, .channels = info.channels, .freq = info.samplerate }; - return convert(gpa, &from, std.mem.sliceAsBytes(raw)); + const bytes = memory.mapReadOnly(path); + if (bytes.len == 0) return error.OpenSoundFile; + defer std.posix.munmap(bytes); + return decodeBytes(gpa, bytes); } /// To host format: f32 mono at `sample_rate`, all finite. diff --git a/src/host/fields.zig b/src/host/fields.zig index 215f176..ba8fc03 100644 --- a/src/host/fields.zig +++ b/src/host/fields.zig @@ -100,7 +100,7 @@ test "fields format, parse and compare by kind" { const S = extern struct { mood: enum(u32) { calm, angry = 5 } = .calm, x: i16 = -3, speed: f32 = 1.5, on: bool = false }; var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); - const layout = try @import("memory.zig").Layout.copy(arena.allocator(), abi.layout.of(S, .forbidden)); + const layout = try @import("memory.zig").Layout.copy(arena.allocator(), abi.layout.of(S, .state)); var s: S = .{}; const bytes = std.mem.asBytes(&s); try std.testing.expect(matches(layout.find("x").?, bytes, "-3")); diff --git a/src/host/gfx.zig b/src/host/gfx.zig index e00d325..16148a6 100644 --- a/src/host/gfx.zig +++ b/src/host/gfx.zig @@ -27,7 +27,7 @@ pub const Images = struct { pub fn load(self: *Images, path: []const u8) abi.Image { if (self.paths.get(path)) |image| return image; var buf: [std.fs.max_path_bytes]u8 = undefined; - const z = std.fmt.bufPrintZ(&buf, "{s}", .{path}) catch return .none; + const z = std.mem.printSentinel(&buf, "{s}", .{path}, 0) catch return .none; const surface = c.SDL_LoadPNG(z.ptr); if (surface == null) std.log.warn("image {s}: {s}", .{ path, c.SDL_GetError() }); const image = self.add(surface); @@ -83,7 +83,7 @@ pub const Images = struct { } pub fn get(self: *Images, image: abi.Image) ?*Entry { - const index = @intFromEnum(image); + const index = @backingInt(image); if (index == 0 or index > self.entries.items.len) return null; return &self.entries.items[index - 1]; } @@ -99,7 +99,7 @@ pub const Images = struct { fn append(self: *Images, entry: Entry) abi.Image { self.entries.append(self.gpa, entry) catch return .none; - return @enumFromInt(self.entries.items.len); + return @fromBackingInt(@intCast(self.entries.items.len)); } fn setup(texture: *c.SDL_Texture) void { @@ -112,6 +112,7 @@ pub const Images = struct { pub fn draw(renderer: *c.SDL_Renderer, images: *Images, command: abi.Command) void { switch (command) { .clear => |color| { + if (@import("builtin").os.tag == .linux) c.sk_text_begin_frame(renderer); setColor(renderer, color); _ = c.SDL_RenderClear(renderer); }, @@ -146,8 +147,10 @@ pub fn draw(renderer: *c.SDL_Renderer, images: *Images, command: abi.Command) vo } } -/// SDL's built-in 8x8 font, `scale` times larger. Stops at the first NUL. +/// Native shaped text on Linux, using a 13px monospace face at scale 1. +/// Other targets retain SDL's built-in 8x8 fallback. Coordinates are line-box tops. pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: []const u8, color: abi.Color) void { + if (@import("builtin").os.tag == .linux and c.sk_text_draw(renderer, x, y, scale, bytes.ptr, bytes.len, color.r, color.g, color.b, color.a)) return; var buf: [1024]u8 = undefined; const len = @min(std.mem.indexOfScalar(u8, bytes, 0) orelse bytes.len, buf.len - 1); @memcpy(buf[0..len], bytes[0..len]); @@ -157,9 +160,22 @@ pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: [] var sy: f32 = 1; _ = c.SDL_GetRenderScale(renderer, &sx, &sy); const s = @max(scale, 1); + // The clip rect scales with the renderer: shrink it to match, to the + // whole pixels around it (so it may let up to `s - 1` pixels more through). + var clip: c.SDL_Rect = undefined; + const clipped = c.SDL_RenderClipEnabled(renderer) and c.SDL_GetRenderClipRect(renderer, &clip); _ = c.SDL_SetRenderScale(renderer, sx * s, sy * s); + if (clipped and s != 1) { + const x0 = @floor(@as(f32, @floatFromInt(clip.x)) / s); + const y0 = @floor(@as(f32, @floatFromInt(clip.y)) / s); + const x1 = @ceil(@as(f32, @floatFromInt(clip.x + clip.w)) / s); + const y1 = @ceil(@as(f32, @floatFromInt(clip.y + clip.h)) / s); + const scaled: c.SDL_Rect = .{ .x = @intFromFloat(x0), .y = @intFromFloat(y0), .w = @intFromFloat(x1 - x0), .h = @intFromFloat(y1 - y0) }; + _ = c.SDL_SetRenderClipRect(renderer, &scaled); + } _ = c.SDL_RenderDebugText(renderer, x / s, y / s, &buf); _ = c.SDL_SetRenderScale(renderer, sx, sy); + if (clipped and s != 1) _ = c.SDL_SetRenderClipRect(renderer, &clip); } fn missing(renderer: *c.SDL_Renderer, dst: abi.Rect) void { diff --git a/src/host/gpu.zig b/src/host/gpu.zig index 34c817f..9c77205 100644 --- a/src/host/gpu.zig +++ b/src/host/gpu.zig @@ -1,7 +1,7 @@ -//! Runs `abi.gpu` passes with SDL_GPU: the translation from GPU work as data -//! to a real API. Shaders and pipelines are built on first use and cached by -//! the hash of their description; entries a sketch stops using (an edited -//! shader, after a reload) are released after a while. +//! Runs `abi.gpu` passes and dispatches with SDL_GPU: the translation from +//! GPU work as data to a real API. Shaders and pipelines are built on first +//! use and cached by the hash of their description; entries a sketch stops +//! using (an edited shader, after a reload) are released after a while. const Gpu = @This(); const std = @import("std"); const c = @import("c"); @@ -17,6 +17,11 @@ device: *c.SDL_GPUDevice, stencil_format: c.SDL_GPUTextureFormat, shaders: std.AutoHashMapUnmanaged(u64, Cached(c.SDL_GPUShader)) = .empty, pipelines: std.AutoHashMapUnmanaged(u64, Cached(c.SDL_GPUGraphicsPipeline)) = .empty, +kernels: std.AutoHashMapUnmanaged(u64, Cached(c.SDL_GPUComputePipeline)) = .empty, +/// Storage buffers by handle: `Buffer` n is `buffers[n - 1]`, null once freed. +buffers: std.ArrayList(?Storage) = .empty, +/// Uploads to storage buffers go through this, in chunks of its size. +staging: ?*c.SDL_GPUTransferBuffer = null, /// Render targets per image: the resolved texture the renderer shows, and /// multisampled and stencil attachments as passes ask for them. targets: std.AutoHashMapUnmanaged(abi.Image, Target) = .empty, @@ -25,11 +30,89 @@ vertex_capacity: u32 = 0, upload: ?*c.SDL_GPUTransferBuffer = null, upload_capacity: u32 = 0, frame: u64 = 0, +/// Set by `--profile-gpu`: every command is timed alone (see `Profile`). +profile: ?Profile = null, +/// Downloads in flight, oldest first, and transfer buffers to reuse. +downloads: std.ArrayList(Download) = .empty, +spare: std.ArrayList(Transfer) = .empty, + +const Transfer = struct { buffer: *c.SDL_GPUTransferBuffer, size: u32 }; +const Download = struct { key: u32, size: u32, transfer: Transfer, fence: *c.SDL_GPUFence }; + +/// GPU time per labelled command. Each command starts on an idle GPU and is +/// waited for, so its time is its own: the total of a frame is what the +/// GPU spends on it, though the waiting makes the frame itself slower. +pub const Profile = struct { + entries: std.StringArrayHashMapUnmanaged(Entry) = .empty, + arena: std.heap.ArenaAllocator, + + const Entry = struct { count: u64 = 0, total: u64 = 0, worst: u64 = 0 }; + + pub fn init(gpa: std.mem.Allocator) Profile { + return .{ .arena = .init(gpa) }; + } + + pub fn deinit(p: *Profile) void { + p.arena.deinit(); + } + + fn add(p: *Profile, label: []const u8, ns: u64) void { + const a = p.arena.allocator(); + const entry = p.entries.getOrPut(a, label) catch return; + if (!entry.found_existing) { + entry.key_ptr.* = a.dupe(u8, label) catch "?"; + entry.value_ptr.* = .{}; + } + entry.value_ptr.count += 1; + entry.value_ptr.total += ns; + entry.value_ptr.worst = @max(entry.value_ptr.worst, ns); + } + + pub fn report(p: *Profile, frames: u64) void { + std.debug.print("gpu time over {d} frames (ms)\n{s: <24} {s: >7} {s: >9} {s: >9} {s: >11}\n", .{ frames, "command", "count", "mean", "worst", "per frame" }); + var sum: u64 = 0; + var it = p.entries.iterator(); + while (it.next()) |e| { + const v = e.value_ptr.*; + sum += v.total; + std.debug.print("{s: <24} {d: >7} {d: >9.3} {d: >9.3} {d: >11.3}\n", .{ e.key_ptr.*, v.count, ms(v.total) / @as(f64, @floatFromInt(v.count)), ms(v.worst), ms(v.total) / @as(f64, @floatFromInt(@max(frames, 1))) }); + } + std.debug.print("{s: <24} {s: >7} {s: >9} {s: >9} {d: >11.3}\n", .{ "all", "", "", "", ms(sum) / @as(f64, @floatFromInt(@max(frames, 1))) }); + } + + fn ms(ns: u64) f64 { + return @as(f64, @floatFromInt(ns)) / std.time.ns_per_ms; + } +}; + +/// Submits `cmd`; when profiling, waits for it and books its time from +/// `start` (taken on an idle GPU, before recording) under `label`. +fn submit(g: *Gpu, cmd: *c.SDL_GPUCommandBuffer, label: []const u8, start: u64) !void { + const p = &(g.profile orelse { + if (!c.SDL_SubmitGPUCommandBuffer(cmd)) return error.GpuSubmit; + return; + }); + const fence = c.SDL_SubmitGPUCommandBufferAndAcquireFence(cmd) orelse return error.GpuSubmit; + defer c.SDL_ReleaseGPUFence(g.device, fence); + _ = c.SDL_WaitForGPUFences(g.device, true, &fence, 1); + p.add(label, c.SDL_GetTicksNS() - start); +} + +/// When profiling: waits for the GPU to be idle, then the time to start from. +fn startTiming(g: *Gpu) u64 { + if (g.profile != null) _ = c.SDL_WaitForGPUIdle(g.device); + return c.SDL_GetTicksNS(); +} + +/// The most bytes one buffer upload stages at a time. +const staging_size = 4 << 20; fn Cached(comptime T: type) type { return struct { object: *T, used: u64 }; } +const Storage = struct { buffer: *c.SDL_GPUBuffer, size: u32 }; + const Target = struct { width: u32, height: u32, @@ -50,27 +133,49 @@ pub fn init(gpa: std.mem.Allocator, device: *c.SDL_GPUDevice) Gpu { } pub fn deinit(g: *Gpu, images: *gfx.Images) void { + if (g.profile) |*p| { + p.report(g.frame); + p.deinit(); + } var shaders = g.shaders.valueIterator(); while (shaders.next()) |s| c.SDL_ReleaseGPUShader(g.device, s.object); var pipelines = g.pipelines.valueIterator(); while (pipelines.next()) |p| c.SDL_ReleaseGPUGraphicsPipeline(g.device, p.object); + var kernels = g.kernels.valueIterator(); + while (kernels.next()) |k| c.SDL_ReleaseGPUComputePipeline(g.device, k.object); + for (g.buffers.items) |entry| if (entry) |b| c.SDL_ReleaseGPUBuffer(g.device, b.buffer); + for (g.downloads.items) |d| { + _ = c.SDL_WaitForGPUFences(g.device, true, &d.fence, 1); + c.SDL_ReleaseGPUFence(g.device, d.fence); + c.SDL_ReleaseGPUTransferBuffer(g.device, d.transfer.buffer); + } + for (g.spare.items) |t| c.SDL_ReleaseGPUTransferBuffer(g.device, t.buffer); + g.downloads.deinit(g.gpa); + g.spare.deinit(g.gpa); + if (g.staging) |b| c.SDL_ReleaseGPUTransferBuffer(g.device, b); var targets = g.targets.iterator(); while (targets.next()) |entry| g.releaseTarget(images, entry.key_ptr.*, entry.value_ptr.*); if (g.vertices) |b| c.SDL_ReleaseGPUBuffer(g.device, b); if (g.upload) |b| c.SDL_ReleaseGPUTransferBuffer(g.device, b); g.shaders.deinit(g.gpa); g.pipelines.deinit(g.gpa); + g.kernels.deinit(g.gpa); + g.buffers.deinit(g.gpa); g.targets.deinit(g.gpa); } /// Releases what nothing has used for a while. pub fn endFrame(g: *Gpu) void { g.frame += 1; - inline for (.{ &g.pipelines, &g.shaders }) |map| { + inline for (.{ &g.pipelines, &g.shaders, &g.kernels }) |map| { var it = map.iterator(); while (it.next()) |entry| { if (g.frame - entry.value_ptr.used < keep_frames) continue; - if (@TypeOf(map) == @TypeOf(&g.pipelines)) c.SDL_ReleaseGPUGraphicsPipeline(g.device, entry.value_ptr.object) else c.SDL_ReleaseGPUShader(g.device, entry.value_ptr.object); + switch (@TypeOf(map)) { + @TypeOf(&g.pipelines) => c.SDL_ReleaseGPUGraphicsPipeline(g.device, entry.value_ptr.object), + @TypeOf(&g.kernels) => c.SDL_ReleaseGPUComputePipeline(g.device, entry.value_ptr.object), + else => c.SDL_ReleaseGPUShader(g.device, entry.value_ptr.object), + } map.removeByPtr(entry.key_ptr); // The iterator is invalid after a removal: the rest waits a frame. break; @@ -83,6 +188,7 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { const samples = g.supportedSamples(pass.samples); const t = try g.renderTarget(images, pass.image, pass.width, pass.height, samples, pass.stencil); + const start = g.startTiming(); // Every draw's vertices go up in one copy. var total: u32 = 0; for (pass.draws) |d| total += @intCast(std.mem.alignForward(usize, d.vertices.len, 4)); @@ -137,7 +243,10 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { c.SDL_BindGPUGraphicsPipeline(rp, built); bound = built; } - c.SDL_BindGPUVertexBuffers(rp, 0, &.{ .buffer = g.vertices, .offset = offset }, 1); + if (d.vertices.len > 0) c.SDL_BindGPUVertexBuffers(rp, 0, &.{ .buffer = g.vertices, .offset = offset }, 1); + var bound_storage: [8]*c.SDL_GPUBuffer = undefined; + if (d.vertex_storage.len > 0) c.SDL_BindGPUVertexStorageBuffers(rp, 0, &bound_storage, g.resolve(d.vertex_storage, &bound_storage) catch continue); + if (d.fragment_storage.len > 0) c.SDL_BindGPUFragmentStorageBuffers(rp, 0, &bound_storage, g.resolve(d.fragment_storage, &bound_storage) catch continue); if (d.vertex_uniforms.len > 0) c.SDL_PushGPUVertexUniformData(cmd, 0, d.vertex_uniforms.ptr, @intCast(d.vertex_uniforms.len)); if (d.fragment_uniforms.len > 0) c.SDL_PushGPUFragmentUniformData(cmd, 0, d.fragment_uniforms.ptr, @intCast(d.fragment_uniforms.len)); if (pass.stencil) c.SDL_SetGPUStencilReference(rp, d.stencil_reference); @@ -147,7 +256,171 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { c.SDL_DrawGPUPrimitives(rp, d.vertex_count, 1, 0, 0); } c.SDL_EndGPURenderPass(rp); + try g.submit(cmd, if (pass.label.len > 0) pass.label else "pass", start); +} + +/// Runs one dispatch in a compute pass of its own. +pub fn compute(g: *Gpu, d: abi.gpu.Dispatch) !void { + if (d.groups[0] == 0 or d.groups[1] == 0 or d.groups[2] == 0) return; + const built = try g.kernel(d.kernel); + const start = g.startTiming(); + var readonly: [8]*c.SDL_GPUBuffer = undefined; + const readonly_count = try g.resolve(d.readonly, &readonly); + var readwrite: [8]*c.SDL_GPUBuffer = undefined; + const readwrite_count = try g.resolve(d.readwrite, &readwrite); + var bindings: [8]c.SDL_GPUStorageBufferReadWriteBinding = undefined; + for (readwrite[0..readwrite_count], bindings[0..readwrite_count]) |b, *binding| binding.* = std.mem.zeroInit(c.SDL_GPUStorageBufferReadWriteBinding, .{ .buffer = b }); + const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; + const pass = c.SDL_BeginGPUComputePass(cmd, null, 0, &bindings, readwrite_count) orelse { + _ = c.SDL_CancelGPUCommandBuffer(cmd); + return error.GpuComputePass; + }; + c.SDL_BindGPUComputePipeline(pass, built); + if (readonly_count > 0) c.SDL_BindGPUComputeStorageBuffers(pass, 0, &readonly, readonly_count); + if (d.uniforms.len > 0) c.SDL_PushGPUComputeUniformData(cmd, 0, d.uniforms.ptr, @intCast(d.uniforms.len)); + c.SDL_DispatchGPUCompute(pass, d.groups[0], d.groups[1], d.groups[2]); + c.SDL_EndGPUComputePass(pass); + try g.submit(cmd, if (d.label.len > 0) d.label else "compute", start); +} + +/// A zeroed storage buffer, readable by every stage and writable by compute. +pub fn newBuffer(g: *Gpu, size: u32) !abi.Buffer { + if (size == 0) return error.EmptyBuffer; + const usage = c.SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ | c.SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_READ | c.SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_WRITE; + const buffer = c.SDL_CreateGPUBuffer(g.device, &.{ .usage = usage, .size = size, .props = 0 }) orelse return error.GpuBuffer; + errdefer c.SDL_ReleaseGPUBuffer(g.device, buffer); + try g.buffers.append(g.gpa, .{ .buffer = buffer, .size = size }); + const handle: abi.Buffer = @fromBackingInt(@intCast(g.buffers.items.len)); + // Vulkan leaves new memory undefined; sketches get zeroes, copied from + // one zeroed staging block as many times as it takes. + const staging = try g.stagingBuffer(); + const block: u32 = @min(size, staging_size); + const mapped: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, staging, true) orelse return error.GpuMap); + @memset(mapped[0..block], 0); + c.SDL_UnmapGPUTransferBuffer(g.device, staging); + const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; + const copy = c.SDL_BeginGPUCopyPass(cmd); + var offset: u32 = 0; + while (offset < size) : (offset += block) { + c.SDL_UploadToGPUBuffer(copy, &.{ .transfer_buffer = staging, .offset = 0 }, &.{ .buffer = buffer, .offset = offset, .size = @min(block, size - offset) }, false); + } + c.SDL_EndGPUCopyPass(copy); if (!c.SDL_SubmitGPUCommandBuffer(cmd)) return error.GpuSubmit; + return handle; +} + +fn stagingBuffer(g: *Gpu) !*c.SDL_GPUTransferBuffer { + if (g.staging == null) g.staging = c.SDL_CreateGPUTransferBuffer(g.device, &.{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = staging_size, .props = 0 }) orelse return error.GpuBuffer; + return g.staging.?; +} + +pub fn freeBuffer(g: *Gpu, handle: abi.Buffer) void { + const index = @backingInt(handle); + if (index == 0 or index > g.buffers.items.len) return; + const entry = &g.buffers.items[index - 1]; + if (entry.*) |b| c.SDL_ReleaseGPUBuffer(g.device, b.buffer); + entry.* = null; +} + +/// Copies `bytes` into a buffer at `offset`, through the staging buffer. +pub fn write(g: *Gpu, handle: abi.Buffer, offset: u32, bytes: []const u8) !void { + const target = g.storage(handle) orelse return error.UnknownBuffer; + if (@as(u64, offset) + bytes.len > target.size) return error.UploadOutOfBounds; + const staging = try g.stagingBuffer(); + var done: usize = 0; + while (done < bytes.len) { + const chunk = bytes[done..][0..@min(staging_size, bytes.len - done)]; + const start = g.startTiming(); + // Cycling: a chunk still in flight keeps its own memory. + const mapped: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, staging, true) orelse return error.GpuMap); + @memcpy(mapped[0..chunk.len], chunk); + c.SDL_UnmapGPUTransferBuffer(g.device, staging); + const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; + const copy = c.SDL_BeginGPUCopyPass(cmd); + c.SDL_UploadToGPUBuffer(copy, &.{ .transfer_buffer = staging, .offset = 0 }, &.{ .buffer = target.buffer, .offset = offset + @as(u32, @intCast(done)), .size = @intCast(chunk.len) }, false); + c.SDL_EndGPUCopyPass(copy); + try g.submit(cmd, "upload", start); + done += chunk.len; + } +} + +/// Starts copying `size` bytes of a buffer from `offset` back to the CPU; +/// `collect` hands them over once the GPU is done, a frame or more later. +pub fn download(g: *Gpu, d: @FieldType(abi.Command, "download")) !void { + const source = g.storage(d.buffer) orelse return error.UnknownBuffer; + if (d.size == 0 or @as(u64, d.offset) + d.size > source.size) return error.DownloadOutOfBounds; + const transfer = for (g.spare.items, 0..) |t, i| { + if (t.size >= d.size) break g.spare.swapRemove(i); + } else Transfer{ + .buffer = c.SDL_CreateGPUTransferBuffer(g.device, &.{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_DOWNLOAD, .size = d.size, .props = 0 }) orelse return error.GpuBuffer, + .size = d.size, + }; + errdefer g.spare.append(g.gpa, transfer) catch c.SDL_ReleaseGPUTransferBuffer(g.device, transfer.buffer); + const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; + const copy = c.SDL_BeginGPUCopyPass(cmd); + c.SDL_DownloadFromGPUBuffer(copy, &.{ .buffer = source.buffer, .offset = d.offset, .size = d.size }, &.{ .transfer_buffer = transfer.buffer, .offset = 0 }); + c.SDL_EndGPUCopyPass(copy); + const fence = c.SDL_SubmitGPUCommandBufferAndAcquireFence(cmd) orelse return error.GpuSubmit; + g.downloads.append(g.gpa, .{ .key = d.key, .size = d.size, .transfer = transfer, .fence = fence }) catch |err| { + _ = c.SDL_WaitForGPUFences(g.device, true, &fence, 1); + c.SDL_ReleaseGPUFence(g.device, fence); + return err; + }; +} + +/// The downloads the GPU has finished, copied into `arena`, in the order +/// they were asked for. +pub fn collect(g: *Gpu, arena: std.mem.Allocator) ![]const abi.Download { + var done: std.ArrayList(abi.Download) = .empty; + while (g.downloads.items.len > 0) { + const d = g.downloads.items[0]; + if (!c.SDL_QueryGPUFence(g.device, d.fence)) break; + _ = g.downloads.orderedRemove(0); + c.SDL_ReleaseGPUFence(g.device, d.fence); + defer g.spare.append(g.gpa, d.transfer) catch c.SDL_ReleaseGPUTransferBuffer(g.device, d.transfer.buffer); + const mapped: [*]const u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, d.transfer.buffer, false) orelse return error.GpuMap); + defer c.SDL_UnmapGPUTransferBuffer(g.device, d.transfer.buffer); + try done.append(arena, .{ .key = d.key, .bytes = try arena.dupe(u8, mapped[0..d.size]) }); + } + return done.items; +} + +fn storage(g: *Gpu, handle: abi.Buffer) ?Storage { + const index = @backingInt(handle); + if (index == 0 or index > g.buffers.items.len) return null; + return g.buffers.items[index - 1]; +} + +/// The SDL buffers behind `handles`, into `out`; returns how many. +fn resolve(g: *Gpu, handles: []const abi.Buffer, out: []*c.SDL_GPUBuffer) !u32 { + if (handles.len > out.len) return error.TooManyBuffers; + for (handles, out[0..handles.len]) |handle, *b| b.* = (g.storage(handle) orelse return error.UnknownBuffer).buffer; + return @intCast(handles.len); +} + +fn kernel(g: *Gpu, k: abi.gpu.Kernel) !*c.SDL_GPUComputePipeline { + var h = std.hash.Wyhash.init(0); + h.update(k.code); + inline for (.{ k.readonly_buffers, k.readwrite_buffers, k.uniform_buffers, k.threads[0], k.threads[1], k.threads[2] }) |v| h.update(std.mem.asBytes(&@as(u32, v))); + const key = h.final(); + const entry = try g.kernels.getOrPut(g.gpa, key); + if (!entry.found_existing) { + errdefer _ = g.kernels.remove(key); + var info: c.SDL_GPUComputePipelineCreateInfo = std.mem.zeroes(c.SDL_GPUComputePipelineCreateInfo); + info.code_size = k.code.len; + info.code = k.code.ptr; + info.entrypoint = "main"; + info.format = c.SDL_GPU_SHADERFORMAT_SPIRV; + info.num_readonly_storage_buffers = k.readonly_buffers; + info.num_readwrite_storage_buffers = k.readwrite_buffers; + info.num_uniform_buffers = k.uniform_buffers; + info.threadcount_x = k.threads[0]; + info.threadcount_y = k.threads[1]; + info.threadcount_z = k.threads[2]; + entry.value_ptr.object = c.SDL_CreateGPUComputePipeline(g.device, &info) orelse return error.GpuComputePipeline; + } + entry.value_ptr.used = g.frame; + return entry.value_ptr.object; } fn supportedSamples(g: *Gpu, wanted: u8) u8 { @@ -236,21 +509,22 @@ fn reserve(g: *Gpu, bytes: u32) !void { fn shader(g: *Gpu, s: abi.gpu.Shader, stage: c.SDL_GPUShaderStage) !struct { *c.SDL_GPUShader, u64 } { var h = std.hash.Wyhash.init(stage); - h.update(s.spirv); + h.update(s.code); h.update(std.mem.asBytes(&s.uniform_buffers)); + h.update(std.mem.asBytes(&s.storage_buffers)); const key = h.final(); const entry = try g.shaders.getOrPut(g.gpa, key); if (!entry.found_existing) { errdefer _ = g.shaders.remove(key); entry.value_ptr.object = c.SDL_CreateGPUShader(g.device, &.{ - .code_size = s.spirv.len, - .code = s.spirv.ptr, + .code_size = s.code.len, + .code = s.code.ptr, .entrypoint = "main", .format = c.SDL_GPU_SHADERFORMAT_SPIRV, .stage = stage, .num_samplers = 0, .num_storage_textures = 0, - .num_storage_buffers = 0, + .num_storage_buffers = s.storage_buffers, .num_uniform_buffers = s.uniform_buffers, .props = 0, }) orelse return error.GpuShader; @@ -264,9 +538,9 @@ fn pipeline(g: *Gpu, p: abi.gpu.Pipeline, samples: u8, stencil: bool) !*c.SDL_GP const fragment, const fragment_key = try g.shader(p.fragment, c.SDL_GPU_SHADERSTAGE_FRAGMENT); // Field by field: no padding bytes in the key. var h = std.hash.Wyhash.init(vertex_key ^ (fragment_key *% 31)); - inline for (.{ p.stride, @intFromEnum(p.primitive), @intFromEnum(p.blend), @intFromBool(p.write_color), samples, @intFromBool(stencil) }) |v| h.update(std.mem.asBytes(&@as(u32, v))); - for (p.attributes) |a| inline for (.{ a.location, @intFromEnum(a.format), a.offset }) |v| h.update(std.mem.asBytes(&@as(u32, v))); - if (p.stencil) |st| inline for (.{ st.front, st.back }) |face| inline for (.{ @intFromEnum(face.compare), @intFromEnum(face.pass), @intFromEnum(face.fail) }) |v| h.update(std.mem.asBytes(&@as(u32, v))); + inline for (.{ p.stride, @backingInt(p.primitive), @backingInt(p.blend), @intFromBool(p.write_color), samples, @intFromBool(stencil) }) |v| h.update(std.mem.asBytes(&@as(u32, v))); + for (p.attributes) |a| inline for (.{ a.location, @backingInt(a.format), a.offset }) |v| h.update(std.mem.asBytes(&@as(u32, v))); + if (p.stencil) |st| inline for (.{ st.front, st.back }) |face| inline for (.{ @backingInt(face.compare), @backingInt(face.pass), @backingInt(face.fail) }) |v| h.update(std.mem.asBytes(&@as(u32, v))); const key = h.final(); const entry = try g.pipelines.getOrPut(g.gpa, key); if (!entry.found_existing) { @@ -310,7 +584,8 @@ fn pipeline(g: *Gpu, p: abi.gpu.Pipeline, samples: u8, stencil: bool) !*c.SDL_GP info.fragment_shader = fragment; info.vertex_input_state = .{ .vertex_buffer_descriptions = &.{ .slot = 0, .pitch = p.stride, .input_rate = c.SDL_GPU_VERTEXINPUTRATE_VERTEX, .instance_step_rate = 0 }, - .num_vertex_buffers = 1, + // No attributes: the vertex shader makes its own vertices. + .num_vertex_buffers = @intFromBool(count > 0), .vertex_attributes = &attributes, .num_vertex_attributes = @intCast(count), }; @@ -338,9 +613,9 @@ fn pipeline(g: *Gpu, p: abi.gpu.Pipeline, samples: u8, stencil: bool) !*c.SDL_GP fn stencilFace(face: abi.gpu.StencilFace) c.SDL_GPUStencilOpState { // Both enums follow SDL's order, after its INVALID. return .{ - .compare_op = @as(u32, @intFromEnum(face.compare)) + 1, - .pass_op = @as(u32, @intFromEnum(face.pass)) + 1, - .fail_op = @as(u32, @intFromEnum(face.fail)) + 1, + .compare_op = @as(u32, @backingInt(face.compare)) + 1, + .pass_op = @as(u32, @backingInt(face.pass)) + 1, + .fail_op = @as(u32, @backingInt(face.fail)) + 1, .depth_fail_op = c.SDL_GPU_STENCILOP_KEEP, }; } diff --git a/src/host/library.zig b/src/host/library.zig index 6c9338f..6958eec 100644 --- a/src/host/library.zig +++ b/src/host/library.zig @@ -17,10 +17,10 @@ pub const Library = struct { const object = c.SDL_LoadObject(path.ptr) orelse return error.LoadLibrary; errdefer c.SDL_UnloadObject(object); var sketch: abi.Sketch = undefined; - inline for (@typeInfo(abi.Sketch).@"struct".fields) |field| { - const symbol = @field(abi.Sketch.symbols, field.name); + inline for (comptime std.meta.fieldNames(abi.Sketch)) |name| { + const symbol = @field(abi.Sketch.symbols, name); const address = c.SDL_LoadFunction(object, symbol) orelse return error.MissingEntryPoint; - @field(sketch, field.name) = @ptrCast(address); + @field(sketch, name) = @ptrCast(address); } if (sketch.info().abi != abi.hash) return error.AbiMismatch; return .{ .object = object, .sketch = sketch }; @@ -29,7 +29,7 @@ pub const Library = struct { /// Loads `source` through a uniquely named copy in `scratch_dir`. pub fn openCopy(io: std.Io, source: []const u8, scratch_dir: []const u8, serial: usize) !Library { var buf: [std.fs.max_path_bytes]u8 = undefined; - const copy = try std.fmt.bufPrintZ(&buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }); + const copy = try std.mem.printSentinel(&buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }, 0); const cwd = std.Io.Dir.cwd(); try cwd.copyFile(source, cwd, copy, io, .{}); defer cwd.deleteFile(io, copy) catch {}; @@ -53,7 +53,7 @@ pub const Watch = struct { errdefer _ = linux.close(fd); var buf: [std.fs.max_path_bytes]u8 = undefined; const dir = std.fs.path.dirname(path) orelse "."; - const dir_z = try std.fmt.bufPrintZ(&buf, "{s}", .{dir}); + const dir_z = try std.mem.printSentinel(&buf, "{s}", .{dir}, 0); if (@as(isize, @bitCast(linux.inotify_add_watch(fd, dir_z, linux.IN.MOVED_TO | linux.IN.CLOSE_WRITE))) < 0) return error.WatchFailed; return .{ .fd = fd, .name = std.fs.path.basename(path) }; } diff --git a/src/host/memory.zig b/src/host/memory.zig index dd9f632..fc44e50 100644 --- a/src/host/memory.zig +++ b/src/host/memory.zig @@ -4,6 +4,12 @@ //! slots, plus a header describing the layout, so the state outlives restarts, //! a probe can read it from another process, and a reload with a changed //! layout can carry the old values over field by field. +//! +//! `StoreFile`: a heap in a file, mapped at the same address in every run, +//! so what it holds may point into it: the sketch's `Store` and everything +//! allocated for it outlive restarts. +//! +//! `Session`: this process's memory, kept across reloads. const std = @import("std"); const posix = std.posix; const linux = std.os.linux; @@ -197,7 +203,7 @@ pub const StateFile = struct { try self.resize(stride); const h = self.header(); h.* = .{ .size = new.size, .alignment = new.alignment, .hash = new.hash, .stride = stride, .used = old_used, .field_count = 0, .strings_len = 0 }; - try self.writeLayout(); + try writeLayout(self.map[0..header_len], self.layout); var total: Migration = .{}; for (old_slots, 0..) |maybe_old, i| { @@ -232,33 +238,34 @@ pub const StateFile = struct { } self.map = try mapFile(self.fd, len, .{ .READ = true, .WRITE = true }); } +}; - fn writeLayout(self: *StateFile) !void { - const h = self.header(); - const fields: [*]StoredField = @ptrCast(@alignCast(self.map.ptr + @sizeOf(Header))); - const strings_start = @sizeOf(Header) + self.layout.fields.len * @sizeOf(StoredField); - var strings: usize = 0; - for (self.layout.fields, 0..) |leaf, i| { - if (strings_start + strings + leaf.path.len + leaf.tags.len > header_len) return error.LayoutTooLarge; - const base = strings_start + strings; - @memcpy(self.map[base..][0..leaf.path.len], leaf.path); - @memcpy(self.map[base + leaf.path.len ..][0..leaf.tags.len], leaf.tags); - fields[i] = .{ - .offset = leaf.offset, - .size = leaf.size, - .hash = leaf.hash, - .kind = leaf.kind, - .path = @intCast(strings), - .path_len = @intCast(leaf.path.len), - .tags = @intCast(strings + leaf.path.len), - .tags_len = @intCast(leaf.tags.len), - }; - strings += leaf.path.len + leaf.tags.len; - } - h.field_count = @intCast(self.layout.fields.len); - h.strings_len = @intCast(strings); +/// Writes `layout`'s leaves after the `Header` at the start of `region`. +fn writeLayout(region: []u8, layout: Layout) !void { + const h: *Header = @ptrCast(@alignCast(region.ptr)); + const fields: [*]StoredField = @ptrCast(@alignCast(region.ptr + @sizeOf(Header))); + const strings_start = @sizeOf(Header) + layout.fields.len * @sizeOf(StoredField); + var strings: usize = 0; + for (layout.fields, 0..) |leaf, i| { + if (strings_start + strings + leaf.path.len + leaf.tags.len > region.len) return error.LayoutTooLarge; + const base = strings_start + strings; + @memcpy(region[base..][0..leaf.path.len], leaf.path); + @memcpy(region[base + leaf.path.len ..][0..leaf.tags.len], leaf.tags); + fields[i] = .{ + .offset = leaf.offset, + .size = leaf.size, + .hash = leaf.hash, + .kind = leaf.kind, + .path = @intCast(strings), + .path_len = @intCast(leaf.path.len), + .tags = @intCast(strings + leaf.path.len), + .tags_len = @intCast(leaf.tags.len), + }; + strings += leaf.path.len + leaf.tags.len; } -}; + h.field_count = @intCast(layout.fields.len); + h.strings_len = @intCast(strings); +} /// The layout stored in a state file's header, or null if it has none. pub fn readLayout(arena: std.mem.Allocator, map: []const u8) !?Layout { @@ -306,15 +313,211 @@ pub fn mapReadOnly(path: []const u8) []align(page) const u8 { return posix.mmap(null, @intCast(len), .{ .READ = true }, .{ .TYPE = .SHARED }, fd, 0) catch &.{}; } +/// Where every store is mapped, in every process: far from where Linux puts +/// executables (0x55...) and mappings (0x7f...). +pub const store_base: usize = 0x2000_0000_0000; +/// The most a store can grow to. Only reserved: pages exist as they are used. +pub const store_reserve: usize = 64 << 30; +pub const store_magic = "SKSTORE1".*; +/// The layout region is a state file header; the allocator's own record +/// follows, then the heap. +const meta_offset = header_len; +const heap_offset = 2 * header_len; + +/// A heap in a file, with the sketch's `Store` at its root. Mapped at +/// `store_base` in every run, so pointers it holds into itself stay valid +/// across restarts. Not crash-safe: a crash in the middle of an allocation +/// can leave it inconsistent (`--fresh` starts over). +/// +/// Blocks come in power-of-two sizes, each class with a free list threaded +/// through the free blocks, so growing a list by doubling reuses what it +/// frees. The file grows by mapping more of it in place: nothing moves. +pub const StoreFile = struct { + fd: posix.fd_t, + /// Bytes of the file mapped at `store_base`. + len: usize, + /// The root's layout, owned by `arena`. + layout: Layout, + arena: std.heap.ArenaAllocator, + + const Meta = extern struct { + magic: [8]u8 = store_magic, + base: u64 = store_base, + /// Offset of the first byte never handed out. + top: u64 = heap_offset, + /// Offset of the sketch's Store, or 0 before the first adopt. + root: u64 = 0, + /// Offsets of the first free block of each size class (2^i bytes), 0 + /// when none; a free block's first 8 bytes hold the next one's. + free: [64]u64 = @splat(0), + }; + + pub fn open(gpa: std.mem.Allocator, path: []const u8, fresh: bool) !StoreFile { + const fd = try posix.openat(posix.AT.FDCWD, path, .{ .ACCMODE = .RDWR, .CREAT = true }, 0o600); + errdefer _ = posix.system.close(fd); + // The same file mapped by two processes would be two heaps writing + // over each other. + if (linux.errno(linux.flock(fd, posix.LOCK.EX | posix.LOCK.NB)) != .SUCCESS) return error.StoreInUse; + const reserved = posix.mmap(@ptrFromInt(store_base), store_reserve, .{}, .{ .TYPE = .PRIVATE, .ANONYMOUS = true, .NORESERVE = true, .FIXED_NOREPLACE = true }, -1, 0) catch return error.StoreAddressTaken; + // Old kernels treat FIXED_NOREPLACE as a hint. + if (@intFromPtr(reserved.ptr) != store_base) { + posix.munmap(reserved); + return error.StoreAddressTaken; + } + errdefer posix.munmap(reserved); + var self: StoreFile = .{ .fd = fd, .len = 0, .layout = .{}, .arena = .init(gpa) }; + errdefer self.arena.deinit(); + const size: usize = @intCast(try fileSize(fd)); + if (!fresh and size >= heap_offset) { + try self.grow(size); + const m = self.meta(); + if (std.mem.eql(u8, &m.magic, &store_magic) and m.base == store_base and m.top <= size) { + self.layout = try readLayout(self.arena.allocator(), self.region()) orelse .{}; + return self; + } + } + try self.wipe(); + return self; + } + + pub fn close(self: *StoreFile) void { + posix.munmap(@as([*]align(page) u8, @ptrFromInt(store_base))[0..store_reserve]); + _ = posix.system.close(self.fd); + self.arena.deinit(); + } + + /// Empties the store: no root, nothing allocated. + fn wipe(self: *StoreFile) !void { + // Mapped pages past the new end would fault: map the reserve back. + if (self.len > heap_offset) { + _ = try posix.mmap(@ptrFromInt(store_base + heap_offset), self.len - heap_offset, .{}, .{ .TYPE = .PRIVATE, .ANONYMOUS = true, .NORESERVE = true, .FIXED = true }, -1, 0); + self.len = heap_offset; + } + try truncate(self.fd, heap_offset); + try self.grow(heap_offset); + @memset(self.region(), 0); + self.header().* = .{ .magic = store_magic, .size = 0, .alignment = 1, .hash = 0, .stride = 0, .used = 0, .field_count = 0, .strings_len = 0 }; + self.meta().* = .{}; + self.layout = .{}; + } + + /// Maps the file up to `need` bytes, growing it when it is shorter. + fn grow(self: *StoreFile, need: usize) !void { + if (need <= self.len) return; + const len = std.mem.alignForward(usize, @max(need, self.len * 2, 1 << 20), page); + if (len > store_reserve) return error.StoreFull; + if (try fileSize(self.fd) < len) try truncate(self.fd, len); + _ = try posix.mmap(@ptrFromInt(store_base + self.len), len - self.len, .{ .READ = true, .WRITE = true }, .{ .TYPE = .SHARED, .FIXED = true }, self.fd, self.len); + self.len = len; + } + + fn region(_: *StoreFile) []u8 { + return @as([*]u8, @ptrFromInt(store_base))[0..header_len]; + } + fn header(_: *StoreFile) *Header { + return @ptrFromInt(store_base); + } + fn meta(_: *StoreFile) *Meta { + return @ptrFromInt(store_base + meta_offset); + } + + /// The sketch's Store. + pub fn root(self: *StoreFile) [*]u8 { + const offset = self.meta().root; + return @ptrFromInt(store_base + if (offset == 0) heap_offset else offset); + } + + pub const Adoption = StateFile.Adoption; + + /// Makes the root hold `new`'s layout: the new defaults, then every field + /// whose path and type are unchanged, copied over. Pointer fields keep + /// pointing where they did, since the store never moves. + pub fn adopt(self: *StoreFile, gpa: std.mem.Allocator, new: abi.Layout, defaults: *const fn (abi.Memory, [*]u8) callconv(.c) void) !Adoption { + const m = self.meta(); + if (m.root != 0 and self.layout.hash == new.hash and self.layout.size == new.size) return .unchanged; + const a = self.allocator(); + const alignment: std.mem.Alignment = .fromByteUnits(@max(new.alignment, 1)); + const fresh = a.rawAlloc(@max(new.size, 1), alignment, @returnAddress()) orelse return error.StoreFull; + @memset(fresh[0..@max(new.size, 1)], 0); + defaults(.store, fresh); + + var kept = self.arena; + defer kept.deinit(); + self.arena = .init(gpa); + const old_layout = self.layout; + self.layout = try Layout.copy(self.arena.allocator(), new); + var result: Adoption = .created; + if (m.root != 0) { + const old: [*]u8 = @ptrFromInt(store_base + m.root); + result = .{ .migrated = migrate(old_layout, old[0..old_layout.size], self.layout, fresh[0..new.size]) }; + a.rawFree(old[0..@max(old_layout.size, 1)], .fromByteUnits(@max(old_layout.alignment, 1)), @returnAddress()); + } + self.header().* = .{ .magic = store_magic, .size = new.size, .alignment = new.alignment, .hash = new.hash, .stride = 0, .used = 1, .field_count = 0, .strings_len = 0 }; + try writeLayout(self.region(), self.layout); + m.root = @intFromPtr(fresh) - store_base; + return result; + } + + pub fn allocator(self: *StoreFile) std.mem.Allocator { + return .{ .ptr = self, .vtable = &.{ .alloc = alloc, .resize = resize, .remap = remap, .free = free } }; + } + + fn class(len: usize, alignment: std.mem.Alignment) u6 { + return @intCast(std.math.log2_int_ceil(usize, @max(len, alignment.toByteUnits(), 16))); + } + + fn alloc(ctx: *anyopaque, len: usize, alignment: std.mem.Alignment, _: usize) ?[*]u8 { + const self: *StoreFile = @ptrCast(@alignCast(ctx)); + const m = self.meta(); + const c = class(len, alignment); + const size = @as(usize, 1) << c; + if (m.free[c] != 0) { + const block: [*]u8 = @ptrFromInt(store_base + m.free[c]); + if (alignment.check(@intFromPtr(block))) { + m.free[c] = @as(*const u64, @ptrCast(@alignCast(block))).*; + return block; + } + } + const offset = std.mem.alignForward(usize, m.top, @max(@min(size, page), alignment.toByteUnits())); + self.grow(offset + size) catch return null; + m.top = offset + size; + return @ptrFromInt(store_base + offset); + } + + /// In place within the block's size class. + fn resize(_: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, _: usize) bool { + return class(memory.len, alignment) == class(new_len, alignment); + } + + fn remap(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret: usize) ?[*]u8 { + return if (resize(ctx, memory, alignment, new_len, ret)) memory.ptr else null; + } + + fn free(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, _: usize) void { + const self: *StoreFile = @ptrCast(@alignCast(ctx)); + const m = self.meta(); + const c = class(memory.len, alignment); + @as(*u64, @ptrCast(@alignCast(memory.ptr))).* = m.free[c]; + m.free[c] = @intFromPtr(memory.ptr) - store_base; + } +}; + +fn truncate(fd: posix.fd_t, len: usize) !void { + switch (posix.errno(posix.system.ftruncate(fd, @intCast(len)))) { + .SUCCESS => {}, + else => |err| return posix.unexpectedErrno(err), + } +} + /// Session memory: survives reloads while its layout hash is unchanged. -/// `heap` is released with it. +/// Everything allocated from `heap` is released with it. pub const Session = struct { bytes: []align(16) u8 = &.{}, hash: u64 = 0, - heap: std.heap.ArenaAllocator, + heap: Heap, pub fn init(gpa: std.mem.Allocator) Session { - return .{ .heap = .init(gpa) }; + return .{ .heap = .{ .gpa = gpa } }; } pub fn deinit(self: *Session, gpa: std.mem.Allocator) void { @@ -328,7 +531,7 @@ pub const Session = struct { if (layout.alignment > 16) return error.SessionOveraligned; gpa.free(self.bytes); self.bytes = &.{}; - _ = self.heap.reset(.free_all); + self.heap.reset(); self.bytes = try gpa.alignedAlloc(u8, .@"16", @max(layout.size, 1)); @memset(self.bytes, 0); defaults(.session, self.bytes.ptr); @@ -337,13 +540,88 @@ pub const Session = struct { } }; +/// The session's allocator: general purpose, so memory frees one allocation +/// at a time (a growing list does not leave its old copies behind), and it +/// remembers what is still live, so a session reset releases all of it. +pub const Heap = struct { + gpa: std.mem.Allocator, + live: std.AutoHashMapUnmanaged(usize, Block) = .empty, + + const Block = struct { len: usize, alignment: std.mem.Alignment }; + + pub fn allocator(self: *Heap) std.mem.Allocator { + return .{ .ptr = self, .vtable = &.{ .alloc = alloc, .resize = resize, .remap = remap, .free = free } }; + } + + /// Releases every live allocation. + pub fn reset(self: *Heap) void { + var it = self.live.iterator(); + while (it.next()) |entry| { + const memory: [*]u8 = @ptrFromInt(entry.key_ptr.*); + self.gpa.rawFree(memory[0..entry.value_ptr.len], entry.value_ptr.alignment, @returnAddress()); + } + self.live.clearRetainingCapacity(); + } + + pub fn deinit(self: *Heap) void { + self.reset(); + self.live.deinit(self.gpa); + } + + fn alloc(ctx: *anyopaque, len: usize, alignment: std.mem.Alignment, ret: usize) ?[*]u8 { + const self: *Heap = @ptrCast(@alignCast(ctx)); + self.live.ensureUnusedCapacity(self.gpa, 1) catch return null; + const memory = self.gpa.rawAlloc(len, alignment, ret) orelse return null; + self.live.putAssumeCapacity(@intFromPtr(memory), .{ .len = len, .alignment = alignment }); + return memory; + } + + fn resize(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret: usize) bool { + const self: *Heap = @ptrCast(@alignCast(ctx)); + if (!self.gpa.rawResize(memory, alignment, new_len, ret)) return false; + self.live.getPtr(@intFromPtr(memory.ptr)).?.len = new_len; + return true; + } + + fn remap(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret: usize) ?[*]u8 { + const self: *Heap = @ptrCast(@alignCast(ctx)); + self.live.ensureUnusedCapacity(self.gpa, 1) catch return null; + const moved = self.gpa.rawRemap(memory, alignment, new_len, ret) orelse return null; + _ = self.live.remove(@intFromPtr(memory.ptr)); + self.live.putAssumeCapacity(@intFromPtr(moved), .{ .len = new_len, .alignment = alignment }); + return moved; + } + + fn free(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, ret: usize) void { + const self: *Heap = @ptrCast(@alignCast(ctx)); + _ = self.live.remove(@intFromPtr(memory.ptr)); + self.gpa.rawFree(memory, alignment, ret); + } +}; + +test "the session heap frees one allocation at a time, and the rest on reset" { + var heap: Heap = .{ .gpa = std.testing.allocator }; + defer heap.deinit(); + const a = heap.allocator(); + var list: std.ArrayList(u32) = .empty; + for (0..1000) |i| try list.append(a, @intCast(i)); + try std.testing.expectEqual(@as(usize, 1), heap.live.count()); + const loose = try a.alloc(u8, 100); + a.free(loose); + try std.testing.expectEqual(@as(usize, 1), heap.live.count()); + _ = try a.create(u64); + heap.reset(); + try std.testing.expectEqual(@as(usize, 0), heap.live.count()); + // std.testing.allocator fails the test if anything leaked. +} + test "enums migrate by tag name" { const V1 = struct { mood: enum(u8) { calm, lost, angry } = .calm, gone: enum(u8) { a, b } = .b }; const V2 = struct { mood: enum(u16) { angry = 7, calm, sleepy } = .sleepy, gone: enum(u8) { c } = .c }; var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); - const l1 = try Layout.copy(arena.allocator(), abi.layout.of(V1, .forbidden)); - const l2 = try Layout.copy(arena.allocator(), abi.layout.of(V2, .forbidden)); + const l1 = try Layout.copy(arena.allocator(), abi.layout.of(V1, .state)); + const l2 = try Layout.copy(arena.allocator(), abi.layout.of(V2, .state)); for ([_]struct { @FieldType(V1, "mood"), @FieldType(V2, "mood") }{ .{ .calm, .calm }, .{ .angry, .angry }, .{ .lost, .sleepy } }) |case| { const old: V1 = .{ .mood = case[0] }; var new: V2 = .{}; @@ -358,8 +636,8 @@ test "migration keeps unchanged fields and defaults the rest" { const V2 = extern struct { b: f32 = 20, a: u32 = 10, b2: [2]u8 = .{ 7, 7 } }; var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); - const l1 = try Layout.copy(arena.allocator(), abi.layout.of(V1, .forbidden)); - const l2 = try Layout.copy(arena.allocator(), abi.layout.of(V2, .forbidden)); + const l1 = try Layout.copy(arena.allocator(), abi.layout.of(V1, .state)); + const l2 = try Layout.copy(arena.allocator(), abi.layout.of(V2, .state)); const old: V1 = .{ .a = 42, .b = 0.5 }; var new: V2 = .{}; const m = migrate(l1, std.mem.asBytes(&old), l2, std.mem.asBytes(&new)); @@ -382,12 +660,12 @@ test "state file migrates live state and snapshots, and a reopen reads the layou var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var buf: [std.fs.max_path_bytes]u8 = undefined; - const path = try std.fmt.bufPrint(&buf, ".zig-cache/tmp/{s}/test.state", .{&tmp.sub_path}); + const path = try std.mem.print(&buf, ".zig-cache/tmp/{s}/test.state", .{&tmp.sub_path}); const gpa = std.testing.allocator; { var file = try StateFile.open(gpa, path); defer file.close(); - try std.testing.expectEqual(StateFile.Adoption.created, try file.adopt(gpa, abi.layout.of(V1, .forbidden), D.v1)); + try std.testing.expectEqual(StateFile.Adoption.created, try file.adopt(gpa, abi.layout.of(V1, .state), D.v1)); file.markUsed(0, true); @as(*V1, @ptrCast(file.slot(0).ptr)).score = 99; @memcpy(file.slot(3), file.slot(0)); @@ -396,7 +674,7 @@ test "state file migrates live state and snapshots, and a reopen reads the layou var file = try StateFile.open(gpa, path); defer file.close(); try std.testing.expectEqual(abi.layout.hash(V1), file.layout.hash); - const adoption = try file.adopt(gpa, abi.layout.of(V2, .forbidden), D.v2); + const adoption = try file.adopt(gpa, abi.layout.of(V2, .state), D.v2); try std.testing.expectEqual(@as(usize, 1), adoption.migrated.reset); for ([_]usize{ 0, 3 }) |i| { const s: *V2 = @ptrCast(file.slot(i).ptr); @@ -404,3 +682,44 @@ test "state file migrates live state and snapshots, and a reopen reads the layou } try std.testing.expect(!file.used(1)); } + +test "a store keeps its root and what it points to across a reopen, and migrates by field" { + const V1 = struct { list: std.ArrayList(u32) = .empty, count: u32 = 0 }; + const V2 = struct { added: u8 = 7, list: std.ArrayList(u32) = .empty }; + const D = struct { + fn v1(_: abi.Memory, p: [*]u8) callconv(.c) void { + @as(*V1, @ptrCast(@alignCast(p))).* = .{}; + } + fn v2(_: abi.Memory, p: [*]u8) callconv(.c) void { + @as(*V2, @ptrCast(@alignCast(p))).* = .{}; + } + }; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var buf: [std.fs.max_path_bytes]u8 = undefined; + const path = try std.mem.print(&buf, ".zig-cache/tmp/{s}/test.store", .{&tmp.sub_path}); + const gpa = std.testing.allocator; + { + var store = try StoreFile.open(gpa, path, false); + defer store.close(); + try std.testing.expectEqual(StoreFile.Adoption.created, try store.adopt(gpa, abi.layout.of(V1, .store), D.v1)); + const v: *V1 = @ptrCast(@alignCast(store.root())); + for (0..10_000) |i| try v.list.append(store.allocator(), @intCast(i)); + v.count = 3; + try std.testing.expectError(error.StoreInUse, StoreFile.open(gpa, path, false)); + } + { + var store = try StoreFile.open(gpa, path, false); + defer store.close(); + try std.testing.expectEqual(StoreFile.Adoption.unchanged, try store.adopt(gpa, abi.layout.of(V1, .store), D.v1)); + const adoption = try store.adopt(gpa, abi.layout.of(V2, .store), D.v2); + try std.testing.expectEqual(@as(usize, 1), adoption.migrated.reset); + const v: *V2 = @ptrCast(@alignCast(store.root())); + try std.testing.expectEqual(@as(u8, 7), v.added); + try std.testing.expectEqual(@as(u32, 9999), v.list.items[9999]); + v.list.deinit(store.allocator()); + } + var store = try StoreFile.open(gpa, path, true); + defer store.close(); + try std.testing.expectEqual(StoreFile.Adoption.created, try store.adopt(gpa, abi.layout.of(V2, .store), D.v2)); +} diff --git a/src/host/root.zig b/src/host/root.zig index 637a325..2876c0f 100644 --- a/src/host/root.zig +++ b/src/host/root.zig @@ -6,6 +6,8 @@ //! --headless offscreen video, silent audio //! --script FILE play a script instead of reading devices //! --fresh start from default state: no snapshots or recordings +//! --profile-gpu time each GPU command alone, print the times at exit +//! --console LINE run a console line once the sketch is loaded (repeatable) //! --define NAME=VALUE substitute {NAME} in the script const std = @import("std"); const c = @import("c"); @@ -24,6 +26,8 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { var script_path: ?[]const u8 = null; var defines: std.ArrayList([2][]const u8) = .empty; defer defines.deinit(gpa); + var lines: std.ArrayList([]const u8) = .empty; + defer lines.deinit(gpa); var args = init.minimal.args.iterate(); _ = args.skip(); @@ -34,6 +38,10 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { options.fresh = true; } else if (std.mem.eql(u8, arg, "--headless")) { options.headless = true; + } else if (std.mem.eql(u8, arg, "--profile-gpu")) { + options.profile_gpu = true; + } else if (std.mem.eql(u8, arg, "--console")) { + try lines.append(gpa, args.next() orelse fatal("--console needs a line")); } else if (std.mem.eql(u8, arg, "--script")) { script_path = args.next() orelse fatal("--script needs a file"); } else if (std.mem.eql(u8, arg, "--define")) { @@ -56,6 +64,7 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { var host: Host = undefined; try host.init(gpa, init.io, init.environ_map, options, linked); defer host.deinit(); + for (lines.items) |line| host.runLine(line, c.SDL_GetTicksNS()) catch {}; if (script_path) |path| { const source = try std.Io.Dir.cwd().readFileAlloc(init.io, path, gpa, .limited(1 << 20)); defer gpa.free(source); @@ -71,6 +80,8 @@ const usage = \\ --headless offscreen video, silent audio, no vsync \\ --script FILE play a script instead of reading devices \\ --fresh start from default state: no snapshots or recordings + \\ --profile-gpu time each GPU command alone, print the times at exit + \\ --console LINE run a console line once the sketch is loaded (repeatable) \\ --define NAME=VALUE substitute {NAME} in the script \\ ; diff --git a/src/host/script.zig b/src/host/script.zig index 38954f3..cf551cd 100644 --- a/src/host/script.zig +++ b/src/host/script.zig @@ -4,7 +4,7 @@ //! # comment //! click 710 320 move there, press and release the left button //! move|down|up X Y one pointer event, then a frame -//! wheel X Y DELTA +//! wheel|wheel-x X Y DELTA //! cancel what losing focus sends //! key NAME press and release a key (`sketch.Key` names) //! type TEXT text input, as typed into a focused field @@ -56,23 +56,26 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons const eq = std.mem.eql; if (eq(u8, command, "click")) { const x, const y = try point(words); - try host.route(.{ .kind = .mouse_move, .time = now.*, .x = x, .y = y }); - try host.route(.{ .kind = .mouse_down, .time = now.*, .x = x, .y = y, .code = 1 }); - try host.route(.{ .kind = .mouse_up, .time = now.*, .x = x, .y = y, .code = 1 }); + try host.route(.{ .kind = .mouse_move, .time = now.*, .x = x, .y = y, .flags = held(host) }); + try host.route(.{ .kind = .mouse_down, .time = now.*, .x = x, .y = y, .code = 1, .flags = held(host) }); + try host.route(.{ .kind = .mouse_up, .time = now.*, .x = x, .y = y, .code = 1, .flags = held(host) }); } else if (eq(u8, command, "move") or eq(u8, command, "down") or eq(u8, command, "up")) { const x, const y = try point(words); const kind: abi.Event.Kind = if (eq(u8, command, "move")) .mouse_move else if (eq(u8, command, "down")) .mouse_down else .mouse_up; - try host.route(.{ .kind = kind, .time = now.*, .x = x, .y = y, .code = if (kind == .mouse_move) 0 else 1 }); - } else if (eq(u8, command, "wheel")) { + try host.route(.{ .kind = kind, .time = now.*, .x = x, .y = y, .code = if (kind == .mouse_move) 0 else 1, .flags = held(host) }); + } else if (eq(u8, command, "wheel") or eq(u8, command, "wheel-x")) { const x, const y = try point(words); const delta = try std.fmt.parseFloat(f32, words.next() orelse return error.ExpectedDelta); - try host.route(.{ .kind = .wheel, .time = now.*, .x = x, .y = y, .value = @bitCast(delta) }); + try host.route(.{ .kind = if (eq(u8, command, "wheel-x")) .wheel_horizontal else .wheel, .time = now.*, .x = x, .y = y, .value = @bitCast(delta), .flags = held(host) }); } else if (eq(u8, command, "cancel")) { try host.route(.{ .kind = .cancel, .time = now.* }); } else if (eq(u8, command, "key")) { const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; - try host.route(.{ .kind = .key_down, .time = now.*, .code = @intFromEnum(key) }); - try host.route(.{ .kind = .key_up, .time = now.*, .code = @intFromEnum(key) }); + try host.route(.{ .kind = .key_down, .time = now.*, .code = @backingInt(key) }); + try host.route(.{ .kind = .key_up, .time = now.*, .code = @backingInt(key) }); + } else if (eq(u8, command, "hold") or eq(u8, command, "release")) { + const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; + try host.route(.{ .kind = if (eq(u8, command, "hold")) .key_down else .key_up, .time = now.*, .code = @backingInt(key) }); } else if (eq(u8, command, "type")) { for (rest) |byte| try host.route(.{ .kind = .text, .time = now.*, .value = byte }); } else if (eq(u8, command, "console")) { @@ -109,7 +112,7 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons return; } else if (eq(u8, command, "screenshot")) { if (std.fs.path.dirname(rest)) |dir| try std.Io.Dir.cwd().createDirPath(host.io, dir); - host.screenshot = try arena.dupeZ(u8, rest); + host.screenshot = try arena.dupeSentinel(u8, rest, 0); } else if (eq(u8, command, "load")) { try host.load(rest); } else if (eq(u8, command, "reload")) { @@ -131,6 +134,16 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons try advance(host, now); } +/// The modifiers `hold` has down, for a pointer event's `flags`. +fn held(host: *const Host) u8 { + var mods: u32 = 0; + if (host.input.held(abi.Key.left_shift) or host.input.held(abi.Key.right_shift)) mods |= abi.Key.Mod.shift; + if (host.input.held(abi.Key.left_ctrl) or host.input.held(abi.Key.right_ctrl)) mods |= abi.Key.Mod.ctrl; + if (host.input.held(abi.Key.left_alt) or host.input.held(abi.Key.right_alt)) mods |= abi.Key.Mod.alt; + if (host.input.held(abi.Key.left_gui) or host.input.held(abi.Key.right_gui)) mods |= abi.Key.Mod.gui; + return abi.Event.withModifiers(mods); +} + fn advance(host: *Host, now: *u64) !void { try host.frame(now.*); now.* += frame_ns; |
