Sign in to follow this  
skow

Loading textures in a win32 thread?

Recommended Posts

I’m trying to create a threaded texture manager. When I want to load a texture on the fly, I spawn a thread, load the texture in that thread, and then kill the thread. Is there any way to give the thread the same context as the parent program? That way I don’t have to create a new context, and when the thread dies the contetx/textures are killed too.

Share this post


Link to post
Share on other sites
Quote:
Original post by skow
Is there any way to give the thread the same context as the parent program? That way I don’t have to create a new context, and when the thread dies the contetx/textures are killed too.

There's ::wglShareLists() but it only works for 'merging' contexts of single process... perhaps you could modify your manager so the separate thread just loads/prepares the data as needed, and then the main program builds the texture out of it when it finds it convenient..?

Share this post


Link to post
Share on other sites
I have tried doing just the loading on the process, and there was too long of a pause when creating a texture from that data, so I really need to find a way to both load and create on the thread.

Share this post


Link to post
Share on other sites
I had the same problem a while ago but everybody said its impossible, so i split the loading into memory and the linking to opengl part and it works for me.
A second thread creates a second rendering context which does not share anything.

Aidamina,

Share this post


Link to post
Share on other sites
Quote:
Original post by Aidamina
so i split the loading into memory and the linking to opengl part and it works for me.
Aidamina,


I'm sorry, i dont understand, could you explain it a little please?

Share this post


Link to post
Share on other sites
Well create a thread that loads the image file.

And let the w32 main thread check if its done while it loops
after that do the opengl calls. Like these:



glGenTextures(1, &TextureList[id].texID);
glBindTexture(GL_TEXTURE_2D, TextureList[id].texID);
glTexImage2D(GL_TEXTURE_2D, 0, TextureList[id].bpp / 8, TextureList[id].width, TextureList[id].height, 0, TextureList[id].type, GL_UNSIGNED_BYTE, TextureList[id].imageData);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);



Note this code wont work on your program since i copy pasted it from my own texture manager.

fixed your code tags as it was breaking the forums width, please be more carefull in future

[Edited by - _the_phantom_ on June 26, 2005 6:37:49 AM]

Share this post


Link to post
Share on other sites
Opengl is not thread safe by default. So texture creation is likely to fail.
Cant rember how to make ogl thread safe though cause I gave up.

Texture creation is usually done in a preeprocess phase anyway though and stored as compressed DXT* dd surfaces.

Share this post


Link to post
Share on other sites
Quote:
Original post by skow
Ahh that is what im currently doing aidamina, im still not happy with it :(


Why? You do realize that seperate threads are for CPU, not GPU, right? You can't except from the card to render a sphere in one thread and carry out a glTexImage2D() command in the other.

Share this post


Link to post
Share on other sites
Quote:
Original post by skow
Quote:
Original post by Aidamina
so i split the loading into memory and the linking to opengl part and it works for me.
Aidamina,


I'm sorry, i dont understand, could you explain it a little please?


You've said you tried that in a previous post - load the texture and have the raw data ready to link in your OpenGL context.

Share this post


Link to post
Share on other sites
I also had big issues with getting texture uploads working via threads. It seems as this is impossible to do since windows seem to tie the rendering context to the thread that owns the window :(

In my case I wanted a simple "progress bar" or rather let the user know the game is loading and is not dead.

Basically the code is like this:

Start OpenGL
Load
Load
Load
Load
...
Game Loop

What I did was insert a simple callback construct (via Observer pattern) in my file class, that was called just before a file was loaded. In the callback I simply stepped an animation and flipped. This way a simple animation and some text is displayed when the game is loading.

This performes ok in my case since most files are rather small and loaded rather quickly. You probably need a much more elaborate sollution if you load resources from a network or want seamless "ingame" loading.

One thing to note is that most games seem to have this type of "jerky" progress animations, making atleas me believe that they do not use some elaborate threading technique.

Regards,
hObbE

Share this post


Link to post
Share on other sites
Quote:
In my case I wanted a simple "progress bar" or rather let the user know the game is loading and is not dead.

textures dont take that long to load IIRC (on my comp) 2048x2048 is less than 0.01 second thus in this case theres no need to have a seperate thread.

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