Sign in to follow this  
Alessandro

copy buffer to std::vector

Recommended Posts

Alessandro    302
In my opengl application I read the framebuffer using glReadPixels and store it into a buffer like this:

[code]
..........................
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboId);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, screenWidth, screenHeight, GL_RGB, GL_UNSIGNED_BYTE, 0);
GLubyte *screenColorBuffer = (GLubyte*)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB);
..........................
[/code]

After that I loop and store buffer values into a std::vector<GLubyte>.
I'd like to ask if there is actually a clever solution like copying the whole buffer in the vector at once, without using a loop.
Thanks in advance. Edited by Alessandro

Share this post


Link to post
Share on other sites
SiCrane    11839
Well instead of binding a buffer before calling glReadPixels() you could make sure that your vector is big enough for the data and pass &vec[0] as the last argument of glReadPixels(). That will make OpenGL store the data in the vector in the first place.

Share this post


Link to post
Share on other sites
Alessandro    302
Thanks for the suggestion. I tried this but the resulting vector is empty [img]http://public.gamedev.net//public/style_emoticons/default/blink.png[/img]

[code]
std::vector<GLubyte> myBuffer; // declare vector
myBuffer.reserve(size*size*3); // reserve size for it
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboId);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, size1,size2, GL_RGB, GL_UNSIGNED_BYTE, &myBuffer[0]);
glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB); // release pointer to the mapped buffer
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
[/code]

Share this post


Link to post
Share on other sites
SiCrane    11839
reserve() only sets the reserved size, it doesn't actually set the vector size. You need to actually make the vector the right size, not just reserve space. One way is to specify the desired size as part of the constructor call. Another is to use resize() after the vector is created.

Share this post


Link to post
Share on other sites
Alessandro    302
Ok, added a
[code]
myBuffer.resize(size1*size2*3);
[/code]

Now the vector has the proper size but it's filled with 0's, so I take the glReadPixels isn't yet storing values there.

Share this post


Link to post
Share on other sites
Alessandro    302
Before. Here is my code:

[code]
std::vector<GLubyte>myBuffer;
myBuffer.resize(size1*size2*3);
glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboId);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, size1,size2, GL_RGB, GL_UNSIGNED_BYTE, &myBuffer[0] );
glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB); // release pointer to the mapped buffer
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
[/code]

Share this post


Link to post
Share on other sites
SiCrane    11839
I just noticed that you're still binding a buffer. As I said before, you would specify the memory address of the vector [b]instead[/b] of binding a buffer. glReadPixels() will copy the pixel information to the bound buffer if one is bound, using the data argument as an offset into that buffer. In order for it to treat the data argument as a destination address, there needs to be no buffer bound.

Share this post


Link to post
Share on other sites
rnlf_in_space    1894
I don't think, a vector is the right tool for this job. I had exactly the same problem a few weeks ago and figured I could not rely on the compiler to optimize away the initialization of the vector elements with zeros, which would be actually a big performance hit, if this code was to run more than a few times per minute or so.

I recommend you use a std::unique_ptr (or boost::shared_ptr if you can't use C++11) to a manually created array like this:

[CODE]
std::unique_ptr<GLubyte[]> myBuffer(new GLubyte[size1*size2*3]);
glReadPixels(...., myBuffer.get());
[/CODE]

This way, you get the benefits you would also get from a vector (which very much boils down to automatic cleanup) but you don't get the drawbacks (unneccessary initialization of the memory buffer. With a vector, you might end up doubling the time needed to read your pixels...

I know, using vector&co for everything is an advice you hear very often, but sometimes you have to know when to use something different. Edited by rnlf

Share this post


Link to post
Share on other sites
rip-off    10976
It depends on what the OP is using it for. For something like a screenshot, I wouldn't bother trying to optimise too heavily, the code is run so rarely relative to other parts of the system. If the code is run more frequently, the bigger performance problem is probably the copying of data from the GPU and the associated pipeline stall.

Share this post


Link to post
Share on other sites
Bregma    9202
[quote name='rnlf' timestamp='1346064346' post='4973729']
Why bother? Where is it more work or less comfort to use a smart-pointer to an array instead of using a vector? It has nothing but advantages (you just cannot use .at() but who does anyways?)
[/quote]
You mean using a simple (smart) pointer has nothing but advantages over a data store that tracks its size automatically, can be easily resized, and can be optimized by the compiler when used with most standard algorithms to produce better code? You're probably right, except for all those places where you're wrong.

Certainly, if you want a raw chunk of memory for a fixed-length byte buffer it makes just as much sense to use a (smart) pointer and ::operator new (or, if you're stuck in the 1970's its grandfather, malloc()) as to use a std::vector. The minute you start doing anything more complicated than that, you're generally better off using an appropriate higher-level data structure.

Share this post


Link to post
Share on other sites
rnlf_in_space    1894
Bregma, yes. You're absolutely right. My comment is just for this situation (read a fixed-size buffer from a C library, do something like writing it to a file and free the buffer), where it is most unlikely that resizing or your off-the-shelf-C++-standard-library-algorithms are required. But how often do you sort() or erase_if() your pixel buffer data? It is most likely that the data will either be written directly to an image file or processed for post-effects. In that case there is really no advantage in using a full-fledged container.

I never said using vectors for anything but this general use case was a bad idea. I didn't say so, because this would in fact be seriously stupid. Edited by rnlf

Share this post


Link to post
Share on other sites
rip-off    10976
Code doesn't exist in isolation, the OP may already have code for dealing with the vector. This might need to be modified to account for changing to a smart array.

The main general drawback is that the vector remembers its size, whereas the smart array does not. This probably is not an issue here, as the vector's size is likely insufficient anyway - it is actually a two dimensional array being hidden in a one dimensional vector.

Share this post


Link to post
Share on other sites
Alessandro    302
For the necessities of my application using a std::vector to store pixels is just fine. I need to grab those pixels each time that the camera orientation/position (managed by the mouse) has changed. And so, each time that the mouse button is released the function runs, grab the pixels into a vector, sort and remove duplicate values (I use it later to do opengl picking based on colors). The operation takes less than 100ms, and is almost unnoticeable.
Interesting the use of the boost library, as I have some time I will try that as well, as I get some spare time...

Share this post


Link to post
Share on other sites
rnlf_in_space    1894
Wow... I didn't think my examples of standard algorithms that I thought nobody would like to use with pixel data are just what you want to do with your pixel data. I withdraw my objections from a few post up ;-)

But the algorithms do of course also work with arrays. ;-)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this