From 1e4e4f7043e4247e299b4affae740885df23d728 Mon Sep 17 00:00:00 2001 From: Marco Buono Date: Tue, 26 Sep 2023 03:01:18 -0300 Subject: [PATCH] Add a fast codepath for fetching background with zero roughness --- crates/bevy_pbr/src/render/pbr_lighting.wgsl | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/bevy_pbr/src/render/pbr_lighting.wgsl b/crates/bevy_pbr/src/render/pbr_lighting.wgsl index 798b33baf2..424b8854fc 100644 --- a/crates/bevy_pbr/src/render/pbr_lighting.wgsl +++ b/crates/bevy_pbr/src/render/pbr_lighting.wgsl @@ -308,7 +308,13 @@ fn specular_transmissive_light(world_position: vec4, frag_coord: vec3, let offset_position = (clip_exit_position.xy / clip_exit_position.w) * vec2(0.5, -0.5) + 0.5; // Fetch background color - let background_color = fetch_transmissive_background(offset_position, frag_coord, view_z, perceptual_roughness); + var background_color: vec4; + if perceptual_roughness == 0.0 { + // If the material has zero roughness, we can use a faster approach without the blur + background_color = fetch_transmissive_background_non_rough(offset_position); + } else { + background_color = fetch_transmissive_background(offset_position, frag_coord, view_z, perceptual_roughness); + } // Calculate final color by applying specular transmissive color to a mix of background color and transmitted specular environment light return specular_transmissive_color * mix(transmitted_environment_light_specular, background_color.rgb, background_color.a); @@ -326,6 +332,14 @@ fn interleaved_gradient_noise(pixel_coordinates: vec2) -> f32 { return fract(52.9829189 * fract(0.06711056 * xy.x + 0.00583715 * xy.y)); } +fn fetch_transmissive_background_non_rough(offset_position: vec2) -> vec4 { + return textureSample( + view_bindings::view_transmission_texture, + view_bindings::view_transmission_sampler, + offset_position, + ); +} + fn fetch_transmissive_background(offset_position: vec2, frag_coord: vec3, view_z: f32, perceptual_roughness: f32) -> vec4 { // Calculate view aspect ratio, used to scale offset so that it's proportionate let aspect = view_bindings::view.viewport.z / view_bindings::view.viewport.w;