Skip to content

tgpu

const tgpu: object

~unstable: object

accessor: <T>(schema, defaultValue?) => TgpuAccessor<T>

T extends AnyWgslData

T

TgpuFn<() => T> | TgpuBufferUsage<T, BindableBufferUsage> | TgpuBufferShorthand<T> | Infer<T>

TgpuAccessor<T>

comptime: <T>(func) => TgpuComptime<T>

Creates a version of func that can called safely in a TypeGPU function to precompute and inject a value into the final shader code.

Note how the function passed into comptime doesn’t have to be marked with ‘use gpu’. That’s because the function doesn’t execute on the GPU, it gets executed before the shader code gets sent to the GPU.

T extends (…args) => unknown

T

TgpuComptime<T>

const injectRand01 = tgpu['~unstable']
.comptime(() => Math.random());
const getColor = (diffuse: d.v3f) => {
'use gpu';
const albedo = hsvToRgb(injectRand01(), 1, 0.5);
return albedo.mul(diffuse);
};

computeFn: (options) => TgpuComputeFnShell<object><ComputeIn>(options) => TgpuComputeFnShell<ComputeIn>

number[]

TgpuComputeFnShell<object>

ComputeIn extends IORecord<AnyComputeBuiltin>

ComputeIn

number[]

TgpuComputeFnShell<ComputeIn>

const: <TDataType>(dataType, value) => TgpuConst<TDataType> = constant

Creates a module constant with specified value.

TDataType extends AnyWgslData

TDataType

InferGPU<TDataType>

TgpuConst<TDataType>

declare: (declaration) => TgpuDeclare

Allows defining extra declarations that shall be included in the final WGSL code, when resolving objects that use them.

Using this API is generally discouraged, as it shouldn’t be necessary in any common scenario. It was developed to ensure full compatibility of TypeGPU programs with current and future versions of WGSL.

string

TgpuDeclare

derived: <T>(compute) => TgpuDerived<T>

T

() => T

TgpuDerived<T>

fn: <Args>(argTypes, returnType?) => TgpuFnShell<Args, Void><Args, Return>(argTypes, returnType) => TgpuFnShell<Args, Return>

Args extends [] | AnyData[]

Args

undefined

TgpuFnShell<Args, Void>

Args extends [] | AnyData[]

Return extends AnyData

Args

Return

TgpuFnShell<Args, Return>

fragmentFn: <FragmentOut>(options) => TgpuFragmentFnShell<object, FragmentOut><FragmentIn, FragmentOut>(options) => TgpuFragmentFnShell<FragmentIn, FragmentOut>

FragmentOut extends FragmentOutConstrained

FragmentOut

TgpuFragmentFnShell<object, FragmentOut>

FragmentIn extends FragmentInConstrained

FragmentOut extends FragmentOutConstrained

FragmentIn

FragmentOut

TgpuFragmentFnShell<FragmentIn, FragmentOut>

namespace: (options?) => Namespace

NamespaceOptions

Namespace

privateVar: <TDataType>(dataType, initialValue?) => TgpuVar<"private", TDataType>

Defines a variable scoped to each entry function (private).

TDataType extends AnyData

TDataType

The schema of the held data’s type

InferGPU<TDataType>

If not provided, the variable will be initialized to the dataType’s “zero-value”.

TgpuVar<"private", TDataType>

rawCodeSnippet: <TDataType>(expression, type, origin?) => TgpuRawCodeSnippet<TDataType>

An advanced API that creates a typed shader expression which can be injected into the final shader bundle upon use.

TDataType extends AnyData

string

The code snippet that will be injected in place of foo.$

TDataType

The type of the expression

RawCodeSnippetOrigin = 'runtime'

Where the value originates from.

— Which origin to choose?

Usually ‘runtime’ (the default) is a safe bet, but if you’re sure that the expression or computation is constant (either a reference to a constant, a numeric literal, or an operation on constants), then pass ‘constant’ as it might lead to better optimizations.

If what the expression is a direct reference to an existing value (e.g. a uniform, a storage binding, …), then choose from ‘uniform’, ‘mutable’, ‘readonly’, ‘workgroup’, ‘private’ or ‘handle’ depending on the address space of the referred value.

TgpuRawCodeSnippet<TDataType>

