커밋 af54ddad5의 AUTHORING.md를 그대로 옮긴 것으로, 이 절의 본문은 아직 영어입니다. 패키지는 0.2.0에서 쓸 typeshade라는 이름으로 가져옵니다.
레이아웃과 리소스
After this page you can declare a vertex, uniform, storage or texture layout once and read every field off that one declaration.
A layout is the agreement between a shader and the pipeline that feeds it: the fields one
stage hands to the next, the uniform block a draw call binds, the buffers and textures at
each slot. Each is a declaration in the file. A struct is a class, an interface or a
type alias, and a resource is a declare const whose type says what it holds. Because
every read names a declared field, a field name or a field type you get wrong is an error at
the line that reads it.
IO structs
An IO struct is a group of fields that crosses a stage boundary: a vertex stage returns
it and a fragment stage takes it as a parameter. It is an interface, a class or a
type. A class field needs no
initializer and no ! (pos: builtin<"position">): the stage that produces the field
fills it. builtin<"id"> declares a value the hardware supplies, of the type WGSL gives it, and takes a WGSL builtin
id such as "position" or "vertex_index"; an id WGSL does not define is an error.
Every other field is a user-defined value, and the compiler gives it its location in the order
written. location<n, T> states the number, with an optional third type argument
{ interpolate: "flat" }, { interpolate: "linear" } or { interpolate: "perspective" }.
The decorated class field of earlier releases (@location(0) uv: vec2) stays accepted until
the release after the migration release. An integer field gets flat without asking, because both
targets require it for an integer varying. "flat" and "perspective" are the modes both
targets support; "linear" is not supported yet by the GLSL backend (Rule 10.3), so a module that uses it has no GLSL.
"use typeshade";
interface VsIn { xy: vec2; // @location(0) uv: vec2; // @location(1)}
interface VsOut { pos: builtin<"position">; uv: vec2; vis: f32; id: u32; // an integer varying: flat on both targets}
// A vertex entry returns the struct. The object literal takes the declared struct,// so a missing or extra field is an error.@vertexexport function vsMain(v: VsIn, inst: builtin<"instance_index">): VsOut { const pos = vec4(v.xy, 0., 1.); return { pos: pos, uv: v.uv, vis: 1., id: inst };}
// Read fields off a parameter declared with the struct.@fragmentexport function fsMain(input: VsOut): vec4 { if (input.vis < 0.) { discard; } return vec4(input.uv, f32(input.id), 1.);}A vertex entry’s user-defined inputs are vertex attributes, loose parameters or the fields of
a struct parameter such as VsIn, and reflect() reports their buffer layout. A struct
value built over several statements is a let of the struct type whose fields you assign.
Uniform blocks
A uniform block is a struct the host fills once per draw and every invocation reads. Declare
the struct, then the binding, with declare const and no initializer: the type is
uniform<T>. Read a field by name. The result is an ordinary value, so component access and
math chain straight off it. A uniform is read-only, so assigning to a field is an error in
the editor and at compile.
"use typeshade";
interface Uniforms { mvp: mat4; projParams: vec4; rasterParams: vec4;}
declare const u: uniform<Uniforms>;
@vertexexport function vs(pos: vec3): vec4 { const opacity = u.rasterParams.x; return u.mvp * vec4(pos, opacity);}The slot is the declaration’s place in the file: each declare const takes the next binding
of group 0, and reflect() reports it to the host. uniform<T> takes no access mode, since a
uniform buffer is read-only.
A uniform may also hold one scalar or vector, declare const mode: uniform<u32>, on WGSL.
That form is not supported yet by the GLSL backend (Rule 10.3). A module that targets WebGL2 puts
the value in a struct, the std140 block GLSL ES 3.00 spells a uniform binding as; for a scalar
uniform compile() keeps the WGSL and reports the GLSL half as a TS8015 warning. The surface reference has every form and each refusal:
docs/use-typeshade-surface.md §1.
Plain structs
A struct that never crosses a stage boundary has no attribute on its fields: the element
type of a storage buffer, or a struct nested inside another one. Write it as a class, an
interface or a type alias over an object type; the three are one struct.
"use typeshade";
class ShapeSegment { kind: u32; colorIdx: u32; flags: u32; pad: u32; p0: vec2; p1: vec2; p2: vec2; p3: vec2;}
declare const segments: storage<array<ShapeSegment>>;
export function segmentLength(i: u32): f32 { const seg = segments[i]; return length(seg.p1 - seg.p0);}A class is always part of the module. A type or an interface is part of it when
something uses it: a binding, a parameter, a return, a local or a field of another used
struct. One that nothing uses is left alone, since it may be a host-side shape.
Storage buffers
A storage buffer is a bound array whose length comes from the buffer the host binds.
Declare it with declare const and no initializer. The access mode is the second type
argument: storage<T> is a read binding, and storage<T, "read_write"> is a writable
one. Index the array as you index any array:
"use typeshade";
declare const src: storage<array<f32>>;declare const dst: storage<array<f32>, "read_write">;
@compute([64, 1, 1])export function twice(gid: builtin<"global_invocation_id">): void { dst[gid.x] = src[gid.x] * 2.0;}The two declarations emit var<storage, read> src: array<f32>; and
var<storage, read_write> dst: array<f32>;, at bindings 0 and 1 of group 0, in source order.
A write to src is an error in the editor and at compile. The element can be a struct, a
scalar or a vector, so storage<array<vec4>> holds one vec4 for each element, and
src.length is the bound buffer’s element count.
GLSL ES 3.00 has no storage buffer object, so a read binding lowers to a data-texture fetch
at emit and the shader source stays as written. On the GLSL target the host allocates that
data texture, and its internal format has to match the sampler the lowering declares: R32F
for a float array, R32UI for array<u32> and array<vecN<u32>>, R32I for array<i32> and
array<vecN<i32>>. A vector element takes its std430 lanes, so a vec4u is four texels. A
struct element takes R32F when its fields are all floats, and R32UI when it has a u32 or
i32 field: upload its std430 bytes as a Uint32Array, and the shader reads its float fields
back through uintBitsToFloat. Nothing checks the pairing at runtime, so allocate from the
binding’s glslDataTexture in reflect() ('r32f', 'r32ui' or 'r32i') instead of
tracking it separately.
That lowering only gathers. A "read_write" binding that a vertex or fragment entry
reaches is not supported yet by the GLSL backend (Rule 10.5): compile() keeps the WGSL, leaves
glsl undefined and reports a TS8015 warning. A compute entry writes one on WebGL2 through its pass program,
which Portable compute kernels describes.
Textures and samplers
A texture and a sampler are written bare, with no uniform<> or storage<> wrapper,
because a handle lives in no address space. The type decides what a read accepts:
texture_2d<f32> is a 2D texture of filtered floats, and sampler is the object that
carries the filtering and wrap state. Each takes the next binding slot in declaration order,
like any other resource.
"use typeshade";
declare const tex: texture_2d<f32>;declare const texSampler: sampler;
@fragmentexport function fs(uv: location<0, vec2>): vec4 { const c = textureSample(tex, texSampler, uv); // fragment only const cv = textureSampleLevel(tex, texSampler, uv, 0.); // any stage const size = textureDimensions(tex); // vec2u, the extent in texels return c + cv * f32(size.x);}textureSample takes its mip level from screen-space derivatives, which only a fragment
invocation has, so it is fragment-only: a use that a vertex or compute entry reaches is an
error naming the entry. Read an explicit level with textureSampleLevel(tex, smp, uv, level),
which is legal in every stage. The derivative builtins fwidth, dpdx and dpdy are
fragment-only for the same reason and have no vertex or compute form, so precompute the
quantity there and pass it in. On GLSL ES 3.00 the texture and the sampler fuse into one
sampler2D, and the sampler argument disappears from the call. One texture binding can
therefore use one sampler across the selected stages. GLSL emission refuses different
samplers for that binding; WGSL remains available. Declare the texture twice, once per
sampler, and bind the same host texture to both declarations.
The WebGL2 runtime checks the same condition when it combines a vertex and fragment entry. A stage that only loads a texture imposes no sampler state. It keeps the sampler used by the other stage. Depth comparisons keep their declared comparison sampler.
Array and integer textures
A 2D array texture holds N layers behind one binding, with the layer picked on each call, so
an atlas of any depth costs one slot. Declare it as texture_2d_array<f32>. The same read
functions cover it: the texture’s type selects the array form, and the layer argument then
becomes required. A layer is an i32 and a textureLoad level a u32, so a bare number
takes the right type. textureDimensions reports width and height only, for an array
texture too, and the layer count is the separate textureNumLayers, which returns a u32.
An integer texture holds exact 32-bit texels, for an id map, a packed colour table or a
bitfield lookup. texture_2d<u32> and texture_2d<i32> declare the 2D forms, and
texture_2d_array<u32> and texture_2d_array<i32> the array twins. The result of a load
follows the texture’s element, vec4u off an unsigned one and vec4i off a signed one.
textureSample and textureSampleLevel refuse an integer texture and name textureLoad
instead, because filtering is a weighted average and WGSL has no sampling form for an
integer texture at all.
"use typeshade";
declare const atlas: texture_2d_array<f32>;declare const atlasSampler: sampler;declare const ids: texture_2d<u32>;
@fragmentexport function fs(p: builtin<"position">, uv: location<0, vec2>): vec4 { const layer: i32 = 2; const a = textureSample(atlas, atlasSampler, uv, layer); // implicit LOD, fragment only const b = textureSampleLevel(atlas, atlasSampler, uv, layer, 0.); // explicit LOD, any stage const c = textureLoad(atlas, vec2i(0, 0), layer, 0); // unfiltered texel fetch const n = textureNumLayers(atlas); // u32 const id = textureLoad(ids, vec2i(p.xy), 0); // vec4u const size = textureDimensions(ids); // vec2u, as for any other texture return a + b + c + vec4(f32(n), f32(id.x), f32(size.x), 1.);}reflect() reports each texture binding’s dimension and element, which is what a host needs
to create a matching view and to pick the sample type for it. Getting that pairing wrong
raises nothing at runtime: a texture whose format disagrees with its sampler type is
incomplete, and a fetch on it silently returns zero. The cube, 3D, depth, multisampled and
storage textures are in docs/use-typeshade-surface.md §33 to §37.