rsScriptC_LibGL.cpp revision 87fe59a2f4d4c74539bfa0bff5f9a7e320e99415
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 "rsMatrix4x4.h" 20#include "rsMatrix3x3.h" 21#include "rsMatrix2x2.h" 22 23#include "utils/Timers.h" 24 25#define GL_GLEXT_PROTOTYPES 26 27#include <GLES/gl.h> 28#include <GLES/glext.h> 29#include <GLES2/gl2.h> 30#include <GLES2/gl2ext.h> 31 32#include <time.h> 33 34using namespace android; 35using namespace android::renderscript; 36 37namespace android { 38namespace renderscript { 39 40////////////////////////////////////////////////////////////////////////////// 41// Context 42////////////////////////////////////////////////////////////////////////////// 43 44void rsrBindTexture(Context *rsc, Script *sc, ProgramFragment *pf, uint32_t slot, Allocation *a) { 45 CHECK_OBJ_OR_NULL(a); 46 CHECK_OBJ(pf); 47 pf->bindTexture(rsc, slot, a); 48} 49 50void rsrBindSampler(Context *rsc, Script *sc, ProgramFragment *pf, uint32_t slot, Sampler *s) { 51 CHECK_OBJ_OR_NULL(vs); 52 CHECK_OBJ(vpf); 53 pf->bindSampler(rsc, slot, s); 54} 55 56void rsrBindProgramStore(Context *rsc, Script *sc, ProgramStore *ps) { 57 CHECK_OBJ_OR_NULL(ps); 58 rsc->setProgramStore(ps); 59} 60 61void rsrBindProgramFragment(Context *rsc, Script *sc, ProgramFragment *pf) { 62 CHECK_OBJ_OR_NULL(pf); 63 rsc->setProgramFragment(pf); 64} 65 66void rsrBindProgramVertex(Context *rsc, Script *sc, ProgramVertex *pv) { 67 CHECK_OBJ_OR_NULL(pv); 68 rsc->setProgramVertex(pv); 69} 70 71void rsrBindProgramRaster(Context *rsc, Script *sc, ProgramRaster *pr) { 72 CHECK_OBJ_OR_NULL(pr); 73 rsc->setProgramRaster(pr); 74} 75 76void rsrBindFrameBufferObjectColorTarget(Context *rsc, Script *sc, Allocation *a, uint32_t slot) { 77 CHECK_OBJ(va); 78 rsc->mFBOCache.bindColorTarget(rsc, a, slot); 79} 80 81void rsrBindFrameBufferObjectDepthTarget(Context *rsc, Script *sc, Allocation *a) { 82 CHECK_OBJ(va); 83 rsc->mFBOCache.bindDepthTarget(rsc, a); 84} 85 86void rsrClearFrameBufferObjectColorTarget(Context *rsc, Script *sc, uint32_t slot) { 87 rsc->mFBOCache.bindColorTarget(rsc, NULL, slot); 88} 89 90void rsrClearFrameBufferObjectDepthTarget(Context *rsc, Script *sc) { 91 rsc->mFBOCache.bindDepthTarget(rsc, NULL); 92} 93 94void rsrClearFrameBufferObjectTargets(Context *rsc, Script *sc) { 95 rsc->mFBOCache.resetAll(rsc); 96} 97 98////////////////////////////////////////////////////////////////////////////// 99// VP 100////////////////////////////////////////////////////////////////////////////// 101 102void rsrVpLoadProjectionMatrix(Context *rsc, Script *sc, const rsc_Matrix *m) { 103 rsc->getProgramVertex()->setProjectionMatrix(rsc, m); 104} 105 106void rsrVpLoadModelMatrix(Context *rsc, Script *sc, const rsc_Matrix *m) { 107 rsc->getProgramVertex()->setModelviewMatrix(rsc, m); 108} 109 110void rsrVpLoadTextureMatrix(Context *rsc, Script *sc, const rsc_Matrix *m) { 111 rsc->getProgramVertex()->setTextureMatrix(rsc, m); 112} 113 114void rsrPfConstantColor(Context *rsc, Script *sc, ProgramFragment *pf, 115 float r, float g, float b, float a) { 116 CHECK_OBJ(pf); 117 pf->setConstantColor(rsc, r, g, b, a); 118} 119 120void rsrVpGetProjectionMatrix(Context *rsc, Script *sc, rsc_Matrix *m) { 121 rsc->getProgramVertex()->getProjectionMatrix(rsc, m); 122} 123 124////////////////////////////////////////////////////////////////////////////// 125// Drawing 126////////////////////////////////////////////////////////////////////////////// 127 128void rsrDrawQuadTexCoords(Context *rsc, Script *sc, 129 float x1, float y1, float z1, float u1, float v1, 130 float x2, float y2, float z2, float u2, float v2, 131 float x3, float y3, float z3, float u3, float v3, 132 float x4, float y4, float z4, float u4, float v4) { 133 if (!rsc->setupCheck()) { 134 return; 135 } 136 137 //LOGE("Quad"); 138 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1); 139 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2); 140 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3); 141 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4); 142 143 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4}; 144 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4}; 145 146 VertexArray::Attrib attribs[2]; 147 attribs[0].set(GL_FLOAT, 3, 12, false, (uint32_t)vtx, "ATTRIB_position"); 148 attribs[1].set(GL_FLOAT, 2, 8, false, (uint32_t)tex, "ATTRIB_texture0"); 149 150 VertexArray va(attribs, 2); 151 va.setupGL2(rsc, &rsc->mStateVertexArray, &rsc->mShaderCache); 152 153 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 154} 155 156void rsrDrawQuad(Context *rsc, Script *sc, 157 float x1, float y1, float z1, 158 float x2, float y2, float z2, 159 float x3, float y3, float z3, 160 float x4, float y4, float z4) { 161 rsrDrawQuadTexCoords(rsc, sc, 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 167void rsrDrawSpriteScreenspace(Context *rsc, Script *sc, 168 float x, float y, float z, float w, float h) { 169 ObjectBaseRef<const ProgramVertex> tmp(rsc->getProgramVertex()); 170 rsc->setProgramVertex(rsc->getDefaultProgramVertex()); 171 //rsc->setupCheck(); 172 173 //GLint crop[4] = {0, h, w, -h}; 174 175 float sh = rsc->getHeight(); 176 177 rsrDrawQuad(rsc, sc, 178 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->setProgramVertex((ProgramVertex *)tmp.get()); 183} 184 185void rsrDrawRect(Context *rsc, Script *sc, float x1, float y1, float x2, float y2, float z) { 186 //LOGE("SC_drawRect %f,%f %f,%f %f", x1, y1, x2, y2, z); 187 rsrDrawQuad(rsc, sc, x1, y2, z, x2, y2, z, x2, y1, z, x1, y1, z); 188} 189 190void rsrDrawMesh(Context *rsc, Script *sc, Mesh *sm) { 191 CHECK_OBJ(sm); 192 if (!rsc->setupCheck()) { 193 return; 194 } 195 sm->render(rsc); 196} 197 198void rsrDrawMeshPrimitive(Context *rsc, Script *sc, Mesh *sm, uint32_t primIndex) { 199 CHECK_OBJ(sm); 200 if (!rsc->setupCheck()) { 201 return; 202 } 203 sm->renderPrimitive(rsc, primIndex); 204} 205 206void rsrDrawMeshPrimitiveRange(Context *rsc, Script *sc, Mesh *sm, uint32_t primIndex, 207 uint32_t start, uint32_t len) { 208 CHECK_OBJ(sm); 209 if (!rsc->setupCheck()) { 210 return; 211 } 212 sm->renderPrimitiveRange(rsc, primIndex, start, len); 213} 214 215void rsrMeshComputeBoundingBox(Context *rsc, Script *sc, Mesh *sm, 216 float *minX, float *minY, float *minZ, 217 float *maxX, float *maxY, float *maxZ) { 218 CHECK_OBJ(sm); 219 sm->computeBBox(); 220 *minX = sm->mBBoxMin[0]; 221 *minY = sm->mBBoxMin[1]; 222 *minZ = sm->mBBoxMin[2]; 223 *maxX = sm->mBBoxMax[0]; 224 *maxY = sm->mBBoxMax[1]; 225 *maxZ = sm->mBBoxMax[2]; 226} 227 228 229////////////////////////////////////////////////////////////////////////////// 230// 231////////////////////////////////////////////////////////////////////////////// 232 233 234void rsrColor(Context *rsc, Script *sc, float r, float g, float b, float a) { 235 ProgramFragment *pf = rsc->getProgramFragment(); 236 pf->setConstantColor(rsc, r, g, b, a); 237} 238 239void rsrFinish(Context *rsc, Script *sc) { 240 glFinish(); 241} 242 243 244void rsrClearColor(Context *rsc, Script *sc, float r, float g, float b, float a) { 245 rsc->mFBOCache.setupGL2(rsc); 246 rsc->setupProgramStore(); 247 248 glClearColor(r, g, b, a); 249 glClear(GL_COLOR_BUFFER_BIT); 250} 251 252void rsrClearDepth(Context *rsc, Script *sc, float v) { 253 rsc->mFBOCache.setupGL2(rsc); 254 rsc->setupProgramStore(); 255 256 glClearDepthf(v); 257 glClear(GL_DEPTH_BUFFER_BIT); 258} 259 260uint32_t rsrGetWidth(Context *rsc, Script *sc) { 261 return rsc->getWidth(); 262} 263 264uint32_t rsrGetHeight(Context *rsc, Script *sc) { 265 return rsc->getHeight(); 266} 267 268void rsrDrawTextAlloc(Context *rsc, Script *sc, Allocation *a, int x, int y) { 269 const char *text = (const char *)a->getPtr(); 270 size_t allocSize = a->getType()->getSizeBytes(); 271 rsc->mStateFont.renderText(text, allocSize, x, y); 272} 273 274void rsrDrawText(Context *rsc, Script *sc, const char *text, int x, int y) { 275 size_t textLen = strlen(text); 276 rsc->mStateFont.renderText(text, textLen, x, y); 277} 278 279static void SetMetrics(Font::Rect *metrics, 280 int32_t *left, int32_t *right, int32_t *top, int32_t *bottom) { 281 if (left) { 282 *left = metrics->left; 283 } 284 if (right) { 285 *right = metrics->right; 286 } 287 if (top) { 288 *top = metrics->top; 289 } 290 if (bottom) { 291 *bottom = metrics->bottom; 292 } 293} 294 295void rsrMeasureTextAlloc(Context *rsc, Script *sc, Allocation *a, 296 int32_t *left, int32_t *right, int32_t *top, int32_t *bottom) { 297 CHECK_OBJ(a); 298 const char *text = (const char *)a->getPtr(); 299 size_t textLen = a->getType()->getSizeBytes(); 300 Font::Rect metrics; 301 rsc->mStateFont.measureText(text, textLen, &metrics); 302 SetMetrics(&metrics, left, right, top, bottom); 303} 304 305void rsrMeasureText(Context *rsc, Script *sc, const char *text, 306 int32_t *left, int32_t *right, int32_t *top, int32_t *bottom) { 307 size_t textLen = strlen(text); 308 Font::Rect metrics; 309 rsc->mStateFont.measureText(text, textLen, &metrics); 310 SetMetrics(&metrics, left, right, top, bottom); 311} 312 313void rsrBindFont(Context *rsc, Script *sc, Font *font) { 314 CHECK_OBJ(font); 315 rsi_ContextBindFont(rsc, font); 316} 317 318void rsrFontColor(Context *rsc, Script *sc, float r, float g, float b, float a) { 319 rsc->mStateFont.setFontColor(r, g, b, a); 320} 321 322} 323} 324