rsProgramVertex.cpp revision 4c9a208262a1fc9ba062d21b8c6c787b8a822991
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.4f, %6.4f, %6.4f, %6.4f", f[0], f[4], f[8], f[12]); 60 LOGV("%6.4f, %6.4f, %6.4f, %6.4f", f[1], f[5], f[9], f[13]); 61 LOGV("%6.4f, %6.4f, %6.4f, %6.4f", f[2], f[6], f[10], f[14]); 62 LOGV("%6.4f, %6.4f, %6.4f, %6.4f", 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[0]->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 mShader.append("varying vec4 varColor;\n"); 120 mShader.append("varying vec4 varTex0;\n"); 121 122 if (mUserShader.length() > 1) { 123 mShader.append("uniform mat4 "); 124 mShader.append(mUniformNames[0]); 125 mShader.append(";\n"); 126 127 for (uint32_t ct=0; ct < mConstantCount; ct++) { 128 const Element *e = mConstantTypes[ct]->getElement(); 129 for (uint32_t field=0; field < e->getFieldCount(); field++) { 130 const Element *f = e->getField(field); 131 132 // Cannot be complex 133 rsAssert(!f->getFieldCount()); 134 switch(f->getComponent().getVectorSize()) { 135 case 1: mShader.append("uniform float UNI_"); break; 136 case 2: mShader.append("uniform vec2 UNI_"); break; 137 case 3: mShader.append("uniform vec3 UNI_"); break; 138 case 4: mShader.append("uniform vec4 UNI_"); break; 139 default: 140 rsAssert(0); 141 } 142 143 mShader.append(e->getFieldName(field)); 144 mShader.append(";\n"); 145 } 146 } 147 148 149 for (uint32_t ct=0; ct < mInputCount; ct++) { 150 const Element *e = mInputElements[ct].get(); 151 for (uint32_t field=0; field < e->getFieldCount(); field++) { 152 const Element *f = e->getField(field); 153 154 // Cannot be complex 155 rsAssert(!f->getFieldCount()); 156 switch(f->getComponent().getVectorSize()) { 157 case 1: mShader.append("attribute float ATTRIB_"); break; 158 case 2: mShader.append("attribute vec2 ATTRIB_"); break; 159 case 3: mShader.append("attribute vec3 ATTRIB_"); break; 160 case 4: mShader.append("attribute vec4 ATTRIB_"); break; 161 default: 162 rsAssert(0); 163 } 164 165 mShader.append(e->getFieldName(field)); 166 mShader.append(";\n"); 167 } 168 } 169 mShader.append(mUserShader); 170 } else { 171 for (uint32_t ct=0; ct < mUniformCount; ct++) { 172 mShader.append("uniform mat4 "); 173 mShader.append(mUniformNames[ct]); 174 mShader.append(";\n"); 175 } 176 177 for (uint32_t ct=VertexArray::POSITION; ct < mAttribCount; ct++) { 178 mShader.append("attribute vec4 "); 179 mShader.append(mAttribNames[ct]); 180 mShader.append(";\n"); 181 } 182 183 mShader.append("void main() {\n"); 184 mShader.append(" gl_Position = UNI_MVP * ATTRIB_Position;\n"); 185 mShader.append(" gl_PointSize = ATTRIB_PointSize.x;\n"); 186 187 mShader.append(" varColor = ATTRIB_Color;\n"); 188 if (mTextureMatrixEnable) { 189 mShader.append(" varTex0 = UNI_TexMatrix * ATTRIB_Texture;\n"); 190 } else { 191 mShader.append(" varTex0 = ATTRIB_Texture;\n"); 192 } 193 //mShader.append(" pos.x = pos.x / 480.0;\n"); 194 //mShader.append(" pos.y = pos.y / 800.0;\n"); 195 //mShader.append(" gl_Position = pos;\n"); 196 mShader.append("}\n"); 197 } 198} 199 200void ProgramVertex::setupGL2(const Context *rsc, ProgramVertexState *state, ShaderCache *sc) 201{ 202 //LOGE("sgl2 vtx1 %x", glGetError()); 203 if ((state->mLast.get() == this) && !mDirty) { 204 //return; 205 } 206 207 glVertexAttrib4f(1, state->color[0], state->color[1], state->color[2], state->color[3]); 208 209 const float *f = static_cast<const float *>(mConstants[0]->getPtr()); 210 211 Matrix mvp; 212 mvp.load(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); 213 Matrix t; 214 t.load(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]); 215 mvp.multiply(&t); 216 217 glUniformMatrix4fv(sc->vtxUniformSlot(0), 1, GL_FALSE, mvp.m); 218 if (mTextureMatrixEnable) { 219 glUniformMatrix4fv(sc->vtxUniformSlot(1), 1, GL_FALSE, 220 &f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]); 221 } 222 223 uint32_t uidx = 1; 224 for (uint32_t ct=0; ct < mConstantCount; ct++) { 225 Allocation *alloc = mConstants[ct+1].get(); 226 if (!alloc) { 227 continue; 228 } 229 230 const uint8_t *data = static_cast<const uint8_t *>(alloc->getPtr()); 231 const Element *e = mConstantTypes[ct]->getElement(); 232 for (uint32_t field=0; field < e->getFieldCount(); field++) { 233 const Element *f = e->getField(field); 234 uint32_t offset = e->getFieldOffsetBytes(field); 235 int32_t slot = sc->vtxUniformSlot(uidx); 236 237 const float *fd = reinterpret_cast<const float *>(&data[offset]); 238 239 //LOGE("Uniform slot=%i, offset=%i, constant=%i, field=%i, uidx=%i", slot, offset, ct, field, uidx); 240 if (slot >= 0) { 241 switch(f->getComponent().getVectorSize()) { 242 case 1: 243 //LOGE("Uniform 1 = %f", fd[0]); 244 glUniform1fv(slot, 1, fd); 245 break; 246 case 2: 247 //LOGE("Uniform 2 = %f %f", fd[0], fd[1]); 248 glUniform2fv(slot, 1, fd); 249 break; 250 case 3: 251 //LOGE("Uniform 3 = %f %f %f", fd[0], fd[1], fd[2]); 252 glUniform3fv(slot, 1, fd); 253 break; 254 case 4: 255 //LOGE("Uniform 4 = %f %f %f %f", fd[0], fd[1], fd[2], fd[3]); 256 glUniform4fv(slot, 1, fd); 257 break; 258 default: 259 rsAssert(0); 260 } 261 } 262 uidx ++; 263 } 264 } 265 266 for (uint32_t ct=0; ct < mConstantCount; ct++) { 267 uint32_t glSlot = sc->vtxUniformSlot(ct + 1); 268 269 } 270 271 state->mLast.set(this); 272 rsc->checkError("ProgramVertex::setupGL2"); 273} 274 275void ProgramVertex::addLight(const Light *l) 276{ 277 if (mLightCount < MAX_LIGHTS) { 278 mLights[mLightCount].set(l); 279 mLightCount++; 280 } 281} 282 283void ProgramVertex::setProjectionMatrix(const rsc_Matrix *m) const 284{ 285 float *f = static_cast<float *>(mConstants[0]->getPtr()); 286 memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix)); 287 mDirty = true; 288} 289 290void ProgramVertex::setModelviewMatrix(const rsc_Matrix *m) const 291{ 292 float *f = static_cast<float *>(mConstants[0]->getPtr()); 293 memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix)); 294 mDirty = true; 295} 296 297void ProgramVertex::setTextureMatrix(const rsc_Matrix *m) const 298{ 299 float *f = static_cast<float *>(mConstants[0]->getPtr()); 300 memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix)); 301 mDirty = true; 302} 303 304void ProgramVertex::transformToScreen(const Context *rsc, float *v4out, const float *v3in) const 305{ 306 float *f = static_cast<float *>(mConstants[0]->getPtr()); 307 Matrix mvp; 308 mvp.loadMultiply((Matrix *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], 309 (Matrix *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); 310 mvp.vectorMultiply(v4out, v3in); 311} 312 313void ProgramVertex::initAddUserElement(const Element *e, String8 *names, uint32_t *count, const char *prefix) 314{ 315 rsAssert(e->getFieldCount()); 316 for (uint32_t ct=0; ct < e->getFieldCount(); ct++) { 317 const Element *ce = e->getField(ct); 318 if (ce->getFieldCount()) { 319 initAddUserElement(ce, names, count, prefix); 320 } else { 321 String8 tmp(prefix); 322 tmp.append(e->getFieldName(ct)); 323 names[*count].setTo(tmp.string()); 324 (*count)++; 325 } 326 } 327} 328 329void ProgramVertex::init(Context *rsc) 330{ 331 if (mUserShader.size() > 0) { 332 mAttribCount = 0; 333 for (uint32_t ct=0; ct < mInputCount; ct++) { 334 initAddUserElement(mInputElements[ct].get(), mAttribNames, &mAttribCount, "ATTRIB_"); 335 } 336 337 mUniformCount = 1; 338 mUniformNames[0].setTo("UNI_MVP"); 339 for (uint32_t ct=0; ct < mConstantCount; ct++) { 340 initAddUserElement(mConstantTypes[ct]->getElement(), mUniformNames, &mUniformCount, "UNI_"); 341 } 342 } else { 343 mAttribCount = 5; 344 mAttribNames[0].setTo("ATTRIB_Position"); 345 mAttribNames[1].setTo("ATTRIB_Color"); 346 mAttribNames[2].setTo("ATTRIB_Normal"); 347 mAttribNames[3].setTo("ATTRIB_PointSize"); 348 mAttribNames[4].setTo("ATTRIB_Texture"); 349 350 mUniformCount = 2; 351 mUniformNames[0].setTo("UNI_MVP"); 352 mUniformNames[1].setTo("UNI_TexMatrix"); 353 } 354 355 createShader(); 356} 357 358 359/////////////////////////////////////////////////////////////////////// 360 361ProgramVertexState::ProgramVertexState() 362{ 363} 364 365ProgramVertexState::~ProgramVertexState() 366{ 367} 368 369void ProgramVertexState::init(Context *rsc, int32_t w, int32_t h) 370{ 371 RsElement e = Element::create(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 1); 372 373 rsi_TypeBegin(rsc, e); 374 rsi_TypeAdd(rsc, RS_DIMENSION_X, 48); 375 mAllocType.set((Type *)rsi_TypeCreate(rsc)); 376 377 ProgramVertex *pv = new ProgramVertex(rsc, false); 378 Allocation *alloc = (Allocation *)rsi_AllocationCreateTyped(rsc, mAllocType.get()); 379 mDefaultAlloc.set(alloc); 380 mDefault.set(pv); 381 pv->init(rsc); 382 pv->bindAllocation(alloc, 0); 383 384 color[0] = 1.f; 385 color[1] = 1.f; 386 color[2] = 1.f; 387 color[3] = 1.f; 388 389 updateSize(rsc, w, h); 390} 391 392void ProgramVertexState::updateSize(Context *rsc, int32_t w, int32_t h) 393{ 394 Matrix m; 395 m.loadOrtho(0,w, h,0, -1,1); 396 mDefaultAlloc->subData(RS_PROGRAM_VERTEX_PROJECTION_OFFSET, 16, &m.m[0], 16*4); 397 398 m.loadIdentity(); 399 mDefaultAlloc->subData(RS_PROGRAM_VERTEX_MODELVIEW_OFFSET, 16, &m.m[0], 16*4); 400} 401 402void ProgramVertexState::deinit(Context *rsc) 403{ 404 mDefaultAlloc.clear(); 405 mDefault.clear(); 406 mAllocType.clear(); 407 mLast.clear(); 408} 409 410 411namespace android { 412namespace renderscript { 413 414 415RsProgramVertex rsi_ProgramVertexCreate(Context *rsc, bool texMat) 416{ 417 ProgramVertex *pv = new ProgramVertex(rsc, texMat); 418 pv->incUserRef(); 419 return pv; 420} 421 422RsProgramVertex rsi_ProgramVertexCreate2(Context *rsc, const char * shaderText, 423 uint32_t shaderLength, const uint32_t * params, 424 uint32_t paramLength) 425{ 426 ProgramVertex *pv = new ProgramVertex(rsc, shaderText, shaderLength, params, paramLength); 427 pv->incUserRef(); 428 return pv; 429} 430 431 432} 433} 434