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