-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Solari: Improved mirror denoising #22459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9448600
WIP
JMS55 03becb3
Scene work
JMS55 8200f9e
Fix
JMS55 bebbe5f
Fix
JMS55 c71d595
Merge commit '59104abe9e4f94413c1a77b11d117d3a4fc3b9a7' into solari6-psr
JMS55 c9ec8d3
Cleanup
JMS55 395e055
Misc refactor
JMS55 48bf412
Make gbuffer writable
JMS55 8d18855
WIP
JMS55 22eb306
Fixes
JMS55 b5b770a
Finish PSR
JMS55 72bba7d
Misc
JMS55 1d2407b
Merge branch 'main' into solari6-psr
JMS55 c02721d
Refactoring
JMS55 4737c3f
Merge branch 'solari6-psr' of github.com:JMS55/bevy into solari6-psr
JMS55 605558e
Do not terminate in world cache on first bounce no matter what (preve…
JMS55 232a4bb
Save change for another PR
JMS55 3fbf46f
Fix link
JMS55 9533746
Merge branch 'main' into solari6-psr
JMS55 9a41ea3
Fix no-DLSS mode (stupid naga_oil behavior)
JMS55 0020226
Merge branch 'main' into solari6-psr
JMS55 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,19 @@ | ||
| #define_import_path bevy_solari::specular_gi | ||
|
|
||
| #import bevy_pbr::pbr_functions::calculate_tbn_mikktspace | ||
| #import bevy_pbr::pbr_functions::{calculate_tbn_mikktspace, calculate_diffuse_color, calculate_F0} | ||
| #import bevy_pbr::prepass_bindings::PreviousViewUniforms | ||
| #import bevy_render::maths::{orthonormalize, PI} | ||
| #import bevy_render::view::View | ||
| #import bevy_solari::brdf::{evaluate_brdf, evaluate_specular_brdf} | ||
| #import bevy_solari::gbuffer_utils::gpixel_resolve | ||
| #import bevy_solari::gbuffer_utils::{gpixel_resolve, ResolvedGPixel} | ||
| #import bevy_solari::sampling::{sample_random_light, random_emissive_light_pdf, sample_ggx_vndf, ggx_vndf_pdf, power_heuristic} | ||
| #import bevy_solari::scene_bindings::{trace_ray, resolve_ray_hit_full, ResolvedRayHitFull, RAY_T_MIN, RAY_T_MAX} | ||
| #import bevy_solari::world_cache::{query_world_cache, get_cell_size, WORLD_CACHE_CELL_LIFETIME} | ||
| #import bevy_solari::realtime_bindings::{view_output, gi_reservoirs_a, gbuffer, depth_buffer, view, constants} | ||
| #ifdef DLSS_RR_GUIDE_BUFFERS | ||
| #import bevy_solari::realtime_bindings::{diffuse_albedo, specular_albedo, normal_roughness, specular_motion_vectors, previous_view} | ||
| #import bevy_solari::resolve_dlss_rr_textures::env_brdf_approx2 | ||
| #endif | ||
|
|
||
| const DIFFUSE_GI_REUSE_ROUGHNESS_THRESHOLD: f32 = 0.4; | ||
| const SPECULAR_GI_FOR_DI_ROUGHNESS_THRESHOLD: f32 = 0.0225; | ||
|
|
@@ -53,7 +58,7 @@ fn specular_gi(@builtin(global_invocation_id) global_id: vec3<u32>) { | |
| var a0 = dot(wo_unnormalized, wo_unnormalized) / (4.0 * PI * cos_theta); | ||
| a0 *= TERMINATE_IN_WORLD_CACHE_THRESHOLD; | ||
|
|
||
| radiance = trace_glossy_path(surface.world_position, wi, surface.material.roughness, pdf, a0, &rng) / pdf; | ||
| radiance = trace_glossy_path(global_id.xy, surface, wi, pdf, a0, &rng) / pdf; | ||
| } | ||
|
|
||
| let brdf = evaluate_specular_brdf(surface.world_normal, wo, wi, surface.material.base_color, surface.material.metallic, | ||
|
|
@@ -70,16 +75,22 @@ fn specular_gi(@builtin(global_invocation_id) global_id: vec3<u32>) { | |
| #endif | ||
| } | ||
|
|
||
| fn trace_glossy_path(initial_ray_origin: vec3<f32>, initial_wi: vec3<f32>, initial_roughness: f32, initial_p_bounce: f32, a0: f32, rng: ptr<function, u32>) -> vec3<f32> { | ||
| var ray_origin = initial_ray_origin; | ||
| fn trace_glossy_path(pixel_id: vec2<u32>, primary_surface: ResolvedGPixel, initial_wi: vec3<f32>, initial_p_bounce: f32, a0: f32, rng: ptr<function, u32>) -> vec3<f32> { | ||
| var radiance = vec3(0.0); | ||
| var throughput = vec3(1.0); | ||
|
|
||
| var ray_origin = primary_surface.world_position; | ||
| var wi = initial_wi; | ||
| var p_bounce = initial_p_bounce; | ||
| var surface_perfectly_specular = false; | ||
| var surface_perfect_mirror = false; | ||
| var path_spread = 0.0; | ||
|
|
||
| // Trace up to three bounces, getting the net throughput from them | ||
| var radiance = vec3(0.0); | ||
| var throughput = vec3(1.0); | ||
| #ifdef DLSS_RR_GUIDE_BUFFERS | ||
| var mirror_rotations = reflection_matrix(primary_surface.world_normal); | ||
| var psr_finished = false; | ||
| #endif | ||
|
|
||
| // Trace up to three bounces | ||
| for (var i = 0u; i < 3u; i += 1u) { | ||
| // Trace ray | ||
| let ray = trace_ray(ray_origin, wi, RAY_T_MIN, RAY_T_MAX, RAY_FLAG_NONE); | ||
|
|
@@ -95,21 +106,47 @@ fn trace_glossy_path(initial_ray_origin: vec3<f32>, initial_wi: vec3<f32>, initi | |
| let wo_tangent = vec3(dot(wo, T), dot(wo, B), dot(wo, N)); | ||
|
|
||
| // Add emissive contribution | ||
| let mis_weight = emissive_mis_weight(i, initial_roughness, p_bounce, ray_hit, surface_perfectly_specular); | ||
| let mis_weight = emissive_mis_weight(i, primary_surface.material.roughness, p_bounce, ray_hit, surface_perfect_mirror); | ||
| radiance += throughput * mis_weight * ray_hit.material.emissive; | ||
|
|
||
| // Should not perform NEE for mirror-like surfaces | ||
| surface_perfectly_specular = ray_hit.material.roughness <= 0.001 && ray_hit.material.metallic > 0.9999; | ||
| surface_perfect_mirror = ray_hit.material.roughness <= 0.001 && ray_hit.material.metallic > 0.9999; | ||
|
|
||
| // https://d1qx31qr3h6wln.cloudfront.net/publications/mueller21realtime.pdf#subsection.3.4, equation (3) | ||
| path_spread += sqrt((ray.t * ray.t) / (p_bounce * wo_tangent.z)); | ||
|
|
||
| // Primary surface replacement for perfect mirrors | ||
| // https://developer.nvidia.com/blog/rendering-perfect-reflections-and-refractions-in-path-traced-games/#DLSS_RR_GUIDE_BUFFERS | ||
| #ifdef DLSS_RR_GUIDE_BUFFERS | ||
| if !psr_finished && primary_surface.material.roughness <= 0.001 && primary_surface.material.metallic > 0.9999 { | ||
| if surface_perfect_mirror { | ||
| mirror_rotations = mirror_rotations * reflection_matrix(ray_hit.world_normal); | ||
| } else { | ||
| psr_finished = true; | ||
|
|
||
| // Simplification: Apply all rotations in the chain around the first mirror, rather than applying each rotation around its respective mirror | ||
| let virtual_position = (mirror_rotations * (ray_hit.world_position - primary_surface.world_position)) + primary_surface.world_position; | ||
| let virtual_previous_frame_position = (mirror_rotations * (ray_hit.previous_frame_world_position - primary_surface.world_position)) + primary_surface.world_position; | ||
| let specular_motion_vector = calculate_motion_vector(virtual_position, virtual_previous_frame_position); | ||
|
|
||
| let F0 = calculate_F0(ray_hit.material.base_color, ray_hit.material.metallic, ray_hit.material.reflectance); | ||
| let wo = normalize(view.world_position - virtual_position); | ||
|
||
| let virtual_normal = normalize(mirror_rotations * ray_hit.world_normal); | ||
|
|
||
| textureStore(specular_motion_vectors, pixel_id, vec4(specular_motion_vector, vec2(0.0))); | ||
| textureStore(diffuse_albedo, pixel_id, vec4(calculate_diffuse_color(ray_hit.material.base_color, ray_hit.material.metallic, 0.0, 0.0), 0.0)); | ||
| textureStore(specular_albedo, pixel_id, vec4(env_brdf_approx2(F0, ray_hit.material.roughness, ray_hit.world_normal, wo), 0.0)); | ||
| textureStore(normal_roughness, pixel_id, vec4(virtual_normal, ray_hit.material.perceptual_roughness)); | ||
| } | ||
| } | ||
| #endif | ||
|
|
||
| if path_spread * path_spread > a0 * get_cell_size(ray_hit.world_position, view.world_position) { | ||
| // Path spread is wide enough, terminate path in the world cache | ||
| let diffuse_brdf = ray_hit.material.base_color / PI; | ||
| radiance += throughput * diffuse_brdf * query_world_cache(ray_hit.world_position, ray_hit.geometric_world_normal, view.world_position, WORLD_CACHE_CELL_LIFETIME, rng); | ||
| break; | ||
| } else if !surface_perfectly_specular { | ||
| } else if !surface_perfect_mirror { | ||
| // Sample direct lighting (NEE) | ||
| let direct_lighting = sample_random_light(ray_hit.world_position, ray_hit.world_normal, rng); | ||
| let direct_lighting_brdf = evaluate_brdf(ray_hit.world_normal, wo, direct_lighting.wi, ray_hit.material); | ||
|
|
@@ -132,9 +169,9 @@ fn trace_glossy_path(initial_ray_origin: vec3<f32>, initial_wi: vec3<f32>, initi | |
| return radiance; | ||
| } | ||
|
|
||
| fn emissive_mis_weight(i: u32, initial_roughness: f32, p_bounce: f32, ray_hit: ResolvedRayHitFull, previous_surface_perfectly_specular: bool) -> f32 { | ||
| fn emissive_mis_weight(i: u32, initial_roughness: f32, p_bounce: f32, ray_hit: ResolvedRayHitFull, previous_surface_perfect_mirror: bool) -> f32 { | ||
| if i != 0u { | ||
| if previous_surface_perfectly_specular { return 1.0; } | ||
| if previous_surface_perfect_mirror { return 1.0; } | ||
|
|
||
| let p_light = random_emissive_light_pdf(ray_hit); | ||
| return power_heuristic(p_bounce, p_light); | ||
|
|
@@ -163,12 +200,30 @@ fn nee_mis_weight(inverse_p_light: f32, brdf_rays_can_hit: bool, wo_tangent: vec | |
| return power_heuristic(p_light, p_bounce); | ||
| } | ||
|
|
||
| // Don't adjust the size of this struct without also adjusting GI_RESERVOIR_STRUCT_SIZE. | ||
| struct Reservoir { | ||
| sample_point_world_position: vec3<f32>, | ||
| weight_sum: f32, | ||
| radiance: vec3<f32>, | ||
| confidence_weight: f32, | ||
| sample_point_world_normal: vec3<f32>, | ||
| unbiased_contribution_weight: f32, | ||
| #ifdef DLSS_RR_GUIDE_BUFFERS | ||
| // https://en.wikipedia.org/wiki/Householder_transformation | ||
| fn reflection_matrix(plane_normal: vec3f) -> mat3x3<f32> { | ||
| // N times Nᵀ. | ||
| let n_nt = mat3x3<f32>( | ||
| plane_normal * plane_normal.x, | ||
| plane_normal * plane_normal.y, | ||
| plane_normal * plane_normal.z, | ||
| ); | ||
| let identity_matrix = mat3x3<f32>(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0); | ||
| return identity_matrix - n_nt * 2.0; | ||
| } | ||
|
|
||
| fn calculate_motion_vector(world_position: vec3<f32>, previous_world_position: vec3<f32>) -> vec2<f32> { | ||
| let clip_position_t = view.unjittered_clip_from_world * vec4(world_position, 1.0); | ||
| let clip_position = clip_position_t.xy / clip_position_t.w; | ||
| let previous_clip_position_t = previous_view.clip_from_world * vec4(previous_world_position, 1.0); | ||
| let previous_clip_position = previous_clip_position_t.xy / previous_clip_position_t.w; | ||
| // These motion vectors are used as offsets to UV positions and are stored | ||
| // in the range -1,1 to allow offsetting from the one corner to the | ||
| // diagonally-opposite corner in UV coordinates, in either direction. | ||
| // A difference between diagonally-opposite corners of clip space is in the | ||
| // range -2,2, so this needs to be scaled by 0.5. And the V direction goes | ||
| // down where clip space y goes up, so y needs to be flipped. | ||
| return (clip_position - previous_clip_position) * vec2(0.5, -0.5); | ||
| } | ||
| #endif | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.