Blender V5.3
gpu_shader_math_matrix_construct.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
12
13/* -------------------------------------------------------------------- */
16
18{
19 return float2x2(float2(v, 0.0f), float2(0.0f, v));
20}
22{
23 return float3x3(float3(v, 0.0f, 0.0f), float3(0.0f, v, 0.0f), float3(0.0f, 0.0f, v));
24}
26{
27 return float4x4(float4(v, 0.0f, 0.0f, 0.0f),
28 float4(0.0f, v, 0.0f, 0.0f),
29 float4(0.0f, 0.0f, v, 0.0f),
30 float4(0.0f, 0.0f, 0.0f, v));
31}
32
34{
35 return float2x2(float2(v), float2(v));
36}
38{
39 return float3x3(float3(v), float3(v), float3(v));
40}
42{
43 return float4x4(float4(v), float4(v), float4(v), float4(v));
44}
45
47{
48 return mat2x2_all(0.0f);
49}
51{
52 return mat3x3_all(0.0f);
53}
55{
56 return mat4x4_all(0.0f);
57}
58
60{
61 return mat2x2_diagonal(1.0f);
62}
64{
65 return mat3x3_diagonal(1.0f);
66}
68{
69 return mat4x4_diagonal(1.0f);
70}
71
73
74/* -------------------------------------------------------------------- */
77
82{
83 float4x4 ret = float4x4(1.0f);
84 ret[3].xyz = location;
85 return ret;
86}
87
92{
93 float2x2 ret = float2x2(0.0f);
94 ret[0][0] = scale[0];
95 ret[1][1] = scale[1];
96 return ret;
97}
98
102{
103 float3x3 ret = float3x3(0.0f);
104 ret[0][0] = scale[0];
105 ret[1][1] = scale[1];
106 ret[2][2] = scale[2];
107 return ret;
108}
109
113{
114 float4x4 ret = float4x4(0.0f);
115 ret[0][0] = scale[0];
116 ret[1][1] = scale[1];
117 ret[2][2] = scale[2];
118 ret[3][3] = scale[3];
119 return ret;
120}
121
126{
127 float c = cos(rotation.angle);
128 float s = sin(rotation.angle);
129 return float2x2(c, -s, s, c);
130}
131
135{
136 float ci = cos(rotation.x);
137 float cj = cos(rotation.y);
138 float ch = cos(rotation.z);
139 float si = sin(rotation.x);
140 float sj = sin(rotation.y);
141 float sh = sin(rotation.z);
142 float cc = ci * ch;
143 float cs = ci * sh;
144 float sc = si * ch;
145 float ss = si * sh;
146
147 float3x3 mat;
148 mat[0][0] = cj * ch;
149 mat[1][0] = sj * sc - cs;
150 mat[2][0] = sj * cc + ss;
151
152 mat[0][1] = cj * sh;
153 mat[1][1] = sj * ss + cc;
154 mat[2][1] = sj * cs - sc;
155
156 mat[0][2] = -sj;
157 mat[1][2] = cj * si;
158 mat[2][2] = cj * ci;
159 return mat;
160}
161
165{
166 /* NOTE: Should be double but support isn't native on most GPUs. */
167 float q0 = M_SQRT2 * float(rotation.x);
168 float q1 = M_SQRT2 * float(rotation.y);
169 float q2 = M_SQRT2 * float(rotation.z);
170 float q3 = M_SQRT2 * float(rotation.w);
171
172 float qda = q0 * q1;
173 float qdb = q0 * q2;
174 float qdc = q0 * q3;
175 float qaa = q1 * q1;
176 float qab = q1 * q2;
177 float qac = q1 * q3;
178 float qbb = q2 * q2;
179 float qbc = q2 * q3;
180 float qcc = q3 * q3;
181
182 float3x3 mat;
183 mat[0][0] = float(1.0f - qbb - qcc);
184 mat[0][1] = float(qdc + qab);
185 mat[0][2] = float(-qdb + qac);
186
187 mat[1][0] = float(-qdc + qab);
188 mat[1][1] = float(1.0f - qaa - qcc);
189 mat[1][2] = float(qda + qbc);
190
191 mat[2][0] = float(qdb + qac);
192 mat[2][1] = float(-qda + qbc);
193 mat[2][2] = float(1.0f - qaa - qbb);
194 return mat;
195}
196
200{
201 float angle_sin = sin(rotation.angle);
202 float angle_cos = cos(rotation.angle);
203 float3 axis = rotation.axis;
204
205 float ico = (float(1) - angle_cos);
206 float3 nsi = axis * angle_sin;
207
208 float3 n012 = (axis * axis) * ico;
209 float n_01 = (axis[0] * axis[1]) * ico;
210 float n_02 = (axis[0] * axis[2]) * ico;
211 float n_12 = (axis[1] * axis[2]) * ico;
212
213 float3x3 mat = from_scale(n012 + angle_cos);
214 mat[0][1] = n_01 + nsi[2];
215 mat[0][2] = n_02 - nsi[1];
216 mat[1][0] = n_01 - nsi[2];
217 mat[1][2] = n_12 + nsi[0];
218 mat[2][0] = n_02 + nsi[1];
219 mat[2][1] = n_12 - nsi[0];
220 return mat;
221}
222
227{
228 return from_rotation(rotation) * from_scale(scale);
229}
230
234{
235 return from_rotation(rotation) * from_scale(scale);
236}
237
241{
242 return from_rotation(rotation) * from_scale(scale);
243}
244
249{
250 float4x4 ret = to_float4x4(from_rotation(rotation));
251 ret[3].xyz = location;
252 return ret;
253}
254
258{
259 float4x4 ret = to_float4x4(from_rotation(rotation));
260 ret[3].xyz = location;
261 return ret;
262}
263
267{
268 float4x4 ret = to_float4x4(from_rotation(rotation));
269 ret[3].xyz = location;
270 return ret;
271}
272
277{
278 float4x4 ret = to_float4x4(from_rot_scale(rotation, scale));
279 ret[3].xyz = location;
280 return ret;
281}
282
286{
287 float4x4 ret = to_float4x4(from_rot_scale(rotation, scale));
288 ret[3].xyz = location;
289 return ret;
290}
291
295{
296 float4x4 ret = to_float4x4(from_rot_scale(rotation, scale));
297 ret[3].xyz = location;
298 return ret;
299}
300
306{
307 float cos_angle = direction.x;
308 float sin_angle = direction.y;
309 return float2x2(cos_angle, sin_angle, -sin_angle, cos_angle);
310}
311
317// mat3x3 from_normalized_axis_data(vec3 forward, vec3 up); /* TODO. */
318
323// mat4x4 from_normalized_axis_data(vec3 location, vec3 forward, vec3 up); /* TODO. */
324
334{
335 /* Duff, Tom, et al. "Building an orthonormal basis, revisited." JCGT 6.1 (2017). */
336 float z_sign = up.z >= 0.0f ? 1.0f : -1.0f;
337 float a = -1.0f / (z_sign + up.z);
338 float b = up.x * up.y * a;
339
340 float3x3 basis;
341 basis[0] = float3(1.0f + z_sign * square(up.x) * a, z_sign * b, -z_sign * up.x);
342 basis[1] = float3(b, z_sign + square(up.y) * a, -up.y);
343 basis[2] = up;
344 return basis;
345}
346
354{
355 float cos_theta = dot(up, forward);
356 if (cos_theta > 0.999999f) {
357 /* Mostly for orthographic view and surfel light eval. */
358 return from_up_axis(up);
359 }
360
361 /* Construct orthonormal basis around N. */
362 float3x3 basis;
363 basis[0] = normalize(forward - up * cos_theta);
364 basis[1] = cross(up, basis[0]);
365 basis[2] = up;
366 return basis;
367}
368
#define M_SQRT2
ccl_device_inline float cos_theta(const float3 w)
#define sin
#define cos
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
float square(float v)
float3x3 scale(float3x3 mat, float2 scale)
float4x4 mat4x4_diagonal(float v)
float4x4 from_location(float3 location)
float3x3 from_up_axis(float3 up)
float4x4 mat4x4_all(float v)
float2x2 mat2x2_all(float v)
float3x3 from_rot_scale(EulerXYZ rotation, float3 scale)
float2x2 from_direction(float2 direction)
float2x2 from_scale(float2 scale)
float4x4 from_loc_rot(float3 location, EulerXYZ rotation)
float2x2 mat2x2_diagonal(float v)
float2x2 from_rotation(AngleRadian rotation)
float3x3 from_incident_vector(float3 up, float3 forward)
float3x3 mat3x3_all(float v)
float4x4 from_loc_rot_scale(float3 location, EulerXYZ rotation, float3 scale)
float3x3 mat3x3_diagonal(float v)
MatBase< float, 2, 2 > float2x2
MatBase< float, 4, 4 > float4x4
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136