Difference between revisions of "Multisampling"

From OpenGL.org
Jump to: navigation, search
(X Windows using GLX)
(Enabling Multisampling)
Line 5: Line 5:
  
 
== Enabling Multisampling ==
 
== Enabling Multisampling ==
Multisampling is a platform-specific feature and requires interaction with the window system. glEnable(GL_MULTISAMPLE)
 
  
=== Windows using wgl ===
+
There are two pieces to rendering with multisampling:
 +
 
 +
# Allocating a multisample render target (window or [[FBO]]), and
 +
# Enabling multisample rasterization (i.e. <code>glEnable( GL_MULTISAMPLE )</code>)
 +
 
 +
While the second is standard across all render target types and platforms, the first is not.  The following sections describe how to perform #1 for various render target types and platforms.  Note that when rendering to FBO, that is platform-independent as it is totally internal to GL.  But when rendering to a window, that depends on the platform's GL integration layer (WGL, GLX, AGL, etc.).
 +
 
 +
=== Rendering to an FBO ===
 +
 
 +
As mentioned, allocation of multisample off-screen FBOs is platform independent.  Here's an example:
 +
 
 +
''FIXME: Add some code''
 +
 
 +
=== Rendering to a Window ===
 +
 
 +
==== Windows (using wgl) ====
 +
 
 
