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_array2D(N, T) \
26 using N = Array2D<T>; \
28 constexpr const char* type_name<N>() noexcept { return "vsg::" #N; }
52 if (_width != 0 && _height != 0)
54 _data = _allocate(_width * _height);
56 for (
auto& v : rhs) *(dest_v++) = v;
62 Data(in_properties,
sizeof(value_type)),
63 _data(_allocate(width * height)),
65 _height(height) {
dirty(); }
67 Array2D(uint32_t width, uint32_t height, value_type* data,
Properties in_properties = {}) :
68 Data(in_properties,
sizeof(value_type)),
71 _height(height) {
dirty(); }
73 Array2D(uint32_t width, uint32_t height,
const value_type& value,
Properties in_properties = {}) :
74 Data(in_properties,
sizeof(value_type)),
75 _data(_allocate(width * height)),
79 for (
auto& v : *
this) v = value;
89 assign(data, offset, stride, width, height, in_properties);
92 template<
typename... Args>
103 size_t sizeofObject()
const noexcept
override {
return sizeof(
Array2D); }
104 const char* className()
const noexcept
override {
return type_name<Array2D>(); }
105 const std::type_info&
type_info() const noexcept
override {
return typeid(*this); }
106 bool is_compatible(
const std::type_info& type)
const noexcept
override {
return typeid(
Array2D) == type || Data::is_compatible(type); }
109 void accept(Visitor& visitor)
override;
110 void accept(ConstVisitor& visitor)
const override;
112 void read(Input& input)
override
114 size_t original_size = size();
118 uint32_t w = input.readValue<uint32_t>(
"width");
119 uint32_t h = input.readValue<uint32_t>(
"height");
121 if (
auto data_storage = input.readObject<Data>(
"storage"))
123 uint32_t offset = input.readValue<uint32_t>(
"offset");
128 if (input.matchPropertyName(
"data"))
130 size_t new_size = computeValueCountIncludingMipmaps(w, h, 1,
properties.maxNumMipmaps);
134 if (original_size != new_size)
137 _data = _allocate(new_size);
142 _data = _allocate(new_size);
150 if (_data) input.read(new_size, _data);
156 void write(Output& output)
const override
160 output.writeValue<uint32_t>(
"width", _width);
161 output.writeValue<uint32_t>(
"height", _height);
163 output.writeObject(
"storage", _storage);
166 auto offset = (
reinterpret_cast<uintptr_t
>(_data) -
reinterpret_cast<uintptr_t
>(_storage->dataPointer()));
167 output.writeValue<uint32_t>(
"offset", offset);
171 output.writePropertyName(
"data");
172 output.write(valueCount(), _data);
173 output.writeEndOfLine();
176 size_t size()
const {
return (
properties.maxNumMipmaps <= 1) ?
static_cast<size_t>(_width * _height) : computeValueCountIncludingMipmaps(_width, _height, 1,
properties.maxNumMipmaps); }
178 bool available()
const {
return _data !=
nullptr; }
179 bool empty()
const {
return _data ==
nullptr; }
191 Array2D& operator=(
const Array2D& rhs)
193 if (&rhs ==
this)
return *
this;
199 _height = rhs._height;
201 if (_width != 0 && _height != 0)
203 _data = _allocate(_width * _height);
205 for (
auto& v : rhs) *(dest_v++) = v;
213 void assign(uint32_t width, uint32_t height, value_type* data, Properties in_properties = {})
227 void assign(ref_ptr<Data> storage, uint32_t offset, uint32_t stride, uint32_t width, uint32_t height, Properties in_properties = {})
234 if (_storage && _storage->dataPointer())
236 _data =
reinterpret_cast<value_type*
>(
reinterpret_cast<uint8_t*
>(_storage->dataPointer()) + offset);
252 void* dataRelease()
override
268 size_t valueSize()
const override {
return sizeof(value_type); }
269 size_t valueCount()
const override {
return size(); }
271 bool dataAvailable()
const override {
return available(); }
272 size_t dataSize()
const override {
return size() *
properties.stride; }
274 void* dataPointer()
override {
return _data; }
275 const void* dataPointer()
const override {
return _data; }
277 void* dataPointer(
size_t i)
override {
return data(i); }
278 const void* dataPointer(
size_t i)
const override {
return data(i); }
280 uint32_t dimensions()
const override {
return 2; }
281 uint32_t width()
const override {
return _width; }
282 uint32_t height()
const override {
return _height; }
283 uint32_t depth()
const override {
return 1; }
285 value_type* data() {
return _data; }
286 const value_type* data()
const {
return _data; }
288 inline value_type* data(
size_t i) {
return reinterpret_cast<value_type*
>(
reinterpret_cast<uint8_t*
>(_data) + i *
properties.stride); }
289 inline const value_type* data(
size_t i)
const {
return reinterpret_cast<const value_type*
>(
reinterpret_cast<const uint8_t*
>(_data) + i *
properties.stride); }
291 size_t index(uint32_t i, uint32_t j)
const noexcept {
return static_cast<size_t>(j) * _width + i; }
293 value_type& operator[](
size_t i) {
return *data(i); }
294 const value_type& operator[](
size_t i)
const {
return *data(i); }
296 value_type& at(
size_t i) {
return *data(i); }
297 const value_type& at(
size_t i)
const {
return *data(i); }
299 value_type& operator()(uint32_t i, uint32_t j) {
return *data(index(i, j)); }
300 const value_type& operator()(uint32_t i, uint32_t j)
const {
return *data(index(i, j)); }
302 value_type& at(uint32_t i, uint32_t j) {
return *data(index(i, j)); }
303 const value_type& at(uint32_t i, uint32_t j)
const {
return *data(index(i, j)); }
305 void set(
size_t i,
const value_type& v) { *data(i) = v; }
306 void set(uint32_t i, uint32_t j,
const value_type& v) { *data(index(i, j)) = v; }
308 Data* storage() {
return _storage; }
309 const Data* storage()
const {
return _storage; }
311 iterator begin() {
return iterator{_data,
properties.stride}; }
312 const_iterator begin()
const {
return const_iterator{_data,
properties.stride}; }
314 iterator end() {
return iterator{data(_width * _height),
properties.stride}; }
315 const_iterator end()
const {
return const_iterator{data(_width * _height),
properties.stride}; }
323 value_type* _allocate(
size_t size)
const
328 return new value_type[size];
330 return new (std::malloc(
sizeof(value_type) * size)) value_type[size];
332 return new (vsg::allocate(
sizeof(value_type) * size, ALLOCATOR_AFFINITY_DATA)) value_type[size];
337 if (!_storage && _data)
344 vsg::deallocate(_data);
352 ref_ptr<Data> _storage;
355 VSG_array2D(byteArray2D, int8_t);
356 VSG_array2D(ubyteArray2D, uint8_t);
357 VSG_array2D(shortArray2D, int16_t);
358 VSG_array2D(ushortArray2D, uint16_t);
359 VSG_array2D(intArray2D, int32_t);
360 VSG_array2D(uintArray2D, uint32_t);
361 VSG_array2D(floatArray2D,
float);
362 VSG_array2D(doubleArray2D,
double);
364 VSG_array2D(vec2Array2D, vec2);
365 VSG_array2D(vec3Array2D, vec3);
366 VSG_array2D(vec4Array2D, vec4);
368 VSG_array2D(dvec2Array2D, dvec2);
369 VSG_array2D(dvec3Array2D, dvec3);
370 VSG_array2D(dvec4Array2D, dvec4);
372 VSG_array2D(bvec2Array2D, bvec2);
373 VSG_array2D(bvec3Array2D, bvec3);
374 VSG_array2D(bvec4Array2D, bvec4);
376 VSG_array2D(ubvec2Array2D, ubvec2);
377 VSG_array2D(ubvec3Array2D, ubvec3);
378 VSG_array2D(ubvec4Array2D, ubvec4);
380 VSG_array2D(svec2Array2D, svec2);
381 VSG_array2D(svec3Array2D, svec3);
382 VSG_array2D(svec4Array2D, svec4);
384 VSG_array2D(usvec2Array2D, usvec2);
385 VSG_array2D(usvec3Array2D, usvec3);
386 VSG_array2D(usvec4Array2D, usvec4);
388 VSG_array2D(ivec2Array2D, ivec2);
389 VSG_array2D(ivec3Array2D, ivec3);
390 VSG_array2D(ivec4Array2D, ivec4);
392 VSG_array2D(uivec2Array2D, uivec2);
393 VSG_array2D(uivec3Array2D, uivec3);
394 VSG_array2D(uivec4Array2D, uivec4);
396 VSG_array2D(block64Array2D, block64);
397 VSG_array2D(block128Array2D, block128);
const std::type_info & type_info() const noexcept override
return the std::type_info of this Object
Definition: Array2D.h:105
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