blob: 0e094748c4a17f0fc060512fabe9b81fec79477e [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 void SC_updateSimpleMesh(RsSimpleMesh mesh)
Romain Guy48b7edc2009-08-06 22:52:13 -0700219{
Jason Samsc460e552009-11-25 13:22:07 -0800220 GET_TLS();
Jason Samsa2b54c42009-09-23 16:38:37 -0700221 SimpleMesh *sm = static_cast<SimpleMesh *>(mesh);
Jason Samsc460e552009-11-25 13:22:07 -0800222 sm->uploadAll(rsc);
Romain Guy48b7edc2009-08-06 22:52:13 -0700223}
Romain Guy06f7c932009-08-06 12:40:41 -0700224
Jason Samsa57c0a72009-09-04 14:42:41 -0700225//////////////////////////////////////////////////////////////////////////////
226// Vec3 routines
227//////////////////////////////////////////////////////////////////////////////
228
229static void SC_vec3Norm(vec3_t *v)
230{
231 float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
232 len = 1 / len;
233 v->x *= len;
234 v->y *= len;
235 v->z *= len;
236}
237
238static float SC_vec3Length(const vec3_t *v)
239{
240 return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
241}
242
243static void SC_vec3Add(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
244{
245 dest->x = lhs->x + rhs->x;
246 dest->y = lhs->y + rhs->y;
247 dest->z = lhs->z + rhs->z;
248}
249
250static void SC_vec3Sub(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_vec3Cross(vec3_t *dest, const vec3_t *lhs, const vec3_t *rhs)
258{
259 float x = lhs->y * rhs->z - lhs->z * rhs->y;
260 float y = lhs->z * rhs->x - lhs->x * rhs->z;
261 float z = lhs->x * rhs->y - lhs->y * rhs->x;
262 dest->x = x;
263 dest->y = y;
264 dest->z = z;
265}
266
267static float SC_vec3Dot(const vec3_t *lhs, const vec3_t *rhs)
268{
269 return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z;
270}
271
272static void SC_vec3Scale(vec3_t *lhs, float scale)
273{
274 lhs->x *= scale;
275 lhs->y *= scale;
276 lhs->z *= scale;
277}
278
Romain Guyd6d4a5f2009-10-09 16:05:25 -0700279//////////////////////////////////////////////////////////////////////////////
280// Vec4 routines
281//////////////////////////////////////////////////////////////////////////////
282
283static void SC_vec4Norm(vec4_t *v)
284{
285 float len = sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w);
286 len = 1 / len;
287 v->x *= len;
288 v->y *= len;
289 v->z *= len;
290 v->w *= len;
291}
292
293static float SC_vec4Length(const vec4_t *v)
294{
295 return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z + v->w * v->w);
296}
297
298static void SC_vec4Add(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs)
299{
300 dest->x = lhs->x + rhs->x;
301 dest->y = lhs->y + rhs->y;
302 dest->z = lhs->z + rhs->z;
303 dest->w = lhs->w + rhs->w;
304}
305
306static void SC_vec4Sub(vec4_t *dest, const vec4_t *lhs, const vec4_t *rhs)
307{
308 dest->x = lhs->x - rhs->x;
309 dest->y = lhs->y - rhs->y;
310 dest->z = lhs->z - rhs->z;
311 dest->w = lhs->w - rhs->w;
312}
313
314static float SC_vec4Dot(const vec4_t *lhs, const vec4_t *rhs)
315{
316 return lhs->x * rhs->x + lhs->y * rhs->y + lhs->z * rhs->z + lhs->w * rhs->w;
317}
318
319static void SC_vec4Scale(vec4_t *lhs, float scale)
320{
321 lhs->x *= scale;
322 lhs->y *= scale;
323 lhs->z *= scale;
324 lhs->w *= scale;
325}
Jason Samse45ac6e2009-07-20 14:31:06 -0700326
327//////////////////////////////////////////////////////////////////////////////
328// Math routines
329//////////////////////////////////////////////////////////////////////////////
330
Romain Guy2275d632009-08-18 11:39:17 -0700331static float SC_sinf_fast(float x)
332{
333 const float A = 1.0f / (2.0f * M_PI);
334 const float B = -16.0f;
335 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -0700336
Romain Guy2275d632009-08-18 11:39:17 -0700337 // scale angle for easy argument reduction
338 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -0700339
Romain Guy2275d632009-08-18 11:39:17 -0700340 if (fabsf(x) >= 0.5f) {
341 // argument reduction
342 x = x - ceilf(x + 0.5f) + 1.0f;
343 }
Jason Samsa57c0a72009-09-04 14:42:41 -0700344
Romain Guy2275d632009-08-18 11:39:17 -0700345 const float y = B * x * fabsf(x) + C * x;
346 return 0.2215f * (y * fabsf(y) - y) + y;
347}
348
349static float SC_cosf_fast(float x)
350{
351 x += float(M_PI / 2);
352
353 const float A = 1.0f / (2.0f * M_PI);
354 const float B = -16.0f;
355 const float C = 8.0f;
Jason Samsa57c0a72009-09-04 14:42:41 -0700356
Romain Guy2275d632009-08-18 11:39:17 -0700357 // scale angle for easy argument reduction
358 x *= A;
Jason Samsa57c0a72009-09-04 14:42:41 -0700359
Romain Guy2275d632009-08-18 11:39:17 -0700360 if (fabsf(x) >= 0.5f) {
361 // argument reduction
362 x = x - ceilf(x + 0.5f) + 1.0f;
363 }
Jason Samsa57c0a72009-09-04 14:42:41 -0700364
Romain Guy2275d632009-08-18 11:39:17 -0700365 const float y = B * x * fabsf(x) + C * x;
366 return 0.2215f * (y * fabsf(y) - y) + y;
367}
368
Jason Samse45ac6e2009-07-20 14:31:06 -0700369static float SC_randf(float max)
370{
Jason Samsbe36bf32010-05-11 14:03:58 -0700371 //LOGE("max %f", max);
Jason Samse45ac6e2009-07-20 14:31:06 -0700372 float r = (float)rand();
373 return r / RAND_MAX * max;
374}
375
Romain Guy39dbc802009-07-31 11:20:59 -0700376static float SC_randf2(float min, float max)
377{
378 float r = (float)rand();
379 return r / RAND_MAX * (max - min) + min;
380}
381
Romain Guyd6d4a5f2009-10-09 16:05:25 -0700382static int SC_sign(int value)
383{
384 return (value > 0) - (value < 0);
385}
386
Romain Guy27162ab2009-08-09 17:04:54 -0700387static int SC_clamp(int amount, int low, int high)
388{
389 return amount < low ? low : (amount > high ? high : amount);
390}
391
Jason Samsdac98f52009-09-18 14:24:24 -0700392static float SC_roundf(float v)
393{
394 return floorf(v + 0.4999999999);
395}
396
Romain Guy39dbc802009-07-31 11:20:59 -0700397static float SC_distf2(float x1, float y1, float x2, float y2)
398{
399 float x = x2 - x1;
400 float y = y2 - y1;
Jason Samse5ffb872009-08-09 17:01:55 -0700401 return sqrtf(x * x + y * y);
Romain Guy39dbc802009-07-31 11:20:59 -0700402}
403
404static float SC_distf3(float x1, float y1, float z1, float x2, float y2, float z2)
405{
406 float x = x2 - x1;
407 float y = y2 - y1;
408 float z = z2 - z1;
Jason Samse5ffb872009-08-09 17:01:55 -0700409 return sqrtf(x * x + y * y + z * z);
Romain Guy39dbc802009-07-31 11:20:59 -0700410}
411
412static float SC_magf2(float a, float b)
413{
414 return sqrtf(a * a + b * b);
415}
416
417static float SC_magf3(float a, float b, float c)
418{
419 return sqrtf(a * a + b * b + c * c);
420}
421
Romain Guy39dbc802009-07-31 11:20:59 -0700422static float SC_normf(float start, float stop, float value)
423{
424 return (value - start) / (stop - start);
425}
426
427static float SC_mapf(float minStart, float minStop, float maxStart, float maxStop, float value)
428{
429 return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart));
430}
Jason Samse45ac6e2009-07-20 14:31:06 -0700431
Jason Samsbe36bf32010-05-11 14:03:58 -0700432static float SC_frac(float v)
433{
434 int i = (int)floor(v);
435 return fmin(v - i, 0x1.fffffep-1f);
436}
437
Romain Guy98e10fd2009-07-30 18:45:01 -0700438//////////////////////////////////////////////////////////////////////////////
439// Time routines
440//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700441
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700442static int32_t SC_second()
Romain Guy98e10fd2009-07-30 18:45:01 -0700443{
444 GET_TLS();
445
446 time_t rawtime;
447 time(&rawtime);
448
Romain Guy519cdc92009-11-11 15:36:06 -0800449 struct tm *timeinfo;
450 timeinfo = localtime(&rawtime);
451 return timeinfo->tm_sec;
Romain Guy98e10fd2009-07-30 18:45:01 -0700452}
453
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700454static int32_t SC_minute()
Romain Guy98e10fd2009-07-30 18:45:01 -0700455{
456 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700457
Romain Guy98e10fd2009-07-30 18:45:01 -0700458 time_t rawtime;
459 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700460
Romain Guy519cdc92009-11-11 15:36:06 -0800461 struct tm *timeinfo;
462 timeinfo = localtime(&rawtime);
463 return timeinfo->tm_min;
Jason Samse5ffb872009-08-09 17:01:55 -0700464}
Romain Guy98e10fd2009-07-30 18:45:01 -0700465
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700466static int32_t SC_hour()
Romain Guy98e10fd2009-07-30 18:45:01 -0700467{
468 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700469
Romain Guy98e10fd2009-07-30 18:45:01 -0700470 time_t rawtime;
471 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700472
Romain Guy519cdc92009-11-11 15:36:06 -0800473 struct tm *timeinfo;
474 timeinfo = localtime(&rawtime);
475 return timeinfo->tm_hour;
Romain Guy39dbc802009-07-31 11:20:59 -0700476}
477
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700478static int32_t SC_day()
Romain Guy39dbc802009-07-31 11:20:59 -0700479{
480 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700481
Romain Guy39dbc802009-07-31 11:20:59 -0700482 time_t rawtime;
483 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700484
Romain Guy519cdc92009-11-11 15:36:06 -0800485 struct tm *timeinfo;
486 timeinfo = localtime(&rawtime);
487 return timeinfo->tm_mday;
Jason Samse5ffb872009-08-09 17:01:55 -0700488}
Jason Samse45ac6e2009-07-20 14:31:06 -0700489
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700490static int32_t SC_month()
Romain Guy39dbc802009-07-31 11:20:59 -0700491{
492 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700493
Romain Guy39dbc802009-07-31 11:20:59 -0700494 time_t rawtime;
495 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700496
Romain Guy519cdc92009-11-11 15:36:06 -0800497 struct tm *timeinfo;
498 timeinfo = localtime(&rawtime);
499 return timeinfo->tm_mon;
Jason Samse5ffb872009-08-09 17:01:55 -0700500}
Romain Guy39dbc802009-07-31 11:20:59 -0700501
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700502static int32_t SC_year()
Romain Guy39dbc802009-07-31 11:20:59 -0700503{
504 GET_TLS();
Jason Samse5ffb872009-08-09 17:01:55 -0700505
Romain Guy39dbc802009-07-31 11:20:59 -0700506 time_t rawtime;
507 time(&rawtime);
Jason Samse5ffb872009-08-09 17:01:55 -0700508
Romain Guy519cdc92009-11-11 15:36:06 -0800509 struct tm *timeinfo;
510 timeinfo = localtime(&rawtime);
511 return timeinfo->tm_year;
Romain Guy39dbc802009-07-31 11:20:59 -0700512}
513
Joe Onorato9c4e4ca2009-08-09 11:39:02 -0700514static int32_t SC_uptimeMillis()
515{
516 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
517}
518
519static int32_t SC_startTimeMillis()
520{
521 GET_TLS();
522 return sc->mEnviroment.mStartTimeMillis;
523}
524
525static int32_t SC_elapsedTimeMillis()
526{
527 GET_TLS();
528 return nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC))
529 - sc->mEnviroment.mStartTimeMillis;
530}
531
Jason Samse45ac6e2009-07-20 14:31:06 -0700532//////////////////////////////////////////////////////////////////////////////
533// Matrix routines
534//////////////////////////////////////////////////////////////////////////////
535
536
537static void SC_matrixLoadIdentity(rsc_Matrix *mat)
538{
539 Matrix *m = reinterpret_cast<Matrix *>(mat);
540 m->loadIdentity();
541}
542
543static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
544{
545 Matrix *m = reinterpret_cast<Matrix *>(mat);
546 m->load(f);
547}
548
549static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
550{
551 Matrix *m = reinterpret_cast<Matrix *>(mat);
552 m->load(reinterpret_cast<const Matrix *>(newmat));
553}
554
555static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
556{
557 Matrix *m = reinterpret_cast<Matrix *>(mat);
558 m->loadRotate(rot, x, y, z);
559}
560
561static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
562{
563 Matrix *m = reinterpret_cast<Matrix *>(mat);
564 m->loadScale(x, y, z);
565}
566
567static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
568{
569 Matrix *m = reinterpret_cast<Matrix *>(mat);
570 m->loadTranslate(x, y, z);
571}
572
573static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
574{
575 Matrix *m = reinterpret_cast<Matrix *>(mat);
576 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
577 reinterpret_cast<const Matrix *>(rhs));
578}
579
580static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
581{
582 Matrix *m = reinterpret_cast<Matrix *>(mat);
583 m->multiply(reinterpret_cast<const Matrix *>(rhs));
584}
585
586static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
587{
588 Matrix *m = reinterpret_cast<Matrix *>(mat);
589 m->rotate(rot, x, y, z);
590}
591
592static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
593{
594 Matrix *m = reinterpret_cast<Matrix *>(mat);
595 m->scale(x, y, z);
596}
597
598static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
599{
600 Matrix *m = reinterpret_cast<Matrix *>(mat);
601 m->translate(x, y, z);
602}
603
604
Jason Samsbe36bf32010-05-11 14:03:58 -0700605static rsvF_2 SC_vec2Rand(float maxLen)
Jason Sams90b36a82009-08-17 13:56:09 -0700606{
Jason Samsbe36bf32010-05-11 14:03:58 -0700607 float2 t;
608 float angle = SC_randf(M_PI * 2);
Jason Sams90b36a82009-08-17 13:56:09 -0700609 float len = SC_randf(maxLen);
Jason Samsbe36bf32010-05-11 14:03:58 -0700610 t.f[0] = len * sinf(angle);
611 t.f[1] = len * cosf(angle);
612 return t.v;
Jason Sams90b36a82009-08-17 13:56:09 -0700613}
614
Jason Samse45ac6e2009-07-20 14:31:06 -0700615
616
617//////////////////////////////////////////////////////////////////////////////
618// Context
619//////////////////////////////////////////////////////////////////////////////
620
621static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
622{
623 GET_TLS();
Jason Sams7dad9c32009-12-17 16:55:08 -0800624 rsi_ProgramBindTexture(rsc,
625 static_cast<ProgramFragment *>(vpf),
626 slot,
627 static_cast<Allocation *>(va));
Jason Samse45ac6e2009-07-20 14:31:06 -0700628
629}
630
631static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
632{
633 GET_TLS();
Jason Sams7dad9c32009-12-17 16:55:08 -0800634 rsi_ProgramBindSampler(rsc,
635 static_cast<ProgramFragment *>(vpf),
636 slot,
637 static_cast<Sampler *>(vs));
Jason Samse45ac6e2009-07-20 14:31:06 -0700638
639}
640
Jason Samsccc010b2010-05-13 18:30:11 -0700641static void SC_bindProgramStore(RsProgramStore pfs)
Jason Samse45ac6e2009-07-20 14:31:06 -0700642{
643 GET_TLS();
Jason Samsccc010b2010-05-13 18:30:11 -0700644 rsi_ContextBindProgramStore(rsc, pfs);
Jason Samse45ac6e2009-07-20 14:31:06 -0700645}
646
647static void SC_bindProgramFragment(RsProgramFragment pf)
648{
649 GET_TLS();
650 rsi_ContextBindProgramFragment(rsc, pf);
Jason Samse45ac6e2009-07-20 14:31:06 -0700651}
652
Jason Samsb5909ce2009-07-21 12:20:54 -0700653static void SC_bindProgramVertex(RsProgramVertex pv)
654{
655 GET_TLS();
656 rsi_ContextBindProgramVertex(rsc, pv);
Jason Sams1b6b7fa2010-05-12 18:26:52 -0700657}
Jason Samsb5909ce2009-07-21 12:20:54 -0700658
Jason Sams1b6b7fa2010-05-12 18:26:52 -0700659static void SC_bindProgramRaster(RsProgramRaster pv)
660{
661 GET_TLS();
662 rsi_ContextBindProgramRaster(rsc, pv);
Jason Samsb5909ce2009-07-21 12:20:54 -0700663}
Jason Samse45ac6e2009-07-20 14:31:06 -0700664
665//////////////////////////////////////////////////////////////////////////////
Jason Samsc9d43db2009-07-28 12:02:16 -0700666// VP
667//////////////////////////////////////////////////////////////////////////////
668
669static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
670{
671 GET_TLS();
672 rsc->getVertex()->setModelviewMatrix(m);
673}
674
675static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
676{
677 GET_TLS();
678 rsc->getVertex()->setTextureMatrix(m);
679}
680
681
682
683//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700684// Drawing
685//////////////////////////////////////////////////////////////////////////////
686
Romain Guyd369e272009-08-07 15:40:32 -0700687static void SC_drawLine(float x1, float y1, float z1,
688 float x2, float y2, float z2)
689{
690 GET_TLS();
Jason Samsa2cf7552010-03-03 13:03:18 -0800691 if (!rsc->setupCheck()) {
692 return;
693 }
Romain Guyd369e272009-08-07 15:40:32 -0700694
695 float vtx[] = { x1, y1, z1, x2, y2, z2 };
Jason Samsc460e552009-11-25 13:22:07 -0800696 VertexArray va;
Jason Samsbe504f22010-01-25 12:31:24 -0800697 va.addLegacy(GL_FLOAT, 3, 12, RS_KIND_POSITION, false, (uint32_t)vtx);
Jason Samsc460e552009-11-25 13:22:07 -0800698 if (rsc->checkVersion2_0()) {
Jason Samsd01d9702009-12-23 14:35:29 -0800699 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsc460e552009-11-25 13:22:07 -0800700 } else {
Jason Samsd01d9702009-12-23 14:35:29 -0800701 va.setupGL(rsc, &rsc->mStateVertexArray);
Jason Samsc460e552009-11-25 13:22:07 -0800702 }
Romain Guyd369e272009-08-07 15:40:32 -0700703
704 glDrawArrays(GL_LINES, 0, 2);
705}
706
Jason Samsb681c8a2009-09-28 18:12:56 -0700707static void SC_drawPoint(float x, float y, float z)
708{
709 GET_TLS();
Jason Samsa2cf7552010-03-03 13:03:18 -0800710 if (!rsc->setupCheck()) {
711 return;
712 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700713
714 float vtx[] = { x, y, z };
715
Jason Samsc460e552009-11-25 13:22:07 -0800716 VertexArray va;
Jason Samsbe504f22010-01-25 12:31:24 -0800717 va.addLegacy(GL_FLOAT, 3, 12, RS_KIND_POSITION, false, (uint32_t)vtx);
Jason Samsc460e552009-11-25 13:22:07 -0800718 if (rsc->checkVersion2_0()) {
Jason Samsd01d9702009-12-23 14:35:29 -0800719 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsc460e552009-11-25 13:22:07 -0800720 } else {
Jason Samsd01d9702009-12-23 14:35:29 -0800721 va.setupGL(rsc, &rsc->mStateVertexArray);
Jason Samsc460e552009-11-25 13:22:07 -0800722 }
Jason Samsb681c8a2009-09-28 18:12:56 -0700723
724 glDrawArrays(GL_POINTS, 0, 1);
725}
726
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700727static void SC_drawQuadTexCoords(float x1, float y1, float z1,
728 float u1, float v1,
729 float x2, float y2, float z2,
730 float u2, float v2,
731 float x3, float y3, float z3,
732 float u3, float v3,
733 float x4, float y4, float z4,
734 float u4, float v4)
Jason Samse45ac6e2009-07-20 14:31:06 -0700735{
736 GET_TLS();
Jason Samsa2cf7552010-03-03 13:03:18 -0800737 if (!rsc->setupCheck()) {
738 return;
739 }
Jason Samse579df42009-08-10 14:55:26 -0700740
Jason Samse45ac6e2009-07-20 14:31:06 -0700741 //LOGE("Quad");
742 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
743 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
744 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
745 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
Jason Samse579df42009-08-10 14:55:26 -0700746
Jason Samse45ac6e2009-07-20 14:31:06 -0700747 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700748 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
Jason Samse45ac6e2009-07-20 14:31:06 -0700749
Jason Samsc460e552009-11-25 13:22:07 -0800750 VertexArray va;
Jason Samsbe504f22010-01-25 12:31:24 -0800751 va.addLegacy(GL_FLOAT, 3, 12, RS_KIND_POSITION, false, (uint32_t)vtx);
752 va.addLegacy(GL_FLOAT, 2, 8, RS_KIND_TEXTURE, false, (uint32_t)tex);
Jason Samsc460e552009-11-25 13:22:07 -0800753 if (rsc->checkVersion2_0()) {
Jason Samsd01d9702009-12-23 14:35:29 -0800754 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsc460e552009-11-25 13:22:07 -0800755 } else {
Jason Samsd01d9702009-12-23 14:35:29 -0800756 va.setupGL(rsc, &rsc->mStateVertexArray);
Jason Samsc460e552009-11-25 13:22:07 -0800757 }
Jason Samse45ac6e2009-07-20 14:31:06 -0700758
Jason Samse45ac6e2009-07-20 14:31:06 -0700759
760 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
761}
762
Romain Guyfcc1c2b2009-08-08 18:30:19 -0700763static void SC_drawQuad(float x1, float y1, float z1,
764 float x2, float y2, float z2,
765 float x3, float y3, float z3,
766 float x4, float y4, float z4)
767{
768 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
769 x2, y2, z2, 1, 1,
770 x3, y3, z3, 1, 0,
771 x4, y4, z4, 0, 0);
772}
773
Jason Sams3a97c592009-09-30 17:36:20 -0700774static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h)
775{
776 GET_TLS();
Jason Samsc460e552009-11-25 13:22:07 -0800777 ObjectBaseRef<const ProgramVertex> tmp(rsc->getVertex());
778 rsc->setVertex(rsc->getDefaultProgramVertex());
779 //rsc->setupCheck();
Jason Sams3a97c592009-09-30 17:36:20 -0700780
Jason Samsc460e552009-11-25 13:22:07 -0800781 //GLint crop[4] = {0, h, w, -h};
782
783 float sh = rsc->getHeight();
784
785 SC_drawQuad(x, sh - y, z,
786 x+w, sh - y, z,
787 x+w, sh - (y+h), z,
788 x, sh - (y+h), z);
789 rsc->setVertex((ProgramVertex *)tmp.get());
Jason Sams3a97c592009-09-30 17:36:20 -0700790}
791
Joe Onoratod08a81a2010-01-14 15:59:35 -0500792static void SC_drawSpriteScreenspaceCropped(float x, float y, float z, float w, float h,
793 float cx0, float cy0, float cx1, float cy1)
794{
795 GET_TLS();
Jason Samsa2cf7552010-03-03 13:03:18 -0800796 if (!rsc->setupCheck()) {
797 return;
798 }
Joe Onoratod08a81a2010-01-14 15:59:35 -0500799
800 GLint crop[4] = {cx0, cy0, cx1, cy1};
801 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
802 glDrawTexfOES(x, y, z, w, h);
803}
804
Jason Sams3a97c592009-09-30 17:36:20 -0700805static void SC_drawSprite(float x, float y, float z, float w, float h)
806{
807 GET_TLS();
Jason Sams3a97c592009-09-30 17:36:20 -0700808 float vin[3] = {x, y, z};
809 float vout[4];
810
811 //LOGE("ds in %f %f %f", x, y, z);
812 rsc->getVertex()->transformToScreen(rsc, vout, vin);
813 //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]);
814 vout[0] /= vout[3];
815 vout[1] /= vout[3];
816 vout[2] /= vout[3];
817
818 vout[0] *= rsc->getWidth() / 2;
819 vout[1] *= rsc->getHeight() / 2;
820 vout[0] += rsc->getWidth() / 2;
821 vout[1] += rsc->getHeight() / 2;
822
823 vout[0] -= w/2;
824 vout[1] -= h/2;
825
826 //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]);
827
828 // U, V, W, H
Jason Samsc460e552009-11-25 13:22:07 -0800829 SC_drawSpriteScreenspace(vout[0], vout[1], z, h, w);
830 //rsc->setupCheck();
Jason Sams3a97c592009-09-30 17:36:20 -0700831}
832
833
Jason Samse9f5c532009-07-28 17:20:11 -0700834static void SC_drawRect(float x1, float y1,
835 float x2, float y2, float z)
836{
Jason Samsbe36bf32010-05-11 14:03:58 -0700837 //LOGE("SC_drawRect %f,%f %f,%f %f", x1, y1, x2, y2, z);
Jason Samse9f5c532009-07-28 17:20:11 -0700838 SC_drawQuad(x1, y2, z,
839 x2, y2, z,
840 x2, y1, z,
841 x1, y1, z);
842}
843
Jason Samse5ffb872009-08-09 17:01:55 -0700844static void SC_drawSimpleMesh(RsSimpleMesh vsm)
845{
846 GET_TLS();
847 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
Jason Samsa2cf7552010-03-03 13:03:18 -0800848 if (!rsc->setupCheck()) {
849 return;
850 }
Jason Samsc460e552009-11-25 13:22:07 -0800851 sm->render(rsc);
Jason Samse5ffb872009-08-09 17:01:55 -0700852}
853
854static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len)
855{
856 GET_TLS();
857 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
Jason Samsa2cf7552010-03-03 13:03:18 -0800858 if (!rsc->setupCheck()) {
859 return;
860 }
Jason Samsc460e552009-11-25 13:22:07 -0800861 sm->renderRange(rsc, start, len);
Jason Samse5ffb872009-08-09 17:01:55 -0700862}
863
864
Jason Samse45ac6e2009-07-20 14:31:06 -0700865//////////////////////////////////////////////////////////////////////////////
866//
867//////////////////////////////////////////////////////////////////////////////
868
Jason Samsbe36bf32010-05-11 14:03:58 -0700869static uint32_t SC_allocGetDimX(RsAllocation va)
870{
871 GET_TLS();
872 const Allocation *a = static_cast<const Allocation *>(va);
873 //LOGE("SC_allocGetDimX a=%p", a);
874 //LOGE(" type=%p", a->getType());
875 return a->getType()->getDimX();
876}
877
878static uint32_t SC_allocGetDimY(RsAllocation va)
879{
880 GET_TLS();
881 const Allocation *a = static_cast<const Allocation *>(va);
882 return a->getType()->getDimY();
883}
884
885static uint32_t SC_allocGetDimZ(RsAllocation va)
886{
887 GET_TLS();
888 const Allocation *a = static_cast<const Allocation *>(va);
889 return a->getType()->getDimZ();
890}
891
892static uint32_t SC_allocGetDimLOD(RsAllocation va)
893{
894 GET_TLS();
895 const Allocation *a = static_cast<const Allocation *>(va);
896 return a->getType()->getDimLOD();
897}
898
899static uint32_t SC_allocGetDimFaces(RsAllocation va)
900{
901 GET_TLS();
902 const Allocation *a = static_cast<const Allocation *>(va);
903 return a->getType()->getDimFaces();
904}
905
906
Jason Samse45ac6e2009-07-20 14:31:06 -0700907static void SC_color(float r, float g, float b, float a)
908{
Jason Samsc460e552009-11-25 13:22:07 -0800909 GET_TLS();
Jason Samse9ed6cc2009-12-16 14:13:06 -0800910 rsc->mStateVertex.color[0] = r;
911 rsc->mStateVertex.color[1] = g;
912 rsc->mStateVertex.color[2] = b;
913 rsc->mStateVertex.color[3] = a;
Jason Sams7dad9c32009-12-17 16:55:08 -0800914 if (!rsc->checkVersion2_0()) {
Jason Samsc460e552009-11-25 13:22:07 -0800915 glColor4f(r, g, b, a);
916 }
Jason Samse45ac6e2009-07-20 14:31:06 -0700917}
918
Romain Guye62cc902009-09-04 17:55:41 -0700919static void SC_pointAttenuation(float a, float b, float c)
920{
921 GLfloat params[] = { a, b, c };
922 glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, params);
923}
924
Romain Guy370ed152009-08-20 17:08:33 -0700925static void SC_hsbToRgb(float h, float s, float b, float* rgb)
Romain Guy9c59d022009-07-31 15:33:59 -0700926{
927 float red = 0.0f;
928 float green = 0.0f;
929 float blue = 0.0f;
Jason Samse5ffb872009-08-09 17:01:55 -0700930
Romain Guy9c59d022009-07-31 15:33:59 -0700931 float x = h;
932 float y = s;
933 float z = b;
Jason Samse5ffb872009-08-09 17:01:55 -0700934
Romain Guy9c59d022009-07-31 15:33:59 -0700935 float hf = (x - (int) x) * 6.0f;
936 int ihf = (int) hf;
937 float f = hf - ihf;
938 float pv = z * (1.0f - y);
939 float qv = z * (1.0f - y * f);
940 float tv = z * (1.0f - y * (1.0f - f));
Jason Samse5ffb872009-08-09 17:01:55 -0700941
Romain Guy9c59d022009-07-31 15:33:59 -0700942 switch (ihf) {
943 case 0: // Red is the dominant color
944 red = z;
945 green = tv;
946 blue = pv;
947 break;
948 case 1: // Green is the dominant color
949 red = qv;
950 green = z;
951 blue = pv;
952 break;
953 case 2:
954 red = pv;
955 green = z;
956 blue = tv;
957 break;
958 case 3: // Blue is the dominant color
959 red = pv;
960 green = qv;
961 blue = z;
962 break;
963 case 4:
964 red = tv;
965 green = pv;
966 blue = z;
967 break;
968 case 5: // Red is the dominant color
969 red = z;
970 green = pv;
971 blue = qv;
972 break;
973 }
Jason Samse5ffb872009-08-09 17:01:55 -0700974
Romain Guy370ed152009-08-20 17:08:33 -0700975 rgb[0] = red;
976 rgb[1] = green;
977 rgb[2] = blue;
978}
979
980static int SC_hsbToAbgr(float h, float s, float b, float a)
981{
Jason Samsbe36bf32010-05-11 14:03:58 -0700982 //LOGE("hsb a %f, %f, %f %f", h, s, b, a);
Romain Guy370ed152009-08-20 17:08:33 -0700983 float rgb[3];
984 SC_hsbToRgb(h, s, b, rgb);
Jason Samsbe36bf32010-05-11 14:03:58 -0700985 //LOGE("rgb %f, %f, %f ", rgb[0], rgb[1], rgb[2]);
Romain Guy370ed152009-08-20 17:08:33 -0700986 return int(a * 255.0f) << 24 |
987 int(rgb[2] * 255.0f) << 16 |
988 int(rgb[1] * 255.0f) << 8 |
989 int(rgb[0] * 255.0f);
990}
991
992static void SC_hsb(float h, float s, float b, float a)
993{
Jason Samsc460e552009-11-25 13:22:07 -0800994 GET_TLS();
Romain Guy370ed152009-08-20 17:08:33 -0700995 float rgb[3];
996 SC_hsbToRgb(h, s, b, rgb);
Jason Samsc460e552009-11-25 13:22:07 -0800997 if (rsc->checkVersion2_0()) {
998 glVertexAttrib4f(1, rgb[0], rgb[1], rgb[2], a);
999 } else {
1000 glColor4f(rgb[0], rgb[1], rgb[2], a);
1001 }
Romain Guy9c59d022009-07-31 15:33:59 -07001002}
1003
Jason Samsc9d43db2009-07-28 12:02:16 -07001004static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samse45ac6e2009-07-20 14:31:06 -07001005{
1006 GET_TLS();
Jason Sams7fabe1a2010-02-23 17:44:28 -08001007 rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel);
Jason Samse45ac6e2009-07-20 14:31:06 -07001008}
1009
Jason Samse5ffb872009-08-09 17:01:55 -07001010static void SC_uploadToBufferObject(RsAllocation va)
1011{
1012 GET_TLS();
1013 rsi_AllocationUploadToBufferObject(rsc, va);
1014}
1015
Jason Sams9ebb0c42010-01-12 12:12:28 -08001016static void SC_syncToGL(RsAllocation va)
1017{
1018 GET_TLS();
1019 Allocation *a = static_cast<Allocation *>(va);
1020
1021}
1022
Jason Samse45ac6e2009-07-20 14:31:06 -07001023static void SC_ClearColor(float r, float g, float b, float a)
1024{
1025 //LOGE("c %f %f %f %f", r, g, b, a);
1026 GET_TLS();
1027 sc->mEnviroment.mClearColor[0] = r;
1028 sc->mEnviroment.mClearColor[1] = g;
1029 sc->mEnviroment.mClearColor[2] = b;
1030 sc->mEnviroment.mClearColor[3] = a;
1031}
1032
Jason Samsc9d43db2009-07-28 12:02:16 -07001033static void SC_debugF(const char *s, float f)
1034{
1035 LOGE("%s %f", s, f);
1036}
1037
Romain Guy370ed152009-08-20 17:08:33 -07001038static void SC_debugHexF(const char *s, float f)
1039{
1040 LOGE("%s 0x%x", s, *((int *) (&f)));
1041}
1042
Jason Samsc9d43db2009-07-28 12:02:16 -07001043static void SC_debugI32(const char *s, int32_t i)
1044{
1045 LOGE("%s %i", s, i);
1046}
1047
Romain Guy370ed152009-08-20 17:08:33 -07001048static void SC_debugHexI32(const char *s, int32_t i)
1049{
1050 LOGE("%s 0x%x", s, i);
1051}
1052
Jason Samse579df42009-08-10 14:55:26 -07001053static uint32_t SC_getWidth()
1054{
1055 GET_TLS();
1056 return rsc->getWidth();
1057}
Jason Samse45ac6e2009-07-20 14:31:06 -07001058
Jason Samse579df42009-08-10 14:55:26 -07001059static uint32_t SC_getHeight()
1060{
1061 GET_TLS();
1062 return rsc->getHeight();
1063}
Jason Samse45ac6e2009-07-20 14:31:06 -07001064
Jason Samsbe36bf32010-05-11 14:03:58 -07001065static uchar4 SC_convertColorTo8888_f3(float r, float g, float b) {
1066 uchar4 t;
1067 t.f[0] = (uint8_t)(r * 255.f);
1068 t.f[1] = (uint8_t)(g * 255.f);
1069 t.f[2] = (uint8_t)(b * 255.f);
1070 t.f[3] = 0xff;
1071 return t;
Jason Sams90b36a82009-08-17 13:56:09 -07001072}
1073
Jason Samsbe36bf32010-05-11 14:03:58 -07001074static uchar4 SC_convertColorTo8888_f4(float r, float g, float b, float a) {
1075 uchar4 t;
1076 t.f[0] = (uint8_t)(r * 255.f);
1077 t.f[1] = (uint8_t)(g * 255.f);
1078 t.f[2] = (uint8_t)(b * 255.f);
1079 t.f[3] = (uint8_t)(a * 255.f);
1080 return t;
Jason Sams90b36a82009-08-17 13:56:09 -07001081}
1082
Jason Sams8c401ef2009-10-06 13:58:47 -07001083static uint32_t SC_toClient(void *data, int cmdID, int len, int waitForSpace)
1084{
1085 GET_TLS();
Jason Samsbe36bf32010-05-11 14:03:58 -07001086 //LOGE("SC_toClient %i %i %i", cmdID, len, waitForSpace);
Jason Sams8c401ef2009-10-06 13:58:47 -07001087 return rsc->sendMessageToClient(data, cmdID, len, waitForSpace != 0);
1088}
1089
Jason Sams3a27c952009-10-07 18:14:01 -07001090static void SC_scriptCall(int scriptID)
1091{
1092 GET_TLS();
1093 rsc->runScript((Script *)scriptID, 0);
1094}
1095
Jason Samsbe36bf32010-05-11 14:03:58 -07001096static void SC_debugP(int i, void *p)
1097{
1098 LOGE("debug P %i %p, %i", i, p, (int)p);
1099}
1100
1101static void SC_debugPi(int i, int p)
1102{
1103 LOGE("debug Pi %i 0x%08x, %i", i, p, (int)p);
1104}
1105
1106static void SC_debugPf(int i, float p)
1107{
1108 LOGE("debug Pf %i %f, 0x%08x", i, p, reinterpret_cast<uint32_t *>(&p)[0]);
1109}
1110
1111int SC_divsi3(int a, int b)
1112{
1113 return a / b;
1114}
Jason Sams3a27c952009-10-07 18:14:01 -07001115
Jason Samse45ac6e2009-07-20 14:31:06 -07001116//////////////////////////////////////////////////////////////////////////////
1117// Class implementation
1118//////////////////////////////////////////////////////////////////////////////
1119
Jason Samsbe36bf32010-05-11 14:03:58 -07001120// llvm name mangling ref
1121// <builtin-type> ::= v # void
1122// ::= b # bool
1123// ::= c # char
1124// ::= a # signed char
1125// ::= h # unsigned char
1126// ::= s # short
1127// ::= t # unsigned short
1128// ::= i # int
1129// ::= j # unsigned int
1130// ::= l # long
1131// ::= m # unsigned long
1132// ::= x # long long, __int64
1133// ::= y # unsigned long long, __int64
1134// ::= f # float
1135// ::= d # double
Jason Samse45ac6e2009-07-20 14:31:06 -07001136
Jason Samsbe36bf32010-05-11 14:03:58 -07001137ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
1138 { "__divsi3", (void *)&SC_divsi3 },
1139
1140 // IO
Jason Samsbe36bf32010-05-11 14:03:58 -07001141 { "updateSimpleMesh", (void *)&SC_updateSimpleMesh },
1142
1143 // OpenCL math
1144 { "_Z4acosf", (void *)&acosf },
1145 { "_Z5acoshf", (void *)&acoshf },
1146 { "_Z6acospif", (void *)&SC_acospi },
1147 { "_Z4asinf", (void *)&asinf },
1148 { "_Z5asinhf", (void *)&asinhf },
1149 { "_Z6asinpif", (void *)&SC_asinpi },
1150 { "_Z4atanf", (void *)&atanf },
1151 { "_Z5atan2f", (void *)&atan2f },
1152 { "_Z6atanpif", (void *)&SC_atanpi },
1153 { "_Z7atan2pif", (void *)&SC_atan2pi },
1154 { "_Z4cbrtf", (void *)&cbrtf },
1155 { "_Z4ceilf", (void *)&ceilf },
1156 { "_Z8copysignff", (void *)&copysignf },
1157 { "_Z3cosf", (void *)&cosf },
1158 { "_Z4coshf", (void *)&coshf },
1159 { "_Z5cospif", (void *)&SC_cospi },
1160 { "_Z4erfcf", (void *)&erfcf },
1161 { "_Z3erff", (void *)&erff },
1162 { "_Z3expf", (void *)&expf },
1163 { "_Z4exp2f", (void *)&exp2f },
1164 { "_Z5exp10f", (void *)&SC_exp10 },
1165 { "_Z5expm1f", (void *)&expm1f },
1166 { "_Z4fabsf", (void *)&fabsf },
1167 { "_Z4fdimff", (void *)&fdimf },
1168 { "_Z5floorf", (void *)&floorf },
1169 { "_Z3fmafff", (void *)&fmaf },
1170 { "_Z4fmaxff", (void *)&fmaxf },
1171 { "_Z4fminff", (void *)&fminf }, // float fmin(float, float)
1172 { "_Z4fmodff", (void *)&fmodf },
1173 { "_Z5fractfPf", (void *)&SC_fract },
1174 { "_Z5frexpfPi", (void *)&frexpf },
1175 { "_Z5hypotff", (void *)&hypotf },
1176 { "_Z5ilogbf", (void *)&ilogbf },
1177 { "_Z5ldexpfi", (void *)&ldexpf },
1178 { "_Z6lgammaf", (void *)&lgammaf },
1179 { "_Z3logf", (void *)&logf },
1180 { "_Z4log2f", (void *)&SC_log2 },
1181 { "_Z5log10f", (void *)&log10f },
1182 { "_Z5log1pf", (void *)&log1pf },
1183 //{ "logb", (void *)& },
1184 //{ "mad", (void *)& },
1185 { "modf", (void *)&modff },
1186 //{ "nan", (void *)& },
1187 { "_Z9nextafterff", (void *)&nextafterf },
1188 { "_Z3powff", (void *)&powf },
1189 { "_Z4pownfi", (void *)&SC_pown },
1190 { "_Z4powrff", (void *)&SC_powr },
1191 { "_Z9remainderff", (void *)&remainderf },
1192 { "remquo", (void *)&remquof },
1193 { "_Z4rintf", (void *)&rintf },
1194 { "_Z5rootnfi", (void *)&SC_rootn },
1195 { "_Z5roundf", (void *)&roundf },
1196 { "_Z5rsqrtf", (void *)&SC_rsqrt },
1197 { "_Z3sinf", (void *)&sinf },
1198 { "sincos", (void *)&SC_sincos },
1199 { "_Z4sinhf", (void *)&sinhf },
1200 { "_Z5sinpif", (void *)&SC_sinpi },
1201 { "_Z4sqrtf", (void *)&sqrtf },
1202 { "_Z3tanf", (void *)&tanf },
1203 { "_Z4tanhf", (void *)&tanhf },
1204 { "_Z5tanpif", (void *)&SC_tanpi },
1205 //{ "tgamma", (void *)& },
1206 { "_Z5truncf", (void *)&truncf },
1207
1208 // OpenCL Int
1209 { "_Z3absi", (void *)&SC_abs_i32 },
1210 { "_Z3abss", (void *)&SC_abs_i16 },
1211 { "_Z3absc", (void *)&SC_abs_i8 },
1212 { "_Z3clzj", (void *)&SC_clz_u32 },
1213 { "_Z3clzt", (void *)&SC_clz_u16 },
1214 { "_Z3clzh", (void *)&SC_clz_u8 },
1215 { "_Z3clzi", (void *)&SC_clz_i32 },
1216 { "_Z3clzs", (void *)&SC_clz_i16 },
1217 { "_Z3clzc", (void *)&SC_clz_i8 },
1218 { "_Z3maxjj", (void *)&SC_max_u32 },
1219 { "_Z3maxtt", (void *)&SC_max_u16 },
1220 { "_Z3maxhh", (void *)&SC_max_u8 },
1221 { "_Z3maxii", (void *)&SC_max_i32 },
1222 { "_Z3maxss", (void *)&SC_max_i16 },
1223 { "_Z3maxcc", (void *)&SC_max_i8 },
1224 { "_Z3minjj", (void *)&SC_min_u32 },
1225 { "_Z3mintt", (void *)&SC_min_u16 },
1226 { "_Z3minhh", (void *)&SC_min_u8 },
1227 { "_Z3minii", (void *)&SC_min_i32 },
1228 { "_Z3minss", (void *)&SC_min_i16 },
1229 { "_Z3mincc", (void *)&SC_min_i8 },
1230
1231 // OpenCL 6.11.4
1232 { "_Z5clampfff", (void *)&SC_clamp_f32 },
1233 { "_Z7degreesf", (void *)&SC_degrees },
1234 { "_Z3maxff", (void *)&SC_max_f32 },
1235 { "_Z3minff", (void *)&SC_min_f32 },
1236 { "_Z3mixfff", (void *)&SC_mix_f32 },
1237 { "_Z7radiansf", (void *)&SC_radians },
1238 { "_Z4stepff", (void *)&SC_step_f32 },
1239 //{ "smoothstep", (void *)& },
1240 { "_Z4signf", (void *)&SC_sign_f32 },
1241
1242
1243
1244
1245 { "modf", (void *)&fmod },
1246 { "_Z4fracf", (void *)&SC_frac },
1247 //{ "sinf_fast", (void *)&SC_sinf_fast },
1248 //{ "cosf_fast", (void *)&SC_cosf_fast },
1249 { "randf", (void *)&SC_randf },
1250 { "randf2", (void *)&SC_randf2 },
1251 { "sign", (void *)&SC_sign },
1252 { "clamp", (void *)&SC_clamp },
1253 { "distf2", (void *)&SC_distf2 },
1254 { "distf3", (void *)&SC_distf3 },
1255 { "magf2", (void *)&SC_magf2 },
1256 { "magf3", (void *)&SC_magf3 },
1257 { "normf", (void *)&SC_normf },
1258 { "mapf", (void *)&SC_mapf },
1259 { "noisef", (void *)&SC_noisef },
1260 { "noisef2", (void *)&SC_noisef2 },
1261 { "noisef3", (void *)&SC_noisef3 },
1262 { "turbulencef2", (void *)&SC_turbulencef2 },
1263 { "turbulencef3", (void *)&SC_turbulencef3 },
Jason Samse45ac6e2009-07-20 14:31:06 -07001264
Romain Guy98e10fd2009-07-30 18:45:01 -07001265 // time
Jason Samsbe36bf32010-05-11 14:03:58 -07001266 { "second", (void *)&SC_second },
1267 { "minute", (void *)&SC_minute },
1268 { "hour", (void *)&SC_hour },
1269 { "day", (void *)&SC_day },
1270 { "month", (void *)&SC_month },
1271 { "year", (void *)&SC_year },
1272 { "uptimeMillis", (void*)&SC_uptimeMillis }, // TODO: use long instead
1273 { "startTimeMillis", (void*)&SC_startTimeMillis }, // TODO: use long instead
1274 { "elapsedTimeMillis", (void*)&SC_elapsedTimeMillis }, // TODO: use long instead
Romain Guy98e10fd2009-07-30 18:45:01 -07001275
Jason Samse45ac6e2009-07-20 14:31:06 -07001276 // matrix
Jason Samsbe36bf32010-05-11 14:03:58 -07001277 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity },
1278 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat },
1279 { "matrixLoadMat", (void *)&SC_matrixLoadMat },
1280 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate },
1281 { "matrixLoadScale", (void *)&SC_matrixLoadScale },
1282 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate },
1283 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply },
1284 { "matrixMultiply", (void *)&SC_matrixMultiply },
1285 { "matrixRotate", (void *)&SC_matrixRotate },
1286 { "matrixScale", (void *)&SC_matrixScale },
1287 { "matrixTranslate", (void *)&SC_matrixTranslate },
Jason Samse45ac6e2009-07-20 14:31:06 -07001288
Jason Sams90b36a82009-08-17 13:56:09 -07001289 // vector
Jason Samsbe36bf32010-05-11 14:03:58 -07001290 { "vec2Rand", (void *)&SC_vec2Rand },
Jason Sams90b36a82009-08-17 13:56:09 -07001291
Jason Samsa57c0a72009-09-04 14:42:41 -07001292 // vec3
Jason Samsbe36bf32010-05-11 14:03:58 -07001293 { "vec3Norm", (void *)&SC_vec3Norm },
1294 { "vec3Length", (void *)&SC_vec3Length },
1295 { "vec3Add", (void *)&SC_vec3Add },
1296 { "vec3Sub", (void *)&SC_vec3Sub },
1297 { "vec3Cross", (void *)&SC_vec3Cross },
1298 { "vec3Dot", (void *)&SC_vec3Dot },
1299 { "vec3Scale", (void *)&SC_vec3Scale },
Jason Samsa57c0a72009-09-04 14:42:41 -07001300
Romain Guyd6d4a5f2009-10-09 16:05:25 -07001301 // vec4
Jason Samsbe36bf32010-05-11 14:03:58 -07001302 { "vec4Norm", (void *)&SC_vec4Norm },
1303 { "vec4Length", (void *)&SC_vec4Length },
1304 { "vec4Add", (void *)&SC_vec4Add },
1305 { "vec4Sub", (void *)&SC_vec4Sub },
1306 { "vec4Dot", (void *)&SC_vec4Dot },
1307 { "vec4Scale", (void *)&SC_vec4Scale },
Romain Guyd6d4a5f2009-10-09 16:05:25 -07001308
Jason Samse45ac6e2009-07-20 14:31:06 -07001309 // context
Jason Samsbe36bf32010-05-11 14:03:58 -07001310 { "bindProgramFragment", (void *)&SC_bindProgramFragment },
Jason Samsccc010b2010-05-13 18:30:11 -07001311 { "bindProgramStore", (void *)&SC_bindProgramStore },
Jason Samsbe36bf32010-05-11 14:03:58 -07001312 { "bindProgramVertex", (void *)&SC_bindProgramVertex },
Jason Sams1b6b7fa2010-05-12 18:26:52 -07001313 { "bindProgramRaster", (void *)&SC_bindProgramRaster },
Jason Samsbe36bf32010-05-11 14:03:58 -07001314 { "bindSampler", (void *)&SC_bindSampler },
1315 { "bindTexture", (void *)&SC_bindTexture },
Jason Samse45ac6e2009-07-20 14:31:06 -07001316
Jason Samsc9d43db2009-07-28 12:02:16 -07001317 // vp
Jason Samsbe36bf32010-05-11 14:03:58 -07001318 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix },
1319 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix },
Jason Samsc9d43db2009-07-28 12:02:16 -07001320
Jason Samsbe36bf32010-05-11 14:03:58 -07001321 // allocation
1322 { "allocGetDimX", (void *)&SC_allocGetDimX },
1323 { "allocGetDimY", (void *)&SC_allocGetDimY },
1324 { "allocGetDimZ", (void *)&SC_allocGetDimZ },
1325 { "allocGetDimLOD", (void *)&SC_allocGetDimLOD },
1326 { "allocGetDimFaces", (void *)&SC_allocGetDimFaces },
Jason Samsc9d43db2009-07-28 12:02:16 -07001327
Jason Samse45ac6e2009-07-20 14:31:06 -07001328 // drawing
Jason Samsbe36bf32010-05-11 14:03:58 -07001329 { "drawRect", (void *)&SC_drawRect },
1330 { "drawQuad", (void *)&SC_drawQuad },
1331 { "drawQuadTexCoords", (void *)&SC_drawQuadTexCoords },
1332 { "drawSprite", (void *)&SC_drawSprite },
1333 { "drawSpriteScreenspace", (void *)&SC_drawSpriteScreenspace },
1334 { "drawSpriteScreenspaceCropped", (void *)&SC_drawSpriteScreenspaceCropped },
1335 { "drawLine", (void *)&SC_drawLine },
1336 { "drawPoint", (void *)&SC_drawPoint },
1337 { "drawSimpleMesh", (void *)&SC_drawSimpleMesh },
1338 { "drawSimpleMeshRange", (void *)&SC_drawSimpleMeshRange },
Jason Samse45ac6e2009-07-20 14:31:06 -07001339
1340
1341 // misc
Jason Samsbe36bf32010-05-11 14:03:58 -07001342 { "pfClearColor", (void *)&SC_ClearColor },
1343 { "color", (void *)&SC_color },
1344 { "hsb", (void *)&SC_hsb },
1345 { "hsbToRgb", (void *)&SC_hsbToRgb },
1346 { "hsbToAbgr", (void *)&SC_hsbToAbgr },
1347 { "pointAttenuation", (void *)&SC_pointAttenuation },
Jason Samse45ac6e2009-07-20 14:31:06 -07001348
Jason Samsbe36bf32010-05-11 14:03:58 -07001349 { "uploadToTexture", (void *)&SC_uploadToTexture },
1350 { "uploadToBufferObject", (void *)&SC_uploadToBufferObject },
Jason Samsc9d43db2009-07-28 12:02:16 -07001351
Jason Samsbe36bf32010-05-11 14:03:58 -07001352 { "syncToGL", (void *)&SC_syncToGL },
Jason Sams9ebb0c42010-01-12 12:12:28 -08001353
Jason Samsbe36bf32010-05-11 14:03:58 -07001354 { "getWidth", (void *)&SC_getWidth },
1355 { "getHeight", (void *)&SC_getHeight },
Jason Sams90b36a82009-08-17 13:56:09 -07001356
Jason Samsbe36bf32010-05-11 14:03:58 -07001357 { "sendToClient", (void *)&SC_toClient },
Jason Sams90b36a82009-08-17 13:56:09 -07001358
Jason Samsbe36bf32010-05-11 14:03:58 -07001359 { "_Z18convertColorTo8888fff", (void *)&SC_convertColorTo8888_f3 },
1360 { "_Z18convertColorTo8888ffff", (void *)&SC_convertColorTo8888_f4 },
Jason Samse579df42009-08-10 14:55:26 -07001361
Jason Samsbe36bf32010-05-11 14:03:58 -07001362 { "debugF", (void *)&SC_debugF },
1363 { "debugI32", (void *)&SC_debugI32 },
1364 { "debugHexF", (void *)&SC_debugHexF },
1365 { "debugHexI32", (void *)&SC_debugHexI32 },
1366 { "debugP", (void *)&SC_debugP },
1367 { "debugPf", (void *)&SC_debugPf },
1368 { "debugPi", (void *)&SC_debugPi },
Jason Samse579df42009-08-10 14:55:26 -07001369
Jason Samsbe36bf32010-05-11 14:03:58 -07001370 { "scriptCall", (void *)&SC_scriptCall },
Jason Samsc9d43db2009-07-28 12:02:16 -07001371
Jason Samsbe36bf32010-05-11 14:03:58 -07001372 { NULL, NULL }
Jason Samse45ac6e2009-07-20 14:31:06 -07001373};
1374
1375const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
1376{
1377 ScriptCState::SymbolTable_t *syms = gSyms;
1378
1379 while (syms->mPtr) {
1380 if (!strcmp(syms->mName, sym)) {
1381 return syms;
1382 }
1383 syms++;
1384 }
1385 return NULL;
1386}
1387