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