diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-13 15:07:42 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-13 15:07:42 -0300 |
| commit | d3074636fff0fbcd5fd9b71c0c3b1c1f5c0c1285 (patch) | |
| tree | 918d67acc8f00f72cc4ae1ba444e41238a689c2b /src | |
| parent | 409c1accefeb7a775c0914c888b469a1e78fc69a (diff) | |
| download | sketchbook-d3074636fff0fbcd5fd9b71c0c3b1c1f5c0c1285.tar.gz sketchbook-d3074636fff0fbcd5fd9b71c0c3b1c1f5c0c1285.zip | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/host/Host.zig | 58 | ||||
| -rw-r--r-- | src/tracy.zig | 50 |
2 files changed, 87 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); diff --git a/src/tracy.zig b/src/tracy.zig new file mode 100644 index 0000000..889ba56 --- /dev/null +++ b/src/tracy.zig @@ -0,0 +1,50 @@ +//! Minimal Tracy client (the Zig compiler's pattern): `zone(@src(), "name")` +//! ... `.end()`, plus `frameMark()`. Everything compiles to a no-op unless the +//! build sets -Dtracy=<path-to-tracy-checkout>, which also compiles and links +//! TracyClient.cpp. Capture with tracy-capture, inspect with tracy-csvexport. +const std = @import("std"); +const builtin = @import("builtin"); + +pub const enable = if (builtin.is_test) false else @import("profile_options").enabled; + +const SourceLocation = extern struct { + name: ?[*:0]const u8, + function: [*:0]const u8, + file: [*:0]const u8, + line: u32, + color: u32, +}; + +pub const Ctx = if (enable) extern struct { + id: u32, + active: i32, + + pub inline fn end(c: @This()) void { + ___tracy_emit_zone_end(c); + } +} else struct { + pub inline fn end(_: @This()) void {} +}; + +pub inline fn zone(comptime src: std.builtin.SourceLocation, comptime name: [:0]const u8) Ctx { + if (!enable) return .{}; + const static = struct { + const loc: SourceLocation = .{ + .name = name.ptr, + .function = src.fn_name.ptr, + .file = src.file.ptr, + .line = src.line, + .color = 0, + }; + }; + return ___tracy_emit_zone_begin(&static.loc, 1); +} + +pub inline fn frameMark() void { + if (!enable) return; + ___tracy_emit_frame_mark(null); +} + +extern fn ___tracy_emit_zone_begin(srcloc: *const SourceLocation, active: c_int) Ctx; +extern fn ___tracy_emit_zone_end(ctx: Ctx) void; +extern fn ___tracy_emit_frame_mark(name: ?[*:0]const u8) void; |
