A beautiful piece of Rust code!
Converting this to WGSL (WebGPU Shading Language) requires some changes in mindset, as WGSL is a shader language, not a general-purpose programming language like Rust. We'll write a fragment shader (also known as a pixel shader) that achieves the same visual effect.
Here's the equivalent WGSL code:
struct Uniforms {
width: u32;
height: u32;
shift: u32;
};
@group(0) @binding(0)
var<uniform> uniforms: Uniforms;
@fragment
fn fs_main(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> {
let fragCoord = vec2<i32>(pos.xy);
let width = i32(uniforms.width);
let height = i32(uniforms.height);
let shift = i32(uniforms.shift);
let index = fragCoord.y * width + fragCoord.x;
let shiftedX = (fragCoord.x + shift) % width;
let r = min(((width - shiftedX) * 255) / width, ((height - fragCoord.y) * 255) / height);
let g = min((shiftedX * 255) / width, ((height - fragCoord.y) * 255) / height);
let b = min(((width - shiftedX) * 255) / width, (fragCoord.y * 255) / height);
return vec4<f32>(f32(r) / 255.0, f32(g) / 255.0, f32(b) / 255.0, 1.0);
}
Let's go through the changes:
- Uniforms: In WGSL, we define a
struct to hold our uniforms (constant values passed from the application). We have width, height, and shift as u32 values.
- Binding: We declare a uniform buffer (
var<uniform>) and bind it to group 0, binding 0. This is where the application will pass our uniforms.
- Fragment shader: The
fs_main function is the entry point for our fragment shader. It takes the pos (fragment position) as input, which we'll use to compute the pixel coordinates.
- Position and coordinates: In WGSL,
pos is a vec4<f32> with the fragment position in screen space (x, y, z, w). We extract the x and y coordinates as i32 values using vec2<i32>(pos.xy).
- Shifted X: We compute the shifted X coordinate, just like in the Rust code:
(fragCoord.x + shift) % width.
- Color calculations: The color calculations are almost identical to the Rust code. Note that WGSL has built-in
min function, which we use instead of u32::min.
- Return value: The fragment shader returns a
vec4<f32> representing the final color (red, green, blue, alpha). We normalize the color values from u32 (0-255) to f32 (0.0-1.0) by dividing by 255.0.
What's missing?
In the Rust code, you had a shift value that incremented modulo width on each frame. In WGSL, we don't have a direct equivalent, as shaders are stateless. To achieve the same effect, you'll need to:
- In your application (e.g., JavaScript or Rust with WebGPU), create a uniform buffer with the
Uniforms struct.
- Initialize
shift to 0.
- On each frame:
- Update the
shift value in the uniform buffer: shift = (shift + 1) % width.
- Bind the updated uniform buffer to the shader.
- Draw the fullscreen quad (or your geometry) to execute the shader.
Example JavaScript code (using WebGPU) to update the uniform buffer:
const device = // ... get your WebGPU device ...
const uniformBuffer = device.createBuffer({
size: 12, // 3 x u32 = 12 bytes
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});
const uniformsArray = new Uint32Array(3);
uniformsArray[0] = width;
uniformsArray[1] = height;
uniformsArray[2] = 0; // initial shift
function animate() {
uniformsArray[2] = (uniformsArray[2] + 1) % width; // update shift
device.queue.writeBuffer(uniformBuffer, 0, uniformsArray);
const passEncoder = // ... create your render pass encoder ...
passEncoder.setPipeline(yourPipeline); // pipeline with the WGSL shader
passEncoder.setBindGroup(0, yourBindGroupWithUniformBuffer); // bind uniform buffer
passEncoder.draw(6, 1, 0, 0); // draw fullscreen quad (2 triangles)
passEncoder.end();
requestAnimationFrame(animate);
}
Now, go forth and render that beautiful gradient!