[Bf-blender-cvs] [a08967c4585] compositor-anti-aliasing: Merge remote-tracking branch 'origin' into compositor-anti-aliasing

Habib Gahbiche noreply at git.blender.org
Sat Mar 20 14:26:31 CET 2021


Commit: a08967c45858fccec795cd4da8cb5b144c1ff781
Author: Habib Gahbiche
Date:   Sat Mar 20 13:17:18 2021 +0100
Branches: compositor-anti-aliasing
https://developer.blender.org/rBa08967c45858fccec795cd4da8cb5b144c1ff781

Merge remote-tracking branch 'origin' into compositor-anti-aliasing

Conflicts:
	source/blender/blenlib/intern/math_base_inline.c

===================================================================



===================================================================

diff --cc source/blender/blenlib/intern/math_base_inline.c
index 822efdb769f,6481fac5a14..2a7c091d1b9
--- a/source/blender/blenlib/intern/math_base_inline.c
+++ b/source/blender/blenlib/intern/math_base_inline.c
@@@ -180,13 -180,18 +180,25 @@@ MINLINE double interpd(double target, d
    return (fac * target) + (1.0f - fac) * origin;
  }
  
+ MINLINE float ratiof(float min, float max, float pos)
+ {
+   float range = max - min;
+   return range == 0 ? 0 : ((pos - min) / range);
+ }
+ 
+ MINLINE double ratiod(double min, double max, double pos)
+ {
+   double range = max - min;
+   return range == 0 ? 0 : ((pos - min) / range);
+ }
+ 
 +/* Map a normalized value, i.e. from interval [0, 1] to interval [a, b]  */
 +MINLINE float scalenorm(float a, float b, float x)
 +{
 +  BLI_assert(x <= 1 && x >= 0);
 +  return (x * (b - a)) + a;
 +}
 +
  /* used for zoom values*/
  MINLINE float power_of_2(float val)
  {
diff --cc source/blender/compositor/operations/COM_SMAAOperation.cc
index 654228d8ad8,00000000000..887f7a760f3
mode 100644,000000..100644
--- a/source/blender/compositor/operations/COM_SMAAOperation.cc
+++ b/source/blender/compositor/operations/COM_SMAAOperation.cc
@@@ -1,977 -1,0 +1,977 @@@
 +/*
 + * Copyright 2017, 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: IRIE Shinsuke
 + */
 +
 +#include "COM_SMAAOperation.h"
 +#include "BLI_math.h"
 +#include "COM_SMAAAreaTexture.h"
 +
 +extern "C" {
 +#include "IMB_colormanagement.h"
 +}
 +
 +/*
 + * An implementation of Enhanced Subpixel Morphological Antialiasing (SMAA)
 + *
 + * The algorithm was proposed by:
 + *   Jorge Jimenez, Jose I. Echevarria, Tiago Sousa, Diego Gutierrez
 + *
 + *   http://www.iryoku.com/smaa/
 + *
 + * This file is based on smaa-cpp:
 + *
 + *   https://github.com/iRi-E/smaa-cpp
 + *
 + * Currently only SMAA 1x mode is provided, so the operation will be done
 + * with no spatial multisampling nor temporal supersampling.
 + *
 + * Note: This program assumes the screen coordinates are DirectX style, so
 + * the vertical direction is upside-down. "top" and "bottom" actually mean
 + * bottom and top, respectively.
 + */
 +
 +/*-----------------------------------------------------------------------------*/
 +/* Non-Configurable Defines */
 +
 +#define SMAA_AREATEX_SIZE 80
 +#define SMAA_AREATEX_MAX_DISTANCE 20
 +#define SMAA_AREATEX_MAX_DISTANCE_DIAG 20
 +#define SMAA_MAX_SEARCH_STEPS 362 /* 362 - 1 = 19^2 */
 +#define SMAA_MAX_SEARCH_STEPS_DIAG 19
 +
 +/*-----------------------------------------------------------------------------*/
 +/* Internal Functions to Sample Pixel Color from Image */
 +
 +static inline void sample(SocketReader *reader, int x, int y, float color[4])
 +{
 +  if (x < 0 || x >= reader->getWidth() || y < 0 || y >= reader->getHeight()) {
 +    color[0] = color[1] = color[2] = color[3] = 0.0;
 +    return;
 +  }
 +
 +  reader->read(color, x, y, nullptr);
 +}
 +
 +static void sample_bilinear_vertical(
 +    SocketReader *reader, int x, int y, float yoffset, float color[4])
 +{
 +  float iy = floorf(yoffset);
 +  float fy = yoffset - iy;
 +  y += (int)iy;
 +
 +  float color00[4], color01[4];
 +
 +  sample(reader, x + 0, y + 0, color00);
 +  sample(reader, x + 0, y + 1, color01);
 +
 +  color[0] = interpf(color01[0], color00[0], fy);
 +  color[1] = interpf(color01[1], color00[1], fy);
 +  color[2] = interpf(color01[2], color00[2], fy);
 +  color[3] = interpf(color01[3], color00[3], fy);
 +}
 +
 +static void sample_bilinear_horizontal(
 +    SocketReader *reader, int x, int y, float xoffset, float color[4])
 +{
 +  float ix = floorf(xoffset);
 +  float fx = xoffset - ix;
 +  x += (int)ix;
 +
 +  float color00[4], color10[4];
 +
 +  sample(reader, x + 0, y + 0, color00);
 +  sample(reader, x + 1, y + 0, color10);
 +
 +  color[0] = interpf(color10[0], color00[0], fx);
 +  color[1] = interpf(color10[1], color00[1], fx);
 +  color[2] = interpf(color10[2], color00[2], fx);
 +  color[3] = interpf(color10[3], color00[3], fx);
 +}
 +
 +/*-----------------------------------------------------------------------------*/
 +/* Internal Functions to Sample Blending Weights from AreaTex */
 +
 +static inline const float *areatex_sample_internal(const float *areatex, int x, int y)
 +{
 +  return &areatex[(CLAMPIS(x, 0, SMAA_AREATEX_SIZE - 1) +
 +                   CLAMPIS(y, 0, SMAA_AREATEX_SIZE - 1) * SMAA_AREATEX_SIZE) *
 +                  2];
 +}
 +
 +/**
 + * We have the distance and both crossing edges. So, what are the areas
 + * at each side of current edge?
 + */
 +static void area(int d1, int d2, int e1, int e2, float weights[2])
 +{
 +  /* The areas texture is compressed  quadratically: */
 +  float x = (float)(SMAA_AREATEX_MAX_DISTANCE * e1) + sqrtf((float)d1);
 +  float y = (float)(SMAA_AREATEX_MAX_DISTANCE * e2) + sqrtf((float)d2);
 +
 +  float ix = floorf(x), iy = floorf(y);
 +  float fx = x - ix, fy = y - iy;
 +  int X = (int)ix, Y = (int)iy;
 +
 +  const float *weights00 = areatex_sample_internal(areatex, X + 0, Y + 0);
 +  const float *weights10 = areatex_sample_internal(areatex, X + 1, Y + 0);
 +  const float *weights01 = areatex_sample_internal(areatex, X + 0, Y + 1);
 +  const float *weights11 = areatex_sample_internal(areatex, X + 1, Y + 1);
 +
 +  weights[0] = interpf(
 +      interpf(weights11[0], weights01[0], fx), interpf(weights10[0], weights00[0], fx), fy);
 +  weights[1] = interpf(
 +      interpf(weights11[1], weights01[1], fx), interpf(weights10[1], weights00[1], fx), fy);
 +}
 +
 +/**
 + * Similar to area(), this calculates the area corresponding to a certain
 + * diagonal distance and crossing edges 'e'.
 + */
 +static void area_diag(int d1, int d2, int e1, int e2, float weights[2])
 +{
 +  int x = SMAA_AREATEX_MAX_DISTANCE_DIAG * e1 + d1;
 +  int y = SMAA_AREATEX_MAX_DISTANCE_DIAG * e2 + d2;
 +
 +  const float *w = areatex_sample_internal(areatex_diag, x, y);
 +  copy_v2_v2(weights, w);
 +}
 +
 +/*-----------------------------------------------------------------------------*/
 +/* Edge Detection (First Pass) */
 +/*-----------------------------------------------------------------------------*/
 +
 +SMAAEdgeDetectionOperation::SMAAEdgeDetectionOperation() : NodeOperation()
 +{
-   this->addInputSocket(COM_DT_COLOR); /* image */
-   this->addInputSocket(COM_DT_VALUE); /* depth, material ID, etc. */
-   this->addOutputSocket(COM_DT_COLOR);
++  this->addInputSocket(DataType::Color); /* image */
++  this->addInputSocket(DataType::Value); /* depth, material ID, etc. */
++  this->addOutputSocket(DataType::Color);
 +  this->setComplex(true);
 +  this->m_imageReader = nullptr;
 +  this->m_valueReader = nullptr;
 +  this->m_threshold = 0.1f;
 +  this->m_contrast_limit = 2.0f;
 +}
 +
 +void SMAAEdgeDetectionOperation::initExecution()
 +{
 +  this->m_imageReader = this->getInputSocketReader(0);
 +  this->m_valueReader = this->getInputSocketReader(1);
 +}
 +
 +void SMAAEdgeDetectionOperation::deinitExecution()
 +{
 +  this->m_imageReader = nullptr;
 +  this->m_valueReader = nullptr;
 +}
 +
 +void SMAAEdgeDetectionOperation::setThreshold(float threshold)
 +{
 +  /* UI values are between 0 and 1 for simplicity but algorithm expects values between 0 and 0.5 */
 +  m_threshold = scalenorm(0, 0.5, threshold);
 +}
 +
 +void SMAAEdgeDetectionOperation::setLocalContrastAdaptationFactor(float factor)
 +{
 +  /* UI values are between 0 and 1 for simplicity but algorithm expects values between 1 and 10 */
 +  m_contrast_limit = scalenorm(1, 10, factor);
 +}
 +
 +bool SMAAEdgeDetectionOperation::determineDependingAreaOfInterest(
 +    rcti *input, ReadBufferOperation *readOperation, rcti *output)
 +{
 +  rcti newInput;
 +  newInput.xmax = input->xmax + 1;
 +  newInput.xmin = input->xmin - 2;
 +  newInput.ymax = input->ymax + 1;
 +  newInput.ymin = input->ymin - 2;
 +
 +  return NodeOperation::determineDependingAreaOfInterest(&newInput, readOperation, output);
 +}
 +
 +/* Luma Edge Detection */
 +
 +void SMAALumaEdgeDetectionOperation::executePixel(float output[4], int x, int y, void * /*data*/)
 +{
 +  float color[4];
 +
 +  /* Calculate luma deltas: */
 +  sample(m_imageReader, x, y, color);
 +  float L = IMB_colormanagement_get_luminance(color);
 +  sample(m_imageReader, x - 1, y, color);
 +  float Lleft = IMB_colormanagement_get_luminance(color);
 +  sample(m_imageReader, x, y - 1, color);
 +  float Ltop = IMB_colormanagement_get_luminance(color);
 +  float Dleft = fabsf(L - Lleft);
 +  float Dtop = fabsf(L - Ltop);
 +
 +  /* We do the usual threshold: */
 +  output[0] = (x > 0 && Dleft >= m_threshold) ? 1.0f : 0.0f;
 +  output[1] = (y > 0 && Dtop >= m_threshold) ? 1.0f : 0.0f;
 +  output[2] = 0.0f;
 +  output[3] = 1.0f;
 +
 +  /* Then discard if there is no edge: */
 +  if (is_zero_v2(output)) {
 +    return;
 +  }
 +
 +  /* Calculate right and bottom deltas: */
 +  sample(m_imageReader, x + 1, y, color);
 +  float Lright = IMB_colormanagement_get_luminance(color);
 +  sample(m_imageReader, x, y + 1, color);
 +  float Lbottom = IMB_colormanagement_get_luminance(color);
 +  float Dright = fabsf(L - Lright);
 +  float Dbottom = fabsf(L - Lbottom);
 +
 +  /* Calculate the maximum delta in the direct neighborhood: */
 +  float maxDelta = fmaxf(fmaxf(Dleft, Dright), fmaxf(Dtop, Dbottom));
 +
 +  /* Calculate luma used for both left and top edges: */
 +  sample(m_imageReader, x - 1, y - 1, color);
 +  float Llefttop = IMB_colormanagement_get_luminance(color);
 +
 +  /* Left edge */
 +  if (output[0] != 0.0f) {
 +    /* Calculate deltas around

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list