Program.cpp revision 32f05e343c5ffb17f3235942bcda651bd3b9f1d6
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#define ATRACE_TAG ATRACE_TAG_VIEW
19
20#include <utils/Trace.h>
21
22#include "Program.h"
23#include "Vertex.h"
24
25namespace android {
26namespace uirenderer {
27
28///////////////////////////////////////////////////////////////////////////////
29// Base program
30///////////////////////////////////////////////////////////////////////////////
31
32Program::Program(const ProgramDescription& description, const char* vertex, const char* fragment) {
33    mInitialized = false;
34    mHasColorUniform = false;
35    mHasSampler = false;
36    mUse = false;
37
38    // No need to cache compiled shaders, rely instead on Android's
39    // persistent shaders cache
40    mVertexShader = buildShader(vertex, GL_VERTEX_SHADER);
41    if (mVertexShader) {
42        mFragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
43        if (mFragmentShader) {
44            mProgramId = glCreateProgram();
45
46            glAttachShader(mProgramId, mVertexShader);
47            glAttachShader(mProgramId, mFragmentShader);
48
49            position = bindAttrib("position", kBindingPosition);
50            if (description.hasTexture || description.hasExternalTexture) {
51                texCoords = bindAttrib("texCoords", kBindingTexCoords);
52            } else {
53                texCoords = -1;
54            }
55
56            ATRACE_BEGIN("linkProgram");
57            glLinkProgram(mProgramId);
58            ATRACE_END();
59
60            GLint status;
61            glGetProgramiv(mProgramId, GL_LINK_STATUS, &status);
62            if (status != GL_TRUE) {
63                ALOGE("Error while linking shaders:");
64                GLint infoLen = 0;
65                glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen);
66                if (infoLen > 1) {
67                    GLchar log[infoLen];
68                    glGetProgramInfoLog(mProgramId, infoLen, 0, &log[0]);
69                    ALOGE("%s", log);
70                }
71
72                glDetachShader(mProgramId, mVertexShader);
73                glDetachShader(mProgramId, mFragmentShader);
74
75                glDeleteShader(mVertexShader);
76                glDeleteShader(mFragmentShader);
77
78                glDeleteProgram(mProgramId);
79            } else {
80                mInitialized = true;
81            }
82        } else {
83            glDeleteShader(mVertexShader);
84        }
85    }
86
87    if (mInitialized) {
88        transform = addUniform("transform");
89        projection = addUniform("projection");
90    }
91}
92
93Program::~Program() {
94    if (mInitialized) {
95        // This would ideally happen after linking the program
96        // but Tegra drivers, especially when perfhud is enabled,
97        // sometimes crash if we do so
98        glDetachShader(mProgramId, mVertexShader);
99        glDetachShader(mProgramId, mFragmentShader);
100
101        glDeleteShader(mVertexShader);
102        glDeleteShader(mFragmentShader);
103
104        glDeleteProgram(mProgramId);
105    }
106}
107
108int Program::addAttrib(const char* name) {
109    int slot = glGetAttribLocation(mProgramId, name);
110    mAttributes.add(name, slot);
111    return slot;
112}
113
114int Program::bindAttrib(const char* name, ShaderBindings bindingSlot) {
115    glBindAttribLocation(mProgramId, bindingSlot, name);
116    mAttributes.add(name, bindingSlot);
117    return bindingSlot;
118}
119
120int Program::getAttrib(const char* name) {
121    ssize_t index = mAttributes.indexOfKey(name);
122    if (index >= 0) {
123        return mAttributes.valueAt(index);
124    }
125    return addAttrib(name);
126}
127
128int Program::addUniform(const char* name) {
129    int slot = glGetUniformLocation(mProgramId, name);
130    mUniforms.add(name, slot);
131    return slot;
132}
133
134int Program::getUniform(const char* name) {
135    ssize_t index = mUniforms.indexOfKey(name);
136    if (index >= 0) {
137        return mUniforms.valueAt(index);
138    }
139    return addUniform(name);
140}
141
142GLuint Program::buildShader(const char* source, GLenum type) {
143    ATRACE_CALL();
144
145    GLuint shader = glCreateShader(type);
146    glShaderSource(shader, 1, &source, 0);
147    glCompileShader(shader);
148
149    GLint status;
150    glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
151    if (status != GL_TRUE) {
152        // Some drivers return wrong values for GL_INFO_LOG_LENGTH
153        // use a fixed size instead
154        GLchar log[512];
155        glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]);
156        ALOGE("Error while compiling shader: %s", log);
157        glDeleteShader(shader);
158        return 0;
159    }
160
161    return shader;
162}
163
164void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
165        const mat4& transformMatrix, bool offset) {
166    if (projectionMatrix != mProjection) {
167        if (CC_LIKELY(!offset)) {
168            glUniformMatrix4fv(projection, 1, GL_FALSE, &projectionMatrix.data[0]);
169        } else {
170            mat4 p(projectionMatrix);
171            // offset screenspace xy by an amount that compensates for typical precision
172            // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted
173            // up and to the left.
174            // This offset value is based on an assumption that some hardware may use as
175            // little as 12.4 precision, so we offset by slightly more than 1/16.
176            p.translate(Vertex::gGeometryFudgeFactor, Vertex::gGeometryFudgeFactor);
177            glUniformMatrix4fv(projection, 1, GL_FALSE, &p.data[0]);
178        }
179        mProjection = projectionMatrix;
180    }
181
182    mat4 t(transformMatrix);
183    t.multiply(modelViewMatrix);
184    glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
185}
186
187void Program::setColor(const float r, const float g, const float b, const float a) {
188    if (!mHasColorUniform) {
189        mColorUniform = getUniform("color");
190        mHasColorUniform = true;
191    }
192    glUniform4f(mColorUniform, r, g, b, a);
193}
194
195void Program::use() {
196    glUseProgram(mProgramId);
197    if (texCoords >= 0 && !mHasSampler) {
198        glUniform1i(getUniform("baseSampler"), 0);
199        mHasSampler = true;
200    }
201    mUse = true;
202}
203
204void Program::remove() {
205    mUse = false;
206}
207
208}; // namespace uirenderer
209}; // namespace android
210