blob: 516546f24a57593bab8a25ee596ac51c22757787 [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 Guy05bbde72011-12-09 12:55:37 -080032 GLuint vertexShader = buildShader(vertex, GL_VERTEX_SHADER);
Romain Guy67f27952010-12-07 20:09:23 -080033 if (vertexShader) {
Romain Guy5cbbce52010-06-27 22:59:20 -070034
Romain Guy05bbde72011-12-09 12:55:37 -080035 GLuint fragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
Romain Guy67f27952010-12-07 20:09:23 -080036 if (fragmentShader) {
Romain Guy5cbbce52010-06-27 22:59:20 -070037
Romain Guy05bbde72011-12-09 12:55:37 -080038 mProgramId = glCreateProgram();
39 glAttachShader(mProgramId, vertexShader);
40 glAttachShader(mProgramId, fragmentShader);
41 glLinkProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080042
43 GLint status;
Romain Guy05bbde72011-12-09 12:55:37 -080044 glGetProgramiv(mProgramId, GL_LINK_STATUS, &status);
Romain Guy67f27952010-12-07 20:09:23 -080045 if (status != GL_TRUE) {
46 LOGE("Error while linking shaders:");
47 GLint infoLen = 0;
Romain Guy05bbde72011-12-09 12:55:37 -080048 glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen);
Romain Guy67f27952010-12-07 20:09:23 -080049 if (infoLen > 1) {
50 GLchar log[infoLen];
Romain Guy05bbde72011-12-09 12:55:37 -080051 glGetProgramInfoLog(mProgramId, infoLen, 0, &log[0]);
Romain Guy67f27952010-12-07 20:09:23 -080052 LOGE("%s", log);
53 }
Romain Guy67f27952010-12-07 20:09:23 -080054 } else {
55 mInitialized = true;
56 }
Romain Guy05bbde72011-12-09 12:55:37 -080057
58 glDetachShader(mProgramId, vertexShader);
59 glDetachShader(mProgramId, fragmentShader);
60
61 glDeleteShader(vertexShader);
62 glDeleteShader(fragmentShader);
63
64 if (!mInitialized) {
65 glDeleteProgram(mProgramId);
66 }
67 } else {
68 glDeleteShader(vertexShader);
Romain Guy5cbbce52010-06-27 22:59:20 -070069 }
Romain Guy5cbbce52010-06-27 22:59:20 -070070 }
Romain Guy260e1022010-07-12 14:41:06 -070071
72 mUse = false;
Romain Guy889f8d12010-07-29 14:37:42 -070073
Romain Guy67f27952010-12-07 20:09:23 -080074 if (mInitialized) {
75 position = addAttrib("position");
76 transform = addUniform("transform");
77 }
Romain Guy5cbbce52010-06-27 22:59:20 -070078}
79
80Program::~Program() {
Romain Guy67f27952010-12-07 20:09:23 -080081 if (mInitialized) {
Romain Guy05bbde72011-12-09 12:55:37 -080082 glDeleteProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080083 }
Romain Guy5cbbce52010-06-27 22:59:20 -070084}
85
Romain Guy5cbbce52010-06-27 22:59:20 -070086int Program::addAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -080087 int slot = glGetAttribLocation(mProgramId, name);
88 mAttributes.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -070089 return slot;
90}
91
92int Program::getAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -080093 ssize_t index = mAttributes.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -070094 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -080095 return mAttributes.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -070096 }
97 return addAttrib(name);
Romain Guy5cbbce52010-06-27 22:59:20 -070098}
99
100int Program::addUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800101 int slot = glGetUniformLocation(mProgramId, name);
102 mUniforms.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -0700103 return slot;
104}
105
106int Program::getUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800107 ssize_t index = mUniforms.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -0700108 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -0800109 return mUniforms.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -0700110 }
111 return addUniform(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700112}
113
114GLuint Program::buildShader(const char* source, GLenum type) {
115 GLuint shader = glCreateShader(type);
116 glShaderSource(shader, 1, &source, 0);
117 glCompileShader(shader);
118
119 GLint status;
120 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
121 if (status != GL_TRUE) {
122 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
123 // use a fixed size instead
124 GLchar log[512];
125 glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]);
126 LOGE("Error while compiling shader: %s", log);
127 glDeleteShader(shader);
Romain Guy67f27952010-12-07 20:09:23 -0800128 return 0;
Romain Guy5cbbce52010-06-27 22:59:20 -0700129 }
130
131 return shader;
132}
133
Romain Guy889f8d12010-07-29 14:37:42 -0700134void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700135 const mat4& transformMatrix, bool offset) {
Romain Guy0b9db912010-07-09 18:53:25 -0700136 mat4 t(projectionMatrix);
Chet Haase8a5cc922011-04-26 07:28:09 -0700137 if (offset) {
138 // offset screenspace xy by an amount that compensates for typical precision issues
139 // in GPU hardware that tends to paint hor/vert lines in pixels shifted up and to the left.
140 // This offset value is based on an assumption that some hardware may use as little
141 // as 12.4 precision, so we offset by slightly more than 1/16.
142 t.translate(.375, .375, 0);
143 }
Romain Guy0b9db912010-07-09 18:53:25 -0700144 t.multiply(transformMatrix);
145 t.multiply(modelViewMatrix);
146
Romain Guy0b9db912010-07-09 18:53:25 -0700147 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
Romain Guy5cbbce52010-06-27 22:59:20 -0700148}
149
Romain Guy707b2f72010-10-11 16:34:59 -0700150void Program::setColor(const float r, const float g, const float b, const float a) {
Romain Guy05bbde72011-12-09 12:55:37 -0800151 if (!mHasColorUniform) {
152 mColorUniform = getUniform("color");
153 mHasColorUniform = false;
154 }
155 glUniform4f(mColorUniform, r, g, b, a);
Romain Guy707b2f72010-10-11 16:34:59 -0700156}
157
Romain Guy889f8d12010-07-29 14:37:42 -0700158void Program::use() {
Romain Guy05bbde72011-12-09 12:55:37 -0800159 glUseProgram(mProgramId);
Romain Guy889f8d12010-07-29 14:37:42 -0700160 mUse = true;
Romain Guy6926c722010-07-12 20:20:03 -0700161 glEnableVertexAttribArray(position);
162}
163
Romain Guy889f8d12010-07-29 14:37:42 -0700164void Program::remove() {
165 mUse = false;
Romain Guy05bbde72011-12-09 12:55:37 -0800166 // TODO: Is this necessary? It should not be since all of our shaders
167 // use slot 0 for the position attrib
168 // glDisableVertexAttribArray(position);
Romain Guyf9764a42010-07-16 23:13:33 -0700169}
170
Romain Guy5cbbce52010-06-27 22:59:20 -0700171}; // namespace uirenderer
172}; // namespace android