blob: a7877cdbcb010d7f9d79502be54cad9fcedb4713 [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
21#include "acc/acc.h"
22#include "utils/Timers.h"
23
24#define GL_GLEXT_PROTOTYPES
25
26#include <GLES/gl.h>
27#include <GLES/glext.h>
28#include <GLES2/gl2.h>
29#include <GLES2/gl2ext.h>
30
31#include <time.h>
32
33using namespace android;
34using namespace android::renderscript;
35
36#define GET_TLS() Context::ScriptTLSStruct * tls = \
37 (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
38 Context * rsc = tls->mContext; \
39 ScriptC * sc = (ScriptC *) tls->mScript
40
41
42//////////////////////////////////////////////////////////////////////////////
Jason Samsaeb094b2010-05-18 13:35:45 -070043// Context
44//////////////////////////////////////////////////////////////////////////////
45
46static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
47{
48 GET_TLS();
49 rsi_ProgramBindTexture(rsc,
50 static_cast<ProgramFragment *>(vpf),
51 slot,
52 static_cast<Allocation *>(va));
53
54}
55
56static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
57{
58 GET_TLS();
59 rsi_ProgramBindSampler(rsc,
60 static_cast<ProgramFragment *>(vpf),
61 slot,
62 static_cast<Sampler *>(vs));
63
64}
65
66static void SC_bindProgramStore(RsProgramStore pfs)
67{
68 GET_TLS();
69 rsi_ContextBindProgramStore(rsc, pfs);
70}
71
72static void SC_bindProgramFragment(RsProgramFragment pf)
73{
74 GET_TLS();
75 rsi_ContextBindProgramFragment(rsc, pf);
76}
77
78static void SC_bindProgramVertex(RsProgramVertex pv)
79{
80 GET_TLS();
81 rsi_ContextBindProgramVertex(rsc, pv);
82}
83
84static void SC_bindProgramRaster(RsProgramRaster pv)
85{
86 GET_TLS();
87 rsi_ContextBindProgramRaster(rsc, pv);
88}
89
90//////////////////////////////////////////////////////////////////////////////
91// VP
92//////////////////////////////////////////////////////////////////////////////
93
Jim Millera490f102010-07-28 14:46:22 -070094static void SC_vpLoadProjectionMatrix(const rsc_Matrix *m)
95{
96 GET_TLS();
97 rsc->getVertex()->setProjectionMatrix(m);
98}
99
Jason Samsaeb094b2010-05-18 13:35:45 -0700100static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
101{
102 GET_TLS();
103 rsc->getVertex()->setModelviewMatrix(m);
104}
105
106static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
107{
108 GET_TLS();
109 rsc->getVertex()->setTextureMatrix(m);
110}
111
112
Jason Sams6445e522010-08-04 17:50:20 -0700113static void SC_pfConstantColor(RsProgramFragment vpf, float r, float g, float b, float a)
114{
115 //GET_TLS();
116 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
117 pf->setConstantColor(r, g, b, a);
118}
119
Alex Sakhartchouk95333f92010-08-16 17:40:10 -0700120static void SC_vpGetProjectionMatrix(rsc_Matrix *m)
121{
122 GET_TLS();
123 rsc->getVertex()->getProjectionMatrix(m);
124}
125
Jason Samsaeb094b2010-05-18 13:35:45 -0700126
127//////////////////////////////////////////////////////////////////////////////
128// Drawing
129//////////////////////////////////////////////////////////////////////////////
130
Jason Samsaeb094b2010-05-18 13:35:45 -0700131static void SC_drawQuadTexCoords(float x1, float y1, float z1,
132 float u1, float v1,
133 float x2, float y2, float z2,
134 float u2, float v2,
135 float x3, float y3, float z3,
136 float u3, float v3,
137 float x4, float y4, float z4,
138 float u4, float v4)
139{
140 GET_TLS();
141 if (!rsc->setupCheck()) {
142 return;
143 }
144
145 //LOGE("Quad");
146 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
147 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
148 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
149 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
150
151 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
152 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
153
154 VertexArray va;
Jason Sams79f52df2010-06-01 15:47:01 -0700155 va.add(GL_FLOAT, 3, 12, false, (uint32_t)vtx, "position");
156 va.add(GL_FLOAT, 2, 8, false, (uint32_t)tex, "texture0");
157 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsaeb094b2010-05-18 13:35:45 -0700158
159 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
160}
161
162static void SC_drawQuad(float x1, float y1, float z1,
163 float x2, float y2, float z2,
164 float x3, float y3, float z3,
165 float x4, float y4, float z4)
166{
167 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
168 x2, y2, z2, 1, 1,
169 x3, y3, z3, 1, 0,
170 x4, y4, z4, 0, 0);
171}
172
173static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h)
174{
175 GET_TLS();
176 ObjectBaseRef<const ProgramVertex> tmp(rsc->getVertex());
177 rsc->setVertex(rsc->getDefaultProgramVertex());
178 //rsc->setupCheck();
179
180 //GLint crop[4] = {0, h, w, -h};
181
182 float sh = rsc->getHeight();
183
184 SC_drawQuad(x, sh - y, z,
185 x+w, sh - y, z,
186 x+w, sh - (y+h), z,
187 x, sh - (y+h), z);
188 rsc->setVertex((ProgramVertex *)tmp.get());
189}
Jason Samsbdb04602010-06-17 18:05:38 -0700190/*
Jason Samsaeb094b2010-05-18 13:35:45 -0700191static void SC_drawSprite(float x, float y, float z, float w, float h)
192{
193 GET_TLS();
194 float vin[3] = {x, y, z};
195 float vout[4];
196
197 //LOGE("ds in %f %f %f", x, y, z);
198 rsc->getVertex()->transformToScreen(rsc, vout, vin);
199 //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]);
200 vout[0] /= vout[3];
201 vout[1] /= vout[3];
202 vout[2] /= vout[3];
203
204 vout[0] *= rsc->getWidth() / 2;
205 vout[1] *= rsc->getHeight() / 2;
206 vout[0] += rsc->getWidth() / 2;
207 vout[1] += rsc->getHeight() / 2;
208
209 vout[0] -= w/2;
210 vout[1] -= h/2;
211
212 //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]);
213
214 // U, V, W, H
215 SC_drawSpriteScreenspace(vout[0], vout[1], z, h, w);
216 //rsc->setupCheck();
217}
Jason Samsbdb04602010-06-17 18:05:38 -0700218*/
Jason Samsaeb094b2010-05-18 13:35:45 -0700219
220static void SC_drawRect(float x1, float y1,
221 float x2, float y2, float z)
222{
223 //LOGE("SC_drawRect %f,%f %f,%f %f", x1, y1, x2, y2, z);
224 SC_drawQuad(x1, y2, z,
225 x2, y2, z,
226 x2, y1, z,
227 x1, y1, z);
228}
229
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700230static void SC_drawMesh(RsMesh vsm)
231{
232 GET_TLS();
233 Mesh *sm = static_cast<Mesh *>(vsm);
Jason Samsaeb094b2010-05-18 13:35:45 -0700234 if (!rsc->setupCheck()) {
235 return;
236 }
237 sm->render(rsc);
238}
239
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700240static void SC_drawMeshPrimitive(RsMesh vsm, uint32_t primIndex)
Jason Samsaeb094b2010-05-18 13:35:45 -0700241{
242 GET_TLS();
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700243 Mesh *sm = static_cast<Mesh *>(vsm);
Jason Samsaeb094b2010-05-18 13:35:45 -0700244 if (!rsc->setupCheck()) {
245 return;
246 }
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700247 sm->renderPrimitive(rsc, primIndex);
248}
249
250static void SC_drawMeshPrimitiveRange(RsMesh vsm, uint32_t primIndex, uint32_t start, uint32_t len)
251{
252 GET_TLS();
253 Mesh *sm = static_cast<Mesh *>(vsm);
254 if (!rsc->setupCheck()) {
255 return;
256 }
257 sm->renderPrimitiveRange(rsc, primIndex, start, len);
Jason Samsaeb094b2010-05-18 13:35:45 -0700258}
259
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700260static void SC_meshComputeBoundingBox(RsMesh vsm, float *minX, float *minY, float *minZ,
261 float *maxX, float *maxY, float *maxZ)
262{
263 GET_TLS();
264 Mesh *sm = static_cast<Mesh *>(vsm);
265 sm->computeBBox();
266 *minX = sm->mBBoxMin[0];
267 *minY = sm->mBBoxMin[1];
268 *minZ = sm->mBBoxMin[2];
269 *maxX = sm->mBBoxMax[0];
270 *maxY = sm->mBBoxMax[1];
271 *maxZ = sm->mBBoxMax[2];
272}
273
Jason Samsaeb094b2010-05-18 13:35:45 -0700274
275//////////////////////////////////////////////////////////////////////////////
276//
277//////////////////////////////////////////////////////////////////////////////
278
279
280static void SC_color(float r, float g, float b, float a)
281{
282 GET_TLS();
Jason Sams6445e522010-08-04 17:50:20 -0700283 ProgramFragment *pf = (ProgramFragment *)rsc->getFragment();
284 pf->setConstantColor(r, g, b, a);
Jason Samsaeb094b2010-05-18 13:35:45 -0700285}
286
Jason Sams22fa3712010-05-19 17:22:57 -0700287static void SC_uploadToTexture2(RsAllocation va, uint32_t baseMipLevel)
Jason Samsaeb094b2010-05-18 13:35:45 -0700288{
289 GET_TLS();
290 rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel);
291}
Jason Sams22fa3712010-05-19 17:22:57 -0700292static void SC_uploadToTexture(RsAllocation va)
293{
294 GET_TLS();
295 rsi_AllocationUploadToTexture(rsc, va, false, 0);
296}
Jason Samsaeb094b2010-05-18 13:35:45 -0700297
298static void SC_uploadToBufferObject(RsAllocation va)
299{
300 GET_TLS();
301 rsi_AllocationUploadToBufferObject(rsc, va);
302}
303
Jason Samsaeb094b2010-05-18 13:35:45 -0700304static void SC_ClearColor(float r, float g, float b, float a)
305{
Jason Samsaeb094b2010-05-18 13:35:45 -0700306 GET_TLS();
Jason Sams22fa3712010-05-19 17:22:57 -0700307 if (!rsc->setupCheck()) {
308 return;
309 }
310
311 glClearColor(r, g, b, a);
312 glClear(GL_COLOR_BUFFER_BIT);
313}
314
315static void SC_ClearDepth(float v)
316{
317 GET_TLS();
318 if (!rsc->setupCheck()) {
319 return;
320 }
321
322 glClearDepthf(v);
323 glClear(GL_DEPTH_BUFFER_BIT);
Jason Samsaeb094b2010-05-18 13:35:45 -0700324}
325
326static uint32_t SC_getWidth()
327{
328 GET_TLS();
329 return rsc->getWidth();
330}
331
332static uint32_t SC_getHeight()
333{
334 GET_TLS();
335 return rsc->getHeight();
336}
337
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700338static void SC_DrawTextAlloc(RsAllocation va, int x, int y)
339{
340 GET_TLS();
341 Allocation *alloc = static_cast<Allocation *>(va);
342 rsc->mStateFont.renderText(alloc, x, y);
343}
344
345static void SC_DrawText(const char *text, int x, int y)
346{
347 GET_TLS();
348 rsc->mStateFont.renderText(text, x, y);
349}
350
351static void SC_BindFont(RsFont font)
352{
353 GET_TLS();
354 rsi_ContextBindFont(rsc, font);
355}
Jason Samsaeb094b2010-05-18 13:35:45 -0700356
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700357static void SC_FontColor(float r, float g, float b, float a)
358{
359 GET_TLS();
360 rsc->mStateFont.setFontColor(r, g, b, a);
361}
362
Jason Samsaeb094b2010-05-18 13:35:45 -0700363//////////////////////////////////////////////////////////////////////////////
364// Class implementation
365//////////////////////////////////////////////////////////////////////////////
366
367// llvm name mangling ref
368// <builtin-type> ::= v # void
369// ::= b # bool
370// ::= c # char
371// ::= a # signed char
372// ::= h # unsigned char
373// ::= s # short
374// ::= t # unsigned short
375// ::= i # int
376// ::= j # unsigned int
377// ::= l # long
378// ::= m # unsigned long
379// ::= x # long long, __int64
380// ::= y # unsigned long long, __int64
381// ::= f # float
382// ::= d # double
383
384static ScriptCState::SymbolTable_t gSyms[] = {
Jason Sams73495472010-07-29 17:31:14 -0700385 { "_Z22rsgBindProgramFragment19rs_program_fragment", (void *)&SC_bindProgramFragment },
386 { "_Z19rsgBindProgramStore16rs_program_store", (void *)&SC_bindProgramStore },
387 { "_Z20rsgBindProgramVertex17rs_program_vertex", (void *)&SC_bindProgramVertex },
388 { "_Z20rsgBindProgramRaster17rs_program_raster", (void *)&SC_bindProgramRaster },
389 { "_Z14rsgBindSampler19rs_program_fragmentj10rs_sampler", (void *)&SC_bindSampler },
390 { "_Z14rsgBindTexture19rs_program_fragmentj13rs_allocation", (void *)&SC_bindTexture },
Jason Sams22fa3712010-05-19 17:22:57 -0700391
Jason Sams73495472010-07-29 17:31:14 -0700392 { "_Z36rsgProgramVertexLoadProjectionMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadProjectionMatrix },
393 { "_Z31rsgProgramVertexLoadModelMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadModelMatrix },
394 { "_Z33rsgProgramVertexLoadTextureMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadTextureMatrix },
Jason Sams22fa3712010-05-19 17:22:57 -0700395
Alex Sakhartchouk95333f92010-08-16 17:40:10 -0700396 { "_Z35rsgProgramVertexGetProjectionMatrixP12rs_matrix4x4", (void *)&SC_vpGetProjectionMatrix },
397
Jason Sams6445e522010-08-04 17:50:20 -0700398 { "_Z31rsgProgramFragmentConstantColor19rs_program_fragmentffff", (void *)&SC_pfConstantColor },
399
Jason Sams73495472010-07-29 17:31:14 -0700400 { "_Z11rsgGetWidthv", (void *)&SC_getWidth },
401 { "_Z12rsgGetHeightv", (void *)&SC_getHeight },
Jason Sams22fa3712010-05-19 17:22:57 -0700402
Jason Sams73495472010-07-29 17:31:14 -0700403 { "_Z18rsgUploadToTexture13rs_allocationj", (void *)&SC_uploadToTexture2 },
Jason Sams8c880902010-06-15 12:15:57 -0700404 { "_Z18rsgUploadToTexture13rs_allocation", (void *)&SC_uploadToTexture },
Jason Sams73495472010-07-29 17:31:14 -0700405 { "_Z23rsgUploadToBufferObject13rs_allocation", (void *)&SC_uploadToBufferObject },
Jason Sams22fa3712010-05-19 17:22:57 -0700406
Jason Sams73495472010-07-29 17:31:14 -0700407 { "_Z11rsgDrawRectfffff", (void *)&SC_drawRect },
408 { "_Z11rsgDrawQuadffffffffffff", (void *)&SC_drawQuad },
409 { "_Z20rsgDrawQuadTexCoordsffffffffffffffffffff", (void *)&SC_drawQuadTexCoords },
410 { "_Z24rsgDrawSpriteScreenspacefffff", (void *)&SC_drawSpriteScreenspace },
Jason Sams22fa3712010-05-19 17:22:57 -0700411
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700412 { "_Z11rsgDrawMesh7rs_mesh", (void *)&SC_drawMesh },
Jason Sams73495472010-07-29 17:31:14 -0700413 { "_Z11rsgDrawMesh7rs_meshj", (void *)&SC_drawMeshPrimitive },
414 { "_Z11rsgDrawMesh7rs_meshjjj", (void *)&SC_drawMeshPrimitiveRange },
Alex Sakhartchoukba4aa5c2010-08-13 14:32:23 -0700415 { "_Z25rsgMeshComputeBoundingBox7rs_meshPfS0_S0_S0_S0_S0_", (void *)&SC_meshComputeBoundingBox },
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700416
Jason Sams73495472010-07-29 17:31:14 -0700417 { "_Z13rsgClearColorffff", (void *)&SC_ClearColor },
418 { "_Z13rsgClearDepthf", (void *)&SC_ClearDepth },
Jason Sams22fa3712010-05-19 17:22:57 -0700419
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700420 { "_Z11rsgDrawTextPKcii", (void *)&SC_DrawText },
421 { "_Z11rsgDrawText13rs_allocationii", (void *)&SC_DrawTextAlloc },
422
Jason Sams73495472010-07-29 17:31:14 -0700423 { "_Z11rsgBindFont7rs_font", (void *)&SC_BindFont },
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700424 { "_Z12rsgFontColorffff", (void *)&SC_FontColor },
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700425
Jason Samsaeb094b2010-05-18 13:35:45 -0700426 // misc
Jason Sams73495472010-07-29 17:31:14 -0700427 { "_Z5colorffff", (void *)&SC_color },
Jason Samsaeb094b2010-05-18 13:35:45 -0700428
Jason Samsaeb094b2010-05-18 13:35:45 -0700429 { NULL, NULL }
430};
431
432const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbolGL(const char *sym)
433{
434 ScriptCState::SymbolTable_t *syms = gSyms;
435
436 while (syms->mPtr) {
437 if (!strcmp(syms->mName, sym)) {
438 return syms;
439 }
440 syms++;
441 }
442 return NULL;
443}
444