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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_PROGRAM_H |
| 18 | #define ANDROID_HWUI_PROGRAM_H |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 19 | |
| 20 | #include <GLES2/gl2.h> |
| 21 | #include <GLES2/gl2ext.h> |
| 22 | |
| 23 | #include <utils/KeyedVector.h> |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 24 | |
Romain Guy | 0b9db91 | 2010-07-09 18:53:25 -0700 | [diff] [blame] | 25 | #include "Matrix.h" |
| 26 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | namespace 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 Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 34 | class Program { |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 35 | public: |
| 36 | /** |
| 37 | * Creates a new program with the specified vertex and fragment |
| 38 | * shaders sources. |
| 39 | */ |
| 40 | Program(const char* vertex, const char* fragment); |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 41 | virtual ~Program(); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Binds this program to the GL context. |
| 45 | */ |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 46 | virtual void use(); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 47 | |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Marks this program as unused. This will not unbind |
| 50 | * the program from the GL context. |
| 51 | */ |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 52 | virtual void remove(); |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 55 | * Returns the OpenGL name of the specified attribute. |
| 56 | */ |
| 57 | int getAttrib(const char* name); |
| 58 | |
| 59 | /** |
| 60 | * Returns the OpenGL name of the specified uniform. |
| 61 | */ |
| 62 | int getUniform(const char* name); |
| 63 | |
| 64 | /** |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 65 | * Indicates whether this program is currently in use with |
| 66 | * the GL context. |
| 67 | */ |
| 68 | inline bool isInUse() const { |
| 69 | return mUse; |
| 70 | } |
| 71 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 72 | /** |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 73 | * Indicates whether this program was correctly compiled and linked. |
| 74 | */ |
| 75 | inline bool isInitialized() const { |
| 76 | return mInitialized; |
| 77 | } |
| 78 | |
| 79 | /** |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 80 | * Binds the program with the specified projection, modelView and |
| 81 | * transform matrices. |
| 82 | */ |
| 83 | void set(const mat4& projectionMatrix, const mat4& modelViewMatrix, |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 84 | const mat4& transformMatrix, bool offset = false); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 87 | * Sets the color associated with this shader. |
| 88 | */ |
| 89 | void setColor(const float r, const float g, const float b, const float a); |
| 90 | |
| 91 | /** |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 92 | * Name of the position attribute. |
| 93 | */ |
| 94 | int position; |
| 95 | |
| 96 | /** |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 97 | * Name of the transform uniform. |
| 98 | */ |
| 99 | int transform; |
| 100 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 101 | protected: |
| 102 | /** |
| 103 | * Adds an attribute with the specified name. |
| 104 | * |
| 105 | * @return The OpenGL name of the attribute. |
| 106 | */ |
| 107 | int addAttrib(const char* name); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Adds a uniform with the specified name. |
| 111 | * |
| 112 | * @return The OpenGL name of the uniform. |
| 113 | */ |
| 114 | int addUniform(const char* name); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 115 | |
| 116 | private: |
| 117 | /** |
| 118 | * Compiles the specified shader of the specified type. |
| 119 | * |
| 120 | * @return The name of the compiled shader. |
| 121 | */ |
| 122 | GLuint buildShader(const char* source, GLenum type); |
| 123 | |
| 124 | // Name of the OpenGL program |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 125 | GLuint mProgramId; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 126 | |
| 127 | // Keeps track of attributes and uniforms slots |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 128 | KeyedVector<const char*, int> mAttributes; |
| 129 | KeyedVector<const char*, int> mUniforms; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 130 | |
| 131 | bool mUse; |
Romain Guy | 67f2795 | 2010-12-07 20:09:23 -0800 | [diff] [blame] | 132 | bool mInitialized; |
Romain Guy | 05bbde7 | 2011-12-09 12:55:37 -0800 | [diff] [blame] | 133 | |
| 134 | bool mHasColorUniform; |
| 135 | int mColorUniform; |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 136 | }; // class Program |
| 137 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 138 | }; // namespace uirenderer |
| 139 | }; // namespace android |
| 140 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 141 | #endif // ANDROID_HWUI_PROGRAM_H |