blob: bed0fcee8e38b934955821de3f29b541430fc599 [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
113
114//////////////////////////////////////////////////////////////////////////////
115// Drawing
116//////////////////////////////////////////////////////////////////////////////
117
Jason Samsaeb094b2010-05-18 13:35:45 -0700118static void SC_drawQuadTexCoords(float x1, float y1, float z1,
119 float u1, float v1,
120 float x2, float y2, float z2,
121 float u2, float v2,
122 float x3, float y3, float z3,
123 float u3, float v3,
124 float x4, float y4, float z4,
125 float u4, float v4)
126{
127 GET_TLS();
128 if (!rsc->setupCheck()) {
129 return;
130 }
131
132 //LOGE("Quad");
133 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
134 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
135 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
136 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
137
138 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
139 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
140
141 VertexArray va;
Jason Sams79f52df2010-06-01 15:47:01 -0700142 va.add(GL_FLOAT, 3, 12, false, (uint32_t)vtx, "position");
143 va.add(GL_FLOAT, 2, 8, false, (uint32_t)tex, "texture0");
144 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Samsaeb094b2010-05-18 13:35:45 -0700145
146 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
147}
148
149static void SC_drawQuad(float x1, float y1, float z1,
150 float x2, float y2, float z2,
151 float x3, float y3, float z3,
152 float x4, float y4, float z4)
153{
154 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
155 x2, y2, z2, 1, 1,
156 x3, y3, z3, 1, 0,
157 x4, y4, z4, 0, 0);
158}
159
160static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h)
161{
162 GET_TLS();
163 ObjectBaseRef<const ProgramVertex> tmp(rsc->getVertex());
164 rsc->setVertex(rsc->getDefaultProgramVertex());
165 //rsc->setupCheck();
166
167 //GLint crop[4] = {0, h, w, -h};
168
169 float sh = rsc->getHeight();
170
171 SC_drawQuad(x, sh - y, z,
172 x+w, sh - y, z,
173 x+w, sh - (y+h), z,
174 x, sh - (y+h), z);
175 rsc->setVertex((ProgramVertex *)tmp.get());
176}
Jason Samsbdb04602010-06-17 18:05:38 -0700177/*
Jason Samsaeb094b2010-05-18 13:35:45 -0700178static void SC_drawSprite(float x, float y, float z, float w, float h)
179{
180 GET_TLS();
181 float vin[3] = {x, y, z};
182 float vout[4];
183
184 //LOGE("ds in %f %f %f", x, y, z);
185 rsc->getVertex()->transformToScreen(rsc, vout, vin);
186 //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]);
187 vout[0] /= vout[3];
188 vout[1] /= vout[3];
189 vout[2] /= vout[3];
190
191 vout[0] *= rsc->getWidth() / 2;
192 vout[1] *= rsc->getHeight() / 2;
193 vout[0] += rsc->getWidth() / 2;
194 vout[1] += rsc->getHeight() / 2;
195
196 vout[0] -= w/2;
197 vout[1] -= h/2;
198
199 //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]);
200
201 // U, V, W, H
202 SC_drawSpriteScreenspace(vout[0], vout[1], z, h, w);
203 //rsc->setupCheck();
204}
Jason Samsbdb04602010-06-17 18:05:38 -0700205*/
Jason Samsaeb094b2010-05-18 13:35:45 -0700206
207static void SC_drawRect(float x1, float y1,
208 float x2, float y2, float z)
209{
210 //LOGE("SC_drawRect %f,%f %f,%f %f", x1, y1, x2, y2, z);
211 SC_drawQuad(x1, y2, z,
212 x2, y2, z,
213 x2, y1, z,
214 x1, y1, z);
215}
216
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700217static void SC_drawMesh(RsMesh vsm)
218{
219 GET_TLS();
220 Mesh *sm = static_cast<Mesh *>(vsm);
Jason Samsaeb094b2010-05-18 13:35:45 -0700221 if (!rsc->setupCheck()) {
222 return;
223 }
224 sm->render(rsc);
225}
226
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700227static void SC_drawMeshPrimitive(RsMesh vsm, uint32_t primIndex)
Jason Samsaeb094b2010-05-18 13:35:45 -0700228{
229 GET_TLS();
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700230 Mesh *sm = static_cast<Mesh *>(vsm);
Jason Samsaeb094b2010-05-18 13:35:45 -0700231 if (!rsc->setupCheck()) {
232 return;
233 }
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700234 sm->renderPrimitive(rsc, primIndex);
235}
236
237static void SC_drawMeshPrimitiveRange(RsMesh vsm, uint32_t primIndex, uint32_t start, uint32_t len)
238{
239 GET_TLS();
240 Mesh *sm = static_cast<Mesh *>(vsm);
241 if (!rsc->setupCheck()) {
242 return;
243 }
244 sm->renderPrimitiveRange(rsc, primIndex, start, len);
Jason Samsaeb094b2010-05-18 13:35:45 -0700245}
246
247
248//////////////////////////////////////////////////////////////////////////////
249//
250//////////////////////////////////////////////////////////////////////////////
251
252
253static void SC_color(float r, float g, float b, float a)
254{
255 GET_TLS();
256 rsc->mStateVertex.color[0] = r;
257 rsc->mStateVertex.color[1] = g;
258 rsc->mStateVertex.color[2] = b;
259 rsc->mStateVertex.color[3] = a;
260 if (!rsc->checkVersion2_0()) {
261 glColor4f(r, g, b, a);
262 }
263}
264
Jason Sams22fa3712010-05-19 17:22:57 -0700265static void SC_uploadToTexture2(RsAllocation va, uint32_t baseMipLevel)
Jason Samsaeb094b2010-05-18 13:35:45 -0700266{
267 GET_TLS();
268 rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel);
269}
Jason Sams22fa3712010-05-19 17:22:57 -0700270static void SC_uploadToTexture(RsAllocation va)
271{
272 GET_TLS();
273 rsi_AllocationUploadToTexture(rsc, va, false, 0);
274}
Jason Samsaeb094b2010-05-18 13:35:45 -0700275
276static void SC_uploadToBufferObject(RsAllocation va)
277{
278 GET_TLS();
279 rsi_AllocationUploadToBufferObject(rsc, va);
280}
281
Jason Samsaeb094b2010-05-18 13:35:45 -0700282static void SC_ClearColor(float r, float g, float b, float a)
283{
Jason Samsaeb094b2010-05-18 13:35:45 -0700284 GET_TLS();
Jason Sams22fa3712010-05-19 17:22:57 -0700285 if (!rsc->setupCheck()) {
286 return;
287 }
288
289 glClearColor(r, g, b, a);
290 glClear(GL_COLOR_BUFFER_BIT);
291}
292
293static void SC_ClearDepth(float v)
294{
295 GET_TLS();
296 if (!rsc->setupCheck()) {
297 return;
298 }
299
300 glClearDepthf(v);
301 glClear(GL_DEPTH_BUFFER_BIT);
Jason Samsaeb094b2010-05-18 13:35:45 -0700302}
303
304static uint32_t SC_getWidth()
305{
306 GET_TLS();
307 return rsc->getWidth();
308}
309
310static uint32_t SC_getHeight()
311{
312 GET_TLS();
313 return rsc->getHeight();
314}
315
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700316static void SC_DrawTextAlloc(RsAllocation va, int x, int y)
317{
318 GET_TLS();
319 Allocation *alloc = static_cast<Allocation *>(va);
320 rsc->mStateFont.renderText(alloc, x, y);
321}
322
323static void SC_DrawText(const char *text, int x, int y)
324{
325 GET_TLS();
326 rsc->mStateFont.renderText(text, x, y);
327}
328
329static void SC_BindFont(RsFont font)
330{
331 GET_TLS();
332 rsi_ContextBindFont(rsc, font);
333}
Jason Samsaeb094b2010-05-18 13:35:45 -0700334
335//////////////////////////////////////////////////////////////////////////////
336// Class implementation
337//////////////////////////////////////////////////////////////////////////////
338
339// llvm name mangling ref
340// <builtin-type> ::= v # void
341// ::= b # bool
342// ::= c # char
343// ::= a # signed char
344// ::= h # unsigned char
345// ::= s # short
346// ::= t # unsigned short
347// ::= i # int
348// ::= j # unsigned int
349// ::= l # long
350// ::= m # unsigned long
351// ::= x # long long, __int64
352// ::= y # unsigned long long, __int64
353// ::= f # float
354// ::= d # double
355
356static ScriptCState::SymbolTable_t gSyms[] = {
Jason Sams22fa3712010-05-19 17:22:57 -0700357 { "rsgBindProgramFragment", (void *)&SC_bindProgramFragment },
358 { "rsgBindProgramStore", (void *)&SC_bindProgramStore },
359 { "rsgBindProgramVertex", (void *)&SC_bindProgramVertex },
360 { "rsgBindProgramRaster", (void *)&SC_bindProgramRaster },
361 { "rsgBindSampler", (void *)&SC_bindSampler },
362 { "rsgBindTexture", (void *)&SC_bindTexture },
363
Jim Millera490f102010-07-28 14:46:22 -0700364 { "rsgProgramVertexLoadProjectionMatrix", (void *)&SC_vpLoadProjectionMatrix },
Jason Sams22fa3712010-05-19 17:22:57 -0700365 { "rsgProgramVertexLoadModelMatrix", (void *)&SC_vpLoadModelMatrix },
366 { "rsgProgramVertexLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix },
367
368 { "rsgGetWidth", (void *)&SC_getWidth },
369 { "rsgGetHeight", (void *)&SC_getHeight },
370
Jason Sams8c880902010-06-15 12:15:57 -0700371 { "_Z18rsgUploadToTexture13rs_allocationi", (void *)&SC_uploadToTexture2 },
372 { "_Z18rsgUploadToTexture13rs_allocation", (void *)&SC_uploadToTexture },
Jason Sams22fa3712010-05-19 17:22:57 -0700373 { "rsgUploadToBufferObject", (void *)&SC_uploadToBufferObject },
374
375 { "rsgDrawRect", (void *)&SC_drawRect },
376 { "rsgDrawQuad", (void *)&SC_drawQuad },
377 { "rsgDrawQuadTexCoords", (void *)&SC_drawQuadTexCoords },
378 //{ "drawSprite", (void *)&SC_drawSprite },
379 { "rsgDrawSpriteScreenspace", (void *)&SC_drawSpriteScreenspace },
Jason Sams22fa3712010-05-19 17:22:57 -0700380
Alex Sakhartchouk4e9a7a82010-07-01 16:14:06 -0700381 { "_Z11rsgDrawMesh7rs_mesh", (void *)&SC_drawMesh },
382 { "_Z11rsgDrawMesh7rs_meshi", (void *)&SC_drawMeshPrimitive },
383 { "_Z11rsgDrawMesh7rs_meshiii", (void *)&SC_drawMeshPrimitiveRange },
384
Jason Sams22fa3712010-05-19 17:22:57 -0700385 { "rsgClearColor", (void *)&SC_ClearColor },
386 { "rsgClearDepth", (void *)&SC_ClearDepth },
387
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700388 { "_Z11rsgDrawTextPKcii", (void *)&SC_DrawText },
389 { "_Z11rsgDrawText13rs_allocationii", (void *)&SC_DrawTextAlloc },
390
391 { "rsgBindFont", (void *)&SC_BindFont },
392
Jason Samsaeb094b2010-05-18 13:35:45 -0700393 // misc
Jason Samsaeb094b2010-05-18 13:35:45 -0700394 { "color", (void *)&SC_color },
Jason Samsaeb094b2010-05-18 13:35:45 -0700395
Jason Samsaeb094b2010-05-18 13:35:45 -0700396 { NULL, NULL }
397};
398
399const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbolGL(const char *sym)
400{
401 ScriptCState::SymbolTable_t *syms = gSyms;
402
403 while (syms->mPtr) {
404 if (!strcmp(syms->mName, sym)) {
405 return syms;
406 }
407 syms++;
408 }
409 return NULL;
410}
411