blob: adbe3e9d07e6b88172969b26c065fd6b2299f510 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -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
Jack Palevichec5a20b2009-05-28 15:53:04 -070021#include "acc/acc.h"
Jason Samsd5680f92009-06-10 18:39:40 -070022#include "utils/String8.h"
Jack Palevichec5a20b2009-05-28 15:53:04 -070023
Jason Sams4b962e52009-06-22 17:15:15 -070024#include <GLES/gl.h>
25#include <GLES/glext.h>
26
Jason Samsd19f10d2009-05-22 14:03:28 -070027using namespace android;
28using namespace android::renderscript;
29
Jason Sams462d11b2009-06-19 16:03:18 -070030#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
Jason Samsd19f10d2009-05-22 14:03:28 -070035
36ScriptC::ScriptC()
37{
Jack Palevichec5a20b2009-05-28 15:53:04 -070038 mAccScript = NULL;
Jason Sams3a833d82009-06-08 15:20:31 -070039 memset(&mProgram, 0, sizeof(mProgram));
Jason Samsd19f10d2009-05-22 14:03:28 -070040}
41
42ScriptC::~ScriptC()
43{
Jack Palevichec5a20b2009-05-28 15:53:04 -070044 if (mAccScript) {
45 accDeleteScript(mAccScript);
46 }
Jason Samsd19f10d2009-05-22 14:03:28 -070047}
48
Jason Samsaf49c742009-06-19 18:33:44 -070049extern "C" float fixedToFloat(int32_t f)
50{
51 return ((float)f) / 0x10000;
52}
53
54extern "C" float intToFloat(int32_t f)
55{
56 return (float)f;
57}
58
Jason Sams462d11b2009-06-19 16:03:18 -070059extern "C" void matrixLoadIdentity(rsc_Matrix *mat)
Jason Samsd19f10d2009-05-22 14:03:28 -070060{
61 Matrix *m = reinterpret_cast<Matrix *>(mat);
62 m->loadIdentity();
63}
64
Jason Sams462d11b2009-06-19 16:03:18 -070065extern "C" void matrixLoadFloat(rsc_Matrix *mat, const float *f)
Jason Samsd19f10d2009-05-22 14:03:28 -070066{
67 Matrix *m = reinterpret_cast<Matrix *>(mat);
68 m->load(f);
69}
70
Jason Sams462d11b2009-06-19 16:03:18 -070071extern "C" void matrixLoadMat(rsc_Matrix *mat, const rsc_Matrix *newmat)
Jason Samsd19f10d2009-05-22 14:03:28 -070072{
73 Matrix *m = reinterpret_cast<Matrix *>(mat);
74 m->load(reinterpret_cast<const Matrix *>(newmat));
75}
76
Jason Sams462d11b2009-06-19 16:03:18 -070077extern "C" void matrixLoadRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
Jason Samsd19f10d2009-05-22 14:03:28 -070078{
79 Matrix *m = reinterpret_cast<Matrix *>(mat);
80 m->loadRotate(rot, x, y, z);
81}
82
Jason Sams462d11b2009-06-19 16:03:18 -070083extern "C" void matrixLoadScale(rsc_Matrix *mat, float x, float y, float z)
Jason Samsd19f10d2009-05-22 14:03:28 -070084{
85 Matrix *m = reinterpret_cast<Matrix *>(mat);
86 m->loadScale(x, y, z);
87}
88
Jason Sams462d11b2009-06-19 16:03:18 -070089extern "C" void matrixLoadTranslate(rsc_Matrix *mat, float x, float y, float z)
Jason Samsd19f10d2009-05-22 14:03:28 -070090{
91 Matrix *m = reinterpret_cast<Matrix *>(mat);
92 m->loadTranslate(x, y, z);
93}
94
Jason Sams462d11b2009-06-19 16:03:18 -070095extern "C" void matrixLoadMultiply(rsc_Matrix *mat, const rsc_Matrix *lhs, const rsc_Matrix *rhs)
Jason Samsd19f10d2009-05-22 14:03:28 -070096{
97 Matrix *m = reinterpret_cast<Matrix *>(mat);
98 m->loadMultiply(reinterpret_cast<const Matrix *>(lhs),
99 reinterpret_cast<const Matrix *>(rhs));
100}
101
Jason Sams462d11b2009-06-19 16:03:18 -0700102extern "C" void matrixMultiply(rsc_Matrix *mat, const rsc_Matrix *rhs)
Jason Samsd19f10d2009-05-22 14:03:28 -0700103{
104 Matrix *m = reinterpret_cast<Matrix *>(mat);
105 m->multiply(reinterpret_cast<const Matrix *>(rhs));
106}
107
Jason Sams462d11b2009-06-19 16:03:18 -0700108extern "C" void matrixRotate(rsc_Matrix *mat, float rot, float x, float y, float z)
Jason Samsd19f10d2009-05-22 14:03:28 -0700109{
110 Matrix *m = reinterpret_cast<Matrix *>(mat);
111 m->rotate(rot, x, y, z);
112}
113
Jason Sams462d11b2009-06-19 16:03:18 -0700114extern "C" void matrixScale(rsc_Matrix *mat, float x, float y, float z)
Jason Samsd19f10d2009-05-22 14:03:28 -0700115{
116 Matrix *m = reinterpret_cast<Matrix *>(mat);
117 m->scale(x, y, z);
118}
119
Jason Sams462d11b2009-06-19 16:03:18 -0700120extern "C" void matrixTranslate(rsc_Matrix *mat, float x, float y, float z)
Jason Samsd19f10d2009-05-22 14:03:28 -0700121{
122 Matrix *m = reinterpret_cast<Matrix *>(mat);
123 m->translate(x, y, z);
124}
125
126
Jason Sams462d11b2009-06-19 16:03:18 -0700127extern "C" const void * loadVp(uint32_t bank, uint32_t offset)
Jason Samsd19f10d2009-05-22 14:03:28 -0700128{
Jason Sams462d11b2009-06-19 16:03:18 -0700129 GET_TLS();
130 return &static_cast<const uint8_t *>(sc->mSlots[bank]->getPtr())[offset];
Jason Samsd19f10d2009-05-22 14:03:28 -0700131}
132
Jason Sams54440a02009-07-16 15:08:06 -0700133static float SC_loadF(uint32_t bank, uint32_t offset)
Jason Samsd19f10d2009-05-22 14:03:28 -0700134{
Jason Sams462d11b2009-06-19 16:03:18 -0700135 GET_TLS();
Jason Samsc75a2572009-07-15 18:35:54 -0700136 float f = static_cast<const float *>(sc->mSlots[bank]->getPtr())[offset];
137 //LOGE("loadF %i %i = %f %x", bank, offset, f, ((int *)&f)[0]);
138 return f;
Jason Samsd19f10d2009-05-22 14:03:28 -0700139}
140
Jason Sams54440a02009-07-16 15:08:06 -0700141static int32_t SC_loadI32(uint32_t bank, uint32_t offset)
Jason Samsd19f10d2009-05-22 14:03:28 -0700142{
Jason Sams462d11b2009-06-19 16:03:18 -0700143 GET_TLS();
Jason Samsc75a2572009-07-15 18:35:54 -0700144 int32_t t = static_cast<const int32_t *>(sc->mSlots[bank]->getPtr())[offset];
145 //LOGE("loadI32 %i %i = %i", bank, offset, t);
146 return t;
Jason Samsd19f10d2009-05-22 14:03:28 -0700147}
148
Jason Sams54440a02009-07-16 15:08:06 -0700149static uint32_t SC_loadU32(uint32_t bank, uint32_t offset)
Jason Samsd19f10d2009-05-22 14:03:28 -0700150{
Jason Sams462d11b2009-06-19 16:03:18 -0700151 GET_TLS();
152 return static_cast<const uint32_t *>(sc->mSlots[bank]->getPtr())[offset];
Jason Samsd19f10d2009-05-22 14:03:28 -0700153}
154
Jason Sams462d11b2009-06-19 16:03:18 -0700155extern "C" void loadEnvVec4(uint32_t bank, uint32_t offset, rsc_Vector4 *v)
Jason Samsd19f10d2009-05-22 14:03:28 -0700156{
Jason Sams462d11b2009-06-19 16:03:18 -0700157 GET_TLS();
158 memcpy(v, &static_cast<const float *>(sc->mSlots[bank]->getPtr())[offset], sizeof(rsc_Vector4));
Jason Samsd19f10d2009-05-22 14:03:28 -0700159}
160
Jason Sams462d11b2009-06-19 16:03:18 -0700161extern "C" void loadEnvMatrix(uint32_t bank, uint32_t offset, rsc_Matrix *m)
Jason Samsd19f10d2009-05-22 14:03:28 -0700162{
Jason Sams462d11b2009-06-19 16:03:18 -0700163 GET_TLS();
164 memcpy(m, &static_cast<const float *>(sc->mSlots[bank]->getPtr())[offset], sizeof(rsc_Matrix));
Jason Samsd19f10d2009-05-22 14:03:28 -0700165}
166
167
Jason Sams54440a02009-07-16 15:08:06 -0700168static void SC_storeF(uint32_t bank, uint32_t offset, float v)
Jason Samsd19f10d2009-05-22 14:03:28 -0700169{
Jason Samsc75a2572009-07-15 18:35:54 -0700170 //LOGE("storeF %i %i %f", bank, offset, v);
Jason Sams462d11b2009-06-19 16:03:18 -0700171 GET_TLS();
172 static_cast<float *>(sc->mSlots[bank]->getPtr())[offset] = v;
Jason Samsd19f10d2009-05-22 14:03:28 -0700173}
174
Jason Sams54440a02009-07-16 15:08:06 -0700175static void SC_storeI32(uint32_t bank, uint32_t offset, int32_t v)
Jason Samsd19f10d2009-05-22 14:03:28 -0700176{
Jason Sams462d11b2009-06-19 16:03:18 -0700177 GET_TLS();
178 static_cast<int32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
Jason Samsd19f10d2009-05-22 14:03:28 -0700179}
180
Jason Sams54440a02009-07-16 15:08:06 -0700181static void SC_storeU32(uint32_t bank, uint32_t offset, uint32_t v)
Jason Samsd19f10d2009-05-22 14:03:28 -0700182{
Jason Sams462d11b2009-06-19 16:03:18 -0700183 GET_TLS();
184 static_cast<uint32_t *>(sc->mSlots[bank]->getPtr())[offset] = v;
Jason Samsd19f10d2009-05-22 14:03:28 -0700185}
186
Jason Sams462d11b2009-06-19 16:03:18 -0700187extern "C" void storeEnvVec4(uint32_t bank, uint32_t offset, const rsc_Vector4 *v)
Jason Samsd19f10d2009-05-22 14:03:28 -0700188{
Jason Sams462d11b2009-06-19 16:03:18 -0700189 GET_TLS();
190 memcpy(&static_cast<float *>(sc->mSlots[bank]->getPtr())[offset], v, sizeof(rsc_Vector4));
Jason Samsd19f10d2009-05-22 14:03:28 -0700191}
192
Jason Sams462d11b2009-06-19 16:03:18 -0700193extern "C" void storeEnvMatrix(uint32_t bank, uint32_t offset, const rsc_Matrix *m)
Jason Samsd19f10d2009-05-22 14:03:28 -0700194{
Jason Sams462d11b2009-06-19 16:03:18 -0700195 GET_TLS();
196 memcpy(&static_cast<float *>(sc->mSlots[bank]->getPtr())[offset], m, sizeof(rsc_Matrix));
Jason Samsd19f10d2009-05-22 14:03:28 -0700197}
198
199
Jason Sams462d11b2009-06-19 16:03:18 -0700200extern "C" void color(float r, float g, float b, float a)
Jason Samsd19f10d2009-05-22 14:03:28 -0700201{
Jason Samsd19f10d2009-05-22 14:03:28 -0700202 glColor4f(r, g, b, a);
203}
204
Jason Sams462d11b2009-06-19 16:03:18 -0700205extern "C" void renderTriangleMesh(RsTriangleMesh mesh)
Jason Samsd19f10d2009-05-22 14:03:28 -0700206{
Jason Sams462d11b2009-06-19 16:03:18 -0700207 GET_TLS();
208 rsi_TriangleMeshRender(rsc, mesh);
Jason Samsd19f10d2009-05-22 14:03:28 -0700209}
210
Jason Sams462d11b2009-06-19 16:03:18 -0700211extern "C" void renderTriangleMeshRange(RsTriangleMesh mesh, uint32_t start, uint32_t count)
Jason Samsd19f10d2009-05-22 14:03:28 -0700212{
Jason Sams462d11b2009-06-19 16:03:18 -0700213 GET_TLS();
214 rsi_TriangleMeshRenderRange(rsc, mesh, start, count);
Jason Samsd19f10d2009-05-22 14:03:28 -0700215}
216
Jason Sams462d11b2009-06-19 16:03:18 -0700217extern "C" void materialDiffuse(float r, float g, float b, float a)
Jason Samsd19f10d2009-05-22 14:03:28 -0700218{
Jason Samsd19f10d2009-05-22 14:03:28 -0700219 float v[] = {r, g, b, a};
220 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, v);
221}
222
Jason Sams462d11b2009-06-19 16:03:18 -0700223extern "C" void materialSpecular(float r, float g, float b, float a)
Jason Samsd19f10d2009-05-22 14:03:28 -0700224{
Jason Samsd19f10d2009-05-22 14:03:28 -0700225 float v[] = {r, g, b, a};
226 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, v);
227}
228
Jason Sams462d11b2009-06-19 16:03:18 -0700229extern "C" void lightPosition(float x, float y, float z, float w)
Jason Samsd19f10d2009-05-22 14:03:28 -0700230{
Jason Samsd19f10d2009-05-22 14:03:28 -0700231 float v[] = {x, y, z, w};
232 glLightfv(GL_LIGHT0, GL_POSITION, v);
233}
234
Jason Sams462d11b2009-06-19 16:03:18 -0700235extern "C" void materialShininess(float s)
Jason Samsd19f10d2009-05-22 14:03:28 -0700236{
Jason Samsd19f10d2009-05-22 14:03:28 -0700237 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &s);
238}
239
Jason Sams462d11b2009-06-19 16:03:18 -0700240extern "C" void uploadToTexture(RsAllocation va, uint32_t baseMipLevel)
Jason Samsd19f10d2009-05-22 14:03:28 -0700241{
Jason Sams462d11b2009-06-19 16:03:18 -0700242 GET_TLS();
243 rsi_AllocationUploadToTexture(rsc, va, baseMipLevel);
Jason Samsd19f10d2009-05-22 14:03:28 -0700244}
245
Jason Sams462d11b2009-06-19 16:03:18 -0700246extern "C" void enable(uint32_t p)
Jason Samsd19f10d2009-05-22 14:03:28 -0700247{
Jason Samsd19f10d2009-05-22 14:03:28 -0700248 glEnable(p);
249}
250
Jason Sams462d11b2009-06-19 16:03:18 -0700251extern "C" void disable(uint32_t p)
Jason Samsd19f10d2009-05-22 14:03:28 -0700252{
Jason Samsd19f10d2009-05-22 14:03:28 -0700253 glDisable(p);
254}
255
Jason Sams462d11b2009-06-19 16:03:18 -0700256extern "C" uint32_t scriptRand(uint32_t max)
Jason Samsd19f10d2009-05-22 14:03:28 -0700257{
258 return (uint32_t)(((float)rand()) * max / RAND_MAX);
259}
260
261// Assumes (GL_FIXED) x,y,z (GL_UNSIGNED_BYTE)r,g,b,a
Jason Sams462d11b2009-06-19 16:03:18 -0700262extern "C" void drawTriangleArray(RsAllocation alloc, uint32_t count)
Jason Samsd19f10d2009-05-22 14:03:28 -0700263{
Jason Sams462d11b2009-06-19 16:03:18 -0700264 GET_TLS();
265
Jason Samsd19f10d2009-05-22 14:03:28 -0700266 const Allocation *a = (const Allocation *)alloc;
267 const uint32_t *ptr = (const uint32_t *)a->getPtr();
268
Jason Sams462d11b2009-06-19 16:03:18 -0700269 rsc->setupCheck();
Jason Samsd19f10d2009-05-22 14:03:28 -0700270
271 glBindBuffer(GL_ARRAY_BUFFER, 0);
272 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
273
274 glEnableClientState(GL_VERTEX_ARRAY);
275 glDisableClientState(GL_NORMAL_ARRAY);
276 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
277 glEnableClientState(GL_COLOR_ARRAY);
278
279 glVertexPointer(2, GL_FIXED, 12, ptr + 1);
280 //glTexCoordPointer(2, GL_FIXED, 24, ptr + 1);
281 glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
282
283 glDrawArrays(GL_TRIANGLES, 0, count * 3);
284}
285
Jason Sams462d11b2009-06-19 16:03:18 -0700286extern "C" void drawRect(int32_t x1, int32_t x2, int32_t y1, int32_t y2)
Jason Samsfe08d992009-05-27 14:45:32 -0700287{
Jason Sams462d11b2009-06-19 16:03:18 -0700288 GET_TLS();
Jason Samsfe08d992009-05-27 14:45:32 -0700289 x1 = (x1 << 16);
290 x2 = (x2 << 16);
291 y1 = (y1 << 16);
292 y2 = (y2 << 16);
293
294 int32_t vtx[] = {x1,y1, x1,y2, x2,y1, x2,y2};
295 static const int32_t tex[] = {0,0, 0,0x10000, 0x10000,0, 0x10000,0x10000};
296
297
Jason Sams462d11b2009-06-19 16:03:18 -0700298 rsc->setupCheck();
Jason Samsfe08d992009-05-27 14:45:32 -0700299
300 glBindBuffer(GL_ARRAY_BUFFER, 0);
301 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
302
303 glEnableClientState(GL_VERTEX_ARRAY);
304 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
305 glDisableClientState(GL_NORMAL_ARRAY);
306 glDisableClientState(GL_COLOR_ARRAY);
307
308 glVertexPointer(2, GL_FIXED, 8, vtx);
309 glTexCoordPointer(2, GL_FIXED, 8, tex);
310 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
311
312 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
313}
314
Jason Sams54440a02009-07-16 15:08:06 -0700315static void SC_drawQuad(float x1, float y1, float z1,
316 float x2, float y2, float z2,
317 float x3, float y3, float z3,
318 float x4, float y4, float z4)
Jason Samsc75a2572009-07-15 18:35:54 -0700319{
320 GET_TLS();
321
322 //LOGE("Quad");
323 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
324 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
325 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
326 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
327
328 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
329 static const float tex[] = {0,0, 0,1, 1,1, 1,0};
330
331
332 rsc->setupCheck();
333
334 glBindBuffer(GL_ARRAY_BUFFER, 0);
335 //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tm->mBufferObjects[1]);
336
337 glEnableClientState(GL_VERTEX_ARRAY);
338 glVertexPointer(3, GL_FLOAT, 0, vtx);
339
340 glClientActiveTexture(GL_TEXTURE0);
341 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
342 glTexCoordPointer(2, GL_FLOAT, 0, tex);
343 glClientActiveTexture(GL_TEXTURE1);
344 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
345 glTexCoordPointer(2, GL_FLOAT, 0, tex);
346 glClientActiveTexture(GL_TEXTURE0);
347
348 glDisableClientState(GL_NORMAL_ARRAY);
349 glDisableClientState(GL_COLOR_ARRAY);
350
351 //glColorPointer(4, GL_UNSIGNED_BYTE, 12, ptr);
352
353 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
354}
355
Jason Samsc75a2572009-07-15 18:35:54 -0700356extern "C" float sinf(float angle)
357{
358 float s = (float)sin(angle);
359 return s;
360}
361
362extern "C" float cosf(float angle)
363{
364 float s = (float)cos(angle);
365 return s;
366}
367
368extern "C" void pfClearColor(float r, float g, float b, float a)
369{
370 //LOGE("c %f %f %f %f", r, g, b, a);
371 GET_TLS();
372 sc->mEnviroment.mClearColor[0] = r;
373 sc->mEnviroment.mClearColor[1] = g;
374 sc->mEnviroment.mClearColor[2] = b;
375 sc->mEnviroment.mClearColor[3] = a;
376}
377
Jason Sams462d11b2009-06-19 16:03:18 -0700378extern "C" void pfBindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
Jason Samsd19f10d2009-05-22 14:03:28 -0700379{
Jason Sams462d11b2009-06-19 16:03:18 -0700380 GET_TLS();
381 rsi_ProgramFragmentBindTexture(rsc,
Jason Samsd19f10d2009-05-22 14:03:28 -0700382 static_cast<ProgramFragment *>(vpf),
383 slot,
384 static_cast<Allocation *>(va));
385
386}
387
Jason Sams462d11b2009-06-19 16:03:18 -0700388extern "C" void pfBindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
Jason Samsd19f10d2009-05-22 14:03:28 -0700389{
Jason Sams462d11b2009-06-19 16:03:18 -0700390 GET_TLS();
391 rsi_ProgramFragmentBindSampler(rsc,
Jason Samsd19f10d2009-05-22 14:03:28 -0700392 static_cast<ProgramFragment *>(vpf),
393 slot,
394 static_cast<Sampler *>(vs));
395
396}
397
Jason Sams462d11b2009-06-19 16:03:18 -0700398extern "C" void contextBindProgramFragmentStore(RsProgramFragmentStore pfs)
Jason Samsd19f10d2009-05-22 14:03:28 -0700399{
Jason Sams462d11b2009-06-19 16:03:18 -0700400 GET_TLS();
401 rsi_ContextBindProgramFragmentStore(rsc, pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -0700402
403}
404
Jason Sams462d11b2009-06-19 16:03:18 -0700405extern "C" void contextBindProgramFragment(RsProgramFragment pf)
Jason Samsd19f10d2009-05-22 14:03:28 -0700406{
Jason Sams462d11b2009-06-19 16:03:18 -0700407 GET_TLS();
408 rsi_ContextBindProgramFragment(rsc, pf);
Jason Samsd19f10d2009-05-22 14:03:28 -0700409
410}
411
412
413static rsc_FunctionTable scriptCPtrTable = {
414 loadVp,
Jason Sams54440a02009-07-16 15:08:06 -0700415 SC_loadF,
416 SC_loadI32,
417 SC_loadU32,
Jason Samsd19f10d2009-05-22 14:03:28 -0700418 loadEnvVec4,
419 loadEnvMatrix,
420
Jason Sams54440a02009-07-16 15:08:06 -0700421 SC_storeF,
422 SC_storeI32,
423 SC_storeU32,
Jason Samsd19f10d2009-05-22 14:03:28 -0700424 storeEnvVec4,
425 storeEnvMatrix,
426
427 matrixLoadIdentity,
428 matrixLoadFloat,
429 matrixLoadMat,
430 matrixLoadRotate,
431 matrixLoadScale,
432 matrixLoadTranslate,
433 matrixLoadMultiply,
434 matrixMultiply,
435 matrixRotate,
436 matrixScale,
437 matrixTranslate,
438
439 color,
Jason Samsd19f10d2009-05-22 14:03:28 -0700440
441 pfBindTexture,
442 pfBindSampler,
443
444 materialDiffuse,
445 materialSpecular,
446 lightPosition,
447 materialShininess,
448 uploadToTexture,
449 enable,
450 disable,
451
452 scriptRand,
Jason Samsd19f10d2009-05-22 14:03:28 -0700453 contextBindProgramFragment,
Jason Samsfe08d992009-05-27 14:45:32 -0700454 contextBindProgramFragmentStore,
455
456
457 renderTriangleMesh,
458 renderTriangleMeshRange,
459
460 drawTriangleArray,
461 drawRect
462
Jason Samsd19f10d2009-05-22 14:03:28 -0700463};
464
465
Jason Sams462d11b2009-06-19 16:03:18 -0700466bool ScriptC::run(Context *rsc, uint32_t launchIndex)
Jason Samsd19f10d2009-05-22 14:03:28 -0700467{
Jason Sams462d11b2009-06-19 16:03:18 -0700468 Context::ScriptTLSStruct * tls =
469 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey);
Jason Sams3eaa3382009-06-10 15:04:38 -0700470
471 if (mEnviroment.mFragmentStore.get()) {
472 rsc->setFragmentStore(mEnviroment.mFragmentStore.get());
473 }
474 if (mEnviroment.mFragment.get()) {
475 rsc->setFragment(mEnviroment.mFragment.get());
476 }
Jason Sams9c54bdb2009-06-17 16:52:59 -0700477 if (mEnviroment.mVertex.get()) {
478 rsc->setVertex(mEnviroment.mVertex.get());
479 }
Jason Sams3eaa3382009-06-10 15:04:38 -0700480
Jason Sams462d11b2009-06-19 16:03:18 -0700481 tls->mScript = this;
482 return mProgram.mScript(launchIndex, &scriptCPtrTable) != 0;
483 tls->mScript = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700484}
485
486ScriptCState::ScriptCState()
487{
488 clear();
489}
490
491ScriptCState::~ScriptCState()
492{
Jack Palevichec5a20b2009-05-28 15:53:04 -0700493 if (mAccScript) {
494 accDeleteScript(mAccScript);
495 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700496}
497
498void ScriptCState::clear()
499{
Jason Sams3a833d82009-06-08 15:20:31 -0700500 memset(&mProgram, 0, sizeof(mProgram));
501
Jason Samsd19f10d2009-05-22 14:03:28 -0700502 mConstantBufferTypes.clear();
Jason Sams3a833d82009-06-08 15:20:31 -0700503
504 memset(&mEnviroment, 0, sizeof(mEnviroment));
505 mEnviroment.mClearColor[0] = 0;
506 mEnviroment.mClearColor[1] = 0;
507 mEnviroment.mClearColor[2] = 0;
508 mEnviroment.mClearColor[3] = 1;
509 mEnviroment.mClearDepth = 1;
510 mEnviroment.mClearStencil = 0;
511 mEnviroment.mIsRoot = false;
Jason Sams3a833d82009-06-08 15:20:31 -0700512
Jack Palevichec5a20b2009-05-28 15:53:04 -0700513 mAccScript = NULL;
Jason Sams3a833d82009-06-08 15:20:31 -0700514
Jason Sams39ddc9502009-06-05 17:35:09 -0700515}
516
Jason Sams54440a02009-07-16 15:08:06 -0700517ScriptCState::SymbolTable_t ScriptCState::gSyms[] = {
518 { "loadI32", (void *)&SC_loadI32, "int loadI32(int, int)" },
519 { "loadF", (void *)&SC_loadF, "float loadF(int, int)" },
520 { "storeI32", (void *)&SC_storeI32, "void storeI32(int, int, int)" },
521 { "storeF", (void *)&SC_storeF, "void storeF(int, int, float)" },
Jason Sams764205c2009-07-16 17:47:40 -0700522 { "drawQuad", (void *)&SC_drawQuad, "void drawQuad(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4)" },
Jason Sams54440a02009-07-16 15:08:06 -0700523 { "sinf", (void *)&sinf, "float sinf(float)" },
524 { "cosf", (void *)&cosf, "float cosf(float)" },
Jason Sams764205c2009-07-16 17:47:40 -0700525 { "contextBindProgramFragmentStore", (void *)&contextBindProgramFragmentStore, "void contextBindProgramFragmentStore(int)" },
526 { "pfClearColor", (void *)&pfClearColor, "void pfClearColor(float, float, float, float)" },
527 { "pfBindTexture", (void *)&pfBindTexture, "void pfBindTexture(int, int, int)" },
Jason Sams54440a02009-07-16 15:08:06 -0700528
529
530 { NULL, NULL, NULL }
531};
532
533const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbol(const char *sym)
534{
535 ScriptCState::SymbolTable_t *syms = gSyms;
536
537 while (syms->mPtr) {
538 if (!strcmp(syms->mName, sym)) {
539 return syms;
540 }
541 syms++;
542 }
543 return NULL;
544}
545
546static ACCvoid* symbolLookup(ACCvoid* pContext, const ACCchar* name)
547{
548 const ScriptCState::SymbolTable_t *sym = ScriptCState::lookupSymbol(name);
549
550 if (sym) {
551 return sym->mPtr;
552 }
553
554 LOGE("ScriptC sym lookup failed for %s", name);
555
556 // Default to calling dlsym to allow any global symbol:
557 return NULL;
558}
Jason Samsd5680f92009-06-10 18:39:40 -0700559
Jason Sams764205c2009-07-16 17:47:40 -0700560void ScriptCState::appendDecls(String8 *str)
561{
562 ScriptCState::SymbolTable_t *syms = gSyms;
563 while (syms->mPtr) {
564 str->append(syms->mDecl);
565 str->append(";\n");
566 syms++;
567 }
568}
569
Jason Sams3eaa3382009-06-10 15:04:38 -0700570void ScriptCState::runCompiler(Context *rsc)
Jason Sams39ddc9502009-06-05 17:35:09 -0700571{
572 mAccScript = accCreateScript();
Jason Samsd5680f92009-06-10 18:39:40 -0700573 String8 tmp;
Jason Sams39ddc9502009-06-05 17:35:09 -0700574
Jason Samsd5680f92009-06-10 18:39:40 -0700575 rsc->appendNameDefines(&tmp);
Jason Sams764205c2009-07-16 17:47:40 -0700576 appendDecls(&tmp);
577 //tmp.append("#line 1\n");
Jason Samsd5680f92009-06-10 18:39:40 -0700578
579 const char* scriptSource[] = {tmp.string(), mProgram.mScriptText};
580 int scriptLength[] = {tmp.length(), mProgram.mScriptTextLength} ;
581 accScriptSource(mAccScript, sizeof(scriptLength) / sizeof(int), scriptSource, scriptLength);
Jason Sams54440a02009-07-16 15:08:06 -0700582 accRegisterSymbolCallback(mAccScript, symbolLookup, NULL);
Jason Sams39ddc9502009-06-05 17:35:09 -0700583 accCompileScript(mAccScript);
Jason Sams3a833d82009-06-08 15:20:31 -0700584 accGetScriptLabel(mAccScript, "main", (ACCvoid**) &mProgram.mScript);
Jason Samsda423d82009-06-09 12:15:30 -0700585 rsAssert(mProgram.mScript);
586
Jason Sams764205c2009-07-16 17:47:40 -0700587 if (!mProgram.mScript) {
588 ACCchar buf[4096];
589 ACCsizei len;
590 accGetScriptInfoLog(mAccScript, sizeof(buf), &len, buf);
591 LOGE(buf);
592
593 }
594
Jason Sams9c54bdb2009-06-17 16:52:59 -0700595 mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
596 mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
597 mEnviroment.mFragmentStore.set(rsc->getDefaultProgramFragmentStore());
Jason Samsd5680f92009-06-10 18:39:40 -0700598
Jason Samsda423d82009-06-09 12:15:30 -0700599 if (mProgram.mScript) {
600 const static int pragmaMax = 16;
601 ACCsizei pragmaCount;
602 ACCchar * str[pragmaMax];
603 accGetPragmas(mAccScript, &pragmaCount, pragmaMax, &str[0]);
604
Jason Samsda423d82009-06-09 12:15:30 -0700605 for (int ct=0; ct < pragmaCount; ct+=2) {
Jason Samsda423d82009-06-09 12:15:30 -0700606 if (!strcmp(str[ct], "version")) {
607 continue;
Jason Samsda423d82009-06-09 12:15:30 -0700608 }
609
Jason Samsda423d82009-06-09 12:15:30 -0700610 if (!strcmp(str[ct], "stateVertex")) {
Jason Sams9c54bdb2009-06-17 16:52:59 -0700611 if (!strcmp(str[ct+1], "default")) {
612 continue;
613 }
614 if (!strcmp(str[ct+1], "parent")) {
615 mEnviroment.mVertex.clear();
616 continue;
617 }
618 ProgramVertex * pv = (ProgramVertex *)rsc->lookupName(str[ct+1]);
619 if (pv != NULL) {
620 mEnviroment.mVertex.set(pv);
621 continue;
622 }
Jason Samsda423d82009-06-09 12:15:30 -0700623 LOGE("Unreconized value %s passed to stateVertex", str[ct+1]);
624 }
625
626 if (!strcmp(str[ct], "stateRaster")) {
Jason Samsda423d82009-06-09 12:15:30 -0700627 LOGE("Unreconized value %s passed to stateRaster", str[ct+1]);
628 }
629
630 if (!strcmp(str[ct], "stateFragment")) {
Jason Sams9c54bdb2009-06-17 16:52:59 -0700631 if (!strcmp(str[ct+1], "default")) {
632 continue;
633 }
634 if (!strcmp(str[ct+1], "parent")) {
635 mEnviroment.mFragment.clear();
636 continue;
637 }
638 ProgramFragment * pf = (ProgramFragment *)rsc->lookupName(str[ct+1]);
Jason Sams3eaa3382009-06-10 15:04:38 -0700639 if (pf != NULL) {
640 mEnviroment.mFragment.set(pf);
Jason Samsda423d82009-06-09 12:15:30 -0700641 continue;
642 }
643 LOGE("Unreconized value %s passed to stateFragment", str[ct+1]);
644 }
645
646 if (!strcmp(str[ct], "stateFragmentStore")) {
Jason Sams9c54bdb2009-06-17 16:52:59 -0700647 if (!strcmp(str[ct+1], "default")) {
648 continue;
649 }
650 if (!strcmp(str[ct+1], "parent")) {
651 mEnviroment.mFragmentStore.clear();
652 continue;
653 }
Jason Sams3eaa3382009-06-10 15:04:38 -0700654 ProgramFragmentStore * pfs =
655 (ProgramFragmentStore *)rsc->lookupName(str[ct+1]);
656 if (pfs != NULL) {
657 mEnviroment.mFragmentStore.set(pfs);
Jason Samsda423d82009-06-09 12:15:30 -0700658 continue;
659 }
Jason Samsda423d82009-06-09 12:15:30 -0700660 LOGE("Unreconized value %s passed to stateFragmentStore", str[ct+1]);
661 }
662
663 }
664
665
666 } else {
667 // Deal with an error.
668 }
669
Jason Samsd19f10d2009-05-22 14:03:28 -0700670}
671
672namespace android {
673namespace renderscript {
674
675void rsi_ScriptCBegin(Context * rsc)
676{
677 ScriptCState *ss = &rsc->mScriptC;
678 ss->clear();
679}
680
681void rsi_ScriptCSetClearColor(Context * rsc, float r, float g, float b, float a)
682{
683 ScriptCState *ss = &rsc->mScriptC;
Jason Sams3a833d82009-06-08 15:20:31 -0700684 ss->mEnviroment.mClearColor[0] = r;
685 ss->mEnviroment.mClearColor[1] = g;
686 ss->mEnviroment.mClearColor[2] = b;
687 ss->mEnviroment.mClearColor[3] = a;
Jason Samsd19f10d2009-05-22 14:03:28 -0700688}
689
690void rsi_ScriptCSetClearDepth(Context * rsc, float v)
691{
692 ScriptCState *ss = &rsc->mScriptC;
Jason Sams3a833d82009-06-08 15:20:31 -0700693 ss->mEnviroment.mClearDepth = v;
Jason Samsd19f10d2009-05-22 14:03:28 -0700694}
695
696void rsi_ScriptCSetClearStencil(Context * rsc, uint32_t v)
697{
698 ScriptCState *ss = &rsc->mScriptC;
Jason Sams3a833d82009-06-08 15:20:31 -0700699 ss->mEnviroment.mClearStencil = v;
Jason Samsd19f10d2009-05-22 14:03:28 -0700700}
701
702void rsi_ScriptCAddType(Context * rsc, RsType vt)
703{
704 ScriptCState *ss = &rsc->mScriptC;
705 ss->mConstantBufferTypes.add(static_cast<const Type *>(vt));
706}
707
Jason Sams3a833d82009-06-08 15:20:31 -0700708void rsi_ScriptCSetScript(Context * rsc, void *vp)
Jason Samsd19f10d2009-05-22 14:03:28 -0700709{
710 ScriptCState *ss = &rsc->mScriptC;
Jason Sams3a833d82009-06-08 15:20:31 -0700711 ss->mProgram.mScript = reinterpret_cast<rsc_RunScript>(vp);
Jason Samsd19f10d2009-05-22 14:03:28 -0700712}
713
714void rsi_ScriptCSetRoot(Context * rsc, bool isRoot)
715{
716 ScriptCState *ss = &rsc->mScriptC;
Jason Sams3a833d82009-06-08 15:20:31 -0700717 ss->mEnviroment.mIsRoot = isRoot;
Jason Samsd19f10d2009-05-22 14:03:28 -0700718}
719
Jason Sams39ddc9502009-06-05 17:35:09 -0700720void rsi_ScriptCSetText(Context *rsc, const char *text, uint32_t len)
721{
722 ScriptCState *ss = &rsc->mScriptC;
Jason Sams3a833d82009-06-08 15:20:31 -0700723 ss->mProgram.mScriptText = text;
724 ss->mProgram.mScriptTextLength = len;
Jason Sams39ddc9502009-06-05 17:35:09 -0700725}
726
727
Jason Samsd19f10d2009-05-22 14:03:28 -0700728RsScript rsi_ScriptCCreate(Context * rsc)
729{
730 ScriptCState *ss = &rsc->mScriptC;
731
Jason Sams3eaa3382009-06-10 15:04:38 -0700732 ss->runCompiler(rsc);
Jason Sams39ddc9502009-06-05 17:35:09 -0700733
Jason Samsd19f10d2009-05-22 14:03:28 -0700734 ScriptC *s = new ScriptC();
Jason Sams3a833d82009-06-08 15:20:31 -0700735 s->incRef();
Jack Palevichec5a20b2009-05-28 15:53:04 -0700736 s->mAccScript = ss->mAccScript;
737 ss->mAccScript = NULL;
Jason Sams3a833d82009-06-08 15:20:31 -0700738 s->mEnviroment = ss->mEnviroment;
739 s->mProgram = ss->mProgram;
740 ss->clear();
Jason Samsda423d82009-06-09 12:15:30 -0700741
Jason Samsd19f10d2009-05-22 14:03:28 -0700742 return s;
743}
744
745}
746}
747
748