structDecl
On this page

structDecl()

Function in Layout

Declare a plain struct: one that never crosses a stage boundary.

import { structDecl } from 'typeshade'

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

Syntax

function structDecl<F extends Record<string, ShaderType>, N extends string>(
name: N,
fields: F,
): PlainStruct<F, N>

Parameters

name N extends string

the emitted struct name.

fields F extends Record<string, ShaderType>

the field map, each value a plain shader type.

Return value

PlainStruct<F, N>

the PlainStruct handle described above.

Description

Declare a plain struct: one that never crosses a stage boundary. That is a storage-buffer element type, or a struct nested inside another struct. Fields are plain shader types with no @location or @builtin attribute, which is the whole difference from ioStruct.

The returned handle carries:

  • .decl for module({ structs }), or pass the handle in uses.
  • .type for the struct’s shader type, which is what a parameter, a nested field or a storageBuffer element takes.
  • .of(node).field for typed field reads off a value you hold as a raw node, and .get(node, 'field') for the positional spelling of the same read, which suits a call site that reads many fields through one shorthand.
  • .var(name) for a mutable var of the struct, and .construct({ ... }) for the one-expression build, exactly as ioStruct has them.

As a storage-buffer element type it is the argument storageBuffer takes, and buf.at(i) then returns this struct’s field proxy directly, with no .of step.

Examples

Example

import { structDecl, storageBuffer, u32T, vec2fT } from 'typeshade'
const ShapeSegment = structDecl('ShapeSegment', { kind: u32T, p0: vec2fT, p1: vec2fT })
const segments = storageBuffer('segments', ShapeSegment, { group: 0, binding: 9, access: 'read' })
segments.at(i).p0 // Node<'vec2<f32>'>
ShapeSegment.get(someNode, 'kind') // the same read, positionally

In the guide

See also

Source

src/core/sot.ts, line 552, at commit 26de7be8

Edit this page Report a problem