blob: 436f48b319efd151a827540f016cf95031169e9e [file] [log] [blame]
Jason Samse45ac6e2009-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 Guyb7f1a6d2009-08-03 21:12:51 -070020#include "rsNoise.h"
Jason Samse45ac6e2009-07-20 14:31:06 -070021
22#include "acc/acc.h"
Joe Onorato9c4e4ca2009-08-09 11:39:02 -070023#include "utils/Timers.h"
Jason Samse45ac6e2009-07-20 14:31:06 -070024
Jason Sams3a97c592009-09-30 17:36:20 -070025#define GL_GLEXT_PROTOTYPES
26
Jason Samse45ac6e2009-07-20 14:31:06 -070027#include <GLES/gl.h>
28#include <GLES/glext.h>
29
Romain Guy98e10fd2009-07-30 18:45:01 -070030#include <time.h>
31#include <cutils/tztime.h>
32
Jason Samse45ac6e2009-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 Samsa57c0a72009-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 Samse45ac6e2009-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 Guy06f7c932009-08-06 12:40:41 -070081static float* SC_loadArrayF(uint32_t bank, uint32_t offset)
Romain Guy36401002009-08-04 17:19:48 -070082{
83 GET_TLS();
84 void *vp = sc->mSlots[bank]->getPtr();
85 float *f = static_cast<float *>(vp);
Romain Guy06f7c932009-08-06 12:40:41 -070086 return f + offset;
Romain Guy36401002009-08-04 17:19:48 -070087}
88
Romain Guy06f7c932009-08-06 12:40:41 -070089static int32_t* SC_loadArrayI32(uint32_t bank, uint32_t offset)
Romain Guy36401002009-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 Guy06f7c932009-08-06 12:40:41 -070094 return i + offset;
Romain Guy36401002009-08-04 17:19:48 -070095}
96
Jason Samsa2b54c42009-09-23 16:38:37 -070097static float* SC_loadSimpleMeshVerticesF(RsSimpleMesh mesh, uint32_t idx)
Romain Guy48b7edc2009-08-06 22:52:13 -070098{
Jason Samsa2b54c42009-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 Guy48b7edc2009-08-06 22:52:13 -0700102}
103
Jason Samsa2b54c42009-09-23 16:38:37 -0700104static void SC_updateSimpleMesh(RsSimpleMesh mesh)
Romain Guy48b7edc2009-08-06 22:52:13 -0700105{
Jason Samsa2b54c42009-09-23 16:38:37 -0700106 SimpleMesh *sm = static_cast<SimpleMesh *>(mesh);
107 sm->uploadAll();
Romain Guy48b7edc2009-08-06 22:52:13 -0700108}
Romain Guy06f7c932009-08-06 12:40:41 -0700109
Jason Samse45ac6e2009-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 Samsa57c0a72009-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 Samse45ac6e2009-07-20 14:31:06 -0700230
231//////////////////////////////////////////////////////////////////////////////
232// Math routines
233//////////////////////////////////////////////////////////////////////////////
234
Romain Guy39dbc802009-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 Guy2275d632009-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 Samsa57c0a72009-09-04 14:42:41 -0700244
Romain Guy2275d632009-08-18 11:39:17 -0700245 // scale angle for easy argument reduction
246 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -0700247
Romain Guy2275d632009-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 Samsa57c0a72009-09-04 14:42:41 -0700252
Romain Guy2275d632009-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 Samsa57c0a72009-09-04 14:42:41 -0700264
Romain Guy2275d632009-08-18 11:39:17 -0700265 // scale angle for easy argument reduction
266 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -0700267
Romain Guy2275d632009-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 Samsa57c0a72009-09-04 14:42:41 -0700272
Romain Guy2275d632009-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 Samse45ac6e2009-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 Guy39dbc802009-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 Guy27162ab2009-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 Guy39dbc802009-07-31 11:20:59 -0700299static float SC_maxf(float a, float b)
300{
Jason Samse5ffb872009-08-09 17:01:55 -0700301 return a > b ? a : b;
Romain Guy39dbc802009-07-31 11:20:59 -0700302}
303
304static float SC_minf(float a, float b)
305{
Jason Samse5ffb872009-08-09 17:01:55 -0700306 return a < b ? a : b;
Romain Guy39dbc802009-07-31 11:20:59 -0700307}
308
309static float SC_sqrf(float v)
310{
Jason Samse5ffb872009-08-09 17:01:55 -0700311 return v * v;
Romain Guy39dbc802009-07-31 11:20:59 -0700312}
313
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700314static int SC_sqr(int v)
315{
316 return v * v;
317}
318
Jason Samsdac98f52009-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 Guy39dbc802009-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 Samse5ffb872009-08-09 17:01:55 -0700333 return sqrtf(x * x + y * y);
Romain Guy39dbc802009-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 Samse5ffb872009-08-09 17:01:55 -0700341 return sqrtf(x * x + y * y + z * z);
Romain Guy39dbc802009-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 Samse5ffb872009-08-09 17:01:55 -0700356 return degrees * DEG_TO_RAD;
Romain Guy39dbc802009-07-31 11:20:59 -0700357}
358
359static float SC_degf(float radians)
360{
Jason Samse5ffb872009-08-09 17:01:55 -0700361 return radians * RAD_TO_DEG;
Romain Guy39dbc802009-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 Samse45ac6e2009-07-20 14:31:06 -0700378
Romain Guy98e10fd2009-07-30 18:45:01 -0700379//////////////////////////////////////////////////////////////////////////////
380// Time routines
381//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700382
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700383static int32_t SC_second()
Romain Guy98e10fd2009-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 Onorato9c4e4ca2009-08-09 11:39:02 -0700401static int32_t SC_minute()
Romain Guy98e10fd2009-07-30 18:45:01 -0700402{
403 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700404
Romain Guy98e10fd2009-07-30 18:45:01 -0700405 time_t rawtime;
406 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700407
Romain Guy98e10fd2009-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 Samse5ffb872009-08-09 17:01:55 -0700417}
Romain Guy98e10fd2009-07-30 18:45:01 -0700418
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700419static int32_t SC_hour()
Romain Guy98e10fd2009-07-30 18:45:01 -0700420{
421 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700422
Romain Guy98e10fd2009-07-30 18:45:01 -0700423 time_t rawtime;
424 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700425
Romain Guy98e10fd2009-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 Guy39dbc802009-07-31 11:20:59 -0700435}
436
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700437static int32_t SC_day()
Romain Guy39dbc802009-07-31 11:20:59 -0700438{
439 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700440
Romain Guy39dbc802009-07-31 11:20:59 -0700441 time_t rawtime;
442 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700443
Romain Guy39dbc802009-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 Samse5ffb872009-08-09 17:01:55 -0700453}
Jason Samse45ac6e2009-07-20 14:31:06 -0700454
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700455static int32_t SC_month()
Romain Guy39dbc802009-07-31 11:20:59 -0700456{
457 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700458
Romain Guy39dbc802009-07-31 11:20:59 -0700459 time_t rawtime;
460 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700461
Romain Guy39dbc802009-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 Samse5ffb872009-08-09 17:01:55 -0700471}
Romain Guy39dbc802009-07-31 11:20:59 -0700472
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700473static int32_t SC_year()
Romain Guy39dbc802009-07-31 11:20:59 -0700474{
475 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700476
Romain Guy39dbc802009-07-31 11:20:59 -0700477 time_t rawtime;
478 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700479
Romain Guy39dbc802009-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 Onorato9c4e4ca2009-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 Samse45ac6e2009-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 Sams90b36a82009-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 Samse45ac6e2009-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 Samsb5909ce2009-07-21 12:20:54 -0700630static void SC_bindProgramVertex(RsProgramVertex pv)
631{
632 GET_TLS();
633 rsi_ContextBindProgramVertex(rsc, pv);
634
635}
Jason Samse45ac6e2009-07-20 14:31:06 -0700636
637//////////////////////////////////////////////////////////////////////////////
Jason Samsc9d43db2009-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 Samse45ac6e2009-07-20 14:31:06 -0700656// Drawing
657//////////////////////////////////////////////////////////////////////////////
658
Romain Guyd369e272009-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 Samsb681c8a2009-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 Guyfcc1c2b2009-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 Samse45ac6e2009-07-20 14:31:06 -0700702{
703 GET_TLS();
Jason Samse579df42009-08-10 14:55:26 -0700704
Jason Samse45ac6e2009-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 Samse579df42009-08-10 14:55:26 -0700710
Jason Samse45ac6e2009-07-20 14:31:06 -0700711 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700712 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
Jason Samse45ac6e2009-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 Guyfcc1c2b2009-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 Sams3a97c592009-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 Samse9f5c532009-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 Samse5ffb872009-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 Samse45ac6e2009-07-20 14:31:06 -0700817//////////////////////////////////////////////////////////////////////////////
818//
819//////////////////////////////////////////////////////////////////////////////
820
Jason Samse45ac6e2009-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 Guy48b7edc2009-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 Guyd369e272009-08-07 15:40:32 -0700850static void SC_shininess(float s)
Romain Guy48b7edc2009-08-06 22:52:13 -0700851{
Romain Guyd369e272009-08-07 15:40:32 -0700852 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, s);
Romain Guy48b7edc2009-08-06 22:52:13 -0700853}
854
Romain Guye62cc902009-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 Guy370ed152009-08-20 17:08:33 -0700861static void SC_hsbToRgb(float h, float s, float b, float* rgb)
Romain Guy9c59d022009-07-31 15:33:59 -0700862{
863 float red = 0.0f;
864 float green = 0.0f;
865 float blue = 0.0f;
Jason Samse5ffb872009-08-09 17:01:55 -0700866
Romain Guy9c59d022009-07-31 15:33:59 -0700867 float x = h;
868 float y = s;
869 float z = b;
Jason Samse5ffb872009-08-09 17:01:55 -0700870
Romain Guy9c59d022009-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 Samse5ffb872009-08-09 17:01:55 -0700877
Romain Guy9c59d022009-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 Samse5ffb872009-08-09 17:01:55 -0700910
Romain Guy370ed152009-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 Guy9c59d022009-07-31 15:33:59 -0700931}
932
Jason Samsc9d43db2009-07-28 12:02:16 -0700933static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samse45ac6e2009-07-20 14:31:06 -0700934{
935 GET_TLS();
936 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
937}
938
Jason Samse5ffb872009-08-09 17:01:55 -0700939static void SC_uploadToBufferObject(RsAllocation va)
940{
941 GET_TLS();
942 rsi_AllocationUploadToBufferObject(rsc, va);
943}
944
Jason Samse45ac6e2009-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 Samsc9d43db2009-07-28 12:02:16 -0700955static void SC_debugF(const char *s, float f)
956{
957 LOGE("%s %f", s, f);
958}
959
Romain Guy370ed152009-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 Samsc9d43db2009-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 Guy370ed152009-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 Samse579df42009-08-10 14:55:26 -0700975static uint32_t SC_getWidth()
976{
977 GET_TLS();
978 return rsc->getWidth();
979}
Jason Samse45ac6e2009-07-20 14:31:06 -0700980
Jason Samse579df42009-08-10 14:55:26 -0700981static uint32_t SC_getHeight()
982{
983 GET_TLS();
984 return rsc->getHeight();
985}
Jason Samse45ac6e2009-07-20 14:31:06 -0700986
Jason Sams90b36a82009-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 Sams8c401ef2009-10-06 13:58:47 -07001005static uint32_t SC_toClient(void *data, int cmdID, int len, int waitForSpace)
1006{
1007 GET_TLS();
1008 return rsc->sendMessageToClient(data, cmdID, len, waitForSpace != 0);
1009}
1010
Jason Sams3a27c952009-10-07 18:14:01 -07001011static void SC_scriptCall(int scriptID)
1012{
1013 GET_TLS();
1014 rsc->runScript((Script *)scriptID, 0);
1015}
1016
1017
Jason Samse45ac6e2009-07-20 14:31:06 -07001018//////////////////////////////////////////////////////////////////////////////
1019// Class implementation
1020//////////////////////////////////////////////////////////////////////////////
1021
1022ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
1023 // IO
1024 { "loadI32", (void *)&SC_loadI32,
1025 "int", "(int, int)" },
1026 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
1027 { "loadF", (void *)&SC_loadF,
1028 "float", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -07001029 { "loadArrayF", (void *)&SC_loadArrayF,
Romain Guy06f7c932009-08-06 12:40:41 -07001030 "float*", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -07001031 { "loadArrayI32", (void *)&SC_loadArrayI32,
Romain Guy06f7c932009-08-06 12:40:41 -07001032 "int*", "(int, int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001033 { "loadVec4", (void *)&SC_loadVec4,
1034 "void", "(int, int, float *)" },
1035 { "loadMatrix", (void *)&SC_loadMatrix,
1036 "void", "(int, int, float *)" },
1037 { "storeI32", (void *)&SC_storeI32,
1038 "void", "(int, int, int)" },
1039 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
1040 { "storeF", (void *)&SC_storeF,
1041 "void", "(int, int, float)" },
1042 { "storeVec4", (void *)&SC_storeVec4,
1043 "void", "(int, int, float *)" },
1044 { "storeMatrix", (void *)&SC_storeMatrix,
1045 "void", "(int, int, float *)" },
Jason Samsa2b54c42009-09-23 16:38:37 -07001046 { "loadSimpleMeshVerticesF", (void *)&SC_loadSimpleMeshVerticesF,
1047 "float*", "(int, int)" },
1048 { "updateSimpleMesh", (void *)&SC_updateSimpleMesh,
Romain Guy48b7edc2009-08-06 22:52:13 -07001049 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001050
1051 // math
Romain Guy27162ab2009-08-09 17:04:54 -07001052 { "modf", (void *)&fmod,
1053 "float", "(float, float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -07001054 { "abs", (void *)&abs,
1055 "int", "(int)" },
Romain Guyc5174c72009-09-29 13:17:27 -07001056 { "absf", (void *)&fabsf,
Romain Guyfcc1c2b2009-08-08 18:30:19 -07001057 "float", "(float)" },
Romain Guy2275d632009-08-18 11:39:17 -07001058 { "sinf_fast", (void *)&SC_sinf_fast,
1059 "float", "(float)" },
1060 { "cosf_fast", (void *)&SC_cosf_fast,
1061 "float", "(float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001062 { "sinf", (void *)&sinf,
1063 "float", "(float)" },
1064 { "cosf", (void *)&cosf,
1065 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001066 { "asinf", (void *)&asinf,
1067 "float", "(float)" },
1068 { "acosf", (void *)&acosf,
1069 "float", "(float)" },
1070 { "atanf", (void *)&atanf,
1071 "float", "(float)" },
1072 { "atan2f", (void *)&atan2f,
Romain Guy9c59d022009-07-31 15:33:59 -07001073 "float", "(float, float)" },
Jason Samse9f5c532009-07-28 17:20:11 -07001074 { "fabsf", (void *)&fabsf,
Jason Samse45ac6e2009-07-20 14:31:06 -07001075 "float", "(float)" },
1076 { "randf", (void *)&SC_randf,
1077 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001078 { "randf2", (void *)&SC_randf2,
1079 "float", "(float, float)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001080 { "floorf", (void *)&floorf,
1081 "float", "(float)" },
Jason Samsdac98f52009-09-18 14:24:24 -07001082 { "fracf", (void *)&SC_fracf,
1083 "float", "(float)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001084 { "ceilf", (void *)&ceilf,
1085 "float", "(float)" },
Jason Samsdac98f52009-09-18 14:24:24 -07001086 { "roundf", (void *)&SC_roundf,
1087 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001088 { "expf", (void *)&expf,
1089 "float", "(float)" },
1090 { "logf", (void *)&logf,
1091 "float", "(float)" },
1092 { "powf", (void *)&powf,
1093 "float", "(float, float)" },
1094 { "maxf", (void *)&SC_maxf,
1095 "float", "(float, float)" },
1096 { "minf", (void *)&SC_minf,
1097 "float", "(float, float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -07001098 { "sqrt", (void *)&sqrt,
1099 "int", "(int)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001100 { "sqrtf", (void *)&sqrtf,
1101 "float", "(float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -07001102 { "sqr", (void *)&SC_sqr,
1103 "int", "(int)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001104 { "sqrf", (void *)&SC_sqrf,
1105 "float", "(float)" },
Romain Guy27162ab2009-08-09 17:04:54 -07001106 { "clamp", (void *)&SC_clamp,
1107 "int", "(int, int, int)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001108 { "clampf", (void *)&SC_clampf,
1109 "float", "(float, float, float)" },
1110 { "distf2", (void *)&SC_distf2,
1111 "float", "(float, float, float, float)" },
1112 { "distf3", (void *)&SC_distf3,
1113 "float", "(float, float, float, float, float, float)" },
1114 { "magf2", (void *)&SC_magf2,
1115 "float", "(float, float)" },
1116 { "magf3", (void *)&SC_magf3,
1117 "float", "(float, float, float)" },
1118 { "radf", (void *)&SC_radf,
1119 "float", "(float)" },
1120 { "degf", (void *)&SC_degf,
1121 "float", "(float)" },
1122 { "lerpf", (void *)&SC_lerpf,
1123 "float", "(float, float, float)" },
1124 { "normf", (void *)&SC_normf,
1125 "float", "(float, float, float)" },
1126 { "mapf", (void *)&SC_mapf,
1127 "float", "(float, float, float, float, float)" },
Romain Guyb7f1a6d2009-08-03 21:12:51 -07001128 { "noisef", (void *)&SC_noisef,
1129 "float", "(float)" },
1130 { "noisef2", (void *)&SC_noisef2,
1131 "float", "(float, float)" },
1132 { "noisef3", (void *)&SC_noisef3,
1133 "float", "(float, float, float)" },
1134 { "turbulencef2", (void *)&SC_turbulencef2,
1135 "float", "(float, float, float)" },
1136 { "turbulencef3", (void *)&SC_turbulencef3,
1137 "float", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001138
Romain Guy98e10fd2009-07-30 18:45:01 -07001139 // time
1140 { "second", (void *)&SC_second,
1141 "int", "()" },
1142 { "minute", (void *)&SC_minute,
1143 "int", "()" },
1144 { "hour", (void *)&SC_hour,
1145 "int", "()" },
Romain Guy39dbc802009-07-31 11:20:59 -07001146 { "day", (void *)&SC_day,
1147 "int", "()" },
1148 { "month", (void *)&SC_month,
1149 "int", "()" },
1150 { "year", (void *)&SC_year,
1151 "int", "()" },
Joe Onorato9c4e4ca2009-08-09 11:39:02 -07001152 { "uptimeMillis", (void*)&SC_uptimeMillis,
1153 "int", "()" }, // TODO: use long instead
1154 { "startTimeMillis", (void*)&SC_startTimeMillis,
1155 "int", "()" }, // TODO: use long instead
1156 { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis,
1157 "int", "()" }, // TODO: use long instead
Romain Guy98e10fd2009-07-30 18:45:01 -07001158
Jason Samse45ac6e2009-07-20 14:31:06 -07001159 // matrix
1160 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
1161 "void", "(float *mat)" },
1162 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
1163 "void", "(float *mat, float *f)" },
1164 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
1165 "void", "(float *mat, float *newmat)" },
1166 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
1167 "void", "(float *mat, float rot, float x, float y, float z)" },
1168 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
1169 "void", "(float *mat, float x, float y, float z)" },
1170 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
1171 "void", "(float *mat, float x, float y, float z)" },
1172 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
1173 "void", "(float *mat, float *lhs, float *rhs)" },
1174 { "matrixMultiply", (void *)&SC_matrixMultiply,
1175 "void", "(float *mat, float *rhs)" },
1176 { "matrixRotate", (void *)&SC_matrixRotate,
1177 "void", "(float *mat, float rot, float x, float y, float z)" },
1178 { "matrixScale", (void *)&SC_matrixScale,
1179 "void", "(float *mat, float x, float y, float z)" },
1180 { "matrixTranslate", (void *)&SC_matrixTranslate,
1181 "void", "(float *mat, float x, float y, float z)" },
1182
Jason Sams90b36a82009-08-17 13:56:09 -07001183 // vector
1184 { "vec2Rand", (void *)&SC_vec2Rand,
1185 "void", "(float *vec, float maxLen)" },
1186
Jason Samsa57c0a72009-09-04 14:42:41 -07001187 // vec3
1188 { "vec3Norm", (void *)&SC_vec3Norm,
1189 "void", "(struct vec3_s *)" },
1190 { "vec3Length", (void *)&SC_vec3Length,
1191 "float", "(struct vec3_s *)" },
1192 { "vec3Add", (void *)&SC_vec3Add,
1193 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1194 { "vec3Sub", (void *)&SC_vec3Sub,
1195 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1196 { "vec3Cross", (void *)&SC_vec3Cross,
1197 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1198 { "vec3Dot", (void *)&SC_vec3Dot,
1199 "float", "(struct vec3_s *lhs, struct vec3_s *rhs)" },
1200 { "vec3Scale", (void *)&SC_vec3Scale,
1201 "void", "(struct vec3_s *lhs, float scale)" },
1202
Jason Samse45ac6e2009-07-20 14:31:06 -07001203 // context
1204 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
1205 "void", "(int)" },
1206 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
1207 "void", "(int)" },
Jason Samsb681c8a2009-09-28 18:12:56 -07001208 { "bindProgramStore", (void *)&SC_bindProgramFragmentStore,
1209 "void", "(int)" },
Jason Samsb5909ce2009-07-21 12:20:54 -07001210 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
1211 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001212 { "bindSampler", (void *)&SC_bindSampler,
1213 "void", "(int, int, int)" },
1214 { "bindTexture", (void *)&SC_bindTexture,
1215 "void", "(int, int, int)" },
1216
Jason Samsc9d43db2009-07-28 12:02:16 -07001217 // vp
Jason Sams50253db2009-07-29 20:55:44 -07001218 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -07001219 "void", "(void *)" },
Jason Sams50253db2009-07-29 20:55:44 -07001220 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -07001221 "void", "(void *)" },
1222
1223
1224
Jason Samse45ac6e2009-07-20 14:31:06 -07001225 // drawing
Jason Samse9f5c532009-07-28 17:20:11 -07001226 { "drawRect", (void *)&SC_drawRect,
1227 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001228 { "drawQuad", (void *)&SC_drawQuad,
1229 "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 Guyfcc1c2b2009-08-08 18:30:19 -07001230 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords,
1231 "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 Sams3a97c592009-09-30 17:36:20 -07001232 { "drawSprite", (void *)&SC_drawSprite,
1233 "void", "(float x, float y, float z, float w, float h)" },
1234 { "drawSpriteScreenspace", (void *)&SC_drawSpriteScreenspace,
1235 "void", "(float x, float y, float z, float w, float h)" },
Romain Guyd369e272009-08-07 15:40:32 -07001236 { "drawLine", (void *)&SC_drawLine,
1237 "void", "(float x1, float y1, float z1, float x2, float y2, float z2)" },
Jason Samsb681c8a2009-09-28 18:12:56 -07001238 { "drawPoint", (void *)&SC_drawPoint,
1239 "void", "(float x1, float y1, float z1)" },
Jason Samse5ffb872009-08-09 17:01:55 -07001240 { "drawSimpleMesh", (void *)&SC_drawSimpleMesh,
1241 "void", "(int ism)" },
1242 { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange,
1243 "void", "(int ism, int start, int len)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001244
1245
1246 // misc
1247 { "pfClearColor", (void *)&SC_ClearColor,
1248 "void", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001249 { "color", (void *)&SC_color,
1250 "void", "(float, float, float, float)" },
Romain Guy9c59d022009-07-31 15:33:59 -07001251 { "hsb", (void *)&SC_hsb,
1252 "void", "(float, float, float, float)" },
Romain Guy370ed152009-08-20 17:08:33 -07001253 { "hsbToRgb", (void *)&SC_hsbToRgb,
1254 "void", "(float, float, float, float*)" },
1255 { "hsbToAbgr", (void *)&SC_hsbToAbgr,
1256 "int", "(float, float, float, float)" },
Romain Guy48b7edc2009-08-06 22:52:13 -07001257 { "ambient", (void *)&SC_ambient,
1258 "void", "(float, float, float, float)" },
1259 { "diffuse", (void *)&SC_diffuse,
1260 "void", "(float, float, float, float)" },
1261 { "specular", (void *)&SC_specular,
1262 "void", "(float, float, float, float)" },
1263 { "emission", (void *)&SC_emission,
1264 "void", "(float, float, float, float)" },
1265 { "shininess", (void *)&SC_shininess,
Romain Guyd369e272009-08-07 15:40:32 -07001266 "void", "(float)" },
Romain Guye62cc902009-09-04 17:55:41 -07001267 { "pointAttenuation", (void *)&SC_pointAttenuation,
1268 "void", "(float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001269
Jason Samsc9d43db2009-07-28 12:02:16 -07001270 { "uploadToTexture", (void *)&SC_uploadToTexture,
1271 "void", "(int, int)" },
Jason Samse5ffb872009-08-09 17:01:55 -07001272 { "uploadToBufferObject", (void *)&SC_uploadToBufferObject,
1273 "void", "(int)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001274
Jason Sams90b36a82009-08-17 13:56:09 -07001275 { "colorFloatRGBAtoUNorm8", (void *)&SC_colorFloatRGBAtoUNorm8,
1276 "int", "(float, float, float, float)" },
1277 { "colorFloatRGBto565", (void *)&SC_colorFloatRGBAto565,
1278 "int", "(float, float, float)" },
1279
1280
Jason Samse579df42009-08-10 14:55:26 -07001281 { "getWidth", (void *)&SC_getWidth,
1282 "int", "()" },
1283 { "getHeight", (void *)&SC_getHeight,
1284 "int", "()" },
1285
Jason Sams8c401ef2009-10-06 13:58:47 -07001286 { "sendToClient", (void *)&SC_toClient,
1287 "int", "(void *data, int cmdID, int len, int waitForSpace)" },
Jason Samse579df42009-08-10 14:55:26 -07001288
Jason Samsc9d43db2009-07-28 12:02:16 -07001289
1290 { "debugF", (void *)&SC_debugF,
1291 "void", "(void *, float)" },
1292 { "debugI32", (void *)&SC_debugI32,
1293 "void", "(void *, int)" },
Romain Guy370ed152009-08-20 17:08:33 -07001294 { "debugHexF", (void *)&SC_debugHexF,
1295 "void", "(void *, float)" },
1296 { "debugHexI32", (void *)&SC_debugHexI32,
1297 "void", "(void *, int)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001298
Jason Sams3a27c952009-10-07 18:14:01 -07001299 { "scriptCall", (void *)&SC_scriptCall,
1300 "void", "(int)" },
1301
Jason Samsc9d43db2009-07-28 12:02:16 -07001302
Jason Samse45ac6e2009-07-20 14:31:06 -07001303 { NULL, NULL, NULL, NULL }
1304};
1305
1306const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
1307{
1308 ScriptCState::SymbolTable_t *syms = gSyms;
1309
1310 while (syms->mPtr) {
1311 if (!strcmp(syms->mName, sym)) {
1312 return syms;
1313 }
1314 syms++;
1315 }
1316 return NULL;
1317}
1318
1319void ScriptCState::appendDecls(String8 *str)
1320{
1321 ScriptCState::SymbolTable_t *syms = gSyms;
1322 while (syms->mPtr) {
1323 str->append(syms->mRet);
1324 str->append(" ");
1325 str->append(syms->mName);
1326 str->append(syms->mParam);
1327 str->append(";\n");
1328 syms++;
1329 }
1330}
1331
1332