View Full Version : Getting Data Back (glMapbuffer?)

01-30-2013, 02:18 AM
I want to know how can I get my data back from a Buffer.
Actually, I'm making use of a vertex shader that draws result into a transformfeedbackbuffer.
Now I want to get my data that is in the transformfeedbackbuffer to check if it is doing the right compuation.

In the initialization I create an update_vao:

glBindBuffer(GL_ARRAY_BUFFER, position[0]);
glBufferData(GL_ARRAY_BUFFER, 3*positions.size() * sizeof(GL_FLOAT), positions.constData(), GL_DYNAMIC_COPY);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, velocity[0]);
glBufferData(GL_ARRAY_BUFFER, 3*velocities.size() * sizeof(GL_FLOAT), velocities.constData(), GL_DYNAMIC_COPY);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);

In the Paint part I do this :

// Write to second
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, position[1]);
glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 1, velocity[1]);

// Turn off rasterization for the simulation pass (no fragment shader)

// Press record...
// Draw
glDrawArrays(GL_POINTS, 0, positions.size());
// Press stop...

When I'm done I want to get what was drawn in the position[1] and velocity[1],
How can I do that, I tried whith glMapBuffer but I have exceptions (Memory Acces)

Dark Photon
01-30-2013, 04:49 AM
As a for-instance, using the traditional buffer binding APIs:

GLuint buf_handle;
GLfloat buf[ BUFSIZE ];

glBindBuffer( GL_ARRAY_BUFFER, buf_handle );
GLfloat *p = (GLfloat *) glMapBufferRange( GL_ARRAY_BUFFER, 0, BUF_SIZE*sizeof(GLfloat), GL_MAP_READ_BIT );
memcpy( buf, p, BUFSIZE*sizeof(GLfloat) );
glUnmapBuffer( GL_ARRAY_BUFFER );

Or an alternative (using EXT_direct_state_access (http://www.opengl.org/registry/specs/EXT/direct_state_access.txt)) (aka DSA) which cuts the needless bind completely out of the picture:

GLuint buf_handle;
GLfloat buf[ BUFSIZE ];

GLfloat *p = (GLfloat *) glMapNamedBufferRangeEXT( buf_handle, 0, BUF_SIZE*sizeof(GLfloat), GL_MAP_READ_BIT );
memcpy( buf, p, BUFSIZE*sizeof(GLfloat) );
glUnmapNamedBufferEXT( buf_handle );

The bind point you'll probably be attaching your output buffer to before transform feedback is GL_TRANSFORM_FEEDBACK_BUFFER (which is an array you can set with glBindBufferBase), but afterwards to map it it doesn't matter what bind point you use (or if you even use one in the case of DSA). You just want a CPU-side pointer to a copy.

Also note that if you want the whole buffer instead of just a piece of it, instead of glMapBufferRange / glMapNamedBufferRangeEXT, you can use glMapBuffer / glMapNamedBufferEXT.

Given the error you are getting, I would make sure that you are not accessing beyond the length of the mapped buffer. For instance, if you've only initialized it to have 20 bytes (via glBufferData), then don't go accessing 21 bytes from the mapped buffer.

01-30-2013, 06:39 AM
Thank, everythink works fine now.