Cycles Denoising: Merge outlier heuristic and confidence interval test
[blender.git] / intern / cycles / kernel / filter / filter_nlm_gpu.h
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 CCL_NAMESPACE_BEGIN
18
19 ccl_device_inline void kernel_filter_nlm_calc_difference(int x, int y,
20                                                          int dx, int dy,
21                                                          const ccl_global float *ccl_restrict weight_image,
22                                                          const ccl_global float *ccl_restrict variance_image,
23                                                          ccl_global float *difference_image,
24                                                          int4 rect, int w,
25                                                          int channel_offset,
26                                                          float a, float k_2)
27 {
28         float diff = 0.0f;
29         int numChannels = channel_offset? 3 : 1;
30         for(int c = 0; c < numChannels; c++) {
31                 float cdiff = weight_image[c*channel_offset + y*w+x] - weight_image[c*channel_offset + (y+dy)*w+(x+dx)];
32                 float pvar = variance_image[c*channel_offset + y*w+x];
33                 float qvar = variance_image[c*channel_offset + (y+dy)*w+(x+dx)];
34                 diff += (cdiff*cdiff - a*(pvar + min(pvar, qvar))) / (1e-8f + k_2*(pvar+qvar));
35         }
36         if(numChannels > 1) {
37                 diff *= 1.0f/numChannels;
38         }
39         difference_image[y*w+x] = diff;
40 }
41
42 ccl_device_inline void kernel_filter_nlm_blur(int x, int y,
43                                               const ccl_global float *ccl_restrict difference_image,
44                                               ccl_global float *out_image,
45                                               int4 rect, int w, int f)
46 {
47         float sum = 0.0f;
48         const int low = max(rect.y, y-f);
49         const int high = min(rect.w, y+f+1);
50         for(int y1 = low; y1 < high; y1++) {
51                 sum += difference_image[y1*w+x];
52         }
53         sum *= 1.0f/(high-low);
54         out_image[y*w+x] = sum;
55 }
56
57 ccl_device_inline void kernel_filter_nlm_calc_weight(int x, int y,
58                                                      const ccl_global float *ccl_restrict difference_image,
59                                                      ccl_global float *out_image,
60                                                      int4 rect, int w, int f)
61 {
62         float sum = 0.0f;
63         const int low = max(rect.x, x-f);
64         const int high = min(rect.z, x+f+1);
65         for(int x1 = low; x1 < high; x1++) {
66                 sum += difference_image[y*w+x1];
67         }
68         sum *= 1.0f/(high-low);
69         out_image[y*w+x] = expf(-max(sum, 0.0f));
70 }
71
72 ccl_device_inline void kernel_filter_nlm_update_output(int x, int y,
73                                                        int dx, int dy,
74                                                        const ccl_global float *ccl_restrict difference_image,
75                                                        const ccl_global float *ccl_restrict image,
76                                                        ccl_global float *out_image,
77                                                        ccl_global float *accum_image,
78                                                        int4 rect, int w, int f)
79 {
80         float sum = 0.0f;
81         const int low = max(rect.x, x-f);
82         const int high = min(rect.z, x+f+1);
83         for(int x1 = low; x1 < high; x1++) {
84                 sum += difference_image[y*w+x1];
85         }
86         sum *= 1.0f/(high-low);
87         if(out_image) {
88                 accum_image[y*w+x] += sum;
89                 out_image[y*w+x] += sum*image[(y+dy)*w+(x+dx)];
90         }
91         else {
92                 accum_image[y*w+x] = sum;
93         }
94 }
95
96 ccl_device_inline void kernel_filter_nlm_construct_gramian(int fx, int fy,
97                                                            int dx, int dy,
98                                                            const ccl_global float *ccl_restrict difference_image,
99                                                            const ccl_global float *ccl_restrict buffer,
100                                                            const ccl_global float *ccl_restrict transform,
101                                                            ccl_global int *rank,
102                                                            ccl_global float *XtWX,
103                                                            ccl_global float3 *XtWY,
104                                                            int4 rect,
105                                                            int4 filter_rect,
106                                                            int w, int h, int f,
107                                                            int pass_stride,
108                                                            int localIdx)
109 {
110         int y = fy + filter_rect.y;
111         int x = fx + filter_rect.x;
112         const int low = max(rect.x, x-f);
113         const int high = min(rect.z, x+f+1);
114         float sum = 0.0f;
115         for(int x1 = low; x1 < high; x1++) {
116                 sum += difference_image[y*w+x1];
117         }
118         float weight = sum * (1.0f/(high - low));
119
120         int storage_ofs = fy*filter_rect.z + fx;
121         transform += storage_ofs;
122         rank += storage_ofs;
123         XtWX += storage_ofs;
124         XtWY += storage_ofs;
125
126         kernel_filter_construct_gramian(x, y,
127                                         filter_rect.z*filter_rect.w,
128                                         dx, dy, w, h,
129                                         pass_stride,
130                                         buffer,
131                                         transform, rank,
132                                         weight, XtWX, XtWY,
133                                         localIdx);
134 }
135
136 ccl_device_inline void kernel_filter_nlm_normalize(int x, int y,
137                                                    ccl_global float *out_image,
138                                                    const ccl_global float *ccl_restrict accum_image,
139                                                    int4 rect, int w)
140 {
141         out_image[y*w+x] /= accum_image[y*w+x];
142 }
143
144 CCL_NAMESPACE_END