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 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 17 | #include <utils/Trace.h> |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 18 | |
| 19 | #include "Program.h" |
Chris Craik | 32f05e3 | 2013-09-17 16:20:29 -0700 | [diff] [blame] | 20 | #include "Vertex.h" |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | namespace uirenderer { |
| 24 | |
| 25 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 26 | // Base program |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | |
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 | |
Chris Craik | 6c15ffa | 2015-02-02 13:50:55 -0800 | [diff] [blame] | 46 | 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 | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 53 | ATRACE_BEGIN("linkProgram"); |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 54 | glLinkProgram(mProgramId); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 55 | ATRACE_END(); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 56 | |
| 57 | GLint status; |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 58 | glGetProgramiv(mProgramId, GL_LINK_STATUS, &status); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 59 | if (status != GL_TRUE) { |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 60 | GLint infoLen = 0; |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 61 | glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 62 | if (infoLen > 1) { |
| 63 | GLchar log[infoLen]; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 64 | glGetProgramInfoLog(mProgramId, infoLen, nullptr, &log[0]); |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 65 | ALOGE("%s", log); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 66 | } |
Chris Craik | 7384258 | 2014-07-10 16:24:14 -0700 | [diff] [blame] | 67 | LOG_ALWAYS_FATAL("Error while linking shaders"); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 68 | } else { |
| 69 | mInitialized = true; |
| 70 | } |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 71 | } else { |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 72 | glDeleteShader(mVertexShader); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 73 | } |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 74 | } |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 75 | |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 76 | if (mInitialized) { |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 77 | transform = addUniform("transform"); |
Romain Guy | 39284b7 | 2012-09-26 16:39:40 -0700 | [diff] [blame] | 78 | projection = addUniform("projection"); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 79 | } |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | Program::~Program() { |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 83 | if (mInitialized) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 84 | // This would ideally happen after linking the program |
| 85 | // but Tegra drivers, especially when perfhud is enabled, |
| 86 | // sometimes crash if we do so |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 87 | glDetachShader(mProgramId, mVertexShader); |
| 88 | glDetachShader(mProgramId, mFragmentShader); |
| 89 | |
| 90 | glDeleteShader(mVertexShader); |
| 91 | glDeleteShader(mFragmentShader); |
| 92 | |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 93 | glDeleteProgram(mProgramId); |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 94 | } |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 97 | int Program::addAttrib(const char* name) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 98 | int slot = glGetAttribLocation(mProgramId, name); |
| 99 | mAttributes.add(name, slot); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 100 | return slot; |
| 101 | } |
| 102 | |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 103 | int Program::bindAttrib(const char* name, ShaderBindings bindingSlot) { |
| 104 | glBindAttribLocation(mProgramId, bindingSlot, name); |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 105 | mAttributes.add(name, bindingSlot); |
| 106 | return bindingSlot; |
| 107 | } |
| 108 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 109 | int Program::getAttrib(const char* name) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 110 | ssize_t index = mAttributes.indexOfKey(name); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 111 | if (index >= 0) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 112 | return mAttributes.valueAt(index); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 113 | } |
| 114 | return addAttrib(name); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | int Program::addUniform(const char* name) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 118 | int slot = glGetUniformLocation(mProgramId, name); |
| 119 | mUniforms.add(name, slot); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 120 | return slot; |
| 121 | } |
| 122 | |
| 123 | int Program::getUniform(const char* name) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 124 | ssize_t index = mUniforms.indexOfKey(name); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 125 | if (index >= 0) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 126 | return mUniforms.valueAt(index); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 127 | } |
| 128 | return addUniform(name); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | GLuint Program::buildShader(const char* source, GLenum type) { |
Chris Craik | 70850ea | 2014-11-18 10:49:23 -0800 | [diff] [blame] | 132 | ATRACE_NAME("Build GL Shader"); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 133 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 134 | GLuint shader = glCreateShader(type); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 135 | glShaderSource(shader, 1, &source, nullptr); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 136 | glCompileShader(shader); |
| 137 | |
| 138 | GLint status; |
| 139 | glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
| 140 | if (status != GL_TRUE) { |
Rob Tsuk | fd15f47 | 2014-11-05 13:26:19 -0800 | [diff] [blame] | 141 | ALOGE("Error while compiling this shader:\n===\n%s\n===", source); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 142 | // Some drivers return wrong values for GL_INFO_LOG_LENGTH |
| 143 | // use a fixed size instead |
| 144 | GLchar log[512]; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 145 | glGetShaderInfoLog(shader, sizeof(log), nullptr, &log[0]); |
Rob Tsuk | fd15f47 | 2014-11-05 13:26:19 -0800 | [diff] [blame] | 146 | LOG_ALWAYS_FATAL("Shader info log: %s", log); |
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) { |
Chris Craik | d04a6b1 | 2014-01-29 13:00:33 -0800 | [diff] [blame] | 155 | if (projectionMatrix != mProjection || offset != mOffset) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 156 | if (CC_LIKELY(!offset)) { |
| 157 | glUniformMatrix4fv(projection, 1, GL_FALSE, &projectionMatrix.data[0]); |
| 158 | } else { |
| 159 | mat4 p(projectionMatrix); |
| 160 | // offset screenspace xy by an amount that compensates for typical precision |
| 161 | // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted |
| 162 | // up and to the left. |
| 163 | // This offset value is based on an assumption that some hardware may use as |
| 164 | // little as 12.4 precision, so we offset by slightly more than 1/16. |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 165 | p.translate(Vertex::GeometryFudgeFactor(), Vertex::GeometryFudgeFactor()); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 166 | glUniformMatrix4fv(projection, 1, GL_FALSE, &p.data[0]); |
| 167 | } |
| 168 | mProjection = projectionMatrix; |
Chris Craik | d04a6b1 | 2014-01-29 13:00:33 -0800 | [diff] [blame] | 169 | mOffset = offset; |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 170 | } |
Romain Guy | 39284b7 | 2012-09-26 16:39:40 -0700 | [diff] [blame] | 171 | |
| 172 | mat4 t(transformMatrix); |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 173 | t.multiply(modelViewMatrix); |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 174 | glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 177 | void Program::setColor(FloatColor color) { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 178 | if (!mHasColorUniform) { |
| 179 | mColorUniform = getUniform("color"); |
Romain Guy | 6752d0a | 2011-12-12 12:15:17 -0800 | [diff] [blame] | 180 | mHasColorUniform = true; |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 181 | } |
Chris Craik | 0519c81 | 2015-02-11 13:17:06 -0800 | [diff] [blame] | 182 | glUniform4f(mColorUniform, color.r, color.g, color.b, color.a); |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 185 | void Program::use() { |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 186 | glUseProgram(mProgramId); |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 187 | if (texCoords >= 0 && !mHasSampler) { |
Chet Haase | 0990ffb | 2012-09-17 17:43:45 -0700 | [diff] [blame] | 188 | glUniform1i(getUniform("baseSampler"), 0); |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 189 | mHasSampler = true; |
| 190 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 191 | mUse = true; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 194 | void Program::remove() { |
| 195 | mUse = false; |
Romain Guy | f9764a4 | 2010-07-16 23:13:33 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 198 | }; // namespace uirenderer |
| 199 | }; // namespace android |