Skip to content

Data Schemas

Writing a GPU program usually involves sharing data between the host (CPU) and the device (GPU), in this case between JavaScript and WGSL. Any misalignments or misinterpretations of data can lead to bugs that are hard to debug (No console.log on the GPU, I am afraid). While data is strongly typed in WGSL shaders, we give up that type safety completely when writing and reading data in JavaScript/TypeScript. This is precisely what TypeGPU data types help with.

Examples

Let’s look at some examples of defining custom data types using the typegpu/data module. If you’re familiar with Zod, then this style of schema definitions may already seem familiar.

import * as
import d
d
from 'typegpu/data';
const
const Circle: d.WgslStruct<{
centerPos: d.Vec3i;
radius: d.F32;
}>
Circle
=
import d
d
.
struct<{
centerPos: d.Vec3i;
radius: d.F32;
}>(props: {
centerPos: d.Vec3i;
radius: d.F32;
}): d.WgslStruct<{
centerPos: d.Vec3i;
radius: 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
({
centerPos: d.Vec3i
centerPos
:
import d
d
.
const vec3i: d.Vec3i
export vec3i

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

@example const vector = d.vec3i(); // (0, 0, 0) const vector = d.vec3i(1); // (1, 1, 1) const vector = d.vec3i(1, 2, -3); // (1, 2, -3)

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

vec3i
,
radius: d.F32
radius
:
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(true); // 1

f32
,
});
type Circle =
import d
d
.
type Infer<T> = T extends {
readonly '~repr': infer TRepr;
} ? TRepr : T
export Infer

Extracts the inferred representation of a resource.

@example type A = Infer // => number type B = Infer<WgslArray> // => number[]

Infer
<typeof
const Circle: d.WgslStruct<{
centerPos: d.Vec3i;
radius: d.F32;
}>
Circle
>;
type Circle = {
centerPos: d.v3i;
radius: number;
}
const
const redCircle: InferRecord<{
centerPos: d.Vec3i;
radius: d.F32;
}>
redCircle
:
type Circle = {
centerPos: d.v3i;
radius: number;
}
Circle
= {
centerPos: d.v3i
centerPos
:
import d
d
.
function vec3i(x: number, y: number, z: number): d.v3i (+2 overloads)
export vec3i

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

@example const vector = d.vec3i(); // (0, 0, 0) const vector = d.vec3i(1); // (1, 1, 1) const vector = d.vec3i(1, 2, -3); // (1, 2, -3)

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

vec3i
(2, 4, 0),
radius: number
radius
: 0.2,
};

By defining the Circle struct in TypeScript via TypeGPU, in a similar way to how we would in WGSL, we gain access to its TypeScript type definition, which we can use to validate our data values. When reading from or writing data to the GPU, the type of the JavaScript value is inferred automatically, and it’s enforced by TypeScript. Thanks to that, whenever we mistakenly set or assume a wrong value for an object, we get a type error, avoiding unnecessary debugging afterwards. That’s a big improvement to the development process.

const
const redCircle1: InferRecord<{
centerPos: d.Vec3i;
radius: d.F32;
}>
redCircle1
:
type Circle = {
centerPos: d.v3i;
radius: number;
}
Circle
= {
centerPos:
import d
d
.
function vec2i(x: number, y: number): d.v2i (+2 overloads)
export vec2i

Schema representing vec2i - a vector with 2 elements of type i32. Also a constructor function for this vector value.

@example const vector = d.vec2i(); // (0, 0) const vector = d.vec2i(1); // (1, 1) const vector = d.vec2i(-1, 1); // (-1, 1)

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

vec2i
(2, 4),
Error ts(2740) ― Type 'v2i' is missing the following properties from type 'v3i': z, xz, yz, zx, and 86 more.
radius: number
radius
: 0.2,
};
const
const redCircle2: InferRecord<{
centerPos: d.Vec3i;
radius: d.F32;
}>
redCircle2
:
type Circle = {
centerPos: d.v3i;
radius: number;
}
Circle
= {
centerPos: d.v3i
centerPos
:
import d
d
.
function vec3i(x: number, y: number, z: number): d.v3i (+2 overloads)
export vec3i

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

@example const vector = d.vec3i(); // (0, 0, 0) const vector = d.vec3i(1); // (1, 1, 1) const vector = d.vec3i(1, 2, -3); // (1, 2, -3)

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

vec3i
(2, 4, 0),
radius: "0.2",
Error ts(2322) ― Type 'string' is not assignable to type 'number'.
};
const redCircle3:
type Circle = {
centerPos: d.v3i;
radius: number;
}
Circle
= {
Error ts(2741) ― Property 'radius' is missing in type '{ centerPos: d.v3i; }' but required in type 'InferRecord<{ centerPos: Vec3i; radius: F32; }>'.
centerPos: d.v3i
centerPos
:
import d
d
.
function vec3i(x: number, y: number, z: number): d.v3i (+2 overloads)
export vec3i

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

@example const vector = d.vec3i(); // (0, 0, 0) const vector = d.vec3i(1); // (1, 1, 1) const vector = d.vec3i(1, 2, -3); // (1, 2, -3)

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

vec3i
(2, 4, 0),
};
const
const diam: number
diam
=
const redCircle1: InferRecord<{
centerPos: d.Vec3i;
radius: d.F32;
}>
redCircle1
.rad * 2;
Error ts(2339) ― Property 'rad' does not exist on type 'InferRecord<{ centerPos: Vec3i; radius: F32; }>'.

Defined data structures automatically measure and hold information about their memory layout parameters, which is useful for writing to and reading data from the GPU.

function sizeOf(schema: d.AnyData): number

Returns the size (in bytes) of data represented by the schema.

sizeOf
(
const Circle: d.WgslStruct<{
centerPos: d.Vec3i;
radius: d.F32;
}>
Circle
) // 16
function alignmentOf(schema: d.AnyData): number

Returns the alignment (in bytes) of data represented by the schema.

alignmentOf
(
const Circle: d.WgslStruct<{
centerPos: d.Vec3i;
radius: d.F32;
}>
Circle
) // 16

TypeGPU data types are essential for the library’s automated data marshalling capabilities.

Scalars, Vectors & Matrices

There are a few ways to categorize numeric data-types in TypeGPU.

  • Characteristic (floating-point f, signed int i, unsigned int u).
  • Size in bits (8, 16, 32).
  • Number of components (1, 2, 3, 4, …).

d.f32, d.i32 and d.u32 all represent single-component 32-bit numeric values. When reading/writing values in JS, they are all seen as just number.

Vectors (d.v2f, d.v3f, …) are interpreted in JS as special objects, which can be created by “calling” the corresponding schema with numeric components, e.g.:

const v0 =
import d
d
.
function vec3f(x: number, y: number, z: number): d.v3f (+2 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
(1.1, 2.5, 3.3);
const v0: d.v3f
const v1 =
import d
d
.
function vec4u(x: number, y: number, z: number, w: number): d.v4u (+2 overloads)
export vec4u

Schema representing vec4u - a vector with 4 elements of type u32. Also a constructor function for this vector value.

@example const vector = d.vec4u(); // (0, 0, 0, 0) const vector = d.vec4u(1); // (1, 1, 1, 1) const vector = d.vec4u(1, 2, 3, 4); // (1, 2, 3, 4)

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

vec4u
(3, 4, 5, 2);
const v1: d.v4u
// ...

Matrices work in a similar way.

const mat0 =
import d
d
.
function mat3x3f(...elements: number[]): d.m3x3f (+2 overloads)
export mat3x3f

Schema representing mat3x3f - a matrix with 3 rows and 3 columns, with elements of type f32. Also a constructor function for this matrix type.

@example const zero3x3 = mat3x3f(); // filled with zeros

@example const mat = mat3x3f(0, 1, 2, 3, 4, 5, 6, 7, 8); mat.columns[0] // vec3f(0, 1, 2) mat.columns[1] // vec3f(3, 4, 5) mat.columns[2] // vec3f(6, 7, 8)

@example const mat = mat3x3f( vec3f(0, 1, 2), // column 0 vec3f(2, 3, 4), // column 1 vec3f(5, 6, 7), // column 2 );

@example const buffer = root.createBuffer(d.mat3x3f, d.mat3x3f()); // buffer holding a d.mat3x3f value, with an initial value of mat3x3f filled with zeros

mat3x3f
(
const mat0: d.m3x3f
1.1, 2.5, 3.3,
1.2, 2.6, 3.4,
1.3, 2.7, 3.5,
);
const mat1 =
import d
d
.
function mat2x2f(...elements: number[]): d.m2x2f (+2 overloads)
export mat2x2f

Schema representing mat2x2f - a matrix with 2 rows and 2 columns, with elements of type f32. Also a constructor function for this matrix type.

@example const zero2x2 = mat2x2f(); // filled with zeros

@example const mat = mat2x2f(0, 1, 2, 3); mat.columns[0] // vec2f(0, 1) mat.columns[1] // vec2f(2, 3)

@example const mat = mat2x2f( vec2f(0, 1), // column 0 vec2f(1, 2), // column 1 );

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

mat2x2f
(3, 4, 5, 2);
const mat1: d.m2x2f
// ...

For a comprehensive list of all available schemas, see the Data Schema Cheatsheet.

Structs

Values of different types can be grouped into structs.

import * as
import d
d
from 'typegpu/data';
const
const Boid: d.WgslStruct<{
position: d.Vec3u;
velocity: d.Vec3f;
color: d.Vec4f;
isActive: d.Bool;
}>
Boid
=
import d
d
.
struct<{
position: d.Vec3u;
velocity: d.Vec3f;
color: d.Vec4f;
isActive: d.Bool;
}>(props: {
position: d.Vec3u;
velocity: d.Vec3f;
color: d.Vec4f;
isActive: d.Bool;
}): d.WgslStruct<{
...;
}>
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
({
position: d.Vec3u
position
:
import d
d
.
const vec3u: d.Vec3u
export vec3u

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

@example const vector = d.vec3u(); // (0, 0, 0) const vector = d.vec3u(1); // (1, 1, 1) const vector = d.vec3u(1, 2, 3); // (1, 2, 3)

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

vec3u
,
velocity: d.Vec3f
velocity
:
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
,
color: d.Vec4f
color
:
import d
d
.
const vec4f: d.Vec4f
export vec4f

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

@example const vector = d.vec4f(); // (0.0, 0.0, 0.0, 0.0) const vector = d.vec4f(1); // (1.0, 1.0, 1.0, 1.0) const vector = d.vec4f(1, 2, 3, 4.5); // (1.0, 2.0, 3.0, 4.5)

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

vec4f
,
isActive: d.Bool
isActive
:
import d
d
.
const bool: d.Bool
export bool

A schema that represents a boolean value. (equivalent to bool in WGSL)

bool
,
});
type Boid =
import d
d
.
type Infer<T> = T extends {
readonly '~repr': infer TRepr;
} ? TRepr : T
export Infer

Extracts the inferred representation of a resource.

@example type A = Infer // => number type B = Infer<WgslArray> // => number[]

Infer
<typeof
const Boid: d.WgslStruct<{
position: d.Vec3u;
velocity: d.Vec3f;
color: d.Vec4f;
isActive: d.Bool;
}>
Boid
>;
type Boid = {
position: d.v3u;
velocity: d.v3f;
color: d.v4f;
isActive: boolean;
}
const
const boid: InferRecord<{
position: d.Vec3u;
velocity: d.Vec3f;
color: d.Vec4f;
isActive: d.Bool;
}>
boid
:
type Boid = {
position: d.v3u;
velocity: d.v3f;
color: d.v4f;
isActive: boolean;
}
Boid
= {
position: d.v3u
position
:
import d
d
.
function vec3u(x: number, y: number, z: number): d.v3u (+2 overloads)
export vec3u

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

@example const vector = d.vec3u(); // (0, 0, 0) const vector = d.vec3u(1); // (1, 1, 1) const vector = d.vec3u(1, 2, 3); // (1, 2, 3)

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

vec3u
(0, 0, 0),
velocity: d.v3f
velocity
:
import d
d
.
function vec3f(x: number, y: number, z: number): d.v3f (+2 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
(1, 0.5, 0.5),
color: d.v4f
color
:
import d
d
.
function vec4f(x: number, y: number, z: number, w: number): d.v4f (+2 overloads)
export vec4f

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

@example const vector = d.vec4f(); // (0.0, 0.0, 0.0, 0.0) const vector = d.vec4f(1); // (1.0, 1.0, 1.0, 1.0) const vector = d.vec4f(1, 2, 3, 4.5); // (1.0, 2.0, 3.0, 4.5)

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

vec4f
(1.0, 0.2, 0.3, 1.0),
isActive: boolean
isActive
: true,
};

You can also use the struct schema as a constructor that type-checks the object literal and provides autocomplete.

const
const boid: InferRecord<{
position: d.Vec3u;
velocity: d.Vec3f;
color: d.Vec4f;
isActive: d.Bool;
}>
boid
=
const Boid: d.WgslStruct
(props: InferRecord<{
position: d.Vec3u;
velocity: d.Vec3f;
color: d.Vec4f;
isActive: d.Bool;
}>) => InferRecord<{
position: d.Vec3u;
velocity: d.Vec3f;
color: d.Vec4f;
isActive: d.Bool;
}>
Boid
({
position: d.v3u
position
:
import d
d
.
function vec3u(x: number, y: number, z: number): d.v3u (+2 overloads)
export vec3u

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

@example const vector = d.vec3u(); // (0, 0, 0) const vector = d.vec3u(1); // (1, 1, 1) const vector = d.vec3u(1, 2, 3); // (1, 2, 3)

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

vec3u
(0, 0, 0),
velocity: d.v3f
velocity
:
import d
d
.
function vec3f(x: number, y: number, z: number): d.v3f (+2 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
(1, 0.5, 0.5),
color: d.v4f
color
:
import d
d
.
function vec4f(x: number, y: number, z: number, w: number): d.v4f (+2 overloads)
export vec4f

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

@example const vector = d.vec4f(); // (0.0, 0.0, 0.0, 0.0) const vector = d.vec4f(1); // (1.0, 1.0, 1.0, 1.0) const vector = d.vec4f(1, 2, 3, 4.5); // (1.0, 2.0, 3.0, 4.5)

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

vec4f
(1.0, 0.2, 0.3, 1.0),
isAct
isAct: any
isActive
});

Struct schemas adjust the padding and alignment automatically, so that they comply with WebGPU’s memory alignment rules. It is also possible to override default byte alignment and size for particular fields via the align and size functions.

const
const Boid: d.WgslStruct<{
position: d.Decorated<d.Vec3u, [d.Align<32>]>;
velocity: d.Vec3f;
color: d.Vec4f;
isActive: d.Decorated<d.Bool, [d.Size<8>]>;
}>
Boid
=
import d
d
.
struct<{
position: d.Decorated<d.Vec3u, [d.Align<32>]>;
velocity: d.Vec3f;
color: d.Vec4f;
isActive: d.Decorated<d.Bool, [d.Size<8>]>;
}>(props: {
position: d.Decorated<d.Vec3u, [d.Align<32>]>;
velocity: d.Vec3f;
color: d.Vec4f;
isActive: d.Decorated<d.Bool, [d.Size<8>]>;
}): d.WgslStruct<...>
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
({
position: d.Decorated<d.Vec3u, [d.Align<32>]>
position
:
import d
d
.
align<32, d.Vec3u>(alignment: 32, data: d.Vec3u): d.Decorated<d.Vec3u, [d.Align<32>]>
export align

Gives the wrapped data-type a custom byte alignment. Useful in order to fulfill uniform alignment requirements.

@example const Data = d.struct({ a: u32, // takes up 4 bytes // 12 bytes of padding, because b is custom aligned to multiples of 16 bytes b: d.align(16, u32), });

@paramalignment The multiple of bytes this data should align itself to.

@paramdata The data-type to align.

align
(32,
import d
d
.
const vec3u: d.Vec3u
export vec3u

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

@example const vector = d.vec3u(); // (0, 0, 0) const vector = d.vec3u(1); // (1, 1, 1) const vector = d.vec3u(1, 2, 3); // (1, 2, 3)

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

vec3u
), // Aligned to multiples of 32 bytes
velocity: d.Vec3f
velocity
:
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
,
color: d.Vec4f
color
:
import d
d
.
const vec4f: d.Vec4f
export vec4f

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

@example const vector = d.vec4f(); // (0.0, 0.0, 0.0, 0.0) const vector = d.vec4f(1); // (1.0, 1.0, 1.0, 1.0) const vector = d.vec4f(1, 2, 3, 4.5); // (1.0, 2.0, 3.0, 4.5)

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

vec4f
,
isActive: d.Decorated<d.Bool, [d.Size<8>]>
isActive
:
import d
d
.
size<8, d.Bool>(size: 8, data: d.Bool): d.Decorated<d.Bool, [d.Size<8>]>
export size

Adds padding bytes after the wrapped data-type, until the whole value takes up size bytes.

@example const Data = d.struct({ a: d.size(16, u32), // takes up 16 bytes, instead of 4 b: u32, // starts at byte 16, because a has a custom size });

@paramsize The amount of bytes that should be reserved for this data-type.

@paramdata The data-type to wrap.

size
(8,
import d
d
.
const bool: d.Bool
export bool

A schema that represents a boolean value. (equivalent to bool in WGSL)

bool
), // Has a minimum size of 8 bytes
});

Arrays

To define arrays of known constant length, use the d.arrayOf function. It accepts as arguments the array’s elements data type constructor and the length of the array.

const
const RecentResults: d.WgslArray<d.F32>
RecentResults
=
import d
d
.
arrayOf<d.F32>(elementType: d.F32, elementCount: number): d.WgslArray<d.F32>
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.

@example const LENGTH = 3; const array = d.arrayOf(d.u32, LENGTH);

@paramelementType The type of elements in the array.

@paramelementCount The number of elements in the array.

arrayOf
(
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(true); // 1

f32
, 4);
type RecentResults =
import d
d
.
type Infer<T> = T extends {
readonly '~repr': infer TRepr;
} ? TRepr : T
export Infer

Extracts the inferred representation of a resource.

@example type A = Infer // => number type B = Infer<WgslArray> // => number[]

Infer
<typeof
const RecentResults: d.WgslArray<d.F32>
RecentResults
>;
type RecentResults = number[]
const
const recentResults: number[]
recentResults
:
type RecentResults = number[]
RecentResults
= [
1, 0, 0.5, 20
];