PDA

View Full Version : increase the number of samples in multisampling



shapeare
11-18-2011, 06:39 AM
I used the following function to know that there are four samples in every texel.


glGetIntegerv(GL_SAMPLES, &sampleCount);

But, how to control the number of samples?

The sample program hdr_msaa in Chapter 9, OpenGL superbible tries to let the user adjust the number of samples per texel, but it failed to run in my computer.

Alfonse Reinheart
11-18-2011, 09:19 AM
If you're not explicitly creating a multisample buffer with FBOs, then the number of samples is controlled by how you initialize OpenGL. So... how do you do that?

shapeare
11-18-2011, 07:01 PM
This is a code segment from my program. I generated a fbo first, then bind a multisample texture to it...


glGenFramebuffers(1, &depthFbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, depthFbo);

glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &depthTexture);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, depthTexture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );

glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_DEPTH_COMPONENT32, TEX_WIDTH, TEX_HEIGHT, GL_FALSE);

glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, depthTexture, 0);

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);


but what statement can I use to generate a multisample fbo with the number of samples needed? Notice that the second parameter of glTexImage2DMultisample is queried using


glGetIntegerv(GL_SAMPLES, &sampleCount);

Alfonse Reinheart
11-18-2011, 07:37 PM
I think you're confusing terms. Or something.

A "multisampled FBO" is just a Framebuffer Object that just so happens to contain images that are multisampled. It's not the FBO that is multisampled; it's the images you put into it.

So the number of samples is set when you use `glTexImage2DMultisample` or `glRenderbufferStorageMultisample`. All you need to do is pick the desired number of samples.

shapeare
11-18-2011, 08:56 PM
Thank you very much for your answer.

Now my code changed to


glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, sampleCount, GL_DEPTH_COMPONENT32, TEX_WIDTH, TEX_HEIGHT, GL_FALSE);

with sampleCount indicating the number of samples I want each texel have.

But another question is, how to make sure it is working. Because even I assign 16 to sampleCount when generating the texture, I can still use a sample number that is bigger than 16 when using


texelFetch( gsampler2DMS sampler,
ivec2 P,
sample sample);

in the shader.

Alfonse Reinheart
11-18-2011, 09:08 PM
Oddly, the standard does not say what happens if `sample` exceeds the number of samples in the texture. But if it were defined, I'm pretty sure it would say, "undefined behavior".

In short: don't rely on it. Always send the number of samples to your shader and pick samples that actually exist.