blob: 17d14f5b7204130bee54cca680a240241f5b6a36 [file] [log] [blame]
Jason Samsc97bb882009-07-20 14:31:06 -07001/*
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 Guyecc7ca032009-08-03 21:12:51 -070020#include "rsNoise.h"
Jason Samsc97bb882009-07-20 14:31:06 -070021
22#include "acc/acc.h"
Joe Onorato3370ec92009-08-09 11:39:02 -070023#include "utils/Timers.h"
Jason Samsc97bb882009-07-20 14:31:06 -070024
Jason Samse9ad9a72009-09-30 17:36:20 -070025#define GL_GLEXT_PROTOTYPES
26
Jason Samsc97bb882009-07-20 14:31:06 -070027#include <GLES/gl.h>
28#include <GLES/glext.h>
29
Romain Guy584a3752009-07-30 18:45:01 -070030#include <time.h>
31#include <cutils/tztime.h>
32
Jason Samsc97bb882009-07-20 14:31:06 -070033using namespace android;
34using namespace android::renderscript;
35
36#define GET_TLS() Context::ScriptTLSStruct * tls = \
37 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
38 Context * rsc = tls->mContext; \
39 ScriptC * sc = (ScriptC *) tls->mScript
40
Jason Samsea84a7c2009-09-04 14:42:41 -070041typedef struct {
42 float x;
43 float y;
44 float z;
45} vec3_t;
46
47typedef struct {
48 float x;
49 float y;
50 float z;
51 float w;
52} vec4_t;
53
54typedef struct {
55 float x;
56 float y;
57} vec2_t;
Jason Samsc97bb882009-07-20 14:31:06 -070058
59//////////////////////////////////////////////////////////////////////////////
60// IO routines
61//////////////////////////////////////////////////////////////////////////////
62
63static float SC_loadF(uint32_t bank, uint32_t offset)
64{
65 GET_TLS();
66 const void *vp = sc->mSlots[bank]->getPtr();
67 const float *f = static_cast<const float *>(vp);
68 //LOGE("loadF %i %i = %f %x", bank, offset, f, ((int *)&f)[0]);
69 return f[offset];
70}
71
72static int32_t SC_loadI32(uint32_t bank, uint32_t offset)
73{
74 GET_TLS();
75 const void *vp = sc->mSlots[bank]->getPtr();
76 const int32_t *i = static_cast<const int32_t *>(vp);
77 //LOGE("loadI32 %i %i = %i", bank, offset, t);
78 return i[offset];
79}
80
Romain Guyf8e136d2009-08-06 12:40:41 -070081static float* SC_loadArrayF(uint32_t bank, uint32_t offset)
Romain Guya2136d62009-08-04 17:19:48 -070082{
83 GET_TLS();
84 void *vp = sc->mSlots[bank]->getPtr();
85 float *f = static_cast<float *>(vp);
Romain Guyf8e136d2009-08-06 12:40:41 -070086 return f + offset;
Romain Guya2136d62009-08-04 17:19:48 -070087}
88
Romain Guyf8e136d2009-08-06 12:40:41 -070089static int32_t* SC_loadArrayI32(uint32_t bank, uint32_t offset)
Romain Guya2136d62009-08-04 17:19:48 -070090{
91 GET_TLS();
92 void *vp = sc->mSlots[bank]->getPtr();
93 int32_t *i = static_cast<int32_t *>(vp);
Romain Guyf8e136d2009-08-06 12:40:41 -070094 return i + offset;
Romain Guya2136d62009-08-04 17:19:48 -070095}
96
Jason Sams6b9dec02009-09-23 16:38:37 -070097static float* SC_loadSimpleMeshVerticesF(RsSimpleMesh mesh, uint32_t idx)
Romain Guyb62627e2009-08-06 22:52:13 -070098{
Jason Sams6b9dec02009-09-23 16:38:37 -070099 SimpleMesh *tm = static_cast<SimpleMesh *>(mesh);
100 void *vp = tm->mVertexBuffers[idx]->getPtr();;
101 return static_cast<float *>(vp);
Romain Guyb62627e2009-08-06 22:52:13 -0700102}
103
Jason Sams6b9dec02009-09-23 16:38:37 -0700104static void SC_updateSimpleMesh(RsSimpleMesh mesh)
Romain Guyb62627e2009-08-06 22:52:13 -0700105{
Jason Sams6b9dec02009-09-23 16:38:37 -0700106 SimpleMesh *sm = static_cast<SimpleMesh *>(mesh);
107 sm->uploadAll();
Romain Guyb62627e2009-08-06 22:52:13 -0700108}
Romain Guyf8e136d2009-08-06 12:40:41 -0700109
Jason Samsc97bb882009-07-20 14:31:06 -0700110static uint32_t SC_loadU32(uint32_t bank, uint32_t offset)
111{
112 GET_TLS();
113 const void *vp = sc->mSlots[bank]->getPtr();
114 const uint32_t *i = static_cast<const uint32_t *>(vp);
115 return i[offset];
116}
117
118static void SC_loadVec4(uint32_t bank, uint32_t offset, rsc_Vector4 *v)
119{
120 GET_TLS();
121 const void *vp = sc->mSlots[bank]->getPtr();
122 const float *f = static_cast<const float *>(vp);
123 memcpy(v, &f[offset], sizeof(rsc_Vector4));
124}
125
126static void SC_loadMatrix(uint32_t bank, uint32_t offset, rsc_Matrix *m)
127{
128 GET_TLS();
129 const void *vp = sc->mSlots[bank]->getPtr();
130 const float *f = static_cast<const float *>(vp);
131 memcpy(m, &f[offset], sizeof(rsc_Matrix));
132}
133
134
135static void SC_storeF(uint32_t bank, uint32_t offset, float v)
136{
137 //LOGE("storeF %i %i %f", bank, offset, v);
138 GET_TLS();
139 void *vp = sc->mSlots[bank]->getPtr();
140 float *f = static_cast<float *>(vp);
141 f[offset] = v;
142}
143
144static void SC_storeI32(uint32_t bank, uint32_t offset, int32_t v)
145{
146 GET_TLS();
147 void *vp = sc->mSlots[bank]->getPtr();
148 int32_t *f = static_cast<int32_t *>(vp);
149 static_cast<int32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
150}
151
152static void SC_storeU32(uint32_t bank, uint32_t offset, uint32_t v)
153{
154 GET_TLS();
155 void *vp = sc->mSlots[bank]->getPtr();
156 uint32_t *f = static_cast<uint32_t *>(vp);
157 static_cast<uint32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
158}
159
160static void SC_storeVec4(uint32_t bank, uint32_t offset, const rsc_Vector4 *v)
161{
162 GET_TLS();
163 void *vp = sc->mSlots[bank]->getPtr();
164 float *f = static_cast<float *>(vp);
165 memcpy(&f[offset], v, sizeof(rsc_Vector4));
166}
167
168static void SC_storeMatrix(uint32_t bank, uint32_t offset, const rsc_Matrix *m)
169{
170 GET_TLS();
171 void *vp = sc->mSlots[bank]->getPtr();
172 float *f = static_cast<float *>(vp);
173 memcpy(&f[offset], m, sizeof(rsc_Matrix));
174}
175
Jason Samsea84a7c2009-09-04 14:42:41 -0700176//////////////////////////////////////////////////////////////////////////////
177// Vec3 routines
178//////////////////////////////////////////////////////////////////////////////
179
180static void SC_vec3Norm(vec3_t *v)
181{
182 float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
183 len = 1 / len;
184 v->x *= len;
185 v->y *= len;
186 v->z *= len;
187}
188
189static float SC_vec3Length(const vec3_t *v)
190{
191 return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
192}
193
194static void SC_vec3Add(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
195{
196 dest->x = lhs->x + rhs->x;
197 dest->y = lhs->y + rhs->y;
198 dest->z = lhs->z + rhs->z;
199}
200
201static void SC_vec3Sub(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
202{
203 dest->x = lhs->x - rhs->x;
204 dest->y = lhs->y - rhs->y;
205 dest->z = lhs->z - rhs->z;
206}
207
208static void SC_vec3Cross(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
209{
210 float x = lhs->y * rhs->z - lhs->z * rhs->y;
211 float y = lhs->z * rhs->x - lhs->x * rhs->z;
212 float z = lhs->x * rhs->y - lhs->y * rhs->x;
213 dest->x = x;
214 dest->y = y;
215 dest->z = z;
216}
217
218static float SC_vec3Dot(const vec3_t *lhs, const vec3_t *rhs)
219{
220 return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z;
221}
222
223static void SC_vec3Scale(vec3_t *lhs, float scale)
224{
225 lhs->x *= scale;
226 lhs->y *= scale;
227 lhs->z *= scale;
228}
229
Jason Samsc97bb882009-07-20 14:31:06 -0700230
231//////////////////////////////////////////////////////////////////////////////
232// Math routines
233//////////////////////////////////////////////////////////////////////////////
234
Romain Guy8839ca52009-07-31 11:20:59 -0700235#define PI 3.1415926f
236#define DEG_TO_RAD PI / 180.0f
237#define RAD_TO_DEG 180.0f / PI
238
Romain Guycac80a62009-08-18 11:39:17 -0700239static float SC_sinf_fast(float x)
240{
241 const float A = 1.0f / (2.0f * M_PI);
242 const float B = -16.0f;
243 const float C = 8.0f;
Jason Samsea84a7c2009-09-04 14:42:41 -0700244
Romain Guycac80a62009-08-18 11:39:17 -0700245 // scale angle for easy argument reduction
246 x *= A;
Jason Samsea84a7c2009-09-04 14:42:41 -0700247
Romain Guycac80a62009-08-18 11:39:17 -0700248 if (fabsf(x) >= 0.5f) {
249 // argument reduction
250 x = x - ceilf(x + 0.5f) + 1.0f;
251 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700252
Romain Guycac80a62009-08-18 11:39:17 -0700253 const float y = B * x * fabsf(x) + C * x;
254 return 0.2215f * (y * fabsf(y) - y) + y;
255}
256
257static float SC_cosf_fast(float x)
258{
259 x += float(M_PI / 2);
260
261 const float A = 1.0f / (2.0f * M_PI);
262 const float B = -16.0f;
263 const float C = 8.0f;
Jason Samsea84a7c2009-09-04 14:42:41 -0700264
Romain Guycac80a62009-08-18 11:39:17 -0700265 // scale angle for easy argument reduction
266 x *= A;
Jason Samsea84a7c2009-09-04 14:42:41 -0700267
Romain Guycac80a62009-08-18 11:39:17 -0700268 if (fabsf(x) >= 0.5f) {
269 // argument reduction
270 x = x - ceilf(x + 0.5f) + 1.0f;
271 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700272
Romain Guycac80a62009-08-18 11:39:17 -0700273 const float y = B * x * fabsf(x) + C * x;
274 return 0.2215f * (y * fabsf(y) - y) + y;
275}
276
Jason Samsc97bb882009-07-20 14:31:06 -0700277static float SC_randf(float max)
278{
279 float r = (float)rand();
280 return r / RAND_MAX * max;
281}
282
Romain Guy8839ca52009-07-31 11:20:59 -0700283static float SC_randf2(float min, float max)
284{
285 float r = (float)rand();
286 return r / RAND_MAX * (max - min) + min;
287}
288
289static float SC_clampf(float amount, float low, float high)
290{
291 return amount < low ? low : (amount > high ? high : amount);
292}
293
Romain Guya9d2d5e2009-08-09 17:04:54 -0700294static int SC_clamp(int amount, int low, int high)
295{
296 return amount < low ? low : (amount > high ? high : amount);
297}
298
Romain Guy8839ca52009-07-31 11:20:59 -0700299static float SC_maxf(float a, float b)
300{
Jason Sams1bada8c2009-08-09 17:01:55 -0700301 return a > b ? a : b;
Romain Guy8839ca52009-07-31 11:20:59 -0700302}
303
304static float SC_minf(float a, float b)
305{
Jason Sams1bada8c2009-08-09 17:01:55 -0700306 return a < b ? a : b;
Romain Guy8839ca52009-07-31 11:20:59 -0700307}
308
309static float SC_sqrf(float v)
310{
Jason Sams1bada8c2009-08-09 17:01:55 -0700311 return v * v;
Romain Guy8839ca52009-07-31 11:20:59 -0700312}
313
Romain Guy8f5c94b2009-08-08 18:30:19 -0700314static int SC_sqr(int v)
315{
316 return v * v;
317}
318
Jason Samsd342fd72009-09-18 14:24:24 -0700319static float SC_fracf(float v)
320{
321 return v - floorf(v);
322}
323
324static float SC_roundf(float v)
325{
326 return floorf(v + 0.4999999999);
327}
328
Romain Guy8839ca52009-07-31 11:20:59 -0700329static float SC_distf2(float x1, float y1, float x2, float y2)
330{
331 float x = x2 - x1;
332 float y = y2 - y1;
Jason Sams1bada8c2009-08-09 17:01:55 -0700333 return sqrtf(x * x + y * y);
Romain Guy8839ca52009-07-31 11:20:59 -0700334}
335
336static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2)
337{
338 float x = x2 - x1;
339 float y = y2 - y1;
340 float z = z2 - z1;
Jason Sams1bada8c2009-08-09 17:01:55 -0700341 return sqrtf(x * x + y * y + z * z);
Romain Guy8839ca52009-07-31 11:20:59 -0700342}
343
344static float SC_magf2(float a, float b)
345{
346 return sqrtf(a * a + b * b);
347}
348
349static float SC_magf3(float a, float b, float c)
350{
351 return sqrtf(a * a + b * b + c * c);
352}
353
354static float SC_radf(float degrees)
355{
Jason Sams1bada8c2009-08-09 17:01:55 -0700356 return degrees * DEG_TO_RAD;
Romain Guy8839ca52009-07-31 11:20:59 -0700357}
358
359static float SC_degf(float radians)
360{
Jason Sams1bada8c2009-08-09 17:01:55 -0700361 return radians * RAD_TO_DEG;
Romain Guy8839ca52009-07-31 11:20:59 -0700362}
363
364static float SC_lerpf(float start, float stop, float amount)
365{
366 return start + (stop - start) * amount;
367}
368
369static float SC_normf(float start, float stop, float value)
370{
371 return (value - start) / (stop - start);
372}
373
374static float SC_mapf(float minStart, float minStop, float maxStart, float maxStop, float value)
375{
376 return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
377}
Jason Samsc97bb882009-07-20 14:31:06 -0700378
Romain Guy584a3752009-07-30 18:45:01 -0700379//////////////////////////////////////////////////////////////////////////////
380// Time routines
381//////////////////////////////////////////////////////////////////////////////
Jason Samsc97bb882009-07-20 14:31:06 -0700382
Joe Onorato3370ec92009-08-09 11:39:02 -0700383static int32_t SC_second()
Romain Guy584a3752009-07-30 18:45:01 -0700384{
385 GET_TLS();
386
387 time_t rawtime;
388 time(&rawtime);
389
390 if (sc->mEnviroment.mTimeZone) {
391 struct tm timeinfo;
392 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
393 return timeinfo.tm_sec;
394 } else {
395 struct tm *timeinfo;
396 timeinfo = localtime(&rawtime);
397 return timeinfo->tm_sec;
398 }
399}
400
Joe Onorato3370ec92009-08-09 11:39:02 -0700401static int32_t SC_minute()
Romain Guy584a3752009-07-30 18:45:01 -0700402{
403 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700404
Romain Guy584a3752009-07-30 18:45:01 -0700405 time_t rawtime;
406 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700407
Romain Guy584a3752009-07-30 18:45:01 -0700408 if (sc->mEnviroment.mTimeZone) {
409 struct tm timeinfo;
410 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
411 return timeinfo.tm_min;
412 } else {
413 struct tm *timeinfo;
414 timeinfo = localtime(&rawtime);
415 return timeinfo->tm_min;
416 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700417}
Romain Guy584a3752009-07-30 18:45:01 -0700418
Joe Onorato3370ec92009-08-09 11:39:02 -0700419static int32_t SC_hour()
Romain Guy584a3752009-07-30 18:45:01 -0700420{
421 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700422
Romain Guy584a3752009-07-30 18:45:01 -0700423 time_t rawtime;
424 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700425
Romain Guy584a3752009-07-30 18:45:01 -0700426 if (sc->mEnviroment.mTimeZone) {
427 struct tm timeinfo;
428 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
429 return timeinfo.tm_hour;
430 } else {
431 struct tm *timeinfo;
432 timeinfo = localtime(&rawtime);
433 return timeinfo->tm_hour;
434 }
Romain Guy8839ca52009-07-31 11:20:59 -0700435}
436
Joe Onorato3370ec92009-08-09 11:39:02 -0700437static int32_t SC_day()
Romain Guy8839ca52009-07-31 11:20:59 -0700438{
439 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700440
Romain Guy8839ca52009-07-31 11:20:59 -0700441 time_t rawtime;
442 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700443
Romain Guy8839ca52009-07-31 11:20:59 -0700444 if (sc->mEnviroment.mTimeZone) {
445 struct tm timeinfo;
446 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
447 return timeinfo.tm_mday;
448 } else {
449 struct tm *timeinfo;
450 timeinfo = localtime(&rawtime);
451 return timeinfo->tm_mday;
452 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700453}
Jason Samsc97bb882009-07-20 14:31:06 -0700454
Joe Onorato3370ec92009-08-09 11:39:02 -0700455static int32_t SC_month()
Romain Guy8839ca52009-07-31 11:20:59 -0700456{
457 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700458
Romain Guy8839ca52009-07-31 11:20:59 -0700459 time_t rawtime;
460 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700461
Romain Guy8839ca52009-07-31 11:20:59 -0700462 if (sc->mEnviroment.mTimeZone) {
463 struct tm timeinfo;
464 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
465 return timeinfo.tm_mon;
466 } else {
467 struct tm *timeinfo;
468 timeinfo = localtime(&rawtime);
469 return timeinfo->tm_mon;
470 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700471}
Romain Guy8839ca52009-07-31 11:20:59 -0700472
Joe Onorato3370ec92009-08-09 11:39:02 -0700473static int32_t SC_year()
Romain Guy8839ca52009-07-31 11:20:59 -0700474{
475 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700476
Romain Guy8839ca52009-07-31 11:20:59 -0700477 time_t rawtime;
478 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700479
Romain Guy8839ca52009-07-31 11:20:59 -0700480 if (sc->mEnviroment.mTimeZone) {
481 struct tm timeinfo;
482 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
483 return timeinfo.tm_year;
484 } else {
485 struct tm *timeinfo;
486 timeinfo = localtime(&rawtime);
487 return timeinfo->tm_year;
488 }
489}
490
Joe Onorato3370ec92009-08-09 11:39:02 -0700491static int32_t SC_uptimeMillis()
492{
493 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
494}
495
496static int32_t SC_startTimeMillis()
497{
498 GET_TLS();
499 return sc->mEnviroment.mStartTimeMillis;
500}
501
502static int32_t SC_elapsedTimeMillis()
503{
504 GET_TLS();
505 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC))
506 - sc->mEnviroment.mStartTimeMillis;
507}
508
Jason Samsc97bb882009-07-20 14:31:06 -0700509//////////////////////////////////////////////////////////////////////////////
510// Matrix routines
511//////////////////////////////////////////////////////////////////////////////
512
513
514static void SC_matrixLoadIdentity(rsc_Matrix *mat)
515{
516 Matrix *m = reinterpret_cast<Matrix *>(mat);
517 m->loadIdentity();
518}
519
520static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
521{
522 Matrix *m = reinterpret_cast<Matrix *>(mat);
523 m->load(f);
524}
525
526static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
527{
528 Matrix *m = reinterpret_cast<Matrix *>(mat);
529 m->load(reinterpret_cast<const Matrix *>(newmat));
530}
531
532static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
533{
534 Matrix *m = reinterpret_cast<Matrix *>(mat);
535 m->loadRotate(rot, x, y, z);
536}
537
538static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
539{
540 Matrix *m = reinterpret_cast<Matrix *>(mat);
541 m->loadScale(x, y, z);
542}
543
544static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
545{
546 Matrix *m = reinterpret_cast<Matrix *>(mat);
547 m->loadTranslate(x, y, z);
548}
549
550static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
551{
552 Matrix *m = reinterpret_cast<Matrix *>(mat);
553 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
554 reinterpret_cast<const Matrix *>(rhs));
555}
556
557static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
558{
559 Matrix *m = reinterpret_cast<Matrix *>(mat);
560 m->multiply(reinterpret_cast<const Matrix *>(rhs));
561}
562
563static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
564{
565 Matrix *m = reinterpret_cast<Matrix *>(mat);
566 m->rotate(rot, x, y, z);
567}
568
569static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
570{
571 Matrix *m = reinterpret_cast<Matrix *>(mat);
572 m->scale(x, y, z);
573}
574
575static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
576{
577 Matrix *m = reinterpret_cast<Matrix *>(mat);
578 m->translate(x, y, z);
579}
580
581
Jason Sams334ea0c2009-08-17 13:56:09 -0700582static void SC_vec2Rand(float *vec, float maxLen)
583{
584 float angle = SC_randf(PI * 2);
585 float len = SC_randf(maxLen);
586 vec[0] = len * sinf(angle);
587 vec[1] = len * cosf(angle);
588}
589
Jason Samsc97bb882009-07-20 14:31:06 -0700590
591
592//////////////////////////////////////////////////////////////////////////////
593// Context
594//////////////////////////////////////////////////////////////////////////////
595
596static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
597{
598 GET_TLS();
599 rsi_ProgramFragmentBindTexture(rsc,
600 static_cast<ProgramFragment *>(vpf),
601 slot,
602 static_cast<Allocation *>(va));
603
604}
605
606static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
607{
608 GET_TLS();
609 rsi_ProgramFragmentBindSampler(rsc,
610 static_cast<ProgramFragment *>(vpf),
611 slot,
612 static_cast<Sampler *>(vs));
613
614}
615
616static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs)
617{
618 GET_TLS();
619 rsi_ContextBindProgramFragmentStore(rsc, pfs);
620
621}
622
623static void SC_bindProgramFragment(RsProgramFragment pf)
624{
625 GET_TLS();
626 rsi_ContextBindProgramFragment(rsc, pf);
627
628}
629
Jason Samsee411122009-07-21 12:20:54 -0700630static void SC_bindProgramVertex(RsProgramVertex pv)
631{
632 GET_TLS();
633 rsi_ContextBindProgramVertex(rsc, pv);
634
635}
Jason Samsc97bb882009-07-20 14:31:06 -0700636
637//////////////////////////////////////////////////////////////////////////////
Jason Samsb0ec1b42009-07-28 12:02:16 -0700638// VP
639//////////////////////////////////////////////////////////////////////////////
640
641static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
642{
643 GET_TLS();
644 rsc->getVertex()->setModelviewMatrix(m);
645}
646
647static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
648{
649 GET_TLS();
650 rsc->getVertex()->setTextureMatrix(m);
651}
652
653
654
655//////////////////////////////////////////////////////////////////////////////
Jason Samsc97bb882009-07-20 14:31:06 -0700656// Drawing
657//////////////////////////////////////////////////////////////////////////////
658
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700659static void SC_drawLine(float x1, float y1, float z1,
660 float x2, float y2, float z2)
661{
662 GET_TLS();
663 rsc->setupCheck();
664
665 float vtx[] = { x1, y1, z1, x2, y2, z2 };
666
667 glBindBuffer(GL_ARRAY_BUFFER, 0);
668 glEnableClientState(GL_VERTEX_ARRAY);
669 glVertexPointer(3, GL_FLOAT, 0, vtx);
670
671 glDisableClientState(GL_NORMAL_ARRAY);
672 glDisableClientState(GL_COLOR_ARRAY);
673
674 glDrawArrays(GL_LINES, 0, 2);
675}
676
Jason Sams5235cf32009-09-28 18:12:56 -0700677static void SC_drawPoint(float x, float y, float z)
678{
679 GET_TLS();
680 rsc->setupCheck();
681
682 float vtx[] = { x, y, z };
683
684 glBindBuffer(GL_ARRAY_BUFFER, 0);
685 glEnableClientState(GL_VERTEX_ARRAY);
686 glVertexPointer(3, GL_FLOAT, 0, vtx);
687
688 glDisableClientState(GL_NORMAL_ARRAY);
689 glDisableClientState(GL_COLOR_ARRAY);
690
691 glDrawArrays(GL_POINTS, 0, 1);
692}
693
Romain Guy8f5c94b2009-08-08 18:30:19 -0700694static void SC_drawQuadTexCoords(float x1, float y1, float z1,
695 float u1, float v1,
696 float x2, float y2, float z2,
697 float u2, float v2,
698 float x3, float y3, float z3,
699 float u3, float v3,
700 float x4, float y4, float z4,
701 float u4, float v4)
Jason Samsc97bb882009-07-20 14:31:06 -0700702{
703 GET_TLS();
Jason Sams40a29e82009-08-10 14:55:26 -0700704
Jason Samsc97bb882009-07-20 14:31:06 -0700705 //LOGE("Quad");
706 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
707 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
708 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
709 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
Jason Sams40a29e82009-08-10 14:55:26 -0700710
Jason Samsc97bb882009-07-20 14:31:06 -0700711 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
Romain Guy8f5c94b2009-08-08 18:30:19 -0700712 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
Jason Samsc97bb882009-07-20 14:31:06 -0700713
714 rsc->setupCheck();
715
716 glBindBuffer(GL_ARRAY_BUFFER, 0);
717 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
718
719 glEnableClientState(GL_VERTEX_ARRAY);
720 glVertexPointer(3, GL_FLOAT, 0, vtx);
721
722 glClientActiveTexture(GL_TEXTURE0);
723 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
724 glTexCoordPointer(2, GL_FLOAT, 0, tex);
725 glClientActiveTexture(GL_TEXTURE1);
726 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
727 glTexCoordPointer(2, GL_FLOAT, 0, tex);
728 glClientActiveTexture(GL_TEXTURE0);
729
730 glDisableClientState(GL_NORMAL_ARRAY);
731 glDisableClientState(GL_COLOR_ARRAY);
732
733 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
734
735 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
736}
737
Romain Guy8f5c94b2009-08-08 18:30:19 -0700738static void SC_drawQuad(float x1, float y1, float z1,
739 float x2, float y2, float z2,
740 float x3, float y3, float z3,
741 float x4, float y4, float z4)
742{
743 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
744 x2, y2, z2, 1, 1,
745 x3, y3, z3, 1, 0,
746 x4, y4, z4, 0, 0);
747}
748
Jason Samse9ad9a72009-09-30 17:36:20 -0700749static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h)
750{
751 GET_TLS();
752 rsc->setupCheck();
753
754 GLint crop[4] = {0, h, w, -h};
755 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
756 glDrawTexfOES(x, y, z, w, h);
757}
758
759static void SC_drawSprite(float x, float y, float z, float w, float h)
760{
761 GET_TLS();
762 rsc->setupCheck();
763
764 float vin[3] = {x, y, z};
765 float vout[4];
766
767 //LOGE("ds in %f %f %f", x, y, z);
768 rsc->getVertex()->transformToScreen(rsc, vout, vin);
769 //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]);
770 vout[0] /= vout[3];
771 vout[1] /= vout[3];
772 vout[2] /= vout[3];
773
774 vout[0] *= rsc->getWidth() / 2;
775 vout[1] *= rsc->getHeight() / 2;
776 vout[0] += rsc->getWidth() / 2;
777 vout[1] += rsc->getHeight() / 2;
778
779 vout[0] -= w/2;
780 vout[1] -= h/2;
781
782 //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]);
783
784 // U, V, W, H
785 GLint crop[4] = {0, h, w, -h};
786 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
787 glDrawTexiOES(vout[0], vout[1], 0/*vout[2]*/, w, h);
788}
789
790
Jason Sams6f5c61c2009-07-28 17:20:11 -0700791static void SC_drawRect(float x1, float y1,
792 float x2, float y2, float z)
793{
794 SC_drawQuad(x1, y2, z,
795 x2, y2, z,
796 x2, y1, z,
797 x1, y1, z);
798}
799
Jason Sams1bada8c2009-08-09 17:01:55 -0700800static void SC_drawSimpleMesh(RsSimpleMesh vsm)
801{
802 GET_TLS();
803 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
804 rsc->setupCheck();
805 sm->render();
806}
807
808static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len)
809{
810 GET_TLS();
811 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
812 rsc->setupCheck();
813 sm->renderRange(start, len);
814}
815
816
Jason Samsc97bb882009-07-20 14:31:06 -0700817//////////////////////////////////////////////////////////////////////////////
818//
819//////////////////////////////////////////////////////////////////////////////
820
Jason Samsc97bb882009-07-20 14:31:06 -0700821static void SC_color(float r, float g, float b, float a)
822{
823 glColor4f(r, g, b, a);
824}
825
Romain Guyb62627e2009-08-06 22:52:13 -0700826static void SC_ambient(float r, float g, float b, float a)
827{
828 GLfloat params[] = { r, g, b, a };
829 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, params);
830}
831
832static void SC_diffuse(float r, float g, float b, float a)
833{
834 GLfloat params[] = { r, g, b, a };
835 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, params);
836}
837
838static void SC_specular(float r, float g, float b, float a)
839{
840 GLfloat params[] = { r, g, b, a };
841 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, params);
842}
843
844static void SC_emission(float r, float g, float b, float a)
845{
846 GLfloat params[] = { r, g, b, a };
847 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, params);
848}
849
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700850static void SC_shininess(float s)
Romain Guyb62627e2009-08-06 22:52:13 -0700851{
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700852 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, s);
Romain Guyb62627e2009-08-06 22:52:13 -0700853}
854
Romain Guy2d496bf2009-09-04 17:55:41 -0700855static void SC_pointAttenuation(float a, float b, float c)
856{
857 GLfloat params[] = { a, b, c };
858 glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, params);
859}
860
Romain Guyd22fff72009-08-20 17:08:33 -0700861static void SC_hsbToRgb(float h, float s, float b, float* rgb)
Romain Guya32d1002009-07-31 15:33:59 -0700862{
863 float red = 0.0f;
864 float green = 0.0f;
865 float blue = 0.0f;
Jason Sams1bada8c2009-08-09 17:01:55 -0700866
Romain Guya32d1002009-07-31 15:33:59 -0700867 float x = h;
868 float y = s;
869 float z = b;
Jason Sams1bada8c2009-08-09 17:01:55 -0700870
Romain Guya32d1002009-07-31 15:33:59 -0700871 float hf = (x - (int) x) * 6.0f;
872 int ihf = (int) hf;
873 float f = hf - ihf;
874 float pv = z * (1.0f - y);
875 float qv = z * (1.0f - y * f);
876 float tv = z * (1.0f - y * (1.0f - f));
Jason Sams1bada8c2009-08-09 17:01:55 -0700877
Romain Guya32d1002009-07-31 15:33:59 -0700878 switch (ihf) {
879 case 0: // Red is the dominant color
880 red = z;
881 green = tv;
882 blue = pv;
883 break;
884 case 1: // Green is the dominant color
885 red = qv;
886 green = z;
887 blue = pv;
888 break;
889 case 2:
890 red = pv;
891 green = z;
892 blue = tv;
893 break;
894 case 3: // Blue is the dominant color
895 red = pv;
896 green = qv;
897 blue = z;
898 break;
899 case 4:
900 red = tv;
901 green = pv;
902 blue = z;
903 break;
904 case 5: // Red is the dominant color
905 red = z;
906 green = pv;
907 blue = qv;
908 break;
909 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700910
Romain Guyd22fff72009-08-20 17:08:33 -0700911 rgb[0] = red;
912 rgb[1] = green;
913 rgb[2] = blue;
914}
915
916static int SC_hsbToAbgr(float h, float s, float b, float a)
917{
918 float rgb[3];
919 SC_hsbToRgb(h, s, b, rgb);
920 return int(a * 255.0f) << 24 |
921 int(rgb[2] * 255.0f) << 16 |
922 int(rgb[1] * 255.0f) << 8 |
923 int(rgb[0] * 255.0f);
924}
925
926static void SC_hsb(float h, float s, float b, float a)
927{
928 float rgb[3];
929 SC_hsbToRgb(h, s, b, rgb);
930 glColor4f(rgb[0], rgb[1], rgb[2], a);
Romain Guya32d1002009-07-31 15:33:59 -0700931}
932
Jason Samsb0ec1b42009-07-28 12:02:16 -0700933static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samsc97bb882009-07-20 14:31:06 -0700934{
935 GET_TLS();
936 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
937}
938
Jason Sams1bada8c2009-08-09 17:01:55 -0700939static void SC_uploadToBufferObject(RsAllocation va)
940{
941 GET_TLS();
942 rsi_AllocationUploadToBufferObject(rsc, va);
943}
944
Jason Samsc97bb882009-07-20 14:31:06 -0700945static void SC_ClearColor(float r, float g, float b, float a)
946{
947 //LOGE("c %f %f %f %f", r, g, b, a);
948 GET_TLS();
949 sc->mEnviroment.mClearColor[0] = r;
950 sc->mEnviroment.mClearColor[1] = g;
951 sc->mEnviroment.mClearColor[2] = b;
952 sc->mEnviroment.mClearColor[3] = a;
953}
954
Jason Samsb0ec1b42009-07-28 12:02:16 -0700955static void SC_debugF(const char *s, float f)
956{
957 LOGE("%s %f", s, f);
958}
959
Romain Guyd22fff72009-08-20 17:08:33 -0700960static void SC_debugHexF(const char *s, float f)
961{
962 LOGE("%s 0x%x", s, *((int *) (&f)));
963}
964
Jason Samsb0ec1b42009-07-28 12:02:16 -0700965static void SC_debugI32(const char *s, int32_t i)
966{
967 LOGE("%s %i", s, i);
968}
969
Romain Guyd22fff72009-08-20 17:08:33 -0700970static void SC_debugHexI32(const char *s, int32_t i)
971{
972 LOGE("%s 0x%x", s, i);
973}
974
Jason Sams40a29e82009-08-10 14:55:26 -0700975static uint32_t SC_getWidth()
976{
977 GET_TLS();
978 return rsc->getWidth();
979}
Jason Samsc97bb882009-07-20 14:31:06 -0700980
Jason Sams40a29e82009-08-10 14:55:26 -0700981static uint32_t SC_getHeight()
982{
983 GET_TLS();
984 return rsc->getHeight();
985}
Jason Samsc97bb882009-07-20 14:31:06 -0700986
Jason Sams334ea0c2009-08-17 13:56:09 -0700987static uint32_t SC_colorFloatRGBAtoUNorm8(float r, float g, float b, float a)
988{
989 uint32_t c = 0;
990 c |= (uint32_t)(r * 255.f + 0.5f);
991 c |= ((uint32_t)(g * 255.f + 0.5f)) << 8;
992 c |= ((uint32_t)(b * 255.f + 0.5f)) << 16;
993 c |= ((uint32_t)(a * 255.f + 0.5f)) << 24;
994 return c;
995}
996
997static uint32_t SC_colorFloatRGBAto565(float r, float g, float b)
998{
999 uint32_t ir = (uint32_t)(r * 255.f + 0.5f);
1000 uint32_t ig = (uint32_t)(g * 255.f + 0.5f);
1001 uint32_t ib = (uint32_t)(b * 255.f + 0.5f);
1002 return rs888to565(ir, ig, ib);
1003}
1004
Jason Samsc97bb882009-07-20 14:31:06 -07001005//////////////////////////////////////////////////////////////////////////////
1006// Class implementation
1007//////////////////////////////////////////////////////////////////////////////
1008
1009ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
1010 // IO
1011 { "loadI32", (void *)&SC_loadI32,
1012 "int", "(int, int)" },
1013 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
1014 { "loadF", (void *)&SC_loadF,
1015 "float", "(int, int)" },
Romain Guya2136d62009-08-04 17:19:48 -07001016 { "loadArrayF", (void *)&SC_loadArrayF,
Romain Guyf8e136d2009-08-06 12:40:41 -07001017 "float*", "(int, int)" },
Romain Guya2136d62009-08-04 17:19:48 -07001018 { "loadArrayI32", (void *)&SC_loadArrayI32,
Romain Guyf8e136d2009-08-06 12:40:41 -07001019 "int*", "(int, int)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001020 { "loadVec4", (void *)&SC_loadVec4,
1021 "void", "(int, int, float *)" },
1022 { "loadMatrix", (void *)&SC_loadMatrix,
1023 "void", "(int, int, float *)" },
1024 { "storeI32", (void *)&SC_storeI32,
1025 "void", "(int, int, int)" },
1026 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
1027 { "storeF", (void *)&SC_storeF,
1028 "void", "(int, int, float)" },
1029 { "storeVec4", (void *)&SC_storeVec4,
1030 "void", "(int, int, float *)" },
1031 { "storeMatrix", (void *)&SC_storeMatrix,
1032 "void", "(int, int, float *)" },
Jason Sams6b9dec02009-09-23 16:38:37 -07001033 { "loadSimpleMeshVerticesF", (void *)&SC_loadSimpleMeshVerticesF,
1034 "float*", "(int, int)" },
1035 { "updateSimpleMesh", (void *)&SC_updateSimpleMesh,
Romain Guyb62627e2009-08-06 22:52:13 -07001036 "void", "(int)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001037
1038 // math
Romain Guya9d2d5e2009-08-09 17:04:54 -07001039 { "modf", (void *)&fmod,
1040 "float", "(float, float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -07001041 { "abs", (void *)&abs,
1042 "int", "(int)" },
Romain Guybd5b5722009-09-29 13:17:27 -07001043 { "absf", (void *)&fabsf,
Romain Guy8f5c94b2009-08-08 18:30:19 -07001044 "float", "(float)" },
Romain Guycac80a62009-08-18 11:39:17 -07001045 { "sinf_fast", (void *)&SC_sinf_fast,
1046 "float", "(float)" },
1047 { "cosf_fast", (void *)&SC_cosf_fast,
1048 "float", "(float)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001049 { "sinf", (void *)&sinf,
1050 "float", "(float)" },
1051 { "cosf", (void *)&cosf,
1052 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001053 { "asinf", (void *)&asinf,
1054 "float", "(float)" },
1055 { "acosf", (void *)&acosf,
1056 "float", "(float)" },
1057 { "atanf", (void *)&atanf,
1058 "float", "(float)" },
1059 { "atan2f", (void *)&atan2f,
Romain Guya32d1002009-07-31 15:33:59 -07001060 "float", "(float, float)" },
Jason Sams6f5c61c2009-07-28 17:20:11 -07001061 { "fabsf", (void *)&fabsf,
Jason Samsc97bb882009-07-20 14:31:06 -07001062 "float", "(float)" },
1063 { "randf", (void *)&SC_randf,
1064 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001065 { "randf2", (void *)&SC_randf2,
1066 "float", "(float, float)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001067 { "floorf", (void *)&floorf,
1068 "float", "(float)" },
Jason Samsd342fd72009-09-18 14:24:24 -07001069 { "fracf", (void *)&SC_fracf,
1070 "float", "(float)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001071 { "ceilf", (void *)&ceilf,
1072 "float", "(float)" },
Jason Samsd342fd72009-09-18 14:24:24 -07001073 { "roundf", (void *)&SC_roundf,
1074 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001075 { "expf", (void *)&expf,
1076 "float", "(float)" },
1077 { "logf", (void *)&logf,
1078 "float", "(float)" },
1079 { "powf", (void *)&powf,
1080 "float", "(float, float)" },
1081 { "maxf", (void *)&SC_maxf,
1082 "float", "(float, float)" },
1083 { "minf", (void *)&SC_minf,
1084 "float", "(float, float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -07001085 { "sqrt", (void *)&sqrt,
1086 "int", "(int)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001087 { "sqrtf", (void *)&sqrtf,
1088 "float", "(float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -07001089 { "sqr", (void *)&SC_sqr,
1090 "int", "(int)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001091 { "sqrf", (void *)&SC_sqrf,
1092 "float", "(float)" },
Romain Guya9d2d5e2009-08-09 17:04:54 -07001093 { "clamp", (void *)&SC_clamp,
1094 "int", "(int, int, int)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001095 { "clampf", (void *)&SC_clampf,
1096 "float", "(float, float, float)" },
1097 { "distf2", (void *)&SC_distf2,
1098 "float", "(float, float, float, float)" },
1099 { "distf3", (void *)&SC_distf3,
1100 "float", "(float, float, float, float, float, float)" },
1101 { "magf2", (void *)&SC_magf2,
1102 "float", "(float, float)" },
1103 { "magf3", (void *)&SC_magf3,
1104 "float", "(float, float, float)" },
1105 { "radf", (void *)&SC_radf,
1106 "float", "(float)" },
1107 { "degf", (void *)&SC_degf,
1108 "float", "(float)" },
1109 { "lerpf", (void *)&SC_lerpf,
1110 "float", "(float, float, float)" },
1111 { "normf", (void *)&SC_normf,
1112 "float", "(float, float, float)" },
1113 { "mapf", (void *)&SC_mapf,
1114 "float", "(float, float, float, float, float)" },
Romain Guyecc7ca032009-08-03 21:12:51 -07001115 { "noisef", (void *)&SC_noisef,
1116 "float", "(float)" },
1117 { "noisef2", (void *)&SC_noisef2,
1118 "float", "(float, float)" },
1119 { "noisef3", (void *)&SC_noisef3,
1120 "float", "(float, float, float)" },
1121 { "turbulencef2", (void *)&SC_turbulencef2,
1122 "float", "(float, float, float)" },
1123 { "turbulencef3", (void *)&SC_turbulencef3,
1124 "float", "(float, float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001125
Romain Guy584a3752009-07-30 18:45:01 -07001126 // time
1127 { "second", (void *)&SC_second,
1128 "int", "()" },
1129 { "minute", (void *)&SC_minute,
1130 "int", "()" },
1131 { "hour", (void *)&SC_hour,
1132 "int", "()" },
Romain Guy8839ca52009-07-31 11:20:59 -07001133 { "day", (void *)&SC_day,
1134 "int", "()" },
1135 { "month", (void *)&SC_month,
1136 "int", "()" },
1137 { "year", (void *)&SC_year,
1138 "int", "()" },
Joe Onorato3370ec92009-08-09 11:39:02 -07001139 { "uptimeMillis", (void*)&SC_uptimeMillis,
1140 "int", "()" }, // TODO: use long instead
1141 { "startTimeMillis", (void*)&SC_startTimeMillis,
1142 "int", "()" }, // TODO: use long instead
1143 { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis,
1144 "int", "()" }, // TODO: use long instead
Romain Guy584a3752009-07-30 18:45:01 -07001145
Jason Samsc97bb882009-07-20 14:31:06 -07001146 // matrix
1147 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
1148 "void", "(float *mat)" },
1149 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
1150 "void", "(float *mat, float *f)" },
1151 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
1152 "void", "(float *mat, float *newmat)" },
1153 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
1154 "void", "(float *mat, float rot, float x, float y, float z)" },
1155 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
1156 "void", "(float *mat, float x, float y, float z)" },
1157 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
1158 "void", "(float *mat, float x, float y, float z)" },
1159 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
1160 "void", "(float *mat, float *lhs, float *rhs)" },
1161 { "matrixMultiply", (void *)&SC_matrixMultiply,
1162 "void", "(float *mat, float *rhs)" },
1163 { "matrixRotate", (void *)&SC_matrixRotate,
1164 "void", "(float *mat, float rot, float x, float y, float z)" },
1165 { "matrixScale", (void *)&SC_matrixScale,
1166 "void", "(float *mat, float x, float y, float z)" },
1167 { "matrixTranslate", (void *)&SC_matrixTranslate,
1168 "void", "(float *mat, float x, float y, float z)" },
1169
Jason Sams334ea0c2009-08-17 13:56:09 -07001170 // vector
1171 { "vec2Rand", (void *)&SC_vec2Rand,
1172 "void", "(float *vec, float maxLen)" },
1173
Jason Samsea84a7c2009-09-04 14:42:41 -07001174 // vec3
1175 { "vec3Norm", (void *)&SC_vec3Norm,
1176 "void", "(struct vec3_s *)" },
1177 { "vec3Length", (void *)&SC_vec3Length,
1178 "float", "(struct vec3_s *)" },
1179 { "vec3Add", (void *)&SC_vec3Add,
1180 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1181 { "vec3Sub", (void *)&SC_vec3Sub,
1182 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1183 { "vec3Cross", (void *)&SC_vec3Cross,
1184 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1185 { "vec3Dot", (void *)&SC_vec3Dot,
1186 "float", "(struct vec3_s *lhs, struct vec3_s *rhs)" },
1187 { "vec3Scale", (void *)&SC_vec3Scale,
1188 "void", "(struct vec3_s *lhs, float scale)" },
1189
Jason Samsc97bb882009-07-20 14:31:06 -07001190 // context
1191 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
1192 "void", "(int)" },
1193 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
1194 "void", "(int)" },
Jason Sams5235cf32009-09-28 18:12:56 -07001195 { "bindProgramStore", (void *)&SC_bindProgramFragmentStore,
1196 "void", "(int)" },
Jason Samsee411122009-07-21 12:20:54 -07001197 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
1198 "void", "(int)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001199 { "bindSampler", (void *)&SC_bindSampler,
1200 "void", "(int, int, int)" },
1201 { "bindTexture", (void *)&SC_bindTexture,
1202 "void", "(int, int, int)" },
1203
Jason Samsb0ec1b42009-07-28 12:02:16 -07001204 // vp
Jason Samsfaf15202009-07-29 20:55:44 -07001205 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsb0ec1b42009-07-28 12:02:16 -07001206 "void", "(void *)" },
Jason Samsfaf15202009-07-29 20:55:44 -07001207 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsb0ec1b42009-07-28 12:02:16 -07001208 "void", "(void *)" },
1209
1210
1211
Jason Samsc97bb882009-07-20 14:31:06 -07001212 // drawing
Jason Sams6f5c61c2009-07-28 17:20:11 -07001213 { "drawRect", (void *)&SC_drawRect,
1214 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001215 { "drawQuad", (void *)&SC_drawQuad,
1216 "void", "(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -07001217 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords,
1218 "void", "(float x1, float y1, float z1, float u1, float v1, float x2, float y2, float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3, float x4, float y4, float z4, float u4, float v4)" },
Jason Samse9ad9a72009-09-30 17:36:20 -07001219 { "drawSprite", (void *)&SC_drawSprite,
1220 "void", "(float x, float y, float z, float w, float h)" },
1221 { "drawSpriteScreenspace", (void *)&SC_drawSpriteScreenspace,
1222 "void", "(float x, float y, float z, float w, float h)" },
Romain Guy6c0cc6d2009-08-07 15:40:32 -07001223 { "drawLine", (void *)&SC_drawLine,
1224 "void", "(float x1, float y1, float z1, float x2, float y2, float z2)" },
Jason Sams5235cf32009-09-28 18:12:56 -07001225 { "drawPoint", (void *)&SC_drawPoint,
1226 "void", "(float x1, float y1, float z1)" },
Jason Sams1bada8c2009-08-09 17:01:55 -07001227 { "drawSimpleMesh", (void *)&SC_drawSimpleMesh,
1228 "void", "(int ism)" },
1229 { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange,
1230 "void", "(int ism, int start, int len)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001231
1232
1233 // misc
1234 { "pfClearColor", (void *)&SC_ClearColor,
1235 "void", "(float, float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001236 { "color", (void *)&SC_color,
1237 "void", "(float, float, float, float)" },
Romain Guya32d1002009-07-31 15:33:59 -07001238 { "hsb", (void *)&SC_hsb,
1239 "void", "(float, float, float, float)" },
Romain Guyd22fff72009-08-20 17:08:33 -07001240 { "hsbToRgb", (void *)&SC_hsbToRgb,
1241 "void", "(float, float, float, float*)" },
1242 { "hsbToAbgr", (void *)&SC_hsbToAbgr,
1243 "int", "(float, float, float, float)" },
Romain Guyb62627e2009-08-06 22:52:13 -07001244 { "ambient", (void *)&SC_ambient,
1245 "void", "(float, float, float, float)" },
1246 { "diffuse", (void *)&SC_diffuse,
1247 "void", "(float, float, float, float)" },
1248 { "specular", (void *)&SC_specular,
1249 "void", "(float, float, float, float)" },
1250 { "emission", (void *)&SC_emission,
1251 "void", "(float, float, float, float)" },
1252 { "shininess", (void *)&SC_shininess,
Romain Guy6c0cc6d2009-08-07 15:40:32 -07001253 "void", "(float)" },
Romain Guy2d496bf2009-09-04 17:55:41 -07001254 { "pointAttenuation", (void *)&SC_pointAttenuation,
1255 "void", "(float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001256
Jason Samsb0ec1b42009-07-28 12:02:16 -07001257 { "uploadToTexture", (void *)&SC_uploadToTexture,
1258 "void", "(int, int)" },
Jason Sams1bada8c2009-08-09 17:01:55 -07001259 { "uploadToBufferObject", (void *)&SC_uploadToBufferObject,
1260 "void", "(int)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001261
Jason Sams334ea0c2009-08-17 13:56:09 -07001262 { "colorFloatRGBAtoUNorm8", (void *)&SC_colorFloatRGBAtoUNorm8,
1263 "int", "(float, float, float, float)" },
1264 { "colorFloatRGBto565", (void *)&SC_colorFloatRGBAto565,
1265 "int", "(float, float, float)" },
1266
1267
Jason Sams40a29e82009-08-10 14:55:26 -07001268 { "getWidth", (void *)&SC_getWidth,
1269 "int", "()" },
1270 { "getHeight", (void *)&SC_getHeight,
1271 "int", "()" },
1272
1273
Jason Samsb0ec1b42009-07-28 12:02:16 -07001274
1275 { "debugF", (void *)&SC_debugF,
1276 "void", "(void *, float)" },
1277 { "debugI32", (void *)&SC_debugI32,
1278 "void", "(void *, int)" },
Romain Guyd22fff72009-08-20 17:08:33 -07001279 { "debugHexF", (void *)&SC_debugHexF,
1280 "void", "(void *, float)" },
1281 { "debugHexI32", (void *)&SC_debugHexI32,
1282 "void", "(void *, int)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001283
1284
Jason Samsc97bb882009-07-20 14:31:06 -07001285 { NULL, NULL, NULL, NULL }
1286};
1287
1288const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
1289{
1290 ScriptCState::SymbolTable_t *syms = gSyms;
1291
1292 while (syms->mPtr) {
1293 if (!strcmp(syms->mName, sym)) {
1294 return syms;
1295 }
1296 syms++;
1297 }
1298 return NULL;
1299}
1300
1301void ScriptCState::appendDecls(String8 *str)
1302{
1303 ScriptCState::SymbolTable_t *syms = gSyms;
1304 while (syms->mPtr) {
1305 str->append(syms->mRet);
1306 str->append(" ");
1307 str->append(syms->mName);
1308 str->append(syms->mParam);
1309 str->append(";\n");
1310 syms++;
1311 }
1312}
1313
1314