blob: b6b6ee09ca1dece53a6db2354eabdcde2053a409 [file] [log] [blame]
Jason Sams044e2ee2011-08-08 16:52:30 -07001/*
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -07002 * Copyright (C) 2015 The Android Open Source Project
Jason Sams044e2ee2011-08-08 16:52:30 -07003 *
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
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070017// Don't edit this file! It is auto-generated by frameworks/rs/api/gen_runtime.
18
19/*
20 * rs_math.rsh: TODO Add documentation
Jason Sams9df3b2b2011-08-08 14:31:25 -070021 *
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070022 * TODO Add documentation
23 */
24#ifndef RENDERSCRIPT_RS_MATH_RSH
25#define RENDERSCRIPT_RS_MATH_RSH
26
27/*
28 * rsClamp: Restrain a value to a range
Jason Sams9df3b2b2011-08-08 14:31:25 -070029 *
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070030 * Clamp a value between low and high.
Jason Sams044e2ee2011-08-08 16:52:30 -070031 *
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070032 * Deprecated. Use clamp() instead.
33 *
34 * Parameters:
35 * amount The value to clamp
36 * low Lower bound
37 * high Upper bound
Jason Sams9df3b2b2011-08-08 14:31:25 -070038 */
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070039extern char __attribute__((const, always_inline, overloadable))
40 rsClamp(char amount, char low, char high);
Jason Sams044e2ee2011-08-08 16:52:30 -070041
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070042extern uchar __attribute__((const, always_inline, overloadable))
43 rsClamp(uchar amount, uchar low, uchar high);
Jason Samsc61346b2010-05-28 18:23:22 -070044
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070045extern short __attribute__((const, always_inline, overloadable))
46 rsClamp(short amount, short low, short high);
Jason Samse1eb6152011-06-21 16:42:30 -070047
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070048extern ushort __attribute__((const, always_inline, overloadable))
49 rsClamp(ushort amount, ushort low, ushort high);
50
51extern int __attribute__((const, always_inline, overloadable))
52 rsClamp(int amount, int low, int high);
53
54extern uint __attribute__((const, always_inline, overloadable))
55 rsClamp(uint amount, uint low, uint high);
56
57/*
Jason Sams044e2ee2011-08-08 16:52:30 -070058 * Computes 6 frustum planes from the view projection matrix
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070059 *
60 * Parameters:
61 * viewProj matrix to extract planes from
62 * left left plane
63 * right right plane
64 * top top plane
65 * bottom bottom plane
66 * near near plane
67 * far far plane
Jason Samse1eb6152011-06-21 16:42:30 -070068 */
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070069static inline void __attribute__((always_inline, overloadable))
70 rsExtractFrustumPlanes(const rs_matrix4x4* viewProj, float4* left, float4* right, float4* top,
71 float4* bottom, float4* near, float4* far) {
Jason Sams044e2ee2011-08-08 16:52:30 -070072 // x y z w = a b c d in the plane equation
73 left->x = viewProj->m[3] + viewProj->m[0];
74 left->y = viewProj->m[7] + viewProj->m[4];
75 left->z = viewProj->m[11] + viewProj->m[8];
76 left->w = viewProj->m[15] + viewProj->m[12];
77
78 right->x = viewProj->m[3] - viewProj->m[0];
79 right->y = viewProj->m[7] - viewProj->m[4];
80 right->z = viewProj->m[11] - viewProj->m[8];
81 right->w = viewProj->m[15] - viewProj->m[12];
82
83 top->x = viewProj->m[3] - viewProj->m[1];
84 top->y = viewProj->m[7] - viewProj->m[5];
85 top->z = viewProj->m[11] - viewProj->m[9];
86 top->w = viewProj->m[15] - viewProj->m[13];
87
88 bottom->x = viewProj->m[3] + viewProj->m[1];
89 bottom->y = viewProj->m[7] + viewProj->m[5];
90 bottom->z = viewProj->m[11] + viewProj->m[9];
91 bottom->w = viewProj->m[15] + viewProj->m[13];
92
93 near->x = viewProj->m[3] + viewProj->m[2];
94 near->y = viewProj->m[7] + viewProj->m[6];
95 near->z = viewProj->m[11] + viewProj->m[10];
96 near->w = viewProj->m[15] + viewProj->m[14];
97
98 far->x = viewProj->m[3] - viewProj->m[2];
99 far->y = viewProj->m[7] - viewProj->m[6];
100 far->z = viewProj->m[11] - viewProj->m[10];
101 far->w = viewProj->m[15] - viewProj->m[14];
102
103 float len = length(left->xyz);
104 *left /= len;
105 len = length(right->xyz);
106 *right /= len;
107 len = length(top->xyz);
108 *top /= len;
109 len = length(bottom->xyz);
110 *bottom /= len;
111 len = length(near->xyz);
112 *near /= len;
113 len = length(far->xyz);
114 *far /= len;
115}
Jason Samse1eb6152011-06-21 16:42:30 -0700116
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700117/*
118 * Returns the fractional part of a float
Jason Samse1eb6152011-06-21 16:42:30 -0700119 */
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700120extern float __attribute__((const, overloadable))
121 rsFrac(float v);
Jason Sams044e2ee2011-08-08 16:52:30 -0700122
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700123/*
124 * Checks if a sphere is withing the 6 frustum planes
125 *
126 * Parameters:
127 * sphere float4 representing the sphere
128 * left left plane
129 * right right plane
130 * top top plane
131 * bottom bottom plane
132 * near near plane
133 * far far plane
134 */
135static inline bool __attribute__((always_inline, overloadable))
136 rsIsSphereInFrustum(float4* sphere, float4* left, float4* right, float4* top, float4* bottom,
137 float4* near, float4* far) {
Jason Sams044e2ee2011-08-08 16:52:30 -0700138 float distToCenter = dot(left->xyz, sphere->xyz) + left->w;
139 if (distToCenter < -sphere->w) {
140 return false;
141 }
142 distToCenter = dot(right->xyz, sphere->xyz) + right->w;
143 if (distToCenter < -sphere->w) {
144 return false;
145 }
146 distToCenter = dot(top->xyz, sphere->xyz) + top->w;
147 if (distToCenter < -sphere->w) {
148 return false;
149 }
150 distToCenter = dot(bottom->xyz, sphere->xyz) + bottom->w;
151 if (distToCenter < -sphere->w) {
152 return false;
153 }
154 distToCenter = dot(near->xyz, sphere->xyz) + near->w;
155 if (distToCenter < -sphere->w) {
156 return false;
157 }
158 distToCenter = dot(far->xyz, sphere->xyz) + far->w;
159 if (distToCenter < -sphere->w) {
160 return false;
161 }
162 return true;
163}
164
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700165/*
166 * Pack floating point (0-1) RGB values into a uchar4.
Jason Samse1eb6152011-06-21 16:42:30 -0700167 *
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700168 * For the float3 variant and the variant that only specifies r, g, b,
169 * the alpha component is set to 255 (1.0).
Jason Samse1eb6152011-06-21 16:42:30 -0700170 */
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700171extern uchar4 __attribute__((const, overloadable))
172 rsPackColorTo8888(float r, float g, float b);
Jason Samse1eb6152011-06-21 16:42:30 -0700173
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700174extern uchar4 __attribute__((const, overloadable))
175 rsPackColorTo8888(float r, float g, float b, float a);
176
177extern uchar4 __attribute__((const, overloadable))
178 rsPackColorTo8888(float3 color);
179
180extern uchar4 __attribute__((const, overloadable))
181 rsPackColorTo8888(float4 color);
182
183/*
184 * Return a random value between 0 (or min_value) and max_malue.
Jason Samse1eb6152011-06-21 16:42:30 -0700185 */
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700186extern int __attribute__((overloadable))
187 rsRand(int max_value);
Jason Samse1eb6152011-06-21 16:42:30 -0700188
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700189extern int __attribute__((overloadable))
190 rsRand(int min_value, int max_value);
Jason Samse1eb6152011-06-21 16:42:30 -0700191
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700192extern float __attribute__((overloadable))
193 rsRand(float max_value);
Jason Samse1eb6152011-06-21 16:42:30 -0700194
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700195extern float __attribute__((overloadable))
196 rsRand(float min_value, float max_value);
197
198/*
Jason Sams044e2ee2011-08-08 16:52:30 -0700199 * Unpack a uchar4 color to float4. The resulting float range will be (0-1).
Jason Samse1eb6152011-06-21 16:42:30 -0700200 */
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700201extern float4 __attribute__((const))
202 rsUnpackColor8888(uchar4 c);
Jason Samse1eb6152011-06-21 16:42:30 -0700203
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700204/*
205 * Convert from YUV to RGBA.
206 */
207extern float4 __attribute__((const, overloadable))
208 rsYuvToRGBA_float4(uchar y, uchar u, uchar v);
Jason Sams4b768a62012-06-13 15:51:49 -0700209
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700210extern uchar4 __attribute__((const, overloadable))
211 rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v);
Jason Samse1eb6152011-06-21 16:42:30 -0700212
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700213#endif // RENDERSCRIPT_RS_MATH_RSH