Skip to content

Commit

Permalink
Fix temporal jitter bug (#9462)
Browse files Browse the repository at this point in the history
* Fixed jitter being applied in the wrong coordinate space, leading to
aliasing.
* Fixed incorrectly using the cached view_proj instead of account for
temporal jitter.
* Added a diagram to ensure the coordinate space is clear.

Before:

![image](https://github.com/bevyengine/bevy/assets/47158642/55b4bed4-4fb0-4fb2-a271-cc10a987e4d7)

After:

![image](https://github.com/bevyengine/bevy/assets/47158642/cbde4553-4e35-44d9-8ccf-f3a06e64a31f)
  • Loading branch information
JMS55 authored Aug 17, 2023
1 parent 9473726 commit 5fac1fe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
5 changes: 3 additions & 2 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use bevy_ecs::{
system::{Commands, Query, Res, ResMut, Resource},
};
use bevy_log::warn;
use bevy_math::{Mat4, Ray, Rect, URect, UVec2, UVec4, Vec2, Vec3};
use bevy_math::{vec2, Mat4, Ray, Rect, URect, UVec2, UVec4, Vec2, Vec3};
use bevy_reflect::prelude::*;
use bevy_transform::components::GlobalTransform;
use bevy_utils::{HashMap, HashSet};
Expand Down Expand Up @@ -806,7 +806,8 @@ impl TemporalJitter {
return;
}

let jitter = self.offset / view_size;
// https://github.com/GPUOpen-LibrariesAndSDKs/FidelityFX-SDK/blob/d7531ae47d8b36a5d4025663e731a47a38be882f/docs/techniques/media/super-resolution-temporal/jitter-space.svg
let jitter = (self.offset * vec2(2.0, -2.0)) / view_size;

projection.z_axis.x += jitter.x;
projection.z_axis.y += jitter.y;
Expand Down
12 changes: 9 additions & 3 deletions crates/bevy_render/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,17 @@ pub fn prepare_view_uniforms(
let view = camera.transform.compute_matrix();
let inverse_view = view.inverse();

let view_proj = if temporal_jitter.is_some() {
projection * inverse_view
} else {
camera
.view_projection
.unwrap_or_else(|| projection * inverse_view)
};

let view_uniforms = ViewUniformOffset {
offset: view_uniforms.uniforms.push(ViewUniform {
view_proj: camera
.view_projection
.unwrap_or_else(|| projection * inverse_view),
view_proj,
unjittered_view_proj: unjittered_projection * inverse_view,
inverse_view_proj: view * inverse_projection,
view,
Expand Down

0 comments on commit 5fac1fe

Please sign in to comment.