blob: 4d3124cace3f827f478cbf9f396e67096b86745e [file] [log] [blame]
Ying Wangb335bb02011-11-29 10:23:55 -08001/*
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_math.rsh
18 * \brief todo-jsams
19 *
20 * todo-jsams
21 *
22 */
23
24#ifndef __RS_MATH_RSH__
25#define __RS_MATH_RSH__
26
27
28/**
29 * Return a random value between 0 (or min_value) and max_malue.
30 */
31extern int __attribute__((overloadable))
32 rsRand(int max_value);
33/**
34 * \overload
35 */
36extern int __attribute__((overloadable))
37 rsRand(int min_value, int max_value);
38/**
39 * \overload
40 */
41extern float __attribute__((overloadable))
42 rsRand(float max_value);
43/**
44 * \overload
45 */
46extern float __attribute__((overloadable))
47 rsRand(float min_value, float max_value);
48
49/**
50 * Returns the fractional part of a float
51 */
Stephen Hines996e4dc2013-08-13 01:04:14 -070052extern float __attribute__((const, overloadable))
Ying Wangb335bb02011-11-29 10:23:55 -080053 rsFrac(float);
54
55
56/////////////////////////////////////////////////////
57// int ops
58/////////////////////////////////////////////////////
59
60/**
61 * Clamp the value amount between low and high.
62 *
63 * @param amount The value to clamp
64 * @param low
65 * @param high
66 */
Stephen Hines996e4dc2013-08-13 01:04:14 -070067_RS_RUNTIME uint __attribute__((const, overloadable, always_inline)) rsClamp(uint amount, uint low, uint high);
Ying Wangb335bb02011-11-29 10:23:55 -080068
69/**
70 * \overload
71 */
Stephen Hines996e4dc2013-08-13 01:04:14 -070072_RS_RUNTIME int __attribute__((const, overloadable, always_inline)) rsClamp(int amount, int low, int high);
Ying Wangb335bb02011-11-29 10:23:55 -080073/**
74 * \overload
75 */
Stephen Hines996e4dc2013-08-13 01:04:14 -070076_RS_RUNTIME ushort __attribute__((const, overloadable, always_inline)) rsClamp(ushort amount, ushort low, ushort high);
Ying Wangb335bb02011-11-29 10:23:55 -080077/**
78 * \overload
79 */
Stephen Hines996e4dc2013-08-13 01:04:14 -070080_RS_RUNTIME short __attribute__((const, overloadable, always_inline)) rsClamp(short amount, short low, short high);
Ying Wangb335bb02011-11-29 10:23:55 -080081/**
82 * \overload
83 */
Stephen Hines996e4dc2013-08-13 01:04:14 -070084_RS_RUNTIME uchar __attribute__((const, overloadable, always_inline)) rsClamp(uchar amount, uchar low, uchar high);
Ying Wangb335bb02011-11-29 10:23:55 -080085/**
86 * \overload
87 */
Stephen Hines996e4dc2013-08-13 01:04:14 -070088_RS_RUNTIME char __attribute__((const, overloadable, always_inline)) rsClamp(char amount, char low, char high);
Ying Wangb335bb02011-11-29 10:23:55 -080089
90
91/**
92 * Computes 6 frustum planes from the view projection matrix
93 * @param viewProj matrix to extract planes from
94 * @param left plane
95 * @param right plane
96 * @param top plane
97 * @param bottom plane
98 * @param near plane
99 * @param far plane
100 */
101__inline__ static void __attribute__((overloadable, always_inline))
102rsExtractFrustumPlanes(const rs_matrix4x4 *viewProj,
103 float4 *left, float4 *right,
104 float4 *top, float4 *bottom,
105 float4 *near, float4 *far) {
106 // x y z w = a b c d in the plane equation
107 left->x = viewProj->m[3] + viewProj->m[0];
108 left->y = viewProj->m[7] + viewProj->m[4];
109 left->z = viewProj->m[11] + viewProj->m[8];
110 left->w = viewProj->m[15] + viewProj->m[12];
111
112 right->x = viewProj->m[3] - viewProj->m[0];
113 right->y = viewProj->m[7] - viewProj->m[4];
114 right->z = viewProj->m[11] - viewProj->m[8];
115 right->w = viewProj->m[15] - viewProj->m[12];
116
117 top->x = viewProj->m[3] - viewProj->m[1];
118 top->y = viewProj->m[7] - viewProj->m[5];
119 top->z = viewProj->m[11] - viewProj->m[9];
120 top->w = viewProj->m[15] - viewProj->m[13];
121
122 bottom->x = viewProj->m[3] + viewProj->m[1];
123 bottom->y = viewProj->m[7] + viewProj->m[5];
124 bottom->z = viewProj->m[11] + viewProj->m[9];
125 bottom->w = viewProj->m[15] + viewProj->m[13];
126
127 near->x = viewProj->m[3] + viewProj->m[2];
128 near->y = viewProj->m[7] + viewProj->m[6];
129 near->z = viewProj->m[11] + viewProj->m[10];
130 near->w = viewProj->m[15] + viewProj->m[14];
131
132 far->x = viewProj->m[3] - viewProj->m[2];
133 far->y = viewProj->m[7] - viewProj->m[6];
134 far->z = viewProj->m[11] - viewProj->m[10];
135 far->w = viewProj->m[15] - viewProj->m[14];
136
137 float len = length(left->xyz);
138 *left /= len;
139 len = length(right->xyz);
140 *right /= len;
141 len = length(top->xyz);
142 *top /= len;
143 len = length(bottom->xyz);
144 *bottom /= len;
145 len = length(near->xyz);
146 *near /= len;
147 len = length(far->xyz);
148 *far /= len;
149}
150
151/**
152 * Checks if a sphere is withing the 6 frustum planes
153 * @param sphere float4 representing the sphere
154 * @param left plane
155 * @param right plane
156 * @param top plane
157 * @param bottom plane
158 * @param near plane
159 * @param far plane
160 */
161__inline__ static bool __attribute__((overloadable, always_inline))
162rsIsSphereInFrustum(float4 *sphere,
163 float4 *left, float4 *right,
164 float4 *top, float4 *bottom,
165 float4 *near, float4 *far) {
166
167 float distToCenter = dot(left->xyz, sphere->xyz) + left->w;
168 if (distToCenter < -sphere->w) {
169 return false;
170 }
171 distToCenter = dot(right->xyz, sphere->xyz) + right->w;
172 if (distToCenter < -sphere->w) {
173 return false;
174 }
175 distToCenter = dot(top->xyz, sphere->xyz) + top->w;
176 if (distToCenter < -sphere->w) {
177 return false;
178 }
179 distToCenter = dot(bottom->xyz, sphere->xyz) + bottom->w;
180 if (distToCenter < -sphere->w) {
181 return false;
182 }
183 distToCenter = dot(near->xyz, sphere->xyz) + near->w;
184 if (distToCenter < -sphere->w) {
185 return false;
186 }
187 distToCenter = dot(far->xyz, sphere->xyz) + far->w;
188 if (distToCenter < -sphere->w) {
189 return false;
190 }
191 return true;
192}
193
194
195/**
196 * Pack floating point (0-1) RGB values into a uchar4. The alpha component is
197 * set to 255 (1.0).
198 *
199 * @param r
200 * @param g
201 * @param b
202 *
203 * @return uchar4
204 */
Stephen Hines996e4dc2013-08-13 01:04:14 -0700205_RS_RUNTIME uchar4 __attribute__((const, overloadable)) rsPackColorTo8888(float r, float g, float b);
Ying Wangb335bb02011-11-29 10:23:55 -0800206
207/**
208 * Pack floating point (0-1) RGBA values into a uchar4.
209 *
210 * @param r
211 * @param g
212 * @param b
213 * @param a
214 *
215 * @return uchar4
216 */
Stephen Hines996e4dc2013-08-13 01:04:14 -0700217_RS_RUNTIME uchar4 __attribute__((const, overloadable)) rsPackColorTo8888(float r, float g, float b, float a);
Ying Wangb335bb02011-11-29 10:23:55 -0800218
219/**
220 * Pack floating point (0-1) RGB values into a uchar4. The alpha component is
221 * set to 255 (1.0).
222 *
223 * @param color
224 *
225 * @return uchar4
226 */
Stephen Hines996e4dc2013-08-13 01:04:14 -0700227_RS_RUNTIME uchar4 __attribute__((const, overloadable)) rsPackColorTo8888(float3 color);
Ying Wangb335bb02011-11-29 10:23:55 -0800228
229/**
230 * Pack floating point (0-1) RGBA values into a uchar4.
231 *
232 * @param color
233 *
234 * @return uchar4
235 */
Stephen Hines996e4dc2013-08-13 01:04:14 -0700236_RS_RUNTIME uchar4 __attribute__((const, overloadable)) rsPackColorTo8888(float4 color);
Ying Wangb335bb02011-11-29 10:23:55 -0800237
238/**
239 * Unpack a uchar4 color to float4. The resulting float range will be (0-1).
240 *
241 * @param c
242 *
243 * @return float4
244 */
Stephen Hines996e4dc2013-08-13 01:04:14 -0700245_RS_RUNTIME float4 __attribute__((const)) rsUnpackColor8888(uchar4 c);
Ying Wangb335bb02011-11-29 10:23:55 -0800246
Stephen Hines996e4dc2013-08-13 01:04:14 -0700247_RS_RUNTIME uchar4 __attribute__((const, overloadable)) rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v);
248_RS_RUNTIME float4 __attribute__((const, overloadable)) rsYuvToRGBA_float4(uchar y, uchar u, uchar v);
Ying Wang60999142013-01-07 13:59:36 -0800249
Ying Wangb335bb02011-11-29 10:23:55 -0800250
251#endif