blob: 153e2d6844aebd3cad7e038b7e4c6548545885b3 [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 Sams4815c0d2009-12-15 12:58:36 -080029ProgramVertex::ProgramVertex(Context *rsc, bool texMat) :
30 Program(rsc)
31{
32 mAllocFile = __FILE__;
33 mAllocLine = __LINE__;
34 mTextureMatrixEnable = texMat;
35 mLightCount = 0;
36 init(rsc);
37}
38
39ProgramVertex::ProgramVertex(Context *rsc, const char * shaderText,
40 uint32_t shaderLength, const uint32_t * params,
41 uint32_t paramLength) :
42 Program(rsc, shaderText, shaderLength, params, paramLength)
Jason Sams326e0dd2009-05-22 14:03:28 -070043{
Jason Samsf2649a92009-09-25 16:37:33 -070044 mAllocFile = __FILE__;
45 mAllocLine = __LINE__;
Jason Sams326e0dd2009-05-22 14:03:28 -070046 mTextureMatrixEnable = false;
Jason Samsb5909ce2009-07-21 12:20:54 -070047 mLightCount = 0;
Jason Sams4815c0d2009-12-15 12:58:36 -080048
49 init(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -070050}
51
52ProgramVertex::~ProgramVertex()
53{
54}
55
Jason Sams56bc1af2009-06-16 17:49:58 -070056static void logMatrix(const char *txt, const float *f)
57{
Jason Sams992a0b72009-06-23 12:22:47 -070058 LOGV("Matrix %s, %p", txt, f);
59 LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[0], f[4], f[8], f[12]);
60 LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[1], f[5], f[9], f[13]);
61 LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[2], f[6], f[10], f[14]);
62 LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[3], f[7], f[11], f[15]);
Jason Sams56bc1af2009-06-16 17:49:58 -070063}
64
Jason Samsafcb25c2009-08-25 11:34:49 -070065void ProgramVertex::setupGL(const Context *rsc, ProgramVertexState *state)
Jason Sams326e0dd2009-05-22 14:03:28 -070066{
Jason Samscfb1d112009-08-05 13:57:03 -070067 if ((state->mLast.get() == this) && !mDirty) {
68 return;
69 }
70 state->mLast.set(this);
71
72 const float *f = static_cast<const float *>(mConstants->getPtr());
Jason Sams326e0dd2009-05-22 14:03:28 -070073
74 glMatrixMode(GL_TEXTURE);
75 if (mTextureMatrixEnable) {
76 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]);
77 } else {
78 glLoadIdentity();
79 }
80
Jason Samsb5909ce2009-07-21 12:20:54 -070081 glMatrixMode(GL_MODELVIEW);
82 glLoadIdentity();
83 if (mLightCount) {
Romain Guy48b7edc2009-08-06 22:52:13 -070084 int v = 0;
Jason Samsb5909ce2009-07-21 12:20:54 -070085 glEnable(GL_LIGHTING);
86 glLightModelxv(GL_LIGHT_MODEL_TWO_SIDE, &v);
87 for (uint32_t ct = 0; ct < mLightCount; ct++) {
88 const Light *l = mLights[ct].get();
89 glEnable(GL_LIGHT0 + ct);
90 l->setupGL(ct);
91 }
92 for (uint32_t ct = mLightCount; ct < MAX_LIGHTS; ct++) {
93 glDisable(GL_LIGHT0 + ct);
94 }
95 } else {
96 glDisable(GL_LIGHTING);
97 }
Jason Samscfb1d112009-08-05 13:57:03 -070098
Jason Samsb5909ce2009-07-21 12:20:54 -070099 if (!f) {
100 LOGE("Must bind constants to vertex program");
101 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700102
103 glMatrixMode(GL_PROJECTION);
Jason Sams56bc1af2009-06-16 17:49:58 -0700104 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700105 glMatrixMode(GL_MODELVIEW);
Jason Sams56bc1af2009-06-16 17:49:58 -0700106 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700107
Jason Samscfb1d112009-08-05 13:57:03 -0700108 mDirty = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700109}
110
Jason Samscd506532009-12-15 19:10:11 -0800111void ProgramVertex::loadShader(Context *rsc) {
112 Program::loadShader(rsc, GL_VERTEX_SHADER);
Jason Samsc460e552009-11-25 13:22:07 -0800113}
114
115void ProgramVertex::createShader()
116{
117 mShader.setTo("");
118
119 for (uint32_t ct=0; ct < mAttribCount; ct++) {
120 mShader.append("attribute vec4 ");
121 mShader.append(mAttribNames[ct]);
122 mShader.append(";\n");
123 }
124
125 for (uint32_t ct=0; ct < mUniformCount; ct++) {
126 mShader.append("uniform mat4 ");
127 mShader.append(mUniformNames[ct]);
128 mShader.append(";\n");
129 }
130
131 mShader.append("varying vec4 varColor;\n");
132 mShader.append("varying vec4 varTex0;\n");
133
Jason Samsf2a5d732009-11-30 14:49:55 -0800134 if (mUserShader.length() > 1) {
135 mShader.append(mUserShader);
Jason Samsc460e552009-11-25 13:22:07 -0800136 } else {
Jason Samsf2a5d732009-11-30 14:49:55 -0800137 mShader.append("void main() {\n");
138 mShader.append(" gl_Position = uni_MVP * attrib_Position;\n");
139 mShader.append(" gl_PointSize = attrib_PointSize.x;\n");
140
141 mShader.append(" varColor = attrib_Color;\n");
142 if (mTextureMatrixEnable) {
143 mShader.append(" varTex0 = uni_TexMatrix * attrib_T0;\n");
144 } else {
145 mShader.append(" varTex0 = attrib_T0;\n");
146 }
147 //mShader.append(" pos.x = pos.x / 480.0;\n");
148 //mShader.append(" pos.y = pos.y / 800.0;\n");
149 //mShader.append(" gl_Position = pos;\n");
150 mShader.append("}\n");
Jason Samsc460e552009-11-25 13:22:07 -0800151 }
Jason Samsc460e552009-11-25 13:22:07 -0800152}
153
154void ProgramVertex::setupGL2(const Context *rsc, ProgramVertexState *state, ShaderCache *sc)
155{
156 //LOGE("sgl2 vtx1 %x", glGetError());
157 if ((state->mLast.get() == this) && !mDirty) {
158 //return;
159 }
160
Jason Samse9ed6cc2009-12-16 14:13:06 -0800161 glVertexAttrib4f(1, state->color[0], state->color[1], state->color[2], state->color[3]);
162
Jason Samsc460e552009-11-25 13:22:07 -0800163 const float *f = static_cast<const float *>(mConstants->getPtr());
164
165 Matrix mvp;
166 mvp.load(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
167 Matrix t;
168 t.load(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]);
169 mvp.multiply(&t);
170
171 glUniformMatrix4fv(sc->vtxUniformSlot(0), 1, GL_FALSE, mvp.m);
172 if (mTextureMatrixEnable) {
173 glUniformMatrix4fv(sc->vtxUniformSlot(1), 1, GL_FALSE,
174 &f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]);
175 }
176
177 state->mLast.set(this);
178 //LOGE("sgl2 vtx2 %x", glGetError());
179}
180
Jason Samsb5909ce2009-07-21 12:20:54 -0700181void ProgramVertex::addLight(const Light *l)
182{
183 if (mLightCount < MAX_LIGHTS) {
184 mLights[mLightCount].set(l);
185 mLightCount++;
186 }
187}
188
Jason Samsc9d43db2009-07-28 12:02:16 -0700189void ProgramVertex::setProjectionMatrix(const rsc_Matrix *m) const
190{
Jason Samscfb1d112009-08-05 13:57:03 -0700191 float *f = static_cast<float *>(mConstants->getPtr());
Jason Samsc9d43db2009-07-28 12:02:16 -0700192 memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix));
Jason Samscfb1d112009-08-05 13:57:03 -0700193 mDirty = true;
Jason Samsc9d43db2009-07-28 12:02:16 -0700194}
195
196void ProgramVertex::setModelviewMatrix(const rsc_Matrix *m) const
197{
Jason Samscfb1d112009-08-05 13:57:03 -0700198 float *f = static_cast<float *>(mConstants->getPtr());
Jason Samsc9d43db2009-07-28 12:02:16 -0700199 memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix));
Jason Samscfb1d112009-08-05 13:57:03 -0700200 mDirty = true;
Jason Samsc9d43db2009-07-28 12:02:16 -0700201}
202
203void ProgramVertex::setTextureMatrix(const rsc_Matrix *m) const
204{
Jason Samscfb1d112009-08-05 13:57:03 -0700205 float *f = static_cast<float *>(mConstants->getPtr());
Jason Samsc9d43db2009-07-28 12:02:16 -0700206 memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix));
Jason Samscfb1d112009-08-05 13:57:03 -0700207 mDirty = true;
Jason Samsc9d43db2009-07-28 12:02:16 -0700208}
209
Jason Sams3a97c592009-09-30 17:36:20 -0700210void ProgramVertex::transformToScreen(const Context *rsc, float *v4out, const float *v3in) const
211{
212 float *f = static_cast<float *>(mConstants->getPtr());
213 Matrix mvp;
214 mvp.loadMultiply((Matrix *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET],
215 (Matrix *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]);
216 mvp.vectorMultiply(v4out, v3in);
217}
Jason Sams326e0dd2009-05-22 14:03:28 -0700218
Jason Samsc460e552009-11-25 13:22:07 -0800219void ProgramVertex::init(Context *rsc)
220{
221 mAttribCount = 6;
222 mAttribNames[VertexArray::POSITION].setTo("attrib_Position");
223 mAttribNames[VertexArray::COLOR].setTo("attrib_Color");
224 mAttribNames[VertexArray::NORMAL].setTo("attrib_Normal");
225 mAttribNames[VertexArray::POINT_SIZE].setTo("attrib_PointSize");
226 mAttribNames[VertexArray::TEXTURE_0].setTo("attrib_T0");
227 mAttribNames[VertexArray::TEXTURE_1].setTo("attrib_T1");
228
229 mUniformCount = 2;
230 mUniformNames[0].setTo("uni_MVP");
231 mUniformNames[1].setTo("uni_TexMatrix");
232
233 createShader();
234}
235
Jason Sams4815c0d2009-12-15 12:58:36 -0800236
Jason Samsc460e552009-11-25 13:22:07 -0800237///////////////////////////////////////////////////////////////////////
238
Jason Sams326e0dd2009-05-22 14:03:28 -0700239ProgramVertexState::ProgramVertexState()
240{
Jason Sams326e0dd2009-05-22 14:03:28 -0700241}
242
243ProgramVertexState::~ProgramVertexState()
244{
Jason Sams326e0dd2009-05-22 14:03:28 -0700245}
246
Jason Sams8ce125b2009-06-17 16:52:59 -0700247void ProgramVertexState::init(Context *rsc, int32_t w, int32_t h)
248{
Jason Sams8287c0c2009-09-24 12:33:45 -0700249 rsi_ElementBegin(rsc);
250 rsi_ElementAdd(rsc, RS_KIND_USER, RS_TYPE_FLOAT, false, 32, NULL);
251 RsElement e = rsi_ElementCreate(rsc);
252
253 rsi_TypeBegin(rsc, e);
254 rsi_TypeAdd(rsc, RS_DIMENSION_X, 48);
Jason Samsf2649a92009-09-25 16:37:33 -0700255 mAllocType.set((Type *)rsi_TypeCreate(rsc));
Jason Sams8287c0c2009-09-24 12:33:45 -0700256
Jason Sams4815c0d2009-12-15 12:58:36 -0800257 ProgramVertex *pv = new ProgramVertex(rsc, false);
Jason Samsf2649a92009-09-25 16:37:33 -0700258 Allocation *alloc = (Allocation *)rsi_AllocationCreateTyped(rsc, mAllocType.get());
Jason Sams8ce125b2009-06-17 16:52:59 -0700259 mDefaultAlloc.set(alloc);
260 mDefault.set(pv);
Jason Samsc460e552009-11-25 13:22:07 -0800261 pv->init(rsc);
Jason Samscfb1d112009-08-05 13:57:03 -0700262 pv->bindAllocation(alloc);
263
Jason Samse9ed6cc2009-12-16 14:13:06 -0800264 color[0] = 1.f;
265 color[1] = 1.f;
266 color[2] = 1.f;
267 color[3] = 1.f;
268
Jason Samse18844a2009-11-12 16:09:45 -0800269 updateSize(rsc, w, h);
270}
271
272void ProgramVertexState::updateSize(Context *rsc, int32_t w, int32_t h)
273{
Jason Sams8ce125b2009-06-17 16:52:59 -0700274 Matrix m;
275 m.loadOrtho(0,w, h,0, -1,1);
Jason Samse18844a2009-11-12 16:09:45 -0800276 mDefaultAlloc->subData(RS_PROGRAM_VERTEX_PROJECTION_OFFSET, 16, &m.m[0], 16*4);
Jason Sams8ce125b2009-06-17 16:52:59 -0700277
278 m.loadIdentity();
Jason Samse18844a2009-11-12 16:09:45 -0800279 mDefaultAlloc->subData(RS_PROGRAM_VERTEX_MODELVIEW_OFFSET, 16, &m.m[0], 16*4);
Jason Sams8ce125b2009-06-17 16:52:59 -0700280}
Jason Sams326e0dd2009-05-22 14:03:28 -0700281
Jason Samsf2649a92009-09-25 16:37:33 -0700282void ProgramVertexState::deinit(Context *rsc)
283{
284 mDefaultAlloc.clear();
285 mDefault.clear();
286 mAllocType.clear();
287 mLast.clear();
Jason Samsf2649a92009-09-25 16:37:33 -0700288}
289
Jason Sams326e0dd2009-05-22 14:03:28 -0700290
291namespace android {
292namespace renderscript {
293
Jason Sams326e0dd2009-05-22 14:03:28 -0700294
Jason Sams4815c0d2009-12-15 12:58:36 -0800295RsProgramVertex rsi_ProgramVertexCreate(Context *rsc, bool texMat)
Jason Sams326e0dd2009-05-22 14:03:28 -0700296{
Jason Sams4815c0d2009-12-15 12:58:36 -0800297 ProgramVertex *pv = new ProgramVertex(rsc, texMat);
Jason Sams9397e302009-08-27 20:23:34 -0700298 pv->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700299 return pv;
300}
301
Jason Sams4815c0d2009-12-15 12:58:36 -0800302RsProgramVertex rsi_ProgramVertexCreate2(Context *rsc, const char * shaderText,
303 uint32_t shaderLength, const uint32_t * params,
304 uint32_t paramLength)
Jason Sams326e0dd2009-05-22 14:03:28 -0700305{
Jason Sams4815c0d2009-12-15 12:58:36 -0800306 ProgramVertex *pv = new ProgramVertex(rsc, shaderText, shaderLength, params, paramLength);
307 pv->incUserRef();
308 return pv;
Jason Samsb5909ce2009-07-21 12:20:54 -0700309}
Jason Sams326e0dd2009-05-22 14:03:28 -0700310
311
312}
313}