PDA

View Full Version : Textures and Multithreading



Xavierkj
08-12-2008, 08:18 AM
Hi,

In my application I need to get the texture that I have created in one thread in some other thread. Is there any way to get the same.? Can anybody provide a sample code for it.?

yooyo
08-13-2008, 02:56 AM
Just add message subsystem in your render loop/thread. Once in loop check message queue and if there is a some job like "upload this data to texture" do that. In the other thread, prepare data and post message to render thread "upload this data to texture". Just be careful to not delete data before you process message in render loop (thread).

The other way is to create additional opengl context, share objects between them and upload texture using standard gl calls.

skalogryz
08-25-2008, 04:39 AM
The other way is to create additional opengl context, share objects between them and upload texture using standard gl calls.

What gl calls can be considered texture uploading?
glTexImage... it actually copies the texture information to the gl, AFAIK, it's not promised, that texture is actually loaded to the GPU.

Should PBOs be used, to make sure, that texture data is actually at VRAM?!

It's important, because, otherwise there would be little sense in using shared contexts! Because on the fist time texture drawing, the programm will be stalled for a moment, to upload the texture (previosly glTexImaged()...) from RAM to VRAM.

muvee
08-27-2008, 12:35 AM
When you create the threads, create a render context in each thread and use wglShareLists()?

This method only works with quite recent graphics cards...

Zengar
08-27-2008, 06:30 AM
This method only works with quite recent graphics cards...

Why that?

skalogryz
09-01-2008, 05:29 AM
I'm using 3 threads on the application.

the main thread does have a timer, that counts each second and takes number of frames drawn from the second thread

the second thread (that creates gl context), is a actually does all the drawing. it draws the scene, flushes it, and calls swapbuffer.
while drawing a scene (a single polygon) 2nd thread always check a flag, about texture loaded and 3d thread

the third thread (has shared from 2nd thread gl context) reads texture file, generates a texture object and uploads texture data using glTexImage2D. after that it sets that flag, for the 2nd thread that texture is ready to use.

Once, 2nd thread reads the flag set, it draws another polygon using the texture, uploaded by the 3d thread

At the first time the uploaded texture is about to drawn, I see a small fps rate drop (or sometimes even significant).
Why does this happen? Is it possible to upload the texture to GPU, so it can be used without any time penalty for the drawing thread?

skalogryz
09-03-2008, 12:40 PM
Unfortunatelly OpenGL, while being cross-platform library, differs with it's implementation in each OS. :(

The best way to avoid first usage time-penalty of uploaded texture, is to draw a primitive using the uploaded thread, at first in the loading thread.

Let's say we have 2 threads (infact 3, just like in example before);
so we have two shared contexts. One for drawing, and another for uploading.
So, then loading thread has uploaded the texture using glTexImage (or glTexSubImage), it's good practice to draw a primitive (a triangle or quad) using the texture.

All GL implementation, allows to select what context is actually perform drawing. Since shared context for loading has no rights to draw, it will not mess the framebuffer. But, since drawing is still required, the OpenGL will upload the texture to the GPU.

After that, loading thread can signal to the drawing thread, that texture has been uploaded! And using it at the drawing thread will not take any time penalty.

muvee
09-03-2008, 08:30 PM
This method only works with quite recent graphics cards...

Why that?


I remember that several months ago, we tried to do something like having a separate thread doing the IO & Texturizing job while a different real-time rendering thread, using wglShareLists(). We hope doing this could improve the rendering performance and result in a better frame rate. In fact we achieved it on some quite recent cards, e.g. Quadro FX 550/540.

However, it failed on at least the following cards:
ATI RADEON XPRESS 200
GeForce MX 440
ATI MOBILITY RADEON X300

Maybe something wrong in our code. Maybe something due to the hardware/driver limitation. We don't know yet.