diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/abi.zig | 24 | ||||
| -rw-r--r-- | src/gltf.zig | 156 | ||||
| -rw-r--r-- | src/gltf_generate.zig | 112 | ||||
| -rw-r--r-- | src/gltf_render/basic.frag | 10 | ||||
| -rw-r--r-- | src/gltf_render/basic.vert | 16 | ||||
| -rw-r--r-- | src/gltf_render/math.zig | 67 | ||||
| -rw-r--r-- | src/gltf_render/root.zig | 164 | ||||
| -rw-r--r-- | src/gltf_render/types.zig | 13 | ||||
| -rw-r--r-- | src/gltf_render/web.zig | 2 | ||||
| -rw-r--r-- | src/gltf_scene.zig | 416 | ||||
| -rw-r--r-- | src/gpu/Executor.zig (renamed from src/host/gpu.zig) | 157 | ||||
| -rw-r--r-- | src/gpu/scene.zig | 263 | ||||
| -rw-r--r-- | src/host/Host.zig | 177 | ||||
| -rw-r--r-- | src/host/control.zig | 368 | ||||
| -rw-r--r-- | src/host/gfx.zig | 9 | ||||
| -rw-r--r-- | src/host/root.zig | 5 | ||||
| -rw-r--r-- | src/sketch/root.zig | 60 | ||||
| -rw-r--r-- | src/sketch/web.zig | 15 |
18 files changed, 1980 insertions, 54 deletions
diff --git a/src/abi.zig b/src/abi.zig index 77a737d..2eed3a7 100644 --- a/src/abi.zig +++ b/src/abi.zig @@ -464,6 +464,8 @@ pub const gpu = struct { primitive: enum { triangle_list, triangle_strip, line_list, line_strip } = .triangle_list, blend: Blend = .premultiplied, write_color: bool = true, + depth_test: ?Compare = null, + depth_write: bool = false, /// Stencil test and update, per triangle facing. Needs a pass with /// a stencil buffer. stencil: ?struct { front: StencilFace, back: StencilFace } = null, @@ -527,6 +529,8 @@ pub const gpu = struct { primitive: @FieldType(Pipeline, "primitive") = .triangle_list, blend: Blend = .premultiplied, write_color: bool = true, + depth_test: ?Compare = null, + depth_write: bool = false, stencil: @FieldType(Pipeline, "stencil") = null, }) Pipeline { return .{ @@ -537,6 +541,8 @@ pub const gpu = struct { .primitive = p.primitive, .blend = p.blend, .write_color = p.write_color, + .depth_test = p.depth_test, + .depth_write = p.depth_write, .stencil = p.stencil, }; } @@ -564,11 +570,20 @@ pub const gpu = struct { /// 1, 2, 4 or 8. More than one renders multisampled and resolves. samples: u8 = 1, stencil: bool = false, + /// Clears depth to 1 each pass; pipelines choose comparison and writes. + depth: bool = false, /// Premultiplied; null keeps the image's previous contents. clear: ?[4]f32 = .{ 0, 0, 0, 0 }, pipelines: []const Pipeline, draws: []const Draw, }; + + /// A fullscreen triangle sampling one completed pass at fragment binding 0. + pub const PostProcess = struct { + vertex: Shader, + fragment: Shader, + fragment_uniforms: []const u8 = &.{}, + }; }; /// Host services. Handles and loaded data outlive reloads. @@ -593,6 +608,8 @@ pub const Host = struct { /// A GPU buffer of `size` bytes, zeroed; `.none` without a GPU. newBuffer: *const fn (*anyopaque, u32) Buffer, freeBuffer: *const fn (*anyopaque, Buffer) void, + /// Submits a copy now; source bytes may be released on return. + writeBuffer: *const fn (*anyopaque, Buffer, u32, []const u8) anyerror!void, /// Zero while unknown. imageSize: *const fn (*anyopaque, Image) [2]u32, /// What `Command.text` gives the bytes at a scale: the width they @@ -642,6 +659,10 @@ pub const Host = struct { pub fn freeBuffer(h: Host, buffer: Buffer) void { h.vtable.freeBuffer(h.ctx, buffer); } + /// Upload immediately, including during session setup. + pub fn writeBuffer(h: Host, buffer: Buffer, offset: u32, bytes: []const u8) !void { + try h.vtable.writeBuffer(h.ctx, buffer, offset, bytes); + } pub fn imageSize(h: Host, image: Image) [2]u32 { return h.vtable.imageSize(h.ctx, image); } @@ -766,6 +787,8 @@ pub const Sketch = struct { defaults: *const fn (Memory, [*]u8) callconv(.c) void, /// Setup for brand new state, after its defaults. init: *const fn (*const Context, [*]u8) callconv(.c) void, + /// Resource setup once per fresh Session; receives no State pointer. + setup: *const fn (*const Context) callconv(.c) bool, /// Deterministic: new state from state and input, nothing else. update: *const fn (*const Context, [*]u8) callconv(.c) void, /// Presentation: reads state, may change session and store, appends @@ -778,6 +801,7 @@ pub const Sketch = struct { .info = "sketch_info", .defaults = "sketch_defaults", .init = "sketch_init", + .setup = "sketch_setup", .update = "sketch_update", .draw = "sketch_draw", .command = "sketch_command", diff --git a/src/gltf.zig b/src/gltf.zig new file mode 100644 index 0000000..2d4ca47 --- /dev/null +++ b/src/gltf.zig @@ -0,0 +1,156 @@ +//! glTF/GLB documents backed by cgltf. Native/libc module; no renderer required. +//! The input bytes must outlive Document: GLB buffers can point into them. +//! C allocations are released by deinit; raw cgltf data stays available in `data`. +const std = @import("std"); +pub const c = @import("cgltf_c"); +pub const scene = @import("gltf_scene.zig"); +pub const Scene = scene.Scene; +/// Build-time code generation, not comptime glTF parsing. +pub const generate = @import("gltf_generate.zig").generate; + +test { + _ = @import("gltf_generate.zig"); + _ = scene; +} + +pub const Error = error{ + DataTooShort, + UnknownFormat, + InvalidJson, + InvalidGltf, + InvalidOptions, + FileNotFound, + IoError, + OutOfMemory, + LegacyGltf, + InvalidAccessor, + MissingBuffer, +}; + +fn check(result: c.cgltf_result) Error!void { + return switch (result) { + c.cgltf_result_success => {}, + c.cgltf_result_data_too_short => error.DataTooShort, + c.cgltf_result_unknown_format => error.UnknownFormat, + c.cgltf_result_invalid_json => error.InvalidJson, + c.cgltf_result_invalid_gltf => error.InvalidGltf, + c.cgltf_result_invalid_options => error.InvalidOptions, + c.cgltf_result_file_not_found => error.FileNotFound, + c.cgltf_result_io_error => error.IoError, + c.cgltf_result_out_of_memory => error.OutOfMemory, + c.cgltf_result_legacy_gltf => error.LegacyGltf, + else => error.InvalidGltf, + }; +} + +pub const Document = struct { + data: *c.cgltf_data, + + /// Parses and validates structure. Does not perform filesystem I/O. + pub fn parse(bytes: []const u8) Error!Document { + const options = std.mem.zeroes(c.cgltf_options); + var data: ?*c.cgltf_data = null; + try check(c.cgltf_parse(&options, bytes.ptr, bytes.len, &data)); + const doc: Document = .{ .data = data.? }; + errdefer doc.deinit(); + try check(c.cgltf_validate(doc.data)); + return doc; + } + + pub fn deinit(self: Document) void { + c.cgltf_free(self.data); + } + + /// Binds GLB/data-URI buffers. A non-null source path also permits external + /// buffer files, resolved relative to that glTF file; only pass trusted paths. + /// Images and compressed geometry are not decoded by this module. + pub fn loadBuffers(self: Document, source_path: ?[:0]const u8) Error!void { + const options = std.mem.zeroes(c.cgltf_options); + try check(c.cgltf_load_buffers(&options, self.data, if (source_path) |p| p.ptr else null)); + try check(c.cgltf_validate(self.data)); + } + + pub fn nodes(self: Document) []c.cgltf_node { + return if (self.data.nodes_count == 0) &.{} else self.data.nodes[0..self.data.nodes_count]; + } + + pub fn findNode(self: Document, name: []const u8) ?*c.cgltf_node { + for (self.nodes()) |*node| { + if (node.name != null and std.mem.eql(u8, std.mem.span(node.name), name)) return node; + } + return null; + } +}; + +/// Borrowed JSON, ready for std.json.parseFromSlice with the caller's schema. +pub fn extrasJson(extras: *const c.cgltf_extras) ?[]const u8 { + return if (extras.data != null) std.mem.span(extras.data) else null; +} + +/// Read one element after loadBuffers; handles strides, normalization and sparse +/// accessors. Bounds and component count are checked before entering C. +pub fn readFloats(accessor: [*c]const c.cgltf_accessor, index: usize, out: []f32) Error!void { + if (accessor == null or index >= accessor.*.count or out.len == 0 or out.len != c.cgltf_num_components(accessor.*.type)) return error.InvalidAccessor; + if (accessor.*.is_sparse != 0 and + (c.cgltf_buffer_view_data(accessor.*.sparse.indices_buffer_view) == null or + c.cgltf_buffer_view_data(accessor.*.sparse.values_buffer_view) == null)) return error.MissingBuffer; + if (c.cgltf_accessor_read_float(accessor, index, out.ptr, out.len) == 0) return error.MissingBuffer; +} + +test "node names and arbitrary typed extras survive parsing" { + const bytes = + \\{"asset":{"version":"2.0"},"nodes":[{"name":"Target","extras":{"role":"look_at_target","id":"target","future":1}}]} + ; + const doc = try Document.parse(bytes); + defer doc.deinit(); + try doc.loadBuffers(null); + const node = doc.findNode("Target").?; + const meta = try std.json.parseFromSlice(struct { role: []const u8, id: []const u8 }, std.testing.allocator, extrasJson(&node.extras).?, .{ .ignore_unknown_fields = true }); + defer meta.deinit(); + try std.testing.expectEqualStrings("look_at_target", meta.value.role); + try std.testing.expectEqualStrings("target", meta.value.id); + try std.testing.expect(doc.findNode("missing") == null); + try std.testing.expect(extrasJson(&std.mem.zeroes(c.cgltf_extras)) == null); +} + +test "embedded buffers and accessor bounds" { + const bytes = + \\{"asset":{"version":"2.0"},"buffers":[{"byteLength":4,"uri":"data:application/octet-stream;base64,AACAPw=="}],"bufferViews":[{"buffer":0,"byteLength":4}],"accessors":[{"bufferView":0,"componentType":5126,"count":1,"type":"SCALAR"}]} + ; + const doc = try Document.parse(bytes); + defer doc.deinit(); + const accessor = &doc.data.accessors[0]; + var value: [1]f32 = undefined; + try std.testing.expectError(error.MissingBuffer, readFloats(accessor, 0, &value)); + try doc.loadBuffers(null); + try readFloats(accessor, 0, &value); + try std.testing.expectEqual(@as(f32, 1), value[0]); + try std.testing.expectError(error.InvalidAccessor, readFloats(accessor, 1, &value)); + var wrong: [3]f32 = undefined; + try std.testing.expectError(error.InvalidAccessor, readFloats(accessor, 0, &wrong)); +} + +test "malformed and truncated input fails" { + try std.testing.expectError(error.DataTooShort, Document.parse("glTF")); + try std.testing.expectError(error.InvalidJson, Document.parse("{\"asset\": invalid json}")); + const invalid = + \\{"asset":{"version":"2.0"},"buffers":[{"byteLength":4}],"bufferViews":[{"buffer":0,"byteLength":16}],"accessors":[{"bufferView":0,"componentType":5126,"count":4,"type":"SCALAR"}]} + ; + try std.testing.expectError(error.DataTooShort, Document.parse(invalid)); +} + +test "sparse values need loaded buffers and preserve zero base elements" { + const bytes = + \\{"asset":{"version":"2.0"},"buffers":[{"byteLength":16,"uri":"data:application/octet-stream;base64,AQAAAAAAgD8AAABAAABAQA=="}],"bufferViews":[{"buffer":0,"byteLength":1},{"buffer":0,"byteOffset":4,"byteLength":12}],"accessors":[{"componentType":5126,"count":2,"type":"VEC3","sparse":{"count":1,"indices":{"bufferView":0,"componentType":5121},"values":{"bufferView":1}}}]} + ; + const doc = try Document.parse(bytes); + defer doc.deinit(); + const accessor = &doc.data.accessors[0]; + var out: [3]f32 = undefined; + try std.testing.expectError(error.MissingBuffer, readFloats(accessor, 1, &out)); + try doc.loadBuffers(null); + try readFloats(accessor, 0, &out); + try std.testing.expectEqualSlices(f32, &.{ 0, 0, 0 }, &out); + try readFloats(accessor, 1, &out); + try std.testing.expectEqualSlices(f32, &.{ 1, 2, 3 }, &out); +} diff --git a/src/gltf_generate.zig b/src/gltf_generate.zig new file mode 100644 index 0000000..a1d1449 --- /dev/null +++ b/src/gltf_generate.zig @@ -0,0 +1,112 @@ +//! Runs before compilation: glTF labels/extras become ordinary Zig constants. +const std = @import("std"); +const gltf = @import("gltf.zig"); + +pub fn generate(allocator: std.mem.Allocator, bytes: []const u8, source_path: []const u8) ![]u8 { + const doc = try gltf.Document.parse(bytes); + defer doc.deinit(); + var out: std.ArrayList(u8) = .empty; + defer out.deinit(allocator); + try out.appendSlice(allocator, "// Generated from glTF; do not edit.\n"); + try out.print(allocator, "pub const source_path: []const u8 = \"{f}\";\n", .{std.zig.fmtString(source_path)}); + var hash: [32]u8 = undefined; + std.crypto.hash.sha2.Sha256.hash(bytes, &hash, .{}); + try out.appendSlice(allocator, "pub const source_sha256: [32]u8 = .{"); + for (hash) |byte| try out.print(allocator, "{d},", .{byte}); + try out.appendSlice(allocator, "};\n"); + try labels(allocator, &out, "nodes", doc.nodes(), doc.data); + try labels(allocator, &out, "animations", if (doc.data.animations_count == 0) &.{} else doc.data.animations[0..doc.data.animations_count], doc.data); + try labels(allocator, &out, "cameras", if (doc.data.cameras_count == 0) &.{} else doc.data.cameras[0..doc.data.cameras_count], doc.data); + try labels(allocator, &out, "meshes", if (doc.data.meshes_count == 0) &.{} else doc.data.meshes[0..doc.data.meshes_count], doc.data); + try labels(allocator, &out, "materials", if (doc.data.materials_count == 0) &.{} else doc.data.materials[0..doc.data.materials_count], doc.data); + const source = try allocator.allocSentinel(u8, out.items.len, 0); + defer allocator.free(source); + @memcpy(source, out.items); + var ast = try std.zig.Ast.parse(allocator, source, .{}); + defer ast.deinit(allocator); + if (ast.errors.len != 0) return error.InvalidGeneratedZig; + return ast.renderAlloc(allocator); +} + +fn labels(a: std.mem.Allocator, out: *std.ArrayList(u8), category: []const u8, items: anytype, data: *const gltf.c.cgltf_data) !void { + var names: std.StringHashMapUnmanaged(void) = .empty; + defer names.deinit(a); + try out.print(a, "pub const {s} = struct {{\n", .{category}); + for (items, 0..) |item, index| { + if (item.name == null) continue; + const name = item.name[0..gltf.c.cgltf_decode_string(item.name)]; + if (name.len == 0) continue; + const entry = try names.getOrPut(a, name); + if (entry.found_existing) return error.DuplicateLabel; + try out.print(a, "pub const {f} = .{{ .index = @as(usize, {d}), .extras = ", .{ std.zig.fmtId(name), index }); + if (gltf.extrasJson(&item.extras)) |json| { + const parsed = try std.json.parseFromSlice(std.json.Value, a, json, .{}); + defer parsed.deinit(); + try value(a, out, parsed.value); + } else try out.appendSlice(a, ".{}"); + if (@TypeOf(item) == gltf.c.cgltf_node) { + try out.appendSlice(a, ", .camera = @as(?usize, "); + if (item.camera != null) { + try out.print(a, "{d}", .{gltf.c.cgltf_camera_index(data, item.camera)}); + } else try out.appendSlice(a, "null"); + try out.appendSlice(a, "), .mesh = @as(?usize, "); + if (item.mesh != null) { + try out.print(a, "{d}", .{gltf.c.cgltf_mesh_index(data, item.mesh)}); + } else try out.appendSlice(a, "null"); + try out.appendSlice(a, ")"); + } + try out.appendSlice(a, " };\n"); + } + try out.appendSlice(a, "};\n"); +} + +fn value(a: std.mem.Allocator, out: *std.ArrayList(u8), json: std.json.Value) std.mem.Allocator.Error!void { + switch (json) { + .null => try out.appendSlice(a, "null"), + .bool => |v| try out.appendSlice(a, if (v) "true" else "false"), + .integer => |v| try out.print(a, "@as(i64, {d})", .{v}), + .float => |v| try out.print(a, "@as(f64, {e})", .{v}), + .number_string => |v| try out.appendSlice(a, v), + .string => |v| try out.print(a, "@as([]const u8, \"{f}\")", .{std.zig.fmtString(v)}), + .array => |array| { + try out.appendSlice(a, ".{"); + for (array.items) |item| { + try value(a, out, item); + try out.appendSlice(a, ","); + } + try out.appendSlice(a, "}"); + }, + .object => |object| { + try out.appendSlice(a, ".{"); + for (object.keys(), object.values()) |key, item| { + try out.print(a, ".{f} = ", .{std.zig.fmtId(key)}); + try value(a, out, item); + try out.appendSlice(a, ","); + } + try out.appendSlice(a, "}"); + }, + } +} + +test "generated labels and nested metadata escape as valid Zig" { + const source = try generate(std.testing.allocator, + \\{"asset":{"version":"2.0"},"nodes":[{"name":"a\"b","extras":{"type":"look\nat","nested":{"enabled":true},"points":[1,2.5,-0.0,null]}},{"name":"while"},{"extras":{}}],"animations":[]} + , "fixtures/a\"b.glb"); + defer std.testing.allocator.free(source); + const terminated = try std.testing.allocator.allocSentinel(u8, source.len, 0); + @memcpy(terminated, source); + defer std.testing.allocator.free(terminated); + var ast = try std.zig.Ast.parse(std.testing.allocator, terminated, .{}); + defer ast.deinit(std.testing.allocator); + try std.testing.expectEqual(@as(usize, 0), ast.errors.len); + try std.testing.expect(std.mem.indexOf(u8, source, "pub const source_path: []const u8 = \"fixtures/a\\\"b.glb\";") != null); + try std.testing.expect(std.mem.indexOf(u8, source, "pub const @\"a\\\"b\"") != null); + try std.testing.expect(std.mem.indexOf(u8, source, "pub const @\"while\"") != null); + try std.testing.expect(std.mem.indexOf(u8, source, ".index = @as(usize, 1)") != null); +} + +test "duplicate labels cannot silently choose an object" { + try std.testing.expectError(error.DuplicateLabel, generate(std.testing.allocator, + \\{"asset":{"version":"2.0"},"nodes":[{"name":"Camera"},{"name":"Camera"}]} + , "fixtures/duplicate.glb")); +} diff --git a/src/gltf_render/basic.frag b/src/gltf_render/basic.frag new file mode 100644 index 0000000..73209ea --- /dev/null +++ b/src/gltf_render/basic.frag @@ -0,0 +1,10 @@ +#version 450 +layout(location = 0) in vec3 world_normal; +layout(location = 1) in vec4 color; +layout(location = 0) out vec4 out_color; +void main() { + vec3 normal = normalize(world_normal); + if (!gl_FrontFacing) normal = -normal; + float light = 0.42 + 0.58 * max(dot(normal, normalize(vec3(-0.45, 0.85, 0.3))), 0.0); + out_color = vec4(pow(max(color.rgb * light, vec3(0.0)), vec3(1.0 / 2.2)), 1.0); +} diff --git a/src/gltf_render/basic.vert b/src/gltf_render/basic.vert new file mode 100644 index 0000000..3239a38 --- /dev/null +++ b/src/gltf_render/basic.vert @@ -0,0 +1,16 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +layout(std430, set = 0, binding = 0) readonly buffer Vertices { float data[]; }; +#include "Transform.glsl" +layout(location = 0) out vec3 world_normal; +layout(location = 1) out vec4 color; +void main() { + uint offset = uint(gl_VertexIndex) * 10; + vec3 position = vec3(data[offset], data[offset+1], data[offset+2]); + vec3 normal = vec3(data[offset+3], data[offset+4], data[offset+5]); + mat4 model = mat4(transform.model[0], transform.model[1], transform.model[2], transform.model[3]); + mat4 vp = mat4(transform.view_projection[0], transform.view_projection[1], transform.view_projection[2], transform.view_projection[3]); + gl_Position = vp * model * vec4(position, 1.0); + world_normal = transpose(inverse(mat3(model))) * normal; + color = transform.color * vec4(data[offset+6], data[offset+7], data[offset+8], data[offset+9]); +} diff --git a/src/gltf_render/math.zig b/src/gltf_render/math.zig new file mode 100644 index 0000000..6279ce4 --- /dev/null +++ b/src/gltf_render/math.zig @@ -0,0 +1,67 @@ +const std = @import("std"); +pub const Mat4 = [16]f32; +pub const identity: Mat4 = .{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; +pub fn multiply(a: Mat4, b: Mat4) Mat4 { + var out: Mat4 = @splat(0); + for (0..4) |col| for (0..4) |row| { + for (0..4) |k| out[col * 4 + row] += a[k * 4 + row] * b[col * 4 + k]; + }; + return out; +} +fn sub(a: [3]f32, b: [3]f32) [3]f32 { + return .{ a[0] - b[0], a[1] - b[1], a[2] - b[2] }; +} +fn dot(a: [3]f32, b: [3]f32) f32 { + return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; +} +fn cross(a: [3]f32, b: [3]f32) [3]f32 { + return .{ a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] }; +} +fn unit(a: [3]f32) [3]f32 { + const n = @sqrt(dot(a, a)); + return .{ a[0] / n, a[1] / n, a[2] / n }; +} +pub const Camera = struct { + eye: [3]f32, + target: [3]f32, + up: [3]f32 = .{ 0, 1, 0 }, + fov: f32 = std.math.pi / 3.0, + near: f32 = 0.1, + far: f32 = 300, + aspect_ratio: ?f32 = null, + orthographic: ?struct { xmag: f32, ymag: f32 } = null, + /// Right-handed, column-major; depth range 0..1, camera forward -Z. + pub fn matrix(c: Camera, aspect: f32) Mat4 { + const z = unit(sub(c.eye, c.target)); + const x = unit(cross(c.up, z)); + const y = cross(z, x); + const view: Mat4 = .{ x[0], y[0], z[0], 0, x[1], y[1], z[1], 0, x[2], y[2], z[2], 0, -dot(x, c.eye), -dot(y, c.eye), -dot(z, c.eye), 1 }; + const scale = 1 / @tan(c.fov / 2); + const d = c.near - c.far; + const projection: Mat4 = if (c.orthographic) |o| + .{ 1 / o.xmag, 0, 0, 0, 0, 1 / o.ymag, 0, 0, 0, 0, 1 / d, 0, 0, 0, c.near / d, 1 } + else if (std.math.isInf(c.far)) + .{ scale / (c.aspect_ratio orelse aspect), 0, 0, 0, 0, scale, 0, 0, 0, 0, -1, -1, 0, 0, -c.near, 0 } + else + .{ scale / (c.aspect_ratio orelse aspect), 0, 0, 0, 0, scale, 0, 0, 0, 0, c.far / d, -1, 0, 0, c.near * c.far / d, 0 }; + return multiply(projection, view); + } +}; +test "perspective maps near and far to zero and one" { + const camera: Camera = .{ .eye = .{ 0, 0, 0 }, .target = .{ 0, 0, -1 } }; + const m = camera.matrix(1); + try std.testing.expectApproxEqAbs(@as(f32, 0), (m[10] * -camera.near + m[14]) / camera.near, 0.00001); + try std.testing.expectApproxEqAbs(@as(f32, 1), (m[10] * -camera.far + m[14]) / camera.far, 0.00001); +} + +test "orthographic and infinite perspective keep valid depth" { + var camera: Camera = .{ .eye = .{ 0, 0, 0 }, .target = .{ 0, 0, -1 }, .orthographic = .{ .xmag = 2, .ymag = 3 } }; + const ortho = camera.matrix(1); + try std.testing.expectApproxEqAbs(@as(f32, 0), ortho[10] * -camera.near + ortho[14], 0.00001); + try std.testing.expectApproxEqAbs(@as(f32, 1), ortho[10] * -camera.far + ortho[14], 0.00001); + camera.orthographic = null; + camera.far = std.math.inf(f32); + const infinite = camera.matrix(1); + for (infinite) |v| try std.testing.expect(std.math.isFinite(v)); + try std.testing.expectApproxEqAbs(@as(f32, 0), (infinite[10] * -camera.near + infinite[14]) / camera.near, 0.00001); +} diff --git a/src/gltf_render/root.zig b/src/gltf_render/root.zig new file mode 100644 index 0000000..6bf8673 --- /dev/null +++ b/src/gltf_render/root.zig @@ -0,0 +1,164 @@ +//! Basic opaque glTF drawing. Geometry uploads once; each frame submits transforms. +//! shortcut: base colors only; add texture/alpha handling when the assets need it. +const std = @import("std"); +const sk = @import("sketch"); +const gltf = @import("gltf"); +const shaders = @import("shaders"); +const types = @import("types"); +pub const math = @import("math.zig"); +pub const Camera = math.Camera; +pub const Vertex = types.Vertex; +pub const supported = true; +const gpu = sk.abi.gpu; + +/// Camera projection plus the sampled node transform; no Blender constraint solver. +pub fn sceneCamera(scene: *const gltf.Scene, node_index: usize) !Camera { + if (node_index >= scene.nodes.len) return error.InvalidCameraNode; + const node = scene.nodes[node_index]; + const index = node.camera orelse return error.NodeHasNoCamera; + const m = node.world; + var camera: Camera = .{ + .eye = .{ m[12], m[13], m[14] }, + .target = .{ m[12] - m[8], m[13] - m[9], m[14] - m[10] }, + .up = .{ m[4], m[5], m[6] }, + }; + switch (scene.cameras[index]) { + .perspective => |p| { + camera.fov = p.yfov; + camera.near = p.znear; + camera.far = p.zfar orelse std.math.inf(f32); + camera.aspect_ratio = p.aspect_ratio; + }, + .orthographic => |o| { + camera.orthographic = .{ .xmag = o.xmag, .ymag = o.ymag }; + camera.near = o.znear; + camera.far = o.zfar; + }, + } + return camera; +} + +pub const Geometry = struct { + vertices: []const Vertex, + buffer: sk.abi.Buffer = .none, + pub fn upload(g: *Geometry, host: sk.Host) !void { + if (g.buffer != .none) return; + const bytes = std.mem.sliceAsBytes(g.vertices); + if (bytes.len == 0 or bytes.len > std.math.maxInt(u32)) return error.InvalidGeometrySize; + g.buffer = host.newBuffer(@intCast(bytes.len)); + if (g.buffer == .none) return error.GpuBufferCreationFailed; + errdefer g.release(host); + try host.writeBuffer(g.buffer, 0, bytes); + } + pub fn release(g: *Geometry, host: sk.Host) void { + if (g.buffer != .none) host.freeBuffer(g.buffer); + g.buffer = .none; + } +}; + +pub const Model = struct { + scene: gltf.Scene, + geometry: []Geometry, + /// Read the generated asset filename, validate its binding hash, and upload once. + pub fn load(setup: *sk.Init, comptime asset: type) !Model { + const bytes = setup.host.readFile(asset.source_path); + var hash: [32]u8 = undefined; + std.crypto.hash.sha2.Sha256.hash(bytes, &hash, .{}); + if (!std.mem.eql(u8, &hash, &asset.source_sha256)) return error.SceneChangedRebuildBindings; + var model = try Model.init(setup.session, bytes); + errdefer model.release(setup.host); + for (model.geometry) |*g| try g.upload(setup.host); + return model; + } + /// Allocate in the sketch's Session arena; GPU handles share its host lifetime. + pub fn init(arena: std.mem.Allocator, bytes: []const u8) !Model { + const doc = try gltf.Document.parse(bytes); + defer doc.deinit(); + try doc.loadBuffers(null); + var scene = try gltf.Scene.init(arena, doc); + errdefer scene.deinit(); + const geometry = try arena.alloc(Geometry, scene.meshes.len); + for (scene.meshes, geometry) |mesh, *g| { + var count: usize = 0; + for (mesh.primitives) |primitive| count = try std.math.add(usize, count, primitive.indices.len); + const vertices = try arena.alloc(Vertex, count); + var offset: usize = 0; + // Opaque materials share one pipeline: carry color per vertex to batch a mesh. + for (mesh.primitives) |primitive| { + for (primitive.indices) |index| { + const source = primitive.vertices[index]; + vertices[offset] = .{ .position = source.position, .normal = source.normal, .color = primitive.color }; + offset += 1; + } + } + g.* = .{ .vertices = vertices }; + } + return .{ .scene = scene, .geometry = geometry }; + } + pub fn release(m: *Model, host: sk.Host) void { + for (m.geometry) |*g| g.release(host); + } +}; + +/// Imperative frame builder: add a whole scene, individual meshes, or geometry. +pub const View = struct { + frame: *sk.Frame, + image: sk.Image, + width: u32, + height: u32, + vp: math.Mat4, + draws: std.ArrayList(gpu.Draw) = .empty, + background: [4]f32 = .{ 0.54, 0.72, 0.83, 1 }, + + pub fn init(f: *sk.Frame, image: sk.Image, width: u32, height: u32, camera: Camera) View { + return .{ .frame = f, .image = image, .width = width, .height = height, .vp = camera.matrix(@as(f32, @floatFromInt(width)) / @as(f32, @floatFromInt(height))) }; + } + pub fn scene(v: *View, model: *Model) !void { + for (model.scene.nodes) |node| if (node.visible) { + if (node.mesh) |index| try v.mesh(model, index, node.world); + }; + } + pub fn mesh(v: *View, model: *Model, index: usize, transform: math.Mat4) !void { + if (index >= model.geometry.len) return error.InvalidMesh; + try v.triangles(&model.geometry[index], transform, .{ 1, 1, 1, 1 }); + } + pub fn triangles(v: *View, geometry: *Geometry, transform: math.Mat4, color: [4]f32) !void { + if (geometry.buffer == .none) return error.GeometryNotUploaded; + try v.draws.append(v.frame.scratch, .{ + .pipeline = 0, + .vertex_count = @intCast(geometry.vertices.len), + .vertex_storage = v.frame.keep(sk.abi.Buffer, &.{geometry.buffer}), + .vertex_uniforms = gpu.uniform(v.frame.scratch, types.Transform{ + .model = @bitCast(transform), + .view_projection = @bitCast(v.vp), + .color = color, + }), + }); + } + pub fn end(v: *View) void { + v.frame.gpu(.{ + .label = "gltf scene", + .image = v.image, + .width = v.width, + .height = v.height, + .depth = true, + .clear = v.background, + .pipelines = &.{pipeline}, + .draws = v.draws.items, + }); + } +}; +const pipeline: gpu.Pipeline = .{ + .vertex = .{ .code = shaders.basic_vert, .uniform_buffers = 1, .storage_buffers = 1 }, + .fragment = .{ .code = shaders.basic_frag }, + .stride = 0, + .attributes = &.{}, + .blend = .none, + .depth_test = .less, + .depth_write = true, +}; +comptime { + // std430 scalar storage is ten contiguous floats, with no vec3 padding. + if (@sizeOf(Vertex) != 40 or @offsetOf(Vertex, "normal") != 12 or @offsetOf(Vertex, "color") != 24) @compileError("unexpected glTF vertex layout"); + gpu.assertStd140(types.Transform); +} diff --git a/src/gltf_render/types.zig b/src/gltf_render/types.zig new file mode 100644 index 0000000..29b784f --- /dev/null +++ b/src/gltf_render/types.zig @@ -0,0 +1,13 @@ +//! These types generate the GLSL vertex and uniform declarations. +pub const Vertex = extern struct { + position: [3]f32, + normal: [3]f32, + color: [4]f32 = .{ 1, 1, 1, 1 }, + pub const glsl = .vertex; +}; +pub const Transform = extern struct { + model: [4][4]f32, + view_projection: [4][4]f32, + color: [4]f32, + pub const glsl = .{ .uniform = .{ .set = 1, .instance = "transform" } }; +}; diff --git a/src/gltf_render/web.zig b/src/gltf_render/web.zig new file mode 100644 index 0000000..f8dc252 --- /dev/null +++ b/src/gltf_render/web.zig @@ -0,0 +1,2 @@ +//! The cgltf loader currently requires libc; the existing 2D web game still works. +pub const supported = false; diff --git a/src/gltf_scene.zig b/src/gltf_scene.zig new file mode 100644 index 0000000..0b8ef03 --- /dev/null +++ b/src/gltf_scene.zig @@ -0,0 +1,416 @@ +//! CPU mesh data and sampled node transforms. No renderer or GPU ownership. +const std = @import("std"); +const gltf = @import("gltf.zig"); +const c = gltf.c; + +pub const Vertex = extern struct { position: [3]f32, normal: [3]f32 }; +pub const Primitive = struct { + vertices: []Vertex, + indices: []u32, + color: [4]f32 = .{ 1, 1, 1, 1 }, + double_sided: bool = false, +}; +pub const Mesh = struct { primitives: []Primitive }; +pub const Camera = union(enum) { + perspective: struct { yfov: f32, znear: f32, zfar: ?f32, aspect_ratio: ?f32 }, + orthographic: struct { xmag: f32, ymag: f32, znear: f32, zfar: f32 }, +}; +pub const Node = struct { + mesh: ?usize, + camera: ?usize = null, + parent: ?usize, + world: [16]f32 = undefined, + visible: bool, +}; +const Track = struct { + node: usize, + path: c.cgltf_animation_path_type, + interpolation: c.cgltf_interpolation_type, + times: []f32, + values: [][4]f32, +}; +const Animation = struct { tracks: []Track, duration: f32 }; + +pub const Scene = struct { + arena: std.heap.ArenaAllocator, + meshes: []Mesh, + cameras: []Camera, + nodes: []Node, + animations: []Animation, + base: []c.cgltf_node, + pose: []c.cgltf_node, + + /// Copies geometry and tracks from a document whose buffers are loaded. + /// The document can be released after this returns. Indices match glTF. + pub fn init(allocator: std.mem.Allocator, document: gltf.Document) !Scene { + var arena = std.heap.ArenaAllocator.init(allocator); + errdefer arena.deinit(); + const a = arena.allocator(); + const data = document.data; + const meshes = try a.alloc(Mesh, data.meshes_count); + for (meshes, 0..) |*mesh, i| { + const source = data.meshes[i]; + mesh.primitives = try a.alloc(Primitive, source.primitives_count); + for (mesh.primitives, 0..) |*primitive, j| primitive.* = try readPrimitive(a, @ptrCast(&source.primitives[j])); + } + const cameras = try a.alloc(Camera, data.cameras_count); + for (cameras, 0..) |*camera, i| { + const source = data.cameras[i]; + camera.* = switch (source.type) { + c.cgltf_camera_type_perspective => blk: { + const p = source.data.perspective; + try finite(&.{ p.yfov, p.znear }); + if (p.yfov <= 0 or p.yfov >= std.math.pi or p.znear <= 0) return error.InvalidCamera; + if (p.has_zfar != 0 and (!std.math.isFinite(p.zfar) or p.zfar <= p.znear)) return error.InvalidCamera; + if (p.has_aspect_ratio != 0 and (!std.math.isFinite(p.aspect_ratio) or p.aspect_ratio <= 0)) return error.InvalidCamera; + break :blk .{ .perspective = .{ + .yfov = p.yfov, + .znear = p.znear, + .zfar = if (p.has_zfar != 0) p.zfar else null, + .aspect_ratio = if (p.has_aspect_ratio != 0) p.aspect_ratio else null, + } }; + }, + c.cgltf_camera_type_orthographic => blk: { + const o = source.data.orthographic; + try finite(&.{ o.xmag, o.ymag, o.znear, o.zfar }); + if (o.xmag == 0 or o.ymag == 0 or o.znear < 0 or o.zfar <= o.znear) return error.InvalidCamera; + break :blk .{ .orthographic = .{ .xmag = o.xmag, .ymag = o.ymag, .znear = o.znear, .zfar = o.zfar } }; + }, + else => return error.UnsupportedCamera, + }; + } + const nodes = try a.alloc(Node, data.nodes_count); + const base = try a.dupe(c.cgltf_node, document.nodes()); + for (nodes, base) |*node, source| { + if (source.skin != null) return error.UnsupportedSkin; + if (source.has_mesh_gpu_instancing != 0) return error.UnsupportedInstancing; + node.* = .{ + .mesh = if (source.mesh != null) c.cgltf_mesh_index(data, source.mesh) else null, + .camera = if (source.camera != null) c.cgltf_camera_index(data, source.camera) else null, + .parent = if (source.parent != null) c.cgltf_node_index(data, source.parent) else null, + .visible = data.scene == null, + }; + } + // A glTF scene selects roots; other scene nodes stay addressable but hidden. + if (data.scene != null) for (nodes, 0..) |*node, i| { + var root = i; + var depth: usize = 0; + while (nodes[root].parent) |parent| { + root = parent; + depth += 1; + if (depth > nodes.len) return error.InvalidHierarchy; + } + const selected = data.scene.*; + for (0..selected.nodes_count) |j| { + if (root == c.cgltf_node_index(data, selected.nodes[j])) node.visible = true; + } + }; + const animations = try a.alloc(Animation, data.animations_count); + for (animations, 0..) |*animation, i| { + const source = data.animations[i]; + animation.* = .{ .tracks = try a.alloc(Track, source.channels_count), .duration = 0 }; + for (animation.tracks, 0..) |*track, j| { + const channel = source.channels[j]; + if (channel.target_node == null) return error.UnsupportedAnimationTarget; + const node = c.cgltf_node_index(data, channel.target_node); + if (base[node].has_matrix != 0) return error.AnimatedMatrix; + const components: usize = switch (channel.target_path) { + c.cgltf_animation_path_type_translation, c.cgltf_animation_path_type_scale => 3, + c.cgltf_animation_path_type_rotation => 4, + else => return error.UnsupportedAnimationTarget, + }; + const sampler = channel.sampler.*; + const multiplier: usize = switch (sampler.interpolation) { + c.cgltf_interpolation_type_linear, c.cgltf_interpolation_type_step => 1, + c.cgltf_interpolation_type_cubic_spline => 3, + else => return error.UnsupportedInterpolation, + }; + if (sampler.input.*.count == 0 or sampler.output.*.count != try std.math.mul(usize, sampler.input.*.count, multiplier)) return error.InvalidAnimation; + track.* = .{ .node = node, .path = channel.target_path, .interpolation = sampler.interpolation, .times = try a.alloc(f32, sampler.input.*.count), .values = try a.alloc([4]f32, sampler.output.*.count) }; + for (track.times, 0..) |*time, k| { + try gltf.readFloats(sampler.input, k, @as(*[1]f32, @ptrCast(time))); + if (!std.math.isFinite(time.*) or time.* < 0 or (k > 0 and time.* <= track.times[k - 1])) return error.InvalidAnimation; + } + for (track.values, 0..) |*value, k| { + value.* = .{ 0, 0, 0, 0 }; + try gltf.readFloats(sampler.output, k, value[0..components]); + try finite(value); + } + animation.duration = @max(animation.duration, track.times[track.times.len - 1]); + } + } + var scene: Scene = .{ .arena = arena, .meshes = meshes, .cameras = cameras, .nodes = nodes, .animations = animations, .base = base, .pose = try a.dupe(c.cgltf_node, base) }; + try scene.updateWorld(); + return scene; + } + + pub fn deinit(self: *Scene) void { + self.arena.deinit(); + self.* = undefined; + } + + pub fn duration(self: *const Scene, animation: usize) !f32 { + if (animation >= self.animations.len) return error.InvalidAnimationIndex; + return self.animations[animation].duration; + } + + /// Samples seconds, clamped to each track's range. Looping belongs to callers. + pub fn sample(self: *Scene, animation: usize, seconds: f32) !void { + if (animation >= self.animations.len) return error.InvalidAnimationIndex; + if (!std.math.isFinite(seconds)) return error.InvalidAnimationTime; + @memcpy(self.pose, self.base); + for (self.animations[animation].tracks) |track| { + const value = try sampleTrack(track, seconds); + const node = &self.pose[track.node]; + switch (track.path) { + c.cgltf_animation_path_type_translation => node.translation = value[0..3].*, + c.cgltf_animation_path_type_rotation => node.rotation = value, + c.cgltf_animation_path_type_scale => node.scale = value[0..3].*, + else => unreachable, + } + } + try self.updateWorld(); + } + + pub fn reset(self: *Scene) !void { + @memcpy(self.pose, self.base); + try self.updateWorld(); + } + + fn updateWorld(self: *Scene) !void { + for (self.nodes, self.pose) |*node, *pose| { + c.cgltf_node_transform_local(pose, &node.world); + var ancestor = node.parent; + var depth: usize = 0; + while (ancestor) |index| { + var parent: [16]f32 = undefined; + c.cgltf_node_transform_local(&self.pose[index], &parent); + node.world = multiply(parent, node.world); + ancestor = self.nodes[index].parent; + depth += 1; + if (depth > self.nodes.len) return error.InvalidHierarchy; + } + try finite(&node.world); + } + } +}; + +fn readPrimitive(a: std.mem.Allocator, primitive: *const c.cgltf_primitive) !Primitive { + if (primitive.type != c.cgltf_primitive_type_triangles) return error.UnsupportedPrimitive; + if (primitive.targets_count != 0) return error.UnsupportedMorphTargets; + if (primitive.has_draco_mesh_compression != 0) return error.UnsupportedCompression; + var positions: [*c]c.cgltf_accessor = null; + var normals: [*c]c.cgltf_accessor = null; + for (0..primitive.attributes_count) |i| { + const attr = primitive.attributes[i]; + if (attr.type == c.cgltf_attribute_type_position) positions = attr.data; + if (attr.type == c.cgltf_attribute_type_normal) normals = attr.data; + } + if (positions == null or positions.*.count == 0 or positions.*.count > std.math.maxInt(u32)) return error.InvalidPositions; + if (normals != null and normals.*.count != positions.*.count) return error.InvalidNormals; + var out: Primitive = .{ .vertices = try a.alloc(Vertex, positions.*.count), .indices = try a.alloc(u32, if (primitive.indices != null) primitive.indices.*.count else positions.*.count) }; + if (out.indices.len % 3 != 0) return error.InvalidTriangles; + for (out.vertices, 0..) |*vertex, i| { + try gltf.readFloats(positions, i, &vertex.position); + try finite(&vertex.position); + vertex.normal = .{ 0, 0, 0 }; + if (normals != null) { + try gltf.readFloats(normals, i, &vertex.normal); + try finite(&vertex.normal); + } + } + if (primitive.indices != null) { + const accessor = primitive.indices; + if (accessor.*.type != c.cgltf_type_scalar or (accessor.*.component_type != c.cgltf_component_type_r_8u and accessor.*.component_type != c.cgltf_component_type_r_16u and accessor.*.component_type != c.cgltf_component_type_r_32u)) return error.InvalidIndices; + // cgltf's uint reader handles sparse indices without lossy f32 conversion. + if (accessor.*.is_sparse != 0 and (c.cgltf_buffer_view_data(accessor.*.sparse.indices_buffer_view) == null or c.cgltf_buffer_view_data(accessor.*.sparse.values_buffer_view) == null)) return error.MissingBuffer; + for (out.indices, 0..) |*index, i| { + if (c.cgltf_accessor_read_uint(accessor, i, @ptrCast(index), 1) == 0) return error.MissingBuffer; + if (index.* >= out.vertices.len) return error.InvalidIndices; + } + } else { + for (out.indices, 0..) |*index, i| index.* = @intCast(i); + } + // With no authored normals, split triangles to preserve flat faces. + if (normals == null) { + const expanded = try a.alloc(Vertex, out.indices.len); + var i: usize = 0; + while (i < out.indices.len) : (i += 3) { + const p = out.vertices[out.indices[i]].position; + const q = out.vertices[out.indices[i + 1]].position; + const r = out.vertices[out.indices[i + 2]].position; + const u = [3]f32{ q[0] - p[0], q[1] - p[1], q[2] - p[2] }; + const v = [3]f32{ r[0] - p[0], r[1] - p[1], r[2] - p[2] }; + var normal = [3]f32{ u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0] }; + const length = @sqrt(normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2]); + if (length > 0) for (&normal) |*n| { + n.* /= length; + }; + for (0..3) |j| { + expanded[i + j] = .{ .position = out.vertices[out.indices[i + j]].position, .normal = normal }; + out.indices[i + j] = @intCast(i + j); + } + } + out.vertices = expanded; + } + if (primitive.material != null) { + out.color = primitive.material.*.pbr_metallic_roughness.base_color_factor; + out.double_sided = primitive.material.*.double_sided != 0; + try finite(&out.color); + } + return out; +} + +fn finite(values: []const f32) !void { + for (values) |v| if (!std.math.isFinite(v)) return error.NonFiniteValue; +} + +/// Column-major multiplication, compatible with glTF and shader mat4 values. +pub fn multiply(a: [16]f32, b: [16]f32) [16]f32 { + var out: [16]f32 = undefined; + for (0..4) |col| for (0..4) |row| { + var value: f32 = 0; + for (0..4) |k| value += a[k * 4 + row] * b[col * 4 + k]; + out[col * 4 + row] = value; + }; + return out; +} + +fn sampleTrack(track: Track, seconds: f32) ![4]f32 { + const cubic = track.interpolation == c.cgltf_interpolation_type_cubic_spline; + const stride: usize = if (cubic) 3 else 1; + const offset: usize = if (cubic) 1 else 0; + var lo: usize = 0; + var hi = track.times.len; + while (lo < hi) { + const mid = lo + (hi - lo) / 2; + if (track.times[mid] <= seconds) lo = mid + 1 else hi = mid; + } + const first = lo -| 1; + const last = @min(first + 1, track.times.len - 1); + var result = track.values[first * stride + offset]; + const rotation = track.path == c.cgltf_animation_path_type_rotation; + if (seconds > track.times[first] and first != last and track.interpolation != c.cgltf_interpolation_type_step) { + const dt = track.times[last] - track.times[first]; + const t = (seconds - track.times[first]) / dt; + var next = track.values[last * stride + offset]; + if (cubic) { + const t2 = t * t; + const t3 = t2 * t; + const a = track.values[first * stride + 2]; + const b = track.values[last * stride]; + for (&result, 0..) |*v, i| v.* = (2 * t3 - 3 * t2 + 1) * v.* + (t3 - 2 * t2 + t) * dt * a[i] + (-2 * t3 + 3 * t2) * next[i] + (t3 - t2) * dt * b[i]; + } else if (rotation) { + result = try normalizeQuaternion(result); + next = try normalizeQuaternion(next); + var dot: f32 = 0; + for (result, next) |a, b| dot += a * b; + if (dot < 0) { + dot = -dot; + for (&next) |*v| v.* = -v.*; + } + dot = std.math.clamp(dot, 0, 1); + const angle = std.math.acos(dot); + const denominator = @sin(angle); + const a = if (dot > 0.9995) 1 - t else @sin((1 - t) * angle) / denominator; + const b = if (dot > 0.9995) t else @sin(t * angle) / denominator; + for (&result, next) |*v, n| v.* = a * v.* + b * n; + } else { + for (&result, next) |*v, n| v.* += (n - v.*) * t; + } + } + try finite(&result); + return if (rotation) normalizeQuaternion(result) else result; +} + +fn normalizeQuaternion(q: [4]f32) ![4]f32 { + var length: f32 = 0; + for (q) |v| length += v * v; + if (!std.math.isFinite(length) or length < 1e-12) return error.InvalidQuaternion; + var out = q; + for (&out) |*v| v.* /= @sqrt(length); + return out; +} + +test "animation interpolation clamps, steps, slerps shortest path, and scales cubic tangents by time" { + var times = [_]f32{ 0, 2 }; + var values = [_][4]f32{ .{ 0, 0, 0, 0 }, .{ 2, 4, 6, 0 } }; + var track: Track = .{ .node = 0, .path = c.cgltf_animation_path_type_translation, .interpolation = c.cgltf_interpolation_type_linear, .times = ×, .values = &values }; + try std.testing.expectEqualSlices(f32, &.{ 1, 2, 3, 0 }, &(try sampleTrack(track, 1))); + try std.testing.expectEqualSlices(f32, &values[0], &(try sampleTrack(track, -1))); + try std.testing.expectEqualSlices(f32, &values[1], &(try sampleTrack(track, 9))); + track.interpolation = c.cgltf_interpolation_type_step; + try std.testing.expectEqualSlices(f32, &values[0], &(try sampleTrack(track, 1))); + track.path = c.cgltf_animation_path_type_rotation; + track.interpolation = c.cgltf_interpolation_type_linear; + values = .{ .{ 0, 0, 0, 1 }, .{ 0, 0, -0.70710678, -0.70710678 } }; + const q = try sampleTrack(track, 1); + try std.testing.expectApproxEqAbs(@as(f32, 0.38268343), q[2], 0.00001); + try std.testing.expectApproxEqAbs(@as(f32, 0.92387953), q[3], 0.00001); + var cubic = [_][4]f32{ .{ 0, 0, 0, 0 }, .{ 0, 0, 0, 0 }, .{ 2, 0, 0, 0 }, .{ 0, 0, 0, 0 }, .{ 0, 0, 0, 0 }, .{ 0, 0, 0, 0 } }; + track.path = c.cgltf_animation_path_type_translation; + track.interpolation = c.cgltf_interpolation_type_cubic_spline; + track.values = &cubic; + try std.testing.expectApproxEqAbs(@as(f32, 0.5), (try sampleTrack(track, 1))[0], 0.00001); +} + +test "scene copies hierarchy and restricts visibility to selected scene" { + const doc = try gltf.Document.parse( + \\{"asset":{"version":"2.0"},"scene":0,"scenes":[{"nodes":[1]}],"nodes":[{"translation":[0,2,0]},{"translation":[3,0,0],"children":[2]},{"translation":[0,4,0]}]} + ); + defer doc.deinit(); + var scene = try Scene.init(std.testing.allocator, doc); + defer scene.deinit(); + try std.testing.expect(!scene.nodes[0].visible); + try std.testing.expect(scene.nodes[2].visible); + try std.testing.expectEqual(@as(f32, 3), scene.nodes[2].world[12]); + try std.testing.expectEqual(@as(f32, 4), scene.nodes[2].world[13]); + try std.testing.expectError(error.InvalidAnimationIndex, scene.sample(0, 0)); +} + +test "indexed mesh flat normals, material and decoded animation survive document release" { + const doc = try gltf.Document.parse( + \\{"asset":{"version":"2.0"},"buffers":[{"byteLength":72,"uri":"data:application/octet-stream;base64,AAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAAAAAgD8AAAAAAgABAAAAAAAAAABAAAAAAAAAAAAAAAAAAACAQAAAAAAAAAAA"}],"bufferViews":[{"buffer":0,"byteOffset":0,"byteLength":36},{"buffer":0,"byteOffset":36,"byteLength":3},{"buffer":0,"byteOffset":40,"byteLength":8},{"buffer":0,"byteOffset":48,"byteLength":24}],"accessors":[{"bufferView":0,"componentType":5126,"count":3,"type":"VEC3","min":[0,0,0],"max":[1,1,0]},{"bufferView":1,"componentType":5121,"count":3,"type":"SCALAR"},{"bufferView":2,"componentType":5126,"count":2,"type":"SCALAR","min":[0],"max":[2]},{"bufferView":3,"componentType":5126,"count":2,"type":"VEC3"}],"materials":[{"pbrMetallicRoughness":{"baseColorFactor":[0.2,0.4,0.6,1]},"doubleSided":true}],"meshes":[{"primitives":[{"attributes":{"POSITION":0},"indices":1,"material":0}]}],"nodes":[{"translation":[0,3,0],"children":[1]},{"mesh":0}],"animations":[{"samplers":[{"input":2,"output":3}],"channels":[{"sampler":0,"target":{"node":1,"path":"translation"}}]}]} + ); + var scene = blk: { + defer doc.deinit(); + try doc.loadBuffers(null); + break :blk try Scene.init(std.testing.allocator, doc); + }; + defer scene.deinit(); + const primitive = scene.meshes[0].primitives[0]; + try std.testing.expectEqualSlices(f32, &.{ 0, 1, 0 }, &primitive.vertices[0].position); + try std.testing.expectEqualSlices(f32, &.{ 0, 0, 1 }, &primitive.vertices[0].normal); + try std.testing.expectEqualSlices(u32, &.{ 0, 1, 2 }, primitive.indices); + try std.testing.expectApproxEqAbs(@as(f32, 0.4), primitive.color[1], 0.00001); + try std.testing.expect(primitive.double_sided); + try std.testing.expectEqual(@as(f32, 2), try scene.duration(0)); + try scene.sample(0, 1); + try std.testing.expectEqual(@as(f32, 2), scene.nodes[1].world[12]); + try std.testing.expectEqual(@as(f32, 3), scene.nodes[1].world[13]); + try scene.sample(0, 0); + try std.testing.expectEqual(@as(f32, 0), scene.nodes[1].world[12]); + try std.testing.expectError(error.InvalidAnimationTime, scene.sample(0, std.math.nan(f32))); +} + +test "perspective and orthographic cameras survive document release" { + const doc = try gltf.Document.parse( + \\{"asset":{"version":"2.0"},"cameras":[{"type":"perspective","perspective":{"yfov":0.8,"znear":0.1}},{"type":"perspective","perspective":{"yfov":1,"znear":1,"zfar":100,"aspectRatio":1.5}},{"type":"orthographic","orthographic":{"xmag":8,"ymag":6,"znear":0,"zfar":50}}],"nodes":[{"camera":0},{"camera":1},{"camera":2}]} + ); + var scene = blk: { + defer doc.deinit(); + break :blk try Scene.init(std.testing.allocator, doc); + }; + defer scene.deinit(); + const infinite = scene.cameras[scene.nodes[0].camera.?].perspective; + try std.testing.expectEqual(@as(f32, 0.8), infinite.yfov); + try std.testing.expect(infinite.zfar == null and infinite.aspect_ratio == null); + const perspective = scene.cameras[scene.nodes[1].camera.?].perspective; + try std.testing.expectEqual(@as(f32, 100), perspective.zfar.?); + try std.testing.expectEqual(@as(f32, 1.5), perspective.aspect_ratio.?); + const orthographic = scene.cameras[scene.nodes[2].camera.?].orthographic; + try std.testing.expectEqual(@as(f32, 8), orthographic.xmag); + try std.testing.expectEqual(@as(f32, 6), orthographic.ymag); + try std.testing.expectEqual(@as(f32, 0), orthographic.znear); + try std.testing.expectEqual(@as(f32, 50), orthographic.zfar); +} diff --git a/src/host/gpu.zig b/src/gpu/Executor.zig index 94b2e39..412d0ed 100644 --- a/src/host/gpu.zig +++ b/src/gpu/Executor.zig @@ -6,7 +6,6 @@ const Gpu = @This(); const std = @import("std"); const c = @import("c"); const abi = @import("abi"); -const gfx = @import("gfx.zig"); pub const color_format = c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM; /// Frames an unused shader or pipeline survives. @@ -29,6 +28,7 @@ vertices: ?*c.SDL_GPUBuffer = null, vertex_capacity: u32 = 0, upload: ?*c.SDL_GPUTransferBuffer = null, upload_capacity: u32 = 0, +post_sampler: ?*c.SDL_GPUSampler = null, frame: u64 = 0, /// Set by `--profile-gpu`: every command is timed alone (see `Profile`). profile: ?Profile = null, @@ -39,14 +39,45 @@ spare: std.ArrayList(Transfer) = .empty, const Transfer = struct { buffer: *c.SDL_GPUTransferBuffer, size: u32 }; const Download = struct { key: u32, size: u32, transfer: Transfer, fence: *c.SDL_GPUFence }; -/// GPU time per labelled command. Each command starts on an idle GPU and is -/// waited for, so its time is its own: the total of a frame is what the -/// GPU spends on it, though the waiting makes the frame itself slower. +/// CPU wall time per labelled GPU command, from recording through fence wait. +/// Includes submission and synchronization overhead; not hardware GPU timestamps. pub const Profile = struct { entries: std.StringArrayHashMapUnmanaged(Entry) = .empty, arena: std.heap.ArenaAllocator, + start_frame: u64 = 0, - const Entry = struct { count: u64 = 0, total: u64 = 0, worst: u64 = 0 }; + pub const Entry = struct { + count: u64 = 0, + total: u64 = 0, + worst: u64 = 0, + samples: [2048]u64 = undefined, + + pub fn add(entry: *Entry, ns: u64) void { + entry.samples[@intCast(entry.count % entry.samples.len)] = ns; + entry.count += 1; + entry.total += ns; + entry.worst = @max(entry.worst, ns); + } + + pub fn retained(entry: *const Entry) usize { + return @intCast(@min(entry.count, entry.samples.len)); + } + + pub fn sample(entry: *const Entry, index: usize) u64 { + std.debug.assert(index < entry.retained()); + return entry.samples[@intCast((entry.count - entry.retained() + index) % entry.samples.len)]; + } + + /// Called by inspection only; the render path just writes the ring. + pub fn percentiles(entry: *const Entry) [2]u64 { + const n = entry.retained(); + if (n == 0) return .{ 0, 0 }; + var scratch: [2048]u64 = undefined; + @memcpy(scratch[0..n], entry.samples[0..n]); + std.mem.sortUnstable(u64, scratch[0..n], {}, std.sort.asc(u64)); + return .{ scratch[(n * 95 + 99) / 100 - 1], scratch[(n * 99 + 99) / 100 - 1] }; + } + }; pub fn init(gpa: std.mem.Allocator) Profile { return .{ .arena = .init(gpa) }; @@ -63,19 +94,18 @@ pub const Profile = struct { entry.key_ptr.* = a.dupe(u8, label) catch "?"; entry.value_ptr.* = .{}; } - entry.value_ptr.count += 1; - entry.value_ptr.total += ns; - entry.value_ptr.worst = @max(entry.value_ptr.worst, ns); + entry.value_ptr.add(ns); } - pub fn report(p: *Profile, frames: u64) void { + pub fn report(p: *Profile, total_frames: u64) void { + const frames = total_frames -| p.start_frame; std.debug.print("gpu time over {d} frames (ms)\n{s: <24} {s: >7} {s: >9} {s: >9} {s: >11}\n", .{ frames, "command", "count", "mean", "worst", "per frame" }); var sum: u64 = 0; var it = p.entries.iterator(); while (it.next()) |e| { - const v = e.value_ptr.*; + const v = e.value_ptr; sum += v.total; - std.debug.print("{s: <24} {d: >7} {d: >9.3} {d: >9.3} {d: >11.3}\n", .{ e.key_ptr.*, v.count, ms(v.total) / @as(f64, @floatFromInt(v.count)), ms(v.worst), ms(v.total) / @as(f64, @floatFromInt(@max(frames, 1))) }); + std.debug.print("{s: <24} {d: >7} {d: >9.3} {d: >9.3} {d: >11.3}\n", .{ e.key_ptr.*, v.count, ms(v.total) / @as(f64, @floatFromInt(@max(v.count, 1))), ms(v.worst), ms(v.total) / @as(f64, @floatFromInt(@max(frames, 1))) }); } std.debug.print("{s: <24} {s: >7} {s: >9} {s: >9} {d: >11.3}\n", .{ "all", "", "", "", ms(sum) / @as(f64, @floatFromInt(@max(frames, 1))) }); } @@ -88,10 +118,10 @@ pub const Profile = struct { /// Submits `cmd`; when profiling, waits for it and books its time from /// `start` (taken on an idle GPU, before recording) under `label`. fn submit(g: *Gpu, cmd: *c.SDL_GPUCommandBuffer, label: []const u8, start: u64) !void { - const p = &(g.profile orelse { + const p = if (g.profile) |*profile| profile else { if (!c.SDL_SubmitGPUCommandBuffer(cmd)) return error.GpuSubmit; return; - }); + }; const fence = c.SDL_SubmitGPUCommandBufferAndAcquireFence(cmd) orelse return error.GpuSubmit; defer c.SDL_ReleaseGPUFence(g.device, fence); _ = c.SDL_WaitForGPUFences(g.device, true, &fence, 1); @@ -136,7 +166,7 @@ pub fn init(gpa: std.mem.Allocator, device: *c.SDL_GPUDevice) Gpu { }; } -pub fn deinit(g: *Gpu, images: *gfx.Images) void { +pub fn deinit(g: *Gpu) void { if (g.profile) |*p| { p.report(g.frame); p.deinit(); @@ -158,9 +188,10 @@ pub fn deinit(g: *Gpu, images: *gfx.Images) void { g.spare.deinit(g.gpa); if (g.staging) |b| c.SDL_ReleaseGPUTransferBuffer(g.device, b); var targets = g.targets.iterator(); - while (targets.next()) |entry| g.releaseTarget(images, entry.key_ptr.*, entry.value_ptr.*); + while (targets.next()) |entry| g.releaseTarget(entry.value_ptr.*); if (g.vertices) |b| c.SDL_ReleaseGPUBuffer(g.device, b); if (g.upload) |b| c.SDL_ReleaseGPUTransferBuffer(g.device, b); + if (g.post_sampler) |s| c.SDL_ReleaseGPUSampler(g.device, s); g.shaders.deinit(g.gpa); g.pipelines.deinit(g.gpa); g.kernels.deinit(g.gpa); @@ -187,22 +218,63 @@ pub fn endFrame(g: *Gpu) void { } } -pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { +pub fn run(g: *Gpu, pass: abi.gpu.Pass) !void { + const start = g.startTiming(); + const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; + errdefer _ = c.SDL_CancelGPUCommandBuffer(cmd); + try g.record(cmd, pass); + try g.submit(cmd, if (pass.label.len > 0) pass.label else "pass", start); +} + +/// Records into the caller's command buffer; the caller submits it. +/// Uploads cycle their buffers, allowing several passes in one frame. +pub fn record(g: *Gpu, cmd: *c.SDL_GPUCommandBuffer, pass: abi.gpu.Pass) !void { + return g.recordWithInput(cmd, pass, null); +} + +/// A second pass on the same command buffer; input and output must differ. +pub fn postProcess(g: *Gpu, cmd: *c.SDL_GPUCommandBuffer, source: abi.Image, target: abi.Image, post: abi.gpu.PostProcess) !void { + if (source == target) return error.FeedbackLoop; + const input = g.output(source) orelse return error.MissingImage; + try g.recordWithInput(cmd, .{ + .image = target, + .width = input.width, + .height = input.height, + .pipelines = &.{.{ .vertex = post.vertex, .fragment = post.fragment, .stride = 0, .attributes = &.{}, .blend = .none }}, + .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = post.fragment_uniforms }}, + }, input.texture); +} + +fn recordWithInput(g: *Gpu, cmd: *c.SDL_GPUCommandBuffer, pass: abi.gpu.Pass, input: ?*c.SDL_GPUTexture) !void { if (pass.width == 0 or pass.height == 0 or pass.draws.len == 0 and pass.clear == null) return; + if (input != null and g.post_sampler == null) { + g.post_sampler = c.SDL_CreateGPUSampler(g.device, &std.mem.zeroInit(c.SDL_GPUSamplerCreateInfo, .{ + .min_filter = c.SDL_GPU_FILTER_NEAREST, + .mag_filter = c.SDL_GPU_FILTER_NEAREST, + .address_mode_u = c.SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, + .address_mode_v = c.SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE, + })) orelse return error.GpuSampler; + } const samples = g.supportedSamples(pass.samples); - const t = try g.renderTarget(images, pass.image, pass.width, pass.height, samples, pass.stencil); + const t = try g.renderTarget(pass.image, pass.width, pass.height, samples, pass.stencil or pass.depth); // Each pipeline is looked up on first use in the pass, not per draw: // the lookup hashes the shaders' code. Outer null: not looked up yet; // inner null: it failed (warned once). const built = try g.gpa.alloc(??*c.SDL_GPUGraphicsPipeline, pass.pipelines.len); defer g.gpa.free(built); @memset(built, null); + // Compile before recording: a refused shader leaves the caller's + // command buffer usable and the fallback picture visible. + for (pass.draws) |d| { + if (d.pipeline >= pass.pipelines.len) return error.BadPipeline; + const p = pass.pipelines[d.pipeline]; + if ((p.depth_test != null or p.depth_write) and !pass.depth) return error.MissingDepthAttachment; + if (built[d.pipeline] == null) built[d.pipeline] = try g.pipeline(pass.pipelines[d.pipeline], samples, pass.stencil or pass.depth, @intFromBool(input != null)); + } - const start = g.startTiming(); // Every draw's vertices go up in one copy. var total: u32 = 0; for (pass.draws) |d| total += @intCast(std.mem.alignForward(usize, d.vertices.len, 4)); - const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; if (total > 0) { try g.reserve(total); const mapped: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, g.upload, true) orelse return error.GpuMap); @@ -238,15 +310,17 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { } t.fresh = false; var depth: c.SDL_GPUDepthStencilTargetInfo = std.mem.zeroes(c.SDL_GPUDepthStencilTargetInfo); - if (pass.stencil) { + if (pass.stencil or pass.depth) { depth.texture = t.stencil; - depth.load_op = c.SDL_GPU_LOADOP_DONT_CARE; + depth.load_op = if (pass.depth) c.SDL_GPU_LOADOP_CLEAR else c.SDL_GPU_LOADOP_DONT_CARE; + depth.clear_depth = 1; depth.store_op = c.SDL_GPU_STOREOP_DONT_CARE; depth.stencil_load_op = c.SDL_GPU_LOADOP_CLEAR; depth.stencil_store_op = c.SDL_GPU_STOREOP_DONT_CARE; depth.clear_stencil = 0; } - const rp = c.SDL_BeginGPURenderPass(cmd, &color, 1, if (pass.stencil) &depth else null) orelse return error.GpuRenderPass; + const rp = c.SDL_BeginGPURenderPass(cmd, &color, 1, if (pass.stencil or pass.depth) &depth else null) orelse return error.GpuRenderPass; + if (input) |texture_| c.SDL_BindGPUFragmentSamplers(rp, 0, &.{ .texture = texture_, .sampler = g.post_sampler.? }, 1); var bound: ?*c.SDL_GPUGraphicsPipeline = null; // State already set in this pass, so draws that repeat it skip the call. var vertex_uniforms: ?[]const u8 = null; @@ -258,10 +332,6 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { defer offset += @intCast(std.mem.alignForward(usize, d.vertices.len, 4)); if (d.pipeline >= pass.pipelines.len or d.vertex_count == 0) continue; const slot = &built[d.pipeline]; - if (slot.* == null) slot.* = g.pipeline(pass.pipelines[d.pipeline], samples, pass.stencil) catch |err| blk: { - std.log.warn("gpu pipeline {d}: {t} ({s})", .{ d.pipeline, err, c.SDL_GetError() }); - break :blk @as(?*c.SDL_GPUGraphicsPipeline, null); - }; const chosen = slot.*.? orelse continue; if (bound != chosen) { c.SDL_BindGPUGraphicsPipeline(rp, chosen); @@ -303,7 +373,6 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { c.SDL_DrawGPUPrimitives(rp, d.vertex_count, 1, first, 0); } c.SDL_EndGPURenderPass(rp); - try g.submit(cmd, if (pass.label.len > 0) pass.label else "pass", start); } /// Runs one dispatch in a compute pass of its own. @@ -486,17 +555,16 @@ fn sampleCount(s: u8) c.SDL_GPUSampleCount { } /// The image's render target, (re)created to fit the pass. -fn renderTarget(g: *Gpu, images: *gfx.Images, image: abi.Image, w: u32, h: u32, samples: u8, stencil: bool) !*Target { +fn renderTarget(g: *Gpu, image: abi.Image, w: u32, h: u32, samples: u8, stencil: bool) !*Target { var entry = try g.targets.getOrPut(g.gpa, image); if (entry.found_existing and (entry.value_ptr.width != w or entry.value_ptr.height != h)) { - g.releaseTarget(images, image, entry.value_ptr.*); + g.releaseTarget(entry.value_ptr.*); entry.found_existing = false; } if (!entry.found_existing) { errdefer _ = g.targets.remove(image); const resolved = try g.texture(w, h, color_format, c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | c.SDL_GPU_TEXTUREUSAGE_SAMPLER, 1); errdefer c.SDL_ReleaseGPUTexture(g.device, resolved); - try images.wrap(image, resolved, w, h); entry.value_ptr.* = .{ .width = w, .height = h, .texture = resolved }; } const t = entry.value_ptr; @@ -514,17 +582,16 @@ fn renderTarget(g: *Gpu, images: *gfx.Images, image: abi.Image, w: u32, h: u32, return t; } -fn releaseTarget(g: *Gpu, images: *gfx.Images, image: abi.Image, t: Target) void { - images.unwrap(image); +fn releaseTarget(g: *Gpu, t: Target) void { c.SDL_ReleaseGPUTexture(g.device, t.texture); if (t.msaa) |m| c.SDL_ReleaseGPUTexture(g.device, m); if (t.stencil) |s| c.SDL_ReleaseGPUTexture(g.device, s); } /// Forgets an image's target, when the image itself is freed. -pub fn forget(g: *Gpu, images: *gfx.Images, image: abi.Image) void { +pub fn forget(g: *Gpu, image: abi.Image) void { const t = g.targets.fetchRemove(image) orelse return; - g.releaseTarget(images, image, t.value); + g.releaseTarget(t.value); } fn texture(g: *Gpu, w: u32, h: u32, format: c.SDL_GPUTextureFormat, usage: c.SDL_GPUTextureUsageFlags, samples: u8) !*c.SDL_GPUTexture { @@ -555,11 +622,12 @@ fn reserve(g: *Gpu, bytes: u32) !void { } } -fn shader(g: *Gpu, s: abi.gpu.Shader, stage: c.SDL_GPUShaderStage) !struct { *c.SDL_GPUShader, u64 } { +fn shader(g: *Gpu, s: abi.gpu.Shader, stage: c.SDL_GPUShaderStage, samplers: u32) !struct { *c.SDL_GPUShader, u64 } { var h = std.hash.Wyhash.init(stage); h.update(s.code); h.update(std.mem.asBytes(&s.uniform_buffers)); h.update(std.mem.asBytes(&s.storage_buffers)); + h.update(std.mem.asBytes(&samplers)); const key = h.final(); const entry = try g.shaders.getOrPut(g.gpa, key); if (!entry.found_existing) { @@ -570,7 +638,7 @@ fn shader(g: *Gpu, s: abi.gpu.Shader, stage: c.SDL_GPUShaderStage) !struct { *c. .entrypoint = "main", .format = c.SDL_GPU_SHADERFORMAT_SPIRV, .stage = stage, - .num_samplers = 0, + .num_samplers = samplers, .num_storage_textures = 0, .num_storage_buffers = s.storage_buffers, .num_uniform_buffers = s.uniform_buffers, @@ -581,12 +649,12 @@ fn shader(g: *Gpu, s: abi.gpu.Shader, stage: c.SDL_GPUShaderStage) !struct { *c. return .{ entry.value_ptr.object, key }; } -fn pipeline(g: *Gpu, p: abi.gpu.Pipeline, samples: u8, stencil: bool) !*c.SDL_GPUGraphicsPipeline { - const vertex, const vertex_key = try g.shader(p.vertex, c.SDL_GPU_SHADERSTAGE_VERTEX); - const fragment, const fragment_key = try g.shader(p.fragment, c.SDL_GPU_SHADERSTAGE_FRAGMENT); +fn pipeline(g: *Gpu, p: abi.gpu.Pipeline, samples: u8, stencil: bool, samplers: u32) !*c.SDL_GPUGraphicsPipeline { + const vertex, const vertex_key = try g.shader(p.vertex, c.SDL_GPU_SHADERSTAGE_VERTEX, 0); + const fragment, const fragment_key = try g.shader(p.fragment, c.SDL_GPU_SHADERSTAGE_FRAGMENT, samplers); // Field by field: no padding bytes in the key. var h = std.hash.Wyhash.init(vertex_key ^ (fragment_key *% 31)); - inline for (.{ p.stride, @backingInt(p.primitive), @backingInt(p.blend), @intFromBool(p.write_color), samples, @intFromBool(stencil) }) |v| h.update(std.mem.asBytes(&@as(u32, v))); + inline for (.{ p.stride, @backingInt(p.primitive), @backingInt(p.blend), @intFromBool(p.write_color), @intFromBool(p.depth_write), if (p.depth_test) |compare| @as(u32, @backingInt(compare)) + 1 else @as(u32, 0), samples, @intFromBool(stencil) }) |v| h.update(std.mem.asBytes(&@as(u32, v))); for (p.attributes) |a| inline for (.{ a.location, @backingInt(a.format), a.offset }) |v| h.update(std.mem.asBytes(&@as(u32, v))); if (p.stencil) |st| inline for (.{ st.front, st.back }) |face| inline for (.{ @backingInt(face.compare), @backingInt(face.pass), @backingInt(face.fail) }) |v| h.update(std.mem.asBytes(&@as(u32, v))); const key = h.final(); @@ -620,6 +688,9 @@ fn pipeline(g: *Gpu, p: abi.gpu.Pipeline, samples: u8, stencil: bool) !*c.SDL_GP blend.color_write_mask = if (p.write_color) c.SDL_GPU_COLORCOMPONENT_R | c.SDL_GPU_COLORCOMPONENT_G | c.SDL_GPU_COLORCOMPONENT_B | c.SDL_GPU_COLORCOMPONENT_A else 0; const target_description: c.SDL_GPUColorTargetDescription = .{ .format = color_format, .blend_state = blend }; var depth_stencil: c.SDL_GPUDepthStencilState = std.mem.zeroes(c.SDL_GPUDepthStencilState); + depth_stencil.enable_depth_test = p.depth_test != null or p.depth_write; + depth_stencil.enable_depth_write = p.depth_write; + depth_stencil.compare_op = @as(u32, @backingInt(p.depth_test orelse .always)) + 1; if (p.stencil) |st| { depth_stencil.enable_stencil_test = true; depth_stencil.compare_mask = 0xff; @@ -667,3 +738,9 @@ fn stencilFace(face: abi.gpu.StencilFace) c.SDL_GPUStencilOpState { .depth_fail_op = c.SDL_GPU_STENCILOP_KEEP, }; } + +/// Borrowed until resize, forget, or deinit. The executor owns the texture. +pub fn output(g: *Gpu, image: abi.Image) ?struct { texture: *c.SDL_GPUTexture, width: u32, height: u32 } { + const t = g.targets.get(image) orelse return null; + return .{ .texture = t.texture, .width = t.width, .height = t.height }; +} diff --git a/src/gpu/scene.zig b/src/gpu/scene.zig new file mode 100644 index 0000000..e5ff3d0 --- /dev/null +++ b/src/gpu/scene.zig @@ -0,0 +1,263 @@ +//! Optional, self-contained GPU frames for embedders and remote views. +//! Version 1: "SGPU", u32 version, then Pass fields in declaration order. +//! Version 2 appends one fullscreen PostProcess sampling that pass's output. +//! Version 3 adds depth fields and an optional PostProcess after the pass. +//! Integers/enums/f32 are little endian; slices have a u32 count, optionals +//! and bools a byte (0 or 1). No pointers, native padding, or device handles. +//! Storage buffers are not part of this version: each frame stands alone. +const std = @import("std"); +pub const abi = @import("abi"); +pub const max_bytes = 4 << 20; +pub const Frame = struct { pass: abi.gpu.Pass, post: ?abi.gpu.PostProcess = null }; + +pub fn encode(w: *std.Io.Writer, pass: abi.gpu.Pass) !void { + try validate(pass); + try w.writeAll("SGPU"); + const version: u32 = if (pass.depth) 3 else 1; + try w.writeInt(u32, version, .little); + try put(w, pass, version); + if (version == 3) try put(w, @as(?abi.gpu.PostProcess, null), version); +} + +pub fn encodePostProcess(w: *std.Io.Writer, pass: abi.gpu.Pass, post: abi.gpu.PostProcess) !void { + try validate(pass); + try validatePostProcess(post); + try w.writeAll("SGPU"); + const version: u32 = if (pass.depth) 3 else 2; + try w.writeInt(u32, version, .little); + try put(w, pass, version); + if (version == 3) try put(w, @as(?abi.gpu.PostProcess, post), version) else try put(w, post, version); +} + +/// Allocations belong to the caller's frame arena. Never run unvalidated +/// wire data through a driver; this checks lengths, layouts and indices. +pub fn decode(arena: std.mem.Allocator, bytes: []const u8) !abi.gpu.Pass { + const frame = try decodeFrame(arena, bytes); + if (frame.post != null) return error.BadScene; + return frame.pass; +} + +pub fn decodeFrame(arena: std.mem.Allocator, bytes: []const u8) !Frame { + if (bytes.len > max_bytes) return error.SceneTooLarge; + var r: std.Io.Reader = .fixed(bytes); + if (!std.mem.eql(u8, try r.take(4), "SGPU")) return error.BadScene; + const version = try r.takeInt(u32, .little); + if (version < 1 or version > 3) return error.BadScene; + var budget: usize = max_bytes * 4; + const pass = try get(abi.gpu.Pass, arena, &r, &budget, version); + const post: ?abi.gpu.PostProcess = if (version == 3) try get(?abi.gpu.PostProcess, arena, &r, &budget, version) else if (version == 2) try get(abi.gpu.PostProcess, arena, &r, &budget, version) else null; + if (r.end != r.seek) return error.BadScene; + try validate(pass); + if (post) |p| try validatePostProcess(p); + return .{ .pass = pass, .post = post }; +} + +fn depthField(comptime T: type, comptime name: []const u8) bool { + return (T == abi.gpu.Pass and std.mem.eql(u8, name, "depth")) or + (T == abi.gpu.Pipeline and (std.mem.eql(u8, name, "depth_test") or std.mem.eql(u8, name, "depth_write"))); +} + +fn put(w: *std.Io.Writer, value: anytype, version: u32) anyerror!void { + const T = @TypeOf(value); + switch (@typeInfo(T)) { + .int => try w.writeInt(T, value, .little), + .float => try w.writeInt(u32, @bitCast(value), .little), + .bool => try w.writeByte(@intFromBool(value)), + .@"enum" => try w.writeInt(u32, @intCast(@backingInt(value)), .little), + .optional => { + try w.writeByte(@intFromBool(value != null)); + if (value) |v| try put(w, v, version); + }, + .array => for (value) |v| try put(w, v, version), + .pointer => { + try w.writeInt(u32, @intCast(value.len), .little); + for (value) |v| try put(w, v, version); + }, + .@"struct" => |s| inline for (s.field_names) |name| { + if (version >= 3 or !depthField(T, name)) try put(w, @field(value, name), version); + }, + else => @compileError("unsupported scene field"), + } +} + +fn get(comptime T: type, a: std.mem.Allocator, r: *std.Io.Reader, budget: *usize, version: u32) anyerror!T { + switch (@typeInfo(T)) { + .int => return r.takeInt(T, .little), + .float => return @bitCast(try r.takeInt(u32, .little)), + .bool => return switch (try r.takeByte()) { + 0 => false, + 1 => true, + else => error.BadScene, + }, + .@"enum" => |e| { + const n = try r.takeInt(u32, .little); + inline for (e.field_names, e.field_values) |name, value| if (n == value) return @field(T, name); + return error.BadScene; + }, + .optional => |o| return switch (try r.takeByte()) { + 0 => null, + 1 => try get(o.child, a, r, budget, version), + else => error.BadScene, + }, + .array => |ar| { + var out: T = undefined; + for (&out) |*v| v.* = try get(ar.child, a, r, budget, version); + return out; + }, + .pointer => |p| { + const n = try r.takeInt(u32, .little); + const size = std.math.mul(usize, n, @sizeOf(p.child)) catch return error.SceneTooLarge; + if (size > budget.* or n > max_bytes) return error.SceneTooLarge; + budget.* -= size; + // SPIR-V must be word aligned, including after a wire decode. + const out = try a.alignedAlloc(p.child, .fromByteUnits(@max(4, @alignOf(p.child))), n); + for (out) |*v| v.* = try get(p.child, a, r, budget, version); + return out; + }, + .@"struct" => |s| { + var out: T = undefined; + inline for (s.field_names, s.field_types) |name, F| { + @field(out, name) = if (version < 3 and comptime depthField(T, name)) std.mem.zeroes(F) else try get(F, a, r, budget, version); + } + return out; + }, + else => @compileError("unsupported scene field"), + } +} + +fn shader(s: abi.gpu.Shader) !void { + if (s.code.len < 20 or s.code.len % 4 != 0 or std.mem.readInt(u32, s.code[0..4], .little) != 0x07230203 or s.uniform_buffers > 1 or s.storage_buffers != 0) return error.BadShader; +} + +pub fn validatePostProcess(p: abi.gpu.PostProcess) !void { + try shader(p.vertex); + try shader(p.fragment); + if (p.vertex.uniform_buffers != 0 or p.fragment_uniforms.len > 16384 or + (p.fragment_uniforms.len > 0) != (p.fragment.uniform_buffers == 1)) return error.BadScene; +} + +pub fn validate(p: abi.gpu.Pass) !void { + if (p.image != .none or p.width == 0 or p.height == 0 or p.width > 4096 or p.height > 4096 or p.pipelines.len > 64 or p.draws.len > 4096 or (p.samples != 1 and p.samples != 2 and p.samples != 4 and p.samples != 8)) return error.BadScene; + // Self-contained frames must not depend on an earlier view's contents. + const clear = p.clear orelse return error.BadScene; + for (clear) |v| if (!std.math.isFinite(v)) return error.BadScene; + for (p.pipelines) |pipe| { + if ((pipe.depth_test != null or pipe.depth_write) and !p.depth) return error.BadScene; + try shader(pipe.vertex); + try shader(pipe.fragment); + if (pipe.attributes.len > 16 or pipe.stride > 4096) return error.BadScene; + var locations: u32 = 0; + for (pipe.attributes) |attr| { + const size: u32 = switch (attr.format) { + .float, .ubyte4_norm => 4, + .float2 => 8, + .float3 => 12, + .float4 => 16, + }; + if (attr.location >= 16 or attr.offset > pipe.stride or size > pipe.stride - attr.offset) return error.BadScene; + const bit = @as(u32, 1) << @intCast(attr.location); + if (locations & bit != 0) return error.BadScene; + locations |= bit; + } + } + var total: usize = 0; + for (p.draws) |d| { + if (d.pipeline >= p.pipelines.len or d.vertex_count > 1 << 20 or d.vertex_storage.len != 0 or d.fragment_storage.len != 0) return error.BadScene; + const pipe = p.pipelines[d.pipeline]; + if (pipe.attributes.len != 0 and @as(u64, pipe.stride) * d.vertex_count > d.vertices.len) return error.BadScene; + if (d.vertex_uniforms.len > 16384 or d.fragment_uniforms.len > 16384 or (d.vertex_uniforms.len > 0) != (pipe.vertex.uniform_buffers == 1) or (d.fragment_uniforms.len > 0) != (pipe.fragment.uniform_buffers == 1)) return error.BadScene; + total += d.vertices.len; + if (total > max_bytes) return error.SceneTooLarge; + if (d.scissor) |r| inline for (.{ r.x, r.y, r.w, r.h }) |v| if (!std.math.isFinite(v) or v < 0 or v > 4096) return error.BadScene; + } +} + +test "GPU scene roundtrip, truncated and oversized inputs are refused" { + const shader_bytes = std.mem.asBytes(&[_]u32{ 0x07230203, 0, 0, 0, 0 }); + const pipelines = [_]abi.gpu.Pipeline{.{ .vertex = .{ .code = shader_bytes }, .fragment = .{ .code = shader_bytes }, .stride = 0, .attributes = &.{} }}; + const pass: abi.gpu.Pass = .{ .image = .none, .width = 32, .height = 24, .pipelines = &pipelines, .draws = &.{.{ .pipeline = 0, .vertex_count = 3 }} }; + var out: std.Io.Writer.Allocating = .init(std.testing.allocator); + defer out.deinit(); + try encode(&out.writer, pass); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const decoded = try decode(arena.allocator(), out.written()); + try std.testing.expectEqual(32, decoded.width); + try std.testing.expectEqual(3, decoded.draws[0].vertex_count); + for (0..out.written().len) |i| { + _ = decode(arena.allocator(), out.written()[0..i]) catch continue; + return error.AcceptedTruncatedScene; + } + var bad = pass; + bad.width = 4097; + try std.testing.expectError(error.BadScene, validate(bad)); + var draw = pass.draws[0]; + draw.pipeline = 1; + bad = pass; + bad.draws = &.{draw}; + try std.testing.expectError(error.BadScene, validate(bad)); +} + +test "GPU postprocess keeps v1 readable and validates the complete second pass" { + const code = std.mem.asBytes(&[_]u32{ 0x07230203, 0, 0, 0, 0 }); + const pipelines = [_]abi.gpu.Pipeline{.{ .vertex = .{ .code = code }, .fragment = .{ .code = code }, .stride = 0, .attributes = &.{} }}; + const pass: abi.gpu.Pass = .{ .image = .none, .width = 32, .height = 24, .pipelines = &pipelines, .draws = &.{.{ .pipeline = 0, .vertex_count = 3 }} }; + const post: abi.gpu.PostProcess = .{ .vertex = .{ .code = code }, .fragment = .{ .code = code } }; + var out: std.Io.Writer.Allocating = .init(std.testing.allocator); + defer out.deinit(); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + try encode(&out.writer, pass); + try std.testing.expect((try decodeFrame(arena.allocator(), out.written())).post == null); + out.clearRetainingCapacity(); + try encodePostProcess(&out.writer, pass, post); + const frame = try decodeFrame(arena.allocator(), out.written()); + try std.testing.expectEqual(32, frame.pass.width); + try std.testing.expectEqualSlices(u8, code, frame.post.?.fragment.code); + for (0..out.written().len) |i| { + _ = decodeFrame(arena.allocator(), out.written()[0..i]) catch continue; + return error.AcceptedTruncatedScene; + } + var bad = post; + bad.vertex.uniform_buffers = 1; + try std.testing.expectError(error.BadScene, validatePostProcess(bad)); + bad = post; + bad.fragment.uniform_buffers = 1; + try std.testing.expectError(error.BadScene, validatePostProcess(bad)); + bad = post; + bad.fragment.storage_buffers = 1; + try std.testing.expectError(error.BadShader, validatePostProcess(bad)); + out.written()[4] = 4; + try std.testing.expectError(error.BadScene, decodeFrame(arena.allocator(), out.written())); +} + +test "depth uses v3 and requires an attachment; legacy frames keep depth disabled" { + const code = std.mem.asBytes(&[_]u32{ 0x07230203, 0, 0, 0, 0 }); + var pipelines = [_]abi.gpu.Pipeline{.{ .vertex = .{ .code = code }, .fragment = .{ .code = code }, .stride = 0, .attributes = &.{}, .depth_test = .less, .depth_write = true }}; + var pass: abi.gpu.Pass = .{ .image = .none, .width = 32, .height = 24, .pipelines = &pipelines, .draws = &.{.{ .pipeline = 0, .vertex_count = 3 }} }; + try std.testing.expectError(error.BadScene, validate(pass)); + pass.depth = true; + var out: std.Io.Writer.Allocating = .init(std.testing.allocator); + defer out.deinit(); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + try encode(&out.writer, pass); + try std.testing.expectEqual(3, out.written()[4]); + const decoded = try decode(arena.allocator(), out.written()); + try std.testing.expect(decoded.depth); + try std.testing.expectEqual(abi.gpu.Compare.less, decoded.pipelines[0].depth_test.?); + try std.testing.expect(decoded.pipelines[0].depth_write); + const post: abi.gpu.PostProcess = .{ .vertex = .{ .code = code }, .fragment = .{ .code = code } }; + out.clearRetainingCapacity(); + try encodePostProcess(&out.writer, pass, post); + const frame = try decodeFrame(arena.allocator(), out.written()); + try std.testing.expect(frame.post != null and frame.pass.depth); + pass.depth = false; + pipelines[0].depth_test = null; + pipelines[0].depth_write = false; + out.clearRetainingCapacity(); + try encode(&out.writer, pass); + try std.testing.expectEqual(1, out.written()[4]); + const legacy = try decode(arena.allocator(), out.written()); + try std.testing.expect(!legacy.depth and legacy.pipelines[0].depth_test == null and !legacy.pipelines[0].depth_write); +} diff --git a/src/host/Host.zig b/src/host/Host.zig index 008d143..02a88c7 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -12,15 +12,18 @@ const Timeline = playback.Timeline; const mapRecordings = playback.mapRecordings; const gfx = @import("gfx.zig"); const sound = @import("audio.zig"); -const Gpu = @import("gpu.zig"); +const Gpu = @import("gpu"); const overlay = @import("overlay.zig"); const Console = @import("Console.zig"); +const Control = @import("control.zig").Control; const Library = @import("library.zig").Library; const Watch = @import("library.zig").Watch; pub const Options = struct { /// A hot-reloaded sketch library. Unused when a sketch is linked in. library: ?[]const u8 = null, + /// Local 9P control socket, opt-in. + ninep: ?[]const u8 = null, /// State file; recordings go next to it with an `.inputs` suffix. state: []const u8 = "sketch.state", /// Offscreen video, silent audio, no vsync: for scripts and CI. @@ -59,6 +62,8 @@ painter: gfx.Painter, audio: sound.Audio, state: memory.StateFile, session: memory.Session, +/// A failed resource setup disables app callbacks while keeping host controls alive. +session_ready: bool = false, store: memory.StoreFile, /// Images and buffers the sketch made (`newImage`, `decodeImage`, /// `newBuffer`): Session holds their handles, so they go when it resets. @@ -98,6 +103,7 @@ screenshot: ?[:0]const u8 = null, /// Frames exported so far (`Options.export_to`). exported: u64 = 0, dialog: Dialog = .{}, +control: ?*Control = null, pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std.process.Environ.Map, options: Options, linked: ?abi.Sketch) !void { if (options.headless) { @@ -200,12 +206,13 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std } pub fn deinit(self: *Host) void { + if (self.control) |control| control.deinit(); self.dialog.deinit(); if (self.watch) |w| w.deinit(); self.audio.deinit(); - if (self.passes) |*g| g.deinit(&self.images); self.painter.deinit(); self.images.deinit(); + if (self.passes) |*g| g.deinit(); if (self.pointer_cursor) |cursor| c.SDL_DestroyCursor(cursor); if (self.offscreen) |texture| c.SDL_DestroyTexture(texture); c.SDL_DestroyRenderer(self.renderer); @@ -233,7 +240,9 @@ pub fn deinit(self: *Host) void { /// The interactive loop. An export runs it on a clock of its own, frame k /// at k/fps seconds (from 1 s, as scripts do), whatever the wall clock says. pub fn run(self: *Host) !void { + if (self.options.ninep) |path| self.control = try Control.init(self.gpa, path); while (!self.quit) { + if (self.control) |control| control.poll(self); try self.poll(); try self.frame(if (self.options.export_to == null) c.SDL_GetTicksNS() else std.time.ns_per_s + self.exported * std.time.ns_per_s / self.options.fps); } @@ -278,6 +287,7 @@ pub fn frame(self: *Host, now: u64) !void { /// Feeds whole frames to the sketch's deterministic `update`, one call per /// frame (events up to and including a tick), live or replayed alike. pub fn update(self: *Host, events: []const abi.Event) void { + if (!self.session_ready) return; var frames = playback.frames(events); while (frames.next()) |one| { // Input follows the events actually fed, frame by frame. @@ -353,7 +363,7 @@ fn draw(self: *Host, now: u64) !void { self.input.downloads = if (self.passes) |*g| try g.collect(arena) else &.{}; self.input.frame += 1; const ctx = self.context(); - self.sketch.draw(&ctx, self.live().ptr); + if (self.session_ready) self.sketch.draw(&ctx, self.live().ptr); _ = c.SDL_SetRenderTarget(self.renderer, self.offscreen); _ = c.SDL_SetRenderClipRect(self.renderer, null); @@ -375,7 +385,7 @@ fn draw(self: *Host, now: u64) !void { .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() }), + .gpu => |pass| if (self.passes) |*g| self.runGpu(g, 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(), @@ -400,13 +410,14 @@ fn draw(self: *Host, now: u64) !void { self.painter.end(); self.applyCursor(); - if (self.screenshot != null or self.options.export_to != null) { + if (self.screenshot != null or self.options.export_to != null or (self.control != null and self.control.?.capture_pending)) { const surface = c.SDL_RenderReadPixels(self.renderer, null) orelse return sdlError("SDL_RenderReadPixels"); defer c.SDL_DestroySurface(surface); if (self.screenshot) |path| { self.screenshot = null; if (!c.SDL_SavePNG(surface, path.ptr)) return sdlError("SDL_SavePNG"); } + if (self.control) |control| if (control.capture_pending) control.capture(surface); if (self.options.export_to) |to| try self.exportFrame(surface, to); } if (!self.options.headless) if (self.offscreen) |canvas| { @@ -573,6 +584,10 @@ fn movesState(line: []const u8) bool { /// Offers a line to the sketch. pub fn forward(self: *Host, _: *Console, line: []const u8, now: u64) abi.Outcome { + if (!self.session_ready) { + self.print("session setup failed; restart after fixing the resource error", .{}); + return .failed; + } self.input.now = now; const ctx = self.context(); return self.sketch.command(&ctx, self.live().ptr, &line); @@ -626,6 +641,11 @@ fn adopt(self: *Host, sketch: abi.Sketch) !void { .migrated => |m| self.print("state layout changed: kept {d} fields, reset {d} to defaults", .{ m.kept, m.reset }), } if (!self.state.used(0)) self.resetState(); + if (session_reset) { + const ctx = self.context(); + self.session_ready = self.sketch.setup(&ctx); + if (!self.session_ready) self.console.visible = true; + } if (session_reset and self.reloads > 0) self.print("session layout changed: session reset", .{}); switch (stored) { .unchanged, .created => {}, @@ -636,7 +656,8 @@ fn adopt(self: *Host, sketch: abi.Sketch) !void { /// Frees what the old Session made: its handles are gone with it. fn releaseMade(self: *Host) void { for (self.made.images.items) |image| { - if (self.passes) |*g| g.forget(&self.images, image); + self.images.unwrap(image); + if (self.passes) |*g| g.forget(image); self.images.free(image); } if (self.passes) |*g| for (self.made.buffers.items) |buffer| g.freeBuffer(buffer); @@ -800,6 +821,7 @@ const api: abi.Host.VTable = .{ .gpu = apiGpu, .newBuffer = apiNewBuffer, .freeBuffer = apiFreeBuffer, + .writeBuffer = apiWriteBuffer, .imageSize = apiImageSize, .textSize = apiTextSize, .loadSound = apiLoadSound, @@ -830,7 +852,8 @@ fn apiNewImage(ctx: *anyopaque, w: u32, h: u32) abi.Image { fn apiFreeImage(ctx: *anyopaque, image: abi.Image) void { const self = hostOf(ctx); forget(&self.made.images, image); - if (self.passes) |*g| g.forget(&self.images, image); + self.images.unwrap(image); + if (self.passes) |*g| g.forget(image); self.images.free(image); } fn apiGpu(ctx: *anyopaque) bool { @@ -838,12 +861,17 @@ fn apiGpu(ctx: *anyopaque) bool { } fn apiNewBuffer(ctx: *anyopaque, size: u32) abi.Buffer { const self = hostOf(ctx); - const g = &(self.passes orelse return .none); + const g = if (self.passes) |*passes| passes else return .none; return self.remember(.buffers, g.newBuffer(size) catch |err| blk: { self.print("gpu buffer: {t} ({s})", .{ err, c.SDL_GetError() }); break :blk .none; }); } +fn apiWriteBuffer(ctx: *anyopaque, buffer: abi.Buffer, offset: u32, bytes: []const u8) !void { + const self = hostOf(ctx); + const gpu = if (self.passes) |*passes| passes else return error.GpuUnavailable; + try gpu.write(buffer, offset, bytes); +} fn apiFreeBuffer(ctx: *anyopaque, buffer: abi.Buffer) void { const self = hostOf(ctx); forget(&self.made.buffers, buffer); @@ -879,3 +907,136 @@ fn apiClock(_: *anyopaque) u64 { fn apiEnv(ctx: *anyopaque, name: []const u8) ?[]const u8 { return hostOf(ctx).environ.get(name); } + +fn runGpu(self: *Host, g: *Gpu, pass: abi.gpu.Pass) !void { + const before = g.output(pass.image); + const changed = if (before) |t| t.width != pass.width or t.height != pass.height else true; + if (changed) self.images.unwrap(pass.image); + try g.run(pass); + if (g.output(pass.image)) |t| try self.images.wrap(pass.image, t.texture, t.width, t.height); +} + +test "session setup runs for restored state and layout reset, not same-layout reload or state reset" { + const Fixture = struct { + const State = extern struct { value: u32 = 1 }; + const Session1 = extern struct { ready: u32 = 0 }; + const Session2 = extern struct { ready: u32 = 0, extra: u32 = 0 }; + const Store = struct {}; + var setup_calls: usize = 0; + var init_calls: usize = 0; + var fail_setup = false; + const description: abi.Info = .{ .abi = abi.hash, .name = "setup-test", .width = 1, .height = 1, .state = abi.layout.of(State, .state), .session = abi.layout.of(Session1, .session), .store = abi.layout.of(Store, .store) }; + const changed: abi.Info = blk: { + var value = description; + value.session = abi.layout.of(Session2, .session); + break :blk value; + }; + fn info() callconv(.c) *const abi.Info { + return &description; + } + fn info2() callconv(.c) *const abi.Info { + return &changed; + } + fn defaults(kind: abi.Memory, ptr: [*]u8) callconv(.c) void { + switch (kind) { + .state => @as(*State, @ptrCast(@alignCast(ptr))).* = .{}, + .session => @as(*Session1, @ptrCast(@alignCast(ptr))).* = .{}, + .store => {}, + } + } + fn defaults2(kind: abi.Memory, ptr: [*]u8) callconv(.c) void { + if (kind == .session) @as(*Session2, @ptrCast(@alignCast(ptr))).* = .{} else defaults(kind, ptr); + } + fn init(_: *const abi.Context, _: [*]u8) callconv(.c) void { + init_calls += 1; + } + fn setup(ctx: *const abi.Context) callconv(.c) bool { + setup_calls += 1; + @as(*u32, @ptrCast(@alignCast(ctx.session_root))).* += 1; + return !fail_setup; + } + fn update(_: *const abi.Context, _: [*]u8) callconv(.c) void {} + fn draw(_: *const abi.Context, _: [*]const u8) callconv(.c) void {} + fn command(_: *const abi.Context, _: [*]u8, _: *const []const u8) callconv(.c) abi.Outcome { + return .unknown; + } + const sketch: abi.Sketch = .{ .info = info, .defaults = defaults, .init = @This().init, .setup = setup, .update = @This().update, .draw = @This().draw, .command = command }; + }; + Fixture.setup_calls = 0; + Fixture.init_calls = 0; + Fixture.fail_setup = false; + const a = std.testing.allocator; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var state_buf: [std.fs.max_path_bytes]u8 = undefined; + var store_buf: [std.fs.max_path_bytes]u8 = undefined; + const state_path = try std.mem.print(&state_buf, ".zig-cache/tmp/{s}/setup.state", .{&tmp.sub_path}); + const store_path = try std.mem.print(&store_buf, ".zig-cache/tmp/{s}/setup.store", .{&tmp.sub_path}); + { + var state = try memory.StateFile.open(a, state_path); + defer state.close(); + _ = try state.adopt(a, Fixture.description.state, Fixture.defaults); + @as(*Fixture.State, @ptrCast(state.slot(0).ptr)).value = 77; + state.markUsed(0, true); + } + // Exercise adoption with real persisted memory, without opening SDL devices. + var host: Host = undefined; + host.gpa = a; + host.state = try memory.StateFile.open(a, state_path); + defer host.state.close(); + host.store = try memory.StoreFile.open(a, store_path, false); + defer host.store.close(); + host.session = .init(a); + defer host.session.deinit(a); + host.info_arena = .init(a); + defer host.info_arena.deinit(); + host.frame_arena = .init(a); + defer host.frame_arena.deinit(); + host.options = .{ .headless = true }; + host.console = .{}; + host.reloads = 0; + host.made = .{}; + host.passes = null; + host.input = .{}; + host.commands = .empty; + try host.adopt(Fixture.sketch); + try std.testing.expectEqual(@as(u32, 77), @as(*Fixture.State, @ptrCast(@alignCast(host.live().ptr))).value); + try std.testing.expectEqual(@as(usize, 0), Fixture.init_calls); + try std.testing.expectEqual(@as(usize, 1), Fixture.setup_calls); + const session_ptr = host.session.bytes.ptr; + try host.adopt(Fixture.sketch); + try std.testing.expectEqual(session_ptr, host.session.bytes.ptr); + try std.testing.expectEqual(@as(usize, 1), Fixture.setup_calls); + host.resetState(); + try std.testing.expectEqual(@as(usize, 1), Fixture.init_calls); + try std.testing.expectEqual(@as(usize, 1), Fixture.setup_calls); + var changed = Fixture.sketch; + changed.info = Fixture.info2; + changed.defaults = Fixture.defaults2; + try host.adopt(changed); + try std.testing.expectEqual(@as(usize, 2), Fixture.setup_calls); + try std.testing.expectEqual(@as(u32, 1), @as(*Fixture.Session2, @ptrCast(host.session.bytes.ptr)).ready); + try std.testing.expect(host.session_ready); + Fixture.fail_setup = true; + defer Fixture.fail_setup = false; + try host.adopt(Fixture.sketch); + try std.testing.expect(!host.session_ready); + try std.testing.expect(host.console.visible); + try std.testing.expectEqual(abi.Outcome.failed, host.forward(&host.console, "anything", 0)); + try host.adopt(Fixture.sketch); + try std.testing.expect(!host.session_ready); // A compatible reload cannot bless partial resources. + try std.testing.expectEqual(@as(usize, 3), Fixture.setup_calls); +} + +test "setup writeBuffer propagates unavailable, unknown and out-of-bounds upload errors" { + const a = std.testing.allocator; + var host: Host = undefined; + host.passes = null; + const client: abi.Host = .{ .ctx = &host, .vtable = &api }; + try std.testing.expectError(error.GpuUnavailable, client.writeBuffer(.none, 0, "data")); + host.passes = .{ .gpa = a, .device = undefined, .stencil_format = undefined }; + defer host.passes.?.buffers.deinit(a); + try std.testing.expectError(error.UnknownBuffer, client.writeBuffer(.none, 0, "data")); + try host.passes.?.buffers.append(a, .{ .buffer = undefined, .size = 4 }); + try std.testing.expectError(error.UploadOutOfBounds, client.writeBuffer(@fromBackingInt(@intCast(1)), 2, "data")); +} diff --git a/src/host/control.zig b/src/host/control.zig new file mode 100644 index 0000000..58b41b3 --- /dev/null +++ b/src/host/control.zig @@ -0,0 +1,368 @@ +//! Local 9P controls, serviced on the frame thread: no SDL or state races. +const std = @import("std"); +const c9 = @import("cloud9"); +const c = @import("c"); +const Host = @import("Host.zig"); +const fs = c9.fs; +const transport = c9.transport; +const Backend = struct { + pub const Req = fs.Req; + pub const Reply = fs.Reply; +}; +const Server = fs.Server(Backend, .{ .fid_capacity = 32, .slot_capacity = 4 }); +const msize = 16384; +const names = [_][]const u8{ "/", "ctl", "status", "log", "screen.png", "profile", "profile-trace" }; + +const Connection = struct { + fd: c_int = -1, + server: Server = undefined, + input: [msize]u8 = undefined, + output: [msize * 2]u8 = undefined, + incoming: [msize]u8 = undefined, + used: usize = 0, + snapshots: [32]?[]u8 = @splat(null), + + fn close(self: *Connection, a: std.mem.Allocator) void { + if (self.fd < 0) return; + transport.close(self.fd); + for (&self.snapshots) |*snapshot| if (snapshot.*) |bytes| { + a.free(bytes); + snapshot.* = null; + }; + self.fd = -1; + } +}; + +pub const Control = struct { + allocator: std.mem.Allocator, + path: [:0]u8, + listener: c_int, + connections: [4]Connection = @splat(.{}), + png: []u8 = &.{}, + capture_pending: bool = false, + generation: u64 = 0, + capture_error: ?anyerror = null, + + pub fn init(a: std.mem.Allocator, path: []const u8) !*Control { + const self = try a.create(Control); + errdefer a.destroy(self); + const z = try a.dupeSentinel(u8, path, 0); + errdefer a.free(z); + // Refuse an existing path; never unlink another process's socket. + const fd = try transport.listenFd(.{ .unix = z }, 4); + errdefer { + transport.close(fd); + _ = std.c.unlink(z); + } + if (std.c.chmod(z, 0o600) != 0) return error.SocketPermissions; + self.* = .{ .allocator = a, .path = z, .listener = fd }; + return self; + } + + pub fn deinit(self: *Control) void { + for (&self.connections) |*conn| conn.close(self.allocator); + transport.close(self.listener); + _ = std.c.unlink(self.path); + self.allocator.free(self.path); + self.allocator.free(self.png); + self.allocator.destroy(self); + } + + pub fn poll(self: *Control, host: *Host) void { + if (transport.acceptFd(self.listener, false) catch null) |fd| { + var accepted = false; + for (&self.connections) |*conn| if (conn.fd < 0) { + conn.fd = fd; + conn.used = 0; + conn.server = .init(.{ .in = &conn.input, .out = &conn.output, .root = 1 }); + accepted = true; + break; + }; + if (!accepted) transport.close(fd); + } + for (&self.connections) |*conn| { + if (conn.fd < 0) continue; + self.service(host, conn) catch conn.close(self.allocator); + } + } + + fn service(self: *Control, host: *Host, conn: *Connection) !void { + // Bound work per frame, including clients which continually write. + for (0..32) |_| { + var progressed = false; + if (conn.used != 0) { + const consumed = conn.server.push(conn.incoming[0..conn.used]); + std.mem.copyForwards(u8, &conn.incoming, conn.incoming[consumed..conn.used]); + conn.used -= consumed; + progressed = consumed != 0; + } + while (conn.server.next()) |req| self.answer(host, conn, req); + if (conn.server.protocol.dead) return error.Closed; + if (conn.server.output().len > 0) { + if (try transport.write(conn.fd, conn.server.output())) |n| { + if (n == 0) return error.Closed; + conn.server.wrote(n); + progressed = true; + } + } + if (conn.used < conn.incoming.len) { + if (try transport.read(conn.fd, conn.incoming[conn.used..])) |n| { + if (n == 0) return error.Closed; + conn.used += n; + progressed = true; + } + } + if (!progressed) break; + } + } + + fn answer(self: *Control, host: *Host, conn: *Connection, req: fs.Req) void { + var reply: fs.Reply = .{ .tag = req.tag }; + var buffer: [msize]u8 = undefined; + const bytes = self.respond(host, conn, req, &reply, &buffer) catch |err| blk: { + reply = .{ .tag = req.tag, .status = .err, .errno = fs.E.INVAL, .ename = @errorName(err) }; + break :blk &.{}; + }; + conn.server.reply(&reply, bytes); + } + + fn respond(self: *Control, host: *Host, conn: *Connection, req: fs.Req, reply: *fs.Reply, buffer: []u8) ![]const u8 { + if (req.node < 1 or req.node > names.len) return error.UnknownFile; + switch (req.op) { + .lookup => { + if (req.node != 1) return error.NotDirectory; + for (names[1..], 2..) |name, node| if (std.mem.eql(u8, name, req.data)) { + reply.attr = self.attr(node); + return &.{}; + }; + return error.UnknownFile; + }, + .getattr => reply.attr = self.attr(req.node), + .open => { + if (req.node == 5 or req.node == 7) { + if (req.node == 5) { + if (self.capture_pending) return error.CapturePending; + if (self.capture_error) |err| return err; + if (self.png.len == 0) return error.CaptureScreenshotFirst; + } + for (&conn.snapshots, 1..) |*slot, handle| if (slot.* == null) { + slot.* = if (req.node == 5) try self.allocator.dupe(u8, self.png) else try self.trace(host); + reply.handle = @intCast(handle); + return &.{}; + }; + return error.TooManySnapshots; + } + }, + .release => if (req.opened and req.handle > 0 and req.handle <= conn.snapshots.len) { + const slot = &conn.snapshots[req.handle - 1]; + if (slot.*) |bytes| self.allocator.free(bytes); + slot.* = null; + }, + .readdir => { + if (req.node != 1) return error.NotDirectory; + var used: usize = 0; + for (names[1..], 0..) |name, index| { + if (index < req.off) continue; + std.mem.writeInt(u64, buffer[used..][0..8], index + 2, .little); + buffer[used + 8] = 0; + buffer[used + 9] = @intCast(name.len); + @memcpy(buffer[used + 10 ..][0..name.len], name); + used += 10 + name.len; + } + return buffer[0..used]; + }, + .read => { + var writer: std.Io.Writer = .fixed(buffer); + const bytes: []const u8 = switch (req.node) { + 2 => "Write one console command per line. screenshot captures the next frame.\n", + 3 => blk: { + try writer.print("name {s}\nsize {d} {d}\nmode {t}\ngpu {s}\nscreenshot_pending {}\nscreenshot_generation {d}\nscreenshot_error {s}\n", .{ host.name, host.canvas[0], host.canvas[1], host.timeline.mode, if (host.gpu != null) "yes" else "no", self.capture_pending, self.generation, if (self.capture_error) |err| @errorName(err) else "none" }); + break :blk writer.buffered(); + }, + 4 => blk: { + for (host.console.lines) |line| { + try writer.writeAll(line.bytes[0..line.len]); + try writer.writeByte('\n'); + } + break :blk writer.buffered(); + }, + 5, 7 => if (req.handle > 0 and req.handle <= conn.snapshots.len) conn.snapshots[req.handle - 1] orelse return error.InvalidHandle else return error.InvalidHandle, + 6 => blk: { + const gpu = if (host.passes) |*gpu| gpu else return error.ProfilingDisabled; + const profile = if (gpu.profile) |*profile| profile else return error.ProfilingDisabled; + try writer.print("# CPU wall milliseconds including GPU submission and fence wait; not hardware timestamps\n# frames {d}\nlabel\tcount\tmean_ms\tworst_ms\tp95_ms\tp99_ms\n", .{gpu.frame - profile.start_frame}); + var entries = profile.entries.iterator(); + while (entries.next()) |entry| { + const value = entry.value_ptr; + const percentiles = value.percentiles(); + const count: f64 = @floatFromInt(@max(value.count, 1)); + try writer.print("{s}\t{d}\t{d:.6}\t{d:.6}\t{d:.6}\t{d:.6}\n", .{ entry.key_ptr.*, value.count, @as(f64, @floatFromInt(value.total)) / count / std.time.ns_per_ms, @as(f64, @floatFromInt(value.worst)) / std.time.ns_per_ms, @as(f64, @floatFromInt(percentiles[0])) / std.time.ns_per_ms, @as(f64, @floatFromInt(percentiles[1])) / std.time.ns_per_ms }); + } + break :blk writer.buffered(); + }, + else => return error.NotFile, + }; + return window(bytes, req.off, req.size); + }, + .write => { + if (req.node != 2) return error.ReadOnly; + if (req.data.len > 4096 or std.mem.indexOfScalar(u8, req.data, 0) != null) return error.InvalidCommand; + var lines = std.mem.tokenizeScalar(u8, req.data, '\n'); + while (lines.next()) |raw| { + const line = std.mem.trim(u8, raw, " \t\r"); + if (std.mem.eql(u8, line, "screenshot")) { + self.capture_pending = true; + self.capture_error = null; + } else if (std.mem.eql(u8, line, "profile-reset")) { + const gpu = if (host.passes) |*gpu| gpu else return error.ProfilingDisabled; + const profile = if (gpu.profile) |*profile| profile else return error.ProfilingDisabled; + for (profile.entries.values()) |*entry| entry.* = .{}; + profile.start_frame = gpu.frame; + } else try host.runLine(line, c.SDL_GetTicksNS()); + } + reply.written = @intCast(req.data.len); + }, + .setattr => { + if (req.node != 2 or !req.truncate) return error.ReadOnly; + reply.attr = self.attr(req.node); + }, + } + return &.{}; + } + + fn trace(self: *Control, host: *Host) ![]u8 { + const gpu = if (host.passes) |*gpu| gpu else return error.ProfilingDisabled; + const profile = if (gpu.profile) |*profile| profile else return error.ProfilingDisabled; + const buffer = try self.allocator.alloc(u8, 256 << 10); + defer self.allocator.free(buffer); + var writer: std.Io.Writer = .fixed(buffer); + try writer.writeAll("label,sample,ns\n"); + var entries = profile.entries.iterator(); + while (entries.next()) |entry| { + const value = entry.value_ptr; + for (0..value.retained()) |i| { + try writer.writeByte('"'); + for (entry.key_ptr.*) |ch| { + if (ch == '"') try writer.writeByte('"'); + try writer.writeByte(ch); + } + try writer.print("\",{d},{d}\n", .{ value.count - value.retained() + i + 1, value.sample(i) }); + } + } + return self.allocator.dupe(u8, writer.buffered()); + } + + fn attr(self: *Control, node: u64) fs.Attr { + return .{ .name = names[@intCast(node - 1)], .node = node, .dir = node == 1, .mode = if (node == 1) 0o700 else if (node == 2) 0o600 else 0o400, .size = if (node == 5) self.png.len else 0, .version = if (node == 5) @truncate(self.generation) else 0 }; + } + + pub fn capture(self: *Control, surface: *c.SDL_Surface) void { + self.capture_pending = false; + self.save(surface) catch |err| { + self.capture_error = err; + }; + } + + fn save(self: *Control, surface: *c.SDL_Surface) !void { + const stream = c.SDL_IOFromDynamicMem() orelse return error.CaptureStream; + defer _ = c.SDL_CloseIO(stream); + if (!c.SDL_SavePNG_IO(surface, stream, false)) return error.EncodePng; + const size = c.SDL_GetIOSize(stream); + if (size <= 0) return error.EncodePng; + const pointer = c.SDL_GetPointerProperty(c.SDL_GetIOProperties(stream), c.SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, null) orelse return error.EncodePng; + const png = try self.allocator.dupe(u8, @as([*]const u8, @ptrCast(pointer))[0..@intCast(size)]); + self.allocator.free(self.png); + self.png = png; + self.generation += 1; + } +}; + +fn window(bytes: []const u8, offset: u64, count: u32) []const u8 { + if (offset >= bytes.len) return &.{}; + const start: usize = @intCast(offset); + return bytes[start..][0..@min(count, bytes.len - start)]; +} + +test "9P reads respect offsets, counts and EOF" { + try std.testing.expectEqualStrings("cd", window("abcdef", 2, 2)); + try std.testing.expectEqualStrings("ef", window("abcdef", 4, 200)); + try std.testing.expectEqualStrings("", window("abcdef", std.math.maxInt(u64), 10)); +} + +test "control writes validate and screenshot opens pin an immutable snapshot" { + const a = std.testing.allocator; + var control: Control = .{ .allocator = a, .path = undefined, .listener = -1 }; + var host: Host = undefined; // These backend operations do not access the host. + var conn: Connection = .{}; + var reply: fs.Reply = .{ .tag = 1 }; + var buf: [256]u8 = undefined; + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .setattr, .node = 2, .truncate = true }, &reply, &buf); + try std.testing.expectError(error.ReadOnly, control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 3, .data = "screenshot" }, &reply, &buf)); + try std.testing.expectError(error.InvalidCommand, control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 2, .data = "screenshot\x00" }, &reply, &buf)); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 2, .data = "screenshot\n" }, &reply, &buf); + try std.testing.expect(control.capture_pending); + try std.testing.expectError(error.CapturePending, control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 5 }, &reply, &buf)); + control.capture_pending = false; + control.png = try a.dupe(u8, "first image"); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 5 }, &reply, &buf); + const handle = reply.handle; + a.free(control.png); + control.png = try a.dupe(u8, "second image"); + defer a.free(control.png); + const bytes = try control.respond(&host, &conn, .{ .tag = 1, .op = .read, .node = 5, .handle = handle, .size = 100 }, &reply, &buf); + try std.testing.expectEqualStrings("first image", bytes); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = 5, .handle = handle, .opened = true }, &reply, &buf); + try std.testing.expect(conn.snapshots[handle - 1] == null); +} + +test "control writes reset profiling counters while retaining labels" { + const a = std.testing.allocator; + var control: Control = .{ .allocator = a, .path = undefined, .listener = -1 }; + var host: Host = undefined; + host.passes = null; + var conn: Connection = .{}; + var reply: fs.Reply = .{ .tag = 1 }; + var buf: [512]u8 = undefined; + const reset: fs.Req = .{ .tag = 1, .op = .write, .node = 2, .data = "profile-reset\n" }; + try std.testing.expectError(error.ProfilingDisabled, control.respond(&host, &conn, reset, &reply, &buf)); + host.passes = @as(@import("gpu"), undefined); + const gpu = &host.passes.?; + gpu.profile = .init(a); + defer gpu.profile.?.deinit(); + const profile = &gpu.profile.?; + try profile.entries.put(profile.arena.allocator(), "scene", .{}); + profile.entries.values()[0].add(400_000); + profile.entries.values()[0].add(600_000); + gpu.frame = 420; + const read: fs.Req = .{ .tag = 1, .op = .read, .node = 6, .size = buf.len }; + const before = try control.respond(&host, &conn, read, &reply, &buf); + try std.testing.expect(std.mem.indexOf(u8, before, "scene\t2\t0.500000\t0.600000") != null); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 7 }, &reply, &buf); + const trace_handle = reply.handle; + _ = try control.respond(&host, &conn, reset, &reply, &buf); + const trace = try control.respond(&host, &conn, .{ .tag = 1, .op = .read, .node = 7, .handle = trace_handle, .size = 100 }, &reply, &buf); + try std.testing.expectEqualStrings("label,sample,ns\n\"scene\",1,400000\n\"scene\",2,600000\n", trace); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = 7, .handle = trace_handle, .opened = true }, &reply, &buf); + try std.testing.expect(conn.snapshots[trace_handle - 1] == null); + try std.testing.expectEqual(@as(usize, 1), profile.entries.count()); + try std.testing.expectEqual(@as(u64, 0), profile.entries.values()[0].total); + try std.testing.expectEqual(@as(u64, 420), profile.start_frame); + const after = try control.respond(&host, &conn, read, &reply, &buf); + try std.testing.expect(std.mem.indexOf(u8, after, "# frames 0") != null); + try std.testing.expect(std.mem.indexOf(u8, after, "scene\t0\t0.000000\t0.000000") != null); +} + +test "control writes profiling ring retains latest samples and nearest rank percentiles" { + var entry: @import("gpu").Profile.Entry = .{}; + for (1..4100) |ns| entry.add(ns); + try std.testing.expectEqual(@as(u64, 4099), entry.count); + try std.testing.expectEqual(@as(usize, 2048), entry.retained()); + try std.testing.expectEqual(@as(u64, 2052), entry.sample(0)); + try std.testing.expectEqual(@as(u64, 4099), entry.sample(2047)); + try std.testing.expectEqual([2]u64{ 3997, 4079 }, entry.percentiles()); + entry = .{}; + try std.testing.expectEqual(@as(usize, 0), entry.retained()); + try std.testing.expectEqual([2]u64{ 0, 0 }, entry.percentiles()); + entry.add(20); + try std.testing.expectEqual([2]u64{ 20, 20 }, entry.percentiles()); +} diff --git a/src/host/gfx.zig b/src/host/gfx.zig index a7ebd05..fef58c3 100644 --- a/src/host/gfx.zig +++ b/src/host/gfx.zig @@ -12,6 +12,7 @@ pub const Images = struct { const Entry = struct { texture: ?*c.SDL_Texture = null, + gpu_texture: ?*anyopaque = null, w: u32 = 0, h: u32 = 0, /// Holds premultiplied colour: a target drawn into, a GPU pass's @@ -79,6 +80,7 @@ pub const Images = struct { /// Makes `image` show a GPU render target (GPU renderer only). pub fn wrap(self: *Images, image: abi.Image, gpu_texture: *anyopaque, w: u32, h: u32) !void { const entry = self.get(image) orelse return error.UnknownImage; + if (entry.gpu_texture == gpu_texture) return; const props = c.SDL_CreateProperties(); defer c.SDL_DestroyProperties(props); _ = c.SDL_SetPointerProperty(props, c.SDL_PROP_TEXTURE_CREATE_GPU_TEXTURE_POINTER, gpu_texture); @@ -89,7 +91,7 @@ pub const Images = struct { // Rendered output: smooth scaling, and already premultiplied. _ = c.SDL_SetTextureScaleMode(texture, c.SDL_SCALEMODE_LINEAR); entry.release(); - entry.* = .{ .texture = texture, .w = w, .h = h, .premultiplied = true, .smooth = true }; + entry.* = .{ .texture = texture, .w = w, .h = h, .premultiplied = true, .smooth = true, .gpu_texture = gpu_texture }; } /// Stops showing a wrapped render target, before the target goes away. @@ -97,6 +99,7 @@ pub const Images = struct { const entry = self.get(image) orelse return; if (entry.texture) |t| c.SDL_DestroyTexture(t); entry.texture = null; + entry.gpu_texture = null; } pub fn free(self: *Images, image: abi.Image) void { @@ -591,7 +594,7 @@ fn fcolor(color: abi.Color) c.SDL_FColor { /// Native shaped text on Linux, using a 13px monospace face at scale 1. /// Other targets retain SDL's built-in 8x8 fallback. Coordinates are line-box tops. pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: []const u8, color: abi.Color, face: abi.Face, raster: f32) void { - if (@import("builtin").os.tag == .linux and c.sk_text_draw_scaled(renderer, @intFromEnum(face), x, y, scale, raster, bytes.ptr, bytes.len, color.r, color.g, color.b, color.a)) return; + if (@import("builtin").os.tag == .linux and c.sk_text_draw_scaled(renderer, @backingInt(face), x, y, scale, raster, bytes.ptr, bytes.len, color.r, color.g, color.b, color.a)) return; var buf: [1024]u8 = undefined; const len = @min(std.mem.indexOfScalar(u8, bytes, 0) orelse bytes.len, buf.len - 1); @memcpy(buf[0..len], bytes[0..len]); @@ -623,7 +626,7 @@ pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: [] /// its line, in canvas units (`Host.textSize`). pub fn textSize(renderer: *c.SDL_Renderer, scale: f32, bytes: []const u8, face: abi.Face) [2]f32 { var size: [2]f32 = .{ 0, 0 }; - if (@import("builtin").os.tag == .linux and c.sk_text_measure_face(renderer, @intFromEnum(face), scale, bytes.ptr, bytes.len, &size[0], &size[1])) return size; + if (@import("builtin").os.tag == .linux and c.sk_text_measure_face(renderer, @backingInt(face), scale, bytes.ptr, bytes.len, &size[0], &size[1])) return size; // The debug font's 8 px cells. const s = @max(scale, 1); return .{ @as(f32, @floatFromInt(bytes.len)) * 8 * s, 8 * s }; diff --git a/src/host/root.zig b/src/host/root.zig index d01a63f..c9cdde6 100644 --- a/src/host/root.zig +++ b/src/host/root.zig @@ -39,6 +39,8 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { while (args.next()) |arg| { if (std.mem.eql(u8, arg, "--state")) { options.state = args.next() orelse fatal("--state needs a file"); + } else if (std.mem.eql(u8, arg, "--9p")) { + options.ninep = args.next() orelse fatal("--9p needs a socket path"); } else if (std.mem.eql(u8, arg, "--fresh")) { options.fresh = true; } else if (std.mem.eql(u8, arg, "--headless")) { @@ -121,6 +123,7 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { const usage = \\usage: sketchbook [options] [LIBRARY] + \\ --9p SOCKET local 9P controls: ctl, status, log, screen.png \\ --state FILE persistent state (default: sketch.state) \\ --headless offscreen video, silent audio, no vsync \\ --script FILE play a script instead of reading devices @@ -191,9 +194,11 @@ const sdl_heap = struct { }; test { + _ = Host; _ = memory; _ = fields; _ = timeline; _ = @import("audio.zig"); _ = @import("Console.zig"); + _ = @import("control.zig"); } 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)); } |
