heyx3 / Bplus.jl

A modern OpenGL 4.6 rendering framework, written in Julia.
Other
72 stars 3 forks source link

Only bind program/mesh if it's not already bound #28

Open heyx3 opened 1 year ago

heyx3 commented 1 year ago

Inside GL.render_mesh(), the mesh and program to use are bound with glUseProgram() and glBindVertexArray() respectively. We would like to avoid doing that if they are already bound.

The context already tracks the currently-bound program and mesh, but it tracks it by their GL handle. And it turns out OpenGL can re-use handles from destroyed objects!

This task has several steps to complete it, although none of them are difficult:

  1. Update the cached mesh and program to be a Mesh and Program instance, respectively, rather than merely their handles.

  2. On context refresh, we don't have an easy way to map a handle back to a mesh/program, and even if we did, it's possible the owner of the handle was destroyed. So I think the easiest thing to do on refresh is simply re-bind whatever mesh and program the context last knew about, or bind null if it thought nothing was bound.

  3. While we're at it, double-check what happens when a Mesh or Program that's currently bound gets destroyed. The Context needs to know if any of its cached most-recently-bound resources have been destroyed. Fortunately, all GL resources implement is_destroyed().

  4. Finally, add checks around the binding of mesh and program in GL.render_mesh(), in the file GL/drawing.jl