blob: 8117ca824b183e57c1b2216405e6312c9d67e1b9 [file] [log] [blame]
Jason Sams044e2ee2011-08-08 16:52:30 -07001/*
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
Jason Sams9df3b2b2011-08-08 14:31:25 -070017/** @file rs_math.rsh
18 * \brief todo-jsams
19 *
20 * todo-jsams
21 *
22 */
Jason Sams044e2ee2011-08-08 16:52:30 -070023
Jason Samsc4cdf452010-07-07 11:55:51 -070024#ifndef __RS_MATH_RSH__
25#define __RS_MATH_RSH__
26
Jason Sams9df3b2b2011-08-08 14:31:25 -070027
Jason Sams9df3b2b2011-08-08 14:31:25 -070028/**
29 * Return a random value between 0 (or min_value) and max_malue.
30 */
Jason Sams73495472010-07-29 17:31:14 -070031extern int __attribute__((overloadable))
32 rsRand(int max_value);
Jason Sams9df3b2b2011-08-08 14:31:25 -070033/**
34 * \overload
35 */
Jason Sams73495472010-07-29 17:31:14 -070036extern int __attribute__((overloadable))
37 rsRand(int min_value, int max_value);
Jason Sams9df3b2b2011-08-08 14:31:25 -070038/**
39 * \overload
40 */
Jason Sams73495472010-07-29 17:31:14 -070041extern float __attribute__((overloadable))
42 rsRand(float max_value);
Jason Sams9df3b2b2011-08-08 14:31:25 -070043/**
44 * \overload
45 */
Jason Sams73495472010-07-29 17:31:14 -070046extern float __attribute__((overloadable))
47 rsRand(float min_value, float max_value);
Jason Sams22fa3712010-05-19 17:22:57 -070048
Jason Sams9df3b2b2011-08-08 14:31:25 -070049/**
50 * Returns the fractional part of a float
51 */
Jason Sams73495472010-07-29 17:31:14 -070052extern float __attribute__((overloadable))
53 rsFrac(float);
Jason Sams22fa3712010-05-19 17:22:57 -070054
Jason Sams044e2ee2011-08-08 16:52:30 -070055
56/////////////////////////////////////////////////////
57// int ops
58/////////////////////////////////////////////////////
59
Jason Sams9df3b2b2011-08-08 14:31:25 -070060/**
Jason Sams044e2ee2011-08-08 16:52:30 -070061 * Clamp the value amount between low and high.
62 *
63 * @param amount The value to clamp
64 * @param low
65 * @param high
Jason Sams9df3b2b2011-08-08 14:31:25 -070066 */
Jason Sams044e2ee2011-08-08 16:52:30 -070067_RS_RUNTIME uint __attribute__((overloadable, always_inline)) rsClamp(uint amount, uint low, uint high);
68
Jason Sams9df3b2b2011-08-08 14:31:25 -070069/**
70 * \overload
71 */
Jason Sams044e2ee2011-08-08 16:52:30 -070072_RS_RUNTIME int __attribute__((overloadable, always_inline)) rsClamp(int amount, int low, int high);
Jason Sams9df3b2b2011-08-08 14:31:25 -070073/**
74 * \overload
75 */
Jason Sams044e2ee2011-08-08 16:52:30 -070076_RS_RUNTIME ushort __attribute__((overloadable, always_inline)) rsClamp(ushort amount, ushort low, ushort high);
Jason Sams9df3b2b2011-08-08 14:31:25 -070077/**
78 * \overload
79 */
Jason Sams044e2ee2011-08-08 16:52:30 -070080_RS_RUNTIME short __attribute__((overloadable, always_inline)) rsClamp(short amount, short low, short high);
Jason Sams9df3b2b2011-08-08 14:31:25 -070081/**
82 * \overload
83 */
Jason Sams044e2ee2011-08-08 16:52:30 -070084_RS_RUNTIME uchar __attribute__((overloadable, always_inline)) rsClamp(uchar amount, uchar low, uchar high);
Jason Sams9df3b2b2011-08-08 14:31:25 -070085/**
86 * \overload
87 */
Jason Sams044e2ee2011-08-08 16:52:30 -070088_RS_RUNTIME char __attribute__((overloadable, always_inline)) rsClamp(char amount, char low, char high);
Jason Samsc61346b2010-05-28 18:23:22 -070089
Jason Samse1eb6152011-06-21 16:42:30 -070090
91/**
Jason Sams044e2ee2011-08-08 16:52:30 -070092 * 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
Jason Samse1eb6152011-06-21 16:42:30 -0700100 */
Jason Sams044e2ee2011-08-08 16:52:30 -0700101__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}
Jason Samse1eb6152011-06-21 16:42:30 -0700150
151/**
Jason Sams044e2ee2011-08-08 16:52:30 -0700152 * 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
Jason Samse1eb6152011-06-21 16:42:30 -0700160 */
Jason Sams044e2ee2011-08-08 16:52:30 -0700161__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
Jason Samse1eb6152011-06-21 16:42:30 -0700194
195/**
Jason Sams044e2ee2011-08-08 16:52:30 -0700196 * Pack floating point (0-1) RGB values into a uchar4. The alpha component is
197 * set to 255 (1.0).
Jason Samse1eb6152011-06-21 16:42:30 -0700198 *
Jason Sams044e2ee2011-08-08 16:52:30 -0700199 * @param r
200 * @param g
201 * @param b
Jason Samse1eb6152011-06-21 16:42:30 -0700202 *
Jason Sams044e2ee2011-08-08 16:52:30 -0700203 * @return uchar4
Jason Samse1eb6152011-06-21 16:42:30 -0700204 */
Jason Sams044e2ee2011-08-08 16:52:30 -0700205_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b);
Jason Samse1eb6152011-06-21 16:42:30 -0700206
207/**
Jason Sams044e2ee2011-08-08 16:52:30 -0700208 * Pack floating point (0-1) RGBA values into a uchar4.
Jason Samse1eb6152011-06-21 16:42:30 -0700209 *
Jason Sams044e2ee2011-08-08 16:52:30 -0700210 * @param r
211 * @param g
212 * @param b
213 * @param a
Jason Samse1eb6152011-06-21 16:42:30 -0700214 *
Jason Sams044e2ee2011-08-08 16:52:30 -0700215 * @return uchar4
Jason Samse1eb6152011-06-21 16:42:30 -0700216 */
Jason Sams044e2ee2011-08-08 16:52:30 -0700217_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b, float a);
Jason Samse1eb6152011-06-21 16:42:30 -0700218
219/**
Jason Sams044e2ee2011-08-08 16:52:30 -0700220 * Pack floating point (0-1) RGB values into a uchar4. The alpha component is
221 * set to 255 (1.0).
Jason Samse1eb6152011-06-21 16:42:30 -0700222 *
Jason Sams044e2ee2011-08-08 16:52:30 -0700223 * @param color
Jason Samse1eb6152011-06-21 16:42:30 -0700224 *
Jason Sams044e2ee2011-08-08 16:52:30 -0700225 * @return uchar4
Jason Samse1eb6152011-06-21 16:42:30 -0700226 */
Jason Sams044e2ee2011-08-08 16:52:30 -0700227_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float3 color);
Jason Samse1eb6152011-06-21 16:42:30 -0700228
229/**
Jason Sams044e2ee2011-08-08 16:52:30 -0700230 * Pack floating point (0-1) RGBA values into a uchar4.
Jason Samse1eb6152011-06-21 16:42:30 -0700231 *
Jason Sams044e2ee2011-08-08 16:52:30 -0700232 * @param color
Jason Samse1eb6152011-06-21 16:42:30 -0700233 *
Jason Sams044e2ee2011-08-08 16:52:30 -0700234 * @return uchar4
Jason Samse1eb6152011-06-21 16:42:30 -0700235 */
Jason Sams044e2ee2011-08-08 16:52:30 -0700236_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float4 color);
Jason Samse1eb6152011-06-21 16:42:30 -0700237
238/**
Jason Sams044e2ee2011-08-08 16:52:30 -0700239 * Unpack a uchar4 color to float4. The resulting float range will be (0-1).
Jason Samse1eb6152011-06-21 16:42:30 -0700240 *
Jason Sams044e2ee2011-08-08 16:52:30 -0700241 * @param c
Jason Samse1eb6152011-06-21 16:42:30 -0700242 *
Jason Sams044e2ee2011-08-08 16:52:30 -0700243 * @return float4
Jason Samse1eb6152011-06-21 16:42:30 -0700244 */
Jason Sams044e2ee2011-08-08 16:52:30 -0700245_RS_RUNTIME float4 rsUnpackColor8888(uchar4 c);
Jason Samse1eb6152011-06-21 16:42:30 -0700246
247
Jason Samsc4cdf452010-07-07 11:55:51 -0700248#endif