📱 Erkannter Endgerättyp 🔇 Sound ist aus. ⛱️ Tag und Nacht. Verbraucht keinen oder einen 🍪.
guest
Login 🗃️ Speicher- und Dateiverwaltung 🍪 0 Anzahl Ihrer gespeicherten Kekse, führt zur Keksdose

WebGPU

Glossar

const – The declaration is of a compile time constant.

let - immutable

var - mutable

attribute - specify data pulled from buffers and fed to each iteration of a vertex shader.

Global variables that may change per vertex, that are passed from the WebGPU application to vertex shaders. This qualifier can only be used in vertex shaders. For the shader this is a read-only variable.

struct Vertex4Input {
    @location(0) position: vec4f,     // <-- attribute!
    @location(1) material_id: u32,    // <-- attribute!
}

uniform – specify values shared by all iterations of a shader function.

Global variables that may change per primitive [...], that are passed from the WebGPU application to the shaders. This qualifier can be used in both vertex and fragment shaders. For the shaders this is a read-only variable.

varying - pass data from a vertex shader to a fragment shader and interpolate between values computed by the vertex shader when rasterizing via a fragment shader.

used for interpolated data between a vertex shader and a fragment shader. Available for writing in the vertex shader, and read-only in a fragment shader. See Varying section.

struct FSOutput {
    @location(0) color: vec4f, //
}

So in WebGPU you dont define them as varying its just another @location

Transforms

Local

(Local Projection (4D to 3D + w))

World

View

Clip (VS Out)

NDC

Screen (FS In)

Built-in Attribute der Shader

CS In

💻 Compute Shader Inputs

Variable Type Meaning
@builtin(local_invocation_id) vec3<u32> Thread ID inside a workgroup.
@builtin(global_invocation_id) vec3<u32> Global thread ID (across all workgroups).
@builtin(workgroup_id) vec3<u32> ID of the current workgroup.
@builtin(num_workgroups) vec3<u32> Total workgroups dispatched.
@builtin(local_invocation_index) u32 Linear index inside the workgroup.

VS In VS Out

🎯 Vertex Shader Inputs

Variable Type Meaning
@builtin(vertex_index) u32 Auto-generated index per vertex. Use when not using vertex buffers or procedural geometry.
@builtin(instance_index) u32 Current instance (used in instanced rendering) — think of this like a "loop index" for multiple draws of the same geometry.

🌀 Vertex Shader Outputs

Variable Type Meaning
vs @builtin(position) vec4<f32> Final clip-space position — required for rasterization.

🔬 Also:

Der Vertex Shader läuft n Mal → 1× pro Vertex.

Der Rasterizer interpoliert Outputs der Vertex Shader → z. B. Farben, UVs, Positionen.

Der Fragment Shader wird nur für gültige Fragmente (Pixel im Dreieck) ausgeführt.

Kein expliziter Index nötig — @builtin(frag_coord) sagt dir, wo du gerade bist.

Schleifen brauchst du nur, wenn du selbst mehrere Samples, Lichtquellen, oder ähnliches berechnen willst. Nicht um über Pixel zu iterieren — das macht die GPU schon für dich.

FS In FS Out

Der Fragment shader wird vom Clipspace aus PRO primitive aufgerufen und loopt dann über frag_coord:

🎨 Fragment Shader Inputs

Variable Type Meaning
fs @builtin(position) vec4<f32> Interpolated position from vertex shader (clip space → screen space) - so the value is in screen space xy pixels, z depth and 1/w!
@builtin(front_facing) bool True if current triangle is front-facing (useful for lighting or backface culling).
@builtin(sample_index) u32 Used in MSAA contexts.
@builtin(sample_mask) u32 MSAA mask if you're doing advanced sampling.
@builtin(frag_coord) vec4<f32> Screen-space position, including depth (z) — very useful for screen effects.

RenderPipeline

the fragment shader output is always location(0) vec4f as this is the canvas texture - location(n) here references the target in

fragment: {
    module: shaderModule,
    targets: [
        // location(0) is a canvas
        {
            // will be rgba8unorm or bgra8unorm
            // comes from navigator.gpu.getPreferredCanvasFormat()
            format: this.presentationFormat,
            blend: this.blendStateAlpha
        },
        // location(1) is a texture for picking
        {
            format: 'r32uint',
            writeMask: 0,
        },
        // location(2)
        {
            format: 'r32uint',
            writeMask: 0,
        },
    ],
},
depthStencil: {
    format: 'depth24plus',
    depthWriteEnabled: true,
    depthCompare: 'less', // for z+ == forward (LH) so only draw if new z is smaller 
},

Texture Output location(n)

Attachments = Textures

