First, usually OpenGL code looks a bit like this:
glBindBuffer(bufferId); glBufferData(pointerToData); glBindBuffer(anotherBufferId); glBufferData(pointerToMoreData); glBindBuffer(bufferId); glDrawElements(/* element type, count and such here */); // Drawing using the last bound object
My thought was to make that look more like this:
buffer.data(pointerToData); buffer.use(); anotherBuffer.data(pointerToMoreData); context.drawElements(/* glDraw* parameters here */); // Draws using buffer, not anotherBufferThe actual OpenGL bind commands have been moved behind the scenes and instead there is a use() method that marks an object to be used when drawing. glBind* should only be called when necessary. This would require some state tracking made by myself, by tracking what's "bound for drawing" and what's actually bound at the moment. The OpenGL context that the gl* functions operate on is very much a thread local concept itself, so it would make sense to have a hidden (from library user's perspective) state tracker object. Using thread_local to store a pointer to my own state tracker seems very obvious choice.
The thread local pointer to the tracker would have to be used pretty much once for every OpenGL call that manipulates GL state. Does this seem like a bad idea to you?
After some rubber ducking (partly whily trying to figure out how to put out things in this post), I've started to think maybe it's after all better to make the user call bind explicitly, and then just validate the calls with the thread-local state. The validation could be disabled in release mode as the program doesn't actually rely on it - well, except maybe for skipping unnecessary bind calls. (Sort of annoyingly shaders in OpenGL work like in the use() example above, though.)
buffer.bind(); anotherBuffer.data(pointerToData); // Throws or does some other terrible thing when validation is on!I wish there was a compile time way to enforce ordering of calls that's not terribly cludgy, but I guess runtime validation has to do.
The Object initialization design issue thread has been going over at least tangentially related issues, so hopefully some of that interest and insight could leak into this thread too.