blob: 59d8e5f0c51b9479f3eba763b0050aff7abcb730 [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
138
139//////////////////////////////////////////////////////////////////////////////
140// Matrix routines
141//////////////////////////////////////////////////////////////////////////////
142
143
144static void SC_matrixLoadIdentity(rsc_Matrix *mat)
145{
146 Matrix *m = reinterpret_cast<Matrix *>(mat);
147 m->loadIdentity();
148}
149
150static void SC_matrixLoadFloat(rsc_Matrix *mat, const float *f)
151{
152 Matrix *m = reinterpret_cast<Matrix *>(mat);
153 m->load(f);
154}
155
156static void SC_matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
157{
158 Matrix *m = reinterpret_cast<Matrix *>(mat);
159 m->load(reinterpret_cast<const Matrix *>(newmat));
160}
161
162static void SC_matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
163{
164 Matrix *m = reinterpret_cast<Matrix *>(mat);
165 m->loadRotate(rot, x, y, z);
166}
167
168static void SC_matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
169{
170 Matrix *m = reinterpret_cast<Matrix *>(mat);
171 m->loadScale(x, y, z);
172}
173
174static void SC_matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
175{
176 Matrix *m = reinterpret_cast<Matrix *>(mat);
177 m->loadTranslate(x, y, z);
178}
179
180static void SC_matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
181{
182 Matrix *m = reinterpret_cast<Matrix *>(mat);
183 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
184 reinterpret_cast<const Matrix *>(rhs));
185}
186
187static void SC_matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
188{
189 Matrix *m = reinterpret_cast<Matrix *>(mat);
190 m->multiply(reinterpret_cast<const Matrix *>(rhs));
191}
192
193static void SC_matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
194{
195 Matrix *m = reinterpret_cast<Matrix *>(mat);
196 m->rotate(rot, x, y, z);
197}
198
199static void SC_matrixScale(rsc_Matrix *mat, float x, float y, float z)
200{
201 Matrix *m = reinterpret_cast<Matrix *>(mat);
202 m->scale(x, y, z);
203}
204
205static void SC_matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
206{
207 Matrix *m = reinterpret_cast<Matrix *>(mat);
208 m->translate(x, y, z);
209}
210
211
212
213
214//////////////////////////////////////////////////////////////////////////////
215// Context
216//////////////////////////////////////////////////////////////////////////////
217
218static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
219{
220 GET_TLS();
221 rsi_ProgramFragmentBindTexture(rsc,
222 static_cast<ProgramFragment *>(vpf),
223 slot,
224 static_cast<Allocation *>(va));
225
226}
227
228static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
229{
230 GET_TLS();
231 rsi_ProgramFragmentBindSampler(rsc,
232 static_cast<ProgramFragment *>(vpf),
233 slot,
234 static_cast<Sampler *>(vs));
235
236}
237
238static void SC_bindProgramFragmentStore(RsProgramFragmentStore pfs)
239{
240 GET_TLS();
241 rsi_ContextBindProgramFragmentStore(rsc, pfs);
242
243}
244
245static void SC_bindProgramFragment(RsProgramFragment pf)
246{
247 GET_TLS();
248 rsi_ContextBindProgramFragment(rsc, pf);
249
250}
251
252
253//////////////////////////////////////////////////////////////////////////////
254// Drawing
255//////////////////////////////////////////////////////////////////////////////
256
257static void SC_drawTriangleMesh(RsTriangleMesh mesh)
258{
259 GET_TLS();
260 rsi_TriangleMeshRender(rsc, mesh);
261}
262
263static void SC_drawTriangleMeshRange(RsTriangleMesh mesh, uint32_t start, uint32_t count)
264{
265 GET_TLS();
266 rsi_TriangleMeshRenderRange(rsc, mesh, start, count);
267}
268
269// Assumes (GL_FIXED) x,y,z (GL_UNSIGNED_BYTE)r,g,b,a
270static void SC_drawTriangleArray(int ialloc, uint32_t count)
271{
272 GET_TLS();
273 RsAllocation alloc = (RsAllocation)ialloc;
274
275 const Allocation *a = (const Allocation *)alloc;
276 const uint32_t *ptr = (const uint32_t *)a->getPtr();
277
278 rsc->setupCheck();
279
280 glBindBuffer(GL_ARRAY_BUFFER, 0);
281 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
282
283 glEnableClientState(GL_VERTEX_ARRAY);
284 glDisableClientState(GL_NORMAL_ARRAY);
285 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
286 glEnableClientState(GL_COLOR_ARRAY);
287
288 glVertexPointer(2, GL_FIXED, 12, ptr + 1);
289 //glTexCoordPointer(2, GL_FIXED, 24, ptr + 1);
290 glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
291
292 glDrawArrays(GL_TRIANGLES, 0, count * 3);
293}
294
295static void SC_drawQuad(float x1, float y1, float z1,
296 float x2, float y2, float z2,
297 float x3, float y3, float z3,
298 float x4, float y4, float z4)
299{
300 GET_TLS();
301
302 //LOGE("Quad");
303 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
304 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
305 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
306 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
307
308 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
309 static const float tex[] = {0,1, 1,1, 1,0, 0,0};
310
311
312 rsc->setupCheck();
313
314 glBindBuffer(GL_ARRAY_BUFFER, 0);
315 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
316
317 glEnableClientState(GL_VERTEX_ARRAY);
318 glVertexPointer(3, GL_FLOAT, 0, vtx);
319
320 glClientActiveTexture(GL_TEXTURE0);
321 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
322 glTexCoordPointer(2, GL_FLOAT, 0, tex);
323 glClientActiveTexture(GL_TEXTURE1);
324 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
325 glTexCoordPointer(2, GL_FLOAT, 0, tex);
326 glClientActiveTexture(GL_TEXTURE0);
327
328 glDisableClientState(GL_NORMAL_ARRAY);
329 glDisableClientState(GL_COLOR_ARRAY);
330
331 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
332
333 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
334}
335
336//////////////////////////////////////////////////////////////////////////////
337//
338//////////////////////////////////////////////////////////////////////////////
339
340extern "C" const void * loadVp(uint32_t bank, uint32_t offset)
341{
342 GET_TLS();
343 return &static_cast<const uint8_t *>(sc->mSlots[bank]->getPtr())[offset];
344}
345
346
347
348static void SC_color(float r, float g, float b, float a)
349{
350 glColor4f(r, g, b, a);
351}
352
353
354extern "C" void materialDiffuse(float r, float g, float b, float a)
355{
356 float v[] = {r, g, b, a};
357 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, v);
358}
359
360extern "C" void materialSpecular(float r, float g, float b, float a)
361{
362 float v[] = {r, g, b, a};
363 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, v);
364}
365
366extern "C" void lightPosition(float x, float y, float z, float w)
367{
368 float v[] = {x, y, z, w};
369 glLightfv(GL_LIGHT0, GL_POSITION, v);
370}
371
372extern "C" void materialShininess(float s)
373{
374 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &s);
375}
376
377extern "C" void uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
378{
379 GET_TLS();
380 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
381}
382
383extern "C" void enable(uint32_t p)
384{
385 glEnable(p);
386}
387
388extern "C" void disable(uint32_t p)
389{
390 glDisable(p);
391}
392
393
394
395static void SC_ClearColor(float r, float g, float b, float a)
396{
397 //LOGE("c %f %f %f %f", r, g, b, a);
398 GET_TLS();
399 sc->mEnviroment.mClearColor[0] = r;
400 sc->mEnviroment.mClearColor[1] = g;
401 sc->mEnviroment.mClearColor[2] = b;
402 sc->mEnviroment.mClearColor[3] = a;
403}
404
405
406
407//////////////////////////////////////////////////////////////////////////////
408// Class implementation
409//////////////////////////////////////////////////////////////////////////////
410
411ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
412 // IO
413 { "loadI32", (void *)&SC_loadI32,
414 "int", "(int, int)" },
415 //{ "loadU32", (void *)&SC_loadU32, "unsigned int", "(int, int)" },
416 { "loadF", (void *)&SC_loadF,
417 "float", "(int, int)" },
418 { "loadVec4", (void *)&SC_loadVec4,
419 "void", "(int, int, float *)" },
420 { "loadMatrix", (void *)&SC_loadMatrix,
421 "void", "(int, int, float *)" },
422 { "storeI32", (void *)&SC_storeI32,
423 "void", "(int, int, int)" },
424 //{ "storeU32", (void *)&SC_storeU32, "void", "(int, int, unsigned int)" },
425 { "storeF", (void *)&SC_storeF,
426 "void", "(int, int, float)" },
427 { "storeVec4", (void *)&SC_storeVec4,
428 "void", "(int, int, float *)" },
429 { "storeMatrix", (void *)&SC_storeMatrix,
430 "void", "(int, int, float *)" },
431
432 // math
433 { "sinf", (void *)&sinf,
434 "float", "(float)" },
435 { "cosf", (void *)&cosf,
436 "float", "(float)" },
437 { "fabs", (void *)&fabs,
438 "float", "(float)" },
439 { "randf", (void *)&SC_randf,
440 "float", "(float)" },
441
442 // matrix
443 { "matrixLoadIdentity", (void *)&SC_matrixLoadIdentity,
444 "void", "(float *mat)" },
445 { "matrixLoadFloat", (void *)&SC_matrixLoadFloat,
446 "void", "(float *mat, float *f)" },
447 { "matrixLoadMat", (void *)&SC_matrixLoadMat,
448 "void", "(float *mat, float *newmat)" },
449 { "matrixLoadRotate", (void *)&SC_matrixLoadRotate,
450 "void", "(float *mat, float rot, float x, float y, float z)" },
451 { "matrixLoadScale", (void *)&SC_matrixLoadScale,
452 "void", "(float *mat, float x, float y, float z)" },
453 { "matrixLoadTranslate", (void *)&SC_matrixLoadTranslate,
454 "void", "(float *mat, float x, float y, float z)" },
455 { "matrixLoadMultiply", (void *)&SC_matrixLoadMultiply,
456 "void", "(float *mat, float *lhs, float *rhs)" },
457 { "matrixMultiply", (void *)&SC_matrixMultiply,
458 "void", "(float *mat, float *rhs)" },
459 { "matrixRotate", (void *)&SC_matrixRotate,
460 "void", "(float *mat, float rot, float x, float y, float z)" },
461 { "matrixScale", (void *)&SC_matrixScale,
462 "void", "(float *mat, float x, float y, float z)" },
463 { "matrixTranslate", (void *)&SC_matrixTranslate,
464 "void", "(float *mat, float x, float y, float z)" },
465
466 // context
467 { "bindProgramFragment", (void *)&SC_bindProgramFragment,
468 "void", "(int)" },
469 { "bindProgramFragmentStore", (void *)&SC_bindProgramFragmentStore,
470 "void", "(int)" },
471 { "bindSampler", (void *)&SC_bindSampler,
472 "void", "(int, int, int)" },
473 { "bindTexture", (void *)&SC_bindTexture,
474 "void", "(int, int, int)" },
475
476 // drawing
477 { "drawQuad", (void *)&SC_drawQuad,
478 "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)" },
479 { "drawTriangleArray", (void *)&SC_drawTriangleArray,
480 "void", "(int ialloc, int count)" },
481 { "drawTriangleMesh", (void *)&SC_drawTriangleMesh,
482 "void", "(int mesh)" },
483 { "drawTriangleMeshRange", (void *)&SC_drawTriangleMeshRange,
484 "void", "(int mesh, int start, int count)" },
485
486
487 // misc
488 { "pfClearColor", (void *)&SC_ClearColor,
489 "void", "(float, float, float, float)" },
490
491 { "color", (void *)&SC_color,
492 "void", "(float, float, float, float)" },
493
494 { NULL, NULL, NULL, NULL }
495};
496
497const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
498{
499 ScriptCState::SymbolTable_t *syms = gSyms;
500
501 while (syms->mPtr) {
502 if (!strcmp(syms->mName, sym)) {
503 return syms;
504 }
505 syms++;
506 }
507 return NULL;
508}
509
510void ScriptCState::appendDecls(String8 *str)
511{
512 ScriptCState::SymbolTable_t *syms = gSyms;
513 while (syms->mPtr) {
514 str->append(syms->mRet);
515 str->append(" ");
516 str->append(syms->mName);
517 str->append(syms->mParam);
518 str->append(";\n");
519 syms++;
520 }
521}
522
523