From e77b01d4b5504cea08a3f88cf8502897d868a1d3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 16 Nov 2026 18:04:21 -0300 Subject: Add glTF scene rendering, resource setup and 9P controls --- build.zig | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 6 deletions(-) (limited to 'build.zig') diff --git a/build.zig b/build.zig index eed0a02..efb4dfb 100644 --- a/build.zig +++ b/build.zig @@ -30,8 +30,25 @@ pub fn build(b: *Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const gltf = gltfModule(b, target, optimize); + b.modules.put(b.graph.arena, "gltf", gltf) catch @panic("OOM"); + const gltf_generator = b.addExecutable(.{ + .name = "gltf-bindings", + .root_module = b.createModule(.{ + .root_source_file = b.path("tools/gltf_bindings.zig"), + .target = b.graph.host, + .optimize = .ReleaseSafe, + .imports = &.{.{ .name = "gltf", .module = gltfModule(b, b.graph.host, .ReleaseSafe) }}, + }), + }); + b.installArtifact(gltf_generator); + // Target-free: they compile for whatever imports them, native or wasm. const abi = b.addModule("abi", .{ .root_source_file = b.path("src/abi.zig") }); + const gpu = b.addModule("gpu", .{ .root_source_file = b.path("src/gpu/Executor.zig"), .imports = &.{.{ .name = "abi", .module = abi }} }); + _ = b.addModule("gpu_scene", .{ .root_source_file = b.path("src/gpu/scene.zig"), .imports = &.{.{ .name = "abi", .module = abi }} }); + // Embedders provide their existing SDL translation and device. + if (b.option(bool, "gpu-only", "export GPU modules without building the sketch host") orelse false) return; // Geometry and noise, shared by the sketch library and `vector`. const math = b.addModule("math", .{ .root_source_file = b.path("src/math/root.zig") }); const sketch_imports: []const Build.Module.Import = &.{ .{ .name = "abi", .module = abi }, .{ .name = "math", .module = math } }; @@ -51,14 +68,21 @@ pub fn build(b: *Build) void { tracy.link_libcpp = true; } const c = cModule(b, target, optimize); + gpu.addImport("c", c); // No optimize mode of its own: the host's Zig code builds in the mode // of the executable it is part of (`script_optimize` makes use of it). + const cloud9 = b.dependency("cloud9", .{ .target = target, .optimize = optimize }).module("cloud9"); const host = b.addModule("host", .{ .root_source_file = b.path("src/host/root.zig"), .target = target, - .imports = &.{ .{ .name = "abi", .module = abi }, .{ .name = "c", .module = c }, .{ .name = "tracy", .module = tracy } }, + .imports = &.{ .{ .name = "abi", .module = abi }, .{ .name = "c", .module = c }, .{ .name = "gpu", .module = gpu }, .{ .name = "tracy", .module = tracy }, .{ .name = "cloud9", .module = cloud9 } }, }); + b.step("test-control", "Test 9P control validation and screenshot snapshots").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = host, .filters = &.{ "control writes", "9P reads" } })).step); + + const setup_tests = b.step("test-setup", "Test resource setup lifecycle and arguments"); + for ([_]*Build.Module{ host, b.createModule(.{ .root_source_file = b.path("src/sketch/root.zig"), .target = target, .optimize = optimize, .imports = sketch_imports }) }) |module| setup_tests.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = module, .filters = &.{"setup"} })).step); + const host_exe = b.addExecutable(.{ .name = "sketchbook", .root_module = b.createModule(.{ @@ -81,7 +105,7 @@ pub fn build(b: *Build) void { }); b.installArtifact(probe); - var sb: Sketchbook = .{ .b = b, .target = target, .optimize = optimize, .modules = .{ .abi = abi, .sketch = sketch, .host = host, .tracy = tracy, .vector = undefined, .scope = undefined, .anim = undefined }, .host_exe = host_exe, .probe_exe = probe, .web = b.path("web"), .glsl = b.path("src/glsl.zig"), .tools = b.path("tools"), .anim_dir = b.path("src/anim"), .script_optimize = optimize, .shared = .create(b) }; + var sb: Sketchbook = .{ .b = b, .target = target, .optimize = optimize, .modules = .{ .abi = abi, .sketch = sketch, .host = host, .tracy = tracy, .vector = undefined, .scope = undefined, .anim = undefined, .gltf = gltf }, .host_exe = host_exe, .probe_exe = probe, .gltf_generator = gltf_generator, .web = b.path("web"), .glsl = b.path("src/glsl.zig"), .tools = b.path("tools"), .anim_dir = b.path("src/anim"), .gltf_render_dir = b.path("src/gltf_render"), .script_optimize = optimize, .shared = .create(b) }; // Vector graphics: built with the same shader step apps use. Both forms // of its shaders are exported, so apps' web builds can swap them too. @@ -129,6 +153,11 @@ pub fn build(b: *Build) void { sb.modules.scope = b.addModule("scope", .{ .root_source_file = b.path("src/scope/root.zig"), .imports = scope_imports }); const test_step = b.step("test", "Run unit tests and the reload end-to-end test"); + const gltf_tests = b.addRunArtifact(b.addTest(.{ .root_module = gltf })); + b.step("test-gltf", "Test glTF parsing, extras and accessor reads").dependOn(&gltf_tests.step); + test_step.dependOn(&gltf_tests.step); + const scene_tests = b.createModule(.{ .root_source_file = b.path("src/gpu/scene.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "abi", .module = abi }} }); + test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = scene_tests })).step); for ([_]*Build.Module{ b.createModule(.{ .root_source_file = b.path("src/vector/root.zig"), .target = target, .optimize = optimize, .imports = vector_imports }), b.createModule(.{ .root_source_file = b.path("src/scope/root.zig"), .target = target, .optimize = optimize, .imports = scope_imports }), @@ -137,7 +166,7 @@ pub fn build(b: *Build) void { b.createModule(.{ .root_source_file = b.path("src/abi.zig"), .target = target, .optimize = optimize }), b.createModule(.{ .root_source_file = b.path("src/sketch/root.zig"), .target = target, .optimize = optimize, .imports = sketch_imports }), b.createModule(.{ .root_source_file = b.path("src/math/root.zig"), .target = target, .optimize = optimize }), - b.createModule(.{ .root_source_file = b.path("src/host/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "abi", .module = abi }, .{ .name = "c", .module = c }, .{ .name = "tracy", .module = tracy } } }), + b.createModule(.{ .root_source_file = b.path("src/host/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "abi", .module = abi }, .{ .name = "c", .module = c }, .{ .name = "gpu", .module = gpu }, .{ .name = "tracy", .module = tracy }, .{ .name = "cloud9", .module = cloud9 } } }), }) |module| test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = module })).step); // The noise tests again on wasm32 as the web builds it, under Node's @@ -264,7 +293,8 @@ pub fn build(b: *Build) void { exported.addArg("--define"); exported.addPrefixedDirectoryArg("b=", export_b.addExport(export_b.addExecutable(), export_options).frames); test_step.dependOn(&exported.step); - const raw = b.addSystemCommand(&.{ "sh", "-c", + const raw = b.addSystemCommand(&.{ + "sh", "-c", \\set -e \\test "$("$0" --state "$1" --export - --fps 30 --size 320x180 | wc -c)" = $((320 * 180 * 4 * 8)) \\command -v ffmpeg >/dev/null && command -v ffprobe >/dev/null || exit 0 @@ -300,6 +330,21 @@ pub fn build(b: *Build) void { test_step.dependOn(&replay.step); } +fn gltfModule(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *Build.Module { + const translator: translate_c.Translator = .init(b.dependency("translate_c", .{}), .{ + .c_source_file = b.path("vendor/cgltf.h"), + .target = target, + .optimize = optimize, + }); + translator.mod.addCSourceFile(.{ .file = b.path("vendor/cgltf.c"), .flags = &.{"-std=c99"} }); + return b.createModule(.{ + .root_source_file = b.path("src/gltf.zig"), + .target = target, + .optimize = optimize, + .imports = &.{.{ .name = "cgltf_c", .module = translator.mod }}, + }); +} + fn cModule(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *Build.Module { const sdl_dep = b.dependency("sdl", .{ .target = target, .optimize = optimize }); const sdl = sdl_dep.artifact("SDL3"); @@ -452,13 +497,15 @@ pub fn init(b: *Build, options: Options) Sketchbook { .b = b, .target = options.target, .optimize = options.optimize, - .modules = .{ .abi = dep.module("abi"), .sketch = dep.module("sketch"), .host = dep.module("host"), .tracy = dep.module("tracy"), .vector = dep.module("vector"), .scope = dep.module("scope"), .anim = dep.module("anim") }, + .modules = .{ .abi = dep.module("abi"), .sketch = dep.module("sketch"), .host = dep.module("host"), .tracy = dep.module("tracy"), .vector = dep.module("vector"), .scope = dep.module("scope"), .anim = dep.module("anim"), .gltf = dep.module("gltf") }, .host_exe = dep.artifact("sketchbook"), .probe_exe = dep.artifact("sketchbook-probe"), + .gltf_generator = dep.artifact("gltf-bindings"), .web = dep.path("web"), .glsl = dep.path("src/glsl.zig"), .tools = dep.path("tools"), .anim_dir = dep.path("src/anim"), + .gltf_render_dir = dep.path("src/gltf_render"), .script_optimize = options.script_optimize orelse options.optimize, .shared = shared, }; @@ -497,7 +544,8 @@ pub const Sketchbook = struct { /// `vector`: SVG, cubic geometry, and CPU and GPU renderers. `scope`: /// waveform, spectrogram, spectrum and oscilloscope views of a sound. /// `anim`: manim-style scenes as code (see `addScene`). - modules: struct { abi: *Build.Module, sketch: *Build.Module, host: *Build.Module, tracy: *Build.Module, vector: *Build.Module, scope: *Build.Module, anim: *Build.Module }, + /// `gltf`: native glTF/GLB documents, accessors and extras through cgltf. + modules: struct { abi: *Build.Module, sketch: *Build.Module, host: *Build.Module, tracy: *Build.Module, vector: *Build.Module, scope: *Build.Module, anim: *Build.Module, gltf: *Build.Module }, /// The generic host: loads and hot-reloads a sketch library. host_exe: *Build.Step.Compile, /// Prints any sketch's state file. @@ -516,6 +564,34 @@ pub const Sketchbook = struct { script_optimize: std.builtin.OptimizeMode, shared: *Shared, + /// Host executable; stays native even when the consumer is cross-compiled. + gltf_generator: *Build.Step.Compile, + gltf_render_dir: Build.LazyPath, + + /// Opaque native glTF rendering, with shader layouts generated from Zig types. + pub fn addGltfRenderer(sb: Sketchbook) *Build.Module { + const b = sb.b; + const types = b.createModule(.{ .root_source_file = sb.gltf_render_dir.path(b, "types.zig") }); + const shaders = sb.addShaders(.{ .layouts = .{ .module = types, .types = &.{"Transform"} }, .sources = &.{ + sb.gltf_render_dir.path(b, "basic.vert"), sb.gltf_render_dir.path(b, "basic.frag"), + } }); + const module = b.createModule(.{ .root_source_file = sb.gltf_render_dir.path(b, "root.zig"), .imports = &.{ + .{ .name = "sketch", .module = sb.modules.sketch }, .{ .name = "gltf", .module = sb.modules.gltf }, + .{ .name = "types", .module = types }, .{ .name = "shaders", .module = shaders.module }, + } }); + sb.shared.web_twins.put(b.graph.arena, module, b.createModule(.{ .root_source_file = sb.gltf_render_dir.path(b, "web.zig") })) catch @panic("OOM"); + return module; + } + + /// A cached generator run writes Zig source first; consumers compile it next. + /// Exact glTF labels become declarations; duplicate labels fail generation. + pub fn addGltf(sb: Sketchbook, source: Build.LazyPath) *Build.Module { + const run = sb.b.addRunArtifact(sb.gltf_generator); + run.addFileArg(source); + const generated = run.addOutputFileArg("gltf_bindings.zig"); + return sb.b.createModule(.{ .root_source_file = generated }); + } + pub const GlslLayouts = struct { run: *Build.Step.Run, /// `.glsl` for each type: pass it to `addGlslc` as an include. @@ -1481,6 +1557,8 @@ pub const App = struct { /// whose display name would be "generated". fn fileName(path: Build.LazyPath) []const u8 { return switch (path) { + .dependency => |d| std.fs.path.basename(d.sub_path), + .relative => |r| std.fs.path.basename(r.sub_path), .generated => |g| if (g.sub_path.len > 0) std.fs.path.basename(g.sub_path) else path.getDisplayName(), else => std.fs.path.basename(path.getDisplayName()), }; -- cgit v1.3