← gpu_vulkan · API reference · version 0.2.4
8 file(s), 1829 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 | 23 | — | 15 120 127 141 147 29 … (30 total) |
checked fn | 45 | — | 25 29 36 45 60 62 … (10 total) |
check fn | 51 | — | 84 91 |
faults fn | 56 | — | 37 39 64 69 77 87 … (7 total) |
check_catches fn | 63 | — | 39 48 88 |
src/gpu_vulkan.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// The Vulkan backend of `gpu`, on Linux.
//
// dev = gpu::open(gpu_vulkan::table(), 960, 720, "view") ?? return;
//
// A game does not name this library: `gpu_host` asks each backend in turn. A program's
// source for it carries the tag "spirv": a SPIR-V 1.3 module as base64 text, which
// gpu_vulkan/spirv.py writes from the program's "glsl-330" source, so a program is written once:
// - the inputs read per vertex are `location = 0` onwards, in the program's order, and the
// inputs read per copy follow them;
// - the uniform block is at set 0, binding 0, std140; a block laid out otherwise than the
// program declares builds no pipeline, and stderr shows both layouts;
// - texture slot n is a `sampler2D` at set 0, binding 1 + n;
// - a program that draws points writes `gl_PointSize`.
fn gv_table() -> integer;
#c "gv_table" "int64_t()"
// This backend's table, for `gpu::open`; 0 on a machine it has no window host for. `open`
// answers null where no Vulkan 1.3 driver draws into the window.
pub fn table() -> i64 {
gv_table()
}
fn gv_check(on: integer);
#c "gv_check" "void(int64_t)"
fn gv_faults() -> integer;
#c "gv_faults" "int64_t()"
fn gv_checked() -> integer;
#c "gv_checked" "int64_t()"
fn gv_check_self() -> integer;
#c "gv_check_self" "int64_t()"
// Whether this library was built checked (`c/checked.sh`) and its checks are on. The library
// as it is published is not, and pays nothing for the checks: `faults` is then always 0.
//
// Vulkan's validation layer (`VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`) sees a wrong
// call of Vulkan. A checked build sees what that layer cannot: a buffer, texture, target or
// pipeline of this backend used after its release, released twice, or used after its device
// closed. The call is refused, counted, and written to stderr with the call it came from.
// `GPU_VULKAN_DEBUG=abort` stops the program at the first fault, `GPU_VULKAN_DEBUG=0` turns
// the checks off.
pub fn checked() -> boolean {
gv_checked() != 0
}
// Turn a checked build's checks off, or on again. Ask before `table()`: a device opened
// through a table taken earlier keeps what that table did.
pub fn check(on: boolean) {
gv_check(if on { 1 } else { 0 });
}
// How many faults a checked build has seen in this program.
pub fn faults() -> integer {
gv_faults()
}
// For this library's own tests: release one buffer of the window opened last twice, then give
// it to a draw, as no program can through `gpu`, and answer how many faults that added — 2 in a
// checked build with its checks on (and not set to abort), 0 otherwise.
pub fn check_catches() -> integer {
gv_check_self()
}
tests/after_close.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// What a program may do with the objects of a window it closed: let them go late. A buffer, a
// texture, a target or a pipeline outlives its device whenever a program closes its window
// before the values that hold them die, and each is released then, into a backend whose
// device is gone, and perhaps beside a second device opened since. That release frees
// nothing twice and touches nothing of the second device.
// Without a display every test returns at once (`DISPLAY`, or `GRAPHICS_TEST_WINDOW=1` on a
// Mac).
use gpu;
use gpu_vulkan;
fn display() -> boolean {
gpu_vulkan::table() != 0 && must_draw((env_variable("DISPLAY") ?? "") != "")
}
// A run that says its tests must draw (`GRAPHICS_TEST_MUST_DRAW=1`: the library CI, on a runner
// it gave a display) fails here, where a test would otherwise return having drawn nothing.
fn must_draw(can: boolean) -> boolean {
assert(can || (env_variable("GRAPHICS_TEST_MUST_DRAW") ?? "") != "1", "this run's tests must draw (GRAPHICS_TEST_MUST_DRAW=1) and found no display to draw on");
can
}
const FLAT_V: text = `
#version 330 core
layout(location = 0) in vec3 pos;
layout(std140) uniform Block {{ vec4 color; vec4 at; }};
void main() {{ gl_Position = vec4(pos.xy * at.zw + at.xy, 0.0, 1.0); }}
`;
// SPIR-V of FLAT_V (vertex), written by gpu_vulkan/spirv.py: do not edit.
const FLAT_V_SPV: text = `AwIjBwADAQALAAgAKgAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABwAAAAAABAAAAG1haW4AAAAADQAAABIAAABHAAMACwAAAAIAAABIAAUACwAAAAAA
AAALAAAAAAAAAEgABQALAAAAAQAAAAsAAAABAAAASAAFAAsAAAACAAAACwAAAAMAAABIAAUACwAA
AAMAAAALAAAABAAAAEcABAASAAAAHgAAAAAAAABHAAMAFgAAAAIAAABIAAUAFgAAAAAAAAAjAAAA
AAAAAEgABQAWAAAAAQAAACMAAAAQAAAARwAEABgAAAAhAAAAAAAAAEcABAAYAAAAIgAAAAAAAAAT
AAIAAgAAACEAAwADAAAAAgAAABYAAwAGAAAAIAAAABcABAAHAAAABgAAAAQAAAAVAAQACAAAACAA
AAAAAAAAKwAEAAgAAAAJAAAAAQAAABwABAAKAAAABgAAAAkAAAAeAAYACwAAAAcAAAAGAAAACgAA
AAoAAAAgAAQADAAAAAMAAAALAAAAOwAEAAwAAAANAAAAAwAAABUABAAOAAAAIAAAAAEAAAArAAQA
DgAAAA8AAAAAAAAAFwAEABAAAAAGAAAAAwAAACAABAARAAAAAQAAABAAAAA7AAQAEQAAABIAAAAB
AAAAFwAEABMAAAAGAAAAAgAAAB4ABAAWAAAABwAAAAcAAAAgAAQAFwAAAAIAAAAWAAAAOwAEABcA
AAAYAAAAAgAAACsABAAOAAAAGQAAAAEAAAAgAAQAGgAAAAIAAAAHAAAAKwAEAAYAAAAjAAAAAAAA
ACsABAAGAAAAJAAAAAAAgD8gAAQAKAAAAAMAAAAHAAAANgAFAAIAAAAEAAAAAAAAAAMAAAD4AAIA
BQAAAD0ABAAQAAAAFAAAABIAAABPAAcAEwAAABUAAAAUAAAAFAAAAAAAAAABAAAAQQAFABoAAAAb
AAAAGAAAABkAAAA9AAQABwAAABwAAAAbAAAATwAHABMAAAAdAAAAHAAAABwAAAACAAAAAwAAAIUA
BQATAAAAHgAAABUAAAAdAAAAQQAFABoAAAAfAAAAGAAAABkAAAA9AAQABwAAACAAAAAfAAAATwAH
ABMAAAAhAAAAIAAAACAAAAAAAAAAAQAAAIEABQATAAAAIgAAAB4AAAAhAAAAUQAFAAYAAAAlAAAA
IgAAAAAAAABRAAUABgAAACYAAAAiAAAAAQAAAFAABwAHAAAAJwAAACUAAAAmAAAAIwAAACQAAABB
AAUAKAAAACkAAAANAAAADwAAAD4AAwApAAAAJwAAAP0AAQA4AAEA`;
const FLAT_F: text = `
#version 330 core
layout(std140) uniform Block {{ vec4 color; vec4 at; }};
out vec4 frag;
void main() {{ frag = color; }}
`;
// SPIR-V of FLAT_F (fragment), written by gpu_vulkan/spirv.py: do not edit.
const FLAT_F_SPV: text = `AwIjBwADAQALAAgAEgAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABgAEAAAABAAAAG1haW4AAAAACQAAABAAAwAEAAAABwAAAEcABAAJAAAAHgAAAAAA
AABHAAMACgAAAAIAAABIAAUACgAAAAAAAAAjAAAAAAAAAEgABQAKAAAAAQAAACMAAAAQAAAARwAE
AAwAAAAhAAAAAAAAAEcABAAMAAAAIgAAAAAAAAATAAIAAgAAACEAAwADAAAAAgAAABYAAwAGAAAA
IAAAABcABAAHAAAABgAAAAQAAAAgAAQACAAAAAMAAAAHAAAAOwAEAAgAAAAJAAAAAwAAAB4ABAAK
AAAABwAAAAcAAAAgAAQACwAAAAIAAAAKAAAAOwAEAAsAAAAMAAAAAgAAABUABAANAAAAIAAAAAEA
AAArAAQADQAAAA4AAAAAAAAAIAAEAA8AAAACAAAABwAAADYABQACAAAABAAAAAAAAAADAAAA+AAC
AAUAAABBAAUADwAAABAAAAAMAAAADgAAAD0ABAAHAAAAEQAAABAAAAA+AAMACQAAABEAAAD9AAEA
OAABAA==`;
fn flat() -> gpu::Program {
gpu::Program {
vertex: [gpu::F3],
instance: [],
block: [gpu::F4, gpu::F4],
textures: 0,
sources: [gpu::source("spirv", FLAT_V_SPV, FLAT_F_SPV)],
}
}
fn whole() -> vector<single> {
[-1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f]
}
// One of everything a device makes.
struct Kit {
mesh: gpu::Buffer,
order: gpu::IndexBuffer,
colour: gpu::Texture,
depth: gpu::Texture,
into: gpu::Target,
pipe: gpu::Pipeline,
}
struct Held { kit: Kit? }
fn kit_of(dev: const gpu::Device) -> Kit? {
colour = dev.texture(16, 16);
depth = dev.depth_texture(16, 16);
Kit {
mesh: dev.buffer(whole()),
order: dev.index_buffer([0, 1, 2, 3, 4, 5]),
into: dev.target(colour, depth),
colour: colour,
depth: depth,
pipe: dev.pipeline(flat(), gpu::State { depth: gpu::DEPTH_OFF }) ?? return null,
}
}
// The rectangle scaled by (sx, sy) about (cx, cy), in one colour.
fn fill(dev: gpu::Device, pipe: const gpu::Pipeline, mesh: const gpu::Buffer, r: float, g: float, b: float, cx: float, sx: float) {
u = pipe.block();
u.f4(0, r, g, b, 1.0);
u.f4(1, cx, 0.0, sx, 1.0);
dev.bind(pipe);
dev.uniforms(u);
dev.vertices(mesh);
dev.draw(0, 6);
}
// A device's objects are let go after it closed, with nothing else open.
fn test_objects_let_go_after_their_device_closed() {
devo = gpu::open(gpu_vulkan::table(), 64, 48, "closed-first");
if !display() { return; }
dev = devo ?? return assert(false, "no device");
held = Held { kit: kit_of(dev) };
assert(held.kit != null, "the kit was made");
dev.close();
held.kit = null;
again = gpu::open(gpu_vulkan::table(), 64, 48, "after") ?? return assert(false, "no second device");
mine = kit_of(again) ?? return assert(false, "no second kit");
again.begin_window(gpu::CLEAR, 0xFF0000FF);
fill(again, mine.pipe, mine.mesh, 0.0, 1.0, 0.0, 0.0, 1.0);
again.end();
shot = again.capture();
assert(len(again.refusals()) == 0, "nothing refused: {again.refusals()}");
assert(shot[24 * 64 + 32] == 0xFF00FF00, "a device opened after draws: {shot[24 * 64 + 32]:x}");
}
// A closed device's objects are let go while a SECOND device is open and holds objects of its
// own, made after the close: the late release frees none of them. The second device draws a
// frame before and a frame after, and the second frame reads as drawn.
fn test_objects_of_a_closed_device_let_go_beside_a_second_device() {
firsto = gpu::open(gpu_vulkan::table(), 64, 48, "first");
if !display() { return; }
first = firsto ?? return assert(false, "no device");
held = Held { kit: kit_of(first) };
assert(held.kit != null, "the first kit was made");
first.close();
second = gpu::open(gpu_vulkan::table(), 64, 48, "second") ?? return assert(false, "no second device");
mine = kit_of(second) ?? return assert(false, "no second kit");
second.begin_window(gpu::CLEAR, 0xFF0000FF);
fill(second, mine.pipe, mine.mesh, 0.0, 1.0, 0.0, 0.0, 1.0);
second.end();
before = second.capture();
held.kit = null;
// the left half red over the green frame, through the second device's own objects
second.begin_window(gpu::KEEP, 0);
fill(second, mine.pipe, mine.mesh, 1.0, 0.0, 0.0, -0.5, 0.5);
second.end();
// and into its own target, which the late release must not have taken either
second.begin(mine.into, gpu::CLEAR, 0xFF000000);
second.end();
after = second.capture();
assert(len(second.refusals()) == 0, "nothing refused: {second.refusals()}");
assert(before[24 * 64 + 16] == 0xFF00FF00, "before the release the frame is green: {before[24 * 64 + 16]:x}");
assert(after[24 * 64 + 16] == 0xFFFF0000, "after it the second device still draws, left: {after[24 * 64 + 16]:x}");
assert(after[24 * 64 + 48] == 0xFF00FF00, "and kept what it had drawn, right: {after[24 * 64 + 48]:x}");
}
tests/checked.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// The checked build (`c/checked.sh`): that it is off in the library as committed, and that a
// checked build sees what it is for — a handle of this backend used after its release, released
// twice, or used after its device closed, none of which Vulkan's validation layer can see. A
// check that never fired would read the same as a library without a fault.
// Without a display every test returns at once (`DISPLAY`).
use gpu;
use gpu_vulkan;
fn display() -> boolean {
must_draw((env_variable("DISPLAY") ?? "") != "")
}
// A run that says its tests must draw (`GRAPHICS_TEST_MUST_DRAW=1`: the library CI, on a runner
// it gave a display) fails here, where a test would otherwise return having drawn nothing.
fn must_draw(can: boolean) -> boolean {
assert(can || (env_variable("GRAPHICS_TEST_MUST_DRAW") ?? "") != "1", "this run's tests must draw (GRAPHICS_TEST_MUST_DRAW=1) and found no display to draw on");
can
}
// A build that stops at the first fault cannot count two of them.
fn counting() -> boolean {
gpu_vulkan::checked() && (env_variable("GPU_VULKAN_DEBUG") ?? "") != "abort"
}
fn test_an_unchecked_build_counts_nothing_and_a_checked_one_starts_at_none() {
devo = gpu::open(gpu_vulkan::table(), 64, 48, "checked-none");
if !display() { return; }
dev = devo ?? return assert(false, "no device");
mesh = dev.buffer([0.0f, 0.0f, 0.0f]);
assert(mesh.floats() == 3, "a buffer was made");
dev.begin_window(gpu::CLEAR, 0xFF00FF00);
dev.end();
if gpu_vulkan::checked() {
assert(gpu_vulkan::faults() == 0, "a program that keeps the rules has no fault: {gpu_vulkan::faults()}");
} else {
assert(gpu_vulkan::faults() == 0 && gpu_vulkan::check_catches() == 0, "an unchecked build counts no fault: {gpu_vulkan::faults()}");
}
}
// A buffer released twice and used after: two faults, and the program goes on.
fn test_a_checked_build_refuses_a_handle_used_after_its_release() {
devo = gpu::open(gpu_vulkan::table(), 64, 48, "checked-handle");
if !display() || !counting() { return; }
dev = devo ?? return assert(false, "no device");
caught = gpu_vulkan::check_catches();
assert(caught == 2, "a second release and a use after it are each a fault: {caught}");
dev.begin_window(gpu::CLEAR, 0xFF00FF00);
dev.end();
shot = dev.capture();
assert(shot[0] == 0xFF00FF00, "the device draws on after the refused calls: {shot[0]:x}");
}
// What a program CAN do through `gpu`: write a buffer after it closed the buffer's window.
// The library as it ships takes the call and does nothing; a checked build names it. Letting
// the buffer go after the close stays in order, and is no fault.
fn test_a_checked_build_names_a_buffer_written_after_its_device_closed() {
devo = gpu::open(gpu_vulkan::table(), 64, 48, "checked-closed");
// a build set to stop at the first fault is not handed one
if !display() || (gpu_vulkan::checked() && !counting()) { return; }
dev = devo ?? return assert(false, "no device");
before = gpu_vulkan::faults();
{
mesh = dev.buffer([0.0f, 0.0f, 0.0f]);
dev.close();
mesh.update([1.0f, 2.0f, 3.0f]);
after = gpu_vulkan::faults();
if counting() {
assert(after == before + 1, "the write after the close is one fault: {after} after {before}");
} else if !gpu_vulkan::checked() {
assert(after == 0, "an unchecked build takes the call in silence: {after}");
}
}
if counting() {
assert(gpu_vulkan::faults() == before + 1, "letting the buffer go after the close is no fault: {gpu_vulkan::faults()}");
}
}
// A checked build with its checks turned off from the program counts nothing more.
fn test_checks_turned_off_count_nothing() {
if !display() || !counting() { return; }
gpu_vulkan::check(false);
devo = gpu::open(gpu_vulkan::table(), 64, 48, "checked-off");
dev = devo ?? return assert(false, "no device");
before = gpu_vulkan::faults();
assert(!gpu_vulkan::checked() && gpu_vulkan::check_catches() == 0, "off: nothing is caught");
assert(gpu_vulkan::faults() == before, "and nothing is counted: {gpu_vulkan::faults()} after {before}");
dev.close();
gpu_vulkan::check(true);
assert(gpu_vulkan::checked(), "on again");
}
tests/host.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// What a host needs of a device beyond drawing, through Vulkan: a target's pixels read back, a
// small machine's RGB565 frame taken as a texture, and the window's keys and pointer. The same
// tests as gpu_gl's, the colours computed by hand. Without a display every test returns at once.
use gpu;
use gpu_vulkan;
// A display, on a machine this backend has a window host for (Linux).
fn display() -> boolean {
gpu_vulkan::table() != 0 && must_draw((env_variable("DISPLAY") ?? "") != "")
}
// A run that says its tests must draw (`GRAPHICS_TEST_MUST_DRAW=1`: the library CI, on a runner
// it gave a display) fails here, where a test would otherwise return having drawn nothing.
fn must_draw(can: boolean) -> boolean {
assert(can || (env_variable("GRAPHICS_TEST_MUST_DRAW") ?? "") != "1", "this run's tests must draw (GRAPHICS_TEST_MUST_DRAW=1) and found no display to draw on");
can
}
const SHOW_V: text = `
#version 330 core
layout(location = 0) in vec2 pos;
out vec2 v_uv;
void main() {{ v_uv = (pos + 1.0) / 2.0; gl_Position = vec4(pos, 0.0, 1.0); }}
`;
const SHOW_F: text = `
#version 330 core
uniform sampler2D tex0;
in vec2 v_uv;
out vec4 frag;
void main() {{ frag = texture(tex0, v_uv); }}
`;
// SPIR-V of SHOW_V (vertex), written by gpu_vulkan/spirv.py: do not edit.
const SHOW_V_SPV: text = `AwIjBwADAQALAAgAIwAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ACAAAAAAABAAAAG1haW4AAAAACQAAAAsAAAAZAAAARwAEAAkAAAAeAAAAAAAAAEcA
BAALAAAAHgAAAAAAAABHAAMAFwAAAAIAAABIAAUAFwAAAAAAAAALAAAAAAAAAEgABQAXAAAAAQAA
AAsAAAABAAAASAAFABcAAAACAAAACwAAAAMAAABIAAUAFwAAAAMAAAALAAAABAAAABMAAgACAAAA
IQADAAMAAAACAAAAFgADAAYAAAAgAAAAFwAEAAcAAAAGAAAAAgAAACAABAAIAAAAAwAAAAcAAAA7
AAQACAAAAAkAAAADAAAAIAAEAAoAAAABAAAABwAAADsABAAKAAAACwAAAAEAAAArAAQABgAAAA0A
AAAAAIA/KwAEAAYAAAAQAAAAAAAAQBcABAATAAAABgAAAAQAAAAVAAQAFAAAACAAAAAAAAAAKwAE
ABQAAAAVAAAAAQAAABwABAAWAAAABgAAABUAAAAeAAYAFwAAABMAAAAGAAAAFgAAABYAAAAgAAQA
GAAAAAMAAAAXAAAAOwAEABgAAAAZAAAAAwAAABUABAAaAAAAIAAAAAEAAAArAAQAGgAAABsAAAAA
AAAAKwAEAAYAAAAdAAAAAAAAACAABAAhAAAAAwAAABMAAAA2AAUAAgAAAAQAAAAAAAAAAwAAAPgA
AgAFAAAAPQAEAAcAAAAMAAAACwAAAFAABQAHAAAADgAAAA0AAAANAAAAgQAFAAcAAAAPAAAADAAA
AA4AAABQAAUABwAAABEAAAAQAAAAEAAAAIgABQAHAAAAEgAAAA8AAAARAAAAPgADAAkAAAASAAAA
PQAEAAcAAAAcAAAACwAAAFEABQAGAAAAHgAAABwAAAAAAAAAUQAFAAYAAAAfAAAAHAAAAAEAAABQ
AAcAEwAAACAAAAAeAAAAHwAAAB0AAAANAAAAQQAFACEAAAAiAAAAGQAAABsAAAA+AAMAIgAAACAA
AAD9AAEAOAABAA==`;
// SPIR-V of SHOW_F (fragment), written by gpu_vulkan/spirv.py: do not edit.
const SHOW_F_SPV: text = `AwIjBwADAQALAAgAFAAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABwAEAAAABAAAAG1haW4AAAAACQAAABEAAAAQAAMABAAAAAcAAABHAAQACQAAAB4A
AAAAAAAARwAEAA0AAAAhAAAAAQAAAEcABAANAAAAIgAAAAAAAABHAAQAEQAAAB4AAAAAAAAAEwAC
AAIAAAAhAAMAAwAAAAIAAAAWAAMABgAAACAAAAAXAAQABwAAAAYAAAAEAAAAIAAEAAgAAAADAAAA
BwAAADsABAAIAAAACQAAAAMAAAAZAAkACgAAAAYAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAb
AAMACwAAAAoAAAAgAAQADAAAAAAAAAALAAAAOwAEAAwAAAANAAAAAAAAABcABAAPAAAABgAAAAIA
AAAgAAQAEAAAAAEAAAAPAAAAOwAEABAAAAARAAAAAQAAADYABQACAAAABAAAAAAAAAADAAAA+AAC
AAUAAAA9AAQACwAAAA4AAAANAAAAPQAEAA8AAAASAAAAEQAAAFcABQAHAAAAEwAAAA4AAAASAAAA
PgADAAkAAAATAAAA/QABADgAAQA=`;
// A texture over the whole target: texture coordinate (0, 0) at clip (-1, -1).
fn show() -> gpu::Program {
gpu::Program {
vertex: [gpu::F2],
instance: [],
block: [],
textures: 1,
sources: [gpu::source("spirv", SHOW_V_SPV, SHOW_F_SPV)],
}
}
fn frame() -> vector<single> {
[-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f]
}
fn test_a_small_machines_frame_is_shown_in_its_colours() {
if !display() { return; }
dev = gpu::open(gpu_vulkan::table(), 64, 48, "host-565") ?? return assert(false, "no device");
pipe = dev.pipeline(show(), gpu::State {}) ?? return assert(false, "pipeline");
quad = dev.buffer(frame());
// Two by two. Red, green and blue at their brightest, and a mixed one: red 16 of 31,
// green 32 of 63, blue 8 of 31 is 16 * 2048 + 32 * 32 + 8 = 33800, which widens to
// (16 << 3 | 16 >> 2, 32 << 2 | 32 >> 4, 8 << 3 | 8 >> 2) = (132, 130, 66).
t = dev.texture(2, 2);
t.nearest(true);
t.put_rgb565(0, 0, [63488 as i32, 2016 as i32, 31 as i32, 33800 as i32], 2, 2);
dev.begin_window(gpu::CLEAR, 0xFF000000);
dev.bind(pipe);
dev.sample(0, t);
dev.vertices(quad);
dev.draw(0, 6);
dev.end();
c = dev.capture();
// The texture's first row is at the bottom of the frame (clip y = -1), and a capture's first
// row is the top: the first two values are in the lower half, the last two in the upper.
low_left = c[40 * 64 + 8] ?? 0;
low_right = c[40 * 64 + 56] ?? 0;
up_left = c[8 * 64 + 8] ?? 0;
up_right = c[8 * 64 + 56] ?? 0;
assert(low_left == 0xFFFF0000, "five bits of red are full red: {low_left:x}");
assert(low_right == 0xFF00FF00, "six of green full green: {low_right:x}");
assert(up_left == 0xFF0000FF, "five of blue full blue: {up_left:x}");
assert(up_right == 0xFF848242, "a mixed one widens by repeating its high bits: {up_right:x}");
}
fn test_a_targets_pixels_are_what_the_window_shows_of_it() {
if !display() { return; }
dev = gpu::open(gpu_vulkan::table(), 64, 48, "host-target") ?? return assert(false, "no device");
into_colour = dev.pipeline(show(), gpu::State { into: gpu::INTO_COLOUR }) ?? return assert(false, "pipeline");
into_window = dev.pipeline(show(), gpu::State {}) ?? return assert(false, "pipeline");
quad = dev.buffer(frame());
picture = dev.texture_from([0xFF102030 as u32, 0xFF405060 as u32, 0xFF708090 as u32, 0xFFA0B0C0 as u32], 2, 2);
picture.nearest(true);
// The picture drawn into a 64 x 48 target, then that target drawn over the window.
held = dev.texture(64, 48);
held.nearest(true);
target = dev.colour_target(held);
dev.begin(target, gpu::CLEAR, 0xFF000000);
dev.bind(into_colour);
dev.sample(0, picture);
dev.vertices(quad);
dev.draw(0, 6);
dev.end();
// A pass into the window in between: the target is read, not whatever was drawn into last.
dev.begin_window(gpu::CLEAR, 0xFF00FF00);
dev.end();
read = dev.capture_of(target);
dev.begin_window(gpu::CLEAR, 0xFFFFFFFF);
dev.bind(into_window);
dev.sample(0, held);
dev.vertices(quad);
dev.draw(0, 6);
dev.end();
seen = dev.capture();
assert(len(read) == 64 * 48 && len(seen) == len(read), "both are 64 x 48: {len(read)} and {len(seen)}");
differ = 0;
for p in read { if p != (seen[p#index] ?? 0) { differ += 1; } }
assert(differ == 0, "{differ} pixels of the target read back differ from the window showing it");
// by hand: the picture's first pixel is at the bottom left, so the capture's last row starts with it
assert((read[47 * 64] ?? 0) == 0xFF102030 && (read[63] ?? 0) == 0xFFA0B0C0, "the corners: {read[47 * 64] ?? 0:x} {read[63] ?? 0:x}");
}
// Begin a pass into a target of `colour` that is gone as this returns.
fn begin_into(dev: gpu::Device, colour: const gpu::Texture) {
gone = dev.colour_target(colour);
dev.begin(gone, gpu::CLEAR, 0xFF000000);
}
// A target that is dropped inside the pass drawing into it (one that leaves its scope before
// `end`) is drawn into until that pass ends. A second target made right after it takes the
// room the first one's record had, were that freed: the draws must not land in `other`.
fn test_a_target_dropped_inside_its_pass_is_drawn_into_until_the_pass_ends() {
if !display() { return; }
dev = gpu::open(gpu_vulkan::table(), 64, 48, "host-dropped") ?? return assert(false, "no device");
into_colour = dev.pipeline(show(), gpu::State { into: gpu::INTO_COLOUR }) ?? return assert(false, "pipeline");
quad = dev.buffer(frame());
picture = dev.texture_from([0xFF102030 as u32, 0xFF405060 as u32, 0xFF708090 as u32, 0xFFA0B0C0 as u32], 2, 2);
picture.nearest(true);
held = dev.texture(64, 48);
aside = dev.texture(64, 48);
wipe = dev.colour_target(aside);
dev.begin(wipe, gpu::CLEAR, 0xFF000000);
dev.end();
begin_into(dev, held);
other = dev.colour_target(aside);
// a texture written inside the pass: a backend that stops drawing for the copy takes the
// pass up again from what it holds of the target
picture.put(0, 0, [0xFF102030 as u32, 0xFF405060 as u32, 0xFF708090 as u32, 0xFFA0B0C0 as u32], 2, 2);
dev.bind(into_colour);
dev.sample(0, picture);
dev.vertices(quad);
dev.draw(0, 6);
dev.end();
// the device draws on after it: a pass into the window, and the two textures read back
dev.begin_window(gpu::CLEAR, 0xFF00FF00);
dev.end();
read = dev.capture_of(dev.colour_target(held));
beside = dev.capture_of(other);
assert(len(read) == 64 * 48, "64 x 48: {len(read)}");
// by hand: the picture's first pixel is at the bottom left, so the capture's last row starts with it
assert((read[47 * 64] ?? 0) == 0xFF102030 && (read[63] ?? 0) == 0xFFA0B0C0, "the corners of the dropped target's texture: {read[47 * 64] ?? 0:x} {read[63] ?? 0:x}");
assert((beside[47 * 64] ?? 0) == 0xFF000000 && (beside[63] ?? 0) == 0xFF000000, "the other target's texture stays as it was cleared: {beside[47 * 64] ?? 0:x} {beside[63] ?? 0:x}");
}
fn test_a_window_nobody_touches_holds_no_key_and_no_text() {
if !display() { return; }
dev = gpu::open(gpu_vulkan::table(), 64, 48, "host-input") ?? return assert(false, "no device");
_ = dev.poll();
assert(!dev.key_down(65), "no key is down");
assert(dev.buttons() == 0, "no button is held");
assert(len(dev.typed()) == 0, "nothing was typed");
assert(len(dev.events()) % gpu::EVENT_WORDS == 0, "events come whole");
}
tests/many_devices.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// Many windows, one after another, each the only one while it lives. Each device holds a
// connection to the display, and an X server serves 256 clients by default: 300 devices that
// were not let go would be refused long before the last. And an X server starts over when
// its last client leaves (unless it was started with `-noreset`, and a desktop always has
// other clients): a connection that arrives while it does is dropped, so a program that
// closes its only window and opens another meets a server that hangs up on it. The backend
// asks again, and every open here answers a device. On an idle machine a drop is rare (one
// open in a thousand under Xvfb); `gpu_host/parity/under_load.sh` runs this file with the
// machine loaded, where one open in three is dropped.
// Without a display the test returns at once.
use gpu;
use gpu_vulkan;
fn display() -> boolean {
gpu_vulkan::table() != 0 && must_draw((env_variable("DISPLAY") ?? "") != "")
}
// A run that says its tests must draw (`GRAPHICS_TEST_MUST_DRAW=1`: the library CI, on a runner
// it gave a display) fails here, where a test would otherwise return having drawn nothing.
fn must_draw(can: boolean) -> boolean {
assert(can || (env_variable("GRAPHICS_TEST_MUST_DRAW") ?? "") != "1", "this run's tests must draw (GRAPHICS_TEST_MUST_DRAW=1) and found no display to draw on");
can
}
// Every one of 300 devices opens, is its own size, and shows the colour it was cleared to: the
// i-th is 8 + i % 5 pixels wide and red i % 256.
fn test_every_device_in_a_long_row_opens() {
if !display() { return; }
// no device is kept beside the one in the loop: the server is left with no client between two
for i in 0..300 {
dev = gpu::open(gpu_vulkan::table(), 8 + i % 5, 8, "one of many") ?? return assert(false, "device {i} of 300 did not open");
dev.begin_window(gpu::CLEAR, 0xFF000000 + ((i % 256) << 16));
dev.end();
shot = dev.capture();
assert(dev.width() == 8 + i % 5 && len(shot) == (8 + i % 5) * 8, "device {i} is {dev.width()} wide, {len(shot)} pixels");
assert(shot[0] == 0xFF000000 + ((i % 256) << 16), "device {i} shows its own red: {shot[0]:x}");
}
}
tests/same_as_gl.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// The same scene drawn through `gpu` on OpenGL (`gpu_gl`) and on Vulkan gives the same pixels.
// Each test draws one scene from one program — its "glsl-330" source and the "spirv" that
// gpu_vulkan/spirv.py writes from it — once per backend, and compares the two captures whole;
// it also asks that the scene shows more than one colour, so that two empty frames do not pass.
//
// The two stacks are held to the same pixels except where `SAME` below says otherwise. A
// backend serves a whole program, so each scene is drawn in a function of its own, and every
// object of the first backend is gone before the second opens. gpu_gl is not a dependency of
// this package: it is the backend this one is measured against. Without a display every test
// returns at once; where one of the two backends opens no window the test says so and fails.
use gpu;
use gpu_gl;
use gpu_vulkan;
// A display, on a machine this backend has a window host for (Linux).
fn display() -> boolean {
gpu_vulkan::table() != 0 && must_draw((env_variable("DISPLAY") ?? "") != "")
}
// A run that says its tests must draw (`GRAPHICS_TEST_MUST_DRAW=1`: the library CI, on a runner
// it gave a display) fails here, where a test would otherwise return having drawn nothing.
fn must_draw(can: boolean) -> boolean {
assert(can || (env_variable("GRAPHICS_TEST_MUST_DRAW") ?? "") != "1", "this run's tests must draw (GRAPHICS_TEST_MUST_DRAW=1) and found no display to draw on");
can
}
const BLUE = 0xFF0000FF;
// ── the shaders, each written once ──
const FLAT_V: text = `
#version 330 core
layout(location = 0) in vec3 pos;
layout(std140) uniform Block {{ vec4 color; float z; }};
void main() {{ gl_Position = vec4(pos.xy, z, 1.0); }}
`;
const FLAT_F: text = `
#version 330 core
layout(std140) uniform Block {{ vec4 color; float z; }};
out vec4 frag;
void main() {{ frag = color; }}
`;
// SPIR-V of FLAT_V (vertex), written by gpu_vulkan/spirv.py: do not edit.
const FLAT_V_SPV: text = `AwIjBwADAQALAAgAIwAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABwAAAAAABAAAAG1haW4AAAAADQAAABIAAABHAAMACwAAAAIAAABIAAUACwAAAAAA
AAALAAAAAAAAAEgABQALAAAAAQAAAAsAAAABAAAASAAFAAsAAAACAAAACwAAAAMAAABIAAUACwAA
AAMAAAALAAAABAAAAEcABAASAAAAHgAAAAAAAABHAAMAFgAAAAIAAABIAAUAFgAAAAAAAAAjAAAA
AAAAAEgABQAWAAAAAQAAACMAAAAQAAAARwAEABgAAAAhAAAAAAAAAEcABAAYAAAAIgAAAAAAAAAT
AAIAAgAAACEAAwADAAAAAgAAABYAAwAGAAAAIAAAABcABAAHAAAABgAAAAQAAAAVAAQACAAAACAA
AAAAAAAAKwAEAAgAAAAJAAAAAQAAABwABAAKAAAABgAAAAkAAAAeAAYACwAAAAcAAAAGAAAACgAA
AAoAAAAgAAQADAAAAAMAAAALAAAAOwAEAAwAAAANAAAAAwAAABUABAAOAAAAIAAAAAEAAAArAAQA
DgAAAA8AAAAAAAAAFwAEABAAAAAGAAAAAwAAACAABAARAAAAAQAAABAAAAA7AAQAEQAAABIAAAAB
AAAAFwAEABMAAAAGAAAAAgAAAB4ABAAWAAAABwAAAAYAAAAgAAQAFwAAAAIAAAAWAAAAOwAEABcA
AAAYAAAAAgAAACsABAAOAAAAGQAAAAEAAAAgAAQAGgAAAAIAAAAGAAAAKwAEAAYAAAAdAAAAAACA
PyAABAAhAAAAAwAAAAcAAAA2AAUAAgAAAAQAAAAAAAAAAwAAAPgAAgAFAAAAPQAEABAAAAAUAAAA
EgAAAE8ABwATAAAAFQAAABQAAAAUAAAAAAAAAAEAAABBAAUAGgAAABsAAAAYAAAAGQAAAD0ABAAG
AAAAHAAAABsAAABRAAUABgAAAB4AAAAVAAAAAAAAAFEABQAGAAAAHwAAABUAAAABAAAAUAAHAAcA
AAAgAAAAHgAAAB8AAAAcAAAAHQAAAEEABQAhAAAAIgAAAA0AAAAPAAAAPgADACIAAAAgAAAA/QAB
ADgAAQA=`;
// SPIR-V of FLAT_F (fragment), written by gpu_vulkan/spirv.py: do not edit.
const FLAT_F_SPV: text = `AwIjBwADAQALAAgAEgAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABgAEAAAABAAAAG1haW4AAAAACQAAABAAAwAEAAAABwAAAEcABAAJAAAAHgAAAAAA
AABHAAMACgAAAAIAAABIAAUACgAAAAAAAAAjAAAAAAAAAEgABQAKAAAAAQAAACMAAAAQAAAARwAE
AAwAAAAhAAAAAAAAAEcABAAMAAAAIgAAAAAAAAATAAIAAgAAACEAAwADAAAAAgAAABYAAwAGAAAA
IAAAABcABAAHAAAABgAAAAQAAAAgAAQACAAAAAMAAAAHAAAAOwAEAAgAAAAJAAAAAwAAAB4ABAAK
AAAABwAAAAYAAAAgAAQACwAAAAIAAAAKAAAAOwAEAAsAAAAMAAAAAgAAABUABAANAAAAIAAAAAEA
AAArAAQADQAAAA4AAAAAAAAAIAAEAA8AAAACAAAABwAAADYABQACAAAABAAAAAAAAAADAAAA+AAC
AAUAAABBAAUADwAAABAAAAAMAAAADgAAAD0ABAAHAAAAEQAAABAAAAA+AAMACQAAABEAAAD9AAEA
OAABAA==`;
fn flat() -> gpu::Program {
gpu::Program {
vertex: [gpu::F3],
instance: [],
block: [gpu::F4, gpu::F1],
textures: 0,
sources: [gpu::source("glsl-330", FLAT_V, FLAT_F), gpu::source("spirv", FLAT_V_SPV, FLAT_F_SPV)],
}
}
// a texture over the frame: position, an unused normal, a texture coordinate
const TEX_V: text = `
#version 330 core
layout(location = 0) in vec3 pos;
layout(location = 2) in vec2 uv;
out vec2 v_uv;
void main() {{ v_uv = uv; gl_Position = vec4(pos, 1.0); }}
`;
const TEX_F: text = `
#version 330 core
uniform sampler2D tex0;
in vec2 v_uv;
out vec4 frag;
void main() {{ frag = texture(tex0, v_uv); }}
`;
// SPIR-V of TEX_V (vertex), written by gpu_vulkan/spirv.py: do not edit.
const TEX_V_SPV: text = `AwIjBwADAQALAAgAIQAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ACQAAAAAABAAAAG1haW4AAAAACQAAAAsAAAATAAAAGAAAAEcABAAJAAAAHgAAAAAA
AABHAAQACwAAAB4AAAACAAAARwADABEAAAACAAAASAAFABEAAAAAAAAACwAAAAAAAABIAAUAEQAA
AAEAAAALAAAAAQAAAEgABQARAAAAAgAAAAsAAAADAAAASAAFABEAAAADAAAACwAAAAQAAABHAAQA
GAAAAB4AAAAAAAAAEwACAAIAAAAhAAMAAwAAAAIAAAAWAAMABgAAACAAAAAXAAQABwAAAAYAAAAC
AAAAIAAEAAgAAAADAAAABwAAADsABAAIAAAACQAAAAMAAAAgAAQACgAAAAEAAAAHAAAAOwAEAAoA
AAALAAAAAQAAABcABAANAAAABgAAAAQAAAAVAAQADgAAACAAAAAAAAAAKwAEAA4AAAAPAAAAAQAA
ABwABAAQAAAABgAAAA8AAAAeAAYAEQAAAA0AAAAGAAAAEAAAABAAAAAgAAQAEgAAAAMAAAARAAAA
OwAEABIAAAATAAAAAwAAABUABAAUAAAAIAAAAAEAAAArAAQAFAAAABUAAAAAAAAAFwAEABYAAAAG
AAAAAwAAACAABAAXAAAAAQAAABYAAAA7AAQAFwAAABgAAAABAAAAKwAEAAYAAAAaAAAAAACAPyAA
BAAfAAAAAwAAAA0AAAA2AAUAAgAAAAQAAAAAAAAAAwAAAPgAAgAFAAAAPQAEAAcAAAAMAAAACwAA
AD4AAwAJAAAADAAAAD0ABAAWAAAAGQAAABgAAABRAAUABgAAABsAAAAZAAAAAAAAAFEABQAGAAAA
HAAAABkAAAABAAAAUQAFAAYAAAAdAAAAGQAAAAIAAABQAAcADQAAAB4AAAAbAAAAHAAAAB0AAAAa
AAAAQQAFAB8AAAAgAAAAEwAAABUAAAA+AAMAIAAAAB4AAAD9AAEAOAABAA==`;
// SPIR-V of TEX_F (fragment), written by gpu_vulkan/spirv.py: do not edit.
const TEX_F_SPV: text = `AwIjBwADAQALAAgAFAAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABwAEAAAABAAAAG1haW4AAAAACQAAABEAAAAQAAMABAAAAAcAAABHAAQACQAAAB4A
AAAAAAAARwAEAA0AAAAhAAAAAQAAAEcABAANAAAAIgAAAAAAAABHAAQAEQAAAB4AAAAAAAAAEwAC
AAIAAAAhAAMAAwAAAAIAAAAWAAMABgAAACAAAAAXAAQABwAAAAYAAAAEAAAAIAAEAAgAAAADAAAA
BwAAADsABAAIAAAACQAAAAMAAAAZAAkACgAAAAYAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAb
AAMACwAAAAoAAAAgAAQADAAAAAAAAAALAAAAOwAEAAwAAAANAAAAAAAAABcABAAPAAAABgAAAAIA
AAAgAAQAEAAAAAEAAAAPAAAAOwAEABAAAAARAAAAAQAAADYABQACAAAABAAAAAAAAAADAAAA+AAC
AAUAAAA9AAQACwAAAA4AAAANAAAAPQAEAA8AAAASAAAAEQAAAFcABQAHAAAAEwAAAA4AAAASAAAA
PgADAAkAAAATAAAA/QABADgAAQA=`;
fn textured() -> gpu::Program {
gpu::Program {
vertex: [gpu::F3, gpu::F3, gpu::F2],
instance: [],
block: [],
textures: 1,
sources: [gpu::source("glsl-330", TEX_V, TEX_F), gpu::source("spirv", TEX_V_SPV, TEX_F_SPV)],
}
}
// copies: a strip moved by a float read once per copy
const COPY_V: text = `
#version 330 core
layout(location = 0) in vec3 pos;
layout(location = 1) in float dx;
layout(std140) uniform Block {{ vec4 color; float z; }};
void main() {{ gl_Position = vec4(pos.x + dx, pos.y, 0.0, 1.0); }}
`;
// SPIR-V of COPY_V (vertex), written by gpu_vulkan/spirv.py: do not edit.
const COPY_V_SPV: text = `AwIjBwADAQALAAgAJAAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ACAAAAAAABAAAAG1haW4AAAAADQAAABIAAAAXAAAARwADAAsAAAACAAAASAAFAAsA
AAAAAAAACwAAAAAAAABIAAUACwAAAAEAAAALAAAAAQAAAEgABQALAAAAAgAAAAsAAAADAAAASAAF
AAsAAAADAAAACwAAAAQAAABHAAQAEgAAAB4AAAAAAAAARwAEABcAAAAeAAAAAQAAAEcAAwAhAAAA
AgAAAEgABQAhAAAAAAAAACMAAAAAAAAASAAFACEAAAABAAAAIwAAABAAAABHAAQAIwAAACEAAAAA
AAAARwAEACMAAAAiAAAAAAAAABMAAgACAAAAIQADAAMAAAACAAAAFgADAAYAAAAgAAAAFwAEAAcA
AAAGAAAABAAAABUABAAIAAAAIAAAAAAAAAArAAQACAAAAAkAAAABAAAAHAAEAAoAAAAGAAAACQAA
AB4ABgALAAAABwAAAAYAAAAKAAAACgAAACAABAAMAAAAAwAAAAsAAAA7AAQADAAAAA0AAAADAAAA
FQAEAA4AAAAgAAAAAQAAACsABAAOAAAADwAAAAAAAAAXAAQAEAAAAAYAAAADAAAAIAAEABEAAAAB
AAAAEAAAADsABAARAAAAEgAAAAEAAAArAAQACAAAABMAAAAAAAAAIAAEABQAAAABAAAABgAAADsA
BAAUAAAAFwAAAAEAAAArAAQABgAAABwAAAAAAAAAKwAEAAYAAAAdAAAAAACAPyAABAAfAAAAAwAA
AAcAAAAeAAQAIQAAAAcAAAAGAAAAIAAEACIAAAACAAAAIQAAADsABAAiAAAAIwAAAAIAAAA2AAUA
AgAAAAQAAAAAAAAAAwAAAPgAAgAFAAAAQQAFABQAAAAVAAAAEgAAABMAAAA9AAQABgAAABYAAAAV
AAAAPQAEAAYAAAAYAAAAFwAAAIEABQAGAAAAGQAAABYAAAAYAAAAQQAFABQAAAAaAAAAEgAAAAkA
AAA9AAQABgAAABsAAAAaAAAAUAAHAAcAAAAeAAAAGQAAABsAAAAcAAAAHQAAAEEABQAfAAAAIAAA
AA0AAAAPAAAAPgADACAAAAAeAAAA/QABADgAAQA=`;
fn copied() -> gpu::Program {
gpu::Program {
vertex: [gpu::F3],
instance: [gpu::F1],
block: [gpu::F4, gpu::F1],
textures: 0,
sources: [gpu::source("glsl-330", COPY_V, FLAT_F), gpu::source("spirv", COPY_V_SPV, FLAT_F_SPV)],
}
}
// a matrix, an array and a float in one block: each field where `gpu::layout` puts it
const MAT_V: text = `
#version 330 core
layout(location = 0) in vec3 pos;
layout(std140) uniform Block {{ mat4 m; vec4 tint[3]; float pick; }};
void main() {{ gl_Position = m * vec4(pos, 1.0); }}
`;
const MAT_F: text = `
#version 330 core
layout(std140) uniform Block {{ mat4 m; vec4 tint[3]; float pick; }};
out vec4 frag;
void main() {{ frag = tint[int(pick)]; }}
`;
// SPIR-V of MAT_V (vertex), written by gpu_vulkan/spirv.py: do not edit.
const MAT_V_SPV: text = `AwIjBwADAQALAAgAJQAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABwAAAAAABAAAAG1haW4AAAAADQAAABsAAABHAAMACwAAAAIAAABIAAUACwAAAAAA
AAALAAAAAAAAAEgABQALAAAAAQAAAAsAAAABAAAASAAFAAsAAAACAAAACwAAAAMAAABIAAUACwAA
AAMAAAALAAAABAAAAEcABAASAAAABgAAABAAAABHAAMAEwAAAAIAAABIAAQAEwAAAAAAAAAFAAAA
SAAFABMAAAAAAAAABwAAABAAAABIAAUAEwAAAAAAAAAjAAAAAAAAAEgABQATAAAAAQAAACMAAABA
AAAASAAFABMAAAACAAAAIwAAAHAAAABHAAQAFQAAACEAAAAAAAAARwAEABUAAAAiAAAAAAAAAEcA
BAAbAAAAHgAAAAAAAAATAAIAAgAAACEAAwADAAAAAgAAABYAAwAGAAAAIAAAABcABAAHAAAABgAA
AAQAAAAVAAQACAAAACAAAAAAAAAAKwAEAAgAAAAJAAAAAQAAABwABAAKAAAABgAAAAkAAAAeAAYA
CwAAAAcAAAAGAAAACgAAAAoAAAAgAAQADAAAAAMAAAALAAAAOwAEAAwAAAANAAAAAwAAABUABAAO
AAAAIAAAAAEAAAArAAQADgAAAA8AAAAAAAAAGAAEABAAAAAHAAAABAAAACsABAAIAAAAEQAAAAMA
AAAcAAQAEgAAAAcAAAARAAAAHgAFABMAAAAQAAAAEgAAAAYAAAAgAAQAFAAAAAIAAAATAAAAOwAE
ABQAAAAVAAAAAgAAACAABAAWAAAAAgAAABAAAAAXAAQAGQAAAAYAAAADAAAAIAAEABoAAAABAAAA
GQAAADsABAAaAAAAGwAAAAEAAAArAAQABgAAAB0AAAAAAIA/IAAEACMAAAADAAAABwAAADYABQAC
AAAABAAAAAAAAAADAAAA+AACAAUAAABBAAUAFgAAABcAAAAVAAAADwAAAD0ABAAQAAAAGAAAABcA
AAA9AAQAGQAAABwAAAAbAAAAUQAFAAYAAAAeAAAAHAAAAAAAAABRAAUABgAAAB8AAAAcAAAAAQAA
AFEABQAGAAAAIAAAABwAAAACAAAAUAAHAAcAAAAhAAAAHgAAAB8AAAAgAAAAHQAAAJEABQAHAAAA
IgAAABgAAAAhAAAAQQAFACMAAAAkAAAADQAAAA8AAAA+AAMAJAAAACIAAAD9AAEAOAABAA==`;
// SPIR-V of MAT_F (fragment), written by gpu_vulkan/spirv.py: do not edit.
const MAT_F_SPV: text = `AwIjBwADAQALAAgAGwAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABgAEAAAABAAAAG1haW4AAAAACQAAABAAAwAEAAAABwAAAEcABAAJAAAAHgAAAAAA
AABHAAQADQAAAAYAAAAQAAAARwADAA4AAAACAAAASAAEAA4AAAAAAAAABQAAAEgABQAOAAAAAAAA
AAcAAAAQAAAASAAFAA4AAAAAAAAAIwAAAAAAAABIAAUADgAAAAEAAAAjAAAAQAAAAEgABQAOAAAA
AgAAACMAAABwAAAARwAEABAAAAAhAAAAAAAAAEcABAAQAAAAIgAAAAAAAAATAAIAAgAAACEAAwAD
AAAAAgAAABYAAwAGAAAAIAAAABcABAAHAAAABgAAAAQAAAAgAAQACAAAAAMAAAAHAAAAOwAEAAgA
AAAJAAAAAwAAABgABAAKAAAABwAAAAQAAAAVAAQACwAAACAAAAAAAAAAKwAEAAsAAAAMAAAAAwAA
ABwABAANAAAABwAAAAwAAAAeAAUADgAAAAoAAAANAAAABgAAACAABAAPAAAAAgAAAA4AAAA7AAQA
DwAAABAAAAACAAAAFQAEABEAAAAgAAAAAQAAACsABAARAAAAEgAAAAEAAAArAAQAEQAAABMAAAAC
AAAAIAAEABQAAAACAAAABgAAACAABAAYAAAAAgAAAAcAAAA2AAUAAgAAAAQAAAAAAAAAAwAAAPgA
AgAFAAAAQQAFABQAAAAVAAAAEAAAABMAAAA9AAQABgAAABYAAAAVAAAAbgAEABEAAAAXAAAAFgAA
AEEABgAYAAAAGQAAABAAAAASAAAAFwAAAD0ABAAHAAAAGgAAABkAAAA+AAMACQAAABoAAAD9AAEA
OAABAA==`;
fn matrixed() -> gpu::Program {
gpu::Program {
vertex: [gpu::F3],
instance: [],
block: [gpu::MAT4, gpu::f4s(3), gpu::F1],
textures: 0,
sources: [gpu::source("glsl-330", MAT_V, MAT_F), gpu::source("spirv", MAT_V_SPV, MAT_F_SPV)],
}
}
// points, one pixel each: a program that draws points writes its size
const POINT_V: text = `
#version 330 core
layout(location = 0) in vec3 pos;
layout(std140) uniform Block {{ vec4 color; float z; }};
void main() {{ gl_Position = vec4(pos.xy, z, 1.0); gl_PointSize = 1.0; }}
`;
// SPIR-V of POINT_V (vertex), written by gpu_vulkan/spirv.py: do not edit.
const POINT_V_SPV: text = `AwIjBwADAQALAAgAJQAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABwAAAAAABAAAAG1haW4AAAAADQAAABIAAABHAAMACwAAAAIAAABIAAUACwAAAAAA
AAALAAAAAAAAAEgABQALAAAAAQAAAAsAAAABAAAASAAFAAsAAAACAAAACwAAAAMAAABIAAUACwAA
AAMAAAALAAAABAAAAEcABAASAAAAHgAAAAAAAABHAAMAFgAAAAIAAABIAAUAFgAAAAAAAAAjAAAA
AAAAAEgABQAWAAAAAQAAACMAAAAQAAAARwAEABgAAAAhAAAAAAAAAEcABAAYAAAAIgAAAAAAAAAT
AAIAAgAAACEAAwADAAAAAgAAABYAAwAGAAAAIAAAABcABAAHAAAABgAAAAQAAAAVAAQACAAAACAA
AAAAAAAAKwAEAAgAAAAJAAAAAQAAABwABAAKAAAABgAAAAkAAAAeAAYACwAAAAcAAAAGAAAACgAA
AAoAAAAgAAQADAAAAAMAAAALAAAAOwAEAAwAAAANAAAAAwAAABUABAAOAAAAIAAAAAEAAAArAAQA
DgAAAA8AAAAAAAAAFwAEABAAAAAGAAAAAwAAACAABAARAAAAAQAAABAAAAA7AAQAEQAAABIAAAAB
AAAAFwAEABMAAAAGAAAAAgAAAB4ABAAWAAAABwAAAAYAAAAgAAQAFwAAAAIAAAAWAAAAOwAEABcA
AAAYAAAAAgAAACsABAAOAAAAGQAAAAEAAAAgAAQAGgAAAAIAAAAGAAAAKwAEAAYAAAAdAAAAAACA
PyAABAAhAAAAAwAAAAcAAAAgAAQAIwAAAAMAAAAGAAAANgAFAAIAAAAEAAAAAAAAAAMAAAD4AAIA
BQAAAD0ABAAQAAAAFAAAABIAAABPAAcAEwAAABUAAAAUAAAAFAAAAAAAAAABAAAAQQAFABoAAAAb
AAAAGAAAABkAAAA9AAQABgAAABwAAAAbAAAAUQAFAAYAAAAeAAAAFQAAAAAAAABRAAUABgAAAB8A
AAAVAAAAAQAAAFAABwAHAAAAIAAAAB4AAAAfAAAAHAAAAB0AAABBAAUAIQAAACIAAAANAAAADwAA
AD4AAwAiAAAAIAAAAEEABQAjAAAAJAAAAA0AAAAZAAAAPgADACQAAAAdAAAA/QABADgAAQA=`;
fn pointed() -> gpu::Program {
gpu::Program {
vertex: [gpu::F3],
instance: [],
block: [gpu::F4, gpu::F1],
textures: 0,
sources: [gpu::source("glsl-330", POINT_V, FLAT_F), gpu::source("spirv", POINT_V_SPV, FLAT_F_SPV)],
}
}
// a depth texture shown as grey: the depth a pass wrote, read back as a shadow map is
const DEPTH_F: text = `
#version 330 core
uniform sampler2D tex0;
in vec2 v_uv;
out vec4 frag;
void main() {{ float d = texture(tex0, v_uv).r; frag = vec4(d, d * d, 1.0 - d, 1.0); }}
`;
// SPIR-V of DEPTH_F (fragment), written by gpu_vulkan/spirv.py: do not edit.
const DEPTH_F_SPV: text = `AwIjBwADAQALAAgAIQAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABwAEAAAABAAAAG1haW4AAAAAEAAAABgAAAAQAAMABAAAAAcAAABHAAQADAAAACEA
AAABAAAARwAEAAwAAAAiAAAAAAAAAEcABAAQAAAAHgAAAAAAAABHAAQAGAAAAB4AAAAAAAAAEwAC
AAIAAAAhAAMAAwAAAAIAAAAWAAMABgAAACAAAAAgAAQABwAAAAcAAAAGAAAAGQAJAAkAAAAGAAAA
AQAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAGwADAAoAAAAJAAAAIAAEAAsAAAAAAAAACgAAADsABAAL
AAAADAAAAAAAAAAXAAQADgAAAAYAAAACAAAAIAAEAA8AAAABAAAADgAAADsABAAPAAAAEAAAAAEA
AAAXAAQAEgAAAAYAAAAEAAAAFQAEABQAAAAgAAAAAAAAACsABAAUAAAAFQAAAAAAAAAgAAQAFwAA
AAMAAAASAAAAOwAEABcAAAAYAAAAAwAAACsABAAGAAAAHQAAAAAAgD82AAUAAgAAAAQAAAAAAAAA
AwAAAPgAAgAFAAAAOwAEAAcAAAAIAAAABwAAAD0ABAAKAAAADQAAAAwAAAA9AAQADgAAABEAAAAQ
AAAAVwAFABIAAAATAAAADQAAABEAAABRAAUABgAAABYAAAATAAAAAAAAAD4AAwAIAAAAFgAAAD0A
BAAGAAAAGQAAAAgAAAA9AAQABgAAABoAAAAIAAAAPQAEAAYAAAAbAAAACAAAAIUABQAGAAAAHAAA
ABoAAAAbAAAAPQAEAAYAAAAeAAAACAAAAIMABQAGAAAAHwAAAB0AAAAeAAAAUAAHABIAAAAgAAAA
GQAAABwAAAAfAAAAHQAAAD4AAwAYAAAAIAAAAP0AAQA4AAEA`;
fn depth_shown() -> gpu::Program {
gpu::Program {
vertex: [gpu::F3, gpu::F3, gpu::F2],
instance: [],
block: [],
textures: 1,
sources: [gpu::source("glsl-330", TEX_V, DEPTH_F), gpu::source("spirv", TEX_V_SPV, DEPTH_F_SPV)],
}
}
// ── geometry ──
// Two triangles over (x0, y0)-(x1, y1), counter-clockwise, or clockwise.
fn rect(x0: float, y0: float, x1: float, y1: float, clockwise: boolean) -> vector<single> {
ccw = [x0, y0, x1, y0, x1, y1, x0, y0, x1, y1, x0, y1];
cw = [x0, y0, x1, y1, x1, y0, x0, y0, x0, y1, x1, y1];
out: vector<single> = [];
for i in 0..6 {
x = if clockwise { cw[2 * i] } else { ccw[2 * i] };
y = if clockwise { cw[2 * i + 1] } else { ccw[2 * i + 1] };
out += [x as single, y as single, 0.0 as single];
}
out
}
// The whole frame with a normal nobody reads and a texture coordinate: 8 floats a vertex.
fn frame8() -> vector<single> {
xy = [-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0];
out: vector<single> = [];
for i in 0..6 {
x = xy[2 * i];
y = xy[2 * i + 1];
out += [x as single, y as single, 0.0f, 0.0f, 0.0f, 1.0f, ((x + 1.0) / 2.0) as single, ((y + 1.0) / 2.0) as single];
}
out
}
fn rgba(r: integer, g: integer, b: integer, a: integer) -> u32 {
((a * 16777216 + r * 65536 + g * 256 + b) & 4294967295) as u32
}
// A 4 x 4 picture, every pixel its own colour.
fn picture() -> vector<u32> {
out: vector<u32> = [];
for i in 0..16 {
out += [rgba(40 + 12 * i, 250 - 13 * i, 17 * (i % 5) + 30, 255)];
}
out
}
// A 64 x 64 checkerboard of 1-pixel squares in two colours, which only its smaller copies show
// as their mixture.
fn checker() -> vector<u32> {
out: vector<u32> = [];
for i in 0..64 * 64 {
out += [if (i % 64 + i / 64) % 2 == 0 { rgba(250, 30, 30, 255) } else { rgba(20, 40, 230, 255) }];
}
out
}
// ── the comparison ──
// How far two stacks may differ in a scene, and why: each exception names its source.
struct Same {
// pixels allowed to differ at all
pixels: integer,
// and by at most this much in any channel
channel: integer,
}
fn exact() -> Same {
Same { pixels: 0, channel: 0 }
}
// Between a texture's pixels: each stack weighs the two neighbours with its own precision.
// Measured: RADV against llvmpipe's OpenGL, 1005 of 3072 pixels by 1; lavapipe, none.
fn filter_weights() -> Same {
Same { pixels: 3072, channel: 1 }
}
// Where a line steps: OpenGL's diamond-exit rule and Vulkan's Bresenham lines choose the same
// pixels except where a step falls on a pixel's edge. Measured: lavapipe against llvmpipe's
// OpenGL, 4 pixels of the steep line; RADV, none.
fn line_steps() -> Same {
Same { pixels: 4, channel: 255 }
}
// A point within a hair of a pixel's edge (x 63.04 here): the stacks round it to either side.
// Measured: lavapipe against llvmpipe's OpenGL, the one point, 2 pixels; RADV, none.
fn point_edges() -> Same {
Same { pixels: 2, channel: 255 }
}
fn channel_gap(a: u32, b: u32) -> integer {
worst = 0;
for shift in [0, 8, 16, 24] {
x = ((a as integer) >> shift) & 255;
y = ((b as integer) >> shift) & 255;
gap = if x > y { x - y } else { y - x };
if gap > worst { worst = gap; }
}
worst
}
fn same(gl: const vector<u32>, vk: const vector<u32>, what: text, allowed: Same) {
assert(len(gl) > 0, "{what}: OpenGL drew nothing (no window, or no device)");
assert(len(vk) > 0, "{what}: Vulkan drew nothing (no window, or no device)");
assert(len(gl) == len(vk), "{what}: {len(gl)} pixels on OpenGL, {len(vk)} on Vulkan");
differ = 0;
worst = 0;
first = -1;
colours = 0;
for p in gl {
q = vk[p#index] ?? 0;
if p != q {
differ += 1;
if first < 0 { first = p#index; }
gap = channel_gap(p, q);
if gap > worst { worst = gap; }
}
if p != gl[0] { colours += 1; }
}
assert(colours > 0, "{what}: the scene shows one colour only");
assert(differ <= allowed.pixels && worst <= allowed.channel, "{what}: {differ} pixels differ, by up to {worst} in a channel, the first at {first} (x {first % 64}, y {first / 64}): {gl[first] ?? 0:x} on OpenGL, {vk[first] ?? 0:x} on Vulkan");
}
// The scene drawn by `scene` through the backend whose table is `table`; empty when no device
// opens. Every object the scene made is gone when it returns.
fn on(table: i64, title: text, scene: fn(gpu::Device) -> vector<u32>) -> vector<u32> {
dev = gpu::open(table, 64, 48, title) ?? return [];
out = scene(dev);
assert(len(dev.refusals()) == 0, "{title}: nothing refused: {dev.refusals()}");
out
}
fn both(what: text, scene: fn(gpu::Device) -> vector<u32>, allowed: Same) {
if !display() { return; }
gl = on(gpu_gl::table(), "gl-{what}", scene);
vk = on(gpu_vulkan::table(), "vk-{what}", scene);
same(gl, vk, what, allowed);
}
// ── the scenes ──
// Red behind, green in front on the left, blue further back hidden, then a half-transparent
// white strip blended over the lower half.
fn depth_and_blend(dev: gpu::Device) -> vector<u32> {
solid = dev.pipeline(flat(), gpu::State { depth: gpu::DEPTH_TEST_WRITE }) ?? return [];
over = dev.pipeline(flat(), gpu::State { blend: gpu::blend(gpu::SRC_ALPHA, gpu::ONE_MINUS_SRC_ALPHA) }) ?? return [];
b_left = dev.buffer(rect(-1.0, -1.0, 0.0, 1.0, false));
b_whole = dev.buffer(rect(-1.0, -1.0, 1.0, 1.0, false));
b_low = dev.buffer(rect(-1.0, -1.0, 1.0, 0.0, false));
u = solid.block();
dev.begin_window(gpu::CLEAR, BLUE);
dev.bind(solid);
u.f4(0, 1.0, 0.0, 0.0, 1.0);
u.f1(1, 0.5);
dev.uniforms(u);
dev.vertices(b_whole);
dev.draw(0, 6);
u.f4(0, 0.0, 1.0, 0.0, 1.0);
u.f1(1, -0.5);
dev.uniforms(u);
dev.vertices(b_left);
dev.draw(0, 6);
u.f4(0, 0.0, 0.0, 1.0, 1.0);
u.f1(1, 0.9);
dev.uniforms(u);
dev.vertices(b_whole);
dev.draw(0, 6);
dev.bind(over);
u.f4(0, 1.0, 1.0, 1.0, 0.4);
u.f1(1, 0.0);
dev.uniforms(u);
dev.vertices(b_low);
dev.draw(0, 6);
dev.end();
dev.capture()
}
fn test_depth_and_blend() {
both("depth-blend", depth_and_blend, exact());
}
// A near red frame everywhere; then, inside a scissor over the left half, a green frame at the
// far end that writes its depth untested, which is what a clear of part of a target does; then
// a yellow frame further than the red one, tested. It shows on the left only, where the depth
// was written over.
fn depth_written_untested(dev: gpu::Device) -> vector<u32> {
solid = dev.pipeline(flat(), gpu::State { depth: gpu::DEPTH_TEST_WRITE }) ?? return [];
wipe = dev.pipeline(flat(), gpu::State { depth: gpu::DEPTH_WRITE }) ?? return [];
b_whole = dev.buffer(rect(-1.0, -1.0, 1.0, 1.0, false));
u = solid.block();
dev.begin_window(gpu::CLEAR, BLUE);
dev.bind(solid);
u.f4(0, 1.0, 0.0, 0.0, 1.0);
u.f1(1, -0.5);
dev.uniforms(u);
dev.vertices(b_whole);
dev.draw(0, 6);
dev.bind(wipe);
dev.scissor(0, 0, 32, 48);
u.f4(0, 0.0, 1.0, 0.0, 1.0);
u.f1(1, 1.0);
dev.uniforms(u);
dev.vertices(b_whole);
dev.draw(0, 6);
dev.no_scissor();
dev.bind(solid);
u.f4(0, 1.0, 1.0, 0.0, 1.0);
u.f1(1, 0.5);
dev.uniforms(u);
dev.vertices(b_whole);
dev.draw(0, 6);
dev.end();
c = dev.capture();
assert(c[24 * 64 + 16] == 0xFFFFFF00, "left: the far frame shows where the depth was written over: {c[24 * 64 + 16]:x}");
assert(c[24 * 64 + 48] == 0xFFFF0000, "right: the near frame kept its depth: {c[24 * 64 + 48]:x}");
c
}
fn test_a_depth_written_without_a_test() {
both("depth-write", depth_written_untested, exact());
}
// A clockwise frame culled away; a green frame cut to a rectangle by the scissor; a red frame
// squeezed into a corner by the viewport; a counter-clockwise strip culled as a front face.
fn cull_scissor_viewport(dev: gpu::Device) -> vector<u32> {
culled = dev.pipeline(flat(), gpu::State { cull: gpu::CULL_BACK }) ?? return [];
fronts = dev.pipeline(flat(), gpu::State { cull: gpu::CULL_FRONT }) ?? return [];
plain = dev.pipeline(flat(), gpu::State {}) ?? return [];
b_whole = dev.buffer(rect(-1.0, -1.0, 1.0, 1.0, false));
b_turned = dev.buffer(rect(-1.0, -1.0, 1.0, 1.0, true));
b_strip = dev.buffer(rect(-1.0, 0.5, 1.0, 0.8, false));
b_strip_cw = dev.buffer(rect(-1.0, -0.8, 1.0, -0.5, true));
u = plain.block();
dev.begin_window(gpu::CLEAR, BLUE);
dev.bind(culled);
u.f4(0, 1.0, 1.0, 0.0, 1.0);
dev.uniforms(u);
dev.vertices(b_turned);
dev.draw(0, 6);
dev.bind(fronts);
u.f4(0, 1.0, 0.0, 1.0, 1.0);
dev.uniforms(u);
dev.vertices(b_strip);
dev.draw(0, 6);
dev.vertices(b_strip_cw);
dev.draw(0, 6);
dev.bind(plain);
dev.scissor(5, 7, 30, 20);
u.f4(0, 0.0, 1.0, 0.0, 1.0);
dev.uniforms(u);
dev.vertices(b_whole);
dev.draw(0, 6);
dev.no_scissor();
dev.viewport(40, 30, 16, 12);
u.f4(0, 1.0, 0.0, 0.0, 1.0);
dev.uniforms(u);
dev.draw(0, 6);
dev.end();
dev.capture()
}
fn test_cull_scissor_and_viewport() {
both("cull-scissor-viewport", cull_scissor_viewport, exact());
}
fn texture_nearest(dev: gpu::Device) -> vector<u32> {
pipe = dev.pipeline(textured(), gpu::State {}) ?? return [];
b = dev.buffer(frame8());
t = dev.texture_from(picture(), 4, 4);
t.nearest(true);
t.put(1, 2, [0xFFFFFFFF as u32, 0xFF000000 as u32], 2, 1);
dev.begin_window(gpu::CLEAR, BLUE);
dev.bind(pipe);
dev.sample(0, t);
dev.vertices(b);
dev.draw(0, 6);
dev.end();
dev.capture()
}
fn test_a_texture_put_and_sampled_nearest() {
both("texture-nearest", texture_nearest, exact());
}
// Between its pixels a texture is sampled from both neighbours, and at its edge from the edge
// pixel alone.
fn texture_linear(dev: gpu::Device) -> vector<u32> {
pipe = dev.pipeline(textured(), gpu::State {}) ?? return [];
b = dev.buffer(frame8());
t = dev.texture_from(picture(), 4, 4);
dev.begin_window(gpu::CLEAR, BLUE);
dev.bind(pipe);
dev.sample(0, t);
dev.vertices(b);
dev.draw(0, 6);
dev.end();
dev.capture()
}
fn test_a_texture_sampled_between_its_pixels() {
both("texture-linear", texture_linear, filter_weights());
}
// Three copies of a strip from an offset into the per-copy buffer, and a box from an index list.
fn copies_and_indices(dev: gpu::Device) -> vector<u32> {
many = dev.pipeline(copied(), gpu::State {}) ?? return [];
one = dev.pipeline(flat(), gpu::State {}) ?? return [];
b = dev.buffer(rect(-1.0, -1.0, -0.75, 1.0, false));
per_copy = dev.buffer([9.0f, 9.0f, 0.0f, 0.5f, 1.25f]);
b_box = dev.buffer([-0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.5f, 0.5f, 0.0f, -0.5f, 0.5f, 0.0f]);
list = dev.index_buffer([0, 1, 2, 0, 2, 3]);
u = many.block();
dev.begin_window(gpu::CLEAR, BLUE);
dev.bind(many);
u.f4(0, 0.0, 1.0, 0.0, 1.0);
dev.uniforms(u);
dev.vertices(b);
dev.instances(per_copy, 2);
dev.draw_copies(6, 3);
dev.bind(one);
u.f4(0, 1.0, 0.0, 0.0, 1.0);
dev.uniforms(u);
dev.vertices(b_box);
dev.indices(list);
dev.draw_indexed(6);
dev.end();
dev.capture()
}
fn test_copies_and_an_index_list() {
both("copies-indices", copies_and_indices, exact());
}
fn lines(dev: gpu::Device) -> vector<u32> {
pipe = dev.pipeline(flat(), gpu::State { shape: gpu::LINES }) ?? return [];
b = dev.buffer([-0.9f, -0.9f, 0.0f, 0.9f, 0.7f, 0.0f, -0.9f, 0.5f, 0.0f, 0.9f, 0.5f, 0.0f, -0.6f, -0.9f, 0.0f, -0.4f, 0.9f, 0.0f]);
u = pipe.block();
u.f4(0, 1.0, 1.0, 0.0, 1.0);
dev.begin_window(gpu::CLEAR, BLUE);
dev.bind(pipe);
dev.uniforms(u);
dev.vertices(b);
dev.draw(0, 6);
dev.end();
dev.capture()
}
fn test_lines() {
both("lines", lines, line_steps());
}
fn points(dev: gpu::Device) -> vector<u32> {
pipe = dev.pipeline(pointed(), gpu::State { shape: gpu::POINTS }) ?? return [];
b = dev.buffer([-0.9f, -0.9f, 0.0f, 0.1f, 0.2f, 0.0f, 0.5f, -0.3f, 0.0f, 0.97f, 0.97f, 0.0f]);
u = pipe.block();
u.f4(0, 1.0, 1.0, 1.0, 1.0);
dev.begin_window(gpu::CLEAR, BLUE);
dev.bind(pipe);
dev.uniforms(u);
dev.vertices(b);
dev.draw(0, 4);
dev.end();
dev.capture()
}
fn test_points() {
both("points", points, point_edges());
}
// Into a 32 x 24 colour and depth target — green on the left in front of red below — then the
// target shown over the window.
fn into_a_target(dev: gpu::Device) -> vector<u32> {
inner = dev.pipeline(flat(), gpu::State { depth: gpu::DEPTH_TEST_WRITE, into: gpu::INTO_COLOUR_DEPTH }) ?? return [];
outer = dev.pipeline(textured(), gpu::State {}) ?? return [];
b_left = dev.buffer(rect(-1.0, -1.0, 0.0, 1.0, false));
b_low = dev.buffer(rect(-1.0, -1.0, 1.0, -0.25, false));
b_frame = dev.buffer(frame8());
colour = dev.texture(32, 24);
colour.nearest(true);
depth = dev.depth_texture(32, 24);
target = dev.target(colour, depth);
u = inner.block();
dev.begin(target, gpu::CLEAR, 0xFF202020);
dev.bind(inner);
u.f4(0, 0.0, 1.0, 0.0, 1.0);
u.f1(1, -0.5);
dev.uniforms(u);
dev.vertices(b_left);
dev.draw(0, 6);
u.f4(0, 1.0, 0.0, 0.0, 1.0);
u.f1(1, 0.5);
dev.uniforms(u);
dev.vertices(b_low);
dev.draw(0, 6);
dev.end();
dev.begin_window(gpu::CLEAR, BLUE);
dev.bind(outer);
dev.sample(0, colour);
dev.vertices(b_frame);
dev.draw(0, 6);
dev.end();
dev.capture()
}
fn test_a_scene_drawn_into_a_texture_first() {
both("target", into_a_target, exact());
}
// A target's colour read back without being shown: its first row at the bottom, as drawn.
fn target_read_back(dev: gpu::Device) -> vector<u32> {
inner = dev.pipeline(flat(), gpu::State { into: gpu::INTO_COLOUR, blend: gpu::blend(gpu::SRC_ALPHA, gpu::ONE_MINUS_SRC_ALPHA) }) ?? return [];
b = dev.buffer(rect(-1.0, -1.0, 0.25, -0.5, false));
colour = dev.texture(64, 48);
target = dev.colour_target(colour);
u = inner.block();
dev.begin(target, gpu::CLEAR, 0x80402010);
dev.bind(inner);
u.f4(0, 0.0, 1.0, 0.5, 0.75);
dev.uniforms(u);
dev.vertices(b);
dev.draw(0, 6);
dev.end();
dev.capture_of(target)
}
fn test_a_targets_colour_and_alpha_read_back() {
both("target-read", target_read_back, exact());
}
// The depth a pass wrote, sampled as a texture: a shadow map's path.
fn depth_sampled(dev: gpu::Device) -> vector<u32> {
inner = dev.pipeline(flat(), gpu::State { depth: gpu::DEPTH_TEST_WRITE, into: gpu::INTO_DEPTH }) ?? return [];
outer = dev.pipeline(depth_shown(), gpu::State {}) ?? return [];
b_left = dev.buffer(rect(-1.0, -1.0, 0.0, 1.0, false));
b_low = dev.buffer(rect(-1.0, -1.0, 1.0, -0.25, false));
b_frame = dev.buffer(frame8());
depth = dev.depth_texture(32, 24);
target = dev.depth_target(depth);
u = inner.block();
dev.begin(target, gpu::CLEAR, 0);
dev.bind(inner);
u.f4(0, 1.0, 1.0, 1.0, 1.0);
u.f1(1, -0.5);
dev.uniforms(u);
dev.vertices(b_left);
dev.draw(0, 6);
u.f1(1, 0.25);
dev.uniforms(u);
dev.vertices(b_low);
dev.draw(0, 6);
dev.end();
dev.begin_window(gpu::CLEAR, BLUE);
dev.bind(outer);
dev.sample(0, depth);
dev.vertices(b_frame);
dev.draw(0, 6);
dev.end();
dev.capture()
}
fn test_a_depth_target_sampled_as_a_texture() {
both("depth-sampled", depth_sampled, exact());
}
// A matrix, an array of vectors and a float in one block: a rectangle turned a quarter and
// moved, coloured by the array entry the float picks.
fn matrix_block(dev: gpu::Device) -> vector<u32> {
pipe = dev.pipeline(matrixed(), gpu::State {}) ?? return [];
b = dev.buffer(rect(-0.5, -0.25, 0.5, 0.25, false));
u = pipe.block();
// column-major: x' = -y * 0.8 + 0.3, y' = x * 0.8 - 0.1
u.mat4(0, [0.0, 0.8, 0.0, 0.0, -0.8, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.3, -0.1, 0.0, 1.0]);
u.f4_at(1, 0, 1.0, 0.0, 0.0, 1.0);
u.f4_at(1, 1, 0.0, 1.0, 0.0, 1.0);
u.f4_at(1, 2, 1.0, 0.5, 0.0, 1.0);
u.f1(2, 2.0);
dev.begin_window(gpu::CLEAR, BLUE);
dev.bind(pipe);
dev.uniforms(u);
dev.vertices(b);
dev.draw(0, 6);
dev.end();
dev.capture()
}
fn test_a_matrix_and_an_array_in_the_block() {
both("matrix-block", matrix_block, exact());
}
// A buffer, a texture and the block rewritten between two draws of one pass: each draw reads
// what was there when it was drawn.
fn rewritten_mid_pass(dev: gpu::Device) -> vector<u32> {
pipe = dev.pipeline(flat(), gpu::State {}) ?? return [];
show = dev.pipeline(textured(), gpu::State { blend: gpu::blend(gpu::ONE, gpu::ONE) }) ?? return [];
b = dev.buffer(rect(-1.0, -1.0, 0.0, 0.0, false));
half = dev.buffer(frame8());
t = dev.texture_from([0xFF400000 as u32], 1, 1);
u = pipe.block();
dev.begin_window(gpu::CLEAR, BLUE);
dev.bind(pipe);
u.f4(0, 1.0, 0.0, 0.0, 1.0);
dev.uniforms(u);
dev.vertices(b);
dev.draw(0, 6);
b.update(rect(0.0, 0.0, 1.0, 1.0, false));
u.f4(0, 0.0, 1.0, 0.0, 1.0);
dev.uniforms(u);
dev.draw(0, 6);
dev.bind(show);
dev.sample(0, t);
dev.vertices(half);
dev.draw(0, 6);
t.put(0, 0, [0xFF000040 as u32], 1, 1);
dev.bind(show);
dev.sample(0, t);
dev.vertices(half);
dev.draw(0, 6);
dev.end();
dev.capture()
}
fn test_what_is_rewritten_mid_pass_is_read_in_order() {
both("rewritten", rewritten_mid_pass, exact());
}
// A checkerboard drawn small: through its smaller copies it shows their mixture.
fn mipmapped(dev: gpu::Device) -> vector<u32> {
pipe = dev.pipeline(textured(), gpu::State {}) ?? return [];
b = dev.buffer(frame8());
t = dev.texture_from(checker(), 64, 64);
t.mipmaps();
dev.begin_window(gpu::CLEAR, BLUE);
dev.bind(pipe);
dev.sample(0, t);
dev.vertices(b);
dev.viewport(4, 4, 16, 8);
dev.draw(0, 6);
dev.end();
dev.capture()
}
fn test_mipmaps() {
both("mipmaps", mipmapped, exact());
}
// A frame drawn twice, presented between: a window keeps its pixels and its alpha at 1.
fn presented(dev: gpu::Device) -> vector<u32> {
pipe = dev.pipeline(flat(), gpu::State { blend: gpu::blend(gpu::DST_ALPHA, gpu::ONE_MINUS_DST_ALPHA) }) ?? return [];
b = dev.buffer(rect(-0.5, -0.5, 0.5, 0.5, false));
u = pipe.block();
for frame in 0..3 {
_ = dev.poll();
dev.begin_window(gpu::CLEAR, 0x00102030);
dev.bind(pipe);
u.f4(0, 0.25 * frame as float, 1.0, 0.5, 0.0);
dev.uniforms(u);
dev.vertices(b);
dev.draw(0, 6);
dev.end();
if frame < 2 { dev.present(); }
}
dev.capture()
}
fn test_a_presented_window_with_its_alpha_held_at_one() {
both("presented", presented, exact());
}
// ── a shader that does not fit its program builds no pipeline ──
const WRONG_V: text = `
#version 330 core
layout(location = 0) in vec3 pos;
layout(std140) uniform Block {{ vec3 tint; float z; }};
void main() {{ gl_Position = vec4(pos.xy + tint.xy, z, 1.0); }}
`;
const PLAIN_F: text = `
#version 330 core
out vec4 frag;
void main() {{ frag = vec4(1.0); }}
`;
const SHORT_V: text = `
#version 330 core
layout(location = 0) in vec3 pos;
layout(std140) uniform Block {{ vec4 color; }};
void main() {{ gl_Position = vec4(pos.xy + color.xy, 0.0, 1.0); }}
`;
const FAR_V: text = `
#version 330 core
layout(location = 0) in vec3 pos;
layout(location = 3) in vec2 uv;
void main() {{ gl_Position = vec4(pos.xy + uv, 0.0, 1.0); }}
`;
// SPIR-V of WRONG_V (vertex), written by gpu_vulkan/spirv.py: do not edit.
const WRONG_V_SPV: text = `AwIjBwADAQALAAgAKAAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABwAAAAAABAAAAG1haW4AAAAADQAAABIAAABHAAMACwAAAAIAAABIAAUACwAAAAAA
AAALAAAAAAAAAEgABQALAAAAAQAAAAsAAAABAAAASAAFAAsAAAACAAAACwAAAAMAAABIAAUACwAA
AAMAAAALAAAABAAAAEcABAASAAAAHgAAAAAAAABHAAMAFgAAAAIAAABIAAUAFgAAAAAAAAAjAAAA
AAAAAEgABQAWAAAAAQAAACMAAAAMAAAARwAEABgAAAAhAAAAAAAAAEcABAAYAAAAIgAAAAAAAAAT
AAIAAgAAACEAAwADAAAAAgAAABYAAwAGAAAAIAAAABcABAAHAAAABgAAAAQAAAAVAAQACAAAACAA
AAAAAAAAKwAEAAgAAAAJAAAAAQAAABwABAAKAAAABgAAAAkAAAAeAAYACwAAAAcAAAAGAAAACgAA
AAoAAAAgAAQADAAAAAMAAAALAAAAOwAEAAwAAAANAAAAAwAAABUABAAOAAAAIAAAAAEAAAArAAQA
DgAAAA8AAAAAAAAAFwAEABAAAAAGAAAAAwAAACAABAARAAAAAQAAABAAAAA7AAQAEQAAABIAAAAB
AAAAFwAEABMAAAAGAAAAAgAAAB4ABAAWAAAAEAAAAAYAAAAgAAQAFwAAAAIAAAAWAAAAOwAEABcA
AAAYAAAAAgAAACAABAAZAAAAAgAAABAAAAArAAQADgAAAB4AAAABAAAAIAAEAB8AAAACAAAABgAA
ACsABAAGAAAAIgAAAAAAgD8gAAQAJgAAAAMAAAAHAAAANgAFAAIAAAAEAAAAAAAAAAMAAAD4AAIA
BQAAAD0ABAAQAAAAFAAAABIAAABPAAcAEwAAABUAAAAUAAAAFAAAAAAAAAABAAAAQQAFABkAAAAa
AAAAGAAAAA8AAAA9AAQAEAAAABsAAAAaAAAATwAHABMAAAAcAAAAGwAAABsAAAAAAAAAAQAAAIEA
BQATAAAAHQAAABUAAAAcAAAAQQAFAB8AAAAgAAAAGAAAAB4AAAA9AAQABgAAACEAAAAgAAAAUQAF
AAYAAAAjAAAAHQAAAAAAAABRAAUABgAAACQAAAAdAAAAAQAAAFAABwAHAAAAJQAAACMAAAAkAAAA
IQAAACIAAABBAAUAJgAAACcAAAANAAAADwAAAD4AAwAnAAAAJQAAAP0AAQA4AAEA`;
// SPIR-V of PLAIN_F (fragment), written by gpu_vulkan/spirv.py: do not edit.
const PLAIN_F_SPV: text = `AwIjBwADAQALAAgADAAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABgAEAAAABAAAAG1haW4AAAAACQAAABAAAwAEAAAABwAAAEcABAAJAAAAHgAAAAAA
AAATAAIAAgAAACEAAwADAAAAAgAAABYAAwAGAAAAIAAAABcABAAHAAAABgAAAAQAAAAgAAQACAAA
AAMAAAAHAAAAOwAEAAgAAAAJAAAAAwAAACsABAAGAAAACgAAAAAAgD8sAAcABwAAAAsAAAAKAAAA
CgAAAAoAAAAKAAAANgAFAAIAAAAEAAAAAAAAAAMAAAD4AAIABQAAAD4AAwAJAAAACwAAAP0AAQA4
AAEA`;
// SPIR-V of SHORT_V (vertex), written by gpu_vulkan/spirv.py: do not edit.
const SHORT_V_SPV: text = `AwIjBwADAQALAAgAJQAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABwAAAAAABAAAAG1haW4AAAAADQAAABIAAABHAAMACwAAAAIAAABIAAUACwAAAAAA
AAALAAAAAAAAAEgABQALAAAAAQAAAAsAAAABAAAASAAFAAsAAAACAAAACwAAAAMAAABIAAUACwAA
AAMAAAALAAAABAAAAEcABAASAAAAHgAAAAAAAABHAAMAFgAAAAIAAABIAAUAFgAAAAAAAAAjAAAA
AAAAAEcABAAYAAAAIQAAAAAAAABHAAQAGAAAACIAAAAAAAAAEwACAAIAAAAhAAMAAwAAAAIAAAAW
AAMABgAAACAAAAAXAAQABwAAAAYAAAAEAAAAFQAEAAgAAAAgAAAAAAAAACsABAAIAAAACQAAAAEA
AAAcAAQACgAAAAYAAAAJAAAAHgAGAAsAAAAHAAAABgAAAAoAAAAKAAAAIAAEAAwAAAADAAAACwAA
ADsABAAMAAAADQAAAAMAAAAVAAQADgAAACAAAAABAAAAKwAEAA4AAAAPAAAAAAAAABcABAAQAAAA
BgAAAAMAAAAgAAQAEQAAAAEAAAAQAAAAOwAEABEAAAASAAAAAQAAABcABAATAAAABgAAAAIAAAAe
AAMAFgAAAAcAAAAgAAQAFwAAAAIAAAAWAAAAOwAEABcAAAAYAAAAAgAAACAABAAZAAAAAgAAAAcA
AAArAAQABgAAAB4AAAAAAAAAKwAEAAYAAAAfAAAAAACAPyAABAAjAAAAAwAAAAcAAAA2AAUAAgAA
AAQAAAAAAAAAAwAAAPgAAgAFAAAAPQAEABAAAAAUAAAAEgAAAE8ABwATAAAAFQAAABQAAAAUAAAA
AAAAAAEAAABBAAUAGQAAABoAAAAYAAAADwAAAD0ABAAHAAAAGwAAABoAAABPAAcAEwAAABwAAAAb
AAAAGwAAAAAAAAABAAAAgQAFABMAAAAdAAAAFQAAABwAAABRAAUABgAAACAAAAAdAAAAAAAAAFEA
BQAGAAAAIQAAAB0AAAABAAAAUAAHAAcAAAAiAAAAIAAAACEAAAAeAAAAHwAAAEEABQAjAAAAJAAA
AA0AAAAPAAAAPgADACQAAAAiAAAA/QABADgAAQA=`;
// SPIR-V of FAR_V (vertex), written by gpu_vulkan/spirv.py: do not edit.
const FAR_V_SPV: text = `AwIjBwADAQALAAgAIQAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ACAAAAAAABAAAAG1haW4AAAAADQAAABIAAAAXAAAARwADAAsAAAACAAAASAAFAAsA
AAAAAAAACwAAAAAAAABIAAUACwAAAAEAAAALAAAAAQAAAEgABQALAAAAAgAAAAsAAAADAAAASAAF
AAsAAAADAAAACwAAAAQAAABHAAQAEgAAAB4AAAAAAAAARwAEABcAAAAeAAAAAwAAABMAAgACAAAA
IQADAAMAAAACAAAAFgADAAYAAAAgAAAAFwAEAAcAAAAGAAAABAAAABUABAAIAAAAIAAAAAAAAAAr
AAQACAAAAAkAAAABAAAAHAAEAAoAAAAGAAAACQAAAB4ABgALAAAABwAAAAYAAAAKAAAACgAAACAA
BAAMAAAAAwAAAAsAAAA7AAQADAAAAA0AAAADAAAAFQAEAA4AAAAgAAAAAQAAACsABAAOAAAADwAA
AAAAAAAXAAQAEAAAAAYAAAADAAAAIAAEABEAAAABAAAAEAAAADsABAARAAAAEgAAAAEAAAAXAAQA
EwAAAAYAAAACAAAAIAAEABYAAAABAAAAEwAAADsABAAWAAAAFwAAAAEAAAArAAQABgAAABoAAAAA
AAAAKwAEAAYAAAAbAAAAAACAPyAABAAfAAAAAwAAAAcAAAA2AAUAAgAAAAQAAAAAAAAAAwAAAPgA
AgAFAAAAPQAEABAAAAAUAAAAEgAAAE8ABwATAAAAFQAAABQAAAAUAAAAAAAAAAEAAAA9AAQAEwAA
ABgAAAAXAAAAgQAFABMAAAAZAAAAFQAAABgAAABRAAUABgAAABwAAAAZAAAAAAAAAFEABQAGAAAA
HQAAABkAAAABAAAAUAAHAAcAAAAeAAAAHAAAAB0AAAAaAAAAGwAAAEEABQAfAAAAIAAAAA0AAAAP
AAAAPgADACAAAAAeAAAA/QABADgAAQA=`;
fn program(vertex: text, block: vector<integer>, textures: integer) -> gpu::Program {
gpu::Program {
vertex: [gpu::F3],
instance: [],
block: block,
textures: textures,
sources: [gpu::source("spirv", vertex, PLAIN_F_SPV)],
}
}
fn test_a_shader_that_does_not_fit_builds_no_pipeline() {
if !display() { return; }
dev = gpu::open(gpu_vulkan::table(), 64, 48, "vk-wrong") ?? return assert(false, "no Vulkan device");
// declared as a vector and a float (the float at byte 16); a vec3 puts it at byte 12
assert(dev.pipeline(program(WRONG_V_SPV, [gpu::F4, gpu::F1], 0), gpu::State {}) == null, "the float sits at byte 12 in the shader and 16 in the program");
// every field the shader has is where the program has one; only the size differs
assert(dev.pipeline(program(SHORT_V_SPV, [gpu::F4, gpu::F1], 0), gpu::State {}) == null, "the shader's block is 16 bytes and the program's 32");
// a block the program does not declare
assert(dev.pipeline(program(SHORT_V_SPV, [], 0), gpu::State {}) == null, "the shader has a block and the program none");
// an input past the program's
assert(dev.pipeline(program(FAR_V_SPV, [], 0), gpu::State {}) == null, "the shader reads location 3 of one input");
// not SPIR-V at all
assert(dev.pipeline(program("not base64 at all!", [], 0), gpu::State {}) == null, "text that is not SPIR-V");
assert(dev.pipeline(program(PLAIN_F_SPV, [], 0), gpu::State {}) == null, "a fragment stage given as the vertex one");
assert(dev.pipeline(program(SHORT_V_SPV, [gpu::F4], 0), gpu::State {}) != null, "and a program the shader fits builds");
}
tests/two_backends.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// Two backends in one program. A device keeps the backend it was opened through and so does
// everything made from it: an OpenGL device and a Vulkan device draw side by side, each reading
// its own frame, and an object of one is released into the backend that made it, also after
// the other opened and after its own device closed.
// Without a display every test returns at once.
use gpu;
use gpu_gl;
use gpu_vulkan;
fn display() -> boolean {
gpu_vulkan::table() != 0 && must_draw((env_variable("DISPLAY") ?? "") != "")
}
// A run that says its tests must draw (`GRAPHICS_TEST_MUST_DRAW=1`: the library CI, on a runner
// it gave a display) fails here, where a test would otherwise return having drawn nothing.
fn must_draw(can: boolean) -> boolean {
assert(can || (env_variable("GRAPHICS_TEST_MUST_DRAW") ?? "") != "1", "this run's tests must draw (GRAPHICS_TEST_MUST_DRAW=1) and found no display to draw on");
can
}
const FLAT_V: text = `
#version 330 core
layout(location = 0) in vec3 pos;
layout(std140) uniform Block {{ vec4 color; vec4 at; }};
void main() {{ gl_Position = vec4(pos.xy * at.zw + at.xy, 0.0, 1.0); }}
`;
// SPIR-V of FLAT_V (vertex), written by gpu_vulkan/spirv.py: do not edit.
const FLAT_V_SPV: text = `AwIjBwADAQALAAgAKgAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABwAAAAAABAAAAG1haW4AAAAADQAAABIAAABHAAMACwAAAAIAAABIAAUACwAAAAAA
AAALAAAAAAAAAEgABQALAAAAAQAAAAsAAAABAAAASAAFAAsAAAACAAAACwAAAAMAAABIAAUACwAA
AAMAAAALAAAABAAAAEcABAASAAAAHgAAAAAAAABHAAMAFgAAAAIAAABIAAUAFgAAAAAAAAAjAAAA
AAAAAEgABQAWAAAAAQAAACMAAAAQAAAARwAEABgAAAAhAAAAAAAAAEcABAAYAAAAIgAAAAAAAAAT
AAIAAgAAACEAAwADAAAAAgAAABYAAwAGAAAAIAAAABcABAAHAAAABgAAAAQAAAAVAAQACAAAACAA
AAAAAAAAKwAEAAgAAAAJAAAAAQAAABwABAAKAAAABgAAAAkAAAAeAAYACwAAAAcAAAAGAAAACgAA
AAoAAAAgAAQADAAAAAMAAAALAAAAOwAEAAwAAAANAAAAAwAAABUABAAOAAAAIAAAAAEAAAArAAQA
DgAAAA8AAAAAAAAAFwAEABAAAAAGAAAAAwAAACAABAARAAAAAQAAABAAAAA7AAQAEQAAABIAAAAB
AAAAFwAEABMAAAAGAAAAAgAAAB4ABAAWAAAABwAAAAcAAAAgAAQAFwAAAAIAAAAWAAAAOwAEABcA
AAAYAAAAAgAAACsABAAOAAAAGQAAAAEAAAAgAAQAGgAAAAIAAAAHAAAAKwAEAAYAAAAjAAAAAAAA
ACsABAAGAAAAJAAAAAAAgD8gAAQAKAAAAAMAAAAHAAAANgAFAAIAAAAEAAAAAAAAAAMAAAD4AAIA
BQAAAD0ABAAQAAAAFAAAABIAAABPAAcAEwAAABUAAAAUAAAAFAAAAAAAAAABAAAAQQAFABoAAAAb
AAAAGAAAABkAAAA9AAQABwAAABwAAAAbAAAATwAHABMAAAAdAAAAHAAAABwAAAACAAAAAwAAAIUA
BQATAAAAHgAAABUAAAAdAAAAQQAFABoAAAAfAAAAGAAAABkAAAA9AAQABwAAACAAAAAfAAAATwAH
ABMAAAAhAAAAIAAAACAAAAAAAAAAAQAAAIEABQATAAAAIgAAAB4AAAAhAAAAUQAFAAYAAAAlAAAA
IgAAAAAAAABRAAUABgAAACYAAAAiAAAAAQAAAFAABwAHAAAAJwAAACUAAAAmAAAAIwAAACQAAABB
AAUAKAAAACkAAAANAAAADwAAAD4AAwApAAAAJwAAAP0AAQA4AAEA`;
const FLAT_F: text = `
#version 330 core
layout(std140) uniform Block {{ vec4 color; vec4 at; }};
out vec4 frag;
void main() {{ frag = color; }}
`;
// SPIR-V of FLAT_F (fragment), written by gpu_vulkan/spirv.py: do not edit.
const FLAT_F_SPV: text = `AwIjBwADAQALAAgAEgAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABgAEAAAABAAAAG1haW4AAAAACQAAABAAAwAEAAAABwAAAEcABAAJAAAAHgAAAAAA
AABHAAMACgAAAAIAAABIAAUACgAAAAAAAAAjAAAAAAAAAEgABQAKAAAAAQAAACMAAAAQAAAARwAE
AAwAAAAhAAAAAAAAAEcABAAMAAAAIgAAAAAAAAATAAIAAgAAACEAAwADAAAAAgAAABYAAwAGAAAA
IAAAABcABAAHAAAABgAAAAQAAAAgAAQACAAAAAMAAAAHAAAAOwAEAAgAAAAJAAAAAwAAAB4ABAAK
AAAABwAAAAcAAAAgAAQACwAAAAIAAAAKAAAAOwAEAAsAAAAMAAAAAgAAABUABAANAAAAIAAAAAEA
AAArAAQADQAAAA4AAAAAAAAAIAAEAA8AAAACAAAABwAAADYABQACAAAABAAAAAAAAAADAAAA+AAC
AAUAAABBAAUADwAAABAAAAAMAAAADgAAAD0ABAAHAAAAEQAAABAAAAA+AAMACQAAABEAAAD9AAEA
OAABAA==`;
fn flat() -> gpu::Program {
gpu::Program {
vertex: [gpu::F3],
instance: [],
block: [gpu::F4, gpu::F4],
textures: 0,
sources: [gpu::source("glsl-330", FLAT_V, FLAT_F), gpu::source("spirv", FLAT_V_SPV, FLAT_F_SPV)],
}
}
fn whole() -> vector<single> {
[-1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f]
}
// The rectangle scaled by (sx, sy) about (cx, cy), in one colour.
fn fill(dev: gpu::Device, pipe: const gpu::Pipeline, mesh: const gpu::Buffer, r: float, g: float, b: float, cx: float, sx: float) {
u = pipe.block();
u.f4(0, r, g, b, 1.0);
u.f4(1, cx, 0.0, sx, 1.0);
dev.bind(pipe);
dev.uniforms(u);
dev.vertices(mesh);
dev.draw(0, 6);
}
// An OpenGL and a Vulkan device, their passes interleaved call for call: one green on blue
// with a red left half, the other yellow on black with a white right half.
fn test_a_device_of_each_backend_draws_and_reads_back_its_own_frame() {
ao = gpu::open(gpu_gl::table(), 64, 48, "on-opengl");
if !display() { return; }
a = ao ?? return assert(false, "no OpenGL device");
b = gpu::open(gpu_vulkan::table(), 32, 24, "on-vulkan") ?? return assert(false, "no Vulkan device beside it");
assert(a.backend() == "gpu_gl" && b.backend() == "gpu_vulkan", "each says its own backend: {a.backend()} {b.backend()}");
pa = a.pipeline(flat(), gpu::State { depth: gpu::DEPTH_OFF }) ?? return assert(false, "no pipeline on OpenGL");
pb = b.pipeline(flat(), gpu::State { depth: gpu::DEPTH_OFF }) ?? return assert(false, "no pipeline on Vulkan");
ma = a.buffer(whole());
mb = b.buffer(whole());
a.begin_window(gpu::CLEAR, 0xFF0000FF);
b.begin_window(gpu::CLEAR, 0xFF000000);
fill(a, pa, ma, 0.0, 1.0, 0.0, 0.0, 1.0);
fill(b, pb, mb, 1.0, 1.0, 0.0, 0.0, 1.0);
fill(a, pa, ma, 1.0, 0.0, 0.0, -0.5, 0.5);
fill(b, pb, mb, 1.0, 1.0, 1.0, 0.5, 0.5);
a.end();
b.end();
sa = a.capture();
sb = b.capture();
assert(len(a.refusals()) == 0 && len(b.refusals()) == 0, "nothing refused: {a.refusals()} {b.refusals()}");
assert(sa[24 * 64 + 16] == 0xFFFF0000 && sa[24 * 64 + 48] == 0xFF00FF00, "OpenGL shows red and green: {sa[24 * 64 + 16]:x} {sa[24 * 64 + 48]:x}");
assert(sb[12 * 32 + 8] == 0xFFFFFF00 && sb[12 * 32 + 24] == 0xFFFFFFFF, "Vulkan shows yellow and white: {sb[12 * 32 + 8]:x} {sb[12 * 32 + 24]:x}");
}
struct Kit {
mesh: gpu::Buffer,
image: gpu::Texture,
pipe: gpu::Pipeline,
}
struct Held { kit: Kit? }
fn kit_of(dev: const gpu::Device) -> Kit? {
Kit {
mesh: dev.buffer(whole()),
image: dev.texture(8, 8),
pipe: dev.pipeline(flat(), gpu::State { depth: gpu::DEPTH_OFF }) ?? return null,
}
}
// What an OpenGL device made is let go after a Vulkan device opened, and after its own device
// closed: each release goes to OpenGL. The Vulkan device draws before and after.
fn test_an_object_is_released_into_the_backend_that_made_it() {
firsto = gpu::open(gpu_gl::table(), 64, 48, "first-opengl");
if !display() { return; }
first = firsto ?? return assert(false, "no OpenGL device");
held = Held { kit: kit_of(first) };
later = Held { kit: kit_of(first) };
assert(held.kit != null && later.kit != null, "two kits on OpenGL");
second = gpu::open(gpu_vulkan::table(), 64, 48, "then-vulkan") ?? return assert(false, "no Vulkan device");
mine = kit_of(second) ?? return assert(false, "no kit on Vulkan");
second.begin_window(gpu::CLEAR, 0xFF0000FF);
fill(second, mine.pipe, mine.mesh, 0.0, 1.0, 0.0, 0.0, 1.0);
second.end();
// one kit while its device is open, the other after it closed: both beside the Vulkan device
held.kit = null;
first.close();
later.kit = null;
second.begin_window(gpu::KEEP, 0);
fill(second, mine.pipe, mine.mesh, 1.0, 0.0, 0.0, -0.5, 0.5);
second.end();
shot = second.capture();
assert(len(second.refusals()) == 0, "nothing refused: {second.refusals()}");
assert(shot[24 * 64 + 16] == 0xFFFF0000 && shot[24 * 64 + 48] == 0xFF00FF00, "the Vulkan device drew on, and kept what it had drawn: {shot[24 * 64 + 16]:x} {shot[24 * 64 + 48]:x}");
}
tests/two_devices.loft
// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// Two windows open at once. Each device draws into its own window and reads its own frame
// back, also when the draws of the two are interleaved and when one is closed while the other
// goes on: nothing a draw depends on is shared between two devices.
// Without a display every test returns at once (`DISPLAY`, or `GRAPHICS_TEST_WINDOW=1` on a
// Mac).
use gpu;
use gpu_vulkan;
fn display() -> boolean {
gpu_vulkan::table() != 0 && must_draw((env_variable("DISPLAY") ?? "") != "")
}
// A run that says its tests must draw (`GRAPHICS_TEST_MUST_DRAW=1`: the library CI, on a runner
// it gave a display) fails here, where a test would otherwise return having drawn nothing.
fn must_draw(can: boolean) -> boolean {
assert(can || (env_variable("GRAPHICS_TEST_MUST_DRAW") ?? "") != "1", "this run's tests must draw (GRAPHICS_TEST_MUST_DRAW=1) and found no display to draw on");
can
}
const FLAT_V: text = `
#version 330 core
layout(location = 0) in vec3 pos;
layout(std140) uniform Block {{ vec4 color; vec4 at; }};
void main() {{ gl_Position = vec4(pos.xy * at.zw + at.xy, 0.0, 1.0); }}
`;
// SPIR-V of FLAT_V (vertex), written by gpu_vulkan/spirv.py: do not edit.
const FLAT_V_SPV: text = `AwIjBwADAQALAAgAKgAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABwAAAAAABAAAAG1haW4AAAAADQAAABIAAABHAAMACwAAAAIAAABIAAUACwAAAAAA
AAALAAAAAAAAAEgABQALAAAAAQAAAAsAAAABAAAASAAFAAsAAAACAAAACwAAAAMAAABIAAUACwAA
AAMAAAALAAAABAAAAEcABAASAAAAHgAAAAAAAABHAAMAFgAAAAIAAABIAAUAFgAAAAAAAAAjAAAA
AAAAAEgABQAWAAAAAQAAACMAAAAQAAAARwAEABgAAAAhAAAAAAAAAEcABAAYAAAAIgAAAAAAAAAT
AAIAAgAAACEAAwADAAAAAgAAABYAAwAGAAAAIAAAABcABAAHAAAABgAAAAQAAAAVAAQACAAAACAA
AAAAAAAAKwAEAAgAAAAJAAAAAQAAABwABAAKAAAABgAAAAkAAAAeAAYACwAAAAcAAAAGAAAACgAA
AAoAAAAgAAQADAAAAAMAAAALAAAAOwAEAAwAAAANAAAAAwAAABUABAAOAAAAIAAAAAEAAAArAAQA
DgAAAA8AAAAAAAAAFwAEABAAAAAGAAAAAwAAACAABAARAAAAAQAAABAAAAA7AAQAEQAAABIAAAAB
AAAAFwAEABMAAAAGAAAAAgAAAB4ABAAWAAAABwAAAAcAAAAgAAQAFwAAAAIAAAAWAAAAOwAEABcA
AAAYAAAAAgAAACsABAAOAAAAGQAAAAEAAAAgAAQAGgAAAAIAAAAHAAAAKwAEAAYAAAAjAAAAAAAA
ACsABAAGAAAAJAAAAAAAgD8gAAQAKAAAAAMAAAAHAAAANgAFAAIAAAAEAAAAAAAAAAMAAAD4AAIA
BQAAAD0ABAAQAAAAFAAAABIAAABPAAcAEwAAABUAAAAUAAAAFAAAAAAAAAABAAAAQQAFABoAAAAb
AAAAGAAAABkAAAA9AAQABwAAABwAAAAbAAAATwAHABMAAAAdAAAAHAAAABwAAAACAAAAAwAAAIUA
BQATAAAAHgAAABUAAAAdAAAAQQAFABoAAAAfAAAAGAAAABkAAAA9AAQABwAAACAAAAAfAAAATwAH
ABMAAAAhAAAAIAAAACAAAAAAAAAAAQAAAIEABQATAAAAIgAAAB4AAAAhAAAAUQAFAAYAAAAlAAAA
IgAAAAAAAABRAAUABgAAACYAAAAiAAAAAQAAAFAABwAHAAAAJwAAACUAAAAmAAAAIwAAACQAAABB
AAUAKAAAACkAAAANAAAADwAAAD4AAwApAAAAJwAAAP0AAQA4AAEA`;
const FLAT_F: text = `
#version 330 core
layout(std140) uniform Block {{ vec4 color; vec4 at; }};
out vec4 frag;
void main() {{ frag = color; }}
`;
// SPIR-V of FLAT_F (fragment), written by gpu_vulkan/spirv.py: do not edit.
const FLAT_F_SPV: text = `AwIjBwADAQALAAgAEgAAAAAAAAARAAIAAQAAAAsABgABAAAAR0xTTC5zdGQuNDUwAAAAAA4AAwAA
AAAAAQAAAA8ABgAEAAAABAAAAG1haW4AAAAACQAAABAAAwAEAAAABwAAAEcABAAJAAAAHgAAAAAA
AABHAAMACgAAAAIAAABIAAUACgAAAAAAAAAjAAAAAAAAAEgABQAKAAAAAQAAACMAAAAQAAAARwAE
AAwAAAAhAAAAAAAAAEcABAAMAAAAIgAAAAAAAAATAAIAAgAAACEAAwADAAAAAgAAABYAAwAGAAAA
IAAAABcABAAHAAAABgAAAAQAAAAgAAQACAAAAAMAAAAHAAAAOwAEAAgAAAAJAAAAAwAAAB4ABAAK
AAAABwAAAAcAAAAgAAQACwAAAAIAAAAKAAAAOwAEAAsAAAAMAAAAAgAAABUABAANAAAAIAAAAAEA
AAArAAQADQAAAA4AAAAAAAAAIAAEAA8AAAACAAAABwAAADYABQACAAAABAAAAAAAAAADAAAA+AAC
AAUAAABBAAUADwAAABAAAAAMAAAADgAAAD0ABAAHAAAAEQAAABAAAAA+AAMACQAAABEAAAD9AAEA
OAABAA==`;
fn flat() -> gpu::Program {
gpu::Program {
vertex: [gpu::F3],
instance: [],
block: [gpu::F4, gpu::F4],
textures: 0,
sources: [gpu::source("spirv", FLAT_V_SPV, FLAT_F_SPV)],
}
}
fn whole() -> vector<single> {
[-1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f]
}
// The rectangle scaled by (sx, sy) about (cx, cy), in one colour.
fn fill(dev: gpu::Device, pipe: const gpu::Pipeline, mesh: const gpu::Buffer, r: float, g: float, b: float, cx: float, sx: float) {
u = pipe.block();
u.f4(0, r, g, b, 1.0);
u.f4(1, cx, 0.0, sx, 1.0);
dev.bind(pipe);
dev.uniforms(u);
dev.vertices(mesh);
dev.draw(0, 6);
}
// Two devices, their passes interleaved call for call: one green on blue with a red left half,
// the other yellow on black with a white right half, and of another size.
fn test_two_devices_draw_and_read_back_their_own_frames() {
ao = gpu::open(gpu_vulkan::table(), 64, 48, "one");
if !display() { return; }
a = ao ?? return assert(false, "no device");
b = gpu::open(gpu_vulkan::table(), 32, 24, "two") ?? return assert(false, "no second device beside the first");
pa = a.pipeline(flat(), gpu::State { depth: gpu::DEPTH_OFF }) ?? return assert(false, "no pipeline on the first");
pb = b.pipeline(flat(), gpu::State { depth: gpu::DEPTH_OFF }) ?? return assert(false, "no pipeline on the second");
ma = a.buffer(whole());
mb = b.buffer(whole());
a.begin_window(gpu::CLEAR, 0xFF0000FF);
b.begin_window(gpu::CLEAR, 0xFF000000);
fill(a, pa, ma, 0.0, 1.0, 0.0, 0.0, 1.0);
fill(b, pb, mb, 1.0, 1.0, 0.0, 0.0, 1.0);
fill(a, pa, ma, 1.0, 0.0, 0.0, -0.5, 0.5);
fill(b, pb, mb, 1.0, 1.0, 1.0, 0.5, 0.5);
a.end();
b.end();
sa = a.capture();
sb = b.capture();
assert(len(a.refusals()) == 0 && len(b.refusals()) == 0, "nothing refused: {a.refusals()} {b.refusals()}");
assert(a.width() == 64 && a.height() == 48 && b.width() == 32 && b.height() == 24, "each keeps its size: {a.width()}x{a.height()} and {b.width()}x{b.height()}");
assert(len(sa) == 64 * 48 && len(sb) == 32 * 24, "each frame has its own size: {len(sa)} and {len(sb)}");
assert(sa[24 * 64 + 16] == 0xFFFF0000 && sa[24 * 64 + 48] == 0xFF00FF00, "the first shows red and green: {sa[24 * 64 + 16]:x} {sa[24 * 64 + 48]:x}");
assert(sb[12 * 32 + 8] == 0xFFFFFF00 && sb[12 * 32 + 24] == 0xFFFFFFFF, "the second shows yellow and white: {sb[12 * 32 + 8]:x} {sb[12 * 32 + 24]:x}");
// the second is closed; the first draws on and still reads its own
b.close();
a.begin_window(gpu::KEEP, 0);
fill(a, pa, ma, 0.0, 0.0, 1.0, 0.5, 0.5);
a.end();
sa = a.capture();
assert(len(a.refusals()) == 0, "nothing refused after the other closed: {a.refusals()}");
assert(sa[24 * 64 + 16] == 0xFFFF0000 && sa[24 * 64 + 48] == 0xFF0000FF, "the first drew on after the second closed: {sa[24 * 64 + 16]:x} {sa[24 * 64 + 48]:x}");
}