blob: 4ee112dc69ae1d2eb8d821604e0a5cc50e7c7642 [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"
23#include "utils/String8.h"
Joe Onorato3370ec92009-08-09 11:39:02 -070024#include "utils/Timers.h"
Jason Samsc97bb882009-07-20 14:31:06 -070025
26#include <GLES/gl.h>
27#include <GLES/glext.h>
28
Romain Guy584a3752009-07-30 18:45:01 -070029#include <time.h>
30#include <cutils/tztime.h>
31
Jason Samsc97bb882009-07-20 14:31:06 -070032using namespace android;
33using namespace android::renderscript;
34
35#define GET_TLS() Context::ScriptTLSStruct * tls = \
36 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
37 Context * rsc = tls->mContext; \
38 ScriptC * sc = (ScriptC *) tls->mScript
39
40
41//////////////////////////////////////////////////////////////////////////////
42// IO routines
43//////////////////////////////////////////////////////////////////////////////
44
45static float SC_loadF(uint32_t bank, uint32_t offset)
46{
47 GET_TLS();
48 const void *vp = sc->mSlots[bank]->getPtr();
49 const float *f = static_cast<const float *>(vp);
50 //LOGE("loadF %i %i = %f %x", bank, offset, f, ((int *)&f)[0]);
51 return f[offset];
52}
53
54static int32_t SC_loadI32(uint32_t bank, uint32_t offset)
55{
56 GET_TLS();
57 const void *vp = sc->mSlots[bank]->getPtr();
58 const int32_t *i = static_cast<const int32_t *>(vp);
59 //LOGE("loadI32 %i %i = %i", bank, offset, t);
60 return i[offset];
61}
62
Romain Guyf8e136d2009-08-06 12:40:41 -070063static float* SC_loadArrayF(uint32_t bank, uint32_t offset)
Romain Guya2136d62009-08-04 17:19:48 -070064{
65 GET_TLS();
66 void *vp = sc->mSlots[bank]->getPtr();
67 float *f = static_cast<float *>(vp);
Romain Guyf8e136d2009-08-06 12:40:41 -070068 return f + offset;
Romain Guya2136d62009-08-04 17:19:48 -070069}
70
Romain Guyf8e136d2009-08-06 12:40:41 -070071static int32_t* SC_loadArrayI32(uint32_t bank, uint32_t offset)
Romain Guya2136d62009-08-04 17:19:48 -070072{
73 GET_TLS();
74 void *vp = sc->mSlots[bank]->getPtr();
75 int32_t *i = static_cast<int32_t *>(vp);
Romain Guyf8e136d2009-08-06 12:40:41 -070076 return i + offset;
Romain Guya2136d62009-08-04 17:19:48 -070077}
78
Romain Guyb62627e2009-08-06 22:52:13 -070079static float* SC_loadTriangleMeshVerticesF(RsTriangleMesh mesh)
80{
81 TriangleMesh *tm = static_cast<TriangleMesh *>(mesh);
82 void *vp = tm->mVertexData;
83 float *f = static_cast<float *>(vp);
84 return f;
85}
86
87static void SC_updateTriangleMesh(RsTriangleMesh mesh)
88{
89 TriangleMesh *tm = static_cast<TriangleMesh *>(mesh);
90 glBindBuffer(GL_ARRAY_BUFFER, tm->mBufferObjects[0]);
91 glBufferData(GL_ARRAY_BUFFER, tm->mVertexDataSize, tm->mVertexData, GL_STATIC_DRAW);
92 glBindBuffer(GL_ARRAY_BUFFER, 0);
Jason Sams1bada8c2009-08-09 17:01:55 -070093
Romain Guyb62627e2009-08-06 22:52:13 -070094 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
95 glBufferData(GL_ELEMENT_ARRAY_BUFFER, tm->mIndexDataSize, tm->mIndexData, GL_STATIC_DRAW);
Jason Sams1bada8c2009-08-09 17:01:55 -070096 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyb62627e2009-08-06 22:52:13 -070097}
Romain Guyf8e136d2009-08-06 12:40:41 -070098
Jason Samsc97bb882009-07-20 14:31:06 -070099static uint32_t SC_loadU32(uint32_t bank, uint32_t offset)
100{
101 GET_TLS();
102 const void *vp = sc->mSlots[bank]->getPtr();
103 const uint32_t *i = static_cast<const uint32_t *>(vp);
104 return i[offset];
105}
106
107static void SC_loadVec4(uint32_t bank, uint32_t offset, rsc_Vector4 *v)
108{
109 GET_TLS();
110 const void *vp = sc->mSlots[bank]->getPtr();
111 const float *f = static_cast<const float *>(vp);
112 memcpy(v, &f[offset], sizeof(rsc_Vector4));
113}
114
115static void SC_loadMatrix(uint32_t bank, uint32_t offset, rsc_Matrix *m)
116{
117 GET_TLS();
118 const void *vp = sc->mSlots[bank]->getPtr();
119 const float *f = static_cast<const float *>(vp);
120 memcpy(m, &f[offset], sizeof(rsc_Matrix));
121}
122
123
124static void SC_storeF(uint32_t bank, uint32_t offset, float v)
125{
126 //LOGE("storeF %i %i %f", bank, offset, v);
127 GET_TLS();
128 void *vp = sc->mSlots[bank]->getPtr();
129 float *f = static_cast<float *>(vp);
130 f[offset] = v;
131}
132
133static void SC_storeI32(uint32_t bank, uint32_t offset, int32_t v)
134{
135 GET_TLS();
136 void *vp = sc->mSlots[bank]->getPtr();
137 int32_t *f = static_cast<int32_t *>(vp);
138 static_cast<int32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
139}
140
141static void SC_storeU32(uint32_t bank, uint32_t offset, uint32_t v)
142{
143 GET_TLS();
144 void *vp = sc->mSlots[bank]->getPtr();
145 uint32_t *f = static_cast<uint32_t *>(vp);
146 static_cast<uint32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
147}
148
149static void SC_storeVec4(uint32_t bank, uint32_t offset, const rsc_Vector4 *v)
150{
151 GET_TLS();
152 void *vp = sc->mSlots[bank]->getPtr();
153 float *f = static_cast<float *>(vp);
154 memcpy(&f[offset], v, sizeof(rsc_Vector4));
155}
156
157static void SC_storeMatrix(uint32_t bank, uint32_t offset, const rsc_Matrix *m)
158{
159 GET_TLS();
160 void *vp = sc->mSlots[bank]->getPtr();
161 float *f = static_cast<float *>(vp);
162 memcpy(&f[offset], m, sizeof(rsc_Matrix));
163}
164
165
166//////////////////////////////////////////////////////////////////////////////
167// Math routines
168//////////////////////////////////////////////////////////////////////////////
169
Romain Guy8839ca52009-07-31 11:20:59 -0700170#define PI 3.1415926f
171#define DEG_TO_RAD PI / 180.0f
172#define RAD_TO_DEG 180.0f / PI
173
Jason Samsc97bb882009-07-20 14:31:06 -0700174static float SC_randf(float max)
175{
176 float r = (float)rand();
177 return r / RAND_MAX * max;
178}
179
Romain Guy8839ca52009-07-31 11:20:59 -0700180static float SC_randf2(float min, float max)
181{
182 float r = (float)rand();
183 return r / RAND_MAX * (max - min) + min;
184}
185
186static float SC_clampf(float amount, float low, float high)
187{
188 return amount < low ? low : (amount > high ? high : amount);
189}
190
Romain Guya9d2d5e2009-08-09 17:04:54 -0700191static int SC_clamp(int amount, int low, int high)
192{
193 return amount < low ? low : (amount > high ? high : amount);
194}
195
Romain Guy8839ca52009-07-31 11:20:59 -0700196static float SC_maxf(float a, float b)
197{
Jason Sams1bada8c2009-08-09 17:01:55 -0700198 return a > b ? a : b;
Romain Guy8839ca52009-07-31 11:20:59 -0700199}
200
201static float SC_minf(float a, float b)
202{
Jason Sams1bada8c2009-08-09 17:01:55 -0700203 return a < b ? a : b;
Romain Guy8839ca52009-07-31 11:20:59 -0700204}
205
206static float SC_sqrf(float v)
207{
Jason Sams1bada8c2009-08-09 17:01:55 -0700208 return v * v;
Romain Guy8839ca52009-07-31 11:20:59 -0700209}
210
Romain Guy8f5c94b2009-08-08 18:30:19 -0700211static int SC_sqr(int v)
212{
213 return v * v;
214}
215
Romain Guy8839ca52009-07-31 11:20:59 -0700216static float SC_distf2(float x1, float y1, float x2, float y2)
217{
218 float x = x2 - x1;
219 float y = y2 - y1;
Jason Sams1bada8c2009-08-09 17:01:55 -0700220 return sqrtf(x * x + y * y);
Romain Guy8839ca52009-07-31 11:20:59 -0700221}
222
223static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2)
224{
225 float x = x2 - x1;
226 float y = y2 - y1;
227 float z = z2 - z1;
Jason Sams1bada8c2009-08-09 17:01:55 -0700228 return sqrtf(x * x + y * y + z * z);
Romain Guy8839ca52009-07-31 11:20:59 -0700229}
230
231static float SC_magf2(float a, float b)
232{
233 return sqrtf(a * a + b * b);
234}
235
236static float SC_magf3(float a, float b, float c)
237{
238 return sqrtf(a * a + b * b + c * c);
239}
240
241static float SC_radf(float degrees)
242{
Jason Sams1bada8c2009-08-09 17:01:55 -0700243 return degrees * DEG_TO_RAD;
Romain Guy8839ca52009-07-31 11:20:59 -0700244}
245
246static float SC_degf(float radians)
247{
Jason Sams1bada8c2009-08-09 17:01:55 -0700248 return radians * RAD_TO_DEG;
Romain Guy8839ca52009-07-31 11:20:59 -0700249}
250
251static float SC_lerpf(float start, float stop, float amount)
252{
253 return start + (stop - start) * amount;
254}
255
256static float SC_normf(float start, float stop, float value)
257{
258 return (value - start) / (stop - start);
259}
260
261static float SC_mapf(float minStart, float minStop, float maxStart, float maxStop, float value)
262{
263 return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
264}
Jason Samsc97bb882009-07-20 14:31:06 -0700265
Romain Guy584a3752009-07-30 18:45:01 -0700266//////////////////////////////////////////////////////////////////////////////
267// Time routines
268//////////////////////////////////////////////////////////////////////////////
Jason Samsc97bb882009-07-20 14:31:06 -0700269
Joe Onorato3370ec92009-08-09 11:39:02 -0700270static int32_t SC_second()
Romain Guy584a3752009-07-30 18:45:01 -0700271{
272 GET_TLS();
273
274 time_t rawtime;
275 time(&rawtime);
276
277 if (sc->mEnviroment.mTimeZone) {
278 struct tm timeinfo;
279 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
280 return timeinfo.tm_sec;
281 } else {
282 struct tm *timeinfo;
283 timeinfo = localtime(&rawtime);
284 return timeinfo->tm_sec;
285 }
286}
287
Joe Onorato3370ec92009-08-09 11:39:02 -0700288static int32_t SC_minute()
Romain Guy584a3752009-07-30 18:45:01 -0700289{
290 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700291
Romain Guy584a3752009-07-30 18:45:01 -0700292 time_t rawtime;
293 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700294
Romain Guy584a3752009-07-30 18:45:01 -0700295 if (sc->mEnviroment.mTimeZone) {
296 struct tm timeinfo;
297 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
298 return timeinfo.tm_min;
299 } else {
300 struct tm *timeinfo;
301 timeinfo = localtime(&rawtime);
302 return timeinfo->tm_min;
303 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700304}
Romain Guy584a3752009-07-30 18:45:01 -0700305
Joe Onorato3370ec92009-08-09 11:39:02 -0700306static int32_t SC_hour()
Romain Guy584a3752009-07-30 18:45:01 -0700307{
308 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700309
Romain Guy584a3752009-07-30 18:45:01 -0700310 time_t rawtime;
311 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700312
Romain Guy584a3752009-07-30 18:45:01 -0700313 if (sc->mEnviroment.mTimeZone) {
314 struct tm timeinfo;
315 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
316 return timeinfo.tm_hour;
317 } else {
318 struct tm *timeinfo;
319 timeinfo = localtime(&rawtime);
320 return timeinfo->tm_hour;
321 }
Romain Guy8839ca52009-07-31 11:20:59 -0700322}
323
Joe Onorato3370ec92009-08-09 11:39:02 -0700324static int32_t SC_day()
Romain Guy8839ca52009-07-31 11:20:59 -0700325{
326 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700327
Romain Guy8839ca52009-07-31 11:20:59 -0700328 time_t rawtime;
329 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700330
Romain Guy8839ca52009-07-31 11:20:59 -0700331 if (sc->mEnviroment.mTimeZone) {
332 struct tm timeinfo;
333 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
334 return timeinfo.tm_mday;
335 } else {
336 struct tm *timeinfo;
337 timeinfo = localtime(&rawtime);
338 return timeinfo->tm_mday;
339 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700340}
Jason Samsc97bb882009-07-20 14:31:06 -0700341
Joe Onorato3370ec92009-08-09 11:39:02 -0700342static int32_t SC_month()
Romain Guy8839ca52009-07-31 11:20:59 -0700343{
344 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700345
Romain Guy8839ca52009-07-31 11:20:59 -0700346 time_t rawtime;
347 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700348
Romain Guy8839ca52009-07-31 11:20:59 -0700349 if (sc->mEnviroment.mTimeZone) {
350 struct tm timeinfo;
351 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
352 return timeinfo.tm_mon;
353 } else {
354 struct tm *timeinfo;
355 timeinfo = localtime(&rawtime);
356 return timeinfo->tm_mon;
357 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700358}
Romain Guy8839ca52009-07-31 11:20:59 -0700359
Joe Onorato3370ec92009-08-09 11:39:02 -0700360static int32_t SC_year()
Romain Guy8839ca52009-07-31 11:20:59 -0700361{
362 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700363
Romain Guy8839ca52009-07-31 11:20:59 -0700364 time_t rawtime;
365 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700366
Romain Guy8839ca52009-07-31 11:20:59 -0700367 if (sc->mEnviroment.mTimeZone) {
368 struct tm timeinfo;
369 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
370 return timeinfo.tm_year;
371 } else {
372 struct tm *timeinfo;
373 timeinfo = localtime(&rawtime);
374 return timeinfo->tm_year;
375 }
376}
377
Joe Onorato3370ec92009-08-09 11:39:02 -0700378static int32_t SC_uptimeMillis()
379{
380 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
381}
382
383static int32_t SC_startTimeMillis()
384{
385 GET_TLS();
386 return sc->mEnviroment.mStartTimeMillis;
387}
388
389static int32_t SC_elapsedTimeMillis()
390{
391 GET_TLS();
392 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC))
393 - sc->mEnviroment.mStartTimeMillis;
394}
395
Jason Samsc97bb882009-07-20 14:31:06 -0700396//////////////////////////////////////////////////////////////////////////////
397// Matrix routines
398//////////////////////////////////////////////////////////////////////////////
399
400
401static void SC_matrixLoadIdentity(rsc_Matrix *mat)
402{
403 Matrix *m = reinterpret_cast<Matrix *>(mat);
404 m->loadIdentity();
405}
406
407static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
408{
409 Matrix *m = reinterpret_cast<Matrix *>(mat);
410 m->load(f);
411}
412
413static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
414{
415 Matrix *m = reinterpret_cast<Matrix *>(mat);
416 m->load(reinterpret_cast<const Matrix *>(newmat));
417}
418
419static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
420{
421 Matrix *m = reinterpret_cast<Matrix *>(mat);
422 m->loadRotate(rot, x, y, z);
423}
424
425static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
426{
427 Matrix *m = reinterpret_cast<Matrix *>(mat);
428 m->loadScale(x, y, z);
429}
430
431static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
432{
433 Matrix *m = reinterpret_cast<Matrix *>(mat);
434 m->loadTranslate(x, y, z);
435}
436
437static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
438{
439 Matrix *m = reinterpret_cast<Matrix *>(mat);
440 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
441 reinterpret_cast<const Matrix *>(rhs));
442}
443
444static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
445{
446 Matrix *m = reinterpret_cast<Matrix *>(mat);
447 m->multiply(reinterpret_cast<const Matrix *>(rhs));
448}
449
450static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
451{
452 Matrix *m = reinterpret_cast<Matrix *>(mat);
453 m->rotate(rot, x, y, z);
454}
455
456static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
457{
458 Matrix *m = reinterpret_cast<Matrix *>(mat);
459 m->scale(x, y, z);
460}
461
462static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
463{
464 Matrix *m = reinterpret_cast<Matrix *>(mat);
465 m->translate(x, y, z);
466}
467
468
469
470
471//////////////////////////////////////////////////////////////////////////////
472// Context
473//////////////////////////////////////////////////////////////////////////////
474
475static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
476{
477 GET_TLS();
478 rsi_ProgramFragmentBindTexture(rsc,
479 static_cast<ProgramFragment *>(vpf),
480 slot,
481 static_cast<Allocation *>(va));
482
483}
484
485static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
486{
487 GET_TLS();
488 rsi_ProgramFragmentBindSampler(rsc,
489 static_cast<ProgramFragment *>(vpf),
490 slot,
491 static_cast<Sampler *>(vs));
492
493}
494
495static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs)
496{
497 GET_TLS();
498 rsi_ContextBindProgramFragmentStore(rsc, pfs);
499
500}
501
502static void SC_bindProgramFragment(RsProgramFragment pf)
503{
504 GET_TLS();
505 rsi_ContextBindProgramFragment(rsc, pf);
506
507}
508
Jason Samsee411122009-07-21 12:20:54 -0700509static void SC_bindProgramVertex(RsProgramVertex pv)
510{
511 GET_TLS();
512 rsi_ContextBindProgramVertex(rsc, pv);
513
514}
Jason Samsc97bb882009-07-20 14:31:06 -0700515
516//////////////////////////////////////////////////////////////////////////////
Jason Samsb0ec1b42009-07-28 12:02:16 -0700517// VP
518//////////////////////////////////////////////////////////////////////////////
519
520static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
521{
522 GET_TLS();
523 rsc->getVertex()->setModelviewMatrix(m);
524}
525
526static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
527{
528 GET_TLS();
529 rsc->getVertex()->setTextureMatrix(m);
530}
531
532
533
534//////////////////////////////////////////////////////////////////////////////
Jason Samsc97bb882009-07-20 14:31:06 -0700535// Drawing
536//////////////////////////////////////////////////////////////////////////////
537
538static void SC_drawTriangleMesh(RsTriangleMesh mesh)
539{
540 GET_TLS();
541 rsi_TriangleMeshRender(rsc, mesh);
542}
543
544static void SC_drawTriangleMeshRange(RsTriangleMesh mesh, uint32_t start, uint32_t count)
545{
546 GET_TLS();
547 rsi_TriangleMeshRenderRange(rsc, mesh, start, count);
548}
549
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700550static void SC_drawLine(float x1, float y1, float z1,
551 float x2, float y2, float z2)
552{
553 GET_TLS();
554 rsc->setupCheck();
555
556 float vtx[] = { x1, y1, z1, x2, y2, z2 };
557
558 glBindBuffer(GL_ARRAY_BUFFER, 0);
559 glEnableClientState(GL_VERTEX_ARRAY);
560 glVertexPointer(3, GL_FLOAT, 0, vtx);
561
562 glDisableClientState(GL_NORMAL_ARRAY);
563 glDisableClientState(GL_COLOR_ARRAY);
564
565 glDrawArrays(GL_LINES, 0, 2);
566}
567
Romain Guy8f5c94b2009-08-08 18:30:19 -0700568static void SC_drawQuadTexCoords(float x1, float y1, float z1,
569 float u1, float v1,
570 float x2, float y2, float z2,
571 float u2, float v2,
572 float x3, float y3, float z3,
573 float u3, float v3,
574 float x4, float y4, float z4,
575 float u4, float v4)
Jason Samsc97bb882009-07-20 14:31:06 -0700576{
577 GET_TLS();
Jason Sams40a29e82009-08-10 14:55:26 -0700578
Jason Samsc97bb882009-07-20 14:31:06 -0700579 //LOGE("Quad");
580 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
581 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
582 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
583 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
Jason Sams40a29e82009-08-10 14:55:26 -0700584
Jason Samsc97bb882009-07-20 14:31:06 -0700585 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
Romain Guy8f5c94b2009-08-08 18:30:19 -0700586 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
Jason Samsc97bb882009-07-20 14:31:06 -0700587
588 rsc->setupCheck();
589
590 glBindBuffer(GL_ARRAY_BUFFER, 0);
591 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
592
593 glEnableClientState(GL_VERTEX_ARRAY);
594 glVertexPointer(3, GL_FLOAT, 0, vtx);
595
596 glClientActiveTexture(GL_TEXTURE0);
597 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
598 glTexCoordPointer(2, GL_FLOAT, 0, tex);
599 glClientActiveTexture(GL_TEXTURE1);
600 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
601 glTexCoordPointer(2, GL_FLOAT, 0, tex);
602 glClientActiveTexture(GL_TEXTURE0);
603
604 glDisableClientState(GL_NORMAL_ARRAY);
605 glDisableClientState(GL_COLOR_ARRAY);
606
607 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
608
609 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
610}
611
Romain Guy8f5c94b2009-08-08 18:30:19 -0700612static void SC_drawQuad(float x1, float y1, float z1,
613 float x2, float y2, float z2,
614 float x3, float y3, float z3,
615 float x4, float y4, float z4)
616{
617 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
618 x2, y2, z2, 1, 1,
619 x3, y3, z3, 1, 0,
620 x4, y4, z4, 0, 0);
621}
622
Jason Sams6f5c61c2009-07-28 17:20:11 -0700623static void SC_drawRect(float x1, float y1,
624 float x2, float y2, float z)
625{
626 SC_drawQuad(x1, y2, z,
627 x2, y2, z,
628 x2, y1, z,
629 x1, y1, z);
630}
631
Jason Sams1bada8c2009-08-09 17:01:55 -0700632static void SC_drawSimpleMesh(RsSimpleMesh vsm)
633{
634 GET_TLS();
635 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
636 rsc->setupCheck();
637 sm->render();
638}
639
640static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len)
641{
642 GET_TLS();
643 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
644 rsc->setupCheck();
645 sm->renderRange(start, len);
646}
647
648
Jason Samsc97bb882009-07-20 14:31:06 -0700649//////////////////////////////////////////////////////////////////////////////
650//
651//////////////////////////////////////////////////////////////////////////////
652
Jason Samsc97bb882009-07-20 14:31:06 -0700653static void SC_color(float r, float g, float b, float a)
654{
655 glColor4f(r, g, b, a);
656}
657
Romain Guyb62627e2009-08-06 22:52:13 -0700658static void SC_ambient(float r, float g, float b, float a)
659{
660 GLfloat params[] = { r, g, b, a };
661 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, params);
662}
663
664static void SC_diffuse(float r, float g, float b, float a)
665{
666 GLfloat params[] = { r, g, b, a };
667 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, params);
668}
669
670static void SC_specular(float r, float g, float b, float a)
671{
672 GLfloat params[] = { r, g, b, a };
673 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, params);
674}
675
676static void SC_emission(float r, float g, float b, float a)
677{
678 GLfloat params[] = { r, g, b, a };
679 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, params);
680}
681
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700682static void SC_shininess(float s)
Romain Guyb62627e2009-08-06 22:52:13 -0700683{
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700684 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, s);
Romain Guyb62627e2009-08-06 22:52:13 -0700685}
686
Romain Guya32d1002009-07-31 15:33:59 -0700687static void SC_hsb(float h, float s, float b, float a)
688{
689 float red = 0.0f;
690 float green = 0.0f;
691 float blue = 0.0f;
Jason Sams1bada8c2009-08-09 17:01:55 -0700692
Romain Guya32d1002009-07-31 15:33:59 -0700693 float x = h;
694 float y = s;
695 float z = b;
Jason Sams1bada8c2009-08-09 17:01:55 -0700696
Romain Guya32d1002009-07-31 15:33:59 -0700697 float hf = (x - (int) x) * 6.0f;
698 int ihf = (int) hf;
699 float f = hf - ihf;
700 float pv = z * (1.0f - y);
701 float qv = z * (1.0f - y * f);
702 float tv = z * (1.0f - y * (1.0f - f));
Jason Sams1bada8c2009-08-09 17:01:55 -0700703
Romain Guya32d1002009-07-31 15:33:59 -0700704 switch (ihf) {
705 case 0: // Red is the dominant color
706 red = z;
707 green = tv;
708 blue = pv;
709 break;
710 case 1: // Green is the dominant color
711 red = qv;
712 green = z;
713 blue = pv;
714 break;
715 case 2:
716 red = pv;
717 green = z;
718 blue = tv;
719 break;
720 case 3: // Blue is the dominant color
721 red = pv;
722 green = qv;
723 blue = z;
724 break;
725 case 4:
726 red = tv;
727 green = pv;
728 blue = z;
729 break;
730 case 5: // Red is the dominant color
731 red = z;
732 green = pv;
733 blue = qv;
734 break;
735 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700736
Romain Guya32d1002009-07-31 15:33:59 -0700737 glColor4f(red, green, blue, a);
738}
739
Jason Samsb0ec1b42009-07-28 12:02:16 -0700740static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samsc97bb882009-07-20 14:31:06 -0700741{
742 GET_TLS();
743 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
744}
745
Jason Sams1bada8c2009-08-09 17:01:55 -0700746static void SC_uploadToBufferObject(RsAllocation va)
747{
748 GET_TLS();
749 rsi_AllocationUploadToBufferObject(rsc, va);
750}
751
Jason Samsc97bb882009-07-20 14:31:06 -0700752static void SC_ClearColor(float r, float g, float b, float a)
753{
754 //LOGE("c %f %f %f %f", r, g, b, a);
755 GET_TLS();
756 sc->mEnviroment.mClearColor[0] = r;
757 sc->mEnviroment.mClearColor[1] = g;
758 sc->mEnviroment.mClearColor[2] = b;
759 sc->mEnviroment.mClearColor[3] = a;
760}
761
Jason Samsb0ec1b42009-07-28 12:02:16 -0700762static void SC_debugF(const char *s, float f)
763{
764 LOGE("%s %f", s, f);
765}
766
767static void SC_debugI32(const char *s, int32_t i)
768{
769 LOGE("%s %i", s, i);
770}
771
Jason Sams40a29e82009-08-10 14:55:26 -0700772static uint32_t SC_getWidth()
773{
774 GET_TLS();
775 return rsc->getWidth();
776}
Jason Samsc97bb882009-07-20 14:31:06 -0700777
Jason Sams40a29e82009-08-10 14:55:26 -0700778static uint32_t SC_getHeight()
779{
780 GET_TLS();
781 return rsc->getHeight();
782}
Jason Samsc97bb882009-07-20 14:31:06 -0700783
784//////////////////////////////////////////////////////////////////////////////
785// Class implementation
786//////////////////////////////////////////////////////////////////////////////
787
788ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
789 // IO
790 { "loadI32", (void *)&SC_loadI32,
791 "int", "(int, int)" },
792 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
793 { "loadF", (void *)&SC_loadF,
794 "float", "(int, int)" },
Romain Guya2136d62009-08-04 17:19:48 -0700795 { "loadArrayF", (void *)&SC_loadArrayF,
Romain Guyf8e136d2009-08-06 12:40:41 -0700796 "float*", "(int, int)" },
Romain Guya2136d62009-08-04 17:19:48 -0700797 { "loadArrayI32", (void *)&SC_loadArrayI32,
Romain Guyf8e136d2009-08-06 12:40:41 -0700798 "int*", "(int, int)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700799 { "loadVec4", (void *)&SC_loadVec4,
800 "void", "(int, int, float *)" },
801 { "loadMatrix", (void *)&SC_loadMatrix,
802 "void", "(int, int, float *)" },
803 { "storeI32", (void *)&SC_storeI32,
804 "void", "(int, int, int)" },
805 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
806 { "storeF", (void *)&SC_storeF,
807 "void", "(int, int, float)" },
808 { "storeVec4", (void *)&SC_storeVec4,
809 "void", "(int, int, float *)" },
810 { "storeMatrix", (void *)&SC_storeMatrix,
811 "void", "(int, int, float *)" },
Romain Guyb62627e2009-08-06 22:52:13 -0700812 { "loadTriangleMeshVerticesF", (void *)&SC_loadTriangleMeshVerticesF,
813 "float*", "(int)" },
814 { "updateTriangleMesh", (void *)&SC_updateTriangleMesh,
815 "void", "(int)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700816
817 // math
Romain Guya9d2d5e2009-08-09 17:04:54 -0700818 { "modf", (void *)&fmod,
819 "float", "(float, float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -0700820 { "abs", (void *)&abs,
821 "int", "(int)" },
822 { "absf", (void *)&fabs,
823 "float", "(float)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700824 { "sinf", (void *)&sinf,
825 "float", "(float)" },
826 { "cosf", (void *)&cosf,
827 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700828 { "asinf", (void *)&asinf,
829 "float", "(float)" },
830 { "acosf", (void *)&acosf,
831 "float", "(float)" },
832 { "atanf", (void *)&atanf,
833 "float", "(float)" },
834 { "atan2f", (void *)&atan2f,
Romain Guya32d1002009-07-31 15:33:59 -0700835 "float", "(float, float)" },
Jason Sams6f5c61c2009-07-28 17:20:11 -0700836 { "fabsf", (void *)&fabsf,
Jason Samsc97bb882009-07-20 14:31:06 -0700837 "float", "(float)" },
838 { "randf", (void *)&SC_randf,
839 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700840 { "randf2", (void *)&SC_randf2,
841 "float", "(float, float)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -0700842 { "floorf", (void *)&floorf,
843 "float", "(float)" },
844 { "ceilf", (void *)&ceilf,
845 "float", "(float)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700846 { "expf", (void *)&expf,
847 "float", "(float)" },
848 { "logf", (void *)&logf,
849 "float", "(float)" },
850 { "powf", (void *)&powf,
851 "float", "(float, float)" },
852 { "maxf", (void *)&SC_maxf,
853 "float", "(float, float)" },
854 { "minf", (void *)&SC_minf,
855 "float", "(float, float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -0700856 { "sqrt", (void *)&sqrt,
857 "int", "(int)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700858 { "sqrtf", (void *)&sqrtf,
859 "float", "(float)" },
Romain Guy8f5c94b2009-08-08 18:30:19 -0700860 { "sqr", (void *)&SC_sqr,
861 "int", "(int)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700862 { "sqrf", (void *)&SC_sqrf,
863 "float", "(float)" },
Romain Guya9d2d5e2009-08-09 17:04:54 -0700864 { "clamp", (void *)&SC_clamp,
865 "int", "(int, int, int)" },
Romain Guy8839ca52009-07-31 11:20:59 -0700866 { "clampf", (void *)&SC_clampf,
867 "float", "(float, float, float)" },
868 { "distf2", (void *)&SC_distf2,
869 "float", "(float, float, float, float)" },
870 { "distf3", (void *)&SC_distf3,
871 "float", "(float, float, float, float, float, float)" },
872 { "magf2", (void *)&SC_magf2,
873 "float", "(float, float)" },
874 { "magf3", (void *)&SC_magf3,
875 "float", "(float, float, float)" },
876 { "radf", (void *)&SC_radf,
877 "float", "(float)" },
878 { "degf", (void *)&SC_degf,
879 "float", "(float)" },
880 { "lerpf", (void *)&SC_lerpf,
881 "float", "(float, float, float)" },
882 { "normf", (void *)&SC_normf,
883 "float", "(float, float, float)" },
884 { "mapf", (void *)&SC_mapf,
885 "float", "(float, float, float, float, float)" },
Romain Guyecc7ca032009-08-03 21:12:51 -0700886 { "noisef", (void *)&SC_noisef,
887 "float", "(float)" },
888 { "noisef2", (void *)&SC_noisef2,
889 "float", "(float, float)" },
890 { "noisef3", (void *)&SC_noisef3,
891 "float", "(float, float, float)" },
892 { "turbulencef2", (void *)&SC_turbulencef2,
893 "float", "(float, float, float)" },
894 { "turbulencef3", (void *)&SC_turbulencef3,
895 "float", "(float, float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700896
Romain Guy584a3752009-07-30 18:45:01 -0700897 // time
898 { "second", (void *)&SC_second,
899 "int", "()" },
900 { "minute", (void *)&SC_minute,
901 "int", "()" },
902 { "hour", (void *)&SC_hour,
903 "int", "()" },
Romain Guy8839ca52009-07-31 11:20:59 -0700904 { "day", (void *)&SC_day,
905 "int", "()" },
906 { "month", (void *)&SC_month,
907 "int", "()" },
908 { "year", (void *)&SC_year,
909 "int", "()" },
Joe Onorato3370ec92009-08-09 11:39:02 -0700910 { "uptimeMillis", (void*)&SC_uptimeMillis,
911 "int", "()" }, // TODO: use long instead
912 { "startTimeMillis", (void*)&SC_startTimeMillis,
913 "int", "()" }, // TODO: use long instead
914 { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis,
915 "int", "()" }, // TODO: use long instead
Romain Guy584a3752009-07-30 18:45:01 -0700916
Jason Samsc97bb882009-07-20 14:31:06 -0700917 // matrix
918 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
919 "void", "(float *mat)" },
920 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
921 "void", "(float *mat, float *f)" },
922 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
923 "void", "(float *mat, float *newmat)" },
924 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
925 "void", "(float *mat, float rot, float x, float y, float z)" },
926 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
927 "void", "(float *mat, float x, float y, float z)" },
928 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
929 "void", "(float *mat, float x, float y, float z)" },
930 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
931 "void", "(float *mat, float *lhs, float *rhs)" },
932 { "matrixMultiply", (void *)&SC_matrixMultiply,
933 "void", "(float *mat, float *rhs)" },
934 { "matrixRotate", (void *)&SC_matrixRotate,
935 "void", "(float *mat, float rot, float x, float y, float z)" },
936 { "matrixScale", (void *)&SC_matrixScale,
937 "void", "(float *mat, float x, float y, float z)" },
938 { "matrixTranslate", (void *)&SC_matrixTranslate,
939 "void", "(float *mat, float x, float y, float z)" },
940
941 // context
942 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
943 "void", "(int)" },
944 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
945 "void", "(int)" },
Jason Samsee411122009-07-21 12:20:54 -0700946 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
947 "void", "(int)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700948 { "bindSampler", (void *)&SC_bindSampler,
949 "void", "(int, int, int)" },
950 { "bindTexture", (void *)&SC_bindTexture,
951 "void", "(int, int, int)" },
952
Jason Samsb0ec1b42009-07-28 12:02:16 -0700953 // vp
Jason Samsfaf15202009-07-29 20:55:44 -0700954 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsb0ec1b42009-07-28 12:02:16 -0700955 "void", "(void *)" },
Jason Samsfaf15202009-07-29 20:55:44 -0700956 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsb0ec1b42009-07-28 12:02:16 -0700957 "void", "(void *)" },
958
959
960
Jason Samsc97bb882009-07-20 14:31:06 -0700961 // drawing
Jason Sams6f5c61c2009-07-28 17:20:11 -0700962 { "drawRect", (void *)&SC_drawRect,
963 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700964 { "drawQuad", (void *)&SC_drawQuad,
965 "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 -0700966 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords,
967 "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 -0700968 { "drawTriangleMesh", (void *)&SC_drawTriangleMesh,
969 "void", "(int mesh)" },
970 { "drawTriangleMeshRange", (void *)&SC_drawTriangleMeshRange,
971 "void", "(int mesh, int start, int count)" },
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700972 { "drawLine", (void *)&SC_drawLine,
973 "void", "(float x1, float y1, float z1, float x2, float y2, float z2)" },
Jason Sams1bada8c2009-08-09 17:01:55 -0700974 { "drawSimpleMesh", (void *)&SC_drawSimpleMesh,
975 "void", "(int ism)" },
976 { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange,
977 "void", "(int ism, int start, int len)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700978
979
980 // misc
981 { "pfClearColor", (void *)&SC_ClearColor,
982 "void", "(float, float, float, float)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700983 { "color", (void *)&SC_color,
984 "void", "(float, float, float, float)" },
Romain Guya32d1002009-07-31 15:33:59 -0700985 { "hsb", (void *)&SC_hsb,
986 "void", "(float, float, float, float)" },
Romain Guyb62627e2009-08-06 22:52:13 -0700987 { "ambient", (void *)&SC_ambient,
988 "void", "(float, float, float, float)" },
989 { "diffuse", (void *)&SC_diffuse,
990 "void", "(float, float, float, float)" },
991 { "specular", (void *)&SC_specular,
992 "void", "(float, float, float, float)" },
993 { "emission", (void *)&SC_emission,
994 "void", "(float, float, float, float)" },
995 { "shininess", (void *)&SC_shininess,
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700996 "void", "(float)" },
Jason Samsc97bb882009-07-20 14:31:06 -0700997
Jason Samsb0ec1b42009-07-28 12:02:16 -0700998 { "uploadToTexture", (void *)&SC_uploadToTexture,
999 "void", "(int, int)" },
Jason Sams1bada8c2009-08-09 17:01:55 -07001000 { "uploadToBufferObject", (void *)&SC_uploadToBufferObject,
1001 "void", "(int)" },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001002
Jason Sams40a29e82009-08-10 14:55:26 -07001003 { "getWidth", (void *)&SC_getWidth,
1004 "int", "()" },
1005 { "getHeight", (void *)&SC_getHeight,
1006 "int", "()" },
1007
1008
Jason Samsb0ec1b42009-07-28 12:02:16 -07001009
1010 { "debugF", (void *)&SC_debugF,
1011 "void", "(void *, float)" },
1012 { "debugI32", (void *)&SC_debugI32,
1013 "void", "(void *, int)" },
1014
1015
Jason Samsc97bb882009-07-20 14:31:06 -07001016 { NULL, NULL, NULL, NULL }
1017};
1018
1019const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
1020{
1021 ScriptCState::SymbolTable_t *syms = gSyms;
1022
1023 while (syms->mPtr) {
1024 if (!strcmp(syms->mName, sym)) {
1025 return syms;
1026 }
1027 syms++;
1028 }
1029 return NULL;
1030}
1031
1032void ScriptCState::appendDecls(String8 *str)
1033{
1034 ScriptCState::SymbolTable_t *syms = gSyms;
1035 while (syms->mPtr) {
1036 str->append(syms->mRet);
1037 str->append(" ");
1038 str->append(syms->mName);
1039 str->append(syms->mParam);
1040 str->append(";\n");
1041 syms++;
1042 }
1043}
1044
1045