Blender  V2.93
filter_transform.h
Go to the documentation of this file.
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 
18 
21  int x,
22  int y,
23  int4 rect,
24  int pass_stride,
25  int frame_stride,
26  bool use_time,
27  float *transform,
28  int *rank,
29  int radius,
30  float pca_threshold)
31 {
32  int buffer_w = align_up(rect.z - rect.x, 4);
33 
34  float features[DENOISE_FEATURES];
35 
36  const float *ccl_restrict pixel_buffer;
37  int3 pixel;
38 
39  int num_features = use_time ? 11 : 10;
40 
41  /* === Calculate denoising window. === */
42  int2 low = make_int2(max(rect.x, x - radius), max(rect.y, y - radius));
43  int2 high = make_int2(min(rect.z, x + radius + 1), min(rect.w, y + radius + 1));
44  int num_pixels = (high.y - low.y) * (high.x - low.x) * tile_info->num_frames;
45 
46  /* === Shift feature passes to have mean 0. === */
47  float feature_means[DENOISE_FEATURES];
48  math_vector_zero(feature_means, num_features);
50  {
51  filter_get_features(pixel, pixel_buffer, features, use_time, NULL, pass_stride);
52  math_vector_add(feature_means, features, num_features);
53  }
55 
56  math_vector_scale(feature_means, 1.0f / num_pixels, num_features);
57 
58  /* === Scale the shifted feature passes to a range of [-1; 1] ===
59  * Will be baked into the transform later. */
60  float feature_scale[DENOISE_FEATURES];
61  math_vector_zero(feature_scale, num_features);
62 
64  {
65  filter_get_feature_scales(pixel, pixel_buffer, features, use_time, feature_means, pass_stride);
66  math_vector_max(feature_scale, features, num_features);
67  }
69 
70  filter_calculate_scale(feature_scale, use_time);
71 
72  /* === Generate the feature transformation. ===
73  * This transformation maps the num_features-dimensional feature space to a reduced feature
74  * (r-feature) space which generally has fewer dimensions.
75  * This mainly helps to prevent over-fitting. */
76  float feature_matrix[DENOISE_FEATURES * DENOISE_FEATURES];
77  math_matrix_zero(feature_matrix, num_features);
79  {
80  filter_get_features(pixel, pixel_buffer, features, use_time, feature_means, pass_stride);
81  math_vector_mul(features, feature_scale, num_features);
82  math_matrix_add_gramian(feature_matrix, num_features, features, 1.0f);
83  }
85 
86  math_matrix_jacobi_eigendecomposition(feature_matrix, transform, num_features, 1);
87  *rank = 0;
88  /* Prevent over-fitting when a small window is used. */
89  int max_rank = min(num_features, num_pixels / 3);
90  if (pca_threshold < 0.0f) {
91  float threshold_energy = 0.0f;
92  for (int i = 0; i < num_features; i++) {
93  threshold_energy += feature_matrix[i * num_features + i];
94  }
95  threshold_energy *= 1.0f - (-pca_threshold);
96 
97  float reduced_energy = 0.0f;
98  for (int i = 0; i < max_rank; i++, (*rank)++) {
99  if (i >= 2 && reduced_energy >= threshold_energy)
100  break;
101  float s = feature_matrix[i * num_features + i];
102  reduced_energy += s;
103  }
104  }
105  else {
106  for (int i = 0; i < max_rank; i++, (*rank)++) {
107  float s = feature_matrix[i * num_features + i];
108  if (i >= 2 && sqrtf(s) < pca_threshold)
109  break;
110  }
111  }
112 
113  /* Bake the feature scaling into the transformation matrix. */
114  for (int i = 0; i < (*rank); i++) {
115  math_vector_mul(transform + i * num_features, feature_scale, num_features);
116  }
117  math_matrix_transpose(transform, num_features, 1);
118 }
119 
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
int x
Definition: btConvexHull.h:149
int w
Definition: btConvexHull.h:149
int y
Definition: btConvexHull.h:149
int z
Definition: btConvexHull.h:149
#define CCL_FILTER_TILE_INFO
#define DENOISE_FEATURES
ccl_device_inline void filter_get_features(int3 pixel, const ccl_global float *ccl_restrict buffer, float *features, bool use_time, const float *ccl_restrict mean, int pass_stride)
#define FOR_PIXEL_WINDOW
#define END_FOR_PIXEL_WINDOW
ccl_device_inline void filter_get_feature_scales(int3 pixel, const ccl_global float *ccl_restrict buffer, float *scales, bool use_time, const float *ccl_restrict mean, int pass_stride)
ccl_device_inline void filter_calculate_scale(float *scale, bool use_time)
CCL_NAMESPACE_BEGIN ccl_device void kernel_filter_construct_transform(const float *ccl_restrict buffer, CCL_FILTER_TILE_INFO, int x, int y, int4 rect, int pass_stride, int frame_stride, bool use_time, float *transform, int *rank, int radius, float pca_threshold)
#define ccl_restrict
#define ccl_device
#define CCL_NAMESPACE_END
#define make_int2(x, y)
#define sqrtf(x)
__kernel void ccl_constant KernelData ccl_global void ccl_global char ccl_global int ccl_global char ccl_global unsigned int ccl_global float * buffer
#define min(a, b)
Definition: sort.c:51
float max
__forceinline ssef low(const avxf &a)
Definition: util_avxf.h:277
__forceinline ssef high(const avxf &a)
Definition: util_avxf.h:281
ccl_device_inline void math_matrix_transpose(ccl_global float *A, int n, int stride)
ccl_device_inline void math_vector_add(float *a, const float *ccl_restrict b, int n)
ccl_device_inline void math_matrix_zero(float *A, int n)
ccl_device_inline void math_vector_zero(float *v, int n)
ccl_device void math_matrix_jacobi_eigendecomposition(float *A, ccl_global float *V, int n, int v_stride)
ccl_device_inline void math_vector_max(float *a, const float *ccl_restrict b, int n)
ccl_device_inline void math_vector_scale(float *a, float b, int n)
ccl_device_inline void math_matrix_add_gramian(float *A, int n, const float *ccl_restrict v, float weight)
ccl_device_inline void math_vector_mul(float *a, const float *ccl_restrict b, int n)
ccl_device_inline size_t align_up(size_t offset, size_t alignment)
Definition: util_types.h:65