wgsl.runLearn WGSL
English Türkçe

Chapter 08

Built-ins

The built-in library is where WGSL feels less like syntax and more like a GPU toolbox: numeric functions, vector operations, texture access, packing helpers, atomics, barriers, and subgroup operations.

Math and vector built-ins

Functions such as sin, cos, dot, normalize, and mix work best when the input types are already intentional. Let types lead; then pick the helper.

vector mathfragment color
@fragment
fn fs(@builtin(position) frag : vec4f) -> @location(0) vec4f {
  let uv = frag.xy / vec2f(800.0, 600.0);
  let wave = 0.5 + 0.5 * sin(uv.x * 24.0);
  return vec4f(uv, wave, 1.0);
}

Textures and sampling built-ins

Texture functions carry pipeline-stage rules, sampler state, coordinate types, and sometimes uniformity requirements. When a sampling diagnostic appears, inspect both the texture type and the control flow around the call.

Synchronization built-ins

workgroupBarrier, storageBarrier, and atomic built-ins are coordination tools. Reach for them only after the memory sharing pattern is clear.

FamilyExamples
Mathabs, min, max, clamp, mix
Vectordot, cross, length, normalize
TexturetextureLoad, textureSample, textureDimensions
AtomicatomicLoad, atomicAdd, atomicCompareExchangeWeak