Correction top previous commit: tiles were counted in opposite way
[blender.git] / source / blender / compositor / operations / COM_OpenCLKernels.cl.h
1 /* clkernelstoh output of file <COM_OpenCLKernels_cl> */
2
3 const char * clkernelstoh_COM_OpenCLKernels_cl = "/*\n" \
4 " * Copyright 2011, Blender Foundation.\n" \
5 " *\n" \
6 " * This program is free software; you can redistribute it and/or\n" \
7 " * modify it under the terms of the GNU General Public License\n" \
8 " * as published by the Free Software Foundation; either version 2\n" \
9 " * of the License, or (at your option) any later version.\n" \
10 " *\n" \
11 " * This program is distributed in the hope that it will be useful,\n" \
12 " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n" \
13 " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n" \
14 " * GNU General Public License for more details.\n" \
15 " *\n" \
16 " * You should have received a copy of the GNU General Public License\n" \
17 " * along with this program; if not, write to the Free Software Foundation,\n" \
18 " * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n" \
19 " *\n" \
20 " * Contributor:\n" \
21 " *             Jeroen Bakker\n" \
22 " *             Monique Dewanchand\n" \
23 " */\n" \
24 "\n" \
25 "/// This file contains all opencl kernels for node-operation implementations\n" \
26 "\n" \
27 "// Global SAMPLERS\n" \
28 "const sampler_t SAMPLER_NEAREST       = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;\n" \
29 "const sampler_t SAMPLER_NEAREST_CLAMP = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;\n" \
30 "\n" \
31 "__constant const int2 zero = {0,0};\n" \
32 "\n" \
33 "// KERNEL --- BOKEH BLUR ---\n" \
34 "__kernel void bokehBlurKernel(__read_only image2d_t boundingBox, __read_only image2d_t inputImage,\n" \
35 "                              __read_only image2d_t bokehImage, __write_only image2d_t output,\n" \
36 "                              int2 offsetInput, int2 offsetOutput, int radius, int step, int2 dimension, int2 offset)\n" \
37 "{\n" \
38 "       int2 coords = {get_global_id(0), get_global_id(1)};\n" \
39 "       coords += offset;\n" \
40 "       float tempBoundingBox;\n" \
41 "       float4 color = {0.0f,0.0f,0.0f,0.0f};\n" \
42 "       float4 multiplyer = {0.0f,0.0f,0.0f,0.0f};\n" \
43 "       float4 bokeh;\n" \
44 "       const float radius2 = radius*2.0f;\n" \
45 "       const int2 realCoordinate = coords + offsetOutput;\n" \
46 "\n" \
47 "       tempBoundingBox = read_imagef(boundingBox, SAMPLER_NEAREST, coords).s0;\n" \
48 "\n" \
49 "       if (tempBoundingBox > 0.0f && radius > 0 ) {\n" \
50 "               const int2 bokehImageDim = get_image_dim(bokehImage);\n" \
51 "               const int2 bokehImageCenter = bokehImageDim/2;\n" \
52 "               const int2 minXY = max(realCoordinate - radius, zero);\n" \
53 "               const int2 maxXY = min(realCoordinate + radius, dimension);\n" \
54 "               int nx, ny;\n" \
55 "\n" \
56 "               float2 uv;\n" \
57 "               int2 inputXy;\n" \
58 "\n" \
59 "               for (ny = minXY.y, inputXy.y = ny - offsetInput.y ; ny < maxXY.y ; ny +=step, inputXy.y+=step) {\n" \
60 "                       uv.y = ((realCoordinate.y-ny)/radius2)*bokehImageDim.y+bokehImageCenter.y;\n" \
61 "\n" \
62 "                       for (nx = minXY.x, inputXy.x = nx - offsetInput.x; nx < maxXY.x ; nx +=step, inputXy.x+=step) {\n" \
63 "                               uv.x = ((realCoordinate.x-nx)/radius2)*bokehImageDim.x+bokehImageCenter.x;\n" \
64 "                               bokeh = read_imagef(bokehImage, SAMPLER_NEAREST, uv);\n" \
65 "                               color += bokeh * read_imagef(inputImage, SAMPLER_NEAREST, inputXy);\n" \
66 "                               multiplyer += bokeh;\n" \
67 "                       }\n" \
68 "               }\n" \
69 "               color /= multiplyer;\n" \
70 "\n" \
71 "       } else {\n" \
72 "               int2 imageCoordinates = realCoordinate - offsetInput;\n" \
73 "               color = read_imagef(inputImage, SAMPLER_NEAREST, imageCoordinates);\n" \
74 "       }\n" \
75 "\n" \
76 "       write_imagef(output, coords, color);\n" \
77 "}\n" \
78 "\n" \
79 "//KERNEL --- DEFOCUS /VARIABLESIZEBOKEHBLUR ---\n" \
80 "__kernel void defocusKernel(__read_only image2d_t inputImage, __read_only image2d_t bokehImage,\n" \
81 "                                       __read_only image2d_t inputSize,\n" \
82 "                                       __write_only image2d_t output, int2 offsetInput, int2 offsetOutput,\n" \
83 "                                       int step, int maxBlur, float threshold, int2 dimension, int2 offset)\n" \
84 "{\n" \
85 "       float4 color = {1.0f, 0.0f, 0.0f, 1.0f};\n" \
86 "       int2 coords = {get_global_id(0), get_global_id(1)};\n" \
87 "       coords += offset;\n" \
88 "       const int2 realCoordinate = coords + offsetOutput;\n" \
89 "\n" \
90 "       float4 readColor;\n" \
91 "       float4 bokeh;\n" \
92 "       float tempSize;\n" \
93 "       float4 multiplier_accum = {1.0f, 1.0f, 1.0f, 1.0f};\n" \
94 "       float4 color_accum;\n" \
95 "\n" \
96 "       int minx = max(realCoordinate.s0 - maxBlur, 0);\n" \
97 "       int miny = max(realCoordinate.s1 - maxBlur, 0);\n" \
98 "       int maxx = min(realCoordinate.s0 + maxBlur, dimension.s0);\n" \
99 "       int maxy = min(realCoordinate.s1 + maxBlur, dimension.s1);\n" \
100 "\n" \
101 "       {\n" \
102 "               int2 inputCoordinate = realCoordinate - offsetInput;\n" \
103 "               float size = read_imagef(inputSize, SAMPLER_NEAREST, inputCoordinate).s0;\n" \
104 "               color_accum = read_imagef(inputImage, SAMPLER_NEAREST, inputCoordinate);\n" \
105 "\n" \
106 "               if (size > threshold) {\n" \
107 "                       for (int ny = miny; ny < maxy; ny += step) {\n" \
108 "                               inputCoordinate.s1 = ny - offsetInput.s1;\n" \
109 "                               float dy = ny - realCoordinate.s1;\n" \
110 "                               for (int nx = minx; nx < maxx; nx += step) {\n" \
111 "                                       float dx = nx - realCoordinate.s0;\n" \
112 "                                       if (dx != 0 || dy != 0) {\n" \
113 "                                               inputCoordinate.s0 = nx - offsetInput.s0;\n" \
114 "                                               tempSize = read_imagef(inputSize, SAMPLER_NEAREST, inputCoordinate).s0;\n" \
115 "                                               if (tempSize > threshold) {\n" \
116 "                                                       if (tempSize >= fabs(dx) && tempSize >= fabs(dy)) {\n" \
117 "                                                               float2 uv = { 256.0f + dx * 256.0f / tempSize, 256.0f + dy * 256.0f / tempSize};\n" \
118 "                                                               bokeh = read_imagef(bokehImage, SAMPLER_NEAREST, uv);\n" \
119 "                                                               readColor = read_imagef(inputImage, SAMPLER_NEAREST, inputCoordinate);\n" \
120 "                                                               color_accum += bokeh*readColor;\n" \
121 "                                                               multiplier_accum += bokeh;\n" \
122 "                                                       }\n" \
123 "                                               }\n" \
124 "                                       }\n" \
125 "                               }\n" \
126 "                       }\n" \
127 "               }\n" \
128 "       }\n" \
129 "\n" \
130 "       color = color_accum * (1.0f / multiplier_accum);\n" \
131 "       write_imagef(output, coords, color);\n" \
132 "}\n" \
133 "\n" \
134 "\n" \
135 "// KERNEL --- DILATE ---\n" \
136 "__kernel void dilateKernel(__read_only image2d_t inputImage,  __write_only image2d_t output,\n" \
137 "                           int2 offsetInput, int2 offsetOutput, int scope, int distanceSquared, int2 dimension,\n" \
138 "                           int2 offset)\n" \
139 "{\n" \
140 "       int2 coords = {get_global_id(0), get_global_id(1)};\n" \
141 "       coords += offset;\n" \
142 "       const int2 realCoordinate = coords + offsetOutput;\n" \
143 "\n" \
144 "       const int2 minXY = max(realCoordinate - scope, zero);\n" \
145 "       const int2 maxXY = min(realCoordinate + scope, dimension);\n" \
146 "\n" \
147 "       float value = 0.0f;\n" \
148 "       int nx, ny;\n" \
149 "       int2 inputXy;\n" \
150 "\n" \
151 "       for (ny = minXY.y, inputXy.y = ny - offsetInput.y ; ny < maxXY.y ; ny ++, inputXy.y++) {\n" \
152 "               const float deltaY = (realCoordinate.y - ny);\n" \
153 "               for (nx = minXY.x, inputXy.x = nx - offsetInput.x; nx < maxXY.x ; nx ++, inputXy.x++) {\n" \
154 "                       const float deltaX = (realCoordinate.x - nx);\n" \
155 "                       const float measuredDistance = deltaX*deltaX+deltaY*deltaY;\n" \
156 "                       if (measuredDistance <= distanceSquared) {\n" \
157 "                               value = max(value, read_imagef(inputImage, SAMPLER_NEAREST, inputXy).s0);\n" \
158 "                       }\n" \
159 "               }\n" \
160 "       }\n" \
161 "\n" \
162 "       float4 color = {value,0.0f,0.0f,0.0f};\n" \
163 "       write_imagef(output, coords, color);\n" \
164 "}\n" \
165 "\n" \
166 "// KERNEL --- DILATE ---\n" \
167 "__kernel void erodeKernel(__read_only image2d_t inputImage,  __write_only image2d_t output,\n" \
168 "                           int2 offsetInput, int2 offsetOutput, int scope, int distanceSquared, int2 dimension,\n" \
169 "                           int2 offset)\n" \
170 "{\n" \
171 "       int2 coords = {get_global_id(0), get_global_id(1)};\n" \
172 "       coords += offset;\n" \
173 "       const int2 realCoordinate = coords + offsetOutput;\n" \
174 "\n" \
175 "       const int2 minXY = max(realCoordinate - scope, zero);\n" \
176 "       const int2 maxXY = min(realCoordinate + scope, dimension);\n" \
177 "\n" \
178 "       float value = 1.0f;\n" \
179 "       int nx, ny;\n" \
180 "       int2 inputXy;\n" \
181 "\n" \
182 "       for (ny = minXY.y, inputXy.y = ny - offsetInput.y ; ny < maxXY.y ; ny ++, inputXy.y++) {\n" \
183 "               for (nx = minXY.x, inputXy.x = nx - offsetInput.x; nx < maxXY.x ; nx ++, inputXy.x++) {\n" \
184 "                       const float deltaX = (realCoordinate.x - nx);\n" \
185 "                       const float deltaY = (realCoordinate.y - ny);\n" \
186 "                       const float measuredDistance = deltaX*deltaX+deltaY*deltaY;\n" \
187 "                       if (measuredDistance <= distanceSquared) {\n" \
188 "                               value = min(value, read_imagef(inputImage, SAMPLER_NEAREST, inputXy).s0);\n" \
189 "                       }\n" \
190 "               }\n" \
191 "       }\n" \
192 "\n" \
193 "       float4 color = {value,0.0f,0.0f,0.0f};\n" \
194 "       write_imagef(output, coords, color);\n" \
195 "}\n" \
196 "\n" \
197 "// KERNEL --- DIRECTIONAL BLUR ---\n" \
198 "__kernel void directionalBlurKernel(__read_only image2d_t inputImage,  __write_only image2d_t output,\n" \
199 "                           int2 offsetOutput, int iterations, float scale, float rotation, float2 translate,\n" \
200 "                           float2 center, int2 offset)\n" \
201 "{\n" \
202 "       int2 coords = {get_global_id(0), get_global_id(1)};\n" \
203 "       coords += offset;\n" \
204 "       const int2 realCoordinate = coords + offsetOutput;\n" \
205 "\n" \
206 "       float4 col;\n" \
207 "       float2 ltxy = translate;\n" \
208 "       float lsc = scale;\n" \
209 "       float lrot = rotation;\n" \
210 "\n" \
211 "       col = read_imagef(inputImage, SAMPLER_NEAREST, realCoordinate);\n" \
212 "\n" \
213 "       /* blur the image */\n" \
214 "       for (int i = 0; i < iterations; ++i) {\n" \
215 "               const float cs = cos(lrot), ss = sin(lrot);\n" \
216 "               const float isc = 1.0f / (1.0f + lsc);\n" \
217 "\n" \
218 "               const float v = isc * (realCoordinate.s1 - center.s1) + ltxy.s1;\n" \
219 "               const float u = isc * (realCoordinate.s0 - center.s0) + ltxy.s0;\n" \
220 "               float2 uv = {\n" \
221 "                       cs * u + ss * v + center.s0,\n" \
222 "                       cs * v - ss * u + center.s1\n" \
223 "               };\n" \
224 "\n" \
225 "               col += read_imagef(inputImage, SAMPLER_NEAREST_CLAMP, uv);\n" \
226 "\n" \
227 "               /* double transformations */\n" \
228 "               ltxy += translate;\n" \
229 "               lrot += rotation;\n" \
230 "               lsc += scale;\n" \
231 "       }\n" \
232 "\n" \
233 "       col *= (1.0f/(iterations+1));\n" \
234 "\n" \
235 "       write_imagef(output, coords, col);\n" \
236 "}\n" \
237 "\0";