blob: 14a23765b301bf8524e93d190aa275479fab3a49 [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
Romain Guy3e263fa2011-12-12 16:47:48 -080028// TODO: Program instance should be created from a factory method
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
46 position = 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 Guy05bbde72011-12-09 12:55:37 -080053 glLinkProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080054
55 GLint status;
Romain Guy05bbde72011-12-09 12:55:37 -080056 glGetProgramiv(mProgramId, GL_LINK_STATUS, &status);
Romain Guy67f27952010-12-07 20:09:23 -080057 if (status != GL_TRUE) {
Steve Block3762c312012-01-06 19:20:56 +000058 ALOGE("Error while linking shaders:");
Romain Guy67f27952010-12-07 20:09:23 -080059 GLint infoLen = 0;
Romain Guy05bbde72011-12-09 12:55:37 -080060 glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen);
Romain Guy67f27952010-12-07 20:09:23 -080061 if (infoLen > 1) {
62 GLchar log[infoLen];
Romain Guy05bbde72011-12-09 12:55:37 -080063 glGetProgramInfoLog(mProgramId, infoLen, 0, &log[0]);
Steve Block3762c312012-01-06 19:20:56 +000064 ALOGE("%s", log);
Romain Guy67f27952010-12-07 20:09:23 -080065 }
Romain Guy3e263fa2011-12-12 16:47:48 -080066
67 glDetachShader(mProgramId, mVertexShader);
68 glDetachShader(mProgramId, mFragmentShader);
69
70 glDeleteShader(mVertexShader);
71 glDeleteShader(mFragmentShader);
72
73 glDeleteProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080074 } else {
75 mInitialized = true;
76 }
Romain Guy05bbde72011-12-09 12:55:37 -080077 } else {
Romain Guy3e263fa2011-12-12 16:47:48 -080078 glDeleteShader(mVertexShader);
Romain Guy5cbbce52010-06-27 22:59:20 -070079 }
Romain Guy5cbbce52010-06-27 22:59:20 -070080 }
Romain Guy260e1022010-07-12 14:41:06 -070081
Romain Guy67f27952010-12-07 20:09:23 -080082 if (mInitialized) {
Romain Guy67f27952010-12-07 20:09:23 -080083 transform = addUniform("transform");
Romain Guy39284b72012-09-26 16:39:40 -070084 projection = addUniform("projection");
Romain Guy67f27952010-12-07 20:09:23 -080085 }
Romain Guy5cbbce52010-06-27 22:59:20 -070086}
87
88Program::~Program() {
Romain Guy67f27952010-12-07 20:09:23 -080089 if (mInitialized) {
Romain Guy3e263fa2011-12-12 16:47:48 -080090 glDetachShader(mProgramId, mVertexShader);
91 glDetachShader(mProgramId, mFragmentShader);
92
93 glDeleteShader(mVertexShader);
94 glDeleteShader(mFragmentShader);
95
Romain Guy05bbde72011-12-09 12:55:37 -080096 glDeleteProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080097 }
Romain Guy5cbbce52010-06-27 22:59:20 -070098}
99
Romain Guy5cbbce52010-06-27 22:59:20 -0700100int Program::addAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800101 int slot = glGetAttribLocation(mProgramId, name);
102 mAttributes.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -0700103 return slot;
104}
105
Romain Guy3e263fa2011-12-12 16:47:48 -0800106int Program::bindAttrib(const char* name, ShaderBindings bindingSlot) {
107 glBindAttribLocation(mProgramId, bindingSlot, name);
Romain Guy3e263fa2011-12-12 16:47:48 -0800108 mAttributes.add(name, bindingSlot);
109 return bindingSlot;
110}
111
Romain Guy5cbbce52010-06-27 22:59:20 -0700112int Program::getAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800113 ssize_t index = mAttributes.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -0700114 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -0800115 return mAttributes.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -0700116 }
117 return addAttrib(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700118}
119
120int Program::addUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800121 int slot = glGetUniformLocation(mProgramId, name);
122 mUniforms.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -0700123 return slot;
124}
125
126int Program::getUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800127 ssize_t index = mUniforms.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -0700128 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -0800129 return mUniforms.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -0700130 }
131 return addUniform(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700132}
133
134GLuint Program::buildShader(const char* source, GLenum type) {
135 GLuint shader = glCreateShader(type);
136 glShaderSource(shader, 1, &source, 0);
137 glCompileShader(shader);
138
139 GLint status;
140 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
141 if (status != GL_TRUE) {
142 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
143 // use a fixed size instead
144 GLchar log[512];
145 glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]);
Steve Block3762c312012-01-06 19:20:56 +0000146 ALOGE("Error while compiling shader: %s", log);
Romain Guy5cbbce52010-06-27 22:59:20 -0700147 glDeleteShader(shader);
Romain Guy67f27952010-12-07 20:09:23 -0800148 return 0;
Romain Guy5cbbce52010-06-27 22:59:20 -0700149 }
150
151 return shader;
152}
153
Romain Guy889f8d12010-07-29 14:37:42 -0700154void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700155 const mat4& transformMatrix, bool offset) {
Romain Guy39284b72012-09-26 16:39:40 -0700156 mat4 p(projectionMatrix);
Chet Haase8a5cc922011-04-26 07:28:09 -0700157 if (offset) {
Romain Guy24edca82011-12-09 13:08:06 -0800158 // offset screenspace xy by an amount that compensates for typical precision
159 // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted
160 // up and to the left.
161 // This offset value is based on an assumption that some hardware may use as
162 // little as 12.4 precision, so we offset by slightly more than 1/16.
Chris Craik62a42c12013-02-15 11:58:14 -0800163 p.translate(.065, .065, 0);
Chet Haase8a5cc922011-04-26 07:28:09 -0700164 }
Romain Guy39284b72012-09-26 16:39:40 -0700165
166 mat4 t(transformMatrix);
Romain Guy0b9db912010-07-09 18:53:25 -0700167 t.multiply(modelViewMatrix);
168
Romain Guy39284b72012-09-26 16:39:40 -0700169 glUniformMatrix4fv(projection, 1, GL_FALSE, &p.data[0]);
Romain Guy0b9db912010-07-09 18:53:25 -0700170 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
Romain Guy5cbbce52010-06-27 22:59:20 -0700171}
172
Romain Guy707b2f72010-10-11 16:34:59 -0700173void Program::setColor(const float r, const float g, const float b, const float a) {
Romain Guy05bbde72011-12-09 12:55:37 -0800174 if (!mHasColorUniform) {
175 mColorUniform = getUniform("color");
Romain Guy6752d0a2011-12-12 12:15:17 -0800176 mHasColorUniform = true;
Romain Guy05bbde72011-12-09 12:55:37 -0800177 }
178 glUniform4f(mColorUniform, r, g, b, a);
Romain Guy707b2f72010-10-11 16:34:59 -0700179}
180
Romain Guy889f8d12010-07-29 14:37:42 -0700181void Program::use() {
Romain Guy05bbde72011-12-09 12:55:37 -0800182 glUseProgram(mProgramId);
Romain Guy2d4fd362011-12-13 22:00:19 -0800183 if (texCoords >= 0 && !mHasSampler) {
Chet Haase0990ffb2012-09-17 17:43:45 -0700184 glUniform1i(getUniform("baseSampler"), 0);
Romain Guy2d4fd362011-12-13 22:00:19 -0800185 mHasSampler = true;
186 }
Romain Guy889f8d12010-07-29 14:37:42 -0700187 mUse = true;
Romain Guy6926c722010-07-12 20:20:03 -0700188}
189
Romain Guy889f8d12010-07-29 14:37:42 -0700190void Program::remove() {
191 mUse = false;
Romain Guyf9764a42010-07-16 23:13:33 -0700192}
193
Romain Guy5cbbce52010-06-27 22:59:20 -0700194}; // namespace uirenderer
195}; // namespace android