100: Fix shadow sampling r=kvark a=kvark



Co-authored-by: Dzmitry Malyshau <kvarkus@gmail.com>
This commit is contained in:
bors[bot]
2019-10-24 18:56:08 +00:00
committed by GitHub
2 changed files with 18 additions and 12 deletions

View File

@@ -1,4 +1,3 @@
use log::info;
use winit::event::WindowEvent;
#[cfg_attr(rustfmt, rustfmt_skip)]
@@ -50,7 +49,7 @@ pub fn run<E: Example>(title: &str) {
env_logger::init();
let event_loop = EventLoop::new();
info!("Initializing the window...");
log::info!("Initializing the window...");
#[cfg(not(feature = "gl"))]
let (_window, hidpi_factor, size, surface) = {
@@ -105,13 +104,13 @@ pub fn run<E: Example>(title: &str) {
};
let mut swap_chain = device.create_swap_chain(&surface, &sc_desc);
info!("Initializing the example...");
log::info!("Initializing the example...");
let (mut example, init_command_buf) = E::init(&sc_desc, &device);
if let Some(command_buf) = init_command_buf {
queue.submit(&[command_buf]);
}
info!("Entering render loop...");
log::info!("Entering render loop...");
event_loop.run(move |event, _, control_flow| {
*control_flow = if cfg!(feature = "metal-auto-capture") {
ControlFlow::Exit
@@ -124,7 +123,7 @@ pub fn run<E: Example>(title: &str) {
..
} => {
let physical = size.to_physical(hidpi_factor);
info!("Resizing to {:?}", physical);
log::info!("Resizing to {:?}", physical);
sc_desc.width = physical.width.round() as u32;
sc_desc.height = physical.height.round() as u32;
swap_chain = device.create_swap_chain(&surface, &sc_desc);

View File

@@ -28,6 +28,19 @@ layout(set = 1, binding = 0) uniform Entity {
vec4 u_Color;
};
float fetch_shadow(int light_id, vec4 homogeneous_coords) {
if (homogeneous_coords.w <= 0.0) {
return 1.0;
}
// compute texture coordinates for shadow lookup
vec4 light_local = vec4(
(homogeneous_coords.xy/homogeneous_coords.w + 1.0) / 2.0,
light_id,
homogeneous_coords.z / homogeneous_coords.w
);
// do the lookup, using HW PCF and comparison
return texture(sampler2DArrayShadow(t_Shadow, s_Shadow), light_local);
}
void main() {
vec3 normal = normalize(v_Normal);
@@ -37,13 +50,7 @@ void main() {
for (int i=0; i<int(u_NumLights.x) && i<MAX_LIGHTS; ++i) {
Light light = u_Lights[i];
// project into the light space
vec4 light_local = light.proj * v_Position;
// compute texture coordinates for shadow lookup
light_local.xy = (light_local.xy/light_local.w + 1.0) / 2.0;
light_local.w = light_local.z / light_local.w;
light_local.z = i;
// do the lookup, using HW PCF and comparison
float shadow = texture(sampler2DArrayShadow(t_Shadow, s_Shadow), light_local);
float shadow = fetch_shadow(i, light.proj * v_Position);
// compute Lambertian diffuse term
vec3 light_dir = normalize(light.pos.xyz - v_Position.xyz);
float diffuse = max(0.0, dot(normal, light_dir));