rawStmt
On this page

rawStmt()

Function in Authoring

Author a raw statement, the escape hatch that splices verbatim text into a function body.

import { rawStmt } from 'typeshade'

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

Syntax

function rawStmt(payload: RawPayload): RawStmt

Parameters

payload RawPayload

the per-target spellings, at least one of wgsl and glsl.

Return value

RawStmt

the raw statement node, ready to place in a body array.

Exceptions

SD0030

One side may be omitted, and the other backend then throws SD0030 naming the missing side and quoting the side you did give, so leaving a side out states that the module does not build for that target.

Description

Reach for it when a statement has to be hand-written: a string from another generator, or a construct the IR does not model.

The payload carries one spelling per target, { wgsl, glsl }. The meaning is fixed, only the spelling differs, and each backend splices its own side. One side may be omitted, and the other backend then throws SD0030 naming the missing side and quoting the side you did give, so leaving a side out states that the module does not build for that target. At least one side is required at the type level: rawStmt({}) does not compile.

Only the first line receives the enclosing body’s indent. The emitter prepends that indent to the payload as a whole, so line two onward of a multi-line payload lands at column zero. Indent the continuation lines yourself when the output shape matters.

Identifiers inside the payload are yours to keep valid. Nothing reads into raw text, so nothing rewrites it, and the GLSL backend renames params and locals that collide with GLSL reserved words (in, sample, filter, texture, and the rest); a glsl payload naming the pre-rename identifier references a variable the renamed text does not have. Only the GLSL backend renames, so the risk is one-sided even though the contract is the same on both. For the same reason a module holding a raw statement makes mangle a no-op module-wide, and the CPU oracle, the same module run in double precision on the CPU, throws when it reaches raw text it cannot evaluate.

This factory returns the Stmt, which is what you want when assembling a Stmt[] body by hand. Inside a fluent fn body use b.raw(payload) instead: a bare rawStmt(...) call there is a discarded expression, since the returned statement is never pushed and nothing is emitted.

Examples

Example

import { fn, rawStmt, vec4fT } from 'typeshade'
// Inside a fn body, through the builder:
const fs = fn('fs_main', {}, vec4fT, (_p, b) => {
b.raw({ wgsl: 'return vec4<f32>(1.0, 0.0, 0.0, 1.0);', glsl: 'return vec4(1.0, 0.0, 0.0, 1.0);' })
}, { stage: 'fragment' })
// Assembling a Stmt[] by hand:
const stmt = rawStmt({ wgsl: 'discard;', glsl: 'discard;' })

In the guide

See also

Source

src/core/ir/builder.ts, line 1451, at commit 26de7be8

Edit this page Report a problem