Aliasing using ellipses

Started by
2 comments, last by MJP 11 years, 6 months ago
Hello everybody!

To draw ellipses in my application i use the following HLSL-code:
[source lang="csharp"]void PixelMainFill(in PixelShaderInput input, out float4 color : COLOR0)
{
float elemX = (-elementSize.x / 2.0f) + elementSize.x * input.texCoord.x;
float elemY = (-elementSize.y / 2.0f) + elementSize.y * input.texCoord.y;

float radius = getEllipseRadius(elemX, elemY);
float radiusCoord = sqrt(elemX * elemX + elemY * elemY);

if(radiusCoord <= radius)
color = input.color;
else
color = float4(0, 0, 0, 0);
}[/source]


While the ellipse really is an ellipse with this code i have a problem that it looks absolutely blocky as with no multisampling even though i have eight samples and a quality of two.

Is there a way to have multisampling applied to my ellipse?

Greetings
Plerion
Advertisement
I'm not a multisampling expert, but the problem might be that the multisampling is only applied to the polygon edges, and since you calculate your ellipse in the pixel shader (I assume that you draw a rectangle) the multisampling may not be applied to the pixels inside the rectangle.

So, to have smoother edges for your ellipse you can either:

- create a ellipse geometry with mesh refinement to make it smooth

- or add a smoothing to your pixel shader. That is, instead of doing (radiusCoord <= radius) which gives either inside or outside (=blocky) output, you'll interpolate between in and out at few pixel radius.

- you can also sample your ellipse multiple times for each pixel shader invocation with a little jitter (less than a pixel) and then use the inside and outside values as interpolator for the color.

Cheers!
Hey

I already used the approach of making multiple samples but it seems i have chosen a stupid way to create the samples :D. I switched now and interpolated at the edges of the ellipse with an inverted sinus function and 2 pixel radius. Its not yet perfect, but id say its a good start!

Before:
50785889b97b08_ellipse_prev.jpg

After:
507858dcb3d709_ellipses.jpg
MSAA does not increase the rate at which the pixel shader runs, it only increases the sampling rate for rasterization and depth testing. In your case you're analytically calculating pixel coverage in your pixel shader instead of with the rasterizer, so MSAA is of no use. What you really want to do is either

A. Use more than one sample per pixel for testing whether a pixel is part of the ellipse. For this randomized sample patterns are better than uniform sampling, and more sample points is always better.

or

B. Analytically calculate the fraction of a pixel that will be covered by your ellipse. Estimating or approximating the fraction may be good enough to provide decent results.

In both cases, you'll end up with some fraction representing the amount of coverage for a pixel. Once you have that, you can use it as an alpha value (with alpha blending enabled, obviously). This will blend your ellipse value with the background based on coverage, giving nice antialiased results. Your lack of alpha-blending is what's currently giving you the black halo around the ellipse when it overlaps with the square.

This topic is closed to new replies.

Advertisement