Jump to content

  • Log In with Google      Sign In   
  • Create Account

copy buffer to std::vector


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 Alessandro   Members   -  Reputation: 296

Like
0Likes
Like

Posted 25 August 2012 - 10:21 AM

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

..........................
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);
..........................

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, 25 August 2012 - 10:26 AM.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9669

Like
2Likes
Like

Posted 25 August 2012 - 10:29 AM

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.

#3 Alessandro   Members   -  Reputation: 296

Like
0Likes
Like

Posted 25 August 2012 - 11:44 AM

Thanks for the suggestion. I tried this but the resulting vector is empty Posted Image

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);


#4 SiCrane   Moderators   -  Reputation: 9669

Like
1Likes
Like

Posted 25 August 2012 - 11:47 AM

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.

#5 Alessandro   Members   -  Reputation: 296

Like
0Likes
Like

Posted 25 August 2012 - 11:56 AM

Ok, added a
myBuffer.resize(size1*size2*3);

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.

#6 SiCrane   Moderators   -  Reputation: 9669

Like
0Likes
Like

Posted 25 August 2012 - 12:02 PM

When did you resize it? Before or after the glReadPixels() call?

#7 Alessandro   Members   -  Reputation: 296

Like
0Likes
Like

Posted 25 August 2012 - 12:07 PM

Before. Here is my 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);


#8 SiCrane   Moderators   -  Reputation: 9669

Like
2Likes
Like

Posted 25 August 2012 - 12:19 PM

I just noticed that you're still binding a buffer. As I said before, you would specify the memory address of the vector instead 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.

#9 Alessandro   Members   -  Reputation: 296

Like
0Likes
Like

Posted 25 August 2012 - 02:52 PM

Thanks very much for the clarification, didn't really got in first instance that the buffer had to be unbound.

#10 rnlf   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 27 August 2012 - 12:39 AM

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:

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

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, 27 August 2012 - 12:40 AM.

my blog (German)


#11 rip-off   Moderators   -  Reputation: 8727

Like
0Likes
Like

Posted 27 August 2012 - 03:02 AM

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.

#12 rnlf   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 27 August 2012 - 04:45 AM

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?)

my blog (German)


#13 Bregma   Crossbones+   -  Reputation: 5440

Like
0Likes
Like

Posted 27 August 2012 - 05:14 AM

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?)

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.
Stephen M. Webb
Professional Free Software Developer

#14 rnlf   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 27 August 2012 - 05:26 AM

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, 27 August 2012 - 05:28 AM.

my blog (German)


#15 rip-off   Moderators   -  Reputation: 8727

Like
0Likes
Like

Posted 27 August 2012 - 05:26 AM

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.

#16 Alessandro   Members   -  Reputation: 296

Like
0Likes
Like

Posted 28 August 2012 - 06:39 AM

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...

#17 rnlf   Members   -  Reputation: 1185

Like
0Likes
Like

Posted 28 August 2012 - 06:42 AM

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. ;-)

my blog (German)





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS