blob: 9e59621eef30836480e97264b2394013ff069d45 [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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_PROGRAM_H
18#define ANDROID_HWUI_PROGRAM_H
Romain Guy5cbbce52010-06-27 22:59:20 -070019
20#include <GLES2/gl2.h>
21#include <GLES2/gl2ext.h>
22
23#include <utils/KeyedVector.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070024
Romain Guy0b9db912010-07-09 18:53:25 -070025#include "Matrix.h"
26
Romain Guy5cbbce52010-06-27 22:59:20 -070027namespace android {
28namespace uirenderer {
29
30/**
31 * A program holds a vertex and a fragment shader. It offers several utility
32 * methods to query attributes and uniforms.
33 */
Romain Guy889f8d12010-07-29 14:37:42 -070034class Program {
Romain Guy5cbbce52010-06-27 22:59:20 -070035public:
Romain Guy3e263fa2011-12-12 16:47:48 -080036 enum ShaderBindings {
37 kBindingPosition,
38 kBindingTexCoords
39 };
40
Romain Guy5cbbce52010-06-27 22:59:20 -070041 /**
42 * Creates a new program with the specified vertex and fragment
43 * shaders sources.
44 */
45 Program(const char* vertex, const char* fragment);
Romain Guy6926c722010-07-12 20:20:03 -070046 virtual ~Program();
Romain Guy5cbbce52010-06-27 22:59:20 -070047
48 /**
49 * Binds this program to the GL context.
50 */
Romain Guy6926c722010-07-12 20:20:03 -070051 virtual void use();
Romain Guy5cbbce52010-06-27 22:59:20 -070052
Romain Guy260e1022010-07-12 14:41:06 -070053 /**
54 * Marks this program as unused. This will not unbind
55 * the program from the GL context.
56 */
Romain Guy6926c722010-07-12 20:20:03 -070057 virtual void remove();
Romain Guy260e1022010-07-12 14:41:06 -070058
59 /**
Romain Guyac670c02010-07-27 17:39:27 -070060 * Returns the OpenGL name of the specified attribute.
61 */
62 int getAttrib(const char* name);
63
64 /**
65 * Returns the OpenGL name of the specified uniform.
66 */
67 int getUniform(const char* name);
68
69 /**
Romain Guy260e1022010-07-12 14:41:06 -070070 * Indicates whether this program is currently in use with
71 * the GL context.
72 */
73 inline bool isInUse() const {
74 return mUse;
75 }
76
Romain Guy889f8d12010-07-29 14:37:42 -070077 /**
Romain Guy67f27952010-12-07 20:09:23 -080078 * Indicates whether this program was correctly compiled and linked.
79 */
80 inline bool isInitialized() const {
81 return mInitialized;
82 }
83
84 /**
Romain Guy889f8d12010-07-29 14:37:42 -070085 * Binds the program with the specified projection, modelView and
86 * transform matrices.
87 */
88 void set(const mat4& projectionMatrix, const mat4& modelViewMatrix,
Chet Haase8a5cc922011-04-26 07:28:09 -070089 const mat4& transformMatrix, bool offset = false);
Romain Guy889f8d12010-07-29 14:37:42 -070090
91 /**
Romain Guy707b2f72010-10-11 16:34:59 -070092 * Sets the color associated with this shader.
93 */
94 void setColor(const float r, const float g, const float b, const float a);
95
96 /**
Romain Guy889f8d12010-07-29 14:37:42 -070097 * Name of the position attribute.
98 */
99 int position;
100
101 /**
Romain Guy889f8d12010-07-29 14:37:42 -0700102 * Name of the transform uniform.
103 */
104 int transform;
105
Romain Guy5cbbce52010-06-27 22:59:20 -0700106protected:
107 /**
108 * Adds an attribute with the specified name.
109 *
110 * @return The OpenGL name of the attribute.
111 */
112 int addAttrib(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700113
114 /**
Romain Guy3e263fa2011-12-12 16:47:48 -0800115 * Binds the specified attribute name to the specified slot.
116 */
117 int bindAttrib(const char* name, ShaderBindings bindingSlot);
118
119 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700120 * Adds a uniform with the specified name.
121 *
122 * @return The OpenGL name of the uniform.
123 */
124 int addUniform(const char* name);
Romain Guy5cbbce52010-06-27 22:59:20 -0700125
126private:
127 /**
128 * Compiles the specified shader of the specified type.
129 *
130 * @return The name of the compiled shader.
131 */
132 GLuint buildShader(const char* source, GLenum type);
133
Romain Guy3e263fa2011-12-12 16:47:48 -0800134 // Name of the OpenGL program and shaders
Romain Guy05bbde72011-12-09 12:55:37 -0800135 GLuint mProgramId;
Romain Guy3e263fa2011-12-12 16:47:48 -0800136 GLuint mVertexShader;
137 GLuint mFragmentShader;
Romain Guy5cbbce52010-06-27 22:59:20 -0700138
139 // Keeps track of attributes and uniforms slots
Romain Guy05bbde72011-12-09 12:55:37 -0800140 KeyedVector<const char*, int> mAttributes;
141 KeyedVector<const char*, int> mUniforms;
Romain Guy260e1022010-07-12 14:41:06 -0700142
143 bool mUse;
Romain Guy67f27952010-12-07 20:09:23 -0800144 bool mInitialized;
Romain Guy05bbde72011-12-09 12:55:37 -0800145
146 bool mHasColorUniform;
147 int mColorUniform;
Romain Guy5cbbce52010-06-27 22:59:20 -0700148}; // class Program
149
Romain Guy5cbbce52010-06-27 22:59:20 -0700150}; // namespace uirenderer
151}; // namespace android
152
Romain Guy5b3b3522010-10-27 18:57:51 -0700153#endif // ANDROID_HWUI_PROGRAM_H