Blender  V3.3
COM_DirectionalBlurOperation.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2011 Blender Foundation. */
3 
5 #include "COM_OpenCLDevice.h"
6 
7 namespace blender::compositor {
8 
10 {
13  flags_.complex = true;
14  flags_.open_cl = true;
15  input_program_ = nullptr;
16 }
17 
19 {
20  input_program_ = get_input_socket_reader(0);
22  const float angle = data_->angle;
23  const float zoom = data_->zoom;
24  const float spin = data_->spin;
25  const float iterations = data_->iter;
26  const float distance = data_->distance;
27  const float center_x = data_->center_x;
28  const float center_y = data_->center_y;
29  const float width = get_width();
30  const float height = get_height();
31 
32  const float a = angle;
33  const float itsc = 1.0f / powf(2.0f, (float)iterations);
34  float D;
35 
36  D = distance * sqrtf(width * width + height * height);
37  center_x_pix_ = center_x * width;
38  center_y_pix_ = center_y * height;
39 
40  tx_ = itsc * D * cosf(a);
41  ty_ = -itsc * D * sinf(a);
42  sc_ = itsc * zoom;
43  rot_ = itsc * spin;
44 }
45 
46 void DirectionalBlurOperation::execute_pixel(float output[4], int x, int y, void * /*data*/)
47 {
48  const int iterations = pow(2.0f, data_->iter);
49  float col[4] = {0.0f, 0.0f, 0.0f, 0.0f};
50  float col2[4] = {0.0f, 0.0f, 0.0f, 0.0f};
51  input_program_->read_sampled(col2, x, y, PixelSampler::Bilinear);
52  float ltx = tx_;
53  float lty = ty_;
54  float lsc = sc_;
55  float lrot = rot_;
56  /* blur the image */
57  for (int i = 0; i < iterations; i++) {
58  const float cs = cosf(lrot), ss = sinf(lrot);
59  const float isc = 1.0f / (1.0f + lsc);
60 
61  const float v = isc * (y - center_y_pix_) + lty;
62  const float u = isc * (x - center_x_pix_) + ltx;
63 
64  input_program_->read_sampled(col,
65  cs * u + ss * v + center_x_pix_,
66  cs * v - ss * u + center_y_pix_,
68 
69  add_v4_v4(col2, col);
70 
71  /* double transformations */
72  ltx += tx_;
73  lty += ty_;
74  lrot += rot_;
75  lsc += sc_;
76  }
77 
78  mul_v4_v4fl(output, col2, 1.0f / (iterations + 1));
79 }
80 
82  MemoryBuffer *output_memory_buffer,
83  cl_mem cl_output_buffer,
84  MemoryBuffer **input_memory_buffers,
85  std::list<cl_mem> *cl_mem_to_clean_up,
86  std::list<cl_kernel> * /*cl_kernels_to_clean_up*/)
87 {
88  cl_kernel directional_blur_kernel = device->COM_cl_create_kernel("directional_blur_kernel",
89  nullptr);
90 
91  cl_int iterations = pow(2.0f, data_->iter);
92  cl_float2 ltxy = {{tx_, ty_}};
93  cl_float2 centerpix = {{center_x_pix_, center_y_pix_}};
94  cl_float lsc = sc_;
95  cl_float lrot = rot_;
96 
98  directional_blur_kernel, 0, -1, cl_mem_to_clean_up, input_memory_buffers, input_program_);
100  directional_blur_kernel, 1, cl_output_buffer);
102  directional_blur_kernel, 2, output_memory_buffer);
103  clSetKernelArg(directional_blur_kernel, 3, sizeof(cl_int), &iterations);
104  clSetKernelArg(directional_blur_kernel, 4, sizeof(cl_float), &lsc);
105  clSetKernelArg(directional_blur_kernel, 5, sizeof(cl_float), &lrot);
106  clSetKernelArg(directional_blur_kernel, 6, sizeof(cl_float2), &ltxy);
107  clSetKernelArg(directional_blur_kernel, 7, sizeof(cl_float2), &centerpix);
108 
109  device->COM_cl_enqueue_range(directional_blur_kernel, output_memory_buffer, 8, this);
110 }
111 
113 {
114  input_program_ = nullptr;
115 }
116 
118  rcti * /*input*/, ReadBufferOperation *read_operation, rcti *output)
119 {
120  rcti new_input;
121 
122  new_input.xmax = this->get_width();
123  new_input.xmin = 0;
124  new_input.ymax = this->get_height();
125  new_input.ymin = 0;
126 
127  return NodeOperation::determine_depending_area_of_interest(&new_input, read_operation, output);
128 }
129 
131  const rcti &UNUSED(output_area),
132  rcti &r_input_area)
133 {
134  BLI_assert(input_idx == 0);
135  UNUSED_VARS_NDEBUG(input_idx);
136  r_input_area = this->get_canvas();
137 }
138 
140  const rcti &area,
142 {
143  const MemoryBuffer *input = inputs[0];
144  const int iterations = pow(2.0f, data_->iter);
145  for (BuffersIterator<float> it = output->iterate_with({}, area); !it.is_end(); ++it) {
146  const int x = it.x;
147  const int y = it.y;
148  float color_accum[4];
149  input->read_elem_bilinear(x, y, color_accum);
150 
151  /* Blur pixel. */
152  /* TODO(manzanilla): Many values used on iterations can be calculated beforehand. Create a
153  * table on operation initialization. */
154  float ltx = tx_;
155  float lty = ty_;
156  float lsc = sc_;
157  float lrot = rot_;
158  for (int i = 0; i < iterations; i++) {
159  const float cs = cosf(lrot), ss = sinf(lrot);
160  const float isc = 1.0f / (1.0f + lsc);
161 
162  const float v = isc * (y - center_y_pix_) + lty;
163  const float u = isc * (x - center_x_pix_) + ltx;
164 
165  float color[4];
166  input->read_elem_bilinear(
167  cs * u + ss * v + center_x_pix_, cs * v - ss * u + center_y_pix_, color);
168  add_v4_v4(color_accum, color);
169 
170  /* Double transformations. */
171  ltx += tx_;
172  lty += ty_;
173  lrot += rot_;
174  lsc += sc_;
175  }
176 
177  mul_v4_v4fl(it.out, color_accum, 1.0f / (iterations + 1));
178  }
179 }
180 
181 } // namespace blender::compositor
#define D
#define BLI_assert(a)
Definition: BLI_assert.h:46
MINLINE void add_v4_v4(float r[4], const float a[4])
MINLINE void mul_v4_v4fl(float r[4], const float a[4], float f)
#define UNUSED_VARS_NDEBUG(...)
#define UNUSED(x)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a color
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition: btVector3.h:356
static SpinLock spin
Definition: cachefile.c:162
void update_memory_buffer_partial(MemoryBuffer *output, const rcti &area, Span< MemoryBuffer * > inputs) override
void execute_pixel(float output[4], int x, int y, void *data) override
void execute_opencl(OpenCLDevice *device, MemoryBuffer *output_memory_buffer, cl_mem cl_output_buffer, MemoryBuffer **input_memory_buffers, std::list< cl_mem > *cl_mem_to_clean_up, std::list< cl_kernel > *cl_kernels_to_clean_up) override
custom handle to add new tasks to the OpenCL command queue in order to execute a chunk on an GPUDevic...
bool determine_depending_area_of_interest(rcti *input, ReadBufferOperation *read_operation, rcti *output) override
void get_area_of_interest(int input_idx, const rcti &output_area, rcti &r_input_area) override
Get input operation area being read by this operation on rendering given output area.
a MemoryBuffer contains access to the data of a chunk
void add_output_socket(DataType datatype)
SocketReader * get_input_socket_reader(unsigned int index)
virtual bool determine_depending_area_of_interest(rcti *input, ReadBufferOperation *read_operation, rcti *output)
void read_sampled(float result[4], float x, float y, PixelSampler sampler)
void add_input_socket(DataType datatype, ResizeMode resize_mode=ResizeMode::Center)
device representing an GPU OpenCL device. an instance of this class represents a single cl_device
cl_mem COM_cl_attach_memory_buffer_to_kernel_parameter(cl_kernel kernel, int parameter_index, int offset_index, std::list< cl_mem > *cleanup, MemoryBuffer **input_memory_buffers, SocketReader *reader)
void COM_cl_attach_output_memory_buffer_to_kernel_parameter(cl_kernel kernel, int parameter_index, cl_mem cl_output_memory_buffer)
void COM_cl_enqueue_range(cl_kernel kernel, MemoryBuffer *output_memory_buffer)
cl_kernel COM_cl_create_kernel(const char *kernelname, std::list< cl_kernel > *cl_kernels_to_clean_up)
void COM_cl_attach_memory_buffer_offset_to_kernel_parameter(cl_kernel kernel, int offset_index, MemoryBuffer *memory_buffers)
#define sinf(x)
Definition: cuda/compat.h:102
#define cosf(x)
Definition: cuda/compat.h:101
#define powf(x, y)
Definition: cuda/compat.h:103
uint col
ccl_global KernelShaderEvalInput ccl_global float * output
ccl_global KernelShaderEvalInput * input
ccl_device_inline float3 pow(float3 v, float e)
Definition: math_float3.h:533
#define sqrtf(x)
Definition: metal/compat.h:243
static unsigned a[3]
Definition: RandGen.cpp:78
static void area(int d1, int d2, int e1, int e2, float weights[2])
typename BuffersIteratorBuilder< T >::Iterator BuffersIterator
T distance(const T &a, const T &b)
static bNodeSocketTemplate inputs[]
int ymin
Definition: DNA_vec_types.h:64
int ymax
Definition: DNA_vec_types.h:64
int xmin
Definition: DNA_vec_types.h:63
int xmax
Definition: DNA_vec_types.h:63