\$10

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net's newsletters to receive the latest updates and exclusive content.

## glMapBuffer slow, even after orphaning old buffer?

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.

9 replies to this topic

### #1Grumple  Members

Posted 13 June 2013 - 06:57 AM

Hi,

I'm working on an OpenGL video player implementation,   I'm playing multiple videos simultaneously through textures.  I create 2 PBO's, each large enough to contain one frame from each video.   On any given update, I have one PBO mapped and writing new frames to it asynchronously, while the other is unmapped and used as a source for glTexSubImage2D to upload previously written data to textures.  On alternate frames, the PBO's get swapped (ie old mapped pbo becomes texture upload source, old texture upload source gets mapped for writing).

I had read about the glMapBuffer sync issues, and stalling the CPU, so I used the 'orphaning' technique.  I  call glBufferData with a NULL ptr to inform OpenGL I dont need the old buffer memory just before mapping it.  According to my research, I should get a new buffer quickly without forcing synchronization with the render pipeline that may still be using the old buffer.

Here is the problem:  glMapBuffer is still slow!   The PBO is admittedly semi-large-ish (ie 22MB), but I am running on modern hardware and am seeing average glMapBuffer calls stalling for 10ms.   Is this normal?  Are there other ways to quickly execute this call?

Thanks!

Edited by Grumple, 13 June 2013 - 07:01 AM.

### #2mhagain  Members

Posted 13 June 2013 - 07:14 AM

You should try using glMapBufferRange instead; it gives you finer-grained control over synchronization, etc.

Alternatively, try a regular glTexSubImage without the PBO - the real key to performance here is getting the parameters correctly matching the internal storage format of the texture, so that the driver can do a DMA transfer without having to go through any intermediate software steps.  Commonly that means using GL_BGRA for format and either GL_UNSIGNED_BYTE or GL_UNSIGNED_INT_8_8_8_8_REV (your mileage may vary depending on your GL implementation) for type.  Whatever else, never ever ever use GL_RGB for format - even if you think it's saving memory - see http://www.opengl.org/wiki/Common_Mistakes#Texture_upload_and_pixel_reads and http://www.opengl.org/wiki/Common_Mistakes#Image_precision for more info on this.

This may well be more than fast enough without the PBO.  If you have (or are willing to use) GL4.3 you may also consider using glInvalidateTexSubImage to get even more control.

Edited by mhagain, 13 June 2013 - 07:15 AM.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #3Grumple  Members

Posted 13 June 2013 - 07:29 AM

Thanks for the tips, mhagain.  I will see about trying glMapBufferRange, and would consider upgrading to OpenGL 4.3 if glInvalidateTexSubImage would alleviate this problem.  The implementation is very specialized and doesnt need to work on a wide variety of (older) hardware.

I have actually come to this point from using raw glTexSubImage calls.  They are unfortunately slower than using the PBO's.   What takes about 11-12 ms through glMapBuffer + glTexSubImage2D is closer to 18 ms when avoiding PBO and just performing the straight glTexSubImage2D calls.

I am using GL_BGRA for exactly the reasons you described.  In particular I read the PBO's hate GL_RGB.  However, interestingly enough, I tried GL_RGB when using the straight glTexSubImage2D calls (my source video frame data is 24 bit so I figured Id try it to avoid the client side conversion to 32-bit).  In my tests, using glTexSubImage2D for a 32 bit GL_BGRA image was actually 25% slower than the 24-bit, so I guess it was more limited by transfer bandwidth than pixel format conversion speed?

Edit:

As an update to this, after doing some additional reading, I wonder if I would benefit from having more than two PBO's?  It seems that maybe creating 3-6 in a circular buffer pattern might reduce the chances of a sync stall when mapping a buffer for writing that hasn't been used for 2-5 frames?

Edited by Grumple, 13 June 2013 - 08:03 AM.

### #4Promit  Senior Moderators

Posted 13 June 2013 - 09:40 AM

A few comments. First, when doing the orphaning thing you're going to be better off double buffering, which means three to four for you. Second, I've seen glMapBufferRange go berserk on AMD Crossfire and fall back to single GPU so watch out for that if it's relevant.

Edited by Promit, 13 June 2013 - 09:41 AM.

SlimDX | Shark Eaters for iOS | Ventspace Blog | Twitter | Proud supporter of diversity and inclusiveness in game development

### #5Grumple  Members

Posted 13 June 2013 - 09:49 AM

Hi Promit,

Thanks for the suggestions, but can you elaborate on what you mean by 'three to four' for double buffering using the orphaning approach?

I'm starting to wonder if maybe I'm better off creating half a dozen PBO's and cycling through them without ever using the orphan call at all.  The idea being that I create enough PBO's to guarantee that by the time I need to map PBO N, it it's contents are 5 frames old and hopefully not in use by the renderer.  Any thoughts on this?

### #6mhagain  Members

Posted 13 June 2013 - 02:15 PM

