rs_math.rsh
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 The Android Open Source Project
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00024 #ifndef __RS_MATH_RSH__
00025 #define __RS_MATH_RSH__
00026 
00027 
00031 extern int __attribute__((overloadable))
00032     rsRand(int max_value);
00036 extern int __attribute__((overloadable))
00037     rsRand(int min_value, int max_value);
00041 extern float __attribute__((overloadable))
00042     rsRand(float max_value);
00046 extern float __attribute__((overloadable))
00047     rsRand(float min_value, float max_value);
00048 
00052 extern float __attribute__((overloadable))
00053     rsFrac(float);
00054 
00055 
00057 // int ops
00059 
00067 _RS_RUNTIME uint __attribute__((overloadable, always_inline)) rsClamp(uint amount, uint low, uint high);
00068 
00072 _RS_RUNTIME int __attribute__((overloadable, always_inline)) rsClamp(int amount, int low, int high);
00076 _RS_RUNTIME ushort __attribute__((overloadable, always_inline)) rsClamp(ushort amount, ushort low, ushort high);
00080 _RS_RUNTIME short __attribute__((overloadable, always_inline)) rsClamp(short amount, short low, short high);
00084 _RS_RUNTIME uchar __attribute__((overloadable, always_inline)) rsClamp(uchar amount, uchar low, uchar high);
00088 _RS_RUNTIME char __attribute__((overloadable, always_inline)) rsClamp(char amount, char low, char high);
00089 
00090 
00101 __inline__ static void __attribute__((overloadable, always_inline))
00102 rsExtractFrustumPlanes(const rs_matrix4x4 *viewProj,
00103                          float4 *left, float4 *right,
00104                          float4 *top, float4 *bottom,
00105                          float4 *near, float4 *far) {
00106     // x y z w = a b c d in the plane equation
00107     left->x = viewProj->m[3] + viewProj->m[0];
00108     left->y = viewProj->m[7] + viewProj->m[4];
00109     left->z = viewProj->m[11] + viewProj->m[8];
00110     left->w = viewProj->m[15] + viewProj->m[12];
00111 
00112     right->x = viewProj->m[3] - viewProj->m[0];
00113     right->y = viewProj->m[7] - viewProj->m[4];
00114     right->z = viewProj->m[11] - viewProj->m[8];
00115     right->w = viewProj->m[15] - viewProj->m[12];
00116 
00117     top->x = viewProj->m[3] - viewProj->m[1];
00118     top->y = viewProj->m[7] - viewProj->m[5];
00119     top->z = viewProj->m[11] - viewProj->m[9];
00120     top->w = viewProj->m[15] - viewProj->m[13];
00121 
00122     bottom->x = viewProj->m[3] + viewProj->m[1];
00123     bottom->y = viewProj->m[7] + viewProj->m[5];
00124     bottom->z = viewProj->m[11] + viewProj->m[9];
00125     bottom->w = viewProj->m[15] + viewProj->m[13];
00126 
00127     near->x = viewProj->m[3] + viewProj->m[2];
00128     near->y = viewProj->m[7] + viewProj->m[6];
00129     near->z = viewProj->m[11] + viewProj->m[10];
00130     near->w = viewProj->m[15] + viewProj->m[14];
00131 
00132     far->x = viewProj->m[3] - viewProj->m[2];
00133     far->y = viewProj->m[7] - viewProj->m[6];
00134     far->z = viewProj->m[11] - viewProj->m[10];
00135     far->w = viewProj->m[15] - viewProj->m[14];
00136 
00137     float len = length(left->xyz);
00138     *left /= len;
00139     len = length(right->xyz);
00140     *right /= len;
00141     len = length(top->xyz);
00142     *top /= len;
00143     len = length(bottom->xyz);
00144     *bottom /= len;
00145     len = length(near->xyz);
00146     *near /= len;
00147     len = length(far->xyz);
00148     *far /= len;
00149 }
00150 
00161 __inline__ static bool __attribute__((overloadable, always_inline))
00162 rsIsSphereInFrustum(float4 *sphere,
00163                       float4 *left, float4 *right,
00164                       float4 *top, float4 *bottom,
00165                       float4 *near, float4 *far) {
00166 
00167     float distToCenter = dot(left->xyz, sphere->xyz) + left->w;
00168     if (distToCenter < -sphere->w) {
00169         return false;
00170     }
00171     distToCenter = dot(right->xyz, sphere->xyz) + right->w;
00172     if (distToCenter < -sphere->w) {
00173         return false;
00174     }
00175     distToCenter = dot(top->xyz, sphere->xyz) + top->w;
00176     if (distToCenter < -sphere->w) {
00177         return false;
00178     }
00179     distToCenter = dot(bottom->xyz, sphere->xyz) + bottom->w;
00180     if (distToCenter < -sphere->w) {
00181         return false;
00182     }
00183     distToCenter = dot(near->xyz, sphere->xyz) + near->w;
00184     if (distToCenter < -sphere->w) {
00185         return false;
00186     }
00187     distToCenter = dot(far->xyz, sphere->xyz) + far->w;
00188     if (distToCenter < -sphere->w) {
00189         return false;
00190     }
00191     return true;
00192 }
00193 
00194 
00205 _RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b);
00206 
00217 _RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b, float a);
00218 
00227 _RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float3 color);
00228 
00236 _RS_RUNTIME uchar4 __attribute__((overloadable)) rsPackColorTo8888(float4 color);
00237 
00245 _RS_RUNTIME float4 rsUnpackColor8888(uchar4 c);
00246 
00247 
00248 #endif