# Shared bindings

> `@typegpu/kit` 0.1.0. If it is installed, read `node_modules/@typegpu/kit/docs/` instead.

Put the buffers and textures that several modules bind into one bind group layout you own, and swap any of them without rebuilding a pipeline.

> `import { bindings } from '@typegpu/kit'`: `bindings.layout(entries) → Layout`, `bindings.share(root, parts) → Shared`. Declares module bindings that an app can combine in one shared layout. **stability** unstable: may change in any way in a minor release. **records** Set `shared.group()` on each pass that reads a part, or give the layout to `passes.create` as `shared`. **owns** a bind group: the shared layout recreates it when a part's resources change. **allocates** on change: a bind group from `group()`, after a shared part's resources change. **injects** definition: each part's accessors, filled from the shared layout or its own resources. **types** `Entries`, `Resources`, `Values`, `Part`, `AnyPart`, `Bound`. `src/bindings.ts`; classroom.
>
> `BindOptions`, taken by `Layout.bind`:
> - `unshared?: string` Makes a pipeline that reads the part throw this message as it builds, unless a `bindings.share` holds the part. Default none: the pipeline binds the resources in its automatic group, and `replace` throws after that.
>
> `Layout`, from `layout`:
> - `entries: E` Holds the layout entries the layout was made from.
> - `$: Values<E>` Provides shader access to the entries, as a `tgpu.bindGroupLayout`'s `$` does.
> - `bind(resources, options?)` Binds one set of resources, using the same inputs as `root.createBindGroup`.
>
> `Shared`, from `share`:
> - `layout: TgpuBindGroupLayout` Contains every part's entries.
> - `inject()` Fills shader accessors for every part from the shared layout.
> - `group()` Returns the group for the current resources, for `pass.setBindGroup(shared.layout, …)`.

A fixed resource such as `root.createUniform(…)` goes in the pipeline's automatic bind group. Replacing that resource requires another pipeline, even for a shadow map resized by a quality setting. A `tgpu.bindGroupLayout` lets you swap resources, but uses one of each pipeline's 4 bind groups.

With `bindings`, a module declares what its shaders read without owning a group. Your app combines module bindings and its own bindings in one layout, then sets one group per pass. Resources in that layout can change without rebuilding pipelines.

```ts
import { bindings } from '@typegpu/kit';

// A fog module declares what its shaders read, beside them
const volume = bindings.layout({
  density: { texture: d.texture3d(d.f32) },
  smooth: { sampler: 'filtering' },
});
const fogAt = (p: d.v3f) => {
  'use gpu';
  return std.textureSampleLevel(volume.$.density, volume.$.smooth, p, 0).x;
};
const density = grid(32);
const fog = volume.bind({ density, smooth });

// The app takes the fog's part, and any others, into one layout it owns
const shared = bindings.share(root, { fog });
const pipeline = root.pipe(fog.inject()).createComputePipeline({ compute: kernel });

frames.create(root, ({ encoder }) => {
  const pass = encoder.beginComputePass();
  pass.setBindGroup(shared.layout, shared.group());
  pass.setPipeline(pipeline);
  pass.dispatchWorkgroups(64);
  pass.end();
});

fog.replace({ density: grid(64) }); // the next group binds it, with no new pipelines
density.destroy();
```

## Declaring bindings

