Separate mesh buffers require rebinding between draws. A mesh pool stores vertices in shared and indices in one index buffer. One pipeline can then draw any mesh by its range. Instances use a pool, and custom shaders can too.
Shapes fill one pool's vertex and index ranges
Try Add shapes until add throws. The pool has room for 2048 vertices and 8192 indices.
Creates shared vertex storage and an index buffer, with bounds and textures per mesh. One
pipeline can draw any mesh. Each vertex uses 12 bytes for position and 8 for normal and UV
coordinates, and 8 for a second uv set with uvs1. Skinned models add 8 bytes for joints and
weights. Extras add the size of their struct per vertex.
@unstable
@injects ― definition the vertices vertexAt, positionAt and uv1At read, the joints and
weights influencesAt reads, and the boxes boundsOf reads
@injects ― member the extras pool.extra reads
@binds ― 1 storage: positionAt, the positions
@binds ― 2 storage: vertexAt, the positions and the normals and uvs
@binds ― 1 storage: uv1At, the second uvs, in a pool made with uvs1
@binds ― 1 storage: influencesAt, the joints and weights
@binds ― 1 storage: boundsOf, every mesh's box
@binds ― 1 storage: pool.extra, every vertex's extras
@binds ― 1 uniform: pool.texturesOf(index) for materials, beside its textures and sampler, in
the fragment stage
@submits ― setup a shape added without an encoder bakes in a compute pass of its own, submitted at
once, and a model's images generate their mips as they decode
@owns ― 5 to 7 buffers, a uniform per way of reading textures, a uniform per shape added, a
pipeline per pair of shape functions, textures, samplers, bind groups, what images makes:
positions, normals and uvs, indices and boxes, second uvs with uvs1, joints and weights once a
skinned model comes, and the extras when extra names any; a bake per vertexAt and indexAt
it bakes, and per shape added the uniform of its ranges and their group; each image decoded once
per format it is read in, and a group per set of a mesh's textures
@returns ― a pool you add meshes and models to and destroy
Bakes a shape into the next free ranges in a compute pass. Given a frame or an encoder, it
records there; otherwise, it submits at once. The bake's pipeline inlines the shape's
vertexAt and indexAt, and compiles, blocking, the first time the pool bakes those
functions, unless initAsync compiled it for the shapes option. Throws when the pool is
full.
Bakes a shape into the next free ranges in a compute pass. Given a frame or an encoder, it
records there; otherwise, it submits at once. The bake's pipeline inlines the shape's
vertexAt and indexAt, and compiles, blocking, the first time the pool bakes those
functions, unless initAsync compiled it for the shapes option. Throws when the pool is
full.
Uploads each mesh into the next free ranges and decodes images in the background. Each image
decodes once per pool and format, and pixels and bitmaps upload during the call. The first skinned mesh allocates a joint-and-weight buffer
for the full vertex capacity. Throws if the pool is full, the model has no meshes, its
arrays disagree or encoded images other than HDR have no decoder.
Reads a binary glTF 2.0 file into meshes, selected parts, a skin and animations. It applies
default-scene node transforms and groups meshes by material. Morph targets are omitted.
Skinned meshes use their bind pose with four joints and weights per vertex. An unskinned mesh
attached to a joint, such as a helmet, binds to that joint. An unskinned mesh without a joint
ancestor must be read as a part or this throws. A part holding a skinned mesh binds to the skin
as meshes do; other parts have no skin bindings.
It checks every index, count and range before reading. Invalid glTF, unsupported file versions
and unsupported required features throw with details. Use load for a model whose buffers or
images are separate files. For extensions that need conversion,
errors name the gltf-transform command.
@param ― source the file's bytes, from anywhere
@param ― options.parts names of nodes to keep apart from the rest, each posed about its own origin
or, holding a skinned mesh, by the skin
@param ― options.rotation a turn of the whole model, for one that doesn't face +z
@returns ― plain arrays and images, for meshPool.add
Resolves when all textures have decoded and uploaded, at once for a model whose materials
name none. Until then, meshes draw without textures. Rejects if image decoding fails or the
pool is destroyed before upload. A destroyed pool uploads nothing.
ready;// wait for image decoding and uploads
add returns a Mesh or a Model with one mesh per material. A mesh has its pool index and ranges firstIndex, indexCount, baseVertex and vertexCount. Meshes cannot be removed.
Bakingpool.add(shape) bakes triangle-list shapes from @typegpu/kit/geometry in a compute pass. Supply a or an encoder to record into it, or omit it to submit immediately.
Bake pipelines A bake’s pipeline inlines the shape’s vertexAt and indexAt. The pool compiles one, blocking, the first time it bakes those functions. It reuses that pipeline when you add the same shape again or a copy that shares them. Add new shapes while loading, or compile their bakes ahead.
Indices and bounds Unindexed shapes get one index per vertex. Baking also computes their bounding boxes.
Defaults Shapes default to white, roughness 1, metallic 0, single-sided, unmasked and untextured.
Name the shapes you will add in shapes, and initAsync compiles their bakes without blocking. Include the pool in pipelines.compile with the scene’s other pipelines, and those shapes bake later without a compile:
Creates shared vertex storage and an index buffer, with bounds and textures per mesh. One
pipeline can draw any mesh. Each vertex uses 12 bytes for position and 8 for normal and UV
coordinates, and 8 for a second uv set with uvs1. Skinned models add 8 bytes for joints and
weights. Extras add the size of their struct per vertex.
@unstable
@injects ― definition the vertices vertexAt, positionAt and uv1At read, the joints and
weights influencesAt reads, and the boxes boundsOf reads
@injects ― member the extras pool.extra reads
@binds ― 1 storage: positionAt, the positions
@binds ― 2 storage: vertexAt, the positions and the normals and uvs
@binds ― 1 storage: uv1At, the second uvs, in a pool made with uvs1
@binds ― 1 storage: influencesAt, the joints and weights
@binds ― 1 storage: boundsOf, every mesh's box
@binds ― 1 storage: pool.extra, every vertex's extras
@binds ― 1 uniform: pool.texturesOf(index) for materials, beside its textures and sampler, in
the fragment stage
@submits ― setup a shape added without an encoder bakes in a compute pass of its own, submitted at
once, and a model's images generate their mips as they decode
@owns ― 5 to 7 buffers, a uniform per way of reading textures, a uniform per shape added, a
pipeline per pair of shape functions, textures, samplers, bind groups, what images makes:
positions, normals and uvs, indices and boxes, second uvs with uvs1, joints and weights once a
skinned model comes, and the extras when extra names any; a bake per vertexAt and indexAt
it bakes, and per shape added the uniform of its ranges and their group; each image decoded once
per format it is read in, and a group per set of a mesh's textures
@returns ― a pool you add meshes and models to and destroy
Starts compiling every item the list holds at the call, yielding to the event loop past the
time budget so the page's other work, such as a loading animation, keeps running. Use before the
frame loop starts. Waits for every item to finish, then rejects if any failed.
@param ― from a list of pipelines and modules that compile their own, or an owner that took them
@returns ― a promise that resolves once every item has compiled
Bakes a shape into the next free ranges in a compute pass. Given a frame or an encoder, it
records there; otherwise, it submits at once. The bake's pipeline inlines the shape's
vertexAt and indexAt, and compiles, blocking, the first time the pool bakes those
functions, unless initAsync compiled it for the shapes option. Throws when the pool is
full.
Addingpool.add(model) copies a gltf.read model into the pool. It validates every mesh before changing the pool. Empty models, insufficient capacity or mismatched normals, UVs, indices, joints or weights throw.
Capacity Size the pool for the combined mesh, vertex and index counts. meshPool.roomFor(items) totals them for models such as impostors.CARD, individual meshes, shapes and explicit counts. Spread the result into the pool options. It includes uvs1: true if a mesh carries uvs1.
Factors Mesh material stores file factors, with colour alpha in opacity. Textures multiply these factors. Instance fields replace them.
Masking An alpha: 'mask' mesh cuts out pixels below alphaCutoff in colour and shadow passes. Alpha is texture alpha times opacity.
Other fieldsdoubleSided draws back faces lit from their side. normalScale controls normal-map strength and occlusionStrength how far the occlusion texture darkens.
Textured meshes A mesh whose material names a texture or masks alpha is textured; read textures only for those.
Vertex colours Preserve glTF vertex colours from COLOR_0 by declaring an extra named color. A pool without it drops those colours and warns once, unless every colour is white.
Skinned meshes Skinned meshes retain joints and weights for poses. The pool creates their buffer when the first skinned model is added.
Declare custom vertex fields with extra, using the same types as tables. Models supply arrays in extra, and shaders read them through pool.extra:
import{
import gltf
gltf}from'@typegpu/kit';
import{
import instances
instances,
import meshPool
meshPool,
import shading
shading}from'@typegpu/kit';
const
constpool:meshPool.MeshPool<{
color:d.Vec4f;
}>
pool=
import meshPool
meshPool.
functioncreate<{
color:d.Vec4f;
}>(root:TgpuRoot,options:meshPool.Options<{
color:d.Vec4f;
}>):meshPool.MeshPool<{
color:d.Vec4f;
}>
Creates shared vertex storage and an index buffer, with bounds and textures per mesh. One
pipeline can draw any mesh. Each vertex uses 12 bytes for position and 8 for normal and UV
coordinates, and 8 for a second uv set with uvs1. Skinned models add 8 bytes for joints and
weights. Extras add the size of their struct per vertex.
@unstable
@injects ― definition the vertices vertexAt, positionAt and uv1At read, the joints and
weights influencesAt reads, and the boxes boundsOf reads
@injects ― member the extras pool.extra reads
@binds ― 1 storage: positionAt, the positions
@binds ― 2 storage: vertexAt, the positions and the normals and uvs
@binds ― 1 storage: uv1At, the second uvs, in a pool made with uvs1
@binds ― 1 storage: influencesAt, the joints and weights
@binds ― 1 storage: boundsOf, every mesh's box
@binds ― 1 storage: pool.extra, every vertex's extras
@binds ― 1 uniform: pool.texturesOf(index) for materials, beside its textures and sampler, in
the fragment stage
@submits ― setup a shape added without an encoder bakes in a compute pass of its own, submitted at
once, and a model's images generate their mips as they decode
@owns ― 5 to 7 buffers, a uniform per way of reading textures, a uniform per shape added, a
pipeline per pair of shape functions, textures, samplers, bind groups, what images makes:
positions, normals and uvs, indices and boxes, second uvs with uvs1, joints and weights once a
skinned model comes, and the extras when extra names any; a bake per vertexAt and indexAt
it bakes, and per shape added the uniform of its ranges and their group; each image decoded once
per format it is read in, and a group per set of a mesh's textures
@returns ― a pool you add meshes and models to and destroy
create(
constroot:TgpuRoot
root,{
Options<E extends Extras ={}>.capacity: number
Limits the number of meshes to at most 256.
capacity:16,
Options<{ color: Vec4f;}>.extra?:{
color:d.Vec4f;
}|undefined
Declares custom vertex attributes, for example { tint: d.vec4f }. Model meshes supply
their data in extra; shaders read it through pool.extra.tint(i). These attributes use a
separate buffer with one struct per vertex. Shapes read as zero. Default none.
Uploads each mesh into the next free ranges and decodes images in the background. Each image
decodes once per pool and format, and pixels and bitmaps upload during the call. The first skinned mesh allocates a joint-and-weight buffer
for the full vertex capacity. Throws if the pool is full, the model has no meshes, its
arrays disagree or encoded images other than HDR have no decoder.
Reads a binary glTF 2.0 file into meshes, selected parts, a skin and animations. It applies
default-scene node transforms and groups meshes by material. Morph targets are omitted.
Skinned meshes use their bind pose with four joints and weights per vertex. An unskinned mesh
attached to a joint, such as a helmet, binds to that joint. An unskinned mesh without a joint
ancestor must be read as a part or this throws. A part holding a skinned mesh binds to the skin
as meshes do; other parts have no skin bindings.
It checks every index, count and range before reading. Invalid glTF, unsupported file versions
and unsupported required features throw with details. Use load for a model whose buffers or
images are separate files. For extensions that need conversion,
errors name the gltf-transform command.
@param ― source the file's bytes, from anywhere
@param ― options.parts names of nodes to keep apart from the rest, each posed about its own origin
or, holding a skinned mesh, by the skin
@param ― options.rotation a turn of the whole model, for one that doesn't face +z
@returns ― plain arrays and images, for meshPool.add
Defines an instance kind with custom fields, transform ownership and callbacks for vertex
motion or materials. Each kind uses colour and shadow pipelines per renderer. name brands
set ids, as in Id<typeof Fish>, and labels the table and WGSL. Throws if both material and
surface are supplied, or a custom field duplicates a built-in or reserved name.
@param ― name a label for ids and WGSL
@returns ― a kind to give instances.create as kind
Returns four values to interpolate between vertices and hand to the fragment hooks as
pixel.carried. For example, read an extra attribute with pool.extra.tint(vertexIndex).
vertexIndex indexes the pool, as in meshPool.vertexAt. Default none, which omits four
floats per vertex and sets carried to zero.
Returns the material at a pixel. base combines the instance material fields with the mesh
textures, as materials.textured does. Default base.
@gpu
material:(
pixel:instances.Pixel<{
rotation:d.v4f;
position:d.v3f;
scale:d.v3f;
color:d.v3f;
roughness:number;
emission:d.v3f;
metallic:number;
mesh:number;
opacity:number;
}>
pixel,
base:{
color:d.v3f;
roughness: number;
emission:d.v3f;
metallic: number;
occlusion: number;
}
base)=>{
'use gpu';
const
consttinted:{
color:d.v3f;
roughness:number;
emission:d.v3f;
metallic:number;
occlusion:number;
}
tinted=
import shading
shading.
constMaterial:d.WgslStruct
(props?:{
color:d.v3f;
roughness: number;
emission: d.v3f;
metallic: number;
occlusion: number;
}|undefined)=>{
color:d.v3f;
roughness: number;
emission:d.v3f;
metallic: number;
occlusion: number;
}
This schema describes a surface with the glTF metallic-roughness model. New fields are only
appended, each off at zero. Make one with a materials preset or copy one with Material(m),
then assign the fields you set: a struct literal stops compiling when a field is added.
Material(
base:{
color:d.v3f;
roughness: number;
emission:d.v3f;
metallic: number;
occlusion: number;
}
base);
consttinted:{
color:d.v3f;
roughness:number;
emission:d.v3f;
metallic:number;
occlusion:number;
}
tinted.
color:d.v3f
Sets the diffuse colour of a dielectric or the reflection colour of a metal, in linear
colour space.
color=
base:{
color:d.v3f;
roughness: number;
emission:d.v3f;
metallic: number;
occlusion: number;
}
base.
color:d.v3f
Sets the diffuse colour of a dielectric or the reflection colour of a metal, in linear
colour space.
color*
pixel:instances.Pixel<{
rotation:d.v4f;
position:d.v3f;
scale:d.v3f;
color:d.v3f;
roughness:number;
emission:d.v3f;
metallic:number;
mesh:number;
opacity:number;
}>
pixel.
Pixel<I>.carried: d.v4f
Holds the values interpolated from carry, or zero without it.
Creates a set of mesh instances with transforms, materials and optional custom fields. Each
instance uses a row, and renderers draw one list of live rows per mesh.
@param ― pool the mesh pool whose meshes it draws
@param ― options.kind a kind from instances.define
@returns ― a set of instances you flush every frame and destroy
Adds an instance of a mesh, model or group of meshes from the pool. Each mesh uses a row,
and all rows move and change together under one id. values may be a function of each mesh,
to give a model's meshes values of their own. Throws if a mesh belongs to another pool, the
group is empty, a posed kind receives a mesh without joints or the set is full. When values
or a write throws, it adds no row and rethrows.
add(
constpainted:meshPool.Model
painted);
Fields accept 32-bit scalars or vectors, including d.f32, d.u32, d.i32 and d.vec2f through d.vec4i. Supply components in vertex order as Float32Array, Uint32Array or Int32Array matching the field type.
Shape extras and omitted fields default to zero. Undeclared extras are dropped with one warning per field, naming the declaration needed to keep it.
In a shader created from root.pipe(pool.inject()), pool.extra.color(i) reads the vertex indexed as vertexAt does. Use carry to pass it to fragments.
Kinds Meshes have colour, metallic-roughness, normal, emission and occlusion textures, all with mipmaps. Colour and emission use ; the others use linear values. Normal maps need no tangents. A material without a normal map skips normal mapping, since its reading’s normalScale is 0.
Transforms A texture’s transform scales, rotates and then offsets the coordinates it reads, as glTF’s KHR_texture_transform does. A mesh can then tile one texture and read another from an . Each texture of a mesh keeps its own. The cloister tiles its paving, turns its border and cuts its panels from one atlas this way.
Second uv set A texture with uvSet: 1 reads the mesh’s second uv set, uvs1, as a lightmap or occlusion map baked on its own unwrap does. The pool keeps uvs1 only when created with uvs1: true, which roomFor sets for meshes that carry it. add throws for a mesh whose texture reads a uv set the pool can’t keep. The cloister bakes its occlusion on a second set, and the classroom its daylight and its lamps’ bounce.
Sampler A material samples all its textures with one . It takes the colour texture’s, or the first texture’s when there is no colour texture. The pool ignores the samplers a model gives its other textures, so give a lightmap’s charts margins when the colour texture repeats.
Shared images The pool decodes each image once per format and shares identical bytes across meshes and models. The pool uploads pixels and bitmaps during add. It reuses the upload when the same image object is added again in the same format. Give changed pixels a new object. If one image is used for both colour and normals, it decodes twice.
Loading Meshes draw without textures until ready settles. A model whose materials name no texture is ready at once. Decode failures or destroying the pool before completion reject ready. Failed decodes are not cached, so later models can retry them.
Images decode with createImageBitmap, which some lack. Radiance files, with the MIME type images.HDR, decode in the kit on any thread:
On React Native, react-native-webgpu installs it on the JS thread only. It ignores colorSpaceConversion: 'none', so an image with a colour profile may decode differently than on the web.
, including hosted scenes, lack createImageBitmap. Decode on the JS thread with images.decodeBitmap, then pass the model with those bitmaps in the scene’s options. A bitmap is a native object, so it reaches the worker as the device does.
Alternatively, supply decode returning { width, height, pixels }, with four unpremultiplied bytes per pixel and rows from the top. A Skia decoder can provide this. Encoded images without a decoder cause add to throw.
Build shaders from root.pipe(pool.inject()) to call meshPool.vertexAt(i). It decodes a pool vertex into the @typegpu/kit/geometry vertex type. Bind pool.indices and draw a mesh range:
Bakes a shape into the next free ranges in a compute pass. Given a frame or an encoder, it
records there; otherwise, it submits at once. The bake's pipeline inlines the shape's
vertexAt and indexAt, and compiles, blocking, the first time the pool bakes those
functions, unless initAsync compiled it for the shapes option. Throws when the pool is
full.
Decodes pool vertex i into an @typegpu/kit/geometry vertex. In a mesh draw, the vertex index
already includes baseVertex. Create calling shaders from root.pipe(pool.inject()).
vertexAt(
i: number
i);// i already counts from the mesh's baseVertex
return{
position:d.v4f
position:
import cameras
cameras.
consttoClip:(point:d.v3f)=>d.v4f
Returns a world point in clip space, as a vertex shader's position. It reads cameras.current;
code that projects with a view of its own multiplies by that view's viewProjection.
Vertex data Depth passes can read only meshPool.positionAt(i). influencesAt(i) reads joints and weights. boundsOf(mesh.index) returns the mesh box in local space.
Textures Bind pool.texturesOf(mesh.index) before drawing a textured mesh. In the fragment shader, use materials.textured and materials.mappedNormal(normal, world, uv, uv1), with uv1 from meshPool.uv1At(i), which reads zero in a pool without uvs1.
Masked meshes For masked meshes, discard where materials.masked(uv, uv1, opacity) requests it.
Double-sided meshes Use cullMode: 'none' for double-sided meshes.
Your own samplingmeshPool.textureLayout exposes the bind-group layout for custom sampling. materials.uvOf(meshPool.textureLayout.$.reading.uvs.color, uv, uv1) gives the coordinates the colour texture reads. It reads uv1 only in a pipeline piped with the inject() of a pool made with uvs1.
Indices use u32 because WGSL has no 16-bit storage.
UVs use half floats. Their precision is 1/2048 below 1 and 1/16 between 64 and 128, so large tiled UV values can drift.
A material’s textures share one sampler.
Normal maps use per-pixel screen-space derivatives without tangents. Maps baked with MikkTSpace tangents can differ near UV seams or stretched coordinates.
storage bufferA storage buffer is GPU memory that shaders read by index and kernels can write, which is where the kit keeps rows.Words →
frameA frame is one turn of the loop, and the object your function gets for it, which holds the encoder, the time and the time since the last frame.You own the frame →
instanceAn instance is one copy of a mesh with its own position, rotation, scale and material, kept as a row of a table.Instances →
rendererA renderer draws one sort of thing, such as instances or particles, by handing a pass description a drawing.Every renderer is a drawing →
sRGBsRGB is the encoding screens and most image files use for colour, which a shader turns into linear light before it does maths on it.Words →
atlasAn atlas is one texture that holds many small pictures side by side, such as every glyph of a font or every view of a model.Words →
samplerA sampler tells the GPU how to read a texture between its texels and past its edges, such as smoothly or in blocks.Words →
runtimeA runtime is one JavaScript thread with its own memory, such as the page, a worker or a React Native worklet thread.Words →
HDRHDR, high dynamic range, is colour stored with more range than a screen shows, so a light can be brighter than white.A module borrows →
workletA worklet is a function that react-native-worklets can run on a thread other than the main JavaScript thread.Words →
5 to 7 buffers ·a uniform per way of reading textures ·a uniform per shape added ·a pipeline per pair of shape functions ·textures ·samplers ·bind groups ·what images makes
Positions, normals and uvs, indices and boxes, second uvs with uvs1, joints and weights once a skinned model comes, and the extras when extra names any; a bake per vertexAt and indexAt it bakes, and per shape added the uniform of its ranges and their group; each image decoded once per format it is read in, and a group per set of a mesh's textures.