tests/cuda4.1sdk/sdk/rendercheck_gl.cpp
//////////////////////////////////////////////////////////////////////
//  readback 
//
//   Code to handle reading back of the FBO data (but with a specified FBO pointer)
//
//////////////////////////////////////////////////////////////////////
readback
Code to handle reading back of the FBO data (but with a specified FBO pointer)
readback Code to handle reading back of the FBO data ( but with a specified FBO pointer )
bool CheckBackBuffer::readback( GLuint width, GLuint height, GLuint bufObject )
{
    bool ret = false;
    if (m_bUseFBO) {
        if (m_bUsePBO) 
        {
            printf("CheckBackBuffer::readback() FBO->PBO->m_pImageData\n");
            bindReadback();
            glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, bufObject );
	        glReadPixels(0, 0, width, height, getPixelFormat(),      GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
            ret = checkStatus(__FILE__, __LINE__, true);
            if (!ret) printf("CheckBackBuffer::readback() FBO->PBO checkStatus = %d\n", ret);
	        void *ioMem = glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB);
            memcpy(m_pImageData,    ioMem, width*height*m_Bpp);
		    glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
		    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 
            unbindReadback();
        } else {
            printf("CheckBackBuffer::readback() FBO->m_pImageData\n");
            glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, bufObject );
            ret = checkStatus(__FILE__, __LINE__, true);
            if (!ret) printf("CheckBackBuffer::readback::glBindFramebufferEXT() fbo=%d checkStatus = %d\n", bufObject, ret);
            glReadBuffer(static_cast<GLenum>(GL_COLOR_ATTACHMENT0_EXT));
            ret &= checkStatus(__FILE__, __LINE__, true);
            if (!ret) printf("CheckBackBuffer::readback::glReadBuffer() fbo=%d checkStatus = %d\n", bufObject, ret);
            glReadPixels(0, 0, width, height, getPixelFormat(), GL_UNSIGNED_BYTE, m_pImageData);
            glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
        }
    } else {
        printf("CheckBackBuffer::readback() PBO->m_pImageData\n");
		glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, bufObject);	
        void *ioMem = glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB);
        unsigned char * temp_buf = (unsigned char *)malloc(width*height*m_Bpp);
        memcpy( temp_buf, ioMem, width*height*m_Bpp );
        for (unsigned int y = 0; y < height; y++) {
            memcpy( (void *)&(m_pImageData[(height-y)*width*m_Bpp]), (void *)&(temp_buf[y*width*m_Bpp]), width*m_Bpp);
        }
        free(temp_buf);
	    glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
		glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);	
    }
	return CHECK_FBO;
}
bool CheckBackBuffer :: readback ( GLuint width , GLuint height , GLuint bufObject ) { bool ret = false ; if ( m_bUseFBO ) { if ( m_bUsePBO ) { printf ( " \n " ) ; bindReadback ( ) ; glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT , bufObject ) ; glReadPixels ( 0 , 0 , width , height , getPixelFormat ( ) , GL_UNSIGNED_BYTE , BUFFER_OFFSET ( 0 ) ) ; ret = checkStatus ( __FILE__ , __LINE__ , true ) ; if ( ! ret ) printf ( " \n " , ret ) ; void * ioMem = glMapBufferARB ( GL_PIXEL_PACK_BUFFER_ARB , GL_READ_ONLY_ARB ) ; memcpy ( m_pImageData , ioMem , width * height * m_Bpp ) ; glUnmapBufferARB ( GL_PIXEL_PACK_BUFFER_ARB ) ; glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT , 0 ) ; unbindReadback ( ) ; } else { printf ( " \n " ) ; glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT , bufObject ) ; ret = checkStatus ( __FILE__ , __LINE__ , true ) ; if ( ! ret ) printf ( " \n " , bufObject , ret ) ; glReadBuffer ( static_cast < GLenum > ( GL_COLOR_ATTACHMENT0_EXT ) ) ; ret &= checkStatus ( __FILE__ , __LINE__ , true ) ; if ( ! ret ) printf ( " \n " , bufObject , ret ) ; glReadPixels ( 0 , 0 , width , height , getPixelFormat ( ) , GL_UNSIGNED_BYTE , m_pImageData ) ; glBindFramebufferEXT ( GL_FRAMEBUFFER_EXT , 0 ) ; } } else { printf ( " \n " ) ; glBindBufferARB ( GL_PIXEL_PACK_BUFFER_ARB , bufObject ) ; void * ioMem = glMapBufferARB ( GL_PIXEL_PACK_BUFFER_ARB , GL_READ_ONLY_ARB ) ; unsigned char * temp_buf = ( unsigned char * ) malloc ( width * height * m_Bpp ) ; memcpy ( temp_buf , ioMem , width * height * m_Bpp ) ; for ( unsigned int y = 0 ; y < height ; y ++ ) { memcpy ( ( void * ) & ( m_pImageData [ ( height - y ) * width * m_Bpp ] ) , ( void * ) & ( temp_buf [ y * width * m_Bpp ] ) , width * m_Bpp ) ; } free ( temp_buf ) ; glUnmapBufferARB ( GL_PIXEL_PACK_BUFFER_ARB ) ; glBindBufferARB ( GL_PIXEL_PACK_BUFFER_ARB , 0 ) ; } return CHECK_FBO ; }
readback
Code to handle reading back of the FBO data (but with a specified FBO pointer)
readback Code to handle reading back of the FBO data ( but with a specified FBO pointer )
// binds the PBO for readback // bind FBO buffer (we want to transfer FBO -> PBO) // Now initiate the readback to PBO // map - unmap simulates readback without the copy // release the FBO // release the PBO // Reading direct to FBO using glReadPixels // read from bufObject (PBO) to system memorys image // Bind the PBO // map - unmap simulates readback without the copy // allocate a buffer so we can flip the image // let's flip the image as we copy // read from bufObject (PBO) to system memory image // unBind the PBO