www.xbdev.net
xbdev - software development
Wednesday May 7, 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...

 


WGSL '%' vs GLSL 'mod'


Are they the same?
NO


In GLSL, the `mod(x, y)` function computes true mathematical modulo, which always returns a non-negative result in the range
[0, y)
, even when
x
is negative. This is useful for periodic operations like wrapping angles or texture coordinates. However, WGSL's
%
operator is a remainder operation, not a true modulo—it preserves the sign of the dividend (
x % y
has the same sign as
x
). This means negative inputs can produce negative results, which can cause issues in polar coordinate calculations, tiling, or other cases where a positive range is expected.

To match GLSL's
mod
behavior in WGSL, we need to manually adjust the result. A common approach is to compute
x - y * floor(x / y)
, which ensures the output stays non-negative. Below is a
mymod(..)
function that replicates GLSL's
mod
in WGSL, followed by a simple example demonstrating the difference between WGSL's
%
and the corrected version.

Example:
mymod
vs
%
in WGSL


// Replicates GLSL's mod(x, y) behavior
fn mymod(xf32yf32) -> f32 {
    return 
floor(y);
}

@
fragment
fn main(@builtin(positionfragCoordvec4<f32>) -> @location(0vec4<f32> {
    
let x = -5.7;
    
let y 3.0;
    
    
// WGSL '%' (remainder, keeps sign)
    
let wgsl_mod y;  // Returns -2.7
    
    // GLSL-style mod (always positive)
    
let glsl_mod mymod(xy);  // Returns 0.3 (same as GLSL)
    
    // Visualize the difference
    
if (fragCoord.300.0) {
        return 
vec4<f32>(wgsl_mod0.00.01.0);  // Red if negative
    
} else {
        return 
vec4<f32>(0.0glsl_mod0.01.0);  // Green if positive
    
}
}


This example shows that
-5.7 % 3.0
in WGSL gives
-2.7
(remainder), while
mymod(-5.7, 3.0)
correctly returns
0.3
(like GLSL). Use
mymod
when you need periodic wrapping without negative results.














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.