2 * Copyright 2011-2017 Blender Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ccl_device void kernel_filter_construct_transform(float ccl_restrict_ptr buffer,
20 int x, int y, int4 rect,
22 float *transform, int *rank,
23 int radius, float pca_threshold)
25 int buffer_w = align_up(rect.z - rect.x, 4);
27 float features[DENOISE_FEATURES];
29 /* Temporary storage, used in different steps of the algorithm. */
30 float tempmatrix[DENOISE_FEATURES*DENOISE_FEATURES];
31 float tempvector[2*DENOISE_FEATURES];
32 float ccl_restrict_ptr pixel_buffer;
38 /* === Calculate denoising window. === */
39 int2 low = make_int2(max(rect.x, x - radius),
40 max(rect.y, y - radius));
41 int2 high = make_int2(min(rect.z, x + radius + 1),
42 min(rect.w, y + radius + 1));
47 /* === Shift feature passes to have mean 0. === */
48 float feature_means[DENOISE_FEATURES];
49 math_vector_zero(feature_means, DENOISE_FEATURES);
51 filter_get_features(pixel, pixel_buffer, features, NULL, pass_stride);
52 math_vector_add(feature_means, features, DENOISE_FEATURES);
53 } END_FOR_PIXEL_WINDOW
55 float pixel_scale = 1.0f / ((high.y - low.y) * (high.x - low.x));
56 math_vector_scale(feature_means, pixel_scale, DENOISE_FEATURES);
58 /* === Scale the shifted feature passes to a range of [-1; 1], will be baked into the transform later. === */
59 float *feature_scale = tempvector;
60 math_vector_zero(feature_scale, DENOISE_FEATURES);
63 filter_get_feature_scales(pixel, pixel_buffer, features, feature_means, pass_stride);
64 math_vector_max(feature_scale, features, DENOISE_FEATURES);
65 } END_FOR_PIXEL_WINDOW
67 filter_calculate_scale(feature_scale);
70 /* === Generate the feature transformation. ===
71 * This transformation maps the DENOISE_FEATURES-dimentional feature space to a reduced feature (r-feature) space
72 * which generally has fewer dimensions. This mainly helps to prevent overfitting. */
73 float* feature_matrix = tempmatrix;
74 math_matrix_zero(feature_matrix, DENOISE_FEATURES);
76 filter_get_features(pixel, pixel_buffer, features, feature_means, pass_stride);
77 math_vector_mul(features, feature_scale, DENOISE_FEATURES);
78 math_matrix_add_gramian(feature_matrix, DENOISE_FEATURES, features, 1.0f);
79 } END_FOR_PIXEL_WINDOW
81 math_matrix_jacobi_eigendecomposition(feature_matrix, transform, DENOISE_FEATURES, 1);
83 if(pca_threshold < 0.0f) {
84 float threshold_energy = 0.0f;
85 for(int i = 0; i < DENOISE_FEATURES; i++) {
86 threshold_energy += feature_matrix[i*DENOISE_FEATURES+i];
88 threshold_energy *= 1.0f - (-pca_threshold);
90 float reduced_energy = 0.0f;
91 for(int i = 0; i < DENOISE_FEATURES; i++, (*rank)++) {
92 if(i >= 2 && reduced_energy >= threshold_energy)
94 float s = feature_matrix[i*DENOISE_FEATURES+i];
99 for(int i = 0; i < DENOISE_FEATURES; i++, (*rank)++) {
100 float s = feature_matrix[i*DENOISE_FEATURES+i];
101 if(i >= 2 && sqrtf(s) < pca_threshold)
106 /* Bake the feature scaling into the transformation matrix. */
107 for(int i = 0; i < (*rank); i++) {
108 math_vector_mul(transform + i*DENOISE_FEATURES, feature_scale, DENOISE_FEATURES);
110 math_matrix_transpose(transform, DENOISE_FEATURES, 1);