← gpu_record · API reference · version 0.3.1
3 file(s), 424 lines. This is the source the published version ships, and it is loft — the library is written in the same language you call it from.
Public items
A worked example is a call site the author chose as the thing to read first. Call sites are every line naming the item, found mechanically over the source with comments excluded — so an item with no worked example is not an item nobody uses.
| Item | Defined | Worked example | Call sites |
|---|---|---|---|
table fn | 27 | — | 47 264 302 339 13 14 … (7 total) |
log fn | 32 | — | 86 141 148 159 207 248 … (9 total) |
reset fn | 37 | — | 46 338 12 |
live fn | 42 | — | 230 239 247 263 266 268 |
refuse_source fn | 49 | — | 294 298 |
src/gpu_record.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// A GPU backend that draws nothing and writes down every call it receives.
//
// gpu_record::reset();
// dev = gpu::open(gpu_record::table(), 64, 48, "test") ?? return;
// …the program's drawing…
// assert(gpu_record::log() == EXPECTED);
//
// It needs no display, so a program's drawing is tested on any machine: the log names each
// pipeline, pass and draw in order, with handles numbered from 1 as they are made. A program
// gives it sources under the tag "record"; their text is not read.
fn rec_table() -> integer;
#c "rec_table" "int64_t()"
fn rec_log() -> text;
#c "rec_log" "const char*()"
fn rec_reset();
#c "rec_reset" "void()"
fn rec_live() -> integer;
#c "rec_live" "int64_t()"
fn rec_refuse_source(on: integer);
#c "rec_refuse_source" "void(int64_t)"
// This backend's table, for `gpu::open`. It is there on every machine.
pub fn table() -> i64 {
rec_table()
}
// Every call received since the last `reset`, one line each.
pub fn log() -> text {
rec_log()
}
// Forget the log and every object, number handles from 1 again, and take sources again.
pub fn reset() {
rec_reset();
}
// How many buffers, textures, targets and pipelines are held now: 0 once their owners died.
pub fn live() -> integer {
rec_live()
}
// Refuse (or take again) a pipeline whose shader arrives as text — what a machine that compiles
// no shader while a program runs does. A source with empty text passes: it stands for a shader
// compiled ahead.
pub fn refuse_source(on: boolean) {
rec_refuse_source(if on { 1 } else { 0 });
}
tests/frame.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// A scripted frame through `gpu`, read back from the recording backend. Every expected log is
// written by hand from the calls above it: handles count from 1 in the order they are made, and
// a block's offsets are gpu's layout (a matrix 16 floats, an `F4` on a multiple of 4).
use gpu;
use gpu_record;
// Three inputs per vertex; a block of a matrix, a vector, a float and a vector; one texture.
fn view() -> gpu::Program {
gpu::Program {
vertex: [gpu::F3, gpu::F3, gpu::F2],
instance: [],
block: [gpu::MAT4, gpu::F4, gpu::F1, gpu::F4],
textures: 1,
sources: [gpu::source("other", "not", "these"), gpu::source("record", "v", "f")],
}
}
// One input per vertex and two per copy; a block of one pair; no texture.
fn copies() -> gpu::Program {
gpu::Program {
vertex: [gpu::F3],
instance: [gpu::F4, gpu::F2],
block: [gpu::F2],
textures: 0,
sources: [gpu::source("record", "v", "f")],
}
}
// A log: each line with its newline.
fn lines(of: const vector<text>) -> text {
out = "";
for l in of { out += "{l}\n"; }
out
}
fn matrix(first: float) -> vector<float> {
m: vector<float> = [for _ in 0..16 { 0.0 }];
m[0] = first;
m
}
fn open() -> gpu::Device {
gpu_record::reset();
gpu::open(gpu_record::table(), 64, 48, "frame")!
}
fn frame_log() -> text {
lines([
"open 1 64x48 \"frame\"",
"pipeline 2 vertex 3 3 2 instance block 16x1@0 4x1@16 1x1@20 4x1@24 size 28 textures 1",
" state shape 0 depth 2 blend 0 cull 0 into 0",
"texture 3 8x8 colour",
"texture_nearest 3 on",
"buffer 4 floats 16",
"begin 0 clear ff102030",
" bind 2",
" uniforms floats 28 first 2.000 last 7.500",
" sample 0 texture 3",
" vertices 4",
" draw from 0 count 2 copies 0",
"end",
"present",
])
}
fn test_a_frame_arrives_at_the_backend_call_for_call() {
dev = open();
pipe = dev.pipeline(view(), gpu::State { depth: gpu::DEPTH_TEST_WRITE })!;
atlas = dev.texture(8, 8);
atlas.nearest(true);
mesh = dev.buffer([for _ in 0..16 { 0.0f }]);
u = pipe.block();
u.mat4(0, matrix(2.0));
u.f4(3, 0.0, 0.0, 0.0, 7.5);
dev.begin_window(gpu::CLEAR, 0xFF102030);
dev.bind(pipe);
dev.uniforms(u);
dev.sample(0, atlas);
dev.vertices(mesh);
dev.draw(0, 2);
dev.end();
dev.present();
assert(gpu_record::log() == frame_log(), "the frame's record:\n{gpu_record::log()}");
assert(len(dev.refusals()) == 0, "nothing refused: {dev.refusals()}");
}
fn copies_log() -> text {
lines([
"open 1 64x48 \"frame\"",
"pipeline 2 vertex 3 instance 4 2 block 2x1@0 size 4 textures 0",
" state shape 0 depth 0 blend 12 cull 1 into 1",
"texture 3 32x16 colour",
"target 4 colour 3 depth 0",
"buffer 5 floats 9",
"buffer 6 floats 24",
"index_buffer 7 count 3",
"begin 4 keep",
" viewport 0,0 32x16",
" bind 2",
" uniforms floats 4 first 32.000 last 0.000",
" vertices 5",
" instances 6 from 12",
" scissor 2,3 10x5",
" draw from 0 count 3 copies 2",
" scissor off",
" indices 7",
" draw from 0 count 3 indexed copies 0",
"end",
"buffer_update 6 floats 12",
])
}
fn test_copies_indices_a_target_and_a_scissor_arrive_too() {
dev = open();
// blend(ONE, ONE_MINUS_SRC_ALPHA) is 1 + 8 * 1 + 3
state = gpu::State { blend: gpu::blend(gpu::ONE, gpu::ONE_MINUS_SRC_ALPHA), cull: gpu::CULL_BACK, into: gpu::INTO_COLOUR };
pipe = dev.pipeline(copies(), state)!;
scene = dev.texture(32, 16);
target = dev.colour_target(scene);
mesh = dev.buffer([for _ in 0..9 { 0.0f }]);
per_copy = dev.buffer([for _ in 0..24 { 0.0f }]);
order = dev.index_buffer([0, 1, 2]);
u = pipe.block();
u.f2(0, 32.0, 16.0);
dev.begin(target, gpu::KEEP, 0);
dev.viewport(0, 0, 32, 16);
dev.bind(pipe);
dev.uniforms(u);
dev.vertices(mesh);
dev.instances(per_copy, 12);
dev.scissor(2, 3, 10, 5);
dev.draw_copies(3, 2);
dev.no_scissor();
dev.indices(order);
dev.draw_indexed(3);
dev.end();
per_copy.update([for _ in 0..12 { 0.0f }]);
assert(gpu_record::log() == copies_log(), "the record:\n{gpu_record::log()}");
assert(len(dev.refusals()) == 0, "nothing refused: {dev.refusals()}");
assert(per_copy.floats() == 12 && order.count() == 3, "the lengths follow the data");
}
// What the backend saw after `open`: nothing, when every call was refused.
fn after_open() -> text {
gpu_record::log()[len("open 1 64x48 \"frame\"\n")..]
}
fn test_a_call_that_breaks_the_rule_never_reaches_the_backend() {
dev = open();
mesh = dev.buffer([0.0f, 0.0f, 0.0f]);
atlas = dev.texture(8, 8);
pipe = dev.pipeline(view(), gpu::State {})!;
into_colour = dev.pipeline(view(), gpu::State { into: gpu::INTO_COLOUR })!;
other = dev.pipeline(copies(), gpu::State {})!;
u = pipe.block();
made = gpu_record::log();
dev.draw(0, 3); // outside a pass
dev.bind(pipe); // outside a pass
dev.end(); // no pass to end
dev.begin_window(gpu::KEEP, 0);
dev.begin_window(gpu::KEEP, 0); // a pass inside a pass
dev.present(); // inside a pass
dev.vertices(mesh); // no pipeline yet
dev.bind(into_colour); // built for a texture, this pass draws into the window
dev.bind(pipe);
dev.draw(0, 3); // no vertices
dev.vertices(mesh);
dev.draw(0, 3); // no block
dev.uniforms(other.block()); // another pipeline's block
dev.uniforms(u);
dev.draw(0, 3); // texture slot 0 empty
dev.sample(1, atlas); // the pipeline has one slot
dev.instances(mesh, 0); // the pipeline reads nothing per copy
dev.sample(0, atlas);
dev.draw_indexed(3); // no indices
u.f2(1, 1.0, 2.0); // field 1 is an F4
dev.uniforms(u); // a marked block
dev.bind(pipe); // a second bind forgets the first one's block
dev.vertices(mesh);
dev.sample(0, atlas);
dev.draw(0, 3); // no block since the bind
dev.end();
want = [
"draw: outside a pass", "bind: outside a pass", "end: outside a pass",
"begin: the pass before it has not ended", "present: a pass has not ended",
"vertices: no pipeline is bound in this pass",
"bind: the pipeline was built for another kind of target than this pass draws into",
"draw: no vertices were given since the pipeline was bound",
"draw: the pipeline's block was not given since it was bound",
"uniforms: the block is another pipeline's",
"draw: a texture slot of the pipeline was not given since it was bound",
"sample: the pipeline has no texture slot 1",
"instances: the pipeline declares no input per copy",
"draw_indexed: no indices were given since the pipeline was bound",
"uniforms: a field of the block was set as another kind than it was declared",
"draw: the pipeline's block was not given since it was bound",
];
got = dev.refusals();
assert(len(got) == len(want), "{len(want)} refusals, got {len(got)}: {got}");
for w in want { assert((got[w#index] ?? "") == w, "refusal {w#index}: {got[w#index] ?? ""}"); }
// Of the 26 calls only the pass, the binds and what was given to a bound pipeline arrived: no draw.
seen = gpu_record::log()[len(made)..];
expect = lines([
"begin 0 keep",
" bind 4",
" vertices 2",
" uniforms floats 28 first 0.000 last 0.000",
" sample 0 texture 3",
" bind 4",
" vertices 2",
" sample 0 texture 3",
"end",
]);
assert(seen == expect, "what arrived:\n{seen}");
}
fn make_and_drop(dev: const gpu::Device) -> integer {
mesh = dev.buffer([0.0f]);
order = dev.index_buffer([0]);
colour = dev.texture(4, 4);
depth = dev.depth_texture(4, 4);
target = dev.target(colour, depth);
pipe = dev.pipeline(copies(), gpu::State {})!;
// six objects are held while their owners live
held = gpu_record::live();
assert(mesh.floats() == 1 && order.count() == 1 && colour.width() == 4, "the objects are used");
held
}
fn test_every_object_is_released_when_its_owner_dies() {
dev = open();
held = make_and_drop(dev);
assert(held == 6, "six objects held inside, got {held}");
assert(gpu_record::live() == 0, "none held after their owners died, got {gpu_record::live()}");
assert(len(after_open()) > 0, "the backend saw them made and released");
}
fn test_an_object_outliving_its_device_dies_quietly() {
dev = open();
mesh = dev.buffer([0.0f]);
dev.close();
assert(gpu_record::live() == 0, "closing the device freed its buffer");
closed = gpu_record::log();
assert(closed.ends_with("close 1\n"), "the close is the last line:\n{closed}");
assert(mesh.floats() == 1, "the buffer's own record stays readable");
}
struct Pair { mesh: gpu::Buffer, image: gpu::Texture }
struct Kept { pair: Pair? }
// Objects held in a record a program empties later are released when it is emptied, also when
// that is after their device closed and a second one is open: the second keeps what it made.
fn test_objects_of_a_closed_device_let_go_beside_a_second_device() {
first = open();
kept = Kept { pair: Pair { mesh: first.buffer([0.0f]), image: first.texture(4, 4) } };
first.close();
assert(gpu_record::live() == 0, "closing the first device freed what it made");
second = gpu::open(gpu_record::table(), 64, 48, "second")!;
mine = second.buffer([1.0f, 2.0f]);
held = gpu_record::live();
kept.pair = null;
assert(gpu_record::live() == held, "letting the first device's objects go frees none of the second's: {gpu_record::live()} of {held}");
assert(mine.floats() == 2, "the second device's buffer is whole");
}
fn test_a_depth_written_without_a_test_reaches_the_backend_as_its_own_state() {
dev = open();
assert(dev.pipeline(copies(), gpu::State { depth: gpu::DEPTH_WRITE }) != null, "this backend takes the state");
assert(after_open().contains(" state shape 0 depth 3 blend 0 cull 0 into 0"), "the state arrives as 3:\n{after_open()}");
}
fn test_a_program_without_this_backends_source_builds_no_pipeline() {
dev = open();
p = gpu::Program { vertex: [gpu::F3], instance: [], block: [], textures: 0, sources: [gpu::source("other", "v", "f")] };
assert(dev.pipeline(p, gpu::State {}) == null, "no source tagged for this backend");
assert(after_open() == "", "the backend was not asked:\n{after_open()}");
}
fn test_a_block_with_a_three_float_field_builds_no_pipeline() {
dev = open();
p = gpu::Program { vertex: [gpu::F3], instance: [], block: [gpu::F3], textures: 0, sources: [gpu::source("record", "v", "f")] };
assert(dev.pipeline(p, gpu::State {}) == null, "a block takes no F3");
assert(after_open() == "", "the backend was not asked:\n{after_open()}");
}
fn test_a_backend_that_compiles_nothing_refuses_a_source_and_takes_a_blob() {
dev = open();
gpu_record::refuse_source(true);
assert(dev.pipeline(copies(), gpu::State {}) == null, "source text is refused in this mode");
ahead = gpu::Program { vertex: [gpu::F3], instance: [], block: [], textures: 0, sources: [gpu::source("record", "", "")] };
assert(dev.pipeline(ahead, gpu::State {}) != null, "a shader compiled ahead passes");
gpu_record::refuse_source(false);
}
fn test_no_table_opens_no_device() {
assert(gpu::open(0, 64, 48, "none") == null, "a backend whose stack is absent answers table 0");
}
fn test_input_a_small_machines_pixels_and_a_targets_pixels_reach_the_backend() {
dev = open();
// the recording backend has no keys, no pointer and no events: each answers nothing
assert(!dev.key_down(65) && dev.pointer_x() == 0 && dev.pointer_y() == 0 && dev.buttons() == 0, "no input");
assert(len(dev.typed()) == 0 && len(dev.events()) == 0, "no text, no events");
held = dev.keys();
down = 0;
for k in held { if k { down += 1; } }
assert(len(held) == 256 && down == 0, "256 flags, none down: {len(held)} {down}");
t = dev.texture(2, 1);
// 63488 is five bits of red (f800), 2016 six of green
t.put_rgb565(0, 0, [63488 as i32, 2016 as i32], 2, 1);
t.put_rgb565(0, 0, [63488 as i32], 2, 1); // too few values: not sent
target = dev.colour_target(t);
got = dev.capture_of(target);
assert(len(got) == 2, "a target's capture is its own size, got {len(got)}");
deep = dev.depth_target(dev.depth_texture(4, 4));
assert(len(dev.capture_of(deep)) == 0, "a target with no colour has no pixels to read");
whole = dev.capture();
assert(len(whole) == 64 * 48, "the window's capture is the window's size");
want = lines([
"texture 2 2x1 colour",
"texture_put565 2 at 0,0 2x1 first f800",
"target 3 colour 2 depth 0",
"capture 3 pixels 2",
"texture 4 4x4 depth",
"target 5 colour 0 depth 4",
"capture 0 pixels 3072",
]);
assert(after_open().starts_with(want), "what arrived:\n{after_open()}");
}
fn test_a_window_over_the_whole_screen_is_asked_for_as_one() {
gpu_record::reset();
dev = gpu::open_fullscreen(gpu_record::table(), "whole") ?? return assert(false, "no device");
// no size is asked: the backend gives the screen's
assert(gpu_record::log() == "open 1 0x0 \"whole\" fullscreen\n", "what the backend was asked:\n{gpu_record::log()}");
assert(dev.poll(), "it is open");
}
tests/width32.loft
#width 32
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// A 32-bit program (`#width 32`, as a small machine runs loft) draws through `gpu`. Here a
// plain `integer` holds 32 bits, and a backend's table is an address and a colour is
// 0xAARRGGBB, neither of which fits: both cross as `i64`, whole.
use gpu;
use gpu_record;
fn test_a_32_bit_program_opens_a_device_and_clears_to_its_colour() {
gpu_record::reset();
table = gpu_record::table();
assert(table != 0l, "the table arrives whole, not as null");
dev = gpu::open(table, 64, 48, "w32") ?? return assert(false, "no device from a 32-bit program");
mesh = dev.buffer([0.0, 0.0, 0.0]);
p = gpu::Program { vertex: [gpu::F3], instance: [], block: [gpu::F2], textures: 0, sources: [gpu::source("record", "", "")] };
pipe = dev.pipeline(p, gpu::State {}) ?? return assert(false, "no pipeline");
u = pipe.block();
u.f2(0, 3.0, 4.0);
dev.begin_window(gpu::CLEAR, 0xFF102030l);
dev.bind(pipe);
dev.uniforms(u);
dev.vertices(mesh);
dev.draw(0, 1);
dev.end();
log = gpu_record::log();
assert(log.ends_with("begin 0 clear ff102030\n bind 3\n uniforms floats 4 first 3.000 last 0.000\n vertices 2\n draw from 0 count 1 copies 0\nend\n"), "the frame, its colour whole:\n{log}");
assert(len(dev.refusals()) == 0, "nothing refused: {dev.refusals()}");
}