constDecl
이 페이지에서

constDecl()

함수, 레이아웃 분류

Declare a module-level scalar constant together with its typed reference in one call.

import { constDecl } from 'typeshade'

시그니처, 설명, 예제는 커밋 c66579bf의 컴파일러 소스에서 그대로 가져온 영어 원문입니다.

구문

function constDecl<T extends ShaderType>(
name: string,
type: T,
values: { readonly wgsl: number; readonly cpu: number },
): ConstHandle<T>

매개변수

name string

the emitted constant name.

type T extends ShaderType

the constant’s scalar type.

values { readonly wgsl: number; readonly cpu: number }

wgsl, the literal WGSL emits, and cpu, the value the CPU oracle uses.

반환값

ConstHandle<T>

the ConstHandle, .decl and .node.

설명

WGSL emits values.wgsl as the literal, in the same spelling a hand-written ConstDecl uses (for example PI = 3.14159265). The CPU oracle, the same module run in double precision on the CPU, evaluates values.cpu at full JS double precision, so a WGSL literal you deliberately shorten does not lower the precision of the parity check.

Returns a ConstHandle. Its .decl goes into module({ consts }) or, more usually, into module({ uses }), which collects the .decl of any handle passed to it. Its .node is the typed reference to use at call sites, so a renamed or misspelled constant is a tsc error everywhere it is used. For a constant of vector, matrix or array type (a vec4<f32> colour, an array<vec4<f32>, N> palette) use constExpr, which takes one constant-foldable literal node in place of the WGSL/CPU value pair.

예제

예제

const TAU = constDecl('TAU', f32T, { wgsl: 6.2831853, cpu: Math.PI * 2 })
const g = fn('g', { t: f32T }, ({ t }) => t.mul(TAU.node))
const m = module({ consts: [TAU.decl], funcs: [g] })

관련 가이드

함께 보기

소스

src/core/sot.ts, 76행, 커밋 c66579bf 기준

이 페이지 편집 문제 보고