builtin
이 페이지에서

builtin()

함수, 레이아웃 분류

Attribute a field or an entry-point parameter with a @builtin(<name>), the value the pipeline supplies to the shader.

import { builtin } from 'typeshade'

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

구문

function builtin<N extends FixedTypeBuiltinName>(
name: N,
): FieldSpec<(typeof WGSL_BUILTIN_TYPES)[N]>;
function builtin<T extends ShaderType>(name: WgslBuiltinName, type: T): FieldSpec<T>;

매개변수

name N extends FixedTypeBuiltinName

the WGSL builtin id.

type

the field’s type, which must match what the builtin supplies. Omit it for any id in WGSL_BUILTIN_TYPES, which is every id except clip_distances.

반환값

FieldSpec<(typeof WGSL_BUILTIN_TYPES)[N]>

a FieldSpec for an ioStruct field map or an fn param record.

설명

The same helper serves an ioStruct field map and an fn param record.

The vocabulary is WGSL’s, passed through verbatim, and name is typed as the closed WgslBuiltinName union: 'position', 'vertex_index', 'instance_index', 'front_facing', 'frag_depth', 'sample_index', 'sample_mask', the compute family ('global_invocation_id', 'local_invocation_id', 'local_invocation_index', 'workgroup_id', 'num_workgroups'), the subgroup pair and 'clip_distances'. A typo or a GLSL spelling is therefore a tsc error at the authoring line, where a WGSL writer would otherwise pass the name through and the failure would land in the GPU compiler with no way back to the source.

The GLSL backend spells each id in that target’s terms, so these are the gl_* globals the ids replace:

idGLSL ES 3.00
position on a vertex outputgl_Position
position on a fragment inputgl_FragCoord
vertex_indexuint(gl_VertexID)
instance_indexuint(gl_InstanceID)
front_facinggl_FrontFacing
frag_depthgl_FragDepth

The two position rows are one id because WGSL has one: a vertex stage writes clip space, a fragment stage reads window space. Mind the y origin when a fragment reads it, since GL window space runs bottom-up and WGSL framebuffer space runs top-down.

vertex_index and instance_index are u32 here and int in GLSL, which is why the GLSL read is wrapped in uint(...).

The type token is optional. WGSL fixes the type of every id but clip_distances, so builtin('vertex_index') reads it from WGSL_BUILTIN_TYPES — and the one-argument call is the spelling that cannot disagree with the spec, where a written token can (a builtin('vertex_index', f32T) type-checks and dies at the driver). Pass the token where the id has no single type, which is clip_distances and its author-chosen array<f32, N>.

예제

예제

import { fn, ioStruct, builtin, location, u32T, vec4fT, vec2fT } from 'typeshade'
const VsOut = ioStruct('VsOut', { pos: builtin('position'), uv: location(0, vec2fT) })
// The same helper attributes an entry point's parameter.
const vs = fn('vs_main', { vid: builtin('vertex_index') }, ({ vid }) => vsOutFor(vid), {
stage: 'vertex',
})

관련 가이드

함께 보기

소스

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

이 페이지 편집 문제 보고