Blender V5.3
gpu_shader_material_matrix_svd.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
9#include "gpu_shader_utildefines.bsl.hh" /* IWYU pragma: export constants */
10
11/* Apply left Jacobi rotation on 3x3 matrix : matrix = rotation * matrix.
12 * Rotates rows p and q. */
13float3x3 jacobi_rotate_left_3x3(float3x3 matrix, int p, int q, float2x2 rotation)
14{
15 float3x3 result = matrix;
16 const float3 row_p = float3(matrix[0][p], matrix[1][p], matrix[2][p]);
17 const float3 row_q = float3(matrix[0][q], matrix[1][q], matrix[2][q]);
18
19 const float3 new_row_p = rotation[0][0] * row_p + rotation[1][0] * row_q;
20 const float3 new_row_q = rotation[0][1] * row_p + rotation[1][1] * row_q;
21
22 result[0][p] = new_row_p[0];
23 result[1][p] = new_row_p[1];
24 result[2][p] = new_row_p[2];
25 result[0][q] = new_row_q[0];
26 result[1][q] = new_row_q[1];
27 result[2][q] = new_row_q[2];
28 return result;
29}
30
31/* Apply right Jacobi rotation on 3x3 matrix : matrix = matrix * rotation.
32 * Rotates columns p and q. */
33float3x3 jacobi_rotate_right_3x3(float3x3 matrix, int p, int q, float2x2 rotation)
34{
35 float3x3 result = matrix;
36 const float3 col_p = matrix[p];
37 const float3 col_q = matrix[q];
38
39 result[p] = rotation[0][0] * col_p + rotation[0][1] * col_q;
40 result[q] = rotation[1][0] * col_p + rotation[1][1] * col_q;
41 return result;
42}
43
44/* Compute Jacobi rotation for symmetric 2x2 matrix [[x, y], [y, z]]. */
46{
47 /* Check if off-diagonal is negligible; no rotation needed. */
48 const float off_diagonal = 2.0f * abs(y);
49 if (off_diagonal < FLT_MIN) {
50 return float2x2(1.0f);
51 }
52
53 const float tau = (x - z) / off_diagonal;
54 const float w = sqrt(tau * tau + 1.0f);
55 const float t = 1.0f / (tau > 0.0f ? (tau + w) : (tau - w));
56
57 const float sign_t = t > 0.0f ? 1.0f : -1.0f;
58 const float c = 1.0f / sqrt(t * t + 1.0f);
59 const float s = -sign_t * sign(y) * abs(t) * c;
60 return float2x2(c, -s, s, c);
61}
62
63/* Compute left and right Jacobi rotations for the 2x2 block at indices (p, q)
64 * (i.e. block obtained from intersecting lines p & q with columns p & q). */
65void jacobi_svd_2x2(float3x3 matrix, int p, int q, float2x2 &j_left, float2x2 &j_right)
66{
67 float2x2 block;
68 block[0] = float2(matrix[p][p], matrix[p][q]);
69 block[1] = float2(matrix[q][p], matrix[q][q]);
70
71 const float t = block[0][0] + block[1][1];
72 const float d = block[0][1] - block[1][0];
73 float2x2 rotation;
74 if (abs(d) < FLT_MIN) {
75 rotation = float2x2(1.0f);
76 }
77 else {
78 const float u = t / d;
79 const float tmp = sqrt(1.0f + u * u);
80 rotation = float2x2(u / tmp, -(1.0f / tmp), 1.0f / tmp, u / tmp);
81 }
82
83 block = rotation * block;
84 j_right = construct_jacobi_rotation(block[0][0], block[0][1], block[1][1]);
85 j_left = rotation * transpose(j_right);
86}
87
89{
90 constexpr float convergence_epsilon = 2.0f * FLT_EPSILON;
91 const float zero_threshold = FLT_MIN;
92 /* Cap on Jacobi sweeps: most inputs converge in ~11 sweeps, but 80 is the
93 * smallest cap at which abs max error reaches ~6.6e-6. */
94 constexpr int max_sweeps = 80;
95
96 /* Normalize matrix for numerical stability. */
97 float scale = reduce_max(
98 float3(reduce_max(abs(A[0])), reduce_max(abs(A[1])), reduce_max(abs(A[2]))));
99 if (scale == 0.0f) {
100 scale = 1.0f;
101 }
102
103 A = A * (1.0f / scale);
104 U = mat3x3_identity();
105 V = mat3x3_identity();
106 float max_diag_entry = max(max(abs(A[0][0]), abs(A[1][1])), abs(A[2][2]));
107
108 /* The main Jacobi SVD iteration. Sweep until converged or max_sweeps is reached. */
109 for (int i = 0; i < max_sweeps; i++) {
110 bool finished = true;
111
112 for (int p = 1; p < 3; p++) {
113 for (int q = 0; q < p; q++) {
114 const float threshold = max(zero_threshold, convergence_epsilon * max_diag_entry);
115
116 /* Skip pairs already converged. */
117 if (abs(A[q][p]) > threshold || abs(A[p][q]) > threshold) {
118 finished = false;
119
120 /* SVD of the 2x2 block at indices (p, q). */
121 float2x2 j_left;
122 float2x2 j_right;
123 jacobi_svd_2x2(A, p, q, j_left, j_right);
124
125 /* Accumulate rotations. */
126 A = jacobi_rotate_left_3x3(A, p, q, j_left);
127 U = jacobi_rotate_right_3x3(U, p, q, transpose(j_left));
128
129 A = jacobi_rotate_right_3x3(A, p, q, j_right);
130 V = jacobi_rotate_right_3x3(V, p, q, j_right);
131
132 max_diag_entry = max(max(abs(A[0][0]), abs(A[1][1])), abs(A[2][2]));
133 }
134 }
135 }
136
137 if (finished) {
138 break;
139 }
140 }
141
142 /* A is now diagonal. Extract singular values, force S >= 0, update sign into U. */
143 S = float3(0.0f);
144 for (int i = 0; i < 3; i++) {
145 const float a = A[i][i];
146 S[i] = abs(a);
147 if (a < 0.0f) {
148 U[i] = -U[i];
149 }
150 }
151
152 /* Revert to original scale. */
153 S = S * scale;
154
155 /* Sort singular values descending & update U,V columns accordingly. */
156 for (int i = 0; i < 3; i++) {
157 float max_val = 0.0f;
158 int pos = i;
159 for (int j = i; j < 3; j++) {
160 const float val = S[j];
161 if (val > max_val) {
162 max_val = val;
163 pos = j;
164 }
165 }
166 if (max_val == 0.0f) {
167 break;
168 }
169 if (pos != i) {
170 const float tmp_s = S[i];
171 S[i] = S[pos];
172 S[pos] = tmp_s;
173
174 const float3 tmp_u = U[i];
175 U[i] = U[pos];
176 U[pos] = tmp_u;
177
178 const float3 tmp_v = V[i];
179 V[i] = V[pos];
180 V[pos] = tmp_v;
181 }
182 }
183}
184
185[[node]]
187{
188 const float3x3 A = to_float3x3(matrix);
189 float3x3 U3;
190 float3x3 V3;
191 jacobi_svd_3x3(A, U3, S, V3);
192 U = to_float4x4(U3);
193 V = to_float4x4(V3);
194}
#define U
#define A
btScalar max
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define FLT_MIN
#define FLT_EPSILON
constexpr T sign(T) RET
#define abs
#define sqrt
MatBase< R, C > transpose(MatBase< C, R >) RET
float3x3 jacobi_rotate_left_3x3(float3x3 matrix, int p, int q, float2x2 rotation)
void jacobi_svd_3x3(float3x3 A, float3x3 &U, float3 &S, float3x3 &V)
float3x3 jacobi_rotate_right_3x3(float3x3 matrix, int p, int q, float2x2 rotation)
float2x2 construct_jacobi_rotation(float x, float y, float z)
void matrix_svd(float4x4 matrix, float4x4 &U, float3 &S, float4x4 &V)
void jacobi_svd_2x2(float3x3 matrix, int p, int q, float2x2 &j_left, float2x2 &j_right)
float3x3 scale(float3x3 mat, float2 scale)
float reduce_max(float2 a)
MatBase< float, 2, 2 > float2x2
MatBase< float, 4, 4 > float4x4
CCL_NAMESPACE_BEGIN struct Window V