2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "OpenGLRenderer"
18 #define ATRACE_TAG ATRACE_TAG_VIEW
20 #include <utils/Trace.h>
26 namespace uirenderer {
28 ///////////////////////////////////////////////////////////////////////////////
30 ///////////////////////////////////////////////////////////////////////////////
32 Program::Program(const ProgramDescription& description, const char* vertex, const char* fragment) {
34 mHasColorUniform = false;
38 // No need to cache compiled shaders, rely instead on Android's
39 // persistent shaders cache
40 mVertexShader = buildShader(vertex, GL_VERTEX_SHADER);
42 mFragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
43 if (mFragmentShader) {
44 mProgramId = glCreateProgram();
46 glAttachShader(mProgramId, mVertexShader);
47 glAttachShader(mProgramId, mFragmentShader);
49 bindAttrib("position", kBindingPosition);
50 if (description.hasTexture || description.hasExternalTexture) {
51 texCoords = bindAttrib("texCoords", kBindingTexCoords);
56 ATRACE_BEGIN("linkProgram");
57 glLinkProgram(mProgramId);
61 glGetProgramiv(mProgramId, GL_LINK_STATUS, &status);
62 if (status != GL_TRUE) {
64 glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen);
67 glGetProgramInfoLog(mProgramId, infoLen, nullptr, &log[0]);
70 LOG_ALWAYS_FATAL("Error while linking shaders");
75 glDeleteShader(mVertexShader);
80 transform = addUniform("transform");
81 projection = addUniform("projection");
87 // This would ideally happen after linking the program
88 // but Tegra drivers, especially when perfhud is enabled,
89 // sometimes crash if we do so
90 glDetachShader(mProgramId, mVertexShader);
91 glDetachShader(mProgramId, mFragmentShader);
93 glDeleteShader(mVertexShader);
94 glDeleteShader(mFragmentShader);
96 glDeleteProgram(mProgramId);
100 int Program::addAttrib(const char* name) {
101 int slot = glGetAttribLocation(mProgramId, name);
102 mAttributes.add(name, slot);
106 int Program::bindAttrib(const char* name, ShaderBindings bindingSlot) {
107 glBindAttribLocation(mProgramId, bindingSlot, name);
108 mAttributes.add(name, bindingSlot);
112 int Program::getAttrib(const char* name) {
113 ssize_t index = mAttributes.indexOfKey(name);
115 return mAttributes.valueAt(index);
117 return addAttrib(name);
120 int Program::addUniform(const char* name) {
121 int slot = glGetUniformLocation(mProgramId, name);
122 mUniforms.add(name, slot);
126 int Program::getUniform(const char* name) {
127 ssize_t index = mUniforms.indexOfKey(name);
129 return mUniforms.valueAt(index);
131 return addUniform(name);
134 GLuint Program::buildShader(const char* source, GLenum type) {
135 ATRACE_NAME("Build GL Shader");
137 GLuint shader = glCreateShader(type);
138 glShaderSource(shader, 1, &source, nullptr);
139 glCompileShader(shader);
142 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
143 if (status != GL_TRUE) {
144 ALOGE("Error while compiling this shader:\n===\n%s\n===", source);
145 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
146 // use a fixed size instead
148 glGetShaderInfoLog(shader, sizeof(log), nullptr, &log[0]);
149 LOG_ALWAYS_FATAL("Shader info log: %s", log);
156 void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
157 const mat4& transformMatrix, bool offset) {
158 if (projectionMatrix != mProjection || offset != mOffset) {
159 if (CC_LIKELY(!offset)) {
160 glUniformMatrix4fv(projection, 1, GL_FALSE, &projectionMatrix.data[0]);
162 mat4 p(projectionMatrix);
163 // offset screenspace xy by an amount that compensates for typical precision
164 // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted
165 // up and to the left.
166 // This offset value is based on an assumption that some hardware may use as
167 // little as 12.4 precision, so we offset by slightly more than 1/16.
168 p.translate(Vertex::GeometryFudgeFactor(), Vertex::GeometryFudgeFactor());
169 glUniformMatrix4fv(projection, 1, GL_FALSE, &p.data[0]);
171 mProjection = projectionMatrix;
175 mat4 t(transformMatrix);
176 t.multiply(modelViewMatrix);
177 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
180 void Program::setColor(FloatColor color) {
181 if (!mHasColorUniform) {
182 mColorUniform = getUniform("color");
183 mHasColorUniform = true;
185 glUniform4f(mColorUniform, color.r, color.g, color.b, color.a);
188 void Program::use() {
189 glUseProgram(mProgramId);
190 if (texCoords >= 0 && !mHasSampler) {
191 glUniform1i(getUniform("baseSampler"), 0);
197 void Program::remove() {
201 }; // namespace uirenderer
202 }; // namespace android