blob: 23888ff81f7a27719dccff884eea13523d01ae5b [file] [log] [blame]
Jason Samsc97bb882009-07-20 14:31:06 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsContext.h"
18#include "rsScriptC.h"
19#include "rsMatrix.h"
Romain Guyecc7ca032009-08-03 21:12:51 -070020#include "rsNoise.h"
Jason Samsc97bb882009-07-20 14:31:06 -070021
22#include "acc/acc.h"
Joe Onorato3370ec92009-08-09 11:39:02 -070023#include "utils/Timers.h"
Jason Samsc97bb882009-07-20 14:31:06 -070024
Jason Samse9ad9a72009-09-30 17:36:20 -070025#define GL_GLEXT_PROTOTYPES
26
Jason Samsc97bb882009-07-20 14:31:06 -070027#include <GLES/gl.h>
28#include <GLES/glext.h>
29
Romain Guy584a3752009-07-30 18:45:01 -070030#include <time.h>
Romain Guy584a3752009-07-30 18:45:01 -070031
Jason Samsc97bb882009-07-20 14:31:06 -070032using namespace android;
33using namespace android::renderscript;
34
35#define GET_TLS() Context::ScriptTLSStruct * tls = \
36 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
37 Context * rsc = tls->mContext; \
38 ScriptC * sc = (ScriptC *) tls->mScript
39
Jason Samsea84a7c2009-09-04 14:42:41 -070040typedef struct {
41 float x;
42 float y;
43 float z;
44} vec3_t;
45
46typedef struct {
47 float x;
48 float y;
49 float z;
50 float w;
51} vec4_t;
52
53typedef struct {
54 float x;
55 float y;
56} vec2_t;
Jason Samsc97bb882009-07-20 14:31:06 -070057
58//////////////////////////////////////////////////////////////////////////////
59// IO routines
60//////////////////////////////////////////////////////////////////////////////
61
62static float SC_loadF(uint32_t bank, uint32_t offset)
63{
64 GET_TLS();
65 const void *vp = sc->mSlots[bank]->getPtr();
66 const float *f = static_cast<const float *>(vp);
67 //LOGE("loadF %i %i = %f %x", bank, offset, f, ((int *)&f)[0]);
68 return f[offset];
69}
70
71static int32_t SC_loadI32(uint32_t bank, uint32_t offset)
72{
73 GET_TLS();
74 const void *vp = sc->mSlots[bank]->getPtr();
75 const int32_t *i = static_cast<const int32_t *>(vp);
76 //LOGE("loadI32 %i %i = %i", bank, offset, t);
77 return i[offset];
78}
79
Romain Guyf8e136d2009-08-06 12:40:41 -070080static float* SC_loadArrayF(uint32_t bank, uint32_t offset)
Romain Guya2136d62009-08-04 17:19:48 -070081{
82 GET_TLS();
83 void *vp = sc->mSlots[bank]->getPtr();
84 float *f = static_cast<float *>(vp);
Romain Guyf8e136d2009-08-06 12:40:41 -070085 return f + offset;
Romain Guya2136d62009-08-04 17:19:48 -070086}
87
Romain Guyf8e136d2009-08-06 12:40:41 -070088static int32_t* SC_loadArrayI32(uint32_t bank, uint32_t offset)
Romain Guya2136d62009-08-04 17:19:48 -070089{
90 GET_TLS();
91 void *vp = sc->mSlots[bank]->getPtr();
92 int32_t *i = static_cast<int32_t *>(vp);
Romain Guyf8e136d2009-08-06 12:40:41 -070093 return i + offset;
Romain Guya2136d62009-08-04 17:19:48 -070094}
95
Jason Sams6b9dec02009-09-23 16:38:37 -070096static float* SC_loadSimpleMeshVerticesF(RsSimpleMesh mesh, uint32_t idx)
Romain Guyb62627e2009-08-06 22:52:13 -070097{
Jason Sams6b9dec02009-09-23 16:38:37 -070098 SimpleMesh *tm = static_cast<SimpleMesh *>(mesh);
99 void *vp = tm->mVertexBuffers[idx]->getPtr();;
100 return static_cast<float *>(vp);
Romain Guyb62627e2009-08-06 22:52:13 -0700101}
102
Jason Sams6b9dec02009-09-23 16:38:37 -0700103static void SC_updateSimpleMesh(RsSimpleMesh mesh)
Romain Guyb62627e2009-08-06 22:52:13 -0700104{
Jason Sams6b9dec02009-09-23 16:38:37 -0700105 SimpleMesh *sm = static_cast<SimpleMesh *>(mesh);
106 sm->uploadAll();
Romain Guyb62627e2009-08-06 22:52:13 -0700107}
Romain Guyf8e136d2009-08-06 12:40:41 -0700108
Jason Samsc97bb882009-07-20 14:31:06 -0700109static uint32_t SC_loadU32(uint32_t bank, uint32_t offset)
110{
111 GET_TLS();
112 const void *vp = sc->mSlots[bank]->getPtr();
113 const uint32_t *i = static_cast<const uint32_t *>(vp);
114 return i[offset];
115}
116
117static void SC_loadVec4(uint32_t bank, uint32_t offset, rsc_Vector4 *v)
118{
119 GET_TLS();
120 const void *vp = sc->mSlots[bank]->getPtr();
121 const float *f = static_cast<const float *>(vp);
122 memcpy(v, &f[offset], sizeof(rsc_Vector4));
123}
124
125static void SC_loadMatrix(uint32_t bank, uint32_t offset, rsc_Matrix *m)
126{
127 GET_TLS();
128 const void *vp = sc->mSlots[bank]->getPtr();
129 const float *f = static_cast<const float *>(vp);
130 memcpy(m, &f[offset], sizeof(rsc_Matrix));
131}
132
133
134static void SC_storeF(uint32_t bank, uint32_t offset, float v)
135{
136 //LOGE("storeF %i %i %f", bank, offset, v);
137 GET_TLS();
138 void *vp = sc->mSlots[bank]->getPtr();
139 float *f = static_cast<float *>(vp);
140 f[offset] = v;
141}
142
143static void SC_storeI32(uint32_t bank, uint32_t offset, int32_t v)
144{
145 GET_TLS();
146 void *vp = sc->mSlots[bank]->getPtr();
147 int32_t *f = static_cast<int32_t *>(vp);
148 static_cast<int32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
149}
150
151static void SC_storeU32(uint32_t bank, uint32_t offset, uint32_t v)
152{
153 GET_TLS();
154 void *vp = sc->mSlots[bank]->getPtr();
155 uint32_t *f = static_cast<uint32_t *>(vp);
156 static_cast<uint32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
157}
158
159static void SC_storeVec4(uint32_t bank, uint32_t offset, const rsc_Vector4 *v)
160{
161 GET_TLS();
162 void *vp = sc->mSlots[bank]->getPtr();
163 float *f = static_cast<float *>(vp);
164 memcpy(&f[offset], v, sizeof(rsc_Vector4));
165}
166
167static void SC_storeMatrix(uint32_t bank, uint32_t offset, const rsc_Matrix *m)
168{
169 GET_TLS();
170 void *vp = sc->mSlots[bank]->getPtr();
171 float *f = static_cast<float *>(vp);
172 memcpy(&f[offset], m, sizeof(rsc_Matrix));
173}
174
Jason Samsea84a7c2009-09-04 14:42:41 -0700175//////////////////////////////////////////////////////////////////////////////
176// Vec3 routines
177//////////////////////////////////////////////////////////////////////////////
178
179static void SC_vec3Norm(vec3_t *v)
180{
181 float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
182 len = 1 / len;
183 v->x *= len;
184 v->y *= len;
185 v->z *= len;
186}
187
188static float SC_vec3Length(const vec3_t *v)
189{
190 return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
191}
192
193static void SC_vec3Add(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
194{
195 dest->x = lhs->x + rhs->x;
196 dest->y = lhs->y + rhs->y;
197 dest->z = lhs->z + rhs->z;
198}
199
200static void SC_vec3Sub(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
201{
202 dest->x = lhs->x - rhs->x;
203 dest->y = lhs->y - rhs->y;
204 dest->z = lhs->z - rhs->z;
205}
206
207static void SC_vec3Cross(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
208{
209 float x = lhs->y * rhs->z - lhs->z * rhs->y;
210 float y = lhs->z * rhs->x - lhs->x * rhs->z;
211 float z = lhs->x * rhs->y - lhs->y * rhs->x;
212 dest->x = x;
213 dest->y = y;
214 dest->z = z;
215}
216
217static float SC_vec3Dot(const vec3_t *lhs, const vec3_t *rhs)
218{
219 return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z;
220}
221
222static void SC_vec3Scale(vec3_t *lhs, float scale)
223{
224 lhs->x *= scale;
225 lhs->y *= scale;
226 lhs->z *= scale;
227}
228
Romain Guyd7fa1222009-10-09 16:05:25 -0700229//////////////////////////////////////////////////////////////////////////////
230// Vec4 routines
231//////////////////////////////////////////////////////////////////////////////
232
233static void SC_vec4Norm(vec4_t *v)
234{
235 float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w);
236 len = 1 / len;
237 v->x *= len;
238 v->y *= len;
239 v->z *= len;
240 v->w *= len;
241}
242
243static float SC_vec4Length(const vec4_t *v)
244{
245 return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w);
246}
247
248static void SC_vec4Add(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs)
249{
250 dest->x = lhs->x + rhs->x;
251 dest->y = lhs->y + rhs->y;
252 dest->z = lhs->z + rhs->z;
253 dest->w = lhs->w + rhs->w;
254}
255
256static void SC_vec4Sub(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs)
257{
258 dest->x = lhs->x - rhs->x;
259 dest->y = lhs->y - rhs->y;
260 dest->z = lhs->z - rhs->z;
261 dest->w = lhs->w - rhs->w;
262}
263
264static float SC_vec4Dot(const vec4_t *lhs, const vec4_t *rhs)
265{
266 return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z + lhs->w * rhs->w;
267}
268
269static void SC_vec4Scale(vec4_t *lhs, float scale)
270{
271 lhs->x *= scale;
272 lhs->y *= scale;
273 lhs->z *= scale;
274 lhs->w *= scale;
275}
Jason Samsc97bb882009-07-20 14:31:06 -0700276
277//////////////////////////////////////////////////////////////////////////////
278// Math routines
279//////////////////////////////////////////////////////////////////////////////
280
Romain Guy8839ca52009-07-31 11:20:59 -0700281#define PI 3.1415926f
282#define DEG_TO_RAD PI / 180.0f
283#define RAD_TO_DEG 180.0f / PI
284
Romain Guycac80a62009-08-18 11:39:17 -0700285static float SC_sinf_fast(float x)
286{
287 const float A = 1.0f / (2.0f * M_PI);
288 const float B = -16.0f;
289 const float C = 8.0f;
Jason Samsea84a7c2009-09-04 14:42:41 -0700290
Romain Guycac80a62009-08-18 11:39:17 -0700291 // scale angle for easy argument reduction
292 x *= A;
Jason Samsea84a7c2009-09-04 14:42:41 -0700293
Romain Guycac80a62009-08-18 11:39:17 -0700294 if (fabsf(x) >= 0.5f) {
295 // argument reduction
296 x = x - ceilf(x + 0.5f) + 1.0f;
297 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700298
Romain Guycac80a62009-08-18 11:39:17 -0700299 const float y = B * x * fabsf(x) + C * x;
300 return 0.2215f * (y * fabsf(y) - y) + y;
301}
302
303static float SC_cosf_fast(float x)
304{
305 x += float(M_PI / 2);
306
307 const float A = 1.0f / (2.0f * M_PI);
308 const float B = -16.0f;
309 const float C = 8.0f;
Jason Samsea84a7c2009-09-04 14:42:41 -0700310
Romain Guycac80a62009-08-18 11:39:17 -0700311 // scale angle for easy argument reduction
312 x *= A;
Jason Samsea84a7c2009-09-04 14:42:41 -0700313
Romain Guycac80a62009-08-18 11:39:17 -0700314 if (fabsf(x) >= 0.5f) {
315 // argument reduction
316 x = x - ceilf(x + 0.5f) + 1.0f;
317 }
Jason Samsea84a7c2009-09-04 14:42:41 -0700318
Romain Guycac80a62009-08-18 11:39:17 -0700319 const float y = B * x * fabsf(x) + C * x;
320 return 0.2215f * (y * fabsf(y) - y) + y;
321}
322
Jason Samsc97bb882009-07-20 14:31:06 -0700323static float SC_randf(float max)
324{
325 float r = (float)rand();
326 return r / RAND_MAX * max;
327}
328
Romain Guy8839ca52009-07-31 11:20:59 -0700329static float SC_randf2(float min, float max)
330{
331 float r = (float)rand();
332 return r / RAND_MAX * (max - min) + min;
333}
334
Romain Guyd7fa1222009-10-09 16:05:25 -0700335static int SC_sign(int value)
336{
337 return (value > 0) - (value < 0);
338}
339
340static float SC_signf(float value)
341{
342 return (value > 0) - (value < 0);
343}
344
Romain Guy8839ca52009-07-31 11:20:59 -0700345static float SC_clampf(float amount, float low, float high)
346{
347 return amount < low ? low : (amount > high ? high : amount);
348}
349
Romain Guya9d2d5e2009-08-09 17:04:54 -0700350static int SC_clamp(int amount, int low, int high)
351{
352 return amount < low ? low : (amount > high ? high : amount);
353}
354
Romain Guy8839ca52009-07-31 11:20:59 -0700355static float SC_maxf(float a, float b)
356{
Jason Sams1bada8c2009-08-09 17:01:55 -0700357 return a > b ? a : b;
Romain Guy8839ca52009-07-31 11:20:59 -0700358}
359
360static float SC_minf(float a, float b)
361{
Jason Sams1bada8c2009-08-09 17:01:55 -0700362 return a < b ? a : b;
Romain Guy8839ca52009-07-31 11:20:59 -0700363}
364
365static float SC_sqrf(float v)
366{
Jason Sams1bada8c2009-08-09 17:01:55 -0700367 return v * v;
Romain Guy8839ca52009-07-31 11:20:59 -0700368}
369
Romain Guy8f5c94b2009-08-08 18:30:19 -0700370static int SC_sqr(int v)
371{
372 return v * v;
373}
374
Jason Samsd342fd72009-09-18 14:24:24 -0700375static float SC_fracf(float v)
376{
377 return v - floorf(v);
378}
379
380static float SC_roundf(float v)
381{
382 return floorf(v + 0.4999999999);
383}
384
Romain Guy8839ca52009-07-31 11:20:59 -0700385static float SC_distf2(float x1, float y1, float x2, float y2)
386{
387 float x = x2 - x1;
388 float y = y2 - y1;
Jason Sams1bada8c2009-08-09 17:01:55 -0700389 return sqrtf(x * x + y * y);
Romain Guy8839ca52009-07-31 11:20:59 -0700390}
391
392static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2)
393{
394 float x = x2 - x1;
395 float y = y2 - y1;
396 float z = z2 - z1;
Jason Sams1bada8c2009-08-09 17:01:55 -0700397 return sqrtf(x * x + y * y + z * z);
Romain Guy8839ca52009-07-31 11:20:59 -0700398}
399
400static float SC_magf2(float a, float b)
401{
402 return sqrtf(a * a + b * b);
403}
404
405static float SC_magf3(float a, float b, float c)
406{
407 return sqrtf(a * a + b * b + c * c);
408}
409
410static float SC_radf(float degrees)
411{
Jason Sams1bada8c2009-08-09 17:01:55 -0700412 return degrees * DEG_TO_RAD;
Romain Guy8839ca52009-07-31 11:20:59 -0700413}
414
415static float SC_degf(float radians)
416{
Jason Sams1bada8c2009-08-09 17:01:55 -0700417 return radians * RAD_TO_DEG;
Romain Guy8839ca52009-07-31 11:20:59 -0700418}
419
420static float SC_lerpf(float start, float stop, float amount)
421{
422 return start + (stop - start) * amount;
423}
424
425static float SC_normf(float start, float stop, float value)
426{
427 return (value - start) / (stop - start);
428}
429
430static float SC_mapf(float minStart, float minStop, float maxStart, float maxStop, float value)
431{
432 return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
433}
Jason Samsc97bb882009-07-20 14:31:06 -0700434
Romain Guy584a3752009-07-30 18:45:01 -0700435//////////////////////////////////////////////////////////////////////////////
436// Time routines
437//////////////////////////////////////////////////////////////////////////////
Jason Samsc97bb882009-07-20 14:31:06 -0700438
Joe Onorato3370ec92009-08-09 11:39:02 -0700439static int32_t SC_second()
Romain Guy584a3752009-07-30 18:45:01 -0700440{
441 GET_TLS();
442
443 time_t rawtime;
444 time(&rawtime);
445
Romain Guybaed7272009-11-11 15:36:06 -0800446 struct tm *timeinfo;
447 timeinfo = localtime(&rawtime);
448 return timeinfo->tm_sec;
Romain Guy584a3752009-07-30 18:45:01 -0700449}
450
Joe Onorato3370ec92009-08-09 11:39:02 -0700451static int32_t SC_minute()
Romain Guy584a3752009-07-30 18:45:01 -0700452{
453 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700454
Romain Guy584a3752009-07-30 18:45:01 -0700455 time_t rawtime;
456 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700457
Romain Guybaed7272009-11-11 15:36:06 -0800458 struct tm *timeinfo;
459 timeinfo = localtime(&rawtime);
460 return timeinfo->tm_min;
Jason Sams1bada8c2009-08-09 17:01:55 -0700461}
Romain Guy584a3752009-07-30 18:45:01 -0700462
Joe Onorato3370ec92009-08-09 11:39:02 -0700463static int32_t SC_hour()
Romain Guy584a3752009-07-30 18:45:01 -0700464{
465 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700466
Romain Guy584a3752009-07-30 18:45:01 -0700467 time_t rawtime;
468 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700469
Romain Guybaed7272009-11-11 15:36:06 -0800470 struct tm *timeinfo;
471 timeinfo = localtime(&rawtime);
472 return timeinfo->tm_hour;
Romain Guy8839ca52009-07-31 11:20:59 -0700473}
474
Joe Onorato3370ec92009-08-09 11:39:02 -0700475static int32_t SC_day()
Romain Guy8839ca52009-07-31 11:20:59 -0700476{
477 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700478
Romain Guy8839ca52009-07-31 11:20:59 -0700479 time_t rawtime;
480 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700481
Romain Guybaed7272009-11-11 15:36:06 -0800482 struct tm *timeinfo;
483 timeinfo = localtime(&rawtime);
484 return timeinfo->tm_mday;
Jason Sams1bada8c2009-08-09 17:01:55 -0700485}
Jason Samsc97bb882009-07-20 14:31:06 -0700486
Joe Onorato3370ec92009-08-09 11:39:02 -0700487static int32_t SC_month()
Romain Guy8839ca52009-07-31 11:20:59 -0700488{
489 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700490
Romain Guy8839ca52009-07-31 11:20:59 -0700491 time_t rawtime;
492 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700493
Romain Guybaed7272009-11-11 15:36:06 -0800494 struct tm *timeinfo;
495 timeinfo = localtime(&rawtime);
496 return timeinfo->tm_mon;
Jason Sams1bada8c2009-08-09 17:01:55 -0700497}
Romain Guy8839ca52009-07-31 11:20:59 -0700498
Joe Onorato3370ec92009-08-09 11:39:02 -0700499static int32_t SC_year()
Romain Guy8839ca52009-07-31 11:20:59 -0700500{
501 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700502
Romain Guy8839ca52009-07-31 11:20:59 -0700503 time_t rawtime;
504 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700505
Romain Guybaed7272009-11-11 15:36:06 -0800506 struct tm *timeinfo;
507 timeinfo = localtime(&rawtime);
508 return timeinfo->tm_year;
Romain Guy8839ca52009-07-31 11:20:59 -0700509}
510
Joe Onorato3370ec92009-08-09 11:39:02 -0700511static int32_t SC_uptimeMillis()
512{
513 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
514}
515
516static int32_t SC_startTimeMillis()
517{
518 GET_TLS();
519 return sc->mEnviroment.mStartTimeMillis;
520}
521
522static int32_t SC_elapsedTimeMillis()
523{
524 GET_TLS();
525 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC))
526 - sc->mEnviroment.mStartTimeMillis;
527}
528
Jason Samsc97bb882009-07-20 14:31:06 -0700529//////////////////////////////////////////////////////////////////////////////
530// Matrix routines
531//////////////////////////////////////////////////////////////////////////////
532
533
534static void SC_matrixLoadIdentity(rsc_Matrix *mat)
535{
536 Matrix *m = reinterpret_cast<Matrix *>(mat);
537 m->loadIdentity();
538}
539
540static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
541{
542 Matrix *m = reinterpret_cast<Matrix *>(mat);
543 m->load(f);
544}
545
546static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
547{
548 Matrix *m = reinterpret_cast<Matrix *>(mat);
549 m->load(reinterpret_cast<const Matrix *>(newmat));
550}
551
552static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
553{
554 Matrix *m = reinterpret_cast<Matrix *>(mat);
555 m->loadRotate(rot, x, y, z);
556}
557
558static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
559{
560 Matrix *m = reinterpret_cast<Matrix *>(mat);
561 m->loadScale(x, y, z);
562}
563
564static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
565{
566 Matrix *m = reinterpret_cast<Matrix *>(mat);
567 m->loadTranslate(x, y, z);
568}
569
570static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
571{
572 Matrix *m = reinterpret_cast<Matrix *>(mat);
573 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
574 reinterpret_cast<const Matrix *>(rhs));
575}
576
577static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
578{
579 Matrix *m = reinterpret_cast<Matrix *>(mat);
580 m->multiply(reinterpret_cast<const Matrix *>(rhs));
581}
582
583static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
584{
585 Matrix *m = reinterpret_cast<Matrix *>(mat);
586 m->rotate(rot, x, y, z);
587}
588
589static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
590{
591 Matrix *m = reinterpret_cast<Matrix *>(mat);
592 m->scale(x, y, z);
593}
594
595static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
596{
597 Matrix *m = reinterpret_cast<Matrix *>(mat);
598 m->translate(x, y, z);
599}
600
601
Jason Sams334ea0c2009-08-17 13:56:09 -0700602static void SC_vec2Rand(float *vec, float maxLen)
603{
604 float angle = SC_randf(PI * 2);
605 float len = SC_randf(maxLen);
606 vec[0] = len * sinf(angle);
607 vec[1] = len * cosf(angle);
608}
609
Jason Samsc97bb882009-07-20 14:31:06 -0700610
611
612//////////////////////////////////////////////////////////////////////////////
613// Context
614//////////////////////////////////////////////////////////////////////////////
615
616static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
617{
618 GET_TLS();
619 rsi_ProgramFragmentBindTexture(rsc,
620 static_cast<ProgramFragment *>(vpf),
621 slot,
622 static_cast<Allocation *>(va));
623
624}
625
626static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
627{
628 GET_TLS();
629 rsi_ProgramFragmentBindSampler(rsc,
630 static_cast<ProgramFragment *>(vpf),
631 slot,
632 static_cast<Sampler *>(vs));
633
634}
635
636static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs)
637{
638 GET_TLS();
639 rsi_ContextBindProgramFragmentStore(rsc, pfs);
640
641}
642
643static void SC_bindProgramFragment(RsProgramFragment pf)
644{
645 GET_TLS();
646 rsi_ContextBindProgramFragment(rsc, pf);
647
648}
649
Jason Samsee411122009-07-21 12:20:54 -0700650static void SC_bindProgramVertex(RsProgramVertex pv)
651{
652 GET_TLS();
653 rsi_ContextBindProgramVertex(rsc, pv);
654
655}
Jason Samsc97bb882009-07-20 14:31:06 -0700656
657//////////////////////////////////////////////////////////////////////////////
Jason Samsb0ec1b42009-07-28 12:02:16 -0700658// VP
659//////////////////////////////////////////////////////////////////////////////
660
661static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
662{
663 GET_TLS();
664 rsc->getVertex()->setModelviewMatrix(m);
665}
666
667static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
668{
669 GET_TLS();
670 rsc->getVertex()->setTextureMatrix(m);
671}
672
673
674
675//////////////////////////////////////////////////////////////////////////////
Jason Samsc97bb882009-07-20 14:31:06 -0700676// Drawing
677//////////////////////////////////////////////////////////////////////////////
678
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700679static void SC_drawLine(float x1, float y1, float z1,
680 float x2, float y2, float z2)
681{
682 GET_TLS();
683 rsc->setupCheck();
684
685 float vtx[] = { x1, y1, z1, x2, y2, z2 };
686
687 glBindBuffer(GL_ARRAY_BUFFER, 0);
688 glEnableClientState(GL_VERTEX_ARRAY);
689 glVertexPointer(3, GL_FLOAT, 0, vtx);
690
691 glDisableClientState(GL_NORMAL_ARRAY);
692 glDisableClientState(GL_COLOR_ARRAY);
693
694 glDrawArrays(GL_LINES, 0, 2);
695}
696
Jason Sams5235cf32009-09-28 18:12:56 -0700697static void SC_drawPoint(float x, float y, float z)
698{
699 GET_TLS();
700 rsc->setupCheck();
701
702 float vtx[] = { x, y, z };
703
704 glBindBuffer(GL_ARRAY_BUFFER, 0);
705 glEnableClientState(GL_VERTEX_ARRAY);
706 glVertexPointer(3, GL_FLOAT, 0, vtx);
707
708 glDisableClientState(GL_NORMAL_ARRAY);
709 glDisableClientState(GL_COLOR_ARRAY);
710
711 glDrawArrays(GL_POINTS, 0, 1);
712}
713
Romain Guy8f5c94b2009-08-08 18:30:19 -0700714static void SC_drawQuadTexCoords(float x1, float y1, float z1,
715 float u1, float v1,
716 float x2, float y2, float z2,
717 float u2, float v2,
718 float x3, float y3, float z3,
719 float u3, float v3,
720 float x4, float y4, float z4,
721 float u4, float v4)
Jason Samsc97bb882009-07-20 14:31:06 -0700722{
723 GET_TLS();
Jason Sams40a29e82009-08-10 14:55:26 -0700724
Jason Samsc97bb882009-07-20 14:31:06 -0700725 //LOGE("Quad");
726 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
727 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
728 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
729 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
Jason Sams40a29e82009-08-10 14:55:26 -0700730
Jason Samsc97bb882009-07-20 14:31:06 -0700731 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
Romain Guy8f5c94b2009-08-08 18:30:19 -0700732 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
Jason Samsc97bb882009-07-20 14:31:06 -0700733
734 rsc->setupCheck();
735
736 glBindBuffer(GL_ARRAY_BUFFER, 0);
737 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
738
739 glEnableClientState(GL_VERTEX_ARRAY);
740 glVertexPointer(3, GL_FLOAT, 0, vtx);
741
742 glClientActiveTexture(GL_TEXTURE0);
743 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
744 glTexCoordPointer(2, GL_FLOAT, 0, tex);
745 glClientActiveTexture(GL_TEXTURE1);
746 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
747 glTexCoordPointer(2, GL_FLOAT, 0, tex);
748 glClientActiveTexture(GL_TEXTURE0);
749
750 glDisableClientState(GL_NORMAL_ARRAY);
751 glDisableClientState(GL_COLOR_ARRAY);
752
753 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
754
755 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
756}
757
Romain Guy8f5c94b2009-08-08 18:30:19 -0700758static void SC_drawQuad(float x1, float y1, float z1,
759 float x2, float y2, float z2,
760 float x3, float y3, float z3,
761 float x4, float y4, float z4)
762{
763 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
764 x2, y2, z2, 1, 1,
765 x3, y3, z3, 1, 0,
766 x4, y4, z4, 0, 0);
767}
768
Jason Samse9ad9a72009-09-30 17:36:20 -0700769static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h)
770{
771 GET_TLS();
772 rsc->setupCheck();
773
774 GLint crop[4] = {0, h, w, -h};
775 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
776 glDrawTexfOES(x, y, z, w, h);
777}
778
779static void SC_drawSprite(float x, float y, float z, float w, float h)
780{
781 GET_TLS();
782 rsc->setupCheck();
783
784 float vin[3] = {x, y, z};
785 float vout[4];
786
787 //LOGE("ds in %f %f %f", x, y, z);
788 rsc->getVertex()->transformToScreen(rsc, vout, vin);
789 //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]);
790 vout[0] /= vout[3];
791 vout[1] /= vout[3];
792 vout[2] /= vout[3];
793
794 vout[0] *= rsc->getWidth() / 2;
795 vout[1] *= rsc->getHeight() / 2;
796 vout[0] += rsc->getWidth() / 2;
797 vout[1] += rsc->getHeight() / 2;
798
799 vout[0] -= w/2;
800 vout[1] -= h/2;
801
802 //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]);
803
804 // U, V, W, H
805 GLint crop[4] = {0, h, w, -h};
806 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
807 glDrawTexiOES(vout[0], vout[1], 0/*vout[2]*/, w, h);
808}
809
810
Jason Sams6f5c61c2009-07-28 17:20:11 -0700811static void SC_drawRect(float x1, float y1,
812 float x2, float y2, float z)
813{
814 SC_drawQuad(x1, y2, z,
815 x2, y2, z,
816 x2, y1, z,
817 x1, y1, z);
818}
819
Jason Sams1bada8c2009-08-09 17:01:55 -0700820static void SC_drawSimpleMesh(RsSimpleMesh vsm)
821{
822 GET_TLS();
823 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
824 rsc->setupCheck();
825 sm->render();
826}
827
828static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len)
829{
830 GET_TLS();
831 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
832 rsc->setupCheck();
833 sm->renderRange(start, len);
834}
835
836
Jason Samsc97bb882009-07-20 14:31:06 -0700837//////////////////////////////////////////////////////////////////////////////
838//
839//////////////////////////////////////////////////////////////////////////////
840
Jason Samsc97bb882009-07-20 14:31:06 -0700841static void SC_color(float r, float g, float b, float a)
842{
843 glColor4f(r, g, b, a);
844}
845
Romain Guyb62627e2009-08-06 22:52:13 -0700846static void SC_ambient(float r, float g, float b, float a)
847{
848 GLfloat params[] = { r, g, b, a };
849 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, params);
850}
851
852static void SC_diffuse(float r, float g, float b, float a)
853{
854 GLfloat params[] = { r, g, b, a };
855 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, params);
856}
857
858static void SC_specular(float r, float g, float b, float a)
859{
860 GLfloat params[] = { r, g, b, a };
861 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, params);
862}
863
864static void SC_emission(float r, float g, float b, float a)
865{
866 GLfloat params[] = { r, g, b, a };
867 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, params);
868}
869
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700870static void SC_shininess(float s)
Romain Guyb62627e2009-08-06 22:52:13 -0700871{
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700872 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, s);
Romain Guyb62627e2009-08-06 22:52:13 -0700873}
874
Romain Guy2d496bf2009-09-04 17:55:41 -0700875static void SC_pointAttenuation(float a, float b, float c)
876{
877 GLfloat params[] = { a, b, c };
878 glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, params);
879}
880
Romain Guyd22fff72009-08-20 17:08:33 -0700881static void SC_hsbToRgb(float h, float s, float b, float* rgb)
Romain Guya32d1002009-07-31 15:33:59 -0700882{
883 float red = 0.0f;
884 float green = 0.0f;
885 float blue = 0.0f;
Jason Sams1bada8c2009-08-09 17:01:55 -0700886
Romain Guya32d1002009-07-31 15:33:59 -0700887 float x = h;
888 float y = s;
889 float z = b;
Jason Sams1bada8c2009-08-09 17:01:55 -0700890
Romain Guya32d1002009-07-31 15:33:59 -0700891 float hf = (x - (int) x) * 6.0f;
892 int ihf = (int) hf;
893 float f = hf - ihf;
894 float pv = z * (1.0f - y);
895 float qv = z * (1.0f - y * f);
896 float tv = z * (1.0f - y * (1.0f - f));
Jason Sams1bada8c2009-08-09 17:01:55 -0700897
Romain Guya32d1002009-07-31 15:33:59 -0700898 switch (ihf) {
899 case 0: // Red is the dominant color
900 red = z;
901 green = tv;
902 blue = pv;
903 break;
904 case 1: // Green is the dominant color
905 red = qv;
906 green = z;
907 blue = pv;
908 break;
909 case 2:
910 red = pv;
911 green = z;
912 blue = tv;
913 break;
914 case 3: // Blue is the dominant color
915 red = pv;
916 green = qv;
917 blue = z;
918 break;
919 case 4:
920 red = tv;
921 green = pv;
922 blue = z;
923 break;
924 case 5: // Red is the dominant color
925 red = z;
926 green = pv;
927 blue = qv;
928 break;
929 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700930
Romain Guyd22fff72009-08-20 17:08:33 -0700931 rgb[0] = red;
932 rgb[1] = green;
933 rgb[2] = blue;
934}
935
936static int SC_hsbToAbgr(float h, float s, float b, float a)
937{
938 float rgb[3];
939 SC_hsbToRgb(h, s, b, rgb);
940 return int(a * 255.0f) << 24 |
941 int(rgb[2] * 255.0f) << 16 |
942 int(rgb[1] * 255.0f) << 8 |
943 int(rgb[0] * 255.0f);
944}
945
946static void SC_hsb(float h, float s, float b, float a)
947{
948 float rgb[3];
949 SC_hsbToRgb(h, s, b, rgb);
950 glColor4f(rgb[0], rgb[1], rgb[2], a);
Romain Guya32d1002009-07-31 15:33:59 -0700951}
952
Jason Samsb0ec1b42009-07-28 12:02:16 -0700953static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samsc97bb882009-07-20 14:31:06 -0700954{
955 GET_TLS();
956 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
957}
958
Jason Sams1bada8c2009-08-09 17:01:55 -0700959static void SC_uploadToBufferObject(RsAllocation va)
960{
961 GET_TLS();
962 rsi_AllocationUploadToBufferObject(rsc, va);
963}
964
Jason Samsc97bb882009-07-20 14:31:06 -0700965static void SC_ClearColor(float r, float g, float b, float a)
966{
967 //LOGE("c %f %f %f %f", r, g, b, a);
968 GET_TLS();
969 sc->mEnviroment.mClearColor[0] = r;
970 sc->mEnviroment.mClearColor[1] = g;
971 sc->mEnviroment.mClearColor[2] = b;
972 sc->mEnviroment.mClearColor[3] = a;
973}
974
Jason Samsb0ec1b42009-07-28 12:02:16 -0700975static void SC_debugF(const char *s, float f)
976{
977 LOGE("%s %f", s, f);
978}
979
Romain Guyd22fff72009-08-20 17:08:33 -0700980static void SC_debugHexF(const char *s, float f)
981{
982 LOGE("%s 0x%x", s, *((int *) (&f)));
983}
984
Jason Samsb0ec1b42009-07-28 12:02:16 -0700985static void SC_debugI32(const char *s, int32_t i)
986{
987 LOGE("%s %i", s, i);
988}
989
Romain Guyd22fff72009-08-20 17:08:33 -0700990static void SC_debugHexI32(const char *s, int32_t i)
991{
992 LOGE("%s 0x%x", s, i);
993}
994
Jason Sams40a29e82009-08-10 14:55:26 -0700995static uint32_t SC_getWidth()
996{
997 GET_TLS();
998 return rsc->getWidth();
999}
Jason Samsc97bb882009-07-20 14:31:06 -07001000
Jason Sams40a29e82009-08-10 14:55:26 -07001001static uint32_t SC_getHeight()
1002{
1003 GET_TLS();
1004 return rsc->getHeight();
1005}
Jason Samsc97bb882009-07-20 14:31:06 -07001006
Jason Sams334ea0c2009-08-17 13:56:09 -07001007static uint32_t SC_colorFloatRGBAtoUNorm8(float r, float g, float b, float a)
1008{
1009 uint32_t c = 0;
1010 c |= (uint32_t)(r * 255.f + 0.5f);
1011 c |= ((uint32_t)(g * 255.f + 0.5f)) << 8;
1012 c |= ((uint32_t)(b * 255.f + 0.5f)) << 16;
1013 c |= ((uint32_t)(a * 255.f + 0.5f)) << 24;
1014 return c;
1015}
1016
1017static uint32_t SC_colorFloatRGBAto565(float r, float g, float b)
1018{
1019 uint32_t ir = (uint32_t)(r * 255.f + 0.5f);
1020 uint32_t ig = (uint32_t)(g * 255.f + 0.5f);
1021 uint32_t ib = (uint32_t)(b * 255.f + 0.5f);
1022 return rs888to565(ir, ig, ib);
1023}
1024
Jason Sams516c3192009-10-06 13:58:47 -07001025static uint32_t SC_toClient(void *data, int cmdID, int len, int waitForSpace)
1026{
1027 GET_TLS();
1028 return rsc->sendMessageToClient(data, cmdID, len, waitForSpace != 0);
1029}
1030
Jason Samsbd2197f2009-10-07 18:14:01 -07001031static void SC_scriptCall(int scriptID)
1032{
1033 GET_TLS();
1034 rsc->runScript((Script *)scriptID, 0);
1035}
1036
1037
Jason Samsc97bb882009-07-20 14:31:06 -07001038//////////////////////////////////////////////////////////////////////////////
1039// Class implementation
1040//////////////////////////////////////////////////////////////////////////////
1041
1042ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
1043 // IO
1044 { "loadI32", (void *)&SC_loadI32,
1045 "int", "(int, int)" },
1046 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
1047 { "loadF", (void *)&SC_loadF,
1048 "float", "(int, int)" },
Romain Guya2136d62009-08-04 17:19:48 -07001049 { "loadArrayF", (void *)&SC_loadArrayF,
Romain Guyf8e136d2009-08-06 12:40:41 -07001050 "float*", "(int, int)" },
Romain Guya2136d62009-08-04 17:19:48 -07001051 { "loadArrayI32", (void *)&SC_loadArrayI32,
Romain Guyf8e136d2009-08-06 12:40:41 -07001052 "int*", "(int, int)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001053 { "loadVec4", (void *)&SC_loadVec4,
1054 "void", "(int, int, float *)" },
1055 { "loadMatrix", (void *)&SC_loadMatrix,
1056 "void", "(int, int, float *)" },
1057 { "storeI32", (void *)&SC_storeI32,
1058 "void", "(int, int, int)" },
1059 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
1060 { "storeF", (void *)&SC_storeF,
1061 "void", "(int, int, float)" },
1062 { "storeVec4", (void *)&SC_storeVec4,
1063 "void", "(int, int, float *)" },
1064 { "storeMatrix", (void *)&SC_storeMatrix,
1065 "void", "(int, int, float *)" },
Jason Sams6b9dec02009-09-23 16:38:37 -07001066 { "loadSimpleMeshVerticesF", (void *)&SC_loadSimpleMeshVerticesF,
1067 "float*", "(int, int)" },
1068 { "updateSimpleMesh", (void *)&SC_updateSimpleMesh,
Romain Guyb62627e2009-08-06 22:52:13 -07001069 "void", "(int)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001070
1071 // math
Romain Guya9d2d5e2009-08-09 17:04:54 -07001072 { "modf", (void *)&fmod,
1073 "float", "(float, float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -07001074 { "abs", (void *)&abs,
1075 "int", "(int)" },
Romain Guybd5b5722009-09-29 13:17:27 -07001076 { "absf", (void *)&fabsf,
Romain Guy8f5c94b2009-08-08 18:30:19 -07001077 "float", "(float)" },
Romain Guycac80a62009-08-18 11:39:17 -07001078 { "sinf_fast", (void *)&SC_sinf_fast,
1079 "float", "(float)" },
1080 { "cosf_fast", (void *)&SC_cosf_fast,
1081 "float", "(float)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001082 { "sinf", (void *)&sinf,
1083 "float", "(float)" },
1084 { "cosf", (void *)&cosf,
1085 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001086 { "asinf", (void *)&asinf,
1087 "float", "(float)" },
1088 { "acosf", (void *)&acosf,
1089 "float", "(float)" },
1090 { "atanf", (void *)&atanf,
1091 "float", "(float)" },
1092 { "atan2f", (void *)&atan2f,
Romain Guya32d1002009-07-31 15:33:59 -07001093 "float", "(float, float)" },
Jason Sams6f5c61c2009-07-28 17:20:11 -07001094 { "fabsf", (void *)&fabsf,
Jason Samsc97bb882009-07-20 14:31:06 -07001095 "float", "(float)" },
1096 { "randf", (void *)&SC_randf,
1097 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001098 { "randf2", (void *)&SC_randf2,
1099 "float", "(float, float)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001100 { "floorf", (void *)&floorf,
1101 "float", "(float)" },
Jason Samsd342fd72009-09-18 14:24:24 -07001102 { "fracf", (void *)&SC_fracf,
1103 "float", "(float)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001104 { "ceilf", (void *)&ceilf,
1105 "float", "(float)" },
Jason Samsd342fd72009-09-18 14:24:24 -07001106 { "roundf", (void *)&SC_roundf,
1107 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001108 { "expf", (void *)&expf,
1109 "float", "(float)" },
1110 { "logf", (void *)&logf,
1111 "float", "(float)" },
1112 { "powf", (void *)&powf,
1113 "float", "(float, float)" },
1114 { "maxf", (void *)&SC_maxf,
1115 "float", "(float, float)" },
1116 { "minf", (void *)&SC_minf,
1117 "float", "(float, float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -07001118 { "sqrt", (void *)&sqrt,
1119 "int", "(int)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001120 { "sqrtf", (void *)&sqrtf,
1121 "float", "(float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -07001122 { "sqr", (void *)&SC_sqr,
1123 "int", "(int)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001124 { "sqrf", (void *)&SC_sqrf,
1125 "float", "(float)" },
Romain Guyd7fa1222009-10-09 16:05:25 -07001126 { "sign", (void *)&SC_sign,
1127 "int", "(int)" },
1128 { "signf", (void *)&SC_signf,
1129 "float", "(float)" },
Romain Guya9d2d5e2009-08-09 17:04:54 -07001130 { "clamp", (void *)&SC_clamp,
1131 "int", "(int, int, int)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001132 { "clampf", (void *)&SC_clampf,
1133 "float", "(float, float, float)" },
1134 { "distf2", (void *)&SC_distf2,
1135 "float", "(float, float, float, float)" },
1136 { "distf3", (void *)&SC_distf3,
1137 "float", "(float, float, float, float, float, float)" },
1138 { "magf2", (void *)&SC_magf2,
1139 "float", "(float, float)" },
1140 { "magf3", (void *)&SC_magf3,
1141 "float", "(float, float, float)" },
1142 { "radf", (void *)&SC_radf,
1143 "float", "(float)" },
1144 { "degf", (void *)&SC_degf,
1145 "float", "(float)" },
1146 { "lerpf", (void *)&SC_lerpf,
1147 "float", "(float, float, float)" },
1148 { "normf", (void *)&SC_normf,
1149 "float", "(float, float, float)" },
1150 { "mapf", (void *)&SC_mapf,
1151 "float", "(float, float, float, float, float)" },
Romain Guyecc7ca032009-08-03 21:12:51 -07001152 { "noisef", (void *)&SC_noisef,
1153 "float", "(float)" },
1154 { "noisef2", (void *)&SC_noisef2,
1155 "float", "(float, float)" },
1156 { "noisef3", (void *)&SC_noisef3,
1157 "float", "(float, float, float)" },
1158 { "turbulencef2", (void *)&SC_turbulencef2,
1159 "float", "(float, float, float)" },
1160 { "turbulencef3", (void *)&SC_turbulencef3,
1161 "float", "(float, float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001162
Romain Guy584a3752009-07-30 18:45:01 -07001163 // time
1164 { "second", (void *)&SC_second,
1165 "int", "()" },
1166 { "minute", (void *)&SC_minute,
1167 "int", "()" },
1168 { "hour", (void *)&SC_hour,
1169 "int", "()" },
Romain Guy8839ca52009-07-31 11:20:59 -07001170 { "day", (void *)&SC_day,
1171 "int", "()" },
1172 { "month", (void *)&SC_month,
1173 "int", "()" },
1174 { "year", (void *)&SC_year,
1175 "int", "()" },
Joe Onorato3370ec92009-08-09 11:39:02 -07001176 { "uptimeMillis", (void*)&SC_uptimeMillis,
1177 "int", "()" }, // TODO: use long instead
1178 { "startTimeMillis", (void*)&SC_startTimeMillis,
1179 "int", "()" }, // TODO: use long instead
1180 { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis,
1181 "int", "()" }, // TODO: use long instead
Romain Guy584a3752009-07-30 18:45:01 -07001182
Jason Samsc97bb882009-07-20 14:31:06 -07001183 // matrix
1184 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
1185 "void", "(float *mat)" },
1186 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
1187 "void", "(float *mat, float *f)" },
1188 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
1189 "void", "(float *mat, float *newmat)" },
1190 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
1191 "void", "(float *mat, float rot, float x, float y, float z)" },
1192 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
1193 "void", "(float *mat, float x, float y, float z)" },
1194 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
1195 "void", "(float *mat, float x, float y, float z)" },
1196 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
1197 "void", "(float *mat, float *lhs, float *rhs)" },
1198 { "matrixMultiply", (void *)&SC_matrixMultiply,
1199 "void", "(float *mat, float *rhs)" },
1200 { "matrixRotate", (void *)&SC_matrixRotate,
1201 "void", "(float *mat, float rot, float x, float y, float z)" },
1202 { "matrixScale", (void *)&SC_matrixScale,
1203 "void", "(float *mat, float x, float y, float z)" },
1204 { "matrixTranslate", (void *)&SC_matrixTranslate,
1205 "void", "(float *mat, float x, float y, float z)" },
1206
Jason Sams334ea0c2009-08-17 13:56:09 -07001207 // vector
1208 { "vec2Rand", (void *)&SC_vec2Rand,
1209 "void", "(float *vec, float maxLen)" },
1210
Jason Samsea84a7c2009-09-04 14:42:41 -07001211 // vec3
1212 { "vec3Norm", (void *)&SC_vec3Norm,
1213 "void", "(struct vec3_s *)" },
1214 { "vec3Length", (void *)&SC_vec3Length,
1215 "float", "(struct vec3_s *)" },
1216 { "vec3Add", (void *)&SC_vec3Add,
1217 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1218 { "vec3Sub", (void *)&SC_vec3Sub,
1219 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1220 { "vec3Cross", (void *)&SC_vec3Cross,
1221 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1222 { "vec3Dot", (void *)&SC_vec3Dot,
1223 "float", "(struct vec3_s *lhs, struct vec3_s *rhs)" },
1224 { "vec3Scale", (void *)&SC_vec3Scale,
1225 "void", "(struct vec3_s *lhs, float scale)" },
1226
Romain Guyd7fa1222009-10-09 16:05:25 -07001227 // vec4
1228 { "vec4Norm", (void *)&SC_vec4Norm,
1229 "void", "(struct vec4_s *)" },
1230 { "vec4Length", (void *)&SC_vec4Length,
1231 "float", "(struct vec4_s *)" },
1232 { "vec4Add", (void *)&SC_vec4Add,
1233 "void", "(struct vec4_s *dest, struct vec4_s *lhs, struct vec4_s *rhs)" },
1234 { "vec4Sub", (void *)&SC_vec4Sub,
1235 "void", "(struct vec4_s *dest, struct vec4_s *lhs, struct vec4_s *rhs)" },
1236 { "vec4Dot", (void *)&SC_vec4Dot,
1237 "float", "(struct vec4_s *lhs, struct vec4_s *rhs)" },
1238 { "vec4Scale", (void *)&SC_vec4Scale,
1239 "void", "(struct vec4_s *lhs, float scale)" },
1240
Jason Samsc97bb882009-07-20 14:31:06 -07001241 // context
1242 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
1243 "void", "(int)" },
1244 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
1245 "void", "(int)" },
Jason Sams5235cf32009-09-28 18:12:56 -07001246 { "bindProgramStore", (void *)&SC_bindProgramFragmentStore,
1247 "void", "(int)" },
Jason Samsee411122009-07-21 12:20:54 -07001248 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
1249 "void", "(int)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001250 { "bindSampler", (void *)&SC_bindSampler,
1251 "void", "(int, int, int)" },
1252 { "bindTexture", (void *)&SC_bindTexture,
1253 "void", "(int, int, int)" },
1254
Jason Samsb0ec1b42009-07-28 12:02:16 -07001255 // vp
Jason Samsfaf15202009-07-29 20:55:44 -07001256 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsb0ec1b42009-07-28 12:02:16 -07001257 "void", "(void *)" },
Jason Samsfaf15202009-07-29 20:55:44 -07001258 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsb0ec1b42009-07-28 12:02:16 -07001259 "void", "(void *)" },
1260
1261
1262
Jason Samsc97bb882009-07-20 14:31:06 -07001263 // drawing
Jason Sams6f5c61c2009-07-28 17:20:11 -07001264 { "drawRect", (void *)&SC_drawRect,
1265 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001266 { "drawQuad", (void *)&SC_drawQuad,
1267 "void", "(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -07001268 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords,
1269 "void", "(float x1, float y1, float z1, float u1, float v1, float x2, float y2, float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3, float x4, float y4, float z4, float u4, float v4)" },
Jason Samse9ad9a72009-09-30 17:36:20 -07001270 { "drawSprite", (void *)&SC_drawSprite,
1271 "void", "(float x, float y, float z, float w, float h)" },
1272 { "drawSpriteScreenspace", (void *)&SC_drawSpriteScreenspace,
1273 "void", "(float x, float y, float z, float w, float h)" },
Romain Guy6c0cc6d2009-08-07 15:40:32 -07001274 { "drawLine", (void *)&SC_drawLine,
1275 "void", "(float x1, float y1, float z1, float x2, float y2, float z2)" },
Jason Sams5235cf32009-09-28 18:12:56 -07001276 { "drawPoint", (void *)&SC_drawPoint,
1277 "void", "(float x1, float y1, float z1)" },
Jason Sams1bada8c2009-08-09 17:01:55 -07001278 { "drawSimpleMesh", (void *)&SC_drawSimpleMesh,
1279 "void", "(int ism)" },
1280 { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange,
1281 "void", "(int ism, int start, int len)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001282
1283
1284 // misc
1285 { "pfClearColor", (void *)&SC_ClearColor,
1286 "void", "(float, float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001287 { "color", (void *)&SC_color,
1288 "void", "(float, float, float, float)" },
Romain Guya32d1002009-07-31 15:33:59 -07001289 { "hsb", (void *)&SC_hsb,
1290 "void", "(float, float, float, float)" },
Romain Guyd22fff72009-08-20 17:08:33 -07001291 { "hsbToRgb", (void *)&SC_hsbToRgb,
1292 "void", "(float, float, float, float*)" },
1293 { "hsbToAbgr", (void *)&SC_hsbToAbgr,
1294 "int", "(float, float, float, float)" },
Romain Guyb62627e2009-08-06 22:52:13 -07001295 { "ambient", (void *)&SC_ambient,
1296 "void", "(float, float, float, float)" },
1297 { "diffuse", (void *)&SC_diffuse,
1298 "void", "(float, float, float, float)" },
1299 { "specular", (void *)&SC_specular,
1300 "void", "(float, float, float, float)" },
1301 { "emission", (void *)&SC_emission,
1302 "void", "(float, float, float, float)" },
1303 { "shininess", (void *)&SC_shininess,
Romain Guy6c0cc6d2009-08-07 15:40:32 -07001304 "void", "(float)" },
Romain Guy2d496bf2009-09-04 17:55:41 -07001305 { "pointAttenuation", (void *)&SC_pointAttenuation,
1306 "void", "(float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001307
Jason Samsb0ec1b42009-07-28 12:02:16 -07001308 { "uploadToTexture", (void *)&SC_uploadToTexture,
1309 "void", "(int, int)" },
Jason Sams1bada8c2009-08-09 17:01:55 -07001310 { "uploadToBufferObject", (void *)&SC_uploadToBufferObject,
1311 "void", "(int)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001312
Jason Sams334ea0c2009-08-17 13:56:09 -07001313 { "colorFloatRGBAtoUNorm8", (void *)&SC_colorFloatRGBAtoUNorm8,
1314 "int", "(float, float, float, float)" },
1315 { "colorFloatRGBto565", (void *)&SC_colorFloatRGBAto565,
1316 "int", "(float, float, float)" },
1317
1318
Jason Sams40a29e82009-08-10 14:55:26 -07001319 { "getWidth", (void *)&SC_getWidth,
1320 "int", "()" },
1321 { "getHeight", (void *)&SC_getHeight,
1322 "int", "()" },
1323
Jason Sams516c3192009-10-06 13:58:47 -07001324 { "sendToClient", (void *)&SC_toClient,
1325 "int", "(void *data, int cmdID, int len, int waitForSpace)" },
Jason Sams40a29e82009-08-10 14:55:26 -07001326
Jason Samsb0ec1b42009-07-28 12:02:16 -07001327
1328 { "debugF", (void *)&SC_debugF,
1329 "void", "(void *, float)" },
1330 { "debugI32", (void *)&SC_debugI32,
1331 "void", "(void *, int)" },
Romain Guyd22fff72009-08-20 17:08:33 -07001332 { "debugHexF", (void *)&SC_debugHexF,
1333 "void", "(void *, float)" },
1334 { "debugHexI32", (void *)&SC_debugHexI32,
1335 "void", "(void *, int)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001336
Jason Samsbd2197f2009-10-07 18:14:01 -07001337 { "scriptCall", (void *)&SC_scriptCall,
1338 "void", "(int)" },
1339
Jason Samsb0ec1b42009-07-28 12:02:16 -07001340
Jason Samsc97bb882009-07-20 14:31:06 -07001341 { NULL, NULL, NULL, NULL }
1342};
1343
1344const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
1345{
1346 ScriptCState::SymbolTable_t *syms = gSyms;
1347
1348 while (syms->mPtr) {
1349 if (!strcmp(syms->mName, sym)) {
1350 return syms;
1351 }
1352 syms++;
1353 }
1354 return NULL;
1355}
1356
1357void ScriptCState::appendDecls(String8 *str)
1358{
1359 ScriptCState::SymbolTable_t *syms = gSyms;
1360 while (syms->mPtr) {
1361 str->append(syms->mRet);
1362 str->append(" ");
1363 str->append(syms->mName);
1364 str->append(syms->mParam);
1365 str->append(";\n");
1366 syms++;
1367 }
1368}
1369
1370