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 | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame^] | 205 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 206 | static float SC_randf(float max) |
| 207 | { |
| 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 | |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame^] | 218 | static int SC_randi(int max) |
| 219 | { |
| 220 | return (int)SC_randf(max); |
| 221 | } |
| 222 | |
| 223 | static int SC_randi2(int min, int max) |
| 224 | { |
| 225 | return (int)SC_randf2(min, max); |
| 226 | } |
| 227 | |
Romain Guy | d7fa122 | 2009-10-09 16:05:25 -0700 | [diff] [blame] | 228 | static int SC_sign(int value) |
| 229 | { |
| 230 | return (value > 0) - (value < 0); |
| 231 | } |
| 232 | |
Romain Guy | a9d2d5e | 2009-08-09 17:04:54 -0700 | [diff] [blame] | 233 | static int SC_clamp(int amount, int low, int high) |
| 234 | { |
| 235 | return amount < low ? low : (amount > high ? high : amount); |
| 236 | } |
| 237 | |
Jason Sams | d342fd7 | 2009-09-18 14:24:24 -0700 | [diff] [blame] | 238 | static float SC_roundf(float v) |
| 239 | { |
| 240 | return floorf(v + 0.4999999999); |
| 241 | } |
| 242 | |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 243 | static float SC_distf2(float x1, float y1, float x2, float y2) |
| 244 | { |
| 245 | float x = x2 - x1; |
| 246 | float y = y2 - y1; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 247 | return sqrtf(x * x + y * y); |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2) |
| 251 | { |
| 252 | float x = x2 - x1; |
| 253 | float y = y2 - y1; |
| 254 | float z = z2 - z1; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 255 | return sqrtf(x * x + y * y + z * z); |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static float SC_magf2(float a, float b) |
| 259 | { |
| 260 | return sqrtf(a * a + b * b); |
| 261 | } |
| 262 | |
| 263 | static float SC_magf3(float a, float b, float c) |
| 264 | { |
| 265 | return sqrtf(a * a + b * b + c * c); |
| 266 | } |
| 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 | |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame^] | 350 | static int64_t SC_uptimeMillis2() |
| 351 | { |
| 352 | return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); |
| 353 | } |
| 354 | |
| 355 | static int64_t SC_startTimeMillis2() |
| 356 | { |
| 357 | GET_TLS(); |
| 358 | return sc->mEnviroment.mStartTimeMillis; |
| 359 | } |
| 360 | |
| 361 | static int64_t SC_elapsedTimeMillis2() |
| 362 | { |
| 363 | GET_TLS(); |
| 364 | return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 365 | - sc->mEnviroment.mStartTimeMillis; |
| 366 | } |
| 367 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 368 | static int32_t SC_uptimeMillis() |
| 369 | { |
| 370 | return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); |
| 371 | } |
| 372 | |
| 373 | static int32_t SC_startTimeMillis() |
| 374 | { |
| 375 | GET_TLS(); |
| 376 | return sc->mEnviroment.mStartTimeMillis; |
| 377 | } |
| 378 | |
| 379 | static int32_t SC_elapsedTimeMillis() |
| 380 | { |
| 381 | GET_TLS(); |
| 382 | return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 383 | - sc->mEnviroment.mStartTimeMillis; |
| 384 | } |
| 385 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 386 | ////////////////////////////////////////////////////////////////////////////// |
| 387 | // Matrix routines |
| 388 | ////////////////////////////////////////////////////////////////////////////// |
| 389 | |
| 390 | |
| 391 | static void SC_matrixLoadIdentity(rsc_Matrix *mat) |
| 392 | { |
| 393 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 394 | m->loadIdentity(); |
| 395 | } |
| 396 | |
| 397 | static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f) |
| 398 | { |
| 399 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 400 | m->load(f); |
| 401 | } |
| 402 | |
| 403 | static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat) |
| 404 | { |
| 405 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 406 | m->load(reinterpret_cast<const Matrix *>(newmat)); |
| 407 | } |
| 408 | |
| 409 | static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z) |
| 410 | { |
| 411 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 412 | m->loadRotate(rot, x, y, z); |
| 413 | } |
| 414 | |
| 415 | static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z) |
| 416 | { |
| 417 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 418 | m->loadScale(x, y, z); |
| 419 | } |
| 420 | |
| 421 | static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z) |
| 422 | { |
| 423 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 424 | m->loadTranslate(x, y, z); |
| 425 | } |
| 426 | |
| 427 | static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs) |
| 428 | { |
| 429 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 430 | m->loadMultiply(reinterpret_cast<const Matrix *>(lhs), |
| 431 | reinterpret_cast<const Matrix *>(rhs)); |
| 432 | } |
| 433 | |
| 434 | static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs) |
| 435 | { |
| 436 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 437 | m->multiply(reinterpret_cast<const Matrix *>(rhs)); |
| 438 | } |
| 439 | |
| 440 | static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z) |
| 441 | { |
| 442 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 443 | m->rotate(rot, x, y, z); |
| 444 | } |
| 445 | |
| 446 | static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z) |
| 447 | { |
| 448 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 449 | m->scale(x, y, z); |
| 450 | } |
| 451 | |
| 452 | static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z) |
| 453 | { |
| 454 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 455 | m->translate(x, y, z); |
| 456 | } |
| 457 | |
| 458 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 459 | ////////////////////////////////////////////////////////////////////////////// |
| 460 | // |
| 461 | ////////////////////////////////////////////////////////////////////////////// |
| 462 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 463 | static uint32_t SC_allocGetDimX(RsAllocation va) |
| 464 | { |
| 465 | GET_TLS(); |
| 466 | const Allocation *a = static_cast<const Allocation *>(va); |
| 467 | //LOGE("SC_allocGetDimX a=%p", a); |
| 468 | //LOGE(" type=%p", a->getType()); |
| 469 | return a->getType()->getDimX(); |
| 470 | } |
| 471 | |
| 472 | static uint32_t SC_allocGetDimY(RsAllocation va) |
| 473 | { |
| 474 | GET_TLS(); |
| 475 | const Allocation *a = static_cast<const Allocation *>(va); |
| 476 | return a->getType()->getDimY(); |
| 477 | } |
| 478 | |
| 479 | static uint32_t SC_allocGetDimZ(RsAllocation va) |
| 480 | { |
| 481 | GET_TLS(); |
| 482 | const Allocation *a = static_cast<const Allocation *>(va); |
| 483 | return a->getType()->getDimZ(); |
| 484 | } |
| 485 | |
| 486 | static uint32_t SC_allocGetDimLOD(RsAllocation va) |
| 487 | { |
| 488 | GET_TLS(); |
| 489 | const Allocation *a = static_cast<const Allocation *>(va); |
| 490 | return a->getType()->getDimLOD(); |
| 491 | } |
| 492 | |
| 493 | static uint32_t SC_allocGetDimFaces(RsAllocation va) |
| 494 | { |
| 495 | GET_TLS(); |
| 496 | const Allocation *a = static_cast<const Allocation *>(va); |
| 497 | return a->getType()->getDimFaces(); |
| 498 | } |
| 499 | |
| 500 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 501 | |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame^] | 502 | static void SC_debugF(const char *s, float f) { |
| 503 | LOGE("%s %f, 0x%08x", s, f, *((int *) (&f))); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 504 | } |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame^] | 505 | static void SC_debugFv2(const char *s, rsvF_2 fv) { |
| 506 | float *f = (float *)&fv; |
| 507 | LOGE("%s {%f, %f}", s, f[0], f[1]); |
Romain Guy | d22fff7 | 2009-08-20 17:08:33 -0700 | [diff] [blame] | 508 | } |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame^] | 509 | static void SC_debugFv3(const char *s, rsvF_4 fv) { |
| 510 | float *f = (float *)&fv; |
| 511 | LOGE("%s {%f, %f, %f}", s, f[0], f[1], f[2]); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 512 | } |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame^] | 513 | static void SC_debugFv4(const char *s, rsvF_4 fv) { |
| 514 | float *f = (float *)&fv; |
| 515 | LOGE("%s {%f, %f, %f, %f}", s, f[0], f[1], f[2], f[3]); |
| 516 | } |
| 517 | static void SC_debugI32(const char *s, int32_t i) { |
| 518 | LOGE("%s %i 0x%x", s, i, i); |
Romain Guy | d22fff7 | 2009-08-20 17:08:33 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 521 | static uchar4 SC_convertColorTo8888_f3(float r, float g, float b) { |
| 522 | uchar4 t; |
| 523 | t.f[0] = (uint8_t)(r * 255.f); |
| 524 | t.f[1] = (uint8_t)(g * 255.f); |
| 525 | t.f[2] = (uint8_t)(b * 255.f); |
| 526 | t.f[3] = 0xff; |
| 527 | return t; |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 530 | static uchar4 SC_convertColorTo8888_f4(float r, float g, float b, float a) { |
| 531 | uchar4 t; |
| 532 | t.f[0] = (uint8_t)(r * 255.f); |
| 533 | t.f[1] = (uint8_t)(g * 255.f); |
| 534 | t.f[2] = (uint8_t)(b * 255.f); |
| 535 | t.f[3] = (uint8_t)(a * 255.f); |
| 536 | return t; |
Jason Sams | 334ea0c | 2009-08-17 13:56:09 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 539 | static uint32_t SC_toClient(void *data, int cmdID, int len, int waitForSpace) |
| 540 | { |
| 541 | GET_TLS(); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 542 | //LOGE("SC_toClient %i %i %i", cmdID, len, waitForSpace); |
Jason Sams | 516c319 | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 543 | return rsc->sendMessageToClient(data, cmdID, len, waitForSpace != 0); |
| 544 | } |
| 545 | |
Jason Sams | bd2197f | 2009-10-07 18:14:01 -0700 | [diff] [blame] | 546 | static void SC_scriptCall(int scriptID) |
| 547 | { |
| 548 | GET_TLS(); |
| 549 | rsc->runScript((Script *)scriptID, 0); |
| 550 | } |
| 551 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 552 | static void SC_debugP(int i, void *p) |
| 553 | { |
| 554 | LOGE("debug P %i %p, %i", i, p, (int)p); |
| 555 | } |
| 556 | |
| 557 | static void SC_debugPi(int i, int p) |
| 558 | { |
| 559 | LOGE("debug Pi %i 0x%08x, %i", i, p, (int)p); |
| 560 | } |
| 561 | |
| 562 | static void SC_debugPf(int i, float p) |
| 563 | { |
| 564 | LOGE("debug Pf %i %f, 0x%08x", i, p, reinterpret_cast<uint32_t *>(&p)[0]); |
| 565 | } |
| 566 | |
| 567 | int SC_divsi3(int a, int b) |
| 568 | { |
| 569 | return a / b; |
| 570 | } |
Jason Sams | bd2197f | 2009-10-07 18:14:01 -0700 | [diff] [blame] | 571 | |
Jason Sams | 1de0b87 | 2010-05-17 14:55:34 -0700 | [diff] [blame] | 572 | int SC_getAllocation(const void *ptr) |
| 573 | { |
| 574 | GET_TLS(); |
| 575 | const Allocation *alloc = sc->ptrToAllocation(ptr); |
| 576 | return (int)alloc; |
| 577 | } |
| 578 | |
| 579 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 580 | ////////////////////////////////////////////////////////////////////////////// |
| 581 | // Class implementation |
| 582 | ////////////////////////////////////////////////////////////////////////////// |
| 583 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 584 | // llvm name mangling ref |
| 585 | // <builtin-type> ::= v # void |
| 586 | // ::= b # bool |
| 587 | // ::= c # char |
| 588 | // ::= a # signed char |
| 589 | // ::= h # unsigned char |
| 590 | // ::= s # short |
| 591 | // ::= t # unsigned short |
| 592 | // ::= i # int |
| 593 | // ::= j # unsigned int |
| 594 | // ::= l # long |
| 595 | // ::= m # unsigned long |
| 596 | // ::= x # long long, __int64 |
| 597 | // ::= y # unsigned long long, __int64 |
| 598 | // ::= f # float |
| 599 | // ::= d # double |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 600 | |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 601 | static ScriptCState::SymbolTable_t gSyms[] = { |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 602 | { "__divsi3", (void *)&SC_divsi3 }, |
| 603 | |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame^] | 604 | // allocation |
| 605 | { "rsAllocationGetDimX", (void *)&SC_allocGetDimX }, |
| 606 | { "rsAllocationGetDimY", (void *)&SC_allocGetDimY }, |
| 607 | { "rsAllocationGetDimZ", (void *)&SC_allocGetDimZ }, |
| 608 | { "rsAllocationGetDimLOD", (void *)&SC_allocGetDimLOD }, |
| 609 | { "rsAllocationGetDimFaces", (void *)&SC_allocGetDimFaces }, |
| 610 | { "rsGetAllocation", (void *)&SC_getAllocation }, |
| 611 | |
| 612 | // color |
| 613 | { "_Z17rsPackColorTo8888fff", (void *)&SC_convertColorTo8888_f3 }, |
| 614 | { "_Z17rsPackColorTo8888ffff", (void *)&SC_convertColorTo8888_f4 }, |
| 615 | //extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float3); |
| 616 | //extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float4); |
| 617 | //extern float4 rsUnpackColor8888(uchar4); |
| 618 | //extern uchar4 __attribute__((overloadable)) rsPackColorTo565(float r, float g, float b); |
| 619 | //extern uchar4 __attribute__((overloadable)) rsPackColorTo565(float3); |
| 620 | //extern float4 rsUnpackColor565(uchar4); |
| 621 | |
| 622 | // Debug |
| 623 | { "_Z7rsDebugPKcf", (void *)&SC_debugF }, |
| 624 | { "_Z7rsDebugPKcDv2_f", (void *)&SC_debugFv2 }, |
| 625 | { "_Z7rsDebugPKcDv3_f", (void *)&SC_debugFv3 }, |
| 626 | { "_Z7rsDebugPKcDv4_f", (void *)&SC_debugFv4 }, |
| 627 | { "_Z7rsDebugPKci", (void *)&SC_debugI32 }, |
| 628 | //extern void __attribute__((overloadable))rsDebug(const char *, const void *); |
| 629 | |
| 630 | |
| 631 | // RS Math |
| 632 | { "_Z6rsRandi", (void *)&SC_randi }, |
| 633 | { "_Z6rsRandii", (void *)&SC_randi2 }, |
| 634 | { "_Z6rsRandf", (void *)&SC_randf }, |
| 635 | { "_Z6rsRandff", (void *)&SC_randf2 }, |
| 636 | { "_Z6rsFracf", (void *)&SC_frac }, |
| 637 | |
| 638 | // time |
| 639 | { "rsSecond", (void *)&SC_second }, |
| 640 | { "rsMinute", (void *)&SC_minute }, |
| 641 | { "rsHour", (void *)&SC_hour }, |
| 642 | { "rsDay", (void *)&SC_day }, |
| 643 | { "rsMonth", (void *)&SC_month }, |
| 644 | { "rsYear", (void *)&SC_year }, |
| 645 | { "rsUptimeMillis", (void*)&SC_uptimeMillis2 }, |
| 646 | { "rsStartTimeMillis", (void*)&SC_startTimeMillis2 }, |
| 647 | { "rsElapsedTimeMillis", (void*)&SC_elapsedTimeMillis2 }, |
| 648 | |
| 649 | { "rsSendToClient", (void *)&SC_toClient }, |
| 650 | |
| 651 | // matrix |
| 652 | { "rsMatrixLoadIdentity", (void *)&SC_matrixLoadIdentity }, |
| 653 | { "rsMatrixLoadFloat", (void *)&SC_matrixLoadFloat }, |
| 654 | { "rsMatrixLoadMat", (void *)&SC_matrixLoadMat }, |
| 655 | { "rsMatrixLoadRotate", (void *)&SC_matrixLoadRotate }, |
| 656 | { "rsMatrixLoadScale", (void *)&SC_matrixLoadScale }, |
| 657 | { "rsMatrixLoadTranslate", (void *)&SC_matrixLoadTranslate }, |
| 658 | { "rsMatrixLoadMultiply", (void *)&SC_matrixLoadMultiply }, |
| 659 | { "rsMatrixMultiply", (void *)&SC_matrixMultiply }, |
| 660 | { "rsMatrixRotate", (void *)&SC_matrixRotate }, |
| 661 | { "rsMatrixScale", (void *)&SC_matrixScale }, |
| 662 | { "rsMatrixTranslate", (void *)&SC_matrixTranslate }, |
| 663 | |
| 664 | |
| 665 | //////////////////////////////////////////////////////////////////// |
| 666 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 667 | { "modf", (void *)&fmod }, |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 668 | //{ "sinf_fast", (void *)&SC_sinf_fast }, |
| 669 | //{ "cosf_fast", (void *)&SC_cosf_fast }, |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame^] | 670 | //{ "sign", (void *)&SC_sign }, |
| 671 | //{ "clamp", (void *)&SC_clamp }, |
| 672 | //{ "distf2", (void *)&SC_distf2 }, |
| 673 | //{ "distf3", (void *)&SC_distf3 }, |
| 674 | //{ "magf2", (void *)&SC_magf2 }, |
| 675 | //{ "magf3", (void *)&SC_magf3 }, |
| 676 | //{ "mapf", (void *)&SC_mapf }, |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 677 | { "noisef", (void *)&SC_noisef }, |
| 678 | { "noisef2", (void *)&SC_noisef2 }, |
| 679 | { "noisef3", (void *)&SC_noisef3 }, |
| 680 | { "turbulencef2", (void *)&SC_turbulencef2 }, |
| 681 | { "turbulencef3", (void *)&SC_turbulencef3 }, |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 682 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 683 | { "scriptCall", (void *)&SC_scriptCall }, |
Jason Sams | 1de0b87 | 2010-05-17 14:55:34 -0700 | [diff] [blame] | 684 | |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 685 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 686 | { NULL, NULL } |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 687 | }; |
| 688 | |
| 689 | const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym) |
| 690 | { |
| 691 | ScriptCState::SymbolTable_t *syms = gSyms; |
| 692 | |
| 693 | while (syms->mPtr) { |
| 694 | if (!strcmp(syms->mName, sym)) { |
| 695 | return syms; |
| 696 | } |
| 697 | syms++; |
| 698 | } |
| 699 | return NULL; |
| 700 | } |
| 701 | |