-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
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
FXAA: Improve quality. #29524
FXAA: Improve quality. #29524
Conversation
This needs a very close review since the original developer of the latest version is very experienced in this area and we had no complains about the anti-aliasing quality so far. It might take a while unit I can provide feedback for this PR. |
@Mugen87 Sure thing. I'm open to being wrong. I just noticed a change in quality immediately. |
Sorry, last piece of evidence, just to cover my base. Here's a scene from the infamously aliased game Kentucky Route Zero, with the two shaders applied. You can check out the results yourself. Original image Current shader completely fails to recognize these areas. Keep in mind that that's supposed to be the primary function of this shader. While areas it does recognize exhibit dithering artifacts. Same here, current completely fails in the background, while overblurring the car windows. When the contrast threshold is brought down, a lot more edges are rocognized, but the quality is just not the same as the previous algorithm. I rest my case. I'll show myself out, I'm too invested in this. 😄 |
The proposed version sure looks much better! |
I also wouldn't mind reverting to the 2018 version. I don't think alpha support is that important? |
More context from the original developer: @mrdoob I think we could easily introduce alpha support to the 2018 version. |
On top of being very long, 2018 also contains a lot of unused code. All of these were motivations to choose an implementation which has the same fidelity, but is clean and maintainable, like the one I'm proposing. |
The community frequently asked for alpha support in context of FX. Since MSAA as well as our manual SSAA support alpha, I think's good to support it with FXAA as well. |
I'm familiar with the current FXAA shader but I currently don't have the time to study the new code. However, I'm okay with supporting the proposed solution now if @swift502 is willing to support the new FXAA implementation. |
@Mugen87 As much as I'm able to. 👍 The code is from a tutorial where every line is explained. So I should be able to fix potential shader issues. |
I've quickly checked the example at https://rawcdn.githack.com/swift502/three.js/fxaa/examples/index.html?q=fxaa#webgpu_postprocessing_fxaa and they look good so far. Just one hint: Be aware that
|
Check how these uv offsets in TSL differ to the GLSL/WebGLRenderer version: three.js/examples/jsm/tsl/display/FXAANode.js Lines 94 to 102 in 668c88e
three.js/examples/jsm/shaders/FXAAShader.js Lines 119 to 126 in 668c88e
|
I'll get onto fixing that. Could you help me with how to pass objects (glsl structs) as parameters, and as return values? three.js/examples/jsm/tsl/display/FXAANode.js Lines 259 to 262 in 3146aa4
three.js/examples/jsm/tsl/display/FXAANode.js Lines 124 to 126 in 3146aa4
|
I'm getting like 99.5% close to WebGL, like in the comparison I posted earlier. #29524 (comment) Is that normal? The result also looks the same whether I flip the vertical coordinates or not. I double checked by flipping the image (to check if the algorithm behaves differently upside down), and the results are always the same. So it seems to be correct? |
PerformanceI did some benchmarks where I put a 1000x for loop in the shader's main function call. No idea how accurate that is, but I got some data. Initially, this new shader was significantly slower (by 25%), so I've lowered the samples from 10 to 6, and now the shader is 10% faster than the 2022 implementation. (according to my test) For reference 2018 used 4 samples, and 2022 is using 5. AlphaI double checked that alpha works, here it is rendering over html text Other
I'm personally happy with the code now. I'll be happy to do further changes based on feedback. |
We need a 100% match since there should be no difference. What I did when porting FXAA to TSL and encountered a similar issue, I literally compared line by line the TSL and GLSL version by assigning the current result values of a line to frag color. In that way I eventually found the difference which I believe was a missing one-minus computation in TSL. Sorry but there should be no pixel differences like in #29524 (comment). You can easily see them at the bottom of the triangle. |
@Mugen87 Have you ever encountered problems with number precision? Everything is the same until it hits a greater than comparison. e.isHorizontal = horizontal >= vertical; vs const isHorizontal = horizontal.greaterThanEqual( vertical ); where suddenly some pixels flip on/off differently, when horizontal and vertical have similar values. |
/cc @sunag |
@Mugen87 I unflipped the shader because now it produces 100% match even upside down. And this way it doesn't need the uv flipping hacks, and is a 1:1 port with glsl. Is that fine? It means "north" refers to the down direction now. But basically that's a naming issue. |
TBH, I find that confusing. The logic/naming should be correct even if the implementation differs from GLSL. Keep in mind that at some point |
Alright I'll fix it. |
Corrected. I got nothing else to add. 👌 These new shaders should be marginally faster, should be much more faithful to the FXAA white paper specification, and provide results much closer to Nvidia's reference implementations of FXAA. And thanks a lot for the help @sunag. 🙏 |
I think we should just have one version. |
Issue
I believe the current FXAA implementation is broken, and this PR attempts to fix it.
In 2022, a new version of FXAA was merged, which exhibits inferior quality compared to the previous version. #23768
It's more blurry.
And has worse edge recognition.
Solution
The MIT-0 FXAA implementation by Jasper Flick.
https://catlikecoding.com/unity/tutorials/advanced-rendering/fxaa/
https://catlikecoding.com/unity/tutorials/license/
2022 downgrade visible on the official example.
Proposed shader matches 2018 in visual fidelity.
Performance considerations
Higher quality means more computation. We're jumping from 5 samples to 10 and have lower contrast thresholds. If this is a concern, let me know. I haven't done any tests so can't confirm how much more expensive this implementation is.
We could even edit the shader to match the 2018 performance and quality, if desirable.
Edit: See #29524 (comment) for performance testing.