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" |
| 20 | |
| 21 | #include "acc/acc.h" |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 22 | #include "utils/Timers.h" |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 23 | |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 24 | #include <time.h> |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 25 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 26 | using namespace android; |
| 27 | using namespace android::renderscript; |
| 28 | |
| 29 | #define GET_TLS() Context::ScriptTLSStruct * tls = \ |
| 30 | (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \ |
| 31 | Context * rsc = tls->mContext; \ |
| 32 | ScriptC * sc = (ScriptC *) tls->mScript |
| 33 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 34 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 35 | |
| 36 | |
| 37 | ////////////////////////////////////////////////////////////////////////////// |
| 38 | // Non-Updated code below |
| 39 | ////////////////////////////////////////////////////////////////////////////// |
| 40 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 41 | typedef struct { |
| 42 | float x; |
| 43 | float y; |
| 44 | float z; |
| 45 | } vec3_t; |
| 46 | |
| 47 | typedef struct { |
| 48 | float x; |
| 49 | float y; |
| 50 | float z; |
| 51 | float w; |
| 52 | } vec4_t; |
| 53 | |
| 54 | typedef struct { |
| 55 | float x; |
| 56 | float y; |
| 57 | } vec2_t; |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 58 | |
Romain Guy | f8e136d | 2009-08-06 12:40:41 -0700 | [diff] [blame] | 59 | |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 60 | ////////////////////////////////////////////////////////////////////////////// |
| 61 | // Vec3 routines |
| 62 | ////////////////////////////////////////////////////////////////////////////// |
| 63 | |
| 64 | static void SC_vec3Norm(vec3_t *v) |
| 65 | { |
| 66 | float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z); |
| 67 | len = 1 / len; |
| 68 | v->x *= len; |
| 69 | v->y *= len; |
| 70 | v->z *= len; |
| 71 | } |
| 72 | |
| 73 | static float SC_vec3Length(const vec3_t *v) |
| 74 | { |
| 75 | return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z); |
| 76 | } |
| 77 | |
| 78 | static void SC_vec3Add(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs) |
| 79 | { |
| 80 | dest->x = lhs->x + rhs->x; |
| 81 | dest->y = lhs->y + rhs->y; |
| 82 | dest->z = lhs->z + rhs->z; |
| 83 | } |
| 84 | |
| 85 | static void SC_vec3Sub(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs) |
| 86 | { |
| 87 | dest->x = lhs->x - rhs->x; |
| 88 | dest->y = lhs->y - rhs->y; |
| 89 | dest->z = lhs->z - rhs->z; |
| 90 | } |
| 91 | |
| 92 | static void SC_vec3Cross(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs) |
| 93 | { |
| 94 | float x = lhs->y * rhs->z - lhs->z * rhs->y; |
| 95 | float y = lhs->z * rhs->x - lhs->x * rhs->z; |
| 96 | float z = lhs->x * rhs->y - lhs->y * rhs->x; |
| 97 | dest->x = x; |
| 98 | dest->y = y; |
| 99 | dest->z = z; |
| 100 | } |
| 101 | |
| 102 | static float SC_vec3Dot(const vec3_t *lhs, const vec3_t *rhs) |
| 103 | { |
| 104 | return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z; |
| 105 | } |
| 106 | |
| 107 | static void SC_vec3Scale(vec3_t *lhs, float scale) |
| 108 | { |
| 109 | lhs->x *= scale; |
| 110 | lhs->y *= scale; |
| 111 | lhs->z *= scale; |
| 112 | } |
| 113 | |
Romain Guy | d7fa122 | 2009-10-09 16:05:25 -0700 | [diff] [blame] | 114 | ////////////////////////////////////////////////////////////////////////////// |
| 115 | // Vec4 routines |
| 116 | ////////////////////////////////////////////////////////////////////////////// |
| 117 | |
| 118 | static void SC_vec4Norm(vec4_t *v) |
| 119 | { |
| 120 | float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w); |
| 121 | len = 1 / len; |
| 122 | v->x *= len; |
| 123 | v->y *= len; |
| 124 | v->z *= len; |
| 125 | v->w *= len; |
| 126 | } |
| 127 | |
| 128 | static float SC_vec4Length(const vec4_t *v) |
| 129 | { |
| 130 | return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w); |
| 131 | } |
| 132 | |
| 133 | static void SC_vec4Add(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs) |
| 134 | { |
| 135 | dest->x = lhs->x + rhs->x; |
| 136 | dest->y = lhs->y + rhs->y; |
| 137 | dest->z = lhs->z + rhs->z; |
| 138 | dest->w = lhs->w + rhs->w; |
| 139 | } |
| 140 | |
| 141 | static void SC_vec4Sub(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs) |
| 142 | { |
| 143 | dest->x = lhs->x - rhs->x; |
| 144 | dest->y = lhs->y - rhs->y; |
| 145 | dest->z = lhs->z - rhs->z; |
| 146 | dest->w = lhs->w - rhs->w; |
| 147 | } |
| 148 | |
| 149 | static float SC_vec4Dot(const vec4_t *lhs, const vec4_t *rhs) |
| 150 | { |
| 151 | return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z + lhs->w * rhs->w; |
| 152 | } |
| 153 | |
| 154 | static void SC_vec4Scale(vec4_t *lhs, float scale) |
| 155 | { |
| 156 | lhs->x *= scale; |
| 157 | lhs->y *= scale; |
| 158 | lhs->z *= scale; |
| 159 | lhs->w *= scale; |
| 160 | } |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 161 | |
| 162 | ////////////////////////////////////////////////////////////////////////////// |
| 163 | // Math routines |
| 164 | ////////////////////////////////////////////////////////////////////////////// |
| 165 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 166 | static float SC_sinf_fast(float x) |
| 167 | { |
| 168 | const float A = 1.0f / (2.0f * M_PI); |
| 169 | const float B = -16.0f; |
| 170 | const float C = 8.0f; |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 171 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 172 | // scale angle for easy argument reduction |
| 173 | x *= A; |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 174 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 175 | if (fabsf(x) >= 0.5f) { |
| 176 | // argument reduction |
| 177 | x = x - ceilf(x + 0.5f) + 1.0f; |
| 178 | } |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 179 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 180 | const float y = B * x * fabsf(x) + C * x; |
| 181 | return 0.2215f * (y * fabsf(y) - y) + y; |
| 182 | } |
| 183 | |
| 184 | static float SC_cosf_fast(float x) |
| 185 | { |
| 186 | x += float(M_PI / 2); |
| 187 | |
| 188 | const float A = 1.0f / (2.0f * M_PI); |
| 189 | const float B = -16.0f; |
| 190 | const float C = 8.0f; |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 191 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 192 | // scale angle for easy argument reduction |
| 193 | x *= A; |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 194 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 195 | if (fabsf(x) >= 0.5f) { |
| 196 | // argument reduction |
| 197 | x = x - ceilf(x + 0.5f) + 1.0f; |
| 198 | } |
Jason Sams | ea84a7c | 2009-09-04 14:42:41 -0700 | [diff] [blame] | 199 | |
Romain Guy | cac80a6 | 2009-08-18 11:39:17 -0700 | [diff] [blame] | 200 | const float y = B * x * fabsf(x) + C * x; |
| 201 | return 0.2215f * (y * fabsf(y) - y) + y; |
| 202 | } |
| 203 | |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 204 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 205 | static float SC_randf(float max) |
| 206 | { |
| 207 | float r = (float)rand(); |
| 208 | return r / RAND_MAX * max; |
| 209 | } |
| 210 | |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 211 | static float SC_randf2(float min, float max) |
| 212 | { |
| 213 | float r = (float)rand(); |
| 214 | return r / RAND_MAX * (max - min) + min; |
| 215 | } |
| 216 | |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 217 | static int SC_randi(int max) |
| 218 | { |
| 219 | return (int)SC_randf(max); |
| 220 | } |
| 221 | |
| 222 | static int SC_randi2(int min, int max) |
| 223 | { |
| 224 | return (int)SC_randf2(min, max); |
| 225 | } |
| 226 | |
Romain Guy | a9d2d5e | 2009-08-09 17:04:54 -0700 | [diff] [blame] | 227 | static int SC_clamp(int amount, int low, int high) |
| 228 | { |
| 229 | return amount < low ? low : (amount > high ? high : amount); |
| 230 | } |
| 231 | |
Jason Sams | d342fd7 | 2009-09-18 14:24:24 -0700 | [diff] [blame] | 232 | static float SC_roundf(float v) |
| 233 | { |
| 234 | return floorf(v + 0.4999999999); |
| 235 | } |
| 236 | |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 237 | static float SC_distf2(float x1, float y1, float x2, float y2) |
| 238 | { |
| 239 | float x = x2 - x1; |
| 240 | float y = y2 - y1; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 241 | return sqrtf(x * x + y * y); |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2) |
| 245 | { |
| 246 | float x = x2 - x1; |
| 247 | float y = y2 - y1; |
| 248 | float z = z2 - z1; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 249 | return sqrtf(x * x + y * y + z * z); |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static float SC_magf2(float a, float b) |
| 253 | { |
| 254 | return sqrtf(a * a + b * b); |
| 255 | } |
| 256 | |
| 257 | static float SC_magf3(float a, float b, float c) |
| 258 | { |
| 259 | return sqrtf(a * a + b * b + c * c); |
| 260 | } |
| 261 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 262 | static float SC_frac(float v) |
| 263 | { |
| 264 | int i = (int)floor(v); |
| 265 | return fmin(v - i, 0x1.fffffep-1f); |
| 266 | } |
| 267 | |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 268 | ////////////////////////////////////////////////////////////////////////////// |
| 269 | // Time routines |
| 270 | ////////////////////////////////////////////////////////////////////////////// |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 271 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 272 | static int32_t SC_second() |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 273 | { |
| 274 | GET_TLS(); |
| 275 | |
| 276 | time_t rawtime; |
| 277 | time(&rawtime); |
| 278 | |
Romain Guy | baed727 | 2009-11-11 15:36:06 -0800 | [diff] [blame] | 279 | struct tm *timeinfo; |
| 280 | timeinfo = localtime(&rawtime); |
| 281 | return timeinfo->tm_sec; |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 284 | static int32_t SC_minute() |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 285 | { |
| 286 | GET_TLS(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 287 | |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 288 | time_t rawtime; |
| 289 | time(&rawtime); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 290 | |
Romain Guy | baed727 | 2009-11-11 15:36:06 -0800 | [diff] [blame] | 291 | struct tm *timeinfo; |
| 292 | timeinfo = localtime(&rawtime); |
| 293 | return timeinfo->tm_min; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 294 | } |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 295 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 296 | static int32_t SC_hour() |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 297 | { |
| 298 | GET_TLS(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 299 | |
Romain Guy | 584a375 | 2009-07-30 18:45:01 -0700 | [diff] [blame] | 300 | time_t rawtime; |
| 301 | time(&rawtime); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 302 | |
Romain Guy | baed727 | 2009-11-11 15:36:06 -0800 | [diff] [blame] | 303 | struct tm *timeinfo; |
| 304 | timeinfo = localtime(&rawtime); |
| 305 | return timeinfo->tm_hour; |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 308 | static int32_t SC_day() |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 309 | { |
| 310 | GET_TLS(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 311 | |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 312 | time_t rawtime; |
| 313 | time(&rawtime); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 314 | |
Romain Guy | baed727 | 2009-11-11 15:36:06 -0800 | [diff] [blame] | 315 | struct tm *timeinfo; |
| 316 | timeinfo = localtime(&rawtime); |
| 317 | return timeinfo->tm_mday; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 318 | } |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 319 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 320 | static int32_t SC_month() |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 321 | { |
| 322 | GET_TLS(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 323 | |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 324 | time_t rawtime; |
| 325 | time(&rawtime); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 326 | |
Romain Guy | baed727 | 2009-11-11 15:36:06 -0800 | [diff] [blame] | 327 | struct tm *timeinfo; |
| 328 | timeinfo = localtime(&rawtime); |
| 329 | return timeinfo->tm_mon; |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 330 | } |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 331 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 332 | static int32_t SC_year() |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 333 | { |
| 334 | GET_TLS(); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 335 | |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 336 | time_t rawtime; |
| 337 | time(&rawtime); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 338 | |
Romain Guy | baed727 | 2009-11-11 15:36:06 -0800 | [diff] [blame] | 339 | struct tm *timeinfo; |
| 340 | timeinfo = localtime(&rawtime); |
| 341 | return timeinfo->tm_year; |
Romain Guy | 8839ca5 | 2009-07-31 11:20:59 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 344 | static int64_t SC_uptimeMillis2() |
| 345 | { |
| 346 | return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); |
| 347 | } |
| 348 | |
| 349 | static int64_t SC_startTimeMillis2() |
| 350 | { |
| 351 | GET_TLS(); |
| 352 | return sc->mEnviroment.mStartTimeMillis; |
| 353 | } |
| 354 | |
| 355 | static int64_t SC_elapsedTimeMillis2() |
| 356 | { |
| 357 | GET_TLS(); |
| 358 | return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 359 | - sc->mEnviroment.mStartTimeMillis; |
| 360 | } |
| 361 | |
Joe Onorato | 3370ec9 | 2009-08-09 11:39:02 -0700 | [diff] [blame] | 362 | static int32_t SC_uptimeMillis() |
| 363 | { |
| 364 | return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)); |
| 365 | } |
| 366 | |
| 367 | static int32_t SC_startTimeMillis() |
| 368 | { |
| 369 | GET_TLS(); |
| 370 | return sc->mEnviroment.mStartTimeMillis; |
| 371 | } |
| 372 | |
| 373 | static int32_t SC_elapsedTimeMillis() |
| 374 | { |
| 375 | GET_TLS(); |
| 376 | return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC)) |
| 377 | - sc->mEnviroment.mStartTimeMillis; |
| 378 | } |
| 379 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 380 | ////////////////////////////////////////////////////////////////////////////// |
| 381 | // Matrix routines |
| 382 | ////////////////////////////////////////////////////////////////////////////// |
| 383 | |
| 384 | |
| 385 | static void SC_matrixLoadIdentity(rsc_Matrix *mat) |
| 386 | { |
| 387 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 388 | m->loadIdentity(); |
| 389 | } |
| 390 | |
| 391 | static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f) |
| 392 | { |
| 393 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 394 | m->load(f); |
| 395 | } |
| 396 | |
| 397 | static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat) |
| 398 | { |
| 399 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 400 | m->load(reinterpret_cast<const Matrix *>(newmat)); |
| 401 | } |
| 402 | |
| 403 | static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z) |
| 404 | { |
| 405 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 406 | m->loadRotate(rot, x, y, z); |
| 407 | } |
| 408 | |
| 409 | static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z) |
| 410 | { |
| 411 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 412 | m->loadScale(x, y, z); |
| 413 | } |
| 414 | |
| 415 | static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z) |
| 416 | { |
| 417 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 418 | m->loadTranslate(x, y, z); |
| 419 | } |
| 420 | |
| 421 | static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs) |
| 422 | { |
| 423 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 424 | m->loadMultiply(reinterpret_cast<const Matrix *>(lhs), |
| 425 | reinterpret_cast<const Matrix *>(rhs)); |
| 426 | } |
| 427 | |
| 428 | static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs) |
| 429 | { |
| 430 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 431 | m->multiply(reinterpret_cast<const Matrix *>(rhs)); |
| 432 | } |
| 433 | |
| 434 | static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z) |
| 435 | { |
| 436 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 437 | m->rotate(rot, x, y, z); |
| 438 | } |
| 439 | |
| 440 | static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z) |
| 441 | { |
| 442 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 443 | m->scale(x, y, z); |
| 444 | } |
| 445 | |
| 446 | static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z) |
| 447 | { |
| 448 | Matrix *m = reinterpret_cast<Matrix *>(mat); |
| 449 | m->translate(x, y, z); |
| 450 | } |
| 451 | |
| 452 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 453 | ////////////////////////////////////////////////////////////////////////////// |
| 454 | // |
| 455 | ////////////////////////////////////////////////////////////////////////////// |
| 456 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 457 | static uint32_t SC_allocGetDimX(RsAllocation va) |
| 458 | { |
| 459 | GET_TLS(); |
| 460 | const Allocation *a = static_cast<const Allocation *>(va); |
| 461 | //LOGE("SC_allocGetDimX a=%p", a); |
| 462 | //LOGE(" type=%p", a->getType()); |
| 463 | return a->getType()->getDimX(); |
| 464 | } |
| 465 | |
| 466 | static uint32_t SC_allocGetDimY(RsAllocation va) |
| 467 | { |
| 468 | GET_TLS(); |
| 469 | const Allocation *a = static_cast<const Allocation *>(va); |
| 470 | return a->getType()->getDimY(); |
| 471 | } |
| 472 | |
| 473 | static uint32_t SC_allocGetDimZ(RsAllocation va) |
| 474 | { |
| 475 | GET_TLS(); |
| 476 | const Allocation *a = static_cast<const Allocation *>(va); |
| 477 | return a->getType()->getDimZ(); |
| 478 | } |
| 479 | |
| 480 | static uint32_t SC_allocGetDimLOD(RsAllocation va) |
| 481 | { |
| 482 | GET_TLS(); |
| 483 | const Allocation *a = static_cast<const Allocation *>(va); |
| 484 | return a->getType()->getDimLOD(); |
| 485 | } |
| 486 | |
| 487 | static uint32_t SC_allocGetDimFaces(RsAllocation va) |
| 488 | { |
| 489 | GET_TLS(); |
| 490 | const Allocation *a = static_cast<const Allocation *>(va); |
| 491 | return a->getType()->getDimFaces(); |
| 492 | } |
| 493 | |
| 494 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 495 | |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 496 | static void SC_debugF(const char *s, float f) { |
| 497 | LOGE("%s %f, 0x%08x", s, f, *((int *) (&f))); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 498 | } |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 499 | static void SC_debugFv2(const char *s, rsvF_2 fv) { |
| 500 | float *f = (float *)&fv; |
| 501 | LOGE("%s {%f, %f}", s, f[0], f[1]); |
Romain Guy | d22fff7 | 2009-08-20 17:08:33 -0700 | [diff] [blame] | 502 | } |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 503 | static void SC_debugFv3(const char *s, rsvF_4 fv) { |
| 504 | float *f = (float *)&fv; |
| 505 | LOGE("%s {%f, %f, %f}", s, f[0], f[1], f[2]); |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 506 | } |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 507 | static void SC_debugFv4(const char *s, rsvF_4 fv) { |
| 508 | float *f = (float *)&fv; |
| 509 | LOGE("%s {%f, %f, %f, %f}", s, f[0], f[1], f[2], f[3]); |
| 510 | } |
| 511 | static void SC_debugI32(const char *s, int32_t i) { |
| 512 | LOGE("%s %i 0x%x", s, i, i); |
Romain Guy | d22fff7 | 2009-08-20 17:08:33 -0700 | [diff] [blame] | 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 | int SC_divsi3(int a, int b) |
| 547 | { |
| 548 | return a / b; |
| 549 | } |
Jason Sams | bd2197f | 2009-10-07 18:14:01 -0700 | [diff] [blame] | 550 | |
Jason Sams | 1de0b87 | 2010-05-17 14:55:34 -0700 | [diff] [blame] | 551 | int SC_getAllocation(const void *ptr) |
| 552 | { |
| 553 | GET_TLS(); |
| 554 | const Allocation *alloc = sc->ptrToAllocation(ptr); |
| 555 | return (int)alloc; |
| 556 | } |
| 557 | |
| 558 | |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 559 | ////////////////////////////////////////////////////////////////////////////// |
| 560 | // Class implementation |
| 561 | ////////////////////////////////////////////////////////////////////////////// |
| 562 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 563 | // llvm name mangling ref |
| 564 | // <builtin-type> ::= v # void |
| 565 | // ::= b # bool |
| 566 | // ::= c # char |
| 567 | // ::= a # signed char |
| 568 | // ::= h # unsigned char |
| 569 | // ::= s # short |
| 570 | // ::= t # unsigned short |
| 571 | // ::= i # int |
| 572 | // ::= j # unsigned int |
| 573 | // ::= l # long |
| 574 | // ::= m # unsigned long |
| 575 | // ::= x # long long, __int64 |
| 576 | // ::= y # unsigned long long, __int64 |
| 577 | // ::= f # float |
| 578 | // ::= d # double |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 579 | |
Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 580 | static ScriptCState::SymbolTable_t gSyms[] = { |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 581 | { "__divsi3", (void *)&SC_divsi3 }, |
| 582 | |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 583 | // allocation |
| 584 | { "rsAllocationGetDimX", (void *)&SC_allocGetDimX }, |
| 585 | { "rsAllocationGetDimY", (void *)&SC_allocGetDimY }, |
| 586 | { "rsAllocationGetDimZ", (void *)&SC_allocGetDimZ }, |
| 587 | { "rsAllocationGetDimLOD", (void *)&SC_allocGetDimLOD }, |
| 588 | { "rsAllocationGetDimFaces", (void *)&SC_allocGetDimFaces }, |
| 589 | { "rsGetAllocation", (void *)&SC_getAllocation }, |
| 590 | |
| 591 | // color |
| 592 | { "_Z17rsPackColorTo8888fff", (void *)&SC_convertColorTo8888_f3 }, |
| 593 | { "_Z17rsPackColorTo8888ffff", (void *)&SC_convertColorTo8888_f4 }, |
| 594 | //extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float3); |
| 595 | //extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float4); |
| 596 | //extern float4 rsUnpackColor8888(uchar4); |
| 597 | //extern uchar4 __attribute__((overloadable)) rsPackColorTo565(float r, float g, float b); |
| 598 | //extern uchar4 __attribute__((overloadable)) rsPackColorTo565(float3); |
| 599 | //extern float4 rsUnpackColor565(uchar4); |
| 600 | |
| 601 | // Debug |
| 602 | { "_Z7rsDebugPKcf", (void *)&SC_debugF }, |
| 603 | { "_Z7rsDebugPKcDv2_f", (void *)&SC_debugFv2 }, |
| 604 | { "_Z7rsDebugPKcDv3_f", (void *)&SC_debugFv3 }, |
| 605 | { "_Z7rsDebugPKcDv4_f", (void *)&SC_debugFv4 }, |
| 606 | { "_Z7rsDebugPKci", (void *)&SC_debugI32 }, |
| 607 | //extern void __attribute__((overloadable))rsDebug(const char *, const void *); |
| 608 | |
| 609 | |
| 610 | // RS Math |
| 611 | { "_Z6rsRandi", (void *)&SC_randi }, |
| 612 | { "_Z6rsRandii", (void *)&SC_randi2 }, |
| 613 | { "_Z6rsRandf", (void *)&SC_randf }, |
| 614 | { "_Z6rsRandff", (void *)&SC_randf2 }, |
| 615 | { "_Z6rsFracf", (void *)&SC_frac }, |
| 616 | |
| 617 | // time |
| 618 | { "rsSecond", (void *)&SC_second }, |
| 619 | { "rsMinute", (void *)&SC_minute }, |
| 620 | { "rsHour", (void *)&SC_hour }, |
| 621 | { "rsDay", (void *)&SC_day }, |
| 622 | { "rsMonth", (void *)&SC_month }, |
| 623 | { "rsYear", (void *)&SC_year }, |
| 624 | { "rsUptimeMillis", (void*)&SC_uptimeMillis2 }, |
| 625 | { "rsStartTimeMillis", (void*)&SC_startTimeMillis2 }, |
| 626 | { "rsElapsedTimeMillis", (void*)&SC_elapsedTimeMillis2 }, |
| 627 | |
| 628 | { "rsSendToClient", (void *)&SC_toClient }, |
| 629 | |
| 630 | // matrix |
| 631 | { "rsMatrixLoadIdentity", (void *)&SC_matrixLoadIdentity }, |
| 632 | { "rsMatrixLoadFloat", (void *)&SC_matrixLoadFloat }, |
| 633 | { "rsMatrixLoadMat", (void *)&SC_matrixLoadMat }, |
| 634 | { "rsMatrixLoadRotate", (void *)&SC_matrixLoadRotate }, |
| 635 | { "rsMatrixLoadScale", (void *)&SC_matrixLoadScale }, |
| 636 | { "rsMatrixLoadTranslate", (void *)&SC_matrixLoadTranslate }, |
| 637 | { "rsMatrixLoadMultiply", (void *)&SC_matrixLoadMultiply }, |
| 638 | { "rsMatrixMultiply", (void *)&SC_matrixMultiply }, |
| 639 | { "rsMatrixRotate", (void *)&SC_matrixRotate }, |
| 640 | { "rsMatrixScale", (void *)&SC_matrixScale }, |
| 641 | { "rsMatrixTranslate", (void *)&SC_matrixTranslate }, |
| 642 | |
| 643 | |
| 644 | //////////////////////////////////////////////////////////////////// |
| 645 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 646 | //{ "sinf_fast", (void *)&SC_sinf_fast }, |
| 647 | //{ "cosf_fast", (void *)&SC_cosf_fast }, |
Jason Sams | d79b2e9 | 2010-05-19 17:22:57 -0700 | [diff] [blame] | 648 | //{ "clamp", (void *)&SC_clamp }, |
| 649 | //{ "distf2", (void *)&SC_distf2 }, |
| 650 | //{ "distf3", (void *)&SC_distf3 }, |
| 651 | //{ "magf2", (void *)&SC_magf2 }, |
| 652 | //{ "magf3", (void *)&SC_magf3 }, |
| 653 | //{ "mapf", (void *)&SC_mapf }, |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 654 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 655 | { "scriptCall", (void *)&SC_scriptCall }, |
Jason Sams | 1de0b87 | 2010-05-17 14:55:34 -0700 | [diff] [blame] | 656 | |
Jason Sams | b0ec1b4 | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 657 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 658 | { NULL, NULL } |
Jason Sams | c97bb88 | 2009-07-20 14:31:06 -0700 | [diff] [blame] | 659 | }; |
| 660 | |
| 661 | const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym) |
| 662 | { |
| 663 | ScriptCState::SymbolTable_t *syms = gSyms; |
| 664 | |
| 665 | while (syms->mPtr) { |
| 666 | if (!strcmp(syms->mName, sym)) { |
| 667 | return syms; |
| 668 | } |
| 669 | syms++; |
| 670 | } |
| 671 | return NULL; |
| 672 | } |
| 673 | |