blob: 61d55a9ffe3fc39083b9d538d2878d4cbb69b13f [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#ifndef ANDROID_UI_PROGRAM_H
18#define ANDROID_UI_PROGRAM_H
19
20#include <GLES2/gl2.h>
21#include <GLES2/gl2ext.h>
22
23#include <utils/KeyedVector.h>
24#include <utils/RefBase.h>
25
Romain Guy0b9db912010-07-09 18:53:25 -070026#include "Matrix.h"
27
Romain Guy5cbbce52010-06-27 22:59:20 -070028namespace android {
29namespace uirenderer {
30
31/**
32 * A program holds a vertex and a fragment shader. It offers several utility
33 * methods to query attributes and uniforms.
34 */
35class Program: public LightRefBase<Program> {
36public:
37 /**
38 * Creates a new program with the specified vertex and fragment
39 * shaders sources.
40 */
41 Program(const char* vertex, const char* fragment);
42 ~Program();
43
44 /**
45 * Binds this program to the GL context.
46 */
47 void use();
48
49protected:
50 /**
51 * Adds an attribute with the specified name.
52 *
53 * @return The OpenGL name of the attribute.
54 */
55 int addAttrib(const char* name);
56 /**
57 * Returns the OpenGL name of the specified attribute.
58 */
59 int getAttrib(const char* name);
60
61 /**
62 * Adds a uniform with the specified name.
63 *
64 * @return The OpenGL name of the uniform.
65 */
66 int addUniform(const char* name);
67 /**
68 * Returns the OpenGL name of the specified uniform.
69 */
70 int getUniform(const char* name);
71
72private:
73 /**
74 * Compiles the specified shader of the specified type.
75 *
76 * @return The name of the compiled shader.
77 */
78 GLuint buildShader(const char* source, GLenum type);
79
80 // Name of the OpenGL program
81 GLuint id;
82
83 // Name of the shaders
84 GLuint vertexShader;
85 GLuint fragmentShader;
86
87 // Keeps track of attributes and uniforms slots
88 KeyedVector<const char*, int> attributes;
89 KeyedVector<const char*, int> uniforms;
90}; // class Program
91
92/**
93 * Program used to draw vertices with a simple color. The shaders must
94 * specify the following attributes:
95 * vec4 position, position of the vertex
96 * vec4 color, RGBA color of the vertex
97 *
98 * And the following uniforms:
99 * mat4 projection, the projection matrix
100 * mat4 modelView, the modelView matrix
101 * mat4 transform, an extra transformation matrix
102 */
103class DrawColorProgram: public Program {
104public:
105 DrawColorProgram();
106 DrawColorProgram(const char* vertex, const char* fragment);
107
108 /**
109 * Binds the program with the specified projection, modelView and
110 * transform matrices.
111 */
Romain Guy0b9db912010-07-09 18:53:25 -0700112 void use(const float* projectionMatrix, const mat4& modelViewMatrix,
113 const mat4& transformMatrix);
Romain Guy5cbbce52010-06-27 22:59:20 -0700114
115 /**
116 * Name of the position attribute.
117 */
118 int position;
Romain Guy5cbbce52010-06-27 22:59:20 -0700119
120 /**
Romain Guy0b9db912010-07-09 18:53:25 -0700121 * Name of the color uniform.
Romain Guy5cbbce52010-06-27 22:59:20 -0700122 */
Romain Guy0b9db912010-07-09 18:53:25 -0700123 int color;
Romain Guy5cbbce52010-06-27 22:59:20 -0700124 /**
125 * Name of the transform uniform.
126 */
127 int transform;
128
129protected:
130 void getAttribsAndUniforms();
131};
132
133/**
134 * Program used to draw textured vertices. In addition to everything that the
135 * DrawColorProgram supports, the following two attributes must be specified:
136 * sampler2D sampler, the texture sampler
137 * vec2 texCoords, the texture coordinates of the vertex
138 */
139class DrawTextureProgram: public DrawColorProgram {
140public:
141 DrawTextureProgram();
142
Romain Guy0b9db912010-07-09 18:53:25 -0700143 /**
144 * Name of the texture sampler uniform.
145 */
Romain Guy5cbbce52010-06-27 22:59:20 -0700146 int sampler;
Romain Guy0b9db912010-07-09 18:53:25 -0700147
148 /**
149 * Name of the texture coordinates attribute.
150 */
Romain Guy5cbbce52010-06-27 22:59:20 -0700151 int texCoords;
152};
153
154}; // namespace uirenderer
155}; // namespace android
156
157#endif // ANDROID_UI_PROGRAM_H