Skip to content

Resolve

Defining shader schemas and objects in JS/TS has lots of benefits, but having to keep them in sync with the corresponding WGSL code is hard to maintain. The tgpu.resolve API takes in a WGSL template, all TypeGPU schemas that you want to use in the shader, and generates a ready-to-use WGSL bundle.

Here’s an example:

import
const tgpu: {
fn: {
<Args extends d.AnyData[] | []>(argTypes: Args, returnType?: undefined): TgpuFnShell<Args, d.Void>;
<Args extends d.AnyData[] | [], Return extends d.AnyData>(argTypes: Args, returnType: Return): TgpuFnShell<Args, Return>;
};
... 7 more ...;
'~unstable': {
...;
};
}
tgpu
from 'typegpu';
import * as
import d
d
from 'typegpu/data';
const
const LightSource: d.WgslStruct<{
ambientColor: d.Vec3f;
intensity: d.F32;
}>
LightSource
=
import d
d
.
struct<{
ambientColor: d.Vec3f;
intensity: d.F32;
}>(props: {
ambientColor: d.Vec3f;
intensity: d.F32;
}): d.WgslStruct<{
ambientColor: d.Vec3f;
intensity: 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.

@example const CircleStruct = d.struct({ radius: d.f32, pos: d.vec3f });

@paramprops Record with string keys and TgpuData values, each entry describing one struct member.

struct
({
ambientColor: d.Vec3f
ambientColor
:
import d
d
.
const vec3f: d.Vec3f
export vec3f

Schema representing vec3f - a vector with 3 elements of type f32. Also a constructor function for this vector value.

@example const vector = d.vec3f(); // (0.0, 0.0, 0.0) const vector = d.vec3f(1); // (1.0, 1.0, 1.0) const vector = d.vec3f(1, 2, 3.5); // (1.0, 2.0, 3.5)

@example const buffer = root.createBuffer(d.vec3f, d.vec3f(0, 1, 2)); // buffer holding a d.vec3f value, with an initial value of vec3f(0, 1, 2);

vec3f
,
intensity: d.F32
intensity
:
import d
d
.
const f32: d.F32
export 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.

@example const value = f32(); // 0

@example const value = f32(1.23); // 1.23

@example const value = f32(true); // 1

f32
,
})
.
TgpuNamable.$name(label: string): d.WgslStruct<{
ambientColor: d.Vec3f;
intensity: d.F32;
}>
$name
('Source');
// ^ giving the struct an explicit name (optional)
const
const layout: TgpuBindGroupLayout<{
lightSource: {
uniform: d.WgslStruct<{
ambientColor: d.Vec3f;
intensity: d.F32;
}>;
};
sampling: {
sampler: "filtering";
};
bgTexture: {
externalTexture: {};
};
}>
layout
=
const tgpu: {
fn: {
<Args extends d.AnyData[] | []>(argTypes: Args, returnType?: undefined): TgpuFnShell<Args, d.Void>;
<Args extends d.AnyData[] | [], Return extends d.AnyData>(argTypes: Args, returnType: Return): TgpuFnShell<Args, Return>;
};
... 7 more ...;
'~unstable': {
...;
};
}
tgpu
.
bindGroupLayout: <{
lightSource: {
uniform: d.WgslStruct<{
ambientColor: d.Vec3f;
intensity: d.F32;
}>;
};
sampling: {
sampler: "filtering";
};
bgTexture: {
externalTexture: {};
};
}>(entries: {
lightSource: {
uniform: d.WgslStruct<{
ambientColor: d.Vec3f;
intensity: d.F32;
}>;
};
sampling: {
sampler: "filtering";
};
bgTexture: {
externalTexture: {};
};
}) => TgpuBindGroupLayout<...>
bindGroupLayout
({
lightSource: {
uniform: d.WgslStruct<{
ambientColor: d.Vec3f;
intensity: d.F32;
}>;
}
lightSource
: {
uniform: d.WgslStruct<{
ambientColor: d.Vec3f;
intensity: d.F32;
}>
uniform
:
const LightSource: d.WgslStruct<{
ambientColor: d.Vec3f;
intensity: d.F32;
}>
LightSource
},
sampling: {
sampler: "filtering";
}
sampling
: {
sampler: "filtering"
sampler
: 'filtering' },
bgTexture: {
externalTexture: {};
}
bgTexture
: {
externalTexture: {}
externalTexture
: {} },
})
.
TgpuBindGroupLayout<{ lightSource: { uniform: WgslStruct<{ ambientColor: Vec3f; intensity: F32; }>; }; sampling: { sampler: "filtering"; }; bgTexture: { externalTexture: {}; }; }>.$idx(index?: number): TgpuBindGroupLayout<{
lightSource: {
uniform: d.WgslStruct<{
ambientColor: d.Vec3f;
intensity: d.F32;
}>;
};
sampling: {
sampler: "filtering";
};
bgTexture: {
externalTexture: {};
};
}>

Associates this bind group layout with an explicit numeric index. When a call to this method is omitted, a unique numeric index is assigned to it automatically.

Used when generating WGSL code: `@group(${index})

$idx
(0);
// ^ forces code-gen to assign `0` as the group index (optional)
const
const rawShader: "\n @fragment\n fn main(@location(0) uv: vec2f) -> @location(0) vec4f {\n var bgColor = textureSampleBaseClampToEdge(bgTexture, sampling, uv).rgb;\n\n var newSource: LightSource;\n newSource.ambientColor = (bgColor + lightSource.ambientColor) * factor;\n newSource.intensity = 0.6;\n\n return vec4f(newSource.ambientColor, newSource.intensity);\n }\n"
rawShader
= /* wgsl */ `
@fragment
fn main(@location(0) uv: vec2f) -> @location(0) vec4f {
var bgColor = textureSampleBaseClampToEdge(bgTexture, sampling, uv).rgb;
var newSource: LightSource;
newSource.ambientColor = (bgColor + lightSource.ambientColor) * factor;
newSource.intensity = 0.6;
return vec4f(newSource.ambientColor, newSource.intensity);
}
`;
const
const resolved: string
resolved
=
const tgpu: {
fn: {
<Args extends d.AnyData[] | []>(argTypes: Args, returnType?: undefined): TgpuFnShell<Args, d.Void>;
<Args extends d.AnyData[] | [], Return extends d.AnyData>(argTypes: Args, returnType: Return): TgpuFnShell<Args, Return>;
};
... 7 more ...;
'~unstable': {
...;
};
}
tgpu
.
resolve: (options: TgpuResolveOptions) => 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.

@paramoptions - The options for the resolution.

@returnsThe resolved code.

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

resolve
({
TgpuResolveOptions.template?: string | undefined

The code template to use for the resolution. All external names will be replaced with their resolved values.

@default''

template
:
const rawShader: "\n @fragment\n fn main(@location(0) uv: vec2f) -> @location(0) vec4f {\n var bgColor = textureSampleBaseClampToEdge(bgTexture, sampling, uv).rgb;\n\n var newSource: LightSource;\n newSource.ambientColor = (bgColor + lightSource.ambientColor) * factor;\n newSource.intensity = 0.6;\n\n return vec4f(newSource.ambientColor, newSource.intensity);\n }\n"
rawShader
,
TgpuResolveOptions.externals: Record<string, object | Wgsl>

Map of external names to their resolvable values.

externals
: {
// mapping names in the template to corresponding resources/values
type LightSource: d.WgslStruct<{
ambientColor: d.Vec3f;
intensity: d.F32;
}>
LightSource
,
factor: d.v3f
factor
:
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.

@example const vector = d.vec3f(); // (0.0, 0.0, 0.0) const vector = d.vec3f(1); // (1.0, 1.0, 1.0) const vector = d.vec3f(1, 2, 3.5); // (1.0, 2.0, 3.5)

@example const buffer = root.createBuffer(d.vec3f, d.vec3f(0, 1, 2)); // buffer holding a d.vec3f value, with an initial value of vec3f(0, 1, 2);

vec3f
(0.4, 0.6, 1.0),
...
const layout: TgpuBindGroupLayout<{
lightSource: {
uniform: d.WgslStruct<{
ambientColor: d.Vec3f;
intensity: d.F32;
}>;
};
sampling: {
sampler: "filtering";
};
bgTexture: {
externalTexture: {};
};
}>
layout
.
TgpuBindGroupLayout<{ lightSource: { uniform: WgslStruct<{ ambientColor: Vec3f; intensity: F32; }>; }; sampling: { sampler: "filtering"; }; bgTexture: { externalTexture: {}; }; }>.bound: {
lightSource: TgpuBufferUniform<d.WgslStruct<{
ambientColor: d.Vec3f;
intensity: d.F32;
}>>;
sampling: TgpuSampler;
bgTexture: never;
}
bound
,
},
});

Resolved WGSL shader code is as follows:

struct Source_0 {
ambientColor: vec3f,
intensity: f32,
}
@group(0) @binding(0) var<uniform> lightSource_1: Source_0;
@group(0) @binding(1) var sampling_2: sampler;
@group(0) @binding(2) var bgTexture_3: texture_external;
@fragment
fn main(@location(0) uv: vec2f) -> @location(0) vec4f {
var bgColor = textureSampleBaseClampToEdge(bgTexture_3, sampling_2, uv).rgb;
var newSource: Source_0; // identifiers for references are generated based on the chosen naming scheme
newSource.ambientColor = (bgColor + lightSource_1.ambientColor) * vec3f(0.4, 0.6, 1);
newSource.intensity = 0.6;
return vec4f(newSource.ambientColor, newSource.intensity);
}

This optional property of the tgpu.resolve function argument is a string containing WGSL code, that is meant to be extended with additional definitions. It can contain references to objects passed in the externals record.

This is a record with TypeGPU objects that are to be included in the final resolved shader code. The values in the mapping are the objects themselves, while the keys are the names by which they are referenced in the template code. Each object is resolved to its WGSL declaration, which is included in the final shader code. Moreover, each reference to the object in the template is replaced with the name used in its newly generated declaration.

If an object is being referenced only by another TypeGPU object in externals, it doesn’t have to be included in the record. Any passed-in object’s dependencies are automatically resolved and included in the final result.

When externals are being resolved, they are given new names based on the specified naming scheme (names parameter).

The default naming scheme is "random". It uses labels assigned to the objects via .$name("foo") method or, if they aren’t present, the keys in the externals record. In this mode labels are later transformed to match the allowed identifier pattern, as well as include some unique suffix to ensure that no identifiers conflict with each other.

Another allowed value of the parameter is "strict" which names resolved objects in the WGSL code exactly as they are labeled by the user in JS, unless there is a name conflict, in which case a suffix is added. If there is no .$name call, an object is named based on its associated key in externals. This approach makes all the generated identifiers predictable, but demands that all labels are valid identifiers and requires explicit naming (via .$name) of all objects that aren’t immediate values in the externals record.

Sometimes, it may not be clear which bind group layouts were used in a given resolution. This may occur especially when using:

  • Buffer usages/shorthands, which use a hidden, automatically created “catchall” bind group,
  • TypeGPU functions implemented in TGSL, which generate their externals automatically.

For these cases, you can use tgpu.resolveWithContext, which has the same input API as tgpu.resolve, but in addition to the resolved code, it also returns information about the layouts used. tgpu.resolveWithContext returns an object with 3 props:

  • code - the resolved code,
  • usedBindGroupLayouts - a list of used tgpu.bindGroupLayout,
  • catchall - a two-element array containing the bind group constructed for buffer usages and buffer shorthands, preceded by its index.

An example, where the “catchall” bind group is created:

const
const countMutable: TgpuMutable<d.U32>
countMutable
=
const root: TgpuRoot
root
.
TgpuRoot.createMutable<d.U32>(typeSchema: d.U32, initial?: number | undefined): TgpuMutable<d.U32> (+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

.

@paramtypeSchema The type of data that this buffer will hold.

@paraminitial The initial value of the buffer. (optional)

createMutable
(
import d
d
.
const u32: d.U32
export 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.

@example const value = u32(); // 0

@example const value = u32(7); // 7

@example const value = u32(3.14); // 3

@example const value = u32(-1); // 4294967295

@example const value = u32(-3.1); // 0

u32
);
const {
const code: string
code
,
const usedBindGroupLayouts: TgpuBindGroupLayout<Record<string, TgpuLayoutEntry | null>>[]
usedBindGroupLayouts
,
const catchall: [number, TgpuBindGroup] | undefined
catchall
} =
const tgpu: {
fn: {
<Args extends d.AnyData[] | []>(argTypes: Args, returnType?: undefined): TgpuFnShell<Args, d.Void>;
<Args extends d.AnyData[] | [], Return extends d.AnyData>(argTypes: Args, returnType: Return): TgpuFnShell<Args, Return>;
};
... 7 more ...;
'~unstable': {
...;
};
}
tgpu
.
resolveWithContext: (options: TgpuResolveOptions) => 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.

@paramoptions - The options for the resolution.

@returns

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

resolveWithContext
({
TgpuResolveOptions.template?: string | undefined

The code template to use for the resolution. All external names will be replaced with their resolved values.

@default''

template
: `
fn exampleFn() {
countMutable += 1;
}
`,
TgpuResolveOptions.externals: Record<string, object | Wgsl>

Map of external names to their resolvable values.

externals
: {
countMutable: TgpuMutable<d.U32>
countMutable
},
});
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const code: string
code
); // same as tgpu.resolve(...)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const usedBindGroupLayouts: TgpuBindGroupLayout<Record<string, TgpuLayoutEntry | null>>[]
usedBindGroupLayouts
); // an array containing the catchall bind group layout
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const catchall: [number, TgpuBindGroup] | undefined
catchall
?.[0]); // 0 - the group index of the catchall bind group
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const catchall: [number, TgpuBindGroup] | undefined
catchall
?.[1]); // the catchall bind group

An example, where a bind group layout is automatically included via a TGSL function:

const
const myLayout: TgpuBindGroupLayout<{
count: {
uniform: d.U32;
};
}>
myLayout
=
const tgpu: {
fn: {
<Args extends d.AnyData[] | []>(argTypes: Args, returnType?: undefined): TgpuFnShell<Args, d.Void>;
<Args extends d.AnyData[] | [], Return extends d.AnyData>(argTypes: Args, returnType: Return): TgpuFnShell<Args, Return>;
};
... 7 more ...;
'~unstable': {
...;
};
}
tgpu
.
bindGroupLayout: <{
count: {
uniform: d.U32;
};
}>(entries: {
count: {
uniform: d.U32;
};
}) => TgpuBindGroupLayout<{
count: {
uniform: d.U32;
};
}>
bindGroupLayout
({
count: {
uniform: d.U32;
}
count
: {
uniform: d.U32
uniform
:
import d
d
.
const u32: d.U32
export 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.

@example const value = u32(); // 0

@example const value = u32(7); // 7

@example const value = u32(3.14); // 3

@example const value = u32(-1); // 4294967295

@example const value = u32(-3.1); // 0

u32
} });
const
const exampleFn: TgpuFn<() => d.Void>
exampleFn
=
const tgpu: {
fn: {
<Args extends d.AnyData[] | []>(argTypes: Args, returnType?: undefined): TgpuFnShell<Args, d.Void>;
<Args extends d.AnyData[] | [], Return extends d.AnyData>(argTypes: Args, returnType: Return): TgpuFnShell<Args, Return>;
};
... 7 more ...;
'~unstable': {
...;
};
}
tgpu
.
fn: <[]>(argTypes: [], returnType?: undefined) => TgpuFnShell<[], d.Void> (+1 overload)
fn
([])(() => {
const myLayout: TgpuBindGroupLayout<{
count: {
uniform: d.U32;
};
}>
myLayout
.
TgpuBindGroupLayout<{ count: { uniform: U32; }; }>.$: {
count: number;
}
$
.
count: number
count
+= 1;
});
const {
const code: string
code
,
const usedBindGroupLayouts: TgpuBindGroupLayout<Record<string, TgpuLayoutEntry | null>>[]
usedBindGroupLayouts
,
const catchall: [number, TgpuBindGroup] | undefined
catchall
} =
const tgpu: {
fn: {
<Args extends d.AnyData[] | []>(argTypes: Args, returnType?: undefined): TgpuFnShell<Args, d.Void>;
<Args extends d.AnyData[] | [], Return extends d.AnyData>(argTypes: Args, returnType: Return): TgpuFnShell<Args, Return>;
};
... 7 more ...;
'~unstable': {
...;
};
}
tgpu
.
resolveWithContext: (options: TgpuResolveOptions) => 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.

@paramoptions - The options for the resolution.

@returns

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

resolveWithContext
({
TgpuResolveOptions.externals: Record<string, object | Wgsl>

Map of external names to their resolvable values.

externals
: {
exampleFn: TgpuFn<() => d.Void>
exampleFn
},
});
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const code: string
code
); // same as tgpu.resolve(...)
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const usedBindGroupLayouts: TgpuBindGroupLayout<Record<string, TgpuLayoutEntry | null>>[]
usedBindGroupLayouts
); // [myLayout]
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const catchall: [number, TgpuBindGroup] | undefined
catchall
); // undefined