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

Use discard_fragment to fix antialiasing #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bohdanovskyi
Copy link

In order to indicate to Metal that we don’t want to provide a color for a fragment, we cannot simply set the returned alpha value to 0. If we did, the fragment depth would still be written into the depth buffer, causing any geometry behind the “transparent” point to be obscured.

Instead, we need to call a special function that avoids specifying a color value for the fragment entirely: discard_fragment. Calling this function prevents Metal from writing the computed depth and color values of the fragment into the renderbuffer, which allows the scene behind the fragment to show through.

Link: http://metalbyexample.com/translucency-and-transparency/

In order to indicate to Metal that we don’t want to provide a color for a fragment, we cannot simply set the returned alpha value to 0. If we did, the fragment depth would still be written into the depth buffer, causing any geometry behind the “transparent” point to be obscured.

Instead, we need to call a special function that avoids specifying a color value for the fragment entirely: discard_fragment. Calling this function prevents Metal from writing the computed depth and color values of the fragment into the renderbuffer, which allows the scene behind the fragment to show through.

Link: http://metalbyexample.com/translucency-and-transparency/
@olliwang
Copy link
Member

olliwang commented Aug 17, 2019

Do you have a real example demonstrating the difference? discard_fragment() was used in the earlier versions, but the performance is much worse than float4(0).

@bohdanovskyi
Copy link
Author

Here's an example running the nanovg demo:

Using discard_fragment:
example1
Current implementation:
example2

You can see how thin lines in the demo (top left) become almost invisible and lines in general look very jagged.

You are right that discard_fragment is slower however but it's still cheaper than using multisampling as an alternative to achieve smooth lines.
discard_fragment:
profile1
Current implementation:
profile2

@olliwang
Copy link
Member

I just tested both methods on my iMac 5K (Late 2014). I think the difference is negligible. Can you tell which one uses discard_fragment() in the pic below?

Screen Shot 2019-08-23 at 9 36 17 PM

@olliwang olliwang force-pushed the master branch 3 times, most recently from d3f84a6 to d0b9aab Compare January 14, 2020 05:28
@zeromake
Copy link

zeromake commented Feb 9, 2023

@olliwang
use discard_fragment(), my black apple 4k

分辨率:	3840 x 2160(2160p/4K UHD 1 - 超高清)
UI看起来类似:	1920 x 1080 @ 60.00Hz

my example code

The trial effect is obviously better than the original:

截屏2023-02-09 11 21 35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants