blob: 972dd87a5d7196b25424850f593b8f87accd1a38 [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
28Program::Program(const char* vertex, const char* fragment) {
Romain Guy67f27952010-12-07 20:09:23 -080029 mInitialized = false;
30
Romain Guy5cbbce52010-06-27 22:59:20 -070031 vertexShader = buildShader(vertex, GL_VERTEX_SHADER);
Romain Guy67f27952010-12-07 20:09:23 -080032 if (vertexShader) {
Romain Guy5cbbce52010-06-27 22:59:20 -070033
Romain Guy67f27952010-12-07 20:09:23 -080034 fragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
35 if (fragmentShader) {
Romain Guy5cbbce52010-06-27 22:59:20 -070036
Romain Guy67f27952010-12-07 20:09:23 -080037 id = glCreateProgram();
38 glAttachShader(id, vertexShader);
39 glAttachShader(id, fragmentShader);
40 glLinkProgram(id);
41
42 GLint status;
43 glGetProgramiv(id, GL_LINK_STATUS, &status);
44 if (status != GL_TRUE) {
45 LOGE("Error while linking shaders:");
46 GLint infoLen = 0;
47 glGetProgramiv(id, GL_INFO_LOG_LENGTH, &infoLen);
48 if (infoLen > 1) {
49 GLchar log[infoLen];
50 glGetProgramInfoLog(id, infoLen, 0, &log[0]);
51 LOGE("%s", log);
52 }
53 glDeleteShader(vertexShader);
54 glDeleteShader(fragmentShader);
55 glDeleteProgram(id);
56 } else {
57 mInitialized = true;
58 }
Romain Guy5cbbce52010-06-27 22:59:20 -070059 }
Romain Guy5cbbce52010-06-27 22:59:20 -070060 }
Romain Guy260e1022010-07-12 14:41:06 -070061
62 mUse = false;
Romain Guy889f8d12010-07-29 14:37:42 -070063
Romain Guy67f27952010-12-07 20:09:23 -080064 if (mInitialized) {
65 position = addAttrib("position");
66 transform = addUniform("transform");
67 }
Romain Guy5cbbce52010-06-27 22:59:20 -070068}
69
70Program::~Program() {
Romain Guy67f27952010-12-07 20:09:23 -080071 if (mInitialized) {
72 glDeleteShader(vertexShader);
73 glDeleteShader(fragmentShader);
74 glDeleteProgram(id);
75 }
Romain Guy5cbbce52010-06-27 22:59:20 -070076}
77
Romain Guy5cbbce52010-06-27 22:59:20 -070078int Program::addAttrib(const char* name) {
79 int slot = glGetAttribLocation(id, name);
80 attributes.add(name, slot);
81 return slot;
82}
83
84int Program::getAttrib(const char* name) {
Romain Guy889f8d12010-07-29 14:37:42 -070085 ssize_t index = attributes.indexOfKey(name);
86 if (index >= 0) {
87 return attributes.valueAt(index);
88 }
89 return addAttrib(name);
Romain Guy5cbbce52010-06-27 22:59:20 -070090}
91
92int Program::addUniform(const char* name) {
93 int slot = glGetUniformLocation(id, name);
94 uniforms.add(name, slot);
95 return slot;
96}
97
98int Program::getUniform(const char* name) {
Romain Guy889f8d12010-07-29 14:37:42 -070099 ssize_t index = uniforms.indexOfKey(name);
100 if (index >= 0) {
101 return uniforms.valueAt(index);
102 }
103 return addUniform(name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700104}
105
106GLuint Program::buildShader(const char* source, GLenum type) {
107 GLuint shader = glCreateShader(type);
108 glShaderSource(shader, 1, &source, 0);
109 glCompileShader(shader);
110
111 GLint status;
112 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
113 if (status != GL_TRUE) {
114 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
115 // use a fixed size instead
116 GLchar log[512];
117 glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]);
118 LOGE("Error while compiling shader: %s", log);
119 glDeleteShader(shader);
Romain Guy67f27952010-12-07 20:09:23 -0800120 return 0;
Romain Guy5cbbce52010-06-27 22:59:20 -0700121 }
122
123 return shader;
124}
125
Romain Guy889f8d12010-07-29 14:37:42 -0700126void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -0700127 const mat4& transformMatrix, bool offset) {
Romain Guy0b9db912010-07-09 18:53:25 -0700128 mat4 t(projectionMatrix);
Chet Haase8a5cc922011-04-26 07:28:09 -0700129 if (offset) {
130 // offset screenspace xy by an amount that compensates for typical precision issues
131 // in GPU hardware that tends to paint hor/vert lines in pixels shifted up and to the left.
132 // This offset value is based on an assumption that some hardware may use as little
133 // as 12.4 precision, so we offset by slightly more than 1/16.
134 t.translate(.375, .375, 0);
135 }
Romain Guy0b9db912010-07-09 18:53:25 -0700136 t.multiply(transformMatrix);
137 t.multiply(modelViewMatrix);
138
Romain Guy0b9db912010-07-09 18:53:25 -0700139 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
Romain Guy5cbbce52010-06-27 22:59:20 -0700140}
141
Romain Guy707b2f72010-10-11 16:34:59 -0700142void Program::setColor(const float r, const float g, const float b, const float a) {
143 glUniform4f(getUniform("color"), r, g, b, a);
144}
145
Romain Guy889f8d12010-07-29 14:37:42 -0700146void Program::use() {
147 glUseProgram(id);
148 mUse = true;
149
Romain Guy6926c722010-07-12 20:20:03 -0700150 glEnableVertexAttribArray(position);
151}
152
Romain Guy889f8d12010-07-29 14:37:42 -0700153void Program::remove() {
154 mUse = false;
155
Romain Guy6926c722010-07-12 20:20:03 -0700156 glDisableVertexAttribArray(position);
Romain Guyf9764a42010-07-16 23:13:33 -0700157}
158
Romain Guy5cbbce52010-06-27 22:59:20 -0700159}; // namespace uirenderer
160}; // namespace android