Command Buffer

In this post I will present a C++ lock-free and API agnostic command buffer implementation that can be used for multi-threaded rendering and reducing overhead (state changes, depth sorting) on the main thread. The implementation is based on Stefan Reinalter's blog post1 on multi-thread rendering which I recommend reading as it shows several alternatives and more insight to certain aspects. It also includes a configurable draw key class (via bitfields), implementation of some basic GL commands and supports multiple material passes.

In most cases you will have multiple command buffers for handling specific rendering logic like in the case of a deferred renderer: shadow maps, g-buffer pass, deferred pass and a post-process pass.

Commands will be dispatched into one or more of the command buffers from multiple threads (main thread can also participate as it shouldn't be idle waiting), after which you'll have to wait until all commands are dispatched and it's safe to sort and submit the commands (to the graphics driver) on the main thread:

Note that in some cases certain command buffers dont require frequent updates (for example the post processing command buffer) as they only need to be updated when something changes.

Defining a command buffer requires a key type with its decoder class and an optional material binder: