ioStruct
On this page

ioStruct()

Function in Layout

Declare a struct that crosses a stage boundary, a vertex output, a fragment input, a compute IO record, from one field map.

import { ioStruct } from 'typeshade'

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

Syntax

function ioStruct<F extends Record<string, FieldSpec>, N extends string>(
name: N,
fields: F,
): IoStruct<F, N>

Parameters

name N extends string

the emitted struct name.

fields F extends Record<string, FieldSpec>

the field map, each value a builtin(...) or location(...) spec.

Return value

IoStruct<F, N>

the IoStruct handle described above.

Description

Every field is a builtin or location spec, so the attributes live with the field they belong to and the struct is declared once.

The returned handle carries everything the rest of the module needs:

  • .type is the struct’s ShaderType. Use it as a parameter type, { vo: VsOut.type }, or pass the handle itself in an fn param record and the body receives the typed field proxy.
  • .decl is the StructDecl for module({ structs }), or pass the handle in uses.
  • .of(node) reads fields off a value of this struct: VsOut.of(p.vo).uv is typed, and a misspelled field is a tsc error. The view follows its base, so a read-only base yields read-only fields.
  • .var(name) declares a var of the struct and returns assignable fields: o.pos.assign(...). The proxy reads as the raw node in value positions, and .$ is that raw struct value where an explicit one is wanted.
  • .construct({ ... }) builds the value in one expression, keyed by field name, with a missing or extra field a tsc error. It is the form to prefer when nothing needs mutating.

Examples

Example

import { ioStruct, builtin, location, vec4fT, vec2fT, f32T } from 'typeshade'
const VsOut = ioStruct('VsOut', {
pos: builtin('position', vec4fT),
uv: location(0, vec2fT),
vis: location(1, f32T),
})
const pin = VsOut.of(p.vo) // pin.uv, pin.vis are typed reads
return VsOut.construct({ pos: clip, uv, vis })

In the guide

See also

Source

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

Edit this page Report a problem