Hello Guest

FSAA Getting started

  • 2 Replies


FSAA Getting started
« on: November 21, 2005, 00:29:40 »
Hey there, I just got FSAA working and was rather pleased with myself, so I wanted to share with you all how I did it.

Basically, all you have to do is create a PixelFormat with the desired number of samples per pixel, and then pass that in to Display.create().

Code: [Select]

PixelFormat pf = new PixelFormat(8, 16, 0, numSamples);

I believe you should also call
Code: [Select]


but in my experience FSAA is enabled without this call.

It gets a little more complicated than that because your machine might not support the number of samples you've requested, or it may not support multisampling at all!  Since you can't check the GL extensions before calling Display.create, you have two options:
  • Call Display.create() to create a GL context, call GL.glGetString(GL.GL_EXTENSIONS) and check for "GL_ARB_multisample" in the string you get back.  Then call Display.create() with a PixelFormat that has multisampling enabled or disabled based on the result.
  • Call Display.create() with the desired level of samples, and catch the exceptions that it throws.
  • [/list:o]

    I chose the second option because it seems a little easier to get off the ground, and also allows me to gracefully degrade the FSAA based on the abilities of the graphics card.

Code: [Select]

boolean displayCreated = false;
int trySamples = 4;  // note that this number needs to be a power of 2
while (!displayCreated) {
try {
// PixelFormat constructor: alpha bits, z-buffer bits, stencil bits, samples
PixelFormat pf = new PixelFormat(8, 16, 0, trySamples);
displayCreated = true;
} catch (LWJGLException e) {
if ("Could not find a valid pixel format".equals(e.getMessage())) {
if (trySamples == 0) {
// something's wrong if we can't create a display w/ 0 samples
throw e;
displayCreated = false;
trySamples /= 2;
} else if("Samples > 0 specified but there's no support for GLX_ARB_multisample".equals(e.getMessage())) {
// this means it doesn't support multisampling
displayCreated = false;
trySamples = 0;
} else {
throw e;

It is a little odd (and brittle) to be checking the text of the exceptions, but it does work.  I'm not sure what the 'ideal' strategy is, but this one will certainly get you started.

I can put this in the wiki if you give me an account, Brian.  :-)

FSAA Getting started
« Reply #1 on: November 21, 2005, 00:31:30 »
Oh hell, looks like I got logged out as I was posting this.

Re: FSAA Getting started
« Reply #2 on: April 25, 2006, 18:02:35 »
Thanks for posting that code.

Quote from: "breath"
Code: [Select]

int trySamples = 4;  // note that this number needs to be a power of 2

For the record:
I hadn't gotten that far in your code and blindly tried 6 samples :) -- my Powerbook runs 6 samples/pixel AA fine.

I think that many cards can do this.

FWIW, it's an ATI Mobility Radeon 9700.

Here's some random technical info from apple.com, I guess it applies to most systems:
Quote from: "apple.com"

The acceptable values for samples are 2, 4 or 6 currently with VRAM, performance and card capabilities limiting the clients choice.

That seems to be the deal for OS X, then.