From 562b503d5007e3afe1de426fc1175eadfbbb45c4 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 01:01:34 -0300 Subject: audio: a GPU buffer streamed within the frame, at a lead the host measures A compute shader's output was heard only after the sketch read it back (f.download, a frame later) and pushed it behind a lead of its own choosing; spheres kept 100 ms, and a change it made left for the device 96 ms later on average (measured, dummy driver, 60 Hz). f.audioBuffer(key, buffer, .{ .count, .offset }) streams `count` f32 a shader wrote: the host copies them back while it runs the frame's commands, waiting on the copy's fence (Executor.read: 0.2 ms on average here, 2 ms at the 99th percentile on a loaded machine), and queues them on the stream, so nothing is in flight between frames and the sketch keeps no books. Input.lead says how far ahead to make sound: the slowest of the last 64 frames' worth of samples (timed between the ends of the frames' commands, where the pushes are), plus the device's pull (what SDL takes from a stream at once, read from the device: 1024 here), plus half a pull for each underrun, a sample less each frame after; capped at 8192. A sketch pushes `in.lead -| in.stream(key).queued` a frame, and its change leaves after about what is queued ahead of it, a pull: 19 to 27 ms on average with SDL's 1024-sample pulls, 6.3 to 6.5 ms with 256 (SDL_AUDIO_DEVICE_SAMPLE_FRAMES; 17 under heavy load). Stream.underruns counts the pushes that found a stream empty though the frame before had fed it (a gap). The voices keep their 2048 samples: mixed to the lead, a one-shot longer than it is mixed over several frames, and voices.script's expect-audio and expect-no-audio, which look one frame at a time, failed. On the web, audio.buffer reads the buffer back with mapAsync and queues it on arrival, in order, counting it as queued meanwhile; in.lead stays at 100 ms there. The measurements (a probe patched into the host for the purpose, kept out of the tree) are in the 02-pardes-code scratch, .scratch/sketch/gpusynth. Tests: the lead's arithmetic (audio.zig, in test-audio's filter); spheres' play script checks its stream is fed. zig build test: 164/164 steps, 49/49 tests run (the rest cached); zig build test-audio: 48/48 steps, with the voices sketch's wasm, so web.zig compiles; voices.script passes four runs in four. web/host.js: node --check only, no browser run. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/gpu/Executor.zig | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/gpu/Executor.zig') diff --git a/src/gpu/Executor.zig b/src/gpu/Executor.zig index 460b2d5..797dcbf 100644 --- a/src/gpu/Executor.zig +++ b/src/gpu/Executor.zig @@ -503,6 +503,32 @@ pub fn download(g: *Gpu, d: @FieldType(abi.Command, "download")) !void { }; } +/// Copies `out.len` bytes of a buffer from `offset` back now, waiting for +/// the GPU to finish the work submitted before (sound that must be queued +/// this frame). +pub fn read(g: *Gpu, buffer: abi.Buffer, offset: u32, out: []u8) !void { + const source = g.storage(buffer) orelse return error.UnknownBuffer; + const size: u32 = @intCast(out.len); + if (size == 0 or @as(u64, offset) + size > source.size) return error.DownloadOutOfBounds; + const transfer = for (g.spare.items, 0..) |t, i| { + if (t.size >= size) break g.spare.swapRemove(i); + } else Transfer{ + .buffer = c.SDL_CreateGPUTransferBuffer(g.device, &.{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_DOWNLOAD, .size = size, .props = 0 }) orelse return error.GpuBuffer, + .size = size, + }; + defer 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 = offset, .size = size }, &.{ .transfer_buffer = transfer.buffer, .offset = 0 }); + c.SDL_EndGPUCopyPass(copy); + const fence = c.SDL_SubmitGPUCommandBufferAndAcquireFence(cmd) orelse return error.GpuSubmit; + defer c.SDL_ReleaseGPUFence(g.device, fence); + if (!c.SDL_WaitForGPUFences(g.device, true, &fence, 1)) return error.GpuFence; + const mapped: [*]const u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, transfer.buffer, false) orelse return error.GpuMap); + defer c.SDL_UnmapGPUTransferBuffer(g.device, transfer.buffer); + @memcpy(out, mapped[0..size]); +} + /// 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 { -- cgit v1.3