WebGLSamples / WebGL2Samples

Short and easy to understand samples demonstrating WebGL 2 features
Other
1.01k stars 143 forks source link

Question about reading pixel from fbo of texture 3d (or 2d array) #154

Closed shrekshao closed 7 years ago

shrekshao commented 7 years ago

@kenrussell There's a question from the community. Since he is working on a gpgpu project for js ( gpu.js ), he would like to read pixels from a fbo with multiple render target, ideally with one call. I searched the spec and made some experiments. It seems readpixels cannot read data compeletely from a MRT FBO. Tested on Chrome 58 Win10 with this sample, readpixels can only read data of the first layer. It won't generate warning if we set the width or height to 3 times, but the data read is zero for the second and third layer. Seems like the only workaround is to create a separate fbo for each of the three layer, and call readpixels from one by one. I know the spec is not for gpgpu purpose. But is there a solution for him? (what does the pixel pack buffer do). Thanks!

kenrussell commented 7 years ago

Take a look at the ES 3.0 spec at https://www.khronos.org/registry/OpenGL/specs/es/3.0/es_spec_3.0.pdf , in particular the definition of the calls of BindFramebuffer, ReadBuffer and ReadPixels.

It's possible to read back COLOR_ATTACHMENTi from a bound framebuffer object by calling ReadBuffer with that attachment.

The attachments have to be read back one at a time.

shrekshao commented 7 years ago

Thanks Ken, neat and clear!