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