From 64a15f1b10ff5a4c0cb2ada1905baeb7125d16e4 Mon Sep 17 00:00:00 2001 From: Elabajaba Date: Fri, 12 Jan 2024 00:33:26 -0500 Subject: [PATCH] Fix ssao only sampling mip 0 (#11292) # Objective Fixes https://github.com/bevyengine/bevy/issues/11222 ## Solution SSAO's sample_mip_level was always giving negative values because it was in UV space (0..1) when it needed to be in pixel units (0..resolution). Fixing it so it properly samples lower mip levels when appropriate is a pretty large speedup (~3.2ms -> ~1ms at 4k, ~507us-> 256us at 1080p on a 6800xt), and I didn't notice any obvious visual quality differences. --------- Co-authored-by: Alice Cecile --- crates/bevy_pbr/src/ssao/gtao.wgsl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_pbr/src/ssao/gtao.wgsl b/crates/bevy_pbr/src/ssao/gtao.wgsl index a735fef2bc349..be5fea01ee230 100644 --- a/crates/bevy_pbr/src/ssao/gtao.wgsl +++ b/crates/bevy_pbr/src/ssao/gtao.wgsl @@ -139,7 +139,8 @@ fn gtao(@builtin(global_invocation_id) global_id: vec3) { s *= s; // https://github.com/GameTechDev/XeGTAO#sample-distribution let sample = s * sample_mul; - let sample_mip_level = clamp(log2(length(sample)) - 3.3, 0.0, 5.0); // https://github.com/GameTechDev/XeGTAO#memory-bandwidth-bottleneck + // * view.viewport.zw gets us from [0, 1] to [0, viewport_size], which is needed for this to get the correct mip levels + let sample_mip_level = clamp(log2(length(sample * view.viewport.zw)) - 3.3, 0.0, 5.0); // https://github.com/GameTechDev/XeGTAO#memory-bandwidth-bottleneck let sample_position_1 = load_and_reconstruct_view_space_position(uv + sample, sample_mip_level); let sample_position_2 = load_and_reconstruct_view_space_position(uv - sample, sample_mip_level);