module 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):
#shadercompiles WGSL while compiling into SPIR-V 1.3 (gpu/shader/), which the CPU device interprets and rasterizes by WebGPU's rules (gpu/cpu/).- Vulkan (
gpu/vulkan/):vk.alloythe structures, numbers and functions used, written by hand from the headers, whichvulkan/layouts.alloychecks them against through clang (sizes and offsets from-fdump-record-layouts-simple, constants as static assertions) rather than readingvk.xml: the same source of truth with no XML to parse.vulkan.alloythe device: Vulkan 1.1 with dynamic rendering and synchronization2 or 1.3, a discrete GPU first, writes staged into the next submission, two submissions in flight, textures resting in one layout between commands and full barriers after passes, copies and dispatches.Device::open(Automatic)takes Vulkan where it opens, else the CPU. tests/scenes.alloydraws the five golden scenes and checks a compute dispatch: the same on the CPU, on lavapipe, on biscuit's GTX 1660 (distrobox-host-exec) and on the Windows desktop's RTX 4070 Ti over SSH, within one level of each channel.- Surfaces:
device.surface(&window), then each frameacquire(a texture of the window's size), commands,present. The window gives its native objects (window.target(): thewl_displayandwl_surface, or theHINSTANCEandHWND) and learns of a frame presented (window.surface_presented()), after which it neither draws its own frame nor waits for the display: the swap chain, in FIFO mode, paces redraws (the Wayland loop then handles the events already there between frames). On Wayland the viewport or buffer scale is set when the size changes, before any frame of that size, whoever commits it. The CPU device's surface presents throughwindow.present.samples/triangle.alloyis the demo;tests/surface.alloydraws ten frames and checks their pixels, under weston on lavapipe and in node's page on the CPU. Not yet run where a person sees it: on biscuit's desktop or the Windows desktop (triangle.exeandsurface.exeare in.alloy\window-teststhere). NVIDIA's driver cannot present to a headless weston, which offers no dmabuf. - WebGPU (
gpu/webgpu/):webgpu.web.jsopens the browser's device inprepare(), which the web glue now awaits beforemain(spec 6.3), and keeps the objects, the program holding their indices; shaders are the WGSL#shaderchecked, carried in the blob for web builds. Each entry point records the resources it uses (blob version 2), so a pipeline's layout holds the shader's layout for each group its entry points use and an empty one for a group below that they skip, as WebGPU wants every group of a layout bound. Surfaces are the window's canvas (Target::Canvas, markeddata-alloy-window), whose 2D context the window now makes only at its firstpresent. WebGPU cannot read back at once, soread_textureandread_buffergive nothing there. - Tested on WebGPU in node through Dawn's package (`npm install --prefix
~/.local/share/alloy/node-webgpu webgpu`, on lavapipe here):
tests/web/webgpu.mjsgives the fake page a WebGPU canvas, fails the run on any WebGPU error, and reads back what the program drew once it is done: the five scenes match the goldens, the compute dispatch validates, and the surface test's canvas holds the triangle. - Objects are freed by
device.release(handle)(question 5A). Each handle carries its index's generation, which a release moves on, and every use checks it, a released handle faulting in every build (std has no way yet to tell a checked build). Indices are taken again by the next object of their kind (gpu/slots.alloy), but for shaders, whose pipelines keep reading their layouts. Vulkan frees an object once the submissions made before its release have run, dropping writes to it still waiting; WebGPU destroys buffers and textures, which it does once the work submitted has run; the CPU frees at once. A surface's textures go with it. The scenes release what each one made, so the next ones draw over taken-again indices, andtests/released.alloymust fault. - The Windows desktop ran
triangle.exe,surface.exeand the demo in its browser (the user, 2026-10-12: "All windows tests work perfectly"). Left: the demo on biscuit's desktop.
**The CPU device compiled** (2026-10-17; decisions/showcase.md, question 8A):
cpu/interpreter.alloystill decodes a module into ops, then compiles each entry point into steps (Step: anActionand the registers it reads and writes), whichrungoes through with no ids, types nor operands looked up. Calls are inlined, parameters reading the arguments' registers; a composite's part, a vector's leading components and a pointer into memory by constants read registers already there; composites of constants and such pointers are made while compiling; a load or store through a variable's pointer is one step on memory; a buffer's value reads the offsets its type's layout gives; a part or a returned value used once is made where it goes; a branch to the next block is dropped and one on a comparison made for it alone is one step; the most frequent operations have steps of their own. Closures were not used: a match over steps is what the compiler makes fastest.- Every golden the same. The showcase's events screen, 1120 by 780,
from 14.7 to 2.0 s; its fourteen screens six at a time from 19.3 s
(99 s of CPU) to 2.7 s (14 s);
tests/widgetsfrom 21.4 to 3.3 s;bench/canvas.alloy cpua whole 1920x1080 frame of 10,000 primitives from 20.9 to 3.3 s, a frame around one node from 32 to 7.6 ms, a page of text from 3.0 to 0.46 s;scripts/test.shfrom 18 to 9 minutes. - Size, as
scripts/bench.shbuilds them: the counter 1,907,088 bytes natively (+14.9 KB) and 1,357,046 of wasm (+6.9 KB), hello world 1,658,880 (+14.4 KB) and 1,103,679 (+6.2 KB): past the budgets of the counter and of hello world's wasm (showcase.md, question 16).
shadermslspirv_wordsResultBackendDeviceDevice::openbackendnameBUFFER_VERTEXBUFFER_INDEXBUFFER_UNIFORMBUFFER_STORAGEBUFFER_COPY_SOURCEBUFFER_COPY_DESTINATIONTEXTURE_SAMPLEDTEXTURE_RENDER_TARGETTEXTURE_COPY_SOURCETEXTURE_COPY_DESTINATIONBufferTextureFormatbytes_per_pixelTextureTextureDescriptorTextureDescriptor::targetSamplerSamplerDescriptorShaderVertexFormatVertexAttributeVertexLayoutBlendTopologyRenderPipelineDescriptorRenderPipelineDescriptor::simpleRenderPipelineComputePipelineBindingResourceBufferRangeBindingBindGroupColorColor::blackColor::rgbColor::rgbaColor::whiteColor::transparentbuffertexturesamplershaderrender_pipelinecompute_pipelinebind_groupbind_groupsurfacereleasereleasereleasereleasereleasereleasereleasereleasewrite_bufferwrite_texturewrite_texture_regionSurfaceacquirepresentEncoderEncoder::emptybegin_render_passend_render_passset_pipelineset_compute_pipelineset_bind_groupset_vertex_bufferset_index_bufferset_scissordrawdraw_fromdraw_indexeddispatchcopy_buffercopy_texturesubmitread_textureread_buffer
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: TError: String
type Backendsource
exp type Backend = enum
Which implementation a device uses.
Variants
AutomaticVulkan on Linux and Windows, Metal on macOS, WebGPU in a browser, the CPU where none is there
VulkanMetalWebGpuCpu
type Devicesource
exp type Device = struct
Functions
exp fn Device::open(backend: Backend) -> Result<Device>exp fn backend(self device: &Device) -> Backendexp fn name(self device: &Device) -> Stringexp fn buffer(self device: &var Device, size: u64, usage: u32) -> Bufferexp fn texture(self device: &var Device, descriptor: &TextureDescriptor) -> Textureexp fn sampler(self device: &var Device, descriptor: &SamplerDescriptor) -> Samplerexp fn shader(self device: &var Device, compiled: &[u8]) -> Shaderexp fn render_pipeline( self device: &var Device, descriptor: &RenderPipelineDescriptor ) -> RenderPipelineexp fn compute_pipeline(self device: &var Device, shader: Shader, entry: &[u8]) -> ComputePipelineexp fn bind_group( self device: &var Device, pipeline: RenderPipeline, group: u32, bindings: &[Binding] ) -> BindGroupexp fn bind_group( self device: &var Device, pipeline: ComputePipeline, group: u32, bindings: &[Binding] ) -> BindGroupexp fn surface(self device: &var Device, window: &Window) -> Result<Surface>exp fn release(self device: &var Device, buffer: Buffer)exp fn release(self device: &var Device, texture: Texture)exp fn release(self device: &var Device, sampler: Sampler)exp fn release(self device: &var Device, shader: Shader)exp fn release(self device: &var Device, pipeline: RenderPipeline)exp fn release(self device: &var Device, pipeline: ComputePipeline)exp fn release(self device: &var Device, group: BindGroup)exp fn release(self device: &var Device, surface: Surface)exp fn write_buffer(self device: &var Device, buffer: Buffer, offset: u64, bytes: &[u8])exp fn write_texture(self device: &var Device, texture: Texture, bytes: &[u8])exp fn write_texture_region( self device: &var Device, texture: Texture, x: u32, y: u32, width: u32, height: u32, bytes: &[u8] )exp fn acquire(self device: &var Device, surface: Surface, size: Size) -> Option<Texture>exp fn present(self device: &var Device, surface: Surface, window: &var Window)exp fn submit(self device: &var Device, encoder: Encoder)exp fn read_texture(self device: &var Device, texture: Texture) -> Vector<u8>exp fn read_buffer(self device: &var Device, buffer: Buffer) -> Vector<u8>
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
Rgba8UnormBgra8UnormR8Unormone 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: u32exp height: u32exp format: TextureFormat
type TextureDescriptorsource
exp type TextureDescriptor = struct
Fields
exp width: u32exp height: u32exp format: TextureFormatexp 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: boollinear filtering, else nearest
exp repeat: boolrepeating coordinates, else clamped to the edge
type Shadersource
exp type Shader = struct
type VertexFormatsource
exp type VertexFormat = enum
Variants
Float32Float32x2Float32x3Float32x4Uint32Sint32Unorm8x4
type VertexAttributesource
exp type VertexAttribute = struct
Fields
exp format: VertexFormatexp offset: u32exp 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: u32exp instanced: boolexp attributes: Vector<VertexAttribute>
type Blendsource
exp type Blend = enum
Variants
ReplaceAlphaover what is there, by the color's alpha
Premultipliedover what is there, the color already multiplied by its alpha
type Topologysource
exp type Topology = enum
Variants
TrianglesTriangleStrip
type RenderPipelineDescriptorsource
exp type RenderPipelineDescriptor = struct
Fields
exp shader: Shaderexp vertex: Stringthe vertex entry point's name
exp fragment: Stringexp buffers: Vector<VertexLayout>exp format: TextureFormatexp blend: Blendexp 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: Bufferall of it
BufferRange: BufferRangeTexture: TextureSampler: Sampler
type BufferRangesource
exp type BufferRange = struct
Fields
exp buffer: Bufferexp offset: u64exp size: u64
type Bindingsource
exp type Binding = struct
Fields
exp binding: u32exp resource: BindingResource
type BindGroupsource
exp type BindGroup = struct
Fields
exp group: u32
type Colorsource
exp type Color = struct
Fields
exp r: f32exp g: f32exp b: f32exp 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: TextureFormatthe 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
exp fn Encoder::empty() -> Encoderexp fn begin_render_pass(self e: &var Encoder, target: Texture, clear: Option<Color>)exp fn end_render_pass(self e: &var Encoder)exp fn set_pipeline(self e: &var Encoder, pipeline: RenderPipeline)exp fn set_compute_pipeline(self e: &var Encoder, pipeline: ComputePipeline)exp fn set_bind_group(self e: &var Encoder, group: BindGroup)exp fn set_vertex_buffer(self e: &var Encoder, slot: u32, buffer: Buffer, offset: u64)exp fn set_index_buffer(self e: &var Encoder, buffer: Buffer, offset: u64, wide: bool)exp fn set_scissor(self e: &var Encoder, x: u32, y: u32, width: u32, height: u32)exp fn draw(self e: &var Encoder, vertices: u32, instances: u32)exp fn draw_from( self e: &var Encoder, vertices: u32, instances: u32, first_vertex: u32, first_instance: u32 )exp fn draw_indexed( self e: &var Encoder, indices: u32, instances: u32, first_index: u32, base_vertex: i32 )exp fn dispatch(self e: &var Encoder, x: u32, y: u32, z: u32)exp fn copy_buffer( self e: &var Encoder, source: Buffer, source_offset: u64, destination: Buffer, destination_offset: u64, size: u64 )exp fn copy_texture(self e: &var Encoder, source: Texture, destination: Texture)
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.