// An identifier that we know will be in the
// final shader bundle, but we cannot
// refer to it in any other way.
const existingGlobal = tgpu['~unstable']
.rawCodeSnippet('EXISTING_GLOBAL', d.f32, 'constant');
const foo = () => {
'use gpu';
return existingGlobal.$ * 2;
};
const wgsl = tgpu.resolve([foo]);
// fn foo() -> f32 {
// return EXISTING_GLOBAL * 2;
// }

simulate: <T>(callback) => SimulationResult<T>

Runs the provided callback in a simulated environment, giving it access to buffers and variables as if it were running on the GPU.

The result of the simulation is returned, and does not affect the actual GPU state, nor does it carry over to other simulations.

T

() => T

The callback to run in the simulated environment.

SimulationResult<T>

An object containing the result of the simulation, and the final state of the environment.

const counter = tgpu.privateVar(d.u32);
const result = tgpu.simulate(() => {
counter.$ += 1;
counter.$ += 2;
return counter.$;
});
console.log(result.value); // 3

slot: <T>(defaultValue?) => TgpuSlot<T>

T

T

TgpuSlot<T>

vertexFn: <VertexOut>(options) => TgpuVertexFnShell<object, VertexOut><VertexIn, VertexOut>(options) => TgpuVertexFnShell<VertexIn, VertexOut>

VertexOut extends VertexOutConstrained

VertexOut

TgpuVertexFnShell<object, VertexOut>

VertexIn extends VertexInConstrained

VertexOut extends VertexOutConstrained

VertexIn

VertexOut

TgpuVertexFnShell<VertexIn, VertexOut>

vertexLayout: <TData>(schemaForCount, stepMode) => TgpuVertexLayout<TData>

TData extends WgslArray<BaseData> | Disarray<BaseData>

(count) => TData

"vertex" | "instance"

TgpuVertexLayout<TData>

workgroupVar: <TDataType>(dataType) => TgpuVar<"workgroup", TDataType>

Defines a variable scoped to the whole workgroup, shared between entry functions of the same invocation.

TDataType extends AnyData

TDataType

The schema of the held data’s type

TgpuVar<"workgroup", TDataType>

bindGroupLayout: <Entries>(entries) => TgpuBindGroupLayout<{ [K in string | number | symbol]: Entries[K] }><Entries>(entries) => TgpuBindGroupLayout<{ [K in string | number | symbol]: MapLegacyTextureToUpToDate<Entries>[K] }>

Entries extends Record<string, null | TgpuLayoutEntry>

Entries

TgpuBindGroupLayout<{ [K in string | number | symbol]: Entries[K] }>

Entries extends Record<string, null | TgpuLegacyLayoutEntry>

Entries

TgpuBindGroupLayout<{ [K in string | number | symbol]: MapLegacyTextureToUpToDate<Entries>[K] }>

const: <TDataType>(dataType, value) => TgpuConst<TDataType> = constant

Creates a module constant with specified value.

TDataType extends AnyWgslData

TDataType

InferGPU<TDataType>

TgpuConst<TDataType>

fn: <Args>(argTypes, returnType?) => TgpuFnShell<Args, Void><Args, Return>(argTypes, returnType) => TgpuFnShell<Args, Return>

Args extends [] | AnyData[]

Args

undefined

TgpuFnShell<Args, Void>

Args extends [] | AnyData[]

Return extends AnyData

Args

Return

TgpuFnShell<Args, Return>

init: (options?) => Promise<TgpuRoot>

Requests a new GPU device and creates a root around it. If a specific device should be used instead, use

InitOptions

Promise<TgpuRoot>

initFromDevice.

When given no options, the function will ask the browser for a suitable GPU device.

const root = await tgpu.init();

If there are specific options that should be used when requesting a device, you can pass those in.

const adapterOptions: GPURequestAdapterOptions = ...;
const deviceDescriptor: GPUDeviceDescriptor = ...;
const root = await tgpu.init({ adapter: adapterOptions, device: deviceDescriptor });

initFromDevice: (options) => TgpuRoot

Creates a root from the given device, instead of requesting it like

InitFromDeviceOptions

TgpuRoot

init.

const device: GPUDevice = ...;
const root = tgpu.initFromDevice({ device });

privateVar: <TDataType>(dataType, initialValue?) => TgpuVar<"private", TDataType>

Defines a variable scoped to each entry function (private).

TDataType extends AnyData

