blob: de8df39944d4003436c1c7184955fab1d2833f8c [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 Guyecc7ca02009-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
Joe Onorato6656c1b2010-01-14 15:59:35 -0500779static void SC_drawSpriteScreenspaceCropped(float x, float y, float z, float w, float h,
780 float cx0, float cy0, float cx1, float cy1)
781{
782 GET_TLS();
783 rsc->setupCheck();
784
785 GLint crop[4] = {cx0, cy0, cx1, cy1};
786 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
787 glDrawTexfOES(x, y, z, w, h);
788}
789
Jason Samse9ad9a72009-09-30 17:36:20 -0700790static void SC_drawSprite(float x, float y, float z, float w, float h)
791{
792 GET_TLS();
793 rsc->setupCheck();
794
795 float vin[3] = {x, y, z};
796 float vout[4];
797
798 //LOGE("ds in %f %f %f", x, y, z);
799 rsc->getVertex()->transformToScreen(rsc, vout, vin);
800 //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]);
801 vout[0] /= vout[3];
802 vout[1] /= vout[3];
803 vout[2] /= vout[3];
804
805 vout[0] *= rsc->getWidth() / 2;
806 vout[1] *= rsc->getHeight() / 2;
807 vout[0] += rsc->getWidth() / 2;
808 vout[1] += rsc->getHeight() / 2;
809
810 vout[0] -= w/2;
811 vout[1] -= h/2;
812
813 //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]);
814
815 // U, V, W, H
816 GLint crop[4] = {0, h, w, -h};
817 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
818 glDrawTexiOES(vout[0], vout[1], 0/*vout[2]*/, w, h);
819}
820
821
Jason Sams6f5c61c2009-07-28 17:20:11 -0700822static void SC_drawRect(float x1, float y1,
823 float x2, float y2, float z)
824{
825 SC_drawQuad(x1, y2, z,
826 x2, y2, z,
827 x2, y1, z,
828 x1, y1, z);
829}
830
Jason Sams1bada8c2009-08-09 17:01:55 -0700831static void SC_drawSimpleMesh(RsSimpleMesh vsm)
832{
833 GET_TLS();
834 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
835 rsc->setupCheck();
836 sm->render();
837}
838
839static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len)
840{
841 GET_TLS();
842 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
843 rsc->setupCheck();
844 sm->renderRange(start, len);
845}
846
847
Jason Samsc97bb882009-07-20 14:31:06 -0700848//////////////////////////////////////////////////////////////////////////////
849//
850//////////////////////////////////////////////////////////////////////////////
851
Jason Samsc97bb882009-07-20 14:31:06 -0700852static void SC_color(float r, float g, float b, float a)
853{
854 glColor4f(r, g, b, a);
855}
856
Romain Guyb62627e2009-08-06 22:52:13 -0700857static void SC_ambient(float r, float g, float b, float a)
858{
859 GLfloat params[] = { r, g, b, a };
860 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, params);
861}
862
863static void SC_diffuse(float r, float g, float b, float a)
864{
865 GLfloat params[] = { r, g, b, a };
866 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, params);
867}
868
869static void SC_specular(float r, float g, float b, float a)
870{
871 GLfloat params[] = { r, g, b, a };
872 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, params);
873}
874
875static void SC_emission(float r, float g, float b, float a)
876{
877 GLfloat params[] = { r, g, b, a };
878 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, params);
879}
880
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700881static void SC_shininess(float s)
Romain Guyb62627e2009-08-06 22:52:13 -0700882{
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700883 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, s);
Romain Guyb62627e2009-08-06 22:52:13 -0700884}
885
Romain Guy2d496bf2009-09-04 17:55:41 -0700886static void SC_pointAttenuation(float a, float b, float c)
887{
888 GLfloat params[] = { a, b, c };
889 glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, params);
890}
891
Romain Guyd22fff72009-08-20 17:08:33 -0700892static void SC_hsbToRgb(float h, float s, float b, float* rgb)
Romain Guya32d1002009-07-31 15:33:59 -0700893{
894 float red = 0.0f;
895 float green = 0.0f;
896 float blue = 0.0f;
Jason Sams1bada8c2009-08-09 17:01:55 -0700897
Romain Guya32d1002009-07-31 15:33:59 -0700898 float x = h;
899 float y = s;
900 float z = b;
Jason Sams1bada8c2009-08-09 17:01:55 -0700901
Romain Guya32d1002009-07-31 15:33:59 -0700902 float hf = (x - (int) x) * 6.0f;
903 int ihf = (int) hf;
904 float f = hf - ihf;
905 float pv = z * (1.0f - y);
906 float qv = z * (1.0f - y * f);
907 float tv = z * (1.0f - y * (1.0f - f));
Jason Sams1bada8c2009-08-09 17:01:55 -0700908
Romain Guya32d1002009-07-31 15:33:59 -0700909 switch (ihf) {
910 case 0: // Red is the dominant color
911 red = z;
912 green = tv;
913 blue = pv;
914 break;
915 case 1: // Green is the dominant color
916 red = qv;
917 green = z;
918 blue = pv;
919 break;
920 case 2:
921 red = pv;
922 green = z;
923 blue = tv;
924 break;
925 case 3: // Blue is the dominant color
926 red = pv;
927 green = qv;
928 blue = z;
929 break;
930 case 4:
931 red = tv;
932 green = pv;
933 blue = z;
934 break;
935 case 5: // Red is the dominant color
936 red = z;
937 green = pv;
938 blue = qv;
939 break;
940 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700941
Romain Guyd22fff72009-08-20 17:08:33 -0700942 rgb[0] = red;
943 rgb[1] = green;
944 rgb[2] = blue;
945}
946
947static int SC_hsbToAbgr(float h, float s, float b, float a)
948{
949 float rgb[3];
950 SC_hsbToRgb(h, s, b, rgb);
951 return int(a * 255.0f) << 24 |
952 int(rgb[2] * 255.0f) << 16 |
953 int(rgb[1] * 255.0f) << 8 |
954 int(rgb[0] * 255.0f);
955}
956
957static void SC_hsb(float h, float s, float b, float a)
958{
959 float rgb[3];
960 SC_hsbToRgb(h, s, b, rgb);
961 glColor4f(rgb[0], rgb[1], rgb[2], a);
Romain Guya32d1002009-07-31 15:33:59 -0700962}
963
Jason Samsb0ec1b42009-07-28 12:02:16 -0700964static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samsc97bb882009-07-20 14:31:06 -0700965{
966 GET_TLS();
967 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
968}
969
Jason Sams1bada8c2009-08-09 17:01:55 -0700970static void SC_uploadToBufferObject(RsAllocation va)
971{
972 GET_TLS();
973 rsi_AllocationUploadToBufferObject(rsc, va);
974}
975
Jason Samsc97bb882009-07-20 14:31:06 -0700976static void SC_ClearColor(float r, float g, float b, float a)
977{
978 //LOGE("c %f %f %f %f", r, g, b, a);
979 GET_TLS();
980 sc->mEnviroment.mClearColor[0] = r;
981 sc->mEnviroment.mClearColor[1] = g;
982 sc->mEnviroment.mClearColor[2] = b;
983 sc->mEnviroment.mClearColor[3] = a;
984}
985
Jason Samsb0ec1b42009-07-28 12:02:16 -0700986static void SC_debugF(const char *s, float f)
987{
988 LOGE("%s %f", s, f);
989}
990
Romain Guyd22fff72009-08-20 17:08:33 -0700991static void SC_debugHexF(const char *s, float f)
992{
993 LOGE("%s 0x%x", s, *((int *) (&f)));
994}
995
Jason Samsb0ec1b42009-07-28 12:02:16 -0700996static void SC_debugI32(const char *s, int32_t i)
997{
998 LOGE("%s %i", s, i);
999}
1000
Romain Guyd22fff72009-08-20 17:08:33 -07001001static void SC_debugHexI32(const char *s, int32_t i)
1002{
1003 LOGE("%s 0x%x", s, i);
1004}
1005
Jason Sams40a29e82009-08-10 14:55:26 -07001006static uint32_t SC_getWidth()
1007{
1008 GET_TLS();
1009 return rsc->getWidth();
1010}
Jason Samsc97bb882009-07-20 14:31:06 -07001011
Jason Sams40a29e82009-08-10 14:55:26 -07001012static uint32_t SC_getHeight()
1013{
1014 GET_TLS();
1015 return rsc->getHeight();
1016}
Jason Samsc97bb882009-07-20 14:31:06 -07001017
Jason Sams334ea0c2009-08-17 13:56:09 -07001018static uint32_t SC_colorFloatRGBAtoUNorm8(float r, float g, float b, float a)
1019{
1020 uint32_t c = 0;
1021 c |= (uint32_t)(r * 255.f + 0.5f);
1022 c |= ((uint32_t)(g * 255.f + 0.5f)) << 8;
1023 c |= ((uint32_t)(b * 255.f + 0.5f)) << 16;
1024 c |= ((uint32_t)(a * 255.f + 0.5f)) << 24;
1025 return c;
1026}
1027
1028static uint32_t SC_colorFloatRGBAto565(float r, float g, float b)
1029{
1030 uint32_t ir = (uint32_t)(r * 255.f + 0.5f);
1031 uint32_t ig = (uint32_t)(g * 255.f + 0.5f);
1032 uint32_t ib = (uint32_t)(b * 255.f + 0.5f);
1033 return rs888to565(ir, ig, ib);
1034}
1035
Jason Sams516c3192009-10-06 13:58:47 -07001036static uint32_t SC_toClient(void *data, int cmdID, int len, int waitForSpace)
1037{
1038 GET_TLS();
1039 return rsc->sendMessageToClient(data, cmdID, len, waitForSpace != 0);
1040}
1041
Jason Samsbd2197f2009-10-07 18:14:01 -07001042static void SC_scriptCall(int scriptID)
1043{
1044 GET_TLS();
1045 rsc->runScript((Script *)scriptID, 0);
1046}
1047
1048
Jason Samsc97bb882009-07-20 14:31:06 -07001049//////////////////////////////////////////////////////////////////////////////
1050// Class implementation
1051//////////////////////////////////////////////////////////////////////////////
1052
1053ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
1054 // IO
1055 { "loadI32", (void *)&SC_loadI32,
1056 "int", "(int, int)" },
1057 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
1058 { "loadF", (void *)&SC_loadF,
1059 "float", "(int, int)" },
Romain Guya2136d62009-08-04 17:19:48 -07001060 { "loadArrayF", (void *)&SC_loadArrayF,
Romain Guyf8e136d2009-08-06 12:40:41 -07001061 "float*", "(int, int)" },
Romain Guya2136d62009-08-04 17:19:48 -07001062 { "loadArrayI32", (void *)&SC_loadArrayI32,
Romain Guyf8e136d2009-08-06 12:40:41 -07001063 "int*", "(int, int)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001064 { "loadVec4", (void *)&SC_loadVec4,
1065 "void", "(int, int, float *)" },
1066 { "loadMatrix", (void *)&SC_loadMatrix,
1067 "void", "(int, int, float *)" },
1068 { "storeI32", (void *)&SC_storeI32,
1069 "void", "(int, int, int)" },
1070 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
1071 { "storeF", (void *)&SC_storeF,
1072 "void", "(int, int, float)" },
1073 { "storeVec4", (void *)&SC_storeVec4,
1074 "void", "(int, int, float *)" },
1075 { "storeMatrix", (void *)&SC_storeMatrix,
1076 "void", "(int, int, float *)" },
Jason Sams6b9dec02009-09-23 16:38:37 -07001077 { "loadSimpleMeshVerticesF", (void *)&SC_loadSimpleMeshVerticesF,
1078 "float*", "(int, int)" },
1079 { "updateSimpleMesh", (void *)&SC_updateSimpleMesh,
Romain Guyb62627e2009-08-06 22:52:13 -07001080 "void", "(int)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001081
1082 // math
Romain Guya9d2d5e2009-08-09 17:04:54 -07001083 { "modf", (void *)&fmod,
1084 "float", "(float, float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -07001085 { "abs", (void *)&abs,
1086 "int", "(int)" },
Romain Guybd5b5722009-09-29 13:17:27 -07001087 { "absf", (void *)&fabsf,
Romain Guy8f5c94b2009-08-08 18:30:19 -07001088 "float", "(float)" },
Romain Guycac80a62009-08-18 11:39:17 -07001089 { "sinf_fast", (void *)&SC_sinf_fast,
1090 "float", "(float)" },
1091 { "cosf_fast", (void *)&SC_cosf_fast,
1092 "float", "(float)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001093 { "sinf", (void *)&sinf,
1094 "float", "(float)" },
1095 { "cosf", (void *)&cosf,
1096 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001097 { "asinf", (void *)&asinf,
1098 "float", "(float)" },
1099 { "acosf", (void *)&acosf,
1100 "float", "(float)" },
1101 { "atanf", (void *)&atanf,
1102 "float", "(float)" },
1103 { "atan2f", (void *)&atan2f,
Romain Guya32d1002009-07-31 15:33:59 -07001104 "float", "(float, float)" },
Jason Sams6f5c61c2009-07-28 17:20:11 -07001105 { "fabsf", (void *)&fabsf,
Jason Samsc97bb882009-07-20 14:31:06 -07001106 "float", "(float)" },
1107 { "randf", (void *)&SC_randf,
1108 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001109 { "randf2", (void *)&SC_randf2,
1110 "float", "(float, float)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001111 { "floorf", (void *)&floorf,
1112 "float", "(float)" },
Jason Samsd342fd72009-09-18 14:24:24 -07001113 { "fracf", (void *)&SC_fracf,
1114 "float", "(float)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001115 { "ceilf", (void *)&ceilf,
1116 "float", "(float)" },
Jason Samsd342fd72009-09-18 14:24:24 -07001117 { "roundf", (void *)&SC_roundf,
1118 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001119 { "expf", (void *)&expf,
1120 "float", "(float)" },
1121 { "logf", (void *)&logf,
1122 "float", "(float)" },
1123 { "powf", (void *)&powf,
1124 "float", "(float, float)" },
1125 { "maxf", (void *)&SC_maxf,
1126 "float", "(float, float)" },
1127 { "minf", (void *)&SC_minf,
1128 "float", "(float, float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -07001129 { "sqrt", (void *)&sqrt,
1130 "int", "(int)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001131 { "sqrtf", (void *)&sqrtf,
1132 "float", "(float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -07001133 { "sqr", (void *)&SC_sqr,
1134 "int", "(int)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001135 { "sqrf", (void *)&SC_sqrf,
1136 "float", "(float)" },
Romain Guyd7fa1222009-10-09 16:05:25 -07001137 { "sign", (void *)&SC_sign,
1138 "int", "(int)" },
1139 { "signf", (void *)&SC_signf,
1140 "float", "(float)" },
Romain Guya9d2d5e2009-08-09 17:04:54 -07001141 { "clamp", (void *)&SC_clamp,
1142 "int", "(int, int, int)" },
Romain Guy8839ca52009-07-31 11:20:59 -07001143 { "clampf", (void *)&SC_clampf,
1144 "float", "(float, float, float)" },
1145 { "distf2", (void *)&SC_distf2,
1146 "float", "(float, float, float, float)" },
1147 { "distf3", (void *)&SC_distf3,
1148 "float", "(float, float, float, float, float, float)" },
1149 { "magf2", (void *)&SC_magf2,
1150 "float", "(float, float)" },
1151 { "magf3", (void *)&SC_magf3,
1152 "float", "(float, float, float)" },
1153 { "radf", (void *)&SC_radf,
1154 "float", "(float)" },
1155 { "degf", (void *)&SC_degf,
1156 "float", "(float)" },
1157 { "lerpf", (void *)&SC_lerpf,
1158 "float", "(float, float, float)" },
1159 { "normf", (void *)&SC_normf,
1160 "float", "(float, float, float)" },
1161 { "mapf", (void *)&SC_mapf,
1162 "float", "(float, float, float, float, float)" },
Romain Guyecc7ca02009-08-03 21:12:51 -07001163 { "noisef", (void *)&SC_noisef,
1164 "float", "(float)" },
1165 { "noisef2", (void *)&SC_noisef2,
1166 "float", "(float, float)" },
1167 { "noisef3", (void *)&SC_noisef3,
1168 "float", "(float, float, float)" },
1169 { "turbulencef2", (void *)&SC_turbulencef2,
1170 "float", "(float, float, float)" },
1171 { "turbulencef3", (void *)&SC_turbulencef3,
1172 "float", "(float, float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001173
Romain Guy584a3752009-07-30 18:45:01 -07001174 // time
1175 { "second", (void *)&SC_second,
1176 "int", "()" },
1177 { "minute", (void *)&SC_minute,
1178 "int", "()" },
1179 { "hour", (void *)&SC_hour,
1180 "int", "()" },
Romain Guy8839ca52009-07-31 11:20:59 -07001181 { "day", (void *)&SC_day,
1182 "int", "()" },
1183 { "month", (void *)&SC_month,
1184 "int", "()" },
1185 { "year", (void *)&SC_year,
1186 "int", "()" },
Joe Onorato3370ec92009-08-09 11:39:02 -07001187 { "uptimeMillis", (void*)&SC_uptimeMillis,
1188 "int", "()" }, // TODO: use long instead
1189 { "startTimeMillis", (void*)&SC_startTimeMillis,
1190 "int", "()" }, // TODO: use long instead
1191 { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis,
1192 "int", "()" }, // TODO: use long instead
Romain Guy584a3752009-07-30 18:45:01 -07001193
Jason Samsc97bb882009-07-20 14:31:06 -07001194 // matrix
1195 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
1196 "void", "(float *mat)" },
1197 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
1198 "void", "(float *mat, float *f)" },
1199 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
1200 "void", "(float *mat, float *newmat)" },
1201 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
1202 "void", "(float *mat, float rot, float x, float y, float z)" },
1203 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
1204 "void", "(float *mat, float x, float y, float z)" },
1205 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
1206 "void", "(float *mat, float x, float y, float z)" },
1207 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
1208 "void", "(float *mat, float *lhs, float *rhs)" },
1209 { "matrixMultiply", (void *)&SC_matrixMultiply,
1210 "void", "(float *mat, float *rhs)" },
1211 { "matrixRotate", (void *)&SC_matrixRotate,
1212 "void", "(float *mat, float rot, float x, float y, float z)" },
1213 { "matrixScale", (void *)&SC_matrixScale,
1214 "void", "(float *mat, float x, float y, float z)" },
1215 { "matrixTranslate", (void *)&SC_matrixTranslate,
1216 "void", "(float *mat, float x, float y, float z)" },
1217
Jason Sams334ea0c2009-08-17 13:56:09 -07001218 // vector
1219 { "vec2Rand", (void *)&SC_vec2Rand,
1220 "void", "(float *vec, float maxLen)" },
1221
Jason Samsea84a7c2009-09-04 14:42:41 -07001222 // vec3
1223 { "vec3Norm", (void *)&SC_vec3Norm,
1224 "void", "(struct vec3_s *)" },
1225 { "vec3Length", (void *)&SC_vec3Length,
1226 "float", "(struct vec3_s *)" },
1227 { "vec3Add", (void *)&SC_vec3Add,
1228 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1229 { "vec3Sub", (void *)&SC_vec3Sub,
1230 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1231 { "vec3Cross", (void *)&SC_vec3Cross,
1232 "void", "(struct vec3_s *dest, struct vec3_s *lhs, struct vec3_s *rhs)" },
1233 { "vec3Dot", (void *)&SC_vec3Dot,
1234 "float", "(struct vec3_s *lhs, struct vec3_s *rhs)" },
1235 { "vec3Scale", (void *)&SC_vec3Scale,
1236 "void", "(struct vec3_s *lhs, float scale)" },
1237
Romain Guyd7fa1222009-10-09 16:05:25 -07001238 // vec4
1239 { "vec4Norm", (void *)&SC_vec4Norm,
1240 "void", "(struct vec4_s *)" },
1241 { "vec4Length", (void *)&SC_vec4Length,
1242 "float", "(struct vec4_s *)" },
1243 { "vec4Add", (void *)&SC_vec4Add,
1244 "void", "(struct vec4_s *dest, struct vec4_s *lhs, struct vec4_s *rhs)" },
1245 { "vec4Sub", (void *)&SC_vec4Sub,
1246 "void", "(struct vec4_s *dest, struct vec4_s *lhs, struct vec4_s *rhs)" },
1247 { "vec4Dot", (void *)&SC_vec4Dot,
1248 "float", "(struct vec4_s *lhs, struct vec4_s *rhs)" },
1249 { "vec4Scale", (void *)&SC_vec4Scale,
1250 "void", "(struct vec4_s *lhs, float scale)" },
1251
Jason Samsc97bb882009-07-20 14:31:06 -07001252 // context
1253 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
1254 "void", "(int)" },
1255 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
1256 "void", "(int)" },
Jason Sams5235cf32009-09-28 18:12:56 -07001257 { "bindProgramStore", (void *)&SC_bindProgramFragmentStore,
1258 "void", "(int)" },
Jason Samsee411122009-07-21 12:20:54 -07001259 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
1260 "void", "(int)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001261 { "bindSampler", (void *)&SC_bindSampler,
1262 "void", "(int, int, int)" },
1263 { "bindTexture", (void *)&SC_bindTexture,
1264 "void", "(int, int, int)" },
1265
Jason Samsb0ec1b42009-07-28 12:02:16 -07001266 // vp
Jason Samsfaf15202009-07-29 20:55:44 -07001267 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsb0ec1b42009-07-28 12:02:16 -07001268 "void", "(void *)" },
Jason Samsfaf15202009-07-29 20:55:44 -07001269 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsb0ec1b42009-07-28 12:02:16 -07001270 "void", "(void *)" },
1271
1272
1273
Jason Samsc97bb882009-07-20 14:31:06 -07001274 // drawing
Jason Sams6f5c61c2009-07-28 17:20:11 -07001275 { "drawRect", (void *)&SC_drawRect,
1276 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001277 { "drawQuad", (void *)&SC_drawQuad,
1278 "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 -07001279 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords,
1280 "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 -07001281 { "drawSprite", (void *)&SC_drawSprite,
1282 "void", "(float x, float y, float z, float w, float h)" },
1283 { "drawSpriteScreenspace", (void *)&SC_drawSpriteScreenspace,
1284 "void", "(float x, float y, float z, float w, float h)" },
Joe Onorato6656c1b2010-01-14 15:59:35 -05001285 { "drawSpriteScreenspaceCropped", (void *)&SC_drawSpriteScreenspaceCropped,
1286 "void", "(float x, float y, float z, float w, float h, float cx0, float cy0, float cx1, float cy1)" },
Romain Guy6c0cc6d2009-08-07 15:40:32 -07001287 { "drawLine", (void *)&SC_drawLine,
1288 "void", "(float x1, float y1, float z1, float x2, float y2, float z2)" },
Jason Sams5235cf32009-09-28 18:12:56 -07001289 { "drawPoint", (void *)&SC_drawPoint,
1290 "void", "(float x1, float y1, float z1)" },
Jason Sams1bada8c2009-08-09 17:01:55 -07001291 { "drawSimpleMesh", (void *)&SC_drawSimpleMesh,
1292 "void", "(int ism)" },
1293 { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange,
1294 "void", "(int ism, int start, int len)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001295
1296
1297 // misc
1298 { "pfClearColor", (void *)&SC_ClearColor,
1299 "void", "(float, float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001300 { "color", (void *)&SC_color,
1301 "void", "(float, float, float, float)" },
Romain Guya32d1002009-07-31 15:33:59 -07001302 { "hsb", (void *)&SC_hsb,
1303 "void", "(float, float, float, float)" },
Romain Guyd22fff72009-08-20 17:08:33 -07001304 { "hsbToRgb", (void *)&SC_hsbToRgb,
1305 "void", "(float, float, float, float*)" },
1306 { "hsbToAbgr", (void *)&SC_hsbToAbgr,
1307 "int", "(float, float, float, float)" },
Romain Guyb62627e2009-08-06 22:52:13 -07001308 { "ambient", (void *)&SC_ambient,
1309 "void", "(float, float, float, float)" },
1310 { "diffuse", (void *)&SC_diffuse,
1311 "void", "(float, float, float, float)" },
1312 { "specular", (void *)&SC_specular,
1313 "void", "(float, float, float, float)" },
1314 { "emission", (void *)&SC_emission,
1315 "void", "(float, float, float, float)" },
1316 { "shininess", (void *)&SC_shininess,
Romain Guy6c0cc6d2009-08-07 15:40:32 -07001317 "void", "(float)" },
Romain Guy2d496bf2009-09-04 17:55:41 -07001318 { "pointAttenuation", (void *)&SC_pointAttenuation,
1319 "void", "(float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -07001320
Jason Samsb0ec1b42009-07-28 12:02:16 -07001321 { "uploadToTexture", (void *)&SC_uploadToTexture,
1322 "void", "(int, int)" },
Jason Sams1bada8c2009-08-09 17:01:55 -07001323 { "uploadToBufferObject", (void *)&SC_uploadToBufferObject,
1324 "void", "(int)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001325
Jason Sams334ea0c2009-08-17 13:56:09 -07001326 { "colorFloatRGBAtoUNorm8", (void *)&SC_colorFloatRGBAtoUNorm8,
1327 "int", "(float, float, float, float)" },
1328 { "colorFloatRGBto565", (void *)&SC_colorFloatRGBAto565,
1329 "int", "(float, float, float)" },
1330
1331
Jason Sams40a29e82009-08-10 14:55:26 -07001332 { "getWidth", (void *)&SC_getWidth,
1333 "int", "()" },
1334 { "getHeight", (void *)&SC_getHeight,
1335 "int", "()" },
1336
Jason Sams516c3192009-10-06 13:58:47 -07001337 { "sendToClient", (void *)&SC_toClient,
1338 "int", "(void *data, int cmdID, int len, int waitForSpace)" },
Jason Sams40a29e82009-08-10 14:55:26 -07001339
Jason Samsb0ec1b42009-07-28 12:02:16 -07001340
1341 { "debugF", (void *)&SC_debugF,
1342 "void", "(void *, float)" },
1343 { "debugI32", (void *)&SC_debugI32,
1344 "void", "(void *, int)" },
Romain Guyd22fff72009-08-20 17:08:33 -07001345 { "debugHexF", (void *)&SC_debugHexF,
1346 "void", "(void *, float)" },
1347 { "debugHexI32", (void *)&SC_debugHexI32,
1348 "void", "(void *, int)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001349
Jason Samsbd2197f2009-10-07 18:14:01 -07001350 { "scriptCall", (void *)&SC_scriptCall,
1351 "void", "(int)" },
1352
Jason Samsb0ec1b42009-07-28 12:02:16 -07001353
Jason Samsc97bb882009-07-20 14:31:06 -07001354 { NULL, NULL, NULL, NULL }
1355};
1356
1357const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
1358{
1359 ScriptCState::SymbolTable_t *syms = gSyms;
1360
1361 while (syms->mPtr) {
1362 if (!strcmp(syms->mName, sym)) {
1363 return syms;
1364 }
1365 syms++;
1366 }
1367 return NULL;
1368}
1369
1370void ScriptCState::appendDecls(String8 *str)
1371{
1372 ScriptCState::SymbolTable_t *syms = gSyms;
1373 while (syms->mPtr) {
1374 str->append(syms->mRet);
1375 str->append(" ");
1376 str->append(syms->mName);
1377 str->append(syms->mParam);
1378 str->append(";\n");
1379 syms++;
1380 }
1381}
1382
1383