blob: b7c66e2a334886671c98bfa331e9108874e89eb5 [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"
23#include "utils/String8.h"
24
25#include <GLES/gl.h>
26#include <GLES/glext.h>
27
Romain Guy584a3752009-07-30 18:45:01 -070028#include <time.h>
29#include <cutils/tztime.h>
30
Jason Samsc97bb882009-07-20 14:31:06 -070031using namespace android;
32using namespace android::renderscript;
33
34#define GET_TLS() Context::ScriptTLSStruct * tls = \
35 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
36 Context * rsc = tls->mContext; \
37 ScriptC * sc = (ScriptC *) tls->mScript
38
39
40//////////////////////////////////////////////////////////////////////////////
41// IO routines
42//////////////////////////////////////////////////////////////////////////////
43
44static float SC_loadF(uint32_t bank, uint32_t offset)
45{
46 GET_TLS();
47 const void *vp = sc->mSlots[bank]->getPtr();
48 const float *f = static_cast<const float *>(vp);
49 //LOGE("loadF %i %i = %f %x", bank, offset, f, ((int *)&f)[0]);
50 return f[offset];
51}
52
53static int32_t SC_loadI32(uint32_t bank, uint32_t offset)
54{
55 GET_TLS();
56 const void *vp = sc->mSlots[bank]->getPtr();
57 const int32_t *i = static_cast<const int32_t *>(vp);
58 //LOGE("loadI32 %i %i = %i", bank, offset, t);
59 return i[offset];
60}
61
Romain Guyf8e136d2009-08-06 12:40:41 -070062static float* SC_loadArrayF(uint32_t bank, uint32_t offset)
Romain Guya2136d62009-08-04 17:19:48 -070063{
64 GET_TLS();
65 void *vp = sc->mSlots[bank]->getPtr();
66 float *f = static_cast<float *>(vp);
Romain Guyf8e136d2009-08-06 12:40:41 -070067 return f + offset;
Romain Guya2136d62009-08-04 17:19:48 -070068}
69
Romain Guyf8e136d2009-08-06 12:40:41 -070070static int32_t* SC_loadArrayI32(uint32_t bank, uint32_t offset)
Romain Guya2136d62009-08-04 17:19:48 -070071{
72 GET_TLS();
73 void *vp = sc->mSlots[bank]->getPtr();
74 int32_t *i = static_cast<int32_t *>(vp);
Romain Guyf8e136d2009-08-06 12:40:41 -070075 return i + offset;
Romain Guya2136d62009-08-04 17:19:48 -070076}
77
Romain Guyf8e136d2009-08-06 12:40:41 -070078
Jason Samsc97bb882009-07-20 14:31:06 -070079static uint32_t SC_loadU32(uint32_t bank, uint32_t offset)
80{
81 GET_TLS();
82 const void *vp = sc->mSlots[bank]->getPtr();
83 const uint32_t *i = static_cast<const uint32_t *>(vp);
84 return i[offset];
85}
86
87static void SC_loadVec4(uint32_t bank, uint32_t offset, rsc_Vector4 *v)
88{
89 GET_TLS();
90 const void *vp = sc->mSlots[bank]->getPtr();
91 const float *f = static_cast<const float *>(vp);
92 memcpy(v, &f[offset], sizeof(rsc_Vector4));
93}
94
95static void SC_loadMatrix(uint32_t bank, uint32_t offset, rsc_Matrix *m)
96{
97 GET_TLS();
98 const void *vp = sc->mSlots[bank]->getPtr();
99 const float *f = static_cast<const float *>(vp);
100 memcpy(m, &f[offset], sizeof(rsc_Matrix));
101}
102
103
104static void SC_storeF(uint32_t bank, uint32_t offset, float v)
105{
106 //LOGE("storeF %i %i %f", bank, offset, v);
107 GET_TLS();
108 void *vp = sc->mSlots[bank]->getPtr();
109 float *f = static_cast<float *>(vp);
110 f[offset] = v;
111}
112
113static void SC_storeI32(uint32_t bank, uint32_t offset, int32_t v)
114{
115 GET_TLS();
116 void *vp = sc->mSlots[bank]->getPtr();
117 int32_t *f = static_cast<int32_t *>(vp);
118 static_cast<int32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
119}
120
121static void SC_storeU32(uint32_t bank, uint32_t offset, uint32_t v)
122{
123 GET_TLS();
124 void *vp = sc->mSlots[bank]->getPtr();
125 uint32_t *f = static_cast<uint32_t *>(vp);
126 static_cast<uint32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
127}
128
129static void SC_storeVec4(uint32_t bank, uint32_t offset, const rsc_Vector4 *v)
130{
131 GET_TLS();
132 void *vp = sc->mSlots[bank]->getPtr();
133 float *f = static_cast<float *>(vp);
134 memcpy(&f[offset], v, sizeof(rsc_Vector4));
135}
136
137static void SC_storeMatrix(uint32_t bank, uint32_t offset, const rsc_Matrix *m)
138{
139 GET_TLS();
140 void *vp = sc->mSlots[bank]->getPtr();
141 float *f = static_cast<float *>(vp);
142 memcpy(&f[offset], m, sizeof(rsc_Matrix));
143}
144
145
146//////////////////////////////////////////////////////////////////////////////
147// Math routines
148//////////////////////////////////////////////////////////////////////////////
149
Romain Guy8839ca52009-07-31 11:20:59 -0700150#define PI 3.1415926f
151#define DEG_TO_RAD PI / 180.0f
152#define RAD_TO_DEG 180.0f / PI
153
Jason Samsc97bb882009-07-20 14:31:06 -0700154static float SC_randf(float max)
155{
156 float r = (float)rand();
157 return r / RAND_MAX * max;
158}
159
Romain Guy8839ca52009-07-31 11:20:59 -0700160static float SC_randf2(float min, float max)
161{
162 float r = (float)rand();
163 return r / RAND_MAX * (max - min) + min;
164}
165
166static float SC_clampf(float amount, float low, float high)
167{
168 return amount < low ? low : (amount > high ? high : amount);
169}
170
171static float SC_maxf(float a, float b)
172{
173 return a > b ? a : b;
174}
175
176static float SC_minf(float a, float b)
177{
178 return a < b ? a : b;
179}
180
181static float SC_sqrf(float v)
182{
183 return v * v;
184}
185
186static float SC_distf2(float x1, float y1, float x2, float y2)
187{
188 float x = x2 - x1;
189 float y = y2 - y1;
190 return sqrtf(x * x + y * y);
191}
192
193static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2)
194{
195 float x = x2 - x1;
196 float y = y2 - y1;
197 float z = z2 - z1;
198 return sqrtf(x * x + y * y + z * z);
199}
200
201static float SC_magf2(float a, float b)
202{
203 return sqrtf(a * a + b * b);
204}
205
206static float SC_magf3(float a, float b, float c)
207{
208 return sqrtf(a * a + b * b + c * c);
209}
210
211static float SC_radf(float degrees)
212{
213 return degrees * DEG_TO_RAD;
214}
215
216static float SC_degf(float radians)
217{
218 return radians * RAD_TO_DEG;
219}
220
221static float SC_lerpf(float start, float stop, float amount)
222{
223 return start + (stop - start) * amount;
224}
225
226static float SC_normf(float start, float stop, float value)
227{
228 return (value - start) / (stop - start);
229}
230
231static float SC_mapf(float minStart, float minStop, float maxStart, float maxStop, float value)
232{
233 return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
234}
Jason Samsc97bb882009-07-20 14:31:06 -0700235
Romain Guy584a3752009-07-30 18:45:01 -0700236//////////////////////////////////////////////////////////////////////////////
237// Time routines
238//////////////////////////////////////////////////////////////////////////////
Jason Samsc97bb882009-07-20 14:31:06 -0700239
Romain Guy584a3752009-07-30 18:45:01 -0700240static uint32_t SC_second()
241{
242 GET_TLS();
243
244 time_t rawtime;
245 time(&rawtime);
246
247 if (sc->mEnviroment.mTimeZone) {
248 struct tm timeinfo;
249 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
250 return timeinfo.tm_sec;
251 } else {
252 struct tm *timeinfo;
253 timeinfo = localtime(&rawtime);
254 return timeinfo->tm_sec;
255 }
256}
257
258static uint32_t SC_minute()
259{
260 GET_TLS();
261
262 time_t rawtime;
263 time(&rawtime);
264
265 if (sc->mEnviroment.mTimeZone) {
266 struct tm timeinfo;
267 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
268 return timeinfo.tm_min;
269 } else {
270 struct tm *timeinfo;
271 timeinfo = localtime(&rawtime);
272 return timeinfo->tm_min;
273 }
274}
275
Romain Guy8839ca52009-07-31 11:20:59 -0700276static uint32_t SC_hour()
Romain Guy584a3752009-07-30 18:45:01 -0700277{
278 GET_TLS();
279
280 time_t rawtime;
281 time(&rawtime);
282
283 if (sc->mEnviroment.mTimeZone) {
284 struct tm timeinfo;
285 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
286 return timeinfo.tm_hour;
287 } else {
288 struct tm *timeinfo;
289 timeinfo = localtime(&rawtime);
290 return timeinfo->tm_hour;
291 }
Romain Guy8839ca52009-07-31 11:20:59 -0700292}
293
294static uint32_t SC_day()
295{
296 GET_TLS();
297
298 time_t rawtime;
299 time(&rawtime);
300
301 if (sc->mEnviroment.mTimeZone) {
302 struct tm timeinfo;
303 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
304 return timeinfo.tm_mday;
305 } else {
306 struct tm *timeinfo;
307 timeinfo = localtime(&rawtime);
308 return timeinfo->tm_mday;
309 }
Romain Guy584a3752009-07-30 18:45:01 -0700310}
Jason Samsc97bb882009-07-20 14:31:06 -0700311
Romain Guy8839ca52009-07-31 11:20:59 -0700312static uint32_t SC_month()
313{
314 GET_TLS();
315
316 time_t rawtime;
317 time(&rawtime);
318
319 if (sc->mEnviroment.mTimeZone) {
320 struct tm timeinfo;
321 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
322 return timeinfo.tm_mon;
323 } else {
324 struct tm *timeinfo;
325 timeinfo = localtime(&rawtime);
326 return timeinfo->tm_mon;
327 }
328}
329
330static uint32_t SC_year()
331{
332 GET_TLS();
333
334 time_t rawtime;
335 time(&rawtime);
336
337 if (sc->mEnviroment.mTimeZone) {
338 struct tm timeinfo;
339 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
340 return timeinfo.tm_year;
341 } else {
342 struct tm *timeinfo;
343 timeinfo = localtime(&rawtime);
344 return timeinfo->tm_year;
345 }
346}
347
Jason Samsc97bb882009-07-20 14:31:06 -0700348//////////////////////////////////////////////////////////////////////////////
349// Matrix routines
350//////////////////////////////////////////////////////////////////////////////
351
352
353static void SC_matrixLoadIdentity(rsc_Matrix *mat)
354{
355 Matrix *m = reinterpret_cast<Matrix *>(mat);
356 m->loadIdentity();
357}
358
359static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
360{
361 Matrix *m = reinterpret_cast<Matrix *>(mat);
362 m->load(f);
363}
364
365static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
366{
367 Matrix *m = reinterpret_cast<Matrix *>(mat);
368 m->load(reinterpret_cast<const Matrix *>(newmat));
369}
370
371static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
372{
373 Matrix *m = reinterpret_cast<Matrix *>(mat);
374 m->loadRotate(rot, x, y, z);
375}
376
377static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
378{
379 Matrix *m = reinterpret_cast<Matrix *>(mat);
380 m->loadScale(x, y, z);
381}
382
383static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
384{
385 Matrix *m = reinterpret_cast<Matrix *>(mat);
386 m->loadTranslate(x, y, z);
387}
388
389static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
390{
391 Matrix *m = reinterpret_cast<Matrix *>(mat);
392 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
393 reinterpret_cast<const Matrix *>(rhs));
394}
395
396static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
397{
398 Matrix *m = reinterpret_cast<Matrix *>(mat);
399 m->multiply(reinterpret_cast<const Matrix *>(rhs));
400}
401
402static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
403{
404 Matrix *m = reinterpret_cast<Matrix *>(mat);
405 m->rotate(rot, x, y, z);
406}
407
408static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
409{
410 Matrix *m = reinterpret_cast<Matrix *>(mat);
411 m->scale(x, y, z);
412}
413
414static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
415{
416 Matrix *m = reinterpret_cast<Matrix *>(mat);
417 m->translate(x, y, z);
418}
419
420
421
422
423//////////////////////////////////////////////////////////////////////////////
424// Context
425//////////////////////////////////////////////////////////////////////////////
426
427static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
428{
429 GET_TLS();
430 rsi_ProgramFragmentBindTexture(rsc,
431 static_cast<ProgramFragment *>(vpf),
432 slot,
433 static_cast<Allocation *>(va));
434
435}
436
437static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
438{
439 GET_TLS();
440 rsi_ProgramFragmentBindSampler(rsc,
441 static_cast<ProgramFragment *>(vpf),
442 slot,
443 static_cast<Sampler *>(vs));
444
445}
446
447static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs)
448{
449 GET_TLS();
450 rsi_ContextBindProgramFragmentStore(rsc, pfs);
451
452}
453
454static void SC_bindProgramFragment(RsProgramFragment pf)
455{
456 GET_TLS();
457 rsi_ContextBindProgramFragment(rsc, pf);
458
459}
460
Jason Samsee411122009-07-21 12:20:54 -0700461static void SC_bindProgramVertex(RsProgramVertex pv)
462{
463 GET_TLS();
464 rsi_ContextBindProgramVertex(rsc, pv);
465
466}
Jason Samsc97bb882009-07-20 14:31:06 -0700467
468//////////////////////////////////////////////////////////////////////////////
Jason Samsb0ec1b42009-07-28 12:02:16 -0700469// VP
470//////////////////////////////////////////////////////////////////////////////
471
472static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
473{
474 GET_TLS();
475 rsc->getVertex()->setModelviewMatrix(m);
476}
477
478static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
479{
480 GET_TLS();
481 rsc->getVertex()->setTextureMatrix(m);
482}
483
484
485
486//////////////////////////////////////////////////////////////////////////////
Jason Samsc97bb882009-07-20 14:31:06 -0700487// Drawing
488//////////////////////////////////////////////////////////////////////////////
489
490static void SC_drawTriangleMesh(RsTriangleMesh mesh)
491{
492 GET_TLS();
493 rsi_TriangleMeshRender(rsc, mesh);
494}
495
496static void SC_drawTriangleMeshRange(RsTriangleMesh mesh, uint32_t start, uint32_t count)
497{
498 GET_TLS();
499 rsi_TriangleMeshRenderRange(rsc, mesh, start, count);
500}
501
502// Assumes (GL_FIXED) x,y,z (GL_UNSIGNED_BYTE)r,g,b,a
503static void SC_drawTriangleArray(int ialloc, uint32_t count)
504{
505 GET_TLS();
506 RsAllocation alloc = (RsAllocation)ialloc;
507
508 const Allocation *a = (const Allocation *)alloc;
509 const uint32_t *ptr = (const uint32_t *)a->getPtr();
510
511 rsc->setupCheck();
512
513 glBindBuffer(GL_ARRAY_BUFFER, 0);
514 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
515
516 glEnableClientState(GL_VERTEX_ARRAY);
517 glDisableClientState(GL_NORMAL_ARRAY);
518 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
519 glEnableClientState(GL_COLOR_ARRAY);
520
521 glVertexPointer(2, GL_FIXED, 12, ptr + 1);
522 //glTexCoordPointer(2, GL_FIXED, 24, ptr + 1);
523 glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
524
525 glDrawArrays(GL_TRIANGLES, 0, count * 3);
526}
527
528static void SC_drawQuad(float x1, float y1, float z1,
529 float x2, float y2, float z2,
530 float x3, float y3, float z3,
531 float x4, float y4, float z4)
532{
533 GET_TLS();
534
535 //LOGE("Quad");
536 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
537 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
538 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
539 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
540
541 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
542 static const float tex[] = {0,1, 1,1, 1,0, 0,0};
543
544
545 rsc->setupCheck();
546
547 glBindBuffer(GL_ARRAY_BUFFER, 0);
548 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
549
550 glEnableClientState(GL_VERTEX_ARRAY);
551 glVertexPointer(3, GL_FLOAT, 0, vtx);
552
553 glClientActiveTexture(GL_TEXTURE0);
554 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
555 glTexCoordPointer(2, GL_FLOAT, 0, tex);
556 glClientActiveTexture(GL_TEXTURE1);
557 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
558 glTexCoordPointer(2, GL_FLOAT, 0, tex);
559 glClientActiveTexture(GL_TEXTURE0);
560
561 glDisableClientState(GL_NORMAL_ARRAY);
562 glDisableClientState(GL_COLOR_ARRAY);
563
564 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
565
566 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
567}
568
Jason Sams6f5c61c2009-07-28 17:20:11 -0700569static void SC_drawRect(float x1, float y1,
570 float x2, float y2, float z)
571{
572 SC_drawQuad(x1, y2, z,
573 x2, y2, z,
574 x2, y1, z,
575 x1, y1, z);
576}
577
Jason Samsc97bb882009-07-20 14:31:06 -0700578//////////////////////////////////////////////////////////////////////////////
579//
580//////////////////////////////////////////////////////////////////////////////
581
Jason Samsc97bb882009-07-20 14:31:06 -0700582static void SC_color(float r, float g, float b, float a)
583{
584 glColor4f(r, g, b, a);
585}
586
Romain Guya32d1002009-07-31 15:33:59 -0700587static void SC_hsb(float h, float s, float b, float a)
588{
589 float red = 0.0f;
590 float green = 0.0f;
591 float blue = 0.0f;
592
593 float x = h;
594 float y = s;
595 float z = b;
596
597 float hf = (x - (int) x) * 6.0f;
598 int ihf = (int) hf;
599 float f = hf - ihf;
600 float pv = z * (1.0f - y);
601 float qv = z * (1.0f - y * f);
602 float tv = z * (1.0f - y * (1.0f - f));
603
604 switch (ihf) {
605 case 0: // Red is the dominant color
606 red = z;
607 green = tv;
608 blue = pv;
609 break;
610 case 1: // Green is the dominant color
611 red = qv;
612 green = z;
613 blue = pv;
614 break;
615 case 2:
616 red = pv;
617 green = z;
618 blue = tv;
619 break;
620 case 3: // Blue is the dominant color
621 red = pv;
622 green = qv;
623 blue = z;
624 break;
625 case 4:
626 red = tv;
627 green = pv;
628 blue = z;
629 break;
630 case 5: // Red is the dominant color
631 red = z;
632 green = pv;
633 blue = qv;
634 break;
635 }
636
637 glColor4f(red, green, blue, a);
638}
639
Jason Samsb0ec1b42009-07-28 12:02:16 -0700640/*
Jason Samsc97bb882009-07-20 14:31:06 -0700641extern "C" void materialDiffuse(float r, float g, float b, float a)
642{
643 float v[] = {r, g, b, a};
644 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, v);
645}
646
647extern "C" void materialSpecular(float r, float g, float b, float a)
648{
649 float v[] = {r, g, b, a};
650 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, v);
651}
652
Jason Samsc97bb882009-07-20 14:31:06 -0700653extern "C" void materialShininess(float s)
654{
655 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &s);
656}
Jason Samsb0ec1b42009-07-28 12:02:16 -0700657*/
Jason Samsc97bb882009-07-20 14:31:06 -0700658
Jason Samsb0ec1b42009-07-28 12:02:16 -0700659static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samsc97bb882009-07-20 14:31:06 -0700660{
661 GET_TLS();
662 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
663}
664
Jason Samsc97bb882009-07-20 14:31:06 -0700665static void SC_ClearColor(float r, float g, float b, float a)
666{
667 //LOGE("c %f %f %f %f", r, g, b, a);
668 GET_TLS();
669 sc->mEnviroment.mClearColor[0] = r;
670 sc->mEnviroment.mClearColor[1] = g;
671 sc->mEnviroment.mClearColor[2] = b;
672 sc->mEnviroment.mClearColor[3] = a;
673}
674
Jason Samsb0ec1b42009-07-28 12:02:16 -0700675static void SC_debugF(const char *s, float f)
676{
677 LOGE("%s %f", s, f);
678}
679
680static void SC_debugI32(const char *s, int32_t i)
681{
682 LOGE("%s %i", s, i);
683}
684
Jason Samsc97bb882009-07-20 14:31:06 -0700685
686
687//////////////////////////////////////////////////////////////////////////////
688// Class implementation
689//////////////////////////////////////////////////////////////////////////////
690
691ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
692 // IO
693 { "loadI32", (void *)&SC_loadI32,
694 "int", "(int, int)" },
695 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
696 { "loadF", (void *)&SC_loadF,
697 "float", "(int, int)" },
Romain Guya2136d62009-08-04 17:19:48 -0700698 { "loadArrayF", (void *)&SC_loadArrayF,
Romain Guyf8e136d2009-08-06 12:40:41 -0700699 "float*", "(int, int)" },
Romain Guya2136d62009-08-04 17:19:48 -0700700 { "loadArrayI32", (void *)&SC_loadArrayI32,
Romain Guyf8e136d2009-08-06 12:40:41 -0700701 "int*", "(int, int)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700702 { "loadVec4", (void *)&SC_loadVec4,
703 "void", "(int, int, float *)" },
704 { "loadMatrix", (void *)&SC_loadMatrix,
705 "void", "(int, int, float *)" },
706 { "storeI32", (void *)&SC_storeI32,
707 "void", "(int, int, int)" },
708 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
709 { "storeF", (void *)&SC_storeF,
710 "void", "(int, int, float)" },
711 { "storeVec4", (void *)&SC_storeVec4,
712 "void", "(int, int, float *)" },
713 { "storeMatrix", (void *)&SC_storeMatrix,
714 "void", "(int, int, float *)" },
715
716 // math
717 { "sinf", (void *)&sinf,
718 "float", "(float)" },
719 { "cosf", (void *)&cosf,
720 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700721 { "asinf", (void *)&asinf,
722 "float", "(float)" },
723 { "acosf", (void *)&acosf,
724 "float", "(float)" },
725 { "atanf", (void *)&atanf,
726 "float", "(float)" },
727 { "atan2f", (void *)&atan2f,
Romain Guya32d1002009-07-31 15:33:59 -0700728 "float", "(float, float)" },
Jason Sams6f5c61c2009-07-28 17:20:11 -0700729 { "fabsf", (void *)&fabsf,
Jason Samsc97bb882009-07-20 14:31:06 -0700730 "float", "(float)" },
731 { "randf", (void *)&SC_randf,
732 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700733 { "randf2", (void *)&SC_randf2,
734 "float", "(float, float)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -0700735 { "floorf", (void *)&floorf,
736 "float", "(float)" },
737 { "ceilf", (void *)&ceilf,
738 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700739 { "expf", (void *)&expf,
740 "float", "(float)" },
741 { "logf", (void *)&logf,
742 "float", "(float)" },
743 { "powf", (void *)&powf,
744 "float", "(float, float)" },
745 { "maxf", (void *)&SC_maxf,
746 "float", "(float, float)" },
747 { "minf", (void *)&SC_minf,
748 "float", "(float, float)" },
749 { "sqrtf", (void *)&sqrtf,
750 "float", "(float)" },
751 { "sqrf", (void *)&SC_sqrf,
752 "float", "(float)" },
753 { "clampf", (void *)&SC_clampf,
754 "float", "(float, float, float)" },
755 { "distf2", (void *)&SC_distf2,
756 "float", "(float, float, float, float)" },
757 { "distf3", (void *)&SC_distf3,
758 "float", "(float, float, float, float, float, float)" },
759 { "magf2", (void *)&SC_magf2,
760 "float", "(float, float)" },
761 { "magf3", (void *)&SC_magf3,
762 "float", "(float, float, float)" },
763 { "radf", (void *)&SC_radf,
764 "float", "(float)" },
765 { "degf", (void *)&SC_degf,
766 "float", "(float)" },
767 { "lerpf", (void *)&SC_lerpf,
768 "float", "(float, float, float)" },
769 { "normf", (void *)&SC_normf,
770 "float", "(float, float, float)" },
771 { "mapf", (void *)&SC_mapf,
772 "float", "(float, float, float, float, float)" },
Romain Guyecc7ca02009-08-03 21:12:51 -0700773 { "noisef", (void *)&SC_noisef,
774 "float", "(float)" },
775 { "noisef2", (void *)&SC_noisef2,
776 "float", "(float, float)" },
777 { "noisef3", (void *)&SC_noisef3,
778 "float", "(float, float, float)" },
779 { "turbulencef2", (void *)&SC_turbulencef2,
780 "float", "(float, float, float)" },
781 { "turbulencef3", (void *)&SC_turbulencef3,
782 "float", "(float, float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700783
Romain Guy584a3752009-07-30 18:45:01 -0700784 // time
785 { "second", (void *)&SC_second,
786 "int", "()" },
787 { "minute", (void *)&SC_minute,
788 "int", "()" },
789 { "hour", (void *)&SC_hour,
790 "int", "()" },
Romain Guy8839ca52009-07-31 11:20:59 -0700791 { "day", (void *)&SC_day,
792 "int", "()" },
793 { "month", (void *)&SC_month,
794 "int", "()" },
795 { "year", (void *)&SC_year,
796 "int", "()" },
Romain Guy584a3752009-07-30 18:45:01 -0700797
Jason Samsc97bb882009-07-20 14:31:06 -0700798 // matrix
799 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
800 "void", "(float *mat)" },
801 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
802 "void", "(float *mat, float *f)" },
803 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
804 "void", "(float *mat, float *newmat)" },
805 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
806 "void", "(float *mat, float rot, float x, float y, float z)" },
807 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
808 "void", "(float *mat, float x, float y, float z)" },
809 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
810 "void", "(float *mat, float x, float y, float z)" },
811 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
812 "void", "(float *mat, float *lhs, float *rhs)" },
813 { "matrixMultiply", (void *)&SC_matrixMultiply,
814 "void", "(float *mat, float *rhs)" },
815 { "matrixRotate", (void *)&SC_matrixRotate,
816 "void", "(float *mat, float rot, float x, float y, float z)" },
817 { "matrixScale", (void *)&SC_matrixScale,
818 "void", "(float *mat, float x, float y, float z)" },
819 { "matrixTranslate", (void *)&SC_matrixTranslate,
820 "void", "(float *mat, float x, float y, float z)" },
821
822 // context
823 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
824 "void", "(int)" },
825 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
826 "void", "(int)" },
Jason Samsee411122009-07-21 12:20:54 -0700827 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
828 "void", "(int)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700829 { "bindSampler", (void *)&SC_bindSampler,
830 "void", "(int, int, int)" },
831 { "bindTexture", (void *)&SC_bindTexture,
832 "void", "(int, int, int)" },
833
Jason Samsb0ec1b42009-07-28 12:02:16 -0700834 // vp
Jason Samsfaf15202009-07-29 20:55:44 -0700835 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsb0ec1b42009-07-28 12:02:16 -0700836 "void", "(void *)" },
Jason Samsfaf15202009-07-29 20:55:44 -0700837 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsb0ec1b42009-07-28 12:02:16 -0700838 "void", "(void *)" },
839
840
841
Jason Samsc97bb882009-07-20 14:31:06 -0700842 // drawing
Jason Sams6f5c61c2009-07-28 17:20:11 -0700843 { "drawRect", (void *)&SC_drawRect,
844 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700845 { "drawQuad", (void *)&SC_drawQuad,
846 "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)" },
847 { "drawTriangleArray", (void *)&SC_drawTriangleArray,
848 "void", "(int ialloc, int count)" },
849 { "drawTriangleMesh", (void *)&SC_drawTriangleMesh,
850 "void", "(int mesh)" },
851 { "drawTriangleMeshRange", (void *)&SC_drawTriangleMeshRange,
852 "void", "(int mesh, int start, int count)" },
853
854
855 // misc
856 { "pfClearColor", (void *)&SC_ClearColor,
857 "void", "(float, float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700858 { "color", (void *)&SC_color,
859 "void", "(float, float, float, float)" },
Romain Guya32d1002009-07-31 15:33:59 -0700860 { "hsb", (void *)&SC_hsb,
861 "void", "(float, float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700862
Jason Samsb0ec1b42009-07-28 12:02:16 -0700863 { "uploadToTexture", (void *)&SC_uploadToTexture,
864 "void", "(int, int)" },
865
866
867 { "debugF", (void *)&SC_debugF,
868 "void", "(void *, float)" },
869 { "debugI32", (void *)&SC_debugI32,
870 "void", "(void *, int)" },
871
872
Jason Samsc97bb882009-07-20 14:31:06 -0700873 { NULL, NULL, NULL, NULL }
874};
875
876const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
877{
878 ScriptCState::SymbolTable_t *syms = gSyms;
879
880 while (syms->mPtr) {
881 if (!strcmp(syms->mName, sym)) {
882 return syms;
883 }
884 syms++;
885 }
886 return NULL;
887}
888
889void ScriptCState::appendDecls(String8 *str)
890{
891 ScriptCState::SymbolTable_t *syms = gSyms;
892 while (syms->mPtr) {
893 str->append(syms->mRet);
894 str->append(" ");
895 str->append(syms->mName);
896 str->append(syms->mParam);
897 str->append(";\n");
898 syms++;
899 }
900}
901
902