blob: 813af5d54d5054a2a143c9cdac27e4185524a710 [file] [log] [blame]
Jason Samse45ac6e2009-07-20 14:31:06 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsContext.h"
18#include "rsScriptC.h"
19#include "rsMatrix.h"
Romain Guyb7f1a6d2009-08-03 21:12:51 -070020#include "rsNoise.h"
Jason Samse45ac6e2009-07-20 14:31:06 -070021
22#include "acc/acc.h"
23#include "utils/String8.h"
24
25#include <GLES/gl.h>
26#include <GLES/glext.h>
27
Romain Guy98e10fd2009-07-30 18:45:01 -070028#include <time.h>
29#include <cutils/tztime.h>
30
Jason Samse45ac6e2009-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 Guy06f7c932009-08-06 12:40:41 -070062static float* SC_loadArrayF(uint32_t bank, uint32_t offset)
Romain Guy36401002009-08-04 17:19:48 -070063{
64 GET_TLS();
65 void *vp = sc->mSlots[bank]->getPtr();
66 float *f = static_cast<float *>(vp);
Romain Guy06f7c932009-08-06 12:40:41 -070067 return f + offset;
Romain Guy36401002009-08-04 17:19:48 -070068}
69
Romain Guy06f7c932009-08-06 12:40:41 -070070static int32_t* SC_loadArrayI32(uint32_t bank, uint32_t offset)
Romain Guy36401002009-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 Guy06f7c932009-08-06 12:40:41 -070075 return i + offset;
Romain Guy36401002009-08-04 17:19:48 -070076}
77
Romain Guy48b7edc2009-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 Guy06f7c932009-08-06 12:40:41 -070097
Jason Samse45ac6e2009-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 Guy39dbc802009-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 Samse45ac6e2009-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 Guy39dbc802009-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
205static float SC_distf2(float x1, float y1, float x2, float y2)
206{
207 float x = x2 - x1;
208 float y = y2 - y1;
209 return sqrtf(x * x + y * y);
210}
211
212static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2)
213{
214 float x = x2 - x1;
215 float y = y2 - y1;
216 float z = z2 - z1;
217 return sqrtf(x * x + y * y + z * z);
218}
219
220static float SC_magf2(float a, float b)
221{
222 return sqrtf(a * a + b * b);
223}
224
225static float SC_magf3(float a, float b, float c)
226{
227 return sqrtf(a * a + b * b + c * c);
228}
229
230static float SC_radf(float degrees)
231{
232 return degrees * DEG_TO_RAD;
233}
234
235static float SC_degf(float radians)
236{
237 return radians * RAD_TO_DEG;
238}
239
240static float SC_lerpf(float start, float stop, float amount)
241{
242 return start + (stop - start) * amount;
243}
244
245static float SC_normf(float start, float stop, float value)
246{
247 return (value - start) / (stop - start);
248}
249
250static float SC_mapf(float minStart, float minStop, float maxStart, float maxStop, float value)
251{
252 return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
253}
Jason Samse45ac6e2009-07-20 14:31:06 -0700254
Romain Guy98e10fd2009-07-30 18:45:01 -0700255//////////////////////////////////////////////////////////////////////////////
256// Time routines
257//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700258
Romain Guy98e10fd2009-07-30 18:45:01 -0700259static uint32_t SC_second()
260{
261 GET_TLS();
262
263 time_t rawtime;
264 time(&rawtime);
265
266 if (sc->mEnviroment.mTimeZone) {
267 struct tm timeinfo;
268 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
269 return timeinfo.tm_sec;
270 } else {
271 struct tm *timeinfo;
272 timeinfo = localtime(&rawtime);
273 return timeinfo->tm_sec;
274 }
275}
276
277static uint32_t SC_minute()
278{
279 GET_TLS();
280
281 time_t rawtime;
282 time(&rawtime);
283
284 if (sc->mEnviroment.mTimeZone) {
285 struct tm timeinfo;
286 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
287 return timeinfo.tm_min;
288 } else {
289 struct tm *timeinfo;
290 timeinfo = localtime(&rawtime);
291 return timeinfo->tm_min;
292 }
293}
294
Romain Guy39dbc802009-07-31 11:20:59 -0700295static uint32_t SC_hour()
Romain Guy98e10fd2009-07-30 18:45:01 -0700296{
297 GET_TLS();
298
299 time_t rawtime;
300 time(&rawtime);
301
302 if (sc->mEnviroment.mTimeZone) {
303 struct tm timeinfo;
304 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
305 return timeinfo.tm_hour;
306 } else {
307 struct tm *timeinfo;
308 timeinfo = localtime(&rawtime);
309 return timeinfo->tm_hour;
310 }
Romain Guy39dbc802009-07-31 11:20:59 -0700311}
312
313static uint32_t SC_day()
314{
315 GET_TLS();
316
317 time_t rawtime;
318 time(&rawtime);
319
320 if (sc->mEnviroment.mTimeZone) {
321 struct tm timeinfo;
322 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
323 return timeinfo.tm_mday;
324 } else {
325 struct tm *timeinfo;
326 timeinfo = localtime(&rawtime);
327 return timeinfo->tm_mday;
328 }
Romain Guy98e10fd2009-07-30 18:45:01 -0700329}
Jason Samse45ac6e2009-07-20 14:31:06 -0700330
Romain Guy39dbc802009-07-31 11:20:59 -0700331static uint32_t SC_month()
332{
333 GET_TLS();
334
335 time_t rawtime;
336 time(&rawtime);
337
338 if (sc->mEnviroment.mTimeZone) {
339 struct tm timeinfo;
340 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
341 return timeinfo.tm_mon;
342 } else {
343 struct tm *timeinfo;
344 timeinfo = localtime(&rawtime);
345 return timeinfo->tm_mon;
346 }
347}
348
349static uint32_t SC_year()
350{
351 GET_TLS();
352
353 time_t rawtime;
354 time(&rawtime);
355
356 if (sc->mEnviroment.mTimeZone) {
357 struct tm timeinfo;
358 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
359 return timeinfo.tm_year;
360 } else {
361 struct tm *timeinfo;
362 timeinfo = localtime(&rawtime);
363 return timeinfo->tm_year;
364 }
365}
366
Jason Samse45ac6e2009-07-20 14:31:06 -0700367//////////////////////////////////////////////////////////////////////////////
368// Matrix routines
369//////////////////////////////////////////////////////////////////////////////
370
371
372static void SC_matrixLoadIdentity(rsc_Matrix *mat)
373{
374 Matrix *m = reinterpret_cast<Matrix *>(mat);
375 m->loadIdentity();
376}
377
378static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
379{
380 Matrix *m = reinterpret_cast<Matrix *>(mat);
381 m->load(f);
382}
383
384static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
385{
386 Matrix *m = reinterpret_cast<Matrix *>(mat);
387 m->load(reinterpret_cast<const Matrix *>(newmat));
388}
389
390static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
391{
392 Matrix *m = reinterpret_cast<Matrix *>(mat);
393 m->loadRotate(rot, x, y, z);
394}
395
396static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
397{
398 Matrix *m = reinterpret_cast<Matrix *>(mat);
399 m->loadScale(x, y, z);
400}
401
402static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
403{
404 Matrix *m = reinterpret_cast<Matrix *>(mat);
405 m->loadTranslate(x, y, z);
406}
407
408static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
409{
410 Matrix *m = reinterpret_cast<Matrix *>(mat);
411 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
412 reinterpret_cast<const Matrix *>(rhs));
413}
414
415static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
416{
417 Matrix *m = reinterpret_cast<Matrix *>(mat);
418 m->multiply(reinterpret_cast<const Matrix *>(rhs));
419}
420
421static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
422{
423 Matrix *m = reinterpret_cast<Matrix *>(mat);
424 m->rotate(rot, x, y, z);
425}
426
427static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
428{
429 Matrix *m = reinterpret_cast<Matrix *>(mat);
430 m->scale(x, y, z);
431}
432
433static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
434{
435 Matrix *m = reinterpret_cast<Matrix *>(mat);
436 m->translate(x, y, z);
437}
438
439
440
441
442//////////////////////////////////////////////////////////////////////////////
443// Context
444//////////////////////////////////////////////////////////////////////////////
445
446static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
447{
448 GET_TLS();
449 rsi_ProgramFragmentBindTexture(rsc,
450 static_cast<ProgramFragment *>(vpf),
451 slot,
452 static_cast<Allocation *>(va));
453
454}
455
456static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
457{
458 GET_TLS();
459 rsi_ProgramFragmentBindSampler(rsc,
460 static_cast<ProgramFragment *>(vpf),
461 slot,
462 static_cast<Sampler *>(vs));
463
464}
465
466static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs)
467{
468 GET_TLS();
469 rsi_ContextBindProgramFragmentStore(rsc, pfs);
470
471}
472
473static void SC_bindProgramFragment(RsProgramFragment pf)
474{
475 GET_TLS();
476 rsi_ContextBindProgramFragment(rsc, pf);
477
478}
479
Jason Samsb5909ce2009-07-21 12:20:54 -0700480static void SC_bindProgramVertex(RsProgramVertex pv)
481{
482 GET_TLS();
483 rsi_ContextBindProgramVertex(rsc, pv);
484
485}
Jason Samse45ac6e2009-07-20 14:31:06 -0700486
487//////////////////////////////////////////////////////////////////////////////
Jason Samsc9d43db2009-07-28 12:02:16 -0700488// VP
489//////////////////////////////////////////////////////////////////////////////
490
491static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
492{
493 GET_TLS();
494 rsc->getVertex()->setModelviewMatrix(m);
495}
496
497static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
498{
499 GET_TLS();
500 rsc->getVertex()->setTextureMatrix(m);
501}
502
503
504
505//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700506// Drawing
507//////////////////////////////////////////////////////////////////////////////
508
509static void SC_drawTriangleMesh(RsTriangleMesh mesh)
510{
511 GET_TLS();
512 rsi_TriangleMeshRender(rsc, mesh);
513}
514
515static void SC_drawTriangleMeshRange(RsTriangleMesh mesh, uint32_t start, uint32_t count)
516{
517 GET_TLS();
518 rsi_TriangleMeshRenderRange(rsc, mesh, start, count);
519}
520
521// Assumes (GL_FIXED) x,y,z (GL_UNSIGNED_BYTE)r,g,b,a
522static void SC_drawTriangleArray(int ialloc, uint32_t count)
523{
524 GET_TLS();
525 RsAllocation alloc = (RsAllocation)ialloc;
526
527 const Allocation *a = (const Allocation *)alloc;
528 const uint32_t *ptr = (const uint32_t *)a->getPtr();
529
530 rsc->setupCheck();
531
532 glBindBuffer(GL_ARRAY_BUFFER, 0);
533 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
534
535 glEnableClientState(GL_VERTEX_ARRAY);
536 glDisableClientState(GL_NORMAL_ARRAY);
537 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
538 glEnableClientState(GL_COLOR_ARRAY);
539
540 glVertexPointer(2, GL_FIXED, 12, ptr + 1);
541 //glTexCoordPointer(2, GL_FIXED, 24, ptr + 1);
542 glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
543
544 glDrawArrays(GL_TRIANGLES, 0, count * 3);
545}
546
Romain Guyd369e272009-08-07 15:40:32 -0700547static void SC_drawLine(float x1, float y1, float z1,
548 float x2, float y2, float z2)
549{
550 GET_TLS();
551 rsc->setupCheck();
552
553 float vtx[] = { x1, y1, z1, x2, y2, z2 };
554
555 glBindBuffer(GL_ARRAY_BUFFER, 0);
556 glEnableClientState(GL_VERTEX_ARRAY);
557 glVertexPointer(3, GL_FLOAT, 0, vtx);
558
559 glDisableClientState(GL_NORMAL_ARRAY);
560 glDisableClientState(GL_COLOR_ARRAY);
561
562 glDrawArrays(GL_LINES, 0, 2);
563}
564
Jason Samse45ac6e2009-07-20 14:31:06 -0700565static void SC_drawQuad(float x1, float y1, float z1,
566 float x2, float y2, float z2,
567 float x3, float y3, float z3,
568 float x4, float y4, float z4)
569{
570 GET_TLS();
571
572 //LOGE("Quad");
573 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
574 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
575 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
576 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
577
578 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
579 static const float tex[] = {0,1, 1,1, 1,0, 0,0};
580
581
582 rsc->setupCheck();
583
584 glBindBuffer(GL_ARRAY_BUFFER, 0);
585 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
586
587 glEnableClientState(GL_VERTEX_ARRAY);
588 glVertexPointer(3, GL_FLOAT, 0, vtx);
589
590 glClientActiveTexture(GL_TEXTURE0);
591 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
592 glTexCoordPointer(2, GL_FLOAT, 0, tex);
593 glClientActiveTexture(GL_TEXTURE1);
594 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
595 glTexCoordPointer(2, GL_FLOAT, 0, tex);
596 glClientActiveTexture(GL_TEXTURE0);
597
598 glDisableClientState(GL_NORMAL_ARRAY);
599 glDisableClientState(GL_COLOR_ARRAY);
600
601 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
602
603 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
604}
605
Jason Samse9f5c532009-07-28 17:20:11 -0700606static void SC_drawRect(float x1, float y1,
607 float x2, float y2, float z)
608{
609 SC_drawQuad(x1, y2, z,
610 x2, y2, z,
611 x2, y1, z,
612 x1, y1, z);
613}
614
Jason Samse45ac6e2009-07-20 14:31:06 -0700615//////////////////////////////////////////////////////////////////////////////
616//
617//////////////////////////////////////////////////////////////////////////////
618
Jason Samse45ac6e2009-07-20 14:31:06 -0700619static void SC_color(float r, float g, float b, float a)
620{
621 glColor4f(r, g, b, a);
622}
623
Romain Guy48b7edc2009-08-06 22:52:13 -0700624static void SC_ambient(float r, float g, float b, float a)
625{
626 GLfloat params[] = { r, g, b, a };
627 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, params);
628}
629
630static void SC_diffuse(float r, float g, float b, float a)
631{
632 GLfloat params[] = { r, g, b, a };
633 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, params);
634}
635
636static void SC_specular(float r, float g, float b, float a)
637{
638 GLfloat params[] = { r, g, b, a };
639 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, params);
640}
641
642static void SC_emission(float r, float g, float b, float a)
643{
644 GLfloat params[] = { r, g, b, a };
645 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, params);
646}
647
Romain Guyd369e272009-08-07 15:40:32 -0700648static void SC_shininess(float s)
Romain Guy48b7edc2009-08-06 22:52:13 -0700649{
Romain Guyd369e272009-08-07 15:40:32 -0700650 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, s);
Romain Guy48b7edc2009-08-06 22:52:13 -0700651}
652
Romain Guy9c59d022009-07-31 15:33:59 -0700653static void SC_hsb(float h, float s, float b, float a)
654{
655 float red = 0.0f;
656 float green = 0.0f;
657 float blue = 0.0f;
658
659 float x = h;
660 float y = s;
661 float z = b;
662
663 float hf = (x - (int) x) * 6.0f;
664 int ihf = (int) hf;
665 float f = hf - ihf;
666 float pv = z * (1.0f - y);
667 float qv = z * (1.0f - y * f);
668 float tv = z * (1.0f - y * (1.0f - f));
669
670 switch (ihf) {
671 case 0: // Red is the dominant color
672 red = z;
673 green = tv;
674 blue = pv;
675 break;
676 case 1: // Green is the dominant color
677 red = qv;
678 green = z;
679 blue = pv;
680 break;
681 case 2:
682 red = pv;
683 green = z;
684 blue = tv;
685 break;
686 case 3: // Blue is the dominant color
687 red = pv;
688 green = qv;
689 blue = z;
690 break;
691 case 4:
692 red = tv;
693 green = pv;
694 blue = z;
695 break;
696 case 5: // Red is the dominant color
697 red = z;
698 green = pv;
699 blue = qv;
700 break;
701 }
702
703 glColor4f(red, green, blue, a);
704}
705
Jason Samsc9d43db2009-07-28 12:02:16 -0700706static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samse45ac6e2009-07-20 14:31:06 -0700707{
708 GET_TLS();
709 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
710}
711
Jason Samse45ac6e2009-07-20 14:31:06 -0700712static void SC_ClearColor(float r, float g, float b, float a)
713{
714 //LOGE("c %f %f %f %f", r, g, b, a);
715 GET_TLS();
716 sc->mEnviroment.mClearColor[0] = r;
717 sc->mEnviroment.mClearColor[1] = g;
718 sc->mEnviroment.mClearColor[2] = b;
719 sc->mEnviroment.mClearColor[3] = a;
720}
721
Jason Samsc9d43db2009-07-28 12:02:16 -0700722static void SC_debugF(const char *s, float f)
723{
724 LOGE("%s %f", s, f);
725}
726
727static void SC_debugI32(const char *s, int32_t i)
728{
729 LOGE("%s %i", s, i);
730}
731
Jason Samse45ac6e2009-07-20 14:31:06 -0700732
733
734//////////////////////////////////////////////////////////////////////////////
735// Class implementation
736//////////////////////////////////////////////////////////////////////////////
737
738ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
739 // IO
740 { "loadI32", (void *)&SC_loadI32,
741 "int", "(int, int)" },
742 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
743 { "loadF", (void *)&SC_loadF,
744 "float", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -0700745 { "loadArrayF", (void *)&SC_loadArrayF,
Romain Guy06f7c932009-08-06 12:40:41 -0700746 "float*", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -0700747 { "loadArrayI32", (void *)&SC_loadArrayI32,
Romain Guy06f7c932009-08-06 12:40:41 -0700748 "int*", "(int, int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700749 { "loadVec4", (void *)&SC_loadVec4,
750 "void", "(int, int, float *)" },
751 { "loadMatrix", (void *)&SC_loadMatrix,
752 "void", "(int, int, float *)" },
753 { "storeI32", (void *)&SC_storeI32,
754 "void", "(int, int, int)" },
755 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
756 { "storeF", (void *)&SC_storeF,
757 "void", "(int, int, float)" },
758 { "storeVec4", (void *)&SC_storeVec4,
759 "void", "(int, int, float *)" },
760 { "storeMatrix", (void *)&SC_storeMatrix,
761 "void", "(int, int, float *)" },
Romain Guy48b7edc2009-08-06 22:52:13 -0700762 { "loadTriangleMeshVerticesF", (void *)&SC_loadTriangleMeshVerticesF,
763 "float*", "(int)" },
764 { "updateTriangleMesh", (void *)&SC_updateTriangleMesh,
765 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700766
767 // math
768 { "sinf", (void *)&sinf,
769 "float", "(float)" },
770 { "cosf", (void *)&cosf,
771 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700772 { "asinf", (void *)&asinf,
773 "float", "(float)" },
774 { "acosf", (void *)&acosf,
775 "float", "(float)" },
776 { "atanf", (void *)&atanf,
777 "float", "(float)" },
778 { "atan2f", (void *)&atan2f,
Romain Guy9c59d022009-07-31 15:33:59 -0700779 "float", "(float, float)" },
Jason Samse9f5c532009-07-28 17:20:11 -0700780 { "fabsf", (void *)&fabsf,
Jason Samse45ac6e2009-07-20 14:31:06 -0700781 "float", "(float)" },
782 { "randf", (void *)&SC_randf,
783 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700784 { "randf2", (void *)&SC_randf2,
785 "float", "(float, float)" },
Jason Samsc9d43db2009-07-28 12:02:16 -0700786 { "floorf", (void *)&floorf,
787 "float", "(float)" },
788 { "ceilf", (void *)&ceilf,
789 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700790 { "expf", (void *)&expf,
791 "float", "(float)" },
792 { "logf", (void *)&logf,
793 "float", "(float)" },
794 { "powf", (void *)&powf,
795 "float", "(float, float)" },
796 { "maxf", (void *)&SC_maxf,
797 "float", "(float, float)" },
798 { "minf", (void *)&SC_minf,
799 "float", "(float, float)" },
800 { "sqrtf", (void *)&sqrtf,
801 "float", "(float)" },
802 { "sqrf", (void *)&SC_sqrf,
803 "float", "(float)" },
804 { "clampf", (void *)&SC_clampf,
805 "float", "(float, float, float)" },
806 { "distf2", (void *)&SC_distf2,
807 "float", "(float, float, float, float)" },
808 { "distf3", (void *)&SC_distf3,
809 "float", "(float, float, float, float, float, float)" },
810 { "magf2", (void *)&SC_magf2,
811 "float", "(float, float)" },
812 { "magf3", (void *)&SC_magf3,
813 "float", "(float, float, float)" },
814 { "radf", (void *)&SC_radf,
815 "float", "(float)" },
816 { "degf", (void *)&SC_degf,
817 "float", "(float)" },
818 { "lerpf", (void *)&SC_lerpf,
819 "float", "(float, float, float)" },
820 { "normf", (void *)&SC_normf,
821 "float", "(float, float, float)" },
822 { "mapf", (void *)&SC_mapf,
823 "float", "(float, float, float, float, float)" },
Romain Guyb7f1a6d2009-08-03 21:12:51 -0700824 { "noisef", (void *)&SC_noisef,
825 "float", "(float)" },
826 { "noisef2", (void *)&SC_noisef2,
827 "float", "(float, float)" },
828 { "noisef3", (void *)&SC_noisef3,
829 "float", "(float, float, float)" },
830 { "turbulencef2", (void *)&SC_turbulencef2,
831 "float", "(float, float, float)" },
832 { "turbulencef3", (void *)&SC_turbulencef3,
833 "float", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700834
Romain Guy98e10fd2009-07-30 18:45:01 -0700835 // time
836 { "second", (void *)&SC_second,
837 "int", "()" },
838 { "minute", (void *)&SC_minute,
839 "int", "()" },
840 { "hour", (void *)&SC_hour,
841 "int", "()" },
Romain Guy39dbc802009-07-31 11:20:59 -0700842 { "day", (void *)&SC_day,
843 "int", "()" },
844 { "month", (void *)&SC_month,
845 "int", "()" },
846 { "year", (void *)&SC_year,
847 "int", "()" },
Romain Guy98e10fd2009-07-30 18:45:01 -0700848
Jason Samse45ac6e2009-07-20 14:31:06 -0700849 // matrix
850 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
851 "void", "(float *mat)" },
852 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
853 "void", "(float *mat, float *f)" },
854 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
855 "void", "(float *mat, float *newmat)" },
856 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
857 "void", "(float *mat, float rot, float x, float y, float z)" },
858 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
859 "void", "(float *mat, float x, float y, float z)" },
860 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
861 "void", "(float *mat, float x, float y, float z)" },
862 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
863 "void", "(float *mat, float *lhs, float *rhs)" },
864 { "matrixMultiply", (void *)&SC_matrixMultiply,
865 "void", "(float *mat, float *rhs)" },
866 { "matrixRotate", (void *)&SC_matrixRotate,
867 "void", "(float *mat, float rot, float x, float y, float z)" },
868 { "matrixScale", (void *)&SC_matrixScale,
869 "void", "(float *mat, float x, float y, float z)" },
870 { "matrixTranslate", (void *)&SC_matrixTranslate,
871 "void", "(float *mat, float x, float y, float z)" },
872
873 // context
874 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
875 "void", "(int)" },
876 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
877 "void", "(int)" },
Jason Samsb5909ce2009-07-21 12:20:54 -0700878 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
879 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700880 { "bindSampler", (void *)&SC_bindSampler,
881 "void", "(int, int, int)" },
882 { "bindTexture", (void *)&SC_bindTexture,
883 "void", "(int, int, int)" },
884
Jason Samsc9d43db2009-07-28 12:02:16 -0700885 // vp
Jason Sams50253db2009-07-29 20:55:44 -0700886 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -0700887 "void", "(void *)" },
Jason Sams50253db2009-07-29 20:55:44 -0700888 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -0700889 "void", "(void *)" },
890
891
892
Jason Samse45ac6e2009-07-20 14:31:06 -0700893 // drawing
Jason Samse9f5c532009-07-28 17:20:11 -0700894 { "drawRect", (void *)&SC_drawRect,
895 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700896 { "drawQuad", (void *)&SC_drawQuad,
897 "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)" },
898 { "drawTriangleArray", (void *)&SC_drawTriangleArray,
899 "void", "(int ialloc, int count)" },
900 { "drawTriangleMesh", (void *)&SC_drawTriangleMesh,
901 "void", "(int mesh)" },
902 { "drawTriangleMeshRange", (void *)&SC_drawTriangleMeshRange,
903 "void", "(int mesh, int start, int count)" },
Romain Guyd369e272009-08-07 15:40:32 -0700904 { "drawLine", (void *)&SC_drawLine,
905 "void", "(float x1, float y1, float z1, float x2, float y2, float z2)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700906
907
908 // misc
909 { "pfClearColor", (void *)&SC_ClearColor,
910 "void", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700911 { "color", (void *)&SC_color,
912 "void", "(float, float, float, float)" },
Romain Guy9c59d022009-07-31 15:33:59 -0700913 { "hsb", (void *)&SC_hsb,
914 "void", "(float, float, float, float)" },
Romain Guy48b7edc2009-08-06 22:52:13 -0700915 { "ambient", (void *)&SC_ambient,
916 "void", "(float, float, float, float)" },
917 { "diffuse", (void *)&SC_diffuse,
918 "void", "(float, float, float, float)" },
919 { "specular", (void *)&SC_specular,
920 "void", "(float, float, float, float)" },
921 { "emission", (void *)&SC_emission,
922 "void", "(float, float, float, float)" },
923 { "shininess", (void *)&SC_shininess,
Romain Guyd369e272009-08-07 15:40:32 -0700924 "void", "(float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700925
Jason Samsc9d43db2009-07-28 12:02:16 -0700926 { "uploadToTexture", (void *)&SC_uploadToTexture,
927 "void", "(int, int)" },
928
929
930 { "debugF", (void *)&SC_debugF,
931 "void", "(void *, float)" },
932 { "debugI32", (void *)&SC_debugI32,
933 "void", "(void *, int)" },
934
935
Jason Samse45ac6e2009-07-20 14:31:06 -0700936 { NULL, NULL, NULL, NULL }
937};
938
939const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
940{
941 ScriptCState::SymbolTable_t *syms = gSyms;
942
943 while (syms->mPtr) {
944 if (!strcmp(syms->mName, sym)) {
945 return syms;
946 }
947 syms++;
948 }
949 return NULL;
950}
951
952void ScriptCState::appendDecls(String8 *str)
953{
954 ScriptCState::SymbolTable_t *syms = gSyms;
955 while (syms->mPtr) {
956 str->append(syms->mRet);
957 str->append(" ");
958 str->append(syms->mName);
959 str->append(syms->mParam);
960 str->append(";\n");
961 syms++;
962 }
963}
964
965