Many parts of the compositor are unnecessarily complicated. This patch aims at reducing the complexity of writing nodes and making the code more transparent. == Separating Nodes and Operations == Currently these are both mixed in the same graph, even though they have very different purposes and are used at distinct stages in the compositing process. The patch introduces dedicated graph classes for nodes and for operations. This removes the need for a lot of special case checks (isOperation etc.) and explicit type casts. It simplifies the code since it becomes clear at every stage what type of node we are dealing with. The compiler can use static typing to avoid common bugs from mixing up these types and fewer runtime sanity checks are needed. == Simplified Node Conversion == Converting nodes to operations was previously based on "relinking", i.e. nodes would start with by mirroring links in the Blender DNA node trees, then add operations and redirect these links to them. This was very hard to follow in many cases and required a lot of attention to avoid invalid states. Now there is a helper class called the NodeConverter, which is passed to nodes and implements a much simpler API for this process. Nodes can add operations and explicit connections as before, but defining "external" links to the inputs/outputs of the original node now uses mapping instead of directly modifying link data. Input data (node graph) and result (operations graph) are cleanly separated. == Removed Redundant Data Structures == A few redundant data structures have been removed, notably the SocketConnection. These are only needed temporarily during graph construction. For executing the compositor operations it is perfectly sufficient to store only the direct input link pointers. A common pointer indirection is avoided this way (which might also give a little performance improvement). == Avoid virtual recursive functions == Recursive virtual functions are evil. They are very hard to follow during debugging. At least in the parts this patch is concerned with these functions have been replaced by a non-virtual recursive core function (which might then call virtual non-recursive functions if needed). See for example NodeOperationBuilder::group_operations.
162 lines
4.4 KiB
C++
162 lines
4.4 KiB
C++
/*
|
|
* Copyright 2011, Blender Foundation.
|
|
*
|
|
* 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.
|
|
*
|
|
* Contributor:
|
|
* Jeroen Bakker
|
|
* Monique Dewanchand
|
|
*/
|
|
|
|
#include "COM_BlurBaseOperation.h"
|
|
#include "BLI_math.h"
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
extern "C" {
|
|
# include "RE_pipeline.h"
|
|
}
|
|
|
|
BlurBaseOperation::BlurBaseOperation(DataType data_type) : NodeOperation()
|
|
{
|
|
/* data_type is almost always COM_DT_COLOR except for alpha-blur */
|
|
this->addInputSocket(data_type);
|
|
this->addInputSocket(COM_DT_VALUE);
|
|
this->addOutputSocket(data_type);
|
|
this->setComplex(true);
|
|
this->m_inputProgram = NULL;
|
|
memset(&m_data, 0, sizeof(NodeBlurData));
|
|
this->m_size = 1.0f;
|
|
this->m_sizeavailable = false;
|
|
}
|
|
void BlurBaseOperation::initExecution()
|
|
{
|
|
this->m_inputProgram = this->getInputSocketReader(0);
|
|
this->m_inputSize = this->getInputSocketReader(1);
|
|
this->m_data.image_in_width = this->getWidth();
|
|
this->m_data.image_in_height = this->getHeight();
|
|
if (this->m_data.relative) {
|
|
switch (this->m_data.aspect) {
|
|
case CMP_NODE_BLUR_ASPECT_NONE:
|
|
this->m_data.sizex = (int)(this->m_data.percentx * 0.01f * this->m_data.image_in_width);
|
|
this->m_data.sizey = (int)(this->m_data.percenty * 0.01f * this->m_data.image_in_height);
|
|
break;
|
|
case CMP_NODE_BLUR_ASPECT_Y:
|
|
this->m_data.sizex = (int)(this->m_data.percentx * 0.01f * this->m_data.image_in_width);
|
|
this->m_data.sizey = (int)(this->m_data.percenty * 0.01f * this->m_data.image_in_width);
|
|
break;
|
|
case CMP_NODE_BLUR_ASPECT_X:
|
|
this->m_data.sizex = (int)(this->m_data.percentx * 0.01f * this->m_data.image_in_height);
|
|
this->m_data.sizey = (int)(this->m_data.percenty * 0.01f * this->m_data.image_in_height);
|
|
break;
|
|
}
|
|
}
|
|
|
|
QualityStepHelper::initExecution(COM_QH_MULTIPLY);
|
|
|
|
}
|
|
|
|
float *BlurBaseOperation::make_gausstab(float rad, int size)
|
|
{
|
|
float *gausstab, sum, val;
|
|
int i, n;
|
|
|
|
n = 2 * size + 1;
|
|
|
|
gausstab = (float *)MEM_mallocN(sizeof(float) * n, __func__);
|
|
|
|
sum = 0.0f;
|
|
float fac = (rad > 0.0f ? 1.0f / rad : 0.0f);
|
|
for (i = -size; i <= size; i++) {
|
|
val = RE_filter_value(this->m_data.filtertype, (float)i * fac);
|
|
sum += val;
|
|
gausstab[i + size] = val;
|
|
}
|
|
|
|
sum = 1.0f / sum;
|
|
for (i = 0; i < n; i++)
|
|
gausstab[i] *= sum;
|
|
|
|
return gausstab;
|
|
}
|
|
|
|
/* normalized distance from the current (inverted so 1.0 is close and 0.0 is far)
|
|
* 'ease' is applied after, looks nicer */
|
|
float *BlurBaseOperation::make_dist_fac_inverse(float rad, int size, int falloff)
|
|
{
|
|
float *dist_fac_invert, val;
|
|
int i, n;
|
|
|
|
n = 2 * size + 1;
|
|
|
|
dist_fac_invert = (float *)MEM_mallocN(sizeof(float) * n, __func__);
|
|
|
|
float fac = (rad > 0.0f ? 1.0f / rad : 0.0f);
|
|
for (i = -size; i <= size; i++) {
|
|
val = 1.0f - fabsf((float)i * fac);
|
|
|
|
/* keep in sync with proportional_falloff_curve_only_items */
|
|
switch (falloff) {
|
|
case PROP_SMOOTH:
|
|
/* ease - gives less hard lines for dilate/erode feather */
|
|
val = (3.0f * val * val - 2.0f * val * val * val);
|
|
break;
|
|
case PROP_SPHERE:
|
|
val = sqrtf(2.0f * val - val * val);
|
|
break;
|
|
case PROP_ROOT:
|
|
val = sqrtf(val);
|
|
break;
|
|
case PROP_SHARP:
|
|
val = val * val;
|
|
break;
|
|
case PROP_LIN:
|
|
/* fall-through */
|
|
#ifndef NDEBUG
|
|
case -1:
|
|
/* uninitialized! */
|
|
BLI_assert(0);
|
|
break;
|
|
#endif
|
|
default:
|
|
/* nothing */
|
|
break;
|
|
}
|
|
dist_fac_invert[i + size] = val;
|
|
}
|
|
|
|
return dist_fac_invert;
|
|
}
|
|
|
|
void BlurBaseOperation::deinitExecution()
|
|
{
|
|
this->m_inputProgram = NULL;
|
|
this->m_inputSize = NULL;
|
|
}
|
|
|
|
void BlurBaseOperation::setData(const NodeBlurData *data)
|
|
{
|
|
memcpy(&m_data, data, sizeof(NodeBlurData));
|
|
}
|
|
|
|
void BlurBaseOperation::updateSize()
|
|
{
|
|
if (!this->m_sizeavailable) {
|
|
float result[4];
|
|
this->getInputSocketReader(1)->readSampled(result, 0, 0, COM_PS_NEAREST);
|
|
this->m_size = result[0];
|
|
this->m_sizeavailable = true;
|
|
}
|
|
}
|