shuping.cui
shuping.cui

Reputation: 1

OpenGL ES 2.x: How to read pixels after glDiscardFramebufferEXT?

I read EXT_discard_framebuffer which causes the contents of the named framebuffer attachable images to become undefined. And after discard framebuffer, the pixel value from glReadPixels are same as before discard framebuffer. why? And, with this extension, an OpenGL ES implementation how to optimize away the storing back of framebuffer contents after rendering the frame?

//
// create a texture object
//
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D, 0);

//
// create a framebuffer object
//
GLuint fbo;
GLboolean check;

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

check = glIsFramebuffer(fbo1);
if (!check) {
    __android_log_print(ANDROID_LOG_ERROR, "debug",
            "------ check Framebuffer object failed ------\n");
    return EGL_FALSE;
}

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
    __android_log_print(ANDROID_LOG_ERROR, "debug",
            "------ fbo not set completely!------\n");
    return EGL_FALSE;
}

draw_texture(fbo);

GLubyte sampledColor[4] = {0, 0, 0, 0};
int randX = 128, randY = 128;

GLenum attachments[] = { GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT };
glBindFramebuffer(GL_FRAMEBUFFER, fbo);

glReadPixels(randX, randY, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, sampledColor);
__android_log_print(ANDROID_LOG_INFO, "debug",
    "[LINE: %d] coordinate(%d, %d) color is (%d, %d, %d, %d)\n",
    __LINE__, randX, randY, sampledColor[0], sampledColor[1],
    sampledColor[2], sampledColor[3]);

glDiscardFramebufferEXT(GL_FRAMEBUFFER, 2, attachments);

glReadPixels(randX, randY, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, sampledColor);
__android_log_print(ANDROID_LOG_INFO, "debug",
    "[LINE: %d] coordinate(%d, %d) color is (%d, %d, %d, %d)\n",
    __LINE__, randX, randY, sampledColor[0], sampledColor[1],
    sampledColor[2], sampledColor[3]);

Upvotes: 0

Views: 1653

Answers (1)

Mārtiņš Možeiko
Mārtiņš Možeiko

Reputation: 12927

Job of glDiscardFramebufferEXT is to inform driver that you don't care about contents of framebuffer. What driver (or GPU) decides to do with it - it's not up to you. Driver can reset all contents to 0, or it can leave as it is, or maybe it will use this information when you will next call glClear and will perform it more efficiently (for example by allocating new memory for contents, instead of performing memset with 0 value). Don't worry about that what it will do exactly.

Upvotes: 0

Related Questions