summaryrefslogtreecommitdiff
path: root/src/host/overlay.zig
blob: e1fc45db74c7d4609fe234b14547adfa78eb2c9e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//! Debug overlays drawn by the host over any sketch: the live state's fields
//! and bytes, from the reflected layout, and the audio output waveform.
const std = @import("std");
const abi = @import("abi");
const memory = @import("memory.zig");
const fields = @import("fields.zig");
const Timing = @import("Timing.zig");
const gfx = @import("gfx.zig");
pub const TimingAtlas = @import("TimingAtlas.zig");

const List = std.ArrayList(abi.Command);
const shown_bytes = 0x100;

pub fn drawTiming(painter: *gfx.Painter, timing: *Timing, canvas: [2]u32, atlas: TimingAtlas) !void {
    var batch: TimingAtlas.Batch = .{ .atlas = atlas };
    const reference: f64 = if (timing.metric == .frame) 1000.0 / 60.0 else 1;
    var ceiling = reference * 1.25;
    for (0..timing.graph_count) |i| ceiling = @max(ceiling, Timing.ms(Timing.value(timing.graphAt(i), timing.metric)) * 1.1);
    if (timing.dirty or ceiling != timing.ceiling) {
        timing.ceiling = ceiling;
        timing.setLabel(3, "8 frames/peak / 0..{d:.2} ms / line {d:.2}", .{ ceiling, reference });
    }
    if (timing.dirty) {
        timing.cached = .{ timing.stats(.frame), timing.stats(.work) };
        timing.setLabel(0, "TIMING / {t} / stats: {d} frames / ms", .{ timing.metric, timing.count });
        for (timing.cached, 0..) |s, i| timing.setLabel(i + 1, "{s} avg {d:.3}  p90 {d:.3}  p99 {d:.3}", .{ if (i == 0) "Frame" else "Work ", s.mean, s.p90, s.p99 });
        timing.dirty = false;
    }
    const width = @min(@as(f32, @floatFromInt(canvas[0])), 480);
    const scale = width / 480;
    const x = @as(f32, @floatFromInt(canvas[0])) - width;
    const green: abi.Color = .{ .r = 105, .g = 245, .b = 160 };
    const muted: abi.Color = .{ .r = 165, .g = 180, .b = 195 };
    try batch.rect(.{ .x = x, .y = 0, .w = width, .h = 200 * scale }, .{ .r = 20, .g = 25, .b = 32 });
    for (0..4) |i| {
        const y: f32 = switch (i) {
            0 => 8,
            1 => 30,
            2 => 48,
            else => 70,
        };
        try batch.text(timing.label(i), x + 12 * scale, y * scale, scale, if (i == 0) green else if (i == 3) muted else .white);
    }
    const left = x + 12 * scale;
    const bottom = 170 * scale;
    const height = 78 * scale;
    const step = 456 * scale / Timing.capacity;
    const line_y = bottom - height * @as(f32, @floatCast(reference / ceiling));
    try batch.rect(.{ .x = left, .y = line_y, .w = 456 * scale, .h = 1 }, muted);
    for (0..timing.graph_count) |i| {
        const value = Timing.ms(Timing.value(timing.graphAt(i), timing.metric));
        const h = @max(1, height * @as(f32, @floatCast(value / ceiling)));
        try batch.rect(.{ .x = left + @as(f32, @floatFromInt(Timing.capacity - timing.graph_count + i)) * step, .y = bottom - h, .w = step, .h = h }, green);
    }
    try batch.text("Frame includes present / Work stops before it", left, 180 * scale, scale, muted);
    batch.draw(painter);
}

/// Each field hashes its path into a dark shade; bytes it owns wear the same
/// shade in the hex view below, and padding stays gray.
fn shade(path: []const u8) abi.Color {
    const hash = std.hash.Wyhash.hash(0, path);
    return .{ .r = @as(u8, @truncate(hash)) / 2, .g = @as(u8, @truncate(hash >> 8)) / 2, .b = @as(u8, @truncate(hash >> 16)) / 2 };
}

