Skip to content

Add SMAA 1x to screenspace AA options#102330

Merged
Repiteo merged 1 commit into
godotengine:masterfrom
RGDTAB:add_partial_smaa
May 26, 2025
Merged

Add SMAA 1x to screenspace AA options#102330
Repiteo merged 1 commit into
godotengine:masterfrom
RGDTAB:add_partial_smaa

Conversation

@RGDTAB
Copy link
Copy Markdown
Contributor

@RGDTAB RGDTAB commented Feb 2, 2025

The quality settings used for edge detection and blending weight calculation are the defaults for SweetFX's ReShade implementation. I didn't add any way to change these settings as the very meager performance improvement between the highest and lowest settings didn't justify the visual difference between them.

@RGDTAB RGDTAB requested review from a team as code owners February 2, 2025 17:11
@RGDTAB RGDTAB marked this pull request as draft February 2, 2025 17:13
@Chaosus Chaosus added this to the 4.x milestone Feb 2, 2025
@RGDTAB RGDTAB marked this pull request as ready for review February 2, 2025 20:02
@tetrapod00

This comment was marked as resolved.

@RGDTAB
Copy link
Copy Markdown
Contributor Author

RGDTAB commented Feb 2, 2025

There is a proposal for SMAA already: godotengine/godot-proposals#2779

Right. I forgot to mention that in my first message. This is aimed at implementing that proposal.

@fire
Copy link
Copy Markdown
Member

fire commented Feb 4, 2025

Are you able to run pre-commit -a and godot --doctool so tests can proceed? I'll try to get some instructions written in the future.

@RGDTAB
Copy link
Copy Markdown
Contributor Author

RGDTAB commented Feb 4, 2025

Sure thing, but it looks like the only thing pre-commit wants to change is the last name of one of the authors of SMAA. Are you sure that's OK?

@tetrapod00
Copy link
Copy Markdown
Contributor

It's definitely not right to misspell an authors name so the CI passes.

Looks like COPYRIGHT.txt is already skipped by codespell:

"COPYRIGHT.txt",

