giawa / opengl4csharp

OpenGL 4 Bindings (partially based on OpenTK) for C#
Other
234 stars 61 forks source link

ShaderProgram.Use() fails under multiple contexts. #64

Closed harmonypiano closed 2 years ago

harmonypiano commented 2 years ago

When ShaderProgram.Use() is called under 2nd context, Gl.UseProgram(this.ProgramID) would not be called, as the ProgramID is the same as in the 1st context. But ProgramID is actually context-specific, Gl.UseProgram(this.ProgramID) still needs to be called under 2nd context even if the ProgramID is the same.

giawa commented 2 years ago

Thanks for writing in with this bug. I have never personally used multiple contexts, so it is very likely that none of the Constructs in this OpenGL library support multiple contexts out of the box. I imagine the issue is this line here:

if (Gl.CurrentProgram != ProgramID) Gl.UseProgram(this.ProgramID);

Gl.CurrentProgram does not have any awareness of what context it is being called from. The simplest fix is to probably update ShaderProgram.Use to remove that check and recompile, or just use Gl.UseProgram(this.ProgramID) as you suggested. I don't think there is a way for me to make an OpenGL call to find out which context the code is being called from. It would have been cool if the OpenGL driver assigned a unique program id that was unique across all contexts, then this wouldn't have happened.

Do you have a suggestion as to how the library code could be changed to support your use case?

harmonypiano commented 2 years ago

Yes, I think just removing the check will do. Since normally we use more than one ShaderProgram in a render, we switch around the ShaderProgram anyway. Another way is to use WGL GetCurrentContext to get the context during ShaderProgram construction, however this is platform specific, so can't apply here.

giawa commented 2 years ago

Sorry for the delay in taking care of this. I have removed that check and hopefully there will be no issue with multiple contexts. Thanks for the bug report, and I hope you have a happy new year.