vsg  1.1.0
VulkanSceneGraph library
vk_buffer.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 Robert Osfield
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 </editor-fold> */
14 
15 #include <vsg/core/Version.h>
16 
17 #include <array>
18 #include <vector>
19 
20 namespace vsg
21 {
22 
23 #if VSG_MAX_DEVICES <= 1
24 
26  template<class T>
27  struct vk_buffer
28  {
29  T value;
30 
31  T& operator[](uint32_t) { return value; }
32  const T& operator[](uint32_t) const { return value; }
33 
34  uint32_t size() const { return 1; }
35 
36  void clear() { value = {}; }
37 
38  auto begin() { return &value; }
39  auto begin() const { return &value; }
40 
41  auto end() { return &value + 1; }
42  auto end() const { return &value + 1; }
43  };
44 
45 #elif VSG_MAX_DEVICES <= 4
46 
48  template<class T>
49  struct vk_buffer
50  {
51  std::array<T, VSG_MAX_DEVICES> buffer;
52 
53  T& operator[](uint32_t deviceID) { return buffer[deviceID]; }
54  const T& operator[](uint32_t deviceID) const { return buffer[deviceID]; }
55 
56  uint32_t size() const { return VSG_MAX_DEVICES; }
57 
58  void clear()
59  {
60  for (auto& value : buffer) value = {};
61  }
62 
63  auto begin() { return buffer.begin(); }
64  auto begin() const { return buffer.begin(); }
65 
66  auto end() { return buffer.end(); }
67  auto end() const { return buffer.end(); }
68  };
69 
70 #else
71 
73  template<class T>
74  struct vk_buffer
75  {
76  std::vector<T> buffer;
77 
78  T& operator[](uint32_t deviceID)
79  {
80  if (deviceID >= buffer.size()) buffer.resize(deviceID + 1);
81  return buffer[deviceID];
82  }
83 
84  const T& operator[](uint32_t deviceID) const
85  {
86  return buffer[deviceID];
87  }
88 
89  uint32_t size() const { return static_cast<uint32_t>(buffer.size()); }
90 
91  void clear() { buffer.clear(); }
92 
93  auto begin() { return buffer.begin(); }
94  auto begin() const { return buffer.begin(); }
95 
96  auto end() { return buffer.end(); }
97  auto end() const { return buffer.end(); }
98  };
99 
100 #endif
101 
102 } // namespace vsg
vk_buffer that manages a single logical device supported.
Definition: vk_buffer.h:28