blob: 7862f3c7d1d339a4753f95da37b858c7161ebfbd [file] [log] [blame]
Jason Sams536923d2010-05-18 13:35:45 -07001/*
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -07002 * Copyright (C) 2011 The Android Open Source Project
Jason Sams536923d2010-05-18 13:35:45 -07003 *
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"
Jason Samsfcf72312011-04-20 15:09:01 -070019#include "rsMatrix4x4.h"
20#include "rsMatrix3x3.h"
21#include "rsMatrix2x2.h"
Jason Sams536923d2010-05-18 13:35:45 -070022
Jason Sams536923d2010-05-18 13:35:45 -070023#include "utils/Timers.h"
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -070024#include "driver/rsdVertexArray.h"
25#include "driver/rsdShaderCache.h"
26#include "driver/rsdCore.h"
Jason Sams536923d2010-05-18 13:35:45 -070027
28#define GL_GLEXT_PROTOTYPES
29
30#include <GLES/gl.h>
31#include <GLES/glext.h>
32#include <GLES2/gl2.h>
33#include <GLES2/gl2ext.h>
34
35#include <time.h>
36
37using namespace android;
38using namespace android::renderscript;
39
Jason Samsfcf72312011-04-20 15:09:01 -070040namespace android {
41namespace renderscript {
Jason Sams536923d2010-05-18 13:35:45 -070042
43//////////////////////////////////////////////////////////////////////////////
Jason Sams536923d2010-05-18 13:35:45 -070044// Context
45//////////////////////////////////////////////////////////////////////////////
46
Jason Samsfcf72312011-04-20 15:09:01 -070047void rsrBindTexture(Context *rsc, Script *sc, ProgramFragment *pf, uint32_t slot, Allocation *a) {
48 CHECK_OBJ_OR_NULL(a);
49 CHECK_OBJ(pf);
50 pf->bindTexture(rsc, slot, a);
Jason Sams536923d2010-05-18 13:35:45 -070051}
52
Jason Samsfcf72312011-04-20 15:09:01 -070053void rsrBindSampler(Context *rsc, Script *sc, ProgramFragment *pf, uint32_t slot, Sampler *s) {
Jason Samsf166d9b2010-09-30 18:15:52 -070054 CHECK_OBJ_OR_NULL(vs);
55 CHECK_OBJ(vpf);
Jason Samsfcf72312011-04-20 15:09:01 -070056 pf->bindSampler(rsc, slot, s);
Jason Sams536923d2010-05-18 13:35:45 -070057}
58
Jason Samsfcf72312011-04-20 15:09:01 -070059void rsrBindProgramStore(Context *rsc, Script *sc, ProgramStore *ps) {
60 CHECK_OBJ_OR_NULL(ps);
61 rsc->setProgramStore(ps);
Jason Sams536923d2010-05-18 13:35:45 -070062}
63
Jason Samsfcf72312011-04-20 15:09:01 -070064void rsrBindProgramFragment(Context *rsc, Script *sc, ProgramFragment *pf) {
Jason Samsf166d9b2010-09-30 18:15:52 -070065 CHECK_OBJ_OR_NULL(pf);
Jason Samsfcf72312011-04-20 15:09:01 -070066 rsc->setProgramFragment(pf);
Jason Sams536923d2010-05-18 13:35:45 -070067}
68
Jason Samsfcf72312011-04-20 15:09:01 -070069void rsrBindProgramVertex(Context *rsc, Script *sc, ProgramVertex *pv) {
Jason Samsf166d9b2010-09-30 18:15:52 -070070 CHECK_OBJ_OR_NULL(pv);
Jason Samsfcf72312011-04-20 15:09:01 -070071 rsc->setProgramVertex(pv);
Jason Sams536923d2010-05-18 13:35:45 -070072}
73
Jason Samsfcf72312011-04-20 15:09:01 -070074void rsrBindProgramRaster(Context *rsc, Script *sc, ProgramRaster *pr) {
75 CHECK_OBJ_OR_NULL(pr);
76 rsc->setProgramRaster(pr);
Jason Sams536923d2010-05-18 13:35:45 -070077}
78
Jason Samsfcf72312011-04-20 15:09:01 -070079void rsrBindFrameBufferObjectColorTarget(Context *rsc, Script *sc, Allocation *a, uint32_t slot) {
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -070080 CHECK_OBJ(va);
Jason Samsfcf72312011-04-20 15:09:01 -070081 rsc->mFBOCache.bindColorTarget(rsc, a, slot);
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -070082}
83
Jason Samsfcf72312011-04-20 15:09:01 -070084void rsrBindFrameBufferObjectDepthTarget(Context *rsc, Script *sc, Allocation *a) {
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -070085 CHECK_OBJ(va);
Jason Samsfcf72312011-04-20 15:09:01 -070086 rsc->mFBOCache.bindDepthTarget(rsc, a);
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -070087}
88
Jason Samsfcf72312011-04-20 15:09:01 -070089void rsrClearFrameBufferObjectColorTarget(Context *rsc, Script *sc, uint32_t slot) {
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -070090 rsc->mFBOCache.bindColorTarget(rsc, NULL, slot);
91}
92
Jason Samsfcf72312011-04-20 15:09:01 -070093void rsrClearFrameBufferObjectDepthTarget(Context *rsc, Script *sc) {
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -070094 rsc->mFBOCache.bindDepthTarget(rsc, NULL);
95}
96
Jason Samsfcf72312011-04-20 15:09:01 -070097void rsrClearFrameBufferObjectTargets(Context *rsc, Script *sc) {
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -070098 rsc->mFBOCache.resetAll(rsc);
99}
100
Jason Sams536923d2010-05-18 13:35:45 -0700101//////////////////////////////////////////////////////////////////////////////
102// VP
103//////////////////////////////////////////////////////////////////////////////
104
Jason Samsfcf72312011-04-20 15:09:01 -0700105void rsrVpLoadProjectionMatrix(Context *rsc, Script *sc, const rsc_Matrix *m) {
Jason Samsa17af042010-11-17 15:29:32 -0800106 rsc->getProgramVertex()->setProjectionMatrix(rsc, m);
Jim Millerd8e76202010-07-28 14:46:22 -0700107}
108
Jason Samsfcf72312011-04-20 15:09:01 -0700109void rsrVpLoadModelMatrix(Context *rsc, Script *sc, const rsc_Matrix *m) {
Jason Samsa17af042010-11-17 15:29:32 -0800110 rsc->getProgramVertex()->setModelviewMatrix(rsc, m);
Jason Sams536923d2010-05-18 13:35:45 -0700111}
112
Jason Samsfcf72312011-04-20 15:09:01 -0700113void rsrVpLoadTextureMatrix(Context *rsc, Script *sc, const rsc_Matrix *m) {
Jason Samsa17af042010-11-17 15:29:32 -0800114 rsc->getProgramVertex()->setTextureMatrix(rsc, m);
Jason Sams536923d2010-05-18 13:35:45 -0700115}
116
Jason Samsfcf72312011-04-20 15:09:01 -0700117void rsrPfConstantColor(Context *rsc, Script *sc, ProgramFragment *pf,
118 float r, float g, float b, float a) {
119 CHECK_OBJ(pf);
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700120 pf->setConstantColor(rsc, r, g, b, a);
Jason Sams442a6472010-08-04 17:50:20 -0700121}
122
Jason Samsfcf72312011-04-20 15:09:01 -0700123void rsrVpGetProjectionMatrix(Context *rsc, Script *sc, rsc_Matrix *m) {
Jason Samsa17af042010-11-17 15:29:32 -0800124 rsc->getProgramVertex()->getProjectionMatrix(rsc, m);
Alex Sakhartchoukcbed7522010-08-16 17:40:10 -0700125}
126
Jason Sams536923d2010-05-18 13:35:45 -0700127//////////////////////////////////////////////////////////////////////////////
128// Drawing
129//////////////////////////////////////////////////////////////////////////////
130
Jason Samsfcf72312011-04-20 15:09:01 -0700131void rsrDrawQuadTexCoords(Context *rsc, Script *sc,
132 float x1, float y1, float z1, float u1, float v1,
133 float x2, float y2, float z2, float u2, float v2,
134 float x3, float y3, float z3, float u3, float v3,
135 float x4, float y4, float z4, float u4, float v4) {
Jason Sams536923d2010-05-18 13:35:45 -0700136 if (!rsc->setupCheck()) {
137 return;
138 }
139
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700140 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
141 if (!dc->gl.shaderCache->setup(rsc)) {
142 return;
143 }
144
Jason Sams536923d2010-05-18 13:35:45 -0700145 //LOGE("Quad");
146 //LOGE("%4.2f, %4.2f, %4.2f", x1, y1, z1);
147 //LOGE("%4.2f, %4.2f, %4.2f", x2, y2, z2);
148 //LOGE("%4.2f, %4.2f, %4.2f", x3, y3, z3);
149 //LOGE("%4.2f, %4.2f, %4.2f", x4, y4, z4);
150
151 float vtx[] = {x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4};
152 const float tex[] = {u1,v1, u2,v2, u3,v3, u4,v4};
153
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700154 RsdVertexArray::Attrib attribs[2];
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800155 attribs[0].set(GL_FLOAT, 3, 12, false, (uint32_t)vtx, "ATTRIB_position");
156 attribs[1].set(GL_FLOAT, 2, 8, false, (uint32_t)tex, "ATTRIB_texture0");
157
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700158 RsdVertexArray va(attribs, 2);
Alex Sakhartchouk407cae92011-05-06 14:59:45 -0700159 va.setup(rsc);
Jason Sams536923d2010-05-18 13:35:45 -0700160
Jason Sams5316b9e2011-09-13 15:41:01 -0700161 RSD_CALL_GL(glDrawArrays, GL_TRIANGLE_FAN, 0, 4);
Jason Sams536923d2010-05-18 13:35:45 -0700162}
163
Jason Samsfcf72312011-04-20 15:09:01 -0700164void rsrDrawQuad(Context *rsc, Script *sc,
165 float x1, float y1, float z1,
166 float x2, float y2, float z2,
167 float x3, float y3, float z3,
168 float x4, float y4, float z4) {
169 rsrDrawQuadTexCoords(rsc, sc, x1, y1, z1, 0, 1,
170 x2, y2, z2, 1, 1,
171 x3, y3, z3, 1, 0,
172 x4, y4, z4, 0, 0);
Jason Sams536923d2010-05-18 13:35:45 -0700173}
174
Jason Samsfcf72312011-04-20 15:09:01 -0700175void rsrDrawSpriteScreenspace(Context *rsc, Script *sc,
176 float x, float y, float z, float w, float h) {
Jason Samsa17af042010-11-17 15:29:32 -0800177 ObjectBaseRef<const ProgramVertex> tmp(rsc->getProgramVertex());
178 rsc->setProgramVertex(rsc->getDefaultProgramVertex());
Jason Sams536923d2010-05-18 13:35:45 -0700179 //rsc->setupCheck();
180
181 //GLint crop[4] = {0, h, w, -h};
182
183 float sh = rsc->getHeight();
184
Jason Samsfcf72312011-04-20 15:09:01 -0700185 rsrDrawQuad(rsc, sc,
186 x, sh - y, z,
Jason Sams536923d2010-05-18 13:35:45 -0700187 x+w, sh - y, z,
188 x+w, sh - (y+h), z,
189 x, sh - (y+h), z);
Jason Samsa17af042010-11-17 15:29:32 -0800190 rsc->setProgramVertex((ProgramVertex *)tmp.get());
Jason Sams536923d2010-05-18 13:35:45 -0700191}
Jason Sams536923d2010-05-18 13:35:45 -0700192
Jason Samsfcf72312011-04-20 15:09:01 -0700193void rsrDrawRect(Context *rsc, Script *sc, float x1, float y1, float x2, float y2, float z) {
Jason Sams536923d2010-05-18 13:35:45 -0700194 //LOGE("SC_drawRect %f,%f %f,%f %f", x1, y1, x2, y2, z);
Jason Samsfcf72312011-04-20 15:09:01 -0700195 rsrDrawQuad(rsc, sc, x1, y2, z, x2, y2, z, x2, y1, z, x1, y1, z);
Jason Sams536923d2010-05-18 13:35:45 -0700196}
197
Jason Samsfcf72312011-04-20 15:09:01 -0700198void rsrDrawMesh(Context *rsc, Script *sc, Mesh *sm) {
199 CHECK_OBJ(sm);
Jason Sams536923d2010-05-18 13:35:45 -0700200 if (!rsc->setupCheck()) {
201 return;
202 }
203 sm->render(rsc);
204}
205
Jason Samsfcf72312011-04-20 15:09:01 -0700206void rsrDrawMeshPrimitive(Context *rsc, Script *sc, Mesh *sm, uint32_t primIndex) {
207 CHECK_OBJ(sm);
Jason Sams536923d2010-05-18 13:35:45 -0700208 if (!rsc->setupCheck()) {
209 return;
210 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700211 sm->renderPrimitive(rsc, primIndex);
212}
213
Jason Samsfcf72312011-04-20 15:09:01 -0700214void rsrDrawMeshPrimitiveRange(Context *rsc, Script *sc, Mesh *sm, uint32_t primIndex,
215 uint32_t start, uint32_t len) {
216 CHECK_OBJ(sm);
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700217 if (!rsc->setupCheck()) {
218 return;
219 }
220 sm->renderPrimitiveRange(rsc, primIndex, start, len);
Jason Sams536923d2010-05-18 13:35:45 -0700221}
222
Jason Samsfcf72312011-04-20 15:09:01 -0700223void rsrMeshComputeBoundingBox(Context *rsc, Script *sc, Mesh *sm,
224 float *minX, float *minY, float *minZ,
225 float *maxX, float *maxY, float *maxZ) {
226 CHECK_OBJ(sm);
Alex Sakhartchouka80145d2010-08-13 14:32:23 -0700227 sm->computeBBox();
228 *minX = sm->mBBoxMin[0];
229 *minY = sm->mBBoxMin[1];
230 *minZ = sm->mBBoxMin[2];
231 *maxX = sm->mBBoxMax[0];
232 *maxY = sm->mBBoxMax[1];
233 *maxZ = sm->mBBoxMax[2];
234}
235
Jason Sams536923d2010-05-18 13:35:45 -0700236
237//////////////////////////////////////////////////////////////////////////////
238//
239//////////////////////////////////////////////////////////////////////////////
240
241
Jason Samsfcf72312011-04-20 15:09:01 -0700242void rsrColor(Context *rsc, Script *sc, float r, float g, float b, float a) {
243 ProgramFragment *pf = rsc->getProgramFragment();
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700244 pf->setConstantColor(rsc, r, g, b, a);
Jason Sams536923d2010-05-18 13:35:45 -0700245}
246
Jason Samsfcf72312011-04-20 15:09:01 -0700247void rsrFinish(Context *rsc, Script *sc) {
Jason Sams5316b9e2011-09-13 15:41:01 -0700248 RSD_CALL_GL(glFinish);
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700249}
250
Jason Sams6d8eb262010-12-15 01:41:00 -0800251
Jason Samsfcf72312011-04-20 15:09:01 -0700252void rsrClearColor(Context *rsc, Script *sc, float r, float g, float b, float a) {
Alex Sakhartchouk407cae92011-05-06 14:59:45 -0700253 rsc->mFBOCache.setup(rsc);
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700254 rsc->setupProgramStore();
Jason Samsd79b2e92010-05-19 17:22:57 -0700255
Jason Sams5316b9e2011-09-13 15:41:01 -0700256 RSD_CALL_GL(glClearColor, r, g, b, a);
257 RSD_CALL_GL(glClear, GL_COLOR_BUFFER_BIT);
Jason Samsd79b2e92010-05-19 17:22:57 -0700258}
259
Jason Samsfcf72312011-04-20 15:09:01 -0700260void rsrClearDepth(Context *rsc, Script *sc, float v) {
Alex Sakhartchouk407cae92011-05-06 14:59:45 -0700261 rsc->mFBOCache.setup(rsc);
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700262 rsc->setupProgramStore();
Jason Samsd79b2e92010-05-19 17:22:57 -0700263
Jason Sams5316b9e2011-09-13 15:41:01 -0700264 RSD_CALL_GL(glClearDepthf, v);
265 RSD_CALL_GL(glClear, GL_DEPTH_BUFFER_BIT);
Jason Sams536923d2010-05-18 13:35:45 -0700266}
267
Jason Samsfcf72312011-04-20 15:09:01 -0700268uint32_t rsrGetWidth(Context *rsc, Script *sc) {
Jason Sams536923d2010-05-18 13:35:45 -0700269 return rsc->getWidth();
270}
271
Jason Samsfcf72312011-04-20 15:09:01 -0700272uint32_t rsrGetHeight(Context *rsc, Script *sc) {
Jason Sams536923d2010-05-18 13:35:45 -0700273 return rsc->getHeight();
274}
275
Jason Samsfcf72312011-04-20 15:09:01 -0700276void rsrDrawTextAlloc(Context *rsc, Script *sc, Allocation *a, int x, int y) {
277 const char *text = (const char *)a->getPtr();
278 size_t allocSize = a->getType()->getSizeBytes();
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700279 rsc->mStateFont.renderText(text, allocSize, x, y);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700280}
281
Jason Samsfcf72312011-04-20 15:09:01 -0700282void rsrDrawText(Context *rsc, Script *sc, const char *text, int x, int y) {
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700283 size_t textLen = strlen(text);
284 rsc->mStateFont.renderText(text, textLen, x, y);
285}
286
Jason Samsfcf72312011-04-20 15:09:01 -0700287static void SetMetrics(Font::Rect *metrics,
288 int32_t *left, int32_t *right, int32_t *top, int32_t *bottom) {
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800289 if (left) {
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700290 *left = metrics->left;
291 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800292 if (right) {
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700293 *right = metrics->right;
294 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800295 if (top) {
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700296 *top = metrics->top;
297 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800298 if (bottom) {
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700299 *bottom = metrics->bottom;
300 }
301}
302
Jason Samsfcf72312011-04-20 15:09:01 -0700303void rsrMeasureTextAlloc(Context *rsc, Script *sc, Allocation *a,
304 int32_t *left, int32_t *right, int32_t *top, int32_t *bottom) {
305 CHECK_OBJ(a);
306 const char *text = (const char *)a->getPtr();
307 size_t textLen = a->getType()->getSizeBytes();
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700308 Font::Rect metrics;
309 rsc->mStateFont.measureText(text, textLen, &metrics);
Jason Samsfcf72312011-04-20 15:09:01 -0700310 SetMetrics(&metrics, left, right, top, bottom);
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700311}
312
Jason Samsfcf72312011-04-20 15:09:01 -0700313void rsrMeasureText(Context *rsc, Script *sc, const char *text,
314 int32_t *left, int32_t *right, int32_t *top, int32_t *bottom) {
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700315 size_t textLen = strlen(text);
316 Font::Rect metrics;
317 rsc->mStateFont.measureText(text, textLen, &metrics);
Jason Samsfcf72312011-04-20 15:09:01 -0700318 SetMetrics(&metrics, left, right, top, bottom);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700319}
320
Jason Samsfcf72312011-04-20 15:09:01 -0700321void rsrBindFont(Context *rsc, Script *sc, Font *font) {
Jason Samsf166d9b2010-09-30 18:15:52 -0700322 CHECK_OBJ(font);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700323 rsi_ContextBindFont(rsc, font);
324}
Jason Sams536923d2010-05-18 13:35:45 -0700325
Jason Samsfcf72312011-04-20 15:09:01 -0700326void rsrFontColor(Context *rsc, Script *sc, float r, float g, float b, float a) {
Alex Sakhartchoukfb10c162010-08-04 14:45:48 -0700327 rsc->mStateFont.setFontColor(r, g, b, a);
328}
329
Jason Sams536923d2010-05-18 13:35:45 -0700330}
Jason Samsfcf72312011-04-20 15:09:01 -0700331}