---
title: "builtin(): function in Layout, TypeShade API reference"
description: "builtin, a function in Layout. Attribute a field or an entry-point parameter with a @builtin(<name>), the value the pipeline supplies to the shader."
url: https://typeshade.dev/api/builtin/
language: en
---

# `builtin()`

Function in Layout

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

`import { builtin } from 'typeshade/core/ir'`

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

## Syntax

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

## Parameters

**`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`](https://typeshade.dev/api/wgslbuiltintypes/), which is every id except `clip_distances`.

## Return value

`FieldSpec<(typeof WGSL_BUILTIN_TYPES)[N]>`

a [`FieldSpec`](https://typeshade.dev/api/fieldspec/) for an `ioStruct` field map or an `fn` param record.

## Description

The same helper serves an [`ioStruct`](https://typeshade.dev/api/iostruct/) field map and an [`fn`](https://typeshade.dev/api/fn/) param record.

The vocabulary is WGSL’s, passed through verbatim, and `name` is typed as the closed [`WgslBuiltinName`](https://typeshade.dev/api/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:

| id | GLSL ES 3.00 |
| --- | --- |
| `position` on a vertex output | `gl_Position` |
| `position` on a fragment input | `gl_FragCoord` |
| `vertex_index` | `uint(gl_VertexID)` |
| `instance_index` | `uint(gl_InstanceID)` |
| `front_facing` | `gl_FrontFacing` |
| `frag_depth` | `gl_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`](https://typeshade.dev/api/wgslbuiltintypes/) — 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>`.

## Examples

### Example

```ts
import { fn, ioStruct, builtin, location, u32T, vec4fT, vec2fT } from 'typeshade/core/ir'

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',
})
```

## In the guide

- [Your first shader](https://typeshade.dev/guide/internals/your-first-shader/)
- [Functions and entry points](https://typeshade.dev/guide/internals/functions-and-entry-points/)
- [Layouts and resources](https://typeshade.dev/guide/internals/layouts-and-resources/)
- [Capabilities & extensions](https://typeshade.dev/guide/internals/capabilities-extensions/)
- [Migrating a GLSL shader](https://typeshade.dev/guide/internals/migrating-a-glsl-shader/)

## See also

- [`ioStruct`](https://typeshade.dev/api/iostruct/)
- [`fn`](https://typeshade.dev/api/fn/)
- [`WgslBuiltinName`](https://typeshade.dev/api/wgslbuiltinname/)
- [`WGSL_BUILTIN_TYPES`](https://typeshade.dev/api/wgslbuiltintypes/)
- [`FieldSpec`](https://typeshade.dev/api/fieldspec/)
- [`location`](https://typeshade.dev/api/location/)

## Source

[`src/core/sot.ts`, line 242](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/sot.ts#L242), at commit af54ddad5
