Skip to content

tgpu

const tgpu: object

Defined in: packages/typegpu/src/index.ts:22

~unstable: object

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

T extends AnyWgslData

T

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

TgpuAccessor<T>

comparisonSampler: (props) => TgpuFixedComparisonSampler

ComparisonSamplerProps

TgpuFixedComparisonSampler

computeFn: (options) => TgpuComputeFnShell<{}><ComputeIn>(options) => TgpuComputeFnShell<ComputeIn>

number[]

TgpuComputeFnShell<{}>

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<{}, FragmentOut><FragmentIn, FragmentOut>(options) => TgpuFragmentFnShell<FragmentIn, FragmentOut>

FragmentOut extends FragmentOutConstrained

FragmentOut

TgpuFragmentFnShell<{}, FragmentOut>

FragmentIn extends FragmentInConstrained

FragmentOut extends FragmentOutConstrained

FragmentIn

FragmentOut

TgpuFragmentFnShell<FragmentIn, FragmentOut>

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>

sampler: (props) => TgpuFixedSampler

SamplerProps

TgpuFixedSampler

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<{}, VertexOut><VertexIn, VertexOut>(options) => TgpuVertexFnShell<VertexIn, VertexOut>

VertexOut extends VertexOutConstrained

VertexOut

TgpuVertexFnShell<{}, 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 extends Record<string, null | TgpuLayoutEntry>

Entries

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

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 });

resolve: (options) => string

Resolves a template with external values. Each external 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.

TgpuResolveOptions

The options for the resolution.

string

The resolved code.

const Gradient = d.struct({
from: d.vec3f,
to: d.vec3f,
});
const resolved = 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(resolved);
// 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

Resolves a template with external values. Each external 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.

TgpuResolveOptions

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);
// }

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>