2021-01-30 05:11:23 +00:00
|
|
|
struct Globals {
|
2022-03-12 21:48:10 +00:00
|
|
|
view_proj: mat4x4<f32>,
|
|
|
|
num_lights: vec4<u32>,
|
2022-03-27 05:35:08 +00:00
|
|
|
}
|
2021-01-30 05:11:23 +00:00
|
|
|
|
2022-03-09 08:01:56 +00:00
|
|
|
@group(0)
|
|
|
|
@binding(0)
|
2021-01-30 05:11:23 +00:00
|
|
|
var<uniform> u_globals: Globals;
|
|
|
|
|
2022-03-09 08:01:56 +00:00
|
|
|
struct Entity {
|
2022-03-12 21:48:10 +00:00
|
|
|
world: mat4x4<f32>,
|
|
|
|
color: vec4<f32>,
|
2022-03-27 05:35:08 +00:00
|
|
|
}
|
2022-03-09 08:01:56 +00:00
|
|
|
|
|
|
|
@group(1)
|
|
|
|
@binding(0)
|
|
|
|
var<uniform> u_entity: Entity;
|
|
|
|
|
2022-03-11 06:17:02 +00:00
|
|
|
/* Not useful for testing
|
2022-03-09 08:01:56 +00:00
|
|
|
@stage(vertex)
|
|
|
|
fn vs_bake(@location(0) position: vec4<i32>) -> @builtin(position) vec4<f32> {
|
|
|
|
return u_globals.view_proj * u_entity.world * vec4<f32>(position);
|
|
|
|
}
|
2022-03-11 06:17:02 +00:00
|
|
|
*/
|
2022-03-09 08:01:56 +00:00
|
|
|
|
|
|
|
struct VertexOutput {
|
2022-03-12 21:48:10 +00:00
|
|
|
@builtin(position) proj_position: vec4<f32>,
|
|
|
|
@location(0) world_normal: vec3<f32>,
|
|
|
|
@location(1) world_position: vec4<f32>,
|
2022-03-27 05:35:08 +00:00
|
|
|
}
|
2022-03-09 08:01:56 +00:00
|
|
|
|
|
|
|
@stage(vertex)
|
|
|
|
fn vs_main(
|
|
|
|
@location(0) position: vec4<i32>,
|
|
|
|
@location(1) normal: vec4<i32>,
|
|
|
|
) -> VertexOutput {
|
|
|
|
let w = u_entity.world;
|
|
|
|
let world_pos = u_entity.world * vec4<f32>(position);
|
|
|
|
var out: VertexOutput;
|
|
|
|
out.world_normal = mat3x3<f32>(w.x.xyz, w.y.xyz, w.z.xyz) * vec3<f32>(normal.xyz);
|
|
|
|
out.world_position = world_pos;
|
|
|
|
out.proj_position = u_globals.view_proj * world_pos;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fragment shader
|
|
|
|
|
2021-01-30 05:11:23 +00:00
|
|
|
struct Light {
|
2022-03-12 21:48:10 +00:00
|
|
|
proj: mat4x4<f32>,
|
|
|
|
pos: vec4<f32>,
|
|
|
|
color: vec4<f32>,
|
2022-03-27 05:35:08 +00:00
|
|
|
}
|
2021-01-30 05:11:23 +00:00
|
|
|
|
2022-03-09 08:01:56 +00:00
|
|
|
@group(0)
|
|
|
|
@binding(1)
|
|
|
|
var<storage, read> s_lights: array<Light>;
|
|
|
|
@group(0)
|
|
|
|
@binding(1)
|
|
|
|
var<uniform> u_lights: array<Light, 10>; // Used when storage types are not supported
|
|
|
|
@group(0)
|
|
|
|
@binding(2)
|
2021-01-30 05:11:23 +00:00
|
|
|
var t_shadow: texture_depth_2d_array;
|
2022-03-09 08:01:56 +00:00
|
|
|
@group(0)
|
|
|
|
@binding(3)
|
2021-03-25 05:05:33 +00:00
|
|
|
var sampler_shadow: sampler_comparison;
|
2021-01-30 05:11:23 +00:00
|
|
|
|
|
|
|
fn fetch_shadow(light_id: u32, homogeneous_coords: vec4<f32>) -> f32 {
|
2022-03-09 08:01:56 +00:00
|
|
|
if (homogeneous_coords.w <= 0.0) {
|
2021-01-30 05:11:23 +00:00
|
|
|
return 1.0;
|
|
|
|
}
|
2022-03-09 08:01:56 +00:00
|
|
|
// compensate for the Y-flip difference between the NDC and texture coordinates
|
2021-04-11 04:35:03 +00:00
|
|
|
let flip_correction = vec2<f32>(0.5, -0.5);
|
2022-03-09 08:01:56 +00:00
|
|
|
// compute texture coordinates for shadow lookup
|
|
|
|
let proj_correction = 1.0 / homogeneous_coords.w;
|
|
|
|
let light_local = homogeneous_coords.xy * flip_correction * proj_correction + vec2<f32>(0.5, 0.5);
|
|
|
|
// do the lookup, using HW PCF and comparison
|
|
|
|
return textureSampleCompareLevel(t_shadow, sampler_shadow, light_local, i32(light_id), homogeneous_coords.z * proj_correction);
|
2021-01-30 05:11:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 04:35:03 +00:00
|
|
|
let c_ambient: vec3<f32> = vec3<f32>(0.05, 0.05, 0.05);
|
|
|
|
let c_max_lights: u32 = 10u;
|
2021-01-30 05:11:23 +00:00
|
|
|
|
2022-01-19 15:33:06 +00:00
|
|
|
@stage(fragment)
|
2022-03-09 08:01:56 +00:00
|
|
|
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
|
|
|
let normal = normalize(in.world_normal);
|
2021-01-30 05:11:23 +00:00
|
|
|
// accumulate color
|
2022-03-09 08:01:56 +00:00
|
|
|
var color: vec3<f32> = c_ambient;
|
2022-03-22 15:07:54 +00:00
|
|
|
for(var i = 0u; i < min(u_globals.num_lights.x, c_max_lights); i++) {
|
2022-03-09 08:01:56 +00:00
|
|
|
let light = s_lights[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);
|
2021-04-11 04:35:03 +00:00
|
|
|
let diffuse = max(0.0, dot(normal, light_dir));
|
2022-03-09 08:01:56 +00:00
|
|
|
// add light contribution
|
|
|
|
color += shadow * diffuse * light.color.xyz;
|
2021-01-30 05:11:23 +00:00
|
|
|
}
|
|
|
|
// multiply the light by material color
|
2022-03-09 08:01:56 +00:00
|
|
|
return vec4<f32>(color, 1.0) * u_entity.color;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The fragment entrypoint used when storage buffers are not available for the lights
|
|
|
|
@stage(fragment)
|
|
|
|
fn fs_main_without_storage(in: VertexOutput) -> @location(0) vec4<f32> {
|
|
|
|
let normal = normalize(in.world_normal);
|
|
|
|
var color: vec3<f32> = c_ambient;
|
2022-03-22 15:07:54 +00:00
|
|
|
for(var i = 0u; i < min(u_globals.num_lights.x, c_max_lights); i++) {
|
2022-03-09 08:01:56 +00:00
|
|
|
// 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[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 += shadow * diffuse * light.color.xyz;
|
|
|
|
}
|
|
|
|
return vec4<f32>(color, 1.0) * u_entity.color;
|
2021-01-30 05:11:23 +00:00
|
|
|
}
|