Skip to content

Commit

Permalink
Update WGSL example shaders to use assignment ops
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jan 11, 2022
1 parent 1bab287 commit 36220b4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 47 deletions.
17 changes: 8 additions & 9 deletions wgpu/examples/boids/compute.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ fn main([[builtin(global_invocation_id)]] global_invocation_id: vec3<u32>) {
let vel = particlesSrc.particles[i].vel;

if (distance(pos, vPos) < params.rule1Distance) {
cMass = cMass + pos;
cMassCount = cMassCount + 1;
cMass += pos;
cMassCount += 1;
}
if (distance(pos, vPos) < params.rule2Distance) {
colVel = colVel - (pos - vPos);
colVel -= pos - vPos;
}
if (distance(pos, vPos) < params.rule3Distance) {
cVel = cVel + vel;
cVelCount = cVelCount + 1;
cVel += vel;
cVelCount += 1;
}

continuing {
Expand All @@ -71,7 +71,7 @@ fn main([[builtin(global_invocation_id)]] global_invocation_id: vec3<u32>) {
cMass = cMass * (1.0 / f32(cMassCount)) - vPos;
}
if (cVelCount > 0) {
cVel = cVel * (1.0 / f32(cVelCount));
cVel *= 1.0 / f32(cVelCount);
}

vVel = vVel + (cMass * params.rule1Scale) +
Expand All @@ -82,7 +82,7 @@ fn main([[builtin(global_invocation_id)]] global_invocation_id: vec3<u32>) {
vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1);

// kinematic update
vPos = vPos + (vVel * params.deltaT);
vPos += vVel * params.deltaT;

// Wrap around boundary
if (vPos.x < -1.0) {
Expand All @@ -99,6 +99,5 @@ fn main([[builtin(global_invocation_id)]] global_invocation_id: vec3<u32>) {
}

// Write back
particlesDst.particles[index].pos = vPos;
particlesDst.particles[index].vel = vVel;
particlesDst.particles[index] = Particle(vPos, vVel);
}
22 changes: 4 additions & 18 deletions wgpu/examples/shadow/shader.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,15 @@ fn fs_main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
let normal = normalize(in.world_normal);
// accumulate color
var color: vec3<f32> = c_ambient;
var i: u32 = 0u;
loop {
if (i >= min(u_globals.num_lights.x, c_max_lights)) {
break;
}
for(var i = 0u; i < min(u_globals.num_lights.x, c_max_lights); i += 1u) {
let light = s_lights.data[i];
// project into the light space
let shadow = fetch_shadow(i, light.proj * in.world_position);
// compute Lambertian diffuse term
let light_dir = normalize(light.pos.xyz - in.world_position.xyz);
let diffuse = max(0.0, dot(normal, light_dir));
// add light contribution
color = color + shadow * diffuse * light.color.xyz;
continuing {
i = i + 1u;
}
color += shadow * diffuse * light.color.xyz;
}
// multiply the light by material color
return vec4<f32>(color, 1.0) * u_entity.color;
Expand All @@ -112,21 +105,14 @@ fn fs_main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
fn fs_main_without_storage(in: VertexOutput) -> [[location(0)]] vec4<f32> {
let normal = normalize(in.world_normal);
var color: vec3<f32> = c_ambient;
var i: u32 = 0u;
loop {
if (i >= min(u_globals.num_lights.x, c_max_lights)) {
break;
}
for(var i = 0u; i < min(u_globals.num_lights.x, c_max_lights); i += 1u) {
// This line is the only difference from the entrypoint above. It uses the lights
// uniform instead of the lights storage buffer
let light = u_lights.data[i];
let shadow = fetch_shadow(i, light.proj * in.world_position);
let light_dir = normalize(light.pos.xyz - in.world_position.xyz);
let diffuse = max(0.0, dot(normal, light_dir));
color = color + shadow * diffuse * light.color.xyz;
continuing {
i = i + 1u;
}
color += shadow * diffuse * light.color.xyz;
}
return vec4<f32>(color, 1.0) * u_entity.color;
}
6 changes: 3 additions & 3 deletions wgpu/examples/water/terrain.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ struct Uniforms {
[[group(0), binding(0)]]
var<uniform> uniforms: Uniforms;

let light: vec3<f32> = vec3<f32>(150.0, 70.0, 0.0);
let light_colour: vec3<f32> = vec3<f32>(1.0, 0.98, 0.82);
let ambient: f32 = 0.2;
let light = vec3<f32>(150.0, 70.0, 0.0);
let light_colour = vec3<f32>(1.0, 0.98, 0.82);
let ambient = 0.2;

struct VertexOutput {
[[builtin(position)]] position: vec4<f32>;
Expand Down
34 changes: 17 additions & 17 deletions wgpu/examples/water/water.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ struct Uniforms {
};
[[group(0), binding(0)]] var<uniform> uniforms: Uniforms;

let light_point: vec3<f32> = vec3<f32>(150.0, 70.0, 0.0);
let light_colour: vec3<f32> = vec3<f32>(1.0, 0.98, 0.82);
let one: vec4<f32> = vec4<f32>(1.0, 1.0, 1.0, 1.0);
let light_point = vec3<f32>(150.0, 70.0, 0.0);
let light_colour = vec3<f32>(1.0, 0.98, 0.82);
let one = vec4<f32>(1.0, 1.0, 1.0, 1.0);

let Y_SCL: f32 = 0.86602540378443864676372317075294;
let CURVE_BIAS: f32 = -0.1;
Expand Down Expand Up @@ -90,21 +90,21 @@ fn snoise(v: vec3<f32>) -> f32 {
let a0 = b0.xzyw + s0.xzyw*sh.xxyy;
let a1 = b1.xzyw + s1.xzyw*sh.zzww;

var p0: vec3<f32> = vec3<f32>(a0.xy, h.x);
var p1: vec3<f32> = vec3<f32>(a0.zw, h.y);
var p2: vec3<f32> = vec3<f32>(a1.xy, h.z);
var p3: vec3<f32> = vec3<f32>(a1.zw, h.w);
var p0 = vec3<f32>(a0.xy, h.x);
var p1 = vec3<f32>(a0.zw, h.y);
var p2 = vec3<f32>(a1.xy, h.z);
var p3 = vec3<f32>(a1.zw, h.w);

//Normalise gradients
let norm = taylorInvSqrt(vec4<f32>(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));
p0 = p0 * norm.x;
p1 = p1 * norm.y;
p2 = p2 * norm.z;
p3 = p3 * norm.w;
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;

// Mix final noise value
var m: vec4<f32> = max(0.6 * one - vec4<f32>(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0 * one);
m = m * m;
m *= m;
return 9.0 * dot(m*m, vec4<f32>(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3)));
}

Expand Down Expand Up @@ -218,17 +218,17 @@ fn vs_main(
}


let water_colour: vec3<f32> = vec3<f32>(0.0, 0.46, 0.95);
let zNear: f32 = 10.0;
let zFar: f32 = 400.0;
let water_colour = vec3<f32>(0.0, 0.46, 0.95);
let zNear = 10.0;
let zFar = 400.0;

[[group(0), binding(1)]] var reflection: texture_2d<f32>;
[[group(0), binding(2)]] var terrain_depth_tex: texture_2d<f32>;
[[group(0), binding(3)]] var colour_sampler: sampler;

fn to_linear_depth(depth: f32) -> f32 {
let z_n: f32 = 2.0 * depth - 1.0;
let z_e: f32 = 2.0 * zNear * zFar / (zFar + zNear - z_n * (zFar - zNear));
let z_n = 2.0 * depth - 1.0;
let z_e = 2.0 * zNear * zFar / (zFar + zNear - z_n * (zFar - zNear));
return z_e;
}

Expand Down

0 comments on commit 36220b4

Please sign in to comment.