WIP shadowmapping

This commit is contained in:
Lauri Räsänen 2023-01-29 18:57:29 +02:00
parent 2fdae1a23a
commit ac67a3d4de
9 changed files with 270 additions and 122 deletions

17
res/shaders/depth.wgsl Normal file
View file

@ -0,0 +1,17 @@
#include globals.wgsl
@vertex
fn vs_main(
model: VertexInput,
instance: InstanceInput,
) -> @builtin(position) vec4<f32> {
let model_matrix = mat4x4<f32>(
instance.model_matrix_0,
instance.model_matrix_1,
instance.model_matrix_2,
instance.model_matrix_3,
);
let world_position = model_matrix * vec4<f32>(model.position, 1.0);
return camera.proj * camera.view * world_position;
}

51
res/shaders/globals.wgsl Normal file
View file

@ -0,0 +1,51 @@
// Vertex shader
struct CameraUniform {
view: mat4x4<f32>,
proj: mat4x4<f32>,
position: vec4<f32>,
}
@group(0) @binding(0)
var<uniform> camera: CameraUniform;
struct Light {
position: vec3<f32>,
color: vec4<f32>,
}
@group(1) @binding(0)
var<uniform> light: Light;
struct VertexInput {
@location(0) position: vec3<f32>,
@location(1) tex_coords: vec2<f32>,
@location(2) normal: vec3<f32>,
@location(3) tangent: vec3<f32>,
@location(4) bitangent: vec3<f32>,
}
struct InstanceInput {
@location(5) model_matrix_0: vec4<f32>,
@location(6) model_matrix_1: vec4<f32>,
@location(7) model_matrix_2: vec4<f32>,
@location(8) model_matrix_3: vec4<f32>,
@location(9) normal_matrix_0: vec3<f32>,
@location(10) normal_matrix_1: vec3<f32>,
@location(11) normal_matrix_2: vec3<f32>,
}
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex_coords: vec2<f32>,
@location(1) tangent_position: vec3<f32>,
@location(2) tangent_light_position: vec3<f32>,
@location(3) tangent_view_position: vec3<f32>,
@location(4) world_position: vec3<f32>,
@location(5) light_local_position: vec4<f32>,
}
// Fragment shader
@group(1)@binding(1)
var t_light_depth: texture_depth_2d;
@group(1) @binding(2)
var s_light_depth: sampler_comparison;

View file

@ -1,43 +1,26 @@
// Vertex shader
#include globals.wgsl
struct Camera {
view: mat4x4<f32>,
proj: mat4x4<f32>,
position: vec4<f32>,
}
@group(0) @binding(0)
var<uniform> camera: Camera;
struct Light {
position: vec3<f32>,
color: vec3<f32>,
}
@group(1) @binding(0)
var<uniform> light: Light;
struct VertexInput {
struct LightVertexInput {
@location(0) position: vec3<f32>,
};
struct VertexOutput {
struct LightVertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) color: vec3<f32>,
};
@vertex
fn vs_main(
model: VertexInput,
) -> VertexOutput {
model: LightVertexInput,
) -> LightVertexOutput {
let scale = 10.0;
var out: VertexOutput;
var out: LightVertexOutput;
out.clip_position = camera.proj * camera.view * vec4<f32>(model.position * scale + light.position, 1.0);
out.color = light.color;
out.color = light.color.xyz;
return out;
}
// Fragment shader
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
fn fs_main(in: LightVertexOutput) -> @location(0) vec4<f32> {
return vec4<f32>(in.color, 1.0);
}

View file

@ -1,50 +1,9 @@
#include constants.wgsl
#include globals.wgsl
#include brdf.wgsl
// Vertex shader
struct CameraUniform {
view: mat4x4<f32>,
proj: mat4x4<f32>,
position: vec4<f32>,
}
@group(1) @binding(0)
var<uniform> camera: CameraUniform;
struct Light {
position: vec3<f32>,
color: vec4<f32>,
}
@group(2) @binding(0)
var<uniform> light: Light;
struct VertexInput {
@location(0) position: vec3<f32>,
@location(1) tex_coords: vec2<f32>,
@location(2) normal: vec3<f32>,
@location(3) tangent: vec3<f32>,
@location(4) bitangent: vec3<f32>,
}
struct InstanceInput {
@location(5) model_matrix_0: vec4<f32>,
@location(6) model_matrix_1: vec4<f32>,
@location(7) model_matrix_2: vec4<f32>,
@location(8) model_matrix_3: vec4<f32>,
@location(9) normal_matrix_0: vec3<f32>,
@location(10) normal_matrix_1: vec3<f32>,
@location(11) normal_matrix_2: vec3<f32>,
}
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex_coords: vec2<f32>,
@location(1) tangent_position: vec3<f32>,
@location(2) tangent_light_position: vec3<f32>,
@location(3) tangent_view_position: vec3<f32>,
@location(4) world_position: vec3<f32>,
}
@vertex
fn vs_main(
model: VertexInput,
@ -81,25 +40,26 @@ fn vs_main(
out.tangent_view_position = tangent_matrix * camera.position.xyz;
out.world_position = world_position.xyz;
out.light_local_position = camera.proj * camera.view * world_position;
return out;
}
// Fragment shader
@group(0) @binding(0)
@group(2) @binding(0)
var t_diffuse: texture_2d<f32>;
@group(0)@binding(1)
@group(2)@binding(1)
var s_diffuse: sampler;
@group(0)@binding(2)
@group(2)@binding(2)
var t_normal: texture_2d<f32>;
@group(0) @binding(3)
@group(2) @binding(3)
var s_normal: sampler;
@group(0)@binding(4)
@group(2)@binding(4)
var t_roughness_metalness: texture_2d<f32>;
@group(0) @binding(5)
@group(2) @binding(5)
var s_roughness_metalness: sampler;
@fragment
@ -109,7 +69,7 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let object_normal: vec4<f32> = textureSample(t_normal, s_normal, in.tex_coords);
let object_roughness_metalness: vec4<f32> = textureSample(
t_roughness_metalness, s_roughness_metalness, in.tex_coords);
// TODO: AO
//let light_depth = textureSampleCompareLevel(t_light_depth, s_light_depth, in.light_local_position.xy, 1.0);
let albedo = object_color.xyz;
// TODO: pass factors to shader