PDA

View Full Version : multisample framebuffer code not working



PeterGali
07-07-2015, 09:50 PM
Hi,
I am attempting to get multisample framebuffer code happening.

Attached is a standalone function that attempts to generate and combine a multisample framebuffer with a multisample texture, clear the buffer, blit a bit to the default framebuffer, then read back some of the main framebuffer. However the data read back is all zero, while the clearing of the buffer used a non-zero color.

I would like to know why the data read back is not what the buffer was cleared with.
Thanks in advance,
Actually the file uploader refused to accept my code as valid, so here it is.
-------------------------------------------------------------------------------------------

void verify_multisample_fbo(void)
{
int i,j;
GLuint multisample_texture;
GLuint multisample_fbo;
GLuint width = 1280;
GLuint height = 1020;
GLenum status;
GLenum err;
float array[4][4][4];
glViewport(0,0,1280,1020);
glGenTextures(1, &multisample_texture);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, multisample_texture);
glGenFramebuffers(1, &multisample_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, multisample_fbo);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 16, GL_RGBA8, width, height, GL_TRUE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, multisample_texture, 0);
assert((err = glGetError()) == GL_NO_ERROR);
status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
assert(status == GL_FRAMEBUFFER_COMPLETE);

glClearColor(0.5, 0.6, 0.7, 0.8);
glClear(GL_COLOR_BUFFER_BIT);

glBindFramebuffer(GL_FRAMEBUFFER, 0);

glBindFramebuffer(GL_READ_FRAMEBUFFER, multisample_fbo);
glReadBuffer(GL_COLOR_ATTACHMENT0);

status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
assert(status == GL_FRAMEBUFFER_COMPLETE);

status = glCheckFramebufferStatus(GL_READ_FRAMEBUFFER);
assert(status == GL_FRAMEBUFFER_COMPLETE);

glBlitFramebuffer(0,0, 4, 4, 0, 0, 4, 4, GL_COLOR_BUFFER_BIT, GL_NEAREST);

glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);

glReadPixels(0,0,4,4, GL_RGBA, GL_FLOAT, array);

glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
assert((err = glGetError()) == GL_NO_ERROR);
for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
printf("Pixel [%d,%d] = %4.3f %4.3f %4.3f %4.3f\n", i, j,
array[i][j][0], array[i][j][1], array[i][j][2], array[i][j][3]);
}
}
exit(0);
}

-------------------------------------------------------------------------------------------

PeterGali
07-14-2015, 01:46 AM
I have been searching through the specs attempting to find the answer to the above. I came across the following in section 8.8 p212 of the opengl 4.4 specs.

"When a multisample texture is accessed in a shader, the access takes one vector of integers describing which texel to fetch and an integer corresponding to the sample numbers described in section 14.3.1 describing which sample within the texel to fetch. No standard sampling instructions are allowed on the multisample texture targets."

My bold. I take it this means that to read a multisample framebuffer, a fragment shader must be used?

Alfonse Reinheart
07-14-2015, 06:55 AM
I take it this means that to read a multisample framebuffer, a fragment shader must be used?

Why do you assume that? The quoted section said nothing about fragment shaders.

PeterGali
07-14-2015, 08:52 PM
Can you tell me how to access a multisample framebuffer then without a shader program? glReadPixels is not working for me.
Regards etc,

Alfonse Reinheart
07-15-2015, 07:52 AM
I understand the question now. You can access multisample textures from any shader stage you like, but you can't read from then outside of shaders. You also can't write to them outside of shaders or fragment shader outputs.