summaryrefslogtreecommitdiff
path: root/src/sketch
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-16 18:04:21 -0300
committerGabriel Schneider <[email protected]>2026-11-16 18:04:21 -0300
commite77b01d4b5504cea08a3f88cf8502897d868a1d3 (patch)
treec8e643d234dd26d920cbc563bfa7f740febf1fc7 /src/sketch
parentd3074636fff0fbcd5fd9b71c0c3b1c1f5c0c1285 (diff)
downloadsketchbook-e77b01d4b5504cea08a3f88cf8502897d868a1d3.tar.gz
sketchbook-e77b01d4b5504cea08a3f88cf8502897d868a1d3.zip
Add glTF scene rendering, resource setup and 9P controls
Diffstat (limited to 'src/sketch')
-rw-r--r--src/sketch/root.zig60
-rw-r--r--src/sketch/web.zig15
2 files changed, 72 insertions, 3 deletions
diff --git a/src/sketch/root.zig b/src/sketch/root.zig
index 52f05b2..434478c 100644
--- a/src/sketch/root.zig
+++ b/src/sketch/root.zig
@@ -10,6 +10,7 @@
//! pub const Store = struct { ... }; // on disk: survives restarts too
//! pub const commands = .{"hello"}; // console completions
//! pub fn init(state: *State, host: sk.Host) void
+//! pub fn setup(session: *Session, init: *sk.Init) !void
//! pub fn update(state: *State, in: *const sk.Input) void
//! pub fn draw(state: *const State, session: *Session, store: *Store, f: *sk.Frame) void
//! pub fn command(state: *State, store: *Store, c: *sk.Console, line: []const u8) !bool
@@ -59,6 +60,14 @@ pub const Voice = abi.Voice;
pub const BlendMode = abi.BlendMode;
pub const Face = abi.Face;
+/// Resource initialization, once per fresh Session (also when State is restored).
+/// No State, Input, scratch allocator, or drawing commands are available here.
+pub const Init = struct {
+ host: Host,
+ session: std.mem.Allocator,
+ store: std.mem.Allocator,
+};
+
/// What `draw` receives: input, host services, memory by lifetime and the
/// command list. Commands may be built from anything: the methods copy what
/// they keep (text, GPU descriptions) into `scratch`, except bulk data
@@ -288,7 +297,7 @@ pub const Console = struct {
/// The entry points of `App`, for a host that links it statically.
pub fn sketch(comptime App: type) abi.Sketch {
const G = Glue(App);
- return .{ .info = G.info, .defaults = G.defaults, .init = G.init, .update = G.update, .draw = G.draw, .command = G.command };
+ return .{ .info = G.info, .defaults = G.defaults, .init = G.init, .setup = G.setup, .update = G.update, .draw = G.draw, .command = G.command };
}
/// Exports `App` under `abi.Sketch.symbols`, for a hot-reloaded library.
@@ -298,6 +307,7 @@ pub fn exportSketch(comptime App: type) void {
@export(&G.info, .{ .name = names.info });
@export(&G.defaults, .{ .name = names.defaults });
@export(&G.init, .{ .name = names.init });
+ @export(&G.setup, .{ .name = names.setup });
@export(&G.update, .{ .name = names.update });
@export(&G.draw, .{ .name = names.draw });
@export(&G.command, .{ .name = names.command });
@@ -325,6 +335,7 @@ pub fn Glue(comptime App: type) type {
ctx: *const abi.Context,
state: [*]u8,
frame: ?*Frame = null,
+ resource_init: ?*Init = null,
console: ?*Console = null,
line: []const u8 = "",
};
@@ -342,6 +353,9 @@ pub fn Glue(comptime App: type) type {
};
if (comptime is.named("update") and (T == *Session or T == *Store))
@compileError("update must be deterministic: it gets State and Input only, so a replay reproduces it");
+ if (comptime is.named("setup") and (T == *State or T == *const State or T == *const Input))
+ @compileError("setup initializes resources: it gets Session, Store, Init and Host only");
+ if (comptime (T == *Init or T == *const Init) and !is.named("setup")) @compileError(name ++ " gets no Init: only setup does");
if (comptime is.named("draw") and T == *State) @compileError("draw only reads State: take *const State");
if (comptime T == *Frame and !is.named("draw")) @compileError(name ++ " gets no Frame: only draw does");
if (comptime T == *Console and !is.named("command")) @compileError(name ++ " gets no Console: only command does");
@@ -355,6 +369,8 @@ pub fn Glue(comptime App: type) type {
offer.ctx.input
else if (T == Host)
offer.ctx.host
+ else if (T == *Init or T == *const Init)
+ offer.resource_init.?
else if (T == *Frame)
offer.frame.?
else if (T == *Console)
@@ -380,6 +396,16 @@ pub fn Glue(comptime App: type) type {
pub fn init(ctx: *const abi.Context, s: [*]u8) callconv(.c) void {
if (@hasDecl(App, "init")) call(App.init, "init", .{ .ctx = ctx, .state = s });
}
+ pub fn setup(ctx: *const abi.Context) callconv(.c) bool {
+ if (!@hasDecl(App, "setup")) return true;
+ var resource_init: Init = .{ .host = ctx.host, .session = ctx.session, .store = ctx.store };
+ const result = call(App.setup, "setup", .{ .ctx = ctx, .state = undefined, .resource_init = &resource_init });
+ if (@typeInfo(@TypeOf(result)) == .error_union) result catch |err| {
+ ctx.host.print("session setup failed: {t}", .{err});
+ return false;
+ };
+ return true;
+ }
pub fn update(ctx: *const abi.Context, s: [*]u8) callconv(.c) void {
if (@hasDecl(App, "update")) call(App.update, "update", .{ .ctx = ctx, .state = s });
}
@@ -411,3 +437,35 @@ test {
_ = @import("list.zig");
_ = @import("derived.zig");
}
+
+test "setup receives session resources without a Frame and reports errors" {
+ const App = struct {
+ pub const Session = struct { bytes: []u8 = &.{}, calls: u32 = 0 };
+ pub fn setup(session: *Session, resources: *Init) !void {
+ session.calls += 1;
+ session.bytes = try resources.session.dupe(u8, "ready");
+ return error.TestSetupFailure;
+ }
+ };
+ const Log = struct {
+ fn log(ctx: *anyopaque, message: []const u8) void {
+ const seen: *bool = @ptrCast(@alignCast(ctx));
+ seen.* = std.mem.eql(u8, message, "session setup failed: TestSetupFailure");
+ }
+ };
+ var seen = false;
+ var vtable: abi.Host.VTable = undefined;
+ vtable.log = Log.log;
+ var session: App.Session = .{};
+ var ctx: abi.Context = undefined;
+ ctx.host = .{ .ctx = &seen, .vtable = &vtable };
+ ctx.session_root = std.mem.asBytes(&session);
+ ctx.session = std.testing.allocator;
+ ctx.store = std.testing.allocator;
+ const app = sketch(App);
+ try std.testing.expect(!app.setup(&ctx));
+ defer std.testing.allocator.free(session.bytes);
+ try std.testing.expectEqualStrings("ready", session.bytes);
+ try std.testing.expectEqual(@as(u32, 1), session.calls);
+ try std.testing.expect(seen);
+}
diff --git a/src/sketch/web.zig b/src/sketch/web.zig
index 3e102ad..682bff6 100644
--- a/src/sketch/web.zig
+++ b/src/sketch/web.zig
@@ -39,7 +39,7 @@ const js = struct {
extern "gpu" fn available() u32;
extern "gpu" fn newBuffer(size: u32) u32;
extern "gpu" fn freeBuffer(id: u32) void;
- extern "gpu" fn write(id: u32, offset: u32, ptr: [*]const u8, len: usize) void;
+ extern "gpu" fn write(id: u32, offset: u32, ptr: [*]const u8, len: usize) u32;
/// A `Dispatch` or a `Pass`, described in JSON (see `describe`).
extern "gpu" fn compute(ptr: [*]const u8, len: usize) void;
extern "gpu" fn pass(ptr: [*]const u8, len: usize) void;
@@ -78,6 +78,7 @@ fn Driver(comptime App: type) type {
const G = root.Glue(App);
const gpa = std.heap.wasm_allocator;
return struct {
+ var ready = false;
var state: G.State = undefined;
var session: G.Session = undefined;
// No disk on the web: the store lasts as long as the page.
@@ -124,6 +125,7 @@ fn Driver(comptime App: type) type {
js.canvas(G.description.width, G.description.height);
const ctx = context();
G.init(&ctx, std.mem.asBytes(&state));
+ ready = G.setup(&ctx);
}
/// The page's room for the canvas, in CSS pixels.
@@ -144,6 +146,10 @@ fn Driver(comptime App: type) type {
}
fn frame(ms: f64) callconv(.c) void {
+ if (!ready) {
+ event_count = 0;
+ return;
+ }
const now: u64 = @intFromFloat(@max(0, ms) * std.time.ns_per_ms);
const dt = if (last == 0) 0 else now -| last;
last = now;
@@ -219,7 +225,7 @@ fn Driver(comptime App: type) type {
js.push(v.key, v.samples.ptr, v.samples.len, @intFromBool(v.clear));
},
.microphone => js.listen(),
- .upload => |v| js.write(@backingInt(v.buffer), v.offset, v.bytes.ptr, v.bytes.len),
+ .upload => |v| _ = js.write(@backingInt(v.buffer), v.offset, v.bytes.ptr, v.bytes.len),
.compute => |v| {
const json = describe(v);
js.compute(json.ptr, json.len);
@@ -243,6 +249,7 @@ fn Driver(comptime App: type) type {
}
fn run(line: []const u8) void {
+ if (!ready) return;
const ctx = context();
if (G.command(&ctx, std.mem.asBytes(&state), &line) == .unknown) {
var buf: [160]u8 = undefined;
@@ -394,6 +401,7 @@ fn Driver(comptime App: type) type {
.gpu = gpu,
.newBuffer = newBuffer,
.freeBuffer = freeBuffer,
+ .writeBuffer = writeBuffer,
.imageSize = imageSize,
.textSize = textSize,
.loadSound = loadSound,
@@ -424,6 +432,9 @@ fn Driver(comptime App: type) type {
fn newBuffer(_: *anyopaque, size: u32) abi.Buffer {
return @fromBackingInt(js.newBuffer(size));
}
+ fn writeBuffer(_: *anyopaque, b: abi.Buffer, offset: u32, bytes: []const u8) !void {
+ if (js.write(@backingInt(b), offset, bytes.ptr, bytes.len) == 0) return error.BufferUploadFailed;
+ }
fn freeBuffer(_: *anyopaque, b: abi.Buffer) void {
js.freeBuffer(@backingInt(b));
}