---
title: "Buffer: interface in Runtime, TypeShade API reference"
description: "Buffer, an interface in Runtime. An array on the device of length elements T: a Buffer of structs…"
url: https://typeshade.dev/api/buffer/
language: en
---

# `Buffer`

Interface in Runtime

An array on the device of length elements T: a Buffer of structs, one range of the region laid out as a storage array with no size (Rule 6.8), made by zeros or array with a class; or a Buffer of scalars or vectors, made by `array(xs)` of a typed array, array or a kernel called on a resource.

`import type { Buffer } from 'typeshade/runtime'`

The signature, the description and the examples come from the compiler's own source at commit af54ddad5.

## Syntax

```ts
interface Buffer<T> extends Disposable {
  readonly length: number;
  resize(length: number): void;
  set(data: BufferData<T>): void;
  at(index: number): BufferElement<T>;
  read(): Promise<BufferRead<T>>;
  needsUpdate: boolean;
  dispose(): void;
}
```

## Description

Every `Buffer` has the same members (A11, Q1). `ps.at(i).pos.set(x, y, z)` writes one field of a struct element, and `xs.at(i).set(1.5)` one scalar; `await b.read()` gives every element: each an instance of the class for a struct, and one typed array of the scalar for a scalar or a vector, the components one after another (A11, Q2 and Q5). From a call that writes it until `read()` brings its values back, `set`, `resize`, `needsUpdate` and the accessors’ `get` and `set` throw a `TypeError`. Freed by `dispose()` or when the garbage collector reclaims it, and by `[Symbol.dispose]()`, which the type declares as `Disposable` where the host’s `lib` declares `Symbol.dispose` (Amendment A5, D4). A binding a shader module exports is a `Buffer` of structs too ([Rule 6.1](https://typeshade.dev/reference/rules/6-1/)): a `storage<array<T, N>>` keeps its `N` elements, and a `storage<array<T>>`, which states no size, has no length until the host calls `resize` or `set`; until then `length` is 0, and `at` and `read` throw a `TypeError` that names the binding. The view types an exported binding without `dispose()` and `[Symbol.dispose]()` (D7).

## Instance properties

**`length` read only `number`**

How many elements it holds; a vector is one element (Amendment A11, Q5). An exported storage array with no size holds 0 until the host sets its length with `resize` or `set`.

**`needsUpdate` `boolean`**

Whether the array holds a change not yet uploaded. Set it to `true` after writing through a view that `get` returned. A new array is dirty.

## Instance methods

**`resize` `(length: number): void`**

Give the array `length` elements (Amendment A3): the elements that fit keep their values, a new element is zero. An accessor that `at` returned before reads the array’s new range, and throws a `TypeError` for an element that is gone.

**`set` `(data: BufferData<T>): void`**

Give the array the length of `data` and its values (Amendment A3): for a struct, each element gives some of its fields, and the others are zero, as `array(Class, data)` reads them; for a scalar or a vector, the numbers, the components one after another. A `data` that does not fit changes nothing.

**`at` `(index: number): BufferElement<T>`**

Element `index`’s accessor: a struct’s fields, or a scalar’s or a vector’s element view with `get` and `set` (Amendment A11, Q1). @throws `RangeError` for an index outside the array.

**`read` `(): Promise<BufferRead<T>>`**

Every element: each an instance of the class, built without its constructor, for a struct; one typed array of the scalar for a scalar or a vector (Amendment A11, Q2 and Q5).

**`dispose` `(): void`**

Free the array. A use of an accessor after it throws a `TypeError`.

## In the guide

- [The CPU oracle](https://typeshade.dev/guide/internals/the-cpu-oracle/)

## See also

- [`zeros`](https://typeshade.dev/api/zeros/)
- [`array`](https://typeshade.dev/api/array/)

## Source

[`src/runtime/resource.ts`, line 617](https://github.com/typeshade/typeshade/blob/af54ddad5/src/runtime/resource.ts#L617), at commit af54ddad5
