diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2022-05-14 22:06:51 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2022-05-17 13:34:35 +0300 |
commit | 65fa34f63fe4bc452249c3f1041d7c79edaeb9e7 (patch) | |
tree | c79775406415fbe4862ae443b7ddf13d956d9dc5 | |
parent | f9ed31b15d7aa1cbbaa54e675fd98cbfeecd1de7 (diff) |
DRW: Add SwapChain container to allow easier usage of double buffering
The template also takes the length of the chain to allow triple buffering.
-rw-r--r-- | source/blender/draw/intern/DRW_gpu_wrapper.hh | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/source/blender/draw/intern/DRW_gpu_wrapper.hh b/source/blender/draw/intern/DRW_gpu_wrapper.hh index d7e752a43f4..d6ee50ff5aa 100644 --- a/source/blender/draw/intern/DRW_gpu_wrapper.hh +++ b/source/blender/draw/intern/DRW_gpu_wrapper.hh @@ -880,4 +880,56 @@ class Framebuffer : NonCopyable { /** \} */ +/* -------------------------------------------------------------------- */ +/** \name Double & Triple buffering util + * + * This is not strictly related to a GPU type and could be moved elsewhere. + * \{ */ + +template<typename T, int64_t len> class SwapChain { + private: + std::array<T, len> chain_; + int64_t index_ = 0; + + public: + void swap() + { + index_ = (index_ + 1) % len; + } + + T ¤t() + { + return chain_[index_]; + } + + T &previous() + { + /* Avoid modulo operation with negative numbers. */ + return chain_[(index_ + len - 1) % len]; + } + + T &next() + { + return chain_[(index_ + 1) % len]; + } + + const T ¤t() const + { + return chain_[index_]; + } + + const T &previous() const + { + /* Avoid modulo operation with negative numbers. */ + return chain_[(index_ + len - 1) % len]; + } + + const T &next() const + { + return chain_[(index_ + 1) % len]; + } +}; + +/** \} */ + } // namespace blender::draw |