Program.cpp revision 24edca8b526515979778e577191089a57f5277d7
1/* 2 * Copyright (C) 2010 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#define LOG_TAG "OpenGLRenderer" 18 19#include "Program.h" 20 21namespace android { 22namespace uirenderer { 23 24/////////////////////////////////////////////////////////////////////////////// 25// Base program 26/////////////////////////////////////////////////////////////////////////////// 27 28Program::Program(const char* vertex, const char* fragment) { 29 mInitialized = false; 30 mHasColorUniform = false; 31 32 // No need to cache compiled shaders, rely instead on Android's 33 // persistent shaders cache 34 GLuint vertexShader = buildShader(vertex, GL_VERTEX_SHADER); 35 if (vertexShader) { 36 37 GLuint fragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER); 38 if (fragmentShader) { 39 40 mProgramId = glCreateProgram(); 41 glAttachShader(mProgramId, vertexShader); 42 glAttachShader(mProgramId, fragmentShader); 43 glLinkProgram(mProgramId); 44 45 GLint status; 46 glGetProgramiv(mProgramId, GL_LINK_STATUS, &status); 47 if (status != GL_TRUE) { 48 LOGE("Error while linking shaders:"); 49 GLint infoLen = 0; 50 glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen); 51 if (infoLen > 1) { 52 GLchar log[infoLen]; 53 glGetProgramInfoLog(mProgramId, infoLen, 0, &log[0]); 54 LOGE("%s", log); 55 } 56 } else { 57 mInitialized = true; 58 } 59 60 glDetachShader(mProgramId, vertexShader); 61 glDetachShader(mProgramId, fragmentShader); 62 63 glDeleteShader(vertexShader); 64 glDeleteShader(fragmentShader); 65 66 if (!mInitialized) { 67 glDeleteProgram(mProgramId); 68 } 69 } else { 70 glDeleteShader(vertexShader); 71 } 72 } 73 74 mUse = false; 75 76 if (mInitialized) { 77 position = addAttrib("position"); 78 transform = addUniform("transform"); 79 } 80} 81 82Program::~Program() { 83 if (mInitialized) { 84 glDeleteProgram(mProgramId); 85 } 86} 87 88int Program::addAttrib(const char* name) { 89 int slot = glGetAttribLocation(mProgramId, name); 90 mAttributes.add(name, slot); 91 return slot; 92} 93 94int Program::getAttrib(const char* name) { 95 ssize_t index = mAttributes.indexOfKey(name); 96 if (index >= 0) { 97 return mAttributes.valueAt(index); 98 } 99 return addAttrib(name); 100} 101 102int Program::addUniform(const char* name) { 103 int slot = glGetUniformLocation(mProgramId, name); 104 mUniforms.add(name, slot); 105 return slot; 106} 107 108int Program::getUniform(const char* name) { 109 ssize_t index = mUniforms.indexOfKey(name); 110 if (index >= 0) { 111 return mUniforms.valueAt(index); 112 } 113 return addUniform(name); 114} 115 116GLuint Program::buildShader(const char* source, GLenum type) { 117 GLuint shader = glCreateShader(type); 118 glShaderSource(shader, 1, &source, 0); 119 glCompileShader(shader); 120 121 GLint status; 122 glGetShaderiv(shader, GL_COMPILE_STATUS, &status); 123 if (status != GL_TRUE) { 124 // Some drivers return wrong values for GL_INFO_LOG_LENGTH 125 // use a fixed size instead 126 GLchar log[512]; 127 glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]); 128 LOGE("Error while compiling shader: %s", log); 129 glDeleteShader(shader); 130 return 0; 131 } 132 133 return shader; 134} 135 136void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix, 137 const mat4& transformMatrix, bool offset) { 138 mat4 t(projectionMatrix); 139 if (offset) { 140 // offset screenspace xy by an amount that compensates for typical precision 141 // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted 142 // up and to the left. 143 // This offset value is based on an assumption that some hardware may use as 144 // little as 12.4 precision, so we offset by slightly more than 1/16. 145 t.translate(.375, .375, 0); 146 } 147 t.multiply(transformMatrix); 148 t.multiply(modelViewMatrix); 149 150 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]); 151} 152 153void Program::setColor(const float r, const float g, const float b, const float a) { 154 if (!mHasColorUniform) { 155 mColorUniform = getUniform("color"); 156 mHasColorUniform = false; 157 } 158 glUniform4f(mColorUniform, r, g, b, a); 159} 160 161void Program::use() { 162 glUseProgram(mProgramId); 163 mUse = true; 164 glEnableVertexAttribArray(position); 165} 166 167void Program::remove() { 168 mUse = false; 169 // TODO: Is this necessary? It should not be since all of our shaders 170 // use slot 0 for the position attrib 171 // glDisableVertexAttribArray(position); 172} 173 174}; // namespace uirenderer 175}; // namespace android 176