🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How to share texture id in different thread

Started by
7 comments, last by yaooo 16 years, 2 months ago
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?
Advertisement
I *think* (although it's been a long time since I've done this) you need to use wglShareLists

I also believe you have to call this function before you call makecurrent on either of the threads.
Member of the NeHe team.
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!
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?
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.
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!
By all means use a multi-threaded application. But keep all your openGL rendering ON ONE THREAD.


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.
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. ^_^

This topic is closed to new replies.

Advertisement