blob: c13894b7ec1afdc6a4095a941cc5c57b1eedb781 [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 Guyecc7ca032009-08-03 21:12:51 -070020#include "rsNoise.h"
Jason Samsc97bb882009-07-20 14:31:06 -070021
22#include "acc/acc.h"
Joe Onorato3370ec92009-08-09 11:39:02 -070023#include "utils/Timers.h"
Jason Samsc97bb882009-07-20 14:31:06 -070024
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);
Jason Sams1bada8c2009-08-09 17:01:55 -070092
Romain Guyb62627e2009-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 Sams1bada8c2009-08-09 17:01:55 -070095 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyb62627e2009-08-06 22:52:13 -070096}
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
Romain Guya9d2d5e2009-08-09 17:04:54 -0700190static int SC_clamp(int amount, int low, int high)
191{
192 return amount < low ? low : (amount > high ? high : amount);
193}
194
Romain Guy8839ca52009-07-31 11:20:59 -0700195static float SC_maxf(float a, float b)
196{
Jason Sams1bada8c2009-08-09 17:01:55 -0700197 return a > b ? a : b;
Romain Guy8839ca52009-07-31 11:20:59 -0700198}
199
200static float SC_minf(float a, float b)
201{
Jason Sams1bada8c2009-08-09 17:01:55 -0700202 return a < b ? a : b;
Romain Guy8839ca52009-07-31 11:20:59 -0700203}
204
205static float SC_sqrf(float v)
206{
Jason Sams1bada8c2009-08-09 17:01:55 -0700207 return v * v;
Romain Guy8839ca52009-07-31 11:20:59 -0700208}
209
Romain Guy8f5c94b2009-08-08 18:30:19 -0700210static int SC_sqr(int v)
211{
212 return v * v;
213}
214
Romain Guy8839ca52009-07-31 11:20:59 -0700215static float SC_distf2(float x1, float y1, float x2, float y2)
216{
217 float x = x2 - x1;
218 float y = y2 - y1;
Jason Sams1bada8c2009-08-09 17:01:55 -0700219 return sqrtf(x * x + y * y);
Romain Guy8839ca52009-07-31 11:20:59 -0700220}
221
222static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2)
223{
224 float x = x2 - x1;
225 float y = y2 - y1;
226 float z = z2 - z1;
Jason Sams1bada8c2009-08-09 17:01:55 -0700227 return sqrtf(x * x + y * y + z * z);
Romain Guy8839ca52009-07-31 11:20:59 -0700228}
229
230static float SC_magf2(float a, float b)
231{
232 return sqrtf(a * a + b * b);
233}
234
235static float SC_magf3(float a, float b, float c)
236{
237 return sqrtf(a * a + b * b + c * c);
238}
239
240static float SC_radf(float degrees)
241{
Jason Sams1bada8c2009-08-09 17:01:55 -0700242 return degrees * DEG_TO_RAD;
Romain Guy8839ca52009-07-31 11:20:59 -0700243}
244
245static float SC_degf(float radians)
246{
Jason Sams1bada8c2009-08-09 17:01:55 -0700247 return radians * RAD_TO_DEG;
Romain Guy8839ca52009-07-31 11:20:59 -0700248}
249
250static float SC_lerpf(float start, float stop, float amount)
251{
252 return start + (stop - start) * amount;
253}
254
255static float SC_normf(float start, float stop, float value)
256{
257 return (value - start) / (stop - start);
258}
259
260static float SC_mapf(float minStart, float minStop, float maxStart, float maxStop, float value)
261{
262 return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
263}
Jason Samsc97bb882009-07-20 14:31:06 -0700264
Romain Guy584a3752009-07-30 18:45:01 -0700265//////////////////////////////////////////////////////////////////////////////
266// Time routines
267//////////////////////////////////////////////////////////////////////////////
Jason Samsc97bb882009-07-20 14:31:06 -0700268
Joe Onorato3370ec92009-08-09 11:39:02 -0700269static int32_t SC_second()
Romain Guy584a3752009-07-30 18:45:01 -0700270{
271 GET_TLS();
272
273 time_t rawtime;
274 time(&rawtime);
275
276 if (sc->mEnviroment.mTimeZone) {
277 struct tm timeinfo;
278 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
279 return timeinfo.tm_sec;
280 } else {
281 struct tm *timeinfo;
282 timeinfo = localtime(&rawtime);
283 return timeinfo->tm_sec;
284 }
285}
286
Joe Onorato3370ec92009-08-09 11:39:02 -0700287static int32_t SC_minute()
Romain Guy584a3752009-07-30 18:45:01 -0700288{
289 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700290
Romain Guy584a3752009-07-30 18:45:01 -0700291 time_t rawtime;
292 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700293
Romain Guy584a3752009-07-30 18:45:01 -0700294 if (sc->mEnviroment.mTimeZone) {
295 struct tm timeinfo;
296 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
297 return timeinfo.tm_min;
298 } else {
299 struct tm *timeinfo;
300 timeinfo = localtime(&rawtime);
301 return timeinfo->tm_min;
302 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700303}
Romain Guy584a3752009-07-30 18:45:01 -0700304
Joe Onorato3370ec92009-08-09 11:39:02 -0700305static int32_t SC_hour()
Romain Guy584a3752009-07-30 18:45:01 -0700306{
307 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700308
Romain Guy584a3752009-07-30 18:45:01 -0700309 time_t rawtime;
310 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700311
Romain Guy584a3752009-07-30 18:45:01 -0700312 if (sc->mEnviroment.mTimeZone) {
313 struct tm timeinfo;
314 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
315 return timeinfo.tm_hour;
316 } else {
317 struct tm *timeinfo;
318 timeinfo = localtime(&rawtime);
319 return timeinfo->tm_hour;
320 }
Romain Guy8839ca52009-07-31 11:20:59 -0700321}
322
Joe Onorato3370ec92009-08-09 11:39:02 -0700323static int32_t SC_day()
Romain Guy8839ca52009-07-31 11:20:59 -0700324{
325 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700326
Romain Guy8839ca52009-07-31 11:20:59 -0700327 time_t rawtime;
328 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700329
Romain Guy8839ca52009-07-31 11:20:59 -0700330 if (sc->mEnviroment.mTimeZone) {
331 struct tm timeinfo;
332 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
333 return timeinfo.tm_mday;
334 } else {
335 struct tm *timeinfo;
336 timeinfo = localtime(&rawtime);
337 return timeinfo->tm_mday;
338 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700339}
Jason Samsc97bb882009-07-20 14:31:06 -0700340
Joe Onorato3370ec92009-08-09 11:39:02 -0700341static int32_t SC_month()
Romain Guy8839ca52009-07-31 11:20:59 -0700342{
343 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700344
Romain Guy8839ca52009-07-31 11:20:59 -0700345 time_t rawtime;
346 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700347
Romain Guy8839ca52009-07-31 11:20:59 -0700348 if (sc->mEnviroment.mTimeZone) {
349 struct tm timeinfo;
350 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
351 return timeinfo.tm_mon;
352 } else {
353 struct tm *timeinfo;
354 timeinfo = localtime(&rawtime);
355 return timeinfo->tm_mon;
356 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700357}
Romain Guy8839ca52009-07-31 11:20:59 -0700358
Joe Onorato3370ec92009-08-09 11:39:02 -0700359static int32_t SC_year()
Romain Guy8839ca52009-07-31 11:20:59 -0700360{
361 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700362
Romain Guy8839ca52009-07-31 11:20:59 -0700363 time_t rawtime;
364 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700365
Romain Guy8839ca52009-07-31 11:20:59 -0700366 if (sc->mEnviroment.mTimeZone) {
367 struct tm timeinfo;
368 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
369 return timeinfo.tm_year;
370 } else {
371 struct tm *timeinfo;
372 timeinfo = localtime(&rawtime);
373 return timeinfo->tm_year;
374 }
375}
376
Joe Onorato3370ec92009-08-09 11:39:02 -0700377static int32_t SC_uptimeMillis()
378{
379 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
380}
381
382static int32_t SC_startTimeMillis()
383{
384 GET_TLS();
385 return sc->mEnviroment.mStartTimeMillis;
386}
387
388static int32_t SC_elapsedTimeMillis()
389{
390 GET_TLS();
391 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC))
392 - sc->mEnviroment.mStartTimeMillis;
393}
394
Jason Samsc97bb882009-07-20 14:31:06 -0700395//////////////////////////////////////////////////////////////////////////////
396// Matrix routines
397//////////////////////////////////////////////////////////////////////////////
398
399
400static void SC_matrixLoadIdentity(rsc_Matrix *mat)
401{
402 Matrix *m = reinterpret_cast<Matrix *>(mat);
403 m->loadIdentity();
404}
405
406static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
407{
408 Matrix *m = reinterpret_cast<Matrix *>(mat);
409 m->load(f);
410}
411
412static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
413{
414 Matrix *m = reinterpret_cast<Matrix *>(mat);
415 m->load(reinterpret_cast<const Matrix *>(newmat));
416}
417
418static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
419{
420 Matrix *m = reinterpret_cast<Matrix *>(mat);
421 m->loadRotate(rot, x, y, z);
422}
423
424static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
425{
426 Matrix *m = reinterpret_cast<Matrix *>(mat);
427 m->loadScale(x, y, z);
428}
429
430static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
431{
432 Matrix *m = reinterpret_cast<Matrix *>(mat);
433 m->loadTranslate(x, y, z);
434}
435
436static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
437{
438 Matrix *m = reinterpret_cast<Matrix *>(mat);
439 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
440 reinterpret_cast<const Matrix *>(rhs));
441}
442
443static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
444{
445 Matrix *m = reinterpret_cast<Matrix *>(mat);
446 m->multiply(reinterpret_cast<const Matrix *>(rhs));
447}
448
449static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
450{
451 Matrix *m = reinterpret_cast<Matrix *>(mat);
452 m->rotate(rot, x, y, z);
453}
454
455static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
456{
457 Matrix *m = reinterpret_cast<Matrix *>(mat);
458 m->scale(x, y, z);
459}
460
461static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
462{
463 Matrix *m = reinterpret_cast<Matrix *>(mat);
464 m->translate(x, y, z);
465}
466
467
468
469
470//////////////////////////////////////////////////////////////////////////////
471// Context
472//////////////////////////////////////////////////////////////////////////////
473
474static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
475{
476 GET_TLS();
477 rsi_ProgramFragmentBindTexture(rsc,
478 static_cast<ProgramFragment *>(vpf),
479 slot,
480 static_cast<Allocation *>(va));
481
482}
483
484static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
485{
486 GET_TLS();
487 rsi_ProgramFragmentBindSampler(rsc,
488 static_cast<ProgramFragment *>(vpf),
489 slot,
490 static_cast<Sampler *>(vs));
491
492}
493
494static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs)
495{
496 GET_TLS();
497 rsi_ContextBindProgramFragmentStore(rsc, pfs);
498
499}
500
501static void SC_bindProgramFragment(RsProgramFragment pf)
502{
503 GET_TLS();
504 rsi_ContextBindProgramFragment(rsc, pf);
505
506}
507
Jason Samsee411122009-07-21 12:20:54 -0700508static void SC_bindProgramVertex(RsProgramVertex pv)
509{
510 GET_TLS();
511 rsi_ContextBindProgramVertex(rsc, pv);
512
513}
Jason Samsc97bb882009-07-20 14:31:06 -0700514
515//////////////////////////////////////////////////////////////////////////////
Jason Samsb0ec1b42009-07-28 12:02:16 -0700516// VP
517//////////////////////////////////////////////////////////////////////////////
518
519static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
520{
521 GET_TLS();
522 rsc->getVertex()->setModelviewMatrix(m);
523}
524
525static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
526{
527 GET_TLS();
528 rsc->getVertex()->setTextureMatrix(m);
529}
530
531
532
533//////////////////////////////////////////////////////////////////////////////
Jason Samsc97bb882009-07-20 14:31:06 -0700534// Drawing
535//////////////////////////////////////////////////////////////////////////////
536
537static void SC_drawTriangleMesh(RsTriangleMesh mesh)
538{
539 GET_TLS();
540 rsi_TriangleMeshRender(rsc, mesh);
541}
542
543static void SC_drawTriangleMeshRange(RsTriangleMesh mesh, uint32_t start, uint32_t count)
544{
545 GET_TLS();
546 rsi_TriangleMeshRenderRange(rsc, mesh, start, count);
547}
548
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700549static void SC_drawLine(float x1, float y1, float z1,
550 float x2, float y2, float z2)
551{
552 GET_TLS();
553 rsc->setupCheck();
554
555 float vtx[] = { x1, y1, z1, x2, y2, z2 };
556
557 glBindBuffer(GL_ARRAY_BUFFER, 0);
558 glEnableClientState(GL_VERTEX_ARRAY);
559 glVertexPointer(3, GL_FLOAT, 0, vtx);
560
561 glDisableClientState(GL_NORMAL_ARRAY);
562 glDisableClientState(GL_COLOR_ARRAY);
563
564 glDrawArrays(GL_LINES, 0, 2);
565}
566
Romain Guy8f5c94b2009-08-08 18:30:19 -0700567static void SC_drawQuadTexCoords(float x1, float y1, float z1,
568 float u1, float v1,
569 float x2, float y2, float z2,
570 float u2, float v2,
571 float x3, float y3, float z3,
572 float u3, float v3,
573 float x4, float y4, float z4,
574 float u4, float v4)
Jason Samsc97bb882009-07-20 14:31:06 -0700575{
576 GET_TLS();
Jason Sams40a29e82009-08-10 14:55:26 -0700577
Jason Samsc97bb882009-07-20 14:31:06 -0700578 //LOGE("Quad");
579 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
580 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
581 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
582 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
Jason Sams40a29e82009-08-10 14:55:26 -0700583
Jason Samsc97bb882009-07-20 14:31:06 -0700584 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
Romain Guy8f5c94b2009-08-08 18:30:19 -0700585 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
Jason Samsc97bb882009-07-20 14:31:06 -0700586
587 rsc->setupCheck();
588
589 glBindBuffer(GL_ARRAY_BUFFER, 0);
590 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
591
592 glEnableClientState(GL_VERTEX_ARRAY);
593 glVertexPointer(3, GL_FLOAT, 0, vtx);
594
595 glClientActiveTexture(GL_TEXTURE0);
596 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
597 glTexCoordPointer(2, GL_FLOAT, 0, tex);
598 glClientActiveTexture(GL_TEXTURE1);
599 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
600 glTexCoordPointer(2, GL_FLOAT, 0, tex);
601 glClientActiveTexture(GL_TEXTURE0);
602
603 glDisableClientState(GL_NORMAL_ARRAY);
604 glDisableClientState(GL_COLOR_ARRAY);
605
606 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
607
608 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
609}
610
Romain Guy8f5c94b2009-08-08 18:30:19 -0700611static void SC_drawQuad(float x1, float y1, float z1,
612 float x2, float y2, float z2,
613 float x3, float y3, float z3,
614 float x4, float y4, float z4)
615{
616 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
617 x2, y2, z2, 1, 1,
618 x3, y3, z3, 1, 0,
619 x4, y4, z4, 0, 0);
620}
621
Jason Sams6f5c61c2009-07-28 17:20:11 -0700622static void SC_drawRect(float x1, float y1,
623 float x2, float y2, float z)
624{
625 SC_drawQuad(x1, y2, z,
626 x2, y2, z,
627 x2, y1, z,
628 x1, y1, z);
629}
630
Jason Sams1bada8c2009-08-09 17:01:55 -0700631static void SC_drawSimpleMesh(RsSimpleMesh vsm)
632{
633 GET_TLS();
634 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
635 rsc->setupCheck();
636 sm->render();
637}
638
639static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len)
640{
641 GET_TLS();
642 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
643 rsc->setupCheck();
644 sm->renderRange(start, len);
645}
646
647
Jason Samsc97bb882009-07-20 14:31:06 -0700648//////////////////////////////////////////////////////////////////////////////
649//
650//////////////////////////////////////////////////////////////////////////////
651
Jason Samsc97bb882009-07-20 14:31:06 -0700652static void SC_color(float r, float g, float b, float a)
653{
654 glColor4f(r, g, b, a);
655}
656
Romain Guyb62627e2009-08-06 22:52:13 -0700657static void SC_ambient(float r, float g, float b, float a)
658{
659 GLfloat params[] = { r, g, b, a };
660 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, params);
661}
662
663static void SC_diffuse(float r, float g, float b, float a)
664{
665 GLfloat params[] = { r, g, b, a };
666 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, params);
667}
668
669static void SC_specular(float r, float g, float b, float a)
670{
671 GLfloat params[] = { r, g, b, a };
672 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, params);
673}
674
675static void SC_emission(float r, float g, float b, float a)
676{
677 GLfloat params[] = { r, g, b, a };
678 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, params);
679}
680
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700681static void SC_shininess(float s)
Romain Guyb62627e2009-08-06 22:52:13 -0700682{
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700683 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, s);
Romain Guyb62627e2009-08-06 22:52:13 -0700684}
685
Romain Guya32d1002009-07-31 15:33:59 -0700686static void SC_hsb(float h, float s, float b, float a)
687{
688 float red = 0.0f;
689 float green = 0.0f;
690 float blue = 0.0f;
Jason Sams1bada8c2009-08-09 17:01:55 -0700691
Romain Guya32d1002009-07-31 15:33:59 -0700692 float x = h;
693 float y = s;
694 float z = b;
Jason Sams1bada8c2009-08-09 17:01:55 -0700695
Romain Guya32d1002009-07-31 15:33:59 -0700696 float hf = (x - (int) x) * 6.0f;
697 int ihf = (int) hf;
698 float f = hf - ihf;
699 float pv = z * (1.0f - y);
700 float qv = z * (1.0f - y * f);
701 float tv = z * (1.0f - y * (1.0f - f));
Jason Sams1bada8c2009-08-09 17:01:55 -0700702
Romain Guya32d1002009-07-31 15:33:59 -0700703 switch (ihf) {
704 case 0: // Red is the dominant color
705 red = z;
706 green = tv;
707 blue = pv;
708 break;
709 case 1: // Green is the dominant color
710 red = qv;
711 green = z;
712 blue = pv;
713 break;
714 case 2:
715 red = pv;
716 green = z;
717 blue = tv;
718 break;
719 case 3: // Blue is the dominant color
720 red = pv;
721 green = qv;
722 blue = z;
723 break;
724 case 4:
725 red = tv;
726 green = pv;
727 blue = z;
728 break;
729 case 5: // Red is the dominant color
730 red = z;
731 green = pv;
732 blue = qv;
733 break;
734 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700735
Romain Guya32d1002009-07-31 15:33:59 -0700736 glColor4f(red, green, blue, a);
737}
738
Jason Samsb0ec1b42009-07-28 12:02:16 -0700739static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samsc97bb882009-07-20 14:31:06 -0700740{
741 GET_TLS();
742 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
743}
744
Jason Sams1bada8c2009-08-09 17:01:55 -0700745static void SC_uploadToBufferObject(RsAllocation va)
746{
747 GET_TLS();
748 rsi_AllocationUploadToBufferObject(rsc, va);
749}
750
Jason Samsc97bb882009-07-20 14:31:06 -0700751static void SC_ClearColor(float r, float g, float b, float a)
752{
753 //LOGE("c %f %f %f %f", r, g, b, a);
754 GET_TLS();
755 sc->mEnviroment.mClearColor[0] = r;
756 sc->mEnviroment.mClearColor[1] = g;
757 sc->mEnviroment.mClearColor[2] = b;
758 sc->mEnviroment.mClearColor[3] = a;
759}
760
Jason Samsb0ec1b42009-07-28 12:02:16 -0700761static void SC_debugF(const char *s, float f)
762{
763 LOGE("%s %f", s, f);
764}
765
766static void SC_debugI32(const char *s, int32_t i)
767{
768 LOGE("%s %i", s, i);
769}
770
Jason Sams40a29e82009-08-10 14:55:26 -0700771static uint32_t SC_getWidth()
772{
773 GET_TLS();
774 return rsc->getWidth();
775}
Jason Samsc97bb882009-07-20 14:31:06 -0700776
Jason Sams40a29e82009-08-10 14:55:26 -0700777static uint32_t SC_getHeight()
778{
779 GET_TLS();
780 return rsc->getHeight();
781}
Jason Samsc97bb882009-07-20 14:31:06 -0700782
783//////////////////////////////////////////////////////////////////////////////
784// Class implementation
785//////////////////////////////////////////////////////////////////////////////
786
787ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
788 // IO
789 { "loadI32", (void *)&SC_loadI32,
790 "int", "(int, int)" },
791 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
792 { "loadF", (void *)&SC_loadF,
793 "float", "(int, int)" },
Romain Guya2136d62009-08-04 17:19:48 -0700794 { "loadArrayF", (void *)&SC_loadArrayF,
Romain Guyf8e136d2009-08-06 12:40:41 -0700795 "float*", "(int, int)" },
Romain Guya2136d62009-08-04 17:19:48 -0700796 { "loadArrayI32", (void *)&SC_loadArrayI32,
Romain Guyf8e136d2009-08-06 12:40:41 -0700797 "int*", "(int, int)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700798 { "loadVec4", (void *)&SC_loadVec4,
799 "void", "(int, int, float *)" },
800 { "loadMatrix", (void *)&SC_loadMatrix,
801 "void", "(int, int, float *)" },
802 { "storeI32", (void *)&SC_storeI32,
803 "void", "(int, int, int)" },
804 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
805 { "storeF", (void *)&SC_storeF,
806 "void", "(int, int, float)" },
807 { "storeVec4", (void *)&SC_storeVec4,
808 "void", "(int, int, float *)" },
809 { "storeMatrix", (void *)&SC_storeMatrix,
810 "void", "(int, int, float *)" },
Romain Guyb62627e2009-08-06 22:52:13 -0700811 { "loadTriangleMeshVerticesF", (void *)&SC_loadTriangleMeshVerticesF,
812 "float*", "(int)" },
813 { "updateTriangleMesh", (void *)&SC_updateTriangleMesh,
814 "void", "(int)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700815
816 // math
Romain Guya9d2d5e2009-08-09 17:04:54 -0700817 { "modf", (void *)&fmod,
818 "float", "(float, float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -0700819 { "abs", (void *)&abs,
820 "int", "(int)" },
821 { "absf", (void *)&fabs,
822 "float", "(float)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700823 { "sinf", (void *)&sinf,
824 "float", "(float)" },
825 { "cosf", (void *)&cosf,
826 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700827 { "asinf", (void *)&asinf,
828 "float", "(float)" },
829 { "acosf", (void *)&acosf,
830 "float", "(float)" },
831 { "atanf", (void *)&atanf,
832 "float", "(float)" },
833 { "atan2f", (void *)&atan2f,
Romain Guya32d1002009-07-31 15:33:59 -0700834 "float", "(float, float)" },
Jason Sams6f5c61c2009-07-28 17:20:11 -0700835 { "fabsf", (void *)&fabsf,
Jason Samsc97bb882009-07-20 14:31:06 -0700836 "float", "(float)" },
837 { "randf", (void *)&SC_randf,
838 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700839 { "randf2", (void *)&SC_randf2,
840 "float", "(float, float)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -0700841 { "floorf", (void *)&floorf,
842 "float", "(float)" },
843 { "ceilf", (void *)&ceilf,
844 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700845 { "expf", (void *)&expf,
846 "float", "(float)" },
847 { "logf", (void *)&logf,
848 "float", "(float)" },
849 { "powf", (void *)&powf,
850 "float", "(float, float)" },
851 { "maxf", (void *)&SC_maxf,
852 "float", "(float, float)" },
853 { "minf", (void *)&SC_minf,
854 "float", "(float, float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -0700855 { "sqrt", (void *)&sqrt,
856 "int", "(int)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700857 { "sqrtf", (void *)&sqrtf,
858 "float", "(float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -0700859 { "sqr", (void *)&SC_sqr,
860 "int", "(int)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700861 { "sqrf", (void *)&SC_sqrf,
862 "float", "(float)" },
Romain Guya9d2d5e2009-08-09 17:04:54 -0700863 { "clamp", (void *)&SC_clamp,
864 "int", "(int, int, int)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700865 { "clampf", (void *)&SC_clampf,
866 "float", "(float, float, float)" },
867 { "distf2", (void *)&SC_distf2,
868 "float", "(float, float, float, float)" },
869 { "distf3", (void *)&SC_distf3,
870 "float", "(float, float, float, float, float, float)" },
871 { "magf2", (void *)&SC_magf2,
872 "float", "(float, float)" },
873 { "magf3", (void *)&SC_magf3,
874 "float", "(float, float, float)" },
875 { "radf", (void *)&SC_radf,
876 "float", "(float)" },
877 { "degf", (void *)&SC_degf,
878 "float", "(float)" },
879 { "lerpf", (void *)&SC_lerpf,
880 "float", "(float, float, float)" },
881 { "normf", (void *)&SC_normf,
882 "float", "(float, float, float)" },
883 { "mapf", (void *)&SC_mapf,
884 "float", "(float, float, float, float, float)" },
Romain Guyecc7ca032009-08-03 21:12:51 -0700885 { "noisef", (void *)&SC_noisef,
886 "float", "(float)" },
887 { "noisef2", (void *)&SC_noisef2,
888 "float", "(float, float)" },
889 { "noisef3", (void *)&SC_noisef3,
890 "float", "(float, float, float)" },
891 { "turbulencef2", (void *)&SC_turbulencef2,
892 "float", "(float, float, float)" },
893 { "turbulencef3", (void *)&SC_turbulencef3,
894 "float", "(float, float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700895
Romain Guy584a3752009-07-30 18:45:01 -0700896 // time
897 { "second", (void *)&SC_second,
898 "int", "()" },
899 { "minute", (void *)&SC_minute,
900 "int", "()" },
901 { "hour", (void *)&SC_hour,
902 "int", "()" },
Romain Guy8839ca52009-07-31 11:20:59 -0700903 { "day", (void *)&SC_day,
904 "int", "()" },
905 { "month", (void *)&SC_month,
906 "int", "()" },
907 { "year", (void *)&SC_year,
908 "int", "()" },
Joe Onorato3370ec92009-08-09 11:39:02 -0700909 { "uptimeMillis", (void*)&SC_uptimeMillis,
910 "int", "()" }, // TODO: use long instead
911 { "startTimeMillis", (void*)&SC_startTimeMillis,
912 "int", "()" }, // TODO: use long instead
913 { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis,
914 "int", "()" }, // TODO: use long instead
Romain Guy584a3752009-07-30 18:45:01 -0700915
Jason Samsc97bb882009-07-20 14:31:06 -0700916 // matrix
917 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
918 "void", "(float *mat)" },
919 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
920 "void", "(float *mat, float *f)" },
921 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
922 "void", "(float *mat, float *newmat)" },
923 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
924 "void", "(float *mat, float rot, float x, float y, float z)" },
925 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
926 "void", "(float *mat, float x, float y, float z)" },
927 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
928 "void", "(float *mat, float x, float y, float z)" },
929 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
930 "void", "(float *mat, float *lhs, float *rhs)" },
931 { "matrixMultiply", (void *)&SC_matrixMultiply,
932 "void", "(float *mat, float *rhs)" },
933 { "matrixRotate", (void *)&SC_matrixRotate,
934 "void", "(float *mat, float rot, float x, float y, float z)" },
935 { "matrixScale", (void *)&SC_matrixScale,
936 "void", "(float *mat, float x, float y, float z)" },
937 { "matrixTranslate", (void *)&SC_matrixTranslate,
938 "void", "(float *mat, float x, float y, float z)" },
939
940 // context
941 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
942 "void", "(int)" },
943 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
944 "void", "(int)" },
Jason Samsee411122009-07-21 12:20:54 -0700945 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
946 "void", "(int)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700947 { "bindSampler", (void *)&SC_bindSampler,
948 "void", "(int, int, int)" },
949 { "bindTexture", (void *)&SC_bindTexture,
950 "void", "(int, int, int)" },
951
Jason Samsb0ec1b42009-07-28 12:02:16 -0700952 // vp
Jason Samsfaf15202009-07-29 20:55:44 -0700953 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsb0ec1b42009-07-28 12:02:16 -0700954 "void", "(void *)" },
Jason Samsfaf15202009-07-29 20:55:44 -0700955 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsb0ec1b42009-07-28 12:02:16 -0700956 "void", "(void *)" },
957
958
959
Jason Samsc97bb882009-07-20 14:31:06 -0700960 // drawing
Jason Sams6f5c61c2009-07-28 17:20:11 -0700961 { "drawRect", (void *)&SC_drawRect,
962 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700963 { "drawQuad", (void *)&SC_drawQuad,
964 "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 -0700965 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords,
966 "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 -0700967 { "drawTriangleMesh", (void *)&SC_drawTriangleMesh,
968 "void", "(int mesh)" },
969 { "drawTriangleMeshRange", (void *)&SC_drawTriangleMeshRange,
970 "void", "(int mesh, int start, int count)" },
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700971 { "drawLine", (void *)&SC_drawLine,
972 "void", "(float x1, float y1, float z1, float x2, float y2, float z2)" },
Jason Sams1bada8c2009-08-09 17:01:55 -0700973 { "drawSimpleMesh", (void *)&SC_drawSimpleMesh,
974 "void", "(int ism)" },
975 { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange,
976 "void", "(int ism, int start, int len)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700977
978
979 // misc
980 { "pfClearColor", (void *)&SC_ClearColor,
981 "void", "(float, float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700982 { "color", (void *)&SC_color,
983 "void", "(float, float, float, float)" },
Romain Guya32d1002009-07-31 15:33:59 -0700984 { "hsb", (void *)&SC_hsb,
985 "void", "(float, float, float, float)" },
Romain Guyb62627e2009-08-06 22:52:13 -0700986 { "ambient", (void *)&SC_ambient,
987 "void", "(float, float, float, float)" },
988 { "diffuse", (void *)&SC_diffuse,
989 "void", "(float, float, float, float)" },
990 { "specular", (void *)&SC_specular,
991 "void", "(float, float, float, float)" },
992 { "emission", (void *)&SC_emission,
993 "void", "(float, float, float, float)" },
994 { "shininess", (void *)&SC_shininess,
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700995 "void", "(float)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700996
Jason Samsb0ec1b42009-07-28 12:02:16 -0700997 { "uploadToTexture", (void *)&SC_uploadToTexture,
998 "void", "(int, int)" },
Jason Sams1bada8c2009-08-09 17:01:55 -0700999 { "uploadToBufferObject", (void *)&SC_uploadToBufferObject,
1000 "void", "(int)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001001
Jason Sams40a29e82009-08-10 14:55:26 -07001002 { "getWidth", (void *)&SC_getWidth,
1003 "int", "()" },
1004 { "getHeight", (void *)&SC_getHeight,
1005 "int", "()" },
1006
1007
Jason Samsb0ec1b42009-07-28 12:02:16 -07001008
1009 { "debugF", (void *)&SC_debugF,
1010 "void", "(void *, float)" },
1011 { "debugI32", (void *)&SC_debugI32,
1012 "void", "(void *, int)" },
1013
1014
Jason Samsc97bb882009-07-20 14:31:06 -07001015 { NULL, NULL, NULL, NULL }
1016};
1017
1018const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
1019{
1020 ScriptCState::SymbolTable_t *syms = gSyms;
1021
1022 while (syms->mPtr) {
1023 if (!strcmp(syms->mName, sym)) {
1024 return syms;
1025 }
1026 syms++;
1027 }
1028 return NULL;
1029}
1030
1031void ScriptCState::appendDecls(String8 *str)
1032{
1033 ScriptCState::SymbolTable_t *syms = gSyms;
1034 while (syms->mPtr) {
1035 str->append(syms->mRet);
1036 str->append(" ");
1037 str->append(syms->mName);
1038 str->append(syms->mParam);
1039 str->append(";\n");
1040 syms++;
1041 }
1042}
1043
1044