blob: dbae38ecb185544243f3d1963a6b6d4d581fffff [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 }
54 glDeleteProgram(id);
55 }
Romain Guy260e1022010-07-12 14:41:06 -070056
57 mUse = false;
Romain Guy889f8d12010-07-29 14:37:42 -070058
59 position = addAttrib("position");
60 color = addUniform("color");
61 transform = addUniform("transform");
Romain Guy5cbbce52010-06-27 22:59:20 -070062}
63
64Program::~Program() {
65 glDeleteShader(vertexShader);
66 glDeleteShader(fragmentShader);
67 glDeleteProgram(id);
68}
69
Romain Guy5cbbce52010-06-27 22:59:20 -070070int Program::addAttrib(const char* name) {
71 int slot = glGetAttribLocation(id, name);
72 attributes.add(name, slot);
73 return slot;
74}
75
76int Program::getAttrib(const char* name) {
Romain Guy889f8d12010-07-29 14:37:42 -070077 ssize_t index = attributes.indexOfKey(name);
78 if (index >= 0) {
79 return attributes.valueAt(index);
80 }
81 return addAttrib(name);
Romain Guy5cbbce52010-06-27 22:59:20 -070082}
83
84int Program::addUniform(const char* name) {
85 int slot = glGetUniformLocation(id, name);
86 uniforms.add(name, slot);
87 return slot;
88}
89
90int Program::getUniform(const char* name) {
Romain Guy889f8d12010-07-29 14:37:42 -070091 ssize_t index = uniforms.indexOfKey(name);
92 if (index >= 0) {
93 return uniforms.valueAt(index);
94 }
95 return addUniform(name);
Romain Guy5cbbce52010-06-27 22:59:20 -070096}
97
98GLuint Program::buildShader(const char* source, GLenum type) {
99 GLuint shader = glCreateShader(type);
100 glShaderSource(shader, 1, &source, 0);
101 glCompileShader(shader);
102
103 GLint status;
104 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
105 if (status != GL_TRUE) {
106 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
107 // use a fixed size instead
108 GLchar log[512];
109 glGetShaderInfoLog(shader, sizeof(log), 0, &log[0]);
110 LOGE("Error while compiling shader: %s", log);
111 glDeleteShader(shader);
112 }
113
114 return shader;
115}
116
Romain Guy889f8d12010-07-29 14:37:42 -0700117void Program::set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Romain Guy0b9db912010-07-09 18:53:25 -0700118 const mat4& transformMatrix) {
119 mat4 t(projectionMatrix);
120 t.multiply(transformMatrix);
121 t.multiply(modelViewMatrix);
122
Romain Guy0b9db912010-07-09 18:53:25 -0700123 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
Romain Guy5cbbce52010-06-27 22:59:20 -0700124}
125
Romain Guy889f8d12010-07-29 14:37:42 -0700126void Program::use() {
127 glUseProgram(id);
128 mUse = true;
129
Romain Guy6926c722010-07-12 20:20:03 -0700130 glEnableVertexAttribArray(position);
131}
132
Romain Guy889f8d12010-07-29 14:37:42 -0700133void Program::remove() {
134 mUse = false;
135
Romain Guy6926c722010-07-12 20:20:03 -0700136 glDisableVertexAttribArray(position);
Romain Guyf9764a42010-07-16 23:13:33 -0700137}
138
Romain Guy5cbbce52010-06-27 22:59:20 -0700139}; // namespace uirenderer
140}; // namespace android