blob: 10d1120a26772d2f26857658edae2e1e3e5cd1ce [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
359//////////////////////////////////////////////////////////////////////////////
360//
361//////////////////////////////////////////////////////////////////////////////
362
Jason Samse45ac6e2009-07-20 14:31:06 -0700363static void SC_color(float r, float g, float b, float a)
364{
365 glColor4f(r, g, b, a);
366}
367
Jason Samsc9d43db2009-07-28 12:02:16 -0700368/*
Jason Samse45ac6e2009-07-20 14:31:06 -0700369extern "C" void materialDiffuse(float r, float g, float b, float a)
370{
371 float v[] = {r, g, b, a};
372 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, v);
373}
374
375extern "C" void materialSpecular(float r, float g, float b, float a)
376{
377 float v[] = {r, g, b, a};
378 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, v);
379}
380
Jason Samse45ac6e2009-07-20 14:31:06 -0700381extern "C" void materialShininess(float s)
382{
383 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &s);
384}
Jason Samsc9d43db2009-07-28 12:02:16 -0700385*/
Jason Samse45ac6e2009-07-20 14:31:06 -0700386
Jason Samsc9d43db2009-07-28 12:02:16 -0700387static void SC_uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samse45ac6e2009-07-20 14:31:06 -0700388{
389 GET_TLS();
390 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
391}
392
Jason Samse45ac6e2009-07-20 14:31:06 -0700393static void SC_ClearColor(float r, float g, float b, float a)
394{
395 //LOGE("c %f %f %f %f", r, g, b, a);
396 GET_TLS();
397 sc->mEnviroment.mClearColor[0] = r;
398 sc->mEnviroment.mClearColor[1] = g;
399 sc->mEnviroment.mClearColor[2] = b;
400 sc->mEnviroment.mClearColor[3] = a;
401}
402
Jason Samsc9d43db2009-07-28 12:02:16 -0700403static void SC_debugF(const char *s, float f)
404{
405 LOGE("%s %f", s, f);
406}
407
408static void SC_debugI32(const char *s, int32_t i)
409{
410 LOGE("%s %i", s, i);
411}
412
Jason Samse45ac6e2009-07-20 14:31:06 -0700413
414
415//////////////////////////////////////////////////////////////////////////////
416// Class implementation
417//////////////////////////////////////////////////////////////////////////////
418
419ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
420 // IO
421 { "loadI32", (void *)&SC_loadI32,
422 "int", "(int, int)" },
423 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
424 { "loadF", (void *)&SC_loadF,
425 "float", "(int, int)" },
426 { "loadVec4", (void *)&SC_loadVec4,
427 "void", "(int, int, float *)" },
428 { "loadMatrix", (void *)&SC_loadMatrix,
429 "void", "(int, int, float *)" },
430 { "storeI32", (void *)&SC_storeI32,
431 "void", "(int, int, int)" },
432 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
433 { "storeF", (void *)&SC_storeF,
434 "void", "(int, int, float)" },
435 { "storeVec4", (void *)&SC_storeVec4,
436 "void", "(int, int, float *)" },
437 { "storeMatrix", (void *)&SC_storeMatrix,
438 "void", "(int, int, float *)" },
439
440 // math
441 { "sinf", (void *)&sinf,
442 "float", "(float)" },
443 { "cosf", (void *)&cosf,
444 "float", "(float)" },
445 { "fabs", (void *)&fabs,
446 "float", "(float)" },
447 { "randf", (void *)&SC_randf,
448 "float", "(float)" },
Jason Samsc9d43db2009-07-28 12:02:16 -0700449 { "floorf", (void *)&floorf,
450 "float", "(float)" },
451 { "ceilf", (void *)&ceilf,
452 "float", "(float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700453
454 // matrix
455 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
456 "void", "(float *mat)" },
457 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
458 "void", "(float *mat, float *f)" },
459 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
460 "void", "(float *mat, float *newmat)" },
461 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
462 "void", "(float *mat, float rot, float x, float y, float z)" },
463 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
464 "void", "(float *mat, float x, float y, float z)" },
465 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
466 "void", "(float *mat, float x, float y, float z)" },
467 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
468 "void", "(float *mat, float *lhs, float *rhs)" },
469 { "matrixMultiply", (void *)&SC_matrixMultiply,
470 "void", "(float *mat, float *rhs)" },
471 { "matrixRotate", (void *)&SC_matrixRotate,
472 "void", "(float *mat, float rot, float x, float y, float z)" },
473 { "matrixScale", (void *)&SC_matrixScale,
474 "void", "(float *mat, float x, float y, float z)" },
475 { "matrixTranslate", (void *)&SC_matrixTranslate,
476 "void", "(float *mat, float x, float y, float z)" },
477
478 // context
479 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
480 "void", "(int)" },
481 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
482 "void", "(int)" },
Jason Samsb5909ce2009-07-21 12:20:54 -0700483 { "bindProgramVertex", (void *)&SC_bindProgramVertex,
484 "void", "(int)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700485 { "bindSampler", (void *)&SC_bindSampler,
486 "void", "(int, int, int)" },
487 { "bindTexture", (void *)&SC_bindTexture,
488 "void", "(int, int, int)" },
489
Jason Samsc9d43db2009-07-28 12:02:16 -0700490 // vp
491 { "vpLoadModelMatrix", (void *)&SC_bindProgramFragment,
492 "void", "(void *)" },
493 { "vpLoadTextureMatrix", (void *)&SC_bindProgramFragmentStore,
494 "void", "(void *)" },
495
496
497
Jason Samse45ac6e2009-07-20 14:31:06 -0700498 // drawing
499 { "drawQuad", (void *)&SC_drawQuad,
500 "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)" },
501 { "drawTriangleArray", (void *)&SC_drawTriangleArray,
502 "void", "(int ialloc, int count)" },
503 { "drawTriangleMesh", (void *)&SC_drawTriangleMesh,
504 "void", "(int mesh)" },
505 { "drawTriangleMeshRange", (void *)&SC_drawTriangleMeshRange,
506 "void", "(int mesh, int start, int count)" },
507
508
509 // misc
510 { "pfClearColor", (void *)&SC_ClearColor,
511 "void", "(float, float, float, float)" },
Jason Samse45ac6e2009-07-20 14:31:06 -0700512 { "color", (void *)&SC_color,
513 "void", "(float, float, float, float)" },
514
Jason Samsc9d43db2009-07-28 12:02:16 -0700515 { "uploadToTexture", (void *)&SC_uploadToTexture,
516 "void", "(int, int)" },
517
518
519 { "debugF", (void *)&SC_debugF,
520 "void", "(void *, float)" },
521 { "debugI32", (void *)&SC_debugI32,
522 "void", "(void *, int)" },
523
524
Jason Samse45ac6e2009-07-20 14:31:06 -0700525 { NULL, NULL, NULL, NULL }
526};
527
528const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
529{
530 ScriptCState::SymbolTable_t *syms = gSyms;
531
532 while (syms->mPtr) {
533 if (!strcmp(syms->mName, sym)) {
534 return syms;
535 }
536 syms++;
537 }
538 return NULL;
539}
540
541void ScriptCState::appendDecls(String8 *str)
542{
543 ScriptCState::SymbolTable_t *syms = gSyms;
544 while (syms->mPtr) {
545 str->append(syms->mRet);
546 str->append(" ");
547 str->append(syms->mName);
548 str->append(syms->mParam);
549 str->append(";\n");
550 syms++;
551 }
552}
553
554