blob: 87ddf5868163bd71cc2ae708fc240eebf7c56b99 [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 Guyb62627e2009-08-06 22:52:13 -070078static float* SC_loadTriangleMeshVerticesF(RsTriangleMesh mesh)
79{
80 TriangleMesh *tm = static_cast<TriangleMesh *>(mesh);
81 void *vp = tm->mVertexData;
82 float *f = static_cast<float *>(vp);
83 return f;
84}
85
86static void SC_updateTriangleMesh(RsTriangleMesh mesh)
87{
88 TriangleMesh *tm = static_cast<TriangleMesh *>(mesh);
89 glBindBuffer(GL_ARRAY_BUFFER, tm->mBufferObjects[0]);
90 glBufferData(GL_ARRAY_BUFFER, tm->mVertexDataSize, tm->mVertexData, GL_STATIC_DRAW);
91 glBindBuffer(GL_ARRAY_BUFFER, 0);
92
93 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
94 glBufferData(GL_ELEMENT_ARRAY_BUFFER, tm->mIndexDataSize, tm->mIndexData, GL_STATIC_DRAW);
95 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
96}
Romain Guyf8e136d2009-08-06 12:40:41 -070097
Jason Samsc97bb882009-07-20 14:31:06 -070098static uint32_t SC_loadU32(uint32_t bank, uint32_t offset)
99{
100 GET_TLS();
101 const void *vp = sc->mSlots[bank]->getPtr();
102 const uint32_t *i = static_cast<const uint32_t *>(vp);
103 return i[offset];
104}
105
106static void SC_loadVec4(uint32_t bank, uint32_t offset, rsc_Vector4 *v)
107{
108 GET_TLS();
109 const void *vp = sc->mSlots[bank]->getPtr();
110 const float *f = static_cast<const float *>(vp);
111 memcpy(v, &f[offset], sizeof(rsc_Vector4));
112}
113
114static void SC_loadMatrix(uint32_t bank, uint32_t offset, rsc_Matrix *m)
115{
116 GET_TLS();
117 const void *vp = sc->mSlots[bank]->getPtr();
118 const float *f = static_cast<const float *>(vp);
119 memcpy(m, &f[offset], sizeof(rsc_Matrix));
120}
121
122
123static void SC_storeF(uint32_t bank, uint32_t offset, float v)
124{
125 //LOGE("storeF %i %i %f", bank, offset, v);
126 GET_TLS();
127 void *vp = sc->mSlots[bank]->getPtr();
128 float *f = static_cast<float *>(vp);
129 f[offset] = v;
130}
131
132static void SC_storeI32(uint32_t bank, uint32_t offset, int32_t v)
133{
134 GET_TLS();
135 void *vp = sc->mSlots[bank]->getPtr();
136 int32_t *f = static_cast<int32_t *>(vp);
137 static_cast<int32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
138}
139
140static void SC_storeU32(uint32_t bank, uint32_t offset, uint32_t v)
141{
142 GET_TLS();
143 void *vp = sc->mSlots[bank]->getPtr();
144 uint32_t *f = static_cast<uint32_t *>(vp);
145 static_cast<uint32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
146}
147
148static void SC_storeVec4(uint32_t bank, uint32_t offset, const rsc_Vector4 *v)
149{
150 GET_TLS();
151 void *vp = sc->mSlots[bank]->getPtr();
152 float *f = static_cast<float *>(vp);
153 memcpy(&f[offset], v, sizeof(rsc_Vector4));
154}
155
156static void SC_storeMatrix(uint32_t bank, uint32_t offset, const rsc_Matrix *m)
157{
158 GET_TLS();
159 void *vp = sc->mSlots[bank]->getPtr();
160 float *f = static_cast<float *>(vp);
161 memcpy(&f[offset], m, sizeof(rsc_Matrix));
162}
163
164
165//////////////////////////////////////////////////////////////////////////////
166// Math routines
167//////////////////////////////////////////////////////////////////////////////
168
Romain Guy8839ca52009-07-31 11:20:59 -0700169#define PI 3.1415926f
170#define DEG_TO_RAD PI / 180.0f
171#define RAD_TO_DEG 180.0f / PI
172
Jason Samsc97bb882009-07-20 14:31:06 -0700173static float SC_randf(float max)
174{
175 float r = (float)rand();
176 return r / RAND_MAX * max;
177}
178
Romain Guy8839ca52009-07-31 11:20:59 -0700179static float SC_randf2(float min, float max)
180{
181 float r = (float)rand();
182 return r / RAND_MAX * (max - min) + min;
183}
184
185static float SC_clampf(float amount, float low, float high)
186{
187 return amount < low ? low : (amount > high ? high : amount);
188}
189
190static float SC_maxf(float a, float b)
191{
192 return a > b ? a : b;
193}
194
195static float SC_minf(float a, float b)
196{
197 return a < b ? a : b;
198}
199
200static float SC_sqrf(float v)
201{
202 return v * v;
203}
204
Romain Guy8f5c94b2009-08-08 18:30:19 -0700205static int SC_sqr(int v)
206{
207 return v * v;
208}
209
Romain Guy8839ca52009-07-31 11:20:59 -0700210static float SC_distf2(float x1, float y1, float x2, float y2)
211{
212 float x = x2 - x1;
213 float y = y2 - y1;
214 return sqrtf(x * x + y * y);
215}
216
217static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2)
218{
219 float x = x2 - x1;
220 float y = y2 - y1;
221 float z = z2 - z1;
222 return sqrtf(x * x + y * y + z * z);
223}
224
225static float SC_magf2(float a, float b)
226{
227 return sqrtf(a * a + b * b);
228}
229
230static float SC_magf3(float a, float b, float c)
231{
232 return sqrtf(a * a + b * b + c * c);
233}
234
235static float SC_radf(float degrees)
236{
237 return degrees * DEG_TO_RAD;
238}
239
240static float SC_degf(float radians)
241{
242 return radians * RAD_TO_DEG;
243}
244
245static float SC_lerpf(float start, float stop, float amount)
246{
247 return start + (stop - start) * amount;
248}
249
250static float SC_normf(float start, float stop, float value)
251{
252 return (value - start) / (stop - start);
253}
254
255static float SC_mapf(float minStart, float minStop, float maxStart, float maxStop, float value)
256{
257 return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
258}
Jason Samsc97bb882009-07-20 14:31:06 -0700259
Romain Guy584a3752009-07-30 18:45:01 -0700260//////////////////////////////////////////////////////////////////////////////
261// Time routines
262//////////////////////////////////////////////////////////////////////////////
Jason Samsc97bb882009-07-20 14:31:06 -0700263
Romain Guy584a3752009-07-30 18:45:01 -0700264static uint32_t SC_second()
265{
266 GET_TLS();
267
268 time_t rawtime;
269 time(&rawtime);
270
271 if (sc->mEnviroment.mTimeZone) {
272 struct tm timeinfo;
273 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
274 return timeinfo.tm_sec;
275 } else {
276 struct tm *timeinfo;
277 timeinfo = localtime(&rawtime);
278 return timeinfo->tm_sec;
279 }
280}
281
282static uint32_t SC_minute()
283{
284 GET_TLS();
285
286 time_t rawtime;
287 time(&rawtime);
288
289 if (sc->mEnviroment.mTimeZone) {
290 struct tm timeinfo;
291 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
292 return timeinfo.tm_min;
293 } else {
294 struct tm *timeinfo;
295 timeinfo = localtime(&rawtime);
296 return timeinfo->tm_min;
297 }
298}
299
Romain Guy8839ca52009-07-31 11:20:59 -0700300static uint32_t SC_hour()
Romain Guy584a3752009-07-30 18:45:01 -0700301{
302 GET_TLS();
303
304 time_t rawtime;
305 time(&rawtime);
306
307 if (sc->mEnviroment.mTimeZone) {
308 struct tm timeinfo;
309 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
310 return timeinfo.tm_hour;
311 } else {
312 struct tm *timeinfo;
313 timeinfo = localtime(&rawtime);
314 return timeinfo->tm_hour;
315 }
Romain Guy8839ca52009-07-31 11:20:59 -0700316}
317
318static uint32_t SC_day()
319{
320 GET_TLS();
321
322 time_t rawtime;
323 time(&rawtime);
324
325 if (sc->mEnviroment.mTimeZone) {
326 struct tm timeinfo;
327 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
328 return timeinfo.tm_mday;
329 } else {
330 struct tm *timeinfo;
331 timeinfo = localtime(&rawtime);
332 return timeinfo->tm_mday;
333 }
Romain Guy584a3752009-07-30 18:45:01 -0700334}
Jason Samsc97bb882009-07-20 14:31:06 -0700335
Romain Guy8839ca52009-07-31 11:20:59 -0700336static uint32_t SC_month()
337{
338 GET_TLS();
339
340 time_t rawtime;
341 time(&rawtime);
342
343 if (sc->mEnviroment.mTimeZone) {
344 struct tm timeinfo;
345 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
346 return timeinfo.tm_mon;
347 } else {
348 struct tm *timeinfo;
349 timeinfo = localtime(&rawtime);
350 return timeinfo->tm_mon;
351 }
352}
353
354static uint32_t SC_year()
355{
356 GET_TLS();
357
358 time_t rawtime;
359 time(&rawtime);
360
361 if (sc->mEnviroment.mTimeZone) {
362 struct tm timeinfo;
363 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
364 return timeinfo.tm_year;
365 } else {
366 struct tm *timeinfo;
367 timeinfo = localtime(&rawtime);
368 return timeinfo->tm_year;
369 }
370}
371
Jason Samsc97bb882009-07-20 14:31:06 -0700372//////////////////////////////////////////////////////////////////////////////
373// Matrix routines
374//////////////////////////////////////////////////////////////////////////////
375
376
377static void SC_matrixLoadIdentity(rsc_Matrix *mat)
378{
379 Matrix *m = reinterpret_cast<Matrix *>(mat);
380 m->loadIdentity();
381}
382
383static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
384{
385 Matrix *m = reinterpret_cast<Matrix *>(mat);
386 m->load(f);
387}
388
389static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
390{
391 Matrix *m = reinterpret_cast<Matrix *>(mat);
392 m->load(reinterpret_cast<const Matrix *>(newmat));
393}
394
395static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
396{
397 Matrix *m = reinterpret_cast<Matrix *>(mat);
398 m->loadRotate(rot, x, y, z);
399}
400
401static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
402{
403 Matrix *m = reinterpret_cast<Matrix *>(mat);
404 m->loadScale(x, y, z);
405}
406
407static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
408{
409 Matrix *m = reinterpret_cast<Matrix *>(mat);
410 m->loadTranslate(x, y, z);
411}
412
413static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
414{
415 Matrix *m = reinterpret_cast<Matrix *>(mat);
416 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
417 reinterpret_cast<const Matrix *>(rhs));
418}
419
420static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
421{
422 Matrix *m = reinterpret_cast<Matrix *>(mat);
423 m->multiply(reinterpret_cast<const Matrix *>(rhs));
424}
425
426static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
427{
428 Matrix *m = reinterpret_cast<Matrix *>(mat);
429 m->rotate(rot, x, y, z);
430}
431
432static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
433{
434 Matrix *m = reinterpret_cast<Matrix *>(mat);
435 m->scale(x, y, z);
436}
437
438static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
439{
440 Matrix *m = reinterpret_cast<Matrix *>(mat);
441 m->translate(x, y, z);
442}
443
444
445
446
447//////////////////////////////////////////////////////////////////////////////
448// Context
449//////////////////////////////////////////////////////////////////////////////
450
451static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
452{
453 GET_TLS();
454 rsi_ProgramFragmentBindTexture(rsc,
455 static_cast<ProgramFragment *>(vpf),
456 slot,
457 static_cast<Allocation *>(va));
458
459}
460
461static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
462{
463 GET_TLS();
464 rsi_ProgramFragmentBindSampler(rsc,
465 static_cast<ProgramFragment *>(vpf),
466 slot,
467 static_cast<Sampler *>(vs));
468
469}
470
471static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs)
472{
473 GET_TLS();
474 rsi_ContextBindProgramFragmentStore(rsc, pfs);
475
476}
477
478static void SC_bindProgramFragment(RsProgramFragment pf)
479{
480 GET_TLS();
481 rsi_ContextBindProgramFragment(rsc, pf);
482
483}
484
Jason Samsee411122009-07-21 12:20:54 -0700485static void SC_bindProgramVertex(RsProgramVertex pv)
486{
487 GET_TLS();
488 rsi_ContextBindProgramVertex(rsc, pv);
489
490}
Jason Samsc97bb882009-07-20 14:31:06 -0700491
492//////////////////////////////////////////////////////////////////////////////
Jason Samsb0ec1b42009-07-28 12:02:16 -0700493// VP
494//////////////////////////////////////////////////////////////////////////////
495
496static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
497{
498 GET_TLS();
499 rsc->getVertex()->setModelviewMatrix(m);
500}
501
502static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
503{
504 GET_TLS();
505 rsc->getVertex()->setTextureMatrix(m);
506}
507
508
509
510//////////////////////////////////////////////////////////////////////////////
Jason Samsc97bb882009-07-20 14:31:06 -0700511// Drawing
512//////////////////////////////////////////////////////////////////////////////
513
514static void SC_drawTriangleMesh(RsTriangleMesh mesh)
515{
516 GET_TLS();
517 rsi_TriangleMeshRender(rsc, mesh);
518}
519
520static void SC_drawTriangleMeshRange(RsTriangleMesh mesh, uint32_t start, uint32_t count)
521{
522 GET_TLS();
523 rsi_TriangleMeshRenderRange(rsc, mesh, start, count);
524}
525
526// Assumes (GL_FIXED) x,y,z (GL_UNSIGNED_BYTE)r,g,b,a
527static void SC_drawTriangleArray(int ialloc, uint32_t count)
528{
529 GET_TLS();
530 RsAllocation alloc = (RsAllocation)ialloc;
531
532 const Allocation *a = (const Allocation *)alloc;
533 const uint32_t *ptr = (const uint32_t *)a->getPtr();
534
535 rsc->setupCheck();
536
537 glBindBuffer(GL_ARRAY_BUFFER, 0);
538 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
539
540 glEnableClientState(GL_VERTEX_ARRAY);
541 glDisableClientState(GL_NORMAL_ARRAY);
542 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
543 glEnableClientState(GL_COLOR_ARRAY);
544
545 glVertexPointer(2, GL_FIXED, 12, ptr + 1);
546 //glTexCoordPointer(2, GL_FIXED, 24, ptr + 1);
547 glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
548
549 glDrawArrays(GL_TRIANGLES, 0, count * 3);
550}
551
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700552static void SC_drawLine(float x1, float y1, float z1,
553 float x2, float y2, float z2)
554{
555 GET_TLS();
556 rsc->setupCheck();
557
558 float vtx[] = { x1, y1, z1, x2, y2, z2 };
559
560 glBindBuffer(GL_ARRAY_BUFFER, 0);
561 glEnableClientState(GL_VERTEX_ARRAY);
562 glVertexPointer(3, GL_FLOAT, 0, vtx);
563
564 glDisableClientState(GL_NORMAL_ARRAY);
565 glDisableClientState(GL_COLOR_ARRAY);
566
567 glDrawArrays(GL_LINES, 0, 2);
568}
569
Romain Guy8f5c94b2009-08-08 18:30:19 -0700570static void SC_drawQuadTexCoords(float x1, float y1, float z1,
571 float u1, float v1,
572 float x2, float y2, float z2,
573 float u2, float v2,
574 float x3, float y3, float z3,
575 float u3, float v3,
576 float x4, float y4, float z4,
577 float u4, float v4)
Jason Samsc97bb882009-07-20 14:31:06 -0700578{
579 GET_TLS();
Romain Guy8f5c94b2009-08-08 18:30:19 -0700580
Jason Samsc97bb882009-07-20 14:31:06 -0700581 //LOGE("Quad");
582 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
583 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
584 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
585 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
Romain Guy8f5c94b2009-08-08 18:30:19 -0700586
Jason Samsc97bb882009-07-20 14:31:06 -0700587 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
Romain Guy8f5c94b2009-08-08 18:30:19 -0700588 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
Jason Samsc97bb882009-07-20 14:31:06 -0700589
590 rsc->setupCheck();
591
592 glBindBuffer(GL_ARRAY_BUFFER, 0);
593 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
594
595 glEnableClientState(GL_VERTEX_ARRAY);
596 glVertexPointer(3, GL_FLOAT, 0, vtx);
597
598 glClientActiveTexture(GL_TEXTURE0);
599 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
600 glTexCoordPointer(2, GL_FLOAT, 0, tex);
601 glClientActiveTexture(GL_TEXTURE1);
602 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
603 glTexCoordPointer(2, GL_FLOAT, 0, tex);
604 glClientActiveTexture(GL_TEXTURE0);
605
606 glDisableClientState(GL_NORMAL_ARRAY);
607 glDisableClientState(GL_COLOR_ARRAY);
608
609 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
610
611 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
612}
613
Romain Guy8f5c94b2009-08-08 18:30:19 -0700614static void SC_drawQuad(float x1, float y1, float z1,
615 float x2, float y2, float z2,
616 float x3, float y3, float z3,
617 float x4, float y4, float z4)
618{
619 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
620 x2, y2, z2, 1, 1,
621 x3, y3, z3, 1, 0,
622 x4, y4, z4, 0, 0);
623}
624
Jason Sams6f5c61c2009-07-28 17:20:11 -0700625static void SC_drawRect(float x1, float y1,
626 float x2, float y2, float z)
627{
628 SC_drawQuad(x1, y2, z,
629 x2, y2, z,
630 x2, y1, z,
631 x1, y1, z);
632}
633
Jason Samsc97bb882009-07-20 14:31:06 -0700634//////////////////////////////////////////////////////////////////////////////
635//
636//////////////////////////////////////////////////////////////////////////////
637
Jason Samsc97bb882009-07-20 14:31:06 -0700638static void SC_color(float r, float g, float b, float a)
639{
640 glColor4f(r, g, b, a);
641}
642
Romain Guyb62627e2009-08-06 22:52:13 -0700643static void SC_ambient(float r, float g, float b, float a)
644{
645 GLfloat params[] = { r, g, b, a };
646 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, params);
647}
648
649static void SC_diffuse(float r, float g, float b, float a)
650{
651 GLfloat params[] = { r, g, b, a };
652 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, params);
653}
654
655static void SC_specular(float r, float g, float b, float a)
656{
657 GLfloat params[] = { r, g, b, a };
658 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, params);
659}
660
661static void SC_emission(float r, float g, float b, float a)
662{
663 GLfloat params[] = { r, g, b, a };
664 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, params);
665}
666
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700667static void SC_shininess(float s)
Romain Guyb62627e2009-08-06 22:52:13 -0700668{
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700669 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, s);
Romain Guyb62627e2009-08-06 22:52:13 -0700670}
671
Romain Guya32d1002009-07-31 15:33:59 -0700672static void SC_hsb(float h, float s, float b, float a)
673{
674 float red = 0.0f;
675 float green = 0.0f;
676 float blue = 0.0f;
677
678 float x = h;
679 float y = s;
680 float z = b;
681
682 float hf = (x - (int) x) * 6.0f;
683 int ihf = (int) hf;
684 float f = hf - ihf;
685 float pv = z * (1.0f - y);
686 float qv = z * (1.0f - y * f);
687 float tv = z * (1.0f - y * (1.0f - f));
688
689 switch (ihf) {
690 case 0: // Red is the dominant color
691 red = z;
692 green = tv;
693 blue = pv;
694 break;
695 case 1: // Green is the dominant color
696 red = qv;
697 green = z;
698 blue = pv;
699 break;
700 case 2:
701 red = pv;
702 green = z;
703 blue = tv;
704 break;
705 case 3: // Blue is the dominant color
706 red = pv;
707 green = qv;
708 blue = z;
709 break;
710 case 4:
711 red = tv;
712 green = pv;
713 blue = z;
714 break;
715 case 5: // Red is the dominant color
716 red = z;
717 green = pv;
718 blue = qv;
719 break;
720 }
721
722 glColor4f(red, green, blue, a);
723}
724
Jason Samsb0ec1b42009-07-28 12:02:16 -0700725static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samsc97bb882009-07-20 14:31:06 -0700726{
727 GET_TLS();
728 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
729}
730
Jason Samsc97bb882009-07-20 14:31:06 -0700731static void SC_ClearColor(float r, float g, float b, float a)
732{
733 //LOGE("c %f %f %f %f", r, g, b, a);
734 GET_TLS();
735 sc->mEnviroment.mClearColor[0] = r;
736 sc->mEnviroment.mClearColor[1] = g;
737 sc->mEnviroment.mClearColor[2] = b;
738 sc->mEnviroment.mClearColor[3] = a;
739}
740
Jason Samsb0ec1b42009-07-28 12:02:16 -0700741static void SC_debugF(const char *s, float f)
742{
743 LOGE("%s %f", s, f);
744}
745
746static void SC_debugI32(const char *s, int32_t i)
747{
748 LOGE("%s %i", s, i);
749}
750
Jason Samsc97bb882009-07-20 14:31:06 -0700751
752
753//////////////////////////////////////////////////////////////////////////////
754// Class implementation
755//////////////////////////////////////////////////////////////////////////////
756
757ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
758 // IO
759 { "loadI32", (void *)&SC_loadI32,
760 "int", "(int, int)" },
761 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
762 { "loadF", (void *)&SC_loadF,
763 "float", "(int, int)" },
Romain Guya2136d62009-08-04 17:19:48 -0700764 { "loadArrayF", (void *)&SC_loadArrayF,
Romain Guyf8e136d2009-08-06 12:40:41 -0700765 "float*", "(int, int)" },
Romain Guya2136d62009-08-04 17:19:48 -0700766 { "loadArrayI32", (void *)&SC_loadArrayI32,
Romain Guyf8e136d2009-08-06 12:40:41 -0700767 "int*", "(int, int)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700768 { "loadVec4", (void *)&SC_loadVec4,
769 "void", "(int, int, float *)" },
770 { "loadMatrix", (void *)&SC_loadMatrix,
771 "void", "(int, int, float *)" },
772 { "storeI32", (void *)&SC_storeI32,
773 "void", "(int, int, int)" },
774 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
775 { "storeF", (void *)&SC_storeF,
776 "void", "(int, int, float)" },
777 { "storeVec4", (void *)&SC_storeVec4,
778 "void", "(int, int, float *)" },
779 { "storeMatrix", (void *)&SC_storeMatrix,
780 "void", "(int, int, float *)" },
Romain Guyb62627e2009-08-06 22:52:13 -0700781 { "loadTriangleMeshVerticesF", (void *)&SC_loadTriangleMeshVerticesF,
782 "float*", "(int)" },
783 { "updateTriangleMesh", (void *)&SC_updateTriangleMesh,
784 "void", "(int)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700785
786 // math
Romain Guy8f5c94b2009-08-08 18:30:19 -0700787 { "abs", (void *)&abs,
788 "int", "(int)" },
789 { "absf", (void *)&fabs,
790 "float", "(float)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700791 { "sinf", (void *)&sinf,
792 "float", "(float)" },
793 { "cosf", (void *)&cosf,
794 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700795 { "asinf", (void *)&asinf,
796 "float", "(float)" },
797 { "acosf", (void *)&acosf,
798 "float", "(float)" },
799 { "atanf", (void *)&atanf,
800 "float", "(float)" },
801 { "atan2f", (void *)&atan2f,
Romain Guya32d1002009-07-31 15:33:59 -0700802 "float", "(float, float)" },
Jason Sams6f5c61c2009-07-28 17:20:11 -0700803 { "fabsf", (void *)&fabsf,
Jason Samsc97bb882009-07-20 14:31:06 -0700804 "float", "(float)" },
805 { "randf", (void *)&SC_randf,
806 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700807 { "randf2", (void *)&SC_randf2,
808 "float", "(float, float)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -0700809 { "floorf", (void *)&floorf,
810 "float", "(float)" },
811 { "ceilf", (void *)&ceilf,
812 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700813 { "expf", (void *)&expf,
814 "float", "(float)" },
815 { "logf", (void *)&logf,
816 "float", "(float)" },
817 { "powf", (void *)&powf,
818 "float", "(float, float)" },
819 { "maxf", (void *)&SC_maxf,
820 "float", "(float, float)" },
821 { "minf", (void *)&SC_minf,
822 "float", "(float, float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -0700823 { "sqrt", (void *)&sqrt,
824 "int", "(int)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700825 { "sqrtf", (void *)&sqrtf,
826 "float", "(float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -0700827 { "sqr", (void *)&SC_sqr,
828 "int", "(int)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700829 { "sqrf", (void *)&SC_sqrf,
830 "float", "(float)" },
831 { "clampf", (void *)&SC_clampf,
832 "float", "(float, float, float)" },
833 { "distf2", (void *)&SC_distf2,
834 "float", "(float, float, float, float)" },
835 { "distf3", (void *)&SC_distf3,
836 "float", "(float, float, float, float, float, float)" },
837 { "magf2", (void *)&SC_magf2,
838 "float", "(float, float)" },
839 { "magf3", (void *)&SC_magf3,
840 "float", "(float, float, float)" },
841 { "radf", (void *)&SC_radf,
842 "float", "(float)" },
843 { "degf", (void *)&SC_degf,
844 "float", "(float)" },
845 { "lerpf", (void *)&SC_lerpf,
846 "float", "(float, float, float)" },
847 { "normf", (void *)&SC_normf,
848 "float", "(float, float, float)" },
849 { "mapf", (void *)&SC_mapf,
850 "float", "(float, float, float, float, float)" },
Romain Guyecc7ca02009-08-03 21:12:51 -0700851 { "noisef", (void *)&SC_noisef,
852 "float", "(float)" },
853 { "noisef2", (void *)&SC_noisef2,
854 "float", "(float, float)" },
855 { "noisef3", (void *)&SC_noisef3,
856 "float", "(float, float, float)" },
857 { "turbulencef2", (void *)&SC_turbulencef2,
858 "float", "(float, float, float)" },
859 { "turbulencef3", (void *)&SC_turbulencef3,
860 "float", "(float, float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700861
Romain Guy584a3752009-07-30 18:45:01 -0700862 // time
863 { "second", (void *)&SC_second,
864 "int", "()" },
865 { "minute", (void *)&SC_minute,
866 "int", "()" },
867 { "hour", (void *)&SC_hour,
868 "int", "()" },
Romain Guy8839ca52009-07-31 11:20:59 -0700869 { "day", (void *)&SC_day,
870 "int", "()" },
871 { "month", (void *)&SC_month,
872 "int", "()" },
873 { "year", (void *)&SC_year,
874 "int", "()" },
Romain Guy584a3752009-07-30 18:45:01 -0700875
Jason Samsc97bb882009-07-20 14:31:06 -0700876 // matrix
877 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
878 "void", "(float *mat)" },
879 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
880 "void", "(float *mat, float *f)" },
881 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
882 "void", "(float *mat, float *newmat)" },
883 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
884 "void", "(float *mat, float rot, float x, float y, float z)" },
885 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
886 "void", "(float *mat, float x, float y, float z)" },
887 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
888 "void", "(float *mat, float x, float y, float z)" },
889 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
890 "void", "(float *mat, float *lhs, float *rhs)" },
891 { "matrixMultiply", (void *)&SC_matrixMultiply,
892 "void", "(float *mat, float *rhs)" },
893 { "matrixRotate", (void *)&SC_matrixRotate,
894 "void", "(float *mat, float rot, float x, float y, float z)" },
895 { "matrixScale", (void *)&SC_matrixScale,
896 "void", "(float *mat, float x, float y, float z)" },
897 { "matrixTranslate", (void *)&SC_matrixTranslate,
898 "void", "(float *mat, float x, float y, float z)" },
899
900 // context
901 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
902 "void", "(int)" },
903 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
904 "void", "(int)" },
Jason Samsee411122009-07-21 12:20:54 -0700905 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
906 "void", "(int)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700907 { "bindSampler", (void *)&SC_bindSampler,
908 "void", "(int, int, int)" },
909 { "bindTexture", (void *)&SC_bindTexture,
910 "void", "(int, int, int)" },
911
Jason Samsb0ec1b42009-07-28 12:02:16 -0700912 // vp
Jason Samsfaf15202009-07-29 20:55:44 -0700913 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsb0ec1b42009-07-28 12:02:16 -0700914 "void", "(void *)" },
Jason Samsfaf15202009-07-29 20:55:44 -0700915 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsb0ec1b42009-07-28 12:02:16 -0700916 "void", "(void *)" },
917
918
919
Jason Samsc97bb882009-07-20 14:31:06 -0700920 // drawing
Jason Sams6f5c61c2009-07-28 17:20:11 -0700921 { "drawRect", (void *)&SC_drawRect,
922 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700923 { "drawQuad", (void *)&SC_drawQuad,
924 "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 -0700925 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords,
926 "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 Samsc97bb882009-07-20 14:31:06 -0700927 { "drawTriangleArray", (void *)&SC_drawTriangleArray,
928 "void", "(int ialloc, int count)" },
929 { "drawTriangleMesh", (void *)&SC_drawTriangleMesh,
930 "void", "(int mesh)" },
931 { "drawTriangleMeshRange", (void *)&SC_drawTriangleMeshRange,
932 "void", "(int mesh, int start, int count)" },
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700933 { "drawLine", (void *)&SC_drawLine,
934 "void", "(float x1, float y1, float z1, float x2, float y2, float z2)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700935
936
937 // misc
938 { "pfClearColor", (void *)&SC_ClearColor,
939 "void", "(float, float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700940 { "color", (void *)&SC_color,
941 "void", "(float, float, float, float)" },
Romain Guya32d1002009-07-31 15:33:59 -0700942 { "hsb", (void *)&SC_hsb,
943 "void", "(float, float, float, float)" },
Romain Guyb62627e2009-08-06 22:52:13 -0700944 { "ambient", (void *)&SC_ambient,
945 "void", "(float, float, float, float)" },
946 { "diffuse", (void *)&SC_diffuse,
947 "void", "(float, float, float, float)" },
948 { "specular", (void *)&SC_specular,
949 "void", "(float, float, float, float)" },
950 { "emission", (void *)&SC_emission,
951 "void", "(float, float, float, float)" },
952 { "shininess", (void *)&SC_shininess,
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700953 "void", "(float)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700954
Jason Samsb0ec1b42009-07-28 12:02:16 -0700955 { "uploadToTexture", (void *)&SC_uploadToTexture,
956 "void", "(int, int)" },
957
958
959 { "debugF", (void *)&SC_debugF,
960 "void", "(void *, float)" },
961 { "debugI32", (void *)&SC_debugI32,
962 "void", "(void *, int)" },
963
964
Jason Samsc97bb882009-07-20 14:31:06 -0700965 { NULL, NULL, NULL, NULL }
966};
967
968const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
969{
970 ScriptCState::SymbolTable_t *syms = gSyms;
971
972 while (syms->mPtr) {
973 if (!strcmp(syms->mName, sym)) {
974 return syms;
975 }
976 syms++;
977 }
978 return NULL;
979}
980
981void ScriptCState::appendDecls(String8 *str)
982{
983 ScriptCState::SymbolTable_t *syms = gSyms;
984 while (syms->mPtr) {
985 str->append(syms->mRet);
986 str->append(" ");
987 str->append(syms->mName);
988 str->append(syms->mParam);
989 str->append(";\n");
990 syms++;
991 }
992}
993
994