1/*
2 * Copyright (C) 2011 The Android Open Source Project
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
17/** @file rs_matrix.rsh
18 *  \brief Matrix routines
19 *
20 *
21 */
22
23#ifndef __RS_MATRIX_RSH__
24#define __RS_MATRIX_RSH__
25
26/**
27 * Set one element of a matrix.
28 *
29 * @param m The matrix to be set
30 * @param row
31 * @param col
32 * @param v
33 *
34 * @return void
35 */
36_RS_RUNTIME void __attribute__((overloadable))
37rsMatrixSet(rs_matrix4x4 *m, uint32_t row, uint32_t col, float v);
38/**
39 * \overload
40 */
41_RS_RUNTIME void __attribute__((overloadable))
42rsMatrixSet(rs_matrix3x3 *m, uint32_t row, uint32_t col, float v);
43/**
44 * \overload
45 */
46_RS_RUNTIME void __attribute__((overloadable))
47rsMatrixSet(rs_matrix2x2 *m, uint32_t row, uint32_t col, float v);
48
49/**
50 * Get one element of a matrix.
51 *
52 * @param m The matrix to read from
53 * @param row
54 * @param col
55 *
56 * @return float
57 */
58_RS_RUNTIME float __attribute__((overloadable))
59rsMatrixGet(const rs_matrix4x4 *m, uint32_t row, uint32_t col);
60/**
61 * \overload
62 */
63_RS_RUNTIME float __attribute__((overloadable))
64rsMatrixGet(const rs_matrix3x3 *m, uint32_t row, uint32_t col);
65/**
66 * \overload
67 */
68_RS_RUNTIME float __attribute__((overloadable))
69rsMatrixGet(const rs_matrix2x2 *m, uint32_t row, uint32_t col);
70
71/**
72 * Set the elements of a matrix to the identity matrix.
73 *
74 * @param m
75 */
76extern void __attribute__((overloadable)) rsMatrixLoadIdentity(rs_matrix4x4 *m);
77/**
78 * \overload
79 */
80extern void __attribute__((overloadable)) rsMatrixLoadIdentity(rs_matrix3x3 *m);
81/**
82 * \overload
83 */
84extern void __attribute__((overloadable)) rsMatrixLoadIdentity(rs_matrix2x2 *m);
85
86/**
87 * Set the elements of a matrix from an array of floats.
88 *
89 * @param m
90 */
91extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix4x4 *m, const float *v);
92/**
93 * \overload
94 */
95extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix3x3 *m, const float *v);
96/**
97 * \overload
98 */
99extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix2x2 *m, const float *v);
100/**
101 * \overload
102 */
103extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix4x4 *v);
104/**
105 * \overload
106 */
107extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix3x3 *v);
108
109/**
110 * Set the elements of a matrix from another matrix.
111 *
112 * @param m
113 */
114extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix4x4 *m, const rs_matrix2x2 *v);
115/**
116 * \overload
117 */
118extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix3x3 *m, const rs_matrix3x3 *v);
119/**
120 * \overload
121 */
122extern void __attribute__((overloadable)) rsMatrixLoad(rs_matrix2x2 *m, const rs_matrix2x2 *v);
123
124/**
125 * Load a rotation matrix.
126 *
127 * @param m
128 * @param rot
129 * @param x
130 * @param y
131 * @param z
132 */
133extern void __attribute__((overloadable))
134rsMatrixLoadRotate(rs_matrix4x4 *m, float rot, float x, float y, float z);
135
136/**
137 * Load a scale matrix.
138 *
139 * @param m
140 * @param x
141 * @param y
142 * @param z
143 */
144extern void __attribute__((overloadable))
145rsMatrixLoadScale(rs_matrix4x4 *m, float x, float y, float z);
146
147/**
148 * Load a translation matrix.
149 *
150 * @param m
151 * @param x
152 * @param y
153 * @param z
154 */
155extern void __attribute__((overloadable))
156rsMatrixLoadTranslate(rs_matrix4x4 *m, float x, float y, float z);
157
158/**
159 * Multiply two matrix (lhs, rhs) and place the result in m.
160 *
161 * @param m
162 * @param lhs
163 * @param rhs
164 */
165extern void __attribute__((overloadable))
166rsMatrixLoadMultiply(rs_matrix4x4 *m, const rs_matrix4x4 *lhs, const rs_matrix4x4 *rhs);
167/**
168 * \overload
169 */
170extern void __attribute__((overloadable))
171rsMatrixLoadMultiply(rs_matrix3x3 *m, const rs_matrix3x3 *lhs, const rs_matrix3x3 *rhs);
172/**
173 * \overload
174 */
175extern void __attribute__((overloadable))
176rsMatrixLoadMultiply(rs_matrix2x2 *m, const rs_matrix2x2 *lhs, const rs_matrix2x2 *rhs);
177
178/**
179 * Multiply the matrix m by rhs and place the result back into m.
180 *
181 * @param m (lhs)
182 * @param rhs
183 */
184extern void __attribute__((overloadable))
185rsMatrixMultiply(rs_matrix4x4 *m, const rs_matrix4x4 *rhs);
186/**
187 * \overload
188 */
189extern void __attribute__((overloadable))
190rsMatrixMultiply(rs_matrix3x3 *m, const rs_matrix3x3 *rhs);
191/**
192 * \overload
193 */
194extern void __attribute__((overloadable))
195rsMatrixMultiply(rs_matrix2x2 *m, const rs_matrix2x2 *rhs);
196
197/**
198 * Multiple matrix m with a rotation matrix
199 *
200 * @param m
201 * @param rot
202 * @param x
203 * @param y
204 * @param z
205 */
206extern void __attribute__((overloadable))
207rsMatrixRotate(rs_matrix4x4 *m, float rot, float x, float y, float z);
208
209/**
210 * Multiple matrix m with a scale matrix
211 *
212 * @param m
213 * @param x
214 * @param y
215 * @param z
216 */
217extern void __attribute__((overloadable))
218rsMatrixScale(rs_matrix4x4 *m, float x, float y, float z);
219
220/**
221 * Multiple matrix m with a translation matrix
222 *
223 * @param m
224 * @param x
225 * @param y
226 * @param z
227 */
228extern void __attribute__((overloadable))
229rsMatrixTranslate(rs_matrix4x4 *m, float x, float y, float z);
230
231/**
232 * Load an Ortho projection matrix constructed from the 6 planes
233 *
234 * @param m
235 * @param left
236 * @param right
237 * @param bottom
238 * @param top
239 * @param near
240 * @param far
241 */
242extern void __attribute__((overloadable))
243rsMatrixLoadOrtho(rs_matrix4x4 *m, float left, float right, float bottom, float top, float near, float far);
244
245/**
246 * Load an Frustum projection matrix constructed from the 6 planes
247 *
248 * @param m
249 * @param left
250 * @param right
251 * @param bottom
252 * @param top
253 * @param near
254 * @param far
255 */
256extern void __attribute__((overloadable))
257rsMatrixLoadFrustum(rs_matrix4x4 *m, float left, float right, float bottom, float top, float near, float far);
258
259/**
260 * Load an perspective projection matrix constructed from the 6 planes
261 *
262 * @param m
263 * @param fovy Field of view, in degrees along the Y axis.
264 * @param aspect Ratio of x / y.
265 * @param near
266 * @param far
267 */
268extern void __attribute__((overloadable))
269rsMatrixLoadPerspective(rs_matrix4x4* m, float fovy, float aspect, float near, float far);
270
271#if !defined(RS_VERSION) || (RS_VERSION < 14)
272/**
273 * Multiply a vector by a matrix and return the result vector.
274 * API version 10-13
275 */
276_RS_RUNTIME float4 __attribute__((overloadable))
277rsMatrixMultiply(rs_matrix4x4 *m, float4 in);
278
279/**
280 * \overload
281 */
282_RS_RUNTIME float4 __attribute__((overloadable))
283rsMatrixMultiply(rs_matrix4x4 *m, float3 in);
284
285/**
286 * \overload
287 */
288_RS_RUNTIME float4 __attribute__((overloadable))
289rsMatrixMultiply(rs_matrix4x4 *m, float2 in);
290
291/**
292 * \overload
293 */
294_RS_RUNTIME float3 __attribute__((overloadable))
295rsMatrixMultiply(rs_matrix3x3 *m, float3 in);
296
297/**
298 * \overload
299 */
300_RS_RUNTIME float3 __attribute__((overloadable))
301rsMatrixMultiply(rs_matrix3x3 *m, float2 in);
302
303/**
304 * \overload
305 */
306_RS_RUNTIME float2 __attribute__((overloadable))
307rsMatrixMultiply(rs_matrix2x2 *m, float2 in);
308#else
309/**
310 * Multiply a vector by a matrix and return the result vector.
311 * API version 14+
312 */
313_RS_RUNTIME float4 __attribute__((overloadable))
314rsMatrixMultiply(const rs_matrix4x4 *m, float4 in);
315
316/**
317 * \overload
318 */
319_RS_RUNTIME float4 __attribute__((overloadable))
320rsMatrixMultiply(const rs_matrix4x4 *m, float3 in);
321
322/**
323 * \overload
324 */
325_RS_RUNTIME float4 __attribute__((overloadable))
326rsMatrixMultiply(const rs_matrix4x4 *m, float2 in);
327
328/**
329 * \overload
330 */
331_RS_RUNTIME float3 __attribute__((overloadable))
332rsMatrixMultiply(const rs_matrix3x3 *m, float3 in);
333
334/**
335 * \overload
336 */
337_RS_RUNTIME float3 __attribute__((overloadable))
338rsMatrixMultiply(const rs_matrix3x3 *m, float2 in);
339
340/**
341 * \overload
342 */
343_RS_RUNTIME float2 __attribute__((overloadable))
344rsMatrixMultiply(const rs_matrix2x2 *m, float2 in);
345#endif
346
347
348/**
349 * Returns true if the matrix was successfully inversed
350 *
351 * @param m
352 */
353extern bool __attribute__((overloadable)) rsMatrixInverse(rs_matrix4x4 *m);
354
355/**
356 * Returns true if the matrix was successfully inversed and transposed.
357 *
358 * @param m
359 */
360extern bool __attribute__((overloadable)) rsMatrixInverseTranspose(rs_matrix4x4 *m);
361
362/**
363 * Transpose the matrix m.
364 *
365 * @param m
366 */
367extern void __attribute__((overloadable)) rsMatrixTranspose(rs_matrix4x4 *m);
368/**
369 * \overload
370 */
371extern void __attribute__((overloadable)) rsMatrixTranspose(rs_matrix3x3 *m);
372/**
373 * \overload
374 */
375extern void __attribute__((overloadable)) rsMatrixTranspose(rs_matrix2x2 *m);
376
377
378#endif
379