Docs
API Reference

ResourcesWebGPU

Shared WebGPU resources for UI and engine UIs. Import from @uno.build/ui/ResourcesWebGPU. The types below describe signatures; this module exports only the class.

Properties

PropertyTypeDescription
canvasWebGPUCanvas | undefinedCanvas supplied at creation.
adapterGPUAdapter | null | undefinedSupplied or requested adapter.
deviceGPUDeviceGPU device.
contextGPUCanvasContextCanvas context.
formatGPUTextureFormatRendering format.
image_atlas_sizenumberImage atlas layer size in pixels.
font_atlas_sizenumberFont atlas layer size in pixels.
image_managerImageManagerImage atlas manager.
font_managerFontManagerFont atlas manager.
eventsEventEmitterEmits image and font resource changes.

Methods

MethodReturnsDescription
static create(options: ResourcesWebGPUOptions)Promise<ResourcesWebGPU>Creates and initializes the resource store.
registerImage(src: string, image: WebGPUImage)ManagedAtlasImageRegisters an image under a resource key.
copyTextureToImage(src: string, source: GPUTexelCopyTextureInfo)voidCopies a texture region the size of a registered image into it.
disposeImage(src: string)voidRemoves a registered image.
getImageSize(src: string){ width: number; height: number } | undefinedGets the registered image dimensions.
registerFont(name: string, font: WebGPUFont)ManagedFontRegisters a font image and its metadata.
disposeFont(name: string)voidRemoves a registered font.
dispose()voidClears registered resources and releases atlas textures.

Duplicate image or font keys throw. Disposing an unknown key does nothing. ui.destroy() does not dispose shared resources; resources.dispose() does not destroy the device or context.

Creation options

OptionTypeDefault
canvasWebGPUCanvasRequired when context is omitted
contextGPUCanvasContextObtained from canvas and configured
adapterGPUAdapter | nullRequested when neither adapter nor device is supplied
deviceGPUDeviceRequested from the adapter
formatGPUTextureFormatnavigator.gpu.getPreferredCanvasFormat()
image_atlas_sizenumber2048
font_atlas_sizenumber2048

A supplied context is used without reconfiguration.

A supplied device must allow three storage buffers in the vertex stage. A device created in compatibility mode has to request them with requiredLimits: { maxStorageBuffersInVertexStage: 3 }; the device that ResourcesWebGPU requests already does.

type WebGPUCanvas = {
    getContext(context_id: 'webgpu'): GPUCanvasContext | null
}

Image input

WebGPUImage fieldTypeDefault
imageGPUCopyExternalImageSourceOmitted: only width × height is reserved in the atlas, for copyTextureToImage()
widthnumberimage.width; required if unavailable
heightnumberimage.height; required if unavailable
preventBleedingbooleanfalse; copies edge pixels into atlas padding when enabled

See Images for usage, and Render to Image for copyTextureToImage().

Font input

WebGPUFont contains the image fields above, with image required, plus a required data: FontData. See Texts and fonts for usage.

type FontData = {
    atlas: {
        size: number
        distanceRange: number
        effectDistanceRange?: number
        yOrigin: 'bottom' | 'top'
        width?: number
        height?: number
        type?: string
    }
    metrics: {
        emSize?: number
        lineHeight: number
        ascender: number
        descender: number
        underlineY?: number
        underlineThickness?: number
    }
    glyphs: Array<{
        unicode: number
        advance: number
        planeBounds?: GlyphBounds
        atlasBounds?: GlyphBounds
    }>
    kerning?: Array<{ unicode1: number; unicode2: number; advance: number }>
}

type GlyphBounds = { left: number; bottom: number; right: number; top: number }

On this page