---
title: SharedUniforms
description: Values-first shared uniform/storage object created by `uniforms(gpu, values)`. It adopts binary WGSL layout lazily from the first compatible shader binding and reuses one stable buffer across shaders.
---

# SharedUniforms



## Import

```ts
import type { SharedUniforms } from "vgpu";
```

## Signature

```ts
interface SharedUniforms<T extends Record<string, unknown> = Record<string, unknown>> {
  set(values: Partial<T>): void;
}
```

## Parameters

| Param             | Type                                | Required | Default | Notes                                                                                                                   |
| ----------------- | ----------------------------------- | -------: | ------- | ----------------------------------------------------------------------------------------------------------------------- |
| uniforms.values   | `T extends Record<string, unknown>` |        ✔ | —       | Initial values are cloned. Layout and buffer are not created until first binding to a reflected uniform/storage buffer. |
| shared.set.values | `Partial<T>`                        |        ✔ | —       | Deep-merges plain objects and clones arrays/typed arrays before writing current values to the adopted layout.           |

**Returns:** `uniforms(gpu)` returns `SharedUniforms<T>`; `shared.set()` returns `void`.

**Throws:** `VGPU-R1-SHARED-UNIFORMS-LAYOUT-MISMATCH` when a later shader declares a structurally different layout for the same shared object; `VGPU-RING1-UNSUPPORTED` when address spaces differ, the binding is not a buffer, the binding has no host-shareable layout, or the layout is runtime-sized; `VGPU-SET-VALUE-INVALID` when the merged value has the wrong reflected shape or an out-of-range integer. Its structured detail includes the failure `reason` and complete value `path`.

## Examples

```ts
import { init, clock, effect, frame, target, uniforms } from "vgpu/mock";
import { prepareShader } from "@vgpu/wgsl/prepare";

const gpu = await init();
const colorTarget = target(gpu, { size: [64, 64] });
const globals = uniforms(gpu, { time: 0, mouse: [0, 0] });
const wave = effect(gpu, prepareShader(`
  struct Globals { time: f32, mouse: vec2f }
  @group(0) @binding(0) var<uniform> globals: Globals;
  @fragment fn fs_main(@location(0) uv: vec2f) -> @location(0) vec4f {
    return vec4f(uv, sin(globals.time) * 0.5 + 0.5, 1);
  }
`), { set: { globals } });

globals.set({ time: clock(gpu).time });
frame(gpu, (currentFrame) => currentFrame.pass({ target: colorTarget }, (pass) => pass.draw(wave)));
```

```ts
import { init, uniforms } from "vgpu/mock";

const gpu = await init();
const globals = uniforms(gpu, { exposure: 1, tint: [1, 1, 1] });
globals.set({ exposure: 1.25 });
```

## Frame value capture

When used in the uniform address space, direct frame draws and compute dispatches capture the current values. Calling `set()` between operations changes only later operations. The same revision can share an upload slice across pipelines in a frame.

`set()` validates and packs the merged values on every call and always updates the stored CPU values, so partial updates merge over the latest call. Frame-only consumers upload through the frame's captured pages instead of also writing the stable backing buffer. One-shot draws and dispatches flush pending stable-buffer values when used; repeated calls without an update do not upload them again. Canceled frames leave CPU updates applied but do not upload their captured pages.

When the newly packed bytes equal the previous ones, the update is not a new revision: later operations in the same frame reuse the snapshot already captured, and a pending upload stays pending (it is still flushed or retried on next use). Equality is bitwise on the packed bytes: re-setting an in-place-mutated typed array is detected, `+0` and `-0` differ, and values that round to the same `f32` are equal. You never need to compare values yourself; skipping a redundant `set()` saves only CPU work.

When adopted as storage, this object keeps a live backing buffer so GPU-written state persists, and every `set()` writes it, even with equal bytes, because the GPU may have changed it. Render bundles, raw/low-level resources and claimed bind groups also retain live contents; they do not acquire per-operation uniform snapshots.

Once a uniform is recorded in a render bundle, later updates keep its stable buffer live, including for raw WebGPU bundle replay. Accessing the implementation's `.buffer` or `.gpu` handle likewise flushes pending values and preserves immediate future writes for direct consumers; those handles are not part of the public `SharedUniforms` type. These live uniforms also write on every `set()`, even with equal bytes.

Frame captures live in pooled uniform pages owned by one frame until its GPU work completes or it is canceled; frames that repeat the same draws in the same order reuse their bind groups (see `Draw`). Each frame still uploads its own captured pages.

## Notes

* The first shader to bind the object chooses the WGSL layout. Keep struct member names/types/order aligned for every later shader that reuses it.
* Values must contain only members of that reflected struct. When spreading a shared settings object, select the fields for this binding; unrelated settings are rejected, not ignored. Validation of initial values happens when the first binding adopts its layout.
* Use shared uniforms for values like time, mouse, camera, exposure, and viewport data consumed by many passes.
* If one shader needs a different layout, create a second `uniforms(gpu)` object rather than mutating the first layout.
* `set()` clones and merges into a candidate, packs that candidate into temporary bytes, and commits only after validation succeeds. A rejected update leaves both the retained values and GPU bytes unchanged.
* **See also:** `uniforms`, `Effect.set`, `Draw.set`, `Uniform`, `StructuredUniform`.


---

For a semantic overview of all documentation, see [/sitemap.md](/sitemap.md)

For an index of all available documentation, see [/llms.txt](/llms.txt)

For agent-facing discovery, including API and MCP surfaces, see [/agents.md](/agents.md)