stetre / moongl

Lua bindings for OpenGL
Other
123 stars 14 forks source link

Doesn't work with multiple concurrent GL contexts #4

Closed stetre closed 4 years ago

stetre commented 4 years ago

See https://github.com/stetre/moonnuklear/issues/6#issuecomment-585070571

stetre commented 4 years ago

This is because MoonGL tracks the creation/deletion of GL objects, for proper deletion at exit.

The problem is that MoonGL identifies objects by the GL 'name' assigned to them by the OpenGL driver, which is reused across contexts.

Add to this that contexts themselves are not managed (created/deleted/made current) via the OpenGL API, but via OS API or libraries such as GLFW, GLUT, etc, so there is no simple way to associate an object with the context it belongs to.

stetre commented 4 years ago

Possible solution:

stetre commented 4 years ago

The above solution is useless, though, without a mean to switch context.

The only reason for the object tracking system is to automatically delete objects at exit, in case the application didn't delete them for some reason (e.g. if an error occurred).

Since the glDeleteXxx() functions are contextual (e.g., glDeleteProgram(1) deletes the program object with name=1 in the current GL context), in order to delete objects from multiple GL contexts we need to be able to switch context during the cleanup.

AFAIK, the OpenGL API does not provide such capability, so either we ask the application to provide it (e.g. via a callback that calls glfw.make_context_current() or equivalent functions), or we just give up tracking objects when multiple GL contexts are in use, leaving to the application the responsibility for deleting objects.

stetre commented 4 years ago

The current fix is to disable object tracking and their automatic deletion as soon as a duplicate object name is detected, from which we infere that multiple GL contexts are being used (in this case, it is up to the application to delete objects in their correct context).