Cycles: Implement denoising option for reducing noise in the rendered image
[blender.git] / intern / cycles / kernel / kernels / opencl / filter.cl
1 /*
2  * Copyright 2011-2017 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* OpenCL kernel entry points */
18
19 #include "kernel/kernel_compat_opencl.h"
20
21 #include "kernel/filter/filter_kernel.h"
22
23 /* kernels */
24
25 __kernel void kernel_ocl_filter_divide_shadow(int sample,
26                                               ccl_global TilesInfo *tiles,
27                                               ccl_global float *unfilteredA,
28                                               ccl_global float *unfilteredB,
29                                               ccl_global float *sampleVariance,
30                                               ccl_global float *sampleVarianceV,
31                                               ccl_global float *bufferVariance,
32                                               int4 prefilter_rect,
33                                               int buffer_pass_stride,
34                                               int buffer_denoising_offset,
35                                               char use_split_variance)
36 {
37         int x = prefilter_rect.x + get_global_id(0);
38         int y = prefilter_rect.y + get_global_id(1);
39         if(x < prefilter_rect.z && y < prefilter_rect.w) {
40                 kernel_filter_divide_shadow(sample,
41                                             tiles,
42                                             x, y,
43                                             unfilteredA,
44                                             unfilteredB,
45                                             sampleVariance,
46                                             sampleVarianceV,
47                                             bufferVariance,
48                                             prefilter_rect,
49                                             buffer_pass_stride,
50                                             buffer_denoising_offset,
51                                             use_split_variance);
52         }
53 }
54
55 __kernel void kernel_ocl_filter_get_feature(int sample,
56                                             ccl_global TilesInfo *tiles,
57                                             int m_offset,
58                                             int v_offset,
59                                             ccl_global float *mean,
60                                             ccl_global float *variance,
61                                             int4 prefilter_rect,
62                                             int buffer_pass_stride,
63                                             int buffer_denoising_offset,
64                                             char use_split_variance)
65 {
66         int x = prefilter_rect.x + get_global_id(0);
67         int y = prefilter_rect.y + get_global_id(1);
68         if(x < prefilter_rect.z && y < prefilter_rect.w) {
69                 kernel_filter_get_feature(sample,
70                                           tiles,
71                                           m_offset, v_offset,
72                                           x, y,
73                                           mean, variance,
74                                           prefilter_rect,
75                                           buffer_pass_stride,
76                                           buffer_denoising_offset,
77                                           use_split_variance);
78         }
79 }
80
81 __kernel void kernel_ocl_filter_combine_halves(ccl_global float *mean,
82                                                ccl_global float *variance,
83                                                ccl_global float *a,
84                                                ccl_global float *b,
85                                                int4 prefilter_rect,
86                                                int r)
87 {
88         int x = prefilter_rect.x + get_global_id(0);
89         int y = prefilter_rect.y + get_global_id(1);
90         if(x < prefilter_rect.z && y < prefilter_rect.w) {
91                 kernel_filter_combine_halves(x, y, mean, variance, a, b, prefilter_rect, r);
92         }
93 }
94
95 __kernel void kernel_ocl_filter_construct_transform(ccl_global float ccl_restrict_ptr buffer,
96                                                     ccl_global float *transform,
97                                                     ccl_global int *rank,
98                                                     int4 filter_area,
99                                                     int4 rect,
100                                                     int pass_stride,
101                                                     int radius,
102                                                     float pca_threshold)
103 {
104         int x = get_global_id(0);
105         int y = get_global_id(1);
106         if(x < filter_area.z && y < filter_area.w) {
107                 ccl_global int *l_rank = rank + y*filter_area.z + x;
108                 ccl_global float *l_transform = transform + y*filter_area.z + x;
109                 kernel_filter_construct_transform(buffer,
110                                                   x + filter_area.x, y + filter_area.y,
111                                                   rect, pass_stride,
112                                                   l_transform, l_rank,
113                                                   radius, pca_threshold,
114                                                   filter_area.z*filter_area.w,
115                                                   get_local_id(1)*get_local_size(0) + get_local_id(0));
116         }
117 }
118
119 __kernel void kernel_ocl_filter_nlm_calc_difference(int dx,
120                                                     int dy,
121                                                     ccl_global float ccl_restrict_ptr weightImage,
122                                                     ccl_global float ccl_restrict_ptr varianceImage,
123                                                     ccl_global float *differenceImage,
124                                                     int4 rect,
125                                                     int w,
126                                                     int channel_offset,
127                                                     float a,
128                                                     float k_2) {
129         int x = get_global_id(0) + rect.x;
130         int y = get_global_id(1) + rect.y;
131         if(x < rect.z && y < rect.w) {
132                 kernel_filter_nlm_calc_difference(x, y, dx, dy, weightImage, varianceImage, differenceImage, rect, w, channel_offset, a, k_2);
133         }
134 }
135
136 __kernel void kernel_ocl_filter_nlm_blur(ccl_global float ccl_restrict_ptr differenceImage,
137                                          ccl_global float *outImage,
138                                          int4 rect,
139                                          int w,
140                                          int f) {
141         int x = get_global_id(0) + rect.x;
142         int y = get_global_id(1) + rect.y;
143         if(x < rect.z && y < rect.w) {
144                 kernel_filter_nlm_blur(x, y, differenceImage, outImage, rect, w, f);
145         }
146 }
147
148 __kernel void kernel_ocl_filter_nlm_calc_weight(ccl_global float ccl_restrict_ptr differenceImage,
149                                                 ccl_global float *outImage,
150                                                 int4 rect,
151                                                 int w,
152                                                 int f) {
153         int x = get_global_id(0) + rect.x;
154         int y = get_global_id(1) + rect.y;
155         if(x < rect.z && y < rect.w) {
156                 kernel_filter_nlm_calc_weight(x, y, differenceImage, outImage, rect, w, f);
157         }
158 }
159
160 __kernel void kernel_ocl_filter_nlm_update_output(int dx,
161                                                   int dy,
162                                                   ccl_global float ccl_restrict_ptr differenceImage,
163                                                   ccl_global float ccl_restrict_ptr image,
164                                                   ccl_global float *outImage,
165                                                   ccl_global float *accumImage,
166                                                   int4 rect,
167                                                   int w,
168                                                   int f) {
169         int x = get_global_id(0) + rect.x;
170         int y = get_global_id(1) + rect.y;
171         if(x < rect.z && y < rect.w) {
172                 kernel_filter_nlm_update_output(x, y, dx, dy, differenceImage, image, outImage, accumImage, rect, w, f);
173         }
174 }
175
176 __kernel void kernel_ocl_filter_nlm_normalize(ccl_global float *outImage,
177                                               ccl_global float ccl_restrict_ptr accumImage,
178                                               int4 rect,
179                                               int w) {
180         int x = get_global_id(0) + rect.x;
181         int y = get_global_id(1) + rect.y;
182         if(x < rect.z && y < rect.w) {
183                 kernel_filter_nlm_normalize(x, y, outImage, accumImage, rect, w);
184         }
185 }
186
187 __kernel void kernel_ocl_filter_nlm_construct_gramian(int dx,
188                                                       int dy,
189                                                       ccl_global float ccl_restrict_ptr differenceImage,
190                                                       ccl_global float ccl_restrict_ptr buffer,
191                                                       ccl_global float *color_pass,
192                                                       ccl_global float *variance_pass,
193                                                       ccl_global float ccl_restrict_ptr transform,
194                                                       ccl_global int *rank,
195                                                       ccl_global float *XtWX,
196                                                       ccl_global float3 *XtWY,
197                                                       int4 rect,
198                                                       int4 filter_rect,
199                                                       int w,
200                                                       int h,
201                                                       int f,
202                                                       int pass_stride) {
203         int x = get_global_id(0) + max(0, rect.x-filter_rect.x);
204         int y = get_global_id(1) + max(0, rect.y-filter_rect.y);
205         if(x < min(filter_rect.z, rect.z-filter_rect.x) && y < min(filter_rect.w, rect.w-filter_rect.y)) {
206                 kernel_filter_nlm_construct_gramian(x, y,
207                                                     dx, dy,
208                                                     differenceImage,
209                                                     buffer,
210                                                     color_pass, variance_pass,
211                                                     transform, rank,
212                                                     XtWX, XtWY,
213                                                     rect, filter_rect,
214                                                     w, h, f,
215                                                     pass_stride,
216                                                     get_local_id(1)*get_local_size(0) + get_local_id(0));
217         }
218 }
219
220 __kernel void kernel_ocl_filter_finalize(int w,
221                                              int h,
222                                          ccl_global float *buffer,
223                                          ccl_global int *rank,
224                                          ccl_global float *XtWX,
225                                          ccl_global float3 *XtWY,
226                                          int4 filter_area,
227                                          int4 buffer_params,
228                                          int sample) {
229         int x = get_global_id(0);
230         int y = get_global_id(1);
231         if(x < filter_area.z && y < filter_area.w) {
232                 int storage_ofs = y*filter_area.z+x;
233                 rank += storage_ofs;
234                 XtWX += storage_ofs;
235                 XtWY += storage_ofs;
236                 kernel_filter_finalize(x, y, w, h, buffer, rank, filter_area.z*filter_area.w, XtWX, XtWY, buffer_params, sample);
237         }
238 }
239
240 __kernel void kernel_ocl_filter_set_tiles(ccl_global TilesInfo* tiles,
241                                           ccl_global float *buffer_1,
242                                           ccl_global float *buffer_2,
243                                           ccl_global float *buffer_3,
244                                           ccl_global float *buffer_4,
245                                           ccl_global float *buffer_5,
246                                           ccl_global float *buffer_6,
247                                           ccl_global float *buffer_7,
248                                           ccl_global float *buffer_8,
249                                           ccl_global float *buffer_9)
250 {
251         if((get_global_id(0) == 0) && (get_global_id(1) == 0)) {
252                 tiles->buffers[0] = buffer_1;
253                 tiles->buffers[1] = buffer_2;
254                 tiles->buffers[2] = buffer_3;
255                 tiles->buffers[3] = buffer_4;
256                 tiles->buffers[4] = buffer_5;
257                 tiles->buffers[5] = buffer_6;
258                 tiles->buffers[6] = buffer_7;
259                 tiles->buffers[7] = buffer_8;
260                 tiles->buffers[8] = buffer_9;
261         }
262 }