1 #ifndef __DALI_INTERNAL_PROGRAM_H__
2 #define __DALI_INTERNAL_PROGRAM_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 // Licensed under the Flora License, Version 1.0 (the License);
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://floralicense.org/license/
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an AS IS BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
24 #include <dali/public-api/common/vector-wrapper.h>
25 #include <dali/public-api/object/ref-object.h>
26 #include <dali/internal/render/gl-resources/context.h>
27 #include <dali/internal/render/gl-resources/context-observer.h>
28 #include <dali/integration-api/resource-cache.h>
47 * A program contains a vertex & fragment shader.
49 * A program will contain vertex attributes and uniform variables
50 * E.g. inside the code for our text fragment shaders we have the line:
52 * uniform lowp vec4 uColor
55 * This describes a variable used to color text as it is drawn.
57 * uColor is set to the value specified by Actor::SetColor and is
58 * animatable through the property Actor::COLOR
60 class Program : public ContextObserver
65 * Size of the uniform cache per program
66 * GLES specification states that minimum uniform count for fragment shader
67 * is 16 and for vertex shader 128. We're caching the 16 common ones for now
69 static const int MAX_UNIFORM_CACHE_SIZE = 16;
72 * Constant for uniform / attribute not found
74 static const int NOT_FOUND = -1;
92 * Common shader uniform names
96 UNIFORM_NOT_QUERIED = -2,
99 UNIFORM_MODELVIEW_MATRIX,
100 UNIFORM_PROJECTION_MATRIX,
101 UNIFORM_MODEL_MATRIX,
103 UNIFORM_NORMAL_MATRIX,
105 UNIFORM_CUSTOM_TEXTURE_COORDS,
107 UNIFORM_SAMPLER_RECT,
108 UNIFORM_EFFECT_SAMPLER,
109 UNIFORM_EFFECT_SAMPLER_RECT,
111 UNIFORM_SAMPLER_OPACITY,
112 UNIFORM_SAMPLER_NORMAL_MAP,
117 UNIFORM_OUTLINE_COLOR,
121 UNIFORM_SHADOW_COLOR,
122 UNIFORM_SHADOW_SMOOTHING,
123 UNIFORM_GRADIENT_COLOR,
124 UNIFORM_GRADIENT_LINE,
125 UNIFORM_INVERSE_TEXT_SIZE,
131 * Creates a new program, or returns a copy of an existing program in the program cache
132 * maintained by Context
133 * @param [in] resourceId ResourceManager resourceId for the shader source and binary.
134 * Used as a lookup key in the program cache
135 * @param[in] shaderData A pointer to a data structure containing the program source
136 * and optionally precompiled binary. If the binary is empty the program bytecode
137 * is copied into it after compilation and linking)
138 * @param [in] context GL context
139 * @return pointer to the program
141 static Program* New( const Integration::ResourceId& resourceId, Integration::ShaderData* shaderData, Context& context );
144 * Takes this program into use
149 * @return true if this program is used currently
154 * @param [in] type of the attribute
155 * @return the index of the attribute
157 GLint GetAttribLocation( AttribType type );
160 * Register a uniform name in our local cache
161 * @param [in] name uniform name
162 * @return the index of the uniform name in local cache
164 unsigned int RegisterUniform( const std::string& name );
167 * Gets the location of a pre-registered uniform.
168 * Uniforms in list UniformType are always registered and in the order of the enumeration
169 * @param [in] uniformIndex of the uniform in local cache
170 * @return the index of the uniform in the GL program
172 GLint GetUniformLocation( unsigned int uniformIndex );
175 * Sets the uniform value
176 * @param [in] location of uniform
177 * @param [in] value0 as int
179 void SetUniform1i( GLint location, GLint value0 );
182 * Sets the uniform value
183 * @param [in] location of uniform
184 * @param [in] value0 as int
185 * @param [in] value1 as int
186 * @param [in] value2 as int
187 * @param [in] value3 as int
189 void SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 );
192 * Sets the uniform value
193 * @param [in] location of uniform
194 * @param [in] value0 as float
196 void SetUniform1f( GLint location, GLfloat value0 );
199 * Sets the uniform value
200 * @param [in] location of uniform
201 * @param [in] value0 as float
202 * @param [in] value1 as float
204 void SetUniform2f( GLint location, GLfloat value0, GLfloat value1 );
207 * Sets the uniform value
208 * @param [in] location of uniform
209 * @param [in] value0 as float
210 * @param [in] value1 as float
211 * @param [in] value2 as float
213 void SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 );
216 * Sets the uniform value
217 * @param [in] location of uniform
218 * @param [in] value0 as float
219 * @param [in] value1 as float
220 * @param [in] value2 as float
221 * @param [in] value3 as float
223 void SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 );
226 * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
227 * so make sure your matrix is in correct order for GL.
228 * @param [in] location Location of uniform
229 * @param [in] count Count of matrices
230 * @param [in] value values as float pointers
232 void SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value );
235 * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
236 * so make sure your matrix is in correct order for GL.
237 * @param [in] location Location of uniform
238 * @param [in] count Count of matrices
239 * @param [in] value values as float pointers
241 void SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value );
243 public: // From ContextObserver
246 * @copydoc ContextObserver::GlContextCreated
248 virtual void GlContextCreated(); // From ContextObserver
251 * @copydoc ContextObserver::GlContextToBeDestroyed
253 virtual void GlContextToBeDestroyed(); // From ContextObserver
255 private: // Implementation
258 * Constructor, private so no direct instantiation
259 * @param[in] shaderData A pointer to a data structure containing the program source and binary
260 * @param[in] context The GL context state cache.
262 Program( Integration::ShaderData* shaderData, Context& context );
273 // default constructor, not defined
275 // assignment operator, not defined
276 Program& operator=( const Program& );
279 * Load the shader, from a precompiled binary if available, else from source code
290 * @param shaderType vertex or fragment shader
291 * @param shaderId of the shader, returned
292 * @param src of the shader
293 * @return true if the compilation succeeded
295 bool CompileShader(GLenum shaderType, GLuint& shaderId, const char* src);
298 * Links the shaders together to create program
303 * Frees the shader programs
310 void ResetAttribsUniforms();
314 Context& mContext; ///< The GL context state cache
315 Integration::GlAbstraction& mGlAbstraction; ///< The OpenGL Abstraction layer
316 bool mLinked; ///< whether the program is linked
317 GLuint mVertexShaderId; ///< GL identifier for vertex shader
318 GLuint mFragmentShaderId; ///< GL identifier for fragment shader
319 GLuint mProgramId; ///< GL identifier for program
320 Integration::ShaderData* mProgramData; ///< Shader program source and binary (when compiled & linked or loaded)
323 GLint mAttribLocations[ ATTRIB_TYPE_LAST ]; ///< attribute location cache
324 std::vector< std::pair< std::string, GLint > > mUniformLocations; ///< uniform location cache
326 // uniform value caching
327 GLint mUniformCacheInt[ MAX_UNIFORM_CACHE_SIZE ]; ///< Value cache for uniforms of single int
328 GLfloat mUniformCacheFloat[ MAX_UNIFORM_CACHE_SIZE ]; ///< Value cache for uniforms of single float
329 GLfloat mUniformCacheFloat4[ MAX_UNIFORM_CACHE_SIZE ][4]; ///< Value cache for uniforms of four float
333 } // namespace Internal
337 #endif // __DALI_INTERNAL_PROGRAM_H__