Cycles Denoising: Merge outlier heuristic and confidence interval test
[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                                                        const ccl_global float *ccl_restrict transform,
26                                                        ccl_global int *rank,
27                                                        float weight,
28                                                        ccl_global float *XtWX,
29                                                        ccl_global float3 *XtWY,
30                                                        int localIdx)
31 {
32         int p_offset =  y    *w +  x;
33         int q_offset = (y+dy)*w + (x+dx);
34
35 #ifdef __KERNEL_CPU__
36         const int stride = 1;
37         (void)storage_stride;
38         (void)localIdx;
39         float design_row[DENOISE_FEATURES+1];
40 #elif defined(__KERNEL_CUDA__)
41         const int stride = storage_stride;
42         ccl_local float shared_design_row[(DENOISE_FEATURES+1)*CCL_MAX_LOCAL_SIZE];
43         ccl_local_param float *design_row = shared_design_row + localIdx*(DENOISE_FEATURES+1);
44 #else
45         const int stride = storage_stride;
46         float design_row[DENOISE_FEATURES+1];
47 #endif
48
49         float3 q_color = filter_get_color(buffer + q_offset, pass_stride);
50
51         /* If the pixel was flagged as an outlier during prefiltering, skip it. */
52         if(ccl_get_feature(buffer + q_offset, 0) < 0.0f) {
53                 return;
54         }
55
56         filter_get_design_row_transform(make_int2(x, y),       buffer + p_offset,
57                                         make_int2(x+dx, y+dy), buffer + q_offset,
58                                         pass_stride, *rank, design_row, transform, stride);
59
60         math_trimatrix_add_gramian_strided(XtWX, (*rank)+1, design_row, weight, stride);
61         math_vec3_add_strided(XtWY, (*rank)+1, design_row, weight * q_color, stride);
62 }
63
64 ccl_device_inline void kernel_filter_finalize(int x, int y, int w, int h,
65                                               ccl_global float *buffer,
66                                               ccl_global int *rank,
67                                               int storage_stride,
68                                               ccl_global float *XtWX,
69                                               ccl_global float3 *XtWY,
70                                               int4 buffer_params,
71                                               int sample)
72 {
73 #ifdef __KERNEL_CPU__
74         const int stride = 1;
75         (void)storage_stride;
76 #else
77         const int stride = storage_stride;
78 #endif
79
80         /* The weighted average of pixel colors (essentially, the NLM-filtered image).
81          * In case the solution of the linear model fails due to numerical issues,
82          * fall back to this value. */
83         float3 mean_color = XtWY[0]/XtWX[0];
84
85         math_trimatrix_vec3_solve(XtWX, XtWY, (*rank)+1, stride);
86
87         float3 final_color = XtWY[0];
88         if(!isfinite3_safe(final_color)) {
89                 final_color = mean_color;
90         }
91
92         ccl_global float *combined_buffer = buffer + (y*buffer_params.y + x + buffer_params.x)*buffer_params.z;
93         final_color *= sample;
94         if(buffer_params.w) {
95                 final_color.x += combined_buffer[buffer_params.w+0];
96                 final_color.y += combined_buffer[buffer_params.w+1];
97                 final_color.z += combined_buffer[buffer_params.w+2];
98         }
99         combined_buffer[0] = final_color.x;
100         combined_buffer[1] = final_color.y;
101         combined_buffer[2] = final_color.z;
102 }
103
104 #undef STORAGE_TYPE
105
106 CCL_NAMESPACE_END