1
votes

i am rendering white cube with green background in non default FBO and attaching the created multisampled texture to this FBO. When i render a cube in default FBO using above texture, it gives corruption in texture.

Here is my code:

viewport_width=32;
viewport_height=32;
        glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textureId);
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindTexture(*target, textureId);"));     

        glUniform1i(glGetUniformLocation(shader_data.psId,"tk_diffuseMap"), 0);
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glUniform1i(glGetUniformLocation(shader_data.psId,\"basetexture\"), 0);"));

        glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4 ,GL_RGBA, viewport_width, viewport_height ,true);  
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4 ,GL_RGBA, 32, 32,true);"));       

        glBindFramebuffer(GL_DRAW_FRAMEBUFFER, Fboid);
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindFramebuffer(GL_FRAMEBUFFER, Fboid);"));

        glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D_MULTISAMPLE,textureId,0);    
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D_MULTISAMPLE,textureId,0);"));

        glEnable(GL_MULTISAMPLE);
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glEnable(GL_MULTISAMPLE);"));

        draw_cube(viewport_width, viewport_height);

                glBindFramebuffer(GL_FRAMEBUFFER, 0);
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindFramebuffer(GL_FRAMEBUFFER, Fboid);"));

        glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textureId);
        nResult |= GL_ERROR_CHECK((GL_NO_ERROR, "glBindTexture(*target, textureId);"));     

                glDrawElements ( GL_TRIANGLES, 36,GL_UNSIGNED_INT, indices );

fragment shader:

uniform sampler2DMS tk_diffuseMap;
in vec3 ps_texCoord;
out vec4 fragColor;

void main(void)
{

    vec2 iTmp = textureSize(tk_diffuseMap);
    vec2 tmp = iTmp * ps_texCoord.xy;

    vec4 color;
    for(int i = 0; i < 4; ++i)
    {
        color = color + texelFetch(tk_diffuseMap, ivec2(tmp), i);
    }

fragColor=vec4(color/4);
}

Let me know where i am going wrong.

1

1 Answers

3
votes

A texture used as a FBO's render target attachment must not be bound as sampling source if the FBO it is attached to, is bound as rendering target as well. It's either texture being bound or FBO being bound.

It's obvious why this must be the case: If you could bind a texture which is also a render target you'd create a circular dependency loop.