rsProgramVertex.cpp revision d01d970cf5973aa5186cc02c80fb2c143a69b0b1
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 "rsProgramVertex.h" 19 20#include <GLES/gl.h> 21#include <GLES/glext.h> 22#include <GLES2/gl2.h> 23#include <GLES2/gl2ext.h> 24 25using namespace android; 26using namespace android::renderscript; 27 28 29ProgramVertex::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) 43{ 44 mAllocFile = __FILE__; 45 mAllocLine = __LINE__; 46 mTextureMatrixEnable = false; 47 mLightCount = 0; 48 49 init(rsc); 50} 51 52ProgramVertex::~ProgramVertex() 53{ 54} 55 56static void logMatrix(const char *txt, const float *f) 57{ 58 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]); 63} 64 65void ProgramVertex::setupGL(const Context *rsc, ProgramVertexState *state) 66{ 67 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()); 73 74 glMatrixMode(GL_TEXTURE); 75 if (mTextureMatrixEnable) { 76 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]); 77 } else { 78 glLoadIdentity(); 79 } 80 81 glMatrixMode(GL_MODELVIEW); 82 glLoadIdentity(); 83 if (mLightCount) { 84 int v = 0; 85 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 } 98 99 if (!f) { 100 LOGE("Must bind constants to vertex program"); 101 } 102 103 glMatrixMode(GL_PROJECTION); 104 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); 105 glMatrixMode(GL_MODELVIEW); 106 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]); 107 108 mDirty = false; 109} 110 111void ProgramVertex::loadShader(Context *rsc) { 112 Program::loadShader(rsc, GL_VERTEX_SHADER); 113} 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 134 if (mUserShader.length() > 1) { 135 mShader.append(mUserShader); 136 } else { 137 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"); 151 } 152} 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 161 glVertexAttrib4f(1, state->color[0], state->color[1], state->color[2], state->color[3]); 162 163 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 181void ProgramVertex::addLight(const Light *l) 182{ 183 if (mLightCount < MAX_LIGHTS) { 184 mLights[mLightCount].set(l); 185 mLightCount++; 186 } 187} 188 189void ProgramVertex::setProjectionMatrix(const rsc_Matrix *m) const 190{ 191 float *f = static_cast<float *>(mConstants->getPtr()); 192 memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix)); 193 mDirty = true; 194} 195 196void ProgramVertex::setModelviewMatrix(const rsc_Matrix *m) const 197{ 198 float *f = static_cast<float *>(mConstants->getPtr()); 199 memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix)); 200 mDirty = true; 201} 202 203void ProgramVertex::setTextureMatrix(const rsc_Matrix *m) const 204{ 205 float *f = static_cast<float *>(mConstants->getPtr()); 206 memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix)); 207 mDirty = true; 208} 209 210void 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} 218 219void 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 236 237/////////////////////////////////////////////////////////////////////// 238 239ProgramVertexState::ProgramVertexState() 240{ 241} 242 243ProgramVertexState::~ProgramVertexState() 244{ 245} 246 247void ProgramVertexState::init(Context *rsc, int32_t w, int32_t h) 248{ 249 RsElement e = Element::create(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 1); 250 251 rsi_TypeBegin(rsc, e); 252 rsi_TypeAdd(rsc, RS_DIMENSION_X, 48); 253 mAllocType.set((Type *)rsi_TypeCreate(rsc)); 254 255 ProgramVertex *pv = new ProgramVertex(rsc, false); 256 Allocation *alloc = (Allocation *)rsi_AllocationCreateTyped(rsc, mAllocType.get()); 257 mDefaultAlloc.set(alloc); 258 mDefault.set(pv); 259 pv->init(rsc); 260 pv->bindAllocation(alloc); 261 262 color[0] = 1.f; 263 color[1] = 1.f; 264 color[2] = 1.f; 265 color[3] = 1.f; 266 267 updateSize(rsc, w, h); 268} 269 270void ProgramVertexState::updateSize(Context *rsc, int32_t w, int32_t h) 271{ 272 Matrix m; 273 m.loadOrtho(0,w, h,0, -1,1); 274 mDefaultAlloc->subData(RS_PROGRAM_VERTEX_PROJECTION_OFFSET, 16, &m.m[0], 16*4); 275 276 m.loadIdentity(); 277 mDefaultAlloc->subData(RS_PROGRAM_VERTEX_MODELVIEW_OFFSET, 16, &m.m[0], 16*4); 278} 279 280void ProgramVertexState::deinit(Context *rsc) 281{ 282 mDefaultAlloc.clear(); 283 mDefault.clear(); 284 mAllocType.clear(); 285 mLast.clear(); 286} 287 288 289namespace android { 290namespace renderscript { 291 292 293RsProgramVertex rsi_ProgramVertexCreate(Context *rsc, bool texMat) 294{ 295 ProgramVertex *pv = new ProgramVertex(rsc, texMat); 296 pv->incUserRef(); 297 return pv; 298} 299 300RsProgramVertex rsi_ProgramVertexCreate2(Context *rsc, const char * shaderText, 301 uint32_t shaderLength, const uint32_t * params, 302 uint32_t paramLength) 303{ 304 ProgramVertex *pv = new ProgramVertex(rsc, shaderText, shaderLength, params, paramLength); 305 pv->incUserRef(); 306 return pv; 307} 308 309 310} 311} 312