I have a multithreaded application.
All calculations are performed in threads and then in the main thread the opengl commands are executed.
This includes uploading textures. The odd thing is, that most of the time this texture uploading takes 0.003-0.005 seconds but occasionally it takes 0.012 and more seconds to upload. This makes animations going a bit choppy.
So I was wondering: is it possible somehow to upload textures from a different thread? Or is there a common explanation for this irregularity?
Please note that the texture is always the same size, comes from the same memory location and that always the same opengl commands are executed (translate/rotate/glTexSubImage2D/create quad). Also in my testcase no other threads are running.
Perhaps using PBO with orphaning might help.
Thanks. Can you elaborate? Or maybe even have an example?
Of course I googled for it (“pbo with orphaning”) but that gives mostly slightly unrelated hits.
The first hit on the Google will help you. Just enter “PBO OpenGL”. It is a Songho tutorial.
PBO may hide part of the transfer time if you have something to do on the CPU in the meanwhile, but don’t expect miracles.
Considering other questions…
Of course, but there is no guarantee it would prevent that irregularity. You need to make another rendering context in the same sharing group as the main (drawing) context, and use it for updating the texture. That requires precise synchronization, since you shouldn’t use the texture while it is updating (use sync object in OpenGL 3.2+).
No. You have to measure both CPU and GPU time in order to find where the problem is. On the other hand, who knows what your multitasking OS is working behind the curtains. I had a very strange problem with the application that worked extremely fluently on XP but had jerky camera motion on Win7 on the same machine. The problem was probably in fetching data from the hard disk that blocked updating GPU’s memory and vice versa. Who knows…
This link describes a scenario that looks like yours.
Regarding PBO and orphaning, its just that you perform a glBufferData(…) with Data pointer equal to NULL(orphaning). By doing this you tell the driver that you dont care about the “current” data thats in the PBO. The driver may then give you a temporary copy of the PBO just to avoid GPU blocking if its currently using it. But like Aleksandar says, dont expect it to solve your issue…