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 | |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 28 | // TODO: Program instance should be created from a factory method |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 29 | Program::Program(const ProgramDescription& description, const char* vertex, const char* fragment) { |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 30 | mInitialized = false; |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 31 | mHasColorUniform = false; |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 32 | mHasSampler = false; |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 33 | mUse = false; |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 34 | |
Romain Guy | 24edca8 | 2011-12-09 13:08:06 -0800 | [diff] [blame] | 35 | // No need to cache compiled shaders, rely instead on Android's |
| 36 | // persistent shaders cache |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 37 | mVertexShader = buildShader(vertex, GL_VERTEX_SHADER); |
| 38 | if (mVertexShader) { |
| 39 | mFragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER); |
| 40 | if (mFragmentShader) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 41 | mProgramId = glCreateProgram(); |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 42 | |
| 43 | glAttachShader(mProgramId, mVertexShader); |
| 44 | glAttachShader(mProgramId, mFragmentShader); |
| 45 | |
| 46 | position = bindAttrib("position", kBindingPosition); |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 47 | if (description.hasTexture || description.hasExternalTexture) { |
| 48 | texCoords = bindAttrib("texCoords", kBindingTexCoords); |
| 49 | } else { |
| 50 | texCoords = -1; |
| 51 | } |
| 52 | |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 53 | glLinkProgram(mProgramId); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 54 | |
| 55 | GLint status; |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 56 | glGetProgramiv(mProgramId, GL_LINK_STATUS, &status); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 57 | if (status != GL_TRUE) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 58 | ALOGE("Error while linking shaders:"); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 59 | GLint infoLen = 0; |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 60 | glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 61 | if (infoLen > 1) { |
| 62 | GLchar log[infoLen]; |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 63 | glGetProgramInfoLog(mProgramId, infoLen, 0, &log[0]); |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 64 | ALOGE("%s", log); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 65 | } |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 66 | |
| 67 | glDetachShader(mProgramId, mVertexShader); |
| 68 | glDetachShader(mProgramId, mFragmentShader); |
| 69 | |
| 70 | glDeleteShader(mVertexShader); |
| 71 | glDeleteShader(mFragmentShader); |
| 72 | |
| 73 | glDeleteProgram(mProgramId); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 74 | } else { |
| 75 | mInitialized = true; |
| 76 | } |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 77 | } else { |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 78 | glDeleteShader(mVertexShader); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 79 | } |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 80 | } |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 81 | |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 82 | if (mInitialized) { |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 83 | transform = addUniform("transform"); |
| 84 | } |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | Program::~Program() { |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 88 | if (mInitialized) { |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 89 | glDetachShader(mProgramId, mVertexShader); |
| 90 | glDetachShader(mProgramId, mFragmentShader); |
| 91 | |
| 92 | glDeleteShader(mVertexShader); |
| 93 | glDeleteShader(mFragmentShader); |
| 94 | |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 95 | glDeleteProgram(mProgramId); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 96 | } |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 99 | int Program::addAttrib(const char* name) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 100 | int slot = glGetAttribLocation(mProgramId, name); |
| 101 | mAttributes.add(name, slot); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 102 | return slot; |
| 103 | } |
| 104 | |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 105 | int Program::bindAttrib(const char* name, ShaderBindings bindingSlot) { |
| 106 | glBindAttribLocation(mProgramId, bindingSlot, name); |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 107 | mAttributes.add(name, bindingSlot); |
| 108 | return bindingSlot; |
| 109 | } |
| 110 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 111 | int Program::getAttrib(const char* name) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 112 | ssize_t index = mAttributes.indexOfKey(name); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 113 | if (index >= 0) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 114 | return mAttributes.valueAt(index); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 115 | } |
| 116 | return addAttrib(name); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | int Program::addUniform(const char* name) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 120 | int slot = glGetUniformLocation(mProgramId, name); |
| 121 | mUniforms.add(name, slot); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 122 | return slot; |
| 123 | } |
| 124 | |
| 125 | int Program::getUniform(const char* name) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 126 | ssize_t index = mUniforms.indexOfKey(name); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 127 | if (index >= 0) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 128 | return mUniforms.valueAt(index); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 129 | } |
| 130 | return addUniform(name); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | GLuint Program::buildShader(const char* source, GLenum type) { |
| 134 | GLuint shader = glCreateShader(type); |
| 135 | glShaderSource(shader, 1, &source, 0); |
| 136 | glCompileShader(shader); |
| 137 | |
| 138 | GLint status; |
| 139 | glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
| 140 | if (status != GL_TRUE) { |
| 141 | // Some drivers return wrong values for GL_INFO_LOG_LENGTH |
| 142 | // use a fixed size instead |
| 143 | GLchar log[512]; |
| 144 | glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]); |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 145 | ALOGE("Error while compiling shader: %s", log); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 146 | glDeleteShader(shader); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 147 | return 0; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | return shader; |
| 151 | } |
| 152 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 153 | void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix, |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 154 | const mat4& transformMatrix, bool offset) { |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 155 | mat4 t(projectionMatrix); |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 156 | if (offset) { |
Romain Guy | 24edca8 | 2011-12-09 13:08:06 -0800 | [diff] [blame] | 157 | // offset screenspace xy by an amount that compensates for typical precision |
| 158 | // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted |
| 159 | // up and to the left. |
| 160 | // This offset value is based on an assumption that some hardware may use as |
| 161 | // little as 12.4 precision, so we offset by slightly more than 1/16. |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 162 | t.translate(.375, .375, 0); |
| 163 | } |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 164 | t.multiply(transformMatrix); |
| 165 | t.multiply(modelViewMatrix); |
| 166 | |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 167 | glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 170 | 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] | 171 | if (!mHasColorUniform) { |
| 172 | mColorUniform = getUniform("color"); |
Romain Guy | 6752d0a | 2011-12-12 12:15:17 -0800 | [diff] [blame] | 173 | mHasColorUniform = true; |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 174 | } |
| 175 | glUniform4f(mColorUniform, r, g, b, a); |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 178 | void Program::use() { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 179 | glUseProgram(mProgramId); |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 180 | if (texCoords >= 0 && !mHasSampler) { |
Chet Haase | 0990ffb | 2012-09-17 17:43:45 -0700 | [diff] [blame^] | 181 | glUniform1i(getUniform("baseSampler"), 0); |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 182 | mHasSampler = true; |
| 183 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 184 | mUse = true; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 187 | void Program::remove() { |
| 188 | mUse = false; |
Romain Guy | f9764a4 | 2010-07-16 23:13:33 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 191 | }; // namespace uirenderer |
| 192 | }; // namespace android |