summaryrefslogtreecommitdiff
path: root/src/host/TimingAtlas.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host/TimingAtlas.zig')
-rw-r--r--src/host/TimingAtlas.zig139
1 files changed, 139 insertions, 0 deletions
diff --git a/src/host/TimingAtlas.zig b/src/host/TimingAtlas.zig
new file mode 100644
index 0000000..a00a87e
--- /dev/null
+++ b/src/host/TimingAtlas.zig
@@ -0,0 +1,139 @@
+//! One immutable texture for the timing overlay's printable ASCII glyphs.
+const Atlas = @This();
+const std = @import("std");
+const abi = @import("abi");
+const gfx = @import("gfx.zig");
+const c = @import("c");
+
+image: abi.Image = .none,
+advance: f32 = 0,
+cell_width: f32 = 0,
+cell_height: f32 = 0,
+
+const padding = 1;
+const glyphs = blk: {
+ var bytes: [95]u8 = undefined;
+ for (&bytes, 0..) |*byte, i| byte.* = @intCast(i + 32);
+ break :blk bytes;
+};
+
+/// Called once with Painter on its canvas. Images owns the texture until shutdown.
+pub fn init(images: *gfx.Images, painter: *gfx.Painter) !Atlas {
+ const run = gfx.textRun(.mono, 1, "0", &.{});
+ const size: [2]f32 = .{ run.width, run.height };
+ var atlas: Atlas = .{
+ .advance = size[0],
+ .cell_width = @ceil(size[0]) + padding * 2,
+ .cell_height = @ceil(size[1]) + padding * 2,
+ };
+ atlas.image = images.new(@intFromFloat(atlas.cell_width * glyphs.len), @intFromFloat(atlas.cell_height));
+ errdefer images.free(atlas.image);
+ const entry = images.get(atlas.image) orelse return error.TimingAtlasCreationFailed;
+ if (entry.texture == null) return error.TimingAtlasCreationFailed;
+ // The atlas stores the native scale-one raster; do not supersample this cache.
+ const smooth = painter.smooth;
+ painter.smooth = false;
+ defer painter.smooth = smooth;
+ painter.draw(.{ .target = atlas.image });
+ painter.draw(.{ .clear = .{ .r = 0, .g = 0, .b = 0, .a = 0 } });
+ for (glyphs, 0..) |_, i| painter.draw(.{ .text = .{
+ .x = @as(f32, @floatFromInt(i)) * atlas.cell_width + padding,
+ .y = padding,
+ .scale = 1,
+ .raster = 1,
+ .color = .white,
+ .bytes = glyphs[i..][0..1],
+ } });
+ // The unused space cell also supplies an opaque texel for bars/backgrounds.
+ painter.draw(.{ .rect = .{ .dst = .{ .x = 0, .y = 0, .w = 1, .h = 1 }, .color = .white } });
+ painter.draw(.{ .target = .none });
+ return atlas;
+}
+
+fn source(atlas: Atlas, byte: u8) abi.Rect {
+ const printable = if (byte >= 32 and byte <= 126) byte else '?';
+ return .{ .x = @as(f32, @floatFromInt(printable - 32)) * atlas.cell_width, .y = 0, .w = atlas.cell_width, .h = atlas.cell_height };
+}
+
+/// The complete overlay fits one fixed vertex/index batch and one SDL draw call.
+pub const Batch = struct {
+ const capacity = 4 * 96 + 48 + @import("Timing.zig").capacity + 2;
+ atlas: Atlas,
+ vertices: [capacity * 4]c.SDL_Vertex = undefined,
+ indices: [capacity * 6]u16 = undefined,
+ count: usize = 0,
+
+ pub fn rect(batch: *Batch, dst: abi.Rect, color: abi.Color) !void {
+ try batch.quad(dst, .{ .x = 0.5, .y = 0.5, .w = 0, .h = 0 }, color);
+ }
+
+ pub fn text(batch: *Batch, bytes: []const u8, x: f32, y: f32, scale: f32, color: abi.Color) !void {
+ const atlas = batch.atlas;
+ for (bytes, 0..) |byte, i| {
+ if (byte == ' ') continue;
+ try batch.quad(.{
+ .x = x + (@as(f32, @floatFromInt(i)) * atlas.advance - padding) * scale,
+ .y = y - padding * scale,
+ .w = atlas.cell_width * scale,
+ .h = atlas.cell_height * scale,
+ }, atlas.source(byte), color);
+ }
+ }
+
+ fn quad(batch: *Batch, dst: abi.Rect, src: abi.Rect, color: abi.Color) !void {
+ if (batch.count == capacity) return error.TimingBatchFull;
+ const width = batch.atlas.cell_width * glyphs.len;
+ const height = batch.atlas.cell_height;
+ const alpha = @as(f32, @floatFromInt(color.a)) / 255;
+ const tint: c.SDL_FColor = .{
+ .r = @as(f32, @floatFromInt(color.r)) / 255 * alpha,
+ .g = @as(f32, @floatFromInt(color.g)) / 255 * alpha,
+ .b = @as(f32, @floatFromInt(color.b)) / 255 * alpha,
+ .a = alpha,
+ };
+ const base = batch.count * 4;
+ const corners = [4][2]f32{ .{ 0, 0 }, .{ 1, 0 }, .{ 1, 1 }, .{ 0, 1 } };
+ for (corners, 0..) |corner, i| batch.vertices[base + i] = .{
+ .position = .{ .x = dst.x + corner[0] * dst.w, .y = dst.y + corner[1] * dst.h },
+ .color = tint,
+ .tex_coord = .{ .x = (src.x + corner[0] * src.w) / width, .y = (src.y + corner[1] * src.h) / height },
+ };
+ for ([_]u16{ 0, 1, 2, 0, 2, 3 }, 0..) |index, i| batch.indices[batch.count * 6 + i] = @as(u16, @intCast(base)) + index;
+ batch.count += 1;
+ }
+
+ pub fn draw(batch: *const Batch, painter: *gfx.Painter) void {
+ painter.texturedTriangles(batch.atlas.image, batch.vertices[0 .. batch.count * 4], batch.indices[0 .. batch.count * 6]);
+ }
+};
+
+test "timing atlas ASCII cells and scaled glyph positions share one image" {
+ const atlas: Atlas = .{ .image = @fromBackingInt(7), .advance = 7.5, .cell_width = 10, .cell_height = 16 };
+ try std.testing.expectEqual(@as(f32, 0), atlas.source(' ').x);
+ try std.testing.expectEqual(@as(f32, 940), atlas.source('~').x);
+ try std.testing.expectEqual(atlas.source('?'), atlas.source(0xff));
+ var batch: Batch = .{ .atlas = atlas };
+ try batch.text("1 2", 10, 20, 0.5, .white);
+ try std.testing.expectEqual(@as(usize, 2), batch.count);
+ try std.testing.expectEqual(@as(f32, 9.5), batch.vertices[0].position.x);
+ try std.testing.expectEqual(@as(f32, 19.5), batch.vertices[0].position.y);
+ try std.testing.expectEqual(@as(f32, 14.5), batch.vertices[2].position.x);
+ try std.testing.expectEqual(@as(f32, 27.5), batch.vertices[2].position.y);
+ try std.testing.expectEqual(@as(f32, 17), batch.vertices[4].position.x);
+ try std.testing.expectEqual(@as(f32, 170.0 / 950.0), batch.vertices[0].tex_coord.x);
+ try std.testing.expectEqual(@as(f32, 180.0 / 950.0), batch.vertices[4].tex_coord.x);
+ try std.testing.expectEqualSlices(u16, &.{ 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7 }, batch.indices[0..12]);
+}
+
+test "timing batch is bounded and solid quads sample one opaque atlas texel" {
+ var batch: Batch = .{ .atlas = .{ .image = @fromBackingInt(1), .advance = 8, .cell_width = 10, .cell_height = 16 } };
+ const box: abi.Rect = .{ .x = 10, .y = 20, .w = 5, .h = 8 };
+ for (0..Batch.capacity) |_| try batch.rect(box, .{ .r = 255, .g = 128, .b = 0, .a = 128 });
+ try std.testing.expectError(error.TimingBatchFull, batch.rect(box, .white));
+ for (batch.indices) |index| try std.testing.expect(index < batch.vertices.len);
+ for (batch.vertices[0..4]) |vertex| {
+ try std.testing.expectEqual(batch.vertices[0].tex_coord, vertex.tex_coord);
+ try std.testing.expectApproxEqAbs(@as(f32, 128.0 / 255.0), vertex.color.r, 0.00001);
+ try std.testing.expectApproxEqAbs(@as(f32, 128.0 / 255.0), vertex.color.a, 0.00001);
+ }
+}