Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 1 | /* |
| 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 Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 26 | #include "Matrix.h" |
| 27 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace 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 | */ |
| 35 | class Program: public LightRefBase<Program> { |
| 36 | public: |
| 37 | /** |
| 38 | * Creates a new program with the specified vertex and fragment |
| 39 | * shaders sources. |
| 40 | */ |
| 41 | Program(const char* vertex, const char* fragment); |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 42 | virtual ~Program(); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Binds this program to the GL context. |
| 46 | */ |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 47 | virtual void use(); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 48 | |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Marks this program as unused. This will not unbind |
| 51 | * the program from the GL context. |
| 52 | */ |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 53 | virtual void remove(); |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Indicates whether this program is currently in use with |
| 57 | * the GL context. |
| 58 | */ |
| 59 | inline bool isInUse() const { |
| 60 | return mUse; |
| 61 | } |
| 62 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 63 | protected: |
| 64 | /** |
| 65 | * Adds an attribute with the specified name. |
| 66 | * |
| 67 | * @return The OpenGL name of the attribute. |
| 68 | */ |
| 69 | int addAttrib(const char* name); |
| 70 | /** |
| 71 | * Returns the OpenGL name of the specified attribute. |
| 72 | */ |
| 73 | int getAttrib(const char* name); |
| 74 | |
| 75 | /** |
| 76 | * Adds a uniform with the specified name. |
| 77 | * |
| 78 | * @return The OpenGL name of the uniform. |
| 79 | */ |
| 80 | int addUniform(const char* name); |
| 81 | /** |
| 82 | * Returns the OpenGL name of the specified uniform. |
| 83 | */ |
| 84 | int getUniform(const char* name); |
| 85 | |
| 86 | private: |
| 87 | /** |
| 88 | * Compiles the specified shader of the specified type. |
| 89 | * |
| 90 | * @return The name of the compiled shader. |
| 91 | */ |
| 92 | GLuint buildShader(const char* source, GLenum type); |
| 93 | |
| 94 | // Name of the OpenGL program |
| 95 | GLuint id; |
| 96 | |
| 97 | // Name of the shaders |
| 98 | GLuint vertexShader; |
| 99 | GLuint fragmentShader; |
| 100 | |
| 101 | // Keeps track of attributes and uniforms slots |
| 102 | KeyedVector<const char*, int> attributes; |
| 103 | KeyedVector<const char*, int> uniforms; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 104 | |
| 105 | bool mUse; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 106 | }; // class Program |
| 107 | |
| 108 | /** |
| 109 | * Program used to draw vertices with a simple color. The shaders must |
| 110 | * specify the following attributes: |
| 111 | * vec4 position, position of the vertex |
| 112 | * vec4 color, RGBA color of the vertex |
| 113 | * |
| 114 | * And the following uniforms: |
| 115 | * mat4 projection, the projection matrix |
| 116 | * mat4 modelView, the modelView matrix |
| 117 | * mat4 transform, an extra transformation matrix |
| 118 | */ |
| 119 | class DrawColorProgram: public Program { |
| 120 | public: |
| 121 | DrawColorProgram(); |
| 122 | DrawColorProgram(const char* vertex, const char* fragment); |
| 123 | |
| 124 | /** |
| 125 | * Binds the program with the specified projection, modelView and |
| 126 | * transform matrices. |
| 127 | */ |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 128 | void set(const mat4& projectionMatrix, const mat4& modelViewMatrix, |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 129 | const mat4& transformMatrix); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 130 | |
| 131 | /** |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 132 | * Binds this program to the GL context. |
| 133 | */ |
| 134 | virtual void use(); |
| 135 | |
| 136 | /** |
| 137 | * Marks this program as unused. This will not unbind |
| 138 | * the program from the GL context. |
| 139 | */ |
| 140 | virtual void remove(); |
| 141 | |
| 142 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 143 | * Name of the position attribute. |
| 144 | */ |
| 145 | int position; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 146 | |
| 147 | /** |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 148 | * Name of the color uniform. |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 149 | */ |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 150 | int color; |
Romain Guy | f9764a4 | 2010-07-16 23:13:33 -0700 | [diff] [blame] | 151 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 152 | /** |
| 153 | * Name of the transform uniform. |
| 154 | */ |
| 155 | int transform; |
| 156 | |
| 157 | protected: |
| 158 | void getAttribsAndUniforms(); |
| 159 | }; |
| 160 | |
| 161 | /** |
| 162 | * Program used to draw textured vertices. In addition to everything that the |
| 163 | * DrawColorProgram supports, the following two attributes must be specified: |
| 164 | * sampler2D sampler, the texture sampler |
| 165 | * vec2 texCoords, the texture coordinates of the vertex |
| 166 | */ |
| 167 | class DrawTextureProgram: public DrawColorProgram { |
| 168 | public: |
| 169 | DrawTextureProgram(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 170 | DrawTextureProgram(const char* vertex, const char* fragment); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 171 | |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 172 | /** |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 173 | * Binds this program to the GL context. |
| 174 | */ |
| 175 | virtual void use(); |
| 176 | |
| 177 | /** |
| 178 | * Marks this program as unused. This will not unbind |
| 179 | * the program from the GL context. |
| 180 | */ |
| 181 | virtual void remove(); |
| 182 | |
| 183 | /** |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 184 | * Name of the texture sampler uniform. |
| 185 | */ |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 186 | int sampler; |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * Name of the texture coordinates attribute. |
| 190 | */ |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 191 | int texCoords; |
| 192 | }; |
| 193 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 194 | class DrawTextProgram: public DrawTextureProgram { |
| 195 | public: |
| 196 | DrawTextProgram(); |
| 197 | }; |
| 198 | |
Romain Guy | f9764a4 | 2010-07-16 23:13:33 -0700 | [diff] [blame] | 199 | /** |
| 200 | * Program used to draw linear gradients. In addition to everything that the |
| 201 | * DrawColorProgram supports, the following two attributes must be specified: |
| 202 | * vec2 gradient, the vector describing the linear gradient |
| 203 | * float gradientLength, the invert of the magnitude of the gradient vector |
| 204 | * sampler2D sampler, the texture sampler |
| 205 | */ |
| 206 | class DrawLinearGradientProgram: public DrawColorProgram { |
| 207 | public: |
| 208 | DrawLinearGradientProgram(); |
| 209 | |
| 210 | /** |
| 211 | * Binds this program to the GL context. |
| 212 | */ |
| 213 | virtual void use(); |
| 214 | |
| 215 | /** |
| 216 | * Marks this program as unused. This will not unbind |
| 217 | * the program from the GL context. |
| 218 | */ |
| 219 | virtual void remove(); |
| 220 | |
| 221 | /** |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 222 | * Name of the matrix used to compute the screen space coordinates |
| 223 | * of the vertices. |
| 224 | */ |
| 225 | int screenSpace; |
| 226 | |
| 227 | /** |
| 228 | * Name of the linear gradient start point. |
| 229 | */ |
| 230 | int start; |
| 231 | |
| 232 | /** |
Romain Guy | f9764a4 | 2010-07-16 23:13:33 -0700 | [diff] [blame] | 233 | * Name of the linear gradient vector. |
| 234 | */ |
| 235 | int gradient; |
| 236 | |
| 237 | /** |
| 238 | * Name of the inverse of linear gradient vector's magnitude. |
| 239 | */ |
| 240 | int gradientLength; |
| 241 | |
| 242 | /** |
| 243 | * Name of the texture sampler uniform. |
| 244 | */ |
| 245 | int sampler; |
| 246 | }; |
| 247 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 248 | }; // namespace uirenderer |
| 249 | }; // namespace android |
| 250 | |
| 251 | #endif // ANDROID_UI_PROGRAM_H |