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_array3D(N, T) \
26 using N = Array3D<T>; \
28 constexpr const char* type_name<N>() noexcept { return "vsg::" #N; }
53 if (_width != 0 && _height != 0 && _depth != 0)
55 _data = _allocate(_width * _height * _depth);
57 for (
auto& v : rhs) *(dest_v++) = v;
62 Array3D(uint32_t width, uint32_t height, uint32_t depth,
Properties in_properties = {}) :
63 Data(in_properties,
sizeof(value_type)),
64 _data(_allocate(width * height * depth)),
72 Array3D(uint32_t width, uint32_t height, uint32_t depth, value_type* data,
Properties in_properties = {}) :
73 Data(in_properties,
sizeof(value_type)),
77 _depth(depth) {
dirty(); }
79 Array3D(uint32_t width, uint32_t height, uint32_t depth,
const value_type& value,
Properties in_properties = {}) :
80 Data(in_properties,
sizeof(value_type)),
81 _data(_allocate(width * height * depth)),
86 for (
auto& v : *
this) v = value;
97 assign(data, offset, stride, width, height, depth, in_properties);
100 template<
typename... Args>
106 std::size_t sizeofObject()
const noexcept
override {
return sizeof(
Array3D); }
107 const char* className()
const noexcept
override {
return type_name<Array3D>(); }
108 const std::type_info&
type_info() const noexcept
override {
return typeid(*this); }
109 bool is_compatible(
const std::type_info& type)
const noexcept
override {
return typeid(
Array3D) == type || Data::is_compatible(type); }
112 void accept(Visitor& visitor)
override;
113 void accept(ConstVisitor& visitor)
const override;
115 void read(Input& input)
override
117 std::size_t original_size = size();
121 uint32_t w = input.readValue<uint32_t>(
"width");
122 uint32_t h = input.readValue<uint32_t>(
"height");
123 uint32_t d = input.readValue<uint32_t>(
"depth");
125 if (
auto data_storage = input.readObject<Data>(
"storage"))
127 uint32_t offset = input.readValue<uint32_t>(
"offset");
132 if (input.matchPropertyName(
"data"))
134 std::size_t new_size = computeValueCountIncludingMipmaps(w, h, d,
properties.maxNumMipmaps);
138 if (original_size != new_size)
141 _data = _allocate(new_size);
146 _data = _allocate(new_size);
155 if (_data) input.read(new_size, _data);
161 void write(Output& output)
const override
165 output.writeValue<uint32_t>(
"width", _width);
166 output.writeValue<uint32_t>(
"height", _height);
167 output.writeValue<uint32_t>(
"depth", _depth);
169 output.writeObject(
"storage", _storage);
172 auto offset = (
reinterpret_cast<uintptr_t
>(_data) -
reinterpret_cast<uintptr_t
>(_storage->dataPointer()));
173 output.writeValue<uint32_t>(
"offset", offset);
177 output.writePropertyName(
"data");
178 output.write(valueCount(), _data);
179 output.writeEndOfLine();
182 std::size_t size()
const {
return (
properties.maxNumMipmaps <= 1) ? (
static_cast<std::size_t
>(_width) * _height * _depth) : computeValueCountIncludingMipmaps(_width, _height, _depth,
properties.maxNumMipmaps); }
184 bool available()
const {
return _data !=
nullptr; }
185 bool empty()
const {
return _data ==
nullptr; }
198 Array3D& operator=(
const Array3D& rhs)
200 if (&rhs ==
this)
return *
this;
206 _height = rhs._height;
209 if (_width != 0 && _height != 0 && _depth != 0)
211 _data = _allocate(_width * _height * _depth);
213 for (
auto& v : rhs) *(dest_v++) = v;
221 void assign(uint32_t width, uint32_t height, uint32_t depth, value_type* data, Properties in_properties = {})
236 void assign(ref_ptr<Data> storage, uint32_t offset, uint32_t stride, uint32_t width, uint32_t height, uint32_t depth, Properties in_properties = {})
243 if (_storage && _storage->dataPointer())
245 _data =
reinterpret_cast<value_type*
>(
reinterpret_cast<uint8_t*
>(_storage->dataPointer()) + offset);
263 void* dataRelease()
override
280 std::size_t valueSize()
const override {
return sizeof(value_type); }
281 std::size_t valueCount()
const override {
return size(); }
283 bool dataAvailable()
const override {
return available(); }
284 std::size_t dataSize()
const override {
return size() *
properties.stride; }
286 void* dataPointer()
override {
return _data; }
287 const void* dataPointer()
const override {
return _data; }
289 void* dataPointer(std::size_t i)
override {
return data(i); }
290 const void* dataPointer(std::size_t i)
const override {
return data(i); }
292 uint32_t dimensions()
const override {
return 3; }
294 uint32_t width()
const override {
return _width; }
295 uint32_t height()
const override {
return _height; }
296 uint32_t depth()
const override {
return _depth; }
298 value_type* data() {
return _data; }
299 const value_type* data()
const {
return _data; }
301 inline value_type* data(std::size_t i) {
return reinterpret_cast<value_type*
>(
reinterpret_cast<uint8_t*
>(_data) + i *
properties.stride); }
302 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); }
304 std::size_t index(uint32_t i, uint32_t j, uint32_t k)
const noexcept {
return static_cast<std::size_t
>(k * _width * _height + j * _width + i); }
306 value_type& operator[](std::size_t i) {
return *data(i); }
307 const value_type& operator[](std::size_t i)
const {
return *data(i); }
309 value_type& at(std::size_t i) {
return *data(i); }
310 const value_type& at(std::size_t i)
const {
return *data(i); }
312 value_type& operator()(uint32_t i, uint32_t j, uint32_t k) {
return *data(index(i, j, k)); }
313 const value_type& operator()(uint32_t i, uint32_t j, uint32_t k)
const {
return *data(index(i, j, k)); }
315 value_type& at(uint32_t i, uint32_t j, uint32_t k) {
return *data(index(i, j, k)); }
316 const value_type& at(uint32_t i, uint32_t j, uint32_t k)
const {
return *data(index(i, j, k)); }
318 void set(std::size_t i,
const value_type& v) { *data(i) = v; }
319 void set(uint32_t i, uint32_t j, uint32_t k,
const value_type& v) { *data(index(i, j, k)) = v; }
321 Data* storage() {
return _storage; }
322 const Data* storage()
const {
return _storage; }
324 iterator begin() {
return iterator{_data,
properties.stride}; }
325 const_iterator begin()
const {
return const_iterator{_data,
properties.stride}; }
327 iterator end() {
return iterator{data(_width * _height * _depth),
properties.stride}; }
328 const_iterator end()
const {
return const_iterator{data(_width * _height * _depth),
properties.stride}; }
336 value_type* _allocate(
size_t size)
const
341 return new value_type[size];
343 return new (std::malloc(
sizeof(value_type) * size)) value_type[size];
345 return new (vsg::allocate(
sizeof(value_type) * size, ALLOCATOR_AFFINITY_DATA)) value_type[size];
350 if (!_storage && _data)
357 vsg::deallocate(_data);
366 ref_ptr<Data> _storage;
369 VSG_array3D(byteArray3D, int8_t);
370 VSG_array3D(ubyteArray3D, uint8_t);
371 VSG_array3D(shortArray3D, int16_t);
372 VSG_array3D(ushortArray3D, uint16_t);
373 VSG_array3D(intArray3D, int32_t);
374 VSG_array3D(uintArray3D, uint32_t);
375 VSG_array3D(floatArray3D,
float);
376 VSG_array3D(doubleArray3D,
double);
378 VSG_array3D(vec2Array3D, vec2);
379 VSG_array3D(vec3Array3D, vec3);
380 VSG_array3D(vec4Array3D, vec4);
382 VSG_array3D(dvec2Array3D, dvec2);
383 VSG_array3D(dvec3Array3D, dvec3);
384 VSG_array3D(dvec4Array3D, dvec4);
386 VSG_array3D(ubvec2Array3D, ubvec2);
387 VSG_array3D(ubvec3Array3D, ubvec3);
388 VSG_array3D(ubvec4Array3D, ubvec4);
390 VSG_array3D(block64Array3D, block64);
391 VSG_array3D(block128Array3D, block128);
const std::type_info & type_info() const noexcept override
return the std::type_info of this Object
Definition: Array3D.h:108
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
AllocatorType allocatorType
hint as how the data values may change during the lifetime of the vsg::Data.
Definition: Data.h:125