memononen / nanovg

Antialiased 2D vector drawing library on top of OpenGL for UI and visualizations.
zlib License
5.15k stars 770 forks source link

OpenGL 3 context sharing is broken #631

Open lochnessdragon opened 2 years ago

lochnessdragon commented 2 years ago

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:

Actual:

To recreate:

nidefawl commented 2 years ago

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.

mulle-nat commented 2 years ago

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.

ib00 commented 1 year ago

Are these three extensions available somewhere?

mulle-nat commented 1 year ago

Not yet. I plan to release my stuff, but I am already a year behind :weary:

mulle-nat commented 7 months ago

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.