blob: cb2a5fd2267ea99cfcef091d64027417b7858f3e [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 Guy5cbbce52010-06-27 22:59:20 -070029Program::Program(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 Guy05bbde72011-12-09 12:55:37 -080046 glLinkProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080047
48 GLint status;
Romain Guy05bbde72011-12-09 12:55:37 -080049 glGetProgramiv(mProgramId, GL_LINK_STATUS, &status);
Romain Guy67f27952010-12-07 20:09:23 -080050 if (status != GL_TRUE) {
51 LOGE("Error while linking shaders:");
52 GLint infoLen = 0;
Romain Guy05bbde72011-12-09 12:55:37 -080053 glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &infoLen);
Romain Guy67f27952010-12-07 20:09:23 -080054 if (infoLen > 1) {
55 GLchar log[infoLen];
Romain Guy05bbde72011-12-09 12:55:37 -080056 glGetProgramInfoLog(mProgramId, infoLen, 0, &log[0]);
Romain Guy67f27952010-12-07 20:09:23 -080057 LOGE("%s", log);
58 }
Romain Guy3e263fa2011-12-12 16:47:48 -080059
60 glDetachShader(mProgramId, mVertexShader);
61 glDetachShader(mProgramId, mFragmentShader);
62
63 glDeleteShader(mVertexShader);
64 glDeleteShader(mFragmentShader);
65
66 glDeleteProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080067 } else {
68 mInitialized = true;
69 }
Romain Guy05bbde72011-12-09 12:55:37 -080070 } else {
Romain Guy3e263fa2011-12-12 16:47:48 -080071 glDeleteShader(mVertexShader);
Romain Guy5cbbce52010-06-27 22:59:20 -070072 }
Romain Guy5cbbce52010-06-27 22:59:20 -070073 }
Romain Guy260e1022010-07-12 14:41:06 -070074
Romain Guy67f27952010-12-07 20:09:23 -080075 if (mInitialized) {
Romain Guy67f27952010-12-07 20:09:23 -080076 transform = addUniform("transform");
77 }
Romain Guy5cbbce52010-06-27 22:59:20 -070078}
79
80Program::~Program() {
Romain Guy67f27952010-12-07 20:09:23 -080081 if (mInitialized) {
Romain Guy3e263fa2011-12-12 16:47:48 -080082 glDetachShader(mProgramId, mVertexShader);
83 glDetachShader(mProgramId, mFragmentShader);
84
85 glDeleteShader(mVertexShader);
86 glDeleteShader(mFragmentShader);
87
Romain Guy05bbde72011-12-09 12:55:37 -080088 glDeleteProgram(mProgramId);
Romain Guy67f27952010-12-07 20:09:23 -080089 }
Romain Guy5cbbce52010-06-27 22:59:20 -070090}
91
Romain Guy5cbbce52010-06-27 22:59:20 -070092int Program::addAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -080093 int slot = glGetAttribLocation(mProgramId, name);
94 mAttributes.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -070095 return slot;
96}
97
Romain Guy3e263fa2011-12-12 16:47:48 -080098int Program::bindAttrib(const char* name, ShaderBindings bindingSlot) {
99 glBindAttribLocation(mProgramId, bindingSlot, name);
Romain Guy3e263fa2011-12-12 16:47:48 -0800100 mAttributes.add(name, bindingSlot);
101 return bindingSlot;
102}
103
Romain Guy5cbbce52010-06-27 22:59:20 -0700104int Program::getAttrib(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800105 ssize_t index = mAttributes.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -0700106 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -0800107 return mAttributes.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -0700108 }
109 return addAttrib(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700110}
111
112int Program::addUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800113 int slot = glGetUniformLocation(mProgramId, name);
114 mUniforms.add(name, slot);
Romain Guy5cbbce52010-06-27 22:59:20 -0700115 return slot;
116}
117
118int Program::getUniform(const char* name) {
Romain Guy05bbde72011-12-09 12:55:37 -0800119 ssize_t index = mUniforms.indexOfKey(name);
Romain Guy889f8d12010-07-29 14:37:42 -0700120 if (index >= 0) {
Romain Guy05bbde72011-12-09 12:55:37 -0800121 return mUniforms.valueAt(index);
Romain Guy889f8d12010-07-29 14:37:42 -0700122 }
123 return addUniform(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700124}
125
126GLuint Program::buildShader(const char* source, GLenum type) {
127 GLuint shader = glCreateShader(type);
128 glShaderSource(shader, 1, &source, 0);
129 glCompileShader(shader);
130
131 GLint status;
132 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
133 if (status != GL_TRUE) {
134 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
135 // use a fixed size instead
136 GLchar log[512];
137 glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]);
138 LOGE("Error while compiling shader: %s", log);
139 glDeleteShader(shader);
Romain Guy67f27952010-12-07 20:09:23 -0800140 return 0;
Romain Guy5cbbce52010-06-27 22:59:20 -0700141 }
142
143 return shader;
144}
145
Romain Guy889f8d12010-07-29 14:37:42 -0700146void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700147 const mat4& transformMatrix, bool offset) {
Romain Guy0b9db912010-07-09 18:53:25 -0700148 mat4 t(projectionMatrix);
Chet Haase8a5cc922011-04-26 07:28:09 -0700149 if (offset) {
Romain Guy24edca82011-12-09 13:08:06 -0800150 // offset screenspace xy by an amount that compensates for typical precision
151 // issues in GPU hardware that tends to paint hor/vert lines in pixels shifted
152 // up and to the left.
153 // This offset value is based on an assumption that some hardware may use as
154 // little as 12.4 precision, so we offset by slightly more than 1/16.
Chet Haase8a5cc922011-04-26 07:28:09 -0700155 t.translate(.375, .375, 0);
156 }
Romain Guy0b9db912010-07-09 18:53:25 -0700157 t.multiply(transformMatrix);
158 t.multiply(modelViewMatrix);
159
Romain Guy0b9db912010-07-09 18:53:25 -0700160 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
Romain Guy5cbbce52010-06-27 22:59:20 -0700161}
162
Romain Guy707b2f72010-10-11 16:34:59 -0700163void Program::setColor(const float r, const float g, const float b, const float a) {
Romain Guy05bbde72011-12-09 12:55:37 -0800164 if (!mHasColorUniform) {
165 mColorUniform = getUniform("color");
Romain Guy6752d0a2011-12-12 12:15:17 -0800166 mHasColorUniform = true;
Romain Guy05bbde72011-12-09 12:55:37 -0800167 }
168 glUniform4f(mColorUniform, r, g, b, a);
Romain Guy707b2f72010-10-11 16:34:59 -0700169}
170
Romain Guy889f8d12010-07-29 14:37:42 -0700171void Program::use() {
Romain Guy05bbde72011-12-09 12:55:37 -0800172 glUseProgram(mProgramId);
Romain Guy889f8d12010-07-29 14:37:42 -0700173 mUse = true;
Romain Guy6926c722010-07-12 20:20:03 -0700174}
175
Romain Guy889f8d12010-07-29 14:37:42 -0700176void Program::remove() {
177 mUse = false;
Romain Guyf9764a42010-07-16 23:13:33 -0700178}
179
Romain Guy5cbbce52010-06-27 22:59:20 -0700180}; // namespace uirenderer
181}; // namespace android