Cleanup: MemoryBuffer do not store width and height.

This commit is contained in:
Jeroen Bakker 2021-03-19 16:45:29 +01:00
parent 8cb1089795
commit 31d5c5078c
3 changed files with 41 additions and 61 deletions

View File

@ -33,30 +33,14 @@ static unsigned int determine_num_channels(DataType datatype)
}
}
unsigned int MemoryBuffer::determineBufferSize()
{
return getWidth() * getHeight();
}
int MemoryBuffer::getWidth() const
{
return this->m_width;
}
int MemoryBuffer::getHeight() const
{
return this->m_height;
}
MemoryBuffer::MemoryBuffer(MemoryProxy *memoryProxy, unsigned int chunkNumber, const rcti &rect)
{
m_rect = rect;
this->m_width = BLI_rcti_size_x(&this->m_rect);
this->m_height = BLI_rcti_size_y(&this->m_rect);
this->m_memoryProxy = memoryProxy;
this->m_chunkNumber = chunkNumber;
this->m_num_channels = determine_num_channels(memoryProxy->getDataType());
this->m_buffer = (float *)MEM_mallocN_aligned(
sizeof(float) * determineBufferSize() * this->m_num_channels, 16, "COM_MemoryBuffer");
sizeof(float) * buffer_len() * this->m_num_channels, 16, "COM_MemoryBuffer");
this->m_state = COM_MB_ALLOCATED;
this->m_datatype = memoryProxy->getDataType();
}
@ -64,13 +48,11 @@ MemoryBuffer::MemoryBuffer(MemoryProxy *memoryProxy, unsigned int chunkNumber, c
MemoryBuffer::MemoryBuffer(MemoryProxy *memoryProxy, const rcti &rect)
{
m_rect = rect;
this->m_width = BLI_rcti_size_x(&this->m_rect);
this->m_height = BLI_rcti_size_y(&this->m_rect);
this->m_memoryProxy = memoryProxy;
this->m_chunkNumber = -1;
this->m_num_channels = determine_num_channels(memoryProxy->getDataType());
this->m_buffer = (float *)MEM_mallocN_aligned(
sizeof(float) * determineBufferSize() * this->m_num_channels, 16, "COM_MemoryBuffer");
sizeof(float) * buffer_len() * this->m_num_channels, 16, "COM_MemoryBuffer");
this->m_state = COM_MB_TEMPORARILY;
this->m_datatype = memoryProxy->getDataType();
}
@ -78,37 +60,29 @@ MemoryBuffer::MemoryBuffer(MemoryProxy *memoryProxy, const rcti &rect)
MemoryBuffer::MemoryBuffer(DataType dataType, const rcti &rect)
{
m_rect = rect;
this->m_width = BLI_rcti_size_x(&this->m_rect);
this->m_height = BLI_rcti_size_y(&this->m_rect);
this->m_height = this->m_rect.ymax - this->m_rect.ymin;
this->m_memoryProxy = nullptr;
this->m_chunkNumber = -1;
this->m_num_channels = determine_num_channels(dataType);
this->m_buffer = (float *)MEM_mallocN_aligned(
sizeof(float) * determineBufferSize() * this->m_num_channels, 16, "COM_MemoryBuffer");
sizeof(float) * buffer_len() * this->m_num_channels, 16, "COM_MemoryBuffer");
this->m_state = COM_MB_TEMPORARILY;
this->m_datatype = dataType;
}
MemoryBuffer::MemoryBuffer(const MemoryBuffer &src) : MemoryBuffer(src.m_memoryProxy, src.m_rect)
{
memcpy(m_buffer, src.m_buffer, determineBufferSize() * m_num_channels * sizeof(float));
memcpy(m_buffer, src.m_buffer, buffer_len() * m_num_channels * sizeof(float));
}
// MemoryBuffer *MemoryBuffer::duplicate()
// {
// return new MemoryBuffer(*this);
// }
void MemoryBuffer::clear()
{
memset(this->m_buffer, 0, this->determineBufferSize() * this->m_num_channels * sizeof(float));
memset(this->m_buffer, 0, this->buffer_len() * this->m_num_channels * sizeof(float));
}
float MemoryBuffer::getMaximumValue()
float MemoryBuffer::get_max_value() const
{
float result = this->m_buffer[0];
const unsigned int size = this->determineBufferSize();
const unsigned int size = this->buffer_len();
unsigned int i;
const float *fp_src = this->m_buffer;
@ -123,17 +97,17 @@ float MemoryBuffer::getMaximumValue()
return result;
}
float MemoryBuffer::getMaximumValue(rcti *rect)
float MemoryBuffer::get_max_value(const rcti &rect) const
{
rcti rect_clamp;
/* first clamp the rect by the bounds or we get un-initialized values */
BLI_rcti_isect(rect, &this->m_rect, &rect_clamp);
BLI_rcti_isect(&rect, &this->m_rect, &rect_clamp);
if (!BLI_rcti_is_empty(&rect_clamp)) {
MemoryBuffer temp_buffer(this->m_datatype, rect_clamp);
temp_buffer.fill_from(*this);
return temp_buffer.getMaximumValue();
return temp_buffer.get_max_value();
}
BLI_assert(0);
@ -159,9 +133,9 @@ void MemoryBuffer::fill_from(const MemoryBuffer &src)
int otherOffset;
for (otherY = minY; otherY < maxY; otherY++) {
otherOffset = ((otherY - src.m_rect.ymin) * src.m_width + minX - src.m_rect.xmin) *
otherOffset = ((otherY - src.m_rect.ymin) * src.getWidth() + minX - src.m_rect.xmin) *
this->m_num_channels;
offset = ((otherY - this->m_rect.ymin) * this->m_width + minX - this->m_rect.xmin) *
offset = ((otherY - this->m_rect.ymin) * getWidth() + minX - this->m_rect.xmin) *
this->m_num_channels;
memcpy(&this->m_buffer[offset],
&src.m_buffer[otherOffset],
@ -173,7 +147,7 @@ void MemoryBuffer::writePixel(int x, int y, const float color[4])
{
if (x >= this->m_rect.xmin && x < this->m_rect.xmax && y >= this->m_rect.ymin &&
y < this->m_rect.ymax) {
const int offset = (this->m_width * (y - this->m_rect.ymin) + x - this->m_rect.xmin) *
const int offset = (getWidth() * (y - this->m_rect.ymin) + x - this->m_rect.xmin) *
this->m_num_channels;
memcpy(&this->m_buffer[offset], color, sizeof(float) * this->m_num_channels);
}
@ -183,7 +157,7 @@ void MemoryBuffer::addPixel(int x, int y, const float color[4])
{
if (x >= this->m_rect.xmin && x < this->m_rect.xmax && y >= this->m_rect.ymin &&
y < this->m_rect.ymax) {
const int offset = (this->m_width * (y - this->m_rect.ymin) + x - this->m_rect.xmin) *
const int offset = (getWidth() * (y - this->m_rect.ymin) + x - this->m_rect.xmin) *
this->m_num_channels;
float *dst = &this->m_buffer[offset];
const float *src = color;

View File

@ -91,9 +91,6 @@ class MemoryBuffer {
*/
unsigned int m_num_channels;
int m_width;
int m_height;
public:
/**
* \brief construct new MemoryBuffer for a chunk
@ -152,8 +149,8 @@ class MemoryBuffer {
inline void wrap_pixel(int &x, int &y, MemoryBufferExtend extend_x, MemoryBufferExtend extend_y)
{
int w = this->m_width;
int h = this->m_height;
const int w = getWidth();
const int h = getHeight();
x = x - m_rect.xmin;
y = y - m_rect.ymin;
@ -195,8 +192,8 @@ class MemoryBuffer {
MemoryBufferExtend extend_x,
MemoryBufferExtend extend_y)
{
float w = (float)this->m_width;
float h = (float)this->m_height;
const float w = (float)getWidth();
const float h = (float)getHeight();
x = x - m_rect.xmin;
y = y - m_rect.ymin;
@ -249,7 +246,7 @@ class MemoryBuffer {
int u = x;
int v = y;
this->wrap_pixel(u, v, extend_x, extend_y);
const int offset = (this->m_width * y + x) * this->m_num_channels;
const int offset = (getWidth() * y + x) * this->m_num_channels;
float *buffer = &this->m_buffer[offset];
memcpy(result, buffer, sizeof(float) * this->m_num_channels);
}
@ -265,10 +262,10 @@ class MemoryBuffer {
int v = y;
this->wrap_pixel(u, v, extend_x, extend_y);
const int offset = (this->m_width * v + u) * this->m_num_channels;
const int offset = (getWidth() * v + u) * this->m_num_channels;
BLI_assert(offset >= 0);
BLI_assert(offset < this->determineBufferSize() * this->m_num_channels);
BLI_assert(offset < this->buffer_len() * this->m_num_channels);
BLI_assert(!(extend_x == COM_MB_CLIP && (u < m_rect.xmin || u >= m_rect.xmax)) &&
!(extend_y == COM_MB_CLIP && (v < m_rect.ymin || v >= m_rect.ymax)));
float *buffer = &this->m_buffer[offset];
@ -286,15 +283,15 @@ class MemoryBuffer {
float u = x;
float v = y;
this->wrap_pixel(u, v, extend_x, extend_y);
if ((extend_x != COM_MB_REPEAT && (u < 0.0f || u >= this->m_width)) ||
(extend_y != COM_MB_REPEAT && (v < 0.0f || v >= this->m_height))) {
if ((extend_x != COM_MB_REPEAT && (u < 0.0f || u >= getWidth())) ||
(extend_y != COM_MB_REPEAT && (v < 0.0f || v >= getHeight()))) {
copy_vn_fl(result, this->m_num_channels, 0.0f);
return;
}
BLI_bilinear_interpolation_wrap_fl(this->m_buffer,
result,
this->m_width,
this->m_height,
getWidth(),
getHeight(),
this->m_num_channels,
u,
v,
@ -332,23 +329,32 @@ class MemoryBuffer {
/**
* \brief get the width of this MemoryBuffer
*/
int getWidth() const;
const int getWidth() const
{
return BLI_rcti_size_x(&m_rect);
}
/**
* \brief get the height of this MemoryBuffer
*/
int getHeight() const;
const int getHeight() const
{
return BLI_rcti_size_y(&m_rect);
}
/**
* \brief clear the buffer. Make all pixels black transparent.
*/
void clear();
float getMaximumValue();
float getMaximumValue(rcti *rect);
float get_max_value() const;
float get_max_value(const rcti &rect) const;
private:
unsigned int determineBufferSize();
const int buffer_len() const
{
return getWidth() * getHeight();
}
#ifdef WITH_CXX_GUARDEDALLOC
MEM_CXX_CLASS_ALLOC_FUNCS("COM:MemoryBuffer")

View File

@ -77,7 +77,7 @@ void *VariableSizeBokehBlurOperation::initializeTileData(rcti *rect)
const float max_dim = MAX2(m_width, m_height);
const float scalar = this->m_do_size_scale ? (max_dim / 100.0f) : 1.0f;
data->maxBlurScalar = (int)(data->size->getMaximumValue(&rect2) * scalar);
data->maxBlurScalar = (int)(data->size->get_max_value(rect2) * scalar);
CLAMP(data->maxBlurScalar, 1.0f, this->m_maxBlur);
return data;
}
@ -200,7 +200,7 @@ void VariableSizeBokehBlurOperation::executeOpenCL(OpenCLDevice *device,
const float max_dim = MAX2(m_width, m_height);
cl_float scalar = this->m_do_size_scale ? (max_dim / 100.0f) : 1.0f;
maxBlur = (cl_int)min_ff(sizeMemoryBuffer->getMaximumValue() * scalar, (float)this->m_maxBlur);
maxBlur = (cl_int)min_ff(sizeMemoryBuffer->get_max_value() * scalar, (float)this->m_maxBlur);
device->COM_clAttachMemoryBufferToKernelParameter(
defocusKernel, 0, -1, clMemToCleanUp, inputMemoryBuffers, this->m_inputProgram);