GLSL 셰이더 옮기기
이 페이지에서

커밋 af54ddad5의 AUTHORING.md를 그대로 옮긴 것으로, 이 절의 본문은 아직 영어입니다. 패키지는 0.2.0에서 쓸 typeshade라는 이름으로 가져옵니다.

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" spellingWGSL resultNotes
uniform Block { … } name;declare const name: uniform<Block>@group/@binding var<uniform>the std140 layout comes from reflect(), so no offset is counted by hand
a loose uniform float u_x;a field of a uniform structa field of a var<uniform> structdeclare const u_x: uniform<f32> is not supported yet by the GLSL backend (Rule 10.3); use a struct
a symbol or a block the host prelude declaresnone; the IR builder’s externVar, hostUniform and hostBlockthe same reference, spelled per targetdocs/ir-builder.md has them
in / out / inout on a function parameternone / an object the function writesa pointer parameteran object is the caller’s, a value is returned
#ifdef FEATURE where this module decidesone file per variantno preprocessorthe variant that does not declare a binding never has it
#ifdef FEATURE where the host decidesvariantFamilyone module per point in the matrixemitGuarded 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 valueoverride<T> with overrideValuesoverride plus pipeline constantsbuilding a separate variant for this multiplies pipelines for nothing
#include "helper.glsl"import from another shader fileone modulefor a host that composes its own program, emitGlslFragment and emitFragment return a module fragment
precision mediump float;the floatPrecision emit optionnothing, since WGSL has no precision qualifiersit sets the stage-wide default at emit
usampler2D and isampler2Dtexture_2d<u32> and texture_2d<i32>texture_2d<u32> and texture_2d<i32>the sampler precision line is emitted for you
#extension … : requirethe 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 passsemanticDiffthe samecompares 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:

"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" spellingNotes
gl_Positionbuiltin<"position"> on the vertex outputwrites gl_Position on GLSL. A vertex entry that returns a bare vec4 returns the position
gl_FragCoordbuiltin<"position"> on a fragment inputreads 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_VertexIDvi: builtin<"vertex_index">GLSL wraps the read as uint(gl_VertexID), since the surface types it u32 and GLSL’s is int
gl_InstanceIDii: builtin<"instance_index">the same uint() wrap
gl_FrontFacingfront: builtin<"front_facing">
gl_FragDepthbuiltin<"frag_depth"> on a field of the return struct
gl_PointSize and gl_PointCoordunsupported on both writerspoint 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 corner uv
float mod(x, y)the builtin 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:

"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 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() keeps the WGSL, leaves glsl undefined and says why in a 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 describes, with what a raw statement costs the rest of the module.

이 페이지 편집 문제 보고