blob: cbea843acf04e1d17d38797811f7318d16ed6f0c [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 Guy3e263fa2011-12-12 16:47:48 -080032 mUse = false;
Romain Guy67f27952010-12-07 20:09:23 -080033
Romain Guy24edca82011-12-09 13:08:06 -080034 // No need to cache compiled shaders, rely instead on Android's
35 // persistent shaders cache
Romain Guy3e263fa2011-12-12 16:47:48 -080036 mVertexShader = buildShader(vertex, GL_VERTEX_SHADER);
37 if (mVertexShader) {
38 mFragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
39 if (mFragmentShader) {
Romain Guy05bbde72011-12-09 12:55:37 -080040 mProgramId = glCreateProgram();
Romain Guy3e263fa2011-12-12 16:47:48 -080041
42 glAttachShader(mProgramId, mVertexShader);
43 glAttachShader(mProgramId, mFragmentShader);
44
45 position = bindAttrib("position", kBindingPosition);
Romain Guyf3a910b42011-12-12 20:35:21 -080046 if (description.hasTexture || description.hasExternalTexture) {
47 texCoords = bindAttrib("texCoords", kBindingTexCoords);
48 } else {
49 texCoords = -1;
50 }
51
Romain Guy05bbde72011-12-09 12:55:37 -080052 glLinkProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080053
54 GLint status;
Romain Guy05bbde72011-12-09 12:55:37 -080055 glGetProgramiv(mProgramId, GL_LINK_STATUS, &status);
Romain Guy67f27952010-12-07 20:09:23 -080056 if (status != GL_TRUE) {
57 LOGE("Error while linking shaders:");
58 GLint infoLen = 0;
Romain Guy05bbde72011-12-09 12:55:37 -080059 glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen);
Romain Guy67f27952010-12-07 20:09:23 -080060 if (infoLen > 1) {
61 GLchar log[infoLen];
Romain Guy05bbde72011-12-09 12:55:37 -080062 glGetProgramInfoLog(mProgramId, infoLen, 0, &log[0]);
Romain Guy67f27952010-12-07 20:09:23 -080063 LOGE("%s", log);
64 }
Romain Guy3e263fa2011-12-12 16:47:48 -080065
66 glDetachShader(mProgramId, mVertexShader);
67 glDetachShader(mProgramId, mFragmentShader);
68
69 glDeleteShader(mVertexShader);
70 glDeleteShader(mFragmentShader);
71
72 glDeleteProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080073 } else {
74 mInitialized = true;
75 }
Romain Guy05bbde72011-12-09 12:55:37 -080076 } else {
Romain Guy3e263fa2011-12-12 16:47:48 -080077 glDeleteShader(mVertexShader);
Romain Guy5cbbce52010-06-27 22:59:20 -070078 }
Romain Guy5cbbce52010-06-27 22:59:20 -070079 }
Romain Guy260e1022010-07-12 14:41:06 -070080
Romain Guy67f27952010-12-07 20:09:23 -080081 if (mInitialized) {
Romain Guy67f27952010-12-07 20:09:23 -080082 transform = addUniform("transform");
83 }
Romain Guy5cbbce52010-06-27 22:59:20 -070084}
85
86Program::~Program() {
Romain Guy67f27952010-12-07 20:09:23 -080087 if (mInitialized) {
Romain Guy3e263fa2011-12-12 16:47:48 -080088 glDetachShader(mProgramId, mVertexShader);
89 glDetachShader(mProgramId, mFragmentShader);
90
91 glDeleteShader(mVertexShader);
92 glDeleteShader(mFragmentShader);
93
Romain Guy05bbde72011-12-09 12:55:37 -080094 glDeleteProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080095 }
Romain Guy5cbbce52010-06-27 22:59:20 -070096}
97
Romain Guy5cbbce52010-06-27 22:59:20 -070098int Program::addAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -080099 int slot = glGetAttribLocation(mProgramId, name);
100 mAttributes.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -0700101 return slot;
102}
103
Romain Guy3e263fa2011-12-12 16:47:48 -0800104int Program::bindAttrib(const char* name, ShaderBindings bindingSlot) {
105 glBindAttribLocation(mProgramId, bindingSlot, name);
Romain Guy3e263fa2011-12-12 16:47:48 -0800106 mAttributes.add(name, bindingSlot);
107 return bindingSlot;
108}
109
Romain Guy5cbbce52010-06-27 22:59:20 -0700110int Program::getAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800111 ssize_t index = mAttributes.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -0700112 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -0800113 return mAttributes.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -0700114 }
115 return addAttrib(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700116}
117
118int Program::addUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800119 int slot = glGetUniformLocation(mProgramId, name);
120 mUniforms.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -0700121 return slot;
122}
123
124int Program::getUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800125 ssize_t index = mUniforms.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -0700126 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -0800127 return mUniforms.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -0700128 }
129 return addUniform(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700130}
131
132GLuint Program::buildShader(const char* source, GLenum type) {
133 GLuint shader = glCreateShader(type);
134 glShaderSource(shader, 1, &source, 0);
135 glCompileShader(shader);
136
137 GLint status;
138 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
139 if (status != GL_TRUE) {
140 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
141 // use a fixed size instead
142 GLchar log[512];
143 glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]);
144 LOGE("Error while compiling shader: %s", log);
145 glDeleteShader(shader);
Romain Guy67f27952010-12-07 20:09:23 -0800146 return 0;
Romain Guy5cbbce52010-06-27 22:59:20 -0700147 }
148
149 return shader;
150}
151
Romain Guy889f8d12010-07-29 14:37:42 -0700152void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700153 const mat4& transformMatrix, bool offset) {
Romain Guy0b9db912010-07-09 18:53:25 -0700154 mat4 t(projectionMatrix);
Chet Haase8a5cc922011-04-26 07:28:09 -0700155 if (offset) {
Romain Guy24edca82011-12-09 13:08:06 -0800156 // offset screenspace xy by an amount that compensates for typical precision
157 // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted
158 // up and to the left.
159 // This offset value is based on an assumption that some hardware may use as
160 // little as 12.4 precision, so we offset by slightly more than 1/16.
Chet Haase8a5cc922011-04-26 07:28:09 -0700161 t.translate(.375, .375, 0);
162 }
Romain Guy0b9db912010-07-09 18:53:25 -0700163 t.multiply(transformMatrix);
164 t.multiply(modelViewMatrix);
165
Romain Guy0b9db912010-07-09 18:53:25 -0700166 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
Romain Guy5cbbce52010-06-27 22:59:20 -0700167}
168
Romain Guy707b2f72010-10-11 16:34:59 -0700169void Program::setColor(const float r, const float g, const float b, const float a) {
Romain Guy05bbde72011-12-09 12:55:37 -0800170 if (!mHasColorUniform) {
171 mColorUniform = getUniform("color");
Romain Guy6752d0a2011-12-12 12:15:17 -0800172 mHasColorUniform = true;
Romain Guy05bbde72011-12-09 12:55:37 -0800173 }
174 glUniform4f(mColorUniform, r, g, b, a);
Romain Guy707b2f72010-10-11 16:34:59 -0700175}
176
Romain Guy889f8d12010-07-29 14:37:42 -0700177void Program::use() {
Romain Guy05bbde72011-12-09 12:55:37 -0800178 glUseProgram(mProgramId);
Romain Guy889f8d12010-07-29 14:37:42 -0700179 mUse = true;
Romain Guy6926c722010-07-12 20:20:03 -0700180}
181
Romain Guy889f8d12010-07-29 14:37:42 -0700182void Program::remove() {
183 mUse = false;
Romain Guyf9764a42010-07-16 23:13:33 -0700184}
185
Romain Guy5cbbce52010-06-27 22:59:20 -0700186}; // namespace uirenderer
187}; // namespace android