wgslLayout
On this page

wgslLayout()

Function in Reflection

Compute the byte layout of one struct under a LayoutKind: each field's offset, alignment and size, plus the struct's total size and alignment.

import { wgslLayout } from 'typeshade'

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

Syntax

function wgslLayout(
struct: StructDecl,
layout: LayoutKind,
structs: ReadonlyMap<string, StructDecl> = new Map(),
): StructLayout

Parameters

struct StructDecl

the struct to lay out.

layout LayoutKind

'std140' for a uniform buffer, 'std430' for a storage buffer.

structsoptional ReadonlyMap<string, StructDecl>

the structs a nested struct-typed field may refer to, by name. Defaults to a map holding only struct itself.

Return value

StructLayout

the StructLayout.

Exceptions

Error

if a field’s type has no byte layout (a texture or a sampler), if a nested struct is not in structs, or if a mat2 field is laid out under 'std140' (see LayoutKind).

Description

Under 'std140' the struct’s and every nested array’s base alignment rounds up to 16; under 'std430' natural alignment applies.

Examples

Example

import { wgslLayout, vec3fT, f32T } from 'typeshade'
const light = { name: 'Light', fields: [{ name: 'pos', type: vec3fT }, { name: 'radius', type: f32T }] }
wgslLayout(light, 'std140')
// { name: 'Light', size: 16, align: 16, fields: [
// { name: 'pos', type: 'vec3<f32>', offset: 0, align: 16, size: 12 },
// { name: 'radius', type: 'f32', offset: 12, align: 4, size: 4 } ] }

In the guide

See also

Source

src/core/reflect.ts, line 276, at commit 26de7be8

Edit this page Report a problem