glMapBuffer slow, even after orphaning old buffer?

Started by
8 comments, last by mattdesl 10 years, 10 months ago

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!

Advertisement

You're not the only person who's observed this.

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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?

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.

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

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?

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

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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

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

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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?

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

This topic is closed to new replies.

Advertisement