blob: b46e1cfd58634ccf0cbf5e174c1963c60de5f999 [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"
Joe Onorato9c4e4ca2009-08-09 11:39:02 -070024#include "utils/Timers.h"
Jason Samse45ac6e2009-07-20 14:31:06 -070025
26#include <GLES/gl.h>
27#include <GLES/glext.h>
28
Romain Guy98e10fd2009-07-30 18:45:01 -070029#include <time.h>
30#include <cutils/tztime.h>
31
Jason Samse45ac6e2009-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 Guy06f7c932009-08-06 12:40:41 -070063static float* SC_loadArrayF(uint32_t bank, uint32_t offset)
Romain Guy36401002009-08-04 17:19:48 -070064{
65 GET_TLS();
66 void *vp = sc->mSlots[bank]->getPtr();
67 float *f = static_cast<float *>(vp);
Romain Guy06f7c932009-08-06 12:40:41 -070068 return f + offset;
Romain Guy36401002009-08-04 17:19:48 -070069}
70
Romain Guy06f7c932009-08-06 12:40:41 -070071static int32_t* SC_loadArrayI32(uint32_t bank, uint32_t offset)
Romain Guy36401002009-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 Guy06f7c932009-08-06 12:40:41 -070076 return i + offset;
Romain Guy36401002009-08-04 17:19:48 -070077}
78
Romain Guy48b7edc2009-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 Samse5ffb872009-08-09 17:01:55 -070093
Romain Guy48b7edc2009-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 Samse5ffb872009-08-09 17:01:55 -070096 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy48b7edc2009-08-06 22:52:13 -070097}
Romain Guy06f7c932009-08-06 12:40:41 -070098
Jason Samse45ac6e2009-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 Guy39dbc802009-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 Samse45ac6e2009-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 Guy39dbc802009-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 Guy27162ab2009-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 Guy39dbc802009-07-31 11:20:59 -0700196static float SC_maxf(float a, float b)
197{
Jason Samse5ffb872009-08-09 17:01:55 -0700198 return a > b ? a : b;
Romain Guy39dbc802009-07-31 11:20:59 -0700199}
200
201static float SC_minf(float a, float b)
202{
Jason Samse5ffb872009-08-09 17:01:55 -0700203 return a < b ? a : b;
Romain Guy39dbc802009-07-31 11:20:59 -0700204}
205
206static float SC_sqrf(float v)
207{
Jason Samse5ffb872009-08-09 17:01:55 -0700208 return v * v;
Romain Guy39dbc802009-07-31 11:20:59 -0700209}
210
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700211static int SC_sqr(int v)
212{
213 return v * v;
214}
215
Romain Guy39dbc802009-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 Samse5ffb872009-08-09 17:01:55 -0700220 return sqrtf(x * x + y * y);
Romain Guy39dbc802009-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 Samse5ffb872009-08-09 17:01:55 -0700228 return sqrtf(x * x + y * y + z * z);
Romain Guy39dbc802009-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 Samse5ffb872009-08-09 17:01:55 -0700243 return degrees * DEG_TO_RAD;
Romain Guy39dbc802009-07-31 11:20:59 -0700244}
245
246static float SC_degf(float radians)
247{
Jason Samse5ffb872009-08-09 17:01:55 -0700248 return radians * RAD_TO_DEG;
Romain Guy39dbc802009-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 Samse45ac6e2009-07-20 14:31:06 -0700265
Romain Guy98e10fd2009-07-30 18:45:01 -0700266//////////////////////////////////////////////////////////////////////////////
267// Time routines
268//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700269
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700270static int32_t SC_second()
Romain Guy98e10fd2009-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 Onorato9c4e4ca2009-08-09 11:39:02 -0700288static int32_t SC_minute()
Romain Guy98e10fd2009-07-30 18:45:01 -0700289{
290 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700291
Romain Guy98e10fd2009-07-30 18:45:01 -0700292 time_t rawtime;
293 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700294
Romain Guy98e10fd2009-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 Samse5ffb872009-08-09 17:01:55 -0700304}
Romain Guy98e10fd2009-07-30 18:45:01 -0700305
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700306static int32_t SC_hour()
Romain Guy98e10fd2009-07-30 18:45:01 -0700307{
308 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700309
Romain Guy98e10fd2009-07-30 18:45:01 -0700310 time_t rawtime;
311 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700312
Romain Guy98e10fd2009-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 Guy39dbc802009-07-31 11:20:59 -0700322}
323
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700324static int32_t SC_day()
Romain Guy39dbc802009-07-31 11:20:59 -0700325{
326 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700327
Romain Guy39dbc802009-07-31 11:20:59 -0700328 time_t rawtime;
329 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700330
Romain Guy39dbc802009-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 Samse5ffb872009-08-09 17:01:55 -0700340}
Jason Samse45ac6e2009-07-20 14:31:06 -0700341
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700342static int32_t SC_month()
Romain Guy39dbc802009-07-31 11:20:59 -0700343{
344 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700345
Romain Guy39dbc802009-07-31 11:20:59 -0700346 time_t rawtime;
347 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700348
Romain Guy39dbc802009-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 Samse5ffb872009-08-09 17:01:55 -0700358}
Romain Guy39dbc802009-07-31 11:20:59 -0700359
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700360static int32_t SC_year()
Romain Guy39dbc802009-07-31 11:20:59 -0700361{
362 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700363
Romain Guy39dbc802009-07-31 11:20:59 -0700364 time_t rawtime;
365 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700366
Romain Guy39dbc802009-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 Onorato9c4e4ca2009-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 Samse45ac6e2009-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 Samsb5909ce2009-07-21 12:20:54 -0700509static void SC_bindProgramVertex(RsProgramVertex pv)
510{
511 GET_TLS();
512 rsi_ContextBindProgramVertex(rsc, pv);
513
514}
Jason Samse45ac6e2009-07-20 14:31:06 -0700515
516//////////////////////////////////////////////////////////////////////////////
Jason Samsc9d43db2009-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 Samse45ac6e2009-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
550// Assumes (GL_FIXED) x,y,z (GL_UNSIGNED_BYTE)r,g,b,a
551static void SC_drawTriangleArray(int ialloc, uint32_t count)
552{
553 GET_TLS();
554 RsAllocation alloc = (RsAllocation)ialloc;
555
556 const Allocation *a = (const Allocation *)alloc;
557 const uint32_t *ptr = (const uint32_t *)a->getPtr();
558
559 rsc->setupCheck();
560
561 glBindBuffer(GL_ARRAY_BUFFER, 0);
562 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
563
564 glEnableClientState(GL_VERTEX_ARRAY);
565 glDisableClientState(GL_NORMAL_ARRAY);
566 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
567 glEnableClientState(GL_COLOR_ARRAY);
568
569 glVertexPointer(2, GL_FIXED, 12, ptr + 1);
570 //glTexCoordPointer(2, GL_FIXED, 24, ptr + 1);
571 glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
572
573 glDrawArrays(GL_TRIANGLES, 0, count * 3);
574}
575
Romain Guyd369e272009-08-07 15:40:32 -0700576static void SC_drawLine(float x1, float y1, float z1,
577 float x2, float y2, float z2)
578{
579 GET_TLS();
580 rsc->setupCheck();
581
582 float vtx[] = { x1, y1, z1, x2, y2, z2 };
583
584 glBindBuffer(GL_ARRAY_BUFFER, 0);
585 glEnableClientState(GL_VERTEX_ARRAY);
586 glVertexPointer(3, GL_FLOAT, 0, vtx);
587
588 glDisableClientState(GL_NORMAL_ARRAY);
589 glDisableClientState(GL_COLOR_ARRAY);
590
591 glDrawArrays(GL_LINES, 0, 2);
592}
593
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700594static void SC_drawQuadTexCoords(float x1, float y1, float z1,
595 float u1, float v1,
596 float x2, float y2, float z2,
597 float u2, float v2,
598 float x3, float y3, float z3,
599 float u3, float v3,
600 float x4, float y4, float z4,
601 float u4, float v4)
Jason Samse45ac6e2009-07-20 14:31:06 -0700602{
603 GET_TLS();
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700604
Jason Samse45ac6e2009-07-20 14:31:06 -0700605 //LOGE("Quad");
606 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
607 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
608 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
609 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700610
Jason Samse45ac6e2009-07-20 14:31:06 -0700611 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700612 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
Jason Samse45ac6e2009-07-20 14:31:06 -0700613
614 rsc->setupCheck();
615
616 glBindBuffer(GL_ARRAY_BUFFER, 0);
617 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
618
619 glEnableClientState(GL_VERTEX_ARRAY);
620 glVertexPointer(3, GL_FLOAT, 0, vtx);
621
622 glClientActiveTexture(GL_TEXTURE0);
623 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
624 glTexCoordPointer(2, GL_FLOAT, 0, tex);
625 glClientActiveTexture(GL_TEXTURE1);
626 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
627 glTexCoordPointer(2, GL_FLOAT, 0, tex);
628 glClientActiveTexture(GL_TEXTURE0);
629
630 glDisableClientState(GL_NORMAL_ARRAY);
631 glDisableClientState(GL_COLOR_ARRAY);
632
633 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
634
635 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
636}
637
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700638static void SC_drawQuad(float x1, float y1, float z1,
639 float x2, float y2, float z2,
640 float x3, float y3, float z3,
641 float x4, float y4, float z4)
642{
643 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
644 x2, y2, z2, 1, 1,
645 x3, y3, z3, 1, 0,
646 x4, y4, z4, 0, 0);
647}
648
Jason Samse9f5c532009-07-28 17:20:11 -0700649static void SC_drawRect(float x1, float y1,
650 float x2, float y2, float z)
651{
652 SC_drawQuad(x1, y2, z,
653 x2, y2, z,
654 x2, y1, z,
655 x1, y1, z);
656}
657
Jason Samse5ffb872009-08-09 17:01:55 -0700658static void SC_drawSimpleMesh(RsSimpleMesh vsm)
659{
660 GET_TLS();
661 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
662 rsc->setupCheck();
663 sm->render();
664}
665
666static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len)
667{
668 GET_TLS();
669 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
670 rsc->setupCheck();
671 sm->renderRange(start, len);
672}
673
674
Jason Samse45ac6e2009-07-20 14:31:06 -0700675//////////////////////////////////////////////////////////////////////////////
676//
677//////////////////////////////////////////////////////////////////////////////
678
Jason Samse45ac6e2009-07-20 14:31:06 -0700679static void SC_color(float r, float g, float b, float a)
680{
681 glColor4f(r, g, b, a);
682}
683
Romain Guy48b7edc2009-08-06 22:52:13 -0700684static void SC_ambient(float r, float g, float b, float a)
685{
686 GLfloat params[] = { r, g, b, a };
687 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, params);
688}
689
690static void SC_diffuse(float r, float g, float b, float a)
691{
692 GLfloat params[] = { r, g, b, a };
693 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, params);
694}
695
696static void SC_specular(float r, float g, float b, float a)
697{
698 GLfloat params[] = { r, g, b, a };
699 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, params);
700}
701
702static void SC_emission(float r, float g, float b, float a)
703{
704 GLfloat params[] = { r, g, b, a };
705 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, params);
706}
707
Romain Guyd369e272009-08-07 15:40:32 -0700708static void SC_shininess(float s)
Romain Guy48b7edc2009-08-06 22:52:13 -0700709{
Romain Guyd369e272009-08-07 15:40:32 -0700710 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, s);
Romain Guy48b7edc2009-08-06 22:52:13 -0700711}
712
Romain Guy9c59d022009-07-31 15:33:59 -0700713static void SC_hsb(float h, float s, float b, float a)
714{
715 float red = 0.0f;
716 float green = 0.0f;
717 float blue = 0.0f;
Jason Samse5ffb872009-08-09 17:01:55 -0700718
Romain Guy9c59d022009-07-31 15:33:59 -0700719 float x = h;
720 float y = s;
721 float z = b;
Jason Samse5ffb872009-08-09 17:01:55 -0700722
Romain Guy9c59d022009-07-31 15:33:59 -0700723 float hf = (x - (int) x) * 6.0f;
724 int ihf = (int) hf;
725 float f = hf - ihf;
726 float pv = z * (1.0f - y);
727 float qv = z * (1.0f - y * f);
728 float tv = z * (1.0f - y * (1.0f - f));
Jason Samse5ffb872009-08-09 17:01:55 -0700729
Romain Guy9c59d022009-07-31 15:33:59 -0700730 switch (ihf) {
731 case 0: // Red is the dominant color
732 red = z;
733 green = tv;
734 blue = pv;
735 break;
736 case 1: // Green is the dominant color
737 red = qv;
738 green = z;
739 blue = pv;
740 break;
741 case 2:
742 red = pv;
743 green = z;
744 blue = tv;
745 break;
746 case 3: // Blue is the dominant color
747 red = pv;
748 green = qv;
749 blue = z;
750 break;
751 case 4:
752 red = tv;
753 green = pv;
754 blue = z;
755 break;
756 case 5: // Red is the dominant color
757 red = z;
758 green = pv;
759 blue = qv;
760 break;
761 }
Jason Samse5ffb872009-08-09 17:01:55 -0700762
Romain Guy9c59d022009-07-31 15:33:59 -0700763 glColor4f(red, green, blue, a);
764}
765
Jason Samsc9d43db2009-07-28 12:02:16 -0700766static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samse45ac6e2009-07-20 14:31:06 -0700767{
768 GET_TLS();
769 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
770}
771
Jason Samse5ffb872009-08-09 17:01:55 -0700772static void SC_uploadToBufferObject(RsAllocation va)
773{
774 GET_TLS();
775 rsi_AllocationUploadToBufferObject(rsc, va);
776}
777
Jason Samse45ac6e2009-07-20 14:31:06 -0700778static void SC_ClearColor(float r, float g, float b, float a)
779{
780 //LOGE("c %f %f %f %f", r, g, b, a);
781 GET_TLS();
782 sc->mEnviroment.mClearColor[0] = r;
783 sc->mEnviroment.mClearColor[1] = g;
784 sc->mEnviroment.mClearColor[2] = b;
785 sc->mEnviroment.mClearColor[3] = a;
786}
787
Jason Samsc9d43db2009-07-28 12:02:16 -0700788static void SC_debugF(const char *s, float f)
789{
790 LOGE("%s %f", s, f);
791}
792
793static void SC_debugI32(const char *s, int32_t i)
794{
795 LOGE("%s %i", s, i);
796}
797
Jason Samse45ac6e2009-07-20 14:31:06 -0700798
799
800//////////////////////////////////////////////////////////////////////////////
801// Class implementation
802//////////////////////////////////////////////////////////////////////////////
803
804ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
805 // IO
806 { "loadI32", (void *)&SC_loadI32,
807 "int", "(int, int)" },
808 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
809 { "loadF", (void *)&SC_loadF,
810 "float", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -0700811 { "loadArrayF", (void *)&SC_loadArrayF,
Romain Guy06f7c932009-08-06 12:40:41 -0700812 "float*", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -0700813 { "loadArrayI32", (void *)&SC_loadArrayI32,
Romain Guy06f7c932009-08-06 12:40:41 -0700814 "int*", "(int, int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700815 { "loadVec4", (void *)&SC_loadVec4,
816 "void", "(int, int, float *)" },
817 { "loadMatrix", (void *)&SC_loadMatrix,
818 "void", "(int, int, float *)" },
819 { "storeI32", (void *)&SC_storeI32,
820 "void", "(int, int, int)" },
821 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
822 { "storeF", (void *)&SC_storeF,
823 "void", "(int, int, float)" },
824 { "storeVec4", (void *)&SC_storeVec4,
825 "void", "(int, int, float *)" },
826 { "storeMatrix", (void *)&SC_storeMatrix,
827 "void", "(int, int, float *)" },
Romain Guy48b7edc2009-08-06 22:52:13 -0700828 { "loadTriangleMeshVerticesF", (void *)&SC_loadTriangleMeshVerticesF,
829 "float*", "(int)" },
830 { "updateTriangleMesh", (void *)&SC_updateTriangleMesh,
831 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700832
833 // math
Romain Guy27162ab2009-08-09 17:04:54 -0700834 { "modf", (void *)&fmod,
835 "float", "(float, float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700836 { "abs", (void *)&abs,
837 "int", "(int)" },
838 { "absf", (void *)&fabs,
839 "float", "(float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700840 { "sinf", (void *)&sinf,
841 "float", "(float)" },
842 { "cosf", (void *)&cosf,
843 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700844 { "asinf", (void *)&asinf,
845 "float", "(float)" },
846 { "acosf", (void *)&acosf,
847 "float", "(float)" },
848 { "atanf", (void *)&atanf,
849 "float", "(float)" },
850 { "atan2f", (void *)&atan2f,
Romain Guy9c59d022009-07-31 15:33:59 -0700851 "float", "(float, float)" },
Jason Samse9f5c532009-07-28 17:20:11 -0700852 { "fabsf", (void *)&fabsf,
Jason Samse45ac6e2009-07-20 14:31:06 -0700853 "float", "(float)" },
854 { "randf", (void *)&SC_randf,
855 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700856 { "randf2", (void *)&SC_randf2,
857 "float", "(float, float)" },
Jason Samsc9d43db2009-07-28 12:02:16 -0700858 { "floorf", (void *)&floorf,
859 "float", "(float)" },
860 { "ceilf", (void *)&ceilf,
861 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700862 { "expf", (void *)&expf,
863 "float", "(float)" },
864 { "logf", (void *)&logf,
865 "float", "(float)" },
866 { "powf", (void *)&powf,
867 "float", "(float, float)" },
868 { "maxf", (void *)&SC_maxf,
869 "float", "(float, float)" },
870 { "minf", (void *)&SC_minf,
871 "float", "(float, float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700872 { "sqrt", (void *)&sqrt,
873 "int", "(int)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700874 { "sqrtf", (void *)&sqrtf,
875 "float", "(float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700876 { "sqr", (void *)&SC_sqr,
877 "int", "(int)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700878 { "sqrf", (void *)&SC_sqrf,
879 "float", "(float)" },
Romain Guy27162ab2009-08-09 17:04:54 -0700880 { "clamp", (void *)&SC_clamp,
881 "int", "(int, int, int)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700882 { "clampf", (void *)&SC_clampf,
883 "float", "(float, float, float)" },
884 { "distf2", (void *)&SC_distf2,
885 "float", "(float, float, float, float)" },
886 { "distf3", (void *)&SC_distf3,
887 "float", "(float, float, float, float, float, float)" },
888 { "magf2", (void *)&SC_magf2,
889 "float", "(float, float)" },
890 { "magf3", (void *)&SC_magf3,
891 "float", "(float, float, float)" },
892 { "radf", (void *)&SC_radf,
893 "float", "(float)" },
894 { "degf", (void *)&SC_degf,
895 "float", "(float)" },
896 { "lerpf", (void *)&SC_lerpf,
897 "float", "(float, float, float)" },
898 { "normf", (void *)&SC_normf,
899 "float", "(float, float, float)" },
900 { "mapf", (void *)&SC_mapf,
901 "float", "(float, float, float, float, float)" },
Romain Guyb7f1a6d2009-08-03 21:12:51 -0700902 { "noisef", (void *)&SC_noisef,
903 "float", "(float)" },
904 { "noisef2", (void *)&SC_noisef2,
905 "float", "(float, float)" },
906 { "noisef3", (void *)&SC_noisef3,
907 "float", "(float, float, float)" },
908 { "turbulencef2", (void *)&SC_turbulencef2,
909 "float", "(float, float, float)" },
910 { "turbulencef3", (void *)&SC_turbulencef3,
911 "float", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700912
Romain Guy98e10fd2009-07-30 18:45:01 -0700913 // time
914 { "second", (void *)&SC_second,
915 "int", "()" },
916 { "minute", (void *)&SC_minute,
917 "int", "()" },
918 { "hour", (void *)&SC_hour,
919 "int", "()" },
Romain Guy39dbc802009-07-31 11:20:59 -0700920 { "day", (void *)&SC_day,
921 "int", "()" },
922 { "month", (void *)&SC_month,
923 "int", "()" },
924 { "year", (void *)&SC_year,
925 "int", "()" },
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700926 { "uptimeMillis", (void*)&SC_uptimeMillis,
927 "int", "()" }, // TODO: use long instead
928 { "startTimeMillis", (void*)&SC_startTimeMillis,
929 "int", "()" }, // TODO: use long instead
930 { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis,
931 "int", "()" }, // TODO: use long instead
Romain Guy98e10fd2009-07-30 18:45:01 -0700932
Jason Samse45ac6e2009-07-20 14:31:06 -0700933 // matrix
934 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
935 "void", "(float *mat)" },
936 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
937 "void", "(float *mat, float *f)" },
938 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
939 "void", "(float *mat, float *newmat)" },
940 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
941 "void", "(float *mat, float rot, float x, float y, float z)" },
942 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
943 "void", "(float *mat, float x, float y, float z)" },
944 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
945 "void", "(float *mat, float x, float y, float z)" },
946 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
947 "void", "(float *mat, float *lhs, float *rhs)" },
948 { "matrixMultiply", (void *)&SC_matrixMultiply,
949 "void", "(float *mat, float *rhs)" },
950 { "matrixRotate", (void *)&SC_matrixRotate,
951 "void", "(float *mat, float rot, float x, float y, float z)" },
952 { "matrixScale", (void *)&SC_matrixScale,
953 "void", "(float *mat, float x, float y, float z)" },
954 { "matrixTranslate", (void *)&SC_matrixTranslate,
955 "void", "(float *mat, float x, float y, float z)" },
956
957 // context
958 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
959 "void", "(int)" },
960 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
961 "void", "(int)" },
Jason Samsb5909ce2009-07-21 12:20:54 -0700962 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
963 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700964 { "bindSampler", (void *)&SC_bindSampler,
965 "void", "(int, int, int)" },
966 { "bindTexture", (void *)&SC_bindTexture,
967 "void", "(int, int, int)" },
968
Jason Samsc9d43db2009-07-28 12:02:16 -0700969 // vp
Jason Sams50253db2009-07-29 20:55:44 -0700970 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -0700971 "void", "(void *)" },
Jason Sams50253db2009-07-29 20:55:44 -0700972 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -0700973 "void", "(void *)" },
974
975
976
Jason Samse45ac6e2009-07-20 14:31:06 -0700977 // drawing
Jason Samse9f5c532009-07-28 17:20:11 -0700978 { "drawRect", (void *)&SC_drawRect,
979 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700980 { "drawQuad", (void *)&SC_drawQuad,
981 "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 -0700982 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords,
983 "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 -0700984 { "drawTriangleArray", (void *)&SC_drawTriangleArray,
985 "void", "(int ialloc, int count)" },
986 { "drawTriangleMesh", (void *)&SC_drawTriangleMesh,
987 "void", "(int mesh)" },
988 { "drawTriangleMeshRange", (void *)&SC_drawTriangleMeshRange,
989 "void", "(int mesh, int start, int count)" },
Romain Guyd369e272009-08-07 15:40:32 -0700990 { "drawLine", (void *)&SC_drawLine,
991 "void", "(float x1, float y1, float z1, float x2, float y2, float z2)" },
Jason Samse5ffb872009-08-09 17:01:55 -0700992 { "drawSimpleMesh", (void *)&SC_drawSimpleMesh,
993 "void", "(int ism)" },
994 { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange,
995 "void", "(int ism, int start, int len)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700996
997
998 // misc
999 { "pfClearColor", (void *)&SC_ClearColor,
1000 "void", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001001 { "color", (void *)&SC_color,
1002 "void", "(float, float, float, float)" },
Romain Guy9c59d022009-07-31 15:33:59 -07001003 { "hsb", (void *)&SC_hsb,
1004 "void", "(float, float, float, float)" },
Romain Guy48b7edc2009-08-06 22:52:13 -07001005 { "ambient", (void *)&SC_ambient,
1006 "void", "(float, float, float, float)" },
1007 { "diffuse", (void *)&SC_diffuse,
1008 "void", "(float, float, float, float)" },
1009 { "specular", (void *)&SC_specular,
1010 "void", "(float, float, float, float)" },
1011 { "emission", (void *)&SC_emission,
1012 "void", "(float, float, float, float)" },
1013 { "shininess", (void *)&SC_shininess,
Romain Guyd369e272009-08-07 15:40:32 -07001014 "void", "(float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001015
Jason Samsc9d43db2009-07-28 12:02:16 -07001016 { "uploadToTexture", (void *)&SC_uploadToTexture,
1017 "void", "(int, int)" },
Jason Samse5ffb872009-08-09 17:01:55 -07001018 { "uploadToBufferObject", (void *)&SC_uploadToBufferObject,
1019 "void", "(int)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001020
1021
1022 { "debugF", (void *)&SC_debugF,
1023 "void", "(void *, float)" },
1024 { "debugI32", (void *)&SC_debugI32,
1025 "void", "(void *, int)" },
1026
1027
Jason Samse45ac6e2009-07-20 14:31:06 -07001028 { NULL, NULL, NULL, NULL }
1029};
1030
1031const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
1032{
1033 ScriptCState::SymbolTable_t *syms = gSyms;
1034
1035 while (syms->mPtr) {
1036 if (!strcmp(syms->mName, sym)) {
1037 return syms;
1038 }
1039 syms++;
1040 }
1041 return NULL;
1042}
1043
1044void ScriptCState::appendDecls(String8 *str)
1045{
1046 ScriptCState::SymbolTable_t *syms = gSyms;
1047 while (syms->mPtr) {
1048 str->append(syms->mRet);
1049 str->append(" ");
1050 str->append(syms->mName);
1051 str->append(syms->mParam);
1052 str->append(";\n");
1053 syms++;
1054 }
1055}
1056
1057