Cycles Denoising: Skip confidence interval test for outlier central pixels
[blender.git] / intern / cycles / kernel / filter / filter_reconstruction.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_construct_gramian(int x, int y,
20                                                        int storage_stride,
21                                                        int dx, int dy,
22                                                        int w, int h,
23                                                        int pass_stride,
24                                                        const ccl_global float *ccl_restrict buffer,
25                                                        ccl_global float *color_pass,
26                                                        ccl_global float *variance_pass,
27                                                        const ccl_global float *ccl_restrict transform,
28                                                        ccl_global int *rank,
29                                                        float weight,
30                                                        ccl_global float *XtWX,
31                                                        ccl_global float3 *XtWY,
32                                                        int localIdx)
33 {
34         int p_offset =  y    *w +  x;
35         int q_offset = (y+dy)*w + (x+dx);
36
37 #ifdef __KERNEL_CPU__
38         const int stride = 1;
39         (void)storage_stride;
40         (void)localIdx;
41         float design_row[DENOISE_FEATURES+1];
42 #elif defined(__KERNEL_CUDA__)
43         const int stride = storage_stride;
44         ccl_local float shared_design_row[(DENOISE_FEATURES+1)*CCL_MAX_LOCAL_SIZE];
45         ccl_local_param float *design_row = shared_design_row + localIdx*(DENOISE_FEATURES+1);
46 #else
47         const int stride = storage_stride;
48         float design_row[DENOISE_FEATURES+1];
49 #endif
50
51         float3 p_color = filter_get_pixel_color(color_pass + p_offset, pass_stride);
52         float3 q_color = filter_get_pixel_color(color_pass + q_offset, pass_stride);
53
54         float p_std_dev = sqrtf(filter_get_pixel_variance(variance_pass + p_offset, pass_stride));
55         float q_std_dev = sqrtf(filter_get_pixel_variance(variance_pass + q_offset, pass_stride));
56
57         /* If the pixel was flagged as an outlier during prefiltering, skip it.
58          * Otherwise, perform the regular confidence interval test unless
59          * the center pixel is an outlier (in that case, using the confidence
60          * interval test could result in no pixels being used at all). */
61         bool p_outlier = (ccl_get_feature(buffer + p_offset, 0) < 0.0f);
62         bool q_outlier = (ccl_get_feature(buffer + q_offset, 0) < 0.0f);
63         bool outside_of_interval = (average(fabs(p_color - q_color)) > 2.0f*(p_std_dev + q_std_dev + 1e-3f));
64
65         if(q_outlier || (!p_outlier && outside_of_interval)) {
66                 return;
67         }
68
69         filter_get_design_row_transform(make_int2(x, y),       buffer + p_offset,
70                                         make_int2(x+dx, y+dy), buffer + q_offset,
71                                         pass_stride, *rank, design_row, transform, stride);
72
73         math_trimatrix_add_gramian_strided(XtWX, (*rank)+1, design_row, weight, stride);
74         math_vec3_add_strided(XtWY, (*rank)+1, design_row, weight * q_color, stride);
75 }
76
77 ccl_device_inline void kernel_filter_finalize(int x, int y, int w, int h,
78                                               ccl_global float *buffer,
79                                               ccl_global int *rank,
80                                               int storage_stride,
81                                               ccl_global float *XtWX,
82                                               ccl_global float3 *XtWY,
83                                               int4 buffer_params,
84                                               int sample)
85 {
86 #ifdef __KERNEL_CPU__
87         const int stride = 1;
88         (void)storage_stride;
89 #else
90         const int stride = storage_stride;
91 #endif
92
93         /* The weighted average of pixel colors (essentially, the NLM-filtered image).
94          * In case the solution of the linear model fails due to numerical issues,
95          * fall back to this value. */
96         float3 mean_color = XtWY[0]/XtWX[0];
97
98         math_trimatrix_vec3_solve(XtWX, XtWY, (*rank)+1, stride);
99
100         float3 final_color = XtWY[0];
101         if(!isfinite3_safe(final_color)) {
102                 final_color = mean_color;
103         }
104
105         ccl_global float *combined_buffer = buffer + (y*buffer_params.y + x + buffer_params.x)*buffer_params.z;
106         final_color *= sample;
107         if(buffer_params.w) {
108                 final_color.x += combined_buffer[buffer_params.w+0];
109                 final_color.y += combined_buffer[buffer_params.w+1];
110                 final_color.z += combined_buffer[buffer_params.w+2];
111         }
112         combined_buffer[0] = final_color.x;
113         combined_buffer[1] = final_color.y;
114         combined_buffer[2] = final_color.z;
115 }
116
117 #undef STORAGE_TYPE
118
119 CCL_NAMESPACE_END