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