blob: 5cf0fdbfc84b98873abd7f3517b9ebf5d573129d [file] [log] [blame]
Jason Samsc97bb882009-07-20 14:31:06 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsContext.h"
18#include "rsScriptC.h"
19#include "rsMatrix.h"
Romain Guyecc7ca032009-08-03 21:12:51 -070020#include "rsNoise.h"
Jason Samsc97bb882009-07-20 14:31:06 -070021
22#include "acc/acc.h"
Joe Onorato3370ec92009-08-09 11:39:02 -070023#include "utils/Timers.h"
Jason Samsc97bb882009-07-20 14:31:06 -070024
Jason Samse9ad9a72009-09-30 17:36:20 -070025#define GL_GLEXT_PROTOTYPES
26
Jason Samsc97bb882009-07-20 14:31:06 -070027#include <GLES/gl.h>
28#include <GLES/glext.h>
Jason Samsbb51c402009-11-25 13:22:07 -080029#include <GLES2/gl2.h>
30#include <GLES2/gl2ext.h>
Jason Samsc97bb882009-07-20 14:31:06 -070031
Romain Guy584a3752009-07-30 18:45:01 -070032#include <time.h>
Romain Guy584a3752009-07-30 18:45:01 -070033
Jason Samsc97bb882009-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 Sams4d339932010-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 Samsea84a7c2009-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 Samsc97bb882009-07-20 14:31:06 -0700213
214//////////////////////////////////////////////////////////////////////////////
215// IO routines
216//////////////////////////////////////////////////////////////////////////////
217
Jason Sams6b9dec02009-09-23 16:38:37 -0700218static float* SC_loadSimpleMeshVerticesF(RsSimpleMesh mesh, uint32_t idx)
Romain Guyb62627e2009-08-06 22:52:13 -0700219{
Jason Sams6b9dec02009-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 Guyb62627e2009-08-06 22:52:13 -0700223}
224
Jason Sams6b9dec02009-09-23 16:38:37 -0700225static void SC_updateSimpleMesh(RsSimpleMesh mesh)
Romain Guyb62627e2009-08-06 22:52:13 -0700226{
Jason Samsbb51c402009-11-25 13:22:07 -0800227 GET_TLS();
Jason Sams6b9dec02009-09-23 16:38:37 -0700228 SimpleMesh *sm = static_cast<SimpleMesh *>(mesh);
Jason Samsbb51c402009-11-25 13:22:07 -0800229 sm->uploadAll(rsc);
Romain Guyb62627e2009-08-06 22:52:13 -0700230}
Romain Guyf8e136d2009-08-06 12:40:41 -0700231
Jason Samsea84a7c2009-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 Guyd7fa1222009-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 Samsc97bb882009-07-20 14:31:06 -0700333
334//////////////////////////////////////////////////////////////////////////////
335// Math routines
336//////////////////////////////////////////////////////////////////////////////
337
Romain Guycac80a62009-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 Samsea84a7c2009-09-04 14:42:41 -0700343
Romain Guycac80a62009-08-18 11:39:17 -0700344 // scale angle for easy argument reduction
345 x *= A;
Jason Samsea84a7c2009-09-04 14:42:41 -0700346
Romain Guycac80a62009-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 Samsea84a7c2009-09-04 14:42:41 -0700351
Romain Guycac80a62009-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 Samsea84a7c2009-09-04 14:42:41 -0700363
Romain Guycac80a62009-08-18 11:39:17 -0700364 // scale angle for easy argument reduction
365 x *= A;
Jason Samsea84a7c2009-09-04 14:42:41 -0700366
Romain Guycac80a62009-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 Samsea84a7c2009-09-04 14:42:41 -0700371
Romain Guycac80a62009-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 Samsc97bb882009-07-20 14:31:06 -0700376static float SC_randf(float max)
377{
Jason Sams4d339932010-05-11 14:03:58 -0700378 //LOGE("max %f", max);
Jason Samsc97bb882009-07-20 14:31:06 -0700379 float r = (float)rand();
380 return r / RAND_MAX * max;
381}
382
Romain Guy8839ca52009-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 Guyd7fa1222009-10-09 16:05:25 -0700389static int SC_sign(int value)
390{
391 return (value > 0) - (value < 0);
392}
393
Romain Guya9d2d5e2009-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 Samsd342fd72009-09-18 14:24:24 -0700399static float SC_roundf(float v)
400{
401 return floorf(v + 0.4999999999);
402}
403
Romain Guy8839ca52009-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 Sams1bada8c2009-08-09 17:01:55 -0700408 return sqrtf(x * x + y * y);
Romain Guy8839ca52009-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 Sams1bada8c2009-08-09 17:01:55 -0700416 return sqrtf(x * x + y * y + z * z);
Romain Guy8839ca52009-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 Guy8839ca52009-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 Samsc97bb882009-07-20 14:31:06 -0700438
Jason Sams4d339932010-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 Guy584a3752009-07-30 18:45:01 -0700445//////////////////////////////////////////////////////////////////////////////
446// Time routines
447//////////////////////////////////////////////////////////////////////////////
Jason Samsc97bb882009-07-20 14:31:06 -0700448
Joe Onorato3370ec92009-08-09 11:39:02 -0700449static int32_t SC_second()
Romain Guy584a3752009-07-30 18:45:01 -0700450{
451 GET_TLS();
452
453 time_t rawtime;
454 time(&rawtime);
455
Romain Guybaed7272009-11-11 15:36:06 -0800456 struct tm *timeinfo;
457 timeinfo = localtime(&rawtime);
458 return timeinfo->tm_sec;
Romain Guy584a3752009-07-30 18:45:01 -0700459}
460
Joe Onorato3370ec92009-08-09 11:39:02 -0700461static int32_t SC_minute()
Romain Guy584a3752009-07-30 18:45:01 -0700462{
463 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700464
Romain Guy584a3752009-07-30 18:45:01 -0700465 time_t rawtime;
466 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700467
Romain Guybaed7272009-11-11 15:36:06 -0800468 struct tm *timeinfo;
469 timeinfo = localtime(&rawtime);
470 return timeinfo->tm_min;
Jason Sams1bada8c2009-08-09 17:01:55 -0700471}
Romain Guy584a3752009-07-30 18:45:01 -0700472
Joe Onorato3370ec92009-08-09 11:39:02 -0700473static int32_t SC_hour()
Romain Guy584a3752009-07-30 18:45:01 -0700474{
475 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700476
Romain Guy584a3752009-07-30 18:45:01 -0700477 time_t rawtime;
478 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700479
Romain Guybaed7272009-11-11 15:36:06 -0800480 struct tm *timeinfo;
481 timeinfo = localtime(&rawtime);
482 return timeinfo->tm_hour;
Romain Guy8839ca52009-07-31 11:20:59 -0700483}
484
Joe Onorato3370ec92009-08-09 11:39:02 -0700485static int32_t SC_day()
Romain Guy8839ca52009-07-31 11:20:59 -0700486{
487 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700488
Romain Guy8839ca52009-07-31 11:20:59 -0700489 time_t rawtime;
490 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700491
Romain Guybaed7272009-11-11 15:36:06 -0800492 struct tm *timeinfo;
493 timeinfo = localtime(&rawtime);
494 return timeinfo->tm_mday;
Jason Sams1bada8c2009-08-09 17:01:55 -0700495}
Jason Samsc97bb882009-07-20 14:31:06 -0700496
Joe Onorato3370ec92009-08-09 11:39:02 -0700497static int32_t SC_month()
Romain Guy8839ca52009-07-31 11:20:59 -0700498{
499 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700500
Romain Guy8839ca52009-07-31 11:20:59 -0700501 time_t rawtime;
502 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700503
Romain Guybaed7272009-11-11 15:36:06 -0800504 struct tm *timeinfo;
505 timeinfo = localtime(&rawtime);
506 return timeinfo->tm_mon;
Jason Sams1bada8c2009-08-09 17:01:55 -0700507}
Romain Guy8839ca52009-07-31 11:20:59 -0700508
Joe Onorato3370ec92009-08-09 11:39:02 -0700509static int32_t SC_year()
Romain Guy8839ca52009-07-31 11:20:59 -0700510{
511 GET_TLS();
Jason Sams1bada8c2009-08-09 17:01:55 -0700512
Romain Guy8839ca52009-07-31 11:20:59 -0700513 time_t rawtime;
514 time(&rawtime);
Jason Sams1bada8c2009-08-09 17:01:55 -0700515
Romain Guybaed7272009-11-11 15:36:06 -0800516 struct tm *timeinfo;
517 timeinfo = localtime(&rawtime);
518 return timeinfo->tm_year;
Romain Guy8839ca52009-07-31 11:20:59 -0700519}
520
Joe Onorato3370ec92009-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 Samsc97bb882009-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 Sams4d339932010-05-11 14:03:58 -0700612static rsvF_2 SC_vec2Rand(float maxLen)
Jason Sams334ea0c2009-08-17 13:56:09 -0700613{
Jason Sams4d339932010-05-11 14:03:58 -0700614 float2 t;
615 float angle = SC_randf(M_PI * 2);
Jason Sams334ea0c2009-08-17 13:56:09 -0700616 float len = SC_randf(maxLen);
Jason Sams4d339932010-05-11 14:03:58 -0700617 t.f[0] = len * sinf(angle);
618 t.f[1] = len * cosf(angle);
619 return t.v;
Jason Sams334ea0c2009-08-17 13:56:09 -0700620}
621
Jason Samsc97bb882009-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 Sams68afd012009-12-17 16:55:08 -0800631 rsi_ProgramBindTexture(rsc,
632 static_cast<ProgramFragment *>(vpf),
633 slot,
634 static_cast<Allocation *>(va));
Jason Samsc97bb882009-07-20 14:31:06 -0700635
636}
637
638static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
639{
640 GET_TLS();
Jason Sams68afd012009-12-17 16:55:08 -0800641 rsi_ProgramBindSampler(rsc,
642 static_cast<ProgramFragment *>(vpf),
643 slot,
644 static_cast<Sampler *>(vs));
Jason Samsc97bb882009-07-20 14:31:06 -0700645
646}
647
648static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs)
649{
650 GET_TLS();
651 rsi_ContextBindProgramFragmentStore(rsc, pfs);
Jason Samsc97bb882009-07-20 14:31:06 -0700652}
653
654static void SC_bindProgramFragment(RsProgramFragment pf)
655{
656 GET_TLS();
657 rsi_ContextBindProgramFragment(rsc, pf);
Jason Samsc97bb882009-07-20 14:31:06 -0700658}
659
Jason Samsee411122009-07-21 12:20:54 -0700660static void SC_bindProgramVertex(RsProgramVertex pv)
661{
662 GET_TLS();
663 rsi_ContextBindProgramVertex(rsc, pv);
Jason Sams0c677312010-05-12 18:26:52 -0700664}
Jason Samsee411122009-07-21 12:20:54 -0700665
Jason Sams0c677312010-05-12 18:26:52 -0700666static void SC_bindProgramRaster(RsProgramRaster pv)
667{
668 GET_TLS();
669 rsi_ContextBindProgramRaster(rsc, pv);
Jason Samsee411122009-07-21 12:20:54 -0700670}
Jason Samsc97bb882009-07-20 14:31:06 -0700671
672//////////////////////////////////////////////////////////////////////////////
Jason Samsb0ec1b42009-07-28 12:02:16 -0700673// VP
674//////////////////////////////////////////////////////////////////////////////
675
676static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
677{
678 GET_TLS();
679 rsc->getVertex()->setModelviewMatrix(m);
680}
681
682static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
683{
684 GET_TLS();
685 rsc->getVertex()->setTextureMatrix(m);
686}
687
688
689
690//////////////////////////////////////////////////////////////////////////////
Jason Samsc97bb882009-07-20 14:31:06 -0700691// Drawing
692//////////////////////////////////////////////////////////////////////////////
693
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700694static void SC_drawLine(float x1, float y1, float z1,
695 float x2, float y2, float z2)
696{
697 GET_TLS();
Jason Sams156cce62010-03-03 13:03:18 -0800698 if (!rsc->setupCheck()) {
699 return;
700 }
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700701
702 float vtx[] = { x1, y1, z1, x2, y2, z2 };
Jason Samsbb51c402009-11-25 13:22:07 -0800703 VertexArray va;
Jason Samsdf48b572010-01-25 12:31:24 -0800704 va.addLegacy(GL_FLOAT, 3, 12, RS_KIND_POSITION, false, (uint32_t)vtx);
Jason Samsbb51c402009-11-25 13:22:07 -0800705 if (rsc->checkVersion2_0()) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800706 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsbb51c402009-11-25 13:22:07 -0800707 } else {
Jason Sams718cd1f2009-12-23 14:35:29 -0800708 va.setupGL(rsc, &rsc->mStateVertexArray);
Jason Samsbb51c402009-11-25 13:22:07 -0800709 }
Romain Guy6c0cc6d2009-08-07 15:40:32 -0700710
711 glDrawArrays(GL_LINES, 0, 2);
712}
713
Jason Sams5235cf32009-09-28 18:12:56 -0700714static void SC_drawPoint(float x, float y, float z)
715{
716 GET_TLS();
Jason Sams156cce62010-03-03 13:03:18 -0800717 if (!rsc->setupCheck()) {
718 return;
719 }
Jason Sams5235cf32009-09-28 18:12:56 -0700720
721 float vtx[] = { x, y, z };
722
Jason Samsbb51c402009-11-25 13:22:07 -0800723 VertexArray va;
Jason Samsdf48b572010-01-25 12:31:24 -0800724 va.addLegacy(GL_FLOAT, 3, 12, RS_KIND_POSITION, false, (uint32_t)vtx);
Jason Samsbb51c402009-11-25 13:22:07 -0800725 if (rsc->checkVersion2_0()) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800726 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsbb51c402009-11-25 13:22:07 -0800727 } else {
Jason Sams718cd1f2009-12-23 14:35:29 -0800728 va.setupGL(rsc, &rsc->mStateVertexArray);
Jason Samsbb51c402009-11-25 13:22:07 -0800729 }
Jason Sams5235cf32009-09-28 18:12:56 -0700730
731 glDrawArrays(GL_POINTS, 0, 1);
732}
733
Romain Guy8f5c94b2009-08-08 18:30:19 -0700734static void SC_drawQuadTexCoords(float x1, float y1, float z1,
735 float u1, float v1,
736 float x2, float y2, float z2,
737 float u2, float v2,
738 float x3, float y3, float z3,
739 float u3, float v3,
740 float x4, float y4, float z4,
741 float u4, float v4)
Jason Samsc97bb882009-07-20 14:31:06 -0700742{
743 GET_TLS();
Jason Sams156cce62010-03-03 13:03:18 -0800744 if (!rsc->setupCheck()) {
745 return;
746 }
Jason Sams40a29e82009-08-10 14:55:26 -0700747
Jason Samsc97bb882009-07-20 14:31:06 -0700748 //LOGE("Quad");
749 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
750 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
751 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
752 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
Jason Sams40a29e82009-08-10 14:55:26 -0700753
Jason Samsc97bb882009-07-20 14:31:06 -0700754 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
Romain Guy8f5c94b2009-08-08 18:30:19 -0700755 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
Jason Samsc97bb882009-07-20 14:31:06 -0700756
Jason Samsbb51c402009-11-25 13:22:07 -0800757 VertexArray va;
Jason Samsdf48b572010-01-25 12:31:24 -0800758 va.addLegacy(GL_FLOAT, 3, 12, RS_KIND_POSITION, false, (uint32_t)vtx);
759 va.addLegacy(GL_FLOAT, 2, 8, RS_KIND_TEXTURE, false, (uint32_t)tex);
Jason Samsbb51c402009-11-25 13:22:07 -0800760 if (rsc->checkVersion2_0()) {
Jason Sams718cd1f2009-12-23 14:35:29 -0800761 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsbb51c402009-11-25 13:22:07 -0800762 } else {
Jason Sams718cd1f2009-12-23 14:35:29 -0800763 va.setupGL(rsc, &rsc->mStateVertexArray);
Jason Samsbb51c402009-11-25 13:22:07 -0800764 }
Jason Samsc97bb882009-07-20 14:31:06 -0700765
Jason Samsc97bb882009-07-20 14:31:06 -0700766
767 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
768}
769
Romain Guy8f5c94b2009-08-08 18:30:19 -0700770static void SC_drawQuad(float x1, float y1, float z1,
771 float x2, float y2, float z2,
772 float x3, float y3, float z3,
773 float x4, float y4, float z4)
774{
775 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
776 x2, y2, z2, 1, 1,
777 x3, y3, z3, 1, 0,
778 x4, y4, z4, 0, 0);
779}
780
Jason Samse9ad9a72009-09-30 17:36:20 -0700781static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h)
782{
783 GET_TLS();
Jason Samsbb51c402009-11-25 13:22:07 -0800784 ObjectBaseRef<const ProgramVertex> tmp(rsc->getVertex());
785 rsc->setVertex(rsc->getDefaultProgramVertex());
786 //rsc->setupCheck();
Jason Samse9ad9a72009-09-30 17:36:20 -0700787
Jason Samsbb51c402009-11-25 13:22:07 -0800788 //GLint crop[4] = {0, h, w, -h};
789
790 float sh = rsc->getHeight();
791
792 SC_drawQuad(x, sh - y, z,
793 x+w, sh - y, z,
794 x+w, sh - (y+h), z,
795 x, sh - (y+h), z);
796 rsc->setVertex((ProgramVertex *)tmp.get());
Jason Samse9ad9a72009-09-30 17:36:20 -0700797}
798
Joe Onorato6656c1b2010-01-14 15:59:35 -0500799static void SC_drawSpriteScreenspaceCropped(float x, float y, float z, float w, float h,
800 float cx0, float cy0, float cx1, float cy1)
801{
802 GET_TLS();
Jason Sams156cce62010-03-03 13:03:18 -0800803 if (!rsc->setupCheck()) {
804 return;
805 }
Joe Onorato6656c1b2010-01-14 15:59:35 -0500806
807 GLint crop[4] = {cx0, cy0, cx1, cy1};
808 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
809 glDrawTexfOES(x, y, z, w, h);
810}
811
Jason Samse9ad9a72009-09-30 17:36:20 -0700812static void SC_drawSprite(float x, float y, float z, float w, float h)
813{
814 GET_TLS();
Jason Samse9ad9a72009-09-30 17:36:20 -0700815 float vin[3] = {x, y, z};
816 float vout[4];
817
818 //LOGE("ds in %f %f %f", x, y, z);
819 rsc->getVertex()->transformToScreen(rsc, vout, vin);
820 //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]);
821 vout[0] /= vout[3];
822 vout[1] /= vout[3];
823 vout[2] /= vout[3];
824
825 vout[0] *= rsc->getWidth() / 2;
826 vout[1] *= rsc->getHeight() / 2;
827 vout[0] += rsc->getWidth() / 2;
828 vout[1] += rsc->getHeight() / 2;
829
830 vout[0] -= w/2;
831 vout[1] -= h/2;
832
833 //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]);
834
835 // U, V, W, H
Jason Samsbb51c402009-11-25 13:22:07 -0800836 SC_drawSpriteScreenspace(vout[0], vout[1], z, h, w);
837 //rsc->setupCheck();
Jason Samse9ad9a72009-09-30 17:36:20 -0700838}
839
840
Jason Sams6f5c61c2009-07-28 17:20:11 -0700841static void SC_drawRect(float x1, float y1,
842 float x2, float y2, float z)
843{
Jason Sams4d339932010-05-11 14:03:58 -0700844 //LOGE("SC_drawRect %f,%f %f,%f %f", x1, y1, x2, y2, z);
Jason Sams6f5c61c2009-07-28 17:20:11 -0700845 SC_drawQuad(x1, y2, z,
846 x2, y2, z,
847 x2, y1, z,
848 x1, y1, z);
849}
850
Jason Sams1bada8c2009-08-09 17:01:55 -0700851static void SC_drawSimpleMesh(RsSimpleMesh vsm)
852{
853 GET_TLS();
854 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
Jason Sams156cce62010-03-03 13:03:18 -0800855 if (!rsc->setupCheck()) {
856 return;
857 }
Jason Samsbb51c402009-11-25 13:22:07 -0800858 sm->render(rsc);
Jason Sams1bada8c2009-08-09 17:01:55 -0700859}
860
861static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len)
862{
863 GET_TLS();
864 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
Jason Sams156cce62010-03-03 13:03:18 -0800865 if (!rsc->setupCheck()) {
866 return;
867 }
Jason Samsbb51c402009-11-25 13:22:07 -0800868 sm->renderRange(rsc, start, len);
Jason Sams1bada8c2009-08-09 17:01:55 -0700869}
870
871
Jason Samsc97bb882009-07-20 14:31:06 -0700872//////////////////////////////////////////////////////////////////////////////
873//
874//////////////////////////////////////////////////////////////////////////////
875
Jason Sams4d339932010-05-11 14:03:58 -0700876static uint32_t SC_allocGetDimX(RsAllocation va)
877{
878 GET_TLS();
879 const Allocation *a = static_cast<const Allocation *>(va);
880 //LOGE("SC_allocGetDimX a=%p", a);
881 //LOGE(" type=%p", a->getType());
882 return a->getType()->getDimX();
883}
884
885static uint32_t SC_allocGetDimY(RsAllocation va)
886{
887 GET_TLS();
888 const Allocation *a = static_cast<const Allocation *>(va);
889 return a->getType()->getDimY();
890}
891
892static uint32_t SC_allocGetDimZ(RsAllocation va)
893{
894 GET_TLS();
895 const Allocation *a = static_cast<const Allocation *>(va);
896 return a->getType()->getDimZ();
897}
898
899static uint32_t SC_allocGetDimLOD(RsAllocation va)
900{
901 GET_TLS();
902 const Allocation *a = static_cast<const Allocation *>(va);
903 return a->getType()->getDimLOD();
904}
905
906static uint32_t SC_allocGetDimFaces(RsAllocation va)
907{
908 GET_TLS();
909 const Allocation *a = static_cast<const Allocation *>(va);
910 return a->getType()->getDimFaces();
911}
912
913
Jason Samsc97bb882009-07-20 14:31:06 -0700914static void SC_color(float r, float g, float b, float a)
915{
Jason Samsbb51c402009-11-25 13:22:07 -0800916 GET_TLS();
Jason Samsce9adcc2009-12-16 14:13:06 -0800917 rsc->mStateVertex.color[0] = r;
918 rsc->mStateVertex.color[1] = g;
919 rsc->mStateVertex.color[2] = b;
920 rsc->mStateVertex.color[3] = a;
Jason Sams68afd012009-12-17 16:55:08 -0800921 if (!rsc->checkVersion2_0()) {
Jason Samsbb51c402009-11-25 13:22:07 -0800922 glColor4f(r, g, b, a);
923 }
Jason Samsc97bb882009-07-20 14:31:06 -0700924}
925
Romain Guy2d496bf2009-09-04 17:55:41 -0700926static void SC_pointAttenuation(float a, float b, float c)
927{
928 GLfloat params[] = { a, b, c };
929 glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, params);
930}
931
Romain Guyd22fff72009-08-20 17:08:33 -0700932static void SC_hsbToRgb(float h, float s, float b, float* rgb)
Romain Guya32d1002009-07-31 15:33:59 -0700933{
934 float red = 0.0f;
935 float green = 0.0f;
936 float blue = 0.0f;
Jason Sams1bada8c2009-08-09 17:01:55 -0700937
Romain Guya32d1002009-07-31 15:33:59 -0700938 float x = h;
939 float y = s;
940 float z = b;
Jason Sams1bada8c2009-08-09 17:01:55 -0700941
Romain Guya32d1002009-07-31 15:33:59 -0700942 float hf = (x - (int) x) * 6.0f;
943 int ihf = (int) hf;
944 float f = hf - ihf;
945 float pv = z * (1.0f - y);
946 float qv = z * (1.0f - y * f);
947 float tv = z * (1.0f - y * (1.0f - f));
Jason Sams1bada8c2009-08-09 17:01:55 -0700948
Romain Guya32d1002009-07-31 15:33:59 -0700949 switch (ihf) {
950 case 0: // Red is the dominant color
951 red = z;
952 green = tv;
953 blue = pv;
954 break;
955 case 1: // Green is the dominant color
956 red = qv;
957 green = z;
958 blue = pv;
959 break;
960 case 2:
961 red = pv;
962 green = z;
963 blue = tv;
964 break;
965 case 3: // Blue is the dominant color
966 red = pv;
967 green = qv;
968 blue = z;
969 break;
970 case 4:
971 red = tv;
972 green = pv;
973 blue = z;
974 break;
975 case 5: // Red is the dominant color
976 red = z;
977 green = pv;
978 blue = qv;
979 break;
980 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700981
Romain Guyd22fff72009-08-20 17:08:33 -0700982 rgb[0] = red;
983 rgb[1] = green;
984 rgb[2] = blue;
985}
986
987static int SC_hsbToAbgr(float h, float s, float b, float a)
988{
Jason Sams4d339932010-05-11 14:03:58 -0700989 //LOGE("hsb a %f, %f, %f %f", h, s, b, a);
Romain Guyd22fff72009-08-20 17:08:33 -0700990 float rgb[3];
991 SC_hsbToRgb(h, s, b, rgb);
Jason Sams4d339932010-05-11 14:03:58 -0700992 //LOGE("rgb %f, %f, %f ", rgb[0], rgb[1], rgb[2]);
Romain Guyd22fff72009-08-20 17:08:33 -0700993 return int(a * 255.0f) << 24 |
994 int(rgb[2] * 255.0f) << 16 |
995 int(rgb[1] * 255.0f) << 8 |
996 int(rgb[0] * 255.0f);
997}
998
999static void SC_hsb(float h, float s, float b, float a)
1000{
Jason Samsbb51c402009-11-25 13:22:07 -08001001 GET_TLS();
Romain Guyd22fff72009-08-20 17:08:33 -07001002 float rgb[3];
1003 SC_hsbToRgb(h, s, b, rgb);
Jason Samsbb51c402009-11-25 13:22:07 -08001004 if (rsc->checkVersion2_0()) {
1005 glVertexAttrib4f(1, rgb[0], rgb[1], rgb[2], a);
1006 } else {
1007 glColor4f(rgb[0], rgb[1], rgb[2], a);
1008 }
Romain Guya32d1002009-07-31 15:33:59 -07001009}
1010
Jason Samsb0ec1b42009-07-28 12:02:16 -07001011static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samsc97bb882009-07-20 14:31:06 -07001012{
1013 GET_TLS();
Jason Samsc2908e62010-02-23 17:44:28 -08001014 rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel);
Jason Samsc97bb882009-07-20 14:31:06 -07001015}
1016
Jason Sams1bada8c2009-08-09 17:01:55 -07001017static void SC_uploadToBufferObject(RsAllocation va)
1018{
1019 GET_TLS();
1020 rsi_AllocationUploadToBufferObject(rsc, va);
1021}
1022
Jason Samsea87e962010-01-12 12:12:28 -08001023static void SC_syncToGL(RsAllocation va)
1024{
1025 GET_TLS();
1026 Allocation *a = static_cast<Allocation *>(va);
1027
1028}
1029
Jason Samsc97bb882009-07-20 14:31:06 -07001030static void SC_ClearColor(float r, float g, float b, float a)
1031{
1032 //LOGE("c %f %f %f %f", r, g, b, a);
1033 GET_TLS();
1034 sc->mEnviroment.mClearColor[0] = r;
1035 sc->mEnviroment.mClearColor[1] = g;
1036 sc->mEnviroment.mClearColor[2] = b;
1037 sc->mEnviroment.mClearColor[3] = a;
1038}
1039
Jason Samsb0ec1b42009-07-28 12:02:16 -07001040static void SC_debugF(const char *s, float f)
1041{
1042 LOGE("%s %f", s, f);
1043}
1044
Romain Guyd22fff72009-08-20 17:08:33 -07001045static void SC_debugHexF(const char *s, float f)
1046{
1047 LOGE("%s 0x%x", s, *((int *) (&f)));
1048}
1049
Jason Samsb0ec1b42009-07-28 12:02:16 -07001050static void SC_debugI32(const char *s, int32_t i)
1051{
1052 LOGE("%s %i", s, i);
1053}
1054
Romain Guyd22fff72009-08-20 17:08:33 -07001055static void SC_debugHexI32(const char *s, int32_t i)
1056{
1057 LOGE("%s 0x%x", s, i);
1058}
1059
Jason Sams40a29e82009-08-10 14:55:26 -07001060static uint32_t SC_getWidth()
1061{
1062 GET_TLS();
1063 return rsc->getWidth();
1064}
Jason Samsc97bb882009-07-20 14:31:06 -07001065
Jason Sams40a29e82009-08-10 14:55:26 -07001066static uint32_t SC_getHeight()
1067{
1068 GET_TLS();
1069 return rsc->getHeight();
1070}
Jason Samsc97bb882009-07-20 14:31:06 -07001071
Jason Sams4d339932010-05-11 14:03:58 -07001072static uchar4 SC_convertColorTo8888_f3(float r, float g, float b) {
1073 uchar4 t;
1074 t.f[0] = (uint8_t)(r * 255.f);
1075 t.f[1] = (uint8_t)(g * 255.f);
1076 t.f[2] = (uint8_t)(b * 255.f);
1077 t.f[3] = 0xff;
1078 return t;
Jason Sams334ea0c2009-08-17 13:56:09 -07001079}
1080
Jason Sams4d339932010-05-11 14:03:58 -07001081static uchar4 SC_convertColorTo8888_f4(float r, float g, float b, float a) {
1082 uchar4 t;
1083 t.f[0] = (uint8_t)(r * 255.f);
1084 t.f[1] = (uint8_t)(g * 255.f);
1085 t.f[2] = (uint8_t)(b * 255.f);
1086 t.f[3] = (uint8_t)(a * 255.f);
1087 return t;
Jason Sams334ea0c2009-08-17 13:56:09 -07001088}
1089
Jason Sams516c3192009-10-06 13:58:47 -07001090static uint32_t SC_toClient(void *data, int cmdID, int len, int waitForSpace)
1091{
1092 GET_TLS();
Jason Sams4d339932010-05-11 14:03:58 -07001093 //LOGE("SC_toClient %i %i %i", cmdID, len, waitForSpace);
Jason Sams516c3192009-10-06 13:58:47 -07001094 return rsc->sendMessageToClient(data, cmdID, len, waitForSpace != 0);
1095}
1096
Jason Samsbd2197f2009-10-07 18:14:01 -07001097static void SC_scriptCall(int scriptID)
1098{
1099 GET_TLS();
1100 rsc->runScript((Script *)scriptID, 0);
1101}
1102
Jason Sams4d339932010-05-11 14:03:58 -07001103static void SC_debugP(int i, void *p)
1104{
1105 LOGE("debug P %i %p, %i", i, p, (int)p);
1106}
1107
1108static void SC_debugPi(int i, int p)
1109{
1110 LOGE("debug Pi %i 0x%08x, %i", i, p, (int)p);
1111}
1112
1113static void SC_debugPf(int i, float p)
1114{
1115 LOGE("debug Pf %i %f, 0x%08x", i, p, reinterpret_cast<uint32_t *>(&p)[0]);
1116}
1117
1118int SC_divsi3(int a, int b)
1119{
1120 return a / b;
1121}
Jason Samsbd2197f2009-10-07 18:14:01 -07001122
Jason Samsc97bb882009-07-20 14:31:06 -07001123//////////////////////////////////////////////////////////////////////////////
1124// Class implementation
1125//////////////////////////////////////////////////////////////////////////////
1126
Jason Sams4d339932010-05-11 14:03:58 -07001127// llvm name mangling ref
1128// <builtin-type> ::= v # void
1129// ::= b # bool
1130// ::= c # char
1131// ::= a # signed char
1132// ::= h # unsigned char
1133// ::= s # short
1134// ::= t # unsigned short
1135// ::= i # int
1136// ::= j # unsigned int
1137// ::= l # long
1138// ::= m # unsigned long
1139// ::= x # long long, __int64
1140// ::= y # unsigned long long, __int64
1141// ::= f # float
1142// ::= d # double
Jason Samsc97bb882009-07-20 14:31:06 -07001143
Jason Sams4d339932010-05-11 14:03:58 -07001144ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
1145 { "__divsi3", (void *)&SC_divsi3 },
1146
1147 // IO
1148 { "loadSimpleMeshVerticesF", (void *)&SC_loadSimpleMeshVerticesF },
1149 { "updateSimpleMesh", (void *)&SC_updateSimpleMesh },
1150
1151 // OpenCL math
1152 { "_Z4acosf", (void *)&acosf },
1153 { "_Z5acoshf", (void *)&acoshf },
1154 { "_Z6acospif", (void *)&SC_acospi },
1155 { "_Z4asinf", (void *)&asinf },
1156 { "_Z5asinhf", (void *)&asinhf },
1157 { "_Z6asinpif", (void *)&SC_asinpi },
1158 { "_Z4atanf", (void *)&atanf },
1159 { "_Z5atan2f", (void *)&atan2f },
1160 { "_Z6atanpif", (void *)&SC_atanpi },
1161 { "_Z7atan2pif", (void *)&SC_atan2pi },
1162 { "_Z4cbrtf", (void *)&cbrtf },
1163 { "_Z4ceilf", (void *)&ceilf },
1164 { "_Z8copysignff", (void *)&copysignf },
1165 { "_Z3cosf", (void *)&cosf },
1166 { "_Z4coshf", (void *)&coshf },
1167 { "_Z5cospif", (void *)&SC_cospi },
1168 { "_Z4erfcf", (void *)&erfcf },
1169 { "_Z3erff", (void *)&erff },
1170 { "_Z3expf", (void *)&expf },
1171 { "_Z4exp2f", (void *)&exp2f },
1172 { "_Z5exp10f", (void *)&SC_exp10 },
1173 { "_Z5expm1f", (void *)&expm1f },
1174 { "_Z4fabsf", (void *)&fabsf },
1175 { "_Z4fdimff", (void *)&fdimf },
1176 { "_Z5floorf", (void *)&floorf },
1177 { "_Z3fmafff", (void *)&fmaf },
1178 { "_Z4fmaxff", (void *)&fmaxf },
1179 { "_Z4fminff", (void *)&fminf }, // float fmin(float, float)
1180 { "_Z4fmodff", (void *)&fmodf },
1181 { "_Z5fractfPf", (void *)&SC_fract },
1182 { "_Z5frexpfPi", (void *)&frexpf },
1183 { "_Z5hypotff", (void *)&hypotf },
1184 { "_Z5ilogbf", (void *)&ilogbf },
1185 { "_Z5ldexpfi", (void *)&ldexpf },
1186 { "_Z6lgammaf", (void *)&lgammaf },
1187 { "_Z3logf", (void *)&logf },
1188 { "_Z4log2f", (void *)&SC_log2 },
1189 { "_Z5log10f", (void *)&log10f },
1190 { "_Z5log1pf", (void *)&log1pf },
1191 //{ "logb", (void *)& },
1192 //{ "mad", (void *)& },
1193 { "modf", (void *)&modff },
1194 //{ "nan", (void *)& },
1195 { "_Z9nextafterff", (void *)&nextafterf },
1196 { "_Z3powff", (void *)&powf },
1197 { "_Z4pownfi", (void *)&SC_pown },
1198 { "_Z4powrff", (void *)&SC_powr },
1199 { "_Z9remainderff", (void *)&remainderf },
1200 { "remquo", (void *)&remquof },
1201 { "_Z4rintf", (void *)&rintf },
1202 { "_Z5rootnfi", (void *)&SC_rootn },
1203 { "_Z5roundf", (void *)&roundf },
1204 { "_Z5rsqrtf", (void *)&SC_rsqrt },
1205 { "_Z3sinf", (void *)&sinf },
1206 { "sincos", (void *)&SC_sincos },
1207 { "_Z4sinhf", (void *)&sinhf },
1208 { "_Z5sinpif", (void *)&SC_sinpi },
1209 { "_Z4sqrtf", (void *)&sqrtf },
1210 { "_Z3tanf", (void *)&tanf },
1211 { "_Z4tanhf", (void *)&tanhf },
1212 { "_Z5tanpif", (void *)&SC_tanpi },
1213 //{ "tgamma", (void *)& },
1214 { "_Z5truncf", (void *)&truncf },
1215
1216 // OpenCL Int
1217 { "_Z3absi", (void *)&SC_abs_i32 },
1218 { "_Z3abss", (void *)&SC_abs_i16 },
1219 { "_Z3absc", (void *)&SC_abs_i8 },
1220 { "_Z3clzj", (void *)&SC_clz_u32 },
1221 { "_Z3clzt", (void *)&SC_clz_u16 },
1222 { "_Z3clzh", (void *)&SC_clz_u8 },
1223 { "_Z3clzi", (void *)&SC_clz_i32 },
1224 { "_Z3clzs", (void *)&SC_clz_i16 },
1225 { "_Z3clzc", (void *)&SC_clz_i8 },
1226 { "_Z3maxjj", (void *)&SC_max_u32 },
1227 { "_Z3maxtt", (void *)&SC_max_u16 },
1228 { "_Z3maxhh", (void *)&SC_max_u8 },
1229 { "_Z3maxii", (void *)&SC_max_i32 },
1230 { "_Z3maxss", (void *)&SC_max_i16 },
1231 { "_Z3maxcc", (void *)&SC_max_i8 },
1232 { "_Z3minjj", (void *)&SC_min_u32 },
1233 { "_Z3mintt", (void *)&SC_min_u16 },
1234 { "_Z3minhh", (void *)&SC_min_u8 },
1235 { "_Z3minii", (void *)&SC_min_i32 },
1236 { "_Z3minss", (void *)&SC_min_i16 },
1237 { "_Z3mincc", (void *)&SC_min_i8 },
1238
1239 // OpenCL 6.11.4
1240 { "_Z5clampfff", (void *)&SC_clamp_f32 },
1241 { "_Z7degreesf", (void *)&SC_degrees },
1242 { "_Z3maxff", (void *)&SC_max_f32 },
1243 { "_Z3minff", (void *)&SC_min_f32 },
1244 { "_Z3mixfff", (void *)&SC_mix_f32 },
1245 { "_Z7radiansf", (void *)&SC_radians },
1246 { "_Z4stepff", (void *)&SC_step_f32 },
1247 //{ "smoothstep", (void *)& },
1248 { "_Z4signf", (void *)&SC_sign_f32 },
1249
1250
1251
1252
1253 { "modf", (void *)&fmod },
1254 { "_Z4fracf", (void *)&SC_frac },
1255 //{ "sinf_fast", (void *)&SC_sinf_fast },
1256 //{ "cosf_fast", (void *)&SC_cosf_fast },
1257 { "randf", (void *)&SC_randf },
1258 { "randf2", (void *)&SC_randf2 },
1259 { "sign", (void *)&SC_sign },
1260 { "clamp", (void *)&SC_clamp },
1261 { "distf2", (void *)&SC_distf2 },
1262 { "distf3", (void *)&SC_distf3 },
1263 { "magf2", (void *)&SC_magf2 },
1264 { "magf3", (void *)&SC_magf3 },
1265 { "normf", (void *)&SC_normf },
1266 { "mapf", (void *)&SC_mapf },
1267 { "noisef", (void *)&SC_noisef },
1268 { "noisef2", (void *)&SC_noisef2 },
1269 { "noisef3", (void *)&SC_noisef3 },
1270 { "turbulencef2", (void *)&SC_turbulencef2 },
1271 { "turbulencef3", (void *)&SC_turbulencef3 },
Jason Samsc97bb882009-07-20 14:31:06 -07001272
Romain Guy584a3752009-07-30 18:45:01 -07001273 // time
Jason Sams4d339932010-05-11 14:03:58 -07001274 { "second", (void *)&SC_second },
1275 { "minute", (void *)&SC_minute },
1276 { "hour", (void *)&SC_hour },
1277 { "day", (void *)&SC_day },
1278 { "month", (void *)&SC_month },
1279 { "year", (void *)&SC_year },
1280 { "uptimeMillis", (void*)&SC_uptimeMillis }, // TODO: use long instead
1281 { "startTimeMillis", (void*)&SC_startTimeMillis }, // TODO: use long instead
1282 { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis }, // TODO: use long instead
Romain Guy584a3752009-07-30 18:45:01 -07001283
Jason Samsc97bb882009-07-20 14:31:06 -07001284 // matrix
Jason Sams4d339932010-05-11 14:03:58 -07001285 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity },
1286 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat },
1287 { "matrixLoadMat", (void *)&SC_matrixLoadMat },
1288 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate },
1289 { "matrixLoadScale", (void *)&SC_matrixLoadScale },
1290 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate },
1291 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply },
1292 { "matrixMultiply", (void *)&SC_matrixMultiply },
1293 { "matrixRotate", (void *)&SC_matrixRotate },
1294 { "matrixScale", (void *)&SC_matrixScale },
1295 { "matrixTranslate", (void *)&SC_matrixTranslate },
Jason Samsc97bb882009-07-20 14:31:06 -07001296
Jason Sams334ea0c2009-08-17 13:56:09 -07001297 // vector
Jason Sams4d339932010-05-11 14:03:58 -07001298 { "vec2Rand", (void *)&SC_vec2Rand },
Jason Sams334ea0c2009-08-17 13:56:09 -07001299
Jason Samsea84a7c2009-09-04 14:42:41 -07001300 // vec3
Jason Sams4d339932010-05-11 14:03:58 -07001301 { "vec3Norm", (void *)&SC_vec3Norm },
1302 { "vec3Length", (void *)&SC_vec3Length },
1303 { "vec3Add", (void *)&SC_vec3Add },
1304 { "vec3Sub", (void *)&SC_vec3Sub },
1305 { "vec3Cross", (void *)&SC_vec3Cross },
1306 { "vec3Dot", (void *)&SC_vec3Dot },
1307 { "vec3Scale", (void *)&SC_vec3Scale },
Jason Samsea84a7c2009-09-04 14:42:41 -07001308
Romain Guyd7fa1222009-10-09 16:05:25 -07001309 // vec4
Jason Sams4d339932010-05-11 14:03:58 -07001310 { "vec4Norm", (void *)&SC_vec4Norm },
1311 { "vec4Length", (void *)&SC_vec4Length },
1312 { "vec4Add", (void *)&SC_vec4Add },
1313 { "vec4Sub", (void *)&SC_vec4Sub },
1314 { "vec4Dot", (void *)&SC_vec4Dot },
1315 { "vec4Scale", (void *)&SC_vec4Scale },
Romain Guyd7fa1222009-10-09 16:05:25 -07001316
Jason Samsc97bb882009-07-20 14:31:06 -07001317 // context
Jason Sams4d339932010-05-11 14:03:58 -07001318 { "bindProgramFragment", (void *)&SC_bindProgramFragment },
1319 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore },
1320 { "bindProgramStore", (void *)&SC_bindProgramFragmentStore },
1321 { "bindProgramVertex", (void *)&SC_bindProgramVertex },
Jason Sams0c677312010-05-12 18:26:52 -07001322 { "bindProgramRaster", (void *)&SC_bindProgramRaster },
Jason Sams4d339932010-05-11 14:03:58 -07001323 { "bindSampler", (void *)&SC_bindSampler },
1324 { "bindTexture", (void *)&SC_bindTexture },
Jason Samsc97bb882009-07-20 14:31:06 -07001325
Jason Samsb0ec1b42009-07-28 12:02:16 -07001326 // vp
Jason Sams4d339932010-05-11 14:03:58 -07001327 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix },
1328 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001329
Jason Sams4d339932010-05-11 14:03:58 -07001330 // allocation
1331 { "allocGetDimX", (void *)&SC_allocGetDimX },
1332 { "allocGetDimY", (void *)&SC_allocGetDimY },
1333 { "allocGetDimZ", (void *)&SC_allocGetDimZ },
1334 { "allocGetDimLOD", (void *)&SC_allocGetDimLOD },
1335 { "allocGetDimFaces", (void *)&SC_allocGetDimFaces },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001336
Jason Samsc97bb882009-07-20 14:31:06 -07001337 // drawing
Jason Sams4d339932010-05-11 14:03:58 -07001338 { "drawRect", (void *)&SC_drawRect },
1339 { "drawQuad", (void *)&SC_drawQuad },
1340 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords },
1341 { "drawSprite", (void *)&SC_drawSprite },
1342 { "drawSpriteScreenspace", (void *)&SC_drawSpriteScreenspace },
1343 { "drawSpriteScreenspaceCropped", (void *)&SC_drawSpriteScreenspaceCropped },
1344 { "drawLine", (void *)&SC_drawLine },
1345 { "drawPoint", (void *)&SC_drawPoint },
1346 { "drawSimpleMesh", (void *)&SC_drawSimpleMesh },
1347 { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange },
Jason Samsc97bb882009-07-20 14:31:06 -07001348
1349
1350 // misc
Jason Sams4d339932010-05-11 14:03:58 -07001351 { "pfClearColor", (void *)&SC_ClearColor },
1352 { "color", (void *)&SC_color },
1353 { "hsb", (void *)&SC_hsb },
1354 { "hsbToRgb", (void *)&SC_hsbToRgb },
1355 { "hsbToAbgr", (void *)&SC_hsbToAbgr },
1356 { "pointAttenuation", (void *)&SC_pointAttenuation },
Jason Samsc97bb882009-07-20 14:31:06 -07001357
Jason Sams4d339932010-05-11 14:03:58 -07001358 { "uploadToTexture", (void *)&SC_uploadToTexture },
1359 { "uploadToBufferObject", (void *)&SC_uploadToBufferObject },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001360
Jason Sams4d339932010-05-11 14:03:58 -07001361 { "syncToGL", (void *)&SC_syncToGL },
Jason Samsea87e962010-01-12 12:12:28 -08001362
Jason Sams4d339932010-05-11 14:03:58 -07001363 { "getWidth", (void *)&SC_getWidth },
1364 { "getHeight", (void *)&SC_getHeight },
Jason Sams334ea0c2009-08-17 13:56:09 -07001365
Jason Sams4d339932010-05-11 14:03:58 -07001366 { "sendToClient", (void *)&SC_toClient },
Jason Sams334ea0c2009-08-17 13:56:09 -07001367
Jason Sams4d339932010-05-11 14:03:58 -07001368 { "_Z18convertColorTo8888fff", (void *)&SC_convertColorTo8888_f3 },
1369 { "_Z18convertColorTo8888ffff", (void *)&SC_convertColorTo8888_f4 },
Jason Sams40a29e82009-08-10 14:55:26 -07001370
Jason Sams4d339932010-05-11 14:03:58 -07001371 { "debugF", (void *)&SC_debugF },
1372 { "debugI32", (void *)&SC_debugI32 },
1373 { "debugHexF", (void *)&SC_debugHexF },
1374 { "debugHexI32", (void *)&SC_debugHexI32 },
1375 { "debugP", (void *)&SC_debugP },
1376 { "debugPf", (void *)&SC_debugPf },
1377 { "debugPi", (void *)&SC_debugPi },
Jason Sams40a29e82009-08-10 14:55:26 -07001378
Jason Sams4d339932010-05-11 14:03:58 -07001379 { "scriptCall", (void *)&SC_scriptCall },
Jason Samsb0ec1b42009-07-28 12:02:16 -07001380
Jason Sams4d339932010-05-11 14:03:58 -07001381 { NULL, NULL }
Jason Samsc97bb882009-07-20 14:31:06 -07001382};
1383
1384const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
1385{
1386 ScriptCState::SymbolTable_t *syms = gSyms;
1387
1388 while (syms->mPtr) {
1389 if (!strcmp(syms->mName, sym)) {
1390 return syms;
1391 }
1392 syms++;
1393 }
1394 return NULL;
1395}
1396