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 {
15  class SwapChain {
16  public:
17 
18  SwapChain(uint8_t size = 2U) : _size{ size } {}
19  virtual ~SwapChain() {}
20 
21  void advance() {
22  _frontIndex = (_frontIndex + 1) % _size;
23  }
24 
25  uint8_t getSize() const { return _size; }
26 
27  protected:
28  const uint8_t _size;
29  uint8_t _frontIndex{ 0U };
30 
31  };
32  typedef std::shared_ptr<SwapChain> SwapChainPointer;
33 
34  template <class R>
35  class ResourceSwapChain : public SwapChain {
36  public:
37 
38  enum {
39  MAX_SIZE = 4
40  };
41 
42  using Type = R;
43  using TypePointer = std::shared_ptr<R>;
44  using TypeConstPointer = std::shared_ptr<const R>;
45 
46  ResourceSwapChain(const std::vector<TypePointer>& v) : SwapChain{ std::min<uint8_t>((uint8_t)v.size(), MAX_SIZE) } {
47  for (size_t i = 0; i < _size; ++i) {
48  _resources[i] = v[i];
49  }
50  }
51  const TypePointer& get(unsigned int index) const { return _resources[(index + _frontIndex) % _size]; }
52 
53  private:
54 
55  std::array<TypePointer, MAX_SIZE> _resources;
56  };
57 }
58 
59 #endif