blob: 1776b02bdc9caf359242dba5c37c18f62217ff8b [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
120 mShader.append("void main() {\n");
121 mShader.append(" gl_Position = uni_MVP * attrib_Position;\n");
122 mShader.append(" varColor = attrib_Color;\n");
123 if (mTextureMatrixEnable) {
124 mShader.append(" varTex0 = uni_TexMatrix * attrib_T0;\n");
125 } else {
126 mShader.append(" varTex0 = attrib_T0;\n");
127 }
128 //mShader.append(" pos.x = pos.x / 480.0;\n");
129 //mShader.append(" pos.y = pos.y / 800.0;\n");
130 //mShader.append(" gl_Position = pos;\n");
131 mShader.append("}\n");
132}
133
134void ProgramVertex::setupGL2(const Context *rsc, ProgramVertexState *state, ShaderCache *sc)
135{
136 //LOGE("sgl2 vtx1 %x", glGetError());
137 if ((state->mLast.get() == this) && !mDirty) {
138 //return;
139 }
140
141 const float *f = static_cast<const float *>(mConstants->getPtr());
142
143 Matrix mvp;
144 mvp.load(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
145 Matrix t;
146 t.load(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
147 mvp.multiply(&t);
148
149 glUniformMatrix4fv(sc->vtxUniformSlot(0), 1, GL_FALSE, mvp.m);
150 if (mTextureMatrixEnable) {
151 glUniformMatrix4fv(sc->vtxUniformSlot(1), 1, GL_FALSE,
152 &f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]);
153 }
154
155 state->mLast.set(this);
156 //LOGE("sgl2 vtx2 %x", glGetError());
157}
158
Jason Samsb5909ce2009-07-21 12:20:54 -0700159void ProgramVertex::addLight(const Light *l)
160{
161 if (mLightCount < MAX_LIGHTS) {
162 mLights[mLightCount].set(l);
163 mLightCount++;
164 }
165}
166
Jason Samsc9d43db2009-07-28 12:02:16 -0700167void ProgramVertex::setProjectionMatrix(const rsc_Matrix *m) const
168{
Jason Samscfb1d112009-08-05 13:57:03 -0700169 float *f = static_cast<float *>(mConstants->getPtr());
Jason Samsc9d43db2009-07-28 12:02:16 -0700170 memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix));
Jason Samscfb1d112009-08-05 13:57:03 -0700171 mDirty = true;
Jason Samsc9d43db2009-07-28 12:02:16 -0700172}
173
174void ProgramVertex::setModelviewMatrix(const rsc_Matrix *m) const
175{
Jason Samscfb1d112009-08-05 13:57:03 -0700176 float *f = static_cast<float *>(mConstants->getPtr());
Jason Samsc9d43db2009-07-28 12:02:16 -0700177 memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix));
Jason Samscfb1d112009-08-05 13:57:03 -0700178 mDirty = true;
Jason Samsc9d43db2009-07-28 12:02:16 -0700179}
180
181void ProgramVertex::setTextureMatrix(const rsc_Matrix *m) const
182{
Jason Samscfb1d112009-08-05 13:57:03 -0700183 float *f = static_cast<float *>(mConstants->getPtr());
Jason Samsc9d43db2009-07-28 12:02:16 -0700184 memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix));
Jason Samscfb1d112009-08-05 13:57:03 -0700185 mDirty = true;
Jason Samsc9d43db2009-07-28 12:02:16 -0700186}
187
Jason Sams3a97c592009-09-30 17:36:20 -0700188void ProgramVertex::transformToScreen(const Context *rsc, float *v4out, const float *v3in) const
189{
190 float *f = static_cast<float *>(mConstants->getPtr());
191 Matrix mvp;
192 mvp.loadMultiply((Matrix *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET],
193 (Matrix *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
194 mvp.vectorMultiply(v4out, v3in);
195}
Jason Sams326e0dd2009-05-22 14:03:28 -0700196
Jason Samsc460e552009-11-25 13:22:07 -0800197void ProgramVertex::init(Context *rsc)
198{
199 mAttribCount = 6;
200 mAttribNames[VertexArray::POSITION].setTo("attrib_Position");
201 mAttribNames[VertexArray::COLOR].setTo("attrib_Color");
202 mAttribNames[VertexArray::NORMAL].setTo("attrib_Normal");
203 mAttribNames[VertexArray::POINT_SIZE].setTo("attrib_PointSize");
204 mAttribNames[VertexArray::TEXTURE_0].setTo("attrib_T0");
205 mAttribNames[VertexArray::TEXTURE_1].setTo("attrib_T1");
206
207 mUniformCount = 2;
208 mUniformNames[0].setTo("uni_MVP");
209 mUniformNames[1].setTo("uni_TexMatrix");
210
211 createShader();
212}
213
214
215///////////////////////////////////////////////////////////////////////
216
Jason Sams326e0dd2009-05-22 14:03:28 -0700217ProgramVertexState::ProgramVertexState()
218{
219 mPV = NULL;
220}
221
222ProgramVertexState::~ProgramVertexState()
223{
224 delete mPV;
225}
226
Jason Sams8ce125b2009-06-17 16:52:59 -0700227void ProgramVertexState::init(Context *rsc, int32_t w, int32_t h)
228{
Jason Sams8287c0c2009-09-24 12:33:45 -0700229 rsi_ElementBegin(rsc);
230 rsi_ElementAdd(rsc, RS_KIND_USER, RS_TYPE_FLOAT, false, 32, NULL);
231 RsElement e = rsi_ElementCreate(rsc);
232
233 rsi_TypeBegin(rsc, e);
234 rsi_TypeAdd(rsc, RS_DIMENSION_X, 48);
Jason Samsf2649a92009-09-25 16:37:33 -0700235 mAllocType.set((Type *)rsi_TypeCreate(rsc));
Jason Sams8287c0c2009-09-24 12:33:45 -0700236
Jason Samse514b452009-09-25 14:51:22 -0700237 ProgramVertex *pv = new ProgramVertex(rsc, NULL, NULL);
Jason Samsf2649a92009-09-25 16:37:33 -0700238 Allocation *alloc = (Allocation *)rsi_AllocationCreateTyped(rsc, mAllocType.get());
Jason Sams8ce125b2009-06-17 16:52:59 -0700239 mDefaultAlloc.set(alloc);
240 mDefault.set(pv);
Jason Samsc460e552009-11-25 13:22:07 -0800241 pv->init(rsc);
Jason Samscfb1d112009-08-05 13:57:03 -0700242 pv->bindAllocation(alloc);
243
Jason Samse18844a2009-11-12 16:09:45 -0800244 updateSize(rsc, w, h);
245}
246
247void ProgramVertexState::updateSize(Context *rsc, int32_t w, int32_t h)
248{
Jason Sams8ce125b2009-06-17 16:52:59 -0700249 Matrix m;
250 m.loadOrtho(0,w, h,0, -1,1);
Jason Samse18844a2009-11-12 16:09:45 -0800251 mDefaultAlloc->subData(RS_PROGRAM_VERTEX_PROJECTION_OFFSET, 16, &m.m[0], 16*4);
Jason Sams8ce125b2009-06-17 16:52:59 -0700252
253 m.loadIdentity();
Jason Samse18844a2009-11-12 16:09:45 -0800254 mDefaultAlloc->subData(RS_PROGRAM_VERTEX_MODELVIEW_OFFSET, 16, &m.m[0], 16*4);
Jason Sams8ce125b2009-06-17 16:52:59 -0700255}
Jason Sams326e0dd2009-05-22 14:03:28 -0700256
Jason Samsf2649a92009-09-25 16:37:33 -0700257void ProgramVertexState::deinit(Context *rsc)
258{
259 mDefaultAlloc.clear();
260 mDefault.clear();
261 mAllocType.clear();
262 mLast.clear();
263 delete mPV;
264 mPV = NULL;
265}
266
Jason Sams326e0dd2009-05-22 14:03:28 -0700267
268namespace android {
269namespace renderscript {
270
271void rsi_ProgramVertexBegin(Context *rsc, RsElement in, RsElement out)
272{
273 delete rsc->mStateVertex.mPV;
Jason Samse514b452009-09-25 14:51:22 -0700274 rsc->mStateVertex.mPV = new ProgramVertex(rsc, (Element *)in, (Element *)out);
Jason Sams326e0dd2009-05-22 14:03:28 -0700275}
276
277RsProgramVertex rsi_ProgramVertexCreate(Context *rsc)
278{
279 ProgramVertex *pv = rsc->mStateVertex.mPV;
Jason Sams9397e302009-08-27 20:23:34 -0700280 pv->incUserRef();
Jason Samsc460e552009-11-25 13:22:07 -0800281 pv->init(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700282 rsc->mStateVertex.mPV = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700283 return pv;
284}
285
Jason Samscfb1d112009-08-05 13:57:03 -0700286void rsi_ProgramVertexBindAllocation(Context *rsc, RsProgramVertex vpgm, RsAllocation constants)
Jason Sams326e0dd2009-05-22 14:03:28 -0700287{
288 ProgramVertex *pv = static_cast<ProgramVertex *>(vpgm);
Jason Samscfb1d112009-08-05 13:57:03 -0700289 pv->bindAllocation(static_cast<Allocation *>(constants));
Jason Sams326e0dd2009-05-22 14:03:28 -0700290}
291
Jason Sams326e0dd2009-05-22 14:03:28 -0700292void rsi_ProgramVertexSetTextureMatrixEnable(Context *rsc, bool enable)
293{
294 rsc->mStateVertex.mPV->setTextureMatrixEnable(enable);
295}
296
Jason Samsb5909ce2009-07-21 12:20:54 -0700297void rsi_ProgramVertexAddLight(Context *rsc, RsLight light)
298{
299 rsc->mStateVertex.mPV->addLight(static_cast<const Light *>(light));
300}
Jason Sams326e0dd2009-05-22 14:03:28 -0700301
302
303}
304}