blob: a9f13f7cd03fff188d63c8a973c0d2bc376d41bd [file] [log] [blame]
Romain Guy5cbbce52010-06-27 22:59:20 -07001/*
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///////////////////////////////////////////////////////////////////////////////
Romain Guy5cbbce52010-06-27 22:59:20 -070025// Base program
26///////////////////////////////////////////////////////////////////////////////
27
28Program::Program(const char* vertex, const char* fragment) {
Romain Guy67f27952010-12-07 20:09:23 -080029 mInitialized = false;
Romain Guy05bbde72011-12-09 12:55:37 -080030 mHasColorUniform = false;
Romain Guy67f27952010-12-07 20:09:23 -080031
Romain Guy24edca82011-12-09 13:08:06 -080032 // No need to cache compiled shaders, rely instead on Android's
33 // persistent shaders cache
Romain Guy05bbde72011-12-09 12:55:37 -080034 GLuint vertexShader = buildShader(vertex, GL_VERTEX_SHADER);
Romain Guy67f27952010-12-07 20:09:23 -080035 if (vertexShader) {
Romain Guy5cbbce52010-06-27 22:59:20 -070036
Romain Guy05bbde72011-12-09 12:55:37 -080037 GLuint fragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
Romain Guy67f27952010-12-07 20:09:23 -080038 if (fragmentShader) {
Romain Guy5cbbce52010-06-27 22:59:20 -070039
Romain Guy05bbde72011-12-09 12:55:37 -080040 mProgramId = glCreateProgram();
41 glAttachShader(mProgramId, vertexShader);
42 glAttachShader(mProgramId, fragmentShader);
43 glLinkProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080044
45 GLint status;
Romain Guy05bbde72011-12-09 12:55:37 -080046 glGetProgramiv(mProgramId, GL_LINK_STATUS, &status);
Romain Guy67f27952010-12-07 20:09:23 -080047 if (status != GL_TRUE) {
48 LOGE("Error while linking shaders:");
49 GLint infoLen = 0;
Romain Guy05bbde72011-12-09 12:55:37 -080050 glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen);
Romain Guy67f27952010-12-07 20:09:23 -080051 if (infoLen > 1) {
52 GLchar log[infoLen];
Romain Guy05bbde72011-12-09 12:55:37 -080053 glGetProgramInfoLog(mProgramId, infoLen, 0, &log[0]);
Romain Guy67f27952010-12-07 20:09:23 -080054 LOGE("%s", log);
55 }
Romain Guy67f27952010-12-07 20:09:23 -080056 } else {
57 mInitialized = true;
58 }
Romain Guy05bbde72011-12-09 12:55:37 -080059
60 glDetachShader(mProgramId, vertexShader);
61 glDetachShader(mProgramId, fragmentShader);
62
63 glDeleteShader(vertexShader);
64 glDeleteShader(fragmentShader);
65
66 if (!mInitialized) {
67 glDeleteProgram(mProgramId);
68 }
69 } else {
70 glDeleteShader(vertexShader);
Romain Guy5cbbce52010-06-27 22:59:20 -070071 }
Romain Guy5cbbce52010-06-27 22:59:20 -070072 }
Romain Guy260e1022010-07-12 14:41:06 -070073
74 mUse = false;
Romain Guy889f8d12010-07-29 14:37:42 -070075
Romain Guy67f27952010-12-07 20:09:23 -080076 if (mInitialized) {
77 position = addAttrib("position");
78 transform = addUniform("transform");
79 }
Romain Guy5cbbce52010-06-27 22:59:20 -070080}
81
82Program::~Program() {
Romain Guy67f27952010-12-07 20:09:23 -080083 if (mInitialized) {
Romain Guy05bbde72011-12-09 12:55:37 -080084 glDeleteProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080085 }
Romain Guy5cbbce52010-06-27 22:59:20 -070086}
87
Romain Guy5cbbce52010-06-27 22:59:20 -070088int Program::addAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -080089 int slot = glGetAttribLocation(mProgramId, name);
90 mAttributes.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -070091 return slot;
92}
93
94int Program::getAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -080095 ssize_t index = mAttributes.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -070096 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -080097 return mAttributes.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -070098 }
99 return addAttrib(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700100}
101
102int Program::addUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800103 int slot = glGetUniformLocation(mProgramId, name);
104 mUniforms.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -0700105 return slot;
106}
107
108int Program::getUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800109 ssize_t index = mUniforms.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -0700110 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -0800111 return mUniforms.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -0700112 }
113 return addUniform(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700114}
115
116GLuint Program::buildShader(const char* source, GLenum type) {
117 GLuint shader = glCreateShader(type);
118 glShaderSource(shader, 1, &source, 0);
119 glCompileShader(shader);
120
121 GLint status;
122 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
123 if (status != GL_TRUE) {
124 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
125 // use a fixed size instead
126 GLchar log[512];
127 glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]);
128 LOGE("Error while compiling shader: %s", log);
129 glDeleteShader(shader);
Romain Guy67f27952010-12-07 20:09:23 -0800130 return 0;
Romain Guy5cbbce52010-06-27 22:59:20 -0700131 }
132
133 return shader;
134}
135
Romain Guy889f8d12010-07-29 14:37:42 -0700136void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700137 const mat4& transformMatrix, bool offset) {
Romain Guy0b9db912010-07-09 18:53:25 -0700138 mat4 t(projectionMatrix);
Chet Haase8a5cc922011-04-26 07:28:09 -0700139 if (offset) {
Romain Guy24edca82011-12-09 13:08:06 -0800140 // offset screenspace xy by an amount that compensates for typical precision
141 // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted
142 // up and to the left.
143 // This offset value is based on an assumption that some hardware may use as
144 // little as 12.4 precision, so we offset by slightly more than 1/16.
Chet Haase8a5cc922011-04-26 07:28:09 -0700145 t.translate(.375, .375, 0);
146 }
Romain Guy0b9db912010-07-09 18:53:25 -0700147 t.multiply(transformMatrix);
148 t.multiply(modelViewMatrix);
149
Romain Guy0b9db912010-07-09 18:53:25 -0700150 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
Romain Guy5cbbce52010-06-27 22:59:20 -0700151}
152
Romain Guy707b2f72010-10-11 16:34:59 -0700153void Program::setColor(const float r, const float g, const float b, const float a) {
Romain Guy05bbde72011-12-09 12:55:37 -0800154 if (!mHasColorUniform) {
155 mColorUniform = getUniform("color");
156 mHasColorUniform = false;
157 }
158 glUniform4f(mColorUniform, r, g, b, a);
Romain Guy707b2f72010-10-11 16:34:59 -0700159}
160
Romain Guy889f8d12010-07-29 14:37:42 -0700161void Program::use() {
Romain Guy05bbde72011-12-09 12:55:37 -0800162 glUseProgram(mProgramId);
Romain Guy889f8d12010-07-29 14:37:42 -0700163 mUse = true;
Romain Guy6926c722010-07-12 20:20:03 -0700164 glEnableVertexAttribArray(position);
165}
166
Romain Guy889f8d12010-07-29 14:37:42 -0700167void Program::remove() {
168 mUse = false;
Romain Guy05bbde72011-12-09 12:55:37 -0800169 // TODO: Is this necessary? It should not be since all of our shaders
170 // use slot 0 for the position attrib
171 // glDisableVertexAttribArray(position);
Romain Guyf9764a42010-07-16 23:13:33 -0700172}
173
Romain Guy5cbbce52010-06-27 22:59:20 -0700174}; // namespace uirenderer
175}; // namespace android