Cycles Denoising: Merge outlier heuristic and confidence interval test
[blender.git] / intern / cycles / kernel / filter / filter_features.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 #define ccl_get_feature(buffer, pass) (buffer)[(pass)*pass_stride]
20
21 /* Loop over the pixels in the range [low.x, high.x) x [low.y, high.y).
22  * pixel_buffer always points to the current pixel in the first pass. */
23 #define FOR_PIXEL_WINDOW     pixel_buffer = buffer + (low.y - rect.y)*buffer_w + (low.x - rect.x); \
24                              for(pixel.y = low.y; pixel.y < high.y; pixel.y++) { \
25                                  for(pixel.x = low.x; pixel.x < high.x; pixel.x++, pixel_buffer++) {
26
27 #define END_FOR_PIXEL_WINDOW     } \
28                                  pixel_buffer += buffer_w - (high.x - low.x); \
29                              }
30
31 ccl_device_inline void filter_get_features(int2 pixel,
32                                            const ccl_global float *ccl_restrict buffer,
33                                            float *features,
34                                            const float *ccl_restrict mean,
35                                            int pass_stride)
36 {
37         features[0] = pixel.x;
38         features[1] = pixel.y;
39         features[2] = fabsf(ccl_get_feature(buffer, 0));
40         features[3] = ccl_get_feature(buffer, 1);
41         features[4] = ccl_get_feature(buffer, 2);
42         features[5] = ccl_get_feature(buffer, 3);
43         features[6] = ccl_get_feature(buffer, 4);
44         features[7] = ccl_get_feature(buffer, 5);
45         features[8] = ccl_get_feature(buffer, 6);
46         features[9] = ccl_get_feature(buffer, 7);
47         if(mean) {
48                 for(int i = 0; i < DENOISE_FEATURES; i++)
49                         features[i] -= mean[i];
50         }
51 }
52
53 ccl_device_inline void filter_get_feature_scales(int2 pixel,
54                                                  const ccl_global float *ccl_restrict buffer,
55                                                  float *scales,
56                                                  const float *ccl_restrict mean,
57                                                  int pass_stride)
58 {
59         scales[0] = fabsf(pixel.x - mean[0]);
60         scales[1] = fabsf(pixel.y - mean[1]);
61         scales[2] = fabsf(fabsf(ccl_get_feature(buffer, 0)) - mean[2]);
62         scales[3] = len_squared(make_float3(ccl_get_feature(buffer, 1) - mean[3],
63                                             ccl_get_feature(buffer, 2) - mean[4],
64                                             ccl_get_feature(buffer, 3) - mean[5]));
65         scales[4] = fabsf(ccl_get_feature(buffer, 4) - mean[6]);
66         scales[5] = len_squared(make_float3(ccl_get_feature(buffer, 5) - mean[7],
67                                             ccl_get_feature(buffer, 6) - mean[8],
68                                             ccl_get_feature(buffer, 7) - mean[9]));
69 }
70
71 ccl_device_inline void filter_calculate_scale(float *scale)
72 {
73         scale[0] = 1.0f/max(scale[0], 0.01f);
74         scale[1] = 1.0f/max(scale[1], 0.01f);
75         scale[2] = 1.0f/max(scale[2], 0.01f);
76         scale[6] = 1.0f/max(scale[4], 0.01f);
77         scale[7] = scale[8] = scale[9] = 1.0f/max(sqrtf(scale[5]), 0.01f);
78         scale[3] = scale[4] = scale[5] = 1.0f/max(sqrtf(scale[3]), 0.01f);
79 }
80
81 ccl_device_inline float3 filter_get_color(const ccl_global float *ccl_restrict buffer,
82                                           int pass_stride)
83 {
84         return make_float3(ccl_get_feature(buffer, 8), ccl_get_feature(buffer, 9), ccl_get_feature(buffer, 10));
85 }
86
87 ccl_device_inline void design_row_add(float *design_row,
88                                       int rank,
89                                       const ccl_global float *ccl_restrict transform,
90                                       int stride,
91                                       int row,
92                                       float feature)
93 {
94         for(int i = 0; i < rank; i++) {
95                 design_row[1+i] += transform[(row*DENOISE_FEATURES + i)*stride]*feature;
96         }
97 }
98
99 /* Fill the design row. */
100 ccl_device_inline void filter_get_design_row_transform(int2 p_pixel,
101                                                        const ccl_global float *ccl_restrict p_buffer,
102                                                        int2 q_pixel,
103                                                        const ccl_global float *ccl_restrict q_buffer,
104                                                        int pass_stride,
105                                                        int rank,
106                                                        float *design_row,
107                                                        const ccl_global float *ccl_restrict transform,
108                                                        int stride)
109 {
110         design_row[0] = 1.0f;
111         math_vector_zero(design_row+1, rank);
112         design_row_add(design_row, rank, transform, stride, 0, q_pixel.x - p_pixel.x);
113         design_row_add(design_row, rank, transform, stride, 1, q_pixel.y - p_pixel.y);
114         design_row_add(design_row, rank, transform, stride, 2, fabsf(ccl_get_feature(q_buffer, 0)) - fabsf(ccl_get_feature(p_buffer, 0)));
115         design_row_add(design_row, rank, transform, stride, 3, ccl_get_feature(q_buffer, 1) - ccl_get_feature(p_buffer, 1));
116         design_row_add(design_row, rank, transform, stride, 4, ccl_get_feature(q_buffer, 2) - ccl_get_feature(p_buffer, 2));
117         design_row_add(design_row, rank, transform, stride, 5, ccl_get_feature(q_buffer, 3) - ccl_get_feature(p_buffer, 3));
118         design_row_add(design_row, rank, transform, stride, 6, ccl_get_feature(q_buffer, 4) - ccl_get_feature(p_buffer, 4));
119         design_row_add(design_row, rank, transform, stride, 7, ccl_get_feature(q_buffer, 5) - ccl_get_feature(p_buffer, 5));
120         design_row_add(design_row, rank, transform, stride, 8, ccl_get_feature(q_buffer, 6) - ccl_get_feature(p_buffer, 6));
121         design_row_add(design_row, rank, transform, stride, 9, ccl_get_feature(q_buffer, 7) - ccl_get_feature(p_buffer, 7));
122 }
123
124 CCL_NAMESPACE_END