blob: 1ef6b4efc2d3e09d0406db84387e3d47ee99308b [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
191static float SC_maxf(float a, float b)
192{
Jason Samse5ffb872009-08-09 17:01:55 -0700193 return a > b ? a : b;
Romain Guy39dbc802009-07-31 11:20:59 -0700194}
195
196static float SC_minf(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_sqrf(float v)
202{
Jason Samse5ffb872009-08-09 17:01:55 -0700203 return v * v;
Romain Guy39dbc802009-07-31 11:20:59 -0700204}
205
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700206static int SC_sqr(int v)
207{
208 return v * v;
209}
210
Romain Guy39dbc802009-07-31 11:20:59 -0700211static float SC_distf2(float x1, float y1, float x2, float y2)
212{
213 float x = x2 - x1;
214 float y = y2 - y1;
Jason Samse5ffb872009-08-09 17:01:55 -0700215 return sqrtf(x * x + y * y);
Romain Guy39dbc802009-07-31 11:20:59 -0700216}
217
218static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2)
219{
220 float x = x2 - x1;
221 float y = y2 - y1;
222 float z = z2 - z1;
Jason Samse5ffb872009-08-09 17:01:55 -0700223 return sqrtf(x * x + y * y + z * z);
Romain Guy39dbc802009-07-31 11:20:59 -0700224}
225
226static float SC_magf2(float a, float b)
227{
228 return sqrtf(a * a + b * b);
229}
230
231static float SC_magf3(float a, float b, float c)
232{
233 return sqrtf(a * a + b * b + c * c);
234}
235
236static float SC_radf(float degrees)
237{
Jason Samse5ffb872009-08-09 17:01:55 -0700238 return degrees * DEG_TO_RAD;
Romain Guy39dbc802009-07-31 11:20:59 -0700239}
240
241static float SC_degf(float radians)
242{
Jason Samse5ffb872009-08-09 17:01:55 -0700243 return radians * RAD_TO_DEG;
Romain Guy39dbc802009-07-31 11:20:59 -0700244}
245
246static float SC_lerpf(float start, float stop, float amount)
247{
248 return start + (stop - start) * amount;
249}
250
251static float SC_normf(float start, float stop, float value)
252{
253 return (value - start) / (stop - start);
254}
255
256static float SC_mapf(float minStart, float minStop, float maxStart, float maxStop, float value)
257{
258 return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
259}
Jason Samse45ac6e2009-07-20 14:31:06 -0700260
Romain Guy98e10fd2009-07-30 18:45:01 -0700261//////////////////////////////////////////////////////////////////////////////
262// Time routines
263//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700264
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700265static int32_t SC_second()
Romain Guy98e10fd2009-07-30 18:45:01 -0700266{
267 GET_TLS();
268
269 time_t rawtime;
270 time(&rawtime);
271
272 if (sc->mEnviroment.mTimeZone) {
273 struct tm timeinfo;
274 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
275 return timeinfo.tm_sec;
276 } else {
277 struct tm *timeinfo;
278 timeinfo = localtime(&rawtime);
279 return timeinfo->tm_sec;
280 }
281}
282
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700283static int32_t SC_minute()
Romain Guy98e10fd2009-07-30 18:45:01 -0700284{
285 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700286
Romain Guy98e10fd2009-07-30 18:45:01 -0700287 time_t rawtime;
288 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700289
Romain Guy98e10fd2009-07-30 18:45:01 -0700290 if (sc->mEnviroment.mTimeZone) {
291 struct tm timeinfo;
292 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
293 return timeinfo.tm_min;
294 } else {
295 struct tm *timeinfo;
296 timeinfo = localtime(&rawtime);
297 return timeinfo->tm_min;
298 }
Jason Samse5ffb872009-08-09 17:01:55 -0700299}
Romain Guy98e10fd2009-07-30 18:45:01 -0700300
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700301static int32_t SC_hour()
Romain Guy98e10fd2009-07-30 18:45:01 -0700302{
303 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700304
Romain Guy98e10fd2009-07-30 18:45:01 -0700305 time_t rawtime;
306 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700307
Romain Guy98e10fd2009-07-30 18:45:01 -0700308 if (sc->mEnviroment.mTimeZone) {
309 struct tm timeinfo;
310 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
311 return timeinfo.tm_hour;
312 } else {
313 struct tm *timeinfo;
314 timeinfo = localtime(&rawtime);
315 return timeinfo->tm_hour;
316 }
Romain Guy39dbc802009-07-31 11:20:59 -0700317}
318
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700319static int32_t SC_day()
Romain Guy39dbc802009-07-31 11:20:59 -0700320{
321 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700322
Romain Guy39dbc802009-07-31 11:20:59 -0700323 time_t rawtime;
324 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700325
Romain Guy39dbc802009-07-31 11:20:59 -0700326 if (sc->mEnviroment.mTimeZone) {
327 struct tm timeinfo;
328 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
329 return timeinfo.tm_mday;
330 } else {
331 struct tm *timeinfo;
332 timeinfo = localtime(&rawtime);
333 return timeinfo->tm_mday;
334 }
Jason Samse5ffb872009-08-09 17:01:55 -0700335}
Jason Samse45ac6e2009-07-20 14:31:06 -0700336
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700337static int32_t SC_month()
Romain Guy39dbc802009-07-31 11:20:59 -0700338{
339 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700340
Romain Guy39dbc802009-07-31 11:20:59 -0700341 time_t rawtime;
342 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700343
Romain Guy39dbc802009-07-31 11:20:59 -0700344 if (sc->mEnviroment.mTimeZone) {
345 struct tm timeinfo;
346 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
347 return timeinfo.tm_mon;
348 } else {
349 struct tm *timeinfo;
350 timeinfo = localtime(&rawtime);
351 return timeinfo->tm_mon;
352 }
Jason Samse5ffb872009-08-09 17:01:55 -0700353}
Romain Guy39dbc802009-07-31 11:20:59 -0700354
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700355static int32_t SC_year()
Romain Guy39dbc802009-07-31 11:20:59 -0700356{
357 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700358
Romain Guy39dbc802009-07-31 11:20:59 -0700359 time_t rawtime;
360 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700361
Romain Guy39dbc802009-07-31 11:20:59 -0700362 if (sc->mEnviroment.mTimeZone) {
363 struct tm timeinfo;
364 localtime_tz(&rawtime, &timeinfo, sc->mEnviroment.mTimeZone);
365 return timeinfo.tm_year;
366 } else {
367 struct tm *timeinfo;
368 timeinfo = localtime(&rawtime);
369 return timeinfo->tm_year;
370 }
371}
372
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700373static int32_t SC_uptimeMillis()
374{
375 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
376}
377
378static int32_t SC_startTimeMillis()
379{
380 GET_TLS();
381 return sc->mEnviroment.mStartTimeMillis;
382}
383
384static int32_t SC_elapsedTimeMillis()
385{
386 GET_TLS();
387 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC))
388 - sc->mEnviroment.mStartTimeMillis;
389}
390
Jason Samse45ac6e2009-07-20 14:31:06 -0700391//////////////////////////////////////////////////////////////////////////////
392// Matrix routines
393//////////////////////////////////////////////////////////////////////////////
394
395
396static void SC_matrixLoadIdentity(rsc_Matrix *mat)
397{
398 Matrix *m = reinterpret_cast<Matrix *>(mat);
399 m->loadIdentity();
400}
401
402static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
403{
404 Matrix *m = reinterpret_cast<Matrix *>(mat);
405 m->load(f);
406}
407
408static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
409{
410 Matrix *m = reinterpret_cast<Matrix *>(mat);
411 m->load(reinterpret_cast<const Matrix *>(newmat));
412}
413
414static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
415{
416 Matrix *m = reinterpret_cast<Matrix *>(mat);
417 m->loadRotate(rot, x, y, z);
418}
419
420static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
421{
422 Matrix *m = reinterpret_cast<Matrix *>(mat);
423 m->loadScale(x, y, z);
424}
425
426static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
427{
428 Matrix *m = reinterpret_cast<Matrix *>(mat);
429 m->loadTranslate(x, y, z);
430}
431
432static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
433{
434 Matrix *m = reinterpret_cast<Matrix *>(mat);
435 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
436 reinterpret_cast<const Matrix *>(rhs));
437}
438
439static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
440{
441 Matrix *m = reinterpret_cast<Matrix *>(mat);
442 m->multiply(reinterpret_cast<const Matrix *>(rhs));
443}
444
445static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
446{
447 Matrix *m = reinterpret_cast<Matrix *>(mat);
448 m->rotate(rot, x, y, z);
449}
450
451static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
452{
453 Matrix *m = reinterpret_cast<Matrix *>(mat);
454 m->scale(x, y, z);
455}
456
457static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
458{
459 Matrix *m = reinterpret_cast<Matrix *>(mat);
460 m->translate(x, y, z);
461}
462
463
464
465
466//////////////////////////////////////////////////////////////////////////////
467// Context
468//////////////////////////////////////////////////////////////////////////////
469
470static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
471{
472 GET_TLS();
473 rsi_ProgramFragmentBindTexture(rsc,
474 static_cast<ProgramFragment *>(vpf),
475 slot,
476 static_cast<Allocation *>(va));
477
478}
479
480static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
481{
482 GET_TLS();
483 rsi_ProgramFragmentBindSampler(rsc,
484 static_cast<ProgramFragment *>(vpf),
485 slot,
486 static_cast<Sampler *>(vs));
487
488}
489
490static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs)
491{
492 GET_TLS();
493 rsi_ContextBindProgramFragmentStore(rsc, pfs);
494
495}
496
497static void SC_bindProgramFragment(RsProgramFragment pf)
498{
499 GET_TLS();
500 rsi_ContextBindProgramFragment(rsc, pf);
501
502}
503
Jason Samsb5909ce2009-07-21 12:20:54 -0700504static void SC_bindProgramVertex(RsProgramVertex pv)
505{
506 GET_TLS();
507 rsi_ContextBindProgramVertex(rsc, pv);
508
509}
Jason Samse45ac6e2009-07-20 14:31:06 -0700510
511//////////////////////////////////////////////////////////////////////////////
Jason Samsc9d43db2009-07-28 12:02:16 -0700512// VP
513//////////////////////////////////////////////////////////////////////////////
514
515static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
516{
517 GET_TLS();
518 rsc->getVertex()->setModelviewMatrix(m);
519}
520
521static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
522{
523 GET_TLS();
524 rsc->getVertex()->setTextureMatrix(m);
525}
526
527
528
529//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700530// Drawing
531//////////////////////////////////////////////////////////////////////////////
532
533static void SC_drawTriangleMesh(RsTriangleMesh mesh)
534{
535 GET_TLS();
536 rsi_TriangleMeshRender(rsc, mesh);
537}
538
539static void SC_drawTriangleMeshRange(RsTriangleMesh mesh, uint32_t start, uint32_t count)
540{
541 GET_TLS();
542 rsi_TriangleMeshRenderRange(rsc, mesh, start, count);
543}
544
545// Assumes (GL_FIXED) x,y,z (GL_UNSIGNED_BYTE)r,g,b,a
546static void SC_drawTriangleArray(int ialloc, uint32_t count)
547{
548 GET_TLS();
549 RsAllocation alloc = (RsAllocation)ialloc;
550
551 const Allocation *a = (const Allocation *)alloc;
552 const uint32_t *ptr = (const uint32_t *)a->getPtr();
553
554 rsc->setupCheck();
555
556 glBindBuffer(GL_ARRAY_BUFFER, 0);
557 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
558
559 glEnableClientState(GL_VERTEX_ARRAY);
560 glDisableClientState(GL_NORMAL_ARRAY);
561 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
562 glEnableClientState(GL_COLOR_ARRAY);
563
564 glVertexPointer(2, GL_FIXED, 12, ptr + 1);
565 //glTexCoordPointer(2, GL_FIXED, 24, ptr + 1);
566 glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
567
568 glDrawArrays(GL_TRIANGLES, 0, count * 3);
569}
570
Romain Guyd369e272009-08-07 15:40:32 -0700571static void SC_drawLine(float x1, float y1, float z1,
572 float x2, float y2, float z2)
573{
574 GET_TLS();
575 rsc->setupCheck();
576
577 float vtx[] = { x1, y1, z1, x2, y2, z2 };
578
579 glBindBuffer(GL_ARRAY_BUFFER, 0);
580 glEnableClientState(GL_VERTEX_ARRAY);
581 glVertexPointer(3, GL_FLOAT, 0, vtx);
582
583 glDisableClientState(GL_NORMAL_ARRAY);
584 glDisableClientState(GL_COLOR_ARRAY);
585
586 glDrawArrays(GL_LINES, 0, 2);
587}
588
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700589static void SC_drawQuadTexCoords(float x1, float y1, float z1,
590 float u1, float v1,
591 float x2, float y2, float z2,
592 float u2, float v2,
593 float x3, float y3, float z3,
594 float u3, float v3,
595 float x4, float y4, float z4,
596 float u4, float v4)
Jason Samse45ac6e2009-07-20 14:31:06 -0700597{
598 GET_TLS();
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700599
Jason Samse45ac6e2009-07-20 14:31:06 -0700600 //LOGE("Quad");
601 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
602 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
603 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
604 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700605
Jason Samse45ac6e2009-07-20 14:31:06 -0700606 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700607 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
Jason Samse45ac6e2009-07-20 14:31:06 -0700608
609 rsc->setupCheck();
610
611 glBindBuffer(GL_ARRAY_BUFFER, 0);
612 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
613
614 glEnableClientState(GL_VERTEX_ARRAY);
615 glVertexPointer(3, GL_FLOAT, 0, vtx);
616
617 glClientActiveTexture(GL_TEXTURE0);
618 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
619 glTexCoordPointer(2, GL_FLOAT, 0, tex);
620 glClientActiveTexture(GL_TEXTURE1);
621 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
622 glTexCoordPointer(2, GL_FLOAT, 0, tex);
623 glClientActiveTexture(GL_TEXTURE0);
624
625 glDisableClientState(GL_NORMAL_ARRAY);
626 glDisableClientState(GL_COLOR_ARRAY);
627
628 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
629
630 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
631}
632
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700633static void SC_drawQuad(float x1, float y1, float z1,
634 float x2, float y2, float z2,
635 float x3, float y3, float z3,
636 float x4, float y4, float z4)
637{
638 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
639 x2, y2, z2, 1, 1,
640 x3, y3, z3, 1, 0,
641 x4, y4, z4, 0, 0);
642}
643
Jason Samse9f5c532009-07-28 17:20:11 -0700644static void SC_drawRect(float x1, float y1,
645 float x2, float y2, float z)
646{
647 SC_drawQuad(x1, y2, z,
648 x2, y2, z,
649 x2, y1, z,
650 x1, y1, z);
651}
652
Jason Samse5ffb872009-08-09 17:01:55 -0700653static void SC_drawSimpleMesh(RsSimpleMesh vsm)
654{
655 GET_TLS();
656 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
657 rsc->setupCheck();
658 sm->render();
659}
660
661static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len)
662{
663 GET_TLS();
664 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
665 rsc->setupCheck();
666 sm->renderRange(start, len);
667}
668
669
Jason Samse45ac6e2009-07-20 14:31:06 -0700670//////////////////////////////////////////////////////////////////////////////
671//
672//////////////////////////////////////////////////////////////////////////////
673
Jason Samse45ac6e2009-07-20 14:31:06 -0700674static void SC_color(float r, float g, float b, float a)
675{
676 glColor4f(r, g, b, a);
677}
678
Romain Guy48b7edc2009-08-06 22:52:13 -0700679static void SC_ambient(float r, float g, float b, float a)
680{
681 GLfloat params[] = { r, g, b, a };
682 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, params);
683}
684
685static void SC_diffuse(float r, float g, float b, float a)
686{
687 GLfloat params[] = { r, g, b, a };
688 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, params);
689}
690
691static void SC_specular(float r, float g, float b, float a)
692{
693 GLfloat params[] = { r, g, b, a };
694 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, params);
695}
696
697static void SC_emission(float r, float g, float b, float a)
698{
699 GLfloat params[] = { r, g, b, a };
700 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, params);
701}
702
Romain Guyd369e272009-08-07 15:40:32 -0700703static void SC_shininess(float s)
Romain Guy48b7edc2009-08-06 22:52:13 -0700704{
Romain Guyd369e272009-08-07 15:40:32 -0700705 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, s);
Romain Guy48b7edc2009-08-06 22:52:13 -0700706}
707
Romain Guy9c59d022009-07-31 15:33:59 -0700708static void SC_hsb(float h, float s, float b, float a)
709{
710 float red = 0.0f;
711 float green = 0.0f;
712 float blue = 0.0f;
Jason Samse5ffb872009-08-09 17:01:55 -0700713
Romain Guy9c59d022009-07-31 15:33:59 -0700714 float x = h;
715 float y = s;
716 float z = b;
Jason Samse5ffb872009-08-09 17:01:55 -0700717
Romain Guy9c59d022009-07-31 15:33:59 -0700718 float hf = (x - (int) x) * 6.0f;
719 int ihf = (int) hf;
720 float f = hf - ihf;
721 float pv = z * (1.0f - y);
722 float qv = z * (1.0f - y * f);
723 float tv = z * (1.0f - y * (1.0f - f));
Jason Samse5ffb872009-08-09 17:01:55 -0700724
Romain Guy9c59d022009-07-31 15:33:59 -0700725 switch (ihf) {
726 case 0: // Red is the dominant color
727 red = z;
728 green = tv;
729 blue = pv;
730 break;
731 case 1: // Green is the dominant color
732 red = qv;
733 green = z;
734 blue = pv;
735 break;
736 case 2:
737 red = pv;
738 green = z;
739 blue = tv;
740 break;
741 case 3: // Blue is the dominant color
742 red = pv;
743 green = qv;
744 blue = z;
745 break;
746 case 4:
747 red = tv;
748 green = pv;
749 blue = z;
750 break;
751 case 5: // Red is the dominant color
752 red = z;
753 green = pv;
754 blue = qv;
755 break;
756 }
Jason Samse5ffb872009-08-09 17:01:55 -0700757
Romain Guy9c59d022009-07-31 15:33:59 -0700758 glColor4f(red, green, blue, a);
759}
760
Jason Samsc9d43db2009-07-28 12:02:16 -0700761static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samse45ac6e2009-07-20 14:31:06 -0700762{
763 GET_TLS();
764 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
765}
766
Jason Samse5ffb872009-08-09 17:01:55 -0700767static void SC_uploadToBufferObject(RsAllocation va)
768{
769 GET_TLS();
770 rsi_AllocationUploadToBufferObject(rsc, va);
771}
772
Jason Samse45ac6e2009-07-20 14:31:06 -0700773static void SC_ClearColor(float r, float g, float b, float a)
774{
775 //LOGE("c %f %f %f %f", r, g, b, a);
776 GET_TLS();
777 sc->mEnviroment.mClearColor[0] = r;
778 sc->mEnviroment.mClearColor[1] = g;
779 sc->mEnviroment.mClearColor[2] = b;
780 sc->mEnviroment.mClearColor[3] = a;
781}
782
Jason Samsc9d43db2009-07-28 12:02:16 -0700783static void SC_debugF(const char *s, float f)
784{
785 LOGE("%s %f", s, f);
786}
787
788static void SC_debugI32(const char *s, int32_t i)
789{
790 LOGE("%s %i", s, i);
791}
792
Jason Samse45ac6e2009-07-20 14:31:06 -0700793
794
795//////////////////////////////////////////////////////////////////////////////
796// Class implementation
797//////////////////////////////////////////////////////////////////////////////
798
799ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
800 // IO
801 { "loadI32", (void *)&SC_loadI32,
802 "int", "(int, int)" },
803 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
804 { "loadF", (void *)&SC_loadF,
805 "float", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -0700806 { "loadArrayF", (void *)&SC_loadArrayF,
Romain Guy06f7c932009-08-06 12:40:41 -0700807 "float*", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -0700808 { "loadArrayI32", (void *)&SC_loadArrayI32,
Romain Guy06f7c932009-08-06 12:40:41 -0700809 "int*", "(int, int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700810 { "loadVec4", (void *)&SC_loadVec4,
811 "void", "(int, int, float *)" },
812 { "loadMatrix", (void *)&SC_loadMatrix,
813 "void", "(int, int, float *)" },
814 { "storeI32", (void *)&SC_storeI32,
815 "void", "(int, int, int)" },
816 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
817 { "storeF", (void *)&SC_storeF,
818 "void", "(int, int, float)" },
819 { "storeVec4", (void *)&SC_storeVec4,
820 "void", "(int, int, float *)" },
821 { "storeMatrix", (void *)&SC_storeMatrix,
822 "void", "(int, int, float *)" },
Romain Guy48b7edc2009-08-06 22:52:13 -0700823 { "loadTriangleMeshVerticesF", (void *)&SC_loadTriangleMeshVerticesF,
824 "float*", "(int)" },
825 { "updateTriangleMesh", (void *)&SC_updateTriangleMesh,
826 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700827
828 // math
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700829 { "abs", (void *)&abs,
830 "int", "(int)" },
831 { "absf", (void *)&fabs,
832 "float", "(float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700833 { "sinf", (void *)&sinf,
834 "float", "(float)" },
835 { "cosf", (void *)&cosf,
836 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700837 { "asinf", (void *)&asinf,
838 "float", "(float)" },
839 { "acosf", (void *)&acosf,
840 "float", "(float)" },
841 { "atanf", (void *)&atanf,
842 "float", "(float)" },
843 { "atan2f", (void *)&atan2f,
Romain Guy9c59d022009-07-31 15:33:59 -0700844 "float", "(float, float)" },
Jason Samse9f5c532009-07-28 17:20:11 -0700845 { "fabsf", (void *)&fabsf,
Jason Samse45ac6e2009-07-20 14:31:06 -0700846 "float", "(float)" },
847 { "randf", (void *)&SC_randf,
848 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700849 { "randf2", (void *)&SC_randf2,
850 "float", "(float, float)" },
Jason Samsc9d43db2009-07-28 12:02:16 -0700851 { "floorf", (void *)&floorf,
852 "float", "(float)" },
853 { "ceilf", (void *)&ceilf,
854 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700855 { "expf", (void *)&expf,
856 "float", "(float)" },
857 { "logf", (void *)&logf,
858 "float", "(float)" },
859 { "powf", (void *)&powf,
860 "float", "(float, float)" },
861 { "maxf", (void *)&SC_maxf,
862 "float", "(float, float)" },
863 { "minf", (void *)&SC_minf,
864 "float", "(float, float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700865 { "sqrt", (void *)&sqrt,
866 "int", "(int)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700867 { "sqrtf", (void *)&sqrtf,
868 "float", "(float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700869 { "sqr", (void *)&SC_sqr,
870 "int", "(int)" },
Romain Guy39dbc802009-07-31 11:20:59 -0700871 { "sqrf", (void *)&SC_sqrf,
872 "float", "(float)" },
873 { "clampf", (void *)&SC_clampf,
874 "float", "(float, float, float)" },
875 { "distf2", (void *)&SC_distf2,
876 "float", "(float, float, float, float)" },
877 { "distf3", (void *)&SC_distf3,
878 "float", "(float, float, float, float, float, float)" },
879 { "magf2", (void *)&SC_magf2,
880 "float", "(float, float)" },
881 { "magf3", (void *)&SC_magf3,
882 "float", "(float, float, float)" },
883 { "radf", (void *)&SC_radf,
884 "float", "(float)" },
885 { "degf", (void *)&SC_degf,
886 "float", "(float)" },
887 { "lerpf", (void *)&SC_lerpf,
888 "float", "(float, float, float)" },
889 { "normf", (void *)&SC_normf,
890 "float", "(float, float, float)" },
891 { "mapf", (void *)&SC_mapf,
892 "float", "(float, float, float, float, float)" },
Romain Guyb7f1a6d2009-08-03 21:12:51 -0700893 { "noisef", (void *)&SC_noisef,
894 "float", "(float)" },
895 { "noisef2", (void *)&SC_noisef2,
896 "float", "(float, float)" },
897 { "noisef3", (void *)&SC_noisef3,
898 "float", "(float, float, float)" },
899 { "turbulencef2", (void *)&SC_turbulencef2,
900 "float", "(float, float, float)" },
901 { "turbulencef3", (void *)&SC_turbulencef3,
902 "float", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700903
Romain Guy98e10fd2009-07-30 18:45:01 -0700904 // time
905 { "second", (void *)&SC_second,
906 "int", "()" },
907 { "minute", (void *)&SC_minute,
908 "int", "()" },
909 { "hour", (void *)&SC_hour,
910 "int", "()" },
Romain Guy39dbc802009-07-31 11:20:59 -0700911 { "day", (void *)&SC_day,
912 "int", "()" },
913 { "month", (void *)&SC_month,
914 "int", "()" },
915 { "year", (void *)&SC_year,
916 "int", "()" },
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700917 { "uptimeMillis", (void*)&SC_uptimeMillis,
918 "int", "()" }, // TODO: use long instead
919 { "startTimeMillis", (void*)&SC_startTimeMillis,
920 "int", "()" }, // TODO: use long instead
921 { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis,
922 "int", "()" }, // TODO: use long instead
Romain Guy98e10fd2009-07-30 18:45:01 -0700923
Jason Samse45ac6e2009-07-20 14:31:06 -0700924 // matrix
925 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
926 "void", "(float *mat)" },
927 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
928 "void", "(float *mat, float *f)" },
929 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
930 "void", "(float *mat, float *newmat)" },
931 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
932 "void", "(float *mat, float rot, float x, float y, float z)" },
933 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
934 "void", "(float *mat, float x, float y, float z)" },
935 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
936 "void", "(float *mat, float x, float y, float z)" },
937 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
938 "void", "(float *mat, float *lhs, float *rhs)" },
939 { "matrixMultiply", (void *)&SC_matrixMultiply,
940 "void", "(float *mat, float *rhs)" },
941 { "matrixRotate", (void *)&SC_matrixRotate,
942 "void", "(float *mat, float rot, float x, float y, float z)" },
943 { "matrixScale", (void *)&SC_matrixScale,
944 "void", "(float *mat, float x, float y, float z)" },
945 { "matrixTranslate", (void *)&SC_matrixTranslate,
946 "void", "(float *mat, float x, float y, float z)" },
947
948 // context
949 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
950 "void", "(int)" },
951 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
952 "void", "(int)" },
Jason Samsb5909ce2009-07-21 12:20:54 -0700953 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
954 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700955 { "bindSampler", (void *)&SC_bindSampler,
956 "void", "(int, int, int)" },
957 { "bindTexture", (void *)&SC_bindTexture,
958 "void", "(int, int, int)" },
959
Jason Samsc9d43db2009-07-28 12:02:16 -0700960 // vp
Jason Sams50253db2009-07-29 20:55:44 -0700961 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -0700962 "void", "(void *)" },
Jason Sams50253db2009-07-29 20:55:44 -0700963 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -0700964 "void", "(void *)" },
965
966
967
Jason Samse45ac6e2009-07-20 14:31:06 -0700968 // drawing
Jason Samse9f5c532009-07-28 17:20:11 -0700969 { "drawRect", (void *)&SC_drawRect,
970 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700971 { "drawQuad", (void *)&SC_drawQuad,
972 "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 -0700973 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords,
974 "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 -0700975 { "drawTriangleArray", (void *)&SC_drawTriangleArray,
976 "void", "(int ialloc, int count)" },
977 { "drawTriangleMesh", (void *)&SC_drawTriangleMesh,
978 "void", "(int mesh)" },
979 { "drawTriangleMeshRange", (void *)&SC_drawTriangleMeshRange,
980 "void", "(int mesh, int start, int count)" },
Romain Guyd369e272009-08-07 15:40:32 -0700981 { "drawLine", (void *)&SC_drawLine,
982 "void", "(float x1, float y1, float z1, float x2, float y2, float z2)" },
Jason Samse5ffb872009-08-09 17:01:55 -0700983 { "drawSimpleMesh", (void *)&SC_drawSimpleMesh,
984 "void", "(int ism)" },
985 { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange,
986 "void", "(int ism, int start, int len)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700987
988
989 // misc
990 { "pfClearColor", (void *)&SC_ClearColor,
991 "void", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700992 { "color", (void *)&SC_color,
993 "void", "(float, float, float, float)" },
Romain Guy9c59d022009-07-31 15:33:59 -0700994 { "hsb", (void *)&SC_hsb,
995 "void", "(float, float, float, float)" },
Romain Guy48b7edc2009-08-06 22:52:13 -0700996 { "ambient", (void *)&SC_ambient,
997 "void", "(float, float, float, float)" },
998 { "diffuse", (void *)&SC_diffuse,
999 "void", "(float, float, float, float)" },
1000 { "specular", (void *)&SC_specular,
1001 "void", "(float, float, float, float)" },
1002 { "emission", (void *)&SC_emission,
1003 "void", "(float, float, float, float)" },
1004 { "shininess", (void *)&SC_shininess,
Romain Guyd369e272009-08-07 15:40:32 -07001005 "void", "(float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001006
Jason Samsc9d43db2009-07-28 12:02:16 -07001007 { "uploadToTexture", (void *)&SC_uploadToTexture,
1008 "void", "(int, int)" },
Jason Samse5ffb872009-08-09 17:01:55 -07001009 { "uploadToBufferObject", (void *)&SC_uploadToBufferObject,
1010 "void", "(int)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001011
1012
1013 { "debugF", (void *)&SC_debugF,
1014 "void", "(void *, float)" },
1015 { "debugI32", (void *)&SC_debugI32,
1016 "void", "(void *, int)" },
1017
1018
Jason Samse45ac6e2009-07-20 14:31:06 -07001019 { NULL, NULL, NULL, NULL }
1020};
1021
1022const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
1023{
1024 ScriptCState::SymbolTable_t *syms = gSyms;
1025
1026 while (syms->mPtr) {
1027 if (!strcmp(syms->mName, sym)) {
1028 return syms;
1029 }
1030 syms++;
1031 }
1032 return NULL;
1033}
1034
1035void ScriptCState::appendDecls(String8 *str)
1036{
1037 ScriptCState::SymbolTable_t *syms = gSyms;
1038 while (syms->mPtr) {
1039 str->append(syms->mRet);
1040 str->append(" ");
1041 str->append(syms->mName);
1042 str->append(syms->mParam);
1043 str->append(";\n");
1044 syms++;
1045 }
1046}
1047
1048