blob: fd4c379cb9041cf7b0b7d59f3a75942bf302ef20 [file] [log] [blame]
Jason Samsaeb094b2010-05-18 13:35:45 -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"
Jason Samsaeb094b2010-05-18 13:35:45 -070020
Jason Samsaeb094b2010-05-18 13:35:45 -070021#include "utils/Timers.h"
22
23#define GL_GLEXT_PROTOTYPES
24
25#include <GLES/gl.h>
26#include <GLES/glext.h>
27#include <GLES2/gl2.h>
28#include <GLES2/gl2ext.h>
29
30#include <time.h>
31
32using namespace android;
33using namespace android::renderscript;
34
35#define GET_TLS() Context::ScriptTLSStruct * tls = \
36 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
37 Context * rsc = tls->mContext; \
38 ScriptC * sc = (ScriptC *) tls->mScript
39
40
41//////////////////////////////////////////////////////////////////////////////
Jason Samsaeb094b2010-05-18 13:35:45 -070042// Context
43//////////////////////////////////////////////////////////////////////////////
44
45static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
46{
47 GET_TLS();
48 rsi_ProgramBindTexture(rsc,
49 static_cast<ProgramFragment *>(vpf),
50 slot,
51 static_cast<Allocation *>(va));
52
53}
54
55static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
56{
57 GET_TLS();
58 rsi_ProgramBindSampler(rsc,
59 static_cast<ProgramFragment *>(vpf),
60 slot,
61 static_cast<Sampler *>(vs));
62
63}
64
65static void SC_bindProgramStore(RsProgramStore pfs)
66{
67 GET_TLS();
68 rsi_ContextBindProgramStore(rsc, pfs);
69}
70
71static void SC_bindProgramFragment(RsProgramFragment pf)
72{
73 GET_TLS();
74 rsi_ContextBindProgramFragment(rsc, pf);
75}
76
77static void SC_bindProgramVertex(RsProgramVertex pv)
78{
79 GET_TLS();
80 rsi_ContextBindProgramVertex(rsc, pv);
81}
82
83static void SC_bindProgramRaster(RsProgramRaster pv)
84{
85 GET_TLS();
86 rsi_ContextBindProgramRaster(rsc, pv);
87}
88
89//////////////////////////////////////////////////////////////////////////////
90// VP
91//////////////////////////////////////////////////////////////////////////////
92
Jim Millera490f102010-07-28 14:46:22 -070093static void SC_vpLoadProjectionMatrix(const rsc_Matrix *m)
94{
95 GET_TLS();
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -070096 rsc->getVertex()->setProjectionMatrix(rsc, m);
Jim Millera490f102010-07-28 14:46:22 -070097}
98
Jason Samsaeb094b2010-05-18 13:35:45 -070099static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
100{
101 GET_TLS();
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700102 rsc->getVertex()->setModelviewMatrix(rsc, m);
Jason Samsaeb094b2010-05-18 13:35:45 -0700103}
104
105static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
106{
107 GET_TLS();
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700108 rsc->getVertex()->setTextureMatrix(rsc, m);
Jason Samsaeb094b2010-05-18 13:35:45 -0700109}
110
111
Jason Sams6445e522010-08-04 17:50:20 -0700112static void SC_pfConstantColor(RsProgramFragment vpf, float r, float g, float b, float a)
113{
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700114 GET_TLS();
Jason Sams6445e522010-08-04 17:50:20 -0700115 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700116 pf->setConstantColor(rsc, r, g, b, a);
Jason Sams6445e522010-08-04 17:50:20 -0700117}
118
Alex Sakhartchouk95333f92010-08-16 17:40:10 -0700119static void SC_vpGetProjectionMatrix(rsc_Matrix *m)
120{
121 GET_TLS();
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700122 rsc->getVertex()->getProjectionMatrix(rsc, m);
Alex Sakhartchouk95333f92010-08-16 17:40:10 -0700123}
124
Jason Samsaeb094b2010-05-18 13:35:45 -0700125
126//////////////////////////////////////////////////////////////////////////////
127// Drawing
128//////////////////////////////////////////////////////////////////////////////
129
Jason Samsaeb094b2010-05-18 13:35:45 -0700130static void SC_drawQuadTexCoords(float x1, float y1, float z1,
131 float u1, float v1,
132 float x2, float y2, float z2,
133 float u2, float v2,
134 float x3, float y3, float z3,
135 float u3, float v3,
136 float x4, float y4, float z4,
137 float u4, float v4)
138{
139 GET_TLS();
140 if (!rsc->setupCheck()) {
141 return;
142 }
143
144 //LOGE("Quad");
145 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
146 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
147 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
148 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
149
150 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
151 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
152
153 VertexArray va;
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700154 va.add(GL_FLOAT, 3, 12, false, (uint32_t)vtx, "ATTRIB_position");
155 va.add(GL_FLOAT, 2, 8, false, (uint32_t)tex, "ATTRIB_texture0");
Jason Sams79f52df2010-06-01 15:47:01 -0700156 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsaeb094b2010-05-18 13:35:45 -0700157
158 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
159}
160
161static void SC_drawQuad(float x1, float y1, float z1,
162 float x2, float y2, float z2,
163 float x3, float y3, float z3,
164 float x4, float y4, float z4)
165{
166 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
167 x2, y2, z2, 1, 1,
168 x3, y3, z3, 1, 0,
169 x4, y4, z4, 0, 0);
170}
171
172static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h)
173{
174 GET_TLS();
175 ObjectBaseRef<const ProgramVertex> tmp(rsc->getVertex());
176 rsc->setVertex(rsc->getDefaultProgramVertex());
177 //rsc->setupCheck();
178
179 //GLint crop[4] = {0, h, w, -h};
180
181 float sh = rsc->getHeight();
182
183 SC_drawQuad(x, sh - y, z,
184 x+w, sh - y, z,
185 x+w, sh - (y+h), z,
186 x, sh - (y+h), z);
187 rsc->setVertex((ProgramVertex *)tmp.get());
188}
Jason Samsbdb04602010-06-17 18:05:38 -0700189/*
Jason Samsaeb094b2010-05-18 13:35:45 -0700190static void SC_drawSprite(float x, float y, float z, float w, float h)
191{
192 GET_TLS();
193 float vin[3] = {x, y, z};
194 float vout[4];
195
196 //LOGE("ds in %f %f %f", x, y, z);
197 rsc->getVertex()->transformToScreen(rsc, vout, vin);
198 //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]);
199 vout[0] /= vout[3];
200 vout[1] /= vout[3];
201 vout[2] /= vout[3];
202
203 vout[0] *= rsc->getWidth() / 2;
204 vout[1] *= rsc->getHeight() / 2;
205 vout[0] += rsc->getWidth() / 2;
206 vout[1] += rsc->getHeight() / 2;
207
208 vout[0] -= w/2;
209 vout[1] -= h/2;
210
211 //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]);
212
213 // U, V, W, H
214 SC_drawSpriteScreenspace(vout[0], vout[1], z, h, w);
215 //rsc->setupCheck();
216}
Jason Samsbdb04602010-06-17 18:05:38 -0700217*/
Jason Samsaeb094b2010-05-18 13:35:45 -0700218
219static void SC_drawRect(float x1, float y1,
220 float x2, float y2, float z)
221{
222 //LOGE("SC_drawRect %f,%f %f,%f %f", x1, y1, x2, y2, z);
223 SC_drawQuad(x1, y2, z,
224 x2, y2, z,
225 x2, y1, z,
226 x1, y1, z);
227}
228
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700229static void SC_drawMesh(RsMesh vsm)
230{
231 GET_TLS();
232 Mesh *sm = static_cast<Mesh *>(vsm);
Jason Samsaeb094b2010-05-18 13:35:45 -0700233 if (!rsc->setupCheck()) {
234 return;
235 }
236 sm->render(rsc);
237}
238
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700239static void SC_drawMeshPrimitive(RsMesh vsm, uint32_t primIndex)
Jason Samsaeb094b2010-05-18 13:35:45 -0700240{
241 GET_TLS();
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700242 Mesh *sm = static_cast<Mesh *>(vsm);
Jason Samsaeb094b2010-05-18 13:35:45 -0700243 if (!rsc->setupCheck()) {
244 return;
245 }
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700246 sm->renderPrimitive(rsc, primIndex);
247}
248
249static void SC_drawMeshPrimitiveRange(RsMesh vsm, uint32_t primIndex, uint32_t start, uint32_t len)
250{
251 GET_TLS();
252 Mesh *sm = static_cast<Mesh *>(vsm);
253 if (!rsc->setupCheck()) {
254 return;
255 }
256 sm->renderPrimitiveRange(rsc, primIndex, start, len);
Jason Samsaeb094b2010-05-18 13:35:45 -0700257}
258
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700259static void SC_meshComputeBoundingBox(RsMesh vsm, float *minX, float *minY, float *minZ,
260 float *maxX, float *maxY, float *maxZ)
261{
262 GET_TLS();
263 Mesh *sm = static_cast<Mesh *>(vsm);
264 sm->computeBBox();
265 *minX = sm->mBBoxMin[0];
266 *minY = sm->mBBoxMin[1];
267 *minZ = sm->mBBoxMin[2];
268 *maxX = sm->mBBoxMax[0];
269 *maxY = sm->mBBoxMax[1];
270 *maxZ = sm->mBBoxMax[2];
271}
272
Jason Samsaeb094b2010-05-18 13:35:45 -0700273
274//////////////////////////////////////////////////////////////////////////////
275//
276//////////////////////////////////////////////////////////////////////////////
277
278
279static void SC_color(float r, float g, float b, float a)
280{
281 GET_TLS();
Jason Sams6445e522010-08-04 17:50:20 -0700282 ProgramFragment *pf = (ProgramFragment *)rsc->getFragment();
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700283 pf->setConstantColor(rsc, r, g, b, a);
Jason Samsaeb094b2010-05-18 13:35:45 -0700284}
285
Jason Sams22fa3712010-05-19 17:22:57 -0700286static void SC_uploadToTexture2(RsAllocation va, uint32_t baseMipLevel)
Jason Samsaeb094b2010-05-18 13:35:45 -0700287{
288 GET_TLS();
289 rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel);
290}
Jason Sams22fa3712010-05-19 17:22:57 -0700291static void SC_uploadToTexture(RsAllocation va)
292{
293 GET_TLS();
294 rsi_AllocationUploadToTexture(rsc, va, false, 0);
295}
Jason Samsaeb094b2010-05-18 13:35:45 -0700296
297static void SC_uploadToBufferObject(RsAllocation va)
298{
299 GET_TLS();
300 rsi_AllocationUploadToBufferObject(rsc, va);
301}
302
Jason Samsaeb094b2010-05-18 13:35:45 -0700303static void SC_ClearColor(float r, float g, float b, float a)
304{
Jason Samsaeb094b2010-05-18 13:35:45 -0700305 GET_TLS();
Jason Sams22fa3712010-05-19 17:22:57 -0700306 if (!rsc->setupCheck()) {
307 return;
308 }
309
310 glClearColor(r, g, b, a);
311 glClear(GL_COLOR_BUFFER_BIT);
312}
313
314static void SC_ClearDepth(float v)
315{
316 GET_TLS();
317 if (!rsc->setupCheck()) {
318 return;
319 }
320
321 glClearDepthf(v);
322 glClear(GL_DEPTH_BUFFER_BIT);
Jason Samsaeb094b2010-05-18 13:35:45 -0700323}
324
325static uint32_t SC_getWidth()
326{
327 GET_TLS();
328 return rsc->getWidth();
329}
330
331static uint32_t SC_getHeight()
332{
333 GET_TLS();
334 return rsc->getHeight();
335}
336
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700337static void SC_DrawTextAlloc(RsAllocation va, int x, int y)
338{
339 GET_TLS();
340 Allocation *alloc = static_cast<Allocation *>(va);
341 rsc->mStateFont.renderText(alloc, x, y);
342}
343
344static void SC_DrawText(const char *text, int x, int y)
345{
346 GET_TLS();
347 rsc->mStateFont.renderText(text, x, y);
348}
349
350static void SC_BindFont(RsFont font)
351{
352 GET_TLS();
353 rsi_ContextBindFont(rsc, font);
354}
Jason Samsaeb094b2010-05-18 13:35:45 -0700355
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700356static void SC_FontColor(float r, float g, float b, float a)
357{
358 GET_TLS();
359 rsc->mStateFont.setFontColor(r, g, b, a);
360}
361
Jason Samsaeb094b2010-05-18 13:35:45 -0700362//////////////////////////////////////////////////////////////////////////////
363// Class implementation
364//////////////////////////////////////////////////////////////////////////////
365
366// llvm name mangling ref
367// <builtin-type> ::= v # void
368// ::= b # bool
369// ::= c # char
370// ::= a # signed char
371// ::= h # unsigned char
372// ::= s # short
373// ::= t # unsigned short
374// ::= i # int
375// ::= j # unsigned int
376// ::= l # long
377// ::= m # unsigned long
378// ::= x # long long, __int64
379// ::= y # unsigned long long, __int64
380// ::= f # float
381// ::= d # double
382
383static ScriptCState::SymbolTable_t gSyms[] = {
Jason Sams73495472010-07-29 17:31:14 -0700384 { "_Z22rsgBindProgramFragment19rs_program_fragment", (void *)&SC_bindProgramFragment },
385 { "_Z19rsgBindProgramStore16rs_program_store", (void *)&SC_bindProgramStore },
386 { "_Z20rsgBindProgramVertex17rs_program_vertex", (void *)&SC_bindProgramVertex },
387 { "_Z20rsgBindProgramRaster17rs_program_raster", (void *)&SC_bindProgramRaster },
388 { "_Z14rsgBindSampler19rs_program_fragmentj10rs_sampler", (void *)&SC_bindSampler },
389 { "_Z14rsgBindTexture19rs_program_fragmentj13rs_allocation", (void *)&SC_bindTexture },
Jason Sams22fa3712010-05-19 17:22:57 -0700390
Jason Sams73495472010-07-29 17:31:14 -0700391 { "_Z36rsgProgramVertexLoadProjectionMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadProjectionMatrix },
392 { "_Z31rsgProgramVertexLoadModelMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadModelMatrix },
393 { "_Z33rsgProgramVertexLoadTextureMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadTextureMatrix },
Jason Sams22fa3712010-05-19 17:22:57 -0700394
Alex Sakhartchouk95333f92010-08-16 17:40:10 -0700395 { "_Z35rsgProgramVertexGetProjectionMatrixP12rs_matrix4x4", (void *)&SC_vpGetProjectionMatrix },
396
Jason Sams6445e522010-08-04 17:50:20 -0700397 { "_Z31rsgProgramFragmentConstantColor19rs_program_fragmentffff", (void *)&SC_pfConstantColor },
398
Jason Sams73495472010-07-29 17:31:14 -0700399 { "_Z11rsgGetWidthv", (void *)&SC_getWidth },
400 { "_Z12rsgGetHeightv", (void *)&SC_getHeight },
Jason Sams22fa3712010-05-19 17:22:57 -0700401
Jason Sams73495472010-07-29 17:31:14 -0700402 { "_Z18rsgUploadToTexture13rs_allocationj", (void *)&SC_uploadToTexture2 },
Jason Sams8c880902010-06-15 12:15:57 -0700403 { "_Z18rsgUploadToTexture13rs_allocation", (void *)&SC_uploadToTexture },
Jason Sams73495472010-07-29 17:31:14 -0700404 { "_Z23rsgUploadToBufferObject13rs_allocation", (void *)&SC_uploadToBufferObject },
Jason Sams22fa3712010-05-19 17:22:57 -0700405
Jason Sams73495472010-07-29 17:31:14 -0700406 { "_Z11rsgDrawRectfffff", (void *)&SC_drawRect },
407 { "_Z11rsgDrawQuadffffffffffff", (void *)&SC_drawQuad },
408 { "_Z20rsgDrawQuadTexCoordsffffffffffffffffffff", (void *)&SC_drawQuadTexCoords },
409 { "_Z24rsgDrawSpriteScreenspacefffff", (void *)&SC_drawSpriteScreenspace },
Jason Sams22fa3712010-05-19 17:22:57 -0700410
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700411 { "_Z11rsgDrawMesh7rs_mesh", (void *)&SC_drawMesh },
Jason Sams73495472010-07-29 17:31:14 -0700412 { "_Z11rsgDrawMesh7rs_meshj", (void *)&SC_drawMeshPrimitive },
413 { "_Z11rsgDrawMesh7rs_meshjjj", (void *)&SC_drawMeshPrimitiveRange },
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700414 { "_Z25rsgMeshComputeBoundingBox7rs_meshPfS0_S0_S0_S0_S0_", (void *)&SC_meshComputeBoundingBox },
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700415
Jason Sams73495472010-07-29 17:31:14 -0700416 { "_Z13rsgClearColorffff", (void *)&SC_ClearColor },
417 { "_Z13rsgClearDepthf", (void *)&SC_ClearDepth },
Jason Sams22fa3712010-05-19 17:22:57 -0700418
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700419 { "_Z11rsgDrawTextPKcii", (void *)&SC_DrawText },
420 { "_Z11rsgDrawText13rs_allocationii", (void *)&SC_DrawTextAlloc },
421
Jason Sams73495472010-07-29 17:31:14 -0700422 { "_Z11rsgBindFont7rs_font", (void *)&SC_BindFont },
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700423 { "_Z12rsgFontColorffff", (void *)&SC_FontColor },
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700424
Jason Samsaeb094b2010-05-18 13:35:45 -0700425 // misc
Jason Sams73495472010-07-29 17:31:14 -0700426 { "_Z5colorffff", (void *)&SC_color },
Jason Samsaeb094b2010-05-18 13:35:45 -0700427
Jason Samsaeb094b2010-05-18 13:35:45 -0700428 { NULL, NULL }
429};
430
431const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbolGL(const char *sym)
432{
433 ScriptCState::SymbolTable_t *syms = gSyms;
434
435 while (syms->mPtr) {
436 if (!strcmp(syms->mName, sym)) {
437 return syms;
438 }
439 syms++;
440 }
441 return NULL;
442}
443