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