blob: 4b8de765f92795d31ddb5650bdd5693e364caf5c [file] [log] [blame]
Jason Sams536923d2010-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 Sams536923d2010-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 Sams536923d2010-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 Millerd8e76202010-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 Sams536923d2010-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 Sams442a6472010-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
Jason Sams536923d2010-05-18 13:35:45 -0700120
121//////////////////////////////////////////////////////////////////////////////
122// Drawing
123//////////////////////////////////////////////////////////////////////////////
124
Jason Sams536923d2010-05-18 13:35:45 -0700125static void SC_drawQuadTexCoords(float x1, float y1, float z1,
126 float u1, float v1,
127 float x2, float y2, float z2,
128 float u2, float v2,
129 float x3, float y3, float z3,
130 float u3, float v3,
131 float x4, float y4, float z4,
132 float u4, float v4)
133{
134 GET_TLS();
135 if (!rsc->setupCheck()) {
136 return;
137 }
138
139 //LOGE("Quad");
140 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
141 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
142 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
143 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
144
145 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
146 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
147
148 VertexArray va;
Jason Sams8cb39de2010-06-01 15:47:01 -0700149 va.add(GL_FLOAT, 3, 12, false, (uint32_t)vtx, "position");
150 va.add(GL_FLOAT, 2, 8, false, (uint32_t)tex, "texture0");
151 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Sams536923d2010-05-18 13:35:45 -0700152
153 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
154}
155
156static void SC_drawQuad(float x1, float y1, float z1,
157 float x2, float y2, float z2,
158 float x3, float y3, float z3,
159 float x4, float y4, float z4)
160{
161 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
162 x2, y2, z2, 1, 1,
163 x3, y3, z3, 1, 0,
164 x4, y4, z4, 0, 0);
165}
166
167static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h)
168{
169 GET_TLS();
170 ObjectBaseRef<const ProgramVertex> tmp(rsc->getVertex());
171 rsc->setVertex(rsc->getDefaultProgramVertex());
172 //rsc->setupCheck();
173
174 //GLint crop[4] = {0, h, w, -h};
175
176 float sh = rsc->getHeight();
177
178 SC_drawQuad(x, sh - y, z,
179 x+w, sh - y, z,
180 x+w, sh - (y+h), z,
181 x, sh - (y+h), z);
182 rsc->setVertex((ProgramVertex *)tmp.get());
183}
Jason Sams6d1cf412010-06-17 18:05:38 -0700184/*
Jason Sams536923d2010-05-18 13:35:45 -0700185static void SC_drawSprite(float x, float y, float z, float w, float h)
186{
187 GET_TLS();
188 float vin[3] = {x, y, z};
189 float vout[4];
190
191 //LOGE("ds in %f %f %f", x, y, z);
192 rsc->getVertex()->transformToScreen(rsc, vout, vin);
193 //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]);
194 vout[0] /= vout[3];
195 vout[1] /= vout[3];
196 vout[2] /= vout[3];
197
198 vout[0] *= rsc->getWidth() / 2;
199 vout[1] *= rsc->getHeight() / 2;
200 vout[0] += rsc->getWidth() / 2;
201 vout[1] += rsc->getHeight() / 2;
202
203 vout[0] -= w/2;
204 vout[1] -= h/2;
205
206 //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]);
207
208 // U, V, W, H
209 SC_drawSpriteScreenspace(vout[0], vout[1], z, h, w);
210 //rsc->setupCheck();
211}
Jason Sams6d1cf412010-06-17 18:05:38 -0700212*/
Jason Sams536923d2010-05-18 13:35:45 -0700213
214static void SC_drawRect(float x1, float y1,
215 float x2, float y2, float z)
216{
217 //LOGE("SC_drawRect %f,%f %f,%f %f", x1, y1, x2, y2, z);
218 SC_drawQuad(x1, y2, z,
219 x2, y2, z,
220 x2, y1, z,
221 x1, y1, z);
222}
223
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700224static void SC_drawMesh(RsMesh vsm)
225{
226 GET_TLS();
227 Mesh *sm = static_cast<Mesh *>(vsm);
Jason Sams536923d2010-05-18 13:35:45 -0700228 if (!rsc->setupCheck()) {
229 return;
230 }
231 sm->render(rsc);
232}
233
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700234static void SC_drawMeshPrimitive(RsMesh vsm, uint32_t primIndex)
Jason Sams536923d2010-05-18 13:35:45 -0700235{
236 GET_TLS();
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700237 Mesh *sm = static_cast<Mesh *>(vsm);
Jason Sams536923d2010-05-18 13:35:45 -0700238 if (!rsc->setupCheck()) {
239 return;
240 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700241 sm->renderPrimitive(rsc, primIndex);
242}
243
244static void SC_drawMeshPrimitiveRange(RsMesh vsm, uint32_t primIndex, uint32_t start, uint32_t len)
245{
246 GET_TLS();
247 Mesh *sm = static_cast<Mesh *>(vsm);
248 if (!rsc->setupCheck()) {
249 return;
250 }
251 sm->renderPrimitiveRange(rsc, primIndex, start, len);
Jason Sams536923d2010-05-18 13:35:45 -0700252}
253
Alex Sakhartchouka80145d2010-08-13 14:32:23 -0700254static void SC_meshComputeBoundingBox(RsMesh vsm, float *minX, float *minY, float *minZ,
255 float *maxX, float *maxY, float *maxZ)
256{
257 GET_TLS();
258 Mesh *sm = static_cast<Mesh *>(vsm);
259 sm->computeBBox();
260 *minX = sm->mBBoxMin[0];
261 *minY = sm->mBBoxMin[1];
262 *minZ = sm->mBBoxMin[2];
263 *maxX = sm->mBBoxMax[0];
264 *maxY = sm->mBBoxMax[1];
265 *maxZ = sm->mBBoxMax[2];
266}
267
Jason Sams536923d2010-05-18 13:35:45 -0700268
269//////////////////////////////////////////////////////////////////////////////
270//
271//////////////////////////////////////////////////////////////////////////////
272
273
274static void SC_color(float r, float g, float b, float a)
275{
276 GET_TLS();
Jason Sams442a6472010-08-04 17:50:20 -0700277 ProgramFragment *pf = (ProgramFragment *)rsc->getFragment();
278 pf->setConstantColor(r, g, b, a);
Jason Sams536923d2010-05-18 13:35:45 -0700279}
280
Jason Samsd79b2e92010-05-19 17:22:57 -0700281static void SC_uploadToTexture2(RsAllocation va, uint32_t baseMipLevel)
Jason Sams536923d2010-05-18 13:35:45 -0700282{
283 GET_TLS();
284 rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel);
285}
Jason Samsd79b2e92010-05-19 17:22:57 -0700286static void SC_uploadToTexture(RsAllocation va)
287{
288 GET_TLS();
289 rsi_AllocationUploadToTexture(rsc, va, false, 0);
290}
Jason Sams536923d2010-05-18 13:35:45 -0700291
292static void SC_uploadToBufferObject(RsAllocation va)
293{
294 GET_TLS();
295 rsi_AllocationUploadToBufferObject(rsc, va);
296}
297
Jason Sams536923d2010-05-18 13:35:45 -0700298static void SC_ClearColor(float r, float g, float b, float a)
299{
Jason Sams536923d2010-05-18 13:35:45 -0700300 GET_TLS();
Jason Samsd79b2e92010-05-19 17:22:57 -0700301 if (!rsc->setupCheck()) {
302 return;
303 }
304
305 glClearColor(r, g, b, a);
306 glClear(GL_COLOR_BUFFER_BIT);
307}
308
309static void SC_ClearDepth(float v)
310{
311 GET_TLS();
312 if (!rsc->setupCheck()) {
313 return;
314 }
315
316 glClearDepthf(v);
317 glClear(GL_DEPTH_BUFFER_BIT);
Jason Sams536923d2010-05-18 13:35:45 -0700318}
319
320static uint32_t SC_getWidth()
321{
322 GET_TLS();
323 return rsc->getWidth();
324}
325
326static uint32_t SC_getHeight()
327{
328 GET_TLS();
329 return rsc->getHeight();
330}
331
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700332static void SC_DrawTextAlloc(RsAllocation va, int x, int y)
333{
334 GET_TLS();
335 Allocation *alloc = static_cast<Allocation *>(va);
336 rsc->mStateFont.renderText(alloc, x, y);
337}
338
339static void SC_DrawText(const char *text, int x, int y)
340{
341 GET_TLS();
342 rsc->mStateFont.renderText(text, x, y);
343}
344
345static void SC_BindFont(RsFont font)
346{
347 GET_TLS();
348 rsi_ContextBindFont(rsc, font);
349}
Jason Sams536923d2010-05-18 13:35:45 -0700350
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700351static void SC_FontColor(float r, float g, float b, float a)
352{
353 GET_TLS();
354 rsc->mStateFont.setFontColor(r, g, b, a);
355}
356
Jason Sams536923d2010-05-18 13:35:45 -0700357//////////////////////////////////////////////////////////////////////////////
358// Class implementation
359//////////////////////////////////////////////////////////////////////////////
360
361// llvm name mangling ref
362// <builtin-type> ::= v # void
363// ::= b # bool
364// ::= c # char
365// ::= a # signed char
366// ::= h # unsigned char
367// ::= s # short
368// ::= t # unsigned short
369// ::= i # int
370// ::= j # unsigned int
371// ::= l # long
372// ::= m # unsigned long
373// ::= x # long long, __int64
374// ::= y # unsigned long long, __int64
375// ::= f # float
376// ::= d # double
377
378static ScriptCState::SymbolTable_t gSyms[] = {
Jason Samsf0690c42010-07-29 17:31:14 -0700379 { "_Z22rsgBindProgramFragment19rs_program_fragment", (void *)&SC_bindProgramFragment },
380 { "_Z19rsgBindProgramStore16rs_program_store", (void *)&SC_bindProgramStore },
381 { "_Z20rsgBindProgramVertex17rs_program_vertex", (void *)&SC_bindProgramVertex },
382 { "_Z20rsgBindProgramRaster17rs_program_raster", (void *)&SC_bindProgramRaster },
383 { "_Z14rsgBindSampler19rs_program_fragmentj10rs_sampler", (void *)&SC_bindSampler },
384 { "_Z14rsgBindTexture19rs_program_fragmentj13rs_allocation", (void *)&SC_bindTexture },
Jason Samsd79b2e92010-05-19 17:22:57 -0700385
Jason Samsf0690c42010-07-29 17:31:14 -0700386 { "_Z36rsgProgramVertexLoadProjectionMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadProjectionMatrix },
387 { "_Z31rsgProgramVertexLoadModelMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadModelMatrix },
388 { "_Z33rsgProgramVertexLoadTextureMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadTextureMatrix },
Jason Samsd79b2e92010-05-19 17:22:57 -0700389
Jason Sams442a6472010-08-04 17:50:20 -0700390 { "_Z31rsgProgramFragmentConstantColor19rs_program_fragmentffff", (void *)&SC_pfConstantColor },
391
Jason Samsf0690c42010-07-29 17:31:14 -0700392 { "_Z11rsgGetWidthv", (void *)&SC_getWidth },
393 { "_Z12rsgGetHeightv", (void *)&SC_getHeight },
Jason Samsd79b2e92010-05-19 17:22:57 -0700394
Jason Samsf0690c42010-07-29 17:31:14 -0700395 { "_Z18rsgUploadToTexture13rs_allocationj", (void *)&SC_uploadToTexture2 },
Jason Sams96ed4cf2010-06-15 12:15:57 -0700396 { "_Z18rsgUploadToTexture13rs_allocation", (void *)&SC_uploadToTexture },
Jason Samsf0690c42010-07-29 17:31:14 -0700397 { "_Z23rsgUploadToBufferObject13rs_allocation", (void *)&SC_uploadToBufferObject },
Jason Samsd79b2e92010-05-19 17:22:57 -0700398
Jason Samsf0690c42010-07-29 17:31:14 -0700399 { "_Z11rsgDrawRectfffff", (void *)&SC_drawRect },
400 { "_Z11rsgDrawQuadffffffffffff", (void *)&SC_drawQuad },
401 { "_Z20rsgDrawQuadTexCoordsffffffffffffffffffff", (void *)&SC_drawQuadTexCoords },
402 { "_Z24rsgDrawSpriteScreenspacefffff", (void *)&SC_drawSpriteScreenspace },
Jason Samsd79b2e92010-05-19 17:22:57 -0700403
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700404 { "_Z11rsgDrawMesh7rs_mesh", (void *)&SC_drawMesh },
Jason Samsf0690c42010-07-29 17:31:14 -0700405 { "_Z11rsgDrawMesh7rs_meshj", (void *)&SC_drawMeshPrimitive },
406 { "_Z11rsgDrawMesh7rs_meshjjj", (void *)&SC_drawMeshPrimitiveRange },
Alex Sakhartchouka80145d2010-08-13 14:32:23 -0700407 { "_Z25rsgMeshComputeBoundingBox7rs_meshPfS0_S0_S0_S0_S0_", (void *)&SC_meshComputeBoundingBox },
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700408
Jason Samsf0690c42010-07-29 17:31:14 -0700409 { "_Z13rsgClearColorffff", (void *)&SC_ClearColor },
410 { "_Z13rsgClearDepthf", (void *)&SC_ClearDepth },
Jason Samsd79b2e92010-05-19 17:22:57 -0700411
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700412 { "_Z11rsgDrawTextPKcii", (void *)&SC_DrawText },
413 { "_Z11rsgDrawText13rs_allocationii", (void *)&SC_DrawTextAlloc },
414
Jason Samsf0690c42010-07-29 17:31:14 -0700415 { "_Z11rsgBindFont7rs_font", (void *)&SC_BindFont },
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700416 { "_Z12rsgFontColorffff", (void *)&SC_FontColor },
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700417
Jason Sams536923d2010-05-18 13:35:45 -0700418 // misc
Jason Samsf0690c42010-07-29 17:31:14 -0700419 { "_Z5colorffff", (void *)&SC_color },
Jason Sams536923d2010-05-18 13:35:45 -0700420
Jason Sams536923d2010-05-18 13:35:45 -0700421 { NULL, NULL }
422};
423
424const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbolGL(const char *sym)
425{
426 ScriptCState::SymbolTable_t *syms = gSyms;
427
428 while (syms->mPtr) {
429 if (!strcmp(syms->mName, sym)) {
430 return syms;
431 }
432 syms++;
433 }
434 return NULL;
435}
436