Handmade Hero » Forums » Code » Day 244: A perplexing problem with Win32CreateOpenGLContextForWorkerThread
Pacifist
5 posts
#13718 Day 244: A perplexing problem with Win32CreateOpenGLContextForWorkerThread
5 months, 2 weeks ago

I have been programming along with the series and up until this point and I hadn't encountered a problem that I couldn't debug on my own, but this one has me stumped.

Basically, the wlgCreateContextAttribsARB() call in Win32CreateOpenGLContextForWorkerThread() fails, breaking the whole texture download system. Using GetLastError() returns the code for ERROR_INVALID_OPERATION, which the OpenGL docs tell me means either the HGLRC for the shared context isn't valid, or the shared context was created on a different window device context to the one passed into wglCreateContextAttribsARB(). (Also a few other things that didn't seem relevent, but hereis the documentation in question).

I verified that the HGLRC for the shared context is valid, but strangely calling GetDC(Window) inside of Win32CreateOpenGLContextForWorkerThread() returns a different HDC that the GlobalDC. I believe this gives some indication of problem, but I cannot explain what exactly.

I have CS_OWNDC specified on the window class, which I thought was supposed to give your window a unique device context. I have also tried CS_CLASSDC to no avail. So, I am at a loss as to why different threads would return different HDCs when GetDC() is called.

Furthermore, this only seems to happen on worker threads in the high and low priority queues. Calling wlgCreateContextAttribsARB() is successful when called using the shared context on the main thread.

I tried transplanting the unaltered Handmade Hero, win32_handmade.cpp file into my codebase (as the platform layer I have written has diverged somewhat from this) however the problem persisted. More concerning is that I built the entire unaltered handmade hero codebase for day 244 and it too had the same issue. Obviously Casey doesn't have this issue in the episode.

So, that's that. I'm utterly clueless on how to proceed as this is beyond my meager knowledge of programming on windows. Clearly a problem to do with threading, but with even vanilla Handmade Hero having the same problem it suggests it is a problem with my computer specifically.

Thanks for reading, I hope my explanation was clear enough. Any thoughts?
mmozeiko
Mārtiņš Možeiko
1670 posts
1 project
#13721 Day 244: A perplexing problem with Win32CreateOpenGLContextForWorkerThread
5 months, 2 weeks ago

Shared contexts were very unreliable. Casey removed them later. If you can I suggest you continue with HH up to the point where Casey removes this and only then debug issue if it still fails.
Pacifist
5 posts
#13725 Day 244: A perplexing problem with Win32CreateOpenGLContextForWorkerThread
5 months, 2 weeks ago

Thank you, it's good to hear that this gets changed later on. My main fear was that this would spell the end of me programming along with the videos.

On the off chance anyone else has this issue I should mention that I have made a work around (sort of) by creating a HGLRC on the main thread and including it in the work queue structure. Then all the Win32CreateOpenGLContextForWorkerThread() function does is wglMakeCurrent() with that context.

Obviously this means that the low-priority queue can only have a single thread associated with it. Doing this has also caused all sorts of weird graphical issues, but it "works" well enough to continue I think.