Program.cpp revision 5b3b35296e8b2c8d3f07d32bb645d5414db41a1d
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 vertexShader = buildShader(vertex, GL_VERTEX_SHADER); 30 fragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER); 31 32 id = glCreateProgram(); 33 glAttachShader(id, vertexShader); 34 glAttachShader(id, fragmentShader); 35 glLinkProgram(id); 36 37 GLint status; 38 glGetProgramiv(id, GL_LINK_STATUS, &status); 39 if (status != GL_TRUE) { 40 GLint infoLen = 0; 41 glGetProgramiv(id, GL_INFO_LOG_LENGTH, &infoLen); 42 if (infoLen > 1) { 43 GLchar log[infoLen]; 44 glGetProgramInfoLog(id, infoLen, 0, &log[0]); 45 LOGE("Error while linking shaders: %s", log); 46 } 47 glDeleteShader(vertexShader); 48 glDeleteShader(fragmentShader); 49 glDeleteProgram(id); 50 } 51 52 mUse = false; 53 54 position = addAttrib("position"); 55 transform = addUniform("transform"); 56} 57 58Program::~Program() { 59 glDeleteShader(vertexShader); 60 glDeleteShader(fragmentShader); 61 glDeleteProgram(id); 62} 63 64int Program::addAttrib(const char* name) { 65 int slot = glGetAttribLocation(id, name); 66 attributes.add(name, slot); 67 return slot; 68} 69 70int Program::getAttrib(const char* name) { 71 ssize_t index = attributes.indexOfKey(name); 72 if (index >= 0) { 73 return attributes.valueAt(index); 74 } 75 return addAttrib(name); 76} 77 78int Program::addUniform(const char* name) { 79 int slot = glGetUniformLocation(id, name); 80 uniforms.add(name, slot); 81 return slot; 82} 83 84int Program::getUniform(const char* name) { 85 ssize_t index = uniforms.indexOfKey(name); 86 if (index >= 0) { 87 return uniforms.valueAt(index); 88 } 89 return addUniform(name); 90} 91 92GLuint Program::buildShader(const char* source, GLenum type) { 93 GLuint shader = glCreateShader(type); 94 glShaderSource(shader, 1, &source, 0); 95 glCompileShader(shader); 96 97 GLint status; 98 glGetShaderiv(shader, GL_COMPILE_STATUS, &status); 99 if (status != GL_TRUE) { 100 // Some drivers return wrong values for GL_INFO_LOG_LENGTH 101 // use a fixed size instead 102 GLchar log[512]; 103 glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]); 104 LOGE("Error while compiling shader: %s", log); 105 glDeleteShader(shader); 106 } 107 108 return shader; 109} 110 111void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix, 112 const mat4& transformMatrix) { 113 mat4 t(projectionMatrix); 114 t.multiply(transformMatrix); 115 t.multiply(modelViewMatrix); 116 117 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]); 118} 119 120void Program::setColor(const float r, const float g, const float b, const float a) { 121 glUniform4f(getUniform("color"), r, g, b, a); 122} 123 124void Program::use() { 125 glUseProgram(id); 126 mUse = true; 127 128 glEnableVertexAttribArray(position); 129} 130 131void Program::remove() { 132 mUse = false; 133 134 glDisableVertexAttribArray(position); 135} 136 137}; // namespace uirenderer 138}; // namespace android 139