Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to eliminate weird moire and line jitter (View zoom/antialiasing)  (Read 6725 times)

0 Members and 1 Guest are viewing this topic.

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
here is short example which demonstrates the issue:

(click to show/hide)

I tried different ways but didn't found any solution to this.
Those distortions and moire looks very bad with some scale values.
Could you please help me to eliminate those weird distortions?

May be there is any way to fix it with some kind of shaders and post processing effects?

Also it looks much worse on the system which doesn't support MSAA (OpenGL 2.3).
Is there any way to fix it? May be there is any kind of software MSAA?

Here is screenshot:

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: How to eliminate weird moire and line jitter (View zoom/antialiasing)
« Reply #1 on: December 27, 2015, 03:15:59 pm »
Tried the same test on D2D with using built-in RenderTarget.DrawLine function, it works a little better (moire is not so high like with SFML example), but it's still exists.

Here is screenshot with comparison between SFML and D2D:
(click to show/hide)

Here is screenshot with comparison for the last frame in the animation:
(click to show/hide)

As you can see D2D antialiasing works much better, but it's still has some issues.

I read some articles on the internet and it seems that it's impossible to render good antialiased line with using of standard MSAA. I found some suggestions to implement DrawLine with shaders. I didn't worked with shaders before. Can anyone help me with examples on how to utilize it for DrawLine function?
« Last Edit: December 27, 2015, 03:33:18 pm by mkalex777 »

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: How to eliminate weird moire and line jitter (View zoom/antialiasing)
« Reply #2 on: December 27, 2015, 10:01:26 pm »
You could draw one of those quads (i.e. the repeating pattern) to a texture that's actually two or even four times the final display size/resolution. Then render the repeated texture to one single big quad.

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: How to eliminate weird moire and line jitter (View zoom/antialiasing)
« Reply #3 on: December 28, 2015, 06:37:11 am »
I found strange issue with SFML antialiasing.
Here is screenshot (I used the same source with static scale=1):
(click to show/hide)

All lines were drawn with Color.Black. But as you can see, SFML antialiasing changes color to grey incorrectly.
While D2D antialiasting shows still black lines.


Here is a small fix in DrawLine function (with line thickness).
It works incorrectly, because draws line 2x times bold.
With using it, SFML result looks very close to D2D.

        public static void DrawLine(
            RenderTarget target,
            Vector2f start,
            Vector2f end,
            float strokeWidth,
            Color color)
        {
            var dv = end - start;
            var dl = (float)Math.Sqrt(dv.X * dv.X + dv.Y * dv.Y);
            var uv = dv / dl;
            var up = new Vector2f(-uv.Y, uv.X);
            var offset = up * strokeWidth;
            var array = new[]
            {
                new Vertex(start + offset, color),
                new Vertex(end + offset, color),
                new Vertex(end - offset, color),
                new Vertex(start - offset, color),
            };
            target.Draw(array, PrimitiveType.Quads);
        }
 

Here is comparison between SFML and D2D antialiasing:
(click to show/hide)

As you can see, the image is very close. But...
There is an issue with line jitter for both - SFML and D2D. May be it's related to precision of float value.

With take into account this issue, D2D antialiasing works great. It shows all lines with no skip.
But there is another picture for SFML - some lines just disappearing.
So, it's definitely SFML antialiasing issue.
« Last Edit: December 28, 2015, 07:10:03 am by mkalex777 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to eliminate weird moire and line jitter (View zoom/antialiasing)
« Reply #4 on: December 28, 2015, 09:38:29 am »
"SFML antialiasing" is just OpenGL multi-sampling. There's nothing much we can do...

D2D anti-aliasing is probably a totally different one, since it is optimized for 2D.
Laurent Gomila - SFML developer

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: How to eliminate weird moire and line jitter (View zoom/antialiasing)
« Reply #5 on: December 28, 2015, 10:20:57 am »
Forgot to mention it earlier. If possible, you should provide a comparison either using raw OpenGL or some other OpenGL library. Behind the scenes the driver will most likely execute the very same instructions, but there might be different defaults and/or commands (as Laurent mentioned).

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: How to eliminate weird moire and line jitter (View zoom/antialiasing)
« Reply #6 on: December 28, 2015, 10:43:49 am »
"SFML antialiasing" is just OpenGL multi-sampling. There's nothing much we can do...

I think it's not "just OpenGL", because OpenGL MSAA depends on many opengl settings (and it's configuration sequence) which is configured by SFML. I read about some issues which happens with OpenGL MSAA with some specific combination of opengl settings.

