rs_math.rsh revision abb430547e61bcf94da826476b463d2ff89bc138
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/* Constants */ 28#define M_E 2.718281828459045235360287471352662498f /* e */ 29#define M_LOG2E 1.442695040888963407359924681001892137f /* log_2 e */ 30#define M_LOG10E 0.434294481903251827651128918916605082f /* log_10 e */ 31#define M_LN2 0.693147180559945309417232121458176568f /* log_e 2 */ 32#define M_LN10 2.302585092994045684017991454684364208f /* log_e 10 */ 33#define M_PI 3.141592653589793238462643383279502884f /* pi */ 34#define M_PI_2 1.570796326794896619231321691639751442f /* pi/2 */ 35#define M_PI_4 0.785398163397448309615660845819875721f /* pi/4 */ 36#define M_1_PI 0.318309886183790671537767526745028724f /* 1/pi */ 37#define M_2_PIl 0.636619772367581343075535053490057448f /* 2/pi */ 38#define M_2_SQRTPI 1.128379167095512573896158903121545172f /* 2/sqrt(pi) */ 39#define M_SQRT2 1.414213562373095048801688724209698079f /* sqrt(2) */ 40#define M_SQRT1_2 0.707106781186547524400844362104849039f /* 1/sqrt(2) */ 41 42/** 43 * Return a random value between 0 (or min_value) and max_malue. 44 */ 45extern int __attribute__((overloadable)) 46 rsRand(int max_value); 47/** 48 * \overload 49 */ 50extern int __attribute__((overloadable)) 51 rsRand(int min_value, int max_value); 52/** 53 * \overload 54 */ 55extern float __attribute__((overloadable)) 56 rsRand(float max_value); 57/** 58 * \overload 59 */ 60extern float __attribute__((overloadable)) 61 rsRand(float min_value, float max_value); 62 63/** 64 * Returns the fractional part of a float 65 */ 66extern float __attribute__((overloadable)) 67 rsFrac(float); 68 69 70///////////////////////////////////////////////////// 71// int ops 72///////////////////////////////////////////////////// 73 74/** 75 * Clamp the value amount between low and high. 76 * 77 * @param amount The value to clamp 78 * @param low 79 * @param high 80 */ 81_RS_RUNTIME uint __attribute__((overloadable, always_inline)) rsClamp(uint amount, uint low, uint high); 82 83/** 84 * \overload 85 */ 86_RS_RUNTIME int __attribute__((overloadable, always_inline)) rsClamp(int amount, int low, int high); 87/** 88 * \overload 89 */ 90_RS_RUNTIME ushort __attribute__((overloadable, always_inline)) rsClamp(ushort amount, ushort low, ushort high); 91/** 92 * \overload 93 */ 94_RS_RUNTIME short __attribute__((overloadable, always_inline)) rsClamp(short amount, short low, short high); 95/** 96 * \overload 97 */ 98_RS_RUNTIME uchar __attribute__((overloadable, always_inline)) rsClamp(uchar amount, uchar low, uchar high); 99/** 100 * \overload 101 */ 102_RS_RUNTIME char __attribute__((overloadable, always_inline)) rsClamp(char amount, char low, char high); 103 104 105/** 106 * Computes 6 frustum planes from the view projection matrix 107 * @param viewProj matrix to extract planes from 108 * @param left plane 109 * @param right plane 110 * @param top plane 111 * @param bottom plane 112 * @param near plane 113 * @param far plane 114 */ 115__inline__ static void __attribute__((overloadable, always_inline)) 116rsExtractFrustumPlanes(const rs_matrix4x4 *viewProj, 117 float4 *left, float4 *right, 118 float4 *top, float4 *bottom, 119 float4 *near, float4 *far) { 120 // x y z w = a b c d in the plane equation 121 left->x = viewProj->m[3] + viewProj->m[0]; 122 left->y = viewProj->m[7] + viewProj->m[4]; 123 left->z = viewProj->m[11] + viewProj->m[8]; 124 left->w = viewProj->m[15] + viewProj->m[12]; 125 126 right->x = viewProj->m[3] - viewProj->m[0]; 127 right->y = viewProj->m[7] - viewProj->m[4]; 128 right->z = viewProj->m[11] - viewProj->m[8]; 129 right->w = viewProj->m[15] - viewProj->m[12]; 130 131 top->x = viewProj->m[3] - viewProj->m[1]; 132 top->y = viewProj->m[7] - viewProj->m[5]; 133 top->z = viewProj->m[11] - viewProj->m[9]; 134 top->w = viewProj->m[15] - viewProj->m[13]; 135 136 bottom->x = viewProj->m[3] + viewProj->m[1]; 137 bottom->y = viewProj->m[7] + viewProj->m[5]; 138 bottom->z = viewProj->m[11] + viewProj->m[9]; 139 bottom->w = viewProj->m[15] + viewProj->m[13]; 140 141 near->x = viewProj->m[3] + viewProj->m[2]; 142 near->y = viewProj->m[7] + viewProj->m[6]; 143 near->z = viewProj->m[11] + viewProj->m[10]; 144 near->w = viewProj->m[15] + viewProj->m[14]; 145 146 far->x = viewProj->m[3] - viewProj->m[2]; 147 far->y = viewProj->m[7] - viewProj->m[6]; 148 far->z = viewProj->m[11] - viewProj->m[10]; 149 far->w = viewProj->m[15] - viewProj->m[14]; 150 151 float len = length(left->xyz); 152 *left /= len; 153 len = length(right->xyz); 154 *right /= len; 155 len = length(top->xyz); 156 *top /= len; 157 len = length(bottom->xyz); 158 *bottom /= len; 159 len = length(near->xyz); 160 *near /= len; 161 len = length(far->xyz); 162 *far /= len; 163} 164 165/** 166 * Checks if a sphere is withing the 6 frustum planes 167 * @param sphere float4 representing the sphere 168 * @param left plane 169 * @param right plane 170 * @param top plane 171 * @param bottom plane 172 * @param near plane 173 * @param far plane 174 */ 175__inline__ static bool __attribute__((overloadable, always_inline)) 176rsIsSphereInFrustum(float4 *sphere, 177 float4 *left, float4 *right, 178 float4 *top, float4 *bottom, 179 float4 *near, float4 *far) { 180 181 float distToCenter = dot(left->xyz, sphere->xyz) + left->w; 182 if (distToCenter < -sphere->w) { 183 return false; 184 } 185 distToCenter = dot(right->xyz, sphere->xyz) + right->w; 186 if (distToCenter < -sphere->w) { 187 return false; 188 } 189 distToCenter = dot(top->xyz, sphere->xyz) + top->w; 190 if (distToCenter < -sphere->w) { 191 return false; 192 } 193 distToCenter = dot(bottom->xyz, sphere->xyz) + bottom->w; 194 if (distToCenter < -sphere->w) { 195 return false; 196 } 197 distToCenter = dot(near->xyz, sphere->xyz) + near->w; 198 if (distToCenter < -sphere->w) { 199 return false; 200 } 201 distToCenter = dot(far->xyz, sphere->xyz) + far->w; 202 if (distToCenter < -sphere->w) { 203 return false; 204 } 205 return true; 206} 207 208 209/** 210 * Pack floating point (0-1) RGB values into a uchar4. The alpha component is 211 * set to 255 (1.0). 212 * 213 * @param r 214 * @param g 215 * @param b 216 * 217 * @return uchar4 218 */ 219_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b); 220 221/** 222 * Pack floating point (0-1) RGBA values into a uchar4. 223 * 224 * @param r 225 * @param g 226 * @param b 227 * @param a 228 * 229 * @return uchar4 230 */ 231_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b, float a); 232 233/** 234 * Pack floating point (0-1) RGB values into a uchar4. The alpha component is 235 * set to 255 (1.0). 236 * 237 * @param color 238 * 239 * @return uchar4 240 */ 241_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float3 color); 242 243/** 244 * Pack floating point (0-1) RGBA values into a uchar4. 245 * 246 * @param color 247 * 248 * @return uchar4 249 */ 250_RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float4 color); 251 252/** 253 * Unpack a uchar4 color to float4. The resulting float range will be (0-1). 254 * 255 * @param c 256 * 257 * @return float4 258 */ 259_RS_RUNTIME float4 rsUnpackColor8888(uchar4 c); 260 261_RS_RUNTIME uchar4 __attribute__((overloadable)) rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v); 262_RS_RUNTIME float4 __attribute__((overloadable)) rsYuvToRGBA_float4(uchar y, uchar u, uchar v); 263 264 265#endif 266