The wgl functions required to create a multisampled OpenGL context are not available until a valid OpenGL context is made current to the thread. This leads to a [[Creating_an_OpenGL_Context#Proper_Context_Creation|temporary context]] code path which can get very involved with platform specific detailed. Users of libraries such as GLUT, GLEW, or GLee can significantly reduce the amount of effort required. The rest of this section assumes that valid context is present and all wgl extension function pointers have been obtained, if not, please see how to create a [[Creating_an_OpenGL_Context#Proper_Context_Creation|temporary context]].
 
The wgl functions required to create a multisampled OpenGL context are not available until a valid OpenGL context is made current to the thread. This leads to a [[Creating_an_OpenGL_Context#Proper_Context_Creation|temporary context]] code path which can get very involved with platform specific detailed. Users of libraries such as GLUT, GLEW, or GLee can significantly reduce the amount of effort required. The rest of this section assumes that valid context is present and all wgl extension function pointers have been obtained, if not, please see how to create a [[Creating_an_OpenGL_Context#Proper_Context_Creation|temporary context]].
  
Line 44: Line 59:
 
Once the correct pixel format is found, creating a context proceeds as normal.
 
Once the correct pixel format is found, creating a context proceeds as normal.
  
=== X Windows using GLX ===
+
==== X Windows (using GLX) ====
  
 
There are a few methods, but the following locates an MSAA FBConfig, looks up its XVisual, and then creates an X window in that visual.  You'll notice some similarities with the WGL method above.
 
There are a few methods, but the following locates an MSAA FBConfig, looks up its XVisual, and then creates an X window in that visual.  You'll notice some similarities with the WGL method above.
Line 99: Line 114:
 
</pre>
 
</pre>
  
=== MacOS X using Cocoa ===
+
==== MacOS X (using Cocoa) ====
 +
 
 
''TBD''
 
''TBD''
 +
 +
==== Cross-platform (using GLUT) ====
 +
 +
GLUT is a wrapper around the platform-specific GL integration layers (e.g. WGL, GLX, etc.) which allows you to prototype simple GL examples quickly.  In GLUT, the key to allocating an MSAA window is this:
 +
 +
  glutInitDisplayMode( ... | GLUT_MULTISAMPLE );
 +
 +
which directs GLUT to allocate an MSAA-capable window.  The following is a complete example showing how to render with multisampling in a simple GLUT test program:
 +
 +
<pre>
 +
#include <GL/gl.h>
 +
#include <GL/glut.h>
 +
#include <GL/glu.h>
 +
 +
#include <stdio.h>
 +
 +
float rotation_angle=0;
 +
int msaa=1;
 +
 +
void reshape(int width, int height)
 +
{
 +
glViewport(0, 0, width, height);
 +
}
 +
 +
void mouse(int button, int state, int x, int y)
 +
{
 +
if (state==GLUT_DOWN)
 +
{
 +
msaa = !msaa;
 +
glutPostRedisplay();
 +
}
 +
}
 +
 +
 +
void display()
 +
{
 +
int err=0;
 +
glClear(GL_COLOR_BUFFER_BIT);
 +
 +
glMatrixMode(GL_PROJECTION);
 +
glLoadIdentity();
 +
glOrtho(-1,1,-1,1,-1,1);
 +
 +
glMatrixMode(GL_MODELVIEW);
 +
glLoadIdentity();
 +
 +
glRotatef(rotation_angle, 0,0,1);
 +
 +
glColor4f(1,0,0,1);
 +
 +
if (msaa)
 +
{
 +
glEnable(GL_MULTISAMPLE_ARB);
 +
printf("msaa on\n");
 +
}
 +
else
 +
{
 +
printf("msaa off\n");
 +
glDisable(GL_MULTISAMPLE_ARB);
 +
}
 +
 +
glRectf(-.5,-.5,.5,.5);
 +
 +
glutSwapBuffers();
 +
err = glGetError();
 +
if (err)
 +
fprintf(stderr, "%s\n", gluErrorString(err));
 +
}
 +
 +
int
 +
main (int argc, char** argv)
 +
{
 +
glutInit(&argc, argv);
 +
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_MULTISAMPLE);
 +
glutCreateWindow(argv[0]);
 +
 +
glutDisplayFunc(display);
 +
glutMouseFunc(mouse);
 +
glutReshapeFunc(reshape);
 +
 +
glutReshapeWindow(400,400);
 +
 +
printf("%s\n",glGetString(GL_RENDERER));
 +
 +
rotation_angle=30;
 +
 +
glutMainLoop();
 +
return 0;
 +
}
 +
</pre>
  
 
== Extension References ===
 
== Extension References ===

Revision as of 23:02, 11 November 2012

Multisampling, also known as multisample antialiasing (MSAA), is one method for achieving full-screen antialiasing (FSAA). Unlike supersampling (SSAA) which can result in the same pixel being shaded multiple times per pixel, multisampling runs the fragment program just once per pixel rasterized. However with MSAA multiple depth/stencil comparisons are performed per pixel, one for each of the subsamples.

History

Before GL_ARB_multisample extension, the edges of lines, polygons, and points could be selectively antialiased using using glEnable(GL_LINE_SMOOTH), glEnable(GL_POLYGON_SMOOTH), glEnable(GL_POINT_SMOOTH) respectively, combined with a blending function, such as glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA). Such features were typically not implemented in hardware in early consumer graphics cards at the time, and were done in software resulting in poor performance. More expensive "workstation" graphics cards from HP, Sun, and SGI at the time did implement these features in hardware. Modern programs should not make use of these features.

Enabling Multisampling

There are two pieces to rendering with multisampling:

  1. Allocating a multisample render target (window or FBO), and
  2. Enabling multisample rasterization (i.e. glEnable( GL_MULTISAMPLE ))

While the second is standard across all render target types and platforms, the first is not. The following sections describe how to perform #1 for various render target types and platforms. Note that when rendering to FBO, that is platform-independent as it is totally internal to GL. But when rendering to a window, that depends on the platform's GL integration layer (WGL, GLX, AGL, etc.).

Rendering to an FBO

As mentioned, allocation of multisample off-screen FBOs is platform independent. Here's an example:

FIXME: Add some code

Rendering to a Window

Windows (using wgl)

The wgl functions required to create a multisampled OpenGL context are not available until a valid OpenGL context is made current to the thread. This leads to a temporary context code path which can get very involved with platform specific detailed. Users of libraries such as GLUT, GLEW, or GLee can significantly reduce the amount of effort required. The rest of this section assumes that valid context is present and all wgl extension function pointers have been obtained, if not, please see how to create a temporary context.

A valid pixel format for the framebuffer is choose using the wglChoosePixelFormatARB function with a list of attributes to specify the multisampling properties. In order to choose a framebuffer format that incorporates multisampling, you must add it to the list of attributes. For example, this list of attributes does not select a multisampled pixel format:

   int attributes[] = {
   WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
   WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
   WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
   WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
   WGL_COLOR_BITS_ARB, 32,
   WGL_DEPTH_BITS_ARB, 24,
   WGL_STENCIL_BITS_ARB, 8,
   0
   };

To consider multisampled visuals, the WGL_SAMPLE_BUFFERS_ARB and WGL_SAMPLES_ARB attributes must be present. The WGL_SAMPLE_BUFFERS_ARB must be set to 1, and WGL_SAMPLES_ARB is the number of samples, e.g. for 8x multisampling, WGL_SAMPLES_ARB would be set to 8. This attribute list is the same as above, but considers 4x multisampled pixel formats too:

   int attributes[] = {
   WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
   WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
   WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
   WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
   WGL_COLOR_BITS_ARB, 32,
   WGL_DEPTH_BITS_ARB, 24,
   WGL_STENCIL_BITS_ARB, 8,
   WGL_SAMPLE_BUFFERS_ARB, 1, //Number of buffers (must be 1 at time of writing)
   WGL_SAMPLES_ARB, 4,        //Number of samples
   0
   };

Once the correct pixel format is found, creating a context proceeds as normal.

X Windows (using GLX)

There are a few methods, but the following locates an MSAA FBConfig, looks up its XVisual, and then creates an X window in that visual. You'll notice some similarities with the WGL method above.

  // --- Find a MSAA FBConfig ---
  static const int Visual_attribs[] =
    {
      GLX_X_RENDERABLE    , True,
      GLX_DRAWABLE_TYPE   , GLX_WINDOW_BIT,
      GLX_RENDER_TYPE     , GLX_RGBA_BIT,
      GLX_X_VISUAL_TYPE   , GLX_TRUE_COLOR,
      GLX_RED_SIZE        , 8,
      GLX_GREEN_SIZE      , 8,
      GLX_BLUE_SIZE       , 8,
      GLX_ALPHA_SIZE      , 8,
      GLX_DEPTH_SIZE      , 24,
      GLX_STENCIL_SIZE    , 8,
      GLX_DOUBLEBUFFER    , True,
      GLX_SAMPLE_BUFFERS  , 1,            // <-- MSAA
      GLX_SAMPLES         , 4,            // <-- MSAA
      None
    };

  int attribs [ 100 ] ;
  memcpy( attribs, Visual_attribs, sizeof( Visual_attribs ) );

  GLXFBConfig fbconfig = 0;
  int         fbcount;
  GLXFBConfig *fbc = glXChooseFBConfig( display, screen, attribs, &fbcount );
  if ( fbc )
  {
    if ( fbcount >= 1 )
      fbconfig = fbc[0];
    XFree( fbc );
  }

  if ( !fbconfig )
  {
    printf( "Failed to get MSAA GLXFBConfig\n" );
    exit(1);
  }

  // --- Get its VisualInfo ---
  XVisualInfo *visinfo = glXGetVisualFromFBConfig( display, fbconfig );
  if ( !visinfo )
  {
    printf( "Failed to get XVisualInfo\n" );
    exit(1);
  }
  printf( "X Visual ID = 0x%.2x\n", int( visinfo->visualid ) );

  // --- Now just create an X window in that visual ---

MacOS X (using Cocoa)

TBD

Cross-platform (using GLUT)

GLUT is a wrapper around the platform-specific GL integration layers (e.g. WGL, GLX, etc.) which allows you to prototype simple GL examples quickly. In GLUT, the key to allocating an MSAA window is this:

 glutInitDisplayMode( ... | GLUT_MULTISAMPLE );

which directs GLUT to allocate an MSAA-capable window. The following is a complete example showing how to render with multisampling in a simple GLUT test program:

#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glu.h>

#include <stdio.h>

float rotation_angle=0;
int msaa=1;

void reshape(int width, int height)
{
glViewport(0, 0, width, height);
}

void mouse(int button, int state, int x, int y)
{
if (state==GLUT_DOWN)
{
msaa = !msaa;
glutPostRedisplay();
}
}


void display()
{
int err=0;
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1,1,-1,1,-1,1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glRotatef(rotation_angle, 0,0,1);

glColor4f(1,0,0,1);

if (msaa)
{
glEnable(GL_MULTISAMPLE_ARB);
printf("msaa on\n");
}
else
{
printf("msaa off\n");
glDisable(GL_MULTISAMPLE_ARB);
}

glRectf(-.5,-.5,.5,.5);

glutSwapBuffers();
err = glGetError();
if (err)
fprintf(stderr, "%s\n", gluErrorString(err));
}

int
main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_MULTISAMPLE);
glutCreateWindow(argv[0]);

glutDisplayFunc(display);
glutMouseFunc(mouse);
glutReshapeFunc(reshape);

glutReshapeWindow(400,400);

printf("%s\n",glGetString(GL_RENDERER));

rotation_angle=30;

glutMainLoop();
return 0;
}

Extension References =

- http://www.opengl.org/registry/specs/ARB/multisample.txt

These might interest you : - http://www.opengl.org/registry/specs/NV/multisample_filter_hint.txt - http://www.opengl.org/registry/specs/NV/explicit_multisample.txt - http://www.opengl.org/registry/specs/NV/multisample_coverage.txt

Conclusion

Setting up FSAA takes a few steps but it is worth it. Today's GPUs are very rapid and the user wants control over the quality of the game's graphics.
On Windows, the extension we are interested in are WGL_ARB_extensions_string which defines wglGetExtensionsStringARB, but the only way to check if this is available is to call wglGetExtensionsStringARB. So this is a chicken and egg situation. Just get the function pointer to wglGetExtensionsStringARB and use it to see if you have WGL_ARB_pixel_format and WGL_ARB_multisample.