rsProgramVertex.cpp revision af83e79895d05ba45c2f8e340f2222dc19e2fffa
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#ifndef ANDROID_RS_BUILD_FOR_HOST 18#include "rsContext.h" 19#include <GLES/gl.h> 20#include <GLES/glext.h> 21#include <GLES2/gl2.h> 22#include <GLES2/gl2ext.h> 23#else 24#include "rsContextHostStub.h" 25#include <OpenGL/gl.h> 26#include <OpenGL/glext.h> 27#endif //ANDROID_RS_BUILD_FOR_HOST 28 29#include "rsProgramVertex.h" 30 31using namespace android; 32using namespace android::renderscript; 33 34 35ProgramVertex::ProgramVertex(Context *rsc, bool texMat) : 36 Program(rsc) 37{ 38 mAllocFile = __FILE__; 39 mAllocLine = __LINE__; 40 mTextureMatrixEnable = texMat; 41 mLightCount = 0; 42 init(rsc); 43} 44 45ProgramVertex::ProgramVertex(Context *rsc, const char * shaderText, 46 uint32_t shaderLength, const uint32_t * params, 47 uint32_t paramLength) : 48 Program(rsc, shaderText, shaderLength, params, paramLength) 49{ 50 mAllocFile = __FILE__; 51 mAllocLine = __LINE__; 52 mTextureMatrixEnable = false; 53 mLightCount = 0; 54 55 init(rsc); 56} 57 58ProgramVertex::~ProgramVertex() 59{ 60} 61 62static void logMatrix(const char *txt, const float *f) 63{ 64 LOGV("Matrix %s, %p", txt, f); 65 LOGV("%6.4f, %6.4f, %6.4f, %6.4f", f[0], f[4], f[8], f[12]); 66 LOGV("%6.4f, %6.4f, %6.4f, %6.4f", f[1], f[5], f[9], f[13]); 67 LOGV("%6.4f, %6.4f, %6.4f, %6.4f", f[2], f[6], f[10], f[14]); 68 LOGV("%6.4f, %6.4f, %6.4f, %6.4f", f[3], f[7], f[11], f[15]); 69} 70 71void ProgramVertex::setupGL(const Context *rsc, ProgramVertexState *state) 72{ 73 if ((state->mLast.get() == this) && !mDirty) { 74 return; 75 } 76 state->mLast.set(this); 77 78 const float *f = static_cast<const float *>(mConstants[0]->getPtr()); 79 80 glMatrixMode(GL_TEXTURE); 81 if (mTextureMatrixEnable) { 82 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]); 83 } else { 84 glLoadIdentity(); 85 } 86 87 glMatrixMode(GL_MODELVIEW); 88 glLoadIdentity(); 89 if (mLightCount) { 90#ifndef ANDROID_RS_BUILD_FOR_HOST // GLES Only 91 int v = 0; 92 glEnable(GL_LIGHTING); 93 94 glLightModelxv(GL_LIGHT_MODEL_TWO_SIDE, &v); 95 96 for (uint32_t ct = 0; ct < mLightCount; ct++) { 97 const Light *l = mLights[ct].get(); 98 glEnable(GL_LIGHT0 + ct); 99 l->setupGL(ct); 100 } 101 for (uint32_t ct = mLightCount; ct < MAX_LIGHTS; ct++) { 102 glDisable(GL_LIGHT0 + ct); 103 } 104#endif //ANDROID_RS_BUILD_FOR_HOST 105 } else { 106 glDisable(GL_LIGHTING); 107 } 108 109 if (!f) { 110 LOGE("Must bind constants to vertex program"); 111 } 112 113 glMatrixMode(GL_PROJECTION); 114 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); 115 glMatrixMode(GL_MODELVIEW); 116 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]); 117 118 mDirty = false; 119} 120 121void ProgramVertex::loadShader(Context *rsc) { 122 Program::loadShader(rsc, GL_VERTEX_SHADER); 123} 124 125void ProgramVertex::createShader() 126{ 127 mShader.setTo(""); 128 129 mShader.append("varying vec4 varColor;\n"); 130 mShader.append("varying vec4 varTex0;\n"); 131 132 if (mUserShader.length() > 1) { 133 mShader.append("uniform mat4 "); 134 mShader.append(mUniformNames[0]); 135 mShader.append(";\n"); 136 137 for (uint32_t ct=0; ct < mConstantCount; ct++) { 138 const Element *e = mConstantTypes[ct]->getElement(); 139 for (uint32_t field=0; field < e->getFieldCount(); field++) { 140 const Element *f = e->getField(field); 141 const char *fn = e->getFieldName(field); 142 143 if (fn[0] == '#') { 144 continue; 145 } 146 147 // Cannot be complex 148 rsAssert(!f->getFieldCount()); 149 if(f->getType() == RS_TYPE_MATRIX_4X4) { 150 mShader.append("uniform mat4 UNI_"); 151 } 152 else if(f->getType() == RS_TYPE_MATRIX_3X3) { 153 mShader.append("uniform mat3 UNI_"); 154 } 155 else if(f->getType() == RS_TYPE_MATRIX_2X2) { 156 mShader.append("uniform mat2 UNI_"); 157 } 158 else { 159 switch(f->getComponent().getVectorSize()) { 160 case 1: mShader.append("uniform float UNI_"); break; 161 case 2: mShader.append("uniform vec2 UNI_"); break; 162 case 3: mShader.append("uniform vec3 UNI_"); break; 163 case 4: mShader.append("uniform vec4 UNI_"); break; 164 default: 165 rsAssert(0); 166 } 167 } 168 169 mShader.append(fn); 170 mShader.append(";\n"); 171 } 172 } 173 174 175 for (uint32_t ct=0; ct < mInputCount; ct++) { 176 const Element *e = mInputElements[ct].get(); 177 for (uint32_t field=0; field < e->getFieldCount(); field++) { 178 const Element *f = e->getField(field); 179 const char *fn = e->getFieldName(field); 180 181 if (fn[0] == '#') { 182 continue; 183 } 184 185 // Cannot be complex 186 rsAssert(!f->getFieldCount()); 187 switch(f->getComponent().getVectorSize()) { 188 case 1: mShader.append("attribute float ATTRIB_"); break; 189 case 2: mShader.append("attribute vec2 ATTRIB_"); break; 190 case 3: mShader.append("attribute vec3 ATTRIB_"); break; 191 case 4: mShader.append("attribute vec4 ATTRIB_"); break; 192 default: 193 rsAssert(0); 194 } 195 196 mShader.append(fn); 197 mShader.append(";\n"); 198 } 199 } 200 mShader.append(mUserShader); 201 } else { 202 mShader.append("attribute vec4 ATTRIB_position;\n"); 203 mShader.append("attribute vec4 ATTRIB_color;\n"); 204 mShader.append("attribute vec3 ATTRIB_normal;\n"); 205 mShader.append("attribute vec4 ATTRIB_texture0;\n"); 206 207 for (uint32_t ct=0; ct < mUniformCount; ct++) { 208 mShader.append("uniform mat4 "); 209 mShader.append(mUniformNames[ct]); 210 mShader.append(";\n"); 211 } 212 213 mShader.append("void main() {\n"); 214 mShader.append(" gl_Position = UNI_MVP * ATTRIB_position;\n"); 215 mShader.append(" gl_PointSize = 1.0;\n"); 216 217 mShader.append(" varColor = ATTRIB_color;\n"); 218 if (mTextureMatrixEnable) { 219 mShader.append(" varTex0 = UNI_TexMatrix * ATTRIB_texture0;\n"); 220 } else { 221 mShader.append(" varTex0 = ATTRIB_texture0;\n"); 222 } 223 mShader.append("}\n"); 224 } 225} 226 227void ProgramVertex::setupGL2(const Context *rsc, ProgramVertexState *state, ShaderCache *sc) 228{ 229 //LOGE("sgl2 vtx1 %x", glGetError()); 230 if ((state->mLast.get() == this) && !mDirty) { 231 return; 232 } 233 234 rsc->checkError("ProgramVertex::setupGL2 start"); 235 236 const float *f = static_cast<const float *>(mConstants[0]->getPtr()); 237 238 Matrix mvp; 239 mvp.load(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); 240 Matrix t; 241 t.load(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]); 242 mvp.multiply(&t); 243 244 glUniformMatrix4fv(sc->vtxUniformSlot(0), 1, GL_FALSE, mvp.m); 245 if (mTextureMatrixEnable) { 246 glUniformMatrix4fv(sc->vtxUniformSlot(1), 1, GL_FALSE, 247 &f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]); 248 } 249 250 rsc->checkError("ProgramVertex::setupGL2 begin uniforms"); 251 uint32_t uidx = 1; 252 for (uint32_t ct=0; ct < mConstantCount; ct++) { 253 Allocation *alloc = mConstants[ct+1].get(); 254 if (!alloc) { 255 continue; 256 } 257 258 const uint8_t *data = static_cast<const uint8_t *>(alloc->getPtr()); 259 const Element *e = mConstantTypes[ct]->getElement(); 260 for (uint32_t field=0; field < e->getFieldCount(); field++) { 261 const Element *f = e->getField(field); 262 uint32_t offset = e->getFieldOffsetBytes(field); 263 int32_t slot = sc->vtxUniformSlot(uidx); 264 const char *fieldName = e->getFieldName(field); 265 266 const float *fd = reinterpret_cast<const float *>(&data[offset]); 267 268 // If this field is padding, skip it 269 if(fieldName[0] == '#') { 270 continue; 271 } 272 273 //LOGE("Uniform slot=%i, offset=%i, constant=%i, field=%i, uidx=%i, name=%s", slot, offset, ct, field, uidx, fieldName); 274 if (slot >= 0) { 275 if(f->getType() == RS_TYPE_MATRIX_4X4) { 276 glUniformMatrix4fv(slot, 1, GL_FALSE, fd); 277 } 278 else if(f->getType() == RS_TYPE_MATRIX_3X3) { 279 glUniformMatrix3fv(slot, 1, GL_FALSE, fd); 280 } 281 else if(f->getType() == RS_TYPE_MATRIX_2X2) { 282 glUniformMatrix2fv(slot, 1, GL_FALSE, fd); 283 } 284 else { 285 switch(f->getComponent().getVectorSize()) { 286 case 1: 287 //LOGE("Uniform 1 = %f", fd[0]); 288 glUniform1fv(slot, 1, fd); 289 break; 290 case 2: 291 //LOGE("Uniform 2 = %f %f", fd[0], fd[1]); 292 glUniform2fv(slot, 1, fd); 293 break; 294 case 3: 295 //LOGE("Uniform 3 = %f %f %f", fd[0], fd[1], fd[2]); 296 glUniform3fv(slot, 1, fd); 297 break; 298 case 4: 299 //LOGE("Uniform 4 = %f %f %f %f", fd[0], fd[1], fd[2], fd[3]); 300 glUniform4fv(slot, 1, fd); 301 break; 302 default: 303 rsAssert(0); 304 } 305 } 306 } 307 uidx ++; 308 } 309 } 310 311 state->mLast.set(this); 312 rsc->checkError("ProgramVertex::setupGL2"); 313} 314 315void ProgramVertex::addLight(const Light *l) 316{ 317 if (mLightCount < MAX_LIGHTS) { 318 mLights[mLightCount].set(l); 319 mLightCount++; 320 } 321} 322 323void ProgramVertex::setProjectionMatrix(const rsc_Matrix *m) const 324{ 325 float *f = static_cast<float *>(mConstants[0]->getPtr()); 326 memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix)); 327 mDirty = true; 328} 329 330void ProgramVertex::setModelviewMatrix(const rsc_Matrix *m) const 331{ 332 float *f = static_cast<float *>(mConstants[0]->getPtr()); 333 memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix)); 334 mDirty = true; 335} 336 337void ProgramVertex::setTextureMatrix(const rsc_Matrix *m) const 338{ 339 float *f = static_cast<float *>(mConstants[0]->getPtr()); 340 memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix)); 341 mDirty = true; 342} 343 344void ProgramVertex::getProjectionMatrix(rsc_Matrix *m) const 345{ 346 float *f = static_cast<float *>(mConstants[0]->getPtr()); 347 memcpy(m, &f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], sizeof(rsc_Matrix)); 348} 349 350void ProgramVertex::transformToScreen(const Context *rsc, float *v4out, const float *v3in) const 351{ 352 float *f = static_cast<float *>(mConstants[0]->getPtr()); 353 Matrix mvp; 354 mvp.loadMultiply((Matrix *)&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], 355 (Matrix *)&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); 356 mvp.vectorMultiply(v4out, v3in); 357} 358 359void ProgramVertex::initAddUserElement(const Element *e, String8 *names, uint32_t *count, const char *prefix) 360{ 361 rsAssert(e->getFieldCount()); 362 for (uint32_t ct=0; ct < e->getFieldCount(); ct++) { 363 const Element *ce = e->getField(ct); 364 if (ce->getFieldCount()) { 365 initAddUserElement(ce, names, count, prefix); 366 } 367 else if(e->getFieldName(ct)[0] != '#') { 368 String8 tmp(prefix); 369 tmp.append(e->getFieldName(ct)); 370 names[*count].setTo(tmp.string()); 371 (*count)++; 372 } 373 } 374} 375 376 377void ProgramVertex::init(Context *rsc) 378{ 379 mAttribCount = 0; 380 if (mUserShader.size() > 0) { 381 for (uint32_t ct=0; ct < mInputCount; ct++) { 382 initAddUserElement(mInputElements[ct].get(), mAttribNames, &mAttribCount, "ATTRIB_"); 383 } 384 385 mUniformCount = 1; 386 mUniformNames[0].setTo("UNI_MVP"); 387 for (uint32_t ct=0; ct < mConstantCount; ct++) { 388 initAddUserElement(mConstantTypes[ct]->getElement(), mUniformNames, &mUniformCount, "UNI_"); 389 } 390 } else { 391 mUniformCount = 2; 392 mUniformNames[0].setTo("UNI_MVP"); 393 mUniformNames[1].setTo("UNI_TexMatrix"); 394 } 395 396 createShader(); 397} 398 399void ProgramVertex::serialize(OStream *stream) const 400{ 401 402} 403 404ProgramVertex *ProgramVertex::createFromStream(Context *rsc, IStream *stream) 405{ 406 return NULL; 407} 408 409 410/////////////////////////////////////////////////////////////////////// 411 412ProgramVertexState::ProgramVertexState() 413{ 414} 415 416ProgramVertexState::~ProgramVertexState() 417{ 418} 419 420void ProgramVertexState::init(Context *rsc) 421{ 422#ifndef ANDROID_RS_BUILD_FOR_HOST 423 RsElement e = (RsElement) Element::create(rsc, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 1); 424 425 rsi_TypeBegin(rsc, e); 426 rsi_TypeAdd(rsc, RS_DIMENSION_X, 48); 427 mAllocType.set((Type *)rsi_TypeCreate(rsc)); 428 429 ProgramVertex *pv = new ProgramVertex(rsc, false); 430 Allocation *alloc = (Allocation *)rsi_AllocationCreateTyped(rsc, mAllocType.get()); 431 432 mDefaultAlloc.set(alloc); 433 mDefault.set(pv); 434 pv->init(rsc); 435 pv->bindAllocation(alloc, 0); 436 437 updateSize(rsc); 438#endif //ANDROID_RS_BUILD_FOR_HOST 439 440} 441 442void ProgramVertexState::updateSize(Context *rsc) 443{ 444 Matrix m; 445 m.loadOrtho(0,rsc->getWidth(), rsc->getHeight(),0, -1,1); 446 mDefaultAlloc->subData(RS_PROGRAM_VERTEX_PROJECTION_OFFSET, 16, &m.m[0], 16*4); 447 448 m.loadIdentity(); 449 mDefaultAlloc->subData(RS_PROGRAM_VERTEX_MODELVIEW_OFFSET, 16, &m.m[0], 16*4); 450} 451 452void ProgramVertexState::deinit(Context *rsc) 453{ 454 mDefaultAlloc.clear(); 455 mDefault.clear(); 456 mAllocType.clear(); 457 mLast.clear(); 458} 459 460 461namespace android { 462namespace renderscript { 463 464 465RsProgramVertex rsi_ProgramVertexCreate(Context *rsc, bool texMat) 466{ 467 ProgramVertex *pv = new ProgramVertex(rsc, texMat); 468 pv->incUserRef(); 469 return pv; 470} 471 472RsProgramVertex rsi_ProgramVertexCreate2(Context *rsc, const char * shaderText, 473 uint32_t shaderLength, const uint32_t * params, 474 uint32_t paramLength) 475{ 476 ProgramVertex *pv = new ProgramVertex(rsc, shaderText, shaderLength, params, paramLength); 477 pv->incUserRef(); 478 return pv; 479} 480 481 482} 483} 484