What does my choice of GLFW_SAMPLES actually do?

Mike Bell picture Mike Bell · Mar 17, 2017 · Viewed 7.1k times · Source

What does setting this variable do? For instance, if I set it to 4, what does that mean?

I read a description on glfw.org (see here: GLFW Window Guide) under the "Framebuffer related hints" section. The manual says "GLFW_SAMPLES specifies the desired number of samples to use for multisampling. Zero disables multisampling. GLFW_DONT_CARE means the application has no preference."

I also read a description of multisampling in general (see here: Multisampling by Shawn Hargreaves).

I have a rough idea of what multisampling means: when resizing and redrawing an image, the number of points used to redraw the image should be close enough together that what we see is an accurate representation of the image. The same idea pops up with digital oscilloscopes---say you're sampling a sinusoidal signal. If the sampling rate just so happens to be exactly equal to the frequency (f) of the wave, the scope displays a constant voltage, which is much different than the input signal you're hoping to see. To avoid that, the Nyquist Theorem tells us that we should sample at a rate of at least 2f. So I see how a problem can arise in computer graphics, but I don't know what exactly the function

glfwWindowHint(GLFW_SAMPLES, 4); does.

Answer

vallentin picture vallentin · Mar 17, 2017

What does setting this variable do? For instance, if I set it to 4, what does that mean?

GLFW_SAMPLES is used to enable multisampling. So glfwWindowHint(GLFW_SAMPLES, 4) is a way to enable 4x MSAA in your application.

4x MSAA means that each pixel of the window's buffer consists of 4 subsamples, which means that each pixel consists of 4 pixels so to speak. Thus a buffer with the size of 200x100 pixels would actually be 800x400 pixels.


If you were to create an additional framebuffer that is 4 times bigger than the screen. Then using it as a texture sampler with GL_LINEAR as the filter, would basically achieve the same result. Note that this is only the case for 4x MSAA, as GL_LINEAR only takes 4 samples closest to the pixel in question.

When it comes to anti-aliasing, then using MSAA is a really effective but expensive solution. If you want a very clean and good looking result, then it's definitely the way to go. 4x MSAA is usually chosen as there's a good balance between quality and performance using it.

The cheaper alternative in terms of performance is to use FXAA. Which is done as a post-processing step and usually comes at no cost. The difference is that MSAA renders at a bigger size and downsamples to to the wanted size, not loosing any quality. Where as FXAA simply averages the pixels as is, basically bluring the image. FXAA usually gives a really decent result.

Also your driver will most likely enables it by default. But if it doesn't then use glEnable(GL_MULTISAMPLE).


Lastly if you haven't already, then I highly recommend reading LearnOpenGL's Anti-Aliasing tutorial. It gives a really in-depth explanation of all of this.