blob: e43b80d440e76770406c3011eb1ad6f4ade45d1f [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
Romain Guy3b748a42013-04-17 18:54:38 -070017#include <utils/Trace.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070018
19#include "Program.h"
Chris Craik32f05e32013-09-17 16:20:29 -070020#include "Vertex.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070021
22namespace android {
23namespace uirenderer {
24
25///////////////////////////////////////////////////////////////////////////////
Romain Guy5cbbce52010-06-27 22:59:20 -070026// Base program
27///////////////////////////////////////////////////////////////////////////////
28
Romain Guyf3a910b42011-12-12 20:35:21 -080029Program::Program(const ProgramDescription& description, const char* vertex, const char* fragment) {
Romain Guy67f27952010-12-07 20:09:23 -080030 mInitialized = false;
Romain Guy05bbde72011-12-09 12:55:37 -080031 mHasColorUniform = false;
Romain Guy2d4fd362011-12-13 22:00:19 -080032 mHasSampler = false;
Romain Guy3e263fa2011-12-12 16:47:48 -080033 mUse = false;
Romain Guy67f27952010-12-07 20:09:23 -080034
Romain Guy24edca82011-12-09 13:08:06 -080035 // No need to cache compiled shaders, rely instead on Android's
36 // persistent shaders cache
Romain Guy3e263fa2011-12-12 16:47:48 -080037 mVertexShader = buildShader(vertex, GL_VERTEX_SHADER);
38 if (mVertexShader) {
39 mFragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
40 if (mFragmentShader) {
Romain Guy05bbde72011-12-09 12:55:37 -080041 mProgramId = glCreateProgram();
Romain Guy3e263fa2011-12-12 16:47:48 -080042
43 glAttachShader(mProgramId, mVertexShader);
44 glAttachShader(mProgramId, mFragmentShader);
45
Chris Craik6c15ffa2015-02-02 13:50:55 -080046 bindAttrib("position", kBindingPosition);
Romain Guyf3a910b42011-12-12 20:35:21 -080047 if (description.hasTexture || description.hasExternalTexture) {
48 texCoords = bindAttrib("texCoords", kBindingTexCoords);
49 } else {
50 texCoords = -1;
51 }
52
Romain Guy3b748a42013-04-17 18:54:38 -070053 ATRACE_BEGIN("linkProgram");
Romain Guy05bbde72011-12-09 12:55:37 -080054 glLinkProgram(mProgramId);
Romain Guy3b748a42013-04-17 18:54:38 -070055 ATRACE_END();
Romain Guy67f27952010-12-07 20:09:23 -080056
57 GLint status;
Romain Guy05bbde72011-12-09 12:55:37 -080058 glGetProgramiv(mProgramId, GL_LINK_STATUS, &status);
Romain Guy67f27952010-12-07 20:09:23 -080059 if (status != GL_TRUE) {
Romain Guy67f27952010-12-07 20:09:23 -080060 GLint infoLen = 0;
Romain Guy05bbde72011-12-09 12:55:37 -080061 glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen);
Romain Guy67f27952010-12-07 20:09:23 -080062 if (infoLen > 1) {
63 GLchar log[infoLen];
Chris Craikd41c4d82015-01-05 15:51:13 -080064 glGetProgramInfoLog(mProgramId, infoLen, nullptr, &log[0]);
Steve Block3762c312012-01-06 19:20:56 +000065 ALOGE("%s", log);
Romain Guy67f27952010-12-07 20:09:23 -080066 }
Chris Craik73842582014-07-10 16:24:14 -070067 LOG_ALWAYS_FATAL("Error while linking shaders");
Romain Guy67f27952010-12-07 20:09:23 -080068 } else {
69 mInitialized = true;
70 }
Romain Guy05bbde72011-12-09 12:55:37 -080071 } else {
Romain Guy3e263fa2011-12-12 16:47:48 -080072 glDeleteShader(mVertexShader);
Romain Guy5cbbce52010-06-27 22:59:20 -070073 }
Romain Guy5cbbce52010-06-27 22:59:20 -070074 }
Romain Guy260e1022010-07-12 14:41:06 -070075
Romain Guy67f27952010-12-07 20:09:23 -080076 if (mInitialized) {
Romain Guy67f27952010-12-07 20:09:23 -080077 transform = addUniform("transform");
Romain Guy39284b72012-09-26 16:39:40 -070078 projection = addUniform("projection");
Romain Guy67f27952010-12-07 20:09:23 -080079 }
Romain Guy5cbbce52010-06-27 22:59:20 -070080}
81
82Program::~Program() {
Romain Guy67f27952010-12-07 20:09:23 -080083 if (mInitialized) {
Romain Guy3b748a42013-04-17 18:54:38 -070084 // This would ideally happen after linking the program
85 // but Tegra drivers, especially when perfhud is enabled,
86 // sometimes crash if we do so
Romain Guy3e263fa2011-12-12 16:47:48 -080087 glDetachShader(mProgramId, mVertexShader);
88 glDetachShader(mProgramId, mFragmentShader);
89
90 glDeleteShader(mVertexShader);
91 glDeleteShader(mFragmentShader);
92
Romain Guy05bbde72011-12-09 12:55:37 -080093 glDeleteProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080094 }
Romain Guy5cbbce52010-06-27 22:59:20 -070095}
96
Romain Guy5cbbce52010-06-27 22:59:20 -070097int Program::addAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -080098 int slot = glGetAttribLocation(mProgramId, name);
99 mAttributes.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -0700100 return slot;
101}
102
Romain Guy3e263fa2011-12-12 16:47:48 -0800103int Program::bindAttrib(const char* name, ShaderBindings bindingSlot) {
104 glBindAttribLocation(mProgramId, bindingSlot, name);
Romain Guy3e263fa2011-12-12 16:47:48 -0800105 mAttributes.add(name, bindingSlot);
106 return bindingSlot;
107}
108
Romain Guy5cbbce52010-06-27 22:59:20 -0700109int Program::getAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800110 ssize_t index = mAttributes.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -0700111 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -0800112 return mAttributes.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -0700113 }
114 return addAttrib(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700115}
116
117int Program::addUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800118 int slot = glGetUniformLocation(mProgramId, name);
119 mUniforms.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -0700120 return slot;
121}
122
123int Program::getUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800124 ssize_t index = mUniforms.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -0700125 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -0800126 return mUniforms.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -0700127 }
128 return addUniform(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700129}
130
131GLuint Program::buildShader(const char* source, GLenum type) {
Chris Craik70850ea2014-11-18 10:49:23 -0800132 ATRACE_NAME("Build GL Shader");
Romain Guy3b748a42013-04-17 18:54:38 -0700133
Romain Guy5cbbce52010-06-27 22:59:20 -0700134 GLuint shader = glCreateShader(type);
Chris Craikd41c4d82015-01-05 15:51:13 -0800135 glShaderSource(shader, 1, &source, nullptr);
Romain Guy5cbbce52010-06-27 22:59:20 -0700136 glCompileShader(shader);
137
138 GLint status;
139 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
140 if (status != GL_TRUE) {
Rob Tsukfd15f472014-11-05 13:26:19 -0800141 ALOGE("Error while compiling this shader:\n===\n%s\n===", source);
Romain Guy5cbbce52010-06-27 22:59:20 -0700142 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
143 // use a fixed size instead
144 GLchar log[512];
Chris Craikd41c4d82015-01-05 15:51:13 -0800145 glGetShaderInfoLog(shader, sizeof(log), nullptr, &log[0]);
Rob Tsukfd15f472014-11-05 13:26:19 -0800146 LOG_ALWAYS_FATAL("Shader info log: %s", log);
Romain Guy67f27952010-12-07 20:09:23 -0800147 return 0;
Romain Guy5cbbce52010-06-27 22:59:20 -0700148 }
149
150 return shader;
151}
152
Romain Guy889f8d12010-07-29 14:37:42 -0700153void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700154 const mat4& transformMatrix, bool offset) {
Chris Craikd04a6b12014-01-29 13:00:33 -0800155 if (projectionMatrix != mProjection || offset != mOffset) {
Romain Guy3b748a42013-04-17 18:54:38 -0700156 if (CC_LIKELY(!offset)) {
157 glUniformMatrix4fv(projection, 1, GL_FALSE, &projectionMatrix.data[0]);
158 } else {
159 mat4 p(projectionMatrix);
160 // offset screenspace xy by an amount that compensates for typical precision
161 // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted
162 // up and to the left.
163 // This offset value is based on an assumption that some hardware may use as
164 // little as 12.4 precision, so we offset by slightly more than 1/16.
Chris Craik564acf72014-01-02 16:46:18 -0800165 p.translate(Vertex::GeometryFudgeFactor(), Vertex::GeometryFudgeFactor());
Romain Guy3b748a42013-04-17 18:54:38 -0700166 glUniformMatrix4fv(projection, 1, GL_FALSE, &p.data[0]);
167 }
168 mProjection = projectionMatrix;
Chris Craikd04a6b12014-01-29 13:00:33 -0800169 mOffset = offset;
Chet Haase8a5cc922011-04-26 07:28:09 -0700170 }
Romain Guy39284b72012-09-26 16:39:40 -0700171
172 mat4 t(transformMatrix);
Romain Guy0b9db912010-07-09 18:53:25 -0700173 t.multiply(modelViewMatrix);
Romain Guy0b9db912010-07-09 18:53:25 -0700174 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
Romain Guy5cbbce52010-06-27 22:59:20 -0700175}
176
Chris Craik0519c812015-02-11 13:17:06 -0800177void Program::setColor(FloatColor color) {
Romain Guy05bbde72011-12-09 12:55:37 -0800178 if (!mHasColorUniform) {
179 mColorUniform = getUniform("color");
Romain Guy6752d0a2011-12-12 12:15:17 -0800180 mHasColorUniform = true;
Romain Guy05bbde72011-12-09 12:55:37 -0800181 }
Chris Craik0519c812015-02-11 13:17:06 -0800182 glUniform4f(mColorUniform, color.r, color.g, color.b, color.a);
Romain Guy707b2f72010-10-11 16:34:59 -0700183}
184
Romain Guy889f8d12010-07-29 14:37:42 -0700185void Program::use() {
Romain Guy05bbde72011-12-09 12:55:37 -0800186 glUseProgram(mProgramId);
Romain Guy2d4fd362011-12-13 22:00:19 -0800187 if (texCoords >= 0 && !mHasSampler) {
Chet Haase0990ffb2012-09-17 17:43:45 -0700188 glUniform1i(getUniform("baseSampler"), 0);
Romain Guy2d4fd362011-12-13 22:00:19 -0800189 mHasSampler = true;
190 }
Romain Guy889f8d12010-07-29 14:37:42 -0700191 mUse = true;
Romain Guy6926c722010-07-12 20:20:03 -0700192}
193
Romain Guy889f8d12010-07-29 14:37:42 -0700194void Program::remove() {
195 mUse = false;
Romain Guyf9764a42010-07-16 23:13:33 -0700196}
197
Romain Guy5cbbce52010-06-27 22:59:20 -0700198}; // namespace uirenderer
199}; // namespace android