Skip to content

Specifying a Pointer

A pointer type in WGSL is parameterized. It defines not only the type of data being pointed to, but also where that data lives and what access permissions are allowed.

A pointer type is written in one of two ways:

  • ptr<AS, T, AM> (with an explicit access mode)
  • ptr<AS, T> (relying on the default access mode)

Where:

  • AS is the Address Space (e.g. function, private, workgroup, uniform, storage).
  • T is the Store Type (e.g. f32, u32, vec3f). This represents the underlying concrete type of the value being stored in memory.
  • AM is the Access Mode (e.g. read, read_write).

When to Specify the Access Mode

The access mode AM is optional for most address spaces, as they have strict default behaviors. In fact, WGSL compilers require you to only specify the access mode when the address space AS is storage.

For all other address spaces, you must omit the access mode—the compiler will implicitly use the default access mode of that address space.


Address Space Reference Matrix

The following reference matrix summarizes how WGSL address spaces, memory tiers, and access modes combine:

Address Space (AS) Default Access Mode Allowed Access Modes Hardware Tier Typical Store Types (T)
function read_write read_write only Core Registers / L1 Any constructible type
private read_write read_write only Core Registers / L1 Any constructible type
workgroup read_write read_write only On-Chip LDS Any constructible type
uniform read read only Global VRAM (Cached) Host-shareable structures, arrays
storage read read, read_write Global VRAM (Uncached) Host-shareable structures, runtime arrays

Code Examples

In the accompanying shader code editor, we use alias definitions to inspect valid and invalid pointer declarations.

Notice how alias ptr_to_f32_in_storage_buffer_rw = ptr<storage, i32, read_write> is completely legal because it targets storage, while adding a read access mode to a private pointer will fail shader compilation!