OpenGL 3 context sharing is broken
lochnessdragon opened this issue · 5 comments
I'm trying to use NanoVG to render to multiple windows whilst sharing the same OpenGL context to resolve the reasons outlined in Issue #379. However, since the OpenGL 3 implementation uses VAOs, this breaks rendering on the second window.
Expected:
- I'd expect both windows to render the content correctly.
Actual:
- The second window doesn't render anything and the error
0x000501 after tex paint tex
is printed to the console.
To recreate:
- Create a window with an OpenGL3 context
- Create an OpenGL 3 NanoVG object
- Create a second window w/ a shared context
- Render something to both windows
I added a comment to your commit.
Is the VAO all that is needed to enable context sharing?
I would rather have nvgAddGlContext/nvgRemoveGlContext that sets up the VAO for the nvg+GL combo.
That way I can control the contexts at a higher level instead of calling glIsVertexArray each render flush.
I am sharing textures (not vertex arrays) between contexts successfully. I extended the nanovg API with these three methods:
// @mulle-nanovg@ >
// This is like nvglCreateImageFromHandleGL3
// In order to share images, the windows these
// contexts belong to, must have been created with object sharing.
// Returns handle to the image.
int nvgCreateImageTexture(NVGcontext* ctx, int w, int h, int imageFlags, int type, void **texture);
// Get the OpenGL/Vulkan texture opaque handle (OpenGL is really unsigned int)
void nvgImageTextureInfo( NVGcontext* ctx, int image, int *imageFlags, int *type, void **texture);
// Relinquish OpenGL/Vulkan texture id, so it won't be deleted when the
// image is deleted
void nvgForgetImageTexture(NVGcontext* ctx, int image);
// @mulle-nanovg@ <
In essence what this does is make an OpenGL texture
known to nanovg and usuable as the image
int and then make nanovg forget it again. nanovg doesn't actually manage the OpenGL texture with this API and retain count management is done above nanovg to keep it simple.
One could extend this scheme to other OpenGL objects besides textures, but if it's just to offload code into nanovg, that could be done elsewhere, it would kill the "nano" in nanovg IMO.
Are these three extensions available somewhere?
Not yet. I plan to release my stuff, but I am already a year behind 😩
I put my nanovg fork out there (not really a release yet). The relevant API tidbits for this issue are in the header and source. The actual work is done via the callbacks declared in struct NVGparams and defined in nanovg_gl.h.
As explained above this is very lowlevel and the actual texture sharing logic is implemented on top of it. But without these functions, you couldn't get nanovg to use a shared texture.