Problem with multisample texture

Hi guys,

I tried multisample texture with fbo. The fbo is completed, and object is rendered. But the result is NOT multisampled, just like rendered to normal texture with normal fbo. I can’t figure it out…

Code to generate texture and fbo:


glGenFramebuffers(1, &m_FrameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, m_FrameBuffer);

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, texture);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 16, GL_RGBA16F, 512, 512, false);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0);

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, texture);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 16, GL_DEPTH_COMPONENT32, 512, 512, false);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, texture, 0);

Code to bind multisample texture:


glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, texture);

Fragment Shader:


#version 150

uniform sampler2DMS g_Texture0;
out vec4 g_FragData;

void main()
{
	g_FragData = texelFetch(g_Texture0, ivec2(gl_FragCoord.xy), 16);
}

What about:


void main()
{
vec4 sum=vec4(0);
    for(int i=0;i<16;i++){
	sum += texelFetch(g_Texture0, ivec2(gl_FragCoord.xy), i);
    }
    g_FragData = sum/16.0;
}

I do not see any specs defining that sample-index 16 (out of bounds) means “the average sum of all samples”.

Did you glEnable(GL_MULTISAMPLE)?

The functioning of all multisampled rendering depends on this.

To Alfonse Reinheart:
I think the “glEnable(GL_MULTISAMPLE)” effects frame buffer only, but not multisample texture?

To Ilian Dinev:
I didn’t realize I have to average the sum of all samples myself. Thanks!

I think the “glEnable(GL_MULTISAMPLE)” effects frame buffer only, but not multisample texture?

Did you have it on when you were rendering to this texture?

http://www.opengl.org/sdk/docs/man/xhtml/glEnable.xml

“The initial value for GL_DITHER and GL_MULTISAMPLE is GL_TRUE.”

I forgot to answer it:
I have tried Enable(GL_MULTISAMPLE) or not, nothing changed.

But when I average the sum of samples, I feel the result is a little blurred, especially on the edge.

Could you post png comparison images against the FramebufferBlit version? Afaik this shouldn’t happen.

Yes, I will try compare them later.

After reading article 2.8 in ShaderX7, Deferred Shading with Multisampling Anti-Aliasing in DirectX 10, I recognize there are much more things to do than just rendering to a multisample texture. For example:

  • All calculations should be performed on a per-sample basis.
  • A sensible optimization is to detect pixels whose samples have different values and only perform per-sample pixel shader execution on those “edge” pixels.

I’d like to try this article and inferred shading in the next few days. Any sharing about these is appreciated.