Merge branch 'master' into blender2.8
[blender.git] / source / blender / compositor / operations / COM_VariableSizeBokehBlurOperation.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor: 
19  *              Jeroen Bakker 
20  *              Monique Dewanchand
21  */
22
23 #include "COM_VariableSizeBokehBlurOperation.h"
24 #include "BLI_math.h"
25 #include "COM_OpenCLDevice.h"
26
27 extern "C" {
28 #  include "RE_pipeline.h"
29 }
30
31 VariableSizeBokehBlurOperation::VariableSizeBokehBlurOperation() : NodeOperation()
32 {
33         this->addInputSocket(COM_DT_COLOR);
34         this->addInputSocket(COM_DT_COLOR, COM_SC_NO_RESIZE); // do not resize the bokeh image.
35         this->addInputSocket(COM_DT_VALUE); // radius
36 #ifdef COM_DEFOCUS_SEARCH
37         this->addInputSocket(COM_DT_COLOR, COM_SC_NO_RESIZE); // inverse search radius optimization structure.
38 #endif
39         this->addOutputSocket(COM_DT_COLOR);
40         this->setComplex(true);
41         this->setOpenCL(true);
42
43         this->m_inputProgram = NULL;
44         this->m_inputBokehProgram = NULL;
45         this->m_inputSizeProgram = NULL;
46         this->m_maxBlur = 32.0f;
47         this->m_threshold = 1.0f;
48         this->m_do_size_scale = false;
49 #ifdef COM_DEFOCUS_SEARCH
50         this->m_inputSearchProgram = NULL;
51 #endif
52 }
53
54
55 void VariableSizeBokehBlurOperation::initExecution()
56 {
57         this->m_inputProgram = getInputSocketReader(0);
58         this->m_inputBokehProgram = getInputSocketReader(1);
59         this->m_inputSizeProgram = getInputSocketReader(2);
60 #ifdef COM_DEFOCUS_SEARCH
61         this->m_inputSearchProgram = getInputSocketReader(3);
62 #endif
63         QualityStepHelper::initExecution(COM_QH_INCREASE);
64 }
65 struct VariableSizeBokehBlurTileData {
66         MemoryBuffer *color;
67         MemoryBuffer *bokeh;
68         MemoryBuffer *size;
69         int maxBlurScalar;
70 };
71
72 void *VariableSizeBokehBlurOperation::initializeTileData(rcti *rect)
73 {
74         VariableSizeBokehBlurTileData *data = new VariableSizeBokehBlurTileData();
75         data->color = (MemoryBuffer *)this->m_inputProgram->initializeTileData(rect);
76         data->bokeh = (MemoryBuffer *)this->m_inputBokehProgram->initializeTileData(rect);
77         data->size = (MemoryBuffer *)this->m_inputSizeProgram->initializeTileData(rect);
78
79
80         rcti rect2;
81         this->determineDependingAreaOfInterest(rect, (ReadBufferOperation *)this->m_inputSizeProgram, &rect2);
82
83         const float max_dim = max(m_width, m_height);
84         const float scalar = this->m_do_size_scale ? (max_dim / 100.0f) : 1.0f;
85
86         data->maxBlurScalar = (int)(data->size->getMaximumValue(&rect2) * scalar);
87         CLAMP(data->maxBlurScalar, 1.0f, this->m_maxBlur);
88         return data;
89 }
90
91 void VariableSizeBokehBlurOperation::deinitializeTileData(rcti * /*rect*/, void *data)
92 {
93         VariableSizeBokehBlurTileData *result = (VariableSizeBokehBlurTileData *)data;
94         delete result;
95 }
96
97 void VariableSizeBokehBlurOperation::executePixel(float output[4], int x, int y, void *data)
98 {
99         VariableSizeBokehBlurTileData *tileData = (VariableSizeBokehBlurTileData *)data;
100         MemoryBuffer *inputProgramBuffer = tileData->color;
101         MemoryBuffer *inputBokehBuffer = tileData->bokeh;
102         MemoryBuffer *inputSizeBuffer = tileData->size;
103         float *inputSizeFloatBuffer = inputSizeBuffer->getBuffer();
104         float *inputProgramFloatBuffer = inputProgramBuffer->getBuffer();
105         float readColor[4];
106         float bokeh[4];
107         float tempSize[4];
108         float multiplier_accum[4];
109         float color_accum[4];
110
111         const float max_dim = max(m_width, m_height);
112         const float scalar = this->m_do_size_scale ? (max_dim / 100.0f) : 1.0f;
113         int maxBlurScalar = tileData->maxBlurScalar;
114
115         BLI_assert(inputBokehBuffer->getWidth()  == COM_BLUR_BOKEH_PIXELS);
116         BLI_assert(inputBokehBuffer->getHeight() == COM_BLUR_BOKEH_PIXELS);
117
118 #ifdef COM_DEFOCUS_SEARCH
119         float search[4];
120         this->m_inputSearchProgram->read(search, x / InverseSearchRadiusOperation::DIVIDER, y / InverseSearchRadiusOperation::DIVIDER, NULL);
121         int minx = search[0];
122         int miny = search[1];
123         int maxx = search[2];
124         int maxy = search[3];
125 #else
126         int minx = max(x - maxBlurScalar, 0);
127         int miny = max(y - maxBlurScalar, 0);
128         int maxx = min(x + maxBlurScalar, (int)m_width);
129         int maxy = min(y + maxBlurScalar, (int)m_height);
130 #endif
131         {
132                 inputSizeBuffer->readNoCheck(tempSize, x, y);
133                 inputProgramBuffer->readNoCheck(readColor, x, y);
134
135                 copy_v4_v4(color_accum, readColor);
136                 copy_v4_fl(multiplier_accum, 1.0f);
137                 float size_center = tempSize[0] * scalar;
138                 
139                 const int addXStepValue = QualityStepHelper::getStep();
140                 const int addYStepValue = addXStepValue;
141                 const int addXStepColor = addXStepValue * COM_NUM_CHANNELS_COLOR;
142
143                 if (size_center > this->m_threshold) {
144                         for (int ny = miny; ny < maxy; ny += addYStepValue) {
145                                 float dy = ny - y;
146                                 int offsetValueNy = ny * inputSizeBuffer->getWidth();
147                                 int offsetValueNxNy = offsetValueNy + (minx);
148                                 int offsetColorNxNy = offsetValueNxNy * COM_NUM_CHANNELS_COLOR;
149                                 for (int nx = minx; nx < maxx; nx += addXStepValue) {
150                                         if (nx != x || ny != y) {
151                                                 float size = min(inputSizeFloatBuffer[offsetValueNxNy] * scalar, size_center);
152                                                 if (size > this->m_threshold) {
153                                                         float dx = nx - x;
154                                                         if (size > fabsf(dx) && size > fabsf(dy)) {
155                                                                 float uv[2] = {
156                                                                         (float)(COM_BLUR_BOKEH_PIXELS / 2) + (dx / size) * (float)((COM_BLUR_BOKEH_PIXELS / 2) - 1),
157                                                                         (float)(COM_BLUR_BOKEH_PIXELS / 2) + (dy / size) * (float)((COM_BLUR_BOKEH_PIXELS / 2) - 1)};
158                                                                 inputBokehBuffer->read(bokeh, uv[0], uv[1]);
159                                                                 madd_v4_v4v4(color_accum, bokeh, &inputProgramFloatBuffer[offsetColorNxNy]);
160                                                                 add_v4_v4(multiplier_accum, bokeh);
161                                                         }
162                                                 }
163                                         }
164                                         offsetColorNxNy += addXStepColor;
165                                         offsetValueNxNy += addXStepValue;                               }
166                         }
167                 }
168
169                 output[0] = color_accum[0] / multiplier_accum[0];
170                 output[1] = color_accum[1] / multiplier_accum[1];
171                 output[2] = color_accum[2] / multiplier_accum[2];
172                 output[3] = color_accum[3] / multiplier_accum[3];
173
174                 /* blend in out values over the threshold, otherwise we get sharp, ugly transitions */
175                 if ((size_center > this->m_threshold) &&
176                     (size_center < this->m_threshold * 2.0f))
177                 {
178                         /* factor from 0-1 */
179                         float fac = (size_center - this->m_threshold) / this->m_threshold;
180                         interp_v4_v4v4(output, readColor, output, fac);
181                 }
182         }
183
184 }
185
186 void VariableSizeBokehBlurOperation::executeOpenCL(OpenCLDevice *device,
187                                        MemoryBuffer *outputMemoryBuffer, cl_mem clOutputBuffer, 
188                                        MemoryBuffer **inputMemoryBuffers, list<cl_mem> *clMemToCleanUp, 
189                                        list<cl_kernel> * /*clKernelsToCleanUp*/)
190 {
191         cl_kernel defocusKernel = device->COM_clCreateKernel("defocusKernel", NULL);
192
193         cl_int step = this->getStep();
194         cl_int maxBlur;
195         cl_float threshold = this->m_threshold;
196         
197         MemoryBuffer *sizeMemoryBuffer = this->m_inputSizeProgram->getInputMemoryBuffer(inputMemoryBuffers);
198
199         const float max_dim = max(m_width, m_height);
200         cl_float scalar = this->m_do_size_scale ? (max_dim / 100.0f) : 1.0f;
201
202         maxBlur = (cl_int)min_ff(sizeMemoryBuffer->getMaximumValue() * scalar,
203                                  (float)this->m_maxBlur);
204
205         device->COM_clAttachMemoryBufferToKernelParameter(defocusKernel, 0, -1, clMemToCleanUp, inputMemoryBuffers, this->m_inputProgram);
206         device->COM_clAttachMemoryBufferToKernelParameter(defocusKernel, 1,  -1, clMemToCleanUp, inputMemoryBuffers, this->m_inputBokehProgram);
207         device->COM_clAttachMemoryBufferToKernelParameter(defocusKernel, 2,  4, clMemToCleanUp, inputMemoryBuffers, this->m_inputSizeProgram);
208         device->COM_clAttachOutputMemoryBufferToKernelParameter(defocusKernel, 3, clOutputBuffer);
209         device->COM_clAttachMemoryBufferOffsetToKernelParameter(defocusKernel, 5, outputMemoryBuffer);
210         clSetKernelArg(defocusKernel, 6, sizeof(cl_int), &step);
211         clSetKernelArg(defocusKernel, 7, sizeof(cl_int), &maxBlur);
212         clSetKernelArg(defocusKernel, 8, sizeof(cl_float), &threshold);
213         clSetKernelArg(defocusKernel, 9, sizeof(cl_float), &scalar);
214         device->COM_clAttachSizeToKernelParameter(defocusKernel, 10, this);
215         
216         device->COM_clEnqueueRange(defocusKernel, outputMemoryBuffer, 11, this);
217 }
218
219 void VariableSizeBokehBlurOperation::deinitExecution()
220 {
221         this->m_inputProgram = NULL;
222         this->m_inputBokehProgram = NULL;
223         this->m_inputSizeProgram = NULL;
224 #ifdef COM_DEFOCUS_SEARCH
225         this->m_inputSearchProgram = NULL;
226 #endif
227 }
228
229 bool VariableSizeBokehBlurOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
230 {
231         rcti newInput;
232         rcti bokehInput;
233
234         const float max_dim = max(m_width, m_height);
235         const float scalar = this->m_do_size_scale ? (max_dim / 100.0f) : 1.0f;
236         int maxBlurScalar = this->m_maxBlur * scalar;
237
238         newInput.xmax = input->xmax + maxBlurScalar + 2;
239         newInput.xmin = input->xmin - maxBlurScalar + 2;
240         newInput.ymax = input->ymax + maxBlurScalar - 2;
241         newInput.ymin = input->ymin - maxBlurScalar - 2;
242         bokehInput.xmax = COM_BLUR_BOKEH_PIXELS;
243         bokehInput.xmin = 0;
244         bokehInput.ymax = COM_BLUR_BOKEH_PIXELS;
245         bokehInput.ymin = 0;
246         
247
248         NodeOperation *operation = getInputOperation(2);
249         if (operation->determineDependingAreaOfInterest(&newInput, readOperation, output) ) {
250                 return true;
251         }
252         operation = getInputOperation(1);
253         if (operation->determineDependingAreaOfInterest(&bokehInput, readOperation, output) ) {
254                 return true;
255         }
256 #ifdef COM_DEFOCUS_SEARCH
257         rcti searchInput;
258         searchInput.xmax = (input->xmax / InverseSearchRadiusOperation::DIVIDER) + 1;
259         searchInput.xmin = (input->xmin / InverseSearchRadiusOperation::DIVIDER) - 1;
260         searchInput.ymax = (input->ymax / InverseSearchRadiusOperation::DIVIDER) + 1;
261         searchInput.ymin = (input->ymin / InverseSearchRadiusOperation::DIVIDER) - 1;
262         operation = getInputOperation(3);
263         if (operation->determineDependingAreaOfInterest(&searchInput, readOperation, output) ) {
264                 return true;
265         }
266 #endif
267         operation = getInputOperation(0);
268         if (operation->determineDependingAreaOfInterest(&newInput, readOperation, output) ) {
269                 return true;
270         }
271         return false;
272 }
273
274 #ifdef COM_DEFOCUS_SEARCH
275 // InverseSearchRadiusOperation
276 InverseSearchRadiusOperation::InverseSearchRadiusOperation() : NodeOperation() 
277 {
278         this->addInputSocket(COM_DT_VALUE, COM_SC_NO_RESIZE); // radius
279         this->addOutputSocket(COM_DT_COLOR);
280         this->setComplex(true);
281         this->m_inputRadius = NULL;
282 }
283
284 void InverseSearchRadiusOperation::initExecution() 
285 {
286         this->m_inputRadius = this->getInputSocketReader(0);
287 }
288
289 void *InverseSearchRadiusOperation::initializeTileData(rcti *rect)
290 {
291         MemoryBuffer * data = new MemoryBuffer(COM_DT_COLOR, rect);
292         float *buffer = data->getBuffer();
293         int x, y;
294         int width = this->m_inputRadius->getWidth();
295         int height = this->m_inputRadius->getHeight();
296         float temp[4];
297         int offset = 0;
298         for (y = rect->ymin; y < rect->ymax ; y++) {
299                 for (x = rect->xmin; x < rect->xmax ; x++) {
300                         int rx = x * DIVIDER;
301                         int ry = y * DIVIDER;
302                         buffer[offset] = MAX2(rx - m_maxBlur, 0);
303                         buffer[offset + 1] = MAX2(ry - m_maxBlur, 0);
304                         buffer[offset + 2] = MIN2(rx + DIVIDER + m_maxBlur, width);
305                         buffer[offset + 3] = MIN2(ry + DIVIDER + m_maxBlur, height);
306                         offset += 4;
307                 }
308         }
309 //      for (x = rect->xmin; x < rect->xmax ; x++) {
310 //              for (y = rect->ymin; y < rect->ymax ; y++) {
311 //                      int rx = x * DIVIDER;
312 //                      int ry = y * DIVIDER;
313 //                      float radius = 0.0f;
314 //                      float maxx = x;
315 //                      float maxy = y;
316         
317 //                      for (int x2 = 0 ; x2 < DIVIDER ; x2 ++) {
318 //                              for (int y2 = 0 ; y2 < DIVIDER ; y2 ++) {
319 //                                      this->m_inputRadius->read(temp, rx+x2, ry+y2, COM_PS_NEAREST);
320 //                                      if (radius < temp[0]) {
321 //                                              radius = temp[0];
322 //                                              maxx = x2;
323 //                                              maxy = y2;
324 //                                      }
325 //                              }
326 //                      }
327 //                      int impactRadius = ceil(radius / DIVIDER);
328 //                      for (int x2 = x - impactRadius ; x2 < x + impactRadius ; x2 ++) {
329 //                              for (int y2 = y - impactRadius ; y2 < y + impactRadius ; y2 ++) {
330 //                                      data->read(temp, x2, y2);
331 //                                      temp[0] = MIN2(temp[0], maxx);
332 //                                      temp[1] = MIN2(temp[1], maxy);
333 //                                      temp[2] = MAX2(temp[2], maxx);
334 //                                      temp[3] = MAX2(temp[3], maxy);
335 //                                      data->writePixel(x2, y2, temp);
336 //                              }
337 //                      }
338 //              }
339 //      }
340         return data;
341 }
342
343 void InverseSearchRadiusOperation::executePixelChunk(float output[4], int x, int y, void *data)
344 {
345         MemoryBuffer *buffer = (MemoryBuffer *)data;
346         buffer->readNoCheck(output, x, y);
347 }
348
349 void InverseSearchRadiusOperation::deinitializeTileData(rcti *rect, void *data) 
350 {
351         if (data) {
352                 MemoryBuffer *mb = (MemoryBuffer *)data;
353                 delete mb;
354         }
355 }
356
357 void InverseSearchRadiusOperation::deinitExecution() 
358 {
359         this->m_inputRadius = NULL;
360 }
361
362 void InverseSearchRadiusOperation::determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2])
363 {
364         NodeOperation::determineResolution(resolution, preferredResolution);
365         resolution[0] = resolution[0] / DIVIDER;
366         resolution[1] = resolution[1] / DIVIDER;
367 }
368
369 bool InverseSearchRadiusOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
370 {
371         rcti newRect;
372         newRect.ymin = input->ymin * DIVIDER - m_maxBlur;
373         newRect.ymax = input->ymax * DIVIDER + m_maxBlur;
374         newRect.xmin = input->xmin * DIVIDER - m_maxBlur;
375         newRect.xmax = input->xmax * DIVIDER + m_maxBlur;
376         return NodeOperation::determineDependingAreaOfInterest(&newRect, readOperation, output);
377 }
378 #endif