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