- **The layout** `bindings.layout(entries)` takes `tgpu.bindGroupLayout`'s entries, and shaders read its `$`. Define it at module level, beside the shaders.
- **The part** `layout.bind(resources)` gives a **part**. A module makes its part in `create`, hands it out as `part`, and pipes `part.inject()` into its own `inject()`.
- **Per layout** A pipeline reads one part per layout, so two fog volumes in one shader need two layouts.
- **Drawings** A drawing added to a description reads a part through its own `inject()`, as in [buffers and textures of your own](passes.md#buffers-and-textures-of-your-own).
- **Unshared parts** Until an app shares it, a part uses fixed bindings without a separate group. These require TypeGPU resources. A raw `GPUBuffer` throws when the pipeline resolves. `replace` throws once a pipeline holds fixed resources, so a part whose resources change [asks for its share](#resources-that-change).

## Sharing

`bindings.share(root, parts)` combines parts in one app-owned layout. Each entry uses its part's key as a prefix, as in `fog_density`, `fog_smooth` and `shadow_map`. A [description of passes](passes.md#buffers-and-textures-of-your-own) given parts, as `{ shared: { fog } }`, shares them itself and sets the group in every pass it begins.

- **Before pipelines** Call it before the pipelines that read the parts build. It checks every part before taking any, and throws for one that already has an owner or that a pipeline holds as fixed bindings.
- **Fills** A shared part's own `inject()` reads from the shared layout. `shared.inject()` fills every part, for one with no fill of its own like [the noise cache](#parts-from-other-packages).
- **In a pass** `pass.setBindGroup(shared.layout, shared.group())` covers every pipeline in the pass.
- **In a description** Give each description that reads the parts `{ shared }`. A dispatch or draw without the group throws `Missing bind groups for layouts`.
- **Replacing** `part.replace({ density: bigger })` swaps resources for the next `shared.group()`, with no new pipelines.
- **New groups** `shared.group()` creates a group only when a part binds a new resource: after a `replace` that changes one, or a change in the record a part reads. A steady frame allocates nothing. Call it after a swap to allocate the group before the next frame.
- **Old resources** Call `replace` between frames, then destroy the old resource. Already submitted work finishes with it. A later pass using the destroyed resource fails at submit.

## Resources that change

A pipeline that holds a part as fixed bindings keeps the resources it was built with. A part whose resources change while the program runs, such as a texture that follows the canvas, needs its share before that pipeline builds. Give it `unshared`, a message naming the share to add, and a pipeline that reads the part without one throws it as it builds, at the first frame or warm-up rather than at the first change:

```ts
const fog = volume.bind(
  { density: grid(32), smooth },
  { unshared: 'The fog grows: `bindings.share(root, { fog })` before the pipelines that read it.' },
);
```

- [Ambient occlusion](ao.md#resizing) makes its part this way, since a new depth size replaces its texture.
- A shadow replaces its map only when you call its `resize`. An unshared [shadow](lighting.md#resizing-a-shadow-map) works until then, and `resize` throws at that call before it changes anything.

## Parts from other packages

Any object with these members can be a part, including an object from a package that doesn't depend on TypeGPU Kit:

| Member             |                                                                                  |
| ------------------ | -------------------------------------------------------------------------------- |
| `entries`          | The layout entries, as `tgpu.bindGroupLayout` takes them.                        |
| `resources`        | What to bind now, read at each `shared.group()` and compared entry by entry.     |
| `inject(values?)`  | Fills the part's shaders from `values`, the `$` of a layout holding its entries. |
| `check?()`         | Throws if `share` can't take the part now, such as one with an owner.            |
| `ownedBy?(values)` | Called by `share`, so the part's own fill can follow its owner.                  |

`@typegpu/noise`'s dynamic Perlin cache can join through an adapter:

```ts
import { perlin3d } from '@typegpu/noise';

const config = perlin3d.dynamicCacheConfig();
const cache = config.instance(root, d.vec3u(16, 16, 16));

const noise: bindings.Part<typeof config.layout> = {
  entries: config.layout,
  get resources() {
    return cache.bindings;
  },
  inject(values) {
    if (!values) throw new Error('The noise cache has no group of its own; share it.');
    return config.inject(values);
  },
};

const shared = bindings.share(root, { noise });
```

Resize with `cache.size = next`. The next `shared.group()` binds the resized cache.

## Counting groups

A pipeline with more groups than the device's `maxBindGroups`, 4 on every device, fails with an error such as `bindGroupLayoutCount (5) is larger than the maximum allowed (4)`. A [census](pipelines.md#census) counts each pipeline's groups, including the automatic group.

A description of passes uses up to 3 of them: the automatic group, the shared group and, drawn into layers, the layer's group. That leaves one for a layout a drawing binds per draw.

## What it leaves alone

- **Binding counts** A shared layout shows each entry to every stage that can use it, so a pipeline that reads one part counts every part. The automatic group shows a resource only to the stages that read it, so moving one into a shared layout can raise a stage's count. [`pipelines.compile` names the bindings](pipelines.md#bindings-per-stage) of a pipeline that goes over.
- **Per-draw bindings** Per-draw bindings, such as a mesh's textures, keep their own layout.
- **Module groups** Groups owned by one module stay separate. The [neighbour grid](grid.md)'s sort uses 4 groups of its own.

## Limits

- A pipeline that reads a shared part and runs outside the app's passes gets no group, and throws `Missing bind groups for layouts`.
- `shared.layout` is typed loosely, without its entries' names.
- `shared.group()` checks a part that `replace` changed with one number. It checks every other part by the identity of every entry, per call, including one that still reads the record `bind` took.

[Resizing the shadow](lighting.md#resizing-a-shadow-map): Share the shadow map's bindings to resize it without rebuilding pipelines.