For example:
Quote
opengl antialiasing does not work properly in recent processing versions unless you
first do a " hint(DISABLE_OPENGL_2X_SMOOTH);" Then you can reenable it or
smooth() or whatever. If you don't disable it first, you don't get the
best AntiAlias quality.

I'm not sure if it's the root of cause, because this commet was related to another software, but incorrect configuration may leads to poor antialiasing

Just look at this test example:
(click to show/hide)

Here is D2D test source:
(click to show/hide)

D2D test sample uses my wrapper, here is some code to understand what happens under the hood:
(click to show/hide)


Results:

(click to show/hide)

(click to show/hide)

SFML screenshot shows that antialiasing is very poor
« Last Edit: December 28, 2015, 06:35:46 pm by mkalex777 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to eliminate weird moire and line jitter (View zoom/antialiasing)
« Reply #7 on: December 28, 2015, 02:46:42 pm »
Quote
I think it's not "just OpenGL", because OpenGL MSAA depends on many opengl settings (and it's configuration sequence) which is configured by SFML
Not really... SFML enables multi-sampling if you tell it to, and uses the number of samples that you set in sf::ContextSettings. That's it for the "many OpenGL settings".

But if you have a good understanding of OpenGL anti-aliasing, and you think we do it the wrong way, why don't you test it yourself and try to come up with a better "configuration sequence"? That would be much more relevant than comparing the result to some other API about which we know nothing.
Laurent Gomila - SFML developer

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: How to eliminate weird moire and line jitter (View zoom/antialiasing)
« Reply #8 on: December 28, 2015, 03:45:59 pm »
Quote
I think it's not "just OpenGL", because OpenGL MSAA depends on many opengl settings (and it's configuration sequence) which is configured by SFML
Not really... SFML enables multi-sampling if you tell it to, and uses the number of samples that you set in sf::ContextSettings. That's it for the "many OpenGL settings".

But if you have a good understanding of OpenGL anti-aliasing, and you think we do it the wrong way, why don't you test it yourself and try to come up with a better "configuration sequence"? That would be much more relevant than comparing the result to some other API about which we know nothing.

I'm noob in opengl. I just see poor antialiasing and trying to understand how to fix it :)
I see that D2D has much better MSAA. I'm not sure but I think that MSAA should works with the same quality (ok, with minor +-) on opengl and d2d. Because it uses the same technique (and probably the same code). But test shows that it works much worse. Why?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to eliminate weird moire and line jitter (View zoom/antialiasing)
« Reply #9 on: December 28, 2015, 04:59:25 pm »
Quote
I'm noob in opengl
So how do you know all that stuff?
Quote
OpenGL MSAA depends on many opengl settings (and it's configuration sequence)

Quote
I see that D2D has much better MSAA
How do you know it is MSAA?

Quote
But test shows that it works much worse. Why?
The only way to spot the difference is to know exactly how OpenGL MSAA works, and how D2D antialiasing works.
Laurent Gomila - SFML developer

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: How to eliminate weird moire and line jitter (View zoom/antialiasing)
« Reply #10 on: December 29, 2015, 02:32:26 pm »
Just read some discussions about D2D internals and it seems that it's really using it's own antialiasing which is highly criticized for high cpu usage. It uses some heavy tessellation which splits even simple line into thousands of triangles and doing it on cpu. As result, it's complicated to make high performance for a lot of primitives with enabled antialiasing on D2D, because it leads to processing millions triangles inside D2D.
But result of D2D rendering is really impressive. I just implemented a small test to render outline texts, and it looks really cool. And the code is pretty easy, because all things to build outline geometry is done with several D2D calls. And the most cool thing is that D2D works good enough even with generating geometry just in real time. SFML lags like hell when I tried to render text with different sizes in real time. So, I implemented single text instance with fixed font size and scale it to desired size in render code. But with D2D it works well just out of the box with no any optimizations and with no need to scale.
Here is how the text looks with D2D (unfortunately I cannot achieve the same result with SFML):


The bad thing is that D2D can run on windows only :(

But my question is still open. How I can improve antialiasing with using SFML?
« Last Edit: December 29, 2015, 02:50:31 pm by mkalex777 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to eliminate weird moire and line jitter (View zoom/antialiasing)
« Reply #11 on: December 29, 2015, 03:09:47 pm »
Quote
But my question is still open. How I can improve antialiasing with using SFML?
The only option that SFML provides is the number of samples when you enable MSAA. For other solutions you'll have to look at what you can do with OpenGL directly. Google "2D antialiasing OpenGL" for a starting point ;)
Laurent Gomila - SFML developer

 

anything