vsg  1.0.4
VulkanSceneGraph library
Array.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/Data.h>
16 
17 #include <vsg/maths/mat4.h>
18 #include <vsg/maths/vec2.h>
19 #include <vsg/maths/vec3.h>
20 #include <vsg/maths/vec4.h>
21 
22 #include <vsg/io/Input.h>
23 #include <vsg/io/Output.h>
24 
25 #define VSG_array(N, T) \
26  using N = Array<T>; \
27  template<> \
28  constexpr const char* type_name<N>() noexcept { return "vsg::" #N; }
29 
30 namespace vsg
31 {
32 
33  template<typename T>
34  class Array : public Data
35  {
36  public:
37  using value_type = T;
40 
41  Array() :
42  _data(nullptr),
43  _size(0) {}
44 
45  Array(const Array& rhs) :
46  Data(rhs.properties, sizeof(value_type)),
47  _data(nullptr),
48  _size(rhs._size)
49  {
50  if (_size != 0)
51  {
52  _data = _allocate(_size);
53  auto dest_v = _data;
54  for (auto& v : rhs) *(dest_v++) = v;
55  }
56  dirty();
57  }
58 
59  explicit Array(uint32_t numElements, Properties in_properties = {}) :
60  Data(in_properties, sizeof(value_type)),
61  _data(_allocate(numElements)),
62  _size(numElements) { dirty(); }
63 
64  Array(uint32_t numElements, value_type* data, Properties in_properties = {}) :
65  Data(in_properties, sizeof(value_type)),
66  _data(data),
67  _size(numElements) { dirty(); }
68 
69  Array(uint32_t numElements, const value_type& value, Properties in_properties = {}) :
70  Data(in_properties, sizeof(value_type)),
71  _data(_allocate(numElements)),
72  _size(numElements)
73  {
74  for (auto& v : *this) v = value;
75  dirty();
76  }
77 
78  Array(ref_ptr<Data> data, uint32_t offset, uint32_t stride, uint32_t numElements, Properties in_properties = {}) :
79  Data(),
80  _data(nullptr),
81  _size(0)
82  {
83  assign(data, offset, stride, numElements, in_properties);
84  }
85 
86  explicit Array(std::initializer_list<value_type> l) :
87  _data(_allocate(l.size())),
88  _size(static_cast<uint32_t>(l.size()))
89  {
90  properties.stride = sizeof(value_type);
91 
92  iterator itr = begin();
93  for (const value_type& v : l) { (*itr++) = v; }
94 
95  dirty();
96  }
97 
98  explicit Array(ref_ptr<Data> data, uint32_t offset, uint32_t stride, std::initializer_list<value_type> l) :
99  _data(nullptr),
100  _size(0)
101  {
102  assign(data, offset, stride, l.size());
103 
104  iterator itr = begin();
105  for (const value_type& v : l) { (*itr++) = v; }
106 
107  dirty();
108  }
109 
110  template<typename... Args>
111  static ref_ptr<Array> create(Args... args)
112  {
113  return ref_ptr<Array>(new Array(args...));
114  }
115 
116  static ref_ptr<Array> create(std::initializer_list<value_type> l)
117  {
118  return ref_ptr<Array>(new Array(l));
119  }
120 
121  static ref_ptr<Array> create(ref_ptr<Data> data, uint32_t offset, uint32_t stride, std::initializer_list<value_type> l)
122  {
123  return ref_ptr<Array>(new Array(data, offset, stride, l));
124  }
125 
126  std::size_t sizeofObject() const noexcept override { return sizeof(Array); }
127  const char* className() const noexcept override { return type_name<Array>(); }
128  const std::type_info& type_info() const noexcept override { return typeid(*this); }
129  bool is_compatible(const std::type_info& type) const noexcept override { return typeid(Array) == type || Data::is_compatible(type); }
130 
131  // implementation provided by Visitor.h
132  void accept(Visitor& visitor) override;
133  void accept(ConstVisitor& visitor) const override;
134 
135  void read(Input& input) override
136  {
137  std::size_t original_total_size = size();
138 
139  Data::read(input);
140 
141  uint32_t width_size = input.readValue<uint32_t>("size");
142 
143  if (auto data_storage = input.readObject<Data>("storage"))
144  {
145  uint32_t offset = input.readValue<uint32_t>("offset");
146  assign(data_storage, offset, properties.stride, width_size, properties);
147  return;
148  }
149 
150  if (input.matchPropertyName("data"))
151  {
152  std::size_t new_total_size = computeValueCountIncludingMipmaps(width_size, 1, 1, properties.maxNumMipmaps);
153 
154  if (_data) // if data already may be able to reuse it
155  {
156  if (original_total_size != new_total_size) // if existing data is a different size delete old, and create new
157  {
158  clear();
159 
160  _data = _allocate(new_total_size);
161  }
162  }
163  else // allocate space for data
164  {
165  _data = _allocate(new_total_size);
166  }
167 
168  properties.stride = sizeof(value_type);
169  _size = width_size;
170  _storage = nullptr;
171 
172  if (_data) input.read(new_total_size, _data);
173 
174  dirty();
175  }
176  }
177 
178  void write(Output& output) const override
179  {
180  Data::write(output);
181 
182  output.writeValue<uint32_t>("size", _size);
183  output.writeObject("storage", _storage);
184  if (_storage)
185  {
186  auto offset = (reinterpret_cast<uintptr_t>(_data) - reinterpret_cast<uintptr_t>(_storage->dataPointer()));
187  output.writeValue<uint32_t>("offset", offset);
188  return;
189  }
190 
191  output.writePropertyName("data");
192  output.write(size(), _data);
193  output.writeEndOfLine();
194  }
195 
196  std::size_t size() const { return (properties.maxNumMipmaps <= 1) ? _size : computeValueCountIncludingMipmaps(_size, 1, 1, properties.maxNumMipmaps); }
197 
198  bool available() const { return _data != nullptr; }
199  bool empty() const { return _data == nullptr; }
200 
201  void clear()
202  {
203  _delete();
204 
205  _size = 0;
206  _data = nullptr;
207  _storage = nullptr;
208  }
209 
210  Array& operator=(const Array& rhs)
211  {
212  if (&rhs == this) return *this;
213 
214  clear();
215 
216  properties = rhs.properties;
217  _size = rhs._size;
218 
219  if (_size != 0)
220  {
221  _data = _allocate(_size);
222  auto dest_v = _data;
223  for (auto& v : rhs) *(dest_v++) = v;
224  }
225 
226  dirty();
227 
228  return *this;
229  }
230 
231  void assign(uint32_t numElements, value_type* data, Properties in_properties = {})
232  {
233  _delete();
234 
235  properties = in_properties;
236  properties.stride = sizeof(value_type);
237  _size = numElements;
238  _data = data;
239  _storage = nullptr;
240 
241  dirty();
242  }
243 
244  void assign(ref_ptr<Data> storage, uint32_t offset, uint32_t stride, uint32_t numElements, Properties in_properties = {})
245  {
246  _delete();
247 
248  _storage = storage;
249  properties = in_properties;
250  properties.stride = stride;
251  if (_storage && _storage->dataPointer())
252  {
253  _data = reinterpret_cast<value_type*>(reinterpret_cast<uint8_t*>(_storage->dataPointer()) + offset);
254  _size = numElements;
255  }
256  else
257  {
258  _data = nullptr;
259  _size = 0;
260  }
261 
262  dirty();
263  }
264 
265  // release the data so that ownership can be passed on, the local data pointer and size is set to 0 and destruction of Array will no result in the data being deleted.
266  // when the data is stored in a separate vsg::Data object then return nullptr and do not attempt to release data.
267  void* dataRelease() override
268  {
269  if (!_storage)
270  {
271  void* tmp = _data;
272  _data = nullptr;
273  _size = 0;
274  return tmp;
275  }
276  else
277  {
278  return nullptr;
279  }
280  }
281 
282  std::size_t valueSize() const override { return sizeof(value_type); }
283  std::size_t valueCount() const override { return size(); }
284 
285  bool dataAvailable() const override { return available(); }
286  std::size_t dataSize() const override { return size() * properties.stride; }
287 
288  void* dataPointer() override { return _data; }
289  const void* dataPointer() const override { return _data; }
290 
291  void* dataPointer(std::size_t i) override { return data(i); }
292  const void* dataPointer(std::size_t i) const override { return data(i); }
293 
294  uint32_t dimensions() const override { return 1; }
295 
296  uint32_t width() const override { return _size; }
297  uint32_t height() const override { return 1; }
298  uint32_t depth() const override { return 1; }
299 
300  value_type* data() { return _data; }
301  const value_type* data() const { return _data; }
302 
303  inline value_type* data(std::size_t i) { return reinterpret_cast<value_type*>(reinterpret_cast<uint8_t*>(_data) + i * properties.stride); }
304  inline const value_type* data(std::size_t i) const { return reinterpret_cast<const value_type*>(reinterpret_cast<const uint8_t*>(_data) + i * properties.stride); }
305 
306  value_type& operator[](std::size_t i) { return *data(i); }
307  const value_type& operator[](std::size_t i) const { return *data(i); }
308 
309  value_type& at(std::size_t i) { return *data(i); }
310  const value_type& at(std::size_t i) const { return *data(i); }
311 
312  void set(std::size_t i, const value_type& v) { *data(i) = v; }
313 
314  Data* storage() { return _storage; }
315  const Data* storage() const { return _storage; }
316 
317  iterator begin() { return iterator{_data, properties.stride}; }
318  const_iterator begin() const { return const_iterator{_data, properties.stride}; }
319 
320  iterator end() { return iterator{data(_size), properties.stride}; }
321  const_iterator end() const { return const_iterator{data(_size), properties.stride}; }
322 
323  protected:
324  virtual ~Array()
325  {
326  _delete();
327  }
328 
329  value_type* _allocate(size_t size) const
330  {
331  if (size == 0)
332  return nullptr;
333  else if (properties.allocatorType == ALLOCATOR_TYPE_NEW_DELETE)
334  return new value_type[size];
335  else if (properties.allocatorType == ALLOCATOR_TYPE_MALLOC_FREE)
336  return new (std::malloc(sizeof(value_type) * size)) value_type[size];
337  else
338  return new (vsg::allocate(sizeof(value_type) * size, ALLOCATOR_AFFINITY_DATA)) value_type[size];
339  }
340 
341  void _delete()
342  {
343  if (!_storage && _data)
344  {
345  if (properties.allocatorType == ALLOCATOR_TYPE_NEW_DELETE)
346  delete[] _data;
347  else if (properties.allocatorType == ALLOCATOR_TYPE_MALLOC_FREE)
348  std::free(_data);
349  else if (properties.allocatorType != 0)
350  vsg::deallocate(_data);
351  }
352  }
353 
354  private:
355  value_type* _data;
356  uint32_t _size;
357  ref_ptr<Data> _storage;
358  };
359 
360  VSG_array(byteArray, int8_t);
361  VSG_array(ubyteArray, uint8_t);
362  VSG_array(shortArray, int16_t);
363  VSG_array(ushortArray, uint16_t);
364  VSG_array(intArray, int32_t);
365  VSG_array(uintArray, uint32_t);
366  VSG_array(floatArray, float);
367  VSG_array(doubleArray, double);
368 
369  VSG_array(vec2Array, vec2);
370  VSG_array(vec3Array, vec3);
371  VSG_array(vec4Array, vec4);
372 
373  VSG_array(dvec2Array, dvec2);
374  VSG_array(dvec3Array, dvec3);
375  VSG_array(dvec4Array, dvec4);
376 
377  VSG_array(bvec2Array, bvec2);
378  VSG_array(bvec3Array, bvec3);
379  VSG_array(bvec4Array, bvec4);
380 
381  VSG_array(ubvec2Array, ubvec2);
382  VSG_array(ubvec3Array, ubvec3);
383  VSG_array(ubvec4Array, ubvec4);
384 
385  VSG_array(svec2Array, svec2);
386  VSG_array(svec3Array, svec3);
387  VSG_array(svec4Array, svec4);
388 
389  VSG_array(usvec2Array, usvec2);
390  VSG_array(usvec3Array, usvec3);
391  VSG_array(usvec4Array, usvec4);
392 
393  VSG_array(ivec2Array, ivec2);
394  VSG_array(ivec3Array, ivec3);
395  VSG_array(ivec4Array, ivec4);
396 
397  VSG_array(uivec2Array, uivec2);
398  VSG_array(uivec3Array, uivec3);
399  VSG_array(uivec4Array, uivec4);
400 
401  VSG_array(mat4Array, mat4);
402  VSG_array(dmat4Array, dmat4);
403 
404  VSG_array(block64Array, block64);
405  VSG_array(block128Array, block128);
406 
407 } // namespace vsg
Definition: Array.h:35
const std::type_info & type_info() const noexcept override
return the std::type_info of this Object
Definition: Array.h:128
Definition: Data.h:104
Properties properties
properties of the data such as format, origin, stride, dataVariance etc.
Definition: Data.h:173
void dirty()
increment the ModifiedCount to signify the data has been modified
Definition: Data.h:206
Definition: ref_ptr.h:22
Definition: Data.h:110
AllocatorType allocatorType
hint as how the data values may change during the lifetime of the vsg::Data.
Definition: Data.h:125
Definition: Data.h:60