1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright 2011, Blender Foundation.
*/
#pragma once
#include "COM_BufferArea.h"
#include "COM_BufferRange.h"
#include "COM_BuffersIterator.h"
#include "COM_Enums.h"
#include "BLI_math_interp.h"
#include "BLI_rect.h"
struct ImBuf;
namespace blender::compositor {
/**
* \brief state of a memory buffer
* \ingroup Memory
*/
enum class MemoryBufferState {
/** \brief memory has been allocated on creator device and CPU machine,
* but kernel has not been executed */
Default = 0,
/** \brief chunk is consolidated from other chunks. special state. */
Temporary = 6,
};
enum class MemoryBufferExtend {
Clip,
Extend,
Repeat,
};
class MemoryProxy;
/**
* \brief a MemoryBuffer contains access to the data of a chunk
*/
class MemoryBuffer {
public:
/**
* Offset between elements.
*
* Should always be used for the x dimension when calculating buffer offsets.
* It will be 0 when is_a_single_elem=true.
* e.g: buffer_index = y * buffer.row_stride + x * buffer.elem_stride
*/
int elem_stride;
/**
* Offset between rows.
*
* Should always be used for the y dimension when calculating buffer offsets.
* It will be 0 when is_a_single_elem=true.
* e.g: buffer_index = y * buffer.row_stride + x * buffer.elem_stride
*/
int row_stride;
private:
/**
* \brief proxy of the memory (same for all chunks in the same buffer)
*/
MemoryProxy *m_memoryProxy;
/**
* \brief the type of buffer DataType::Value, DataType::Vector, DataType::Color
*/
DataType m_datatype;
/**
* \brief region of this buffer inside relative to the MemoryProxy
*/
rcti m_rect;
/**
* \brief state of the buffer
*/
MemoryBufferState m_state;
/**
* \brief the actual float buffer/data
*/
float *m_buffer;
/**
* \brief the number of channels of a single value in the buffer.
* For value buffers this is 1, vector 3 and color 4
*/
uint8_t m_num_channels;
/**
* Whether buffer is a single element in memory.
*/
bool m_is_a_single_elem;
/**
* Whether MemoryBuffer owns buffer data.
*/
bool owns_data_;
/** Stride to make any x coordinate within buffer positive (non-zero). */
int to_positive_x_stride_;
/** Stride to make any y coordinate within buffer positive (non-zero). */
int to_positive_y_stride_;
public:
/**
* \brief construct new temporarily MemoryBuffer for an area
*/
MemoryBuffer(MemoryProxy *memoryProxy, const rcti &rect, MemoryBufferState state);
/**
* \brief construct new temporarily MemoryBuffer for an area
*/
MemoryBuffer(DataType datatype, const rcti &rect, bool is_a_single_elem = false);
MemoryBuffer(
float *buffer, int num_channels, int width, int height, bool is_a_single_elem = false);
MemoryBuffer(float *buffer, int num_channels, const rcti &rect, bool is_a_single_elem = false);
/**
* Copy constructor
*/
MemoryBuffer(const MemoryBuffer &src);
/**
* \brief destructor
*/
~MemoryBuffer();
/**
* Whether buffer is a single element in memory independently of its resolution. True for set
* operations buffers.
*/
bool is_a_single_elem() const
{
return m_is_a_single_elem;
}
float &operator[](int index)
{
BLI_assert(m_is_a_single_elem ? index < m_num_channels :
index < get_coords_offset(getWidth(), getHeight()));
return m_buffer[index];
}
const float &operator[](int index) const
{
BLI_assert(m_is_a_single_elem ? index < m_num_channels :
index < get_coords_offset(getWidth(), getHeight()));
return m_buffer[index];
}
/**
* Get offset needed to jump from buffer start to given coordinates.
*/
intptr_t get_coords_offset(int x, int y) const
{
return ((intptr_t)y - m_rect.ymin) * row_stride + ((intptr_t)x - m_rect.xmin) * elem_stride;
}
/**
* Get buffer element at given coordinates.
*/
float *get_elem(int x, int y)
{
BLI_assert(has_coords(x, y));
return m_buffer + get_coords_offset(x, y);
}
/**
* Get buffer element at given coordinates.
*/
const float *get_elem(int x, int y) const
{
BLI_assert(has_coords(x, y));
return m_buffer + get_coords_offset(x, y);
}
void read_elem(int x, int y, float *out) const
{
memcpy(out, get_elem(x, y), get_elem_bytes_len());
}
void read_elem_checked(int x, int y, float *out) const
{
if (!has_coords(x, y)) {
clear_elem(out);
}
else {
read_elem(x, y, out);
}
}
void read_elem_checked(float x, float y, float *out) const
{
read_elem_checked(floor_x(x), floor_y(y), out);
}
void read_elem_bilinear(float x, float y, float *out) const
{
/* Only clear past +/-1 borders to be able to smooth edges. */
if (x <= m_rect.xmin - 1.0f || x >= m_rect.xmax || y <= m_rect.ymin - 1.0f ||
y >= m_rect.ymax) {
clear_elem(out);
return;
}
if (m_is_a_single_elem) {
if (x >= m_rect.xmin && x < m_rect.xmax - 1.0f && y >= m_rect.ymin &&
y < m_rect.ymax - 1.0f) {
memcpy(out, m_buffer, get_elem_bytes_len());
return;
}
/* Do sampling at borders to smooth edges. */
const float last_x = getWidth() - 1.0f;
const float rel_x = get_relative_x(x);
float single_x = 0.0f;
if (rel_x < 0.0f) {
single_x = rel_x;
}
else if (rel_x > last_x) {
single_x = rel_x - last_x;
}
const float last_y = getHeight() - 1.0f;
const float rel_y = get_relative_y(y);
float single_y = 0.0f;
if (rel_y < 0.0f) {
single_y = rel_y;
}
else if (rel_y > last_y) {
single_y = rel_y - last_y;
}
BLI_bilinear_interpolation_fl(m_buffer, out, 1, 1, m_num_channels, single_x, single_y);
return;
}
BLI_bilinear_interpolation_fl(m_buffer,
out,
getWidth(),
getHeight(),
m_num_channels,
get_relative_x(x),
get_relative_y(y));
}
void read_elem_sampled(float x, float y, PixelSampler sampler, float *out) const
{
switch (sampler) {
case PixelSampler::Nearest:
read_elem_checked(x, y, out);
break;
case PixelSampler::Bilinear:
case PixelSampler::Bicubic:
/* No bicubic. Current implementation produces fuzzy results. */
read_elem_bilinear(x, y, out);
break;
}
}
void read_elem_filtered(
const float x, const float y, float dx[2], float dy[2], float *out) const;
/**
* Get channel value at given coordinates.
*/
float &get_value(int x, int y, int channel)
{
BLI_assert(has_coords(x, y) && channel >= 0 && channel < m_num_channels);
return m_buffer[get_coords_offset(x, y) + channel];
}
/**
* Get channel value at given coordinates.
*/
const float &get_value(int x, int y, int channel) const
{
BLI_assert(has_coords(x, y) && channel >= 0 && channel < m_num_channels);
return m_buffer[get_coords_offset(x, y) + channel];
}
/**
* Get the buffer row end.
*/
const float *get_row_end(int y) const
{
BLI_assert(has_y(y));
return m_buffer + (is_a_single_elem() ? m_num_channels : get_coords_offset(getWidth(), y));
}
/**
* Get the number of elements in memory for a row. For single element buffers it will always
* be 1.
*/
int get_memory_width() const
{
return is_a_single_elem() ? 1 : getWidth();
}
/**
* Get number of elements in memory for a column. For single element buffers it will
* always be 1.
*/
int get_memory_height() const
{
return is_a_single_elem() ? 1 : getHeight();
}
uint8_t get_num_channels() const
{
return m_num_channels;
}
uint8_t get_elem_bytes_len() const
{
return m_num_channels * sizeof(float);
}
/**
* Get all buffer elements as a range with no offsets.
*/
BufferRange<float> as_range()
{
return BufferRange<float>(m_buffer, 0, buffer_len(), elem_stride);
}
BufferRange<const float> as_range() const
{
return BufferRange<const float>(m_buffer, 0, buffer_len(), elem_stride);
}
BufferArea<float> get_buffer_area(const rcti &area)
{
return BufferArea<float>(m_buffer, getWidth(), area, elem_stride);
}
BufferArea<const float> get_buffer_area(const rcti &area) const
{
return BufferArea<const float>(m_buffer, getWidth(), area, elem_stride);
}
BuffersIterator<float> iterate_with(Span<MemoryBuffer *> inputs);
BuffersIterator<float> iterate_with(Span<MemoryBuffer *> inputs, const rcti &area);
/**
* \brief get the data of this MemoryBuffer
* \note buffer should already be available in memory
*/
float *getBuffer()
{
return m_buffer;
}
float *release_ownership_buffer()
{
owns_data_ = false;
return m_buffer;
}
MemoryBuffer *inflate() const;
inline void wrap_pixel(int &x, int &y, MemoryBufferExtend extend_x, MemoryBufferExtend extend_y)
{
const int w = getWidth();
const int h = getHeight();
x = x - m_rect.xmin;
y = y - m_rect.ymin;
switch (extend_x) {
case MemoryBufferExtend::Clip:
break;
case MemoryBufferExtend::Extend:
if (x < 0) {
x = 0;
}
if (x >= w) {
x = w - 1;
}
break;
case MemoryBufferExtend::Repeat:
x %= w;
if (x < 0) {
x += w;
}
break;
}
switch (extend_y) {
case MemoryBufferExtend::Clip:
break;
case MemoryBufferExtend::Extend:
if (y < 0) {
y = 0;
}
if (y >= h) {
y = h - 1;
}
break;
case MemoryBufferExtend::Repeat:
y %= h;
if (y < 0) {
y += h;
}
break;
}
x = x + m_rect.xmin;
y = y + m_rect.ymin;
}
inline void wrap_pixel(float &x,
float &y,
MemoryBufferExtend extend_x,
MemoryBufferExtend extend_y) const
{
const float w = (float)getWidth();
const float h = (float)getHeight();
x = x - m_rect.xmin;
y = y - m_rect.ymin;
switch (extend_x) {
case MemoryBufferExtend::Clip:
break;
case MemoryBufferExtend::Extend:
if (x < 0) {
x = 0.0f;
}
if (x >= w) {
x = w - 1;
}
break;
case MemoryBufferExtend::Repeat:
x = fmodf(x, w);
if (x < 0.0f) {
x += w;
}
break;
}
switch (extend_y) {
case MemoryBufferExtend::Clip:
break;
case MemoryBufferExtend::Extend:
if (y < 0) {
y = 0.0f;
}
if (y >= h) {
y = h - 1;
}
break;
case MemoryBufferExtend::Repeat:
y = fmodf(y, h);
if (y < 0.0f) {
y += h;
}
break;
}
x = x + m_rect.xmin;
y = y + m_rect.ymin;
}
/* TODO(manzanilla): to be removed with tiled implementation. For applying #MemoryBufferExtend
* use #wrap_pixel. */
inline void read(float *result,
int x,
int y,
MemoryBufferExtend extend_x = MemoryBufferExtend::Clip,
MemoryBufferExtend extend_y = MemoryBufferExtend::Clip)
{
bool clip_x = (extend_x == MemoryBufferExtend::Clip && (x < m_rect.xmin || x >= m_rect.xmax));
bool clip_y = (extend_y == MemoryBufferExtend::Clip && (y < m_rect.ymin || y >= m_rect.ymax));
if (clip_x || clip_y) {
/* clip result outside rect is zero */
memset(result, 0, m_num_channels * sizeof(float));
}
else {
int u = x;
int v = y;
this->wrap_pixel(u, v, extend_x, extend_y);
const int offset = get_coords_offset(u, v);
float *buffer = &m_buffer[offset];
memcpy(result, buffer, sizeof(float) * m_num_channels);
}
}
/* TODO(manzanilla): to be removed with tiled implementation. */
inline void readNoCheck(float *result,
int x,
int y,
MemoryBufferExtend extend_x = MemoryBufferExtend::Clip,
MemoryBufferExtend extend_y = MemoryBufferExtend::Clip)
{
int u = x;
int v = y;
this->wrap_pixel(u, v, extend_x, extend_y);
const int offset = get_coords_offset(u, v);
BLI_assert(offset >= 0);
BLI_assert(offset < this->buffer_len() * m_num_channels);
BLI_assert(!(extend_x == MemoryBufferExtend::Clip && (u < m_rect.xmin || u >= m_rect.xmax)) &&
!(extend_y == MemoryBufferExtend::Clip && (v < m_rect.ymin || v >= m_rect.ymax)));
float *buffer = &m_buffer[offset];
memcpy(result, buffer, sizeof(float) * m_num_channels);
}
void writePixel(int x, int y, const float color[4]);
void addPixel(int x, int y, const float color[4]);
inline void readBilinear(float *result,
float x,
float y,
MemoryBufferExtend extend_x = MemoryBufferExtend::Clip,
MemoryBufferExtend extend_y = MemoryBufferExtend::Clip) const
{
float u = x;
float v = y;
this->wrap_pixel(u, v, extend_x, extend_y);
if ((extend_x != MemoryBufferExtend::Repeat && (u < 0.0f || u >= getWidth())) ||
(extend_y != MemoryBufferExtend::Repeat && (v < 0.0f || v >= getHeight()))) {
copy_vn_fl(result, m_num_channels, 0.0f);
return;
}
if (m_is_a_single_elem) {
memcpy(result, m_buffer, sizeof(float) * m_num_channels);
}
else {
BLI_bilinear_interpolation_wrap_fl(m_buffer,
result,
getWidth(),
getHeight(),
m_num_channels,
u,
v,
extend_x == MemoryBufferExtend::Repeat,
extend_y == MemoryBufferExtend::Repeat);
}
}
void readEWA(float *result, const float uv[2], const float derivatives[2][2]);
/**
* \brief is this MemoryBuffer a temporarily buffer (based on an area, not on a chunk)
*/
inline bool isTemporarily() const
{
return m_state == MemoryBufferState::Temporary;
}
void copy_from(const MemoryBuffer *src, const rcti &area);
void copy_from(const MemoryBuffer *src, const rcti &area, int to_x, int to_y);
void copy_from(const MemoryBuffer *src,
const rcti &area,
int channel_offset,
int elem_size,
int to_channel_offset);
void copy_from(const MemoryBuffer *src,
const rcti &area,
int channel_offset,
int elem_size,
int to_x,
int to_y,
int to_channel_offset);
void copy_from(const uchar *src, const rcti &area);
void copy_from(const uchar *src,
const rcti &area,
int channel_offset,
int elem_size,
int elem_stride,
int row_stride,
int to_channel_offset);
void copy_from(const uchar *src,
const rcti &area,
int channel_offset,
int elem_size,
int elem_stride,
int row_stride,
int to_x,
int to_y,
int to_channel_offset);
void copy_from(const struct ImBuf *src, const rcti &area, bool ensure_linear_space = false);
void copy_from(const struct ImBuf *src,
const rcti &area,
int channel_offset,
int elem_size,
int to_channel_offset,
bool ensure_linear_space = false);
void copy_from(const struct ImBuf *src,
const rcti &src_area,
int channel_offset,
int elem_size,
int to_x,
int to_y,
int to_channel_offset,
bool ensure_linear_space = false);
void fill(const rcti &area, const float *value);
void fill(const rcti &area, int channel_offset, const float *value, int value_size);
/**
* \brief add the content from otherBuffer to this MemoryBuffer
* \param otherBuffer: source buffer
*
* \note take care when running this on a new buffer since it won't fill in
* uninitialized values in areas where the buffers don't overlap.
*/
void fill_from(const MemoryBuffer &src);
/**
* \brief get the rect of this MemoryBuffer
*/
const rcti &get_rect() const
{
return m_rect;
}
/**
* \brief get the width of this MemoryBuffer
*/
const int getWidth() const
{
return BLI_rcti_size_x(&m_rect);
}
/**
* \brief get the height of this MemoryBuffer
*/
const int getHeight() const
{
return BLI_rcti_size_y(&m_rect);
}
/**
* \brief clear the buffer. Make all pixels black transparent.
*/
void clear();
float get_max_value() const;
float get_max_value(const rcti &rect) const;
private:
void set_strides();
const int buffer_len() const
{
return get_memory_width() * get_memory_height();
}
void clear_elem(float *out) const
{
memset(out, 0, m_num_channels * sizeof(float));
}
template<typename T> T get_relative_x(T x) const
{
return x - m_rect.xmin;
}
template<typename T> T get_relative_y(T y) const
{
return y - m_rect.ymin;
}
template<typename T> bool has_coords(T x, T y) const
{
return has_x(x) && has_y(y);
}
template<typename T> bool has_x(T x) const
{
return x >= m_rect.xmin && x < m_rect.xmax;
}
template<typename T> bool has_y(T y) const
{
return y >= m_rect.ymin && y < m_rect.ymax;
}
/* Fast `floor(..)` functions. The caller should check result is within buffer bounds.
* It `ceil(..)` in near cases and when given coordinate
* is negative and less than buffer rect `min - 1`. */
int floor_x(float x) const
{
return (int)(x + to_positive_x_stride_) - to_positive_x_stride_;
}
int floor_y(float y) const
{
return (int)(y + to_positive_y_stride_) - to_positive_y_stride_;
}
void copy_single_elem_from(const MemoryBuffer *src,
int channel_offset,
int elem_size,
const int to_channel_offset);
void copy_rows_from(const MemoryBuffer *src,
const rcti &src_area,
const int to_x,
const int to_y);
void copy_elems_from(const MemoryBuffer *src,
const rcti &area,
const int channel_offset,
const int elem_size,
const int to_x,
const int to_y,
const int to_channel_offset);
#ifdef WITH_CXX_GUARDEDALLOC
MEM_CXX_CLASS_ALLOC_FUNCS("COM:MemoryBuffer")
#endif
};
} // namespace blender::compositor
|