blob: ecd8373667bd22c828c5b3b04e3e9cdeadfc2bed [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);
Jason Samse5ffb872009-08-09 17:01:55 -070092
Romain Guy48b7edc2009-08-06 22:52:13 -070093 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
94 glBufferData(GL_ELEMENT_ARRAY_BUFFER, tm->mIndexDataSize, tm->mIndexData, GL_STATIC_DRAW);
Jason Samse5ffb872009-08-09 17:01:55 -070095 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy48b7edc2009-08-06 22:52:13 -070096}
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{
Jason Samse5ffb872009-08-09 17:01:55 -0700192 return a > b ? a : b;
Romain Guy39dbc802009-07-31 11:20:59 -0700193}
194
195static float SC_minf(float a, float b)
196{
Jason Samse5ffb872009-08-09 17:01:55 -0700197 return a < b ? a : b;
Romain Guy39dbc802009-07-31 11:20:59 -0700198}
199
200static float SC_sqrf(float v)
201{
Jason Samse5ffb872009-08-09 17:01:55 -0700202 return v * v;
Romain Guy39dbc802009-07-31 11:20:59 -0700203}
204
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700205static int SC_sqr(int v)
206{
207 return v * v;
208}
209
Romain Guy39dbc802009-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;
Jason Samse5ffb872009-08-09 17:01:55 -0700214 return sqrtf(x * x + y * y);
Romain Guy39dbc802009-07-31 11:20:59 -0700215}
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;
Jason Samse5ffb872009-08-09 17:01:55 -0700222 return sqrtf(x * x + y * y + z * z);
Romain Guy39dbc802009-07-31 11:20:59 -0700223}
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{
Jason Samse5ffb872009-08-09 17:01:55 -0700237 return degrees * DEG_TO_RAD;
Romain Guy39dbc802009-07-31 11:20:59 -0700238}
239
240static float SC_degf(float radians)
241{
Jason Samse5ffb872009-08-09 17:01:55 -0700242 return radians * RAD_TO_DEG;
Romain Guy39dbc802009-07-31 11:20:59 -0700243}
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 Samse45ac6e2009-07-20 14:31:06 -0700259
Romain Guy98e10fd2009-07-30 18:45:01 -0700260//////////////////////////////////////////////////////////////////////////////
261// Time routines
262//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700263
Romain Guy98e10fd2009-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();
Jason Samse5ffb872009-08-09 17:01:55 -0700285
Romain Guy98e10fd2009-07-30 18:45:01 -0700286 time_t rawtime;
287 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700288
Romain Guy98e10fd2009-07-30 18:45:01 -0700289 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 }
Jason Samse5ffb872009-08-09 17:01:55 -0700298}
Romain Guy98e10fd2009-07-30 18:45:01 -0700299
Romain Guy39dbc802009-07-31 11:20:59 -0700300static uint32_t SC_hour()
Romain Guy98e10fd2009-07-30 18:45:01 -0700301{
302 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700303
Romain Guy98e10fd2009-07-30 18:45:01 -0700304 time_t rawtime;
305 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700306
Romain Guy98e10fd2009-07-30 18:45:01 -0700307 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 Guy39dbc802009-07-31 11:20:59 -0700316}
317
318static uint32_t SC_day()
319{
320 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700321
Romain Guy39dbc802009-07-31 11:20:59 -0700322 time_t rawtime;
323 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700324
Romain Guy39dbc802009-07-31 11:20:59 -0700325 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 }
Jason Samse5ffb872009-08-09 17:01:55 -0700334}
Jason Samse45ac6e2009-07-20 14:31:06 -0700335
Romain Guy39dbc802009-07-31 11:20:59 -0700336static uint32_t SC_month()
337{
338 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700339
Romain Guy39dbc802009-07-31 11:20:59 -0700340 time_t rawtime;
341 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700342
Romain Guy39dbc802009-07-31 11:20:59 -0700343 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 }
Jason Samse5ffb872009-08-09 17:01:55 -0700352}
Romain Guy39dbc802009-07-31 11:20:59 -0700353
354static uint32_t SC_year()
355{
356 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700357
Romain Guy39dbc802009-07-31 11:20:59 -0700358 time_t rawtime;
359 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700360
Romain Guy39dbc802009-07-31 11:20:59 -0700361 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 Samse45ac6e2009-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 Samsb5909ce2009-07-21 12:20:54 -0700485static void SC_bindProgramVertex(RsProgramVertex pv)
486{
487 GET_TLS();
488 rsi_ContextBindProgramVertex(rsc, pv);
489
490}
Jason Samse45ac6e2009-07-20 14:31:06 -0700491
492//////////////////////////////////////////////////////////////////////////////
Jason Samsc9d43db2009-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 Samse45ac6e2009-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 Guyd369e272009-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 Guyfcc1c2b2009-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 Samse45ac6e2009-07-20 14:31:06 -0700578{
579 GET_TLS();
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700580
Jason Samse45ac6e2009-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 Guyfcc1c2b2009-08-08 18:30:19 -0700586
Jason Samse45ac6e2009-07-20 14:31:06 -0700587 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700588 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
Jason Samse45ac6e2009-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 Guyfcc1c2b2009-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 Samse9f5c532009-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 Samse5ffb872009-08-09 17:01:55 -0700634static void SC_drawSimpleMesh(RsSimpleMesh vsm)
635{
636 GET_TLS();
637 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
638 rsc->setupCheck();
639 sm->render();
640}
641
642static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len)
643{
644 GET_TLS();
645 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
646 rsc->setupCheck();
647 sm->renderRange(start, len);
648}
649
650
Jason Samse45ac6e2009-07-20 14:31:06 -0700651//////////////////////////////////////////////////////////////////////////////
652//
653//////////////////////////////////////////////////////////////////////////////
654
Jason Samse45ac6e2009-07-20 14:31:06 -0700655static void SC_color(float r, float g, float b, float a)
656{
657 glColor4f(r, g, b, a);
658}
659
Romain Guy48b7edc2009-08-06 22:52:13 -0700660static void SC_ambient(float r, float g, float b, float a)
661{
662 GLfloat params[] = { r, g, b, a };
663 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, params);
664}
665
666static void SC_diffuse(float r, float g, float b, float a)
667{
668 GLfloat params[] = { r, g, b, a };
669 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, params);
670}
671
672static void SC_specular(float r, float g, float b, float a)
673{
674 GLfloat params[] = { r, g, b, a };
675 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, params);
676}
677
678static void SC_emission(float r, float g, float b, float a)
679{
680 GLfloat params[] = { r, g, b, a };
681 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, params);
682}
683
Romain Guyd369e272009-08-07 15:40:32 -0700684static void SC_shininess(float s)
Romain Guy48b7edc2009-08-06 22:52:13 -0700685{
Romain Guyd369e272009-08-07 15:40:32 -0700686 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, s);
Romain Guy48b7edc2009-08-06 22:52:13 -0700687}
688
Romain Guy9c59d022009-07-31 15:33:59 -0700689static void SC_hsb(float h, float s, float b, float a)
690{
691 float red = 0.0f;
692 float green = 0.0f;
693 float blue = 0.0f;
Jason Samse5ffb872009-08-09 17:01:55 -0700694
Romain Guy9c59d022009-07-31 15:33:59 -0700695 float x = h;
696 float y = s;
697 float z = b;
Jason Samse5ffb872009-08-09 17:01:55 -0700698
Romain Guy9c59d022009-07-31 15:33:59 -0700699 float hf = (x - (int) x) * 6.0f;
700 int ihf = (int) hf;
701 float f = hf - ihf;
702 float pv = z * (1.0f - y);
703 float qv = z * (1.0f - y * f);
704 float tv = z * (1.0f - y * (1.0f - f));
Jason Samse5ffb872009-08-09 17:01:55 -0700705
Romain Guy9c59d022009-07-31 15:33:59 -0700706 switch (ihf) {
707 case 0: // Red is the dominant color
708 red = z;
709 green = tv;
710 blue = pv;
711 break;
712 case 1: // Green is the dominant color
713 red = qv;
714 green = z;
715 blue = pv;
716 break;
717 case 2:
718 red = pv;
719 green = z;
720 blue = tv;
721 break;
722 case 3: // Blue is the dominant color
723 red = pv;
724 green = qv;
725 blue = z;
726 break;
727 case 4:
728 red = tv;
729 green = pv;
730 blue = z;
731 break;
732 case 5: // Red is the dominant color
733 red = z;
734 green = pv;
735 blue = qv;
736 break;
737 }
Jason Samse5ffb872009-08-09 17:01:55 -0700738
Romain Guy9c59d022009-07-31 15:33:59 -0700739 glColor4f(red, green, blue, a);
740}
741
Jason Samsc9d43db2009-07-28 12:02:16 -0700742static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samse45ac6e2009-07-20 14:31:06 -0700743{
744 GET_TLS();
745 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
746}
747
Jason Samse5ffb872009-08-09 17:01:55 -0700748static void SC_uploadToBufferObject(RsAllocation va)
749{
750 GET_TLS();
751 rsi_AllocationUploadToBufferObject(rsc, va);
752}
753
Jason Samse45ac6e2009-07-20 14:31:06 -0700754static void SC_ClearColor(float r, float g, float b, float a)
755{
756 //LOGE("c %f %f %f %f", r, g, b, a);
757 GET_TLS();
758 sc->mEnviroment.mClearColor[0] = r;
759 sc->mEnviroment.mClearColor[1] = g;
760 sc->mEnviroment.mClearColor[2] = b;
761 sc->mEnviroment.mClearColor[3] = a;
762}
763
Jason Samsc9d43db2009-07-28 12:02:16 -0700764static void SC_debugF(const char *s, float f)
765{
766 LOGE("%s %f", s, f);
767}
768
769static void SC_debugI32(const char *s, int32_t i)
770{
771 LOGE("%s %i", s, i);
772}
773
Jason Samse45ac6e2009-07-20 14:31:06 -0700774
775
776//////////////////////////////////////////////////////////////////////////////
777// Class implementation
778//////////////////////////////////////////////////////////////////////////////
779
780ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
781 // IO
782 { "loadI32", (void *)&SC_loadI32,
783 "int", "(int, int)" },
784 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
785 { "loadF", (void *)&SC_loadF,
786 "float", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -0700787 { "loadArrayF", (void *)&SC_loadArrayF,
Romain Guy06f7c932009-08-06 12:40:41 -0700788 "float*", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -0700789 { "loadArrayI32", (void *)&SC_loadArrayI32,
Romain Guy06f7c932009-08-06 12:40:41 -0700790 "int*", "(int, int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700791 { "loadVec4", (void *)&SC_loadVec4,
792 "void", "(int, int, float *)" },
793 { "loadMatrix", (void *)&SC_loadMatrix,
794 "void", "(int, int, float *)" },
795 { "storeI32", (void *)&SC_storeI32,
796 "void", "(int, int, int)" },
797 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
798 { "storeF", (void *)&SC_storeF,
799 "void", "(int, int, float)" },
800 { "storeVec4", (void *)&SC_storeVec4,
801 "void", "(int, int, float *)" },
802 { "storeMatrix", (void *)&SC_storeMatrix,
803 "void", "(int, int, float *)" },
Romain Guy48b7edc2009-08-06 22:52:13 -0700804 { "loadTriangleMeshVerticesF", (void *)&SC_loadTriangleMeshVerticesF,
805 "float*", "(int)" },
806 { "updateTriangleMesh", (void *)&SC_updateTriangleMesh,
807 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700808
809 // math
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700810 { "abs", (void *)&abs,
811 "int", "(int)" },
812 { "absf", (void *)&fabs,
813 "float", "(float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700814 { "sinf", (void *)&sinf,
815 "float", "(float)" },
816 { "cosf", (void *)&cosf,
817 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700818 { "asinf", (void *)&asinf,
819 "float", "(float)" },
820 { "acosf", (void *)&acosf,
821 "float", "(float)" },
822 { "atanf", (void *)&atanf,
823 "float", "(float)" },
824 { "atan2f", (void *)&atan2f,
Romain Guy9c59d022009-07-31 15:33:59 -0700825 "float", "(float, float)" },
Jason Samse9f5c532009-07-28 17:20:11 -0700826 { "fabsf", (void *)&fabsf,
Jason Samse45ac6e2009-07-20 14:31:06 -0700827 "float", "(float)" },
828 { "randf", (void *)&SC_randf,
829 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700830 { "randf2", (void *)&SC_randf2,
831 "float", "(float, float)" },
Jason Samsc9d43db2009-07-28 12:02:16 -0700832 { "floorf", (void *)&floorf,
833 "float", "(float)" },
834 { "ceilf", (void *)&ceilf,
835 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700836 { "expf", (void *)&expf,
837 "float", "(float)" },
838 { "logf", (void *)&logf,
839 "float", "(float)" },
840 { "powf", (void *)&powf,
841 "float", "(float, float)" },
842 { "maxf", (void *)&SC_maxf,
843 "float", "(float, float)" },
844 { "minf", (void *)&SC_minf,
845 "float", "(float, float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700846 { "sqrt", (void *)&sqrt,
847 "int", "(int)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700848 { "sqrtf", (void *)&sqrtf,
849 "float", "(float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700850 { "sqr", (void *)&SC_sqr,
851 "int", "(int)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700852 { "sqrf", (void *)&SC_sqrf,
853 "float", "(float)" },
854 { "clampf", (void *)&SC_clampf,
855 "float", "(float, float, float)" },
856 { "distf2", (void *)&SC_distf2,
857 "float", "(float, float, float, float)" },
858 { "distf3", (void *)&SC_distf3,
859 "float", "(float, float, float, float, float, float)" },
860 { "magf2", (void *)&SC_magf2,
861 "float", "(float, float)" },
862 { "magf3", (void *)&SC_magf3,
863 "float", "(float, float, float)" },
864 { "radf", (void *)&SC_radf,
865 "float", "(float)" },
866 { "degf", (void *)&SC_degf,
867 "float", "(float)" },
868 { "lerpf", (void *)&SC_lerpf,
869 "float", "(float, float, float)" },
870 { "normf", (void *)&SC_normf,
871 "float", "(float, float, float)" },
872 { "mapf", (void *)&SC_mapf,
873 "float", "(float, float, float, float, float)" },
Romain Guyb7f1a6d2009-08-03 21:12:51 -0700874 { "noisef", (void *)&SC_noisef,
875 "float", "(float)" },
876 { "noisef2", (void *)&SC_noisef2,
877 "float", "(float, float)" },
878 { "noisef3", (void *)&SC_noisef3,
879 "float", "(float, float, float)" },
880 { "turbulencef2", (void *)&SC_turbulencef2,
881 "float", "(float, float, float)" },
882 { "turbulencef3", (void *)&SC_turbulencef3,
883 "float", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700884
Romain Guy98e10fd2009-07-30 18:45:01 -0700885 // time
886 { "second", (void *)&SC_second,
887 "int", "()" },
888 { "minute", (void *)&SC_minute,
889 "int", "()" },
890 { "hour", (void *)&SC_hour,
891 "int", "()" },
Romain Guy39dbc802009-07-31 11:20:59 -0700892 { "day", (void *)&SC_day,
893 "int", "()" },
894 { "month", (void *)&SC_month,
895 "int", "()" },
896 { "year", (void *)&SC_year,
897 "int", "()" },
Romain Guy98e10fd2009-07-30 18:45:01 -0700898
Jason Samse45ac6e2009-07-20 14:31:06 -0700899 // matrix
900 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
901 "void", "(float *mat)" },
902 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
903 "void", "(float *mat, float *f)" },
904 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
905 "void", "(float *mat, float *newmat)" },
906 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
907 "void", "(float *mat, float rot, float x, float y, float z)" },
908 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
909 "void", "(float *mat, float x, float y, float z)" },
910 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
911 "void", "(float *mat, float x, float y, float z)" },
912 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
913 "void", "(float *mat, float *lhs, float *rhs)" },
914 { "matrixMultiply", (void *)&SC_matrixMultiply,
915 "void", "(float *mat, float *rhs)" },
916 { "matrixRotate", (void *)&SC_matrixRotate,
917 "void", "(float *mat, float rot, float x, float y, float z)" },
918 { "matrixScale", (void *)&SC_matrixScale,
919 "void", "(float *mat, float x, float y, float z)" },
920 { "matrixTranslate", (void *)&SC_matrixTranslate,
921 "void", "(float *mat, float x, float y, float z)" },
922
923 // context
924 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
925 "void", "(int)" },
926 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
927 "void", "(int)" },
Jason Samsb5909ce2009-07-21 12:20:54 -0700928 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
929 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700930 { "bindSampler", (void *)&SC_bindSampler,
931 "void", "(int, int, int)" },
932 { "bindTexture", (void *)&SC_bindTexture,
933 "void", "(int, int, int)" },
934
Jason Samsc9d43db2009-07-28 12:02:16 -0700935 // vp
Jason Sams50253db2009-07-29 20:55:44 -0700936 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -0700937 "void", "(void *)" },
Jason Sams50253db2009-07-29 20:55:44 -0700938 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -0700939 "void", "(void *)" },
940
941
942
Jason Samse45ac6e2009-07-20 14:31:06 -0700943 // drawing
Jason Samse9f5c532009-07-28 17:20:11 -0700944 { "drawRect", (void *)&SC_drawRect,
945 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700946 { "drawQuad", (void *)&SC_drawQuad,
947 "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 Guyfcc1c2b2009-08-08 18:30:19 -0700948 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords,
949 "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 Samse45ac6e2009-07-20 14:31:06 -0700950 { "drawTriangleArray", (void *)&SC_drawTriangleArray,
951 "void", "(int ialloc, int count)" },
952 { "drawTriangleMesh", (void *)&SC_drawTriangleMesh,
953 "void", "(int mesh)" },
954 { "drawTriangleMeshRange", (void *)&SC_drawTriangleMeshRange,
955 "void", "(int mesh, int start, int count)" },
Romain Guyd369e272009-08-07 15:40:32 -0700956 { "drawLine", (void *)&SC_drawLine,
957 "void", "(float x1, float y1, float z1, float x2, float y2, float z2)" },
Jason Samse5ffb872009-08-09 17:01:55 -0700958 { "drawSimpleMesh", (void *)&SC_drawSimpleMesh,
959 "void", "(int ism)" },
960 { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange,
961 "void", "(int ism, int start, int len)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700962
963
964 // misc
965 { "pfClearColor", (void *)&SC_ClearColor,
966 "void", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700967 { "color", (void *)&SC_color,
968 "void", "(float, float, float, float)" },
Romain Guy9c59d022009-07-31 15:33:59 -0700969 { "hsb", (void *)&SC_hsb,
970 "void", "(float, float, float, float)" },
Romain Guy48b7edc2009-08-06 22:52:13 -0700971 { "ambient", (void *)&SC_ambient,
972 "void", "(float, float, float, float)" },
973 { "diffuse", (void *)&SC_diffuse,
974 "void", "(float, float, float, float)" },
975 { "specular", (void *)&SC_specular,
976 "void", "(float, float, float, float)" },
977 { "emission", (void *)&SC_emission,
978 "void", "(float, float, float, float)" },
979 { "shininess", (void *)&SC_shininess,
Romain Guyd369e272009-08-07 15:40:32 -0700980 "void", "(float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700981
Jason Samsc9d43db2009-07-28 12:02:16 -0700982 { "uploadToTexture", (void *)&SC_uploadToTexture,
983 "void", "(int, int)" },
Jason Samse5ffb872009-08-09 17:01:55 -0700984 { "uploadToBufferObject", (void *)&SC_uploadToBufferObject,
985 "void", "(int)" },
Jason Samsc9d43db2009-07-28 12:02:16 -0700986
987
988 { "debugF", (void *)&SC_debugF,
989 "void", "(void *, float)" },
990 { "debugI32", (void *)&SC_debugI32,
991 "void", "(void *, int)" },
992
993
Jason Samse45ac6e2009-07-20 14:31:06 -0700994 { NULL, NULL, NULL, NULL }
995};
996
997const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
998{
999 ScriptCState::SymbolTable_t *syms = gSyms;
1000
1001 while (syms->mPtr) {
1002 if (!strcmp(syms->mName, sym)) {
1003 return syms;
1004 }
1005 syms++;
1006 }
1007 return NULL;
1008}
1009
1010void ScriptCState::appendDecls(String8 *str)
1011{
1012 ScriptCState::SymbolTable_t *syms = gSyms;
1013 while (syms->mPtr) {
1014 str->append(syms->mRet);
1015 str->append(" ");
1016 str->append(syms->mName);
1017 str->append(syms->mParam);
1018 str->append(";\n");
1019 syms++;
1020 }
1021}
1022
1023