www.xbdev.net
xbdev - software development
Wednesday June 25, 2025
Home | Contact | Support | Shaders.. Programming the GPU... | WebGPU Shader Language... Even web-pages can use the power of the GPU...
     
 

WebGPU Shader Language...

Even web-pages can use the power of the GPU...

 


Mutable Global Variable


In WGSL,
var<private>
declares a mutable global variable that is only accessible within the same shader module (unlike
uniform
or
storage
buffers, which can be shared across pipelines). Unlike
const
(which is immutable and must be initialized at declaration), a
private
variable can be modified at runtime.

For example:

const variable:f32 = 2.0;
→ Fixed value, cannot change.

var<private> gTime: f32 = 0.0;
→ Can be updated (e.g.,
gTime = 1.0;
).

Why Use
var<private>
? Pros and Cons


Use cases:
• Track state across multiple functions in a shader (e.g., animation time).
• Store intermediate results reused in later calculations.

Pros:

✔️ Retains value across function calls.
✔️ Avoids passing values through parameters repeatedly.

Cons:

Not shared between shader invocations (unlike
workgroup
or
storage
vars).
❌ Overuse can make code harder to debug (global state).


Simple WGSL Example


// A private variable (mutable, module-scoped)
var<private> counterf32 0.0;

// A constant (immutable)
const MAX_COUNTf32 5.0;

@
fragment
fn main() -> @location(0vec4<f32> {
    
// Update the private variable
    
counter counter 1.0;
    
    
// Use it to compute color (resets after MAX_COUNT)
    
let progress counter MAX_COUNT;
    return 
vec4<f32>(progress0.01.0 progress1.0);
}


Key Notes:

counter
persists across shader invocations (but only for the current GPU thread).
• Unlike
const MAX_COUNT
,
counter
can be modified.
• Try replacing
var<private>
with
const
—it will fail because
const
can’t be reassigned!


When to Avoid
private


Use
workgroup
for compute shaders (shared across threads) or
uniform
for CPU-GPU data. Reserve
private
for shader-internal state.


Where To Learn More


• WebGPU Tutorials/Books [LINK]
• WebGPU Lab (100s of WebGPU Examples) [LINK]
• Official WGSL Documentation [LINK]















101 WebGPU Programming Projects. WebGPU Development Pixels - coding fragment shaders from post processing to ray tracing! WebGPU by Example: Fractals, Image Effects, Ray-Tracing, Procedural Geometry, 2D/3D, Particles, Simulations WebGPU Games WGSL 2d 3d interactive web-based fun learning WebGPU Compute WebGPU API - Owners WebGPU Development Cookbook - coding recipes for all your webgpu needs! WebGPU & WGSL Essentials: A Hands-On Approach to Interactive Graphics, Games, 2D Interfaces, 3D Meshes, Animation, Security and Production Kenwright graphics and animations using the webgpu api 12 week course kenwright learn webgpu api kenwright programming compute and graphics applications with html5 and webgpu api kenwright real-time 3d graphics with webgpu kenwright webgpu for dummies kenwright webgpu wgsl compute graphics all in one kenwright webgpu api develompent a quick start guide kenwright webgpu by example 2022 kenwright webgpu gems kenwright webgpu interactive compute and graphics visualization cookbook kenwright wgsl webgpu shading language cookbook kenwright WebGPU Shader Language Development: Vertex, Fragment, Compute Shaders for Programmers Kenwright WGSL Fundamentals book kenwright WebGPU Data Visualization Cookbook kenwright Special Effects Programming with WebGPU kenwright WebGPU Programming Guide: Interactive Graphics and Compute Programming with WebGPU & WGSL kenwright Ray-Tracing with WebGPU kenwright



 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.