Multi-threaded PBO Question

Started by
1 comment, last by Grumple 10 years, 10 months ago

I'm working on an OpenGL video player that uses Media Foundation as my video frame source. I start one video frame capture thread per video source, with potentially multiple videos playing at once. Currently I dump incoming frames to a local buffer on the sub thread, then my main render thread runs through my video capture objects, locking access to the buffer, and calling glTexSubImage2D() to upload to texture. It renders each texture as a quad for display.

I'm intending to switch to using Pixel Buffer Objects. What I would LIKE to do, is have twice as many PBO's as video sources, so on any given frame I can be drawing from the previous frames' upload target, while starting an upload to the previous frames render source. To speed things up even more, I was intending to map all upload target PBO's, give a mapped memory pointer to each video capture sub thread to write frames directly to as they arrive, then unmap and upload on my next main-thread render pass.

However, it seems as though you can only have one PBO bound/mapped at a time. So, this means I cant leave mapped ptrs to multiple PBO's to multiple video capture threads at a time between my render. Is this correct?

As far as I can tell, I have one of two reasonable options:

I can forget having my video capture threads write directly to mapped PBO's. They can cache frame pixels in a standard local memory buffer, then my my thread can do two loops in its render function; One to render the previous frames PBO uploads, and one to start uploading newly cached video frame data to the PBO's that are not being rendered from. It seems this would still benefit from asynchronous texture uploads, but would also still be bottlenecking. For example, I'd still be wastefully caching video frames to RAM in sub threads before trying to upload them to PBO/texture memory, and my main thread would be responsible to do the memcpy into the mapped PBO memory.

The second method I though of, was to create two large PBO's that each contain space for one frame of all video sources. This way my main thread could bind the current render source PBO, loop calling glTexSubImage2D for each texture with appropriate offsets into the PBO and render these textures. Then it could map the current 'back buffer' PBO, and pass pointers to the correct frame offsets to the video capture threads for direct writes as they receive frame data. On the next pass I could un-map the 'back buffer' pbo, and repeat previous steps to upload via glTexSubImage2D, then map the previous frames render PBO as the sub thread frame destination, etc, etc.

Sorry for making this long winded but I want to get some general advice/input with a good description of what I'm up to. Is my general understanding correct? What are thoughts on trade offs between sub threads being able to perform the memcpy into mapped PBO memory vs having to use a single large PBO to get around only being able to have one mapped with multiple data source threads?

Cheers!

Advertisement

I did a similar system like that in the past. What I did is that in my background thread it just decode the frame and store it into an buffer. then send an event to my main thread letting it know it has a buffer available. Then that frame get send down to the main thread along with movie handle which contains an array of PBO. then that frame get inserted into one of the PBO. When it comes time to render I figure which frame the movie is on then grab the correct PBO and apply that to the image and draw. Once the frame changed, I marked that PBO index as invalid and when the thread sends the next event that PBO will be used. So to summarize you have two threads one that is doing Decoding and the main thread which uploads the decoding data to the PBO. then on your render function you just draw the correct pbo.

Hi BornToCode,

I thought i had replied to this but apparently it didnt commit for some reason. Thanks for the insight into your approach. I ended up doing the 'two large PBOs' method described above, and it worked reasonably well, but I ran into a bottleneck mapping a PBO for writes that eliminated most of my gains. Here is a new post I made regarding my updated implementation and current glMapBuffer woes:

http://www.gamedev.net/topic/644241-glmapbuffer-slow-even-after-orphaning-old-buffer/

I'm about to try a circular buffer of PBO's now to (hopefully) eliminate my stall, and will post my results in case it can help anyone else.

This topic is closed to new replies.

Advertisement