ui

module pkg::alloy::gpusource

The GPU, the same way on every platform (PLAN.md, stage F2): an API in WebGPU's shape over Vulkan, Metal on macOS (F7b), WebGPU in the browser, and a CPU rasterizer that runs the same shaders, for tests and where no GPU is.

The device owns every object it makes; the program holds handles, small values it copies freely, and gives them back with release (PLAN.md, F2 question 5): a handle used after its release faults. Commands are recorded into an encoder, then submitted.

const TRIANGLE = #shader(#read_file("triangle.wgsl"));

var device = if (Device::open(Backend::Automatic) is ::Ok |move d|) move d else { ... };
const shader = device.shader(&TRIANGLE);
const pipeline = device.render_pipeline(&RenderPipelineDescriptor::simple(shader, TextureFormat::Rgba8Unorm));
const target = device.texture(&TextureDescriptor::target(256, 256, TextureFormat::Rgba8Unorm));
var encoder = Encoder::empty();
encoder.begin_render_pass(target, ::Some(Color::black()));
encoder.set_pipeline(pipeline);
encoder.draw(3, 1);
encoder.end_render_pass();
device.submit(move encoder);
const pixels = device.read_texture(target);

## How it was built

Notes kept from each stage as it was finished, the sizes and numbers of their day; what changed since is in REVIEW.md and the history.

**F2 so far** (2026-10-12):

**The CPU device compiled** (2026-10-17; decisions/showcase.md, question 8A):

macro shadersource

exp macro shader(source: [u8]) -> [u8]

Compiles a WGSL module while compiling the program, for the device's shader: errors in it are the program's compile-time errors.

const TRIANGLE = #shader(#read_file("triangle.wgsl"));

macro mslsource

exp macro msl(source: [u8]) -> [u8]

