blob: f0de908799cdd937dbf2774a4a60521c94cdf410 [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//////////////////////////////////////////////////////////////////////////////
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 Samsaeb094b2010-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 Sams79f52df2010-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 Samsaeb094b2010-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 Samsbdb04602010-06-17 18:05:38 -0700183/*
Jason Samsaeb094b2010-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 Samsbdb04602010-06-17 18:05:38 -0700211*/
Jason Samsaeb094b2010-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 Sams22fa3712010-05-19 17:22:57 -0700261static void SC_uploadToTexture2(RsAllocation va, uint32_t baseMipLevel)
Jason Samsaeb094b2010-05-18 13:35:45 -0700262{
263 GET_TLS();
264 rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel);
265}
Jason Sams22fa3712010-05-19 17:22:57 -0700266static void SC_uploadToTexture(RsAllocation va)
267{
268 GET_TLS();
269 rsi_AllocationUploadToTexture(rsc, va, false, 0);
270}
Jason Samsaeb094b2010-05-18 13:35:45 -0700271
272static void SC_uploadToBufferObject(RsAllocation va)
273{
274 GET_TLS();
275 rsi_AllocationUploadToBufferObject(rsc, va);
276}
277
Jason Samsaeb094b2010-05-18 13:35:45 -0700278static void SC_ClearColor(float r, float g, float b, float a)
279{
Jason Samsaeb094b2010-05-18 13:35:45 -0700280 GET_TLS();
Jason Sams22fa3712010-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 Samsaeb094b2010-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
312
313//////////////////////////////////////////////////////////////////////////////
314// Class implementation
315//////////////////////////////////////////////////////////////////////////////
316
317// llvm name mangling ref
318// <builtin-type> ::= v # void
319// ::= b # bool
320// ::= c # char
321// ::= a # signed char
322// ::= h # unsigned char
323// ::= s # short
324// ::= t # unsigned short
325// ::= i # int
326// ::= j # unsigned int
327// ::= l # long
328// ::= m # unsigned long
329// ::= x # long long, __int64
330// ::= y # unsigned long long, __int64
331// ::= f # float
332// ::= d # double
333
334static ScriptCState::SymbolTable_t gSyms[] = {
Jason Sams22fa3712010-05-19 17:22:57 -0700335 { "rsgBindProgramFragment", (void *)&SC_bindProgramFragment },
336 { "rsgBindProgramStore", (void *)&SC_bindProgramStore },
337 { "rsgBindProgramVertex", (void *)&SC_bindProgramVertex },
338 { "rsgBindProgramRaster", (void *)&SC_bindProgramRaster },
339 { "rsgBindSampler", (void *)&SC_bindSampler },
340 { "rsgBindTexture", (void *)&SC_bindTexture },
341
342 { "rsgProgramVertexLoadModelMatrix", (void *)&SC_vpLoadModelMatrix },
343 { "rsgProgramVertexLoadTextureMatrix", (void *)&SC_vpLoadTextureMatrix },
344
345 { "rsgGetWidth", (void *)&SC_getWidth },
346 { "rsgGetHeight", (void *)&SC_getHeight },
347
Jason Samsbdb04602010-06-17 18:05:38 -0700348 { "_Z18rsgUploadToTextureii", (void *)&SC_uploadToTexture2 },
349 { "_Z18rsgUploadToTexturei", (void *)&SC_uploadToTexture },
Jason Sams8c880902010-06-15 12:15:57 -0700350 { "_Z18rsgUploadToTexture13rs_allocationi", (void *)&SC_uploadToTexture2 },
351 { "_Z18rsgUploadToTexture13rs_allocation", (void *)&SC_uploadToTexture },
Jason Sams22fa3712010-05-19 17:22:57 -0700352 { "rsgUploadToBufferObject", (void *)&SC_uploadToBufferObject },
353
354 { "rsgDrawRect", (void *)&SC_drawRect },
355 { "rsgDrawQuad", (void *)&SC_drawQuad },
356 { "rsgDrawQuadTexCoords", (void *)&SC_drawQuadTexCoords },
357 //{ "drawSprite", (void *)&SC_drawSprite },
358 { "rsgDrawSpriteScreenspace", (void *)&SC_drawSpriteScreenspace },
Jason Sams8c880902010-06-15 12:15:57 -0700359 { "_Z17rsgDrawSimpleMesh7rs_mesh", (void *)&SC_drawSimpleMesh },
360 { "_Z17rsgDrawSimpleMesh7rs_meshii", (void *)&SC_drawSimpleMeshRange },
Jason Samsbdb04602010-06-17 18:05:38 -0700361 { "_Z17rsgDrawSimpleMeshi", (void *)&SC_drawSimpleMesh },
362 { "_Z17rsgDrawSimpleMeshiii", (void *)&SC_drawSimpleMeshRange },
Jason Sams22fa3712010-05-19 17:22:57 -0700363
364 { "rsgClearColor", (void *)&SC_ClearColor },
365 { "rsgClearDepth", (void *)&SC_ClearDepth },
366
367
368 //////////////////////////////////////
Jason Samsaeb094b2010-05-18 13:35:45 -0700369 // IO
370 { "updateSimpleMesh", (void *)&SC_updateSimpleMesh },
371
Jason Samsaeb094b2010-05-18 13:35:45 -0700372 // misc
Jason Sams22fa3712010-05-19 17:22:57 -0700373 //{ "pfClearColor", (void *)&SC_ClearColor },
Jason Samsaeb094b2010-05-18 13:35:45 -0700374 { "color", (void *)&SC_color },
Jason Samsaeb094b2010-05-18 13:35:45 -0700375
Jason Samsaeb094b2010-05-18 13:35:45 -0700376 { NULL, NULL }
377};
378
379const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbolGL(const char *sym)
380{
381 ScriptCState::SymbolTable_t *syms = gSyms;
382
383 while (syms->mPtr) {
384 if (!strcmp(syms->mName, sym)) {
385 return syms;
386 }
387 syms++;
388 }
389 return NULL;
390}
391