pub fn drawMemory(out: *List, arena: std.mem.Allocator, layout: memory.Layout, state: []const u8, width: f32) !void {
    // Arrays show as wholes: their element leaves (`list[].x`) are left out.
    var top_level: usize = 0;
    for (layout.fields) |leaf| top_level += @intFromBool(leaf.dims.len == 0);
    const rows = (top_level + 1) / 2;
    const legend_top: f32 = 24;
    const hex_top = legend_top + @as(f32, @floatFromInt(rows)) * 14 + 16;
    const shown = @min(state.len, shown_bytes);
    const height = hex_top + @as(f32, @floatFromInt((shown + 15) / 16)) * 14 + 10;
    try out.append(arena, .{ .rect = .{ .dst = .{ .x = 12, .y = 4, .w = width - 24, .h = height }, .color = .{ .r = 230, .g = 254, .b = 242 } } });
    try out.append(arena, .{ .text = .{ .x = 30, .y = 8, .scale = 1, .color = .{ .r = 24, .g = 24, .b = 24 }, .bytes = "MEMORY  /  overlay memory to hide" } });

    // Legend: two fixed-width columns filled down then across.
    const column = (width - 60) / 2;
    var row: usize = 0;
    for (layout.fields) |leaf| {
        if (leaf.dims.len != 0) continue;
        const i = row;
        row += 1;
        var value: std.Io.Writer.Allocating = .init(arena);
        try fields.format(leaf, state, &value.writer);
        const name = if (leaf.path.len > 26) try std.fmt.allocPrint(arena, "{s}...", .{leaf.path[0..23]}) else leaf.path;
        const line = try std.fmt.allocPrint(arena, "{x:0>4} {s: <26} {s}", .{ leaf.offset, name, value.written() });
        try out.append(arena, .{ .text = .{
            .x = 30 + @as(f32, @floatFromInt(i / rows)) * column,
            .y = legend_top + @as(f32, @floatFromInt(i % rows)) * 14,
            .scale = 1,
            .color = shade(leaf.path),
            .bytes = line[0..@min(line.len, @as(usize, @intFromFloat(column / 8)) - 1)],
        } });
    }

    var colors: [shown_bytes]abi.Color = @splat(.{ .r = 0x9f, .g = 0x9f, .b = 0x9f });
    for (layout.fields) |leaf| {
        if (leaf.offset >= shown or leaf.dims.len != 0) continue;
        @memset(colors[leaf.offset..@min(shown, leaf.offset + leaf.size)], shade(leaf.path));
    }
    for (state[0..shown], 0..) |byte, i| {
        try out.append(arena, .{ .text = .{
            .x = 30 + @as(f32, @floatFromInt(i % 16)) * 24,
            .y = hex_top + @as(f32, @floatFromInt(i / 16)) * 14,
            .scale = 1,
            .color = colors[i],
            .bytes = try std.fmt.allocPrint(arena, "{X:0>2}", .{byte}),
        } });
    }
}

pub fn drawScope(out: *List, arena: std.mem.Allocator, samples: []const f32, rate: u32, canvas: [2]u32) !void {
    const green: abi.Color = .{ .r = 105, .g = 245, .b = 160 };
    const x = @as(f32, @floatFromInt(canvas[0])) - 526;
    const top = @as(f32, @floatFromInt(canvas[1])) - 130;
    const mid = top + 72;
    try out.append(arena, .{ .rect = .{ .dst = .{ .x = x, .y = top, .w = 514, .h = 118 }, .color = .{ .r = 24, .g = 24, .b = 24 } } });
    try out.append(arena, .{ .rect = .{ .dst = .{ .x = x + 1, .y = mid, .w = 512, .h = 1 }, .color = .{ .r = 60, .g = 85, .b = 65 } } });
    const ms = @as(f32, @floatFromInt(samples.len * 1000)) / @as(f32, @floatFromInt(@max(rate, 1)));
    try out.append(arena, .{ .text = .{ .x = x + 8, .y = top + 10, .scale = 1, .color = green, .bytes = try std.fmt.allocPrint(arena, "OUTPUT ch1 / {d:.1}ms / overlay sound to hide", .{ms}) } });
    for (samples, 0..) |sample, i| {
        const a = std.math.clamp(if (std.math.isFinite(sample)) sample else 0, -1, 1);
        const previous = samples[i -| 1];
        const b = std.math.clamp(if (std.math.isFinite(previous)) previous else 0, -1, 1);
        const y1 = mid - a * 36;
        const y2 = mid - b * 36;
        try out.append(arena, .{ .rect = .{ .dst = .{ .x = x + 1 + @as(f32, @floatFromInt(i)), .y = @min(y1, y2), .w = 1, .h = @max(1, @abs(y2 - y1)) }, .color = green } });
    }
}