rsProgramVertex.cpp revision c9d43db4d216b01b13aebfdb31d5615909591b33
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 23using namespace android; 24using namespace android::renderscript; 25 26 27ProgramVertex::ProgramVertex(Element *in, Element *out) : 28 Program(in, out) 29{ 30 mTextureMatrixEnable = false; 31 mLightCount = 0; 32} 33 34ProgramVertex::~ProgramVertex() 35{ 36} 37 38static void logMatrix(const char *txt, const float *f) 39{ 40 LOGV("Matrix %s, %p", txt, f); 41 LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[0], f[4], f[8], f[12]); 42 LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[1], f[5], f[9], f[13]); 43 LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[2], f[6], f[10], f[14]); 44 LOGV("%6.2f, %6.2f, %6.2f, %6.2f", f[3], f[7], f[11], f[15]); 45} 46 47void ProgramVertex::setupGL() 48{ 49 const float *f = static_cast<const float *>(mConstants[0]->getPtr()); 50 51 glMatrixMode(GL_TEXTURE); 52 if (mTextureMatrixEnable) { 53 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET]); 54 } else { 55 glLoadIdentity(); 56 } 57 58 glMatrixMode(GL_MODELVIEW); 59 glLoadIdentity(); 60 if (mLightCount) { 61 int v = 1; 62 glEnable(GL_LIGHTING); 63 glLightModelxv(GL_LIGHT_MODEL_TWO_SIDE, &v); 64 for (uint32_t ct = 0; ct < mLightCount; ct++) { 65 const Light *l = mLights[ct].get(); 66 glEnable(GL_LIGHT0 + ct); 67 l->setupGL(ct); 68 } 69 for (uint32_t ct = mLightCount; ct < MAX_LIGHTS; ct++) { 70 glDisable(GL_LIGHT0 + ct); 71 } 72 } else { 73 glDisable(GL_LIGHTING); 74 } 75 76 if (!f) { 77 LOGE("Must bind constants to vertex program"); 78 } 79 80 glMatrixMode(GL_PROJECTION); 81 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET]); 82 glMatrixMode(GL_MODELVIEW); 83 glLoadMatrixf(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET]); 84} 85 86void ProgramVertex::setConstantType(uint32_t slot, const Type *t) 87{ 88 mConstantTypes[slot].set(t); 89} 90 91void ProgramVertex::bindAllocation(uint32_t slot, Allocation *a) 92{ 93 mConstants[slot].set(a); 94} 95 96void ProgramVertex::addLight(const Light *l) 97{ 98 if (mLightCount < MAX_LIGHTS) { 99 mLights[mLightCount].set(l); 100 mLightCount++; 101 } 102} 103 104void ProgramVertex::setProjectionMatrix(const rsc_Matrix *m) const 105{ 106 float *f = static_cast<float *>(mConstants[0]->getPtr()); 107 memcpy(&f[RS_PROGRAM_VERTEX_PROJECTION_OFFSET], m, sizeof(rsc_Matrix)); 108} 109 110void ProgramVertex::setModelviewMatrix(const rsc_Matrix *m) const 111{ 112 float *f = static_cast<float *>(mConstants[0]->getPtr()); 113 memcpy(&f[RS_PROGRAM_VERTEX_MODELVIEW_OFFSET], m, sizeof(rsc_Matrix)); 114} 115 116void ProgramVertex::setTextureMatrix(const rsc_Matrix *m) const 117{ 118 float *f = static_cast<float *>(mConstants[0]->getPtr()); 119 memcpy(&f[RS_PROGRAM_VERTEX_TEXTURE_OFFSET], m, sizeof(rsc_Matrix)); 120} 121 122 123 124ProgramVertexState::ProgramVertexState() 125{ 126 mPV = NULL; 127} 128 129ProgramVertexState::~ProgramVertexState() 130{ 131 delete mPV; 132} 133 134void ProgramVertexState::init(Context *rsc, int32_t w, int32_t h) 135{ 136 ProgramVertex *pv = new ProgramVertex(NULL, NULL); 137 Allocation *alloc = (Allocation *) 138 rsi_AllocationCreatePredefSized(rsc, RS_ELEMENT_USER_FLOAT, 48); 139 mDefaultAlloc.set(alloc); 140 mDefault.set(pv); 141 142 pv->bindAllocation(0, alloc); 143 144 Matrix m; 145 m.loadOrtho(0,w, h,0, -1,1); 146 alloc->subData(RS_PROGRAM_VERTEX_PROJECTION_OFFSET, 16, &m.m[0]); 147 148 m.loadIdentity(); 149 alloc->subData(RS_PROGRAM_VERTEX_MODELVIEW_OFFSET, 16, &m.m[0]); 150} 151 152 153namespace android { 154namespace renderscript { 155 156void rsi_ProgramVertexBegin(Context *rsc, RsElement in, RsElement out) 157{ 158 delete rsc->mStateVertex.mPV; 159 rsc->mStateVertex.mPV = new ProgramVertex((Element *)in, (Element *)out); 160} 161 162RsProgramVertex rsi_ProgramVertexCreate(Context *rsc) 163{ 164 ProgramVertex *pv = rsc->mStateVertex.mPV; 165 pv->incRef(); 166 rsc->mStateVertex.mPV = 0; 167 return pv; 168} 169 170void rsi_ProgramVertexBindAllocation(Context *rsc, RsProgramVertex vpgm, uint32_t slot, RsAllocation constants) 171{ 172 ProgramVertex *pv = static_cast<ProgramVertex *>(vpgm); 173 pv->bindAllocation(slot, static_cast<Allocation *>(constants)); 174} 175 176void rsi_ProgramVertexSetType(Context *rsc, uint32_t slot, RsType constants) 177{ 178 rsc->mStateVertex.mPV->setConstantType(slot, static_cast<const Type *>(constants)); 179} 180 181void rsi_ProgramVertexSetTextureMatrixEnable(Context *rsc, bool enable) 182{ 183 rsc->mStateVertex.mPV->setTextureMatrixEnable(enable); 184} 185 186void rsi_ProgramVertexAddLight(Context *rsc, RsLight light) 187{ 188 rsc->mStateVertex.mPV->addLight(static_cast<const Light *>(light)); 189} 190 191 192} 193} 194