20 #if !defined(__MITSUBA_CORE_BARRAY_H_)
21 #define __MITSUBA_CORE_BARRAY_H_
23 #include <mitsuba/mitsuba.h>
33 template <
typename Value,
size_t logblockSize = 2>
class BlockedArray {
35 static const size_t blockSize = 1 << logblockSize;
45 m_size(-1), m_owner(false) {
54 if (m_data && m_owner)
57 m_xBlocks = (size.x + blockSize - 1) / blockSize;
58 m_yBlocks = (size.y + blockSize - 1) / blockSize;
60 * blockSize * blockSize *
sizeof(Value));
72 if (m_data && m_owner)
75 m_xBlocks = (size.x + blockSize - 1) / blockSize;
76 m_yBlocks = (size.y + blockSize - 1) / blockSize;
77 m_data = (Value *) ptr;
88 template <
typename AltValue>
void init(
const AltValue *data) {
89 for (
int y=0; y<m_size.y; ++y)
90 for (
int x=0; x<m_size.x; ++x)
91 (*
this)(x, y) = Value(*data++);
103 template <
typename AltValue>
void init(
const AltValue *data,
104 AltValue &min_, AltValue &max_, AltValue &avg_) {
105 typedef typename AltValue::Scalar Scalar;
108 min(+std::numeric_limits<Scalar>::infinity()),
109 max(-std::numeric_limits<Scalar>::infinity()),
112 for (
int y=0; y<m_size.y; ++y) {
113 for (
int x=0; x<m_size.x; ++x) {
114 const AltValue &value = *data++;
115 for (
int i=0; i<AltValue::dim; ++i) {
116 min[i] = std::min(min[i], value[i]);
117 max[i] = std::max(max[i], value[i]);
120 (*this)(x, y) = Value(value);
125 avg_ = avg / (Scalar) (m_size.x * m_size.y);
136 template <
typename AltValue>
void copyTo(AltValue *data)
const {
137 for (
int y=0; y<m_size.y; ++y)
138 for (
int x=0; x<m_size.x; ++x)
139 *data++ = AltValue((*
this)(x, y));
151 size_t unusedColsRight = m_xBlocks * blockSize - m_size.x,
152 unusedRowsBottom = m_yBlocks * blockSize - m_size.y;
154 for (
int y=0; y < (int) unusedRowsBottom; ++y)
155 memset(&(this->
operator()(0, y + m_size.y)), 0,
sizeof(Value) * m_xBlocks * blockSize);
157 if (unusedColsRight > 0) {
158 for (
int y=0; y < (int) (m_yBlocks * blockSize); ++y)
159 for (
int x=0; x < (int) unusedColsRight; ++x)
160 memset(&(this->
operator()(x + m_size.x, y)), 0,
sizeof(Value));
169 size_t xBlocks = (size.x + blockSize - 1) / blockSize,
170 yBlocks = (size.y + blockSize - 1) / blockSize,
171 fullWidth = xBlocks * blockSize,
172 fullHeight = yBlocks * blockSize;
173 return fullWidth * fullHeight *
sizeof(Value);
178 return m_xBlocks * m_yBlocks * blockSize * blockSize *
sizeof(Value);
189 if (m_data && m_owner)
195 size_t xb = getBlock(x), yb = getBlock(y),
196 xo = getOffset(x), yo = getOffset(y);
200 blockSize * blockSize * (xb + yb * m_xBlocks) +
208 size_t xb = getBlock(x), yb = getBlock(y),
209 xo = getOffset(x), yo = getOffset(y);
213 blockSize * blockSize * (xb + yb * m_xBlocks) +
223 inline const Value *
getData()
const {
return m_data; }
226 inline size_t getBlock(
int a)
const {
return (
size_t) (a >> logblockSize); }
229 inline size_t getOffset(
int a)
const {
return (
size_t) (a & (blockSize - 1)); }
233 size_t m_xBlocks, m_yBlocks;
254 m_size(-1), m_owner(false) {
263 if (m_data && m_owner)
266 size_t arraySize = (size_t) size.x * (
size_t) size.y *
sizeof(Value);
278 if (m_data && m_owner)
281 m_data = (Value *) ptr;
292 template <
typename AltValue>
void init(
const AltValue *data) {
294 for (
int y=0; y<m_size.y; ++y)
295 for (
int x=0; x<m_size.x; ++x)
296 *ptr++ = Value(*data++);
308 template <
typename AltValue>
void init(
const AltValue *data,
309 AltValue &min_, AltValue &max_, AltValue &avg_) {
310 typedef typename AltValue::Scalar Scalar;
313 min(+std::numeric_limits<Scalar>::infinity()),
314 max(-std::numeric_limits<Scalar>::infinity()),
319 for (
int y=0; y<m_size.y; ++y) {
320 for (
int x=0; x<m_size.x; ++x) {
321 const AltValue &value = *data++;
322 for (
int i=0; i<AltValue::dim; ++i) {
323 min[i] = std::min(min[i], value[i]);
324 max[i] = std::max(max[i], value[i]);
327 *ptr++ = Value(value);
332 avg_ = avg / (Scalar) (m_size.x * m_size.y);
343 template <
typename AltValue>
void copyTo(AltValue *data)
const {
345 for (
int y=0; y<m_size.y; ++y)
346 for (
int x=0; x<m_size.x; ++x)
347 *data++ = AltValue(*ptr++);
364 return (
size_t) size.x * (size_t) size.y *
sizeof(Value);
369 return (
size_t) m_size.x * (size_t) m_size.y *
sizeof(Value);
380 if (m_data && m_owner)
386 return m_data[x + (size_t) m_size.x * y];
391 return m_data[x + (size_t) m_size.x * y];
398 inline const Value *
getData()
const {
return m_data; }
void alloc(const Vector2i &size)
Allocate memory for a blocked array of the specified width and height.
Definition: barray.h:53
const Value * getData() const
Return a pointer to the internal representation (const version)
Definition: barray.h:398
void init(const AltValue *data, AltValue &min_, AltValue &max_, AltValue &avg_)
Initialize the contents of the blocked array with values from a non-blocked source in row-major order...
Definition: barray.h:103
static size_t bufferSize(const Vector2i &size)
Return the hypothetical heap memory requirements of a blocked array for the given size...
Definition: barray.h:168
BlockedArray(const Vector2i &size)
Allocate memory for a new blocked array of the specified width and height.
Definition: barray.h:44
void init(const AltValue *data)
Initialize the contents of the blocked array with values from a non-blocked source in row-major order...
Definition: barray.h:88
~BlockedArray()
Release all memory.
Definition: barray.h:188
LinearArray()
Create an unitialized linear array.
Definition: barray.h:247
const Vector2i & getSize() const
Return the size of the array.
Definition: barray.h:360
const Value & operator()(int x, int y) const
Access the specified entry (const version)
Definition: barray.h:390
void init(const AltValue *data, AltValue &min_, AltValue &max_, AltValue &avg_)
Initialize the contents of the linear array with values from a non-blocked source in row-major order ...
Definition: barray.h:308
Value & operator()(int x, int y)
Access the specified entry.
Definition: barray.h:194
size_t getBufferSize() const
Return the size of the allocated buffer.
Definition: barray.h:177
Blocked generic 2D array data type.
Definition: barray.h:33
size_t getOffset(int a) const
Determine the offset within the block that contains the given global index.
Definition: barray.h:229
~LinearArray()
Release all memory.
Definition: barray.h:379
MTS_EXPORT_CORE void *__restrict allocAligned(size_t size)
Allocate an aligned region of memory.
void map(void *ptr, const Vector2i &size)
Definition: barray.h:277
void cleanup()
Zero out unused memory portions.
Definition: barray.h:356
Linear (i.e. non-blocked) generic 2D array data type.
Definition: barray.h:244
Value * getData()
Return a pointer to the internal representation.
Definition: barray.h:395
void init(const AltValue *data)
Initialize the contents of the linear array with values from a non-blocked source in row-major order...
Definition: barray.h:292
const Value & operator()(int x, int y) const
Access the specified entry (const version)
Definition: barray.h:207
void cleanup()
Zero out unused memory portions.
Definition: barray.h:150
int getHeight() const
Return the height of the array.
Definition: barray.h:185
Value * getData()
Return a pointer to the internal representation.
Definition: barray.h:220
static size_t bufferSize(const Vector2i &size)
Return the hypothetical heap memory requirements of a blocked array for the given size...
Definition: barray.h:363
void copyTo(AltValue *data) const
Copy the contents of the linear array to a non-blocked destination buffer in row-major order...
Definition: barray.h:343
MTS_EXPORT_CORE void freeAligned(void *ptr)
Free an aligned region of memory.
int getWidth() const
Return the width of the array.
Definition: barray.h:182
LinearArray(const Vector2i &size)
Allocate memory for a new linear array of the specified width and height.
Definition: barray.h:253
void map(void *ptr, const Vector2i &size)
Initialize the blocked array with a given pointer and array size.
Definition: barray.h:71
const Value * getData() const
Return a pointer to the internal representation (const version)
Definition: barray.h:223
int getHeight() const
Return the height of the array.
Definition: barray.h:376
void copyTo(AltValue *data) const
Copy the contents of the blocked array to a non-blocked destination buffer in row-major order...
Definition: barray.h:136
size_t getBlock(int a) const
Determine the index of the block which contains the given global index.
Definition: barray.h:226
size_t getBufferSize() const
Return the size of the allocated buffer.
Definition: barray.h:368
void alloc(const Vector2i &size)
Allocate memory for a linear array of the specified width and height.
Definition: barray.h:262
int getWidth() const
Return the width of the array.
Definition: barray.h:373
BlockedArray()
Create an unitialized blocked array.
Definition: barray.h:38
const Vector2i & getSize() const
Return the size of the array.
Definition: barray.h:165
Value & operator()(int x, int y)
Access the specified entry.
Definition: barray.h:385