jdryg / vg-renderer

A vector graphics renderer for bgfx, based on ideas from NanoVG and ImDrawList (Dear ImGUI)
BSD 2-Clause "Simplified" License
504 stars 55 forks source link

Unable to cleanly shutdown #16

Closed tfvlrue closed 5 years ago

tfvlrue commented 5 years ago

Hi, Just started playing around with this library and bgfx for the first time and having trouble getting it to shutdown cleanly. It seems like vg and bgfx are each dependent on the other shutting down first and I'm not sure how to resolve it.

Here's the essence of what I'm trying. First, initialization:

bgfx::init(...);
_vgContext = vg::createContext(...);

Then render a square:

bgfx::setViewRect(0, 0, 0, _width, _height);
bgfx::touch(0);

vg::beginFrame(_vgContext, GetWidth(), GetHeight(), 1.0f);
vg::beginPath(_vgContext);
vg::moveTo(_vgContext, 0.0f, 0.0f);
vg::lineTo(_vgContext, 0.0f, 100.0f);
vg::lineTo(_vgContext, 100.0f, 100.0f);
vg::lineTo(_vgContext, 100.0f, 0.0f);
vg::closePath(_vgContext);
vg::fillPath(_vgContext, vg::color4f(1.0f, 0.0f, 0.0f, 1.0f), vg::FillFlags::Enum::Convex);
vg::endFrame(_vgContext);

bgfx::frame();

Finally, shutdown:

vg::destroyContext(_vgContext);
bgfx::shutdown();

If I destroy the vg context first, then bgfx's render thread tries to release (via releaseIndexBufferCallback) an index buffer that vg has already freed. If I shutdown bgfx first, then vg::destroyContext tries to destroy programs that bgfx has already freed. If I don't destroy the vg context, then bgfx complains about a bunch of leaked handles on shutdown.

Any assistance sorting this out is much appreciated. Thanks!

jdryg commented 5 years ago

Hi,

vg-renderer uses bgfx::makeRef to pass vertex and index buffer data to bgfx. According to the docs,

Data passed must be available for at least 2 bgfx::frame calls

Can you try calling bgfx::frame() a couple of times before calling vg::destroyContext()?

For the record, the shutdown order you posted is correct because vg-renderer depends on bgfx and not the opposite (bgfx doesn't know about vg-renderer).

Hope that helps.

tfvlrue commented 5 years ago

Aha, that did the trick! Thanks for the quick response.