rsScriptC_LibGL.cpp revision ba4aa5c768a498bc3fbb8cb5547b7a9ad6f4b771
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
254static void SC_meshComputeBoundingBox(RsMesh vsm, float *minX, float *minY, float *minZ,
255                                                     float *maxX, float *maxY, float *maxZ)
256{
257    GET_TLS();
258    Mesh *sm = static_cast<Mesh *>(vsm);
259    sm->computeBBox();
260    *minX = sm->mBBoxMin[0];
261    *minY = sm->mBBoxMin[1];
262    *minZ = sm->mBBoxMin[2];
263    *maxX = sm->mBBoxMax[0];
264    *maxY = sm->mBBoxMax[1];
265    *maxZ = sm->mBBoxMax[2];
266}
267
268
269//////////////////////////////////////////////////////////////////////////////
270//
271//////////////////////////////////////////////////////////////////////////////
272
273
274static void SC_color(float r, float g, float b, float a)
275{
276    GET_TLS();
277    ProgramFragment *pf = (ProgramFragment *)rsc->getFragment();
278    pf->setConstantColor(r, g, b, a);
279}
280
281static void SC_uploadToTexture2(RsAllocation va, uint32_t baseMipLevel)
282{
283    GET_TLS();
284    rsi_AllocationUploadToTexture(rsc, va, false, baseMipLevel);
285}
286static void SC_uploadToTexture(RsAllocation va)
287{
288    GET_TLS();
289    rsi_AllocationUploadToTexture(rsc, va, false, 0);
290}
291
292static void SC_uploadToBufferObject(RsAllocation va)
293{
294    GET_TLS();
295    rsi_AllocationUploadToBufferObject(rsc, va);
296}
297
298static void SC_ClearColor(float r, float g, float b, float a)
299{
300    GET_TLS();
301    if (!rsc->setupCheck()) {
302        return;
303    }
304
305    glClearColor(r, g, b, a);
306    glClear(GL_COLOR_BUFFER_BIT);
307}
308
309static void SC_ClearDepth(float v)
310{
311    GET_TLS();
312    if (!rsc->setupCheck()) {
313        return;
314    }
315
316    glClearDepthf(v);
317    glClear(GL_DEPTH_BUFFER_BIT);
318}
319
320static uint32_t SC_getWidth()
321{
322    GET_TLS();
323    return rsc->getWidth();
324}
325
326static uint32_t SC_getHeight()
327{
328    GET_TLS();
329    return rsc->getHeight();
330}
331
332static void SC_DrawTextAlloc(RsAllocation va, int x, int y)
333{
334    GET_TLS();
335    Allocation *alloc = static_cast<Allocation *>(va);
336    rsc->mStateFont.renderText(alloc, x, y);
337}
338
339static void SC_DrawText(const char *text, int x, int y)
340{
341    GET_TLS();
342    rsc->mStateFont.renderText(text, x, y);
343}
344
345static void SC_BindFont(RsFont font)
346{
347    GET_TLS();
348    rsi_ContextBindFont(rsc, font);
349}
350
351static void SC_FontColor(float r, float g, float b, float a)
352{
353    GET_TLS();
354    rsc->mStateFont.setFontColor(r, g, b, a);
355}
356
357//////////////////////////////////////////////////////////////////////////////
358// Class implementation
359//////////////////////////////////////////////////////////////////////////////
360
361// llvm name mangling ref
362//  <builtin-type> ::= v  # void
363//                 ::= b  # bool
364//                 ::= c  # char
365//                 ::= a  # signed char
366//                 ::= h  # unsigned char
367//                 ::= s  # short
368//                 ::= t  # unsigned short
369//                 ::= i  # int
370//                 ::= j  # unsigned int
371//                 ::= l  # long
372//                 ::= m  # unsigned long
373//                 ::= x  # long long, __int64
374//                 ::= y  # unsigned long long, __int64
375//                 ::= f  # float
376//                 ::= d  # double
377
378static ScriptCState::SymbolTable_t gSyms[] = {
379    { "_Z22rsgBindProgramFragment19rs_program_fragment", (void *)&SC_bindProgramFragment },
380    { "_Z19rsgBindProgramStore16rs_program_store", (void *)&SC_bindProgramStore },
381    { "_Z20rsgBindProgramVertex17rs_program_vertex", (void *)&SC_bindProgramVertex },
382    { "_Z20rsgBindProgramRaster17rs_program_raster", (void *)&SC_bindProgramRaster },
383    { "_Z14rsgBindSampler19rs_program_fragmentj10rs_sampler", (void *)&SC_bindSampler },
384    { "_Z14rsgBindTexture19rs_program_fragmentj13rs_allocation", (void *)&SC_bindTexture },
385
386    { "_Z36rsgProgramVertexLoadProjectionMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadProjectionMatrix },
387    { "_Z31rsgProgramVertexLoadModelMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadModelMatrix },
388    { "_Z33rsgProgramVertexLoadTextureMatrixPK12rs_matrix4x4", (void *)&SC_vpLoadTextureMatrix },
389
390    { "_Z31rsgProgramFragmentConstantColor19rs_program_fragmentffff", (void *)&SC_pfConstantColor },
391
392    { "_Z11rsgGetWidthv", (void *)&SC_getWidth },
393    { "_Z12rsgGetHeightv", (void *)&SC_getHeight },
394
395    { "_Z18rsgUploadToTexture13rs_allocationj", (void *)&SC_uploadToTexture2 },
396    { "_Z18rsgUploadToTexture13rs_allocation", (void *)&SC_uploadToTexture },
397    { "_Z23rsgUploadToBufferObject13rs_allocation", (void *)&SC_uploadToBufferObject },
398
399    { "_Z11rsgDrawRectfffff", (void *)&SC_drawRect },
400    { "_Z11rsgDrawQuadffffffffffff", (void *)&SC_drawQuad },
401    { "_Z20rsgDrawQuadTexCoordsffffffffffffffffffff", (void *)&SC_drawQuadTexCoords },
402    { "_Z24rsgDrawSpriteScreenspacefffff", (void *)&SC_drawSpriteScreenspace },
403
404    { "_Z11rsgDrawMesh7rs_mesh", (void *)&SC_drawMesh },
405    { "_Z11rsgDrawMesh7rs_meshj", (void *)&SC_drawMeshPrimitive },
406    { "_Z11rsgDrawMesh7rs_meshjjj", (void *)&SC_drawMeshPrimitiveRange },
407    { "_Z25rsgMeshComputeBoundingBox7rs_meshPfS0_S0_S0_S0_S0_", (void *)&SC_meshComputeBoundingBox },
408
409    { "_Z13rsgClearColorffff", (void *)&SC_ClearColor },
410    { "_Z13rsgClearDepthf", (void *)&SC_ClearDepth },
411
412    { "_Z11rsgDrawTextPKcii", (void *)&SC_DrawText },
413    { "_Z11rsgDrawText13rs_allocationii", (void *)&SC_DrawTextAlloc },
414
415    { "_Z11rsgBindFont7rs_font", (void *)&SC_BindFont },
416    { "_Z12rsgFontColorffff", (void *)&SC_FontColor },
417
418    // misc
419    { "_Z5colorffff", (void *)&SC_color },
420
421    { NULL, NULL }
422};
423
424const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbolGL(const char *sym)
425{
426    ScriptCState::SymbolTable_t *syms = gSyms;
427
428    while (syms->mPtr) {
429        if (!strcmp(syms->mName, sym)) {
430            return syms;
431        }
432        syms++;
433    }
434    return NULL;
435}
436
437