41998c792b69c8c32e2d5593c5728fc48add7448
[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, ccl_global float ccl_restrict_ptr buffer, float *features, float ccl_restrict_ptr mean, int pass_stride)
32 {
33         features[0] = pixel.x;
34         features[1] = pixel.y;
35         features[2] = fabsf(ccl_get_feature(buffer, 0));
36         features[3] = ccl_get_feature(buffer, 1);
37         features[4] = ccl_get_feature(buffer, 2);
38         features[5] = ccl_get_feature(buffer, 3);
39         features[6] = ccl_get_feature(buffer, 4);
40         features[7] = ccl_get_feature(buffer, 5);
41         features[8] = ccl_get_feature(buffer, 6);
42         features[9] = ccl_get_feature(buffer, 7);
43         if(mean) {
44                 for(int i = 0; i < DENOISE_FEATURES; i++)
45                         features[i] -= mean[i];
46         }
47 }
48
49 ccl_device_inline void filter_get_feature_scales(int2 pixel, ccl_global float ccl_restrict_ptr buffer, float *scales, float ccl_restrict_ptr mean, int pass_stride)
50 {
51         scales[0] = fabsf(pixel.x - mean[0]);
52         scales[1] = fabsf(pixel.y - mean[1]);
53         scales[2] = fabsf(fabsf(ccl_get_feature(buffer, 0)) - mean[2]);
54         scales[3] = len_squared(make_float3(ccl_get_feature(buffer, 1) - mean[3],
55                                             ccl_get_feature(buffer, 2) - mean[4],
56                                             ccl_get_feature(buffer, 3) - mean[5]));
57         scales[4] = fabsf(ccl_get_feature(buffer, 4) - mean[6]);
58         scales[5] = len_squared(make_float3(ccl_get_feature(buffer, 5) - mean[7],
59                                             ccl_get_feature(buffer, 6) - mean[8],
60                                             ccl_get_feature(buffer, 7) - mean[9]));
61 }
62
63 ccl_device_inline void filter_calculate_scale(float *scale)
64 {
65         scale[0] = 1.0f/max(scale[0], 0.01f);
66         scale[1] = 1.0f/max(scale[1], 0.01f);
67         scale[2] = 1.0f/max(scale[2], 0.01f);
68         scale[6] = 1.0f/max(scale[4], 0.01f);
69         scale[7] = scale[8] = scale[9] = 1.0f/max(sqrtf(scale[5]), 0.01f);
70         scale[3] = scale[4] = scale[5] = 1.0f/max(sqrtf(scale[3]), 0.01f);
71 }
72
73 ccl_device_inline float3 filter_get_pixel_color(ccl_global float ccl_restrict_ptr buffer, int pass_stride)
74 {
75         return make_float3(ccl_get_feature(buffer, 0), ccl_get_feature(buffer, 1), ccl_get_feature(buffer, 2));
76 }
77
78 ccl_device_inline float filter_get_pixel_variance(ccl_global float ccl_restrict_ptr buffer, int pass_stride)
79 {
80         return average(make_float3(ccl_get_feature(buffer, 0), ccl_get_feature(buffer, 1), ccl_get_feature(buffer, 2)));
81 }
82
83 ccl_device_inline void design_row_add(float *design_row,
84                                       int rank,
85                                       ccl_global float ccl_restrict_ptr transform,
86                                       int stride,
87                                       int row,
88                                       float feature)
89 {
90         for(int i = 0; i < rank; i++) {
91                 design_row[1+i] += transform[(row*DENOISE_FEATURES + i)*stride]*feature;
92         }
93 }
94
95 /* Fill the design row. */
96 ccl_device_inline void filter_get_design_row_transform(int2 p_pixel,
97                                                        ccl_global float ccl_restrict_ptr p_buffer,
98                                                        int2 q_pixel,
99                                                        ccl_global float ccl_restrict_ptr q_buffer,
100                                                        int pass_stride,
101                                                        int rank,
102                                                        float *design_row,
103                                                        ccl_global float ccl_restrict_ptr transform,
104                                                        int stride)
105 {
106         design_row[0] = 1.0f;
107         math_vector_zero(design_row+1, rank);
108         design_row_add(design_row, rank, transform, stride, 0, q_pixel.x - p_pixel.x);
109         design_row_add(design_row, rank, transform, stride, 1, q_pixel.y - p_pixel.y);
110         design_row_add(design_row, rank, transform, stride, 2, fabsf(ccl_get_feature(q_buffer, 0)) - fabsf(ccl_get_feature(p_buffer, 0)));
111         design_row_add(design_row, rank, transform, stride, 3, ccl_get_feature(q_buffer, 1) - ccl_get_feature(p_buffer, 1));
112         design_row_add(design_row, rank, transform, stride, 4, ccl_get_feature(q_buffer, 2) - ccl_get_feature(p_buffer, 2));
113         design_row_add(design_row, rank, transform, stride, 5, ccl_get_feature(q_buffer, 3) - ccl_get_feature(p_buffer, 3));
114         design_row_add(design_row, rank, transform, stride, 6, ccl_get_feature(q_buffer, 4) - ccl_get_feature(p_buffer, 4));
115         design_row_add(design_row, rank, transform, stride, 7, ccl_get_feature(q_buffer, 5) - ccl_get_feature(p_buffer, 5));
116         design_row_add(design_row, rank, transform, stride, 8, ccl_get_feature(q_buffer, 6) - ccl_get_feature(p_buffer, 6));
117         design_row_add(design_row, rank, transform, stride, 9, ccl_get_feature(q_buffer, 7) - ccl_get_feature(p_buffer, 7));
118 }
119
120 CCL_NAMESPACE_END