Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Strange (and new!) openGL multithreaded behavior (Mac)

This topic is 2122 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,


I have some code that worked last time I used it.  I have recently come back to it, and without changing anything at all, it behaves oddly.


First off, it's OpenGL-- loading textures in a secondary thread so that a progress bar can display during load, and so that lower priority graphics can continue to load while the player ponders the menu screens.


Before I attempt to load anything, I call this:

void EnableThreadGraphics()
    if (gOpenGLThreading==0)
        CGLError aError=(CGLError)0;
        if (aError!=kCGLNoError) printf("*** Enabling Multithreaded OpenGL not supported ***\n");
        else printf("*** OpenGL Multithreading started... ***\n");
        CGLContextObj aCurrentContext;

...and that is working correctly.  I.E. I get the right message printed out.
That call gets called first in the main thread (meaning gOpenGLThreading is zero then, so it enables the multithreading) and then gets called in the loading thread, at which time it only hits the last two lines, since it's not the first enabling.


Late, I call, simply:

	GLuint aTexture;

...and glGenTextures crashes *exactly* as it would if I had not enabled the openGL context.


(A further note: Yes, I am thread locking with GLLockContext during draws and other concurrent operations, to make sure they don't step on eachother's toes)


Fiddling around, I found that if I put this:


...right before glGenTextures, it will not crash, but all my textures come in pure white.


Can anyone shed any light on this?  I'm tearing my hair out here, especially since this code worked just fine, exact same code, a month and a half ago.  No idea what the difference is here.



Share this post

Link to post
Share on other sites

What is CGLEnable(gOpenGLContext,kCGLCEMPEngine); doing, or supposed to do? You enable a feature with the GL enumeration constant equal to the current context handle? Or something? What's that supposed to do?


Also, you seem to use only a single context which you make current in your loader thread. This does not make much sense, and it is probably the reason for the crash, too. To parallelize loading textures, you either only do OpenGL calls from one thread and do the disk work and shuffling data into buffer objects in another thread (passing mapped buffer object pointers between threads, never any actual OpenGL object!), or you use two shared contexts, one bound to each thread.

You will never want to do anything else, because not only will it not be running concurrently (even though adding complexity due to multithreading) but also because it will likely end up with one thread calling an OpenGL function without a valid context at some point.

Edited by samoth

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!