blob: 9e4670e62dcd86c182b56e3f3a65304dc1246478 [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"
Romain Guy3b748a42013-04-17 18:54:38 -070018#define ATRACE_TAG ATRACE_TAG_VIEW
19
20#include <utils/Trace.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070021
22#include "Program.h"
23
24namespace android {
25namespace uirenderer {
26
27///////////////////////////////////////////////////////////////////////////////
Romain Guy5cbbce52010-06-27 22:59:20 -070028// Base program
29///////////////////////////////////////////////////////////////////////////////
30
Romain Guyf3a910b42011-12-12 20:35:21 -080031Program::Program(const ProgramDescription& description, const char* vertex, const char* fragment) {
Romain Guy67f27952010-12-07 20:09:23 -080032 mInitialized = false;
Romain Guy05bbde72011-12-09 12:55:37 -080033 mHasColorUniform = false;
Romain Guy2d4fd362011-12-13 22:00:19 -080034 mHasSampler = false;
Romain Guy3e263fa2011-12-12 16:47:48 -080035 mUse = false;
Romain Guy67f27952010-12-07 20:09:23 -080036
Romain Guy24edca82011-12-09 13:08:06 -080037 // No need to cache compiled shaders, rely instead on Android's
38 // persistent shaders cache
Romain Guy3e263fa2011-12-12 16:47:48 -080039 mVertexShader = buildShader(vertex, GL_VERTEX_SHADER);
40 if (mVertexShader) {
41 mFragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
42 if (mFragmentShader) {
Romain Guy05bbde72011-12-09 12:55:37 -080043 mProgramId = glCreateProgram();
Romain Guy3e263fa2011-12-12 16:47:48 -080044
45 glAttachShader(mProgramId, mVertexShader);
46 glAttachShader(mProgramId, mFragmentShader);
47
48 position = bindAttrib("position", kBindingPosition);
Romain Guyf3a910b42011-12-12 20:35:21 -080049 if (description.hasTexture || description.hasExternalTexture) {
50 texCoords = bindAttrib("texCoords", kBindingTexCoords);
51 } else {
52 texCoords = -1;
53 }
54
Romain Guy3b748a42013-04-17 18:54:38 -070055 ATRACE_BEGIN("linkProgram");
Romain Guy05bbde72011-12-09 12:55:37 -080056 glLinkProgram(mProgramId);
Romain Guy3b748a42013-04-17 18:54:38 -070057 ATRACE_END();
Romain Guy67f27952010-12-07 20:09:23 -080058
59 GLint status;
Romain Guy05bbde72011-12-09 12:55:37 -080060 glGetProgramiv(mProgramId, GL_LINK_STATUS, &status);
Romain Guy67f27952010-12-07 20:09:23 -080061 if (status != GL_TRUE) {
Steve Block3762c312012-01-06 19:20:56 +000062 ALOGE("Error while linking shaders:");
Romain Guy67f27952010-12-07 20:09:23 -080063 GLint infoLen = 0;
Romain Guy05bbde72011-12-09 12:55:37 -080064 glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen);
Romain Guy67f27952010-12-07 20:09:23 -080065 if (infoLen > 1) {
66 GLchar log[infoLen];
Romain Guy05bbde72011-12-09 12:55:37 -080067 glGetProgramInfoLog(mProgramId, infoLen, 0, &log[0]);
Steve Block3762c312012-01-06 19:20:56 +000068 ALOGE("%s", log);
Romain Guy67f27952010-12-07 20:09:23 -080069 }
Romain Guy3e263fa2011-12-12 16:47:48 -080070
71 glDetachShader(mProgramId, mVertexShader);
72 glDetachShader(mProgramId, mFragmentShader);
73
74 glDeleteShader(mVertexShader);
75 glDeleteShader(mFragmentShader);
76
77 glDeleteProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080078 } else {
79 mInitialized = true;
80 }
Romain Guy05bbde72011-12-09 12:55:37 -080081 } else {
Romain Guy3e263fa2011-12-12 16:47:48 -080082 glDeleteShader(mVertexShader);
Romain Guy5cbbce52010-06-27 22:59:20 -070083 }
Romain Guy5cbbce52010-06-27 22:59:20 -070084 }
Romain Guy260e1022010-07-12 14:41:06 -070085
Romain Guy67f27952010-12-07 20:09:23 -080086 if (mInitialized) {
Romain Guy67f27952010-12-07 20:09:23 -080087 transform = addUniform("transform");
Romain Guy39284b72012-09-26 16:39:40 -070088 projection = addUniform("projection");
Romain Guy67f27952010-12-07 20:09:23 -080089 }
Romain Guy5cbbce52010-06-27 22:59:20 -070090}
91
92Program::~Program() {
Romain Guy67f27952010-12-07 20:09:23 -080093 if (mInitialized) {
Romain Guy3b748a42013-04-17 18:54:38 -070094 // This would ideally happen after linking the program
95 // but Tegra drivers, especially when perfhud is enabled,
96 // sometimes crash if we do so
Romain Guy3e263fa2011-12-12 16:47:48 -080097 glDetachShader(mProgramId, mVertexShader);
98 glDetachShader(mProgramId, mFragmentShader);
99
100 glDeleteShader(mVertexShader);
101 glDeleteShader(mFragmentShader);
102
Romain Guy05bbde72011-12-09 12:55:37 -0800103 glDeleteProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -0800104 }
Romain Guy5cbbce52010-06-27 22:59:20 -0700105}
106
Romain Guy5cbbce52010-06-27 22:59:20 -0700107int Program::addAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800108 int slot = glGetAttribLocation(mProgramId, name);
109 mAttributes.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -0700110 return slot;
111}
112
Romain Guy3e263fa2011-12-12 16:47:48 -0800113int Program::bindAttrib(const char* name, ShaderBindings bindingSlot) {
114 glBindAttribLocation(mProgramId, bindingSlot, name);
Romain Guy3e263fa2011-12-12 16:47:48 -0800115 mAttributes.add(name, bindingSlot);
116 return bindingSlot;
117}
118
Romain Guy5cbbce52010-06-27 22:59:20 -0700119int Program::getAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800120 ssize_t index = mAttributes.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -0700121 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -0800122 return mAttributes.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -0700123 }
124 return addAttrib(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700125}
126
127int Program::addUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800128 int slot = glGetUniformLocation(mProgramId, name);
129 mUniforms.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -0700130 return slot;
131}
132
133int Program::getUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800134 ssize_t index = mUniforms.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -0700135 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -0800136 return mUniforms.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -0700137 }
138 return addUniform(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700139}
140
141GLuint Program::buildShader(const char* source, GLenum type) {
Romain Guy3b748a42013-04-17 18:54:38 -0700142 ATRACE_CALL();
143
Romain Guy5cbbce52010-06-27 22:59:20 -0700144 GLuint shader = glCreateShader(type);
145 glShaderSource(shader, 1, &source, 0);
146 glCompileShader(shader);
147
148 GLint status;
149 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
150 if (status != GL_TRUE) {
151 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
152 // use a fixed size instead
153 GLchar log[512];
154 glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]);
Steve Block3762c312012-01-06 19:20:56 +0000155 ALOGE("Error while compiling shader: %s", log);
Romain Guy5cbbce52010-06-27 22:59:20 -0700156 glDeleteShader(shader);
Romain Guy67f27952010-12-07 20:09:23 -0800157 return 0;
Romain Guy5cbbce52010-06-27 22:59:20 -0700158 }
159
160 return shader;
161}
162
Romain Guy889f8d12010-07-29 14:37:42 -0700163void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700164 const mat4& transformMatrix, bool offset) {
Romain Guy3b748a42013-04-17 18:54:38 -0700165 if (projectionMatrix != mProjection) {
166 if (CC_LIKELY(!offset)) {
167 glUniformMatrix4fv(projection, 1, GL_FALSE, &projectionMatrix.data[0]);
168 } else {
169 mat4 p(projectionMatrix);
170 // offset screenspace xy by an amount that compensates for typical precision
171 // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted
172 // up and to the left.
173 // This offset value is based on an assumption that some hardware may use as
174 // little as 12.4 precision, so we offset by slightly more than 1/16.
Romain Guy4c2547f2013-06-11 16:19:24 -0700175 p.translate(.065, .065);
Romain Guy3b748a42013-04-17 18:54:38 -0700176 glUniformMatrix4fv(projection, 1, GL_FALSE, &p.data[0]);
177 }
178 mProjection = projectionMatrix;
Chet Haase8a5cc922011-04-26 07:28:09 -0700179 }
Romain Guy39284b72012-09-26 16:39:40 -0700180
181 mat4 t(transformMatrix);
Romain Guy0b9db912010-07-09 18:53:25 -0700182 t.multiply(modelViewMatrix);
Romain Guy0b9db912010-07-09 18:53:25 -0700183 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
Romain Guy5cbbce52010-06-27 22:59:20 -0700184}
185
Romain Guy707b2f72010-10-11 16:34:59 -0700186void Program::setColor(const float r, const float g, const float b, const float a) {
Romain Guy05bbde72011-12-09 12:55:37 -0800187 if (!mHasColorUniform) {
188 mColorUniform = getUniform("color");
Romain Guy6752d0a2011-12-12 12:15:17 -0800189 mHasColorUniform = true;
Romain Guy05bbde72011-12-09 12:55:37 -0800190 }
191 glUniform4f(mColorUniform, r, g, b, a);
Romain Guy707b2f72010-10-11 16:34:59 -0700192}
193
Romain Guy889f8d12010-07-29 14:37:42 -0700194void Program::use() {
Romain Guy05bbde72011-12-09 12:55:37 -0800195 glUseProgram(mProgramId);
Romain Guy2d4fd362011-12-13 22:00:19 -0800196 if (texCoords >= 0 && !mHasSampler) {
Chet Haase0990ffb2012-09-17 17:43:45 -0700197 glUniform1i(getUniform("baseSampler"), 0);
Romain Guy2d4fd362011-12-13 22:00:19 -0800198 mHasSampler = true;
199 }
Romain Guy889f8d12010-07-29 14:37:42 -0700200 mUse = true;
Romain Guy6926c722010-07-12 20:20:03 -0700201}
202
Romain Guy889f8d12010-07-29 14:37:42 -0700203void Program::remove() {
204 mUse = false;
Romain Guyf9764a42010-07-16 23:13:33 -0700205}
206
Romain Guy5cbbce52010-06-27 22:59:20 -0700207}; // namespace uirenderer
208}; // namespace android