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