33 class VSG_TEMPLATE_DECLSPEC Array2D :
public Data
45 Array2D(
const Array2D& rhs,
const CopyOp copyop = {}) :
51 _data = _allocate(
static_cast<size_t>(_width) *
static_cast<size_t>(_height));
55 for (
const auto& v : rhs) *(dest_v++) = v;
60 Array2D(uint32_t width, uint32_t height,
Properties in_properties = {}) :
61 Data(in_properties,
sizeof(value_type)),
66 _data = _allocate(
static_cast<size_t>(_width) *
static_cast<size_t>(_height));
70 Array2D(uint32_t width, uint32_t height, value_type* data,
Properties in_properties = {},
MipmapLayout* mipmapLayout =
nullptr) :
71 Data(in_properties,
sizeof(value_type)),
80 Array2D(uint32_t width, uint32_t height,
const value_type& value,
Properties in_properties = {}) :
81 Data(in_properties,
sizeof(value_type)),
86 _data = _allocate(
static_cast<size_t>(_width) *
static_cast<size_t>(_height));
89 for (
auto& v : *
this) v = value;
100 assign(data, offset, stride, width, height, in_properties, mipmapLayout);
103 template<
typename... Args>
109 template<
typename... Args>
123 size_t sizeofObject() const noexcept
override {
return sizeof(Array2D); }
124 const char* className() const noexcept
override {
return type_name<Array2D>(); }
125 const std::type_info&
type_info() const noexcept
override {
return typeid(*this); }
126 bool is_compatible(
const std::type_info& type)
const noexcept override {
return typeid(
Array2D) == type || Data::is_compatible(type); }
129 void accept(Visitor& visitor)
override;
130 void accept(ConstVisitor& visitor)
const override;
132 void read(Input& input)
override
134 size_t original_size = size();
138 uint32_t w = input.readValue<uint32_t>(
"width");
139 uint32_t h = input.readValue<uint32_t>(
"height");
141 if (
auto data_storage = input.readObject<Data>(
"storage"))
143 uint32_t offset = input.readValue<uint32_t>(
"offset");
144 assign(data_storage, offset, properties.stride, w, h, properties);
148 if (input.matchPropertyName(
"data"))
150 properties.stride =
sizeof(value_type);
155 size_t new_size = computeValueCountIncludingMipmaps();
159 if (original_size != new_size)
162 _data = _allocate(new_size);
167 _data = _allocate(new_size);
170 if (_data) input.read(new_size, _data);
176 void write(Output& output)
const override
180 output.writeValue<uint32_t>(
"width", _width);
181 output.writeValue<uint32_t>(
"height", _height);
183 output.writeObject(
"storage", _storage);
186 auto offset = (
reinterpret_cast<uintptr_t
>(_data) -
reinterpret_cast<uintptr_t
>(_storage->dataPointer()));
187 output.writeValue<uint32_t>(
"offset", offset);
191 output.writePropertyName(
"data");
192 output.write(valueCount(), _data);
193 output.writeEndOfLine();
196 size_t size()
const {
return (properties.mipLevels <= 1) ? (
static_cast<size_t>(_width) *
static_cast<size_t>(_height)) : computeValueCountIncludingMipmaps(); }
198 bool available()
const {
return _data !=
nullptr; }
199 bool empty()
const {
return _data ==
nullptr; }
211 Array2D& operator=(
const Array2D& rhs)
213 if (&rhs ==
this)
return *
this;
220 _height = rhs._height;
222 _data = _allocate(
static_cast<size_t>(_width) *
static_cast<size_t>(_height));
226 for (
const auto& v : rhs) *(dest_v++) = v;
234 void assign(uint32_t width, uint32_t height, value_type* data, Properties in_properties = {}, MipmapLayout* mipmapLayout =
nullptr)
238 properties = in_properties;
239 properties.stride =
sizeof(value_type);
245 setMipmapLayout(mipmapLayout);
250 void assign(ref_ptr<Data> storage, uint32_t offset, uint32_t stride, uint32_t width, uint32_t height, Properties in_properties = {}, MipmapLayout* mipmapLayout =
nullptr)
255 properties = in_properties;
256 properties.stride = stride;
257 if (_storage && _storage->dataPointer())
259 _data =
reinterpret_cast<value_type*
>(
reinterpret_cast<uint8_t*
>(_storage->dataPointer()) + offset);
270 setMipmapLayout(mipmapLayout);
277 void* dataRelease()
override
293 size_t valueSize()
const override {
return sizeof(value_type); }
294 size_t valueCount()
const override {
return size(); }
296 bool dataAvailable()
const override {
return available(); }
297 size_t dataSize()
const override {
return size() * properties.stride; }
299 void* dataPointer()
override {
return _data; }
300 const void* dataPointer()
const override {
return _data; }
302 void* dataPointer(
size_t i)
override {
return data(i); }
303 const void* dataPointer(
size_t i)
const override {
return data(i); }
305 uint32_t dimensions()
const override {
return 2; }
306 uint32_t width()
const override {
return _width; }
307 uint32_t height()
const override {
return _height; }
308 uint32_t depth()
const override {
return 1; }
310 value_type* data() {
return _data; }
311 const value_type* data()
const {
return _data; }
313 inline value_type* data(
size_t i) {
return reinterpret_cast<value_type*
>(
reinterpret_cast<uint8_t*
>(_data) + i *
static_cast<size_t>(properties.stride)); }
314 inline const value_type* data(
size_t i)
const {
return reinterpret_cast<const value_type*
>(
reinterpret_cast<const uint8_t*
>(_data) + i *
static_cast<size_t>(properties.stride)); }
316 size_t index(uint32_t i, uint32_t j)
const noexcept {
return static_cast<size_t>(j) *
static_cast<size_t>(_width) +
static_cast<size_t>(i); }
318 value_type& operator[](
size_t i) {
return *data(i); }
319 const value_type& operator[](
size_t i)
const {
return *data(i); }
321 value_type& at(
size_t i) {
return *data(i); }
322 const value_type& at(
size_t i)
const {
return *data(i); }
324 value_type& operator()(uint32_t i, uint32_t j) {
return *data(index(i, j)); }
325 const value_type& operator()(uint32_t i, uint32_t j)
const {
return *data(index(i, j)); }
327 value_type& at(uint32_t i, uint32_t j) {
return *data(index(i, j)); }
328 const value_type& at(uint32_t i, uint32_t j)
const {
return *data(index(i, j)); }
330 void set(
size_t i,
const value_type& v) { *data(i) = v; }
331 void set(uint32_t i, uint32_t j,
const value_type& v) { *data(index(i, j)) = v; }
333 Data* storage() {
return _storage; }
334 const Data* storage()
const {
return _storage; }
336 iterator begin() {
return iterator{_data, properties.stride}; }
337 const_iterator begin()
const {
return const_iterator{_data, properties.stride}; }
339 iterator end() {
return iterator{data(_width * _height), properties.stride}; }
340 const_iterator end()
const {
return const_iterator{data(_width * _height), properties.stride}; }
348 value_type* _allocate(
size_t size)
const
352 else if (properties.allocatorType == ALLOCATOR_TYPE_NEW_DELETE)
353 return new value_type[size];
354 else if (properties.allocatorType == ALLOCATOR_TYPE_MALLOC_FREE)
355 return new (std::malloc(
sizeof(value_type) * size)) value_type[size];
357 return new (vsg::allocate(
sizeof(value_type) * size, ALLOCATOR_AFFINITY_DATA)) value_type[size];
362 if (!_storage && _data)
364 if (properties.allocatorType == ALLOCATOR_TYPE_NEW_DELETE)
366 else if (properties.allocatorType == ALLOCATOR_TYPE_MALLOC_FREE)
368 else if (properties.allocatorType != 0)
369 vsg::deallocate(_data);
379 ref_ptr<Data> _storage;