Sign in to follow this  

OpenGL glMapBuffer slow, even after orphaning old buffer?

This topic is 1675 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Edited by mhagain

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Similar Content

    • By _OskaR
      Hi,
      I have an OpenGL application but without possibility to wite own shaders.
      I need to perform small VS modification - is possible to do it in an alternative way? Do we have apps or driver modifictions which will catch the shader sent to GPU and override it?
    • By xhcao
      Does sync be needed to read texture content after access texture image in compute shader?
      My simple code is as below,
      glUseProgram(program.get());
      glBindImageTexture(0, texture[0], 0, GL_FALSE, 3, GL_READ_ONLY, GL_R32UI);
      glBindImageTexture(1, texture[1], 0, GL_FALSE, 4, GL_WRITE_ONLY, GL_R32UI);
      glDispatchCompute(1, 1, 1);
      // Does sync be needed here?
      glUseProgram(0);
      glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
      glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                     GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[1], 0);
      glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
       
      Compute shader is very simple, imageLoad content from texture[0], and imageStore content to texture[1]. Does need to sync after dispatchCompute?
    • By Jonathan2006
      My question: is it possible to transform multiple angular velocities so that they can be reinserted as one? My research is below:
      // This works quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); quat quaternion2 = GEMultiplyQuaternions(quaternion1, GEQuaternionFromAngleRadians(angleRadiansVector2)); quat quaternion3 = GEMultiplyQuaternions(quaternion2, GEQuaternionFromAngleRadians(angleRadiansVector3)); glMultMatrixf(GEMat4FromQuaternion(quaternion3).array); // The first two work fine but not the third. Why? quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); vec3 vector1 = GETransformQuaternionAndVector(quaternion1, angularVelocity1); quat quaternion2 = GEQuaternionFromAngleRadians(angleRadiansVector2); vec3 vector2 = GETransformQuaternionAndVector(quaternion2, angularVelocity2); // This doesn't work //quat quaternion3 = GEQuaternionFromAngleRadians(angleRadiansVector3); //vec3 vector3 = GETransformQuaternionAndVector(quaternion3, angularVelocity3); vec3 angleVelocity = GEAddVectors(vector1, vector2); // Does not work: vec3 angleVelocity = GEAddVectors(vector1, GEAddVectors(vector2, vector3)); static vec3 angleRadiansVector; vec3 angularAcceleration = GESetVector(0.0, 0.0, 0.0); // Sending it through one angular velocity later in my motion engine angleVelocity = GEAddVectors(angleVelocity, GEMultiplyVectorAndScalar(angularAcceleration, timeStep)); angleRadiansVector = GEAddVectors(angleRadiansVector, GEMultiplyVectorAndScalar(angleVelocity, timeStep)); glMultMatrixf(GEMat4FromEulerAngle(angleRadiansVector).array); Also how do I combine multiple angularAcceleration variables? Is there an easier way to transform the angular values?
    • By dpadam450
      I have this code below in both my vertex and fragment shader, however when I request glGetUniformLocation("Lights[0].diffuse") or "Lights[0].attenuation", it returns -1. It will only give me a valid uniform location if I actually use the diffuse/attenuation variables in the VERTEX shader. Because I use position in the vertex shader, it always returns a valid uniform location. I've read that I can share uniforms across both vertex and fragment, but I'm confused what this is even compiling to if this is the case.
       
      #define NUM_LIGHTS 2
      struct Light
      {
          vec3 position;
          vec3 diffuse;
          float attenuation;
      };
      uniform Light Lights[NUM_LIGHTS];
       
       
    • By pr033r
      Hello,
      I have a Bachelor project on topic "Implenet 3D Boid's algorithm in OpenGL". All OpenGL issues works fine for me, all rendering etc. But when I started implement the boid's algorithm it was getting worse and worse. I read article (http://natureofcode.com/book/chapter-6-autonomous-agents/) inspirate from another code (here: https://github.com/jyanar/Boids/tree/master/src) but it still doesn't work like in tutorials and videos. For example the main problem: when I apply Cohesion (one of three main laws of boids) it makes some "cycling knot". Second, when some flock touch to another it scary change the coordination or respawn in origin (x: 0, y:0. z:0). Just some streng things. 
      I followed many tutorials, change a try everything but it isn't so smooth, without lags like in another videos. I really need your help. 
      My code (optimalizing branch): https://github.com/pr033r/BachelorProject/tree/Optimalizing
      Exe file (if you want to look) and models folder (for those who will download the sources):
      http://leteckaposta.cz/367190436
      Thanks for any help...

  • Popular Now