blob: 6528d91501105d1b1ec23dbd6786291462e915b0 [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///////////////////////////////////////////////////////////////////////////////
25// Shaders
26///////////////////////////////////////////////////////////////////////////////
27
28#define SHADER_SOURCE(name, source) const char* name = #source
29
Romain Guy5cbbce52010-06-27 22:59:20 -070030///////////////////////////////////////////////////////////////////////////////
31// Base program
32///////////////////////////////////////////////////////////////////////////////
33
34Program::Program(const char* vertex, const char* fragment) {
35 vertexShader = buildShader(vertex, GL_VERTEX_SHADER);
36 fragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER);
37
38 id = glCreateProgram();
39 glAttachShader(id, vertexShader);
40 glAttachShader(id, fragmentShader);
41 glLinkProgram(id);
42
43 GLint status;
44 glGetProgramiv(id, GL_LINK_STATUS, &status);
45 if (status != GL_TRUE) {
46 GLint infoLen = 0;
47 glGetProgramiv(id, GL_INFO_LOG_LENGTH, &infoLen);
48 if (infoLen > 1) {
49 char* log = (char*) malloc(sizeof(char) * infoLen);
50 glGetProgramInfoLog(id, infoLen, 0, log);
51 LOGE("Error while linking shaders: %s", log);
52 delete log;
53 }
Romain Guy98173a22010-08-11 16:40:25 -070054 glDeleteShader(vertexShader);
55 glDeleteShader(fragmentShader);
Romain Guy5cbbce52010-06-27 22:59:20 -070056 glDeleteProgram(id);
57 }
Romain Guy260e1022010-07-12 14:41:06 -070058
59 mUse = false;
Romain Guy889f8d12010-07-29 14:37:42 -070060
61 position = addAttrib("position");
62 color = addUniform("color");
63 transform = addUniform("transform");
Romain Guy5cbbce52010-06-27 22:59:20 -070064}
65
66Program::~Program() {
67 glDeleteShader(vertexShader);
68 glDeleteShader(fragmentShader);
69 glDeleteProgram(id);
70}
71
Romain Guy5cbbce52010-06-27 22:59:20 -070072int Program::addAttrib(const char* name) {
73 int slot = glGetAttribLocation(id, name);
74 attributes.add(name, slot);
75 return slot;
76}
77
78int Program::getAttrib(const char* name) {
Romain Guy889f8d12010-07-29 14:37:42 -070079 ssize_t index = attributes.indexOfKey(name);
80 if (index >= 0) {
81 return attributes.valueAt(index);
82 }
83 return addAttrib(name);
Romain Guy5cbbce52010-06-27 22:59:20 -070084}
85
86int Program::addUniform(const char* name) {
87 int slot = glGetUniformLocation(id, name);
88 uniforms.add(name, slot);
89 return slot;
90}
91
92int Program::getUniform(const char* name) {
Romain Guy889f8d12010-07-29 14:37:42 -070093 ssize_t index = uniforms.indexOfKey(name);
94 if (index >= 0) {
95 return uniforms.valueAt(index);
96 }
97 return addUniform(name);
Romain Guy5cbbce52010-06-27 22:59:20 -070098}
99
100GLuint Program::buildShader(const char* source, GLenum type) {
101 GLuint shader = glCreateShader(type);
102 glShaderSource(shader, 1, &source, 0);
103 glCompileShader(shader);
104
105 GLint status;
106 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
107 if (status != GL_TRUE) {
108 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
109 // use a fixed size instead
110 GLchar log[512];
111 glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]);
112 LOGE("Error while compiling shader: %s", log);
113 glDeleteShader(shader);
114 }
115
116 return shader;
117}
118
Romain Guy889f8d12010-07-29 14:37:42 -0700119void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Romain Guy0b9db912010-07-09 18:53:25 -0700120 const mat4& transformMatrix) {
121 mat4 t(projectionMatrix);
122 t.multiply(transformMatrix);
123 t.multiply(modelViewMatrix);
124
Romain Guy0b9db912010-07-09 18:53:25 -0700125 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
Romain Guy5cbbce52010-06-27 22:59:20 -0700126}
127
Romain Guy889f8d12010-07-29 14:37:42 -0700128void Program::use() {
129 glUseProgram(id);
130 mUse = true;
131
Romain Guy6926c722010-07-12 20:20:03 -0700132 glEnableVertexAttribArray(position);
133}
134
Romain Guy889f8d12010-07-29 14:37:42 -0700135void Program::remove() {
136 mUse = false;
137
Romain Guy6926c722010-07-12 20:20:03 -0700138 glDisableVertexAttribArray(position);
Romain Guyf9764a42010-07-16 23:13:33 -0700139}
140
Romain Guy5cbbce52010-06-27 22:59:20 -0700141}; // namespace uirenderer
142}; // namespace android