rsScriptC_LibGL.cpp revision 6445e5210c6d7f8689e94be9026153d017c9545b
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
113static 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
120
121//////////////////////////////////////////////////////////////////////////////
122// Drawing
123//////////////////////////////////////////////////////////////////////////////
124
125static 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;
149    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);
152
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}
184/*
185static 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}
212*/
213
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
224static void SC_drawMesh(RsMesh vsm)
225{
226    GET_TLS();
227    Mesh *sm = static_cast<Mesh *>(vsm);
228    if (!rsc->setupCheck()) {
229        return;
230    }
231    sm->render(rsc);
232}
233
234static void SC_drawMeshPrimitive(RsMesh vsm, uint32_t primIndex)
235{
236    GET_TLS();
237    Mesh *sm = static_cast<Mesh *>(vsm);
238    if (!rsc->setupCheck()) {
239        return;
240    }
241    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);
252}
253
254
255//////////////////////////////////////////////////////////////////////////////
256//
257//////////////////////////////////////////////////////////////////////////////
258
259
260static void SC_color(float r, float g, float b, float a)
261{
262    GET_TLS();
263    ProgramFragment *pf = (ProgramFragment *)rsc->getFragment();
264    pf->setConstantColor(r, g, b, a);
265}
266
267static void SC_uploadToTexture2(RsAllocation va, uint32_t baseMipLevel)
268{
269    GET_TLS();
270    rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel);
271}
272static void SC_uploadToTexture(RsAllocation va)
273{
274    GET_TLS();
275    rsi_AllocationUploadToTexture(rsc, va, false, 0);
276}
277
278static void SC_uploadToBufferObject(RsAllocation va)
279{
280    GET_TLS();
281    rsi_AllocationUploadToBufferObject(rsc, va);
282}
283
284static void SC_ClearColor(float r, float g, float b, float a)
285{
286    GET_TLS();
287    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);
304}
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
318static 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}
336
337static 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
343//////////////////////////////////////////////////////////////////////////////
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[] = {
365    { "_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 },
371
372    { "_Z36rsgProgramVertexLoadProjectionMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadProjectionMatrix },
373    { "_Z31rsgProgramVertexLoadModelMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadModelMatrix },
374    { "_Z33rsgProgramVertexLoadTextureMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadTextureMatrix },
375
376    { "_Z31rsgProgramFragmentConstantColor19rs_program_fragmentffff", (void *)&SC_pfConstantColor },
377
378    { "_Z11rsgGetWidthv", (void *)&SC_getWidth },
379    { "_Z12rsgGetHeightv", (void *)&SC_getHeight },
380
381    { "_Z18rsgUploadToTexture13rs_allocationj", (void *)&SC_uploadToTexture2 },
382    { "_Z18rsgUploadToTexture13rs_allocation", (void *)&SC_uploadToTexture },
383    { "_Z23rsgUploadToBufferObject13rs_allocation", (void *)&SC_uploadToBufferObject },
384
385    { "_Z11rsgDrawRectfffff", (void *)&SC_drawRect },
386    { "_Z11rsgDrawQuadffffffffffff", (void *)&SC_drawQuad },
387    { "_Z20rsgDrawQuadTexCoordsffffffffffffffffffff", (void *)&SC_drawQuadTexCoords },
388    { "_Z24rsgDrawSpriteScreenspacefffff", (void *)&SC_drawSpriteScreenspace },
389
390    { "_Z11rsgDrawMesh7rs_mesh", (void *)&SC_drawMesh },
391    { "_Z11rsgDrawMesh7rs_meshj", (void *)&SC_drawMeshPrimitive },
392    { "_Z11rsgDrawMesh7rs_meshjjj", (void *)&SC_drawMeshPrimitiveRange },
393
394    { "_Z13rsgClearColorffff", (void *)&SC_ClearColor },
395    { "_Z13rsgClearDepthf", (void *)&SC_ClearDepth },
396
397    { "_Z11rsgDrawTextPKcii", (void *)&SC_DrawText },
398    { "_Z11rsgDrawText13rs_allocationii", (void *)&SC_DrawTextAlloc },
399
400    { "_Z11rsgBindFont7rs_font", (void *)&SC_BindFont },
401    { "_Z12rsgFontColorffff", (void *)&SC_FontColor },
402
403    // misc
404    { "_Z5colorffff", (void *)&SC_color },
405
406    { 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
422