From 7386a05a0dc50a7580378725bb2d482d0853a701 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Fri, 22 Feb 2019 12:59:05 -0500 Subject: [PATCH] Shadow shaders --- gfx-examples/Cargo.toml | 5 +++ gfx-examples/data/shadow-forward.frag | 51 +++++++++++++++++++++++++-- gfx-examples/data/shadow-forward.vert | 13 +++---- gfx-examples/src/cube.rs | 2 +- gfx-examples/src/framework.rs | 1 + gfx-examples/src/shadow.rs | 12 +++---- 6 files changed, 66 insertions(+), 18 deletions(-) diff --git a/gfx-examples/Cargo.toml b/gfx-examples/Cargo.toml index ce4959801a..d8b3698e54 100644 --- a/gfx-examples/Cargo.toml +++ b/gfx-examples/Cargo.toml @@ -12,8 +12,13 @@ publish = false name = "cube" path = "src/cube.rs" +[[bin]] +name = "shadow" +path = "src/shadow.rs" + [features] default = [] +metal-auto-capture = ["wgpu/metal-auto-capture"] metal = ["wgpu/metal"] dx11 = ["wgpu/dx11"] dx12 = ["wgpu/dx12"] diff --git a/gfx-examples/data/shadow-forward.frag b/gfx-examples/data/shadow-forward.frag index 22c79b3e9c..0928adce86 100644 --- a/gfx-examples/data/shadow-forward.frag +++ b/gfx-examples/data/shadow-forward.frag @@ -1,10 +1,55 @@ #version 450 +const int MAX_LIGHTS = 10; + +layout(location = 0) in vec3 v_Normal; +layout(location = 1) in vec4 v_Position; + layout(location = 0) out vec4 o_Target; -layout(set = 0, binding = 2) uniform texture2D u_ShadowTexture; -layout(set = 0, binding = 3) uniform sampler u_ShadowSampler; +struct Light { + mat4 proj; + vec4 pos; + vec4 color; +}; + +layout(set = 0, binding = 0) uniform Globals { + mat4 u_ViewProj; + uvec4 u_NumLights; +}; +layout(set = 0, binding = 1) uniform Lights { + Light u_Lights[MAX_LIGHTS]; +}; +layout(set = 0, binding = 2) uniform texture2DArray t_Shadow; +layout(set = 0, binding = 3) uniform samplerShadow s_Shadow; + +layout(set = 1, binding = 0) uniform Entity { + mat4 u_World; + vec4 u_Color; +}; + void main() { - o_Target = vec4(1.0); + vec3 normal = normalize(v_Normal); + vec3 ambient = vec3(0.05, 0.05, 0.05); + // accumulate color + vec3 color = ambient; + for (int i=0; i(title: &str) { let frame = swap_chain.get_next_texture(); example.render(&frame, &mut device); + running &= !cfg!(feature = "metal-auto-capture"); } } diff --git a/gfx-examples/src/shadow.rs b/gfx-examples/src/shadow.rs index f0dd5ce404..5200f6da93 100644 --- a/gfx-examples/src/shadow.rs +++ b/gfx-examples/src/shadow.rs @@ -172,11 +172,11 @@ impl Example { }; fn generate_matrix(aspect_ratio: f32) -> cgmath::Matrix4 { - let mx_projection = cgmath::perspective(cgmath::Deg(45f32), aspect_ratio, 1.0, 10.0); + let mx_projection = cgmath::perspective(cgmath::Deg(45f32), aspect_ratio, 1.0, 20.0); let mx_view = cgmath::Matrix4::look_at( - cgmath::Point3::new(1.5f32, -5.0, 3.0), + cgmath::Point3::new(3.0f32, -10.0, 6.0), cgmath::Point3::new(0f32, 0.0, 0.0), - cgmath::Vector3::unit_z(), + -cgmath::Vector3::unit_z(), ); mx_projection * mx_view } @@ -219,7 +219,7 @@ impl framework::Example for Example { bindings: &[ wgpu::BindGroupLayoutBinding { binding: 0, - visibility: wgpu::ShaderStageFlags::VERTEX, + visibility: wgpu::ShaderStageFlags::VERTEX | wgpu::ShaderStageFlags::FRAGMENT, ty: wgpu::BindingType::UniformBuffer, }, ], @@ -447,8 +447,8 @@ impl framework::Example for Example { rasterization_state: wgpu::RasterizationStateDescriptor { front_face: wgpu::FrontFace::Cw, cull_mode: wgpu::CullMode::Back, - depth_bias: 0, - depth_bias_slope_scale: 0.0, + depth_bias: 2, + depth_bias_slope_scale: 1.0, depth_bias_clamp: wgpu::MAX_DEPTH_BIAS_CLAMP, }, primitive_topology: wgpu::PrimitiveTopology::TriangleList,