increase the number of samples in multisampling

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.

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?

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);

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.

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.

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.