Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | #include "rsContext.h" |
| 18 | #include "rsScriptC.h" |
| 19 | #include "rsMatrix.h" |
Romain Guy | ecc7ca03 | 2009-08-03 21:12:51 -0700 | [diff] [blame] | 20 | #include "rsNoise.h" |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 21 | |
| 22 | #include "acc/acc.h" |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 23 | #include "utils/Timers.h" |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 24 | |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 25 | #include <time.h> |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 26 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 27 | using namespace android; |
| 28 | using namespace android::renderscript; |
| 29 | |
| 30 | #define GET_TLS() Context::ScriptTLSStruct * tls = \ |
| 31 | (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \ |
| 32 | Context * rsc = tls->mContext; \ |
| 33 | ScriptC * sc = (ScriptC *) tls->mScript |
| 34 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 35 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 36 | |
| 37 | |
| 38 | ////////////////////////////////////////////////////////////////////////////// |
| 39 | // Non-Updated code below |
| 40 | ////////////////////////////////////////////////////////////////////////////// |
| 41 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 42 | typedef struct { |
| 43 | float x; |
| 44 | float y; |
| 45 | float z; |
| 46 | } vec3_t; |
| 47 | |
| 48 | typedef struct { |
| 49 | float x; |
| 50 | float y; |
| 51 | float z; |
| 52 | float w; |
| 53 | } vec4_t; |
| 54 | |
| 55 | typedef struct { |
| 56 | float x; |
| 57 | float y; |
| 58 | } vec2_t; |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 59 | |
Romain Guy | f8e136d | 2009-08-06 12:40:41 -0700 | [diff] [blame] | 60 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 61 | ////////////////////////////////////////////////////////////////////////////// |
| 62 | // Vec3 routines |
| 63 | ////////////////////////////////////////////////////////////////////////////// |
| 64 | |
| 65 | static void SC_vec3Norm(vec3_t *v) |
| 66 | { |
| 67 | float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z); |
| 68 | len = 1 / len; |
| 69 | v->x *= len; |
| 70 | v->y *= len; |
| 71 | v->z *= len; |
| 72 | } |
| 73 | |
| 74 | static float SC_vec3Length(const vec3_t *v) |
| 75 | { |
| 76 | return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z); |
| 77 | } |
| 78 | |
| 79 | static void SC_vec3Add(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs) |
| 80 | { |
| 81 | dest->x = lhs->x + rhs->x; |
| 82 | dest->y = lhs->y + rhs->y; |
| 83 | dest->z = lhs->z + rhs->z; |
| 84 | } |
| 85 | |
| 86 | static void SC_vec3Sub(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs) |
| 87 | { |
| 88 | dest->x = lhs->x - rhs->x; |
| 89 | dest->y = lhs->y - rhs->y; |
| 90 | dest->z = lhs->z - rhs->z; |
| 91 | } |
| 92 | |
| 93 | static void SC_vec3Cross(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs) |
| 94 | { |
| 95 | float x = lhs->y * rhs->z - lhs->z * rhs->y; |
| 96 | float y = lhs->z * rhs->x - lhs->x * rhs->z; |
| 97 | float z = lhs->x * rhs->y - lhs->y * rhs->x; |
| 98 | dest->x = x; |
| 99 | dest->y = y; |
| 100 | dest->z = z; |
| 101 | } |
| 102 | |
| 103 | static float SC_vec3Dot(const vec3_t *lhs, const vec3_t *rhs) |
| 104 | { |
| 105 | return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z; |
| 106 | } |
| 107 | |
| 108 | static void SC_vec3Scale(vec3_t *lhs, float scale) |
| 109 | { |
| 110 | lhs->x *= scale; |
| 111 | lhs->y *= scale; |
| 112 | lhs->z *= scale; |
| 113 | } |
| 114 | |
Romain Guy | d7fa122 | 2009-10-09 16:05:25 -0700 | [diff] [blame] | 115 | ////////////////////////////////////////////////////////////////////////////// |
| 116 | // Vec4 routines |
| 117 | ////////////////////////////////////////////////////////////////////////////// |
| 118 | |
| 119 | static void SC_vec4Norm(vec4_t *v) |
| 120 | { |
| 121 | float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w); |
| 122 | len = 1 / len; |
| 123 | v->x *= len; |
| 124 | v->y *= len; |
| 125 | v->z *= len; |
| 126 | v->w *= len; |
| 127 | } |
| 128 | |
| 129 | static float SC_vec4Length(const vec4_t *v) |
| 130 | { |
| 131 | return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w); |
| 132 | } |
| 133 | |
| 134 | static void SC_vec4Add(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs) |
| 135 | { |
| 136 | dest->x = lhs->x + rhs->x; |
| 137 | dest->y = lhs->y + rhs->y; |
| 138 | dest->z = lhs->z + rhs->z; |
| 139 | dest->w = lhs->w + rhs->w; |
| 140 | } |
| 141 | |
| 142 | static void SC_vec4Sub(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs) |
| 143 | { |
| 144 | dest->x = lhs->x - rhs->x; |
| 145 | dest->y = lhs->y - rhs->y; |
| 146 | dest->z = lhs->z - rhs->z; |
| 147 | dest->w = lhs->w - rhs->w; |
| 148 | } |
| 149 | |
| 150 | static float SC_vec4Dot(const vec4_t *lhs, const vec4_t *rhs) |
| 151 | { |
| 152 | return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z + lhs->w * rhs->w; |
| 153 | } |
| 154 | |
| 155 | static void SC_vec4Scale(vec4_t *lhs, float scale) |
| 156 | { |
| 157 | lhs->x *= scale; |
| 158 | lhs->y *= scale; |
| 159 | lhs->z *= scale; |
| 160 | lhs->w *= scale; |
| 161 | } |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 162 | |
| 163 | ////////////////////////////////////////////////////////////////////////////// |
| 164 | // Math routines |
| 165 | ////////////////////////////////////////////////////////////////////////////// |
| 166 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 167 | static float SC_sinf_fast(float x) |
| 168 | { |
| 169 | const float A = 1.0f / (2.0f * M_PI); |
| 170 | const float B = -16.0f; |
| 171 | const float C = 8.0f; |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 172 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 173 | // scale angle for easy argument reduction |
| 174 | x *= A; |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 175 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 176 | if (fabsf(x) >= 0.5f) { |
| 177 | // argument reduction |
| 178 | x = x - ceilf(x + 0.5f) + 1.0f; |
| 179 | } |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 180 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 181 | const float y = B * x * fabsf(x) + C * x; |
| 182 | return 0.2215f * (y * fabsf(y) - y) + y; |
| 183 | } |
| 184 | |
| 185 | static float SC_cosf_fast(float x) |
| 186 | { |
| 187 | x += float(M_PI / 2); |
| 188 | |
| 189 | const float A = 1.0f / (2.0f * M_PI); |
| 190 | const float B = -16.0f; |
| 191 | const float C = 8.0f; |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 192 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 193 | // scale angle for easy argument reduction |
| 194 | x *= A; |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 195 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 196 | if (fabsf(x) >= 0.5f) { |
| 197 | // argument reduction |
| 198 | x = x - ceilf(x + 0.5f) + 1.0f; |
| 199 | } |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 200 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 201 | const float y = B * x * fabsf(x) + C * x; |
| 202 | return 0.2215f * (y * fabsf(y) - y) + y; |
| 203 | } |
| 204 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 205 | static float SC_randf(float max) |
| 206 | { |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 207 | //LOGE("max %f", max); |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 208 | float r = (float)rand(); |
| 209 | return r / RAND_MAX * max; |
| 210 | } |
| 211 | |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 212 | static float SC_randf2(float min, float max) |
| 213 | { |
| 214 | float r = (float)rand(); |
| 215 | return r / RAND_MAX * (max - min) + min; |
| 216 | } |
| 217 | |
Romain Guy | d7fa122 | 2009-10-09 16:05:25 -0700 | [diff] [blame] | 218 | static int SC_sign(int value) |
| 219 | { |
| 220 | return (value > 0) - (value < 0); |
| 221 | } |
| 222 | |
Romain Guy | a9d2d5e | 2009-08-09 17:04:54 -0700 | [diff] [blame] | 223 | static int SC_clamp(int amount, int low, int high) |
| 224 | { |
| 225 | return amount < low ? low : (amount > high ? high : amount); |
| 226 | } |
| 227 | |
Jason Sams | d342fd7 | 2009-09-18 14:24:24 -0700 | [diff] [blame] | 228 | static float SC_roundf(float v) |
| 229 | { |
| 230 | return floorf(v + 0.4999999999); |
| 231 | } |
| 232 | |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 233 | static float SC_distf2(float x1, float y1, float x2, float y2) |
| 234 | { |
| 235 | float x = x2 - x1; |
| 236 | float y = y2 - y1; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 237 | return sqrtf(x * x + y * y); |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2) |
| 241 | { |
| 242 | float x = x2 - x1; |
| 243 | float y = y2 - y1; |
| 244 | float z = z2 - z1; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 245 | return sqrtf(x * x + y * y + z * z); |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static float SC_magf2(float a, float b) |
| 249 | { |
| 250 | return sqrtf(a * a + b * b); |
| 251 | } |
| 252 | |
| 253 | static float SC_magf3(float a, float b, float c) |
| 254 | { |
| 255 | return sqrtf(a * a + b * b + c * c); |
| 256 | } |
| 257 | |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 258 | static float SC_normf(float start, float stop, float value) |
| 259 | { |
| 260 | return (value - start) / (stop - start); |
| 261 | } |
| 262 | |
| 263 | static float SC_mapf(float minStart, float minStop, float maxStart, float maxStop, float value) |
| 264 | { |
| 265 | return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart)); |
| 266 | } |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 267 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 268 | static float SC_frac(float v) |
| 269 | { |
| 270 | int i = (int)floor(v); |
| 271 | return fmin(v - i, 0x1.fffffep-1f); |
| 272 | } |
| 273 | |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 274 | ////////////////////////////////////////////////////////////////////////////// |
| 275 | // Time routines |
| 276 | ////////////////////////////////////////////////////////////////////////////// |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 277 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 278 | static int32_t SC_second() |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 279 | { |
| 280 | GET_TLS(); |
| 281 | |
| 282 | time_t rawtime; |
| 283 | time(&rawtime); |
| 284 | |
Romain Guy | baed727 | 2009-11-11 15:36:06 -0800 | [diff] [blame] | 285 | struct tm *timeinfo; |
| 286 | timeinfo = localtime(&rawtime); |
| 287 | return timeinfo->tm_sec; |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 290 | static int32_t SC_minute() |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 291 | { |
| 292 | GET_TLS(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 293 | |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 294 | time_t rawtime; |
| 295 | time(&rawtime); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 296 | |
Romain Guy | baed727 | 2009-11-11 15:36:06 -0800 | [diff] [blame] | 297 | struct tm *timeinfo; |
| 298 | timeinfo = localtime(&rawtime); |
| 299 | return timeinfo->tm_min; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 300 | } |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 301 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 302 | static int32_t SC_hour() |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 303 | { |
| 304 | GET_TLS(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 305 | |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 306 | time_t rawtime; |
| 307 | time(&rawtime); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 308 | |
Romain Guy | baed727 | 2009-11-11 15:36:06 -0800 | [diff] [blame] | 309 | struct tm *timeinfo; |
| 310 | timeinfo = localtime(&rawtime); |
| 311 | return timeinfo->tm_hour; |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 314 | static int32_t SC_day() |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 315 | { |
| 316 | GET_TLS(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 317 | |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 318 | time_t rawtime; |
| 319 | time(&rawtime); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 320 | |
Romain Guy | baed727 | 2009-11-11 15:36:06 -0800 | [diff] [blame] | 321 | struct tm *timeinfo; |
| 322 | timeinfo = localtime(&rawtime); |
| 323 | return timeinfo->tm_mday; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 324 | } |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 325 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 326 | static int32_t SC_month() |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 327 | { |
| 328 | GET_TLS(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 329 | |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 330 | time_t rawtime; |
| 331 | time(&rawtime); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 332 | |
Romain Guy | baed727 | 2009-11-11 15:36:06 -0800 | [diff] [blame] | 333 | struct tm *timeinfo; |
| 334 | timeinfo = localtime(&rawtime); |
| 335 | return timeinfo->tm_mon; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 336 | } |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 337 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 338 | static int32_t SC_year() |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 339 | { |
| 340 | GET_TLS(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 341 | |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 342 | time_t rawtime; |
| 343 | time(&rawtime); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 344 | |
Romain Guy | baed727 | 2009-11-11 15:36:06 -0800 | [diff] [blame] | 345 | struct tm *timeinfo; |
| 346 | timeinfo = localtime(&rawtime); |
| 347 | return timeinfo->tm_year; |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 350 | static int32_t SC_uptimeMillis() |
| 351 | { |
| 352 | return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); |
| 353 | } |
| 354 | |
| 355 | static int32_t SC_startTimeMillis() |
| 356 | { |
| 357 | GET_TLS(); |
| 358 | return sc->mEnviroment.mStartTimeMillis; |
| 359 | } |
| 360 | |
| 361 | static int32_t SC_elapsedTimeMillis() |
| 362 | { |
| 363 | GET_TLS(); |
| 364 | return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 365 | - sc->mEnviroment.mStartTimeMillis; |
| 366 | } |
| 367 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 368 | ////////////////////////////////////////////////////////////////////////////// |
| 369 | // Matrix routines |
| 370 | ////////////////////////////////////////////////////////////////////////////// |
| 371 | |
| 372 | |
| 373 | static void SC_matrixLoadIdentity(rsc_Matrix *mat) |
| 374 | { |
| 375 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 376 | m->loadIdentity(); |
| 377 | } |
| 378 | |
| 379 | static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f) |
| 380 | { |
| 381 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 382 | m->load(f); |
| 383 | } |
| 384 | |
| 385 | static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat) |
| 386 | { |
| 387 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 388 | m->load(reinterpret_cast<const Matrix *>(newmat)); |
| 389 | } |
| 390 | |
| 391 | static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z) |
| 392 | { |
| 393 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 394 | m->loadRotate(rot, x, y, z); |
| 395 | } |
| 396 | |
| 397 | static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z) |
| 398 | { |
| 399 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 400 | m->loadScale(x, y, z); |
| 401 | } |
| 402 | |
| 403 | static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z) |
| 404 | { |
| 405 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 406 | m->loadTranslate(x, y, z); |
| 407 | } |
| 408 | |
| 409 | static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs) |
| 410 | { |
| 411 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 412 | m->loadMultiply(reinterpret_cast<const Matrix *>(lhs), |
| 413 | reinterpret_cast<const Matrix *>(rhs)); |
| 414 | } |
| 415 | |
| 416 | static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs) |
| 417 | { |
| 418 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 419 | m->multiply(reinterpret_cast<const Matrix *>(rhs)); |
| 420 | } |
| 421 | |
| 422 | static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z) |
| 423 | { |
| 424 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 425 | m->rotate(rot, x, y, z); |
| 426 | } |
| 427 | |
| 428 | static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z) |
| 429 | { |
| 430 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 431 | m->scale(x, y, z); |
| 432 | } |
| 433 | |
| 434 | static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z) |
| 435 | { |
| 436 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 437 | m->translate(x, y, z); |
| 438 | } |
| 439 | |
| 440 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 441 | static rsvF_2 SC_vec2Rand(float maxLen) |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 442 | { |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 443 | float2 t; |
| 444 | float angle = SC_randf(M_PI * 2); |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 445 | float len = SC_randf(maxLen); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 446 | t.f[0] = len * sinf(angle); |
| 447 | t.f[1] = len * cosf(angle); |
| 448 | return t.v; |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 451 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 452 | ////////////////////////////////////////////////////////////////////////////// |
| 453 | // |
| 454 | ////////////////////////////////////////////////////////////////////////////// |
| 455 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 456 | static uint32_t SC_allocGetDimX(RsAllocation va) |
| 457 | { |
| 458 | GET_TLS(); |
| 459 | const Allocation *a = static_cast<const Allocation *>(va); |
| 460 | //LOGE("SC_allocGetDimX a=%p", a); |
| 461 | //LOGE(" type=%p", a->getType()); |
| 462 | return a->getType()->getDimX(); |
| 463 | } |
| 464 | |
| 465 | static uint32_t SC_allocGetDimY(RsAllocation va) |
| 466 | { |
| 467 | GET_TLS(); |
| 468 | const Allocation *a = static_cast<const Allocation *>(va); |
| 469 | return a->getType()->getDimY(); |
| 470 | } |
| 471 | |
| 472 | static uint32_t SC_allocGetDimZ(RsAllocation va) |
| 473 | { |
| 474 | GET_TLS(); |
| 475 | const Allocation *a = static_cast<const Allocation *>(va); |
| 476 | return a->getType()->getDimZ(); |
| 477 | } |
| 478 | |
| 479 | static uint32_t SC_allocGetDimLOD(RsAllocation va) |
| 480 | { |
| 481 | GET_TLS(); |
| 482 | const Allocation *a = static_cast<const Allocation *>(va); |
| 483 | return a->getType()->getDimLOD(); |
| 484 | } |
| 485 | |
| 486 | static uint32_t SC_allocGetDimFaces(RsAllocation va) |
| 487 | { |
| 488 | GET_TLS(); |
| 489 | const Allocation *a = static_cast<const Allocation *>(va); |
| 490 | return a->getType()->getDimFaces(); |
| 491 | } |
| 492 | |
| 493 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 494 | |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 495 | static void SC_debugF(const char *s, float f) |
| 496 | { |
| 497 | LOGE("%s %f", s, f); |
| 498 | } |
| 499 | |
Romain Guy | d22fff7 | 2009-08-20 17:08:33 -0700 | [diff] [blame] | 500 | static void SC_debugHexF(const char *s, float f) |
| 501 | { |
| 502 | LOGE("%s 0x%x", s, *((int *) (&f))); |
| 503 | } |
| 504 | |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 505 | static void SC_debugI32(const char *s, int32_t i) |
| 506 | { |
| 507 | LOGE("%s %i", s, i); |
| 508 | } |
| 509 | |
Romain Guy | d22fff7 | 2009-08-20 17:08:33 -0700 | [diff] [blame] | 510 | static void SC_debugHexI32(const char *s, int32_t i) |
| 511 | { |
| 512 | LOGE("%s 0x%x", s, i); |
| 513 | } |
| 514 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 515 | static uchar4 SC_convertColorTo8888_f3(float r, float g, float b) { |
| 516 | uchar4 t; |
| 517 | t.f[0] = (uint8_t)(r * 255.f); |
| 518 | t.f[1] = (uint8_t)(g * 255.f); |
| 519 | t.f[2] = (uint8_t)(b * 255.f); |
| 520 | t.f[3] = 0xff; |
| 521 | return t; |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 524 | static uchar4 SC_convertColorTo8888_f4(float r, float g, float b, float a) { |
| 525 | uchar4 t; |
| 526 | t.f[0] = (uint8_t)(r * 255.f); |
| 527 | t.f[1] = (uint8_t)(g * 255.f); |
| 528 | t.f[2] = (uint8_t)(b * 255.f); |
| 529 | t.f[3] = (uint8_t)(a * 255.f); |
| 530 | return t; |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 533 | static uint32_t SC_toClient(void *data, int cmdID, int len, int waitForSpace) |
| 534 | { |
| 535 | GET_TLS(); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 536 | //LOGE("SC_toClient %i %i %i", cmdID, len, waitForSpace); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 537 | return rsc->sendMessageToClient(data, cmdID, len, waitForSpace != 0); |
| 538 | } |
| 539 | |
Jason Sams | bd2197f | 2009-10-07 18:14:01 -0700 | [diff] [blame] | 540 | static void SC_scriptCall(int scriptID) |
| 541 | { |
| 542 | GET_TLS(); |
| 543 | rsc->runScript((Script *)scriptID, 0); |
| 544 | } |
| 545 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 546 | static void SC_debugP(int i, void *p) |
| 547 | { |
| 548 | LOGE("debug P %i %p, %i", i, p, (int)p); |
| 549 | } |
| 550 | |
| 551 | static void SC_debugPi(int i, int p) |
| 552 | { |
| 553 | LOGE("debug Pi %i 0x%08x, %i", i, p, (int)p); |
| 554 | } |
| 555 | |
| 556 | static void SC_debugPf(int i, float p) |
| 557 | { |
| 558 | LOGE("debug Pf %i %f, 0x%08x", i, p, reinterpret_cast<uint32_t *>(&p)[0]); |
| 559 | } |
| 560 | |
| 561 | int SC_divsi3(int a, int b) |
| 562 | { |
| 563 | return a / b; |
| 564 | } |
Jason Sams | bd2197f | 2009-10-07 18:14:01 -0700 | [diff] [blame] | 565 | |
Jason Sams | 1de0b87 | 2010-05-17 14:55:34 -0700 | [diff] [blame] | 566 | int SC_getAllocation(const void *ptr) |
| 567 | { |
| 568 | GET_TLS(); |
| 569 | const Allocation *alloc = sc->ptrToAllocation(ptr); |
| 570 | return (int)alloc; |
| 571 | } |
| 572 | |
| 573 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 574 | ////////////////////////////////////////////////////////////////////////////// |
| 575 | // Class implementation |
| 576 | ////////////////////////////////////////////////////////////////////////////// |
| 577 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 578 | // llvm name mangling ref |
| 579 | // <builtin-type> ::= v # void |
| 580 | // ::= b # bool |
| 581 | // ::= c # char |
| 582 | // ::= a # signed char |
| 583 | // ::= h # unsigned char |
| 584 | // ::= s # short |
| 585 | // ::= t # unsigned short |
| 586 | // ::= i # int |
| 587 | // ::= j # unsigned int |
| 588 | // ::= l # long |
| 589 | // ::= m # unsigned long |
| 590 | // ::= x # long long, __int64 |
| 591 | // ::= y # unsigned long long, __int64 |
| 592 | // ::= f # float |
| 593 | // ::= d # double |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 594 | |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 595 | static ScriptCState::SymbolTable_t gSyms[] = { |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 596 | { "__divsi3", (void *)&SC_divsi3 }, |
| 597 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 598 | { "modf", (void *)&fmod }, |
| 599 | { "_Z4fracf", (void *)&SC_frac }, |
| 600 | //{ "sinf_fast", (void *)&SC_sinf_fast }, |
| 601 | //{ "cosf_fast", (void *)&SC_cosf_fast }, |
| 602 | { "randf", (void *)&SC_randf }, |
| 603 | { "randf2", (void *)&SC_randf2 }, |
| 604 | { "sign", (void *)&SC_sign }, |
| 605 | { "clamp", (void *)&SC_clamp }, |
| 606 | { "distf2", (void *)&SC_distf2 }, |
| 607 | { "distf3", (void *)&SC_distf3 }, |
| 608 | { "magf2", (void *)&SC_magf2 }, |
| 609 | { "magf3", (void *)&SC_magf3 }, |
| 610 | { "normf", (void *)&SC_normf }, |
| 611 | { "mapf", (void *)&SC_mapf }, |
| 612 | { "noisef", (void *)&SC_noisef }, |
| 613 | { "noisef2", (void *)&SC_noisef2 }, |
| 614 | { "noisef3", (void *)&SC_noisef3 }, |
| 615 | { "turbulencef2", (void *)&SC_turbulencef2 }, |
| 616 | { "turbulencef3", (void *)&SC_turbulencef3 }, |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 617 | |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 618 | // time |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 619 | { "second", (void *)&SC_second }, |
| 620 | { "minute", (void *)&SC_minute }, |
| 621 | { "hour", (void *)&SC_hour }, |
| 622 | { "day", (void *)&SC_day }, |
| 623 | { "month", (void *)&SC_month }, |
| 624 | { "year", (void *)&SC_year }, |
| 625 | { "uptimeMillis", (void*)&SC_uptimeMillis }, // TODO: use long instead |
| 626 | { "startTimeMillis", (void*)&SC_startTimeMillis }, // TODO: use long instead |
| 627 | { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis }, // TODO: use long instead |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 628 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 629 | // matrix |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 630 | { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity }, |
| 631 | { "matrixLoadFloat", (void *)&SC_matrixLoadFloat }, |
| 632 | { "matrixLoadMat", (void *)&SC_matrixLoadMat }, |
| 633 | { "matrixLoadRotate", (void *)&SC_matrixLoadRotate }, |
| 634 | { "matrixLoadScale", (void *)&SC_matrixLoadScale }, |
| 635 | { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate }, |
| 636 | { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply }, |
| 637 | { "matrixMultiply", (void *)&SC_matrixMultiply }, |
| 638 | { "matrixRotate", (void *)&SC_matrixRotate }, |
| 639 | { "matrixScale", (void *)&SC_matrixScale }, |
| 640 | { "matrixTranslate", (void *)&SC_matrixTranslate }, |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 641 | |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 642 | // vector |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 643 | { "vec2Rand", (void *)&SC_vec2Rand }, |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 644 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 645 | // vec3 |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 646 | { "vec3Norm", (void *)&SC_vec3Norm }, |
| 647 | { "vec3Length", (void *)&SC_vec3Length }, |
| 648 | { "vec3Add", (void *)&SC_vec3Add }, |
| 649 | { "vec3Sub", (void *)&SC_vec3Sub }, |
| 650 | { "vec3Cross", (void *)&SC_vec3Cross }, |
| 651 | { "vec3Dot", (void *)&SC_vec3Dot }, |
| 652 | { "vec3Scale", (void *)&SC_vec3Scale }, |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 653 | |
Romain Guy | d7fa122 | 2009-10-09 16:05:25 -0700 | [diff] [blame] | 654 | // vec4 |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 655 | { "vec4Norm", (void *)&SC_vec4Norm }, |
| 656 | { "vec4Length", (void *)&SC_vec4Length }, |
| 657 | { "vec4Add", (void *)&SC_vec4Add }, |
| 658 | { "vec4Sub", (void *)&SC_vec4Sub }, |
| 659 | { "vec4Dot", (void *)&SC_vec4Dot }, |
| 660 | { "vec4Scale", (void *)&SC_vec4Scale }, |
Romain Guy | d7fa122 | 2009-10-09 16:05:25 -0700 | [diff] [blame] | 661 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 662 | // allocation |
| 663 | { "allocGetDimX", (void *)&SC_allocGetDimX }, |
| 664 | { "allocGetDimY", (void *)&SC_allocGetDimY }, |
| 665 | { "allocGetDimZ", (void *)&SC_allocGetDimZ }, |
| 666 | { "allocGetDimLOD", (void *)&SC_allocGetDimLOD }, |
| 667 | { "allocGetDimFaces", (void *)&SC_allocGetDimFaces }, |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 668 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 669 | |
| 670 | // misc |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 671 | { "sendToClient", (void *)&SC_toClient }, |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 672 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 673 | { "_Z18convertColorTo8888fff", (void *)&SC_convertColorTo8888_f3 }, |
| 674 | { "_Z18convertColorTo8888ffff", (void *)&SC_convertColorTo8888_f4 }, |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 675 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 676 | { "debugF", (void *)&SC_debugF }, |
| 677 | { "debugI32", (void *)&SC_debugI32 }, |
| 678 | { "debugHexF", (void *)&SC_debugHexF }, |
| 679 | { "debugHexI32", (void *)&SC_debugHexI32 }, |
| 680 | { "debugP", (void *)&SC_debugP }, |
| 681 | { "debugPf", (void *)&SC_debugPf }, |
| 682 | { "debugPi", (void *)&SC_debugPi }, |
Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 683 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 684 | { "scriptCall", (void *)&SC_scriptCall }, |
Jason Sams | 1de0b87 | 2010-05-17 14:55:34 -0700 | [diff] [blame] | 685 | { "rsGetAllocation", (void *)&SC_getAllocation }, |
| 686 | |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 687 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 688 | { NULL, NULL } |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 689 | }; |
| 690 | |
| 691 | const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym) |
| 692 | { |
| 693 | ScriptCState::SymbolTable_t *syms = gSyms; |
| 694 | |
| 695 | while (syms->mPtr) { |
| 696 | if (!strcmp(syms->mName, sym)) { |
| 697 | return syms; |
| 698 | } |
| 699 | syms++; |
| 700 | } |
| 701 | return NULL; |
| 702 | } |
| 703 | |