rsScriptC_LibGL.cpp revision 9fc9f0375a92fe22fecb3782b18a5c6060a07290
1/*
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"
20
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// 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
94static void SC_vpLoadProjectionMatrix(const rsc_Matrix *m)
95{
96    GET_TLS();
97    rsc->getVertex()->setProjectionMatrix(m);
98}
99
100static 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
118static 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;
142    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);
145
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}
177/*
178static 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}
205*/
206
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
217static void SC_drawMesh(RsMesh vsm)
218{
219    GET_TLS();
220    Mesh *sm = static_cast<Mesh *>(vsm);
221    if (!rsc->setupCheck()) {
222        return;
223    }
224    sm->render(rsc);
225}
226
227static void SC_drawMeshPrimitive(RsMesh vsm, uint32_t primIndex)
228{
229    GET_TLS();
230    Mesh *sm = static_cast<Mesh *>(vsm);
231    if (!rsc->setupCheck()) {
232        return;
233    }
234    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);
245}
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
265static void SC_uploadToTexture2(RsAllocation va, uint32_t baseMipLevel)
266{
267    GET_TLS();
268    rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel);
269}
270static void SC_uploadToTexture(RsAllocation va)
271{
272    GET_TLS();
273    rsi_AllocationUploadToTexture(rsc, va, false, 0);
274}
275
276static void SC_uploadToBufferObject(RsAllocation va)
277{
278    GET_TLS();
279    rsi_AllocationUploadToBufferObject(rsc, va);
280}
281
282static void SC_ClearColor(float r, float g, float b, float a)
283{
284    GET_TLS();
285    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);
302}
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
316static 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}
334
335static void SC_FontColor(float r, float g, float b, float a)
336{
337    GET_TLS();
338    rsc->mStateFont.setFontColor(r, g, b, a);
339}
340
341//////////////////////////////////////////////////////////////////////////////
342// Class implementation
343//////////////////////////////////////////////////////////////////////////////
344
345// llvm name mangling ref
346//  <builtin-type> ::= v  # void
347//                 ::= b  # bool
348//                 ::= c  # char
349//                 ::= a  # signed char
350//                 ::= h  # unsigned char
351//                 ::= s  # short
352//                 ::= t  # unsigned short
353//                 ::= i  # int
354//                 ::= j  # unsigned int
355//                 ::= l  # long
356//                 ::= m  # unsigned long
357//                 ::= x  # long long, __int64
358//                 ::= y  # unsigned long long, __int64
359//                 ::= f  # float
360//                 ::= d  # double
361
362static ScriptCState::SymbolTable_t gSyms[] = {
363    { "_Z22rsgBindProgramFragment19rs_program_fragment", (void *)&SC_bindProgramFragment },
364    { "_Z19rsgBindProgramStore16rs_program_store", (void *)&SC_bindProgramStore },
365    { "_Z20rsgBindProgramVertex17rs_program_vertex", (void *)&SC_bindProgramVertex },
366    { "_Z20rsgBindProgramRaster17rs_program_raster", (void *)&SC_bindProgramRaster },
367    { "_Z14rsgBindSampler19rs_program_fragmentj10rs_sampler", (void *)&SC_bindSampler },
368    { "_Z14rsgBindTexture19rs_program_fragmentj13rs_allocation", (void *)&SC_bindTexture },
369
370    { "_Z36rsgProgramVertexLoadProjectionMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadProjectionMatrix },
371    { "_Z31rsgProgramVertexLoadModelMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadModelMatrix },
372    { "_Z33rsgProgramVertexLoadTextureMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadTextureMatrix },
373
374    { "_Z11rsgGetWidthv", (void *)&SC_getWidth },
375    { "_Z12rsgGetHeightv", (void *)&SC_getHeight },
376
377    { "_Z18rsgUploadToTexture13rs_allocationj", (void *)&SC_uploadToTexture2 },
378    { "_Z18rsgUploadToTexture13rs_allocation", (void *)&SC_uploadToTexture },
379    { "_Z23rsgUploadToBufferObject13rs_allocation", (void *)&SC_uploadToBufferObject },
380
381    { "_Z11rsgDrawRectfffff", (void *)&SC_drawRect },
382    { "_Z11rsgDrawQuadffffffffffff", (void *)&SC_drawQuad },
383    { "_Z20rsgDrawQuadTexCoordsffffffffffffffffffff", (void *)&SC_drawQuadTexCoords },
384    { "_Z24rsgDrawSpriteScreenspacefffff", (void *)&SC_drawSpriteScreenspace },
385
386    { "_Z11rsgDrawMesh7rs_mesh", (void *)&SC_drawMesh },
387    { "_Z11rsgDrawMesh7rs_meshj", (void *)&SC_drawMeshPrimitive },
388    { "_Z11rsgDrawMesh7rs_meshjjj", (void *)&SC_drawMeshPrimitiveRange },
389
390    { "_Z13rsgClearColorffff", (void *)&SC_ClearColor },
391    { "_Z13rsgClearDepthf", (void *)&SC_ClearDepth },
392
393    { "_Z11rsgDrawTextPKcii", (void *)&SC_DrawText },
394    { "_Z11rsgDrawText13rs_allocationii", (void *)&SC_DrawTextAlloc },
395
396    { "_Z11rsgBindFont7rs_font", (void *)&SC_BindFont },
397    { "_Z12rsgFontColorffff", (void *)&SC_FontColor },
398
399    // misc
400    { "_Z5colorffff", (void *)&SC_color },
401
402    { NULL, NULL }
403};
404
405const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbolGL(const char *sym)
406{
407    ScriptCState::SymbolTable_t *syms = gSyms;
408
409    while (syms->mPtr) {
410        if (!strcmp(syms->mName, sym)) {
411            return syms;
412        }
413        syms++;
414    }
415    return NULL;
416}
417
418