vsg  1.1.0
VulkanSceneGraph library
Data.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/Allocator.h>
16 #include <vsg/core/Object.h>
17 #include <vsg/core/compare.h>
18 #include <vsg/core/type_name.h>
19 #include <vsg/vk/vulkan.h>
20 
21 #include <cstring>
22 #include <vector>
23 
24 namespace vsg
25 {
26 
29  {
30  uint32_t count = 0;
31 
32  bool operator==(const ModifiedCount& rhs) const { return count == rhs.count; }
33  bool operator!=(const ModifiedCount& rhs) const { return count != rhs.count; }
34 
35  void operator++() { ++count; }
36  };
37 
39  struct block64
40  {
41  uint8_t value[8];
42  };
43 
45  struct block128
46  {
47  uint8_t value[16];
48  };
49 
50  enum Origin : uint8_t
51  {
52  TOP_LEFT = 0,
53  BOTTOM_LEFT = 2
54  };
55 
56  enum DataVariance : uint8_t
57  {
58  STATIC_DATA = 0,
59  STATIC_DATA_UNREF_AFTER_TRANSFER = 1,
60  DYNAMIC_DATA = 2,
61  DYNAMIC_DATA_TRANSFER_AFTER_RECORD = 3
62  };
63 
64  template<typename T>
66  {
67  using value_type = T;
68  using iterator_category = std::forward_iterator_tag;
69  using difference_type = std::ptrdiff_t;
70  using pointer = T*;
71  using reference = T&;
72 
73  value_type* ptr;
74  uint32_t stride; // stride in bytes
75 
76  inline void advance()
77  {
78  if constexpr (std::is_const<value_type>::value)
79  ptr = reinterpret_cast<value_type*>(reinterpret_cast<const uint8_t*>(ptr) + stride);
80  else
81  ptr = reinterpret_cast<value_type*>(reinterpret_cast<uint8_t*>(ptr) + stride);
82  }
83 
84  stride_iterator& operator++()
85  {
86  advance();
87  return *this;
88  }
89  stride_iterator operator++(int)
90  {
91  stride_iterator reval(*this);
92  advance();
93  return reval;
94  }
95 
96  bool operator==(stride_iterator rhs) const { return ptr == rhs.ptr; }
97  bool operator!=(stride_iterator rhs) const { return ptr != rhs.ptr; }
98  bool operator<(stride_iterator rhs) const { return ptr < rhs.ptr; }
99  bool operator<=(stride_iterator rhs) const { return ptr <= rhs.ptr; }
100  bool operator>(stride_iterator rhs) const { return ptr > rhs.ptr; }
101  bool operator>=(stride_iterator rhs) const { return ptr >= rhs.ptr; }
102 
103  value_type& operator*() { return *reinterpret_cast<value_type*>(ptr); }
104  value_type* operator->() { return reinterpret_cast<value_type*>(ptr); }
105  };
106 
109  class VSG_DECLSPEC Data : public Object
110  {
111  public:
112  /* Properties used for specifying the format of the data, use of mipmaps, block compressed data and origin.
113  * Default of no mipmapping and {1,1,1} is uncompressed.
114  * A single block (Block64/Block128) is stored as a single value with the Data object. */
115  struct VSG_DECLSPEC Properties
116  {
117  Properties() = default;
118  Properties(const Properties& rhs) = default;
119  explicit Properties(VkFormat in_format) :
120  format(in_format) {}
121 
122  VkFormat format = VK_FORMAT_UNDEFINED;
123  uint32_t stride = 0;
124  uint8_t maxNumMipmaps = 0;
125  uint8_t blockWidth = 1;
126  uint8_t blockHeight = 1;
127  uint8_t blockDepth = 1;
128  uint8_t origin = TOP_LEFT;
129  int8_t imageViewType = -1;
130  DataVariance dataVariance = STATIC_DATA;
131  AllocatorType allocatorType = ALLOCATOR_TYPE_VSG_ALLOCATOR;
132 
133  int compare(const Properties& rhs) const;
134  Properties& operator=(const Properties& rhs);
135  };
136 
137  Data() {}
138 
139  explicit Data(Properties layout) :
140  properties(layout) {}
141 
142  Data(Properties layout, uint32_t min_stride) :
143  properties(layout)
144  {
145  if (properties.stride < min_stride) properties.stride = min_stride;
146  }
147 
149  static void* operator new(size_t count);
150  static void operator delete(void* ptr);
151 
152  size_t sizeofObject() const noexcept override { return sizeof(Data); }
153  bool is_compatible(const std::type_info& type) const noexcept override { return typeid(Data) == type || Object::is_compatible(type); }
154 
155  int compare(const Object& rhs_object) const override;
156 
157  void read(Input& input) override;
158  void write(Output& output) const override;
159 
162 
163  bool dynamic() const { return properties.dataVariance >= DYNAMIC_DATA; }
164 
165  virtual ref_ptr<Data> clone() const = 0;
166 
167  virtual size_t valueSize() const = 0;
168  virtual size_t valueCount() const = 0;
169 
170  virtual bool dataAvailable() const = 0;
171  virtual size_t dataSize() const = 0;
172 
173  virtual void* dataPointer() = 0;
174  virtual const void* dataPointer() const = 0;
175 
176  virtual void* dataPointer(size_t index) = 0;
177  virtual const void* dataPointer(size_t index) const = 0;
178 
179  virtual void* dataRelease() = 0;
180 
181  virtual uint32_t dimensions() const = 0;
182 
183  virtual uint32_t width() const = 0;
184  virtual uint32_t height() const = 0;
185  virtual uint32_t depth() const = 0;
186 
187  bool contiguous() const { return valueSize() == properties.stride; }
188 
189  uint32_t stride() const { return properties.stride ? properties.stride : static_cast<uint32_t>(valueSize()); }
190 
191  using MipmapOffsets = std::vector<size_t>;
192  MipmapOffsets computeMipmapOffsets() const;
193  static size_t computeValueCountIncludingMipmaps(size_t w, size_t h, size_t d, uint32_t maxNumMipmaps);
194 
196  void dirty() { ++_modifiedCount; }
197 
200  {
201  if (_modifiedCount != mc)
202  {
203  mc = _modifiedCount;
204  return true;
205  }
206  else
207  return false;
208  }
209 
211  bool differentModifiedCount(const ModifiedCount& mc) const { return _modifiedCount != mc; }
212 
213  protected:
214  virtual ~Data() {}
215 
216  ModifiedCount _modifiedCount;
217 
218 #if 1
219  public:
222 
224  void setLayout(Layout layout)
225  {
226  VkFormat previous_format = properties.format; // temporary hack to keep applications that call setFormat(..) before setLayout(..) working
227  uint32_t previous_stride = properties.stride;
228  properties = layout;
229  if (properties.format == 0 && previous_format != 0) properties.format = previous_format; // temporary hack to keep existing applications working
230  if (properties.stride == 0 && previous_stride != 0) properties.stride = previous_stride; // make sure the layout has a valid stride.
231  }
233  Layout& getLayout() { return properties; }
235  Layout getLayout() const { return properties; }
236 #endif
237  };
238  VSG_type_name(vsg::Data);
239 
240  using DataList = std::vector<ref_ptr<Data>>;
241 
242 } // namespace vsg
Definition: Data.h:110
Properties properties
properties of the data such as format, origin, stride, dataVariance etc.
Definition: Data.h:161
bool differentModifiedCount(const ModifiedCount &mc) const
return true if Data's ModifiedCount is different from the specified ModifiedCount
Definition: Data.h:211
Layout getLayout() const
deprecated: use data->properties
Definition: Data.h:235
void setLayout(Layout layout)
deprecated: use data->properties = properties instead.
Definition: Data.h:224
void dirty()
increment the ModifiedCount to signify the data has been modified
Definition: Data.h:196
Layout & getLayout()
deprecated: use data->properties
Definition: Data.h:233
bool getModifiedCount(ModifiedCount &mc) const
get the Data's ModifiedCount and return true if this changes the specified ModifiedCount
Definition: Data.h:199
int compare(const Object &rhs_object) const override
compare two objects, return -1 if this object is less than rhs, return 0 if it's equal,...
Definition: Input.h:44
Definition: Object.h:42
Definition: Output.h:41
Definition: ref_ptr.h:22
Definition: Data.h:116
DataVariance dataVariance
-1 signifies undefined VkImageViewType, if value >=0 then value should be treated as valid VkImageVie...
Definition: Data.h:130
ModifiedCount provides a count value to keep track of modifications to data.
Definition: Data.h:29
Definition: Data.h:46
Definition: Data.h:40
Definition: Data.h:66