blob: baed5fd7ea8edb5571bb0a89975c2cd5a84033a1 [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) {
29 vertexShader = buildShader(vertex, GL_VERTEX_SHADER);
30 fragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
31
32 id = glCreateProgram();
33 glAttachShader(id, vertexShader);
34 glAttachShader(id, fragmentShader);
35 glLinkProgram(id);
36
37 GLint status;
38 glGetProgramiv(id, GL_LINK_STATUS, &status);
39 if (status != GL_TRUE) {
40 GLint infoLen = 0;
41 glGetProgramiv(id, GL_INFO_LOG_LENGTH, &infoLen);
42 if (infoLen > 1) {
Romain Guy31529ff2010-09-17 10:26:31 -070043 GLchar log[infoLen];
44 glGetProgramInfoLog(id, infoLen, 0, &log[0]);
Romain Guy5cbbce52010-06-27 22:59:20 -070045 LOGE("Error while linking shaders: %s", log);
Romain Guy5cbbce52010-06-27 22:59:20 -070046 }
Romain Guy98173a22010-08-11 16:40:25 -070047 glDeleteShader(vertexShader);
48 glDeleteShader(fragmentShader);
Romain Guy5cbbce52010-06-27 22:59:20 -070049 glDeleteProgram(id);
50 }
Romain Guy260e1022010-07-12 14:41:06 -070051
52 mUse = false;
Romain Guy889f8d12010-07-29 14:37:42 -070053
54 position = addAttrib("position");
Romain Guy889f8d12010-07-29 14:37:42 -070055 transform = addUniform("transform");
Romain Guy5cbbce52010-06-27 22:59:20 -070056}
57
58Program::~Program() {
59 glDeleteShader(vertexShader);
60 glDeleteShader(fragmentShader);
61 glDeleteProgram(id);
62}
63
Romain Guy5cbbce52010-06-27 22:59:20 -070064int Program::addAttrib(const char* name) {
65 int slot = glGetAttribLocation(id, name);
66 attributes.add(name, slot);
67 return slot;
68}
69
70int Program::getAttrib(const char* name) {
Romain Guy889f8d12010-07-29 14:37:42 -070071 ssize_t index = attributes.indexOfKey(name);
72 if (index >= 0) {
73 return attributes.valueAt(index);
74 }
75 return addAttrib(name);
Romain Guy5cbbce52010-06-27 22:59:20 -070076}
77
78int Program::addUniform(const char* name) {
79 int slot = glGetUniformLocation(id, name);
80 uniforms.add(name, slot);
81 return slot;
82}
83
84int Program::getUniform(const char* name) {
Romain Guy889f8d12010-07-29 14:37:42 -070085 ssize_t index = uniforms.indexOfKey(name);
86 if (index >= 0) {
87 return uniforms.valueAt(index);
88 }
89 return addUniform(name);
Romain Guy5cbbce52010-06-27 22:59:20 -070090}
91
92GLuint Program::buildShader(const char* source, GLenum type) {
93 GLuint shader = glCreateShader(type);
94 glShaderSource(shader, 1, &source, 0);
95 glCompileShader(shader);
96
97 GLint status;
98 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
99 if (status != GL_TRUE) {
100 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
101 // use a fixed size instead
102 GLchar log[512];
103 glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]);
104 LOGE("Error while compiling shader: %s", log);
105 glDeleteShader(shader);
106 }
107
108 return shader;
109}
110
Romain Guy889f8d12010-07-29 14:37:42 -0700111void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Romain Guy0b9db912010-07-09 18:53:25 -0700112 const mat4& transformMatrix) {
113 mat4 t(projectionMatrix);
114 t.multiply(transformMatrix);
115 t.multiply(modelViewMatrix);
116
Romain Guy0b9db912010-07-09 18:53:25 -0700117 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
Romain Guy5cbbce52010-06-27 22:59:20 -0700118}
119
Romain Guy707b2f72010-10-11 16:34:59 -0700120void Program::setColor(const float r, const float g, const float b, const float a) {
121 glUniform4f(getUniform("color"), r, g, b, a);
122}
123
Romain Guy889f8d12010-07-29 14:37:42 -0700124void Program::use() {
125 glUseProgram(id);
126 mUse = true;
127
Romain Guy6926c722010-07-12 20:20:03 -0700128 glEnableVertexAttribArray(position);
129}
130
Romain Guy889f8d12010-07-29 14:37:42 -0700131void Program::remove() {
132 mUse = false;
133
Romain Guy6926c722010-07-12 20:20:03 -0700134 glDisableVertexAttribArray(position);
Romain Guyf9764a42010-07-16 23:13:33 -0700135}
136
Romain Guy5cbbce52010-06-27 22:59:20 -0700137}; // namespace uirenderer
138}; // namespace android