summaryrefslogtreecommitdiff
path: root/src/gui/gui.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/gui.zig')
-rw-r--r--src/gui/gui.zig102
1 files changed, 27 insertions, 75 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 71635cb2..427d3fd0 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -534,7 +534,6 @@ const ImageInstance = extern struct {
cell_coord: u32 = 0,
};
-const initial_image_capacity: u32 = pardes.MAX_PANES;
const old_layer_bit: u32 = 0x8000_0000;
const clear_bg_bit: u32 = 0x4000_0000;
const opaque_bg_bit: u32 = 0x2000_0000;
@@ -1395,8 +1394,8 @@ const Gui = struct {
overlay_vbuf: *c.SDL_GPUBuffer,
overlay_vxfer: *c.SDL_GPUTransferBuffer,
overlay_vertices: []OverlayVertex,
- image_vbuf: *c.SDL_GPUBuffer,
- image_vxfer: *c.SDL_GPUTransferBuffer,
+ image_vbuf: ?*c.SDL_GPUBuffer = null,
+ image_vxfer: ?*c.SDL_GPUTransferBuffer = null,
image_capacity: u32 = 0,
native_images: std.AutoHashMapUnmanaged(pardes.ImageCacheKey, *c.SDL_GPUTexture) = .empty,
presented_images: std.ArrayListUnmanaged(SavedImagePlace) = .empty,
@@ -2167,12 +2166,6 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di
var ovx_info = c.SDL_GPUTransferBufferCreateInfo{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = overlay_buffer_size, .props = 0 };
const overlay_vxfer = c.SDL_CreateGPUTransferBuffer(device, &ovx_info) orelse return error.GpuCreate;
- const image_buffer_size: u32 = initial_image_capacity * @sizeOf(ImageInstance);
- var ivb_info = c.SDL_GPUBufferCreateInfo{ .usage = c.SDL_GPU_BUFFERUSAGE_VERTEX, .size = image_buffer_size, .props = 0 };
- const image_vbuf = c.SDL_CreateGPUBuffer(device, &ivb_info) orelse return error.GpuCreate;
- var ivx_info = c.SDL_GPUTransferBufferCreateInfo{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = image_buffer_size, .props = 0 };
- const image_vxfer = c.SDL_CreateGPUTransferBuffer(device, &ivx_info) orelse return error.GpuCreate;
-
const atlas_stage = try gpa.alloc(u8, atlas_w * atlas_h);
defer gpa.free(atlas_stage);
@memset(atlas_stage, 0);
@@ -2198,9 +2191,6 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di
.overlay_vbuf = overlay_vbuf,
.overlay_vxfer = overlay_vxfer,
.overlay_vertices = overlay_vertices,
- .image_vbuf = image_vbuf,
- .image_vxfer = image_vxfer,
- .image_capacity = initial_image_capacity,
.font = font,
.px = px,
.tagline_px = tagline.px,
@@ -2243,6 +2233,8 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di
c.SDL_ReleaseGPUTransferBuffer(device, g.overlay_vxfer);
if (g.vbuf) |buffer| c.SDL_ReleaseGPUBuffer(device, buffer);
if (g.vxfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(device, transfer);
+ if (g.image_vbuf) |buffer| c.SDL_ReleaseGPUBuffer(device, buffer);
+ if (g.image_vxfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(device, transfer);
if (g.scene_tex) |texture| c.SDL_ReleaseGPUTexture(device, texture);
if (g.capture_tex) |texture| c.SDL_ReleaseGPUTexture(device, texture);
if (g.capture_xfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(device, transfer);
@@ -2262,8 +2254,6 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di
g.decor.deinit(gpa);
g.post.deinit(gpa, device);
}
- defer c.SDL_ReleaseGPUTransferBuffer(device, g.image_vxfer);
- defer c.SDL_ReleaseGPUBuffer(device, g.image_vbuf);
defer c.SDL_ReleaseGPUGraphicsPipeline(device, image_pipeline);
defer if (g.font_bytes.len != 0) gpa.free(g.font_bytes); // set by Font, if it ran
defer c.ui_font_free(g.font);
@@ -6057,39 +6047,6 @@ fn placeIntersectsBox(place: SavedImagePlace, box: pardes.animation.Box) bool {
y0 < box.y + box.h and y1 > box.y;
}
-fn ensureImageBuffers(g: *Gui, needed: u32) bool {
- if (needed <= g.image_capacity) return true;
- var capacity = @max(@as(u32, 1), g.image_capacity);
- while (capacity < needed)
- capacity = std.math.mul(u32, capacity, 2) catch return false;
- const buffer_size = std.math.mul(
- u32,
- capacity,
- @as(u32, @intCast(@sizeOf(ImageInstance))),
- ) catch return false;
- var vb_info = c.SDL_GPUBufferCreateInfo{
- .usage = c.SDL_GPU_BUFFERUSAGE_VERTEX,
- .size = buffer_size,
- .props = 0,
- };
- const vbuf = c.SDL_CreateGPUBuffer(g.device, &vb_info) orelse return false;
- var vx_info = c.SDL_GPUTransferBufferCreateInfo{
- .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
- .size = buffer_size,
- .props = 0,
- };
- const vxfer = c.SDL_CreateGPUTransferBuffer(g.device, &vx_info) orelse {
- c.SDL_ReleaseGPUBuffer(g.device, vbuf);
- return false;
- };
- c.SDL_ReleaseGPUTransferBuffer(g.device, g.image_vxfer);
- c.SDL_ReleaseGPUBuffer(g.device, g.image_vbuf);
- g.image_vbuf = vbuf;
- g.image_vxfer = vxfer;
- g.image_capacity = capacity;
- return true;
-}
-
fn validImageBytes(place: pardes.ImagePlace) ?u32 {
if (place.iw == 0 or place.ih == 0 or
place.iw > std.math.maxInt(u32) or place.ih > std.math.maxInt(u32)) return null;
@@ -6354,9 +6311,9 @@ fn prepareNativeImages(
const capacity = std.math.cast(u32, g.prepared_images.items.len) orelse return false;
if (capacity == 0) return true;
- if (!ensureImageBuffers(g, capacity)) return false;
+ growInstances(g, &g.image_vbuf, &g.image_vxfer, &g.image_capacity, capacity, @sizeOf(ImageInstance)) catch return false;
- const ptr: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, g.image_vxfer, false) orelse return false);
+ const ptr: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, g.image_vxfer.?, false) orelse return false);
const instances: [*]ImageInstance = @ptrCast(@alignCast(ptr));
const win_w: f32 = @floatFromInt(sw);
const win_h: f32 = @floatFromInt(sh);
@@ -6366,12 +6323,12 @@ fn prepareNativeImages(
if (!emitPreparedImage(g, &instances[idx], prepared, layout, win_w, win_h)) continue;
idx += 1;
}
- c.SDL_UnmapGPUTransferBuffer(g.device, g.image_vxfer);
+ c.SDL_UnmapGPUTransferBuffer(g.device, g.image_vxfer.?);
std.debug.assert(idx == capacity);
const copy = c.SDL_BeginGPUCopyPass(cmd);
- const src = c.SDL_GPUTransferBufferLocation{ .transfer_buffer = g.image_vxfer, .offset = 0 };
- const dst = c.SDL_GPUBufferRegion{ .buffer = g.image_vbuf, .offset = 0, .size = idx * @sizeOf(ImageInstance) };
+ const src = c.SDL_GPUTransferBufferLocation{ .transfer_buffer = g.image_vxfer.?, .offset = 0 };
+ const dst = c.SDL_GPUBufferRegion{ .buffer = g.image_vbuf.?, .offset = 0, .size = idx * @sizeOf(ImageInstance) };
c.SDL_UploadToGPUBuffer(copy, &src, &dst, false);
c.SDL_EndGPUCopyPass(copy);
return true;
@@ -6433,7 +6390,7 @@ fn drawNativeImagesGpu(
c.SDL_SetGPUScissor(pass, &clip);
const sampler = c.SDL_GPUTextureSamplerBinding{ .texture = prepared.texture, .sampler = g.linear_sampler };
c.SDL_BindGPUFragmentSamplers(pass, 0, &sampler, 1);
- const binding = c.SDL_GPUBufferBinding{ .buffer = g.image_vbuf, .offset = idx * @sizeOf(ImageInstance) };
+ const binding = c.SDL_GPUBufferBinding{ .buffer = g.image_vbuf.?, .offset = idx * @sizeOf(ImageInstance) };
c.SDL_BindGPUVertexBuffers(pass, 0, &binding, 1);
c.SDL_DrawGPUPrimitives(pass, 6, 1, 0, 0);
}
@@ -6665,7 +6622,7 @@ fn renderFrame(
if (cell_total != 0 or decor_len != 0) {
// The shifted rows of a scroll: its body, and a glide's rows past it.
const capacity = std.math.add(u32, cell_total, 2 * cells + decor_len) catch return error.GpuCapacity;
- try ensureVbuf(g, capacity);
+ try growInstances(g, &g.vbuf, &g.vxfer, &g.vbuf_cells, capacity, @sizeOf(CellInstance));
const vptr: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, g.vxfer.?, false) orelse {
command_consumed = true;
@@ -8164,8 +8121,6 @@ fn testGui(cell_w: u32, cell_h: u32, tagline_width: u32, tagline_height: u32) Gu
.overlay_vbuf = undefined,
.overlay_vxfer = undefined,
.overlay_vertices = &.{},
- .image_vbuf = undefined,
- .image_vxfer = undefined,
.font = undefined,
.px = 0,
.tagline_px = 0,
@@ -8629,27 +8584,24 @@ test "the atlas's dirty rows hold every row a new glyph draws into, and a reset
try std.testing.expect(g.pen_y >= 2 * g.cell_h);
}
-fn ensureVbuf(g: *Gui, cells: u32) !void {
- if (cells <= g.vbuf_cells and g.vbuf != null and g.vxfer != null) return;
- var capacity = @max(@as(u32, 1), g.vbuf_cells);
- while (capacity < cells)
- capacity = std.math.mul(u32, capacity, 2) catch return error.GpuCapacity;
- const size = std.math.mul(
- u32,
- capacity,
- @as(u32, @intCast(@sizeOf(CellInstance))),
- ) catch return error.GpuCapacity;
+/// A vertex buffer and its upload buffer holding at least `needed`
+/// elements of `stride` bytes: those there when they do, else twice as
+/// many again until they would (the old ones let go).
+fn growInstances(g: *Gui, buffer: *?*c.SDL_GPUBuffer, transfer: *?*c.SDL_GPUTransferBuffer, capacity: *u32, needed: u32, stride: u32) !void {
+ if (needed <= capacity.* and buffer.* != null and transfer.* != null) return;
+ var grown = @max(@as(u32, 1), capacity.*);
+ while (grown < needed) grown = std.math.mul(u32, grown, 2) catch return error.GpuCapacity;
+ const size = std.math.mul(u32, grown, stride) catch return error.GpuCapacity;
var vb_info = c.SDL_GPUBufferCreateInfo{ .usage = c.SDL_GPU_BUFFERUSAGE_VERTEX, .size = size, .props = 0 };
- const next_vbuf = c.SDL_CreateGPUBuffer(g.device, &vb_info) orelse return error.GpuCreate;
- errdefer c.SDL_ReleaseGPUBuffer(g.device, next_vbuf);
+ const next_buffer = c.SDL_CreateGPUBuffer(g.device, &vb_info) orelse return error.GpuCreate;
+ errdefer c.SDL_ReleaseGPUBuffer(g.device, next_buffer);
var xf_info = c.SDL_GPUTransferBufferCreateInfo{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = size, .props = 0 };
- const next_vxfer = c.SDL_CreateGPUTransferBuffer(g.device, &xf_info) orelse return error.GpuCreate;
-
- if (g.vbuf) |buffer| c.SDL_ReleaseGPUBuffer(g.device, buffer);
- if (g.vxfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(g.device, transfer);
- g.vbuf = next_vbuf;
- g.vxfer = next_vxfer;
- g.vbuf_cells = capacity;
+ const next_transfer = c.SDL_CreateGPUTransferBuffer(g.device, &xf_info) orelse return error.GpuCreate;
+ if (buffer.*) |old| c.SDL_ReleaseGPUBuffer(g.device, old);
+ if (transfer.*) |old| c.SDL_ReleaseGPUTransferBuffer(g.device, old);
+ buffer.* = next_buffer;
+ transfer.* = next_transfer;
+ capacity.* = grown;
}
/// One vertex buffer of a pipeline: `pitch` bytes an element, read per