PBO 2 way copy from and into the same FBO texture attachment
I am trying to achieve this : I have A custom frame buffer into which I render some geometry.The FBO has a color attachment texture at that moment.Then I am copying it into PBO .After that I want to copy back pixels from the PBO into that texture and blit the custom FBO into default frame buffer.It doesn't work.
I updated the code to be more understandable in terms what I do :
// Here I init the PBO :
void Init(const int bufferWidth,const int bufferHeight){
glGenBuffers(1,&_imagePBO);
glBindBuffer(GL_PIXEL_PACK_BUFFER, _imagePBO);
glBufferData(GL_PIXEL_PACK_BUFFER, bufferWidth * bufferHeight * 4,0, GL_DYNAMIC_COPY);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
}
//Inside Render loop:
_fboA.BindForWrite();
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0,0, _viewportWidth,_viewportHeight);
//================== RENDER SOME GEOMETRY =======================//
..............
.........................
//===============READ from texture into PBO=======================//
_fboA.BindForRead();
glBindBuffer(GL_PIXEL_PACK_BUFFER, *_exporter->GetPBOID());
glReadPixels(400,400,_viewportWidth ,_viewportHeight,GL_RGBA,GL_UNSIGNED_BYTE,NULL);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
//=============== CLEAR TEXTURE IN FBO to GREEN ===============//
_fboA.BindForWrite();
glClearColor(0,1,0,1);
glClear(GL_COLOR_BUFFER_BIT);//|GL_DEPTH_BUFFER_BIT
_fboA.Unbind();
//==================Read from PBO back into texture ===========//
_texA->Bind();
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, *_exporter->GetPBOID());
glTexSubImage2D(GL_TEXTURE_2D,100,100,0,_viewportWidth,_viewportHeight,
GL_RGBA,GL_UNSIGNED_BYTE ,NULL);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER,0);
_texA->Unbind();
//======================== BLIT ===============================//
_fboA.BindForRead();
glBindFramebuffer(GL_DRAW_FRAMEBUFFER , 0);
glBlitFramebuffer(0,0,_viewportWidth,_viewportHeight,0,0,
_viewportWidth,_viewportHeight,GL_COLOR_BUFFER_BIT,GL_LINEAR);
What I am getting into the screen is green color and not the rendered content which I supposedly copied into PBO from _fboA .
Looks like _fboA texture doesn't get back the pixels from the PBO or PBO doesn't read the pixels from _texA.
Now , the approach probably not the optimal in terms of OpenGL ,as some people noted here but I need this kind of ping-pong for the CUDA part of my application where I share PBO as resource for CUDA kernels.
UPDATE:
For those interested , the OpenGL interop demos in CUDA SDK show cases where the texture gets into PBO from CPU ,then being copied into target texture 2D. And in my case I need to get the image into PBO from the FBO, pass it via image processing in CUDA kernel, then get it back into target texture from the PBO which will be rendered again to the screen.
Does it work if you take out the framebuffer read-back?
I'm not sure why you feel the need to use PBO here? Generally PBO is only going to be a benefit when your source or destination is not on the graphics card already.
Consider that the FBO can be bound to a texture handle directly, for render-to-texture capability. (http://www.songho.ca/opengl/gl_fbo.html#example). If that shoe fits, you could replace the glBlitFrameBuffer() with drawing a textured quad.
Also, consider using glCopyTexSubImage2D() to read pixels from frame buffer directly into a texture object.
我认为你可以在你的cuda操作后从pbo中将像素绘制到默认的帧缓冲区。
链接地址: http://www.djcxy.com/p/45736.html