The MSL source the Metal device compiles for a WGSL module, on any target, for tools and tests (#shader keeps it on macOS only).

fn spirv_wordssource

exp fn spirv_words(compiled: &[u8]) -> Vector<u32>

The SPIR-V module #shader compiled, for tools such as spirv-val.

type Resultsource

exp type Result<T> = enum

A value, or why there is none.

Variants

Ok: T
Error: String

type Backendsource

exp type Backend = enum

Which implementation a device uses.

Variants

Automatic

Vulkan on Linux and Windows, Metal on macOS, WebGPU in a browser, the CPU where none is there

Vulkan
Metal
WebGpu
Cpu

type Devicesource

exp type Device = struct

Functions

fn Device::opensource

exp fn Device::open(backend: Backend) -> Result<Device>

Opens a device: on the backend asked for, failing when it is not there, or the best one there for Automatic.

fn backendsource

exp fn backend(self device: &Device) -> Backend

The backend the device runs on: never Automatic.

fn namesource

exp fn name(self device: &Device) -> String

The GPU's name, or CPU.

const BUFFER_VERTEXsource

exp const BUFFER_VERTEX: u32 = 1;

What a buffer may be used for: these combined with |.

const BUFFER_INDEXsource

exp const BUFFER_INDEX: u32 = 2;

const BUFFER_UNIFORMsource

exp const BUFFER_UNIFORM: u32 = 4;

const BUFFER_STORAGEsource

exp const BUFFER_STORAGE: u32 = 8;

const BUFFER_COPY_SOURCEsource

exp const BUFFER_COPY_SOURCE: u32 = 16;

const BUFFER_COPY_DESTINATIONsource

exp const BUFFER_COPY_DESTINATION: u32 = 32;

const TEXTURE_SAMPLEDsource

exp const TEXTURE_SAMPLED: u32 = 1;

What a texture may be used for: these combined with |.

const TEXTURE_RENDER_TARGETsource

exp const TEXTURE_RENDER_TARGET: u32 = 2;

const TEXTURE_COPY_SOURCEsource

exp const TEXTURE_COPY_SOURCE: u32 = 4;

const TEXTURE_COPY_DESTINATIONsource

exp const TEXTURE_COPY_DESTINATION: u32 = 8;

type Buffersource

exp type Buffer = struct

Fields

exp size: u64

type TextureFormatsource

exp type TextureFormat = enum

Variants

Rgba8Unorm
Bgra8Unorm
R8Unorm

one channel, as coverage for masks

Functions

fn bytes_per_pixelsource

exp fn bytes_per_pixel(self format: &TextureFormat) -> u64

The bytes a pixel of the format takes.

type Texturesource

exp type Texture = struct

Fields

exp width: u32
exp height: u32
exp format: TextureFormat

type TextureDescriptorsource

exp type TextureDescriptor = struct

Fields

exp width: u32
exp height: u32
exp format: TextureFormat
exp usage: u32

Functions

fn TextureDescriptor::targetsource

exp fn TextureDescriptor::target(
    width: u32,
    height: u32,
    format: TextureFormat
) -> TextureDescriptor

A texture to render into and read back.

type Samplersource

exp type Sampler = struct

type SamplerDescriptorsource

exp type SamplerDescriptor = struct

Fields

exp linear: bool

linear filtering, else nearest

exp repeat: bool

repeating coordinates, else clamped to the edge

type Shadersource

exp type Shader = struct

type VertexFormatsource

exp type VertexFormat = enum

Variants

Float32
Float32x2
Float32x3
Float32x4
Uint32
Sint32
Unorm8x4

type VertexAttributesource

exp type VertexAttribute = struct

Fields

exp format: VertexFormat
exp offset: u32
exp location: u32

type VertexLayoutsource

exp type VertexLayout = struct

A vertex buffer's layout: the distance between vertices, whether it steps once an instance rather than once a vertex, and its attributes.

Fields

exp stride: u32
exp instanced: bool
exp attributes: Vector<VertexAttribute>

type Blendsource

exp type Blend = enum

Variants

Replace
Alpha

over what is there, by the color's alpha

Premultiplied

over what is there, the color already multiplied by its alpha

type Topologysource

exp type Topology = enum

Variants

Triangles
TriangleStrip

type RenderPipelineDescriptorsource

exp type RenderPipelineDescriptor = struct

Fields

exp shader: Shader
exp vertex: String

the vertex entry point's name

exp fragment: String
exp buffers: Vector<VertexLayout>
exp format: TextureFormat
exp blend: Blend
exp topology: Topology

Functions

fn RenderPipelineDescriptor::simplesource

exp fn RenderPipelineDescriptor::simple(
    shader: Shader,
    format: TextureFormat
) -> RenderPipelineDescriptor

Entry points vs and fs, no vertex buffers, triangles, replacing.

type RenderPipelinesource

exp type RenderPipeline = struct

type ComputePipelinesource

exp type ComputePipeline = struct

type BindingResourcesource

exp type BindingResource = enum

What a binding of a bind group holds.

Variants

Buffer: Buffer

all of it

BufferRange: BufferRange
Texture: Texture
Sampler: Sampler

type BufferRangesource

exp type BufferRange = struct

Fields

exp buffer: Buffer
exp offset: u64
exp size: u64

type Bindingsource

exp type Binding = struct

Fields

exp binding: u32
exp resource: BindingResource

type BindGroupsource

exp type BindGroup = struct

Fields

exp group: u32

type Colorsource

exp type Color = struct

Fields

exp r: f32
exp g: f32
exp b: f32
exp a: f32

Functions

fn Color::blacksource

exp fn Color::black() -> Color

fn Color::rgbsource

exp fn Color::rgb(r: f32, g: f32, b: f32) -> Color

fn Color::rgbasource

exp fn Color::rgba(r: f32, g: f32, b: f32, a: f32) -> Color

fn Color::whitesource

exp fn Color::white() -> Color

fn Color::transparentsource

exp fn Color::transparent() -> Color

fn buffersource

exp fn buffer(self device: &var Device, size: u64, usage: u32) -> Buffer

fn texturesource

exp fn texture(self device: &var Device, descriptor: &TextureDescriptor) -> Texture

fn samplersource

exp fn sampler(self device: &var Device, descriptor: &SamplerDescriptor) -> Sampler

fn shadersource

exp fn shader(self device: &var Device, compiled: &[u8]) -> Shader

The shader #shader compiled.

fn render_pipelinesource

exp fn render_pipeline(
    self device: &var Device,
    descriptor: &RenderPipelineDescriptor
) -> RenderPipeline

fn compute_pipelinesource

exp fn compute_pipeline(self device: &var Device, shader: Shader, entry: &[u8]) -> ComputePipeline

A compute pipeline running the shader's entry point of that name.

fn bind_groupsource

exp fn bind_group(
    self device: &var Device,
    pipeline: RenderPipeline,
    group: u32,
    bindings: &[Binding]
) -> BindGroup

The resources the pipeline's shaders read at the group's bindings.

fn bind_groupsource

exp fn bind_group(
    self device: &var Device,
    pipeline: ComputePipeline,
    group: u32,
    bindings: &[Binding]
) -> BindGroup

fn surfacesource

exp fn surface(self device: &var Device, window: &Window) -> Result<Surface>

The window's surface, for frames drawn on this device.

fn releasesource

exp fn release(self device: &var Device, buffer: Buffer)

Releases the buffer once the commands submitted before have run. A bind group holding it, or commands not yet submitted using it, must not be used after.

fn releasesource

exp fn release(self device: &var Device, texture: Texture)

Releases the texture as release does a buffer; a surface's textures go with their surface.

fn releasesource

exp fn release(self device: &var Device, sampler: Sampler)

fn releasesource

exp fn release(self device: &var Device, shader: Shader)

Releases the shader; the pipelines made from it stay.

fn releasesource

exp fn release(self device: &var Device, pipeline: RenderPipeline)

fn releasesource

exp fn release(self device: &var Device, pipeline: ComputePipeline)

fn releasesource

exp fn release(self device: &var Device, group: BindGroup)

fn releasesource

exp fn release(self device: &var Device, surface: Surface)

Releases the surface and its textures, before its window closes.

fn write_buffersource

exp fn write_buffer(self device: &var Device, buffer: Buffer, offset: u64, bytes: &[u8])

Writes the bytes into the buffer at the offset, before the commands submitted next.

fn write_texturesource

exp fn write_texture(self device: &var Device, texture: Texture, bytes: &[u8])

Writes the texture's pixels, in its format's order, row after row.

fn write_texture_regionsource

exp fn write_texture_region(
    self device: &var Device,
    texture: Texture,
    x: u32,
    y: u32,
    width: u32,
    height: u32,
    bytes: &[u8]
)

Writes a rectangle of the texture's pixels, width by height from (x, y), row after row.

type Surfacesource

exp type Surface = struct

A window's surface: textures to draw frames into, shown in the window one after another, paced by the display.

Fields

exp format: TextureFormat

the format of its textures, for pipelines drawing into them

fn acquiresource

exp fn acquire(self device: &var Device, surface: Surface, size: Size) -> Option<Texture>

The texture to draw the surface's next frame into, of the size, the window's; None when the window has no area. present shows it.

if (device.acquire(surface, window.size()) is ::Some |frame|) {
    var encoder = Encoder::empty();
    encoder.begin_render_pass(frame, ::Some(Color::black()));
    ...
    encoder.end_render_pass();
    device.submit(move encoder);
    device.present(surface, &var window);
}

fn presentsource

exp fn present(self device: &var Device, surface: Surface, window: &var Window)

Shows the texture acquired in the window once the commands submitted have drawn it.

type Encodersource

exp type Encoder = struct

Commands to submit to a device, recorded in order.

Functions

fn Encoder::emptysource

exp fn Encoder::empty() -> Encoder

fn begin_render_passsource

exp fn begin_render_pass(self e: &var Encoder, target: Texture, clear: Option<Color>)

Starts drawing into the texture, cleared to the color first when one is given, else keeping what it holds.

fn end_render_passsource

exp fn end_render_pass(self e: &var Encoder)

fn set_pipelinesource

exp fn set_pipeline(self e: &var Encoder, pipeline: RenderPipeline)

fn set_compute_pipelinesource

exp fn set_compute_pipeline(self e: &var Encoder, pipeline: ComputePipeline)

fn set_bind_groupsource

exp fn set_bind_group(self e: &var Encoder, group: BindGroup)

fn set_vertex_buffersource

exp fn set_vertex_buffer(self e: &var Encoder, slot: u32, buffer: Buffer, offset: u64)

fn set_index_buffersource

exp fn set_index_buffer(self e: &var Encoder, buffer: Buffer, offset: u64, wide: bool)

Indices of 32 bits when wide, else of 16.

fn set_scissorsource

exp fn set_scissor(self e: &var Encoder, x: u32, y: u32, width: u32, height: u32)

Draws only inside the rectangle, until the pass ends.

fn drawsource

exp fn draw(self e: &var Encoder, vertices: u32, instances: u32)

fn draw_fromsource

exp fn draw_from(
    self e: &var Encoder,
    vertices: u32,
    instances: u32,
    first_vertex: u32,
    first_instance: u32
)

fn draw_indexedsource

exp fn draw_indexed(
    self e: &var Encoder,
    indices: u32,
    instances: u32,
    first_index: u32,
    base_vertex: i32
)

fn dispatchsource

exp fn dispatch(self e: &var Encoder, x: u32, y: u32, z: u32)

Runs the compute pipeline set over that many workgroups.

fn copy_buffersource

exp fn copy_buffer(
    self e: &var Encoder,
    source: Buffer,
    source_offset: u64,
    destination: Buffer,
    destination_offset: u64,
    size: u64
)

fn copy_texturesource

exp fn copy_texture(self e: &var Encoder, source: Texture, destination: Texture)

Copies a texture over another of its size and format: a surface's, say, from one the program keeps drawn. Outside render passes.

fn submitsource

exp fn submit(self device: &var Device, encoder: Encoder)

Runs the encoder's commands, in order.

fn read_texturesource

exp fn read_texture(self device: &var Device, texture: Texture) -> Vector<u8>

The texture's pixels once every command submitted has run, in its format's order, row after row; none on WebGPU, which only reads back later, in the browser's event loop.

fn read_buffersource

exp fn read_buffer(self device: &var Device, buffer: Buffer) -> Vector<u8>

The buffer's bytes once every command submitted has run; none on WebGPU.