From 10115489f8988cf6a649707202a51757708883a2 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:59:53 -0300 Subject: svg: an image that is an SVG document in a data URI is drawn Typst places an SVG image, and a PDF page imported with image(), as an whose href is the document in base64; svg.zig skipped every , so any Typst object holding one silently lost it. The parser now decodes such a document and walks it into the same drawing, mapped into the image's x, y, width and height (stretched with preserveAspectRatio="none", else fitted and centered, as SVG says), with its own ids, gradients and percentages and nothing inherited from around it but opacity. It is not clipped to its box (a PDF page never draws outside itself, and a clip per shape costs the renderers a pass over every contour). Raster images still draw nothing. The root's view box and size come from one function both documents use. Tests: a unit test at the end of svg.zig (ids of its own, stretched and fitted corners, a PNG skipped); two corpus cases for zig build diff, image.svg (two data-URI images, one stretched and turned, beside an outer gradient that shares an id with an inner one) and typst-image.typ (two SVGs placed by Typst): both renderers match resvg (mean 0.09-0.11). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- test/svg/image.svg | 9 +++++++++ test/svg/typst-image.typ | 6 ++++++ 2 files changed, 15 insertions(+) create mode 100644 test/svg/image.svg create mode 100644 test/svg/typst-image.typ (limited to 'test') diff --git a/test/svg/image.svg b/test/svg/image.svg new file mode 100644 index 0000000..f5a5d8e --- /dev/null +++ b/test/svg/image.svg @@ -0,0 +1,9 @@ + + + + + + + diff --git a/test/svg/typst-image.typ b/test/svg/typst-image.typ new file mode 100644 index 0000000..2c88207 --- /dev/null +++ b/test/svg/typst-image.typ @@ -0,0 +1,6 @@ +// An SVG placed by Typst: an with the document as a data URI. +#set page(width: auto, height: auto, margin: 6pt, fill: none) +#stack(dir: ltr, spacing: 8pt, + image("use.svg", width: 120pt), + image("gradients.svg", width: 80pt, height: 60pt, fit: "stretch"), +) -- cgit v1.3 From 9d10fc880bf4e62f8421d351ce15bddeec075322 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 01:10:28 -0300 Subject: examples: manual, Figure 2-1 of the Intel SDM comes alive, as a talk examples/manual.zig is a live scene with pauses, played by the stock player: page 31 of the SDM's Vol. 2 (Typst's import of the PDF, every glyph a shape) fades in on paper beside a title; the camera closes on Figure 2-1, the instruction format, while the rest of the page greys and its footnotes go; each field (Prefixes, Opcode, ModR/M, SIB, Displacement, Immediate) is tinted and glows in turn; 83 C0 01, add eax, 1, is written below; each byte flies into its field as the fields it has none for fade; C0 morphs down the arrow into the ModR/M byte's boxes as 11000000, which splits into 11, 000 and 000 over Mod, Reg and R/M, each with what it says (direct, /0 ADD, EAX); and the camera goes back to the page, filled in. Twelve steps, 18 s played straight. The labels, the ModR/M boxes, the figure and its footnotes are parts of the page, by rectangles in the page's units read off pdftotext -bbox (points to units: (pt - 306) * 4/3, (pt - 396) * 4/3) and checked with the guides; each tint is the s.rect of its label's rectangle. The bits are parts of a Typst object, cut by character (0.6 em of DejaVu Sans Mono at 14 pt). The PDF is never committed. build.zig's -Dmanual names it, by default ~/01-resources/intel-devref-ii.pdf; when the file is not there the scene is one line saying how to build with it. zig build manual plays it in a window. test/manual.script, run by zig build test (and test-manual) only when the file is there, steps through the talk with pixel checks (the paper, a tint where it should be and not where it should not, a step back) and exports it twice at 10 fps, the same frames both times. zig build bench also times the manual's page (its one #image source, when the build has the PDF), with a budget of its own for a drawing of over 1000 shapes: parse under 25 ms, CPU draw under 10, GPU record under 4, at 1280x720. Measured: 6.4, 1.9 and 0.77 ms on a quiet run; 14.8, 3.5 and 1.4 at load average 34; once 28.6 ms to parse, at that load, which failed it (the simple objects' 1 ms is as sensitive: 0.87 ms then). The manual's simple objects are not benched: a two-glyph 83 fitted to the whole canvas takes 1.1-1.2 ms on the CPU. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- build.zig | 29 +++++++++- examples/manual.zig | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++++ test/manual.script | 73 +++++++++++++++++++++++++ tools/bench.zig | 101 +++++++++++++++++++++------------- 4 files changed, 318 insertions(+), 40 deletions(-) create mode 100644 examples/manual.zig create mode 100644 test/manual.script (limited to 'test') diff --git a/build.zig b/build.zig index 02d312e..08db674 100644 --- a/build.zig +++ b/build.zig @@ -300,6 +300,31 @@ pub fn build(b: *Build) void { test_talk.dependOn(&talk_check.run.step); test_step.dependOn(&talk_check.run.step); + // The manual example: Figure 2-1 of the Intel SDM, Vol. 2, page 31, as + // a talk in the stock player. The PDF is named, never committed: built + // without it, the scene says so, and only its script needs it. + const pdf = b.option([]const u8, "manual", "The Intel SDM Vol. 2 PDF for the manual example (default ~/01-resources/intel-devref-ii.pdf)") orelse + b.pathJoin(&.{ b.graph.environ_map.get("HOME") orelse "/", "01-resources", "intel-devref-ii.pdf" }); + const have_pdf = if (std.Io.Dir.cwd().access(b.graph.io, pdf, .{})) true else |_| false; + const manual_options = b.addOptions(); + manual_options.addOption(?[]const u8, "pdf", if (have_pdf) pdf else null); + const manual = sb.addScene(.{ .root_source_file = b.path("examples/manual.zig"), .imports = &.{.{ .name = "manual", .module = manual_options.createModule() }} }); + const manual_lib = manual.app.addLibrary(); + b.step("manual", "Play the manual example in a window (-Dmanual=PDF, Vol. 2 of the Intel SDM)").dependOn(&manual.app.addRun(manual_lib, .{ .state = ".zig-cache/manual.state" }).step); + test_step.dependOn(&manual_lib.step); + if (have_pdf) { + const manual_exe = manual.app.addExecutable(); + const manual_script = manual.app.addScript(manual_exe, b.path("test/manual.script"), .{}); + // Two exports (a frame cap apiece, so the build runs both), which + // quit at the end, as the stock player does. + for ([_][]const u8{ "a", "b" }, [_]u64{ 1000, 1001 }) |define, cap| { + manual_script.addArg("--define"); + manual_script.addPrefixedDirectoryArg(b.fmt("{s}=", .{define}), manual.app.addExport(manual_exe, .{ .fps = 10, .frames = cap, .size = .{ 640, 360 } }).frames); + } + b.step("test-manual", "Play the manual example through its steps, and export it twice").dependOn(&manual_script.step); + test_step.dependOn(&manual_script.step); + } + // Both vector renderers against resvg, through the host (tools/svgdiff.py). const svgview = sb.app(.{ .name = "svgview", .root_source_file = b.path("test/svgview.zig"), .imports = &.{.{ .name = "vector", .module = sb.modules.vector }} }); const diff = b.addSystemCommand(&.{ "python3", "tools/svgdiff.py" }); @@ -324,11 +349,13 @@ pub fn build(b: *Build) void { .{ .name = "anim", .module = sb.modules.anim }, .{ .name = "scene", .module = compiled.module }, .{ .name = "typst", .module = compiled.typst }, + .{ .name = "manual", .module = manual.module }, + .{ .name = "manual_typst", .module = manual.typst }, }, }), })); bench.has_side_effects = true; - b.step("bench", "Time parsing and drawing a scene's Typst (release mode, must be under 1ms)").dependOn(&bench.step); + b.step("bench", "Time parsing and drawing scenes' Typst (release mode: a simple object under 1ms, a PDF page in its own budget)").dependOn(&bench.step); // After the tests above, not beside them: it measures time. for (test_step.dependencies.items) |step| bench.step.dependOn(step); test_step.dependOn(&bench.step); diff --git a/examples/manual.zig b/examples/manual.zig new file mode 100644 index 0000000..8b11247 --- /dev/null +++ b/examples/manual.zig @@ -0,0 +1,155 @@ +//! A page of the Intel SDM as a talk: Figure 2-1, the instruction format +//! (Vol. 2, page 31), comes alive. The page is Typst's import of the PDF, +//! every glyph and rule a vector shape; the fields are `part`s of it, cut +//! out by rectangles in the page's units (read off the player's guides and +//! `pdftotext -bbox`, points to units: (pt - 306) * 4/3 across, (pt - 396) +//! * 4/3 down). `83 C0 01`, `add eax, 1`, is written below, its bytes fly +//! into their fields, and C0 splits into its ModR/M bits. +//! +//! The PDF is the build's `-Dmanual` (`~/01-resources/intel-devref-ii.pdf` +//! by default) and is never committed; without it the scene says so. +//! +//! click, space, page down next step page up back +const std = @import("std"); +const anim = @import("anim"); +const pdf: ?[]const u8 = @import("manual").pdf; + +pub const name = "manual"; +pub const hold = 1.5; +pub const typst_preamble = + \\#set page(width: auto, height: auto, margin: 0pt, fill: none) + \\#set text(font: "DejaVu Sans Mono", size: 14pt, fill: rgb("#c0392b")) +; +pub const typst = struct { + pub const page = "#image(\"" ++ (pdf orelse "") ++ "\", page: 31)"; + pub const title = "#text(font: \"New Computer Modern\", size: 40pt, fill: rgb(\"#ece6e2\"))[Figure 2-1, alive]"; + pub const from = "#text(font: \"New Computer Modern\", size: 16pt, fill: rgb(\"#8c8c8c\"))[IntelĀ® 64 and IA-32 SDM, Vol. 2, page 31]"; + pub const add = "#text(fill: rgb(\"#555555\"))[add eax, 1]"; + pub const b83 = "83"; + pub const c0 = "C0"; + pub const b01 = "01"; + pub const bits = "11000000"; + pub const direct = "#text(size: 8pt, fill: rgb(\"#555555\"))[direct]"; + pub const is_add = "#text(size: 8pt, fill: rgb(\"#555555\"))[/0 ADD]"; + pub const eax = "#text(size: 8pt, fill: rgb(\"#555555\"))[EAX]"; +}; +const missing = struct { + pub const note = "#text(font: \"New Computer Modern\", size: 24pt, fill: rgb(\"#ece6e2\"))[No manual: build with `-Dmanual=/path/to/intel-sdm-vol2.pdf`]"; +}; +pub const scene = anim.Live(if (pdf == null) placeholder else talk, if (pdf == null) missing else typst); + +fn placeholder(s: *anim.Frame) void { + s.play(.{s.typst(missing.note, .{}).write()}, .{}); +} + +/// Rectangles in the page's units, as `s.part` and `s.rect` take them. +const Area = struct { x: f32, y: f32, w: f32, h: f32 }; + +/// The labels of the figure's top row of fields: Prefixes, Opcode, +/// ModR/M, SIB, Displacement, Immediate (inside the rules, which stay). +const fields = [_]Area{ + .{ .x = -199.5, .y = -110.7, .w = 69.3, .h = 33.3 }, + .{ .x = -127.5, .y = -110.7, .w = 69.3, .h = 33.3 }, + .{ .x = -55.5, .y = -110.7, .w = 69.3, .h = 33.3 }, + .{ .x = 16.5, .y = -110.7, .w = 69.3, .h = 33.3 }, + .{ .x = 100.5, .y = -110.7, .w = 93.3, .h = 33.3 }, + .{ .x = 184.9, .y = -110.7, .w = 70.1, .h = 33.3 }, +}; +const opcode = 1; +const modrm = 2; +const immediate = 5; +/// The ModR/M byte's own boxes: Mod, Reg/Opcode, R/M. +const bit_boxes = [_]Area{ + .{ .x = -144.0, .y = 5.3, .w = 33.3, .h = 25.3 }, + .{ .x = -102.0, .y = 5.3, .w = 45.3, .h = 25.3 }, + .{ .x = -53.7, .y = 5.3, .w = 46.0, .h = 25.3 }, +}; +/// The ModR/M byte's three boxes together, across. +const byte_x = -95.7; +/// The figure: both rows of boxes, the arrows and the notes; and its +/// footnotes, under it. +const figure: Area = .{ .x = -7.3, .y = -54.7, .w = 468, .h = 160 }; +const footnotes: Area = .{ .x = -8, .y = 70.7, .w = 453, .h = 56 }; +/// `11000000` in its own units: 8 characters of 0.6 em at 14 pt. +const char = 0.6 * 14 * 4.0 / 3.0; + +fn talk(s: *anim.Frame) void { + // The page, whole, on paper; the camera far enough out to see it all. + const paper = s.rect(.{ .w = 816, .h = 1056, .fill = 0xffffff }); + const page = s.typst(typst.page, .{}); + const title = s.typst(typst.title, .{ .x = -700, .y = -380 }); + const from = s.typst(typst.from, .{ .x = -700, .y = -325 }); + // A tint over each field's label box (the same rectangle as its part), + // laid before the parts so that they draw over it. + var tints: [fields.len]anim.Obj = undefined; + for (&tints, fields) |*t, f| t.* = s.rect(.{ .x = f.x, .y = f.y, .w = f.w, .h = f.h, .fill = 0x58c4dd60 }); + s.play(.{anim.camera(.{ .zoom = 0.65 })}, .{ .seconds = 0 }); + s.play(.{ title.write(), from.fadeIn(), paper.fadeIn(), page.fadeIn() }, .{ .seconds = 2 }); + s.pause(); + + // Onto the figure. Its labels, the ModR/M byte's boxes, the figure + // around them and its footnotes become parts of the page; the rest of + // the page greys, and the footnotes go, to make room. + var labels: [fields.len]anim.Obj = undefined; + for (&labels, fields) |*l, f| l.* = s.part(page, f); + var boxes: [bit_boxes.len]anim.Obj = undefined; + for (&boxes, bit_boxes) |*b, area| b.* = s.part(page, area); + _ = s.part(page, figure); + const notes = s.part(page, footnotes); + s.play(.{ anim.camera(.{ .zoom = 2.3, .x = -7, .y = -35 }), page.move(.{ .opacity = 0.25 }), notes.fadeOut(), title.fadeOut(), from.fadeOut() }, .{ .seconds = 1.5 }); + s.pause(); + + // Each field in turn. + for (labels, tints, 0..) |label, tint, i| { + if (i > 0) s.play(.{ tints[i - 1].fadeOut(), label.indicate(), tint.fadeIn() }, .{}) else s.play(.{ label.indicate(), tint.fadeIn() }, .{}); + s.pause(); + } + s.play(.{tints[fields.len - 1].fadeOut()}, .{ .seconds = 0.5 }); + + // An instruction: add eax, 1. + const b83 = s.typst(typst.b83, .{ .x = -40, .y = 70 }); + const c0 = s.typst(typst.c0, .{ .y = 70 }); + const b01 = s.typst(typst.b01, .{ .x = 40, .y = 70 }); + const add = s.typst(typst.add, .{ .x = 150, .y = 70 }); + s.play(.{ b83.write(), c0.write(), b01.write(), add.write() }, .{ .seconds = 1.5, .lag = 0.3 }); + s.pause(); + + // Each byte flies into its field; the fields it has none for fade. + var flights: [fields.len + 3]anim.Animation = undefined; + for (labels, 0..) |label, i| flights[i] = switch (i) { + opcode, modrm, immediate => label.fadeOut(), + else => label.move(.{ .opacity = 0.3 }), + }; + flights[6] = b83.move(.{ .x = fields[opcode].x, .y = fields[opcode].y, .scale = 1.3 }); + flights[7] = c0.move(.{ .x = fields[modrm].x, .y = fields[modrm].y, .scale = 1.3 }); + flights[8] = b01.move(.{ .x = fields[immediate].x, .y = fields[immediate].y, .scale = 1.3 }); + s.play(flights, .{ .seconds = 1.5, .lag = 0.2 }); + s.pause(); + + // C0 is a ModR/M byte: down the arrow into its boxes, as bits. + const bits = s.typst(typst.bits, .{ .x = byte_x, .y = bit_boxes[0].y }); + s.play(.{ boxes[0].fadeOut(), boxes[1].fadeOut(), boxes[2].fadeOut(), c0.transformInto(bits) }, .{ .seconds = 1.5 }); + s.pause(); + + // 11 000 000: mod, reg, r/m, each into its box, and what it says. + const mod = s.part(bits, .{ .x = -3 * char, .w = 2 * char, .h = 40 }); + const reg = s.part(bits, .{ .x = -0.5 * char, .w = 3 * char, .h = 40 }); + const rm = s.part(bits, .{ .x = 2.5 * char, .w = 3 * char, .h = 40 }); + const says = [_]anim.Obj{ + s.typst(typst.direct, .{ .x = bit_boxes[0].x, .y = 28 }), + s.typst(typst.is_add, .{ .x = bit_boxes[1].x, .y = 28 }), + s.typst(typst.eax, .{ .x = bit_boxes[2].x, .y = 28 }), + }; + s.play(.{ + mod.move(.{ .x = bit_boxes[0].x }), + reg.move(.{ .x = bit_boxes[1].x }), + rm.move(.{ .x = bit_boxes[2].x }), + says[0].write(), + says[1].write(), + says[2].write(), + }, .{ .seconds = 1.5 }); + s.pause(); + + // Back to the page, filled in. + s.play(.{ anim.camera(.{ .zoom = 0.65, .x = 0, .y = 0 }), page.move(.{ .opacity = 1 }), notes.fadeIn(), add.fadeOut(), title.fadeIn(), from.fadeIn() }, .{ .seconds = 1.5 }); +} diff --git a/test/manual.script b/test/manual.script new file mode 100644 index 0000000..3bc3c73 --- /dev/null +++ b/test/manual.script @@ -0,0 +1,73 @@ +# The manual example (examples/manual.zig) on Vol. 2 of the Intel SDM: +# Figure 2-1 stepped through in the stock player, and exported twice. +# Built only when the PDF is there (-Dmanual). +frames 150 +expect deck.waiting true +expect deck.log.len 0 +screenshot .zig-cache/manual/page.png +# The page on its paper, the whole of it in view. +expect-pixel .zig-cache/manual/page.png 400 650 255 255 255 255 2 +# Onto the figure; the rest of the page greys. +key space +await deck.waiting true +screenshot .zig-cache/manual/figure.png +expect-pixel .zig-cache/manual/figure.png 460 160 255 255 255 255 2 +# Each field in turn, tinted: the third is ModR/M. +key space +await deck.waiting true +key space +await deck.waiting true +key space +await deck.waiting true +expect deck.log.len 4 +screenshot .zig-cache/manual/modrm.png +expect-pixel .zig-cache/manual/modrm.png 460 160 192 233 242 255 3 +expect-pixel .zig-cache/manual/modrm.png 300 160 255 255 255 255 2 +# Back a step: Opcode's tint again, ModR/M's gone. +key page_up +expect deck.log.len 3 +frames 2 +screenshot .zig-cache/manual/opcode.png +expect-pixel .zig-cache/manual/opcode.png 460 160 255 255 255 255 2 +expect-pixel .zig-cache/manual/opcode.png 300 160 192 233 242 255 3 +key space +await deck.waiting true +key space +await deck.waiting true +key space +await deck.waiting true +key space +await deck.waiting true +# 83 C0 01 written below, where the footnotes were. +key space +await deck.waiting true +screenshot .zig-cache/manual/bytes.png +# Into their fields: ModR/M's label gives way to C0. +key space +await deck.waiting true +screenshot .zig-cache/manual/flown.png +expect-different-screenshot .zig-cache/manual/bytes.png .zig-cache/manual/flown.png 440 140 180 90 +# C0 down into its boxes as bits, then split over Mod, Reg and R/M. +key space +await deck.waiting true +screenshot .zig-cache/manual/bits.png +expect-pixel .zig-cache/manual/bits.png 527 185 255 255 255 255 2 +key space +await deck.waiting true +screenshot .zig-cache/manual/split.png +expect-different-screenshot .zig-cache/manual/bits.png .zig-cache/manual/split.png 280 420 310 70 +expect deck.log.len 11 +# Back to the page, filled in, and the end of the talk. +key space +frames 120 +expect deck.log.len 12 +expect deck.waiting false +screenshot .zig-cache/manual/end.png +expect-different-screenshot .zig-cache/manual/page.png .zig-cache/manual/end.png 470 260 340 120 +# Exported ({a}, {b}: 10 fps, half size), each step held 1.5 s: the same +# frames twice, and the talk moves through them. +expect-same-screenshot {a}/000040.png {b}/000040.png +expect-same-screenshot {a}/000200.png {b}/000200.png +expect-same-screenshot {a}/000340.png {b}/000340.png +expect-different-screenshot {a}/000040.png {a}/000200.png +expect-different-screenshot {a}/000200.png {a}/000340.png diff --git a/tools/bench.zig b/tools/bench.zig index faa769e..5412866 100644 --- a/tools/bench.zig +++ b/tools/bench.zig @@ -1,17 +1,29 @@ //! How long a frame of vector graphics takes, per stage, in release mode: //! parsing the SVG, turning it into polygons and coverage (CPU backend), or //! into a GPU pass description (GPU backend; the GPU's own time is not -//! measured here). +//! measured here). The objects: the compiled test scene's, and the manual +//! example's page of the Intel SDM (when the build has the PDF), thousands +//! of shapes, with a budget of its own. const std = @import("std"); const vector = @import("vector"); const scene = @import("scene").scene; const typst = @import("typst"); +const manual = @import("manual").scene; +const manual_typst = @import("manual_typst"); const width = 1280; const height = 720; const runs = 300; /// Drawings up to this many shapes count as simple: they must take < 1ms. const simple_shapes = 30; +/// Drawings of more count as a page (Figure 2-1's page of the SDM is 1954 +/// shapes; measured on a loaded 16-core machine: parse 6-12 ms, CPU draw +/// 2-5 ms, GPU record 0.85-1.7 ms). Parsing is once per object, drawing +/// every frame. +const page_shapes = 1000; +const page_parse_ms = 25; +const page_cpu_ms = 10; +const page_gpu_ms = 4; /// Stand-in for `sketch.Host`: the renderers only need images (and to free /// the GPU backend's paint buffer, which this bench never makes). @@ -34,48 +46,59 @@ pub fn main(init: std.process.Init) !void { defer objects_arena.deinit(); const shot = try scene.shot(objects_arena.allocator(), .{ .t = 0 }); for (shot.objects, 0..) |object, index| if (object.source == .typst) { - const svg = typst.find(object.source.typst).?; - var arena: std.heap.ArenaAllocator = .init(gpa); - defer arena.deinit(); - var cpu: vector.Renderer(.cpu) = try .init(gpa, Host{}, width, height); - defer cpu.deinit(Host{}); - var gpu: vector.Renderer(.gpu) = try .init(gpa, Host{}, width, height); - defer gpu.deinit(Host{}); - const Frame = struct { scratch: std.mem.Allocator }; - - var parse_ns: [runs]u64 = undefined; - var cpu_ns: [runs]u64 = undefined; - var gpu_ns: [runs]u64 = undefined; - var shapes: usize = 0; - for (0..runs) |i| { - _ = arena.reset(.retain_capacity); - const t0 = now(io); - const d = try vector.svg.parse(arena.allocator(), svg); - const t1 = now(io); - // Fit to the canvas, as large as it goes: the worst case. - const m = vector.fit(d, .{ .w = width, .h = height }); - cpu.begin(Frame{ .scratch = arena.allocator() }, .{ 0, 0, 0, 1 }); - try cpu.draw(d, m, 1); - const t2 = now(io); - gpu.begin(Frame{ .scratch = arena.allocator() }, .{ 0, 0, 0, 1 }); - try gpu.draw(d, m, 1); - std.mem.doNotOptimizeAway(gpu.target.pass()); - const t3 = now(io); - parse_ns[i] = t1 - t0; - cpu_ns[i] = t2 - t1; - gpu_ns[i] = t3 - t2; - shapes = d.shapes.len; - } - const p = median(&parse_ns); - const c = median(&cpu_ns); - const g = median(&gpu_ns); - std.debug.print("{d:>2} {s:<21} {d:>7} {d:>8.3}ms {d:>8.3}ms {d:>8.3}ms {d:>8.3}ms\n", .{ index, label(object.source.typst), shapes, ms(p), ms(c), ms(g), ms(p + c) }); - if (shapes <= simple_shapes and (p + c > std.time.ns_per_ms or p + g > std.time.ns_per_ms)) failed = true; + if (try bench(gpa, io, index, object.source.typst, typst.find(object.source.typst).?)) failed = true; + }; + // The manual's page of the SDM, when the build has the PDF. + for (manual.typst_sources, 0..) |source, index| if (std.mem.startsWith(u8, source, "#image(")) { + if (try bench(gpa, io, index, source, manual_typst.find(source).?)) failed = true; }; - std.debug.print("\nup to {d} shapes must parse and draw in under 1ms at {d}x{d}, filling the canvas.\n", .{ simple_shapes, width, height }); + std.debug.print("\nup to {d} shapes must parse and draw in under 1ms at {d}x{d}, filling the canvas;\n", .{ simple_shapes, width, height }); + std.debug.print("a page (over {d}) parse in under {d}ms, draw on the CPU in {d}ms and record for the GPU in {d}ms.\n", .{ page_shapes, page_parse_ms, page_cpu_ms, page_gpu_ms }); if (failed) return error.TooSlow; } +/// Times one SVG, prints its line, and says whether it is over budget. +fn bench(gpa: std.mem.Allocator, io: std.Io, index: usize, source: []const u8, svg: []const u8) !bool { + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + var cpu: vector.Renderer(.cpu) = try .init(gpa, Host{}, width, height); + defer cpu.deinit(Host{}); + var gpu: vector.Renderer(.gpu) = try .init(gpa, Host{}, width, height); + defer gpu.deinit(Host{}); + const Frame = struct { scratch: std.mem.Allocator }; + + var parse_ns: [runs]u64 = undefined; + var cpu_ns: [runs]u64 = undefined; + var gpu_ns: [runs]u64 = undefined; + var shapes: usize = 0; + for (0..runs) |i| { + _ = arena.reset(.retain_capacity); + const t0 = now(io); + const d = try vector.svg.parse(arena.allocator(), svg); + const t1 = now(io); + // Fit to the canvas, as large as it goes: the worst case. + const m = vector.fit(d, .{ .w = width, .h = height }); + cpu.begin(Frame{ .scratch = arena.allocator() }, .{ 0, 0, 0, 1 }); + try cpu.draw(d, m, 1); + const t2 = now(io); + gpu.begin(Frame{ .scratch = arena.allocator() }, .{ 0, 0, 0, 1 }); + try gpu.draw(d, m, 1); + std.mem.doNotOptimizeAway(gpu.target.pass()); + const t3 = now(io); + parse_ns[i] = t1 - t0; + cpu_ns[i] = t2 - t1; + gpu_ns[i] = t3 - t2; + shapes = d.shapes.len; + } + const p = median(&parse_ns); + const c = median(&cpu_ns); + const g = median(&gpu_ns); + std.debug.print("{d:>2} {s:<21} {d:>7} {d:>8.3}ms {d:>8.3}ms {d:>8.3}ms {d:>8.3}ms\n", .{ index, label(source), shapes, ms(p), ms(c), ms(g), ms(p + c) }); + if (shapes <= simple_shapes) return p + c > std.time.ns_per_ms or p + g > std.time.ns_per_ms; + if (shapes > page_shapes) return p > page_parse_ms * std.time.ns_per_ms or c > page_cpu_ms * std.time.ns_per_ms or g > page_gpu_ms * std.time.ns_per_ms; + return false; +} + /// The start of a Typst source's first line. fn label(source: []const u8) []const u8 { const line = source[0 .. std.mem.indexOfScalar(u8, source, '\n') orelse source.len]; -- cgit v1.3