gpu_host source

← gpu_host · API reference · version 0.1.1

2 file(s), 159 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.

ItemDefinedWorked exampleCall sites
backends fn38—23 24 54 77 78
open fn54—45 52
open_fullscreen fn60—45
open_on fn66—32 46

src/gpu_host.loft

// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// A `gpu` device on the backend this machine has.
//
//   dev = gpu_host::open(960, 720, "view") ?? return;
//
// A game depends on this library and on `gpu`, and names no backend: each one is asked in turn,
// and a backend whose stack this machine lacks answers none.  The order is Metal, then OpenGL,
// then Vulkan — so a Mac draws through Metal, and Linux, Android and a browser page through
// OpenGL.  Vulkan answers on Linux after OpenGL until it is timed against OpenGL on a machine's
// own display (bench/backends.loft); before that it is reached by name.
//
// The environment variable `GPU_HOST` names the one backend to ask (`gpu_metal`, `gpu_gl`,
// `gpu_vulkan`): for
// a comparison of two backends on one machine, not for a game to set.
use gpu;
use gpu_gl;
use gpu_metal;
use gpu_vulkan;

// The backends in the order they are asked, each with its table (0: not on this machine).
struct Backend {
  name: text,
  table: i64,
}

fn all() -> vector<Backend> {
  [
    Backend { name: "gpu_metal", table: gpu_metal::table() },
    Backend { name: "gpu_gl", table: gpu_gl::table() },
    Backend { name: "gpu_vulkan", table: gpu_vulkan::table() },
  ]
}

// The names of the backends this machine has a table for, in the order they are asked.  One
// that is listed may still open no window: its stack is looked for when a device opens.
pub fn backends() -> vector<text> {
  [for b in all() if b.table != 0 { b.name }]
}

fn first(only: text, width: integer, height: integer, title: text, whole_screen: boolean) -> gpu::Device? {
  for b in all() {
    if b.table != 0 && (only == "" || only == b.name) {
      dev = if whole_screen { gpu::open_fullscreen(b.table, title) } else { gpu::open(b.table, width, height, title) };
      if dev != null { return dev; }
    }
  }
  null
}

// Open a window of `width` x `height` pixels on the first backend that has one to give.  Null
// when none does: the machine has no GPU this library knows, or no display.
pub fn open(width: integer, height: integer, title: text) -> gpu::Device? {
  first(env_variable("GPU_HOST") ?? "", width, height, title, false)
}

// Open a window over the whole screen, as `gpu::open_fullscreen` does, on the first backend
// that has one to give.
pub fn open_fullscreen(title: text) -> gpu::Device? {
  first(env_variable("GPU_HOST") ?? "", 0, 0, title, true)
}

// Open a window on the backend named `backend` alone (a name `backends()` lists).  For a test
// or a measurement that compares two backends; a game calls `open`.
pub fn open_on(backend: text, width: integer, height: integer, title: text) -> gpu::Device? {
  if backend == "" { return null; }
  first(backend, width, height, title, false)
}

tests/host.loft

// Copyright (c) 2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// The host opens the first backend this machine has, and a program that names none draws the
// same picture on each.  Without a display every test that opens a window returns at once
// (`DISPLAY`, or `GRAPHICS_TEST_WINDOW=1` on a Mac).
use gpu;
use gpu_host;
use gpu_paint;

fn display() -> boolean {
  must_draw((env_variable("DISPLAY") ?? "") != "" || (env_variable("GRAPHICS_TEST_WINDOW") ?? "") == "1")
}

// 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
}

fn test_the_backends_are_asked_in_one_order() {
  names = gpu_host::backends();
  assert(len(names) <= 3, "three backends are known: {names}");
  // whichever this machine has, in the one order: Metal, OpenGL, Vulkan
  order = ["gpu_metal", "gpu_gl", "gpu_vulkan"];
  at = 0;
  for n in names {
    while at < len(order) && (order[at] ?? "") != n { at += 1; }
    assert(at < len(order), "{n} is asked in the order Metal, OpenGL, Vulkan: {names}");
  }
  assert(gpu_host::open_on("", 8, 8, "none") == null && gpu_host::open_on("gpu_none", 8, 8, "none") == null, "a backend nobody knows opens nothing");
}

// A painter's picture, by a program that names no backend.
fn painted(dev: gpu::Device) -> vector<u32> {
  paint = gpu_paint::painter(dev, gpu::INTO_WINDOW) ?? return [];
  dev.begin_window(gpu::CLEAR, 0xFF102030);
  paint.rect(dev, 8.0, 4.0, 16.0, 10.0, 1.0, 0.0, 0.0, 1.0);
  paint.rect(dev, 20.5, 9.25, 30.0, 20.5, 0.0, 0.0, 1.0, 0.5);
  dev.end();
  dev.capture()
}

fn on(backend: text) -> vector<u32> {
  dev = gpu_host::open_on(backend, 64, 48, "host-{backend}") ?? return [];
  assert(gpu::backend_name() == backend, "the backend asked for serves the program: {gpu::backend_name()}");
  painted(dev)
}

fn first() -> vector<u32> {
  dev = gpu_host::open(64, 48, "host-first") ?? return [];
  wanted = env_variable("GPU_HOST") ?? "";
  if wanted == "" { wanted = gpu_host::backends()[0] ?? ""; }
  assert(gpu::backend_name() == wanted, "the first backend this machine has, or the one GPU_HOST names: {gpu::backend_name()}");
  painted(dev)
}

// The largest gap between two pixels in any one channel.
fn 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;
    d = if x > y { x - y } else { y - x };
    if d > worst { worst = d; }
  }
  worst
}

fn test_a_program_that_names_no_backend_draws_one_picture_on_each() {
  if !display() { return; }
  shown = first();
  assert(len(shown) == 64 * 48, "the host opened a window: {len(shown)} pixels");
  // by hand: the red rectangle is opaque, the sky shows beside it
  assert((shown[6 * 64 + 10] ?? 0) == 0xFFFF0000 && (shown[0] ?? 0) == 0xFF102030, "the picture: {shown[6 * 64 + 10] ?? 0:x} {shown[0] ?? 0:x}");
  first_name = gpu_host::backends()[0] ?? "";
  for name in gpu_host::backends() {
    one = on(name);
    assert(len(one) == len(shown), "{name} opened a window: {len(one)} pixels");
    // Byte for byte, but that another stack may round the half-transparent blend the other
    // way: a channel off by 1 (RADV against llvmpipe's OpenGL: 630 pixels).
    differ = 0;
    for p in one {
      q = shown[p#index] ?? 0;
      if p != q && (name == first_name || gap(p, q) > 1) { differ += 1; }
    }
    assert(differ == 0, "{differ} pixels on {name} differ from the first backend's");
  }
}