blob: f5e595340aff761cab0038446b2c7421c570aee0 [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
254
255//////////////////////////////////////////////////////////////////////////////
256//
257//////////////////////////////////////////////////////////////////////////////
258
259
260static void SC_color(float r, float g, float b, float a)
261{
262 GET_TLS();
Jason Sams442a6472010-08-04 17:50:20 -0700263 ProgramFragment *pf = (ProgramFragment *)rsc->getFragment();
264 pf->setConstantColor(r, g, b, a);
Jason Sams536923d2010-05-18 13:35:45 -0700265}
266
Jason Samsd79b2e92010-05-19 17:22:57 -0700267static void SC_uploadToTexture2(RsAllocation va, uint32_t baseMipLevel)
Jason Sams536923d2010-05-18 13:35:45 -0700268{
269 GET_TLS();
270 rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel);
271}
Jason Samsd79b2e92010-05-19 17:22:57 -0700272static void SC_uploadToTexture(RsAllocation va)
273{
274 GET_TLS();
275 rsi_AllocationUploadToTexture(rsc, va, false, 0);
276}
Jason Sams536923d2010-05-18 13:35:45 -0700277
278static void SC_uploadToBufferObject(RsAllocation va)
279{
280 GET_TLS();
281 rsi_AllocationUploadToBufferObject(rsc, va);
282}
283
Jason Sams536923d2010-05-18 13:35:45 -0700284static void SC_ClearColor(float r, float g, float b, float a)
285{
Jason Sams536923d2010-05-18 13:35:45 -0700286 GET_TLS();
Jason Samsd79b2e92010-05-19 17:22:57 -0700287 if (!rsc->setupCheck()) {
288 return;
289 }
290
291 glClearColor(r, g, b, a);
292 glClear(GL_COLOR_BUFFER_BIT);
293}
294
295static void SC_ClearDepth(float v)
296{
297 GET_TLS();
298 if (!rsc->setupCheck()) {
299 return;
300 }
301
302 glClearDepthf(v);
303 glClear(GL_DEPTH_BUFFER_BIT);
Jason Sams536923d2010-05-18 13:35:45 -0700304}
305
306static uint32_t SC_getWidth()
307{
308 GET_TLS();
309 return rsc->getWidth();
310}
311
312static uint32_t SC_getHeight()
313{
314 GET_TLS();
315 return rsc->getHeight();
316}
317
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700318static void SC_DrawTextAlloc(RsAllocation va, int x, int y)
319{
320 GET_TLS();
321 Allocation *alloc = static_cast<Allocation *>(va);
322 rsc->mStateFont.renderText(alloc, x, y);
323}
324
325static void SC_DrawText(const char *text, int x, int y)
326{
327 GET_TLS();
328 rsc->mStateFont.renderText(text, x, y);
329}
330
331static void SC_BindFont(RsFont font)
332{
333 GET_TLS();
334 rsi_ContextBindFont(rsc, font);
335}
Jason Sams536923d2010-05-18 13:35:45 -0700336
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700337static void SC_FontColor(float r, float g, float b, float a)
338{
339 GET_TLS();
340 rsc->mStateFont.setFontColor(r, g, b, a);
341}
342
Jason Sams536923d2010-05-18 13:35:45 -0700343//////////////////////////////////////////////////////////////////////////////
344// Class implementation
345//////////////////////////////////////////////////////////////////////////////
346
347// llvm name mangling ref
348// <builtin-type> ::= v # void
349// ::= b # bool
350// ::= c # char
351// ::= a # signed char
352// ::= h # unsigned char
353// ::= s # short
354// ::= t # unsigned short
355// ::= i # int
356// ::= j # unsigned int
357// ::= l # long
358// ::= m # unsigned long
359// ::= x # long long, __int64
360// ::= y # unsigned long long, __int64
361// ::= f # float
362// ::= d # double
363
364static ScriptCState::SymbolTable_t gSyms[] = {
Jason Samsf0690c42010-07-29 17:31:14 -0700365 { "_Z22rsgBindProgramFragment19rs_program_fragment", (void *)&SC_bindProgramFragment },
366 { "_Z19rsgBindProgramStore16rs_program_store", (void *)&SC_bindProgramStore },
367 { "_Z20rsgBindProgramVertex17rs_program_vertex", (void *)&SC_bindProgramVertex },
368 { "_Z20rsgBindProgramRaster17rs_program_raster", (void *)&SC_bindProgramRaster },
369 { "_Z14rsgBindSampler19rs_program_fragmentj10rs_sampler", (void *)&SC_bindSampler },
370 { "_Z14rsgBindTexture19rs_program_fragmentj13rs_allocation", (void *)&SC_bindTexture },
Jason Samsd79b2e92010-05-19 17:22:57 -0700371
Jason Samsf0690c42010-07-29 17:31:14 -0700372 { "_Z36rsgProgramVertexLoadProjectionMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadProjectionMatrix },
373 { "_Z31rsgProgramVertexLoadModelMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadModelMatrix },
374 { "_Z33rsgProgramVertexLoadTextureMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadTextureMatrix },
Jason Samsd79b2e92010-05-19 17:22:57 -0700375
Jason Sams442a6472010-08-04 17:50:20 -0700376 { "_Z31rsgProgramFragmentConstantColor19rs_program_fragmentffff", (void *)&SC_pfConstantColor },
377
Jason Samsf0690c42010-07-29 17:31:14 -0700378 { "_Z11rsgGetWidthv", (void *)&SC_getWidth },
379 { "_Z12rsgGetHeightv", (void *)&SC_getHeight },
Jason Samsd79b2e92010-05-19 17:22:57 -0700380
Jason Samsf0690c42010-07-29 17:31:14 -0700381 { "_Z18rsgUploadToTexture13rs_allocationj", (void *)&SC_uploadToTexture2 },
Jason Sams96ed4cf2010-06-15 12:15:57 -0700382 { "_Z18rsgUploadToTexture13rs_allocation", (void *)&SC_uploadToTexture },
Jason Samsf0690c42010-07-29 17:31:14 -0700383 { "_Z23rsgUploadToBufferObject13rs_allocation", (void *)&SC_uploadToBufferObject },
Jason Samsd79b2e92010-05-19 17:22:57 -0700384
Jason Samsf0690c42010-07-29 17:31:14 -0700385 { "_Z11rsgDrawRectfffff", (void *)&SC_drawRect },
386 { "_Z11rsgDrawQuadffffffffffff", (void *)&SC_drawQuad },
387 { "_Z20rsgDrawQuadTexCoordsffffffffffffffffffff", (void *)&SC_drawQuadTexCoords },
388 { "_Z24rsgDrawSpriteScreenspacefffff", (void *)&SC_drawSpriteScreenspace },
Jason Samsd79b2e92010-05-19 17:22:57 -0700389
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700390 { "_Z11rsgDrawMesh7rs_mesh", (void *)&SC_drawMesh },
Jason Samsf0690c42010-07-29 17:31:14 -0700391 { "_Z11rsgDrawMesh7rs_meshj", (void *)&SC_drawMeshPrimitive },
392 { "_Z11rsgDrawMesh7rs_meshjjj", (void *)&SC_drawMeshPrimitiveRange },
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700393
Jason Samsf0690c42010-07-29 17:31:14 -0700394 { "_Z13rsgClearColorffff", (void *)&SC_ClearColor },
395 { "_Z13rsgClearDepthf", (void *)&SC_ClearDepth },
Jason Samsd79b2e92010-05-19 17:22:57 -0700396
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700397 { "_Z11rsgDrawTextPKcii", (void *)&SC_DrawText },
398 { "_Z11rsgDrawText13rs_allocationii", (void *)&SC_DrawTextAlloc },
399
Jason Samsf0690c42010-07-29 17:31:14 -0700400 { "_Z11rsgBindFont7rs_font", (void *)&SC_BindFont },
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700401 { "_Z12rsgFontColorffff", (void *)&SC_FontColor },
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700402
Jason Sams536923d2010-05-18 13:35:45 -0700403 // misc
Jason Samsf0690c42010-07-29 17:31:14 -0700404 { "_Z5colorffff", (void *)&SC_color },
Jason Sams536923d2010-05-18 13:35:45 -0700405
Jason Sams536923d2010-05-18 13:35:45 -0700406 { NULL, NULL }
407};
408
409const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbolGL(const char *sym)
410{
411 ScriptCState::SymbolTable_t *syms = gSyms;
412
413 while (syms->mPtr) {
414 if (!strcmp(syms->mName, sym)) {
415 return syms;
416 }
417 syms++;
418 }
419 return NULL;
420}
421