const renderPass = this.renderCommandEncoder.beginRenderPass(
    {
        label: "RenderPass Label",
        colorAttachments: [
            // target / location 0                
            rendertarget.gpuContext.colorAttachments[0]
            // target / location 1
            {
                view: rendertarget.nodeRimTextureView,
                clearValue: [0, 0, 0, 0],
                loadOp: 'clear',
                storeOp: 'store'
            },
            {
                view: rendertarget.nodeCenterTextureView,
                clearValue: [0, 0, 0, 0],
                loadOp: 'clear',
                storeOp: 'store'
            }
            ...
        ],
        depthStencilAttachment: {
            view: depthTextureView, // texture
            depthClearValue: 1.0, // 1 so it's true when less for z+ == forward (LH)
            depthLoadOp: 'clear',
            depthStoreOp: 'store',
        },
    },

);

Load and Store Op

Now in a renderpass you define what happens to these on

loadOp == renderpass starts

usually it will be cleared on load

Options clear, load

storeOp == renderpass.end()

you could discard here too but then ofc would need to get data you want while its still in the renderpass

Opions: store, discard

depthStencilAttachment

Theres only one of these when depth buffering is enabled

depthCompare Options always | equal | greater | greater-equal | less | less-equal | never | not-equal

Beware less-equal can flicker so sometimes better use a small offset

Write Masks and Alpha Blending

Now what happens to color when you write multiple times to it?

fragment: {
    module: shaderModule,
    targets: [
        // location(0) is a canvas
        {
            format: many options, usually 'bgra8unorm' or 'r32uint'
            blend: {
                    color:      GPUBlendComponent
                    alpha:      GPUBlendComponent
            }
            writeMask:            R=1, G=2, B=4, A=8 or no write = 0, default is GPUColorWrite.ALL == 15 (?)
        }
    ],
}

interface GPUBlendComponent {
    dstFactor?: GPUBlendFactor;
    operation?: GPUBlendOperation;      "add" | "max" | "min" | "reverse-subtract" | "subtract";
    srcFactor?: GPUBlendFactor;         "constant" | "dst" | "dst-alpha" | "one" | "one-minus-constant" | "one-minus-dst" | "one-minus-dst-alpha" | "one-minus-src" | "one-minus-src-alpha" | "src" | "src-alpha" | "src-alpha-saturated" | "zero";
}

Alpha Blending

You need the alpha-information preapplied to the rgb color channels, see

https://en.wikipedia.org/wiki/Alpha_compositing

and the canvas will be alphaMode: "premultiplied"

So when you define the blendMode for this target:

color: 
    srcFactor: 'one', 
    dstFactor: 'one-minus-src-alpha',
    operation: 'add',

alpha: 
    srcFactor: 'one', 
    dstFactor: 'one-minus-src-alpha',
    operation: 'add',

Draw Flow

  • You draw a 0.1 R 0.2 G 0.3 B 0.4 A (60% transparent), that's output from your fragment shader:

    output.color = vec4(rgb * alpha, alpha);

So 0.04, 0.08, 0.12, 0.4 RGBA

dst is 0 on the first draw

The srcFactor of one takes that 1:1 and calculates the dstFactor with 1 - alpha in src (0.4)

1.0 − src.a = 1.0 − 0.4 = 0.6

This dstFactor of 0.6 now gets multiplied with what is already on the dst (0 on first draw)

dstNew.rgb  = (dst.rgb * 0.6) + (src.rgb  * 1.0) = (0.0, 0.0, 0.0) + (0.04, 0.08, 0.12) = (0.04, 0.08, 0.12)
dstNew.a    = (dst.a   * 0.6) + (src.a    * 1.0) = 0.0             + 0.4                = 0.4

So we end up in dst 0.04, 0.08, 0.12, 0.4

BUT NOW SECOND DRAW

dst is 0.04, 0.08, 0.12, 0.4

another shader outputs a 50% green, so 0.0, 0.5, 0.0, 0.5

The srcFactor of one takes that 1:1 and calculates the dstFactor with 1 - alpha in src (0.5)

1.0 − src.a = 1.0 − 0.5 = 0.5

This dstFactor of 0.5 again gets multiplied with what is already on the dst: 0.04, 0.08, 0.12, 0.4

dstNew.rgb  = (dst.rgb * 0.5) + (src.rgb  * 1.0) = (0.02, 0.04, 0.06) + (0.0, 0.5, 0.0) = (0.02, 0.54, 0.06)
dstNew.a    = (dst.a   * 0.5) + (src.a    * 1.0) = 0.2                + 0.5             = 0.7

rinse and repeat.

Browser Canvas

browser's compositor takes your this canvas texture and blends it over your HTML page (e.g. your dark backdrop):

PagePixel = Canvas.rgb + HTMLBackdrop.rgb * (1.0 − Canvas.a)

Clamp alpha!

If a distance fade ever drops src.a negative, 1.0 - src.a becomes > 1.0 and multiplies the canvas light upwards instead of attenuating it—clamping keeps dstFactor strictly in [0.0, 1.0].

Samplers

Current PTF

Hintergrund ändern. Verbraucht keinen oder einen 🍪.

Verknüpften Viewport öffnen

🎮 Steuerung
Dokumentation 🕹️
Sie sind leider kein Entwickler :(

Content Nodes Amount

Diligence / PTF Amount

FPS

Vertex-Count