Overte C++ Documentation
ResourceSwapChain.h
1 //
2 // Created by Olivier Prat on 2018/02/19
3 // Copyright 2013-2018 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 #ifndef hifi_gpu_ResourceSwapChain_h
9 #define hifi_gpu_ResourceSwapChain_h
10 
11 #include <memory>
12 #include <array>
13 
14 namespace gpu {
16  class SwapChain {
17  public:
21  SwapChain(uint8_t size = 2U) : _size{ size } {}
22  virtual ~SwapChain() {}
23 
27  void advance() {
28  _frontIndex = (_frontIndex + 1) % _size;
29  }
30 
34  uint8_t getSize() const { return _size; }
35 
36  protected:
38  const uint8_t _size;
39 
41  uint8_t _frontIndex{ 0U };
42 
43  };
44  typedef std::shared_ptr<SwapChain> SwapChainPointer;
45 
48  template <class R>
49  class ResourceSwapChain : public SwapChain {
50  public:
51 
52  enum {
53  MAX_SIZE = 4
54  };
55 
56  using Type = R;
57  using TypePointer = std::shared_ptr<R>;
58  using TypeConstPointer = std::shared_ptr<const R>;
59 
63  ResourceSwapChain(const std::vector<TypePointer>& v) : SwapChain{ std::min<uint8_t>((uint8_t)v.size(), MAX_SIZE) } {
64  for (size_t i = 0; i < _size; ++i) {
65  _resources[i] = v[i];
66  }
67  }
68 
75  const TypePointer& get(unsigned int index) const { return _resources[(index + _frontIndex) % _size]; }
76 
77  private:
78 
80  std::array<TypePointer, MAX_SIZE> _resources;
81  };
82 }
83 
84 #endif
Definition: ResourceSwapChain.h:49
ResourceSwapChain(const std::vector< TypePointer > &v)
Definition: ResourceSwapChain.h:63
const TypePointer & get(unsigned int index) const
Definition: ResourceSwapChain.h:75
Base class for swapchains.
Definition: ResourceSwapChain.h:16
uint8_t _frontIndex
Index of the current object in the circular buffer.
Definition: ResourceSwapChain.h:41
void advance()
Definition: ResourceSwapChain.h:27
uint8_t getSize() const
Definition: ResourceSwapChain.h:34
SwapChain(uint8_t size=2U)
Definition: ResourceSwapChain.h:21
const uint8_t _size
Number of objects stored in the swapchain.
Definition: ResourceSwapChain.h:38