2 * Copyright (C) 2016 The Android Open Source Project
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.
17 // Don't edit this file! It is auto-generated by frameworks/rs/api/generate.sh.
20 * rs_matrix.rsh: Matrix Functions
22 * These functions let you manipulate square matrices of rank 2x2, 3x3, and 4x4.
23 * They are particularly useful for graphical transformations and are compatible
26 * We use a zero-based index for rows and columns. E.g. the last element of a
27 * rs_matrix4x4 is found at (3, 3).
29 * RenderScript uses column-major matrices and column-based vectors. Transforming
30 * a vector is done by postmultiplying the vector, e.g. (matrix * vector),
31 * as provided by rsMatrixMultiply().
33 * To create a transformation matrix that performs two transformations at once,
34 * multiply the two source matrices, with the first transformation as the right
35 * argument. E.g. to create a transformation matrix that applies the
36 * transformation s1 followed by s2, call rsMatrixLoadMultiply(&combined, &s2, &s1).
37 * This derives from s2 * (s1 * v), which is (s2 * s1) * v.
39 * We have two style of functions to create transformation matrices:
40 * rsMatrixLoadTransformation and rsMatrixTransformation. The former
41 * style simply stores the transformation matrix in the first argument. The latter
42 * modifies a pre-existing transformation matrix so that the new transformation
43 * happens first. E.g. if you call rsMatrixTranslate() on a matrix that already
44 * does a scaling, the resulting matrix when applied to a vector will first do the
45 * translation then the scaling.
48 #ifndef RENDERSCRIPT_RS_MATRIX_RSH
49 #define RENDERSCRIPT_RS_MATRIX_RSH
51 #include "rs_vector_math.rsh"
54 * rsExtractFrustumPlanes: Compute frustum planes
56 * Computes 6 frustum planes from the view projection matrix
59 * viewProj: Matrix to extract planes from.
63 * bottom: Bottom plane.
67 #if !defined(RS_VERSION) || (RS_VERSION <= 23)
68 static inline void __attribute__((overloadable))
69 rsExtractFrustumPlanes(const rs_matrix4x4* viewProj, float4* left, float4* right, float4* top,
70 float4* bottom, float4* near, float4* far) {
71 // x y z w = a b c d in the plane equation
72 left->x = viewProj->m[3] + viewProj->m[0];
73 left->y = viewProj->m[7] + viewProj->m[4];
74 left->z = viewProj->m[11] + viewProj->m[8];
75 left->w = viewProj->m[15] + viewProj->m[12];
77 right->x = viewProj->m[3] - viewProj->m[0];
78 right->y = viewProj->m[7] - viewProj->m[4];
79 right->z = viewProj->m[11] - viewProj->m[8];
80 right->w = viewProj->m[15] - viewProj->m[12];
82 top->x = viewProj->m[3] - viewProj->m[1];
83 top->y = viewProj->m[7] - viewProj->m[5];
84 top->z = viewProj->m[11] - viewProj->m[9];
85 top->w = viewProj->m[15] - viewProj->m[13];
87 bottom->x = viewProj->m[3] + viewProj->m[1];
88 bottom->y = viewProj->m[7] + viewProj->m[5];
89 bottom->z = viewProj->m[11] + viewProj->m[9];
90 bottom->w = viewProj->m[15] + viewProj->m[13];
92 near->x = viewProj->m[3] + viewProj->m[2];
93 near->y = viewProj->m[7] + viewProj->m[6];
94 near->z = viewProj->m[11] + viewProj->m[10];
95 near->w = viewProj->m[15] + viewProj->m[14];
97 far->x = viewProj->m[3] - viewProj->m[2];
98 far->y = viewProj->m[7] - viewProj->m[6];
99 far->z = viewProj->m[11] - viewProj->m[10];
100 far->w = viewProj->m[15] - viewProj->m[14];
102 float len = length(left->xyz);
104 len = length(right->xyz);
106 len = length(top->xyz);
108 len = length(bottom->xyz);
110 len = length(near->xyz);
112 len = length(far->xyz);
117 #if (defined(RS_VERSION) && (RS_VERSION >= 24))
118 extern void __attribute__((overloadable))
119 rsExtractFrustumPlanes(const rs_matrix4x4* viewProj, float4* left, float4* righ, float4* top,
120 float4* bottom, float4* near, float4* far);
124 * rsIsSphereInFrustum: Checks if a sphere is within the frustum planes
126 * Returns true if the sphere is within the 6 frustum planes.
129 * sphere: float4 representing the sphere.
131 * right: Right plane.
133 * bottom: Bottom plane.
137 #if !defined(RS_VERSION) || (RS_VERSION <= 23)
138 static inline bool __attribute__((always_inline, overloadable))
139 rsIsSphereInFrustum(float4* sphere, float4* left, float4* right, float4* top, float4* bottom,
140 float4* near, float4* far) {
141 float distToCenter = dot(left->xyz, sphere->xyz) + left->w;
142 if (distToCenter < -sphere->w) {
145 distToCenter = dot(right->xyz, sphere->xyz) + right->w;
146 if (distToCenter < -sphere->w) {
149 distToCenter = dot(top->xyz, sphere->xyz) + top->w;
150 if (distToCenter < -sphere->w) {
153 distToCenter = dot(bottom->xyz, sphere->xyz) + bottom->w;
154 if (distToCenter < -sphere->w) {
157 distToCenter = dot(near->xyz, sphere->xyz) + near->w;
158 if (distToCenter < -sphere->w) {
161 distToCenter = dot(far->xyz, sphere->xyz) + far->w;
162 if (distToCenter < -sphere->w) {
169 #if (defined(RS_VERSION) && (RS_VERSION >= 24))
170 extern bool __attribute__((overloadable))
171 rsIsSphereInFrustum(float4* sphere, float4* left, float4* right, float4* top, float4* bottom,
172 float4* near, float4* far);
176 * rsMatrixGet: Get one element
178 * Returns one element of a matrix.
180 * Warning: The order of the column and row parameters may be unexpected.
183 * m: Matrix to extract the element from.
184 * col: Zero-based column of the element to be extracted.
185 * row: Zero-based row of the element to extracted.
187 extern float __attribute__((overloadable))
188 rsMatrixGet(const rs_matrix4x4* m, uint32_t col, uint32_t row);
190 extern float __attribute__((overloadable))
191 rsMatrixGet(const rs_matrix3x3* m, uint32_t col, uint32_t row);
193 extern float __attribute__((overloadable))
194 rsMatrixGet(const rs_matrix2x2* m, uint32_t col, uint32_t row);
197 * rsMatrixInverse: Inverts a matrix in place
199 * Returns true if the matrix was successfully inverted.
202 * m: Matrix to invert.
204 extern bool __attribute__((overloadable))
205 rsMatrixInverse(rs_matrix4x4* m);
208 * rsMatrixInverseTranspose: Inverts and transpose a matrix in place
210 * The matrix is first inverted then transposed. Returns true if the matrix was
211 * successfully inverted.
214 * m: Matrix to modify.
216 extern bool __attribute__((overloadable))
217 rsMatrixInverseTranspose(rs_matrix4x4* m);
220 * rsMatrixLoad: Load or copy a matrix
222 * Set the elements of a matrix from an array of floats or from another matrix.
224 * If loading from an array, the floats should be in row-major order, i.e. the element a
225 * row 0, column 0 should be first, followed by the element at
226 * row 0, column 1, etc.
228 * If loading from a matrix and the source is smaller than the destination, the rest
229 * of the destination is filled with elements of the identity matrix. E.g.
230 * loading a rs_matrix2x2 into a rs_matrix4x4 will give:
239 * destination: Matrix to set.
240 * array: Array of values to set the matrix to. These arrays should be 4, 9, or 16 floats long, depending on the matrix size.
241 * source: Source matrix.
243 extern void __attribute__((overloadable))
244 rsMatrixLoad(rs_matrix4x4* destination, const float* array);
246 extern void __attribute__((overloadable))
247 rsMatrixLoad(rs_matrix3x3* destination, const float* array);
249 extern void __attribute__((overloadable))
250 rsMatrixLoad(rs_matrix2x2* destination, const float* array);
252 extern void __attribute__((overloadable))
253 rsMatrixLoad(rs_matrix4x4* destination, const rs_matrix4x4* source);
255 extern void __attribute__((overloadable))
256 rsMatrixLoad(rs_matrix3x3* destination, const rs_matrix3x3* source);
258 extern void __attribute__((overloadable))
259 rsMatrixLoad(rs_matrix2x2* destination, const rs_matrix2x2* source);
261 extern void __attribute__((overloadable))
262 rsMatrixLoad(rs_matrix4x4* destination, const rs_matrix3x3* source);
264 extern void __attribute__((overloadable))
265 rsMatrixLoad(rs_matrix4x4* destination, const rs_matrix2x2* source);
268 * rsMatrixLoadFrustum: Load a frustum projection matrix
270 * Constructs a frustum projection matrix, transforming the box identified by
271 * the six clipping planes left, right, bottom, top, near, far.
273 * To apply this projection to a vector, multiply the vector by the created
274 * matrix using rsMatrixMultiply().
279 extern void __attribute__((overloadable))
280 rsMatrixLoadFrustum(rs_matrix4x4* m, float left, float right, float bottom, float top,
281 float near, float far);
284 * rsMatrixLoadIdentity: Load identity matrix
286 * Set the elements of a matrix to the identity matrix.
291 extern void __attribute__((overloadable))
292 rsMatrixLoadIdentity(rs_matrix4x4* m);
294 extern void __attribute__((overloadable))
295 rsMatrixLoadIdentity(rs_matrix3x3* m);
297 extern void __attribute__((overloadable))
298 rsMatrixLoadIdentity(rs_matrix2x2* m);
301 * rsMatrixLoadMultiply: Multiply two matrices
303 * Sets m to the matrix product of lhs * rhs.
305 * To combine two 4x4 transformaton matrices, multiply the second transformation matrix
306 * by the first transformation matrix. E.g. to create a transformation matrix that applies
307 * the transformation s1 followed by s2, call rsMatrixLoadMultiply(&combined, &s2, &s1).
309 * Warning: Prior to version 21, storing the result back into right matrix is not supported and
310 * will result in undefined behavior. Use rsMatrixMulitply instead. E.g. instead of doing
311 * rsMatrixLoadMultiply (&m2r, &m2r, &m2l), use rsMatrixMultiply (&m2r, &m2l).
312 * rsMatrixLoadMultiply (&m2l, &m2r, &m2l) works as expected.
316 * lhs: Left matrix of the product.
317 * rhs: Right matrix of the product.
319 extern void __attribute__((overloadable))
320 rsMatrixLoadMultiply(rs_matrix4x4* m, const rs_matrix4x4* lhs, const rs_matrix4x4* rhs);
322 extern void __attribute__((overloadable))
323 rsMatrixLoadMultiply(rs_matrix3x3* m, const rs_matrix3x3* lhs, const rs_matrix3x3* rhs);
325 extern void __attribute__((overloadable))
326 rsMatrixLoadMultiply(rs_matrix2x2* m, const rs_matrix2x2* lhs, const rs_matrix2x2* rhs);
329 * rsMatrixLoadOrtho: Load an orthographic projection matrix
331 * Constructs an orthographic projection matrix, transforming the box identified by the
332 * six clipping planes left, right, bottom, top, near, far into a unit cube
333 * with a corner at (-1, -1, -1) and the opposite at (1, 1, 1).
335 * To apply this projection to a vector, multiply the vector by the created matrix
336 * using rsMatrixMultiply().
338 * See https://en.wikipedia.org/wiki/Orthographic_projection .
343 extern void __attribute__((overloadable))
344 rsMatrixLoadOrtho(rs_matrix4x4* m, float left, float right, float bottom, float top, float near,
348 * rsMatrixLoadPerspective: Load a perspective projection matrix
350 * Constructs a perspective projection matrix, assuming a symmetrical field of view.
352 * To apply this projection to a vector, multiply the vector by the created matrix
353 * using rsMatrixMultiply().
357 * fovy: Field of view, in degrees along the Y axis.
358 * aspect: Ratio of x / y.
359 * near: Near clipping plane.
360 * far: Far clipping plane.
362 extern void __attribute__((overloadable))
363 rsMatrixLoadPerspective(rs_matrix4x4* m, float fovy, float aspect, float near, float far);
366 * rsMatrixLoadRotate: Load a rotation matrix
368 * This function creates a rotation matrix. The axis of rotation is the (x, y, z) vector.
370 * To rotate a vector, multiply the vector by the created matrix using rsMatrixMultiply().
372 * See http://en.wikipedia.org/wiki/Rotation_matrix .
376 * rot: How much rotation to do, in degrees.
377 * x: X component of the vector that is the axis of rotation.
378 * y: Y component of the vector that is the axis of rotation.
379 * z: Z component of the vector that is the axis of rotation.
381 extern void __attribute__((overloadable))
382 rsMatrixLoadRotate(rs_matrix4x4* m, float rot, float x, float y, float z);
385 * rsMatrixLoadScale: Load a scaling matrix
387 * This function creates a scaling matrix, where each component of a vector is multiplied
388 * by a number. This number can be negative.
390 * To scale a vector, multiply the vector by the created matrix using rsMatrixMultiply().
394 * x: Multiple to scale the x components by.
395 * y: Multiple to scale the y components by.
396 * z: Multiple to scale the z components by.
398 extern void __attribute__((overloadable))
399 rsMatrixLoadScale(rs_matrix4x4* m, float x, float y, float z);
402 * rsMatrixLoadTranslate: Load a translation matrix
404 * This function creates a translation matrix, where a number is added to each element of
407 * To translate a vector, multiply the vector by the created matrix using
408 * rsMatrixMultiply().
412 * x: Number to add to each x component.
413 * y: Number to add to each y component.
414 * z: Number to add to each z component.
416 extern void __attribute__((overloadable))
417 rsMatrixLoadTranslate(rs_matrix4x4* m, float x, float y, float z);
420 * rsMatrixMultiply: Multiply a matrix by a vector or another matrix
422 * For the matrix by matrix variant, sets m to the matrix product m * rhs.
424 * When combining two 4x4 transformation matrices using this function, the resulting
425 * matrix will correspond to performing the rhs transformation first followed by
426 * the original m transformation.
428 * For the matrix by vector variant, returns the post-multiplication of the vector
429 * by the matrix, ie. m * in.
431 * When multiplying a float3 to a rs_matrix4x4, the vector is expanded with (1).
433 * When multiplying a float2 to a rs_matrix4x4, the vector is expanded with (0, 1).
435 * When multiplying a float2 to a rs_matrix3x3, the vector is expanded with (0).
437 * Starting with API 14, this function takes a const matrix as the first argument.
440 * m: Left matrix of the product and the matrix to be set.
441 * rhs: Right matrix of the product.
443 extern void __attribute__((overloadable))
444 rsMatrixMultiply(rs_matrix4x4* m, const rs_matrix4x4* rhs);
446 extern void __attribute__((overloadable))
447 rsMatrixMultiply(rs_matrix3x3* m, const rs_matrix3x3* rhs);
449 extern void __attribute__((overloadable))
450 rsMatrixMultiply(rs_matrix2x2* m, const rs_matrix2x2* rhs);
452 #if !defined(RS_VERSION) || (RS_VERSION <= 13)
453 extern float4 __attribute__((overloadable))
454 rsMatrixMultiply(rs_matrix4x4* m, float4 in);
457 #if !defined(RS_VERSION) || (RS_VERSION <= 13)
458 extern float4 __attribute__((overloadable))
459 rsMatrixMultiply(rs_matrix4x4* m, float3 in);
462 #if !defined(RS_VERSION) || (RS_VERSION <= 13)
463 extern float4 __attribute__((overloadable))
464 rsMatrixMultiply(rs_matrix4x4* m, float2 in);
467 #if !defined(RS_VERSION) || (RS_VERSION <= 13)
468 extern float3 __attribute__((overloadable))
469 rsMatrixMultiply(rs_matrix3x3* m, float3 in);
472 #if !defined(RS_VERSION) || (RS_VERSION <= 13)
473 extern float3 __attribute__((overloadable))
474 rsMatrixMultiply(rs_matrix3x3* m, float2 in);
477 #if !defined(RS_VERSION) || (RS_VERSION <= 13)
478 extern float2 __attribute__((overloadable))
479 rsMatrixMultiply(rs_matrix2x2* m, float2 in);
482 #if (defined(RS_VERSION) && (RS_VERSION >= 14))
483 extern float4 __attribute__((overloadable))
484 rsMatrixMultiply(const rs_matrix4x4* m, float4 in);
487 #if (defined(RS_VERSION) && (RS_VERSION >= 14))
488 extern float4 __attribute__((overloadable))
489 rsMatrixMultiply(const rs_matrix4x4* m, float3 in);
492 #if (defined(RS_VERSION) && (RS_VERSION >= 14))
493 extern float4 __attribute__((overloadable))
494 rsMatrixMultiply(const rs_matrix4x4* m, float2 in);
497 #if (defined(RS_VERSION) && (RS_VERSION >= 14))
498 extern float3 __attribute__((overloadable))
499 rsMatrixMultiply(const rs_matrix3x3* m, float3 in);
502 #if (defined(RS_VERSION) && (RS_VERSION >= 14))
503 extern float3 __attribute__((overloadable))
504 rsMatrixMultiply(const rs_matrix3x3* m, float2 in);
507 #if (defined(RS_VERSION) && (RS_VERSION >= 14))
508 extern float2 __attribute__((overloadable))
509 rsMatrixMultiply(const rs_matrix2x2* m, float2 in);
513 * rsMatrixRotate: Apply a rotation to a transformation matrix
515 * Multiply the matrix m with a rotation matrix.
517 * This function modifies a transformation matrix to first do a rotation. The axis of
518 * rotation is the (x, y, z) vector.
520 * To apply this combined transformation to a vector, multiply the vector by the created
521 * matrix using rsMatrixMultiply().
524 * m: Matrix to modify.
525 * rot: How much rotation to do, in degrees.
526 * x: X component of the vector that is the axis of rotation.
527 * y: Y component of the vector that is the axis of rotation.
528 * z: Z component of the vector that is the axis of rotation.
530 extern void __attribute__((overloadable))
531 rsMatrixRotate(rs_matrix4x4* m, float rot, float x, float y, float z);
534 * rsMatrixScale: Apply a scaling to a transformation matrix
536 * Multiply the matrix m with a scaling matrix.
538 * This function modifies a transformation matrix to first do a scaling. When scaling,
539 * each component of a vector is multiplied by a number. This number can be negative.
541 * To apply this combined transformation to a vector, multiply the vector by the created
542 * matrix using rsMatrixMultiply().
545 * m: Matrix to modify.
546 * x: Multiple to scale the x components by.
547 * y: Multiple to scale the y components by.
548 * z: Multiple to scale the z components by.
550 extern void __attribute__((overloadable))
551 rsMatrixScale(rs_matrix4x4* m, float x, float y, float z);
554 * rsMatrixSet: Set one element
556 * Set an element of a matrix.
558 * Warning: The order of the column and row parameters may be unexpected.
561 * m: Matrix that will be modified.
562 * col: Zero-based column of the element to be set.
563 * row: Zero-based row of the element to be set.
566 extern void __attribute__((overloadable))
567 rsMatrixSet(rs_matrix4x4* m, uint32_t col, uint32_t row, float v);
569 extern void __attribute__((overloadable))
570 rsMatrixSet(rs_matrix3x3* m, uint32_t col, uint32_t row, float v);
572 extern void __attribute__((overloadable))
573 rsMatrixSet(rs_matrix2x2* m, uint32_t col, uint32_t row, float v);
576 * rsMatrixTranslate: Apply a translation to a transformation matrix
578 * Multiply the matrix m with a translation matrix.
580 * This function modifies a transformation matrix to first do a translation. When
581 * translating, a number is added to each component of a vector.
583 * To apply this combined transformation to a vector, multiply the vector by the
584 * created matrix using rsMatrixMultiply().
587 * m: Matrix to modify.
588 * x: Number to add to each x component.
589 * y: Number to add to each y component.
590 * z: Number to add to each z component.
592 extern void __attribute__((overloadable))
593 rsMatrixTranslate(rs_matrix4x4* m, float x, float y, float z);
596 * rsMatrixTranspose: Transpose a matrix place
598 * Transpose the matrix m in place.
601 * m: Matrix to transpose.
603 extern void __attribute__((overloadable))
604 rsMatrixTranspose(rs_matrix4x4* m);
606 extern void __attribute__((overloadable))
607 rsMatrixTranspose(rs_matrix3x3* m);
609 extern void __attribute__((overloadable))
610 rsMatrixTranspose(rs_matrix2x2* m);
612 #endif // RENDERSCRIPT_RS_MATRIX_RSH