blob: dbd398e34c42bfe5ebc5b620eb212fee1af5772d [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//////////////////////////////////////////////////////////////////////////////
43// IO routines
44//////////////////////////////////////////////////////////////////////////////
45
46static void SC_updateSimpleMesh(RsSimpleMesh mesh)
47{
48 GET_TLS();
49 SimpleMesh *sm = static_cast<SimpleMesh *>(mesh);
50 sm->uploadAll(rsc);
51}
52
53
54//////////////////////////////////////////////////////////////////////////////
55// Context
56//////////////////////////////////////////////////////////////////////////////
57
58static void SC_bindTexture(RsProgramFragment vpf, uint32_t slot, RsAllocation va)
59{
60 GET_TLS();
61 rsi_ProgramBindTexture(rsc,
62 static_cast<ProgramFragment *>(vpf),
63 slot,
64 static_cast<Allocation *>(va));
65
66}
67
68static void SC_bindSampler(RsProgramFragment vpf, uint32_t slot, RsSampler vs)
69{
70 GET_TLS();
71 rsi_ProgramBindSampler(rsc,
72 static_cast<ProgramFragment *>(vpf),
73 slot,
74 static_cast<Sampler *>(vs));
75
76}
77
78static void SC_bindProgramStore(RsProgramStore pfs)
79{
80 GET_TLS();
81 rsi_ContextBindProgramStore(rsc, pfs);
82}
83
84static void SC_bindProgramFragment(RsProgramFragment pf)
85{
86 GET_TLS();
87 rsi_ContextBindProgramFragment(rsc, pf);
88}
89
90static void SC_bindProgramVertex(RsProgramVertex pv)
91{
92 GET_TLS();
93 rsi_ContextBindProgramVertex(rsc, pv);
94}
95
96static void SC_bindProgramRaster(RsProgramRaster pv)
97{
98 GET_TLS();
99 rsi_ContextBindProgramRaster(rsc, pv);
100}
101
102//////////////////////////////////////////////////////////////////////////////
103// VP
104//////////////////////////////////////////////////////////////////////////////
105
106static void SC_vpLoadModelMatrix(const rsc_Matrix *m)
107{
108 GET_TLS();
109 rsc->getVertex()->setModelviewMatrix(m);
110}
111
112static void SC_vpLoadTextureMatrix(const rsc_Matrix *m)
113{
114 GET_TLS();
115 rsc->getVertex()->setTextureMatrix(m);
116}
117
118
119
120//////////////////////////////////////////////////////////////////////////////
121// Drawing
122//////////////////////////////////////////////////////////////////////////////
123
Jason Sams536923d2010-05-18 13:35:45 -0700124static void SC_drawQuadTexCoords(float x1, float y1, float z1,
125 float u1, float v1,
126 float x2, float y2, float z2,
127 float u2, float v2,
128 float x3, float y3, float z3,
129 float u3, float v3,
130 float x4, float y4, float z4,
131 float u4, float v4)
132{
133 GET_TLS();
134 if (!rsc->setupCheck()) {
135 return;
136 }
137
138 //LOGE("Quad");
139 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
140 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
141 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
142 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
143
144 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
145 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
146
147 VertexArray va;
Jason Sams8cb39de2010-06-01 15:47:01 -0700148 va.add(GL_FLOAT, 3, 12, false, (uint32_t)vtx, "position");
149 va.add(GL_FLOAT, 2, 8, false, (uint32_t)tex, "texture0");
150 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache);
Jason Sams536923d2010-05-18 13:35:45 -0700151
152 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
153}
154
155static void SC_drawQuad(float x1, float y1, float z1,
156 float x2, float y2, float z2,
157 float x3, float y3, float z3,
158 float x4, float y4, float z4)
159{
160 SC_drawQuadTexCoords(x1, y1, z1, 0, 1,
161 x2, y2, z2, 1, 1,
162 x3, y3, z3, 1, 0,
163 x4, y4, z4, 0, 0);
164}
165
166static void SC_drawSpriteScreenspace(float x, float y, float z, float w, float h)
167{
168 GET_TLS();
169 ObjectBaseRef<const ProgramVertex> tmp(rsc->getVertex());
170 rsc->setVertex(rsc->getDefaultProgramVertex());
171 //rsc->setupCheck();
172
173 //GLint crop[4] = {0, h, w, -h};
174
175 float sh = rsc->getHeight();
176
177 SC_drawQuad(x, sh - y, z,
178 x+w, sh - y, z,
179 x+w, sh - (y+h), z,
180 x, sh - (y+h), z);
181 rsc->setVertex((ProgramVertex *)tmp.get());
182}
Jason Sams6d1cf412010-06-17 18:05:38 -0700183/*
Jason Sams536923d2010-05-18 13:35:45 -0700184static void SC_drawSprite(float x, float y, float z, float w, float h)
185{
186 GET_TLS();
187 float vin[3] = {x, y, z};
188 float vout[4];
189
190 //LOGE("ds in %f %f %f", x, y, z);
191 rsc->getVertex()->transformToScreen(rsc, vout, vin);
192 //LOGE("ds out %f %f %f %f", vout[0], vout[1], vout[2], vout[3]);
193 vout[0] /= vout[3];
194 vout[1] /= vout[3];
195 vout[2] /= vout[3];
196
197 vout[0] *= rsc->getWidth() / 2;
198 vout[1] *= rsc->getHeight() / 2;
199 vout[0] += rsc->getWidth() / 2;
200 vout[1] += rsc->getHeight() / 2;
201
202 vout[0] -= w/2;
203 vout[1] -= h/2;
204
205 //LOGE("ds out2 %f %f %f", vout[0], vout[1], vout[2]);
206
207 // U, V, W, H
208 SC_drawSpriteScreenspace(vout[0], vout[1], z, h, w);
209 //rsc->setupCheck();
210}
Jason Sams6d1cf412010-06-17 18:05:38 -0700211*/
Jason Sams536923d2010-05-18 13:35:45 -0700212
213static void SC_drawRect(float x1, float y1,
214 float x2, float y2, float z)
215{
216 //LOGE("SC_drawRect %f,%f %f,%f %f", x1, y1, x2, y2, z);
217 SC_drawQuad(x1, y2, z,
218 x2, y2, z,
219 x2, y1, z,
220 x1, y1, z);
221}
222
223static void SC_drawSimpleMesh(RsSimpleMesh vsm)
224{
225 GET_TLS();
226 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
227 if (!rsc->setupCheck()) {
228 return;
229 }
230 sm->render(rsc);
231}
232
233static void SC_drawSimpleMeshRange(RsSimpleMesh vsm, uint32_t start, uint32_t len)
234{
235 GET_TLS();
236 SimpleMesh *sm = static_cast<SimpleMesh *>(vsm);
237 if (!rsc->setupCheck()) {
238 return;
239 }
240 sm->renderRange(rsc, start, len);
241}
242
243
244//////////////////////////////////////////////////////////////////////////////
245//
246//////////////////////////////////////////////////////////////////////////////
247
248
249static void SC_color(float r, float g, float b, float a)
250{
251 GET_TLS();
252 rsc->mStateVertex.color[0] = r;
253 rsc->mStateVertex.color[1] = g;
254 rsc->mStateVertex.color[2] = b;
255 rsc->mStateVertex.color[3] = a;
256 if (!rsc->checkVersion2_0()) {
257 glColor4f(r, g, b, a);
258 }
259}
260
Jason Samsd79b2e92010-05-19 17:22:57 -0700261static void SC_uploadToTexture2(RsAllocation va, uint32_t baseMipLevel)
Jason Sams536923d2010-05-18 13:35:45 -0700262{
263 GET_TLS();
264 rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel);
265}
Jason Samsd79b2e92010-05-19 17:22:57 -0700266static void SC_uploadToTexture(RsAllocation va)
267{
268 GET_TLS();
269 rsi_AllocationUploadToTexture(rsc, va, false, 0);
270}
Jason Sams536923d2010-05-18 13:35:45 -0700271
272static void SC_uploadToBufferObject(RsAllocation va)
273{
274 GET_TLS();
275 rsi_AllocationUploadToBufferObject(rsc, va);
276}
277
Jason Sams536923d2010-05-18 13:35:45 -0700278static void SC_ClearColor(float r, float g, float b, float a)
279{
Jason Sams536923d2010-05-18 13:35:45 -0700280 GET_TLS();
Jason Samsd79b2e92010-05-19 17:22:57 -0700281 if (!rsc->setupCheck()) {
282 return;
283 }
284
285 glClearColor(r, g, b, a);
286 glClear(GL_COLOR_BUFFER_BIT);
287}
288
289static void SC_ClearDepth(float v)
290{
291 GET_TLS();
292 if (!rsc->setupCheck()) {
293 return;
294 }
295
296 glClearDepthf(v);
297 glClear(GL_DEPTH_BUFFER_BIT);
Jason Sams536923d2010-05-18 13:35:45 -0700298}
299
300static uint32_t SC_getWidth()
301{
302 GET_TLS();
303 return rsc->getWidth();
304}
305
306static uint32_t SC_getHeight()
307{
308 GET_TLS();
309 return rsc->getHeight();
310}
311
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700312static void SC_DrawTextAlloc(RsAllocation va, int x, int y)
313{
314 GET_TLS();
315 Allocation *alloc = static_cast<Allocation *>(va);
316 rsc->mStateFont.renderText(alloc, x, y);
317}
318
319static void SC_DrawText(const char *text, int x, int y)
320{
321 GET_TLS();
322 rsc->mStateFont.renderText(text, x, y);
323}
324
325static void SC_BindFont(RsFont font)
326{
327 GET_TLS();
328 rsi_ContextBindFont(rsc, font);
329}
Jason Sams536923d2010-05-18 13:35:45 -0700330
331//////////////////////////////////////////////////////////////////////////////
332// Class implementation
333//////////////////////////////////////////////////////////////////////////////
334
335// llvm name mangling ref
336// <builtin-type> ::= v # void
337// ::= b # bool
338// ::= c # char
339// ::= a # signed char
340// ::= h # unsigned char
341// ::= s # short
342// ::= t # unsigned short
343// ::= i # int
344// ::= j # unsigned int
345// ::= l # long
346// ::= m # unsigned long
347// ::= x # long long, __int64
348// ::= y # unsigned long long, __int64
349// ::= f # float
350// ::= d # double
351
352static ScriptCState::SymbolTable_t gSyms[] = {
Jason Samsd79b2e92010-05-19 17:22:57 -0700353 { "rsgBindProgramFragment", (void *)&SC_bindProgramFragment },
354 { "rsgBindProgramStore", (void *)&SC_bindProgramStore },
355 { "rsgBindProgramVertex", (void *)&SC_bindProgramVertex },
356 { "rsgBindProgramRaster", (void *)&SC_bindProgramRaster },
357 { "rsgBindSampler", (void *)&SC_bindSampler },
358 { "rsgBindTexture", (void *)&SC_bindTexture },
359
360 { "rsgProgramVertexLoadModelMatrix", (void *)&SC_vpLoadModelMatrix },
361 { "rsgProgramVertexLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix },
362
363 { "rsgGetWidth", (void *)&SC_getWidth },
364 { "rsgGetHeight", (void *)&SC_getHeight },
365
Jason Sams96ed4cf2010-06-15 12:15:57 -0700366 { "_Z18rsgUploadToTexture13rs_allocationi", (void *)&SC_uploadToTexture2 },
367 { "_Z18rsgUploadToTexture13rs_allocation", (void *)&SC_uploadToTexture },
Jason Samsd79b2e92010-05-19 17:22:57 -0700368 { "rsgUploadToBufferObject", (void *)&SC_uploadToBufferObject },
369
370 { "rsgDrawRect", (void *)&SC_drawRect },
371 { "rsgDrawQuad", (void *)&SC_drawQuad },
372 { "rsgDrawQuadTexCoords", (void *)&SC_drawQuadTexCoords },
373 //{ "drawSprite", (void *)&SC_drawSprite },
374 { "rsgDrawSpriteScreenspace", (void *)&SC_drawSpriteScreenspace },
Jason Sams96ed4cf2010-06-15 12:15:57 -0700375 { "_Z17rsgDrawSimpleMesh7rs_mesh", (void *)&SC_drawSimpleMesh },
376 { "_Z17rsgDrawSimpleMesh7rs_meshii", (void *)&SC_drawSimpleMeshRange },
Jason Samsd79b2e92010-05-19 17:22:57 -0700377
378 { "rsgClearColor", (void *)&SC_ClearColor },
379 { "rsgClearDepth", (void *)&SC_ClearDepth },
380
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700381 { "_Z11rsgDrawTextPKcii", (void *)&SC_DrawText },
382 { "_Z11rsgDrawText13rs_allocationii", (void *)&SC_DrawTextAlloc },
383
384 { "rsgBindFont", (void *)&SC_BindFont },
385
Jason Samsd79b2e92010-05-19 17:22:57 -0700386
387 //////////////////////////////////////
Jason Sams536923d2010-05-18 13:35:45 -0700388 // IO
389 { "updateSimpleMesh", (void *)&SC_updateSimpleMesh },
390
Jason Sams536923d2010-05-18 13:35:45 -0700391 // misc
Jason Sams536923d2010-05-18 13:35:45 -0700392 { "color", (void *)&SC_color },
Jason Sams536923d2010-05-18 13:35:45 -0700393
Jason Sams536923d2010-05-18 13:35:45 -0700394 { NULL, NULL }
395};
396
397const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbolGL(const char *sym)
398{
399 ScriptCState::SymbolTable_t *syms = gSyms;
400
401 while (syms->mPtr) {
402 if (!strcmp(syms->mName, sym)) {
403 return syms;
404 }
405 syms++;
406 }
407 return NULL;
408}
409