Ying Wang | b335bb0 | 2011-11-29 10:23:55 -0800 | [diff] [blame] | 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_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 | */ |
| 31 | extern int __attribute__((overloadable)) |
| 32 | rsRand(int max_value); |
| 33 | /** |
| 34 | * \overload |
| 35 | */ |
| 36 | extern int __attribute__((overloadable)) |
| 37 | rsRand(int min_value, int max_value); |
| 38 | /** |
| 39 | * \overload |
| 40 | */ |
| 41 | extern float __attribute__((overloadable)) |
| 42 | rsRand(float max_value); |
| 43 | /** |
| 44 | * \overload |
| 45 | */ |
| 46 | extern float __attribute__((overloadable)) |
| 47 | rsRand(float min_value, float max_value); |
| 48 | |
| 49 | /** |
| 50 | * Returns the fractional part of a float |
| 51 | */ |
Stephen Hines | 996e4dc | 2013-08-13 01:04:14 -0700 | [diff] [blame] | 52 | extern float __attribute__((const, overloadable)) |
Ying Wang | b335bb0 | 2011-11-29 10:23:55 -0800 | [diff] [blame] | 53 | 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 Hines | 996e4dc | 2013-08-13 01:04:14 -0700 | [diff] [blame] | 67 | _RS_RUNTIME uint __attribute__((const, overloadable, always_inline)) rsClamp(uint amount, uint low, uint high); |
Ying Wang | b335bb0 | 2011-11-29 10:23:55 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * \overload |
| 71 | */ |
Stephen Hines | 996e4dc | 2013-08-13 01:04:14 -0700 | [diff] [blame] | 72 | _RS_RUNTIME int __attribute__((const, overloadable, always_inline)) rsClamp(int amount, int low, int high); |
Ying Wang | b335bb0 | 2011-11-29 10:23:55 -0800 | [diff] [blame] | 73 | /** |
| 74 | * \overload |
| 75 | */ |
Stephen Hines | 996e4dc | 2013-08-13 01:04:14 -0700 | [diff] [blame] | 76 | _RS_RUNTIME ushort __attribute__((const, overloadable, always_inline)) rsClamp(ushort amount, ushort low, ushort high); |
Ying Wang | b335bb0 | 2011-11-29 10:23:55 -0800 | [diff] [blame] | 77 | /** |
| 78 | * \overload |
| 79 | */ |
Stephen Hines | 996e4dc | 2013-08-13 01:04:14 -0700 | [diff] [blame] | 80 | _RS_RUNTIME short __attribute__((const, overloadable, always_inline)) rsClamp(short amount, short low, short high); |
Ying Wang | b335bb0 | 2011-11-29 10:23:55 -0800 | [diff] [blame] | 81 | /** |
| 82 | * \overload |
| 83 | */ |
Stephen Hines | 996e4dc | 2013-08-13 01:04:14 -0700 | [diff] [blame] | 84 | _RS_RUNTIME uchar __attribute__((const, overloadable, always_inline)) rsClamp(uchar amount, uchar low, uchar high); |
Ying Wang | b335bb0 | 2011-11-29 10:23:55 -0800 | [diff] [blame] | 85 | /** |
| 86 | * \overload |
| 87 | */ |
Stephen Hines | 996e4dc | 2013-08-13 01:04:14 -0700 | [diff] [blame] | 88 | _RS_RUNTIME char __attribute__((const, overloadable, always_inline)) rsClamp(char amount, char low, char high); |
Ying Wang | b335bb0 | 2011-11-29 10:23:55 -0800 | [diff] [blame] | 89 | |
| 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)) |
| 102 | rsExtractFrustumPlanes(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)) |
| 162 | rsIsSphereInFrustum(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 Hines | 996e4dc | 2013-08-13 01:04:14 -0700 | [diff] [blame] | 205 | _RS_RUNTIME uchar4 __attribute__((const, overloadable)) rsPackColorTo8888(float r, float g, float b); |
Ying Wang | b335bb0 | 2011-11-29 10:23:55 -0800 | [diff] [blame] | 206 | |
| 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 Hines | 996e4dc | 2013-08-13 01:04:14 -0700 | [diff] [blame] | 217 | _RS_RUNTIME uchar4 __attribute__((const, overloadable)) rsPackColorTo8888(float r, float g, float b, float a); |
Ying Wang | b335bb0 | 2011-11-29 10:23:55 -0800 | [diff] [blame] | 218 | |
| 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 Hines | 996e4dc | 2013-08-13 01:04:14 -0700 | [diff] [blame] | 227 | _RS_RUNTIME uchar4 __attribute__((const, overloadable)) rsPackColorTo8888(float3 color); |
Ying Wang | b335bb0 | 2011-11-29 10:23:55 -0800 | [diff] [blame] | 228 | |
| 229 | /** |
| 230 | * Pack floating point (0-1) RGBA values into a uchar4. |
| 231 | * |
| 232 | * @param color |
| 233 | * |
| 234 | * @return uchar4 |
| 235 | */ |
Stephen Hines | 996e4dc | 2013-08-13 01:04:14 -0700 | [diff] [blame] | 236 | _RS_RUNTIME uchar4 __attribute__((const, overloadable)) rsPackColorTo8888(float4 color); |
Ying Wang | b335bb0 | 2011-11-29 10:23:55 -0800 | [diff] [blame] | 237 | |
| 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 Hines | 996e4dc | 2013-08-13 01:04:14 -0700 | [diff] [blame] | 245 | _RS_RUNTIME float4 __attribute__((const)) rsUnpackColor8888(uchar4 c); |
Ying Wang | b335bb0 | 2011-11-29 10:23:55 -0800 | [diff] [blame] | 246 | |
Stephen Hines | 996e4dc | 2013-08-13 01:04:14 -0700 | [diff] [blame] | 247 | _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 Wang | 6099914 | 2013-01-07 13:59:36 -0800 | [diff] [blame] | 249 | |
Ying Wang | b335bb0 | 2011-11-29 10:23:55 -0800 | [diff] [blame] | 250 | |
| 251 | #endif |