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 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 25 | // Base program |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | |
| 28 | Program::Program(const char* vertex, const char* fragment) { |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 29 | mInitialized = false; |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 30 | mHasColorUniform = false; |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 31 | |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 32 | GLuint vertexShader = buildShader(vertex, GL_VERTEX_SHADER); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 33 | if (vertexShader) { |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 34 | |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 35 | GLuint fragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 36 | if (fragmentShader) { |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 37 | |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 38 | mProgramId = glCreateProgram(); |
| 39 | glAttachShader(mProgramId, vertexShader); |
| 40 | glAttachShader(mProgramId, fragmentShader); |
| 41 | glLinkProgram(mProgramId); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 42 | |
| 43 | GLint status; |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 44 | glGetProgramiv(mProgramId, GL_LINK_STATUS, &status); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 45 | if (status != GL_TRUE) { |
| 46 | LOGE("Error while linking shaders:"); |
| 47 | GLint infoLen = 0; |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 48 | glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 49 | if (infoLen > 1) { |
| 50 | GLchar log[infoLen]; |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 51 | glGetProgramInfoLog(mProgramId, infoLen, 0, &log[0]); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 52 | LOGE("%s", log); |
| 53 | } |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 54 | } else { |
| 55 | mInitialized = true; |
| 56 | } |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 57 | |
| 58 | glDetachShader(mProgramId, vertexShader); |
| 59 | glDetachShader(mProgramId, fragmentShader); |
| 60 | |
| 61 | glDeleteShader(vertexShader); |
| 62 | glDeleteShader(fragmentShader); |
| 63 | |
| 64 | if (!mInitialized) { |
| 65 | glDeleteProgram(mProgramId); |
| 66 | } |
| 67 | } else { |
| 68 | glDeleteShader(vertexShader); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 69 | } |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 70 | } |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 71 | |
| 72 | mUse = false; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 73 | |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 74 | if (mInitialized) { |
| 75 | position = addAttrib("position"); |
| 76 | transform = addUniform("transform"); |
| 77 | } |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | Program::~Program() { |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 81 | if (mInitialized) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 82 | glDeleteProgram(mProgramId); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 83 | } |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 86 | int Program::addAttrib(const char* name) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 87 | int slot = glGetAttribLocation(mProgramId, name); |
| 88 | mAttributes.add(name, slot); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 89 | return slot; |
| 90 | } |
| 91 | |
| 92 | int Program::getAttrib(const char* name) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 93 | ssize_t index = mAttributes.indexOfKey(name); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 94 | if (index >= 0) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 95 | return mAttributes.valueAt(index); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 96 | } |
| 97 | return addAttrib(name); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | int Program::addUniform(const char* name) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 101 | int slot = glGetUniformLocation(mProgramId, name); |
| 102 | mUniforms.add(name, slot); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 103 | return slot; |
| 104 | } |
| 105 | |
| 106 | int Program::getUniform(const char* name) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 107 | ssize_t index = mUniforms.indexOfKey(name); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 108 | if (index >= 0) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 109 | return mUniforms.valueAt(index); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 110 | } |
| 111 | return addUniform(name); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | GLuint Program::buildShader(const char* source, GLenum type) { |
| 115 | GLuint shader = glCreateShader(type); |
| 116 | glShaderSource(shader, 1, &source, 0); |
| 117 | glCompileShader(shader); |
| 118 | |
| 119 | GLint status; |
| 120 | glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
| 121 | if (status != GL_TRUE) { |
| 122 | // Some drivers return wrong values for GL_INFO_LOG_LENGTH |
| 123 | // use a fixed size instead |
| 124 | GLchar log[512]; |
| 125 | glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]); |
| 126 | LOGE("Error while compiling shader: %s", log); |
| 127 | glDeleteShader(shader); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 128 | return 0; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | return shader; |
| 132 | } |
| 133 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 134 | void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix, |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 135 | const mat4& transformMatrix, bool offset) { |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 136 | mat4 t(projectionMatrix); |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 137 | if (offset) { |
| 138 | // offset screenspace xy by an amount that compensates for typical precision issues |
| 139 | // in GPU hardware that tends to paint hor/vert lines in pixels shifted up and to the left. |
| 140 | // This offset value is based on an assumption that some hardware may use as little |
| 141 | // as 12.4 precision, so we offset by slightly more than 1/16. |
| 142 | t.translate(.375, .375, 0); |
| 143 | } |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 144 | t.multiply(transformMatrix); |
| 145 | t.multiply(modelViewMatrix); |
| 146 | |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 147 | glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 150 | void Program::setColor(const float r, const float g, const float b, const float a) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 151 | if (!mHasColorUniform) { |
| 152 | mColorUniform = getUniform("color"); |
| 153 | mHasColorUniform = false; |
| 154 | } |
| 155 | glUniform4f(mColorUniform, r, g, b, a); |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 158 | void Program::use() { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 159 | glUseProgram(mProgramId); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 160 | mUse = true; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 161 | glEnableVertexAttribArray(position); |
| 162 | } |
| 163 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 164 | void Program::remove() { |
| 165 | mUse = false; |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame^] | 166 | // TODO: Is this necessary? It should not be since all of our shaders |
| 167 | // use slot 0 for the position attrib |
| 168 | // glDisableVertexAttribArray(position); |
Romain Guy | f9764a4 | 2010-07-16 23:13:33 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 171 | }; // namespace uirenderer |
| 172 | }; // namespace android |