blob: 7814a01ad9295c5854349f2227f00fe851bc7028 [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"
Chris Craik32f05e32013-09-17 16:20:29 -070023#include "Vertex.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070024
25namespace android {
26namespace uirenderer {
27
28///////////////////////////////////////////////////////////////////////////////
Romain Guy5cbbce52010-06-27 22:59:20 -070029// Base program
30///////////////////////////////////////////////////////////////////////////////
31
Romain Guyf3a910b42011-12-12 20:35:21 -080032Program::Program(const ProgramDescription& description, const char* vertex, const char* fragment) {
Romain Guy67f27952010-12-07 20:09:23 -080033 mInitialized = false;
Romain Guy05bbde72011-12-09 12:55:37 -080034 mHasColorUniform = false;
Romain Guy2d4fd362011-12-13 22:00:19 -080035 mHasSampler = false;
Romain Guy3e263fa2011-12-12 16:47:48 -080036 mUse = false;
Romain Guy67f27952010-12-07 20:09:23 -080037
Romain Guy24edca82011-12-09 13:08:06 -080038 // No need to cache compiled shaders, rely instead on Android's
39 // persistent shaders cache
Romain Guy3e263fa2011-12-12 16:47:48 -080040 mVertexShader = buildShader(vertex, GL_VERTEX_SHADER);
41 if (mVertexShader) {
42 mFragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
43 if (mFragmentShader) {
Romain Guy05bbde72011-12-09 12:55:37 -080044 mProgramId = glCreateProgram();
Romain Guy3e263fa2011-12-12 16:47:48 -080045
46 glAttachShader(mProgramId, mVertexShader);
47 glAttachShader(mProgramId, mFragmentShader);
48
49 position = bindAttrib("position", kBindingPosition);
Romain Guyf3a910b42011-12-12 20:35:21 -080050 if (description.hasTexture || description.hasExternalTexture) {
51 texCoords = bindAttrib("texCoords", kBindingTexCoords);
52 } else {
53 texCoords = -1;
54 }
55
Romain Guy3b748a42013-04-17 18:54:38 -070056 ATRACE_BEGIN("linkProgram");
Romain Guy05bbde72011-12-09 12:55:37 -080057 glLinkProgram(mProgramId);
Romain Guy3b748a42013-04-17 18:54:38 -070058 ATRACE_END();
Romain Guy67f27952010-12-07 20:09:23 -080059
60 GLint status;
Romain Guy05bbde72011-12-09 12:55:37 -080061 glGetProgramiv(mProgramId, GL_LINK_STATUS, &status);
Romain Guy67f27952010-12-07 20:09:23 -080062 if (status != GL_TRUE) {
Steve Block3762c312012-01-06 19:20:56 +000063 ALOGE("Error while linking shaders:");
Romain Guy67f27952010-12-07 20:09:23 -080064 GLint infoLen = 0;
Romain Guy05bbde72011-12-09 12:55:37 -080065 glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen);
Romain Guy67f27952010-12-07 20:09:23 -080066 if (infoLen > 1) {
67 GLchar log[infoLen];
Romain Guy05bbde72011-12-09 12:55:37 -080068 glGetProgramInfoLog(mProgramId, infoLen, 0, &log[0]);
Steve Block3762c312012-01-06 19:20:56 +000069 ALOGE("%s", log);
Romain Guy67f27952010-12-07 20:09:23 -080070 }
Romain Guy3e263fa2011-12-12 16:47:48 -080071
72 glDetachShader(mProgramId, mVertexShader);
73 glDetachShader(mProgramId, mFragmentShader);
74
75 glDeleteShader(mVertexShader);
76 glDeleteShader(mFragmentShader);
77
78 glDeleteProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080079 } else {
80 mInitialized = true;
81 }
Romain Guy05bbde72011-12-09 12:55:37 -080082 } else {
Romain Guy3e263fa2011-12-12 16:47:48 -080083 glDeleteShader(mVertexShader);
Romain Guy5cbbce52010-06-27 22:59:20 -070084 }
Romain Guy5cbbce52010-06-27 22:59:20 -070085 }
Romain Guy260e1022010-07-12 14:41:06 -070086
Romain Guy67f27952010-12-07 20:09:23 -080087 if (mInitialized) {
Romain Guy67f27952010-12-07 20:09:23 -080088 transform = addUniform("transform");
Romain Guy39284b72012-09-26 16:39:40 -070089 projection = addUniform("projection");
Romain Guy67f27952010-12-07 20:09:23 -080090 }
Romain Guy5cbbce52010-06-27 22:59:20 -070091}
92
93Program::~Program() {
Romain Guy67f27952010-12-07 20:09:23 -080094 if (mInitialized) {
Romain Guy3b748a42013-04-17 18:54:38 -070095 // This would ideally happen after linking the program
96 // but Tegra drivers, especially when perfhud is enabled,
97 // sometimes crash if we do so
Romain Guy3e263fa2011-12-12 16:47:48 -080098 glDetachShader(mProgramId, mVertexShader);
99 glDetachShader(mProgramId, mFragmentShader);
100
101 glDeleteShader(mVertexShader);
102 glDeleteShader(mFragmentShader);
103
Romain Guy05bbde72011-12-09 12:55:37 -0800104 glDeleteProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -0800105 }
Romain Guy5cbbce52010-06-27 22:59:20 -0700106}
107
Romain Guy5cbbce52010-06-27 22:59:20 -0700108int Program::addAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800109 int slot = glGetAttribLocation(mProgramId, name);
110 mAttributes.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -0700111 return slot;
112}
113
Romain Guy3e263fa2011-12-12 16:47:48 -0800114int Program::bindAttrib(const char* name, ShaderBindings bindingSlot) {
115 glBindAttribLocation(mProgramId, bindingSlot, name);
Romain Guy3e263fa2011-12-12 16:47:48 -0800116 mAttributes.add(name, bindingSlot);
117 return bindingSlot;
118}
119
Romain Guy5cbbce52010-06-27 22:59:20 -0700120int Program::getAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800121 ssize_t index = mAttributes.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -0700122 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -0800123 return mAttributes.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -0700124 }
125 return addAttrib(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700126}
127
128int Program::addUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800129 int slot = glGetUniformLocation(mProgramId, name);
130 mUniforms.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -0700131 return slot;
132}
133
134int Program::getUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800135 ssize_t index = mUniforms.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -0700136 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -0800137 return mUniforms.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -0700138 }
139 return addUniform(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700140}
141
142GLuint Program::buildShader(const char* source, GLenum type) {
Romain Guy3b748a42013-04-17 18:54:38 -0700143 ATRACE_CALL();
144
Romain Guy5cbbce52010-06-27 22:59:20 -0700145 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]);
Steve Block3762c312012-01-06 19:20:56 +0000156 ALOGE("Error while compiling shader: %s", log);
Romain Guy5cbbce52010-06-27 22:59:20 -0700157 glDeleteShader(shader);
Romain Guy67f27952010-12-07 20:09:23 -0800158 return 0;
Romain Guy5cbbce52010-06-27 22:59:20 -0700159 }
160
161 return shader;
162}
163
Romain Guy889f8d12010-07-29 14:37:42 -0700164void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700165 const mat4& transformMatrix, bool offset) {
Romain Guy3b748a42013-04-17 18:54:38 -0700166 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.
Chris Craik32f05e32013-09-17 16:20:29 -0700176 p.translate(Vertex::gGeometryFudgeFactor, Vertex::gGeometryFudgeFactor);
Romain Guy3b748a42013-04-17 18:54:38 -0700177 glUniformMatrix4fv(projection, 1, GL_FALSE, &p.data[0]);
178 }
179 mProjection = projectionMatrix;
Chet Haase8a5cc922011-04-26 07:28:09 -0700180 }
Romain Guy39284b72012-09-26 16:39:40 -0700181
182 mat4 t(transformMatrix);
Romain Guy0b9db912010-07-09 18:53:25 -0700183 t.multiply(modelViewMatrix);
Romain Guy0b9db912010-07-09 18:53:25 -0700184 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
Romain Guy5cbbce52010-06-27 22:59:20 -0700185}
186
Romain Guy707b2f72010-10-11 16:34:59 -0700187void Program::setColor(const float r, const float g, const float b, const float a) {
Romain Guy05bbde72011-12-09 12:55:37 -0800188 if (!mHasColorUniform) {
189 mColorUniform = getUniform("color");
Romain Guy6752d0a2011-12-12 12:15:17 -0800190 mHasColorUniform = true;
Romain Guy05bbde72011-12-09 12:55:37 -0800191 }
192 glUniform4f(mColorUniform, r, g, b, a);
Romain Guy707b2f72010-10-11 16:34:59 -0700193}
194
Romain Guy889f8d12010-07-29 14:37:42 -0700195void Program::use() {
Romain Guy05bbde72011-12-09 12:55:37 -0800196 glUseProgram(mProgramId);
Romain Guy2d4fd362011-12-13 22:00:19 -0800197 if (texCoords >= 0 && !mHasSampler) {
Chet Haase0990ffb2012-09-17 17:43:45 -0700198 glUniform1i(getUniform("baseSampler"), 0);
Romain Guy2d4fd362011-12-13 22:00:19 -0800199 mHasSampler = true;
200 }
Romain Guy889f8d12010-07-29 14:37:42 -0700201 mUse = true;
Romain Guy6926c722010-07-12 20:20:03 -0700202}
203
Romain Guy889f8d12010-07-29 14:37:42 -0700204void Program::remove() {
205 mUse = false;
Romain Guyf9764a42010-07-16 23:13:33 -0700206}
207
Romain Guy5cbbce52010-06-27 22:59:20 -0700208}; // namespace uirenderer
209}; // namespace android