Skip to content
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

Improve TAA #8974

Closed
wants to merge 32 commits into from
Closed

Improve TAA #8974

wants to merge 32 commits into from

Conversation

JMS55
Copy link
Contributor

@JMS55 JMS55 commented Jun 27, 2023

Objective

  • Improve TAA quality
  • Prevent smearing from motion vectors that point to pixels no longer on-screen
  • Use textureSampleLevel() which is probably faster than textureSample() (Thanks to @DGriffin91)
  • Use a uniform for reset, instead of a shaderdef (sadly WebGPU doesn't support push constants :/)
  • Fix Using TAA causing panic when entity is despawned #9721
  • Adopt parts of the improved AA example from @DGriffin91

Followup

Changelog

  • Improve TAA quality
  • Fixed TAA smearing when reprojecting pixels that are no longer on screen
  • Fixed a bug where despawning an entity in PreUpdate while using MotionVectorPrepass would cause a panic
  • Made TemporalAntiAliasNode public

@JMS55 JMS55 added C-Enhancement A new feature A-Rendering Drawing game state to the screen labels Jun 27, 2023
@JMS55 JMS55 added this to the 0.11 milestone Jun 27, 2023
Copy link
Member

@cart cart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@DGriffin91
Copy link
Contributor

@JMS55 Just tested this PR with the anti_aliasing example from my PR (#8847) and it appears that it does not fix the smearing.

image

@JMS55
Copy link
Contributor Author

JMS55 commented Jul 7, 2023

@DGriffin91 I can only get the smearing when running your example with the noise texture specifically. Using any other texture/material (I tested a few) works fine.

I think the reason it smears on your example is due to neighborhood color clamping. Reprojection succeeds, but it's not 100% perfectly accurate, and with such a high frequency, multi-colored texture, the color clamping (which is based on neighboring pixels) performs very poorly. I could be wrong though.

I'm guessing that the reason it works in #8847 is that in my previous TAA code (what's currently in main, and what that PR is based off of), I reset the accumulation/blend factor when any motion (non-zero motion vector) is detected. Which in hindsight, wasn't a great idea. However, that did mean you didn't get any ghosting even when neighborhood clamping failed. This PR tries to keep the history even under motion.

A possible solution would be to reset/reduce accumulation when color clips by a lot or is near clipping, which iirc is what Brian Karis/Unreal did. That can be very finicky and introduce more artifacts if we're not careful. Doesn't seem like the kind of thing we should try right before 0.11.

@cart
Copy link
Member

cart commented Jul 8, 2023

Removing this from 0.11 as theres too much controversy here.

@cart cart modified the milestones: 0.11, 0.12 Jul 8, 2023
@github-actions
Copy link
Contributor

Example alien_cake_addict failed to run, please try running it locally and check the result.

@github-actions
Copy link
Contributor

Example alien_cake_addict failed to run, please try running it locally and check the result.

@github-actions
Copy link
Contributor

Example alien_cake_addict failed to run, please try running it locally and check the result.

@JMS55 JMS55 added the C-Bug An unexpected or incorrect behavior label Aug 15, 2023
@JMS55 JMS55 requested a review from superdump August 15, 2023 19:59
@github-actions
Copy link
Contributor

Example alien_cake_addict failed to run, please try running it locally and check the result.

1 similar comment
@github-actions
Copy link
Contributor

Example alien_cake_addict failed to run, please try running it locally and check the result.

@JMS55 JMS55 marked this pull request as draft August 15, 2023 22:27
@github-actions
Copy link
Contributor

Example alien_cake_addict failed to run, please try running it locally and check the result.

@github-actions
Copy link
Contributor

Example alien_cake_addict failed to run, please try running it locally and check the result.

@JMS55
Copy link
Contributor Author

JMS55 commented Oct 12, 2023

image
Shadows are clipping as you move the camera :/. I hate messing with shadows, can someone else try and fix this? @DGriffin91 :)

@Elabajaba
Copy link
Contributor

Elabajaba commented Oct 15, 2023

Shadows are clipping as you move the camera :/. I hate messing with shadows, can someone else try and fix this?

You just need to extend the maximum_distance of the cascade_shadow_config to be eg. 5m instead of 3m.

@JMS55
Copy link
Contributor Author

JMS55 commented Oct 15, 2023

Thanks, that fixed it!

@DGriffin91
Copy link
Contributor

DGriffin91 commented Oct 17, 2023

I've mentioned this in the discord but want to comment here so it doesn't get lost. I think the state of the TAA in the PR currently has much more smearing/blurring under motion than the current implementation in bevy main. And still does not address the edge smearing issue that #8847 fixes.

Main as of 5733d24
main

#8847
8847

#8974 (this PR)
8974

Main | #8847 | #8974
compare_large

@Elabajaba
Copy link
Contributor

I don't think this can be merged until the NaN issues are solved.

image

Potentially related is that the history texture has negative RGB values for some things for some reason.

@superdump
Copy link
Contributor

Nor because of the ghosting/smearing that @DGriffin91 showed above.

