Skip to content

Texture Workflows

PNGine keeps texture operations close to WebGPU. The document declares descriptors, views, copies, and their position in the frame; the host can write or observe named resources without rebuilding the shader.

(write-texture …) maps to GPUQueue.writeTexture. Its :data names raw bytes, not an image-MIME wrapper. This complete program writes a 2×2 RGBA texture:

(data :name texels (bytes :base64 "AAAAAAAAAAAAAAAAAAAAAA=="))
(texture :name field :size [2 2] :format rgba8unorm :usage [copy-dst])
(queue :name upload
(write-texture :texture field :data texels :bytes-per-row 8 :rows-per-image 2 :size [2 2]))
(frame :name main :perform [upload])

There is deliberately no render pass in this minimal example: it isolates the upload. A real program declares texture-binding usage and samples the texture in an ordinary pass.

The write accepts full and partial regions, origins, mip levels, array layers/3D slices, aspects, and explicit extents. Omitted :size means the remaining physical extent at the selected origin. Omitted strides remain absent at the WebGPU boundary; writing zero is a value, not omission.

Path Source layout
write-texture and host writeTexture offset and bytesPerRow have no 256-byte rule; tight rows and unaligned offsets are legal
copy-buffer-to-texture / copy-texture-to-buffer A supplied bytesPerRow is a multiple of 256; the buffer offset follows the texel-block footprint
Compressed BC/ETC2/EAC/ASTC Origins and extents use whole texel blocks; physical mip extents round up to whole blocks

The compiler and runtime share the same layout calculator and refusal codes. Compilation checks known descriptors; runtime checks untrusted bytes, current canvas sizes, image-derived extents, and adapter capabilities.

An (image-bitmap …) decodes an embedded image. Use its name as the texture’s size and copy it with copy-external-image-to-texture:

(data :name photoBytes :file "textures/photo.png" :mime "image/png")
(image-bitmap :name photo :data photoBytes)
(texture :name albedo :size photo :format rgba8unorm
:usage [texture-binding copy-dst render-attachment])
(queue :name upload
(copy-external-image-to-texture
(source :image photo)
(destination :texture albedo)))

Do not pass an image-wrapped (data :file …) to write-texture; the raw-byte operation and decoded-image operation are intentionally different.

A named texture that the recurring frame does not overwrite is host-writable:

const bytes = new Uint8Array([
255, 0, 0, 255, 0, 255, 0, 255,
0, 0, 255, 255, 255, 255, 255, 255,
]);
writeTexture(p, "field", bytes, {
bytesPerRow: 8,
rowsPerImage: 2,
size: [2, 2],
});

The destination needs copy-dst, one sample, and a supported 2D/3D shape. Use redraw: false to ride the next draw, or draw(p, { textures: { field: bytes } }) to carry the write in an explicit frame. The worker applies host writes before the frame’s own queue operations and passes.

frameWritten prevents a host write that the frame would immediately overwrite. Pooled textures accept poolIndex: k, "active", or absent for every member. getTextures(p) reports the live descriptor, writability, pool, and legal aspects. See JavaScript API for ownership, ordering, and error details.

The host-only document pattern is examples/texture_host_write.sjon.

An explicit (texture-view …) can select one mip level or array layer. Render-pass colour, resolve, and depth/stencil attachments accept that view directly:

(texture-view :name level1 :texture mipTex
:base-mip-level 1 :mip-level-count 1)
(render-pass :name drawLevel1 :pipeline pipe
(color-attachment :view level1 :load-op clear :store-op store)
(draw :vertex-count 3))

This is also the mechanism for layer rendering, depth-only targets, and explicit resolve views. Invalid spans, incompatible formats/aspects, pooled attachments, and illegal multisample cases are refused before the driver sees them.

(mip-chain …) lowers to one sampled view and one attachment view per generated level, a shared sampler/module/pipeline, and one named step group:

(texture :name mipTex :size [100 100] :format rgba8unorm
:mip-level-count 4
:usage [texture-binding render-attachment copy-dst])
(mip-chain :name fillMips :texture mipTex)
(frame :name main
:perform [uploadLevel0 fillMips draw])

Level 0 is the input and is never written by the chain. Generation runs only when the frame performs the group, in ascending level order. There is no hidden upload, load-time trigger, or automatic regeneration. A static image currently placed in a per-frame queue repeats that upload when the frame repeats; schedule accordingly.

The first slice supports single-layer, single-sample 2D filterable color textures with texture-binding and render-attachment usage. Depth, compressed, array, 3D, pooled, and MSAA shapes are refused. The POT and non-power-of-two pixel example is examples/mip_chain_view.sjon.

Read texture bytes without steering the frame

Section titled “Read texture bytes without steering the frame”

A texture-to-buffer copy into a map-read copy-dst buffer produces an onTextureReadback callback with an owned byte copy and its exact layout:

const p = await pngine("readback.png", {
canvas,
onTextureReadback: ({ texture, bytes, bytesPerRow, copySize }) => {
console.log(texture, bytes.byteLength, bytesPerRow, copySize);
},
});

This channel is separate from onQueryResult, whose 64-bit lanes remain unchanged. No unpadding, swizzling, or format conversion is invented. Readback observes the artifact boundary and cannot select later payload work.

The schema exposes all 52 BC, ETC2/EAC, and ASTC names. Creation checks the required feature before createTexture, including the sliced-3D sibling where one exists. There is no fallback format: an unsupported adapter gets a named refusal with the texture and feature.

Container decoding and transcompression are offline tooling concerns. The runtime accepts raw block bytes; it does not decode KTX2 or silently substitute another format.