New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OpenGL context created on wrong device #1381
Comments
OpenTK 4 uses glfw for context creation which if i recall correctly does not support choosing which GPU to create the context on. |
Oh sorry, I thought this was about OpenTK 4, in OpenTK 3 you should still be able to request a specific minimum version, which is what I would test first. |
Thanks for your comments. I added a bit more info in the OP. I am requesting the version 4.0 via the constructor to GLControl. |
Just so that I can be sure, how are you checking the version of the context you are getting? You might want to try using the |
I'm using the below snippet of code now to request and check the returned context OpenGL version. When I was debugging this issue I added the calls to I hadn't seen the referenced
|
Oh I see, I missed that you where using GLControl. I don't know if that code handles that, but let me know what you find out :) |
I cant seem to find where the If this is the case I don't think I can use this fix, since I cant control where the Below is the code path I think is used on windows. opentk/src/OpenTK/GameWindow.cs Lines 184 to 188 in 803d8ba
opentk/src/OpenTK/NativeWindow.cs Lines 74 to 77 in 803d8ba
opentk/src/OpenTK/NativeWindow.cs Line 90 in 803d8ba
|
The answer @l_belev gives at that link is interesting, and I suspect it works, but it's probably not obvious why it works; it delves pretty deep into Win32 to do what it does. The core idea of it, though, is to force OpenGL to pick up the correct driver before OpenGL actually starts by hacking the Windows DLL-loading process. Windows won't reload a DLL that's already in your process space, and the Microsoft implementation of OpenGL, it seems, won't attempt to load a GPU driver if there's one already loaded in memory as well. So the code there forcibly creates a Device Context (HDC) that requires OpenGL on the desired display device, which causes the correct GPU's driver to be loaded if it's not already in memory. Then it immediately destroys the HDC, leaving the GPU's DLL still sitting in memory, so that when you then use OpenGL afterward, OpenGL picks up the already-loaded DLL for the desired GPU instead of loading whichever one it otherwise would. Critically, this is a one-shot operation: You get to pick which GPU your program uses exactly once, upfront, and once that driver is loaded, none of the others will be. Clever hackery. It works, but it's not something you'd want to rely on, since anything could make an OpenGL context in your process space before your program gets a chance to. This functionality is most definitely not built into OpenTK, or built into GLFW to my knowledge either. If you want to do it from C#, you're going to need a lot of |
Description
I am getting an exception
System.AccessViolationException at OpenTK.Graphics.OpenGL4.GL.CreateShader(OpenTK.Graphics.OpenGL4.ShaderType)
.I have tracked down the root cause of this exception to the OpenGL context being created on a device which only supports OpenGL 1.1, even though the system has a GPU which supports OpenGL 4.0.
Edit:
I did request a version 4.0 using the below code, however it returned a context with version 1.1.
I have found a few references to this issue for OpenGL in general, but none related directly to OpenTk.
The below link has a suggested workaround, but I don't understand it enough to try to implement it.
https://stackoverflow.com/questions/62372029/can-i-use-different-multigpu-in-opengl
Is there any feature in OpenTK to allow targeting a specific device (GPU) when creating an OpenGL context?
Related information
Windows 7
Winforms (using GLControl)
OpenTK 3.1.0.0
.Net Framework 4.8
Workaround
The issue can be worked around by setting the primary display device to a monitor which is connected to the target GPU. The OpenGL context is created on that device, preventing the exception.
The text was updated successfully, but these errors were encountered: