blob: d94a62cf76c7ab74244776499bcb53eefa585a4a [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 Samsbe36bf32010-05-11 14:03:58 -070042
43static float SC_acospi(float v) {
44 return acosf(v)/ M_PI;
45}
46
47static float SC_asinpi(float v) {
48 return asinf(v) / M_PI;
49}
50
51static float SC_atanpi(float v) {
52 return atanf(v) / M_PI;
53}
54
55static float SC_atan2pi(float y, float x) {
56 return atan2f(y, x) / M_PI;
57}
58
59static float SC_cospi(float v) {
60 return cosf(v * M_PI);
61}
62
63static float SC_exp10(float v) {
64 return pow(10.f, v);
65
66}
67
68static float SC_fract(float v, int *iptr) {
69 int i = (int)floor(v);
70 iptr[0] = i;
71 return fmin(v - i, 0x1.fffffep-1f);
72}
73
74static float SC_log2(float v) {
75 return log10(v) / log10(2.f);
76}
77
78static float SC_pown(float v, int p) {
79 return powf(v, (float)p);
80}
81
82static float SC_powr(float v, float p) {
83 return powf(v, p);
84}
85
86float SC_rootn(float v, int r) {
87 return pow(v, 1.f / r);
88}
89
90float SC_rsqrt(float v) {
91 return 1.f / sqrtf(v);
92}
93
94float SC_sincos(float v, float *cosptr) {
95 *cosptr = cosf(v);
96 return sinf(v);
97}
98
99static float SC_sinpi(float v) {
100 return sinf(v * M_PI);
101}
102
103static float SC_tanpi(float v) {
104 return tanf(v * M_PI);
105}
106
107 //{ "logb", (void *)& },
108 //{ "mad", (void *)& },
109 //{ "nan", (void *)& },
110 //{ "tgamma", (void *)& },
111
112//////////////////////////////////////////////////////////////////////////////
113// Integer
114//////////////////////////////////////////////////////////////////////////////
115
116
117static uint32_t SC_abs_i32(int32_t v) {return abs(v);}
118static uint16_t SC_abs_i16(int16_t v) {return (uint16_t)abs(v);}
119static uint8_t SC_abs_i8(int8_t v) {return (uint8_t)abs(v);}
120
121static uint32_t SC_clz_u32(uint32_t v) {return __builtin_clz(v);}
122static uint16_t SC_clz_u16(uint16_t v) {return (uint16_t)__builtin_clz(v);}
123static uint8_t SC_clz_u8(uint8_t v) {return (uint8_t)__builtin_clz(v);}
124static int32_t SC_clz_i32(int32_t v) {return (int32_t)__builtin_clz((uint32_t)v);}
125static int16_t SC_clz_i16(int16_t v) {return (int16_t)__builtin_clz(v);}
126static int8_t SC_clz_i8(int8_t v) {return (int8_t)__builtin_clz(v);}
127
128static uint32_t SC_max_u32(uint32_t v, uint32_t v2) {return rsMax(v, v2);}
129static uint16_t SC_max_u16(uint16_t v, uint16_t v2) {return rsMax(v, v2);}
130static uint8_t SC_max_u8(uint8_t v, uint8_t v2) {return rsMax(v, v2);}
131static int32_t SC_max_i32(int32_t v, int32_t v2) {return rsMax(v, v2);}
132static int16_t SC_max_i16(int16_t v, int16_t v2) {return rsMax(v, v2);}
133static int8_t SC_max_i8(int8_t v, int8_t v2) {return rsMax(v, v2);}
134
135static uint32_t SC_min_u32(uint32_t v, uint32_t v2) {return rsMin(v, v2);}
136static uint16_t SC_min_u16(uint16_t v, uint16_t v2) {return rsMin(v, v2);}
137static uint8_t SC_min_u8(uint8_t v, uint8_t v2) {return rsMin(v, v2);}
138static int32_t SC_min_i32(int32_t v, int32_t v2) {return rsMin(v, v2);}
139static int16_t SC_min_i16(int16_t v, int16_t v2) {return rsMin(v, v2);}
140static int8_t SC_min_i8(int8_t v, int8_t v2) {return rsMin(v, v2);}
141
142//////////////////////////////////////////////////////////////////////////////
143// Float util
144//////////////////////////////////////////////////////////////////////////////
145
146static float SC_clamp_f32(float amount, float low, float high)
147{
148 return amount < low ? low : (amount > high ? high : amount);
149}
150
151static float SC_degrees(float radians)
152{
153 return radians * (180.f / M_PI);
154}
155
156static float SC_max_f32(float v, float v2)
157{
158 return rsMax(v, v2);
159}
160
161static float SC_min_f32(float v, float v2)
162{
163 return rsMin(v, v2);
164}
165
166static float SC_mix_f32(float start, float stop, float amount)
167{
168 //LOGE("lerpf %f %f %f", start, stop, amount);
169 return start + (stop - start) * amount;
170}
171
172static float SC_radians(float degrees)
173{
174 return degrees * (M_PI / 180.f);
175}
176
177static float SC_step_f32(float edge, float v)
178{
179 if (v < edge) return 0.f;
180 return 1.f;
181}
182
183static float SC_sign_f32(float value)
184{
185 if (value > 0) return 1.f;
186 if (value < 0) return -1.f;
187 return value;
188}
189
190
191
192//////////////////////////////////////////////////////////////////////////////
193// Non-Updated code below
194//////////////////////////////////////////////////////////////////////////////
195
Jason Samsa57c0a72009-09-04 14:42:41 -0700196typedef struct {
197 float x;
198 float y;
199 float z;
200} vec3_t;
201
202typedef struct {
203 float x;
204 float y;
205 float z;
206 float w;
207} vec4_t;
208
209typedef struct {
210 float x;
211 float y;
212} vec2_t;
Jason Samse45ac6e2009-07-20 14:31:06 -0700213
214//////////////////////////////////////////////////////////////////////////////
215// IO routines
216//////////////////////////////////////////////////////////////////////////////
217
Jason Samsa2b54c42009-09-23 16:38:37 -0700218static float* SC_loadSimpleMeshVerticesF(RsSimpleMesh mesh, uint32_t idx)
Romain Guy48b7edc2009-08-06 22:52:13 -0700219{
Jason Samsa2b54c42009-09-23 16:38:37 -0700220 SimpleMesh *tm = static_cast<SimpleMesh *>(mesh);
221 void *vp = tm->mVertexBuffers[idx]->getPtr();;
222 return static_cast<float *>(vp);
Romain Guy48b7edc2009-08-06 22:52:13 -0700223}
224
Jason Samsa2b54c42009-09-23 16:38:37 -0700225static void SC_updateSimpleMesh(RsSimpleMesh mesh)
Romain Guy48b7edc2009-08-06 22:52:13 -0700226{
Jason Samsc460e552009-11-25 13:22:07 -0800227 GET_TLS();
Jason Samsa2b54c42009-09-23 16:38:37 -0700228 SimpleMesh *sm = static_cast<SimpleMesh *>(mesh);
Jason Samsc460e552009-11-25 13:22:07 -0800229 sm->uploadAll(rsc);
Romain Guy48b7edc2009-08-06 22:52:13 -0700230}
Romain Guy06f7c932009-08-06 12:40:41 -0700231
Jason Samsa57c0a72009-09-04 14:42:41 -0700232//////////////////////////////////////////////////////////////////////////////
233// Vec3 routines
234//////////////////////////////////////////////////////////////////////////////
235
236static void SC_vec3Norm(vec3_t *v)
237{
238 float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
239 len = 1 / len;
240 v->x *= len;
241 v->y *= len;
242 v->z *= len;
243}
244
245static float SC_vec3Length(const vec3_t *v)
246{
247 return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
248}
249
250static void SC_vec3Add(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
251{
252 dest->x = lhs->x + rhs->x;
253 dest->y = lhs->y + rhs->y;
254 dest->z = lhs->z + rhs->z;
255}
256
257static void SC_vec3Sub(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
258{
259 dest->x = lhs->x - rhs->x;
260 dest->y = lhs->y - rhs->y;
261 dest->z = lhs->z - rhs->z;
262}
263
264static void SC_vec3Cross(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
265{
266 float x = lhs->y * rhs->z - lhs->z * rhs->y;
267 float y = lhs->z * rhs->x - lhs->x * rhs->z;
268 float z = lhs->x * rhs->y - lhs->y * rhs->x;
269 dest->x = x;
270 dest->y = y;
271 dest->z = z;
272}
273
274static float SC_vec3Dot(const vec3_t *lhs, const vec3_t *rhs)
275{
276 return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z;
277}
278
279static void SC_vec3Scale(vec3_t *lhs, float scale)
280{
281 lhs->x *= scale;
282 lhs->y *= scale;
283 lhs->z *= scale;
284}
285
Romain Guyd6d4a5f2009-10-09 16:05:25 -0700286//////////////////////////////////////////////////////////////////////////////
287// Vec4 routines
288//////////////////////////////////////////////////////////////////////////////
289
290static void SC_vec4Norm(vec4_t *v)
291{
292 float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w);
293 len = 1 / len;
294 v->x *= len;
295 v->y *= len;
296 v->z *= len;
297 v->w *= len;
298}
299
300static float SC_vec4Length(const vec4_t *v)
301{
302 return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w);
303}
304
305static void SC_vec4Add(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs)
306{
307 dest->x = lhs->x + rhs->x;
308 dest->y = lhs->y + rhs->y;
309 dest->z = lhs->z + rhs->z;
310 dest->w = lhs->w + rhs->w;
311}
312
313static void SC_vec4Sub(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs)
314{
315 dest->x = lhs->x - rhs->x;
316 dest->y = lhs->y - rhs->y;
317 dest->z = lhs->z - rhs->z;
318 dest->w = lhs->w - rhs->w;
319}
320
321static float SC_vec4Dot(const vec4_t *lhs, const vec4_t *rhs)
322{
323 return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z + lhs->w * rhs->w;
324}
325
326static void SC_vec4Scale(vec4_t *lhs, float scale)
327{
328 lhs->x *= scale;
329 lhs->y *= scale;
330 lhs->z *= scale;
331 lhs->w *= scale;
332}
Jason Samse45ac6e2009-07-20 14:31:06 -0700333
334//////////////////////////////////////////////////////////////////////////////
335// Math routines
336//////////////////////////////////////////////////////////////////////////////
337
Romain Guy2275d632009-08-18 11:39:17 -0700338static float SC_sinf_fast(float x)
339{
340 const float A = 1.0f / (2.0f * M_PI);
341 const float B = -16.0f;
342 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -0700343
Romain Guy2275d632009-08-18 11:39:17 -0700344 // scale angle for easy argument reduction
345 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -0700346
Romain Guy2275d632009-08-18 11:39:17 -0700347 if (fabsf(x) >= 0.5f) {
348 // argument reduction
349 x = x - ceilf(x + 0.5f) + 1.0f;
350 }
Jason Samsa57c0a72009-09-04 14:42:41 -0700351
Romain Guy2275d632009-08-18 11:39:17 -0700352 const float y = B * x * fabsf(x) + C * x;
353 return 0.2215f * (y * fabsf(y) - y) + y;
354}
355
356static float SC_cosf_fast(float x)
357{
358 x += float(M_PI / 2);
359
360 const float A = 1.0f / (2.0f * M_PI);
361 const float B = -16.0f;
362 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -0700363
Romain Guy2275d632009-08-18 11:39:17 -0700364 // scale angle for easy argument reduction
365 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -0700366
Romain Guy2275d632009-08-18 11:39:17 -0700367 if (fabsf(x) >= 0.5f) {
368 // argument reduction
369 x = x - ceilf(x + 0.5f) + 1.0f;
370 }
Jason Samsa57c0a72009-09-04 14:42:41 -0700371
Romain Guy2275d632009-08-18 11:39:17 -0700372 const float y = B * x * fabsf(x) + C * x;
373 return 0.2215f * (y * fabsf(y) - y) + y;
374}
375
Jason Samse45ac6e2009-07-20 14:31:06 -0700376static float SC_randf(float max)
377{
Jason Samsbe36bf32010-05-11 14:03:58 -0700378 //LOGE("max %f", max);
Jason Samse45ac6e2009-07-20 14:31:06 -0700379 float r = (float)rand();
380 return r / RAND_MAX * max;
381}
382
Romain Guy39dbc802009-07-31 11:20:59 -0700383static float SC_randf2(float min, float max)
384{
385 float r = (float)rand();
386 return r / RAND_MAX * (max - min) + min;
387}
388
Romain Guyd6d4a5f2009-10-09 16:05:25 -0700389static int SC_sign(int value)
390{
391 return (value > 0) - (value < 0);
392}
393
Romain Guy27162ab2009-08-09 17:04:54 -0700394static int SC_clamp(int amount, int low, int high)
395{
396 return amount < low ? low : (amount > high ? high : amount);
397}
398
Jason Samsdac98f52009-09-18 14:24:24 -0700399static float SC_roundf(float v)
400{
401 return floorf(v + 0.4999999999);
402}
403
Romain Guy39dbc802009-07-31 11:20:59 -0700404static float SC_distf2(float x1, float y1, float x2, float y2)
405{
406 float x = x2 - x1;
407 float y = y2 - y1;
Jason Samse5ffb872009-08-09 17:01:55 -0700408 return sqrtf(x * x + y * y);
Romain Guy39dbc802009-07-31 11:20:59 -0700409}
410
411static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2)
412{
413 float x = x2 - x1;
414 float y = y2 - y1;
415 float z = z2 - z1;
Jason Samse5ffb872009-08-09 17:01:55 -0700416 return sqrtf(x * x + y * y + z * z);
Romain Guy39dbc802009-07-31 11:20:59 -0700417}
418
419static float SC_magf2(float a, float b)
420{
421 return sqrtf(a * a + b * b);
422}
423
424static float SC_magf3(float a, float b, float c)
425{
426 return sqrtf(a * a + b * b + c * c);
427}
428
Romain Guy39dbc802009-07-31 11:20:59 -0700429static float SC_normf(float start, float stop, float value)
430{
431 return (value - start) / (stop - start);
432}
433
434static float SC_mapf(float minStart, float minStop, float maxStart, float maxStop, float value)
435{
436 return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
437}
Jason Samse45ac6e2009-07-20 14:31:06 -0700438
Jason Samsbe36bf32010-05-11 14:03:58 -0700439static float SC_frac(float v)
440{
441 int i = (int)floor(v);
442 return fmin(v - i, 0x1.fffffep-1f);
443}
444
Romain Guy98e10fd2009-07-30 18:45:01 -0700445//////////////////////////////////////////////////////////////////////////////
446// Time routines
447//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700448
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700449static int32_t SC_second()
Romain Guy98e10fd2009-07-30 18:45:01 -0700450{
451 GET_TLS();
452
453 time_t rawtime;
454 time(&rawtime);
455
Romain Guy519cdc92009-11-11 15:36:06 -0800456 struct tm *timeinfo;
457 timeinfo = localtime(&rawtime);
458 return timeinfo->tm_sec;
Romain Guy98e10fd2009-07-30 18:45:01 -0700459}
460
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700461static int32_t SC_minute()
Romain Guy98e10fd2009-07-30 18:45:01 -0700462{
463 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700464
Romain Guy98e10fd2009-07-30 18:45:01 -0700465 time_t rawtime;
466 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700467
Romain Guy519cdc92009-11-11 15:36:06 -0800468 struct tm *timeinfo;
469 timeinfo = localtime(&rawtime);
470 return timeinfo->tm_min;
Jason Samse5ffb872009-08-09 17:01:55 -0700471}
Romain Guy98e10fd2009-07-30 18:45:01 -0700472
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700473static int32_t SC_hour()
Romain Guy98e10fd2009-07-30 18:45:01 -0700474{
475 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700476
Romain Guy98e10fd2009-07-30 18:45:01 -0700477 time_t rawtime;
478 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700479
Romain Guy519cdc92009-11-11 15:36:06 -0800480 struct tm *timeinfo;
481 timeinfo = localtime(&rawtime);
482 return timeinfo->tm_hour;
Romain Guy39dbc802009-07-31 11:20:59 -0700483}
484
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700485static int32_t SC_day()
Romain Guy39dbc802009-07-31 11:20:59 -0700486{
487 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700488
Romain Guy39dbc802009-07-31 11:20:59 -0700489 time_t rawtime;
490 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700491
Romain Guy519cdc92009-11-11 15:36:06 -0800492 struct tm *timeinfo;
493 timeinfo = localtime(&rawtime);
494 return timeinfo->tm_mday;
Jason Samse5ffb872009-08-09 17:01:55 -0700495}
Jason Samse45ac6e2009-07-20 14:31:06 -0700496
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700497static int32_t SC_month()
Romain Guy39dbc802009-07-31 11:20:59 -0700498{
499 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700500
Romain Guy39dbc802009-07-31 11:20:59 -0700501 time_t rawtime;
502 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700503
Romain Guy519cdc92009-11-11 15:36:06 -0800504 struct tm *timeinfo;
505 timeinfo = localtime(&rawtime);
506 return timeinfo->tm_mon;
Jason Samse5ffb872009-08-09 17:01:55 -0700507}
Romain Guy39dbc802009-07-31 11:20:59 -0700508
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700509static int32_t SC_year()
Romain Guy39dbc802009-07-31 11:20:59 -0700510{
511 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700512
Romain Guy39dbc802009-07-31 11:20:59 -0700513 time_t rawtime;
514 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700515
Romain Guy519cdc92009-11-11 15:36:06 -0800516 struct tm *timeinfo;
517 timeinfo = localtime(&rawtime);
518 return timeinfo->tm_year;
Romain Guy39dbc802009-07-31 11:20:59 -0700519}
520
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700521static int32_t SC_uptimeMillis()
522{
523 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
524}
525
526static int32_t SC_startTimeMillis()
527{
528 GET_TLS();
529 return sc->mEnviroment.mStartTimeMillis;
530}
531
532static int32_t SC_elapsedTimeMillis()
533{
534 GET_TLS();
535 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC))
536 - sc->mEnviroment.mStartTimeMillis;
537}
538
Jason Samse45ac6e2009-07-20 14:31:06 -0700539//////////////////////////////////////////////////////////////////////////////
540// Matrix routines
541//////////////////////////////////////////////////////////////////////////////
542
543
544static void SC_matrixLoadIdentity(rsc_Matrix *mat)
545{
546 Matrix *m = reinterpret_cast<Matrix *>(mat);
547 m->loadIdentity();
548}
549
550static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
551{
552 Matrix *m = reinterpret_cast<Matrix *>(mat);
553 m->load(f);
554}
555
556static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
557{
558 Matrix *m = reinterpret_cast<Matrix *>(mat);
559 m->load(reinterpret_cast<const Matrix *>(newmat));
560}
561
562static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
563{
564 Matrix *m = reinterpret_cast<Matrix *>(mat);
565 m->loadRotate(rot, x, y, z);
566}
567
568static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
569{
570 Matrix *m = reinterpret_cast<Matrix *>(mat);
571 m->loadScale(x, y, z);
572}
573
574static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
575{
576 Matrix *m = reinterpret_cast<Matrix *>(mat);
577 m->loadTranslate(x, y, z);
578}
579
580static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
581{
582 Matrix *m = reinterpret_cast<Matrix *>(mat);
583 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
584 reinterpret_cast<const Matrix *>(rhs));
585}
586
587static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
588{
589 Matrix *m = reinterpret_cast<Matrix *>(mat);
590 m->multiply(reinterpret_cast<const Matrix *>(rhs));
591}
592
593static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
594{
595 Matrix *m = reinterpret_cast<Matrix *>(mat);
596 m->rotate(rot, x, y, z);
597}
598
599static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
600{
601 Matrix *m = reinterpret_cast<Matrix *>(mat);
602 m->scale(x, y, z);
603}
604
605static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
606{
607 Matrix *m = reinterpret_cast<Matrix *>(mat);
608 m->translate(x, y, z);
609}
610
611
Jason Samsbe36bf32010-05-11 14:03:58 -0700612static rsvF_2 SC_vec2Rand(float maxLen)
Jason Sams90b36a82009-08-17 13:56:09 -0700613{
Jason Samsbe36bf32010-05-11 14:03:58 -0700614 float2 t;
615 float angle = SC_randf(M_PI * 2);
Jason Sams90b36a82009-08-17 13:56:09 -0700616 float len = SC_randf(maxLen);
Jason Samsbe36bf32010-05-11 14:03:58 -0700617 t.f[0] = len * sinf(angle);
618 t.f[1] = len * cosf(angle);
619 return t.v;
Jason Sams90b36a82009-08-17 13:56:09 -0700620}
621
Jason Samse45ac6e2009-07-20 14:31:06 -0700622
623
624//////////////////////////////////////////////////////////////////////////////
625// Context
626//////////////////////////////////////////////////////////////////////////////
627
628static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
629{
630 GET_TLS();
Jason Sams7dad9c32009-12-17 16:55:08 -0800631 rsi_ProgramBindTexture(rsc,
632 static_cast<ProgramFragment *>(vpf),
633 slot,
634 static_cast<Allocation *>(va));
Jason Samse45ac6e2009-07-20 14:31:06 -0700635
636}
637
638static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
639{
640 GET_TLS();
Jason Sams7dad9c32009-12-17 16:55:08 -0800641 rsi_ProgramBindSampler(rsc,
642 static_cast<ProgramFragment *>(vpf),
643 slot,
644 static_cast<Sampler *>(vs));
Jason Samse45ac6e2009-07-20 14:31:06 -0700645
646}
647
648static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs)
649{
650 GET_TLS();
651 rsi_ContextBindProgramFragmentStore(rsc, pfs);
652
653}
654
655static void SC_bindProgramFragment(RsProgramFragment pf)
656{
657 GET_TLS();
658 rsi_ContextBindProgramFragment(rsc, pf);
659
660}
661
Jason Samsb5909ce2009-07-21 12:20:54 -0700662static void SC_bindProgramVertex(RsProgramVertex pv)
663{
664 GET_TLS();
665 rsi_ContextBindProgramVertex(rsc, pv);
666
667}
Jason Samse45ac6e2009-07-20 14:31:06 -0700668
669//////////////////////////////////////////////////////////////////////////////
Jason Samsc9d43db2009-07-28 12:02:16 -0700670// VP
671//////////////////////////////////////////////////////////////////////////////
672
673static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
674{
675 GET_TLS();
676 rsc->getVertex()->setModelviewMatrix(m);
677}
678
679static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
680{
681 GET_TLS();
682 rsc->getVertex()->setTextureMatrix(m);
683}
684
685
686
687//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700688// Drawing
689//////////////////////////////////////////////////////////////////////////////
690
Romain Guyd369e272009-08-07 15:40:32 -0700691static void SC_drawLine(float x1, float y1, float z1,
692 float x2, float y2, float z2)
693{
694 GET_TLS();
Jason Samsa2cf7552010-03-03 13:03:18 -0800695 if (!rsc->setupCheck()) {
696 return;
697 }
Romain Guyd369e272009-08-07 15:40:32 -0700698
699 float vtx[] = { x1, y1, z1, x2, y2, z2 };
Jason Samsc460e552009-11-25 13:22:07 -0800700 VertexArray va;
Jason Samsbe504f22010-01-25 12:31:24 -0800701 va.addLegacy(GL_FLOAT, 3, 12, RS_KIND_POSITION, false, (uint32_t)vtx);
Jason Samsc460e552009-11-25 13:22:07 -0800702 if (rsc->checkVersion2_0()) {
Jason Samsd01d9702009-12-23 14:35:29 -0800703 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsc460e552009-11-25 13:22:07 -0800704 } else {
Jason Samsd01d9702009-12-23 14:35:29 -0800705 va.setupGL(rsc, &rsc->mStateVertexArray);
Jason Samsc460e552009-11-25 13:22:07 -0800706 }
Romain Guyd369e272009-08-07 15:40:32 -0700707
708 glDrawArrays(GL_LINES, 0, 2);
709}
710
Jason Samsb681c8a2009-09-28 18:12:56 -0700711static void SC_drawPoint(float x, float y, float z)
712{
713 GET_TLS();
Jason Samsa2cf7552010-03-03 13:03:18 -0800714 if (!rsc->setupCheck()) {
715 return;
716 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700717
718 float vtx[] = { x, y, z };
719
Jason Samsc460e552009-11-25 13:22:07 -0800720 VertexArray va;
Jason Samsbe504f22010-01-25 12:31:24 -0800721 va.addLegacy(GL_FLOAT, 3, 12, RS_KIND_POSITION, false, (uint32_t)vtx);
Jason Samsc460e552009-11-25 13:22:07 -0800722 if (rsc->checkVersion2_0()) {
Jason Samsd01d9702009-12-23 14:35:29 -0800723 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsc460e552009-11-25 13:22:07 -0800724 } else {
Jason Samsd01d9702009-12-23 14:35:29 -0800725 va.setupGL(rsc, &rsc->mStateVertexArray);
Jason Samsc460e552009-11-25 13:22:07 -0800726 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700727
728 glDrawArrays(GL_POINTS, 0, 1);
729}
730
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700731static void SC_drawQuadTexCoords(float x1, float y1, float z1,
732 float u1, float v1,
733 float x2, float y2, float z2,
734 float u2, float v2,
735 float x3, float y3, float z3,
736 float u3, float v3,
737 float x4, float y4, float z4,
738 float u4, float v4)
Jason Samse45ac6e2009-07-20 14:31:06 -0700739{
740 GET_TLS();
Jason Samsa2cf7552010-03-03 13:03:18 -0800741 if (!rsc->setupCheck()) {
742 return;
743 }
Jason Samse579df42009-08-10 14:55:26 -0700744
Jason Samse45ac6e2009-07-20 14:31:06 -0700745 //LOGE("Quad");
746 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
747 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
748 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
749 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
Jason Samse579df42009-08-10 14:55:26 -0700750
Jason Samse45ac6e2009-07-20 14:31:06 -0700751 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700752 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
Jason Samse45ac6e2009-07-20 14:31:06 -0700753
Jason Samsc460e552009-11-25 13:22:07 -0800754 VertexArray va;
Jason Samsbe504f22010-01-25 12:31:24 -0800755 va.addLegacy(GL_FLOAT, 3, 12, RS_KIND_POSITION, false, (uint32_t)vtx);
756 va.addLegacy(GL_FLOAT, 2, 8, RS_KIND_TEXTURE, false, (uint32_t)tex);
Jason Samsc460e552009-11-25 13:22:07 -0800757 if (rsc->checkVersion2_0()) {
Jason Samsd01d9702009-12-23 14:35:29 -0800758 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsc460e552009-11-25 13:22:07 -0800759 } else {
Jason Samsd01d9702009-12-23 14:35:29 -0800760 va.setupGL(rsc, &rsc->mStateVertexArray);
Jason Samsc460e552009-11-25 13:22:07 -0800761 }
Jason Samse45ac6e2009-07-20 14:31:06 -0700762
Jason Samse45ac6e2009-07-20 14:31:06 -0700763
764 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
765}
766
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700767static void SC_drawQuad(float x1, float y1, float z1,
768 float x2, float y2, float z2,
769 float x3, float y3, float z3,
770 float x4, float y4, float z4)
771{
772 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
773 x2, y2, z2, 1, 1,
774 x3, y3, z3, 1, 0,
775 x4, y4, z4, 0, 0);
776}
777
Jason Sams3a97c592009-09-30 17:36:20 -0700778static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h)
779{
780 GET_TLS();
Jason Samsc460e552009-11-25 13:22:07 -0800781 ObjectBaseRef<const ProgramVertex> tmp(rsc->getVertex());
782 rsc->setVertex(rsc->getDefaultProgramVertex());
783 //rsc->setupCheck();
Jason Sams3a97c592009-09-30 17:36:20 -0700784
Jason Samsc460e552009-11-25 13:22:07 -0800785 //GLint crop[4] = {0, h, w, -h};
786
787 float sh = rsc->getHeight();
788
789 SC_drawQuad(x, sh - y, z,
790 x+w, sh - y, z,
791 x+w, sh - (y+h), z,
792 x, sh - (y+h), z);
793 rsc->setVertex((ProgramVertex *)tmp.get());
Jason Sams3a97c592009-09-30 17:36:20 -0700794}
795
Joe Onoratod08a81a2010-01-14 15:59:35 -0500796static void SC_drawSpriteScreenspaceCropped(float x, float y, float z, float w, float h,
797 float cx0, float cy0, float cx1, float cy1)
798{
799 GET_TLS();
Jason Samsa2cf7552010-03-03 13:03:18 -0800800 if (!rsc->setupCheck()) {
801 return;
802 }
Joe Onoratod08a81a2010-01-14 15:59:35 -0500803
804 GLint crop[4] = {cx0, cy0, cx1, cy1};
805 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
806 glDrawTexfOES(x, y, z, w, h);
807}
808
Jason Sams3a97c592009-09-30 17:36:20 -0700809static void SC_drawSprite(float x, float y, float z, float w, float h)
810{
811 GET_TLS();
Jason Sams3a97c592009-09-30 17:36:20 -0700812 float vin[3] = {x, y, z};
813 float vout[4];
814
815 //LOGE("ds in %f %f %f", x, y, z);
816 rsc->getVertex()->transformToScreen(rsc, vout, vin);
817 //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]);
818 vout[0] /= vout[3];
819 vout[1] /= vout[3];
820 vout[2] /= vout[3];
821
822 vout[0] *= rsc->getWidth() / 2;
823 vout[1] *= rsc->getHeight() / 2;
824 vout[0] += rsc->getWidth() / 2;
825 vout[1] += rsc->getHeight() / 2;
826
827 vout[0] -= w/2;
828 vout[1] -= h/2;
829
830 //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]);
831
832 // U, V, W, H
Jason Samsc460e552009-11-25 13:22:07 -0800833 SC_drawSpriteScreenspace(vout[0], vout[1], z, h, w);
834 //rsc->setupCheck();
Jason Sams3a97c592009-09-30 17:36:20 -0700835}
836
837
Jason Samse9f5c532009-07-28 17:20:11 -0700838static void SC_drawRect(float x1, float y1,
839 float x2, float y2, float z)
840{
Jason Samsbe36bf32010-05-11 14:03:58 -0700841 //LOGE("SC_drawRect %f,%f %f,%f %f", x1, y1, x2, y2, z);
Jason Samse9f5c532009-07-28 17:20:11 -0700842 SC_drawQuad(x1, y2, z,
843 x2, y2, z,
844 x2, y1, z,
845 x1, y1, z);
846}
847
Jason Samse5ffb872009-08-09 17:01:55 -0700848static void SC_drawSimpleMesh(RsSimpleMesh vsm)
849{
850 GET_TLS();
851 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
Jason Samsa2cf7552010-03-03 13:03:18 -0800852 if (!rsc->setupCheck()) {
853 return;
854 }
Jason Samsc460e552009-11-25 13:22:07 -0800855 sm->render(rsc);
Jason Samse5ffb872009-08-09 17:01:55 -0700856}
857
858static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len)
859{
860 GET_TLS();
861 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
Jason Samsa2cf7552010-03-03 13:03:18 -0800862 if (!rsc->setupCheck()) {
863 return;
864 }
Jason Samsc460e552009-11-25 13:22:07 -0800865 sm->renderRange(rsc, start, len);
Jason Samse5ffb872009-08-09 17:01:55 -0700866}
867
868
Jason Samse45ac6e2009-07-20 14:31:06 -0700869//////////////////////////////////////////////////////////////////////////////
870//
871//////////////////////////////////////////////////////////////////////////////
872
Jason Samsbe36bf32010-05-11 14:03:58 -0700873static uint32_t SC_allocGetDimX(RsAllocation va)
874{
875 GET_TLS();
876 const Allocation *a = static_cast<const Allocation *>(va);
877 //LOGE("SC_allocGetDimX a=%p", a);
878 //LOGE(" type=%p", a->getType());
879 return a->getType()->getDimX();
880}
881
882static uint32_t SC_allocGetDimY(RsAllocation va)
883{
884 GET_TLS();
885 const Allocation *a = static_cast<const Allocation *>(va);
886 return a->getType()->getDimY();
887}
888
889static uint32_t SC_allocGetDimZ(RsAllocation va)
890{
891 GET_TLS();
892 const Allocation *a = static_cast<const Allocation *>(va);
893 return a->getType()->getDimZ();
894}
895
896static uint32_t SC_allocGetDimLOD(RsAllocation va)
897{
898 GET_TLS();
899 const Allocation *a = static_cast<const Allocation *>(va);
900 return a->getType()->getDimLOD();
901}
902
903static uint32_t SC_allocGetDimFaces(RsAllocation va)
904{
905 GET_TLS();
906 const Allocation *a = static_cast<const Allocation *>(va);
907 return a->getType()->getDimFaces();
908}
909
910
Jason Samse45ac6e2009-07-20 14:31:06 -0700911static void SC_color(float r, float g, float b, float a)
912{
Jason Samsc460e552009-11-25 13:22:07 -0800913 GET_TLS();
Jason Samse9ed6cc2009-12-16 14:13:06 -0800914 rsc->mStateVertex.color[0] = r;
915 rsc->mStateVertex.color[1] = g;
916 rsc->mStateVertex.color[2] = b;
917 rsc->mStateVertex.color[3] = a;
Jason Sams7dad9c32009-12-17 16:55:08 -0800918 if (!rsc->checkVersion2_0()) {
Jason Samsc460e552009-11-25 13:22:07 -0800919 glColor4f(r, g, b, a);
920 }
Jason Samse45ac6e2009-07-20 14:31:06 -0700921}
922
Romain Guye62cc902009-09-04 17:55:41 -0700923static void SC_pointAttenuation(float a, float b, float c)
924{
925 GLfloat params[] = { a, b, c };
926 glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, params);
927}
928
Romain Guy370ed152009-08-20 17:08:33 -0700929static void SC_hsbToRgb(float h, float s, float b, float* rgb)
Romain Guy9c59d022009-07-31 15:33:59 -0700930{
931 float red = 0.0f;
932 float green = 0.0f;
933 float blue = 0.0f;
Jason Samse5ffb872009-08-09 17:01:55 -0700934
Romain Guy9c59d022009-07-31 15:33:59 -0700935 float x = h;
936 float y = s;
937 float z = b;
Jason Samse5ffb872009-08-09 17:01:55 -0700938
Romain Guy9c59d022009-07-31 15:33:59 -0700939 float hf = (x - (int) x) * 6.0f;
940 int ihf = (int) hf;
941 float f = hf - ihf;
942 float pv = z * (1.0f - y);
943 float qv = z * (1.0f - y * f);
944 float tv = z * (1.0f - y * (1.0f - f));
Jason Samse5ffb872009-08-09 17:01:55 -0700945
Romain Guy9c59d022009-07-31 15:33:59 -0700946 switch (ihf) {
947 case 0: // Red is the dominant color
948 red = z;
949 green = tv;
950 blue = pv;
951 break;
952 case 1: // Green is the dominant color
953 red = qv;
954 green = z;
955 blue = pv;
956 break;
957 case 2:
958 red = pv;
959 green = z;
960 blue = tv;
961 break;
962 case 3: // Blue is the dominant color
963 red = pv;
964 green = qv;
965 blue = z;
966 break;
967 case 4:
968 red = tv;
969 green = pv;
970 blue = z;
971 break;
972 case 5: // Red is the dominant color
973 red = z;
974 green = pv;
975 blue = qv;
976 break;
977 }
Jason Samse5ffb872009-08-09 17:01:55 -0700978
Romain Guy370ed152009-08-20 17:08:33 -0700979 rgb[0] = red;
980 rgb[1] = green;
981 rgb[2] = blue;
982}
983
984static int SC_hsbToAbgr(float h, float s, float b, float a)
985{
Jason Samsbe36bf32010-05-11 14:03:58 -0700986 //LOGE("hsb a %f, %f, %f %f", h, s, b, a);
Romain Guy370ed152009-08-20 17:08:33 -0700987 float rgb[3];
988 SC_hsbToRgb(h, s, b, rgb);
Jason Samsbe36bf32010-05-11 14:03:58 -0700989 //LOGE("rgb %f, %f, %f ", rgb[0], rgb[1], rgb[2]);
Romain Guy370ed152009-08-20 17:08:33 -0700990 return int(a * 255.0f) << 24 |
991 int(rgb[2] * 255.0f) << 16 |
992 int(rgb[1] * 255.0f) << 8 |
993 int(rgb[0] * 255.0f);
994}
995
996static void SC_hsb(float h, float s, float b, float a)
997{
Jason Samsc460e552009-11-25 13:22:07 -0800998 GET_TLS();
Romain Guy370ed152009-08-20 17:08:33 -0700999 float rgb[3];
1000 SC_hsbToRgb(h, s, b, rgb);
Jason Samsc460e552009-11-25 13:22:07 -08001001 if (rsc->checkVersion2_0()) {
1002 glVertexAttrib4f(1, rgb[0], rgb[1], rgb[2], a);
1003 } else {
1004 glColor4f(rgb[0], rgb[1], rgb[2], a);
1005 }
Romain Guy9c59d022009-07-31 15:33:59 -07001006}
1007
Jason Samsc9d43db2009-07-28 12:02:16 -07001008static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samse45ac6e2009-07-20 14:31:06 -07001009{
1010 GET_TLS();
Jason Sams7fabe1a2010-02-23 17:44:28 -08001011 rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel);
Jason Samse45ac6e2009-07-20 14:31:06 -07001012}
1013
Jason Samse5ffb872009-08-09 17:01:55 -07001014static void SC_uploadToBufferObject(RsAllocation va)
1015{
1016 GET_TLS();
1017 rsi_AllocationUploadToBufferObject(rsc, va);
1018}
1019
Jason Sams9ebb0c42010-01-12 12:12:28 -08001020static void SC_syncToGL(RsAllocation va)
1021{
1022 GET_TLS();
1023 Allocation *a = static_cast<Allocation *>(va);
1024
1025}
1026
Jason Samse45ac6e2009-07-20 14:31:06 -07001027static void SC_ClearColor(float r, float g, float b, float a)
1028{
1029 //LOGE("c %f %f %f %f", r, g, b, a);
1030 GET_TLS();
1031 sc->mEnviroment.mClearColor[0] = r;
1032 sc->mEnviroment.mClearColor[1] = g;
1033 sc->mEnviroment.mClearColor[2] = b;
1034 sc->mEnviroment.mClearColor[3] = a;
1035}
1036
Jason Samsc9d43db2009-07-28 12:02:16 -07001037static void SC_debugF(const char *s, float f)
1038{
1039 LOGE("%s %f", s, f);
1040}
1041
Romain Guy370ed152009-08-20 17:08:33 -07001042static void SC_debugHexF(const char *s, float f)
1043{
1044 LOGE("%s 0x%x", s, *((int *) (&f)));
1045}
1046
Jason Samsc9d43db2009-07-28 12:02:16 -07001047static void SC_debugI32(const char *s, int32_t i)
1048{
1049 LOGE("%s %i", s, i);
1050}
1051
Romain Guy370ed152009-08-20 17:08:33 -07001052static void SC_debugHexI32(const char *s, int32_t i)
1053{
1054 LOGE("%s 0x%x", s, i);
1055}
1056
Jason Samse579df42009-08-10 14:55:26 -07001057static uint32_t SC_getWidth()
1058{
1059 GET_TLS();
1060 return rsc->getWidth();
1061}
Jason Samse45ac6e2009-07-20 14:31:06 -07001062
Jason Samse579df42009-08-10 14:55:26 -07001063static uint32_t SC_getHeight()
1064{
1065 GET_TLS();
1066 return rsc->getHeight();
1067}
Jason Samse45ac6e2009-07-20 14:31:06 -07001068
Jason Samsbe36bf32010-05-11 14:03:58 -07001069static uchar4 SC_convertColorTo8888_f3(float r, float g, float b) {
1070 uchar4 t;
1071 t.f[0] = (uint8_t)(r * 255.f);
1072 t.f[1] = (uint8_t)(g * 255.f);
1073 t.f[2] = (uint8_t)(b * 255.f);
1074 t.f[3] = 0xff;
1075 return t;
Jason Sams90b36a82009-08-17 13:56:09 -07001076}
1077
Jason Samsbe36bf32010-05-11 14:03:58 -07001078static uchar4 SC_convertColorTo8888_f4(float r, float g, float b, float a) {
1079 uchar4 t;
1080 t.f[0] = (uint8_t)(r * 255.f);
1081 t.f[1] = (uint8_t)(g * 255.f);
1082 t.f[2] = (uint8_t)(b * 255.f);
1083 t.f[3] = (uint8_t)(a * 255.f);
1084 return t;
Jason Sams90b36a82009-08-17 13:56:09 -07001085}
1086
Jason Sams8c401ef2009-10-06 13:58:47 -07001087static uint32_t SC_toClient(void *data, int cmdID, int len, int waitForSpace)
1088{
1089 GET_TLS();
Jason Samsbe36bf32010-05-11 14:03:58 -07001090 //LOGE("SC_toClient %i %i %i", cmdID, len, waitForSpace);
Jason Sams8c401ef2009-10-06 13:58:47 -07001091 return rsc->sendMessageToClient(data, cmdID, len, waitForSpace != 0);
1092}
1093
Jason Sams3a27c952009-10-07 18:14:01 -07001094static void SC_scriptCall(int scriptID)
1095{
1096 GET_TLS();
1097 rsc->runScript((Script *)scriptID, 0);
1098}
1099
Jason Samsbe36bf32010-05-11 14:03:58 -07001100static void SC_debugP(int i, void *p)
1101{
1102 LOGE("debug P %i %p, %i", i, p, (int)p);
1103}
1104
1105static void SC_debugPi(int i, int p)
1106{
1107 LOGE("debug Pi %i 0x%08x, %i", i, p, (int)p);
1108}
1109
1110static void SC_debugPf(int i, float p)
1111{
1112 LOGE("debug Pf %i %f, 0x%08x", i, p, reinterpret_cast<uint32_t *>(&p)[0]);
1113}
1114
1115int SC_divsi3(int a, int b)
1116{
1117 return a / b;
1118}
Jason Sams3a27c952009-10-07 18:14:01 -07001119
Jason Samse45ac6e2009-07-20 14:31:06 -07001120//////////////////////////////////////////////////////////////////////////////
1121// Class implementation
1122//////////////////////////////////////////////////////////////////////////////
1123
Jason Samsbe36bf32010-05-11 14:03:58 -07001124// llvm name mangling ref
1125// <builtin-type> ::= v # void
1126// ::= b # bool
1127// ::= c # char
1128// ::= a # signed char
1129// ::= h # unsigned char
1130// ::= s # short
1131// ::= t # unsigned short
1132// ::= i # int
1133// ::= j # unsigned int
1134// ::= l # long
1135// ::= m # unsigned long
1136// ::= x # long long, __int64
1137// ::= y # unsigned long long, __int64
1138// ::= f # float
1139// ::= d # double
Jason Samse45ac6e2009-07-20 14:31:06 -07001140
Jason Samsbe36bf32010-05-11 14:03:58 -07001141ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
1142 { "__divsi3", (void *)&SC_divsi3 },
1143
1144 // IO
1145 { "loadSimpleMeshVerticesF", (void *)&SC_loadSimpleMeshVerticesF },
1146 { "updateSimpleMesh", (void *)&SC_updateSimpleMesh },
1147
1148 // OpenCL math
1149 { "_Z4acosf", (void *)&acosf },
1150 { "_Z5acoshf", (void *)&acoshf },
1151 { "_Z6acospif", (void *)&SC_acospi },
1152 { "_Z4asinf", (void *)&asinf },
1153 { "_Z5asinhf", (void *)&asinhf },
1154 { "_Z6asinpif", (void *)&SC_asinpi },
1155 { "_Z4atanf", (void *)&atanf },
1156 { "_Z5atan2f", (void *)&atan2f },
1157 { "_Z6atanpif", (void *)&SC_atanpi },
1158 { "_Z7atan2pif", (void *)&SC_atan2pi },
1159 { "_Z4cbrtf", (void *)&cbrtf },
1160 { "_Z4ceilf", (void *)&ceilf },
1161 { "_Z8copysignff", (void *)&copysignf },
1162 { "_Z3cosf", (void *)&cosf },
1163 { "_Z4coshf", (void *)&coshf },
1164 { "_Z5cospif", (void *)&SC_cospi },
1165 { "_Z4erfcf", (void *)&erfcf },
1166 { "_Z3erff", (void *)&erff },
1167 { "_Z3expf", (void *)&expf },
1168 { "_Z4exp2f", (void *)&exp2f },
1169 { "_Z5exp10f", (void *)&SC_exp10 },
1170 { "_Z5expm1f", (void *)&expm1f },
1171 { "_Z4fabsf", (void *)&fabsf },
1172 { "_Z4fdimff", (void *)&fdimf },
1173 { "_Z5floorf", (void *)&floorf },
1174 { "_Z3fmafff", (void *)&fmaf },
1175 { "_Z4fmaxff", (void *)&fmaxf },
1176 { "_Z4fminff", (void *)&fminf }, // float fmin(float, float)
1177 { "_Z4fmodff", (void *)&fmodf },
1178 { "_Z5fractfPf", (void *)&SC_fract },
1179 { "_Z5frexpfPi", (void *)&frexpf },
1180 { "_Z5hypotff", (void *)&hypotf },
1181 { "_Z5ilogbf", (void *)&ilogbf },
1182 { "_Z5ldexpfi", (void *)&ldexpf },
1183 { "_Z6lgammaf", (void *)&lgammaf },
1184 { "_Z3logf", (void *)&logf },
1185 { "_Z4log2f", (void *)&SC_log2 },
1186 { "_Z5log10f", (void *)&log10f },
1187 { "_Z5log1pf", (void *)&log1pf },
1188 //{ "logb", (void *)& },
1189 //{ "mad", (void *)& },
1190 { "modf", (void *)&modff },
1191 //{ "nan", (void *)& },
1192 { "_Z9nextafterff", (void *)&nextafterf },
1193 { "_Z3powff", (void *)&powf },
1194 { "_Z4pownfi", (void *)&SC_pown },
1195 { "_Z4powrff", (void *)&SC_powr },
1196 { "_Z9remainderff", (void *)&remainderf },
1197 { "remquo", (void *)&remquof },
1198 { "_Z4rintf", (void *)&rintf },
1199 { "_Z5rootnfi", (void *)&SC_rootn },
1200 { "_Z5roundf", (void *)&roundf },
1201 { "_Z5rsqrtf", (void *)&SC_rsqrt },
1202 { "_Z3sinf", (void *)&sinf },
1203 { "sincos", (void *)&SC_sincos },
1204 { "_Z4sinhf", (void *)&sinhf },
1205 { "_Z5sinpif", (void *)&SC_sinpi },
1206 { "_Z4sqrtf", (void *)&sqrtf },
1207 { "_Z3tanf", (void *)&tanf },
1208 { "_Z4tanhf", (void *)&tanhf },
1209 { "_Z5tanpif", (void *)&SC_tanpi },
1210 //{ "tgamma", (void *)& },
1211 { "_Z5truncf", (void *)&truncf },
1212
1213 // OpenCL Int
1214 { "_Z3absi", (void *)&SC_abs_i32 },
1215 { "_Z3abss", (void *)&SC_abs_i16 },
1216 { "_Z3absc", (void *)&SC_abs_i8 },
1217 { "_Z3clzj", (void *)&SC_clz_u32 },
1218 { "_Z3clzt", (void *)&SC_clz_u16 },
1219 { "_Z3clzh", (void *)&SC_clz_u8 },
1220 { "_Z3clzi", (void *)&SC_clz_i32 },
1221 { "_Z3clzs", (void *)&SC_clz_i16 },
1222 { "_Z3clzc", (void *)&SC_clz_i8 },
1223 { "_Z3maxjj", (void *)&SC_max_u32 },
1224 { "_Z3maxtt", (void *)&SC_max_u16 },
1225 { "_Z3maxhh", (void *)&SC_max_u8 },
1226 { "_Z3maxii", (void *)&SC_max_i32 },
1227 { "_Z3maxss", (void *)&SC_max_i16 },
1228 { "_Z3maxcc", (void *)&SC_max_i8 },
1229 { "_Z3minjj", (void *)&SC_min_u32 },
1230 { "_Z3mintt", (void *)&SC_min_u16 },
1231 { "_Z3minhh", (void *)&SC_min_u8 },
1232 { "_Z3minii", (void *)&SC_min_i32 },
1233 { "_Z3minss", (void *)&SC_min_i16 },
1234 { "_Z3mincc", (void *)&SC_min_i8 },
1235
1236 // OpenCL 6.11.4
1237 { "_Z5clampfff", (void *)&SC_clamp_f32 },
1238 { "_Z7degreesf", (void *)&SC_degrees },
1239 { "_Z3maxff", (void *)&SC_max_f32 },
1240 { "_Z3minff", (void *)&SC_min_f32 },
1241 { "_Z3mixfff", (void *)&SC_mix_f32 },
1242 { "_Z7radiansf", (void *)&SC_radians },
1243 { "_Z4stepff", (void *)&SC_step_f32 },
1244 //{ "smoothstep", (void *)& },
1245 { "_Z4signf", (void *)&SC_sign_f32 },
1246
1247
1248
1249
1250 { "modf", (void *)&fmod },
1251 { "_Z4fracf", (void *)&SC_frac },
1252 //{ "sinf_fast", (void *)&SC_sinf_fast },
1253 //{ "cosf_fast", (void *)&SC_cosf_fast },
1254 { "randf", (void *)&SC_randf },
1255 { "randf2", (void *)&SC_randf2 },
1256 { "sign", (void *)&SC_sign },
1257 { "clamp", (void *)&SC_clamp },
1258 { "distf2", (void *)&SC_distf2 },
1259 { "distf3", (void *)&SC_distf3 },
1260 { "magf2", (void *)&SC_magf2 },
1261 { "magf3", (void *)&SC_magf3 },
1262 { "normf", (void *)&SC_normf },
1263 { "mapf", (void *)&SC_mapf },
1264 { "noisef", (void *)&SC_noisef },
1265 { "noisef2", (void *)&SC_noisef2 },
1266 { "noisef3", (void *)&SC_noisef3 },
1267 { "turbulencef2", (void *)&SC_turbulencef2 },
1268 { "turbulencef3", (void *)&SC_turbulencef3 },
Jason Samse45ac6e2009-07-20 14:31:06 -07001269
Romain Guy98e10fd2009-07-30 18:45:01 -07001270 // time
Jason Samsbe36bf32010-05-11 14:03:58 -07001271 { "second", (void *)&SC_second },
1272 { "minute", (void *)&SC_minute },
1273 { "hour", (void *)&SC_hour },
1274 { "day", (void *)&SC_day },
1275 { "month", (void *)&SC_month },
1276 { "year", (void *)&SC_year },
1277 { "uptimeMillis", (void*)&SC_uptimeMillis }, // TODO: use long instead
1278 { "startTimeMillis", (void*)&SC_startTimeMillis }, // TODO: use long instead
1279 { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis }, // TODO: use long instead
Romain Guy98e10fd2009-07-30 18:45:01 -07001280
Jason Samse45ac6e2009-07-20 14:31:06 -07001281 // matrix
Jason Samsbe36bf32010-05-11 14:03:58 -07001282 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity },
1283 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat },
1284 { "matrixLoadMat", (void *)&SC_matrixLoadMat },
1285 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate },
1286 { "matrixLoadScale", (void *)&SC_matrixLoadScale },
1287 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate },
1288 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply },
1289 { "matrixMultiply", (void *)&SC_matrixMultiply },
1290 { "matrixRotate", (void *)&SC_matrixRotate },
1291 { "matrixScale", (void *)&SC_matrixScale },
1292 { "matrixTranslate", (void *)&SC_matrixTranslate },
Jason Samse45ac6e2009-07-20 14:31:06 -07001293
Jason Sams90b36a82009-08-17 13:56:09 -07001294 // vector
Jason Samsbe36bf32010-05-11 14:03:58 -07001295 { "vec2Rand", (void *)&SC_vec2Rand },
Jason Sams90b36a82009-08-17 13:56:09 -07001296
Jason Samsa57c0a72009-09-04 14:42:41 -07001297 // vec3
Jason Samsbe36bf32010-05-11 14:03:58 -07001298 { "vec3Norm", (void *)&SC_vec3Norm },
1299 { "vec3Length", (void *)&SC_vec3Length },
1300 { "vec3Add", (void *)&SC_vec3Add },
1301 { "vec3Sub", (void *)&SC_vec3Sub },
1302 { "vec3Cross", (void *)&SC_vec3Cross },
1303 { "vec3Dot", (void *)&SC_vec3Dot },
1304 { "vec3Scale", (void *)&SC_vec3Scale },
Jason Samsa57c0a72009-09-04 14:42:41 -07001305
Romain Guyd6d4a5f2009-10-09 16:05:25 -07001306 // vec4
Jason Samsbe36bf32010-05-11 14:03:58 -07001307 { "vec4Norm", (void *)&SC_vec4Norm },
1308 { "vec4Length", (void *)&SC_vec4Length },
1309 { "vec4Add", (void *)&SC_vec4Add },
1310 { "vec4Sub", (void *)&SC_vec4Sub },
1311 { "vec4Dot", (void *)&SC_vec4Dot },
1312 { "vec4Scale", (void *)&SC_vec4Scale },
Romain Guyd6d4a5f2009-10-09 16:05:25 -07001313
Jason Samse45ac6e2009-07-20 14:31:06 -07001314 // context
Jason Samsbe36bf32010-05-11 14:03:58 -07001315 { "bindProgramFragment", (void *)&SC_bindProgramFragment },
1316 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore },
1317 { "bindProgramStore", (void *)&SC_bindProgramFragmentStore },
1318 { "bindProgramVertex", (void *)&SC_bindProgramVertex },
1319 { "bindSampler", (void *)&SC_bindSampler },
1320 { "bindTexture", (void *)&SC_bindTexture },
Jason Samse45ac6e2009-07-20 14:31:06 -07001321
Jason Samsc9d43db2009-07-28 12:02:16 -07001322 // vp
Jason Samsbe36bf32010-05-11 14:03:58 -07001323 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix },
1324 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix },
Jason Samsc9d43db2009-07-28 12:02:16 -07001325
Jason Samsbe36bf32010-05-11 14:03:58 -07001326 // allocation
1327 { "allocGetDimX", (void *)&SC_allocGetDimX },
1328 { "allocGetDimY", (void *)&SC_allocGetDimY },
1329 { "allocGetDimZ", (void *)&SC_allocGetDimZ },
1330 { "allocGetDimLOD", (void *)&SC_allocGetDimLOD },
1331 { "allocGetDimFaces", (void *)&SC_allocGetDimFaces },
Jason Samsc9d43db2009-07-28 12:02:16 -07001332
Jason Samse45ac6e2009-07-20 14:31:06 -07001333 // drawing
Jason Samsbe36bf32010-05-11 14:03:58 -07001334 { "drawRect", (void *)&SC_drawRect },
1335 { "drawQuad", (void *)&SC_drawQuad },
1336 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords },
1337 { "drawSprite", (void *)&SC_drawSprite },
1338 { "drawSpriteScreenspace", (void *)&SC_drawSpriteScreenspace },
1339 { "drawSpriteScreenspaceCropped", (void *)&SC_drawSpriteScreenspaceCropped },
1340 { "drawLine", (void *)&SC_drawLine },
1341 { "drawPoint", (void *)&SC_drawPoint },
1342 { "drawSimpleMesh", (void *)&SC_drawSimpleMesh },
1343 { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange },
Jason Samse45ac6e2009-07-20 14:31:06 -07001344
1345
1346 // misc
Jason Samsbe36bf32010-05-11 14:03:58 -07001347 { "pfClearColor", (void *)&SC_ClearColor },
1348 { "color", (void *)&SC_color },
1349 { "hsb", (void *)&SC_hsb },
1350 { "hsbToRgb", (void *)&SC_hsbToRgb },
1351 { "hsbToAbgr", (void *)&SC_hsbToAbgr },
1352 { "pointAttenuation", (void *)&SC_pointAttenuation },
Jason Samse45ac6e2009-07-20 14:31:06 -07001353
Jason Samsbe36bf32010-05-11 14:03:58 -07001354 { "uploadToTexture", (void *)&SC_uploadToTexture },
1355 { "uploadToBufferObject", (void *)&SC_uploadToBufferObject },
Jason Samsc9d43db2009-07-28 12:02:16 -07001356
Jason Samsbe36bf32010-05-11 14:03:58 -07001357 { "syncToGL", (void *)&SC_syncToGL },
Jason Sams9ebb0c42010-01-12 12:12:28 -08001358
Jason Samsbe36bf32010-05-11 14:03:58 -07001359 { "getWidth", (void *)&SC_getWidth },
1360 { "getHeight", (void *)&SC_getHeight },
Jason Sams90b36a82009-08-17 13:56:09 -07001361
Jason Samsbe36bf32010-05-11 14:03:58 -07001362 { "sendToClient", (void *)&SC_toClient },
Jason Sams90b36a82009-08-17 13:56:09 -07001363
Jason Samsbe36bf32010-05-11 14:03:58 -07001364 { "_Z18convertColorTo8888fff", (void *)&SC_convertColorTo8888_f3 },
1365 { "_Z18convertColorTo8888ffff", (void *)&SC_convertColorTo8888_f4 },
Jason Samse579df42009-08-10 14:55:26 -07001366
Jason Samsbe36bf32010-05-11 14:03:58 -07001367 { "debugF", (void *)&SC_debugF },
1368 { "debugI32", (void *)&SC_debugI32 },
1369 { "debugHexF", (void *)&SC_debugHexF },
1370 { "debugHexI32", (void *)&SC_debugHexI32 },
1371 { "debugP", (void *)&SC_debugP },
1372 { "debugPf", (void *)&SC_debugPf },
1373 { "debugPi", (void *)&SC_debugPi },
Jason Samse579df42009-08-10 14:55:26 -07001374
Jason Samsbe36bf32010-05-11 14:03:58 -07001375 { "scriptCall", (void *)&SC_scriptCall },
Jason Samsc9d43db2009-07-28 12:02:16 -07001376
Jason Samsbe36bf32010-05-11 14:03:58 -07001377 { NULL, NULL }
Jason Samse45ac6e2009-07-20 14:31:06 -07001378};
1379
1380const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
1381{
1382 ScriptCState::SymbolTable_t *syms = gSyms;
1383
1384 while (syms->mPtr) {
1385 if (!strcmp(syms->mName, sym)) {
1386 return syms;
1387 }
1388 syms++;
1389 }
1390 return NULL;
1391}
1392