Sign in to follow this  
Grumple

OpenGL glMapBuffer slow, even after orphaning old buffer?

Recommended Posts

Grumple    177

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
mhagain    13430

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
Grumple    177

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
Promit    13246

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
Grumple    177

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
mhagain    13430

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
Grumple    177

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
mhagain    13430

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
Grumple    177

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

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  

  • Similar Content

    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
  • Popular Now