But since the author name is in multiple files, the right fix might be to add "Masia" to the ignored word list:
ignore-words-list = [

@RGDTAB
Copy link
Copy Markdown
Contributor Author

RGDTAB commented Feb 4, 2025

Thanks for the clarification. It should be fixed now.

Copy link
Copy Markdown
Member

@Calinou Calinou left a comment

Choose a reason for hiding this comment

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

Tested locally on macOS with Metal, it works as expected.

Testing project: test_pr_89582.zip
This uses the approach described in #89582 (comment) for testing.

Click to view at full size.

No AA FXAA master FXAA #89582 SMAA
No AA FXAA master FXAA PR SMAA

SMAA does a better job at preserving detail compared to FXAA, while having slightly better edge coverage.

SMAA is not affected by #83089 either:

No AA FXAA master SMAA
image image image

@mrjustaguy
Copy link
Copy Markdown
Contributor

mrjustaguy commented Feb 10, 2025

While I know were in the beta stage as is right now and that's a feature freeze, I personally don't see much harm in getting this in 4.4 if it's ready, given there's practically no compat breakage, and our current screen space aa is bad (until FXAA 3.11 gets merged if it gets merged in the future which will break compatibility given significantly different behavior and performance of it compared to our current FXAA) and there are cases where only Screen Space AA is usable

Example: when using https://github.com/TokisanGames/Terrain3D breaking TAA/FSR2 due to Motion Vector issues (FSR2's somewhat usable, TAA however is not) and MSAA isn't an option due to Vulkan Synchronization issues in Godot when used with other effects like in #102646

Though do keep in mind Compositor SMAA (https://github.com/RGDTAB/Godot-SMAA) is also an option, though it's much harder to work with it, making a setting to turn it off/on

Comment thread servers/rendering/renderer_rd/effects/smaa.cpp Outdated
Comment thread servers/rendering/renderer_rd/effects/smaa.cpp Outdated
Comment thread servers/rendering/renderer_rd/effects/smaa.cpp Outdated
Comment thread servers/rendering/renderer_rd/effects/smaa.cpp Outdated
Comment thread servers/rendering/renderer_rd/effects/smaa.cpp Outdated
Comment thread servers/rendering/renderer_rd/effects/smaa.cpp Outdated
Comment thread servers/rendering/renderer_rd/shaders/effects/smaa_blending.glsl Outdated
Comment thread servers/rendering/renderer_rd/shaders/effects/smaa_weight_calculation.glsl Outdated
Comment thread servers/rendering/renderer_rd/shaders/effects/smaa_weight_calculation.glsl Outdated
Comment thread servers/rendering/renderer_rd/effects/smaa.cpp Outdated
@DeeJayLSP
Copy link
Copy Markdown
Contributor

DeeJayLSP commented Feb 17, 2025

Testing on the TPS demo (Native resolution, SMAA enabled, other AAs disabled). Edge detection seems a bit broken.

image

image

This also happened in Godot-SMAA back then

@mrjustaguy
Copy link
Copy Markdown
Contributor

No, this is the SMAA settings.. the edge detection threshold needs to get really really low to catch some of the aliasing, and some of the aliasing just is due to a lack of samples, that SMAA and FXAA cannot help with.

@RGDTAB
Copy link
Copy Markdown
Contributor Author

RGDTAB commented Mar 26, 2025

I think you're right that WebP would shrink the binary size, just maybe not by as much as you reported.
The WebP file you provided didn't have color information on pixels where the alpha value is zero. I've done a little testing with cwebp and found that if we preserve the colors with full transparency the raw WebP file is still about 4.5KB smaller than the PNG file. That would translate to about 6KB of savings when encoded to base64.

As for loading the WebP at runtime, we could try waiting until we actually try to apply the effect. By then all the modules should be loaded. We should probably be doing that even with PNG so that the texture doesn't take up room in the VRAM if SMAA isn't in use. WebP only supports RGB and RGBA, so it would have to be converted to RG or LA after loading so it doesn't take up more memory than needed.

The ultimate downside for using WebP for compression is that it would force a dependency on an optional module. We could either try and find a workaround to re-encode to PNG if WebP is disabled, or we could just issue a warning if someone tries to enable SMAA if WebP is disabled. If the binary size matters more than the header size then we could try skipping the Base64 encoding. That might shave off about 10KB from the binary. Maybe we could just store the PNG or WebP file in the repository, and then convert it to a header file when building.

@Calinou
Copy link
Copy Markdown
Member

Calinou commented Mar 26, 2025

Maybe we could just store the PNG or WebP file in the repository, and then convert it to a header file when building.

I agree this would be the way to go. This is traditionally what we do for things like icons, while we haven't relied on Base64 encoding too much.

Copy link
Copy Markdown
Member

@Calinou Calinou left a comment

Choose a reason for hiding this comment

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

Tested again, it works as expected on Forward+ only.

In Mobile, there's an issue where having SMAA enabled in the project settings has no effect in the running project (you need to change it at runtime for it to work). It will also be visible in the editor. In Forward+, it works on startup correctly. FXAA is also affected by this issue.

In Compatibility, there's now an error message printed when you try to enable either SMAA or FXAA (which is expected as neither is implemented there yet). We should probably harmonize these messages to turn them into warnings, but this can be done in a separate PR as there's many of them right now.

In the editor, a capitalization for smaa to SMAA should be added to this list in alphabetical order (as the SMAA Edge Detection Threshold project setting would then make use of it):

EditorPropertyNameProcessor::EditorPropertyNameProcessor() {

Also, I found a slight binary size optimization using oxipng:

❯ oxipng -Zso6 AreaTex.png SearchTex.png 
Processing: AreaTex.png
Processing: SearchTex.png
120 bytes (11.76% smaller): SearchTex.png
27091 bytes (7.48% smaller): AreaTex.png

You can integrate the textures from this ZIP to the PR: smaa_textures.zip

I made sure fully transparent pixels are not affected this time (oxipng doesn't touch them by default).

Note

These figures take the above optimization I made into account.

Binary size comparison for a Linux release export template:

  • master: 70,647,752 bytes
  • This PR: 70,713,320 bytes

Binary size comparison for a web release export template:

  • master: 37,222,703 bytes
  • This PR: 37,225,000 bytes

It's only a 3 KB difference, but the web platform only includes the Compatibility rendering method, so this is expected.

Comment thread servers/rendering_server.cpp Outdated
@RGDTAB RGDTAB force-pushed the add_partial_smaa branch from b926008 to 7dbc4fe Compare May 11, 2025 17:40
@RGDTAB
Copy link
Copy Markdown
Contributor Author

RGDTAB commented May 11, 2025

I'll get to work investigating what's happening with the Mobile renderer.
For now I've integrated the oxipng textures and your other suggestions, and fixed some merge conflicts.

@RGDTAB
Copy link
Copy Markdown
Contributor Author

RGDTAB commented May 19, 2025

As @mrjustaguy has noted, the issue also exists outside of this PR with FXAA. There wasn't any check for screen space AA, so the mobile renderer will just run the tonemapper as a subpass for performance purposes if there aren't any other post processing effects enabled such as glow or DOF.
We can either fix it with this PR or I could open up another one, I'm cool with either.

@BlueCube3310
Copy link
Copy Markdown
Contributor

As @mrjustaguy has noted, the issue also exists outside of this PR with FXAA. There wasn't any check for screen space AA, so the mobile renderer will just run the tonemapper as a subpass for performance purposes if there aren't any other post processing effects enabled such as glow or DOF. We can either fix it with this PR or I could open up another one, I'm cool with either.

Sorry for not reading this before, I only saw this comment now. That got fixed by #106631

@RGDTAB RGDTAB force-pushed the add_partial_smaa branch from 7dbc4fe to 6b99608 Compare May 21, 2025 22:46
@RGDTAB
Copy link
Copy Markdown
Contributor Author

RGDTAB commented May 21, 2025

Sorry for not reading this before, I only saw this comment now. That got fixed by #106631

No problem! You got around to fixing it faster than I would have anyhow. Just updated this PR to make sure that the full post-processing pass occurs with SMAA also, so we should be all good now.

@BlueCube3310
Copy link
Copy Markdown
Contributor

Tested again, it works as expected in both Mobile and Forward+

@Jamsers
Copy link
Copy Markdown

Jamsers commented May 23, 2025

here are some comparisons from the latest build artifacts

no aa:
godot_no_aa

fxaa:
godot_fxaa

smaa:
godot_smaa

@clayjohn clayjohn modified the milestones: 4.x, 4.5 May 23, 2025
Copy link
Copy Markdown
Member

@Calinou Calinou left a comment

Choose a reason for hiding this comment

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

Tested locally, it works as expected in all rendering methods (in Compatibility, an error is displayed as intended).

I've also tested it in combination with TAA/FSR2 and it helps further reduce aliasing there, particularly in fast-moving scenes or just after a camera cut (the impact is more noticeable at lower FPS). Since SMAA doesn't blur the rendering as much as FXAA, it's more viable for this use case.

@Repiteo Repiteo merged commit e89c4b0 into godotengine:master May 26, 2025
20 checks passed
@Repiteo
Copy link
Copy Markdown
Contributor

Repiteo commented May 26, 2025

Thanks! Congratulations on your first merged contribution! 🎉

@akien-mga
Copy link
Copy Markdown
Member

I notice now that this added some textures in thirdparty/smaa/. What's the source for those textures?

@Jamsers
Copy link
Copy Markdown

Jamsers commented Jun 5, 2025

These are most likely the precomputed area textures that encode the location of edges, coverage areas, and precomputed areas for blending (SMAA paper)

Can be found here in the original SMAA repository. Generated with these scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integrate sub-pixel morphological antialiasing (SMAA) in the Vulkan renderer