Correction top previous commit: tiles were counted in opposite way
[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 #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 *color, 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] = {0.0f, 0.0f, 0.0f, 0.0f};
105         float color_accum[4]      = {0.0f, 0.0f, 0.0f, 0.0f};
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 = MAX2(x - maxBlur, 0.0f);
117         int miny = MAX2(y - maxBlur, 0.0f);
118         int maxx = MIN2(x + maxBlur, m_width);
119         int maxy = MIN2(y + maxBlur, m_height);
120 #endif
121         {
122                 inputSizeBuffer->readNoCheck(tempSize, x, y);
123                 inputProgramBuffer->readNoCheck(readColor, x, y);
124
125                 add_v4_v4(color_accum, readColor);
126                 add_v4_fl(multiplier_accum, 1.0f);
127                 float sizeCenter = tempSize[0];
128                 
129                 const int addXStep = QualityStepHelper::getStep()*COM_NUMBER_OF_CHANNELS;
130                 
131                 if (sizeCenter > 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                                         {
139                                                 float size = inputSizeFloatBuffer[offsetNxNy];
140                                                 if (size > this->m_threshold) {
141                                                         float fsize = fabsf(size);
142                                                         float dx = nx - x;
143                                                         if (fsize > fabsf(dx) && fsize > fabsf(dy)) {
144                                                                 float u = (256.0f + (dx/size) * 255.0f);
145                                                                 float v = (256.0f + (dy/size) * 255.0f);
146                                                                 inputBokehBuffer->readNoCheck(bokeh, u, v);
147                                                                 madd_v4_v4v4(color_accum, bokeh, &inputProgramFloatBuffer[offsetNxNy]);
148                                                                 add_v4_v4(multiplier_accum, bokeh);
149                                                         }
150                                                 }
151                                         }
152                                         offsetNxNy += addXStep;
153                                 }
154                         }
155                 }
156
157                 color[0] = color_accum[0] / multiplier_accum[0];
158                 color[1] = color_accum[1] / multiplier_accum[1];
159                 color[2] = color_accum[2] / multiplier_accum[2];
160                 color[3] = color_accum[3] / multiplier_accum[3];
161         }
162
163 }
164
165 void VariableSizeBokehBlurOperation::executeOpenCL(OpenCLDevice* device,
166                                        MemoryBuffer *outputMemoryBuffer, cl_mem clOutputBuffer, 
167                                        MemoryBuffer **inputMemoryBuffers, list<cl_mem> *clMemToCleanUp, 
168                                        list<cl_kernel> *clKernelsToCleanUp) 
169 {
170         cl_kernel defocusKernel = device->COM_clCreateKernel("defocusKernel", NULL);
171
172         cl_int step = this->getStep();
173         cl_int maxBlur;
174         cl_float threshold = this->m_threshold;
175         
176         MemoryBuffer *sizeMemoryBuffer = (MemoryBuffer *)this->m_inputSizeProgram->getInputMemoryBuffer(inputMemoryBuffers);
177         maxBlur = (cl_int)sizeMemoryBuffer->getMaximumValue();
178         maxBlur = MIN2(maxBlur, this->m_maxBlur);
179
180         device->COM_clAttachMemoryBufferToKernelParameter(defocusKernel, 0, -1, clMemToCleanUp, inputMemoryBuffers, this->m_inputProgram);
181         device->COM_clAttachMemoryBufferToKernelParameter(defocusKernel, 1,  -1, clMemToCleanUp, inputMemoryBuffers, this->m_inputBokehProgram);
182         device->COM_clAttachMemoryBufferToKernelParameter(defocusKernel, 2,  4, clMemToCleanUp, inputMemoryBuffers, this->m_inputSizeProgram);
183         device->COM_clAttachOutputMemoryBufferToKernelParameter(defocusKernel, 3, clOutputBuffer);
184         device->COM_clAttachMemoryBufferOffsetToKernelParameter(defocusKernel, 5, outputMemoryBuffer);
185         clSetKernelArg(defocusKernel, 6, sizeof(cl_int), &step);
186         clSetKernelArg(defocusKernel, 7, sizeof(cl_int), &maxBlur);
187         clSetKernelArg(defocusKernel, 8, sizeof(cl_float), &threshold);
188         device->COM_clAttachSizeToKernelParameter(defocusKernel, 9, this);
189         
190         device->COM_clEnqueueRange(defocusKernel, outputMemoryBuffer, 10, this);
191 }
192
193 void VariableSizeBokehBlurOperation::deinitExecution()
194 {
195         this->m_inputProgram = NULL;
196         this->m_inputBokehProgram = NULL;
197         this->m_inputSizeProgram = NULL;
198 #ifdef COM_DEFOCUS_SEARCH
199         this->m_inputSearchProgram = NULL;
200 #endif
201 }
202
203 bool VariableSizeBokehBlurOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
204 {
205         rcti newInput;
206         rcti bokehInput;
207
208         newInput.xmax = input->xmax + this->m_maxBlur + 2;
209         newInput.xmin = input->xmin - this->m_maxBlur + 2;
210         newInput.ymax = input->ymax + this->m_maxBlur - 2;
211         newInput.ymin = input->ymin - this->m_maxBlur - 2;
212         bokehInput.xmax = 512;
213         bokehInput.xmin = 0;
214         bokehInput.ymax = 512;
215         bokehInput.ymin = 0;
216         
217
218         NodeOperation *operation = getInputOperation(2);
219         if (operation->determineDependingAreaOfInterest(&newInput, readOperation, output) ) {
220                 return true;
221         }
222         operation = getInputOperation(1);
223         if (operation->determineDependingAreaOfInterest(&bokehInput, readOperation, output) ) {
224                 return true;
225         }
226 #ifdef COM_DEFOCUS_SEARCH
227         rcti searchInput;
228         searchInput.xmax = (input->xmax / InverseSearchRadiusOperation::DIVIDER) + 1;
229         searchInput.xmin = (input->xmin / InverseSearchRadiusOperation::DIVIDER) - 1;
230         searchInput.ymax = (input->ymax / InverseSearchRadiusOperation::DIVIDER) + 1;
231         searchInput.ymin = (input->ymin / InverseSearchRadiusOperation::DIVIDER) - 1;
232         operation = getInputOperation(3);
233         if (operation->determineDependingAreaOfInterest(&searchInput, readOperation, output) ) {
234                 return true;
235         }
236 #endif
237         operation = getInputOperation(0);
238         if (operation->determineDependingAreaOfInterest(&newInput, readOperation, output) ) {
239                 return true;
240         }
241         return false;
242 }
243
244 #ifdef COM_DEFOCUS_SEARCH
245 // InverseSearchRadiusOperation
246 InverseSearchRadiusOperation::InverseSearchRadiusOperation() : NodeOperation() 
247 {
248         this->addInputSocket(COM_DT_VALUE, COM_SC_NO_RESIZE); // radius
249         this->addOutputSocket(COM_DT_COLOR);
250         this->setComplex(true);
251         this->m_inputRadius = NULL;
252 }
253
254 void InverseSearchRadiusOperation::initExecution() 
255 {
256         this->m_inputRadius = this->getInputSocketReader(0);
257 }
258
259 void* InverseSearchRadiusOperation::initializeTileData(rcti *rect) 
260 {
261         MemoryBuffer * data = new MemoryBuffer(NULL, rect);
262         float* buffer = data->getBuffer();
263         int x, y;
264         int width = this->m_inputRadius->getWidth();
265         int height = this->m_inputRadius->getHeight();
266         float temp[4];
267         int offset = 0;
268         for (y = rect->ymin; y < rect->ymax ; y++) {
269                 for (x = rect->xmin; x < rect->xmax ; x++) {
270                         int rx = x * DIVIDER;
271                         int ry = y * DIVIDER;
272                         buffer[offset] = MAX2(rx - m_maxBlur, 0);
273                         buffer[offset+1] = MAX2(ry- m_maxBlur, 0);
274                         buffer[offset+2] = MIN2(rx+DIVIDER + m_maxBlur, width);
275                         buffer[offset+3] = MIN2(ry+DIVIDER + m_maxBlur, height);
276                         offset += 4;
277                 }
278         }
279 //      for (x = rect->xmin; x < rect->xmax ; x++) {
280 //              for (y = rect->ymin; y < rect->ymax ; y++) {
281 //                      int rx = x * DIVIDER;
282 //                      int ry = y * DIVIDER;
283 //                      float radius = 0.0f;
284 //                      float maxx = x;
285 //                      float maxy = y;
286         
287 //                      for (int x2 = 0 ; x2 < DIVIDER ; x2 ++) {
288 //                              for (int y2 = 0 ; y2 < DIVIDER ; y2 ++) {
289 //                                      this->m_inputRadius->read(temp, rx+x2, ry+y2, COM_PS_NEAREST);
290 //                                      if (radius < temp[0]) {
291 //                                              radius = temp[0];
292 //                                              maxx = x2;
293 //                                              maxy = y2;
294 //                                      }
295 //                              }
296 //                      }
297 //                      int impactRadius = ceil(radius / DIVIDER);
298 //                      for (int x2 = x - impactRadius ; x2 < x + impactRadius ; x2 ++) {
299 //                              for (int y2 = y - impactRadius ; y2 < y + impactRadius ; y2 ++) {
300 //                                      data->read(temp, x2, y2);
301 //                                      temp[0] = MIN2(temp[0], maxx);
302 //                                      temp[1] = MIN2(temp[1], maxy);
303 //                                      temp[2] = MAX2(temp[2], maxx);
304 //                                      temp[3] = MAX2(temp[3], maxy);
305 //                                      data->writePixel(x2, y2, temp);
306 //                              }
307 //                      }
308 //              }
309 //      }
310         return data;
311 }
312
313 void InverseSearchRadiusOperation::executePixel(float *color, int x, int y, void *data) 
314 {
315         MemoryBuffer *buffer = (MemoryBuffer*)data;
316         buffer->readNoCheck(color, x, y);
317 }
318
319 void InverseSearchRadiusOperation::deinitializeTileData(rcti *rect, void *data) 
320 {
321         if (data) {
322                 MemoryBuffer* mb = (MemoryBuffer*)data;
323                 delete mb;
324         }
325 }
326
327 void InverseSearchRadiusOperation::deinitExecution() 
328 {
329         this->m_inputRadius = NULL;
330 }
331
332 void InverseSearchRadiusOperation::determineResolution(unsigned int resolution[], unsigned int preferredResolution[])
333 {
334         NodeOperation::determineResolution(resolution, preferredResolution);
335         resolution[0] = resolution[0] / DIVIDER;
336         resolution[1] = resolution[1] / DIVIDER;
337 }
338
339 bool InverseSearchRadiusOperation::determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output)
340 {
341         rcti newRect;
342         newRect.ymin = input->ymin*DIVIDER - m_maxBlur;
343         newRect.ymax = input->ymax*DIVIDER + m_maxBlur;
344         newRect.xmin = input->xmin*DIVIDER - m_maxBlur;
345         newRect.xmax = input->xmax*DIVIDER + m_maxBlur;
346         return NodeOperation::determineDependingAreaOfInterest(&newRect, readOperation, output);
347 }
348 #endif