summaryrefslogtreecommitdiff
path: root/src/host/Host.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-13 15:07:42 -0300
committerGabriel Schneider <[email protected]>2026-11-13 15:07:42 -0300
commitd3074636fff0fbcd5fd9b71c0c3b1c1f5c0c1285 (patch)
tree918d67acc8f00f72cc4ae1ba444e41238a689c2b /src/host/Host.zig
parent409c1accefeb7a775c0914c888b469a1e78fc69a (diff)
downloadsketchbook-d3074636fff0fbcd5fd9b71c0c3b1c1f5c0c1285.tar.gz
sketchbook-d3074636fff0fbcd5fd9b71c0c3b1c1f5c0c1285.zip
host: add optional Tracy profiling and reduce upload allocation stallsHEADmain
Diffstat (limited to 'src/host/Host.zig')
-rw-r--r--src/host/Host.zig58
1 files changed, 37 insertions, 21 deletions
diff --git a/src/host/Host.zig b/src/host/Host.zig
index 43f030a..008d143 100644
--- a/src/host/Host.zig
+++ b/src/host/Host.zig
@@ -3,6 +3,7 @@
//! exercise exactly what a player does.
const Host = @This();
const std = @import("std");
+const tracy = @import("tracy");
const c = @import("c");
const abi = @import("abi");
const memory = @import("memory.zig");
@@ -246,6 +247,9 @@ pub fn live(self: *Host) []u8 {
/// One frame: reload if a build replaced the library, feed input through
/// `update`, then `draw` and present.
pub fn frame(self: *Host, now: u64) !void {
+ const zone = tracy.zone(@src(), "frame");
+ tracy.frameMark();
+ defer zone.end();
_ = self.frame_arena.reset(.retain_capacity);
self.commands = .empty;
if (self.watch) |w| if (w.changed()) self.reload() catch |err| self.print("reload failed: {t}", .{err});
@@ -318,6 +322,8 @@ fn restoredInput(self: *Host, now: u64) !void {
}
fn draw(self: *Host, now: u64) !void {
+ const zone = tracy.zone(@src(), "Host.zig:draw");
+ defer zone.end();
const arena = self.frame_arena.allocator();
self.input.now = now;
self.input.mode = self.timeline.mode;
@@ -356,27 +362,31 @@ fn draw(self: *Host, now: u64) !void {
self.audio.beginFrame();
self.painter.begin(self.offscreen, self.canvas, arena);
var lines: std.ArrayList([]const u8) = .empty;
- for (self.commands.items) |command| switch (command) {
- .canvas => |size| {
- self.painter.end();
- self.setCanvas(size.w, size.h);
- self.painter.begin(self.offscreen, self.canvas, arena);
- },
- .cursor => |kind| self.cursor = kind,
- .voice => |voice| self.audio.declare(voice),
- .audio => |a| self.audio.push(a.key, a.samples, a.clear),
- .quit => self.quit = true,
- .gpu => |pass| if (self.passes) |*g| g.run(&self.images, pass) catch |err| self.print("gpu pass: {t} ({s})", .{ err, c.SDL_GetError() }),
- .upload => |u| if (self.passes) |*g| g.write(u.buffer, u.offset, u.bytes) catch |err| self.print("gpu upload: {t} ({s})", .{ err, c.SDL_GetError() }),
- .compute => |d| if (self.passes) |*g| g.compute(d) catch |err| self.print("gpu compute: {t} ({s})", .{ err, c.SDL_GetError() }),
- .microphone => self.audio.listen(),
- .download => |d| if (self.passes) |*g| g.download(d) catch |err| self.print("gpu download: {t} ({s})", .{ err, c.SDL_GetError() }),
- .open_file => |o| self.openFile(o.command, o.pattern),
- .run => |line| try lines.append(arena, line),
- else => self.painter.draw(command),
- };
- self.painter.end();
- self.audio.endFrame();
+ {
+ const paint_zone = tracy.zone(@src(), "paint commands");
+ defer paint_zone.end();
+ for (self.commands.items) |command| switch (command) {
+ .canvas => |size| {
+ self.painter.end();
+ self.setCanvas(size.w, size.h);
+ self.painter.begin(self.offscreen, self.canvas, arena);
+ },
+ .cursor => |kind| self.cursor = kind,
+ .voice => |voice| self.audio.declare(voice),
+ .audio => |a| self.audio.push(a.key, a.samples, a.clear),
+ .quit => self.quit = true,
+ .gpu => |pass| if (self.passes) |*g| g.run(&self.images, pass) catch |err| self.print("gpu pass: {t} ({s})", .{ err, c.SDL_GetError() }),
+ .upload => |u| if (self.passes) |*g| g.write(u.buffer, u.offset, u.bytes) catch |err| self.print("gpu upload: {t} ({s})", .{ err, c.SDL_GetError() }),
+ .compute => |d| if (self.passes) |*g| g.compute(d) catch |err| self.print("gpu compute: {t} ({s})", .{ err, c.SDL_GetError() }),
+ .microphone => self.audio.listen(),
+ .download => |d| if (self.passes) |*g| g.download(d) catch |err| self.print("gpu download: {t} ({s})", .{ err, c.SDL_GetError() }),
+ .open_file => |o| self.openFile(o.command, o.pattern),
+ .run => |line| try lines.append(arena, line),
+ else => self.painter.draw(command),
+ };
+ self.painter.end();
+ self.audio.endFrame();
+ }
for (lines.items) |line| self.runLine(line, now) catch {};
var extra: std.ArrayList(abi.Command) = .empty;
@@ -404,7 +414,9 @@ fn draw(self: *Host, now: u64) !void {
_ = c.SDL_SetTextureBlendMode(canvas, c.SDL_BLENDMODE_NONE);
_ = c.SDL_RenderTexture(self.renderer, canvas, null, null);
};
+ const present_zone = tracy.zone(@src(), "present");
_ = c.SDL_RenderPresent(self.renderer);
+ present_zone.end();
if (self.passes) |*g| g.endFrame();
}
@@ -535,6 +547,8 @@ pub fn route(self: *Host, e: abi.Event) !void {
/// Runs a console line now, replaying whatever a seek asks for. An error is
/// printed to the console, and returned for scripts to fail on.
pub fn runLine(self: *Host, line: []const u8, now: u64) !void {
+ const zone = tracy.zone(@src(), "Host.zig:runLine");
+ defer zone.end();
const replay = self.console.execute(self, line, now) catch |err| {
// The sketch has printed its own error already.
if (err != error.CommandFailed) self.console.print("{t}. Type help for usage.", .{err});
@@ -681,6 +695,8 @@ pub fn print(self: *Host, comptime fmt: []const u8, args: anytype) void {
/// Translates SDL events into sketch events and routes them.
pub fn poll(self: *Host) !void {
+ const zone = tracy.zone(@src(), "Host.zig:poll");
+ defer zone.end();
var event: c.SDL_Event = undefined;
while (c.SDL_PollEvent(&event)) {
_ = c.SDL_ConvertEventToRenderCoordinates(self.renderer, &event);