I am using GL_BGRA for exactly the reasons you described.  In particular I read the PBO's hate GL_RGB.  However, interestingly enough, I tried GL_RGB when using the straight glTexSubImage2D calls (my source video frame data is 24 bit so I figured Id try it to avoid the client side conversion to 32-bit).  In my tests, using glTexSubImage2D for a 32 bit GL_BGRA image was actually 25% slower than the 24-bit, so I guess it was more limited by transfer bandwidth than pixel format conversion speed?

There's something else wrong if you're getting this kind of behaviour, because using GL_RGB is exactly what will cause the driver to do a client-side conversion (I've even seen one GPU vendor where the driver behaved as though it read the entire texture back from GPU memory in order to do the update when GL_RGB is used - you can probably guess which).  No GPU will store textures as 24-bit data internally; when you specify a 24-bit internal format you're actually going to get 32-bits with the extra 8 unused.  Refer to those "common mistakes" links I posted earlier for more info on that.

If your source data is 24-bit, then you normally should get better performance by converting to 32-bit BGRA yourself rather than letting the driver do it for you, and my hunch is that you may have been allocating and then freeing a system memory buffer to do this conversion into each frame (do it one-time-only at startup instead).

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #7Grumple  Members

Posted 14 June 2013 - 06:38 AM

If your source data is 24-bit, then you normally should get better performance by converting to 32-bit BGRA yourself rather than letting the driver do it for you, and my hunch is that you may have been allocating and then freeing a system memory buffer to do this conversion into each frame (do it one-time-only at startup instead).

I agree 100% that I would expect to get better performance passing 32 bit textures, but that just doesn't seem to be the case.  Passing 24 bit (it was actually GL_BGR) texture data to glTexSubImage2D was faster than GL_BGRA.  vOv

I will review my texture formats, etc but I am 99% certain I am using the OpenGL preferred formats everywhere.

Also, I do allocate a RAM buffer each time I perform a video frame conversion in order to simplify my threading model (new frames are allocated, and given to the main thread for processing, such that I never have to mutex access to a shared, persistent buffer).   However, this alloc is very cheap, and unrelated to my glMapBuffer performance (PBO's are allocated once and persist).  I'm timing my specific calls with a profiler, so I am 100% certain the gl calls are the point of slowdown.

Related to all this, I did a new test last night whereas I created a ring buffer of PBO's (tested creating 2-6 of them), and tried glMapBufferRange, as well as various combinations of using the orphaning technique, etc.   All I've managed to do is make my performance unpredictable.   In any given run, glMapBuffer/glMapBufferRange would report taking an average of anywhere from 1 to 14 ms.   As far as I can tell it was random whether it executed quickly or not.  I am still seeing stutter in my frame rate though so at some level the data upload is still taking too long.

At this point I can't imagine where my stall is coming from.  Surely calling glMapBuffer or glMapBufferRange (with flags to discard and access without synchronization) on a PBO that hasn't bee used in five frames should execute without a stall?!  =P

### #8mhagain  Members

Posted 14 June 2013 - 07:16 AM

Is it possible that you may be reading from a buffer mapped as write-only?  This can happen, yes, and it can be fairly insidious: otherwise totally innocent looking code can cause it.

a = b = 0;


This will cause a read from a mapped buffer if "a" and "b" are relating to contents of the buffer.

*((int*) data) = 0;


This will also cause a read from a mapped buffer because it generates the following asm:

xor [eax],0


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #9Grumple  Members

Posted 14 June 2013 - 10:23 AM

No, I don't think I am inadvertently reading from the mapped write buffer, although that is a really good tip.....I'll have to remember that for the future.

I've been thinking about my implementation and might have a few trouble maker candidates.

The first is that at the end of any given render frame, I map a buffer and allow sub threads to write to it for (up to) the duration of the frame.  Then at the end of the next render frame if writing is signaled as complete, I unmap it, and immediately call my glTexSubImage2D to ask ogl to start transferring its contents to texture(s).    I wonder if I should be deferring this and allowing some time (ie 1 frame) between my unmap and my call to glTexSubImage2D?  I had assumed OGL would handly this nicely internally on its own but now i'm not feeling so sure of how the texture access is handled.  This leads me to my next question...

Has anyone tried creating multiple sets of destination textures and copying to/rendering from different sets each frame?  I wonder if I could see an improvement in performance if I mirror my 'back buffer' pbo's with 'back buffer textures'?   For example:

Init:

- init texture_A through PBO_A

Frame 1:

- Render texture_A

- Map PBO_B, copy data into it, unmap it

- Init transfer to texture_B

Frame 2:

- Render texture_B

- Map PBO_A, copy data into it, unmap it

- Init transfer to texture_A

Frame 3:

- Repeat Frame 1

Any thoughts?

### #10mattdesl  Members

Posted 17 June 2013 - 08:25 AM

There is some useful information and code here:

http://www.java-gaming.org/topics/opengl-lightning-fast-managed-vbo-mapping/28209/msg/255576/view.html#msg255576

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.