@DGriffin91
Copy link
Contributor

In addition to ghosting/smearing things are just blurry in general:

#8847 | #8974 (this PR)
combined2

With some areas highlighted in case it's not initially obvious:
combined3

@DGriffin91
Copy link
Contributor

I just realized that I had done these tests with vsync at 165hz and high framerate minimizes the issue. Here's setting my monitor for vsync at 60hz:

#8847
8847_60fps

#8974 (this PR)
8974_60fps

@alice-i-cecile alice-i-cecile added the X-Controversial There is active debate or serious implications around merging this PR label Oct 17, 2023
@superdump
Copy link
Contributor

@DGriffin91 what does that last example at 60Hz look like on main?

OK, so, we all want to make progress on TAA. None of what I am writing is intended to offend anyone, and all efforts and contributions are sincerely appreciated.

It looks to me like this PR causes significant regressions in visual quality in its current form. As this PR currently combines multiple changes into one PR, it makes it difficult to understand what part of the changes introduces what improvements and what problems.

I think we need better in-repo test cases for common problems, such as a moving / rotating camera, perhaps if heuristics for when disocclusions are detected are based on depth differences then we need to test disocclusions at different depths, etc. Then this PR could be split into multiple smaller steps to be able to verify that each in isolation is as good or better. That will also make reviewing much easier which increases the likelihood of faster merges. When situations with PRs become unclear, it is difficult to prioritise reviewing them over other PRs that have a clear state.

I looked through #8847 and aside from needing to check the FXAA code move line-by-line, it looks like it adds camera movement and a noise texture to stress test TAA functionality. That change to example code should probably be a separate PR so we can first see how it looks on main and then how it looks with the introduced changes from any of the PRs modifying TAA.

@JMS55 JMS55 modified the milestones: 0.12, 0.13 Oct 17, 2023
@DGriffin91
Copy link
Contributor

DGriffin91 commented Oct 19, 2023

@superdump I've created a PR with just the example from #8847: #10183

I've also updated it with a few things:

  1. The noisiness of the noise texture was a bit excessive without mipmaps so it now generates those.
  2. I used deterministic PRNG for the white noise so it's consistent (ported from some gpu code)
  3. I made the UV scale follow the window height so the density of the noise on screen is consistent. (without this a small window would render solid grey, for example)
  4. I added a screenshot mode that animates the camera based on the current frame instead of time delta and takes a screenshot at a specific frame. This allows the TAA response and camera motion to be consistent regardless of the frame rate. I tested this at 60hz and 165hz and the resulting image looked the same.
  5. I added a second helmet to give a bit more context for seeing detail.
    Here's the result (preferably view at full 1:1 resolution):

Bevy main:
main

This pr:
8974

I've abandon #8847 in favor of using a 3rd party plugin: https://github.com/DGriffin91/bevy_mod_taa
taa_screenshot-0

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
@JMS55 JMS55 mentioned this pull request Oct 20, 2023
github-merge-queue bot pushed a commit that referenced this pull request Oct 27, 2023
Extracted the easy stuff from #8974 .

# Problem
1. Commands from `update_previous_view_projections` would crash when
matching entities were despawned.
2. `TaaPipelineId` and `draw_3d_graph` module were not public.
3. When the motion vectors pointed to pixels that are now off screen, a
smearing artifact could occur.

# Solution
1. Use `try_insert` command instead.
2. Make them public, renaming to `TemporalAntiAliasPipelineId`.
3. Check for this case, and ignore history for pixels that are
off-screen.
ameknite pushed a commit to ameknite/bevy that referenced this pull request Nov 6, 2023
Extracted the easy stuff from bevyengine#8974 .

# Problem
1. Commands from `update_previous_view_projections` would crash when
matching entities were despawned.
2. `TaaPipelineId` and `draw_3d_graph` module were not public.
3. When the motion vectors pointed to pixels that are now off screen, a
smearing artifact could occur.

# Solution
1. Use `try_insert` command instead.
2. Make them public, renaming to `TemporalAntiAliasPipelineId`.
3. Check for this case, and ignore history for pixels that are
off-screen.
@JMS55 JMS55 marked this pull request as draft December 31, 2023 06:46
rdrpenguin04 pushed a commit to rdrpenguin04/bevy that referenced this pull request Jan 9, 2024
Extracted the easy stuff from bevyengine#8974 .

# Problem
1. Commands from `update_previous_view_projections` would crash when
matching entities were despawned.
2. `TaaPipelineId` and `draw_3d_graph` module were not public.
3. When the motion vectors pointed to pixels that are now off screen, a
smearing artifact could occur.

# Solution
1. Use `try_insert` command instead.
2. Make them public, renaming to `TemporalAntiAliasPipelineId`.
3. Check for this case, and ignore history for pixels that are
off-screen.
@JMS55 JMS55 removed this from the 0.13 milestone Jan 26, 2024
@JMS55 JMS55 closed this Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior C-Enhancement A new feature X-Controversial There is active debate or serious implications around merging this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using TAA causing panic when entity is despawned
8 participants