Sign in to follow this  
yaooo

How to share texture id in different thread

Recommended Posts

yaooo    122
i made a program with two threads. The main thread renders user interface, and the second thread renders images for computing. Because the threads cannot share HGLRC, so i create different CDC* and HGLRC for different threads. But then i find the texture binding does not work in the second thread. And only the texture does not work. Who can help me?

Share this post


Link to post
Share on other sites
yaooo    122
Thank Kazade very much!!! I create one CDC* and two GLHRC in my renderer, share the list using wglShareLists and switch GLHRC in different thread. Now it works!

Share this post


Link to post
Share on other sites
yaooo    122
Also I have another question. The glReadPixels function is very slow, especially for reading single channel. For example,

glReadPixels(0, 0, 256, 256, GL_LUMINANCE, GL_UNSIGNED_BYTE, pGray);
this calling takes about 55 millisecond.

glReadPixels(0, 0, 256, 256, GL_BGR, GL_UNSIGNED_BYTE, pRGB);
this takes only 5 millisecond.

How are they different so much?

Share this post


Link to post
Share on other sites
lc_overlord    436
It differs not so much on what you read but where as glReadPixels stalls the pipeline.
The only sure way of reading the actual speed if these reads is to call glFinish first, get the first time, call glReadPixels, get the second time and compare them.
Either way, it's much better to use FBOs for that kind of work.

Also to answer your earlier question, yes what Kazade said does work, but what you should be pondering is, can you do without two rendering threads?
You still only have one hardware resource (even in SLI mode) and all render threads are horribly linear, this means that when one is rendering the other one is not.

Share this post


Link to post
Share on other sites
yaooo    122
I tried calling glFinish before glReadPixels, but it seems no difference between with and without the calling.

My application is programed to do lots of computation, and some of the computation require OpenGL to accelerate generating images. So I use multi-thread to avoid the application "dead". I've thinking about using one renderer but have no idea how to do.

I haven't used FBO before, and I'll read some documents if it helps.

Thanks for your reply!

Share this post


Link to post
Share on other sites
lc_overlord    436
While i don't know what kind of stuff your doing, i can speak in general terms.
Rendering is usually done in 3 stages.
1. precomputation
2. Prerendering
3. final rendering

The first one is done entirely on the cpu and can quite preferably be done in it's own thread or in several threads, it can also be done one frame in advance, so that you do the precomputation for the next frame at the same time as you do all the rendering.
Prerender and final render is done on the same hardware resource so multi threading is a moot point, all rendering stacks in a buffer before it's actually rendered, that's why it's important that whatever you do do not stall the GPU.
There are 3 calls that does this in openGL besides not having enough content to render that is, they are.
glFinish
glReadPixels
swapbuffers

So as long as you avoid them you should be fine.
But once it's stalled you can't stall it again immediately, so it's not uncommon to call readpixels before swapbuffers if you absolutley need to.

FBOs avoid stalls and allows you to render directly to a texture, i don't know if you can use them effectively depending on your use of them, but i'm pretty sure you can adapt your algorithms around this.

Share this post


Link to post
Share on other sites
yaooo    122
Let me explain my work.

My goal is to synthesis individual face model from one photo. Which means the final rendered image will be the same with the input photo. To achieve this, the program have to iterate and modify parameters of renderer and model. The rendered images are refference for modifying, that's why I need calling glReadPixels. And the iteration needs at least hundreds times of calling and will makes program "dead". That's why I need multi-thread, and can see the progress and stop iteration at any time.

In fact, OpenGL is more fast than computing rendered image by CPU. But I just think it should be faster. ^_^

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