blob: 3ba9cee0993478cf9015553cc2df8f37c3470344 [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"
Joe Onorato9c4e4ca2009-08-09 11:39:02 -070023#include "utils/Timers.h"
Jason Samse45ac6e2009-07-20 14:31:06 -070024
Jason Sams3a97c592009-09-30 17:36:20 -070025#define GL_GLEXT_PROTOTYPES
26
Jason Samse45ac6e2009-07-20 14:31:06 -070027#include <GLES/gl.h>
28#include <GLES/glext.h>
Jason Samsc460e552009-11-25 13:22:07 -080029#include <GLES2/gl2.h>
30#include <GLES2/gl2ext.h>
Jason Samse45ac6e2009-07-20 14:31:06 -070031
Romain Guy98e10fd2009-07-30 18:45:01 -070032#include <time.h>
Romain Guy98e10fd2009-07-30 18:45:01 -070033
Jason Samse45ac6e2009-07-20 14:31:06 -070034using namespace android;
35using namespace android::renderscript;
36
37#define GET_TLS() Context::ScriptTLSStruct * tls = \
38 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
39 Context * rsc = tls->mContext; \
40 ScriptC * sc = (ScriptC *) tls->mScript
41
Jason Samsa57c0a72009-09-04 14:42:41 -070042typedef struct {
43 float x;
44 float y;
45 float z;
46} vec3_t;
47
48typedef struct {
49 float x;
50 float y;
51 float z;
52 float w;
53} vec4_t;
54
55typedef struct {
56 float x;
57 float y;
58} vec2_t;
Jason Samse45ac6e2009-07-20 14:31:06 -070059
60//////////////////////////////////////////////////////////////////////////////
61// IO routines
62//////////////////////////////////////////////////////////////////////////////
63
64static float SC_loadF(uint32_t bank, uint32_t offset)
65{
66 GET_TLS();
67 const void *vp = sc->mSlots[bank]->getPtr();
68 const float *f = static_cast<const float *>(vp);
69 //LOGE("loadF %i %i = %f %x", bank, offset, f, ((int *)&f)[0]);
70 return f[offset];
71}
72
73static int32_t SC_loadI32(uint32_t bank, uint32_t offset)
74{
75 GET_TLS();
76 const void *vp = sc->mSlots[bank]->getPtr();
77 const int32_t *i = static_cast<const int32_t *>(vp);
78 //LOGE("loadI32 %i %i = %i", bank, offset, t);
79 return i[offset];
80}
81
Romain Guy06f7c932009-08-06 12:40:41 -070082static float* SC_loadArrayF(uint32_t bank, uint32_t offset)
Romain Guy36401002009-08-04 17:19:48 -070083{
84 GET_TLS();
85 void *vp = sc->mSlots[bank]->getPtr();
86 float *f = static_cast<float *>(vp);
Romain Guy06f7c932009-08-06 12:40:41 -070087 return f + offset;
Romain Guy36401002009-08-04 17:19:48 -070088}
89
Romain Guy06f7c932009-08-06 12:40:41 -070090static int32_t* SC_loadArrayI32(uint32_t bank, uint32_t offset)
Romain Guy36401002009-08-04 17:19:48 -070091{
92 GET_TLS();
93 void *vp = sc->mSlots[bank]->getPtr();
94 int32_t *i = static_cast<int32_t *>(vp);
Romain Guy06f7c932009-08-06 12:40:41 -070095 return i + offset;
Romain Guy36401002009-08-04 17:19:48 -070096}
97
Jason Samsa2b54c42009-09-23 16:38:37 -070098static float* SC_loadSimpleMeshVerticesF(RsSimpleMesh mesh, uint32_t idx)
Romain Guy48b7edc2009-08-06 22:52:13 -070099{
Jason Samsa2b54c42009-09-23 16:38:37 -0700100 SimpleMesh *tm = static_cast<SimpleMesh *>(mesh);
101 void *vp = tm->mVertexBuffers[idx]->getPtr();;
102 return static_cast<float *>(vp);
Romain Guy48b7edc2009-08-06 22:52:13 -0700103}
104
Jason Samsa2b54c42009-09-23 16:38:37 -0700105static void SC_updateSimpleMesh(RsSimpleMesh mesh)
Romain Guy48b7edc2009-08-06 22:52:13 -0700106{
Jason Samsc460e552009-11-25 13:22:07 -0800107 GET_TLS();
Jason Samsa2b54c42009-09-23 16:38:37 -0700108 SimpleMesh *sm = static_cast<SimpleMesh *>(mesh);
Jason Samsc460e552009-11-25 13:22:07 -0800109 sm->uploadAll(rsc);
Romain Guy48b7edc2009-08-06 22:52:13 -0700110}
Romain Guy06f7c932009-08-06 12:40:41 -0700111
Jason Samse45ac6e2009-07-20 14:31:06 -0700112static uint32_t SC_loadU32(uint32_t bank, uint32_t offset)
113{
114 GET_TLS();
115 const void *vp = sc->mSlots[bank]->getPtr();
116 const uint32_t *i = static_cast<const uint32_t *>(vp);
117 return i[offset];
118}
119
120static void SC_loadVec4(uint32_t bank, uint32_t offset, rsc_Vector4 *v)
121{
122 GET_TLS();
123 const void *vp = sc->mSlots[bank]->getPtr();
124 const float *f = static_cast<const float *>(vp);
125 memcpy(v, &f[offset], sizeof(rsc_Vector4));
126}
127
128static void SC_loadMatrix(uint32_t bank, uint32_t offset, rsc_Matrix *m)
129{
130 GET_TLS();
131 const void *vp = sc->mSlots[bank]->getPtr();
132 const float *f = static_cast<const float *>(vp);
133 memcpy(m, &f[offset], sizeof(rsc_Matrix));
134}
135
136
137static void SC_storeF(uint32_t bank, uint32_t offset, float v)
138{
139 //LOGE("storeF %i %i %f", bank, offset, v);
140 GET_TLS();
141 void *vp = sc->mSlots[bank]->getPtr();
142 float *f = static_cast<float *>(vp);
143 f[offset] = v;
144}
145
146static void SC_storeI32(uint32_t bank, uint32_t offset, int32_t v)
147{
148 GET_TLS();
149 void *vp = sc->mSlots[bank]->getPtr();
150 int32_t *f = static_cast<int32_t *>(vp);
151 static_cast<int32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
152}
153
154static void SC_storeU32(uint32_t bank, uint32_t offset, uint32_t v)
155{
156 GET_TLS();
157 void *vp = sc->mSlots[bank]->getPtr();
158 uint32_t *f = static_cast<uint32_t *>(vp);
159 static_cast<uint32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
160}
161
162static void SC_storeVec4(uint32_t bank, uint32_t offset, const rsc_Vector4 *v)
163{
164 GET_TLS();
165 void *vp = sc->mSlots[bank]->getPtr();
166 float *f = static_cast<float *>(vp);
167 memcpy(&f[offset], v, sizeof(rsc_Vector4));
168}
169
170static void SC_storeMatrix(uint32_t bank, uint32_t offset, const rsc_Matrix *m)
171{
172 GET_TLS();
173 void *vp = sc->mSlots[bank]->getPtr();
174 float *f = static_cast<float *>(vp);
175 memcpy(&f[offset], m, sizeof(rsc_Matrix));
176}
177
Jason Samsa57c0a72009-09-04 14:42:41 -0700178//////////////////////////////////////////////////////////////////////////////
179// Vec3 routines
180//////////////////////////////////////////////////////////////////////////////
181
182static void SC_vec3Norm(vec3_t *v)
183{
184 float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
185 len = 1 / len;
186 v->x *= len;
187 v->y *= len;
188 v->z *= len;
189}
190
191static float SC_vec3Length(const vec3_t *v)
192{
193 return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
194}
195
196static void SC_vec3Add(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
197{
198 dest->x = lhs->x + rhs->x;
199 dest->y = lhs->y + rhs->y;
200 dest->z = lhs->z + rhs->z;
201}
202
203static void SC_vec3Sub(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
204{
205 dest->x = lhs->x - rhs->x;
206 dest->y = lhs->y - rhs->y;
207 dest->z = lhs->z - rhs->z;
208}
209
210static void SC_vec3Cross(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
211{
212 float x = lhs->y * rhs->z - lhs->z * rhs->y;
213 float y = lhs->z * rhs->x - lhs->x * rhs->z;
214 float z = lhs->x * rhs->y - lhs->y * rhs->x;
215 dest->x = x;
216 dest->y = y;
217 dest->z = z;
218}
219
220static float SC_vec3Dot(const vec3_t *lhs, const vec3_t *rhs)
221{
222 return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z;
223}
224
225static void SC_vec3Scale(vec3_t *lhs, float scale)
226{
227 lhs->x *= scale;
228 lhs->y *= scale;
229 lhs->z *= scale;
230}
231
Romain Guyd6d4a5f2009-10-09 16:05:25 -0700232//////////////////////////////////////////////////////////////////////////////
233// Vec4 routines
234//////////////////////////////////////////////////////////////////////////////
235
236static void SC_vec4Norm(vec4_t *v)
237{
238 float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w);
239 len = 1 / len;
240 v->x *= len;
241 v->y *= len;
242 v->z *= len;
243 v->w *= len;
244}
245
246static float SC_vec4Length(const vec4_t *v)
247{
248 return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w);
249}
250
251static void SC_vec4Add(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs)
252{
253 dest->x = lhs->x + rhs->x;
254 dest->y = lhs->y + rhs->y;
255 dest->z = lhs->z + rhs->z;
256 dest->w = lhs->w + rhs->w;
257}
258
259static void SC_vec4Sub(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs)
260{
261 dest->x = lhs->x - rhs->x;
262 dest->y = lhs->y - rhs->y;
263 dest->z = lhs->z - rhs->z;
264 dest->w = lhs->w - rhs->w;
265}
266
267static float SC_vec4Dot(const vec4_t *lhs, const vec4_t *rhs)
268{
269 return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z + lhs->w * rhs->w;
270}
271
272static void SC_vec4Scale(vec4_t *lhs, float scale)
273{
274 lhs->x *= scale;
275 lhs->y *= scale;
276 lhs->z *= scale;
277 lhs->w *= scale;
278}
Jason Samse45ac6e2009-07-20 14:31:06 -0700279
280//////////////////////////////////////////////////////////////////////////////
281// Math routines
282//////////////////////////////////////////////////////////////////////////////
283
Romain Guy39dbc802009-07-31 11:20:59 -0700284#define PI 3.1415926f
285#define DEG_TO_RAD PI / 180.0f
286#define RAD_TO_DEG 180.0f / PI
287
Romain Guy2275d632009-08-18 11:39:17 -0700288static float SC_sinf_fast(float x)
289{
290 const float A = 1.0f / (2.0f * M_PI);
291 const float B = -16.0f;
292 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -0700293
Romain Guy2275d632009-08-18 11:39:17 -0700294 // scale angle for easy argument reduction
295 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -0700296
Romain Guy2275d632009-08-18 11:39:17 -0700297 if (fabsf(x) >= 0.5f) {
298 // argument reduction
299 x = x - ceilf(x + 0.5f) + 1.0f;
300 }
Jason Samsa57c0a72009-09-04 14:42:41 -0700301
Romain Guy2275d632009-08-18 11:39:17 -0700302 const float y = B * x * fabsf(x) + C * x;
303 return 0.2215f * (y * fabsf(y) - y) + y;
304}
305
306static float SC_cosf_fast(float x)
307{
308 x += float(M_PI / 2);
309
310 const float A = 1.0f / (2.0f * M_PI);
311 const float B = -16.0f;
312 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -0700313
Romain Guy2275d632009-08-18 11:39:17 -0700314 // scale angle for easy argument reduction
315 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -0700316
Romain Guy2275d632009-08-18 11:39:17 -0700317 if (fabsf(x) >= 0.5f) {
318 // argument reduction
319 x = x - ceilf(x + 0.5f) + 1.0f;
320 }
Jason Samsa57c0a72009-09-04 14:42:41 -0700321
Romain Guy2275d632009-08-18 11:39:17 -0700322 const float y = B * x * fabsf(x) + C * x;
323 return 0.2215f * (y * fabsf(y) - y) + y;
324}
325
Jason Samse45ac6e2009-07-20 14:31:06 -0700326static float SC_randf(float max)
327{
328 float r = (float)rand();
329 return r / RAND_MAX * max;
330}
331
Romain Guy39dbc802009-07-31 11:20:59 -0700332static float SC_randf2(float min, float max)
333{
334 float r = (float)rand();
335 return r / RAND_MAX * (max - min) + min;
336}
337
Romain Guyd6d4a5f2009-10-09 16:05:25 -0700338static int SC_sign(int value)
339{
340 return (value > 0) - (value < 0);
341}
342
343static float SC_signf(float value)
344{
345 return (value > 0) - (value < 0);
346}
347
Romain Guy39dbc802009-07-31 11:20:59 -0700348static float SC_clampf(float amount, float low, float high)
349{
350 return amount < low ? low : (amount > high ? high : amount);
351}
352
Romain Guy27162ab2009-08-09 17:04:54 -0700353static int SC_clamp(int amount, int low, int high)
354{
355 return amount < low ? low : (amount > high ? high : amount);
356}
357
Romain Guy39dbc802009-07-31 11:20:59 -0700358static float SC_maxf(float a, float b)
359{
Jason Samse5ffb872009-08-09 17:01:55 -0700360 return a > b ? a : b;
Romain Guy39dbc802009-07-31 11:20:59 -0700361}
362
363static float SC_minf(float a, float b)
364{
Jason Samse5ffb872009-08-09 17:01:55 -0700365 return a < b ? a : b;
Romain Guy39dbc802009-07-31 11:20:59 -0700366}
367
368static float SC_sqrf(float v)
369{
Jason Samse5ffb872009-08-09 17:01:55 -0700370 return v * v;
Romain Guy39dbc802009-07-31 11:20:59 -0700371}
372
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700373static int SC_sqr(int v)
374{
375 return v * v;
376}
377
Jason Samsdac98f52009-09-18 14:24:24 -0700378static float SC_fracf(float v)
379{
380 return v - floorf(v);
381}
382
383static float SC_roundf(float v)
384{
385 return floorf(v + 0.4999999999);
386}
387
Romain Guy39dbc802009-07-31 11:20:59 -0700388static float SC_distf2(float x1, float y1, float x2, float y2)
389{
390 float x = x2 - x1;
391 float y = y2 - y1;
Jason Samse5ffb872009-08-09 17:01:55 -0700392 return sqrtf(x * x + y * y);
Romain Guy39dbc802009-07-31 11:20:59 -0700393}
394
395static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2)
396{
397 float x = x2 - x1;
398 float y = y2 - y1;
399 float z = z2 - z1;
Jason Samse5ffb872009-08-09 17:01:55 -0700400 return sqrtf(x * x + y * y + z * z);
Romain Guy39dbc802009-07-31 11:20:59 -0700401}
402
403static float SC_magf2(float a, float b)
404{
405 return sqrtf(a * a + b * b);
406}
407
408static float SC_magf3(float a, float b, float c)
409{
410 return sqrtf(a * a + b * b + c * c);
411}
412
413static float SC_radf(float degrees)
414{
Jason Samse5ffb872009-08-09 17:01:55 -0700415 return degrees * DEG_TO_RAD;
Romain Guy39dbc802009-07-31 11:20:59 -0700416}
417
418static float SC_degf(float radians)
419{
Jason Samse5ffb872009-08-09 17:01:55 -0700420 return radians * RAD_TO_DEG;
Romain Guy39dbc802009-07-31 11:20:59 -0700421}
422
423static float SC_lerpf(float start, float stop, float amount)
424{
425 return start + (stop - start) * amount;
426}
427
428static float SC_normf(float start, float stop, float value)
429{
430 return (value - start) / (stop - start);
431}
432
433static float SC_mapf(float minStart, float minStop, float maxStart, float maxStop, float value)
434{
435 return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
436}
Jason Samse45ac6e2009-07-20 14:31:06 -0700437
Romain Guy98e10fd2009-07-30 18:45:01 -0700438//////////////////////////////////////////////////////////////////////////////
439// Time routines
440//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700441
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700442static int32_t SC_second()
Romain Guy98e10fd2009-07-30 18:45:01 -0700443{
444 GET_TLS();
445
446 time_t rawtime;
447 time(&rawtime);
448
Romain Guy519cdc92009-11-11 15:36:06 -0800449 struct tm *timeinfo;
450 timeinfo = localtime(&rawtime);
451 return timeinfo->tm_sec;
Romain Guy98e10fd2009-07-30 18:45:01 -0700452}
453
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700454static int32_t SC_minute()
Romain Guy98e10fd2009-07-30 18:45:01 -0700455{
456 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700457
Romain Guy98e10fd2009-07-30 18:45:01 -0700458 time_t rawtime;
459 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700460
Romain Guy519cdc92009-11-11 15:36:06 -0800461 struct tm *timeinfo;
462 timeinfo = localtime(&rawtime);
463 return timeinfo->tm_min;
Jason Samse5ffb872009-08-09 17:01:55 -0700464}
Romain Guy98e10fd2009-07-30 18:45:01 -0700465
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700466static int32_t SC_hour()
Romain Guy98e10fd2009-07-30 18:45:01 -0700467{
468 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700469
Romain Guy98e10fd2009-07-30 18:45:01 -0700470 time_t rawtime;
471 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700472
Romain Guy519cdc92009-11-11 15:36:06 -0800473 struct tm *timeinfo;
474 timeinfo = localtime(&rawtime);
475 return timeinfo->tm_hour;
Romain Guy39dbc802009-07-31 11:20:59 -0700476}
477
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700478static int32_t SC_day()
Romain Guy39dbc802009-07-31 11:20:59 -0700479{
480 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700481
Romain Guy39dbc802009-07-31 11:20:59 -0700482 time_t rawtime;
483 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700484
Romain Guy519cdc92009-11-11 15:36:06 -0800485 struct tm *timeinfo;
486 timeinfo = localtime(&rawtime);
487 return timeinfo->tm_mday;
Jason Samse5ffb872009-08-09 17:01:55 -0700488}
Jason Samse45ac6e2009-07-20 14:31:06 -0700489
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700490static int32_t SC_month()
Romain Guy39dbc802009-07-31 11:20:59 -0700491{
492 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700493
Romain Guy39dbc802009-07-31 11:20:59 -0700494 time_t rawtime;
495 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700496
Romain Guy519cdc92009-11-11 15:36:06 -0800497 struct tm *timeinfo;
498 timeinfo = localtime(&rawtime);
499 return timeinfo->tm_mon;
Jason Samse5ffb872009-08-09 17:01:55 -0700500}
Romain Guy39dbc802009-07-31 11:20:59 -0700501
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700502static int32_t SC_year()
Romain Guy39dbc802009-07-31 11:20:59 -0700503{
504 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700505
Romain Guy39dbc802009-07-31 11:20:59 -0700506 time_t rawtime;
507 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700508
Romain Guy519cdc92009-11-11 15:36:06 -0800509 struct tm *timeinfo;
510 timeinfo = localtime(&rawtime);
511 return timeinfo->tm_year;
Romain Guy39dbc802009-07-31 11:20:59 -0700512}
513
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700514static int32_t SC_uptimeMillis()
515{
516 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
517}
518
519static int32_t SC_startTimeMillis()
520{
521 GET_TLS();
522 return sc->mEnviroment.mStartTimeMillis;
523}
524
525static int32_t SC_elapsedTimeMillis()
526{
527 GET_TLS();
528 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC))
529 - sc->mEnviroment.mStartTimeMillis;
530}
531
Jason Samse45ac6e2009-07-20 14:31:06 -0700532//////////////////////////////////////////////////////////////////////////////
533// Matrix routines
534//////////////////////////////////////////////////////////////////////////////
535
536
537static void SC_matrixLoadIdentity(rsc_Matrix *mat)
538{
539 Matrix *m = reinterpret_cast<Matrix *>(mat);
540 m->loadIdentity();
541}
542
543static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
544{
545 Matrix *m = reinterpret_cast<Matrix *>(mat);
546 m->load(f);
547}
548
549static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
550{
551 Matrix *m = reinterpret_cast<Matrix *>(mat);
552 m->load(reinterpret_cast<const Matrix *>(newmat));
553}
554
555static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
556{
557 Matrix *m = reinterpret_cast<Matrix *>(mat);
558 m->loadRotate(rot, x, y, z);
559}
560
561static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
562{
563 Matrix *m = reinterpret_cast<Matrix *>(mat);
564 m->loadScale(x, y, z);
565}
566
567static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
568{
569 Matrix *m = reinterpret_cast<Matrix *>(mat);
570 m->loadTranslate(x, y, z);
571}
572
573static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
574{
575 Matrix *m = reinterpret_cast<Matrix *>(mat);
576 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
577 reinterpret_cast<const Matrix *>(rhs));
578}
579
580static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
581{
582 Matrix *m = reinterpret_cast<Matrix *>(mat);
583 m->multiply(reinterpret_cast<const Matrix *>(rhs));
584}
585
586static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
587{
588 Matrix *m = reinterpret_cast<Matrix *>(mat);
589 m->rotate(rot, x, y, z);
590}
591
592static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
593{
594 Matrix *m = reinterpret_cast<Matrix *>(mat);
595 m->scale(x, y, z);
596}
597
598static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
599{
600 Matrix *m = reinterpret_cast<Matrix *>(mat);
601 m->translate(x, y, z);
602}
603
604
Jason Sams90b36a82009-08-17 13:56:09 -0700605static void SC_vec2Rand(float *vec, float maxLen)
606{
607 float angle = SC_randf(PI * 2);
608 float len = SC_randf(maxLen);
609 vec[0] = len * sinf(angle);
610 vec[1] = len * cosf(angle);
611}
612
Jason Samse45ac6e2009-07-20 14:31:06 -0700613
614
615//////////////////////////////////////////////////////////////////////////////
616// Context
617//////////////////////////////////////////////////////////////////////////////
618
619static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
620{
621 GET_TLS();
Jason Sams7dad9c32009-12-17 16:55:08 -0800622 rsi_ProgramBindTexture(rsc,
623 static_cast<ProgramFragment *>(vpf),
624 slot,
625 static_cast<Allocation *>(va));
Jason Samse45ac6e2009-07-20 14:31:06 -0700626
627}
628
629static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
630{
631 GET_TLS();
Jason Sams7dad9c32009-12-17 16:55:08 -0800632 rsi_ProgramBindSampler(rsc,
633 static_cast<ProgramFragment *>(vpf),
634 slot,
635 static_cast<Sampler *>(vs));
Jason Samse45ac6e2009-07-20 14:31:06 -0700636
637}
638
639static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs)
640{
641 GET_TLS();
642 rsi_ContextBindProgramFragmentStore(rsc, pfs);
643
644}
645
646static void SC_bindProgramFragment(RsProgramFragment pf)
647{
648 GET_TLS();
649 rsi_ContextBindProgramFragment(rsc, pf);
650
651}
652
Jason Samsb5909ce2009-07-21 12:20:54 -0700653static void SC_bindProgramVertex(RsProgramVertex pv)
654{
655 GET_TLS();
656 rsi_ContextBindProgramVertex(rsc, pv);
657
658}
Jason Samse45ac6e2009-07-20 14:31:06 -0700659
660//////////////////////////////////////////////////////////////////////////////
Jason Samsc9d43db2009-07-28 12:02:16 -0700661// VP
662//////////////////////////////////////////////////////////////////////////////
663
664static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
665{
666 GET_TLS();
667 rsc->getVertex()->setModelviewMatrix(m);
668}
669
670static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
671{
672 GET_TLS();
673 rsc->getVertex()->setTextureMatrix(m);
674}
675
676
677
678//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700679// Drawing
680//////////////////////////////////////////////////////////////////////////////
681
Romain Guyd369e272009-08-07 15:40:32 -0700682static void SC_drawLine(float x1, float y1, float z1,
683 float x2, float y2, float z2)
684{
685 GET_TLS();
686 rsc->setupCheck();
687
688 float vtx[] = { x1, y1, z1, x2, y2, z2 };
Jason Samsc460e552009-11-25 13:22:07 -0800689 VertexArray va;
690 va.setPosition(2, GL_FLOAT, 12, (uint32_t)&vtx);
691 if (rsc->checkVersion2_0()) {
Jason Samsd01d9702009-12-23 14:35:29 -0800692 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsc460e552009-11-25 13:22:07 -0800693 } else {
Jason Samsd01d9702009-12-23 14:35:29 -0800694 va.setupGL(rsc, &rsc->mStateVertexArray);
Jason Samsc460e552009-11-25 13:22:07 -0800695 }
Romain Guyd369e272009-08-07 15:40:32 -0700696
697 glDrawArrays(GL_LINES, 0, 2);
698}
699
Jason Samsb681c8a2009-09-28 18:12:56 -0700700static void SC_drawPoint(float x, float y, float z)
701{
702 GET_TLS();
703 rsc->setupCheck();
704
705 float vtx[] = { x, y, z };
706
Jason Samsc460e552009-11-25 13:22:07 -0800707 VertexArray va;
708 va.setPosition(1, GL_FLOAT, 12, (uint32_t)&vtx);
709 if (rsc->checkVersion2_0()) {
Jason Samsd01d9702009-12-23 14:35:29 -0800710 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsc460e552009-11-25 13:22:07 -0800711 } else {
Jason Samsd01d9702009-12-23 14:35:29 -0800712 va.setupGL(rsc, &rsc->mStateVertexArray);
Jason Samsc460e552009-11-25 13:22:07 -0800713 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700714
715 glDrawArrays(GL_POINTS, 0, 1);
716}
717
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700718static void SC_drawQuadTexCoords(float x1, float y1, float z1,
719 float u1, float v1,
720 float x2, float y2, float z2,
721 float u2, float v2,
722 float x3, float y3, float z3,
723 float u3, float v3,
724 float x4, float y4, float z4,
725 float u4, float v4)
Jason Samse45ac6e2009-07-20 14:31:06 -0700726{
727 GET_TLS();
Jason Samsc460e552009-11-25 13:22:07 -0800728 rsc->setupCheck();
Jason Samse579df42009-08-10 14:55:26 -0700729
Jason Samse45ac6e2009-07-20 14:31:06 -0700730 //LOGE("Quad");
731 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
732 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
733 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
734 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
Jason Samse579df42009-08-10 14:55:26 -0700735
Jason Samse45ac6e2009-07-20 14:31:06 -0700736 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700737 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
Jason Samse45ac6e2009-07-20 14:31:06 -0700738
Jason Samsc460e552009-11-25 13:22:07 -0800739 VertexArray va;
Jason Samsb2a219b2009-12-16 14:24:17 -0800740 va.setPosition(3, GL_FLOAT, 12, (uint32_t)&vtx);
Jason Sams433eca32010-01-06 11:57:52 -0800741 va.setTexture(2, GL_FLOAT, 8, (uint32_t)&tex);
Jason Samsc460e552009-11-25 13:22:07 -0800742 if (rsc->checkVersion2_0()) {
Jason Samsd01d9702009-12-23 14:35:29 -0800743 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsc460e552009-11-25 13:22:07 -0800744 } else {
Jason Samsd01d9702009-12-23 14:35:29 -0800745 va.setupGL(rsc, &rsc->mStateVertexArray);
Jason Samsc460e552009-11-25 13:22:07 -0800746 }
Jason Samse45ac6e2009-07-20 14:31:06 -0700747
Jason Samse45ac6e2009-07-20 14:31:06 -0700748
749 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
750}
751
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700752static void SC_drawQuad(float x1, float y1, float z1,
753 float x2, float y2, float z2,
754 float x3, float y3, float z3,
755 float x4, float y4, float z4)
756{
757 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
758 x2, y2, z2, 1, 1,
759 x3, y3, z3, 1, 0,
760 x4, y4, z4, 0, 0);
761}
762
Jason Sams3a97c592009-09-30 17:36:20 -0700763static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h)
764{
765 GET_TLS();
Jason Samsc460e552009-11-25 13:22:07 -0800766 ObjectBaseRef<const ProgramVertex> tmp(rsc->getVertex());
767 rsc->setVertex(rsc->getDefaultProgramVertex());
768 //rsc->setupCheck();
Jason Sams3a97c592009-09-30 17:36:20 -0700769
Jason Samsc460e552009-11-25 13:22:07 -0800770 //GLint crop[4] = {0, h, w, -h};
771
772 float sh = rsc->getHeight();
773
774 SC_drawQuad(x, sh - y, z,
775 x+w, sh - y, z,
776 x+w, sh - (y+h), z,
777 x, sh - (y+h), z);
778 rsc->setVertex((ProgramVertex *)tmp.get());
Jason Sams3a97c592009-09-30 17:36:20 -0700779}
780
781static void SC_drawSprite(float x, float y, float z, float w, float h)
782{
783 GET_TLS();
Jason Sams3a97c592009-09-30 17:36:20 -0700784 float vin[3] = {x, y, z};
785 float vout[4];
786
787 //LOGE("ds in %f %f %f", x, y, z);
788 rsc->getVertex()->transformToScreen(rsc, vout, vin);
789 //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]);
790 vout[0] /= vout[3];
791 vout[1] /= vout[3];
792 vout[2] /= vout[3];
793
794 vout[0] *= rsc->getWidth() / 2;
795 vout[1] *= rsc->getHeight() / 2;
796 vout[0] += rsc->getWidth() / 2;
797 vout[1] += rsc->getHeight() / 2;
798
799 vout[0] -= w/2;
800 vout[1] -= h/2;
801
802 //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]);
803
804 // U, V, W, H
Jason Samsc460e552009-11-25 13:22:07 -0800805 SC_drawSpriteScreenspace(vout[0], vout[1], z, h, w);
806 //rsc->setupCheck();
Jason Sams3a97c592009-09-30 17:36:20 -0700807}
808
809
Jason Samse9f5c532009-07-28 17:20:11 -0700810static void SC_drawRect(float x1, float y1,
811 float x2, float y2, float z)
812{
813 SC_drawQuad(x1, y2, z,
814 x2, y2, z,
815 x2, y1, z,
816 x1, y1, z);
817}
818
Jason Samse5ffb872009-08-09 17:01:55 -0700819static void SC_drawSimpleMesh(RsSimpleMesh vsm)
820{
821 GET_TLS();
822 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
823 rsc->setupCheck();
Jason Samsc460e552009-11-25 13:22:07 -0800824 sm->render(rsc);
Jason Samse5ffb872009-08-09 17:01:55 -0700825}
826
827static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len)
828{
829 GET_TLS();
830 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
831 rsc->setupCheck();
Jason Samsc460e552009-11-25 13:22:07 -0800832 sm->renderRange(rsc, start, len);
Jason Samse5ffb872009-08-09 17:01:55 -0700833}
834
835
Jason Samse45ac6e2009-07-20 14:31:06 -0700836//////////////////////////////////////////////////////////////////////////////
837//
838//////////////////////////////////////////////////////////////////////////////
839
Jason Samse45ac6e2009-07-20 14:31:06 -0700840static void SC_color(float r, float g, float b, float a)
841{
Jason Samsc460e552009-11-25 13:22:07 -0800842 GET_TLS();
Jason Samse9ed6cc2009-12-16 14:13:06 -0800843 rsc->mStateVertex.color[0] = r;
844 rsc->mStateVertex.color[1] = g;
845 rsc->mStateVertex.color[2] = b;
846 rsc->mStateVertex.color[3] = a;
Jason Sams7dad9c32009-12-17 16:55:08 -0800847 if (!rsc->checkVersion2_0()) {
Jason Samsc460e552009-11-25 13:22:07 -0800848 glColor4f(r, g, b, a);
849 }
Jason Samse45ac6e2009-07-20 14:31:06 -0700850}
851
Romain Guy48b7edc2009-08-06 22:52:13 -0700852static void SC_ambient(float r, float g, float b, float a)
853{
854 GLfloat params[] = { r, g, b, a };
855 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, params);
856}
857
858static void SC_diffuse(float r, float g, float b, float a)
859{
860 GLfloat params[] = { r, g, b, a };
861 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, params);
862}
863
864static void SC_specular(float r, float g, float b, float a)
865{
866 GLfloat params[] = { r, g, b, a };
867 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, params);
868}
869
870static void SC_emission(float r, float g, float b, float a)
871{
872 GLfloat params[] = { r, g, b, a };
873 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, params);
874}
875
Romain Guyd369e272009-08-07 15:40:32 -0700876static void SC_shininess(float s)
Romain Guy48b7edc2009-08-06 22:52:13 -0700877{
Romain Guyd369e272009-08-07 15:40:32 -0700878 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, s);
Romain Guy48b7edc2009-08-06 22:52:13 -0700879}
880
Romain Guye62cc902009-09-04 17:55:41 -0700881static void SC_pointAttenuation(float a, float b, float c)
882{
883 GLfloat params[] = { a, b, c };
884 glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, params);
885}
886
Romain Guy370ed152009-08-20 17:08:33 -0700887static void SC_hsbToRgb(float h, float s, float b, float* rgb)
Romain Guy9c59d022009-07-31 15:33:59 -0700888{
889 float red = 0.0f;
890 float green = 0.0f;
891 float blue = 0.0f;
Jason Samse5ffb872009-08-09 17:01:55 -0700892
Romain Guy9c59d022009-07-31 15:33:59 -0700893 float x = h;
894 float y = s;
895 float z = b;
Jason Samse5ffb872009-08-09 17:01:55 -0700896
Romain Guy9c59d022009-07-31 15:33:59 -0700897 float hf = (x - (int) x) * 6.0f;
898 int ihf = (int) hf;
899 float f = hf - ihf;
900 float pv = z * (1.0f - y);
901 float qv = z * (1.0f - y * f);
902 float tv = z * (1.0f - y * (1.0f - f));
Jason Samse5ffb872009-08-09 17:01:55 -0700903
Romain Guy9c59d022009-07-31 15:33:59 -0700904 switch (ihf) {
905 case 0: // Red is the dominant color
906 red = z;
907 green = tv;
908 blue = pv;
909 break;
910 case 1: // Green is the dominant color
911 red = qv;
912 green = z;
913 blue = pv;
914 break;
915 case 2:
916 red = pv;
917 green = z;
918 blue = tv;
919 break;
920 case 3: // Blue is the dominant color
921 red = pv;
922 green = qv;
923 blue = z;
924 break;
925 case 4:
926 red = tv;
927 green = pv;
928 blue = z;
929 break;
930 case 5: // Red is the dominant color
931 red = z;
932 green = pv;
933 blue = qv;
934 break;
935 }
Jason Samse5ffb872009-08-09 17:01:55 -0700936
Romain Guy370ed152009-08-20 17:08:33 -0700937 rgb[0] = red;
938 rgb[1] = green;
939 rgb[2] = blue;
940}
941
942static int SC_hsbToAbgr(float h, float s, float b, float a)
943{
944 float rgb[3];
945 SC_hsbToRgb(h, s, b, rgb);
946 return int(a * 255.0f) << 24 |
947 int(rgb[2] * 255.0f) << 16 |
948 int(rgb[1] * 255.0f) << 8 |
949 int(rgb[0] * 255.0f);
950}
951
952static void SC_hsb(float h, float s, float b, float a)
953{
Jason Samsc460e552009-11-25 13:22:07 -0800954 GET_TLS();
Romain Guy370ed152009-08-20 17:08:33 -0700955 float rgb[3];
956 SC_hsbToRgb(h, s, b, rgb);
Jason Samsc460e552009-11-25 13:22:07 -0800957 if (rsc->checkVersion2_0()) {
958 glVertexAttrib4f(1, rgb[0], rgb[1], rgb[2], a);
959 } else {
960 glColor4f(rgb[0], rgb[1], rgb[2], a);
961 }
Romain Guy9c59d022009-07-31 15:33:59 -0700962}
963
Jason Samsc9d43db2009-07-28 12:02:16 -0700964static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samse45ac6e2009-07-20 14:31:06 -0700965{
966 GET_TLS();
967 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
968}
969
Jason Samse5ffb872009-08-09 17:01:55 -0700970static void SC_uploadToBufferObject(RsAllocation va)
971{
972 GET_TLS();
973 rsi_AllocationUploadToBufferObject(rsc, va);
974}
975
Jason Sams9ebb0c42010-01-12 12:12:28 -0800976static void SC_syncToGL(RsAllocation va)
977{
978 GET_TLS();
979 Allocation *a = static_cast<Allocation *>(va);
980
981}
982
Jason Samse45ac6e2009-07-20 14:31:06 -0700983static void SC_ClearColor(float r, float g, float b, float a)
984{
985 //LOGE("c %f %f %f %f", r, g, b, a);
986 GET_TLS();
987 sc->mEnviroment.mClearColor[0] = r;
988 sc->mEnviroment.mClearColor[1] = g;
989 sc->mEnviroment.mClearColor[2] = b;
990 sc->mEnviroment.mClearColor[3] = a;
991}
992
Jason Samsc9d43db2009-07-28 12:02:16 -0700993static void SC_debugF(const char *s, float f)
994{
995 LOGE("%s %f", s, f);
996}
997
Romain Guy370ed152009-08-20 17:08:33 -0700998static void SC_debugHexF(const char *s, float f)
999{
1000 LOGE("%s 0x%x", s, *((int *) (&f)));
1001}
1002
Jason Samsc9d43db2009-07-28 12:02:16 -07001003static void SC_debugI32(const char *s, int32_t i)
1004{
1005 LOGE("%s %i", s, i);
1006}
1007
Romain Guy370ed152009-08-20 17:08:33 -07001008static void SC_debugHexI32(const char *s, int32_t i)
1009{
1010 LOGE("%s 0x%x", s, i);
1011}
1012
Jason Samse579df42009-08-10 14:55:26 -07001013static uint32_t SC_getWidth()
1014{
1015 GET_TLS();
1016 return rsc->getWidth();
1017}
Jason Samse45ac6e2009-07-20 14:31:06 -07001018
Jason Samse579df42009-08-10 14:55:26 -07001019static uint32_t SC_getHeight()
1020{
1021 GET_TLS();
1022 return rsc->getHeight();
1023}
Jason Samse45ac6e2009-07-20 14:31:06 -07001024
Jason Sams90b36a82009-08-17 13:56:09 -07001025static uint32_t SC_colorFloatRGBAtoUNorm8(float r, float g, float b, float a)
1026{
1027 uint32_t c = 0;
1028 c |= (uint32_t)(r * 255.f + 0.5f);
1029 c |= ((uint32_t)(g * 255.f + 0.5f)) << 8;
1030 c |= ((uint32_t)(b * 255.f + 0.5f)) << 16;
1031 c |= ((uint32_t)(a * 255.f + 0.5f)) << 24;
1032 return c;
1033}
1034
1035static uint32_t SC_colorFloatRGBAto565(float r, float g, float b)
1036{
1037 uint32_t ir = (uint32_t)(r * 255.f + 0.5f);
1038 uint32_t ig = (uint32_t)(g * 255.f + 0.5f);
1039 uint32_t ib = (uint32_t)(b * 255.f + 0.5f);
1040 return rs888to565(ir, ig, ib);
1041}
1042
Jason Sams8c401ef2009-10-06 13:58:47 -07001043static uint32_t SC_toClient(void *data, int cmdID, int len, int waitForSpace)
1044{
1045 GET_TLS();
1046 return rsc->sendMessageToClient(data, cmdID, len, waitForSpace != 0);
1047}
1048
Jason Sams3a27c952009-10-07 18:14:01 -07001049static void SC_scriptCall(int scriptID)
1050{
1051 GET_TLS();
1052 rsc->runScript((Script *)scriptID, 0);
1053}
1054
1055
Jason Samse45ac6e2009-07-20 14:31:06 -07001056//////////////////////////////////////////////////////////////////////////////
1057// Class implementation
1058//////////////////////////////////////////////////////////////////////////////
1059
1060ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
1061 // IO
1062 { "loadI32", (void *)&SC_loadI32,
1063 "int", "(int, int)" },
1064 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
1065 { "loadF", (void *)&SC_loadF,
1066 "float", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -07001067 { "loadArrayF", (void *)&SC_loadArrayF,
Romain Guy06f7c932009-08-06 12:40:41 -07001068 "float*", "(int, int)" },
Romain Guy36401002009-08-04 17:19:48 -07001069 { "loadArrayI32", (void *)&SC_loadArrayI32,
Romain Guy06f7c932009-08-06 12:40:41 -07001070 "int*", "(int, int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001071 { "loadVec4", (void *)&SC_loadVec4,
1072 "void", "(int, int, float *)" },
1073 { "loadMatrix", (void *)&SC_loadMatrix,
1074 "void", "(int, int, float *)" },
1075 { "storeI32", (void *)&SC_storeI32,
1076 "void", "(int, int, int)" },
1077 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
1078 { "storeF", (void *)&SC_storeF,
1079 "void", "(int, int, float)" },
1080 { "storeVec4", (void *)&SC_storeVec4,
1081 "void", "(int, int, float *)" },
1082 { "storeMatrix", (void *)&SC_storeMatrix,
1083 "void", "(int, int, float *)" },
Jason Samsa2b54c42009-09-23 16:38:37 -07001084 { "loadSimpleMeshVerticesF", (void *)&SC_loadSimpleMeshVerticesF,
1085 "float*", "(int, int)" },
1086 { "updateSimpleMesh", (void *)&SC_updateSimpleMesh,
Romain Guy48b7edc2009-08-06 22:52:13 -07001087 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001088
1089 // math
Romain Guy27162ab2009-08-09 17:04:54 -07001090 { "modf", (void *)&fmod,
1091 "float", "(float, float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -07001092 { "abs", (void *)&abs,
1093 "int", "(int)" },
Romain Guyc5174c72009-09-29 13:17:27 -07001094 { "absf", (void *)&fabsf,
Romain Guyfcc1c2b2009-08-08 18:30:19 -07001095 "float", "(float)" },
Romain Guy2275d632009-08-18 11:39:17 -07001096 { "sinf_fast", (void *)&SC_sinf_fast,
1097 "float", "(float)" },
1098 { "cosf_fast", (void *)&SC_cosf_fast,
1099 "float", "(float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001100 { "sinf", (void *)&sinf,
1101 "float", "(float)" },
1102 { "cosf", (void *)&cosf,
1103 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001104 { "asinf", (void *)&asinf,
1105 "float", "(float)" },
1106 { "acosf", (void *)&acosf,
1107 "float", "(float)" },
1108 { "atanf", (void *)&atanf,
1109 "float", "(float)" },
1110 { "atan2f", (void *)&atan2f,
Romain Guy9c59d022009-07-31 15:33:59 -07001111 "float", "(float, float)" },
Jason Samse9f5c532009-07-28 17:20:11 -07001112 { "fabsf", (void *)&fabsf,
Jason Samse45ac6e2009-07-20 14:31:06 -07001113 "float", "(float)" },
1114 { "randf", (void *)&SC_randf,
1115 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001116 { "randf2", (void *)&SC_randf2,
1117 "float", "(float, float)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001118 { "floorf", (void *)&floorf,
1119 "float", "(float)" },
Jason Samsdac98f52009-09-18 14:24:24 -07001120 { "fracf", (void *)&SC_fracf,
1121 "float", "(float)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001122 { "ceilf", (void *)&ceilf,
1123 "float", "(float)" },
Jason Samsdac98f52009-09-18 14:24:24 -07001124 { "roundf", (void *)&SC_roundf,
1125 "float", "(float)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001126 { "expf", (void *)&expf,
1127 "float", "(float)" },
1128 { "logf", (void *)&logf,
1129 "float", "(float)" },
1130 { "powf", (void *)&powf,
1131 "float", "(float, float)" },
1132 { "maxf", (void *)&SC_maxf,
1133 "float", "(float, float)" },
1134 { "minf", (void *)&SC_minf,
1135 "float", "(float, float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -07001136 { "sqrt", (void *)&sqrt,
1137 "int", "(int)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001138 { "sqrtf", (void *)&sqrtf,
1139 "float", "(float)" },
Romain Guyfcc1c2b2009-08-08 18:30:19 -07001140 { "sqr", (void *)&SC_sqr,
1141 "int", "(int)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001142 { "sqrf", (void *)&SC_sqrf,
1143 "float", "(float)" },
Romain Guyd6d4a5f2009-10-09 16:05:25 -07001144 { "sign", (void *)&SC_sign,
1145 "int", "(int)" },
1146 { "signf", (void *)&SC_signf,
1147 "float", "(float)" },
Romain Guy27162ab2009-08-09 17:04:54 -07001148 { "clamp", (void *)&SC_clamp,
1149 "int", "(int, int, int)" },
Romain Guy39dbc802009-07-31 11:20:59 -07001150 { "clampf", (void *)&SC_clampf,
1151 "float", "(float, float, float)" },
1152 { "distf2", (void *)&SC_distf2,
1153 "float", "(float, float, float, float)" },
1154 { "distf3", (void *)&SC_distf3,
1155 "float", "(float, float, float, float, float, float)" },
1156 { "magf2", (void *)&SC_magf2,
1157 "float", "(float, float)" },
1158 { "magf3", (void *)&SC_magf3,
1159 "float", "(float, float, float)" },
1160 { "radf", (void *)&SC_radf,
1161 "float", "(float)" },
1162 { "degf", (void *)&SC_degf,
1163 "float", "(float)" },
1164 { "lerpf", (void *)&SC_lerpf,
1165 "float", "(float, float, float)" },
1166 { "normf", (void *)&SC_normf,
1167 "float", "(float, float, float)" },
1168 { "mapf", (void *)&SC_mapf,
1169 "float", "(float, float, float, float, float)" },
Romain Guyb7f1a6d2009-08-03 21:12:51 -07001170 { "noisef", (void *)&SC_noisef,
1171 "float", "(float)" },
1172 { "noisef2", (void *)&SC_noisef2,
1173 "float", "(float, float)" },
1174 { "noisef3", (void *)&SC_noisef3,
1175 "float", "(float, float, float)" },
1176 { "turbulencef2", (void *)&SC_turbulencef2,
1177 "float", "(float, float, float)" },
1178 { "turbulencef3", (void *)&SC_turbulencef3,
1179 "float", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001180
Romain Guy98e10fd2009-07-30 18:45:01 -07001181 // time
1182 { "second", (void *)&SC_second,
1183 "int", "()" },
1184 { "minute", (void *)&SC_minute,
1185 "int", "()" },
1186 { "hour", (void *)&SC_hour,
1187 "int", "()" },
Romain Guy39dbc802009-07-31 11:20:59 -07001188 { "day", (void *)&SC_day,
1189 "int", "()" },
1190 { "month", (void *)&SC_month,
1191 "int", "()" },
1192 { "year", (void *)&SC_year,
1193 "int", "()" },
Joe Onorato9c4e4ca2009-08-09 11:39:02 -07001194 { "uptimeMillis", (void*)&SC_uptimeMillis,
1195 "int", "()" }, // TODO: use long instead
1196 { "startTimeMillis", (void*)&SC_startTimeMillis,
1197 "int", "()" }, // TODO: use long instead
1198 { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis,
1199 "int", "()" }, // TODO: use long instead
Romain Guy98e10fd2009-07-30 18:45:01 -07001200
Jason Samse45ac6e2009-07-20 14:31:06 -07001201 // matrix
1202 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
1203 "void", "(float *mat)" },
1204 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
1205 "void", "(float *mat, float *f)" },
1206 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
1207 "void", "(float *mat, float *newmat)" },
1208 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
1209 "void", "(float *mat, float rot, float x, float y, float z)" },
1210 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
1211 "void", "(float *mat, float x, float y, float z)" },
1212 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
1213 "void", "(float *mat, float x, float y, float z)" },
1214 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
1215 "void", "(float *mat, float *lhs, float *rhs)" },
1216 { "matrixMultiply", (void *)&SC_matrixMultiply,
1217 "void", "(float *mat, float *rhs)" },
1218 { "matrixRotate", (void *)&SC_matrixRotate,
1219 "void", "(float *mat, float rot, float x, float y, float z)" },
1220 { "matrixScale", (void *)&SC_matrixScale,
1221 "void", "(float *mat, float x, float y, float z)" },
1222 { "matrixTranslate", (void *)&SC_matrixTranslate,
1223 "void", "(float *mat, float x, float y, float z)" },
1224
Jason Sams90b36a82009-08-17 13:56:09 -07001225 // vector
1226 { "vec2Rand", (void *)&SC_vec2Rand,
1227 "void", "(float *vec, float maxLen)" },
1228
Jason Samsa57c0a72009-09-04 14:42:41 -07001229 // vec3
1230 { "vec3Norm", (void *)&SC_vec3Norm,
Jason Samsd01d9702009-12-23 14:35:29 -08001231 "void", "(struct vecF32_3_s *)" },
Jason Samsa57c0a72009-09-04 14:42:41 -07001232 { "vec3Length", (void *)&SC_vec3Length,
Jason Samsd01d9702009-12-23 14:35:29 -08001233 "float", "(struct vecF32_3_s *)" },
Jason Samsa57c0a72009-09-04 14:42:41 -07001234 { "vec3Add", (void *)&SC_vec3Add,
Jason Samsd01d9702009-12-23 14:35:29 -08001235 "void", "(struct vecF32_3_s *dest, struct vecF32_3_s *lhs, struct vecF32_3_s *rhs)" },
Jason Samsa57c0a72009-09-04 14:42:41 -07001236 { "vec3Sub", (void *)&SC_vec3Sub,
Jason Samsd01d9702009-12-23 14:35:29 -08001237 "void", "(struct vecF32_3_s *dest, struct vecF32_3_s *lhs, struct vecF32_3_s *rhs)" },
Jason Samsa57c0a72009-09-04 14:42:41 -07001238 { "vec3Cross", (void *)&SC_vec3Cross,
Jason Samsd01d9702009-12-23 14:35:29 -08001239 "void", "(struct vecF32_3_s *dest, struct vecF32_3_s *lhs, struct vecF32_3_s *rhs)" },
Jason Samsa57c0a72009-09-04 14:42:41 -07001240 { "vec3Dot", (void *)&SC_vec3Dot,
Jason Samsd01d9702009-12-23 14:35:29 -08001241 "float", "(struct vecF32_3_s *lhs, struct vecF32_3_s *rhs)" },
Jason Samsa57c0a72009-09-04 14:42:41 -07001242 { "vec3Scale", (void *)&SC_vec3Scale,
Jason Samsd01d9702009-12-23 14:35:29 -08001243 "void", "(struct vecF32_3_s *lhs, float scale)" },
Jason Samsa57c0a72009-09-04 14:42:41 -07001244
Romain Guyd6d4a5f2009-10-09 16:05:25 -07001245 // vec4
1246 { "vec4Norm", (void *)&SC_vec4Norm,
Jason Samsd01d9702009-12-23 14:35:29 -08001247 "void", "(struct vecF32_4_s *)" },
Romain Guyd6d4a5f2009-10-09 16:05:25 -07001248 { "vec4Length", (void *)&SC_vec4Length,
Jason Samsd01d9702009-12-23 14:35:29 -08001249 "float", "(struct vecF32_4_s *)" },
Romain Guyd6d4a5f2009-10-09 16:05:25 -07001250 { "vec4Add", (void *)&SC_vec4Add,
Jason Samsd01d9702009-12-23 14:35:29 -08001251 "void", "(struct vecF32_4_s *dest, struct vecF32_4_s *lhs, struct vecF32_4_s *rhs)" },
Romain Guyd6d4a5f2009-10-09 16:05:25 -07001252 { "vec4Sub", (void *)&SC_vec4Sub,
Jason Samsd01d9702009-12-23 14:35:29 -08001253 "void", "(struct vecF32_4_s *dest, struct vecF32_4_s *lhs, struct vecF32_4_s *rhs)" },
Romain Guyd6d4a5f2009-10-09 16:05:25 -07001254 { "vec4Dot", (void *)&SC_vec4Dot,
Jason Samsd01d9702009-12-23 14:35:29 -08001255 "float", "(struct vecF32_4_s *lhs, struct vecF32_4_s *rhs)" },
Romain Guyd6d4a5f2009-10-09 16:05:25 -07001256 { "vec4Scale", (void *)&SC_vec4Scale,
Jason Samsd01d9702009-12-23 14:35:29 -08001257 "void", "(struct vecF32_4_s *lhs, float scale)" },
Romain Guyd6d4a5f2009-10-09 16:05:25 -07001258
Jason Samse45ac6e2009-07-20 14:31:06 -07001259 // context
1260 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
1261 "void", "(int)" },
1262 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
1263 "void", "(int)" },
Jason Samsb681c8a2009-09-28 18:12:56 -07001264 { "bindProgramStore", (void *)&SC_bindProgramFragmentStore,
1265 "void", "(int)" },
Jason Samsb5909ce2009-07-21 12:20:54 -07001266 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
1267 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001268 { "bindSampler", (void *)&SC_bindSampler,
1269 "void", "(int, int, int)" },
1270 { "bindTexture", (void *)&SC_bindTexture,
1271 "void", "(int, int, int)" },
1272
Jason Samsc9d43db2009-07-28 12:02:16 -07001273 // vp
Jason Sams50253db2009-07-29 20:55:44 -07001274 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -07001275 "void", "(void *)" },
Jason Sams50253db2009-07-29 20:55:44 -07001276 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -07001277 "void", "(void *)" },
1278
1279
1280
Jason Samse45ac6e2009-07-20 14:31:06 -07001281 // drawing
Jason Samse9f5c532009-07-28 17:20:11 -07001282 { "drawRect", (void *)&SC_drawRect,
1283 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001284 { "drawQuad", (void *)&SC_drawQuad,
1285 "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 -07001286 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords,
1287 "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 Sams3a97c592009-09-30 17:36:20 -07001288 { "drawSprite", (void *)&SC_drawSprite,
1289 "void", "(float x, float y, float z, float w, float h)" },
1290 { "drawSpriteScreenspace", (void *)&SC_drawSpriteScreenspace,
1291 "void", "(float x, float y, float z, float w, float h)" },
Romain Guyd369e272009-08-07 15:40:32 -07001292 { "drawLine", (void *)&SC_drawLine,
1293 "void", "(float x1, float y1, float z1, float x2, float y2, float z2)" },
Jason Samsb681c8a2009-09-28 18:12:56 -07001294 { "drawPoint", (void *)&SC_drawPoint,
1295 "void", "(float x1, float y1, float z1)" },
Jason Samse5ffb872009-08-09 17:01:55 -07001296 { "drawSimpleMesh", (void *)&SC_drawSimpleMesh,
1297 "void", "(int ism)" },
1298 { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange,
1299 "void", "(int ism, int start, int len)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001300
1301
1302 // misc
1303 { "pfClearColor", (void *)&SC_ClearColor,
1304 "void", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001305 { "color", (void *)&SC_color,
1306 "void", "(float, float, float, float)" },
Romain Guy9c59d022009-07-31 15:33:59 -07001307 { "hsb", (void *)&SC_hsb,
1308 "void", "(float, float, float, float)" },
Romain Guy370ed152009-08-20 17:08:33 -07001309 { "hsbToRgb", (void *)&SC_hsbToRgb,
1310 "void", "(float, float, float, float*)" },
1311 { "hsbToAbgr", (void *)&SC_hsbToAbgr,
1312 "int", "(float, float, float, float)" },
Romain Guy48b7edc2009-08-06 22:52:13 -07001313 { "ambient", (void *)&SC_ambient,
1314 "void", "(float, float, float, float)" },
1315 { "diffuse", (void *)&SC_diffuse,
1316 "void", "(float, float, float, float)" },
1317 { "specular", (void *)&SC_specular,
1318 "void", "(float, float, float, float)" },
1319 { "emission", (void *)&SC_emission,
1320 "void", "(float, float, float, float)" },
1321 { "shininess", (void *)&SC_shininess,
Romain Guyd369e272009-08-07 15:40:32 -07001322 "void", "(float)" },
Romain Guye62cc902009-09-04 17:55:41 -07001323 { "pointAttenuation", (void *)&SC_pointAttenuation,
1324 "void", "(float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -07001325
Jason Samsc9d43db2009-07-28 12:02:16 -07001326 { "uploadToTexture", (void *)&SC_uploadToTexture,
1327 "void", "(int, int)" },
Jason Samse5ffb872009-08-09 17:01:55 -07001328 { "uploadToBufferObject", (void *)&SC_uploadToBufferObject,
1329 "void", "(int)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001330
Jason Sams9ebb0c42010-01-12 12:12:28 -08001331 { "syncToGL", (void *)&SC_syncToGL,
1332 "void", "(int)" },
1333
Jason Sams90b36a82009-08-17 13:56:09 -07001334 { "colorFloatRGBAtoUNorm8", (void *)&SC_colorFloatRGBAtoUNorm8,
1335 "int", "(float, float, float, float)" },
1336 { "colorFloatRGBto565", (void *)&SC_colorFloatRGBAto565,
1337 "int", "(float, float, float)" },
1338
1339
Jason Samse579df42009-08-10 14:55:26 -07001340 { "getWidth", (void *)&SC_getWidth,
1341 "int", "()" },
1342 { "getHeight", (void *)&SC_getHeight,
1343 "int", "()" },
1344
Jason Sams8c401ef2009-10-06 13:58:47 -07001345 { "sendToClient", (void *)&SC_toClient,
1346 "int", "(void *data, int cmdID, int len, int waitForSpace)" },
Jason Samse579df42009-08-10 14:55:26 -07001347
Jason Samsc9d43db2009-07-28 12:02:16 -07001348
1349 { "debugF", (void *)&SC_debugF,
1350 "void", "(void *, float)" },
1351 { "debugI32", (void *)&SC_debugI32,
1352 "void", "(void *, int)" },
Romain Guy370ed152009-08-20 17:08:33 -07001353 { "debugHexF", (void *)&SC_debugHexF,
1354 "void", "(void *, float)" },
1355 { "debugHexI32", (void *)&SC_debugHexI32,
1356 "void", "(void *, int)" },
Jason Samsc9d43db2009-07-28 12:02:16 -07001357
Jason Sams3a27c952009-10-07 18:14:01 -07001358 { "scriptCall", (void *)&SC_scriptCall,
1359 "void", "(int)" },
1360
Jason Samsc9d43db2009-07-28 12:02:16 -07001361
Jason Samse45ac6e2009-07-20 14:31:06 -07001362 { NULL, NULL, NULL, NULL }
1363};
1364
1365const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
1366{
1367 ScriptCState::SymbolTable_t *syms = gSyms;
1368
1369 while (syms->mPtr) {
1370 if (!strcmp(syms->mName, sym)) {
1371 return syms;
1372 }
1373 syms++;
1374 }
1375 return NULL;
1376}
1377
1378void ScriptCState::appendDecls(String8 *str)
1379{
1380 ScriptCState::SymbolTable_t *syms = gSyms;
1381 while (syms->mPtr) {
1382 str->append(syms->mRet);
1383 str->append(" ");
1384 str->append(syms->mName);
1385 str->append(syms->mParam);
1386 str->append(";\n");
1387 syms++;
1388 }
1389}
1390
1391