---
title: "GLSL 셰이더 옮기기, TypeShade 작성 가이드"
description: "GLSL 셰이더의 구문에 대응하는 \"use typeshade\" 표기와 각 표기에서 생성되는 WGSL, GLSL 코드를 비교합니다."
url: https://typeshade.dev/ko/guide/internals/migrating-a-glsl-shader/
language: ko
---

# GLSL 셰이더 옮기기

After this page you can take the GLSL construct in front of you, look up how it is spelled in a `"use typeshade"` file, and see what that spelling turns into on each target. The rest of this guide is ordered for someone writing a new shader. This page is ordered for someone holding a working GLSL ES 3.00 shader and asking what each line becomes.

## The construct table

A construct here is one piece of GLSL source you have to account for: a uniform block, a preprocessor branch, an `#include`, an extension directive. Find the row, follow the spelling to the page that explains it, and read the note for what changes.

| GLSL construct | `"use typeshade"` spelling | WGSL result | Notes |
| --- | --- | --- | --- |
| `uniform Block { … } name;` | `declare const name: uniform<Block>` | `@group`/`@binding` `var<uniform>` | the std140 layout comes from [`reflect()`](https://typeshade.dev/ko/api/reflect/), so no offset is counted by hand |
| a loose `uniform float u_x;` | a field of a uniform struct | a field of a `var<uniform>` struct | `declare const u_x: uniform<f32>` is not supported yet by the GLSL backend ([Rule 10.3](https://typeshade.dev/ko/reference/rules/10-3/)); use a struct |
| a symbol or a block the host prelude declares | none; the IR builder’s [`externVar`](https://typeshade.dev/ko/api/externvar/), [`hostUniform`](https://typeshade.dev/ko/api/hostuniform/) and [`hostBlock`](https://typeshade.dev/ko/api/hostblock/) | the same reference, spelled per target | [`docs/ir-builder.md`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/ir-builder.md) has them |
| `in` / `out` / `inout` on a function parameter | none / [an object the function writes](https://typeshade.dev/ko/guide/internals/migrating-a-glsl-shader/#an-object-a-function-writes-and-a-value-it-gives-back) | a pointer parameter | an object is the caller’s, a value is returned |
| `#ifdef FEATURE` where this module decides | one file per variant | no preprocessor | the variant that does not declare a binding never has it |
| `#ifdef FEATURE` where the host decides | [`variantFamily`](https://typeshade.dev/ko/api/variantfamily/) | one module per point in the matrix | `emitGuarded` generates the `#if` ladder for a GLSL host that owns the define, and every arm is byte-identical to the standalone variant |
| a variant that changes only a value | `override<T>` with `overrideValues` | `override` plus pipeline constants | building a separate variant for this multiplies pipelines for nothing |
| `#include "helper.glsl"` | `import` from another shader file | one module | for a host that composes its own program, [`emitGlslFragment`](https://typeshade.dev/ko/api/emitglslfragment/) and [`emitFragment`](https://typeshade.dev/ko/api/emitfragment/) return a module fragment |
| `precision mediump float;` | the `floatPrecision` emit option | nothing, since WGSL has no precision qualifiers | it sets the stage-wide default at emit |
| `usampler2D` and `isampler2D` | `texture_2d<u32>` and `texture_2d<i32>` | `texture_2d<u32>` and `texture_2d<i32>` | the sampler precision line is emitted for you |
| `#extension … : require` | the use that needs it, or `"enable …"` | `enable …;` | fails closed with SD0030 on a backend whose profile has no row. That page also says which capabilities survive on WGSL |
| comparing two emits after an optimizer pass | [`semanticDiff`](https://typeshade.dev/ko/api/semanticdiff/) | the same | compares IR and reflection, so folding and renaming do not drown the diff. Declare the production plugins as `transforms` and their rewrites classify into `explained` |

A block this module owns is the most common first row. Declare the struct and the binding, and read the fields by name:

```ts
"use typeshade";

interface Camera {
  u_matrix: mat4;
  u_viewport_px: vec2;
}

// uniform Camera { mat4 u_matrix; vec2 u_viewport_px; } u_camera;
declare const u_camera: uniform<Camera>;

@vertex
export function vs(pos: vec2): vec4 {
  return u_camera.u_matrix * vec4(pos / u_camera.u_viewport_px, 0., 1.);
}
```

## Builtin values

A builtin is a value the hardware supplies to a stage. The vocabulary for them is WGSL’s, written as the type `builtin<"id">` on a parameter or a struct field, so a `gl_*` spelling or a typo is an error that names TypeShade’s spelling. Each backend then spells the id its own way.

| GLSL global | `"use typeshade"` spelling | Notes |
| --- | --- | --- |
| `gl_Position` | `builtin<"position">` on the vertex output | writes `gl_Position` on GLSL. A vertex entry that returns a bare [`vec4`](https://typeshade.dev/ko/reference/types/vec4/) returns the position |
| `gl_FragCoord` | `builtin<"position">` on a fragment input | reads `gl_FragCoord` on GLSL. Mind the y origin: GL window space is bottom-left and WGSL framebuffer space is top-left, so flip per target, or derive a y-symmetric value, before consuming `.y` |
| `gl_VertexID` | `vi: builtin<"vertex_index">` | GLSL wraps the read as `uint(gl_VertexID)`, since the surface types it `u32` and GLSL’s is int |
| `gl_InstanceID` | `ii: builtin<"instance_index">` | the same `uint()` wrap |
| `gl_FrontFacing` | `front: builtin<"front_facing">` | |
| `gl_FragDepth` | `builtin<"frag_depth">` on a field of the return struct | |
| `gl_PointSize` and `gl_PointCoord` | unsupported on both writers | point size caps vary per vendor, and WebGPU point primitives are always one pixel. Expand an instanced quad in the vertex stage and interpolate a [`vec2`](https://typeshade.dev/ko/reference/types/vec2/) corner uv |
| float `mod(x, y)` | the builtin [`mod()`](https://typeshade.dev/ko/reference/functions/mod/) | that is floor-mod. `%` is trunc-mod, which is WGSL’s semantics and spells portably on GLSL too. Pick by the semantics you mean on negative operands |

A fragment stage reads the framebuffer coordinate through the same `position` builtin the vertex stage writes, and writes the depth through a field of its return struct:

```ts
"use typeshade";

interface FragOut {
  color: vec4;
  depth: builtin<"frag_depth">;
}

@fragment
export function fsCoord(pos: builtin<"position">): FragOut {
  return { color: vec4(pos.x * 0.001, pos.y * 0.001, 0., 1.), depth: 0.5 };
}
```

## Targeting WebGL2 only

Nothing on this page narrows what the GLSL writer can express. The neutral names are spellings, and several of the rules behind them exist to make WebGL2 output more defined: [`round`](https://typeshade.dev/ko/reference/functions/round/) emits `roundEven`, and float `%` emits a trunc-mod that GLSL ES 3.00 compiles.

A file that uses a form the GLSL backend does not support yet, a scalar uniform, a `"read_write"` storage binding in a draw or `{ interpolate: "linear" }`, still compiles: [`compile()`](https://typeshade.dev/ko/api/compile/) keeps the WGSL, leaves `glsl` undefined and says why in a [`TS8015`](https://typeshade.dev/ko/reference/errors/ts8015/) warning. The reverse has no spelling. A GLSL construct the language has no spelling for yet, `gl_PointSize` for one, is not supported yet in a `"use typeshade"` file. An engine that must emit one builds that statement with the IR builder’s raw statements, which [`docs/ir-builder.md`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/ir-builder.md) describes, with what a raw statement costs the rest of the module.
