blob: 482739c8aef81bb921293878aed67d5b0df92012 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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 "rsProgramVertex.h"
19
Jason Sams1aa5a4e2009-06-22 17:15:15 -070020#include <GLES/gl.h>
21#include <GLES/glext.h>
Jason Samsc460e552009-11-25 13:22:07 -080022#include <GLES2/gl2.h>
23#include <GLES2/gl2ext.h>
Jason Sams1aa5a4e2009-06-22 17:15:15 -070024
Jason Sams326e0dd2009-05-22 14:03:28 -070025using namespace android;
26using namespace android::renderscript;
27
28
Jason Samse514b452009-09-25 14:51:22 -070029ProgramVertex::ProgramVertex(Context *rsc, Element *in, Element *out) :
30 Program(rsc, in, out)
Jason Sams326e0dd2009-05-22 14:03:28 -070031{
Jason Samsf2649a92009-09-25 16:37:33 -070032 mAllocFile = __FILE__;
33 mAllocLine = __LINE__;
Jason Sams326e0dd2009-05-22 14:03:28 -070034 mTextureMatrixEnable = false;
Jason Samsb5909ce2009-07-21 12:20:54 -070035 mLightCount = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -070036}
37
38ProgramVertex::~ProgramVertex()
39{
40}
41
Jason Sams56bc1af2009-06-16 17:49:58 -070042static void logMatrix(const char *txt, const float *f)
43{
Jason Sams992a0b72009-06-23 12:22:47 -070044 LOGV("Matrix %s, %p", txt, f);
45 LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[0], f[4], f[8], f[12]);
46 LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[1], f[5], f[9], f[13]);
47 LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[2], f[6], f[10], f[14]);
48 LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[3], f[7], f[11], f[15]);
Jason Sams56bc1af2009-06-16 17:49:58 -070049}
50
Jason Samsafcb25c2009-08-25 11:34:49 -070051void ProgramVertex::setupGL(const Context *rsc, ProgramVertexState *state)
Jason Sams326e0dd2009-05-22 14:03:28 -070052{
Jason Samscfb1d112009-08-05 13:57:03 -070053 if ((state->mLast.get() == this) && !mDirty) {
54 return;
55 }
56 state->mLast.set(this);
57
58 const float *f = static_cast<const float *>(mConstants->getPtr());
Jason Sams326e0dd2009-05-22 14:03:28 -070059
60 glMatrixMode(GL_TEXTURE);
61 if (mTextureMatrixEnable) {
62 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]);
63 } else {
64 glLoadIdentity();
65 }
66
Jason Samsb5909ce2009-07-21 12:20:54 -070067 glMatrixMode(GL_MODELVIEW);
68 glLoadIdentity();
69 if (mLightCount) {
Romain Guy48b7edc2009-08-06 22:52:13 -070070 int v = 0;
Jason Samsb5909ce2009-07-21 12:20:54 -070071 glEnable(GL_LIGHTING);
72 glLightModelxv(GL_LIGHT_MODEL_TWO_SIDE, &v);
73 for (uint32_t ct = 0; ct < mLightCount; ct++) {
74 const Light *l = mLights[ct].get();
75 glEnable(GL_LIGHT0 + ct);
76 l->setupGL(ct);
77 }
78 for (uint32_t ct = mLightCount; ct < MAX_LIGHTS; ct++) {
79 glDisable(GL_LIGHT0 + ct);
80 }
81 } else {
82 glDisable(GL_LIGHTING);
83 }
Jason Samscfb1d112009-08-05 13:57:03 -070084
Jason Samsb5909ce2009-07-21 12:20:54 -070085 if (!f) {
86 LOGE("Must bind constants to vertex program");
87 }
Jason Sams326e0dd2009-05-22 14:03:28 -070088
89 glMatrixMode(GL_PROJECTION);
Jason Sams56bc1af2009-06-16 17:49:58 -070090 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
Jason Sams326e0dd2009-05-22 14:03:28 -070091 glMatrixMode(GL_MODELVIEW);
Jason Sams56bc1af2009-06-16 17:49:58 -070092 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
Jason Sams326e0dd2009-05-22 14:03:28 -070093
Jason Samscfb1d112009-08-05 13:57:03 -070094 mDirty = false;
Jason Sams326e0dd2009-05-22 14:03:28 -070095}
96
Jason Samsc460e552009-11-25 13:22:07 -080097void ProgramVertex::loadShader() {
98 Program::loadShader(GL_VERTEX_SHADER);
99}
100
101void ProgramVertex::createShader()
102{
103 mShader.setTo("");
104
105 for (uint32_t ct=0; ct < mAttribCount; ct++) {
106 mShader.append("attribute vec4 ");
107 mShader.append(mAttribNames[ct]);
108 mShader.append(";\n");
109 }
110
111 for (uint32_t ct=0; ct < mUniformCount; ct++) {
112 mShader.append("uniform mat4 ");
113 mShader.append(mUniformNames[ct]);
114 mShader.append(";\n");
115 }
116
117 mShader.append("varying vec4 varColor;\n");
118 mShader.append("varying vec4 varTex0;\n");
119
Jason Samsf2a5d732009-11-30 14:49:55 -0800120 if (mUserShader.length() > 1) {
121 mShader.append(mUserShader);
Jason Samsc460e552009-11-25 13:22:07 -0800122 } else {
Jason Samsf2a5d732009-11-30 14:49:55 -0800123 mShader.append("void main() {\n");
124 mShader.append(" gl_Position = uni_MVP * attrib_Position;\n");
125 mShader.append(" gl_PointSize = attrib_PointSize.x;\n");
126
127 mShader.append(" varColor = attrib_Color;\n");
128 if (mTextureMatrixEnable) {
129 mShader.append(" varTex0 = uni_TexMatrix * attrib_T0;\n");
130 } else {
131 mShader.append(" varTex0 = attrib_T0;\n");
132 }
133 //mShader.append(" pos.x = pos.x / 480.0;\n");
134 //mShader.append(" pos.y = pos.y / 800.0;\n");
135 //mShader.append(" gl_Position = pos;\n");
136 mShader.append("}\n");
Jason Samsc460e552009-11-25 13:22:07 -0800137 }
Jason Samsc460e552009-11-25 13:22:07 -0800138}
139
140void ProgramVertex::setupGL2(const Context *rsc, ProgramVertexState *state, ShaderCache *sc)
141{
142 //LOGE("sgl2 vtx1 %x", glGetError());
143 if ((state->mLast.get() == this) && !mDirty) {
144 //return;
145 }
146
147 const float *f = static_cast<const float *>(mConstants->getPtr());
148
149 Matrix mvp;
150 mvp.load(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
151 Matrix t;
152 t.load(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
153 mvp.multiply(&t);
154
155 glUniformMatrix4fv(sc->vtxUniformSlot(0), 1, GL_FALSE, mvp.m);
156 if (mTextureMatrixEnable) {
157 glUniformMatrix4fv(sc->vtxUniformSlot(1), 1, GL_FALSE,
158 &f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]);
159 }
160
161 state->mLast.set(this);
162 //LOGE("sgl2 vtx2 %x", glGetError());
163}
164
Jason Samsb5909ce2009-07-21 12:20:54 -0700165void ProgramVertex::addLight(const Light *l)
166{
167 if (mLightCount < MAX_LIGHTS) {
168 mLights[mLightCount].set(l);
169 mLightCount++;
170 }
171}
172
Jason Samsc9d43db2009-07-28 12:02:16 -0700173void ProgramVertex::setProjectionMatrix(const rsc_Matrix *m) const
174{
Jason Samscfb1d112009-08-05 13:57:03 -0700175 float *f = static_cast<float *>(mConstants->getPtr());
Jason Samsc9d43db2009-07-28 12:02:16 -0700176 memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix));
Jason Samscfb1d112009-08-05 13:57:03 -0700177 mDirty = true;
Jason Samsc9d43db2009-07-28 12:02:16 -0700178}
179
180void ProgramVertex::setModelviewMatrix(const rsc_Matrix *m) const
181{
Jason Samscfb1d112009-08-05 13:57:03 -0700182 float *f = static_cast<float *>(mConstants->getPtr());
Jason Samsc9d43db2009-07-28 12:02:16 -0700183 memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix));
Jason Samscfb1d112009-08-05 13:57:03 -0700184 mDirty = true;
Jason Samsc9d43db2009-07-28 12:02:16 -0700185}
186
187void ProgramVertex::setTextureMatrix(const rsc_Matrix *m) const
188{
Jason Samscfb1d112009-08-05 13:57:03 -0700189 float *f = static_cast<float *>(mConstants->getPtr());
Jason Samsc9d43db2009-07-28 12:02:16 -0700190 memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix));
Jason Samscfb1d112009-08-05 13:57:03 -0700191 mDirty = true;
Jason Samsc9d43db2009-07-28 12:02:16 -0700192}
193
Jason Sams3a97c592009-09-30 17:36:20 -0700194void ProgramVertex::transformToScreen(const Context *rsc, float *v4out, const float *v3in) const
195{
196 float *f = static_cast<float *>(mConstants->getPtr());
197 Matrix mvp;
198 mvp.loadMultiply((Matrix *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET],
199 (Matrix *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
200 mvp.vectorMultiply(v4out, v3in);
201}
Jason Sams326e0dd2009-05-22 14:03:28 -0700202
Jason Samsc460e552009-11-25 13:22:07 -0800203void ProgramVertex::init(Context *rsc)
204{
205 mAttribCount = 6;
206 mAttribNames[VertexArray::POSITION].setTo("attrib_Position");
207 mAttribNames[VertexArray::COLOR].setTo("attrib_Color");
208 mAttribNames[VertexArray::NORMAL].setTo("attrib_Normal");
209 mAttribNames[VertexArray::POINT_SIZE].setTo("attrib_PointSize");
210 mAttribNames[VertexArray::TEXTURE_0].setTo("attrib_T0");
211 mAttribNames[VertexArray::TEXTURE_1].setTo("attrib_T1");
212
213 mUniformCount = 2;
214 mUniformNames[0].setTo("uni_MVP");
215 mUniformNames[1].setTo("uni_TexMatrix");
216
217 createShader();
218}
219
Jason Samsc460e552009-11-25 13:22:07 -0800220///////////////////////////////////////////////////////////////////////
221
Jason Sams326e0dd2009-05-22 14:03:28 -0700222ProgramVertexState::ProgramVertexState()
223{
224 mPV = NULL;
225}
226
227ProgramVertexState::~ProgramVertexState()
228{
229 delete mPV;
230}
231
Jason Sams8ce125b2009-06-17 16:52:59 -0700232void ProgramVertexState::init(Context *rsc, int32_t w, int32_t h)
233{
Jason Sams8287c0c2009-09-24 12:33:45 -0700234 rsi_ElementBegin(rsc);
235 rsi_ElementAdd(rsc, RS_KIND_USER, RS_TYPE_FLOAT, false, 32, NULL);
236 RsElement e = rsi_ElementCreate(rsc);
237
238 rsi_TypeBegin(rsc, e);
239 rsi_TypeAdd(rsc, RS_DIMENSION_X, 48);
Jason Samsf2649a92009-09-25 16:37:33 -0700240 mAllocType.set((Type *)rsi_TypeCreate(rsc));
Jason Sams8287c0c2009-09-24 12:33:45 -0700241
Jason Samse514b452009-09-25 14:51:22 -0700242 ProgramVertex *pv = new ProgramVertex(rsc, NULL, NULL);
Jason Samsf2649a92009-09-25 16:37:33 -0700243 Allocation *alloc = (Allocation *)rsi_AllocationCreateTyped(rsc, mAllocType.get());
Jason Sams8ce125b2009-06-17 16:52:59 -0700244 mDefaultAlloc.set(alloc);
245 mDefault.set(pv);
Jason Samsc460e552009-11-25 13:22:07 -0800246 pv->init(rsc);
Jason Samscfb1d112009-08-05 13:57:03 -0700247 pv->bindAllocation(alloc);
248
Jason Samse18844a2009-11-12 16:09:45 -0800249 updateSize(rsc, w, h);
250}
251
252void ProgramVertexState::updateSize(Context *rsc, int32_t w, int32_t h)
253{
Jason Sams8ce125b2009-06-17 16:52:59 -0700254 Matrix m;
255 m.loadOrtho(0,w, h,0, -1,1);
Jason Samse18844a2009-11-12 16:09:45 -0800256 mDefaultAlloc->subData(RS_PROGRAM_VERTEX_PROJECTION_OFFSET, 16, &m.m[0], 16*4);
Jason Sams8ce125b2009-06-17 16:52:59 -0700257
258 m.loadIdentity();
Jason Samse18844a2009-11-12 16:09:45 -0800259 mDefaultAlloc->subData(RS_PROGRAM_VERTEX_MODELVIEW_OFFSET, 16, &m.m[0], 16*4);
Jason Sams8ce125b2009-06-17 16:52:59 -0700260}
Jason Sams326e0dd2009-05-22 14:03:28 -0700261
Jason Samsf2649a92009-09-25 16:37:33 -0700262void ProgramVertexState::deinit(Context *rsc)
263{
264 mDefaultAlloc.clear();
265 mDefault.clear();
266 mAllocType.clear();
267 mLast.clear();
268 delete mPV;
269 mPV = NULL;
270}
271
Jason Sams326e0dd2009-05-22 14:03:28 -0700272
273namespace android {
274namespace renderscript {
275
276void rsi_ProgramVertexBegin(Context *rsc, RsElement in, RsElement out)
277{
278 delete rsc->mStateVertex.mPV;
Jason Samse514b452009-09-25 14:51:22 -0700279 rsc->mStateVertex.mPV = new ProgramVertex(rsc, (Element *)in, (Element *)out);
Jason Sams326e0dd2009-05-22 14:03:28 -0700280}
281
282RsProgramVertex rsi_ProgramVertexCreate(Context *rsc)
283{
284 ProgramVertex *pv = rsc->mStateVertex.mPV;
Jason Sams9397e302009-08-27 20:23:34 -0700285 pv->incUserRef();
Jason Samsc460e552009-11-25 13:22:07 -0800286 pv->init(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700287 rsc->mStateVertex.mPV = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700288 return pv;
289}
290
Jason Samscfb1d112009-08-05 13:57:03 -0700291void rsi_ProgramVertexBindAllocation(Context *rsc, RsProgramVertex vpgm, RsAllocation constants)
Jason Sams326e0dd2009-05-22 14:03:28 -0700292{
293 ProgramVertex *pv = static_cast<ProgramVertex *>(vpgm);
Jason Samscfb1d112009-08-05 13:57:03 -0700294 pv->bindAllocation(static_cast<Allocation *>(constants));
Jason Sams326e0dd2009-05-22 14:03:28 -0700295}
296
Jason Sams326e0dd2009-05-22 14:03:28 -0700297void rsi_ProgramVertexSetTextureMatrixEnable(Context *rsc, bool enable)
298{
299 rsc->mStateVertex.mPV->setTextureMatrixEnable(enable);
300}
301
Jason Samsf2a5d732009-11-30 14:49:55 -0800302void rsi_ProgramVertexSetShader(Context *rsc, const char *txt, uint32_t len)
303{
304 rsc->mStateVertex.mPV->setShader(txt, len);
305}
306
Jason Samsb5909ce2009-07-21 12:20:54 -0700307void rsi_ProgramVertexAddLight(Context *rsc, RsLight light)
308{
309 rsc->mStateVertex.mPV->addLight(static_cast<const Light *>(light));
310}
Jason Sams326e0dd2009-05-22 14:03:28 -0700311
312
313}
314}