blob: 5aef56d3fead3c61d6d474ec2ab4ad90c2116b70 [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
Romain Guyd6d4a5f2009-10-09 16:05:25 -0700230//////////////////////////////////////////////////////////////////////////////
231// Vec4 routines
232//////////////////////////////////////////////////////////////////////////////
233
234static void SC_vec4Norm(vec4_t *v)
235{
236 float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w);
237 len = 1 / len;
238 v->x *= len;
239 v->y *= len;
240 v->z *= len;
241 v->w *= len;
242}
243
244static float SC_vec4Length(const vec4_t *v)
245{
246 return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w);
247}
248
249static void SC_vec4Add(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs)
250{
251 dest->x = lhs->x + rhs->x;
252 dest->y = lhs->y + rhs->y;
253 dest->z = lhs->z + rhs->z;
254 dest->w = lhs->w + rhs->w;
255}
256
257static void SC_vec4Sub(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs)
258{
259 dest->x = lhs->x - rhs->x;
260 dest->y = lhs->y - rhs->y;
261 dest->z = lhs->z - rhs->z;
262 dest->w = lhs->w - rhs->w;
263}
264
265static float SC_vec4Dot(const vec4_t *lhs, const vec4_t *rhs)
266{
267 return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z + lhs->w * rhs->w;
268}
269
270static void SC_vec4Scale(vec4_t *lhs, float scale)
271{
272 lhs->x *= scale;
273 lhs->y *= scale;
274 lhs->z *= scale;
275 lhs->w *= scale;
276}
Jason Samse45ac6e2009-07-20 14:31:06 -0700277
278//////////////////////////////////////////////////////////////////////////////
279// Math routines
280//////////////////////////////////////////////////////////////////////////////
281
Romain Guy39dbc802009-07-31 11:20:59 -0700282#define PI 3.1415926f
283#define DEG_TO_RAD PI / 180.0f
284#define RAD_TO_DEG 180.0f / PI
285
Romain Guy2275d632009-08-18 11:39:17 -0700286static float SC_sinf_fast(float x)
287{
288 const float A = 1.0f / (2.0f * M_PI);
289 const float B = -16.0f;
290 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -0700291
Romain Guy2275d632009-08-18 11:39:17 -0700292 // scale angle for easy argument reduction
293 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -0700294
Romain Guy2275d632009-08-18 11:39:17 -0700295 if (fabsf(x) >= 0.5f) {
296 // argument reduction
297 x = x - ceilf(x + 0.5f) + 1.0f;
298 }
Jason Samsa57c0a72009-09-04 14:42:41 -0700299
Romain Guy2275d632009-08-18 11:39:17 -0700300 const float y = B * x * fabsf(x) + C * x;
301 return 0.2215f * (y * fabsf(y) - y) + y;
302}
303
304static float SC_cosf_fast(float x)
305{
306 x += float(M_PI / 2);
307
308 const float A = 1.0f / (2.0f * M_PI);
309 const float B = -16.0f;
310 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -0700311
Romain Guy2275d632009-08-18 11:39:17 -0700312 // scale angle for easy argument reduction
313 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -0700314
Romain Guy2275d632009-08-18 11:39:17 -0700315 if (fabsf(x) >= 0.5f) {
316 // argument reduction
317 x = x - ceilf(x + 0.5f) + 1.0f;
318 }
Jason Samsa57c0a72009-09-04 14:42:41 -0700319
Romain Guy2275d632009-08-18 11:39:17 -0700320 const float y = B * x * fabsf(x) + C * x;
321 return 0.2215f * (y * fabsf(y) - y) + y;
322}
323
Jason Samse45ac6e2009-07-20 14:31:06 -0700324static float SC_randf(float max)
325{
326 float r = (float)rand();
327 return r / RAND_MAX * max;
328}
329
Romain Guy39dbc802009-07-31 11:20:59 -0700330static float SC_randf2(float min, float max)
331{
332 float r = (float)rand();
333 return r / RAND_MAX * (max - min) + min;
334}
335
Romain Guyd6d4a5f2009-10-09 16:05:25 -0700336static int SC_sign(int value)
337{
338 return (value > 0) - (value < 0);
339}
340
341static float SC_signf(float value)
342{
343 return (value > 0) - (value < 0);
344}
345
Romain Guy39dbc802009-07-31 11:20:59 -0700346static float SC_clampf(float amount, float low, float high)
347{
348 return amount < low ? low : (amount > high ? high : amount);
349}
350
Romain Guy27162ab2009-08-09 17:04:54 -0700351static int SC_clamp(int amount, int low, int high)
352{
353 return amount < low ? low : (amount > high ? high : amount);
354}
355
Romain Guy39dbc802009-07-31 11:20:59 -0700356static float SC_maxf(float a, float b)
357{
Jason Samse5ffb872009-08-09 17:01:55 -0700358 return a > b ? a : b;
Romain Guy39dbc802009-07-31 11:20:59 -0700359}
360
361static float SC_minf(float a, float b)
362{
Jason Samse5ffb872009-08-09 17:01:55 -0700363 return a < b ? a : b;
Romain Guy39dbc802009-07-31 11:20:59 -0700364}
365
366static float SC_sqrf(float v)
367{
Jason Samse5ffb872009-08-09 17:01:55 -0700368 return v * v;
Romain Guy39dbc802009-07-31 11:20:59 -0700369}
370
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700371static int SC_sqr(int v)
372{
373 return v * v;
374}
375
Jason Samsdac98f52009-09-18 14:24:24 -0700376static float SC_fracf(float v)
377{
378 return v - floorf(v);
379}
380
381static float SC_roundf(float v)
382{
383 return floorf(v + 0.4999999999);
384}
385
Romain Guy39dbc802009-07-31 11:20:59 -0700386static float SC_distf2(float x1, float y1, float x2, float y2)
387{
388 float x = x2 - x1;
389 float y = y2 - y1;
Jason Samse5ffb872009-08-09 17:01:55 -0700390 return sqrtf(x * x + y * y);
Romain Guy39dbc802009-07-31 11:20:59 -0700391}
392
393static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2)
394{
395 float x = x2 - x1;
396 float y = y2 - y1;
397 float z = z2 - z1;
Jason Samse5ffb872009-08-09 17:01:55 -0700398 return sqrtf(x * x + y * y + z * z);
Romain Guy39dbc802009-07-31 11:20:59 -0700399}
400
401static float SC_magf2(float a, float b)
402{
403 return sqrtf(a * a + b * b);
404}
405
406static float SC_magf3(float a, float b, float c)
407{
408 return sqrtf(a * a + b * b + c * c);
409}
410
411static float SC_radf(float degrees)
412{
Jason Samse5ffb872009-08-09 17:01:55 -0700413 return degrees * DEG_TO_RAD;
Romain Guy39dbc802009-07-31 11:20:59 -0700414}
415
416static float SC_degf(float radians)
417{
Jason Samse5ffb872009-08-09 17:01:55 -0700418 return radians * RAD_TO_DEG;
Romain Guy39dbc802009-07-31 11:20:59 -0700419}
420
421static float SC_lerpf(float start, float stop, float amount)
422{
423 return start + (stop - start) * amount;
424}
425
426static float SC_normf(float start, float stop, float value)
427{
428 return (value - start) / (stop - start);
429}
430
431static float SC_mapf(float minStart, float minStop, float maxStart, float maxStop, float value)
432{
433 return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
434}
Jason Samse45ac6e2009-07-20 14:31:06 -0700435
Romain Guy98e10fd2009-07-30 18:45:01 -0700436//////////////////////////////////////////////////////////////////////////////
437// Time routines
438//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700439
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700440static int32_t SC_second()
Romain Guy98e10fd2009-07-30 18:45:01 -0700441{
442 GET_TLS();
443
444 time_t rawtime;
445 time(&rawtime);
446
447 if (sc->mEnviroment.mTimeZone) {
448 struct tm timeinfo;
449 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
450 return timeinfo.tm_sec;
451 } else {
452 struct tm *timeinfo;
453 timeinfo = localtime(&rawtime);
454 return timeinfo->tm_sec;
455 }
456}
457
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700458static int32_t SC_minute()
Romain Guy98e10fd2009-07-30 18:45:01 -0700459{
460 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700461
Romain Guy98e10fd2009-07-30 18:45:01 -0700462 time_t rawtime;
463 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700464
Romain Guy98e10fd2009-07-30 18:45:01 -0700465 if (sc->mEnviroment.mTimeZone) {
466 struct tm timeinfo;
467 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
468 return timeinfo.tm_min;
469 } else {
470 struct tm *timeinfo;
471 timeinfo = localtime(&rawtime);
472 return timeinfo->tm_min;
473 }
Jason Samse5ffb872009-08-09 17:01:55 -0700474}
Romain Guy98e10fd2009-07-30 18:45:01 -0700475
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700476static int32_t SC_hour()
Romain Guy98e10fd2009-07-30 18:45:01 -0700477{
478 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700479
Romain Guy98e10fd2009-07-30 18:45:01 -0700480 time_t rawtime;
481 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700482
Romain Guy98e10fd2009-07-30 18:45:01 -0700483 if (sc->mEnviroment.mTimeZone) {
484 struct tm timeinfo;
485 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
486 return timeinfo.tm_hour;
487 } else {
488 struct tm *timeinfo;
489 timeinfo = localtime(&rawtime);
490 return timeinfo->tm_hour;
491 }
Romain Guy39dbc802009-07-31 11:20:59 -0700492}
493
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700494static int32_t SC_day()
Romain Guy39dbc802009-07-31 11:20:59 -0700495{
496 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700497
Romain Guy39dbc802009-07-31 11:20:59 -0700498 time_t rawtime;
499 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700500
Romain Guy39dbc802009-07-31 11:20:59 -0700501 if (sc->mEnviroment.mTimeZone) {
502 struct tm timeinfo;
503 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
504 return timeinfo.tm_mday;
505 } else {
506 struct tm *timeinfo;
507 timeinfo = localtime(&rawtime);
508 return timeinfo->tm_mday;
509 }
Jason Samse5ffb872009-08-09 17:01:55 -0700510}
Jason Samse45ac6e2009-07-20 14:31:06 -0700511
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700512static int32_t SC_month()
Romain Guy39dbc802009-07-31 11:20:59 -0700513{
514 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700515
Romain Guy39dbc802009-07-31 11:20:59 -0700516 time_t rawtime;
517 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700518
Romain Guy39dbc802009-07-31 11:20:59 -0700519 if (sc->mEnviroment.mTimeZone) {
520 struct tm timeinfo;
521 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
522 return timeinfo.tm_mon;
523 } else {
524 struct tm *timeinfo;
525 timeinfo = localtime(&rawtime);
526 return timeinfo->tm_mon;
527 }
Jason Samse5ffb872009-08-09 17:01:55 -0700528}
Romain Guy39dbc802009-07-31 11:20:59 -0700529
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700530static int32_t SC_year()
Romain Guy39dbc802009-07-31 11:20:59 -0700531{
532 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700533
Romain Guy39dbc802009-07-31 11:20:59 -0700534 time_t rawtime;
535 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700536
Romain Guy39dbc802009-07-31 11:20:59 -0700537 if (sc->mEnviroment.mTimeZone) {
538 struct tm timeinfo;
539 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
540 return timeinfo.tm_year;
541 } else {
542 struct tm *timeinfo;
543 timeinfo = localtime(&rawtime);
544 return timeinfo->tm_year;
545 }
546}
547
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700548static int32_t SC_uptimeMillis()
549{
550 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
551}
552
553static int32_t SC_startTimeMillis()
554{
555 GET_TLS();
556 return sc->mEnviroment.mStartTimeMillis;
557}
558
559static int32_t SC_elapsedTimeMillis()
560{
561 GET_TLS();
562 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC))
563 - sc->mEnviroment.mStartTimeMillis;
564}
565
Jason Samse45ac6e2009-07-20 14:31:06 -0700566//////////////////////////////////////////////////////////////////////////////
567// Matrix routines
568//////////////////////////////////////////////////////////////////////////////
569
570
571static void SC_matrixLoadIdentity(rsc_Matrix *mat)
572{
573 Matrix *m = reinterpret_cast<Matrix *>(mat);
574 m->loadIdentity();
575}
576
577static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
578{
579 Matrix *m = reinterpret_cast<Matrix *>(mat);
580 m->load(f);
581}
582
583static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
584{
585 Matrix *m = reinterpret_cast<Matrix *>(mat);
586 m->load(reinterpret_cast<const Matrix *>(newmat));
587}
588
589static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
590{
591 Matrix *m = reinterpret_cast<Matrix *>(mat);
592 m->loadRotate(rot, x, y, z);
593}
594
595static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
596{
597 Matrix *m = reinterpret_cast<Matrix *>(mat);
598 m->loadScale(x, y, z);
599}
600
601static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
602{
603 Matrix *m = reinterpret_cast<Matrix *>(mat);
604 m->loadTranslate(x, y, z);
605}
606
607static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
608{
609 Matrix *m = reinterpret_cast<Matrix *>(mat);
610 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
611 reinterpret_cast<const Matrix *>(rhs));
612}
613
614static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
615{
616 Matrix *m = reinterpret_cast<Matrix *>(mat);
617 m->multiply(reinterpret_cast<const Matrix *>(rhs));
618}
619
620static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
621{
622 Matrix *m = reinterpret_cast<Matrix *>(mat);
623 m->rotate(rot, x, y, z);
624}
625
626static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
627{
628 Matrix *m = reinterpret_cast<Matrix *>(mat);
629 m->scale(x, y, z);
630}
631
632static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
633{
634 Matrix *m = reinterpret_cast<Matrix *>(mat);
635 m->translate(x, y, z);
636}
637
638
Jason Sams90b36a82009-08-17 13:56:09 -0700639static void SC_vec2Rand(float *vec, float maxLen)
640{
641 float angle = SC_randf(PI * 2);
642 float len = SC_randf(maxLen);
643 vec[0] = len * sinf(angle);
644 vec[1] = len * cosf(angle);
645}
646
Jason Samse45ac6e2009-07-20 14:31:06 -0700647
648
649//////////////////////////////////////////////////////////////////////////////
650// Context
651//////////////////////////////////////////////////////////////////////////////
652
653static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
654{
655 GET_TLS();
656 rsi_ProgramFragmentBindTexture(rsc,
657 static_cast<ProgramFragment *>(vpf),
658 slot,
659 static_cast<Allocation *>(va));
660
661}
662
663static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
664{
665 GET_TLS();
666 rsi_ProgramFragmentBindSampler(rsc,
667 static_cast<ProgramFragment *>(vpf),
668 slot,
669 static_cast<Sampler *>(vs));
670
671}
672
673static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs)
674{
675 GET_TLS();
676 rsi_ContextBindProgramFragmentStore(rsc, pfs);
677
678}
679
680static void SC_bindProgramFragment(RsProgramFragment pf)
681{
682 GET_TLS();
683 rsi_ContextBindProgramFragment(rsc, pf);
684
685}
686
Jason Samsb5909ce2009-07-21 12:20:54 -0700687static void SC_bindProgramVertex(RsProgramVertex pv)
688{
689 GET_TLS();
690 rsi_ContextBindProgramVertex(rsc, pv);
691
692}
Jason Samse45ac6e2009-07-20 14:31:06 -0700693
694//////////////////////////////////////////////////////////////////////////////
Jason Samsc9d43db2009-07-28 12:02:16 -0700695// VP
696//////////////////////////////////////////////////////////////////////////////
697
698static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
699{
700 GET_TLS();
701 rsc->getVertex()->setModelviewMatrix(m);
702}
703
704static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
705{
706 GET_TLS();
707 rsc->getVertex()->setTextureMatrix(m);
708}
709
710
711
712//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700713// Drawing
714//////////////////////////////////////////////////////////////////////////////
715
Romain Guyd369e272009-08-07 15:40:32 -0700716static void SC_drawLine(float x1, float y1, float z1,
717 float x2, float y2, float z2)
718{
719 GET_TLS();
720 rsc->setupCheck();
721
722 float vtx[] = { x1, y1, z1, x2, y2, z2 };
723
724 glBindBuffer(GL_ARRAY_BUFFER, 0);
725 glEnableClientState(GL_VERTEX_ARRAY);
726 glVertexPointer(3, GL_FLOAT, 0, vtx);
727
728 glDisableClientState(GL_NORMAL_ARRAY);
729 glDisableClientState(GL_COLOR_ARRAY);
730
731 glDrawArrays(GL_LINES, 0, 2);
732}
733
Jason Samsb681c8a2009-09-28 18:12:56 -0700734static void SC_drawPoint(float x, float y, float z)
735{
736 GET_TLS();
737 rsc->setupCheck();
738
739 float vtx[] = { x, y, z };
740
741 glBindBuffer(GL_ARRAY_BUFFER, 0);
742 glEnableClientState(GL_VERTEX_ARRAY);
743 glVertexPointer(3, GL_FLOAT, 0, vtx);
744
745 glDisableClientState(GL_NORMAL_ARRAY);
746 glDisableClientState(GL_COLOR_ARRAY);
747
748 glDrawArrays(GL_POINTS, 0, 1);
749}
750
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700751static void SC_drawQuadTexCoords(float x1, float y1, float z1,
752 float u1, float v1,
753 float x2, float y2, float z2,
754 float u2, float v2,
755 float x3, float y3, float z3,
756 float u3, float v3,
757 float x4, float y4, float z4,
758 float u4, float v4)
Jason Samse45ac6e2009-07-20 14:31:06 -0700759{
760 GET_TLS();
Jason Samse579df42009-08-10 14:55:26 -0700761
Jason Samse45ac6e2009-07-20 14:31:06 -0700762 //LOGE("Quad");
763 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
764 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
765 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
766 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
Jason Samse579df42009-08-10 14:55:26 -0700767
Jason Samse45ac6e2009-07-20 14:31:06 -0700768 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700769 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
Jason Samse45ac6e2009-07-20 14:31:06 -0700770
771 rsc->setupCheck();
772
773 glBindBuffer(GL_ARRAY_BUFFER, 0);
774 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
775
776 glEnableClientState(GL_VERTEX_ARRAY);
777 glVertexPointer(3, GL_FLOAT, 0, vtx);
778
779 glClientActiveTexture(GL_TEXTURE0);
780 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
781 glTexCoordPointer(2, GL_FLOAT, 0, tex);
782 glClientActiveTexture(GL_TEXTURE1);
783 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
784 glTexCoordPointer(2, GL_FLOAT, 0, tex);
785 glClientActiveTexture(GL_TEXTURE0);
786
787 glDisableClientState(GL_NORMAL_ARRAY);
788 glDisableClientState(GL_COLOR_ARRAY);
789
790 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
791
792 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
793}
794
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700795static void SC_drawQuad(float x1, float y1, float z1,
796 float x2, float y2, float z2,
797 float x3, float y3, float z3,
798 float x4, float y4, float z4)
799{
800 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
801 x2, y2, z2, 1, 1,
802 x3, y3, z3, 1, 0,
803 x4, y4, z4, 0, 0);
804}
805
Jason Sams3a97c592009-09-30 17:36:20 -0700806static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h)
807{
808 GET_TLS();
809 rsc->setupCheck();
810
811 GLint crop[4] = {0, h, w, -h};
812 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
813 glDrawTexfOES(x, y, z, w, h);
814}
815
816static void SC_drawSprite(float x, float y, float z, float w, float h)
817{
818 GET_TLS();
819 rsc->setupCheck();
820
821 float vin[3] = {x, y, z};
822 float vout[4];
823
824 //LOGE("ds in %f %f %f", x, y, z);
825 rsc->getVertex()->transformToScreen(rsc, vout, vin);
826 //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]);
827 vout[0] /= vout[3];
828 vout[1] /= vout[3];
829 vout[2] /= vout[3];
830
831 vout[0] *= rsc->getWidth() / 2;
832 vout[1] *= rsc->getHeight() / 2;
833 vout[0] += rsc->getWidth() / 2;
834 vout[1] += rsc->getHeight() / 2;
835
836 vout[0] -= w/2;
837 vout[1] -= h/2;
838
839 //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]);
840
841 // U, V, W, H
842 GLint crop[4] = {0, h, w, -h};
843 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
844 glDrawTexiOES(vout[0], vout[1], 0/*vout[2]*/, w, h);
845}
846
847
Jason Samse9f5c532009-07-28 17:20:11 -0700848static void SC_drawRect(float x1, float y1,
849 float x2, float y2, float z)
850{
851 SC_drawQuad(x1, y2, z,
852 x2, y2, z,
853 x2, y1, z,
854 x1, y1, z);
855}
856
Jason Samse5ffb872009-08-09 17:01:55 -0700857static void SC_drawSimpleMesh(RsSimpleMesh vsm)
858{
859 GET_TLS();
860 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
861 rsc->setupCheck();
862 sm->render();
863}
864
865static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len)
866{
867 GET_TLS();
868 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
869 rsc->setupCheck();
870 sm->renderRange(start, len);
871}
872
873
Jason Samse45ac6e2009-07-20 14:31:06 -0700874//////////////////////////////////////////////////////////////////////////////
875//
876//////////////////////////////////////////////////////////////////////////////
877
Jason Samse45ac6e2009-07-20 14:31:06 -0700878static void SC_color(float r, float g, float b, float a)
879{
880 glColor4f(r, g, b, a);
881}
882
Romain Guy48b7edc2009-08-06 22:52:13 -0700883static void SC_ambient(float r, float g, float b, float a)
884{
885 GLfloat params[] = { r, g, b, a };
886 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, params);
887}
888
889static void SC_diffuse(float r, float g, float b, float a)
890{
891 GLfloat params[] = { r, g, b, a };
892 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, params);
893}
894
895static void SC_specular(float r, float g, float b, float a)
896{
897 GLfloat params[] = { r, g, b, a };
898 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, params);
899}
900
901static void SC_emission(float r, float g, float b, float a)
902{
903 GLfloat params[] = { r, g, b, a };
904 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, params);
905}
906
Romain Guyd369e272009-08-07 15:40:32 -0700907static void SC_shininess(float s)
Romain Guy48b7edc2009-08-06 22:52:13 -0700908{
Romain Guyd369e272009-08-07 15:40:32 -0700909 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, s);
Romain Guy48b7edc2009-08-06 22:52:13 -0700910}
911
Romain Guye62cc902009-09-04 17:55:41 -0700912static void SC_pointAttenuation(float a, float b, float c)
913{
914 GLfloat params[] = { a, b, c };
915 glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, params);
916}
917
Romain Guy370ed152009-08-20 17:08:33 -0700918static void SC_hsbToRgb(float h, float s, float b, float* rgb)
Romain Guy9c59d022009-07-31 15:33:59 -0700919{
920 float red = 0.0f;
921 float green = 0.0f;
922 float blue = 0.0f;
Jason Samse5ffb872009-08-09 17:01:55 -0700923
Romain Guy9c59d022009-07-31 15:33:59 -0700924 float x = h;
925 float y = s;
926 float z = b;
Jason Samse5ffb872009-08-09 17:01:55 -0700927
Romain Guy9c59d022009-07-31 15:33:59 -0700928 float hf = (x - (int) x) * 6.0f;
929 int ihf = (int) hf;
930 float f = hf - ihf;
931 float pv = z * (1.0f - y);
932 float qv = z * (1.0f - y * f);
933 float tv = z * (1.0f - y * (1.0f - f));
Jason Samse5ffb872009-08-09 17:01:55 -0700934
Romain Guy9c59d022009-07-31 15:33:59 -0700935 switch (ihf) {
936 case 0: // Red is the dominant color
937 red = z;
938 green = tv;
939 blue = pv;
940 break;
941 case 1: // Green is the dominant color
942 red = qv;
943 green = z;
944 blue = pv;
945 break;
946 case 2:
947 red = pv;
948 green = z;
949 blue = tv;
950 break;
951 case 3: // Blue is the dominant color
952 red = pv;
953 green = qv;
954 blue = z;
955 break;
956 case 4:
957 red = tv;
958 green = pv;
959 blue = z;
960 break;
961 case 5: // Red is the dominant color
962 red = z;
963 green = pv;
964 blue = qv;
965 break;
966 }
Jason Samse5ffb872009-08-09 17:01:55 -0700967
Romain Guy370ed152009-08-20 17:08:33 -0700968 rgb[0] = red;
969 rgb[1] = green;
970 rgb[2] = blue;
971}
972
973static int SC_hsbToAbgr(float h, float s, float b, float a)
974{
975 float rgb[3];
976 SC_hsbToRgb(h, s, b, rgb);
977 return int(a * 255.0f) << 24 |
978 int(rgb[2] * 255.0f) << 16 |
979 int(rgb[1] * 255.0f) << 8 |
980 int(rgb[0] * 255.0f);
981}
982
983static void SC_hsb(float h, float s, float b, float a)
984{
985 float rgb[3];
986 SC_hsbToRgb(h, s, b, rgb);
987 glColor4f(rgb[0], rgb[1], rgb[2], a);
Romain Guy9c59d022009-07-31 15:33:59 -0700988}
989
Jason Samsc9d43db2009-07-28 12:02:16 -0700990static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samse45ac6e2009-07-20 14:31:06 -0700991{
992 GET_TLS();
993 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
994}
995
Jason Samse5ffb872009-08-09 17:01:55 -0700996static void SC_uploadToBufferObject(RsAllocation va)
997{
998 GET_TLS();
999 rsi_AllocationUploadToBufferObject(rsc, va);
1000}
1001
Jason Samse45ac6e2009-07-20 14:31:06 -07001002static void SC_ClearColor(float r, float g, float b, float a)
1003{
1004 //LOGE("c %f %f %f %f", r, g, b, a);
1005 GET_TLS();
1006 sc->mEnviroment.mClearColor[0] = r;
1007 sc->mEnviroment.mClearColor[1] = g;
1008 sc->mEnviroment.mClearColor[2] = b;
1009 sc->mEnviroment.mClearColor[3] = a;
1010}
1011
Jason Samsc9d43db2009-07-28 12:02:16 -07001012static void SC_debugF(const char *s, float f)
1013{
1014 LOGE("%s %f", s, f);
1015}
1016
Romain Guy370ed152009-08-20 17:08:33 -07001017static void SC_debugHexF(const char *s, float f)
1018{
1019 LOGE("%s 0x%x", s, *((int *) (&f)));
1020}
1021
Jason Samsc9d43db2009-07-28 12:02:16 -07001022static void SC_debugI32(const char *s, int32_t i)
1023{
1024 LOGE("%s %i", s, i);
1025}
1026
Romain Guy370ed152009-08-20 17:08:33 -07001027static void SC_debugHexI32(const char *s, int32_t i)
1028{
1029 LOGE("%s 0x%x", s, i);
1030}
1031
Jason Samse579df42009-08-10 14:55:26 -07001032static uint32_t SC_getWidth()
1033{
1034 GET_TLS();
1035 return rsc->getWidth();
1036}
Jason Samse45ac6e2009-07-20 14:31:06 -07001037
Jason Samse579df42009-08-10 14:55:26 -07001038static uint32_t SC_getHeight()
1039{
1040 GET_TLS();
1041 return rsc->getHeight();
1042}
Jason Samse45ac6e2009-07-20 14:31:06 -07001043
Jason Sams90b36a82009-08-17 13:56:09 -07001044static uint32_t SC_colorFloatRGBAtoUNorm8(float r, float g, float b, float a)
1045{
1046 uint32_t c = 0;
1047 c |= (uint32_t)(r * 255.f + 0.5f);
1048 c |= ((uint32_t)(g * 255.f + 0.5f)) << 8;
1049 c |= ((uint32_t)(b * 255.f + 0.5f)) << 16;
1050 c |= ((uint32_t)(a * 255.f + 0.5f)) << 24;
1051 return c;
1052}
1053
1054static uint32_t SC_colorFloatRGBAto565(float r, float g, float b)
1055{
1056 uint32_t ir = (uint32_t)(r * 255.f + 0.5f);
1057 uint32_t ig = (uint32_t)(g * 255.f + 0.5f);
1058 uint32_t ib = (uint32_t)(b * 255.f + 0.5f);
1059 return rs888to565(ir, ig, ib);
1060}
1061
Jason Sams8c401ef2009-10-06 13:58:47 -07001062static uint32_t SC_toClient(void *data, int cmdID, int len, int waitForSpace)
1063{
1064 GET_TLS();
1065 return rsc->sendMessageToClient(data, cmdID, len, waitForSpace != 0);
1066}
1067
Jason Sams3a27c952009-10-07 18:14:01 -07001068static void SC_scriptCall(int scriptID)
1069{
1070 GET_TLS();
1071 rsc->runScript((Script *)scriptID, 0);
1072}
1073
1074
Jason Samse45ac6e2009-07-20 14:31:06 -07001075//////////////////////////////////////////////////////////////////////////////
1076// Class implementation
1077//////////////////////////////////////////////////////////////////////////////
1078
1079ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
1080 // IO
1081 { "loadI32", (void *)&SC_loadI32,
1082 "int", "(int, int)" },
1083 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
1084 { "loadF", (void *)&SC_loadF,
1085 "float", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -07001086 { "loadArrayF", (void *)&SC_loadArrayF,
Romain Guy06f7c932009-08-06 12:40:41 -07001087 "float*", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -07001088 { "loadArrayI32", (void *)&SC_loadArrayI32,
Romain Guy06f7c932009-08-06 12:40:41 -07001089 "int*", "(int, int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001090 { "loadVec4", (void *)&SC_loadVec4,
1091 "void", "(int, int, float *)" },
1092 { "loadMatrix", (void *)&SC_loadMatrix,
1093 "void", "(int, int, float *)" },
1094 { "storeI32", (void *)&SC_storeI32,
1095 "void", "(int, int, int)" },
1096 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
1097 { "storeF", (void *)&SC_storeF,
1098 "void", "(int, int, float)" },
1099 { "storeVec4", (void *)&SC_storeVec4,
1100 "void", "(int, int, float *)" },
1101 { "storeMatrix", (void *)&SC_storeMatrix,
1102 "void", "(int, int, float *)" },
Jason Samsa2b54c42009-09-23 16:38:37 -07001103 { "loadSimpleMeshVerticesF", (void *)&SC_loadSimpleMeshVerticesF,
1104 "float*", "(int, int)" },
1105 { "updateSimpleMesh", (void *)&SC_updateSimpleMesh,
Romain Guy48b7edc2009-08-06 22:52:13 -07001106 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001107
1108 // math
Romain Guy27162ab2009-08-09 17:04:54 -07001109 { "modf", (void *)&fmod,
1110 "float", "(float, float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -07001111 { "abs", (void *)&abs,
1112 "int", "(int)" },
Romain Guyc5174c72009-09-29 13:17:27 -07001113 { "absf", (void *)&fabsf,
Romain Guyfcc1c2b2009-08-08 18:30:19 -07001114 "float", "(float)" },
Romain Guy2275d632009-08-18 11:39:17 -07001115 { "sinf_fast", (void *)&SC_sinf_fast,
1116 "float", "(float)" },
1117 { "cosf_fast", (void *)&SC_cosf_fast,
1118 "float", "(float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001119 { "sinf", (void *)&sinf,
1120 "float", "(float)" },
1121 { "cosf", (void *)&cosf,
1122 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001123 { "asinf", (void *)&asinf,
1124 "float", "(float)" },
1125 { "acosf", (void *)&acosf,
1126 "float", "(float)" },
1127 { "atanf", (void *)&atanf,
1128 "float", "(float)" },
1129 { "atan2f", (void *)&atan2f,
Romain Guy9c59d022009-07-31 15:33:59 -07001130 "float", "(float, float)" },
Jason Samse9f5c532009-07-28 17:20:11 -07001131 { "fabsf", (void *)&fabsf,
Jason Samse45ac6e2009-07-20 14:31:06 -07001132 "float", "(float)" },
1133 { "randf", (void *)&SC_randf,
1134 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001135 { "randf2", (void *)&SC_randf2,
1136 "float", "(float, float)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001137 { "floorf", (void *)&floorf,
1138 "float", "(float)" },
Jason Samsdac98f52009-09-18 14:24:24 -07001139 { "fracf", (void *)&SC_fracf,
1140 "float", "(float)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001141 { "ceilf", (void *)&ceilf,
1142 "float", "(float)" },
Jason Samsdac98f52009-09-18 14:24:24 -07001143 { "roundf", (void *)&SC_roundf,
1144 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001145 { "expf", (void *)&expf,
1146 "float", "(float)" },
1147 { "logf", (void *)&logf,
1148 "float", "(float)" },
1149 { "powf", (void *)&powf,
1150 "float", "(float, float)" },
1151 { "maxf", (void *)&SC_maxf,
1152 "float", "(float, float)" },
1153 { "minf", (void *)&SC_minf,
1154 "float", "(float, float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -07001155 { "sqrt", (void *)&sqrt,
1156 "int", "(int)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001157 { "sqrtf", (void *)&sqrtf,
1158 "float", "(float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -07001159 { "sqr", (void *)&SC_sqr,
1160 "int", "(int)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001161 { "sqrf", (void *)&SC_sqrf,
1162 "float", "(float)" },
Romain Guyd6d4a5f2009-10-09 16:05:25 -07001163 { "sign", (void *)&SC_sign,
1164 "int", "(int)" },
1165 { "signf", (void *)&SC_signf,
1166 "float", "(float)" },
Romain Guy27162ab2009-08-09 17:04:54 -07001167 { "clamp", (void *)&SC_clamp,
1168 "int", "(int, int, int)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001169 { "clampf", (void *)&SC_clampf,
1170 "float", "(float, float, float)" },
1171 { "distf2", (void *)&SC_distf2,
1172 "float", "(float, float, float, float)" },
1173 { "distf3", (void *)&SC_distf3,
1174 "float", "(float, float, float, float, float, float)" },
1175 { "magf2", (void *)&SC_magf2,
1176 "float", "(float, float)" },
1177 { "magf3", (void *)&SC_magf3,
1178 "float", "(float, float, float)" },
1179 { "radf", (void *)&SC_radf,
1180 "float", "(float)" },
1181 { "degf", (void *)&SC_degf,
1182 "float", "(float)" },
1183 { "lerpf", (void *)&SC_lerpf,
1184 "float", "(float, float, float)" },
1185 { "normf", (void *)&SC_normf,
1186 "float", "(float, float, float)" },
1187 { "mapf", (void *)&SC_mapf,
1188 "float", "(float, float, float, float, float)" },
Romain Guyb7f1a6d2009-08-03 21:12:51 -07001189 { "noisef", (void *)&SC_noisef,
1190 "float", "(float)" },
1191 { "noisef2", (void *)&SC_noisef2,
1192 "float", "(float, float)" },
1193 { "noisef3", (void *)&SC_noisef3,
1194 "float", "(float, float, float)" },
1195 { "turbulencef2", (void *)&SC_turbulencef2,
1196 "float", "(float, float, float)" },
1197 { "turbulencef3", (void *)&SC_turbulencef3,
1198 "float", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001199
Romain Guy98e10fd2009-07-30 18:45:01 -07001200 // time
1201 { "second", (void *)&SC_second,
1202 "int", "()" },
1203 { "minute", (void *)&SC_minute,
1204 "int", "()" },
1205 { "hour", (void *)&SC_hour,
1206 "int", "()" },
Romain Guy39dbc802009-07-31 11:20:59 -07001207 { "day", (void *)&SC_day,
1208 "int", "()" },
1209 { "month", (void *)&SC_month,
1210 "int", "()" },
1211 { "year", (void *)&SC_year,
1212 "int", "()" },
Joe Onorato9c4e4ca2009-08-09 11:39:02 -07001213 { "uptimeMillis", (void*)&SC_uptimeMillis,
1214 "int", "()" }, // TODO: use long instead
1215 { "startTimeMillis", (void*)&SC_startTimeMillis,
1216 "int", "()" }, // TODO: use long instead
1217 { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis,
1218 "int", "()" }, // TODO: use long instead
Romain Guy98e10fd2009-07-30 18:45:01 -07001219
Jason Samse45ac6e2009-07-20 14:31:06 -07001220 // matrix
1221 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
1222 "void", "(float *mat)" },
1223 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
1224 "void", "(float *mat, float *f)" },
1225 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
1226 "void", "(float *mat, float *newmat)" },
1227 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
1228 "void", "(float *mat, float rot, float x, float y, float z)" },
1229 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
1230 "void", "(float *mat, float x, float y, float z)" },
1231 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
1232 "void", "(float *mat, float x, float y, float z)" },
1233 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
1234 "void", "(float *mat, float *lhs, float *rhs)" },
1235 { "matrixMultiply", (void *)&SC_matrixMultiply,
1236 "void", "(float *mat, float *rhs)" },
1237 { "matrixRotate", (void *)&SC_matrixRotate,
1238 "void", "(float *mat, float rot, float x, float y, float z)" },
1239 { "matrixScale", (void *)&SC_matrixScale,
1240 "void", "(float *mat, float x, float y, float z)" },
1241 { "matrixTranslate", (void *)&SC_matrixTranslate,
1242 "void", "(float *mat, float x, float y, float z)" },
1243
Jason Sams90b36a82009-08-17 13:56:09 -07001244 // vector
1245 { "vec2Rand", (void *)&SC_vec2Rand,
1246 "void", "(float *vec, float maxLen)" },
1247
Jason Samsa57c0a72009-09-04 14:42:41 -07001248 // vec3
1249 { "vec3Norm", (void *)&SC_vec3Norm,
1250 "void", "(struct vec3_s *)" },
1251 { "vec3Length", (void *)&SC_vec3Length,
1252 "float", "(struct vec3_s *)" },
1253 { "vec3Add", (void *)&SC_vec3Add,
1254 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1255 { "vec3Sub", (void *)&SC_vec3Sub,
1256 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1257 { "vec3Cross", (void *)&SC_vec3Cross,
1258 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1259 { "vec3Dot", (void *)&SC_vec3Dot,
1260 "float", "(struct vec3_s *lhs, struct vec3_s *rhs)" },
1261 { "vec3Scale", (void *)&SC_vec3Scale,
1262 "void", "(struct vec3_s *lhs, float scale)" },
1263
Romain Guyd6d4a5f2009-10-09 16:05:25 -07001264 // vec4
1265 { "vec4Norm", (void *)&SC_vec4Norm,
1266 "void", "(struct vec4_s *)" },
1267 { "vec4Length", (void *)&SC_vec4Length,
1268 "float", "(struct vec4_s *)" },
1269 { "vec4Add", (void *)&SC_vec4Add,
1270 "void", "(struct vec4_s *dest, struct vec4_s *lhs, struct vec4_s *rhs)" },
1271 { "vec4Sub", (void *)&SC_vec4Sub,
1272 "void", "(struct vec4_s *dest, struct vec4_s *lhs, struct vec4_s *rhs)" },
1273 { "vec4Dot", (void *)&SC_vec4Dot,
1274 "float", "(struct vec4_s *lhs, struct vec4_s *rhs)" },
1275 { "vec4Scale", (void *)&SC_vec4Scale,
1276 "void", "(struct vec4_s *lhs, float scale)" },
1277
Jason Samse45ac6e2009-07-20 14:31:06 -07001278 // context
1279 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
1280 "void", "(int)" },
1281 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
1282 "void", "(int)" },
Jason Samsb681c8a2009-09-28 18:12:56 -07001283 { "bindProgramStore", (void *)&SC_bindProgramFragmentStore,
1284 "void", "(int)" },
Jason Samsb5909ce2009-07-21 12:20:54 -07001285 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
1286 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001287 { "bindSampler", (void *)&SC_bindSampler,
1288 "void", "(int, int, int)" },
1289 { "bindTexture", (void *)&SC_bindTexture,
1290 "void", "(int, int, int)" },
1291
Jason Samsc9d43db2009-07-28 12:02:16 -07001292 // vp
Jason Sams50253db2009-07-29 20:55:44 -07001293 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -07001294 "void", "(void *)" },
Jason Sams50253db2009-07-29 20:55:44 -07001295 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -07001296 "void", "(void *)" },
1297
1298
1299
Jason Samse45ac6e2009-07-20 14:31:06 -07001300 // drawing
Jason Samse9f5c532009-07-28 17:20:11 -07001301 { "drawRect", (void *)&SC_drawRect,
1302 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001303 { "drawQuad", (void *)&SC_drawQuad,
1304 "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 -07001305 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords,
1306 "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 -07001307 { "drawSprite", (void *)&SC_drawSprite,
1308 "void", "(float x, float y, float z, float w, float h)" },
1309 { "drawSpriteScreenspace", (void *)&SC_drawSpriteScreenspace,
1310 "void", "(float x, float y, float z, float w, float h)" },
Romain Guyd369e272009-08-07 15:40:32 -07001311 { "drawLine", (void *)&SC_drawLine,
1312 "void", "(float x1, float y1, float z1, float x2, float y2, float z2)" },
Jason Samsb681c8a2009-09-28 18:12:56 -07001313 { "drawPoint", (void *)&SC_drawPoint,
1314 "void", "(float x1, float y1, float z1)" },
Jason Samse5ffb872009-08-09 17:01:55 -07001315 { "drawSimpleMesh", (void *)&SC_drawSimpleMesh,
1316 "void", "(int ism)" },
1317 { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange,
1318 "void", "(int ism, int start, int len)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001319
1320
1321 // misc
1322 { "pfClearColor", (void *)&SC_ClearColor,
1323 "void", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001324 { "color", (void *)&SC_color,
1325 "void", "(float, float, float, float)" },
Romain Guy9c59d022009-07-31 15:33:59 -07001326 { "hsb", (void *)&SC_hsb,
1327 "void", "(float, float, float, float)" },
Romain Guy370ed152009-08-20 17:08:33 -07001328 { "hsbToRgb", (void *)&SC_hsbToRgb,
1329 "void", "(float, float, float, float*)" },
1330 { "hsbToAbgr", (void *)&SC_hsbToAbgr,
1331 "int", "(float, float, float, float)" },
Romain Guy48b7edc2009-08-06 22:52:13 -07001332 { "ambient", (void *)&SC_ambient,
1333 "void", "(float, float, float, float)" },
1334 { "diffuse", (void *)&SC_diffuse,
1335 "void", "(float, float, float, float)" },
1336 { "specular", (void *)&SC_specular,
1337 "void", "(float, float, float, float)" },
1338 { "emission", (void *)&SC_emission,
1339 "void", "(float, float, float, float)" },
1340 { "shininess", (void *)&SC_shininess,
Romain Guyd369e272009-08-07 15:40:32 -07001341 "void", "(float)" },
Romain Guye62cc902009-09-04 17:55:41 -07001342 { "pointAttenuation", (void *)&SC_pointAttenuation,
1343 "void", "(float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001344
Jason Samsc9d43db2009-07-28 12:02:16 -07001345 { "uploadToTexture", (void *)&SC_uploadToTexture,
1346 "void", "(int, int)" },
Jason Samse5ffb872009-08-09 17:01:55 -07001347 { "uploadToBufferObject", (void *)&SC_uploadToBufferObject,
1348 "void", "(int)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001349
Jason Sams90b36a82009-08-17 13:56:09 -07001350 { "colorFloatRGBAtoUNorm8", (void *)&SC_colorFloatRGBAtoUNorm8,
1351 "int", "(float, float, float, float)" },
1352 { "colorFloatRGBto565", (void *)&SC_colorFloatRGBAto565,
1353 "int", "(float, float, float)" },
1354
1355
Jason Samse579df42009-08-10 14:55:26 -07001356 { "getWidth", (void *)&SC_getWidth,
1357 "int", "()" },
1358 { "getHeight", (void *)&SC_getHeight,
1359 "int", "()" },
1360
Jason Sams8c401ef2009-10-06 13:58:47 -07001361 { "sendToClient", (void *)&SC_toClient,
1362 "int", "(void *data, int cmdID, int len, int waitForSpace)" },
Jason Samse579df42009-08-10 14:55:26 -07001363
Jason Samsc9d43db2009-07-28 12:02:16 -07001364
1365 { "debugF", (void *)&SC_debugF,
1366 "void", "(void *, float)" },
1367 { "debugI32", (void *)&SC_debugI32,
1368 "void", "(void *, int)" },
Romain Guy370ed152009-08-20 17:08:33 -07001369 { "debugHexF", (void *)&SC_debugHexF,
1370 "void", "(void *, float)" },
1371 { "debugHexI32", (void *)&SC_debugHexI32,
1372 "void", "(void *, int)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001373
Jason Sams3a27c952009-10-07 18:14:01 -07001374 { "scriptCall", (void *)&SC_scriptCall,
1375 "void", "(int)" },
1376
Jason Samsc9d43db2009-07-28 12:02:16 -07001377
Jason Samse45ac6e2009-07-20 14:31:06 -07001378 { NULL, NULL, NULL, NULL }
1379};
1380
1381const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
1382{
1383 ScriptCState::SymbolTable_t *syms = gSyms;
1384
1385 while (syms->mPtr) {
1386 if (!strcmp(syms->mName, sym)) {
1387 return syms;
1388 }
1389 syms++;
1390 }
1391 return NULL;
1392}
1393
1394void ScriptCState::appendDecls(String8 *str)
1395{
1396 ScriptCState::SymbolTable_t *syms = gSyms;
1397 while (syms->mPtr) {
1398 str->append(syms->mRet);
1399 str->append(" ");
1400 str->append(syms->mName);
1401 str->append(syms->mParam);
1402 str->append(";\n");
1403 syms++;
1404 }
1405}
1406
1407