Program.cpp revision 0b9db91c3dc8007b47c8fd4fb9dd85be97201a88
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#include "shaders/drawColor.vert" 31#include "shaders/drawColor.frag" 32 33#include "shaders/drawTexture.vert" 34#include "shaders/drawTexture.frag" 35 36/////////////////////////////////////////////////////////////////////////////// 37// Base program 38/////////////////////////////////////////////////////////////////////////////// 39 40Program::Program(const char* vertex, const char* fragment) { 41 vertexShader = buildShader(vertex, GL_VERTEX_SHADER); 42 fragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER); 43 44 id = glCreateProgram(); 45 glAttachShader(id, vertexShader); 46 glAttachShader(id, fragmentShader); 47 glLinkProgram(id); 48 49 GLint status; 50 glGetProgramiv(id, GL_LINK_STATUS, &status); 51 if (status != GL_TRUE) { 52 GLint infoLen = 0; 53 glGetProgramiv(id, GL_INFO_LOG_LENGTH, &infoLen); 54 if (infoLen > 1) { 55 char* log = (char*) malloc(sizeof(char) * infoLen); 56 glGetProgramInfoLog(id, infoLen, 0, log); 57 LOGE("Error while linking shaders: %s", log); 58 delete log; 59 } 60 glDeleteProgram(id); 61 } 62} 63 64Program::~Program() { 65 glDeleteShader(vertexShader); 66 glDeleteShader(fragmentShader); 67 glDeleteProgram(id); 68} 69 70void Program::use() { 71 glUseProgram(id); 72} 73 74int Program::addAttrib(const char* name) { 75 int slot = glGetAttribLocation(id, name); 76 attributes.add(name, slot); 77 return slot; 78} 79 80int Program::getAttrib(const char* name) { 81 return attributes.valueFor(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 return uniforms.valueFor(name); 92} 93 94GLuint Program::buildShader(const char* source, GLenum type) { 95 GLuint shader = glCreateShader(type); 96 glShaderSource(shader, 1, &source, 0); 97 glCompileShader(shader); 98 99 GLint status; 100 glGetShaderiv(shader, GL_COMPILE_STATUS, &status); 101 if (status != GL_TRUE) { 102 // Some drivers return wrong values for GL_INFO_LOG_LENGTH 103 // use a fixed size instead 104 GLchar log[512]; 105 glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]); 106 LOGE("Error while compiling shader: %s", log); 107 glDeleteShader(shader); 108 } 109 110 return shader; 111} 112 113/////////////////////////////////////////////////////////////////////////////// 114// Draw color 115/////////////////////////////////////////////////////////////////////////////// 116 117DrawColorProgram::DrawColorProgram(): 118 Program(gDrawColorVertexShader, gDrawColorFragmentShader) { 119 getAttribsAndUniforms(); 120} 121 122DrawColorProgram::DrawColorProgram(const char* vertex, const char* fragment): 123 Program(vertex, fragment) { 124 getAttribsAndUniforms(); 125} 126 127void DrawColorProgram::getAttribsAndUniforms() { 128 position = addAttrib("position"); 129 color = addUniform("color"); 130 transform = addUniform("transform"); 131} 132 133void DrawColorProgram::use(const float* projectionMatrix, const mat4& modelViewMatrix, 134 const mat4& transformMatrix) { 135 mat4 t(projectionMatrix); 136 t.multiply(transformMatrix); 137 t.multiply(modelViewMatrix); 138 139 Program::use(); 140 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]); 141} 142 143/////////////////////////////////////////////////////////////////////////////// 144// Draw texture 145/////////////////////////////////////////////////////////////////////////////// 146 147DrawTextureProgram::DrawTextureProgram(): 148 DrawColorProgram(gDrawTextureVertexShader, gDrawTextureFragmentShader) { 149 texCoords = addAttrib("texCoords"); 150 sampler = addUniform("sampler"); 151} 152 153}; // namespace uirenderer 154}; // namespace android 155