blob: 2187f24e2c83d39a05c588d0349e80c335b13892 [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,
Romain Guy0b9db912010-07-09 18:53:25 -0700127 const mat4& transformMatrix) {
128 mat4 t(projectionMatrix);
129 t.multiply(transformMatrix);
130 t.multiply(modelViewMatrix);
131
Romain Guy0b9db912010-07-09 18:53:25 -0700132 glUniformMatrix4fv(transform, 1, GL_FALSE, &t.data[0]);
Romain Guy5cbbce52010-06-27 22:59:20 -0700133}
134
Romain Guy707b2f72010-10-11 16:34:59 -0700135void Program::setColor(const float r, const float g, const float b, const float a) {
136 glUniform4f(getUniform("color"), r, g, b, a);
137}
138
Romain Guy889f8d12010-07-29 14:37:42 -0700139void Program::use() {
140 glUseProgram(id);
141 mUse = true;
142
Romain Guy6926c722010-07-12 20:20:03 -0700143 glEnableVertexAttribArray(position);
144}
145
Romain Guy889f8d12010-07-29 14:37:42 -0700146void Program::remove() {
147 mUse = false;
148
Romain Guy6926c722010-07-12 20:20:03 -0700149 glDisableVertexAttribArray(position);
Romain Guyf9764a42010-07-16 23:13:33 -0700150}
151
Romain Guy5cbbce52010-06-27 22:59:20 -0700152}; // namespace uirenderer
153}; // namespace android