View Full Version : textures and threads

John Stewart
02-23-2006, 07:20 AM
Hi all;

I have a threaded application that I would like to change so that textures are created in 1 thread, and used in another.

It kind of works, but, although the texture info appears correct for both threads (glGetTexLevelParameteriv calls), the texture appears to not be.

Main thread, which "consumes" textures, has:
GLcx = glXCreateContext(Xdpy, Xvi, 0, GL_FALSE);

The creator thread does:

textureContext = glXCreateContext(Xdpy, Xvi, GLcx, GL_FALSE);
glBindTexture (texnum);
then signals that the texture is ok.

This method, according to what I can see on-line, should be thread safe.

The textures are kind of written; but both Linux and OS X show the textures incorrect; if they come in as images of say 128x128, RGB, then they are displayed, if not, then the image is "skewed" or some other corruption.

The application is FreeWRL - an X3D/VRML Browser; the major threading code is old; the producer used to read data to a malloc'd bit of code to be glTexImaged (etc) in the consumer thread.

Any thoughts??

02-23-2006, 07:33 AM
Not that I expect this to solve the problem, but what if you explicitly bind texture zero (aka none) after uploading the texture data but before signalling the main thread "Go ahead, use it"?

John Stewart
02-23-2006, 08:06 AM
glBindTexture to 0 after uploading, but before telling the main thread that the texture is there does not affect anything on Linux or OS X.

Thanks for the thought, though...

More investigation:
ok; TEXTURE_INTERNAL_FORMAT on OS X is 0x8058, which is GL_RGBA8 (linux returns "3" which does not make much sense...) in both one-context-thread code, and in my multithreaded example.

If I send in a 4 element PixelTexture (VRML-code) it "kind of" works better on OS X and Linux - the colors are off, but at least
it displays better.

I can understand storing textures as RGBA8; but why would the glTexImage2D that works when only one OpenGL context is there, fail when the contexts are shared?

John Stewart
02-23-2006, 08:54 AM
I think I found it...

On my second context, I had not set glPixelStorei(GL_PACK_ALIGNMENT,1), etc.

I don't know whether to say "sigh" or "arrrgh". I have spent a couple of days at this; at least my knowledge of OpenGL and texturing is *much* better than it was beforehand.

I'm off to thoroughly test this on a few platforms, with quite a few textures.