Why glReadPixels' buffer is too small?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Why glReadPixels' buffer is too small?

Artur
Hello, what's wrong with the following code?

        gl.glGetIntegerv(GL2.GL_VIEWPORT, viewport);
        int capacity = viewport.get(2)*viewport.get(3);
        if(depth == null || depth.capacity() != capacity)
            depth = GLBuffers.newDirectByteBuffer(capacity);
        gl.glReadPixels(viewport.get(0), viewport.get(1),
                viewport.get(2), viewport.get(3),
                GL2.GL_DEPTH_COMPONENT, GL2.GL_UNSIGNED_BYTE,
                depth);

It is supposed to read the entire depth buffer, but throws an exception:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Required 274750 remaining bytes in buffer, only had 273888
        at com.jogamp.common.nio.Buffers.rangeCheckBytes(Buffers.java:780)
        at jogamp.opengl.gl4.GL4bcImpl.glReadPixels(GL4bcImpl.java:21024)

content of the buffer "viewport": 0 0 634 432

634*432=273888, so should not it be enough?
Reply | Threaded
Open this post in threaded view
|

Re: Why glReadPixels' buffer is too small?

gouessej
Administrator
Hi

Are you sure you can use GL_DEPTH_COMPONENT with GL_UNSIGNED_BYTE?

636 * 432 = 274752

I don't understand why it requires 274750 bytes.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Why glReadPixels' buffer is too small?

Wade Walker
Administrator
In reply to this post by Artur
If you look at the source code at https://github.com/sgothel/jogl/blob/master/src/jogl/classes/com/jogamp/opengl/util/GLBuffers.java, you can see it's doing some complex calculations with GL.GL_PACK* and GL.GL_UNPACK* to figure out the required buffer size -- it's not as simple as just multiplying x and y size and the depth bytes together  Those GLBuffers.sizeof() methods are invoked by Buffers.rangeCheckBytes(), which is invoked by GL4bcImpl.glReadPixels(). All this checking is apparently to prevent inadvertently aksing OpenGL to write outside allocated memory by passing it a buffer that's too small.
Reply | Threaded
Open this post in threaded view
|

Re: Why glReadPixels' buffer is too small?

Sven Gothel
Administrator
On 04/18/2012 05:34 PM, Wade Walker [via jogamp] wrote:
> If you look at the source code at
> https://github.com/sgothel/jogl/blob/master/src/jogl/classes/com/jogamp/opengl/util/GLBuffers.java,
> you can see it's doing some complex calculations with GL.GL_PACK* and
> GL.GL_UNPACK* to figure out the required buffer size -- it's not as simple as
> just multiplying x and y size and the depth bytes together  Those
> GLBuffers.sizeof() methods are invoked by Buffers.rangeCheckBytes(), which is
> invoked by GL4bcImpl.glReadPixels(). All this checking is apparently to
> prevent inadvertently aksing OpenGL to write outside allocated memory by
> passing it a buffer that's too small.

Very good, yup that is it - it secures our binding
and makes it fail fast.

Hence you can do the reverse, asking GLBuffers which size you need,
using it's sizeof operation yourself.

[or set alignment properly]

~Sven


signature.asc (910 bytes) Download Attachment