WGSL Compute Shader vs CPU Procedural Hydrology
WGSL compute dispatches parallel workgroups across grid nodes, maintaining constant 60 FPS frame times vs exponential CPU iteration delays.
// SNOWFLOW WebGPU WGSL Heightfield & Waterbending Compute Shader
struct TerrainNode {
height : f32,
snow_depth : f32,
water_depth : f32,
velocity : vec2<f32>,
};
@group(0) @binding(0) var<storage, read_write> terrainMap : array<TerrainNode>;
@group(0) @binding(1) var<uniform> params : SimParams;
struct SimParams {
grid_res : u32,
delta_time : f32,
flow_rate : f32,
viscosity : f32,
};
@compute @workgroup_size(16, 16)
fn mainCompute(@builtin(global_invocation_id) global_id : vec3<u32>) {
let x = global_id.x;
let z = global_id.y;
if (x >= params.grid_res || z >= params.grid_res) { return; }
let index = z * params.grid_res + x;
var node = terrainMap[index];
// Hydraulic Erosion & Snow Accumulation Integration Step
if (node.water_depth > 0.001) {
let dh_dx = terrainMap[index + 1].height - node.height;
let dh_dz = terrainMap[index + params.grid_res].height - node.height;
node.velocity += vec2<f32>(-dh_dx, -dh_dz) * params.flow_rate * params.delta_time;
node.height -= length(node.velocity) * 0.02 * params.delta_time; // Sediment pick-up
}
// Thermal Erosion Slope Stabilization
terrainMap[index] = node;
}