tgpu
const
tgpu:object
Defined in: packages/typegpu/src/index.ts:22
Type declaration
Section titled “Type declaration”~unstable
Section titled “~unstable”~unstable:
object
~unstable.accessor()
Section titled “~unstable.accessor()”accessor: <
T
>(schema
,defaultValue
?) =>TgpuAccessor
<T
>
Type Parameters
Section titled “Type Parameters”• T extends AnyWgslData
Parameters
Section titled “Parameters”schema
Section titled “schema”T
defaultValue?
Section titled “defaultValue?”TgpuFn
<() => T
> | TgpuBufferUsage
<T
, BindableBufferUsage
> | Infer
<T
>
Returns
Section titled “Returns”TgpuAccessor
<T
>
~unstable.comparisonSampler()
Section titled “~unstable.comparisonSampler()”comparisonSampler: (
props
) =>TgpuFixedComparisonSampler
Parameters
Section titled “Parameters”ComparisonSamplerProps
Returns
Section titled “Returns”TgpuFixedComparisonSampler
~unstable.computeFn()
Section titled “~unstable.computeFn()”computeFn: (
options
) =>TgpuComputeFnShell
<{}><ComputeIn
>(options
) =>TgpuComputeFnShell
<ComputeIn
>
Parameters
Section titled “Parameters”options
Section titled “options”workgroupSize
Section titled “workgroupSize”number
[]
Returns
Section titled “Returns”Type Parameters
Section titled “Type Parameters”• ComputeIn extends IORecord
<AnyComputeBuiltin
>
Parameters
Section titled “Parameters”options
Section titled “options”ComputeIn
workgroupSize
Section titled “workgroupSize”number
[]
Returns
Section titled “Returns”TgpuComputeFnShell
<ComputeIn
>
~unstable.const()
Section titled “~unstable.const()”const: <
TDataType
>(dataType
,value
) =>TgpuConst
<TDataType
> =constant
Creates a module constant with specified value.
Type Parameters
Section titled “Type Parameters”• TDataType extends AnyWgslData
Parameters
Section titled “Parameters”dataType
Section titled “dataType”TDataType
InferGPU
<TDataType
>
Returns
Section titled “Returns”TgpuConst
<TDataType
>
~unstable.declare()
Section titled “~unstable.declare()”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.
Parameters
Section titled “Parameters”declaration
Section titled “declaration”string
Returns
Section titled “Returns”~unstable.derived()
Section titled “~unstable.derived()”derived: <
T
>(compute
) =>TgpuDerived
<T
>
Type Parameters
Section titled “Type Parameters”• T
Parameters
Section titled “Parameters”compute
Section titled “compute”() => T
Returns
Section titled “Returns”TgpuDerived
<T
>
~unstable.fn()
Section titled “~unstable.fn()”fn: <
Args
>(argTypes
,returnType
?) =>TgpuFnShell
<Args
,Void
><Args
,Return
>(argTypes
,returnType
) =>TgpuFnShell
<Args
,Return
>
Type Parameters
Section titled “Type Parameters”• Args extends [] | AnyData
[]
Parameters
Section titled “Parameters”argTypes
Section titled “argTypes”Args
returnType?
Section titled “returnType?”undefined
Returns
Section titled “Returns”TgpuFnShell
<Args
, Void
>
Type Parameters
Section titled “Type Parameters”• Args extends [] | AnyData
[]
• Return extends AnyData
Parameters
Section titled “Parameters”argTypes
Section titled “argTypes”Args
returnType
Section titled “returnType”Return
Returns
Section titled “Returns”TgpuFnShell
<Args
, Return
>
~unstable.fragmentFn()
Section titled “~unstable.fragmentFn()”fragmentFn: <
FragmentOut
>(options
) =>TgpuFragmentFnShell
<{},FragmentOut
><FragmentIn
,FragmentOut
>(options
) =>TgpuFragmentFnShell
<FragmentIn
,FragmentOut
>
Type Parameters
Section titled “Type Parameters”• FragmentOut extends FragmentOutConstrained
Parameters
Section titled “Parameters”options
Section titled “options”FragmentOut
Returns
Section titled “Returns”TgpuFragmentFnShell
<{}, FragmentOut
>
Type Parameters
Section titled “Type Parameters”• FragmentIn extends FragmentInConstrained
• FragmentOut extends FragmentOutConstrained
Parameters
Section titled “Parameters”options
Section titled “options”FragmentIn
FragmentOut
Returns
Section titled “Returns”TgpuFragmentFnShell
<FragmentIn
, FragmentOut
>
~unstable.privateVar()
Section titled “~unstable.privateVar()”privateVar: <
TDataType
>(dataType
,initialValue
?) =>TgpuVar
<"private"
,TDataType
>
Defines a variable scoped to each entry function (private).
Type Parameters
Section titled “Type Parameters”• TDataType extends AnyData
Parameters
Section titled “Parameters”dataType
Section titled “dataType”TDataType
The schema of the held data’s type
initialValue?
Section titled “initialValue?”InferGPU
<TDataType
>
If not provided, the variable will be initialized to the dataType’s “zero-value”.
Returns
Section titled “Returns”TgpuVar
<"private"
, TDataType
>
~unstable.sampler()
Section titled “~unstable.sampler()”sampler: (
props
) =>TgpuFixedSampler
Parameters
Section titled “Parameters”SamplerProps
Returns
Section titled “Returns”TgpuFixedSampler
~unstable.simulate()
Section titled “~unstable.simulate()”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.
Type Parameters
Section titled “Type Parameters”• T
Parameters
Section titled “Parameters”callback
Section titled “callback”() => T
The callback to run in the simulated environment.
Returns
Section titled “Returns”SimulationResult
<T
>
An object containing the result of the simulation, and the final state of the environment.
Example
Section titled “Example”const counter = tgpu.privateVar(d.u32);
const result = tgpu.simulate(() => { counter.$ += 1; counter.$ += 2; return counter.$;});
console.log(result.value); // 3
~unstable.slot()
Section titled “~unstable.slot()”slot: <
T
>(defaultValue
?) =>TgpuSlot
<T
>
Type Parameters
Section titled “Type Parameters”• T
Parameters
Section titled “Parameters”defaultValue?
Section titled “defaultValue?”T
Returns
Section titled “Returns”TgpuSlot
<T
>
~unstable.vertexFn()
Section titled “~unstable.vertexFn()”vertexFn: <
VertexOut
>(options
) =>TgpuVertexFnShell
<{},VertexOut
><VertexIn
,VertexOut
>(options
) =>TgpuVertexFnShell
<VertexIn
,VertexOut
>
Type Parameters
Section titled “Type Parameters”• VertexOut extends VertexOutConstrained
Parameters
Section titled “Parameters”options
Section titled “options”VertexOut
Returns
Section titled “Returns”TgpuVertexFnShell
<{}, VertexOut
>
Type Parameters
Section titled “Type Parameters”• VertexIn extends VertexInConstrained
• VertexOut extends VertexOutConstrained
Parameters
Section titled “Parameters”options
Section titled “options”VertexIn
VertexOut
Returns
Section titled “Returns”TgpuVertexFnShell
<VertexIn
, VertexOut
>
~unstable.vertexLayout()
Section titled “~unstable.vertexLayout()”vertexLayout: <
TData
>(schemaForCount
,stepMode
) =>TgpuVertexLayout
<TData
>
Type Parameters
Section titled “Type Parameters”• TData extends WgslArray
<BaseData
> | Disarray
<BaseData
>
Parameters
Section titled “Parameters”schemaForCount
Section titled “schemaForCount”(count
) => TData
stepMode
Section titled “stepMode”"vertex"
| "instance"
Returns
Section titled “Returns”TgpuVertexLayout
<TData
>
~unstable.workgroupVar()
Section titled “~unstable.workgroupVar()”workgroupVar: <
TDataType
>(dataType
) =>TgpuVar
<"workgroup"
,TDataType
>
Defines a variable scoped to the whole workgroup, shared between entry functions of the same invocation.
Type Parameters
Section titled “Type Parameters”• TDataType extends AnyData
Parameters
Section titled “Parameters”dataType
Section titled “dataType”TDataType
The schema of the held data’s type
Returns
Section titled “Returns”TgpuVar
<"workgroup"
, TDataType
>
bindGroupLayout()
Section titled “bindGroupLayout()”bindGroupLayout: <
Entries
>(entries
) =>TgpuBindGroupLayout
<{ [K in string | number | symbol]: Entries[K] }>
Type Parameters
Section titled “Type Parameters”• Entries extends Record
<string
, null
| TgpuLayoutEntry
>
Parameters
Section titled “Parameters”entries
Section titled “entries”Entries
Returns
Section titled “Returns”TgpuBindGroupLayout
<{ [K in string | number | symbol]: Entries[K] }>
fn: <
Args
>(argTypes
,returnType
?) =>TgpuFnShell
<Args
,Void
><Args
,Return
>(argTypes
,returnType
) =>TgpuFnShell
<Args
,Return
>
Type Parameters
Section titled “Type Parameters”• Args extends [] | AnyData
[]
Parameters
Section titled “Parameters”argTypes
Section titled “argTypes”Args
returnType?
Section titled “returnType?”undefined
Returns
Section titled “Returns”TgpuFnShell
<Args
, Void
>
Type Parameters
Section titled “Type Parameters”• Args extends [] | AnyData
[]
• Return extends AnyData
Parameters
Section titled “Parameters”argTypes
Section titled “argTypes”Args
returnType
Section titled “returnType”Return
Returns
Section titled “Returns”TgpuFnShell
<Args
, Return
>
init()
Section titled “init()”init: (
options
?) =>Promise
<TgpuRoot
>
Requests a new GPU device and creates a root around it. If a specific device should be used instead, use
Parameters
Section titled “Parameters”options?
Section titled “options?”Returns
Section titled “Returns”Promise
<TgpuRoot
>
initFromDevice.
Examples
Section titled “Examples”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()
Section titled “initFromDevice()”initFromDevice: (
options
) =>TgpuRoot
Creates a root from the given device, instead of requesting it like
Parameters
Section titled “Parameters”options
Section titled “options”Returns
Section titled “Returns”init.
Example
Section titled “Example”const device: GPUDevice = ...;const root = tgpu.initFromDevice({ device });
resolve()
Section titled “resolve()”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.
Parameters
Section titled “Parameters”options
Section titled “options”TgpuResolveOptions
The options for the resolution.
Returns
Section titled “Returns”string
The resolved code.
Example
Section titled “Example”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()
Section titled “resolveWithContext()”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.
Parameters
Section titled “Parameters”options
Section titled “options”TgpuResolveOptions
The options for the resolution.
Returns
Section titled “Returns”ResolutionResult
Example
Section titled “Example”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()
Section titled “slot()”slot: <
T
>(defaultValue
?) =>TgpuSlot
<T
>
Type Parameters
Section titled “Type Parameters”• T
Parameters
Section titled “Parameters”defaultValue?
Section titled “defaultValue?”T
Returns
Section titled “Returns”TgpuSlot
<T
>
vertexLayout()
Section titled “vertexLayout()”vertexLayout: <
TData
>(schemaForCount
,stepMode
) =>TgpuVertexLayout
<TData
>
Type Parameters
Section titled “Type Parameters”• TData extends WgslArray
<BaseData
> | Disarray
<BaseData
>
Parameters
Section titled “Parameters”schemaForCount
Section titled “schemaForCount”(count
) => TData
stepMode
Section titled “stepMode”"vertex"
| "instance"
Returns
Section titled “Returns”TgpuVertexLayout
<TData
>