Accessors
When writing reusable GPU functions in TypeGPU, you often need to reference βsome global valueβ without coupling to a specific buffer, texture or variable. Slots seem like the right tool for the job, but they encode the delivery mechanism into the slot type (e.g. a buffer or texture), making it inflexible for reusable functions.
// π only works with JS literalsconst const staticColorSlot: TgpuSlot<d.v3f>
staticColorSlot = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.slot: <d.v3f>(defaultValue?: d.v3f | undefined) => TgpuSlot<d.v3f>
slot<import d
d.export v3f
Interface representing its WGSL vector type counterpart: vec3f or vec3.
A vector with 3 elements of type f32
v3f>();
// π requires a uniform binding that only carries that valueconst const uniformColorSlot: TgpuSlot<TgpuUniform<d.Vec3f>>
uniformColorSlot = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.slot: <TgpuUniform<d.Vec3f>>(defaultValue?: TgpuUniform<d.Vec3f> | undefined) => TgpuSlot<TgpuUniform<d.Vec3f>>
slot<interface TgpuUniform<out TData extends BaseData>
TgpuUniform<import d
d.export Vec3f
Type of the d.vec3f object/function: vector data type schema/constructor
Vec3f>>();
// π requires the value to be returned from a functionconst const getColorSlot: TgpuSlot<() => d.v3f>
getColorSlot = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.slot: <() => d.v3f>(defaultValue?: (() => d.v3f) | undefined) => TgpuSlot<() => d.v3f>
slot<() => import d
d.export v3f
Interface representing its WGSL vector type counterpart: vec3f or vec3.
A vector with 3 elements of type f32
v3f>();
// π works with any value that matches the schemaconst const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <d.Vec3f>(schemaOrConstructor: d.Vec3f, defaultValue?: DataAccessorIn<NoInfer<d.Vec3f>> | undefined) => TgpuAccessor<d.Vec3f>
accessor(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f);Accessors fill this gap. They allow code to care only about the type of a value, delegating the delivery mechanism of the actual value to the caller. An accessor knows the data type of the resource it represents (d.vec3f, d.struct(...), etc.), and TypeGPU validates that whatever is bound to it matches that schema.
Basic usage
Section titled βBasic usageβCreate an accessor with tgpu.accessor(schema). Optionally pass a default value as the second argument:
// No default β must be bound before resolving.const const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <d.Vec3f>(schemaOrConstructor: d.Vec3f, defaultValue?: DataAccessorIn<NoInfer<d.Vec3f>> | undefined) => TgpuAccessor<d.Vec3f>
accessor(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f);
// With a default β used if no binding is provided.const const colorWithDefault: TgpuAccessor<d.Vec3f>
colorWithDefault = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <d.Vec3f>(schemaOrConstructor: d.Vec3f, defaultValue?: DataAccessorIn<NoInfer<d.Vec3f>> | undefined) => TgpuAccessor<d.Vec3f>
accessor(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f, import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(1, 0, 0));Inside a GPU function, read the accessorβs value using .$:
const const getColor: () => d.v3f
getColor = () => { 'use gpu'; return const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess.TgpuAccessor<Vec3f>.$: d.v3f
$; // reads the bound resource};If no default is set and no binding is provided at resolution time, TypeGPU throws a descriptive error:
const const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <d.Vec3f>(schemaOrConstructor: d.Vec3f, defaultValue?: DataAccessorIn<NoInfer<d.Vec3f>> | undefined) => TgpuAccessor<d.Vec3f>
accessor(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f);
function function getColor(): d.v3f
getColor() { 'use gpu'; return const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess.TgpuAccessor<Vec3f>.$: d.v3f
$;}
const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.resolve: (items: ResolvableObject[], options?: TgpuResolveOptions) => string (+1 overload)
A shorthand for calling tgpu.resolveWithContext(...).code.
resolve([function getColor(): d.v3f
getColor]);// Error: Resolution of the following tree failed:// - <root>// - fn:getColor// - accessor:colorAccess: Missing value for 'slot:colorAccess'Overriding the binding
Section titled βOverriding the bindingβUse .with(accessor, value) on a function or pipeline β the same pattern as slots:
const const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <d.Vec3f>(schemaOrConstructor: d.Vec3f, defaultValue?: DataAccessorIn<NoInfer<d.Vec3f>> | undefined) => TgpuAccessor<d.Vec3f>
accessor(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f, const RED: d.v3f
RED); // red by default
function function getColor(): d.v3f
getColor() { 'use gpu'; return const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess.TgpuAccessor<Vec3f>.$: d.v3f
$;}
// Override the default with green for a specific variant.const const getGreenColor: TgpuGenericFn<() => d.v3f>
getGreenColor = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.fn: <() => d.v3f>(inner: () => d.v3f) => TgpuGenericFn<() => d.v3f> (+2 overloads)
fn(function getColor(): d.v3f
getColor).Withable<TgpuGenericFn<() => v3f>>.with<d.Vec3f>(accessor: TgpuAccessor<d.Vec3f>, value: DataAccessorIn<NoInfer<d.Vec3f>>): TgpuGenericFn<() => d.v3f> (+2 overloads)
with(const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess, const GREEN: d.v3f
GREEN);The two functions resolve to separate WGSL:
// getColor (uses default)fn getColor() -> vec3f { return vec3f(1, 0, 0);}
// getColor_1 (overridden)fn getColor_1() -> vec3f { return vec3f(0, 1, 0);}You can also override on a pipeline:
const const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <d.Vec3f>(schemaOrConstructor: d.Vec3f, defaultValue?: DataAccessorIn<NoInfer<d.Vec3f>> | undefined) => TgpuAccessor<d.Vec3f>
accessor(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f, import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(1, 0, 0));
const const computeEntry: TgpuComputeFn<{}>
computeEntry = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.computeFn: (options: { workgroupSize: number[];}) => TgpuComputeFnShell<{}> (+1 overload)
computeFn({ workgroupSize: number[]
workgroupSize: [1] })(() => { const const _color: d.v3f
_color = const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess.TgpuAccessor<Vec3f>.$: d.v3f
$;});
const const pipeline: TgpuComputePipeline
pipeline = const root: TgpuRoot
root .Withable<WithBinding>.with<d.Vec3f>(accessor: TgpuAccessor<d.Vec3f>, value: DataAccessorIn<NoInfer<d.Vec3f>>): WithBinding (+2 overloads)
with(const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess, import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0, 0, 1)) // override to blue for this pipeline .WithBinding.createComputePipeline<{}>(descriptor: TgpuComputePipeline.Descriptor<{}>): TgpuComputePipeline
createComputePipeline({ compute: TgpuComputeFn<{}>
compute: const computeEntry: TgpuComputeFn<{}>
computeEntry });Supported binding types
Section titled βSupported binding typesβAccessors accept several types of GPU resources as values.
Raw JS value (inlined as a literal)
Section titled βRaw JS value (inlined as a literal)βThe simplest binding β the value is resolved at compile time and inlined directly into the WGSL:
const const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <d.Vec3f>(schemaOrConstructor: d.Vec3f, defaultValue?: DataAccessorIn<NoInfer<d.Vec3f>> | undefined) => TgpuAccessor<d.Vec3f>
accessor(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f);const const multiplierAccess: TgpuAccessor<d.F32>
multiplierAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <d.F32>(schemaOrConstructor: d.F32, defaultValue?: DataAccessorIn<NoInfer<d.F32>> | undefined) => TgpuAccessor<d.F32>
accessor(import d
d.const f32: d.F32export f32
A schema that represents a 32-bit float value. (equivalent to f32 in WGSL)
Can also be called to cast a value to an f32.
f32);
function function getColor(): d.v3f
getColor() { 'use gpu'; return const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess.TgpuAccessor<Vec3f>.$: d.v3f
$ * const multiplierAccess: TgpuAccessor<d.F32>
multiplierAccess.TgpuAccessor<F32>.$: number
$;}
const const getDoubleRed: TgpuGenericFn<() => d.v3f>
getDoubleRed = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu .fn: <() => d.v3f>(inner: () => d.v3f) => TgpuGenericFn<() => d.v3f> (+2 overloads)
fn(function getColor(): d.v3f
getColor) .Withable<TgpuGenericFn<() => v3f>>.with<d.Vec3f>(accessor: TgpuAccessor<d.Vec3f>, value: DataAccessorIn<NoInfer<d.Vec3f>>): TgpuGenericFn<() => d.v3f> (+2 overloads)
with(const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess, import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(1, 0, 0)) .Withable<TgpuGenericFn<() => v3f>>.with<d.F32>(accessor: TgpuAccessor<d.F32>, value: DataAccessorIn<NoInfer<d.F32>>): TgpuGenericFn<() => d.v3f> (+2 overloads)
with(const multiplierAccess: TgpuAccessor<d.F32>
multiplierAccess, 2);// Generated WGSLfn getColor() -> vec3f { return vec3f(2, 0, 0);}GPU function or shellless callback
Section titled βGPU function or shellless callbackβResolves to a function call in the generated WGSL:
const const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <d.Vec3f>(schemaOrConstructor: d.Vec3f, defaultValue?: DataAccessorIn<NoInfer<d.Vec3f>> | undefined) => TgpuAccessor<d.Vec3f>
accessor(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f, () => { 'use gpu'; return import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(1, 2, 3);});
function function getColor(): d.v3f
getColor() { 'use gpu'; return const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess.TgpuAccessor<Vec3f>.$: d.v3f
$;}fn colorAccess() -> vec3f { return vec3f(1, 2, 3);}
fn getColor() -> vec3f { return colorAccess();}Buffer usage
Section titled βBuffer usageβResolves to the WGSL variable declaration for that buffer binding:
const const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <d.Vec3f>(schemaOrConstructor: d.Vec3f, defaultValue?: DataAccessorIn<NoInfer<d.Vec3f>> | undefined) => TgpuAccessor<d.Vec3f>
accessor(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f);
function function getColor(): d.v3f
getColor() { 'use gpu'; return const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess.TgpuAccessor<Vec3f>.$: d.v3f
$;}
const const redUniform: TgpuUniform<d.Vec3f>
redUniform = const root: TgpuRoot
root.TgpuRoot.createUniform<d.Vec3f>(typeSchema: d.Vec3f, initial?: d.v3f | undefined): TgpuUniform<d.Vec3f> (+1 overload)
Allocates memory on the GPU, allows passing data between host and shader.
Read-only on the GPU, optimized for small data. For a general-purpose buffer,
use
TgpuRoot.createBuffer
.
createUniform(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f, const RED: d.v3f
RED);const const getRed: TgpuGenericFn<() => d.v3f>
getRed = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.fn: <() => d.v3f>(inner: () => d.v3f) => TgpuGenericFn<() => d.v3f> (+2 overloads)
fn(function getColor(): d.v3f
getColor).Withable<TgpuGenericFn<() => v3f>>.with<d.Vec3f>(accessor: TgpuAccessor<d.Vec3f>, value: DataAccessorIn<NoInfer<d.Vec3f>>): TgpuGenericFn<() => d.v3f> (+2 overloads)
with(const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess, const redUniform: TgpuUniform<d.Vec3f>
redUniform);@group(0) @binding(0) var<uniform> redUniform: vec3f;
fn getColor() -> vec3f { return redUniform;}Bind group layout entry
Section titled βBind group layout entryβA common pattern for library-style code: use a bind group layout entry as the default, making it easy to swap the resource via .with():
const const ImageData: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>
ImageData = (count: number
count: number) => import d
d.struct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>(props: { width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}): d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>export struct
Creates a struct schema that can be used to construct GPU buffers.
Ensures proper alignment and padding of properties (as opposed to a d.unstruct schema).
The order of members matches the passed in properties object.
struct({ width: d.U32
width: import d
d.const u32: d.U32export u32
A schema that represents an unsigned 32-bit integer value. (equivalent to u32 in WGSL)
Can also be called to cast a value to an u32 in accordance with WGSL casting rules.
u32, height: d.U32
height: import d
d.const u32: d.U32export u32
A schema that represents an unsigned 32-bit integer value. (equivalent to u32 in WGSL)
Can also be called to cast a value to an u32 in accordance with WGSL casting rules.
u32, pixels: d.WgslArray<d.Vec4f>
pixels: import d
d.arrayOf<d.Vec4f>(elementType: d.Vec4f, elementCount: number): d.WgslArray<d.Vec4f> (+1 overload)export arrayOf
Creates an array schema that can be used to construct gpu buffers.
Describes arrays with fixed-size length, storing elements of the same type.
arrayOf(import d
d.const vec4f: d.Vec4fexport vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f, count: number
count), });
const const layout: TgpuBindGroupLayout<{ image: { storage: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>; }>; access: "readonly"; };}>
layout = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.bindGroupLayout: <{ image: { storage: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>; }>; access: "readonly"; };}>(entries: { image: { storage: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>; }>; access: "readonly"; };}) => TgpuBindGroupLayout<{ image: { storage: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>; }>; access: "readonly"; };}> (+1 overload)
bindGroupLayout({ image: { storage: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>; }>; access: "readonly";}
image: { storage: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>
storage: const ImageData: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>
ImageData, access: "readonly"
access: 'readonly' },});
// Default is the layout entry, but can be overridden.const const imageAccess: TgpuAccessor<d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>>
imageAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <(count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>>(schemaOrConstructor: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>, defaultValue?: DataAccessorIn<d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>> | undefined) => TgpuAccessor<d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>>
accessor(const ImageData: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>
ImageData, () => const layout: TgpuBindGroupLayout<{ image: { storage: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>; }>; access: "readonly"; };}>
layout.TgpuBindGroupLayout<{ image: { storage: (count: number) => WgslStruct<{ width: U32; height: U32; pixels: WgslArray<Vec4f>; }>; access: "readonly"; }; }>.$: { image: { width: number; height: number; pixels: d.v4f[]; };}
$.image: { width: number; height: number; pixels: d.v4f[];}
image);Variable
Section titled βVariableβBind a private or workgroup variable:
const const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <d.Vec3f>(schemaOrConstructor: d.Vec3f, defaultValue?: DataAccessorIn<NoInfer<d.Vec3f>> | undefined) => TgpuAccessor<d.Vec3f>
accessor(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f);
const const getColor: TgpuFn<() => d.Vec3f>
getColor = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.fn: <[], d.Vec3f>(argTypes: [], returnType: d.Vec3f) => TgpuFnShell<[], d.Vec3f> (+2 overloads)
fn([], import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f)(() => { 'use gpu'; return const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess.TgpuAccessor<Vec3f>.$: d.v3f
$;});
const const privateColor: TgpuVar<"private", d.Vec3f>
privateColor = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.privateVar: <d.Vec3f>(dataType: d.Vec3f, initialValue?: d.v3f | undefined) => TgpuVar<"private", d.Vec3f>
Defines a variable scoped to each entry function (private).
privateVar(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f);const const workgroupColor: TgpuVar<"workgroup", d.Vec3f>
workgroupColor = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.workgroupVar: <d.Vec3f>(dataType: d.Vec3f) => TgpuVar<"workgroup", d.Vec3f>
Defines a variable scoped to the whole workgroup, shared between entry functions
of the same invocation.
workgroupVar(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f);
const const getColorPrivate: TgpuFn<() => d.Vec3f>
getColorPrivate = const getColor: TgpuFn<() => d.Vec3f>
getColor.Withable<TgpuFn<() => Vec3f>>.with<d.Vec3f>(accessor: TgpuAccessor<d.Vec3f>, value: DataAccessorIn<NoInfer<d.Vec3f>>): TgpuFn<() => d.Vec3f> (+2 overloads)
with(const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess, const privateColor: TgpuVar<"private", d.Vec3f>
privateColor);const const getColorWorkgroup: TgpuFn<() => d.Vec3f>
getColorWorkgroup = const getColor: TgpuFn<() => d.Vec3f>
getColor.Withable<TgpuFn<() => Vec3f>>.with<d.Vec3f>(accessor: TgpuAccessor<d.Vec3f>, value: DataAccessorIn<NoInfer<d.Vec3f>>): TgpuFn<() => d.Vec3f> (+2 overloads)
with(const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess, const workgroupColor: TgpuVar<"workgroup", d.Vec3f>
workgroupColor);var<private> privateColor: vec3f;
fn getColor() -> vec3f { return privateColor;}
var<workgroup> workgroupColor: vec3f;
fn getColor_1() -> vec3f { return workgroupColor;}Constant
Section titled βConstantβconst const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <d.Vec3f>(schemaOrConstructor: d.Vec3f, defaultValue?: DataAccessorIn<NoInfer<d.Vec3f>> | undefined) => TgpuAccessor<d.Vec3f>
accessor(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f);
const const getColor: TgpuFn<() => d.Vec3f>
getColor = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.fn: <[], d.Vec3f>(argTypes: [], returnType: d.Vec3f) => TgpuFnShell<[], d.Vec3f> (+2 overloads)
fn([], import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f)(() => { 'use gpu'; return const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess.TgpuAccessor<Vec3f>.$: d.v3f
$;});
const const constantColor: TgpuConst<d.Vec3f>
constantColor = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.const: <d.Vec3f>(dataType: d.Vec3f, value: d.v3f) => TgpuConst<d.Vec3f>
Creates a module constant with specified value.
const(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f, import d
d.function vec3f(x: number, y: number, z: number): d.v3f (+5 overloads)export vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f(0.1, 0.5, 0.3));const const getColorConst: TgpuFn<() => d.Vec3f>
getColorConst = const getColor: TgpuFn<() => d.Vec3f>
getColor.Withable<TgpuFn<() => Vec3f>>.with<d.Vec3f>(accessor: TgpuAccessor<d.Vec3f>, value: DataAccessorIn<NoInfer<d.Vec3f>>): TgpuFn<() => d.Vec3f> (+2 overloads)
with(const colorAccess: TgpuAccessor<d.Vec3f>
colorAccess, const constantColor: TgpuConst<d.Vec3f>
constantColor);const constantColor: vec3f = vec3f(0.10000000149011612, 0.5, 0.30000001192092896);
fn getColor() -> vec3f { return constantColor;}Texture view
Section titled βTexture viewβconst const texture: TgpuTexture<{ size: [100, 100]; format: "rgba8unorm";}> & StorageFlag
texture = const root: TgpuRoot
root['~unstable'] .createTexture<number, number, number, [100, 100], "rgba8unorm", number, number, GPUTextureFormat[], GPUTextureDimension>(props: CreateTextureOptions<[100, 100], "rgba8unorm", number, number, GPUTextureFormat[], GPUTextureDimension>): TgpuTexture<{ size: [100, 100]; format: "rgba8unorm";}>
createTexture({ format: "rgba8unorm"
The format of the texture.
format: 'rgba8unorm', size: [100, 100]
The width, height, and depth or layer count of the texture.
size: [100, 100] }) .TgpuTexture<{ size: [100, 100]; format: "rgba8unorm"; }>.$usage<["storage"]>(usages_0: "storage"): TgpuTexture<{ size: [100, 100]; format: "rgba8unorm";}> & StorageFlag
$usage('storage');
const const storageView: TgpuTextureView<d.WgslStorageTexture2d<"rgba8unorm", "write-only">>
storageView = const texture: TgpuTexture<{ size: [100, 100]; format: "rgba8unorm";}> & StorageFlag
texture.TgpuTexture<{ size: [100, 100]; format: "rgba8unorm"; }>.createView<d.WgslStorageTexture2d<"rgba8unorm", "write-only">>(schema: d.WgslStorageTexture2d<"rgba8unorm", "write-only">, viewDescriptor?: TgpuTextureViewDescriptor): TgpuTextureView<d.WgslStorageTexture2d<"rgba8unorm", "write-only">> (+3 overloads)
createView(import d
d.textureStorage2d<"rgba8unorm">(format: "rgba8unorm"): d.WgslStorageTexture2d<"rgba8unorm", "write-only"> (+1 overload)export textureStorage2d
textureStorage2d('rgba8unorm'));
const const textureAccess: TgpuAccessor<d.WgslStorageTexture2d<"rgba8unorm", "write-only">>
textureAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <d.WgslStorageTexture2d<"rgba8unorm", "write-only">>(schemaOrConstructor: d.WgslStorageTexture2d<"rgba8unorm", "write-only">, defaultValue?: TextureAccessorIn<NoInfer<d.WgslStorageTexture2d<"rgba8unorm", "write-only">>> | undefined) => TgpuAccessor<d.WgslStorageTexture2d<"rgba8unorm", "write-only">>
accessor(import d
d.textureStorage2d<"rgba8unorm">(format: "rgba8unorm"): d.WgslStorageTexture2d<"rgba8unorm", "write-only"> (+1 overload)export textureStorage2d
textureStorage2d('rgba8unorm'), const storageView: TgpuTextureView<d.WgslStorageTexture2d<"rgba8unorm", "write-only">>
storageView);
function function main(): void
main() { 'use gpu'; import std
std.textureStore<d.textureStorage2d<"rgba8unorm", "write-only">>(texture: d.textureStorage2d<"rgba8unorm", "write-only">, coords: d.v2i | d.v2u, value: d.v4f): void (+3 overloads)export textureStore
textureStore(const textureAccess: TgpuAccessor<d.WgslStorageTexture2d<"rgba8unorm", "write-only">>
textureAccess.TgpuAccessor<WgslStorageTexture2d<"rgba8unorm", "write-only">>.$: d.textureStorage2d<"rgba8unorm", "write-only">
$, import d
d.function vec2u(): d.v2u (+3 overloads)export vec2u
Schema representing vec2u - a vector with 2 elements of type u32.
Also a constructor function for this vector value.
vec2u(), import d
d.function vec4f(xyzw: number): d.v4f (+9 overloads)export vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f(1));}@group(0) @binding(0) var storageView: texture_storage_2d<rgba8unorm, write>;
fn main() { textureStore(storageView, vec2u(), vec4f(1));}Deep and nested property access
Section titled βDeep and nested property accessβBecause accessors know the schema of their resource, you can access struct fields directly through .$:
const const ImageData: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>
ImageData = (count: number
count: number) => import d
d.struct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>(props: { width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}): d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>export struct
Creates a struct schema that can be used to construct GPU buffers.
Ensures proper alignment and padding of properties (as opposed to a d.unstruct schema).
The order of members matches the passed in properties object.
struct({ width: d.U32
width: import d
d.const u32: d.U32export u32
A schema that represents an unsigned 32-bit integer value. (equivalent to u32 in WGSL)
Can also be called to cast a value to an u32 in accordance with WGSL casting rules.
u32, height: d.U32
height: import d
d.const u32: d.U32export u32
A schema that represents an unsigned 32-bit integer value. (equivalent to u32 in WGSL)
Can also be called to cast a value to an u32 in accordance with WGSL casting rules.
u32, pixels: d.WgslArray<d.Vec4f>
pixels: import d
d.arrayOf<d.Vec4f>(elementType: d.Vec4f, elementCount: number): d.WgslArray<d.Vec4f> (+1 overload)export arrayOf
Creates an array schema that can be used to construct gpu buffers.
Describes arrays with fixed-size length, storing elements of the same type.
arrayOf(import d
d.const vec4f: d.Vec4fexport vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f, count: number
count), });
const const layout: TgpuBindGroupLayout<{ image: { storage: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>; }>; access: "readonly"; };}>
layout = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.bindGroupLayout: <{ image: { storage: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>; }>; access: "readonly"; };}>(entries: { image: { storage: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>; }>; access: "readonly"; };}) => TgpuBindGroupLayout<{ image: { storage: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>; }>; access: "readonly"; };}> (+1 overload)
bindGroupLayout({ image: { storage: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>; }>; access: "readonly";}
image: { storage: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>
storage: const ImageData: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>
ImageData, access: "readonly"
access: 'readonly' },});
const const imageAccess: TgpuAccessor<d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>>
imageAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <(count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>>(schemaOrConstructor: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>, defaultValue?: DataAccessorIn<d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>> | undefined) => TgpuAccessor<d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>>
accessor(const ImageData: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>
ImageData, () => const layout: TgpuBindGroupLayout<{ image: { storage: (count: number) => d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>; }>; access: "readonly"; };}>
layout.TgpuBindGroupLayout<{ image: { storage: (count: number) => WgslStruct<{ width: U32; height: U32; pixels: WgslArray<Vec4f>; }>; access: "readonly"; }; }>.$: { image: { width: number; height: number; pixels: d.v4f[]; };}
$.image: { width: number; height: number; pixels: d.v4f[];}
image);
const const getPixel: (x: number, y: number) => d.v4f
getPixel = (x: number
x: number, y: number
y: number) => { 'use gpu'; const const width: number
width = const imageAccess: TgpuAccessor<d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>>
imageAccess.TgpuAccessor<WgslStruct<{ width: U32; height: U32; pixels: WgslArray<Vec4f>; }>>.$: { width: number; height: number; pixels: d.v4f[];}
$.width: number
width; // struct field access const const pixels: d.v4f[]
pixels = const imageAccess: TgpuAccessor<d.WgslStruct<{ width: d.U32; height: d.U32; pixels: d.WgslArray<d.Vec4f>;}>>
imageAccess.TgpuAccessor<WgslStruct<{ width: U32; height: U32; pixels: WgslArray<Vec4f>; }>>.$: { width: number; height: number; pixels: d.v4f[];}
$.pixels: d.v4f[]
pixels; return import d
d.function vec4f(v: AnyNumericVec4Instance): d.v4f (+9 overloads)export vec4f
Schema representing vec4f - a vector with 4 elements of type f32.
Also a constructor function for this vector value.
vec4f(const pixels: d.v4f[]
pixels[x: number
x + y: number
y * const width: number
width]!);};This resolves correctly to WGSL field accesses:
struct item { width: u32, height: u32, pixels: array<vec4f>,}
@group(0) @binding(0) var<storage, read> image: item;
fn getPixel(x: i32, y: i32) -> vec4f { let width = image.width; let pixels = (&image.pixels); return (*pixels)[(x + (y * i32(width)))];}You can also access deeply nested references β e.g., a specific element of a bufferβs array field:
const const boids: TgpuReadonly<d.WgslArray<d.WgslStruct<{ pos: d.Vec3f;}>>>
boids = const root: TgpuRoot
root.TgpuRoot.createReadonly<d.WgslArray<d.WgslStruct<{ pos: d.Vec3f;}>>>(typeSchema: d.WgslArray<d.WgslStruct<{ pos: d.Vec3f;}>>, initial?: { pos: d.v3f;}[] | undefined): TgpuReadonly<d.WgslArray<d.WgslStruct<{ pos: d.Vec3f;}>>> (+1 overload)
Allocates memory on the GPU, allows passing data between host and shader.
Read-only on the GPU, optimized for large data. For a general-purpose buffer,
use
TgpuRoot.createBuffer
.
createReadonly(const BoidArray: d.WgslArray<d.WgslStruct<{ pos: d.Vec3f;}>>
BoidArray);
// Access the first boid's position through the accessor.const const anyBoidPosAccess: TgpuAccessor<d.Vec3f>
anyBoidPosAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.accessor: <d.Vec3f>(schemaOrConstructor: d.Vec3f, defaultValue?: DataAccessorIn<NoInfer<d.Vec3f>> | undefined) => TgpuAccessor<d.Vec3f>
accessor(import d
d.const vec3f: d.Vec3fexport vec3f
Schema representing vec3f - a vector with 3 elements of type f32.
Also a constructor function for this vector value.
vec3f, () => const boids: TgpuReadonly<d.WgslArray<d.WgslStruct<{ pos: d.Vec3f;}>>>
boids.TgpuReadonly<WgslArray<WgslStruct<{ pos: Vec3f; }>>>.$: { pos: d.v3f;}[]
$[0]!.pos: d.v3f
pos);
function function main(): void
main() { 'use gpu'; const const firstX: number
firstX = const anyBoidPosAccess: TgpuAccessor<d.Vec3f>
anyBoidPosAccess.TgpuAccessor<Vec3f>.$: d.v3f
$.v3f.x: number
x;}struct Boid { pos: vec3f,}
@group(0) @binding(0) var<storage, read> boids: array<Boid, 100>;
fn main() { let firstX = boids[0].pos.x;}Mutable accessors
Section titled βMutable accessorsβUse tgpu.mutableAccessor when you need to write to the resource:
const const Ctx: d.WgslStruct<{ counter: d.F32;}>
Ctx = import d
d.struct<{ counter: d.F32;}>(props: { counter: d.F32;}): d.WgslStruct<{ counter: d.F32;}>export struct
Creates a struct schema that can be used to construct GPU buffers.
Ensures proper alignment and padding of properties (as opposed to a d.unstruct schema).
The order of members matches the passed in properties object.
struct({ counter: d.F32
counter: import d
d.const f32: d.F32export f32
A schema that represents a 32-bit float value. (equivalent to f32 in WGSL)
Can also be called to cast a value to an f32.
f32 });const const ctx: TgpuMutable<d.WgslStruct<{ counter: d.F32;}>>
ctx = const root: TgpuRoot
root.TgpuRoot.createMutable<d.WgslStruct<{ counter: d.F32;}>>(typeSchema: d.WgslStruct<{ counter: d.F32;}>, initial?: { counter: number;} | undefined): TgpuMutable<d.WgslStruct<{ counter: d.F32;}>> (+1 overload)
Allocates memory on the GPU, allows passing data between host and shader.
Can be mutated in-place on the GPU. For a general-purpose buffer,
use
TgpuRoot.createBuffer
.
createMutable(const Ctx: d.WgslStruct<{ counter: d.F32;}>
Ctx);
const const counterAccess: TgpuMutableAccessor<d.F32>
counterAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.mutableAccessor: <d.F32>(schemaOrConstructor: d.F32, defaultValue?: MutableDataAccessorIn<NoInfer<d.F32>> | undefined) => TgpuMutableAccessor<d.F32>
mutableAccessor(import d
d.const f32: d.F32export f32
A schema that represents a 32-bit float value. (equivalent to f32 in WGSL)
Can also be called to cast a value to an f32.
f32, () => const ctx: TgpuMutable<d.WgslStruct<{ counter: d.F32;}>>
ctx.TgpuMutable<WgslStruct<{ counter: F32; }>>.$: { counter: number;}
$.counter: number
counter);
function function main(): void
main() { 'use gpu'; const counterAccess: TgpuMutableAccessor<d.F32>
counterAccess.TgpuMutableAccessor<F32>.$: number
$ += 1; // write through the accessor}struct Ctx { counter: f32,}
@group(0) @binding(0) var<storage, read_write> ctx: Ctx;
fn main() { ctx.counter += 1f;}The split between tgpu.accessor and tgpu.mutableAccessor is useful, as the read-only restriction on tgpu.accessor is both a contract between the function and the caller, as well as allows for more varied delivery mechanisms (e.g. a getter function that computes a value is inherently read-only).
You can also mutate fields of a non-primitive struct:
const const boids: TgpuMutable<d.WgslArray<d.WgslStruct<{ pos: d.Vec3f;}>>>
boids = const root: TgpuRoot
root.TgpuRoot.createMutable<d.WgslArray<d.WgslStruct<{ pos: d.Vec3f;}>>>(typeSchema: d.WgslArray<d.WgslStruct<{ pos: d.Vec3f;}>>, initial?: { pos: d.v3f;}[] | undefined): TgpuMutable<d.WgslArray<d.WgslStruct<{ pos: d.Vec3f;}>>> (+1 overload)
Allocates memory on the GPU, allows passing data between host and shader.
Can be mutated in-place on the GPU. For a general-purpose buffer,
use
TgpuRoot.createBuffer
.
createMutable(const BoidArray: d.WgslArray<d.WgslStruct<{ pos: d.Vec3f;}>>
BoidArray);
const const boidAccess: TgpuMutableAccessor<d.WgslStruct<{ pos: d.Vec3f;}>>
boidAccess = const tgpu: { const: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/constant/tgpuConstant").constant; fn: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/tgpuFn").fn; comptime: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/function/comptime").comptime; resolve: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolve; resolveWithContext: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/resolve/tgpuResolve").resolveWithContext; init: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").init; initFromDevice: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/root/init").initFromDevice; slot: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/slot").slot; lazy: typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/core/slot/lazy").lazy; ... 10 more ...; '~unstable': typeof import("/home/runner/work/TypeGPU/TypeGPU/packages/typegpu/src/tgpuUnstable");}
tgpu.mutableAccessor: <d.WgslStruct<{ pos: d.Vec3f;}>>(schemaOrConstructor: d.WgslStruct<{ pos: d.Vec3f;}>, defaultValue?: MutableDataAccessorIn<NoInfer<d.WgslStruct<{ pos: d.Vec3f;}>>> | undefined) => TgpuMutableAccessor<d.WgslStruct<{ pos: d.Vec3f;}>>
mutableAccessor(const Boid: d.WgslStruct<{ pos: d.Vec3f;}>
Boid, () => const boids: TgpuMutable<d.WgslArray<d.WgslStruct<{ pos: d.Vec3f;}>>>
boids.TgpuMutable<WgslArray<WgslStruct<{ pos: Vec3f; }>>>.$: { pos: d.v3f;}[]
$[0]!);
function function main(): void
main() { 'use gpu'; const boidAccess: TgpuMutableAccessor<d.WgslStruct<{ pos: d.Vec3f;}>>
boidAccess.TgpuMutableAccessor<WgslStruct<{ pos: Vec3f; }>>.$: { pos: d.v3f;}
$.pos: d.v3f
pos.v3f.x: number
x += 1;}struct Boid { pos: vec3f,}
@group(0) @binding(0) var<storage, read_write> boids: array<Boid, 100>;
fn main() { boids[0].pos.x += 1f;}Accessors vs Slots vs Bind Groups
Section titled βAccessors vs Slots vs Bind Groupsβ| Accessors | Slots | Bind Groups | |
|---|---|---|---|
| What can be bound | GPU resources (buffers, variables, textures, constants, functions, raw values) | Any JavaScript value | Buffers, textures, samplers |
| Schema-aware | Yes β schema is declared upfront, TypeGPU validates the binding | No | Yes (layout defines types) |
| Indirect access of deep fields | Yes β accessor.$ can access struct fields | No (a slot value cannot be a reference to a nested field) | N/A |
| Potentially mutable | Through tgpu.mutableAccessor | No | Yes (storage buffers) |
| When resolved | Compile time (shader generation) | Compile time (shader generation) | Runtime (before draw/dispatch) |
| Primary use case | Parameterising GPU functions over resources | Parameterising shader logic (callbacks, numbers, config) | Swapping resources between draw calls without recompiling |
Choose slots when you want to inject arbitrary JavaScript values or TypeGPU functions that affect shader generation β for example, a gravity function or a boolean compile-time flag.
Choose accessors when you want to decouple shader logic from how the data is sourced, or where the results are stored.
Choose bind groups when you need to swap resources at runtime without changing the compiled shader β the classic WebGPU pattern for per-frame or per-draw-call resource binding.
Accessors and slots are complementary β you can combine them. For example, an accessorβs default can reference a bind group layout entry (() => layout.$.image), and you can override it with .with() the same way as a slot.
For more on slots, see the Slots guide.
For more on bind groups, see the Bind Groups guide.