blob: 5b19f178db0aeaefd66edba61ddcf0bf08092cf4 [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
25#include <GLES/gl.h>
26#include <GLES/glext.h>
27
Romain Guy98e10fd2009-07-30 18:45:01 -070028#include <time.h>
29#include <cutils/tztime.h>
30
Jason Samse45ac6e2009-07-20 14:31:06 -070031using namespace android;
32using namespace android::renderscript;
33
34#define GET_TLS() Context::ScriptTLSStruct * tls = \
35 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
36 Context * rsc = tls->mContext; \
37 ScriptC * sc = (ScriptC *) tls->mScript
38
Jason Samsa57c0a72009-09-04 14:42:41 -070039typedef struct {
40 float x;
41 float y;
42 float z;
43} vec3_t;
44
45typedef struct {
46 float x;
47 float y;
48 float z;
49 float w;
50} vec4_t;
51
52typedef struct {
53 float x;
54 float y;
55} vec2_t;
Jason Samse45ac6e2009-07-20 14:31:06 -070056
57//////////////////////////////////////////////////////////////////////////////
58// IO routines
59//////////////////////////////////////////////////////////////////////////////
60
61static float SC_loadF(uint32_t bank, uint32_t offset)
62{
63 GET_TLS();
64 const void *vp = sc->mSlots[bank]->getPtr();
65 const float *f = static_cast<const float *>(vp);
66 //LOGE("loadF %i %i = %f %x", bank, offset, f, ((int *)&f)[0]);
67 return f[offset];
68}
69
70static int32_t SC_loadI32(uint32_t bank, uint32_t offset)
71{
72 GET_TLS();
73 const void *vp = sc->mSlots[bank]->getPtr();
74 const int32_t *i = static_cast<const int32_t *>(vp);
75 //LOGE("loadI32 %i %i = %i", bank, offset, t);
76 return i[offset];
77}
78
Romain Guy06f7c932009-08-06 12:40:41 -070079static float* SC_loadArrayF(uint32_t bank, uint32_t offset)
Romain Guy36401002009-08-04 17:19:48 -070080{
81 GET_TLS();
82 void *vp = sc->mSlots[bank]->getPtr();
83 float *f = static_cast<float *>(vp);
Romain Guy06f7c932009-08-06 12:40:41 -070084 return f + offset;
Romain Guy36401002009-08-04 17:19:48 -070085}
86
Romain Guy06f7c932009-08-06 12:40:41 -070087static int32_t* SC_loadArrayI32(uint32_t bank, uint32_t offset)
Romain Guy36401002009-08-04 17:19:48 -070088{
89 GET_TLS();
90 void *vp = sc->mSlots[bank]->getPtr();
91 int32_t *i = static_cast<int32_t *>(vp);
Romain Guy06f7c932009-08-06 12:40:41 -070092 return i + offset;
Romain Guy36401002009-08-04 17:19:48 -070093}
94
Romain Guy48b7edc2009-08-06 22:52:13 -070095static float* SC_loadTriangleMeshVerticesF(RsTriangleMesh mesh)
96{
97 TriangleMesh *tm = static_cast<TriangleMesh *>(mesh);
98 void *vp = tm->mVertexData;
99 float *f = static_cast<float *>(vp);
100 return f;
101}
102
103static void SC_updateTriangleMesh(RsTriangleMesh mesh)
104{
105 TriangleMesh *tm = static_cast<TriangleMesh *>(mesh);
106 glBindBuffer(GL_ARRAY_BUFFER, tm->mBufferObjects[0]);
107 glBufferData(GL_ARRAY_BUFFER, tm->mVertexDataSize, tm->mVertexData, GL_STATIC_DRAW);
108 glBindBuffer(GL_ARRAY_BUFFER, 0);
Jason Samse5ffb872009-08-09 17:01:55 -0700109
Romain Guy48b7edc2009-08-06 22:52:13 -0700110 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
111 glBufferData(GL_ELEMENT_ARRAY_BUFFER, tm->mIndexDataSize, tm->mIndexData, GL_STATIC_DRAW);
Jason Samse5ffb872009-08-09 17:01:55 -0700112 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy48b7edc2009-08-06 22:52:13 -0700113}
Romain Guy06f7c932009-08-06 12:40:41 -0700114
Jason Samse45ac6e2009-07-20 14:31:06 -0700115static uint32_t SC_loadU32(uint32_t bank, uint32_t offset)
116{
117 GET_TLS();
118 const void *vp = sc->mSlots[bank]->getPtr();
119 const uint32_t *i = static_cast<const uint32_t *>(vp);
120 return i[offset];
121}
122
123static void SC_loadVec4(uint32_t bank, uint32_t offset, rsc_Vector4 *v)
124{
125 GET_TLS();
126 const void *vp = sc->mSlots[bank]->getPtr();
127 const float *f = static_cast<const float *>(vp);
128 memcpy(v, &f[offset], sizeof(rsc_Vector4));
129}
130
131static void SC_loadMatrix(uint32_t bank, uint32_t offset, rsc_Matrix *m)
132{
133 GET_TLS();
134 const void *vp = sc->mSlots[bank]->getPtr();
135 const float *f = static_cast<const float *>(vp);
136 memcpy(m, &f[offset], sizeof(rsc_Matrix));
137}
138
139
140static void SC_storeF(uint32_t bank, uint32_t offset, float v)
141{
142 //LOGE("storeF %i %i %f", bank, offset, v);
143 GET_TLS();
144 void *vp = sc->mSlots[bank]->getPtr();
145 float *f = static_cast<float *>(vp);
146 f[offset] = v;
147}
148
149static void SC_storeI32(uint32_t bank, uint32_t offset, int32_t v)
150{
151 GET_TLS();
152 void *vp = sc->mSlots[bank]->getPtr();
153 int32_t *f = static_cast<int32_t *>(vp);
154 static_cast<int32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
155}
156
157static void SC_storeU32(uint32_t bank, uint32_t offset, uint32_t v)
158{
159 GET_TLS();
160 void *vp = sc->mSlots[bank]->getPtr();
161 uint32_t *f = static_cast<uint32_t *>(vp);
162 static_cast<uint32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
163}
164
165static void SC_storeVec4(uint32_t bank, uint32_t offset, const rsc_Vector4 *v)
166{
167 GET_TLS();
168 void *vp = sc->mSlots[bank]->getPtr();
169 float *f = static_cast<float *>(vp);
170 memcpy(&f[offset], v, sizeof(rsc_Vector4));
171}
172
173static void SC_storeMatrix(uint32_t bank, uint32_t offset, const rsc_Matrix *m)
174{
175 GET_TLS();
176 void *vp = sc->mSlots[bank]->getPtr();
177 float *f = static_cast<float *>(vp);
178 memcpy(&f[offset], m, sizeof(rsc_Matrix));
179}
180
Jason Samsa57c0a72009-09-04 14:42:41 -0700181//////////////////////////////////////////////////////////////////////////////
182// Vec3 routines
183//////////////////////////////////////////////////////////////////////////////
184
185static void SC_vec3Norm(vec3_t *v)
186{
187 float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
188 len = 1 / len;
189 v->x *= len;
190 v->y *= len;
191 v->z *= len;
192}
193
194static float SC_vec3Length(const vec3_t *v)
195{
196 return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
197}
198
199static void SC_vec3Add(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
200{
201 dest->x = lhs->x + rhs->x;
202 dest->y = lhs->y + rhs->y;
203 dest->z = lhs->z + rhs->z;
204}
205
206static void SC_vec3Sub(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
207{
208 dest->x = lhs->x - rhs->x;
209 dest->y = lhs->y - rhs->y;
210 dest->z = lhs->z - rhs->z;
211}
212
213static void SC_vec3Cross(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
214{
215 float x = lhs->y * rhs->z - lhs->z * rhs->y;
216 float y = lhs->z * rhs->x - lhs->x * rhs->z;
217 float z = lhs->x * rhs->y - lhs->y * rhs->x;
218 dest->x = x;
219 dest->y = y;
220 dest->z = z;
221}
222
223static float SC_vec3Dot(const vec3_t *lhs, const vec3_t *rhs)
224{
225 return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z;
226}
227
228static void SC_vec3Scale(vec3_t *lhs, float scale)
229{
230 lhs->x *= scale;
231 lhs->y *= scale;
232 lhs->z *= scale;
233}
234
Jason Samse45ac6e2009-07-20 14:31:06 -0700235
236//////////////////////////////////////////////////////////////////////////////
237// Math routines
238//////////////////////////////////////////////////////////////////////////////
239
Romain Guy39dbc802009-07-31 11:20:59 -0700240#define PI 3.1415926f
241#define DEG_TO_RAD PI / 180.0f
242#define RAD_TO_DEG 180.0f / PI
243
Romain Guy2275d632009-08-18 11:39:17 -0700244static float SC_sinf_fast(float x)
245{
246 const float A = 1.0f / (2.0f * M_PI);
247 const float B = -16.0f;
248 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -0700249
Romain Guy2275d632009-08-18 11:39:17 -0700250 // scale angle for easy argument reduction
251 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -0700252
Romain Guy2275d632009-08-18 11:39:17 -0700253 if (fabsf(x) >= 0.5f) {
254 // argument reduction
255 x = x - ceilf(x + 0.5f) + 1.0f;
256 }
Jason Samsa57c0a72009-09-04 14:42:41 -0700257
Romain Guy2275d632009-08-18 11:39:17 -0700258 const float y = B * x * fabsf(x) + C * x;
259 return 0.2215f * (y * fabsf(y) - y) + y;
260}
261
262static float SC_cosf_fast(float x)
263{
264 x += float(M_PI / 2);
265
266 const float A = 1.0f / (2.0f * M_PI);
267 const float B = -16.0f;
268 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -0700269
Romain Guy2275d632009-08-18 11:39:17 -0700270 // scale angle for easy argument reduction
271 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -0700272
Romain Guy2275d632009-08-18 11:39:17 -0700273 if (fabsf(x) >= 0.5f) {
274 // argument reduction
275 x = x - ceilf(x + 0.5f) + 1.0f;
276 }
Jason Samsa57c0a72009-09-04 14:42:41 -0700277
Romain Guy2275d632009-08-18 11:39:17 -0700278 const float y = B * x * fabsf(x) + C * x;
279 return 0.2215f * (y * fabsf(y) - y) + y;
280}
281
Jason Samse45ac6e2009-07-20 14:31:06 -0700282static float SC_randf(float max)
283{
284 float r = (float)rand();
285 return r / RAND_MAX * max;
286}
287
Romain Guy39dbc802009-07-31 11:20:59 -0700288static float SC_randf2(float min, float max)
289{
290 float r = (float)rand();
291 return r / RAND_MAX * (max - min) + min;
292}
293
294static float SC_clampf(float amount, float low, float high)
295{
296 return amount < low ? low : (amount > high ? high : amount);
297}
298
Romain Guy27162ab2009-08-09 17:04:54 -0700299static int SC_clamp(int amount, int low, int high)
300{
301 return amount < low ? low : (amount > high ? high : amount);
302}
303
Romain Guy39dbc802009-07-31 11:20:59 -0700304static float SC_maxf(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_minf(float a, float b)
310{
Jason Samse5ffb872009-08-09 17:01:55 -0700311 return a < b ? a : b;
Romain Guy39dbc802009-07-31 11:20:59 -0700312}
313
314static float SC_sqrf(float v)
315{
Jason Samse5ffb872009-08-09 17:01:55 -0700316 return v * v;
Romain Guy39dbc802009-07-31 11:20:59 -0700317}
318
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700319static int SC_sqr(int v)
320{
321 return v * v;
322}
323
Romain Guy39dbc802009-07-31 11:20:59 -0700324static float SC_distf2(float x1, float y1, float x2, float y2)
325{
326 float x = x2 - x1;
327 float y = y2 - y1;
Jason Samse5ffb872009-08-09 17:01:55 -0700328 return sqrtf(x * x + y * y);
Romain Guy39dbc802009-07-31 11:20:59 -0700329}
330
331static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2)
332{
333 float x = x2 - x1;
334 float y = y2 - y1;
335 float z = z2 - z1;
Jason Samse5ffb872009-08-09 17:01:55 -0700336 return sqrtf(x * x + y * y + z * z);
Romain Guy39dbc802009-07-31 11:20:59 -0700337}
338
339static float SC_magf2(float a, float b)
340{
341 return sqrtf(a * a + b * b);
342}
343
344static float SC_magf3(float a, float b, float c)
345{
346 return sqrtf(a * a + b * b + c * c);
347}
348
349static float SC_radf(float degrees)
350{
Jason Samse5ffb872009-08-09 17:01:55 -0700351 return degrees * DEG_TO_RAD;
Romain Guy39dbc802009-07-31 11:20:59 -0700352}
353
354static float SC_degf(float radians)
355{
Jason Samse5ffb872009-08-09 17:01:55 -0700356 return radians * RAD_TO_DEG;
Romain Guy39dbc802009-07-31 11:20:59 -0700357}
358
359static float SC_lerpf(float start, float stop, float amount)
360{
361 return start + (stop - start) * amount;
362}
363
364static float SC_normf(float start, float stop, float value)
365{
366 return (value - start) / (stop - start);
367}
368
369static float SC_mapf(float minStart, float minStop, float maxStart, float maxStop, float value)
370{
371 return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
372}
Jason Samse45ac6e2009-07-20 14:31:06 -0700373
Romain Guy98e10fd2009-07-30 18:45:01 -0700374//////////////////////////////////////////////////////////////////////////////
375// Time routines
376//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700377
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700378static int32_t SC_second()
Romain Guy98e10fd2009-07-30 18:45:01 -0700379{
380 GET_TLS();
381
382 time_t rawtime;
383 time(&rawtime);
384
385 if (sc->mEnviroment.mTimeZone) {
386 struct tm timeinfo;
387 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
388 return timeinfo.tm_sec;
389 } else {
390 struct tm *timeinfo;
391 timeinfo = localtime(&rawtime);
392 return timeinfo->tm_sec;
393 }
394}
395
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700396static int32_t SC_minute()
Romain Guy98e10fd2009-07-30 18:45:01 -0700397{
398 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700399
Romain Guy98e10fd2009-07-30 18:45:01 -0700400 time_t rawtime;
401 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700402
Romain Guy98e10fd2009-07-30 18:45:01 -0700403 if (sc->mEnviroment.mTimeZone) {
404 struct tm timeinfo;
405 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
406 return timeinfo.tm_min;
407 } else {
408 struct tm *timeinfo;
409 timeinfo = localtime(&rawtime);
410 return timeinfo->tm_min;
411 }
Jason Samse5ffb872009-08-09 17:01:55 -0700412}
Romain Guy98e10fd2009-07-30 18:45:01 -0700413
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700414static int32_t SC_hour()
Romain Guy98e10fd2009-07-30 18:45:01 -0700415{
416 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700417
Romain Guy98e10fd2009-07-30 18:45:01 -0700418 time_t rawtime;
419 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700420
Romain Guy98e10fd2009-07-30 18:45:01 -0700421 if (sc->mEnviroment.mTimeZone) {
422 struct tm timeinfo;
423 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
424 return timeinfo.tm_hour;
425 } else {
426 struct tm *timeinfo;
427 timeinfo = localtime(&rawtime);
428 return timeinfo->tm_hour;
429 }
Romain Guy39dbc802009-07-31 11:20:59 -0700430}
431
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700432static int32_t SC_day()
Romain Guy39dbc802009-07-31 11:20:59 -0700433{
434 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700435
Romain Guy39dbc802009-07-31 11:20:59 -0700436 time_t rawtime;
437 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700438
Romain Guy39dbc802009-07-31 11:20:59 -0700439 if (sc->mEnviroment.mTimeZone) {
440 struct tm timeinfo;
441 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
442 return timeinfo.tm_mday;
443 } else {
444 struct tm *timeinfo;
445 timeinfo = localtime(&rawtime);
446 return timeinfo->tm_mday;
447 }
Jason Samse5ffb872009-08-09 17:01:55 -0700448}
Jason Samse45ac6e2009-07-20 14:31:06 -0700449
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700450static int32_t SC_month()
Romain Guy39dbc802009-07-31 11:20:59 -0700451{
452 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700453
Romain Guy39dbc802009-07-31 11:20:59 -0700454 time_t rawtime;
455 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700456
Romain Guy39dbc802009-07-31 11:20:59 -0700457 if (sc->mEnviroment.mTimeZone) {
458 struct tm timeinfo;
459 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
460 return timeinfo.tm_mon;
461 } else {
462 struct tm *timeinfo;
463 timeinfo = localtime(&rawtime);
464 return timeinfo->tm_mon;
465 }
Jason Samse5ffb872009-08-09 17:01:55 -0700466}
Romain Guy39dbc802009-07-31 11:20:59 -0700467
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700468static int32_t SC_year()
Romain Guy39dbc802009-07-31 11:20:59 -0700469{
470 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700471
Romain Guy39dbc802009-07-31 11:20:59 -0700472 time_t rawtime;
473 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700474
Romain Guy39dbc802009-07-31 11:20:59 -0700475 if (sc->mEnviroment.mTimeZone) {
476 struct tm timeinfo;
477 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
478 return timeinfo.tm_year;
479 } else {
480 struct tm *timeinfo;
481 timeinfo = localtime(&rawtime);
482 return timeinfo->tm_year;
483 }
484}
485
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700486static int32_t SC_uptimeMillis()
487{
488 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
489}
490
491static int32_t SC_startTimeMillis()
492{
493 GET_TLS();
494 return sc->mEnviroment.mStartTimeMillis;
495}
496
497static int32_t SC_elapsedTimeMillis()
498{
499 GET_TLS();
500 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC))
501 - sc->mEnviroment.mStartTimeMillis;
502}
503
Jason Samse45ac6e2009-07-20 14:31:06 -0700504//////////////////////////////////////////////////////////////////////////////
505// Matrix routines
506//////////////////////////////////////////////////////////////////////////////
507
508
509static void SC_matrixLoadIdentity(rsc_Matrix *mat)
510{
511 Matrix *m = reinterpret_cast<Matrix *>(mat);
512 m->loadIdentity();
513}
514
515static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
516{
517 Matrix *m = reinterpret_cast<Matrix *>(mat);
518 m->load(f);
519}
520
521static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
522{
523 Matrix *m = reinterpret_cast<Matrix *>(mat);
524 m->load(reinterpret_cast<const Matrix *>(newmat));
525}
526
527static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
528{
529 Matrix *m = reinterpret_cast<Matrix *>(mat);
530 m->loadRotate(rot, x, y, z);
531}
532
533static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
534{
535 Matrix *m = reinterpret_cast<Matrix *>(mat);
536 m->loadScale(x, y, z);
537}
538
539static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
540{
541 Matrix *m = reinterpret_cast<Matrix *>(mat);
542 m->loadTranslate(x, y, z);
543}
544
545static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
546{
547 Matrix *m = reinterpret_cast<Matrix *>(mat);
548 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
549 reinterpret_cast<const Matrix *>(rhs));
550}
551
552static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
553{
554 Matrix *m = reinterpret_cast<Matrix *>(mat);
555 m->multiply(reinterpret_cast<const Matrix *>(rhs));
556}
557
558static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
559{
560 Matrix *m = reinterpret_cast<Matrix *>(mat);
561 m->rotate(rot, x, y, z);
562}
563
564static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
565{
566 Matrix *m = reinterpret_cast<Matrix *>(mat);
567 m->scale(x, y, z);
568}
569
570static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
571{
572 Matrix *m = reinterpret_cast<Matrix *>(mat);
573 m->translate(x, y, z);
574}
575
576
Jason Sams90b36a82009-08-17 13:56:09 -0700577static void SC_vec2Rand(float *vec, float maxLen)
578{
579 float angle = SC_randf(PI * 2);
580 float len = SC_randf(maxLen);
581 vec[0] = len * sinf(angle);
582 vec[1] = len * cosf(angle);
583}
584
Jason Samse45ac6e2009-07-20 14:31:06 -0700585
586
587//////////////////////////////////////////////////////////////////////////////
588// Context
589//////////////////////////////////////////////////////////////////////////////
590
591static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
592{
593 GET_TLS();
594 rsi_ProgramFragmentBindTexture(rsc,
595 static_cast<ProgramFragment *>(vpf),
596 slot,
597 static_cast<Allocation *>(va));
598
599}
600
601static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
602{
603 GET_TLS();
604 rsi_ProgramFragmentBindSampler(rsc,
605 static_cast<ProgramFragment *>(vpf),
606 slot,
607 static_cast<Sampler *>(vs));
608
609}
610
611static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs)
612{
613 GET_TLS();
614 rsi_ContextBindProgramFragmentStore(rsc, pfs);
615
616}
617
618static void SC_bindProgramFragment(RsProgramFragment pf)
619{
620 GET_TLS();
621 rsi_ContextBindProgramFragment(rsc, pf);
622
623}
624
Jason Samsb5909ce2009-07-21 12:20:54 -0700625static void SC_bindProgramVertex(RsProgramVertex pv)
626{
627 GET_TLS();
628 rsi_ContextBindProgramVertex(rsc, pv);
629
630}
Jason Samse45ac6e2009-07-20 14:31:06 -0700631
632//////////////////////////////////////////////////////////////////////////////
Jason Samsc9d43db2009-07-28 12:02:16 -0700633// VP
634//////////////////////////////////////////////////////////////////////////////
635
636static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
637{
638 GET_TLS();
639 rsc->getVertex()->setModelviewMatrix(m);
640}
641
642static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
643{
644 GET_TLS();
645 rsc->getVertex()->setTextureMatrix(m);
646}
647
648
649
650//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700651// Drawing
652//////////////////////////////////////////////////////////////////////////////
653
654static void SC_drawTriangleMesh(RsTriangleMesh mesh)
655{
656 GET_TLS();
657 rsi_TriangleMeshRender(rsc, mesh);
658}
659
660static void SC_drawTriangleMeshRange(RsTriangleMesh mesh, uint32_t start, uint32_t count)
661{
662 GET_TLS();
663 rsi_TriangleMeshRenderRange(rsc, mesh, start, count);
664}
665
Romain Guyd369e272009-08-07 15:40:32 -0700666static void SC_drawLine(float x1, float y1, float z1,
667 float x2, float y2, float z2)
668{
669 GET_TLS();
670 rsc->setupCheck();
671
672 float vtx[] = { x1, y1, z1, x2, y2, z2 };
673
674 glBindBuffer(GL_ARRAY_BUFFER, 0);
675 glEnableClientState(GL_VERTEX_ARRAY);
676 glVertexPointer(3, GL_FLOAT, 0, vtx);
677
678 glDisableClientState(GL_NORMAL_ARRAY);
679 glDisableClientState(GL_COLOR_ARRAY);
680
681 glDrawArrays(GL_LINES, 0, 2);
682}
683
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700684static void SC_drawQuadTexCoords(float x1, float y1, float z1,
685 float u1, float v1,
686 float x2, float y2, float z2,
687 float u2, float v2,
688 float x3, float y3, float z3,
689 float u3, float v3,
690 float x4, float y4, float z4,
691 float u4, float v4)
Jason Samse45ac6e2009-07-20 14:31:06 -0700692{
693 GET_TLS();
Jason Samse579df42009-08-10 14:55:26 -0700694
Jason Samse45ac6e2009-07-20 14:31:06 -0700695 //LOGE("Quad");
696 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
697 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
698 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
699 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
Jason Samse579df42009-08-10 14:55:26 -0700700
Jason Samse45ac6e2009-07-20 14:31:06 -0700701 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700702 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
Jason Samse45ac6e2009-07-20 14:31:06 -0700703
704 rsc->setupCheck();
705
706 glBindBuffer(GL_ARRAY_BUFFER, 0);
707 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
708
709 glEnableClientState(GL_VERTEX_ARRAY);
710 glVertexPointer(3, GL_FLOAT, 0, vtx);
711
712 glClientActiveTexture(GL_TEXTURE0);
713 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
714 glTexCoordPointer(2, GL_FLOAT, 0, tex);
715 glClientActiveTexture(GL_TEXTURE1);
716 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
717 glTexCoordPointer(2, GL_FLOAT, 0, tex);
718 glClientActiveTexture(GL_TEXTURE0);
719
720 glDisableClientState(GL_NORMAL_ARRAY);
721 glDisableClientState(GL_COLOR_ARRAY);
722
723 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
724
725 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
726}
727
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700728static void SC_drawQuad(float x1, float y1, float z1,
729 float x2, float y2, float z2,
730 float x3, float y3, float z3,
731 float x4, float y4, float z4)
732{
733 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
734 x2, y2, z2, 1, 1,
735 x3, y3, z3, 1, 0,
736 x4, y4, z4, 0, 0);
737}
738
Jason Samse9f5c532009-07-28 17:20:11 -0700739static void SC_drawRect(float x1, float y1,
740 float x2, float y2, float z)
741{
742 SC_drawQuad(x1, y2, z,
743 x2, y2, z,
744 x2, y1, z,
745 x1, y1, z);
746}
747
Jason Samse5ffb872009-08-09 17:01:55 -0700748static void SC_drawSimpleMesh(RsSimpleMesh vsm)
749{
750 GET_TLS();
751 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
752 rsc->setupCheck();
753 sm->render();
754}
755
756static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len)
757{
758 GET_TLS();
759 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
760 rsc->setupCheck();
761 sm->renderRange(start, len);
762}
763
764
Jason Samse45ac6e2009-07-20 14:31:06 -0700765//////////////////////////////////////////////////////////////////////////////
766//
767//////////////////////////////////////////////////////////////////////////////
768
Jason Samse45ac6e2009-07-20 14:31:06 -0700769static void SC_color(float r, float g, float b, float a)
770{
771 glColor4f(r, g, b, a);
772}
773
Romain Guy48b7edc2009-08-06 22:52:13 -0700774static void SC_ambient(float r, float g, float b, float a)
775{
776 GLfloat params[] = { r, g, b, a };
777 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, params);
778}
779
780static void SC_diffuse(float r, float g, float b, float a)
781{
782 GLfloat params[] = { r, g, b, a };
783 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, params);
784}
785
786static void SC_specular(float r, float g, float b, float a)
787{
788 GLfloat params[] = { r, g, b, a };
789 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, params);
790}
791
792static void SC_emission(float r, float g, float b, float a)
793{
794 GLfloat params[] = { r, g, b, a };
795 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, params);
796}
797
Romain Guyd369e272009-08-07 15:40:32 -0700798static void SC_shininess(float s)
Romain Guy48b7edc2009-08-06 22:52:13 -0700799{
Romain Guyd369e272009-08-07 15:40:32 -0700800 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, s);
Romain Guy48b7edc2009-08-06 22:52:13 -0700801}
802
Romain Guy370ed152009-08-20 17:08:33 -0700803static void SC_hsbToRgb(float h, float s, float b, float* rgb)
Romain Guy9c59d022009-07-31 15:33:59 -0700804{
805 float red = 0.0f;
806 float green = 0.0f;
807 float blue = 0.0f;
Jason Samse5ffb872009-08-09 17:01:55 -0700808
Romain Guy9c59d022009-07-31 15:33:59 -0700809 float x = h;
810 float y = s;
811 float z = b;
Jason Samse5ffb872009-08-09 17:01:55 -0700812
Romain Guy9c59d022009-07-31 15:33:59 -0700813 float hf = (x - (int) x) * 6.0f;
814 int ihf = (int) hf;
815 float f = hf - ihf;
816 float pv = z * (1.0f - y);
817 float qv = z * (1.0f - y * f);
818 float tv = z * (1.0f - y * (1.0f - f));
Jason Samse5ffb872009-08-09 17:01:55 -0700819
Romain Guy9c59d022009-07-31 15:33:59 -0700820 switch (ihf) {
821 case 0: // Red is the dominant color
822 red = z;
823 green = tv;
824 blue = pv;
825 break;
826 case 1: // Green is the dominant color
827 red = qv;
828 green = z;
829 blue = pv;
830 break;
831 case 2:
832 red = pv;
833 green = z;
834 blue = tv;
835 break;
836 case 3: // Blue is the dominant color
837 red = pv;
838 green = qv;
839 blue = z;
840 break;
841 case 4:
842 red = tv;
843 green = pv;
844 blue = z;
845 break;
846 case 5: // Red is the dominant color
847 red = z;
848 green = pv;
849 blue = qv;
850 break;
851 }
Jason Samse5ffb872009-08-09 17:01:55 -0700852
Romain Guy370ed152009-08-20 17:08:33 -0700853 rgb[0] = red;
854 rgb[1] = green;
855 rgb[2] = blue;
856}
857
858static int SC_hsbToAbgr(float h, float s, float b, float a)
859{
860 float rgb[3];
861 SC_hsbToRgb(h, s, b, rgb);
862 return int(a * 255.0f) << 24 |
863 int(rgb[2] * 255.0f) << 16 |
864 int(rgb[1] * 255.0f) << 8 |
865 int(rgb[0] * 255.0f);
866}
867
868static void SC_hsb(float h, float s, float b, float a)
869{
870 float rgb[3];
871 SC_hsbToRgb(h, s, b, rgb);
872 glColor4f(rgb[0], rgb[1], rgb[2], a);
Romain Guy9c59d022009-07-31 15:33:59 -0700873}
874
Jason Samsc9d43db2009-07-28 12:02:16 -0700875static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samse45ac6e2009-07-20 14:31:06 -0700876{
877 GET_TLS();
878 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
879}
880
Jason Samse5ffb872009-08-09 17:01:55 -0700881static void SC_uploadToBufferObject(RsAllocation va)
882{
883 GET_TLS();
884 rsi_AllocationUploadToBufferObject(rsc, va);
885}
886
Jason Samse45ac6e2009-07-20 14:31:06 -0700887static void SC_ClearColor(float r, float g, float b, float a)
888{
889 //LOGE("c %f %f %f %f", r, g, b, a);
890 GET_TLS();
891 sc->mEnviroment.mClearColor[0] = r;
892 sc->mEnviroment.mClearColor[1] = g;
893 sc->mEnviroment.mClearColor[2] = b;
894 sc->mEnviroment.mClearColor[3] = a;
895}
896
Jason Samsc9d43db2009-07-28 12:02:16 -0700897static void SC_debugF(const char *s, float f)
898{
899 LOGE("%s %f", s, f);
900}
901
Romain Guy370ed152009-08-20 17:08:33 -0700902static void SC_debugHexF(const char *s, float f)
903{
904 LOGE("%s 0x%x", s, *((int *) (&f)));
905}
906
Jason Samsc9d43db2009-07-28 12:02:16 -0700907static void SC_debugI32(const char *s, int32_t i)
908{
909 LOGE("%s %i", s, i);
910}
911
Romain Guy370ed152009-08-20 17:08:33 -0700912static void SC_debugHexI32(const char *s, int32_t i)
913{
914 LOGE("%s 0x%x", s, i);
915}
916
Jason Samse579df42009-08-10 14:55:26 -0700917static uint32_t SC_getWidth()
918{
919 GET_TLS();
920 return rsc->getWidth();
921}
Jason Samse45ac6e2009-07-20 14:31:06 -0700922
Jason Samse579df42009-08-10 14:55:26 -0700923static uint32_t SC_getHeight()
924{
925 GET_TLS();
926 return rsc->getHeight();
927}
Jason Samse45ac6e2009-07-20 14:31:06 -0700928
Jason Sams90b36a82009-08-17 13:56:09 -0700929static uint32_t SC_colorFloatRGBAtoUNorm8(float r, float g, float b, float a)
930{
931 uint32_t c = 0;
932 c |= (uint32_t)(r * 255.f + 0.5f);
933 c |= ((uint32_t)(g * 255.f + 0.5f)) << 8;
934 c |= ((uint32_t)(b * 255.f + 0.5f)) << 16;
935 c |= ((uint32_t)(a * 255.f + 0.5f)) << 24;
936 return c;
937}
938
939static uint32_t SC_colorFloatRGBAto565(float r, float g, float b)
940{
941 uint32_t ir = (uint32_t)(r * 255.f + 0.5f);
942 uint32_t ig = (uint32_t)(g * 255.f + 0.5f);
943 uint32_t ib = (uint32_t)(b * 255.f + 0.5f);
944 return rs888to565(ir, ig, ib);
945}
946
Jason Samse45ac6e2009-07-20 14:31:06 -0700947//////////////////////////////////////////////////////////////////////////////
948// Class implementation
949//////////////////////////////////////////////////////////////////////////////
950
951ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
952 // IO
953 { "loadI32", (void *)&SC_loadI32,
954 "int", "(int, int)" },
955 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
956 { "loadF", (void *)&SC_loadF,
957 "float", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -0700958 { "loadArrayF", (void *)&SC_loadArrayF,
Romain Guy06f7c932009-08-06 12:40:41 -0700959 "float*", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -0700960 { "loadArrayI32", (void *)&SC_loadArrayI32,
Romain Guy06f7c932009-08-06 12:40:41 -0700961 "int*", "(int, int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700962 { "loadVec4", (void *)&SC_loadVec4,
963 "void", "(int, int, float *)" },
964 { "loadMatrix", (void *)&SC_loadMatrix,
965 "void", "(int, int, float *)" },
966 { "storeI32", (void *)&SC_storeI32,
967 "void", "(int, int, int)" },
968 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
969 { "storeF", (void *)&SC_storeF,
970 "void", "(int, int, float)" },
971 { "storeVec4", (void *)&SC_storeVec4,
972 "void", "(int, int, float *)" },
973 { "storeMatrix", (void *)&SC_storeMatrix,
974 "void", "(int, int, float *)" },
Romain Guy48b7edc2009-08-06 22:52:13 -0700975 { "loadTriangleMeshVerticesF", (void *)&SC_loadTriangleMeshVerticesF,
976 "float*", "(int)" },
977 { "updateTriangleMesh", (void *)&SC_updateTriangleMesh,
978 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700979
980 // math
Romain Guy27162ab2009-08-09 17:04:54 -0700981 { "modf", (void *)&fmod,
982 "float", "(float, float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700983 { "abs", (void *)&abs,
984 "int", "(int)" },
985 { "absf", (void *)&fabs,
986 "float", "(float)" },
Romain Guy2275d632009-08-18 11:39:17 -0700987 { "sinf_fast", (void *)&SC_sinf_fast,
988 "float", "(float)" },
989 { "cosf_fast", (void *)&SC_cosf_fast,
990 "float", "(float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700991 { "sinf", (void *)&sinf,
992 "float", "(float)" },
993 { "cosf", (void *)&cosf,
994 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700995 { "asinf", (void *)&asinf,
996 "float", "(float)" },
997 { "acosf", (void *)&acosf,
998 "float", "(float)" },
999 { "atanf", (void *)&atanf,
1000 "float", "(float)" },
1001 { "atan2f", (void *)&atan2f,
Romain Guy9c59d022009-07-31 15:33:59 -07001002 "float", "(float, float)" },
Jason Samse9f5c532009-07-28 17:20:11 -07001003 { "fabsf", (void *)&fabsf,
Jason Samse45ac6e2009-07-20 14:31:06 -07001004 "float", "(float)" },
1005 { "randf", (void *)&SC_randf,
1006 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001007 { "randf2", (void *)&SC_randf2,
1008 "float", "(float, float)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001009 { "floorf", (void *)&floorf,
1010 "float", "(float)" },
1011 { "ceilf", (void *)&ceilf,
1012 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001013 { "expf", (void *)&expf,
1014 "float", "(float)" },
1015 { "logf", (void *)&logf,
1016 "float", "(float)" },
1017 { "powf", (void *)&powf,
1018 "float", "(float, float)" },
1019 { "maxf", (void *)&SC_maxf,
1020 "float", "(float, float)" },
1021 { "minf", (void *)&SC_minf,
1022 "float", "(float, float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -07001023 { "sqrt", (void *)&sqrt,
1024 "int", "(int)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001025 { "sqrtf", (void *)&sqrtf,
1026 "float", "(float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -07001027 { "sqr", (void *)&SC_sqr,
1028 "int", "(int)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001029 { "sqrf", (void *)&SC_sqrf,
1030 "float", "(float)" },
Romain Guy27162ab2009-08-09 17:04:54 -07001031 { "clamp", (void *)&SC_clamp,
1032 "int", "(int, int, int)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001033 { "clampf", (void *)&SC_clampf,
1034 "float", "(float, float, float)" },
1035 { "distf2", (void *)&SC_distf2,
1036 "float", "(float, float, float, float)" },
1037 { "distf3", (void *)&SC_distf3,
1038 "float", "(float, float, float, float, float, float)" },
1039 { "magf2", (void *)&SC_magf2,
1040 "float", "(float, float)" },
1041 { "magf3", (void *)&SC_magf3,
1042 "float", "(float, float, float)" },
1043 { "radf", (void *)&SC_radf,
1044 "float", "(float)" },
1045 { "degf", (void *)&SC_degf,
1046 "float", "(float)" },
1047 { "lerpf", (void *)&SC_lerpf,
1048 "float", "(float, float, float)" },
1049 { "normf", (void *)&SC_normf,
1050 "float", "(float, float, float)" },
1051 { "mapf", (void *)&SC_mapf,
1052 "float", "(float, float, float, float, float)" },
Romain Guyb7f1a6d2009-08-03 21:12:51 -07001053 { "noisef", (void *)&SC_noisef,
1054 "float", "(float)" },
1055 { "noisef2", (void *)&SC_noisef2,
1056 "float", "(float, float)" },
1057 { "noisef3", (void *)&SC_noisef3,
1058 "float", "(float, float, float)" },
1059 { "turbulencef2", (void *)&SC_turbulencef2,
1060 "float", "(float, float, float)" },
1061 { "turbulencef3", (void *)&SC_turbulencef3,
1062 "float", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001063
Romain Guy98e10fd2009-07-30 18:45:01 -07001064 // time
1065 { "second", (void *)&SC_second,
1066 "int", "()" },
1067 { "minute", (void *)&SC_minute,
1068 "int", "()" },
1069 { "hour", (void *)&SC_hour,
1070 "int", "()" },
Romain Guy39dbc802009-07-31 11:20:59 -07001071 { "day", (void *)&SC_day,
1072 "int", "()" },
1073 { "month", (void *)&SC_month,
1074 "int", "()" },
1075 { "year", (void *)&SC_year,
1076 "int", "()" },
Joe Onorato9c4e4ca2009-08-09 11:39:02 -07001077 { "uptimeMillis", (void*)&SC_uptimeMillis,
1078 "int", "()" }, // TODO: use long instead
1079 { "startTimeMillis", (void*)&SC_startTimeMillis,
1080 "int", "()" }, // TODO: use long instead
1081 { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis,
1082 "int", "()" }, // TODO: use long instead
Romain Guy98e10fd2009-07-30 18:45:01 -07001083
Jason Samse45ac6e2009-07-20 14:31:06 -07001084 // matrix
1085 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
1086 "void", "(float *mat)" },
1087 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
1088 "void", "(float *mat, float *f)" },
1089 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
1090 "void", "(float *mat, float *newmat)" },
1091 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
1092 "void", "(float *mat, float rot, float x, float y, float z)" },
1093 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
1094 "void", "(float *mat, float x, float y, float z)" },
1095 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
1096 "void", "(float *mat, float x, float y, float z)" },
1097 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
1098 "void", "(float *mat, float *lhs, float *rhs)" },
1099 { "matrixMultiply", (void *)&SC_matrixMultiply,
1100 "void", "(float *mat, float *rhs)" },
1101 { "matrixRotate", (void *)&SC_matrixRotate,
1102 "void", "(float *mat, float rot, float x, float y, float z)" },
1103 { "matrixScale", (void *)&SC_matrixScale,
1104 "void", "(float *mat, float x, float y, float z)" },
1105 { "matrixTranslate", (void *)&SC_matrixTranslate,
1106 "void", "(float *mat, float x, float y, float z)" },
1107
Jason Sams90b36a82009-08-17 13:56:09 -07001108 // vector
1109 { "vec2Rand", (void *)&SC_vec2Rand,
1110 "void", "(float *vec, float maxLen)" },
1111
Jason Samsa57c0a72009-09-04 14:42:41 -07001112 // vec3
1113 { "vec3Norm", (void *)&SC_vec3Norm,
1114 "void", "(struct vec3_s *)" },
1115 { "vec3Length", (void *)&SC_vec3Length,
1116 "float", "(struct vec3_s *)" },
1117 { "vec3Add", (void *)&SC_vec3Add,
1118 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1119 { "vec3Sub", (void *)&SC_vec3Sub,
1120 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1121 { "vec3Cross", (void *)&SC_vec3Cross,
1122 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1123 { "vec3Dot", (void *)&SC_vec3Dot,
1124 "float", "(struct vec3_s *lhs, struct vec3_s *rhs)" },
1125 { "vec3Scale", (void *)&SC_vec3Scale,
1126 "void", "(struct vec3_s *lhs, float scale)" },
1127
Jason Samse45ac6e2009-07-20 14:31:06 -07001128 // context
1129 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
1130 "void", "(int)" },
1131 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
1132 "void", "(int)" },
Jason Samsb5909ce2009-07-21 12:20:54 -07001133 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
1134 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001135 { "bindSampler", (void *)&SC_bindSampler,
1136 "void", "(int, int, int)" },
1137 { "bindTexture", (void *)&SC_bindTexture,
1138 "void", "(int, int, int)" },
1139
Jason Samsc9d43db2009-07-28 12:02:16 -07001140 // vp
Jason Sams50253db2009-07-29 20:55:44 -07001141 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -07001142 "void", "(void *)" },
Jason Sams50253db2009-07-29 20:55:44 -07001143 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -07001144 "void", "(void *)" },
1145
1146
1147
Jason Samse45ac6e2009-07-20 14:31:06 -07001148 // drawing
Jason Samse9f5c532009-07-28 17:20:11 -07001149 { "drawRect", (void *)&SC_drawRect,
1150 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001151 { "drawQuad", (void *)&SC_drawQuad,
1152 "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 -07001153 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords,
1154 "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 Samse45ac6e2009-07-20 14:31:06 -07001155 { "drawTriangleMesh", (void *)&SC_drawTriangleMesh,
1156 "void", "(int mesh)" },
1157 { "drawTriangleMeshRange", (void *)&SC_drawTriangleMeshRange,
1158 "void", "(int mesh, int start, int count)" },
Romain Guyd369e272009-08-07 15:40:32 -07001159 { "drawLine", (void *)&SC_drawLine,
1160 "void", "(float x1, float y1, float z1, float x2, float y2, float z2)" },
Jason Samse5ffb872009-08-09 17:01:55 -07001161 { "drawSimpleMesh", (void *)&SC_drawSimpleMesh,
1162 "void", "(int ism)" },
1163 { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange,
1164 "void", "(int ism, int start, int len)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001165
1166
1167 // misc
1168 { "pfClearColor", (void *)&SC_ClearColor,
1169 "void", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001170 { "color", (void *)&SC_color,
1171 "void", "(float, float, float, float)" },
Romain Guy9c59d022009-07-31 15:33:59 -07001172 { "hsb", (void *)&SC_hsb,
1173 "void", "(float, float, float, float)" },
Romain Guy370ed152009-08-20 17:08:33 -07001174 { "hsbToRgb", (void *)&SC_hsbToRgb,
1175 "void", "(float, float, float, float*)" },
1176 { "hsbToAbgr", (void *)&SC_hsbToAbgr,
1177 "int", "(float, float, float, float)" },
Romain Guy48b7edc2009-08-06 22:52:13 -07001178 { "ambient", (void *)&SC_ambient,
1179 "void", "(float, float, float, float)" },
1180 { "diffuse", (void *)&SC_diffuse,
1181 "void", "(float, float, float, float)" },
1182 { "specular", (void *)&SC_specular,
1183 "void", "(float, float, float, float)" },
1184 { "emission", (void *)&SC_emission,
1185 "void", "(float, float, float, float)" },
1186 { "shininess", (void *)&SC_shininess,
Romain Guyd369e272009-08-07 15:40:32 -07001187 "void", "(float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001188
Jason Samsc9d43db2009-07-28 12:02:16 -07001189 { "uploadToTexture", (void *)&SC_uploadToTexture,
1190 "void", "(int, int)" },
Jason Samse5ffb872009-08-09 17:01:55 -07001191 { "uploadToBufferObject", (void *)&SC_uploadToBufferObject,
1192 "void", "(int)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001193
Jason Sams90b36a82009-08-17 13:56:09 -07001194 { "colorFloatRGBAtoUNorm8", (void *)&SC_colorFloatRGBAtoUNorm8,
1195 "int", "(float, float, float, float)" },
1196 { "colorFloatRGBto565", (void *)&SC_colorFloatRGBAto565,
1197 "int", "(float, float, float)" },
1198
1199
Jason Samse579df42009-08-10 14:55:26 -07001200 { "getWidth", (void *)&SC_getWidth,
1201 "int", "()" },
1202 { "getHeight", (void *)&SC_getHeight,
1203 "int", "()" },
1204
1205
Jason Samsc9d43db2009-07-28 12:02:16 -07001206
1207 { "debugF", (void *)&SC_debugF,
1208 "void", "(void *, float)" },
1209 { "debugI32", (void *)&SC_debugI32,
1210 "void", "(void *, int)" },
Romain Guy370ed152009-08-20 17:08:33 -07001211 { "debugHexF", (void *)&SC_debugHexF,
1212 "void", "(void *, float)" },
1213 { "debugHexI32", (void *)&SC_debugHexI32,
1214 "void", "(void *, int)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001215
1216
Jason Samse45ac6e2009-07-20 14:31:06 -07001217 { NULL, NULL, NULL, NULL }
1218};
1219
1220const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
1221{
1222 ScriptCState::SymbolTable_t *syms = gSyms;
1223
1224 while (syms->mPtr) {
1225 if (!strcmp(syms->mName, sym)) {
1226 return syms;
1227 }
1228 syms++;
1229 }
1230 return NULL;
1231}
1232
1233void ScriptCState::appendDecls(String8 *str)
1234{
1235 ScriptCState::SymbolTable_t *syms = gSyms;
1236 while (syms->mPtr) {
1237 str->append(syms->mRet);
1238 str->append(" ");
1239 str->append(syms->mName);
1240 str->append(syms->mParam);
1241 str->append(";\n");
1242 syms++;
1243 }
1244}
1245
1246