summaryrefslogtreecommitdiff
path: root/src/gpu
diff options
context:
space:
mode:
Diffstat (limited to 'src/gpu')
-rw-r--r--src/gpu/Executor.zig26
1 files changed, 26 insertions, 0 deletions
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 {