TDataType

The schema of the held data’s type

InferGPU<TDataType>

If not provided, the variable will be initialized to the dataType’s “zero-value”.

TgpuVar<"private", TDataType>

resolve: (options) => string(items, options?) => string

A shorthand for calling tgpu.resolveWithContext(...).code.

TgpuExtendedResolveOptions

string

const Gradient = d.struct({ from: d.vec3f, to: d.vec3f });
const resolved = tgpu.resolve([Gradient]);
console.log(resolved);
// struct Gradient_0 {
// from: vec3f,
// to: vec3f,
// }
const Gradient = d.struct({ from: d.vec3f, to: d.vec3f });
const code = tgpu.resolve({
template: `
fn getGradientAngle(gradient: Gradient) -> f32 {
return atan(gradient.to.y - gradient.from.y, gradient.to.x - gradient.from.x);
}
`,
externals: {
Gradient,
},
});
console.log(code);
// struct Gradient_0 {
// from: vec3f,
// to: vec3f,
// }
// fn getGradientAngle(gradient: Gradient_0) -> f32 {
// return atan(gradient.to.y - gradient.from.y, gradient.to.x - gradient.from.x);
// }

A shorthand for calling tgpu.resolveWithContext(...).code.

ResolvableObject[]

TgpuResolveOptions

string

const Gradient = d.struct({ from: d.vec3f, to: d.vec3f });
const resolved = tgpu.resolve([Gradient]);
console.log(resolved);
// struct Gradient_0 {
// from: vec3f,
// to: vec3f,
// }
const Gradient = d.struct({ from: d.vec3f, to: d.vec3f });
const code = tgpu.resolve({
template: `
fn getGradientAngle(gradient: Gradient) -> f32 {
return atan(gradient.to.y - gradient.from.y, gradient.to.x - gradient.from.x);
}
`,
externals: {
Gradient,
},
});
console.log(code);
// struct Gradient_0 {
// from: vec3f,
// to: vec3f,
// }
// fn getGradientAngle(gradient: Gradient_0) -> f32 {
// return atan(gradient.to.y - gradient.from.y, gradient.to.x - gradient.from.x);
// }

resolveWithContext: (options) => ResolutionResult(items, options?) => ResolutionResult

Resolves a template with external values. Each external that is used will get resolved to a code string and replaced in the template. Any dependencies of the externals will also be resolved and included in the output.

TgpuExtendedResolveOptions

The options for the resolution.

ResolutionResult

const Gradient = d.struct({ from: d.vec3f, to: d.vec3f });
const { code, usedBindGroupLayouts, catchall } = tgpu.resolveWithContext({
template: `
fn getGradientAngle(gradient: Gradient) -> f32 {
return atan(gradient.to.y - gradient.from.y, gradient.to.x - gradient.from.x);
}
`,
externals: {
Gradient,
},
});
console.log(code);
// struct Gradient_0 {
// from: vec3f,
// to: vec3f,
// }
// fn getGradientAngle(gradient: Gradient_0) -> f32 {
// return atan(gradient.to.y - gradient.from.y, gradient.to.x - gradient.from.x);
// }

Resolves given TypeGPU resources. Any dependencies of the externals will also be resolved and included in the output.

ResolvableObject[]

An array of items to resolve.

TgpuResolveOptions

The options for the resolution.

ResolutionResult

const Gradient = d.struct({
from: d.vec3f,
to: d.vec3f,
});
const { code, usedBindGroupLayouts, catchall } =
tgpu.resolveWithContext([Gradient]);
console.log(code);
// struct Gradient_0 {
// from: vec3f,
// to: vec3f,
// }

slot: <T>(defaultValue?) => TgpuSlot<T>

T

T

TgpuSlot<T>

vertexLayout: <TData>(schemaForCount, stepMode) => TgpuVertexLayout<TData>

TData extends WgslArray<BaseData> | Disarray<BaseData>

(count) => TData

"vertex" | "instance"

TgpuVertexLayout<TData>

workgroupVar: <TDataType>(dataType) => TgpuVar<"workgroup", TDataType>

Defines a variable scoped to the whole workgroup, shared between entry functions of the same invocation.

TDataType extends AnyData

TDataType

The schema of the held data’s type

TgpuVar<"workgroup", TDataType>

packages/typegpu/src/index.ts:25