blob: 7db3619f291b2f5e9693d729025f182ed7192e70 [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"
20
21#include "acc/acc.h"
22#include "utils/String8.h"
23
24#include <GLES/gl.h>
25#include <GLES/glext.h>
26
27using namespace android;
28using namespace android::renderscript;
29
30#define GET_TLS() Context::ScriptTLSStruct * tls = \
31 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
32 Context * rsc = tls->mContext; \
33 ScriptC * sc = (ScriptC *) tls->mScript
34
35
36//////////////////////////////////////////////////////////////////////////////
37// IO routines
38//////////////////////////////////////////////////////////////////////////////
39
40static float SC_loadF(uint32_t bank, uint32_t offset)
41{
42 GET_TLS();
43 const void *vp = sc->mSlots[bank]->getPtr();
44 const float *f = static_cast<const float *>(vp);
45 //LOGE("loadF %i %i = %f %x", bank, offset, f, ((int *)&f)[0]);
46 return f[offset];
47}
48
49static int32_t SC_loadI32(uint32_t bank, uint32_t offset)
50{
51 GET_TLS();
52 const void *vp = sc->mSlots[bank]->getPtr();
53 const int32_t *i = static_cast<const int32_t *>(vp);
54 //LOGE("loadI32 %i %i = %i", bank, offset, t);
55 return i[offset];
56}
57
58static uint32_t SC_loadU32(uint32_t bank, uint32_t offset)
59{
60 GET_TLS();
61 const void *vp = sc->mSlots[bank]->getPtr();
62 const uint32_t *i = static_cast<const uint32_t *>(vp);
63 return i[offset];
64}
65
66static void SC_loadVec4(uint32_t bank, uint32_t offset, rsc_Vector4 *v)
67{
68 GET_TLS();
69 const void *vp = sc->mSlots[bank]->getPtr();
70 const float *f = static_cast<const float *>(vp);
71 memcpy(v, &f[offset], sizeof(rsc_Vector4));
72}
73
74static void SC_loadMatrix(uint32_t bank, uint32_t offset, rsc_Matrix *m)
75{
76 GET_TLS();
77 const void *vp = sc->mSlots[bank]->getPtr();
78 const float *f = static_cast<const float *>(vp);
79 memcpy(m, &f[offset], sizeof(rsc_Matrix));
80}
81
82
83static void SC_storeF(uint32_t bank, uint32_t offset, float v)
84{
85 //LOGE("storeF %i %i %f", bank, offset, v);
86 GET_TLS();
87 void *vp = sc->mSlots[bank]->getPtr();
88 float *f = static_cast<float *>(vp);
89 f[offset] = v;
90}
91
92static void SC_storeI32(uint32_t bank, uint32_t offset, int32_t v)
93{
94 GET_TLS();
95 void *vp = sc->mSlots[bank]->getPtr();
96 int32_t *f = static_cast<int32_t *>(vp);
97 static_cast<int32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
98}
99
100static void SC_storeU32(uint32_t bank, uint32_t offset, uint32_t v)
101{
102 GET_TLS();
103 void *vp = sc->mSlots[bank]->getPtr();
104 uint32_t *f = static_cast<uint32_t *>(vp);
105 static_cast<uint32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
106}
107
108static void SC_storeVec4(uint32_t bank, uint32_t offset, const rsc_Vector4 *v)
109{
110 GET_TLS();
111 void *vp = sc->mSlots[bank]->getPtr();
112 float *f = static_cast<float *>(vp);
113 memcpy(&f[offset], v, sizeof(rsc_Vector4));
114}
115
116static void SC_storeMatrix(uint32_t bank, uint32_t offset, const rsc_Matrix *m)
117{
118 GET_TLS();
119 void *vp = sc->mSlots[bank]->getPtr();
120 float *f = static_cast<float *>(vp);
121 memcpy(&f[offset], m, sizeof(rsc_Matrix));
122}
123
124
125//////////////////////////////////////////////////////////////////////////////
126// Math routines
127//////////////////////////////////////////////////////////////////////////////
128
129static float SC_randf(float max)
130{
131 float r = (float)rand();
132 return r / RAND_MAX * max;
133}
134
135
136
137
Jason Samse45ac6e2009-07-20 14:31:06 -0700138//////////////////////////////////////////////////////////////////////////////
139// Matrix routines
140//////////////////////////////////////////////////////////////////////////////
141
142
143static void SC_matrixLoadIdentity(rsc_Matrix *mat)
144{
145 Matrix *m = reinterpret_cast<Matrix *>(mat);
146 m->loadIdentity();
147}
148
149static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
150{
151 Matrix *m = reinterpret_cast<Matrix *>(mat);
152 m->load(f);
153}
154
155static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
156{
157 Matrix *m = reinterpret_cast<Matrix *>(mat);
158 m->load(reinterpret_cast<const Matrix *>(newmat));
159}
160
161static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
162{
163 Matrix *m = reinterpret_cast<Matrix *>(mat);
164 m->loadRotate(rot, x, y, z);
165}
166
167static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
168{
169 Matrix *m = reinterpret_cast<Matrix *>(mat);
170 m->loadScale(x, y, z);
171}
172
173static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
174{
175 Matrix *m = reinterpret_cast<Matrix *>(mat);
176 m->loadTranslate(x, y, z);
177}
178
179static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
180{
181 Matrix *m = reinterpret_cast<Matrix *>(mat);
182 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
183 reinterpret_cast<const Matrix *>(rhs));
184}
185
186static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
187{
188 Matrix *m = reinterpret_cast<Matrix *>(mat);
189 m->multiply(reinterpret_cast<const Matrix *>(rhs));
190}
191
192static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
193{
194 Matrix *m = reinterpret_cast<Matrix *>(mat);
195 m->rotate(rot, x, y, z);
196}
197
198static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
199{
200 Matrix *m = reinterpret_cast<Matrix *>(mat);
201 m->scale(x, y, z);
202}
203
204static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
205{
206 Matrix *m = reinterpret_cast<Matrix *>(mat);
207 m->translate(x, y, z);
208}
209
210
211
212
213//////////////////////////////////////////////////////////////////////////////
214// Context
215//////////////////////////////////////////////////////////////////////////////
216
217static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
218{
219 GET_TLS();
220 rsi_ProgramFragmentBindTexture(rsc,
221 static_cast<ProgramFragment *>(vpf),
222 slot,
223 static_cast<Allocation *>(va));
224
225}
226
227static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
228{
229 GET_TLS();
230 rsi_ProgramFragmentBindSampler(rsc,
231 static_cast<ProgramFragment *>(vpf),
232 slot,
233 static_cast<Sampler *>(vs));
234
235}
236
237static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs)
238{
239 GET_TLS();
240 rsi_ContextBindProgramFragmentStore(rsc, pfs);
241
242}
243
244static void SC_bindProgramFragment(RsProgramFragment pf)
245{
246 GET_TLS();
247 rsi_ContextBindProgramFragment(rsc, pf);
248
249}
250
Jason Samsb5909ce2009-07-21 12:20:54 -0700251static void SC_bindProgramVertex(RsProgramVertex pv)
252{
253 GET_TLS();
254 rsi_ContextBindProgramVertex(rsc, pv);
255
256}
Jason Samse45ac6e2009-07-20 14:31:06 -0700257
258//////////////////////////////////////////////////////////////////////////////
Jason Samsc9d43db2009-07-28 12:02:16 -0700259// VP
260//////////////////////////////////////////////////////////////////////////////
261
262static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
263{
264 GET_TLS();
265 rsc->getVertex()->setModelviewMatrix(m);
266}
267
268static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
269{
270 GET_TLS();
271 rsc->getVertex()->setTextureMatrix(m);
272}
273
274
275
276//////////////////////////////////////////////////////////////////////////////
Jason Samse45ac6e2009-07-20 14:31:06 -0700277// Drawing
278//////////////////////////////////////////////////////////////////////////////
279
280static void SC_drawTriangleMesh(RsTriangleMesh mesh)
281{
282 GET_TLS();
283 rsi_TriangleMeshRender(rsc, mesh);
284}
285
286static void SC_drawTriangleMeshRange(RsTriangleMesh mesh, uint32_t start, uint32_t count)
287{
288 GET_TLS();
289 rsi_TriangleMeshRenderRange(rsc, mesh, start, count);
290}
291
292// Assumes (GL_FIXED) x,y,z (GL_UNSIGNED_BYTE)r,g,b,a
293static void SC_drawTriangleArray(int ialloc, uint32_t count)
294{
295 GET_TLS();
296 RsAllocation alloc = (RsAllocation)ialloc;
297
298 const Allocation *a = (const Allocation *)alloc;
299 const uint32_t *ptr = (const uint32_t *)a->getPtr();
300
301 rsc->setupCheck();
302
303 glBindBuffer(GL_ARRAY_BUFFER, 0);
304 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
305
306 glEnableClientState(GL_VERTEX_ARRAY);
307 glDisableClientState(GL_NORMAL_ARRAY);
308 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
309 glEnableClientState(GL_COLOR_ARRAY);
310
311 glVertexPointer(2, GL_FIXED, 12, ptr + 1);
312 //glTexCoordPointer(2, GL_FIXED, 24, ptr + 1);
313 glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
314
315 glDrawArrays(GL_TRIANGLES, 0, count * 3);
316}
317
318static void SC_drawQuad(float x1, float y1, float z1,
319 float x2, float y2, float z2,
320 float x3, float y3, float z3,
321 float x4, float y4, float z4)
322{
323 GET_TLS();
324
325 //LOGE("Quad");
326 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
327 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
328 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
329 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
330
331 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
332 static const float tex[] = {0,1, 1,1, 1,0, 0,0};
333
334
335 rsc->setupCheck();
336
337 glBindBuffer(GL_ARRAY_BUFFER, 0);
338 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
339
340 glEnableClientState(GL_VERTEX_ARRAY);
341 glVertexPointer(3, GL_FLOAT, 0, vtx);
342
343 glClientActiveTexture(GL_TEXTURE0);
344 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
345 glTexCoordPointer(2, GL_FLOAT, 0, tex);
346 glClientActiveTexture(GL_TEXTURE1);
347 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
348 glTexCoordPointer(2, GL_FLOAT, 0, tex);
349 glClientActiveTexture(GL_TEXTURE0);
350
351 glDisableClientState(GL_NORMAL_ARRAY);
352 glDisableClientState(GL_COLOR_ARRAY);
353
354 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
355
356 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
357}
358
Jason Samse9f5c532009-07-28 17:20:11 -0700359static void SC_drawRect(float x1, float y1,
360 float x2, float y2, float z)
361{
362 SC_drawQuad(x1, y2, z,
363 x2, y2, z,
364 x2, y1, z,
365 x1, y1, z);
366}
367
Jason Samse45ac6e2009-07-20 14:31:06 -0700368//////////////////////////////////////////////////////////////////////////////
369//
370//////////////////////////////////////////////////////////////////////////////
371
Jason Samse45ac6e2009-07-20 14:31:06 -0700372static void SC_color(float r, float g, float b, float a)
373{
374 glColor4f(r, g, b, a);
375}
376
Jason Samsc9d43db2009-07-28 12:02:16 -0700377/*
Jason Samse45ac6e2009-07-20 14:31:06 -0700378extern "C" void materialDiffuse(float r, float g, float b, float a)
379{
380 float v[] = {r, g, b, a};
381 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, v);
382}
383
384extern "C" void materialSpecular(float r, float g, float b, float a)
385{
386 float v[] = {r, g, b, a};
387 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, v);
388}
389
Jason Samse45ac6e2009-07-20 14:31:06 -0700390extern "C" void materialShininess(float s)
391{
392 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &s);
393}
Jason Samsc9d43db2009-07-28 12:02:16 -0700394*/
Jason Samse45ac6e2009-07-20 14:31:06 -0700395
Jason Samsc9d43db2009-07-28 12:02:16 -0700396static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samse45ac6e2009-07-20 14:31:06 -0700397{
398 GET_TLS();
399 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
400}
401
Jason Samse45ac6e2009-07-20 14:31:06 -0700402static void SC_ClearColor(float r, float g, float b, float a)
403{
404 //LOGE("c %f %f %f %f", r, g, b, a);
405 GET_TLS();
406 sc->mEnviroment.mClearColor[0] = r;
407 sc->mEnviroment.mClearColor[1] = g;
408 sc->mEnviroment.mClearColor[2] = b;
409 sc->mEnviroment.mClearColor[3] = a;
410}
411
Jason Samsc9d43db2009-07-28 12:02:16 -0700412static void SC_debugF(const char *s, float f)
413{
414 LOGE("%s %f", s, f);
415}
416
417static void SC_debugI32(const char *s, int32_t i)
418{
419 LOGE("%s %i", s, i);
420}
421
Jason Samse45ac6e2009-07-20 14:31:06 -0700422
423
424//////////////////////////////////////////////////////////////////////////////
425// Class implementation
426//////////////////////////////////////////////////////////////////////////////
427
428ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
429 // IO
430 { "loadI32", (void *)&SC_loadI32,
431 "int", "(int, int)" },
432 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
433 { "loadF", (void *)&SC_loadF,
434 "float", "(int, int)" },
435 { "loadVec4", (void *)&SC_loadVec4,
436 "void", "(int, int, float *)" },
437 { "loadMatrix", (void *)&SC_loadMatrix,
438 "void", "(int, int, float *)" },
439 { "storeI32", (void *)&SC_storeI32,
440 "void", "(int, int, int)" },
441 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
442 { "storeF", (void *)&SC_storeF,
443 "void", "(int, int, float)" },
444 { "storeVec4", (void *)&SC_storeVec4,
445 "void", "(int, int, float *)" },
446 { "storeMatrix", (void *)&SC_storeMatrix,
447 "void", "(int, int, float *)" },
448
449 // math
450 { "sinf", (void *)&sinf,
451 "float", "(float)" },
452 { "cosf", (void *)&cosf,
453 "float", "(float)" },
Jason Samse9f5c532009-07-28 17:20:11 -0700454 { "fabsf", (void *)&fabsf,
Jason Samse45ac6e2009-07-20 14:31:06 -0700455 "float", "(float)" },
456 { "randf", (void *)&SC_randf,
457 "float", "(float)" },
Jason Samsc9d43db2009-07-28 12:02:16 -0700458 { "floorf", (void *)&floorf,
459 "float", "(float)" },
460 { "ceilf", (void *)&ceilf,
461 "float", "(float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700462
463 // matrix
464 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
465 "void", "(float *mat)" },
466 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
467 "void", "(float *mat, float *f)" },
468 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
469 "void", "(float *mat, float *newmat)" },
470 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
471 "void", "(float *mat, float rot, float x, float y, float z)" },
472 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
473 "void", "(float *mat, float x, float y, float z)" },
474 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
475 "void", "(float *mat, float x, float y, float z)" },
476 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
477 "void", "(float *mat, float *lhs, float *rhs)" },
478 { "matrixMultiply", (void *)&SC_matrixMultiply,
479 "void", "(float *mat, float *rhs)" },
480 { "matrixRotate", (void *)&SC_matrixRotate,
481 "void", "(float *mat, float rot, float x, float y, float z)" },
482 { "matrixScale", (void *)&SC_matrixScale,
483 "void", "(float *mat, float x, float y, float z)" },
484 { "matrixTranslate", (void *)&SC_matrixTranslate,
485 "void", "(float *mat, float x, float y, float z)" },
486
487 // context
488 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
489 "void", "(int)" },
490 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
491 "void", "(int)" },
Jason Samsb5909ce2009-07-21 12:20:54 -0700492 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
493 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700494 { "bindSampler", (void *)&SC_bindSampler,
495 "void", "(int, int, int)" },
496 { "bindTexture", (void *)&SC_bindTexture,
497 "void", "(int, int, int)" },
498
Jason Samsc9d43db2009-07-28 12:02:16 -0700499 // vp
Jason Sams50253db2009-07-29 20:55:44 -0700500 { "vpLoadModelMatrix", (void *)&SC_vpLoadModelMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -0700501 "void", "(void *)" },
Jason Sams50253db2009-07-29 20:55:44 -0700502 { "vpLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix,
Jason Samsc9d43db2009-07-28 12:02:16 -0700503 "void", "(void *)" },
504
505
506
Jason Samse45ac6e2009-07-20 14:31:06 -0700507 // drawing
Jason Samse9f5c532009-07-28 17:20:11 -0700508 { "drawRect", (void *)&SC_drawRect,
509 "void", "(float x1, float y1, float x2, float y2, float z)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700510 { "drawQuad", (void *)&SC_drawQuad,
511 "void", "(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4)" },
512 { "drawTriangleArray", (void *)&SC_drawTriangleArray,
513 "void", "(int ialloc, int count)" },
514 { "drawTriangleMesh", (void *)&SC_drawTriangleMesh,
515 "void", "(int mesh)" },
516 { "drawTriangleMeshRange", (void *)&SC_drawTriangleMeshRange,
517 "void", "(int mesh, int start, int count)" },
518
519
520 // misc
521 { "pfClearColor", (void *)&SC_ClearColor,
522 "void", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700523 { "color", (void *)&SC_color,
524 "void", "(float, float, float, float)" },
525
Jason Samsc9d43db2009-07-28 12:02:16 -0700526 { "uploadToTexture", (void *)&SC_uploadToTexture,
527 "void", "(int, int)" },
528
529
530 { "debugF", (void *)&SC_debugF,
531 "void", "(void *, float)" },
532 { "debugI32", (void *)&SC_debugI32,
533 "void", "(void *, int)" },
534
535
Jason Samse45ac6e2009-07-20 14:31:06 -0700536 { NULL, NULL, NULL, NULL }
537};
538
539const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
540{
541 ScriptCState::SymbolTable_t *syms = gSyms;
542
543 while (syms->mPtr) {
544 if (!strcmp(syms->mName, sym)) {
545 return syms;
546 }
547 syms++;
548 }
549 return NULL;
550}
551
552void ScriptCState::appendDecls(String8 *str)
553{
554 ScriptCState::SymbolTable_t *syms = gSyms;
555 while (syms->mPtr) {
556 str->append(syms->mRet);
557 str->append(" ");
558 str->append(syms->mName);
559 str->append(syms->mParam);
560 str->append(";\n");
561 syms++;
562 }
563}
564
565