15 #include <vsg/core/Data.h>
17 #include <vsg/maths/mat4.h>
18 #include <vsg/maths/vec2.h>
19 #include <vsg/maths/vec3.h>
20 #include <vsg/maths/vec4.h>
22 #include <vsg/io/Input.h>
23 #include <vsg/io/Output.h>
25 #define VSG_array(N, T) \
28 constexpr const char* type_name<N>() noexcept { return "vsg::" #N; }
52 _data = _allocate(_size);
54 for (
auto& v : rhs) *(dest_v++) = v;
60 Data(in_properties,
sizeof(value_type)),
61 _data(_allocate(numElements)),
62 _size(numElements) {
dirty(); }
64 Array(uint32_t numElements, value_type* data,
Properties in_properties = {}) :
65 Data(in_properties,
sizeof(value_type)),
67 _size(numElements) {
dirty(); }
69 Array(uint32_t numElements,
const value_type& value,
Properties in_properties = {}) :
70 Data(in_properties,
sizeof(value_type)),
71 _data(_allocate(numElements)),
74 for (
auto& v : *
this) v = value;
83 assign(data, offset, stride, numElements, in_properties);
86 explicit Array(std::initializer_list<value_type> l) :
87 _data(_allocate(l.size())),
88 _size(
static_cast<uint32_t
>(l.size()))
93 for (
const value_type& v : l) { (*itr++) = v; }
98 explicit Array(
ref_ptr<Data> data, uint32_t offset, uint32_t stride, std::initializer_list<value_type> l) :
102 assign(data, offset, stride, l.size());
105 for (
const value_type& v : l) { (*itr++) = v; }
110 template<
typename... Args>
131 size_t sizeofObject()
const noexcept
override {
return sizeof(
Array); }
132 const char* className()
const noexcept
override {
return type_name<Array>(); }
133 const std::type_info&
type_info() const noexcept
override {
return typeid(*this); }
134 bool is_compatible(
const std::type_info& type)
const noexcept
override {
return typeid(
Array) == type || Data::is_compatible(type); }
137 void accept(Visitor& visitor)
override;
138 void accept(ConstVisitor& visitor)
const override;
140 void read(Input& input)
override
142 size_t original_total_size = size();
146 uint32_t width_size = input.readValue<uint32_t>(
"size");
148 if (
auto data_storage = input.readObject<Data>(
"storage"))
150 uint32_t offset = input.readValue<uint32_t>(
"offset");
155 if (input.matchPropertyName(
"data"))
157 size_t new_total_size = computeValueCountIncludingMipmaps(width_size, 1, 1,
properties.maxNumMipmaps);
161 if (original_total_size != new_total_size)
165 _data = _allocate(new_total_size);
170 _data = _allocate(new_total_size);
177 if (_data) input.read(new_total_size, _data);
183 void write(Output& output)
const override
187 output.writeValue<uint32_t>(
"size", _size);
188 output.writeObject(
"storage", _storage);
191 auto offset = (
reinterpret_cast<uintptr_t
>(_data) -
reinterpret_cast<uintptr_t
>(_storage->dataPointer()));
192 output.writeValue<uint32_t>(
"offset", offset);
196 output.writePropertyName(
"data");
197 output.write(size(), _data);
198 output.writeEndOfLine();
201 size_t size()
const {
return (
properties.maxNumMipmaps <= 1) ? _size : computeValueCountIncludingMipmaps(_size, 1, 1,
properties.maxNumMipmaps); }
203 bool available()
const {
return _data !=
nullptr; }
204 bool empty()
const {
return _data ==
nullptr; }
215 Array& operator=(
const Array& rhs)
217 if (&rhs ==
this)
return *
this;
226 _data = _allocate(_size);
228 for (
auto& v : rhs) *(dest_v++) = v;
236 void assign(uint32_t numElements, value_type* data, Properties in_properties = {})
249 void assign(ref_ptr<Data> storage, uint32_t offset, uint32_t stride, uint32_t numElements, Properties in_properties = {})
256 if (_storage && _storage->dataPointer())
258 _data =
reinterpret_cast<value_type*
>(
reinterpret_cast<uint8_t*
>(_storage->dataPointer()) + offset);
272 void* dataRelease()
override
287 size_t valueSize()
const override {
return sizeof(value_type); }
288 size_t valueCount()
const override {
return size(); }
290 bool dataAvailable()
const override {
return available(); }
291 size_t dataSize()
const override {
return size() *
properties.stride; }
293 void* dataPointer()
override {
return _data; }
294 const void* dataPointer()
const override {
return _data; }
296 void* dataPointer(
size_t i)
override {
return data(i); }
297 const void* dataPointer(
size_t i)
const override {
return data(i); }
299 uint32_t dimensions()
const override {
return 1; }
301 uint32_t width()
const override {
return _size; }
302 uint32_t height()
const override {
return 1; }
303 uint32_t depth()
const override {
return 1; }
305 value_type* data() {
return _data; }
306 const value_type* data()
const {
return _data; }
308 inline value_type* data(
size_t i) {
return reinterpret_cast<value_type*
>(
reinterpret_cast<uint8_t*
>(_data) + i *
properties.stride); }
309 inline const value_type* data(
size_t i)
const {
return reinterpret_cast<const value_type*
>(
reinterpret_cast<const uint8_t*
>(_data) + i *
properties.stride); }
311 value_type& operator[](
size_t i) {
return *data(i); }
312 const value_type& operator[](
size_t i)
const {
return *data(i); }
314 value_type& at(
size_t i) {
return *data(i); }
315 const value_type& at(
size_t i)
const {
return *data(i); }
317 void set(
size_t i,
const value_type& v) { *data(i) = v; }
319 Data* storage() {
return _storage; }
320 const Data* storage()
const {
return _storage; }
322 iterator begin() {
return iterator{_data,
properties.stride}; }
323 const_iterator begin()
const {
return const_iterator{_data,
properties.stride}; }
325 iterator end() {
return iterator{data(_size),
properties.stride}; }
326 const_iterator end()
const {
return const_iterator{data(_size),
properties.stride}; }
334 value_type* _allocate(
size_t size)
const
339 return new value_type[size];
341 return new (std::malloc(
sizeof(value_type) * size)) value_type[size];
343 return new (vsg::allocate(
sizeof(value_type) * size, ALLOCATOR_AFFINITY_DATA)) value_type[size];
348 if (!_storage && _data)
355 vsg::deallocate(_data);
362 ref_ptr<Data> _storage;
365 VSG_array(byteArray, int8_t);
366 VSG_array(ubyteArray, uint8_t);
367 VSG_array(shortArray, int16_t);
368 VSG_array(ushortArray, uint16_t);
369 VSG_array(intArray, int32_t);
370 VSG_array(uintArray, uint32_t);
371 VSG_array(floatArray,
float);
372 VSG_array(doubleArray,
double);
374 VSG_array(vec2Array, vec2);
375 VSG_array(vec3Array, vec3);
376 VSG_array(vec4Array, vec4);
378 VSG_array(dvec2Array, dvec2);
379 VSG_array(dvec3Array, dvec3);
380 VSG_array(dvec4Array, dvec4);
382 VSG_array(bvec2Array, bvec2);
383 VSG_array(bvec3Array, bvec3);
384 VSG_array(bvec4Array, bvec4);
386 VSG_array(ubvec2Array, ubvec2);
387 VSG_array(ubvec3Array, ubvec3);
388 VSG_array(ubvec4Array, ubvec4);
390 VSG_array(svec2Array, svec2);
391 VSG_array(svec3Array, svec3);
392 VSG_array(svec4Array, svec4);
394 VSG_array(usvec2Array, usvec2);
395 VSG_array(usvec3Array, usvec3);
396 VSG_array(usvec4Array, usvec4);
398 VSG_array(ivec2Array, ivec2);
399 VSG_array(ivec3Array, ivec3);
400 VSG_array(ivec4Array, ivec4);
402 VSG_array(uivec2Array, uivec2);
403 VSG_array(uivec3Array, uivec3);
404 VSG_array(uivec4Array, uivec4);
406 VSG_array(mat4Array, mat4);
407 VSG_array(dmat4Array, dmat4);
409 VSG_array(block64Array, block64);
410 VSG_array(block128Array, block128);
const std::type_info & type_info() const noexcept override
return the std::type_info of this Object
Definition: Array.h:133
Properties properties
properties of the data such as format, origin, stride, dataVariance etc.
Definition: Data.h:161
void dirty()
increment the ModifiedCount to signify the data has been modified
Definition: Data.h:196
AllocatorType allocatorType
hint as to how the data values may change during the lifetime of the vsg::Data.
Definition: Data.h:131