e4f88dc7659f4238f985b819f08df7e153d500b3
[blender-staging.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 #ifdef COM_DEFOCUS_SEARCH
49         this->m_inputSearchProgram = NULL;
50 #endif
51 }
52
53
54 void VariableSizeBokehBlurOperation::initExecution()
55 {
56         this->m_inputProgram = getInputSocketReader(0);
57         this->m_inputBokehProgram = getInputSocketReader(1);
58         this->m_inputSizeProgram = getInputSocketReader(2);
59 #ifdef COM_DEFOCUS_SEARCH
60         this->m_inputSearchProgram = getInputSocketReader(3);
61 #endif
62         QualityStepHelper::initExecution(COM_QH_INCREASE);
63 }
64 struct VariableSizeBokehBlurTileData
65 {
66         MemoryBuffer *color;
67         MemoryBuffer *bokeh;
68         MemoryBuffer *size;
69         int maxBlur;
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         data->maxBlur = (int)data->size->getMaximumValue(&rect2);
83         CLAMP(data->maxBlur, 1.0f, this->m_maxBlur);
84         return data;
85 }
86
87 void VariableSizeBokehBlurOperation::deinitializeTileData(rcti *rect, void *data)
88 {
89         VariableSizeBokehBlurTileData *result = (VariableSizeBokehBlurTileData *)data;
90         delete result;
91 }
92
93 void VariableSizeBokehBlurOperation::executePixel(float output[4], int x, int y, void *data)
94 {
95         VariableSizeBokehBlurTileData *tileData = (VariableSizeBokehBlurTileData *)data;
96         MemoryBuffer *inputProgramBuffer = tileData->color;
97         MemoryBuffer *inputBokehBuffer = tileData->bokeh;
98         MemoryBuffer *inputSizeBuffer = tileData->size;
99         float *inputSizeFloatBuffer = inputSizeBuffer->getBuffer();
100         float *inputProgramFloatBuffer = inputProgramBuffer->getBuffer();
101         float readColor[4];
102         float bokeh[4];
103         float tempSize[4];
104         float multiplier_accum[4];
105         float color_accum[4];
106         int maxBlur = tileData->maxBlur;
107
108 #ifdef COM_DEFOCUS_SEARCH
109         float search[4];
110         this->m_inputSearchProgram->read(search, x/InverseSearchRadiusOperation::DIVIDER, y / InverseSearchRadiusOperation::DIVIDER, NULL);
111         int minx = search[0];
112         int miny = search[1];
113         int maxx = search[2];
114         int maxy = search[3];
115 #else
116         int minx = max(x - maxBlur, 0);
117         int miny = max(y - maxBlur, 0);
118         int maxx = min(x + maxBlur, (int)m_width);
119         int maxy = min(y + maxBlur, (int)m_height);
120 #endif
121         {
122                 inputSizeBuffer->readNoCheck(tempSize, x, y);
123                 inputProgramBuffer->readNoCheck(readColor, x, y);
124
125                 copy_v4_v4(color_accum, readColor);
126                 copy_v4_fl(multiplier_accum, 1.0f);
127                 float size_center = tempSize[0];
128                 
129                 const int addXStep = QualityStepHelper::getStep() * COM_NUMBER_OF_CHANNELS;
130                 
131                 if (size_center > this->m_threshold) {
132                         for (int ny = miny; ny < maxy; ny += QualityStepHelper::getStep()) {
133                                 float dy = ny - y;
134                                 int offsetNy = ny * inputSizeBuffer->getWidth() * COM_NUMBER_OF_CHANNELS;
135                                 int offsetNxNy = offsetNy + (minx * COM_NUMBER_OF_CHANNELS);
136                                 for (int nx = minx; nx < maxx; nx += QualityStepHelper::getStep()) {
137                                         if (nx != x || ny != y) {
138                                                 float size = inputSizeFloatBuffer[offsetNxNy];
139                                                 if (size > this->m_threshold) {
140                                                         float dx = nx - x;
141                                                         if (size > fabsf(dx) && size > fabsf(dy)) {
142                                                                 float uv[2] = {
143                                                                     (float)(COM_BLUR_BOKEH_PIXELS / 2) + (dx / size) * (float)((COM_BLUR_BOKEH_PIXELS / 2) - 1),
144                                                                     (float)(COM_BLUR_BOKEH_PIXELS / 2) + (dy / size) * (float)((COM_BLUR_BOKEH_PIXELS / 2) - 1)};
145                                                                 inputBokehBuffer->readNoCheck(bokeh, uv[0], uv[1]);
146                                                                 madd_v4_v4v4(color_accum, bokeh, &inputProgramFloatBuffer[offsetNxNy]);
147                                                                 add_v4_v4(multiplier_accum, bokeh);
148                                                         }
149                                                 }
150                                         }
151                                         offsetNxNy += addXStep;
152                                 }
153                         }
154                 }
155
156                 output[0] = color_accum[0] / multiplier_accum[0];
157                 output[1] = color_accum[1] / multiplier_accum[1];
158                 output[2] = color_accum[2] / multiplier_accum[2];
159                 output[3] = color_accum[3] / multiplier_accum[3];
160
161                 /* blend in out values over the threshold, otherwise we get sharp, ugly transitions */
162                 if ((size_center > this->m_threshold) &&
163                     (size_center < this->m_threshold * 2.0f))
164                 {
165                         /* factor from 0-1 */
166                         float fac = (size_center - this->m_threshold) / this->m_threshold;
167                         interp_v4_v4v4(output, readColor, output, fac);
168                 }
169         }
170
171 }
172
173 void VariableSizeBokehBlurOperation::executeOpenCL(OpenCLDevice *device,
174                                        MemoryBuffer *outputMemoryBuffer, cl_mem clOutputBuffer, 
175                                        MemoryBuffer **inputMemoryBuffers, list<cl_mem> *clMemToCleanUp, 
176                                        list<cl_kernel> *clKernelsToCleanUp) 
177 {
178         cl_kernel defocusKernel = device->COM_clCreateKernel("defocusKernel", NULL);
179
180         cl_int step = this->getStep();
181         cl_int maxBlur;
182         cl_float threshold = this->m_threshold;
183         
184         MemoryBuffer *sizeMemoryBuffer = (MemoryBuffer *)this->m_inputSizeProgram->getInputMemoryBuffer(inputMemoryBuffers);
185         maxBlur = (cl_int)sizeMemoryBuffer->getMaximumValue();
186         maxBlur = MIN2(maxBlur, this->m_maxBlur);
187
188         device->COM_clAttachMemoryBufferToKernelParameter(defocusKernel, 0, -1, clMemToCleanUp, inputMemoryBuffers, this->m_inputProgram);
189         device->COM_clAttachMemoryBufferToKernelParameter(defocusKernel, 1,  -1, clMemToCleanUp, inputMemoryBuffers, this->m_inputBokehProgram);
190         device->COM_clAttachMemoryBufferToKernelParameter(defocusKernel, 2,  4, clMemToCleanUp, inputMemoryBuffers, this->m_inputSizeProgram);
191         device->COM_clAttachOutputMemoryBufferToKernelParameter(defocusKernel, 3, clOutputBuffer);
192         device->COM_clAttachMemoryBufferOffsetToKernelParameter(defocusKernel, 5, outputMemoryBuffer);
193         clSetKernelArg(defocusKernel, 6, sizeof(cl_int), &step);
194         clSetKernelArg(defocusKernel, 7, sizeof(cl_int), &maxBlur);
195         clSetKernelArg(defocusKernel, 8, sizeof(cl_float), &threshold);
196         device->COM_clAttachSizeToKernelParameter(defocusKernel, 9, this);
197         
198         device->COM_clEnqueueRange(defocusKernel, outputMemoryBuffer, 10, this);
199 }
200
201 void VariableSizeBokehBlurOperation::deinitExecution()
202 {
203         this->m_inputProgram = NULL;
204         this->m_inputBokehProgram = NULL;
205         this->m_inputSizeProgram = NULL;
206 #ifdef COM_DEFOCUS_SEARCH
207         this->m_inputSearchProgram = NULL;
208 #endif
209 }
210
211 bool VariableSizeBokehBlurOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
212 {
213         rcti newInput;
214         rcti bokehInput;
215
216         newInput.xmax = input->xmax + this->m_maxBlur + 2;
217         newInput.xmin = input->xmin - this->m_maxBlur + 2;
218         newInput.ymax = input->ymax + this->m_maxBlur - 2;
219         newInput.ymin = input->ymin - this->m_maxBlur - 2;
220         bokehInput.xmax = COM_BLUR_BOKEH_PIXELS;
221         bokehInput.xmin = 0;
222         bokehInput.ymax = COM_BLUR_BOKEH_PIXELS;
223         bokehInput.ymin = 0;
224         
225
226         NodeOperation *operation = getInputOperation(2);
227         if (operation->determineDependingAreaOfInterest(&newInput, readOperation, output) ) {
228                 return true;
229         }
230         operation = getInputOperation(1);
231         if (operation->determineDependingAreaOfInterest(&bokehInput, readOperation, output) ) {
232                 return true;
233         }
234 #ifdef COM_DEFOCUS_SEARCH
235         rcti searchInput;
236         searchInput.xmax = (input->xmax / InverseSearchRadiusOperation::DIVIDER) + 1;
237         searchInput.xmin = (input->xmin / InverseSearchRadiusOperation::DIVIDER) - 1;
238         searchInput.ymax = (input->ymax / InverseSearchRadiusOperation::DIVIDER) + 1;
239         searchInput.ymin = (input->ymin / InverseSearchRadiusOperation::DIVIDER) - 1;
240         operation = getInputOperation(3);
241         if (operation->determineDependingAreaOfInterest(&searchInput, readOperation, output) ) {
242                 return true;
243         }
244 #endif
245         operation = getInputOperation(0);
246         if (operation->determineDependingAreaOfInterest(&newInput, readOperation, output) ) {
247                 return true;
248         }
249         return false;
250 }
251
252 #ifdef COM_DEFOCUS_SEARCH
253 // InverseSearchRadiusOperation
254 InverseSearchRadiusOperation::InverseSearchRadiusOperation() : NodeOperation() 
255 {
256         this->addInputSocket(COM_DT_VALUE, COM_SC_NO_RESIZE); // radius
257         this->addOutputSocket(COM_DT_COLOR);
258         this->setComplex(true);
259         this->m_inputRadius = NULL;
260 }
261
262 void InverseSearchRadiusOperation::initExecution() 
263 {
264         this->m_inputRadius = this->getInputSocketReader(0);
265 }
266
267 voi *InverseSearchRadiusOperation::initializeTileData(rcti *rect)
268 {
269         MemoryBuffer * data = new MemoryBuffer(NULL, rect);
270         float *buffer = data->getBuffer();
271         int x, y;
272         int width = this->m_inputRadius->getWidth();
273         int height = this->m_inputRadius->getHeight();
274         float temp[4];
275         int offset = 0;
276         for (y = rect->ymin; y < rect->ymax ; y++) {
277                 for (x = rect->xmin; x < rect->xmax ; x++) {
278                         int rx = x * DIVIDER;
279                         int ry = y * DIVIDER;
280                         buffer[offset] = MAX2(rx - m_maxBlur, 0);
281                         buffer[offset+1] = MAX2(ry- m_maxBlur, 0);
282                         buffer[offset+2] = MIN2(rx+DIVIDER + m_maxBlur, width);
283                         buffer[offset+3] = MIN2(ry+DIVIDER + m_maxBlur, height);
284                         offset += 4;
285                 }
286         }
287 //      for (x = rect->xmin; x < rect->xmax ; x++) {
288 //              for (y = rect->ymin; y < rect->ymax ; y++) {
289 //                      int rx = x * DIVIDER;
290 //                      int ry = y * DIVIDER;
291 //                      float radius = 0.0f;
292 //                      float maxx = x;
293 //                      float maxy = y;
294         
295 //                      for (int x2 = 0 ; x2 < DIVIDER ; x2 ++) {
296 //                              for (int y2 = 0 ; y2 < DIVIDER ; y2 ++) {
297 //                                      this->m_inputRadius->read(temp, rx+x2, ry+y2, COM_PS_NEAREST);
298 //                                      if (radius < temp[0]) {
299 //                                              radius = temp[0];
300 //                                              maxx = x2;
301 //                                              maxy = y2;
302 //                                      }
303 //                              }
304 //                      }
305 //                      int impactRadius = ceil(radius / DIVIDER);
306 //                      for (int x2 = x - impactRadius ; x2 < x + impactRadius ; x2 ++) {
307 //                              for (int y2 = y - impactRadius ; y2 < y + impactRadius ; y2 ++) {
308 //                                      data->read(temp, x2, y2);
309 //                                      temp[0] = MIN2(temp[0], maxx);
310 //                                      temp[1] = MIN2(temp[1], maxy);
311 //                                      temp[2] = MAX2(temp[2], maxx);
312 //                                      temp[3] = MAX2(temp[3], maxy);
313 //                                      data->writePixel(x2, y2, temp);
314 //                              }
315 //                      }
316 //              }
317 //      }
318         return data;
319 }
320
321 void InverseSearchRadiusOperation::executePixel(float output[4], int x, int y, void *data)
322 {
323         MemoryBuffer *buffer = (MemoryBuffer *)data;
324         buffer->readNoCheck(color, x, y);
325 }
326
327 void InverseSearchRadiusOperation::deinitializeTileData(rcti *rect, void *data) 
328 {
329         if (data) {
330                 MemoryBuffer *mb = (MemoryBuffer *)data;
331                 delete mb;
332         }
333 }
334
335 void InverseSearchRadiusOperation::deinitExecution() 
336 {
337         this->m_inputRadius = NULL;
338 }
339
340 void InverseSearchRadiusOperation::determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2])
341 {
342         NodeOperation::determineResolution(resolution, preferredResolution);
343         resolution[0] = resolution[0] / DIVIDER;
344         resolution[1] = resolution[1] / DIVIDER;
345 }
346
347 bool InverseSearchRadiusOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
348 {
349         rcti newRect;
350         newRect.ymin = input->ymin * DIVIDER - m_maxBlur;
351         newRect.ymax = input->ymax * DIVIDER + m_maxBlur;
352         newRect.xmin = input->xmin * DIVIDER - m_maxBlur;
353         newRect.xmax = input->xmax * DIVIDER + m_maxBlur;
354         return NodeOperation::determineDependingAreaOfInterest(&newRect, readOperation, output);
355 }
356 #endif