Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 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 | |
| 21 | namespace android { |
| 22 | namespace uirenderer { |
| 23 | |
| 24 | /////////////////////////////////////////////////////////////////////////////// |
| 25 | // Shaders |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | |
| 28 | #define SHADER_SOURCE(name, source) const char* name = #source |
| 29 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | // Base program |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | |
| 34 | Program::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 | } |
Romain Guy | 98173a2 | 2010-08-11 16:40:25 -0700 | [diff] [blame] | 54 | glDeleteShader(vertexShader); |
| 55 | glDeleteShader(fragmentShader); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 56 | glDeleteProgram(id); |
| 57 | } |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 58 | |
| 59 | mUse = false; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 60 | |
| 61 | position = addAttrib("position"); |
| 62 | color = addUniform("color"); |
| 63 | transform = addUniform("transform"); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | Program::~Program() { |
| 67 | glDeleteShader(vertexShader); |
| 68 | glDeleteShader(fragmentShader); |
| 69 | glDeleteProgram(id); |
| 70 | } |
| 71 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 72 | int Program::addAttrib(const char* name) { |
| 73 | int slot = glGetAttribLocation(id, name); |
| 74 | attributes.add(name, slot); |
| 75 | return slot; |
| 76 | } |
| 77 | |
| 78 | int Program::getAttrib(const char* name) { |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 79 | ssize_t index = attributes.indexOfKey(name); |
| 80 | if (index >= 0) { |
| 81 | return attributes.valueAt(index); |
| 82 | } |
| 83 | return addAttrib(name); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | int Program::addUniform(const char* name) { |
| 87 | int slot = glGetUniformLocation(id, name); |
| 88 | uniforms.add(name, slot); |
| 89 | return slot; |
| 90 | } |
| 91 | |
| 92 | int Program::getUniform(const char* name) { |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 93 | ssize_t index = uniforms.indexOfKey(name); |
| 94 | if (index >= 0) { |
| 95 | return uniforms.valueAt(index); |
| 96 | } |
| 97 | return addUniform(name); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | GLuint 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 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 119 | void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix, |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 120 | const mat4& transformMatrix) { |
| 121 | mat4 t(projectionMatrix); |
| 122 | t.multiply(transformMatrix); |
| 123 | t.multiply(modelViewMatrix); |
| 124 | |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 125 | glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 128 | void Program::use() { |
| 129 | glUseProgram(id); |
| 130 | mUse = true; |
| 131 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 132 | glEnableVertexAttribArray(position); |
| 133 | } |
| 134 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 135 | void Program::remove() { |
| 136 | mUse = false; |
| 137 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 138 | glDisableVertexAttribArray(position); |
Romain Guy | f9764a4 | 2010-07-16 23:13:33 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 141 | }; // namespace uirenderer |
| 142 | }; // namespace android |