1 #ifndef __DALI_INTERNAL_PROGRAM_H__
2 #define __DALI_INTERNAL_PROGRAM_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <dali/public-api/common/vector-wrapper.h>
26 #include <dali/public-api/object/ref-object.h>
27 #include <dali/integration-api/gl-abstraction.h>
28 #include <dali/integration-api/shader-data.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
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 * @param[in] cache where the programs are stored
133 * @param[in] shaderData A pointer to a data structure containing the program source
134 * and optionally precompiled binary. If the binary is empty the program bytecode
135 * is copied into it after compilation and linking)
136 * @param[in] modifiesGeometry True if the shader modifies geometry
137 * @return pointer to the program
139 static Program* New( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry );
142 * Takes this program into use
147 * @return true if this program is used currently
152 * @param [in] type of the attribute
153 * @return the index of the attribute
155 GLint GetAttribLocation( AttribType type );
158 * Register a uniform name in our local cache
159 * @param [in] name uniform name
160 * @return the index of the uniform name in local cache
162 unsigned int RegisterUniform( const std::string& name );
165 * Gets the location of a pre-registered uniform.
166 * Uniforms in list UniformType are always registered and in the order of the enumeration
167 * @param [in] uniformIndex of the uniform in local cache
168 * @return the index of the uniform in the GL program
170 GLint GetUniformLocation( unsigned int uniformIndex );
173 * Sets the uniform value
174 * @param [in] location of uniform
175 * @param [in] value0 as int
177 void SetUniform1i( GLint location, GLint value0 );
180 * Sets the uniform value
181 * @param [in] location of uniform
182 * @param [in] value0 as int
183 * @param [in] value1 as int
184 * @param [in] value2 as int
185 * @param [in] value3 as int
187 void SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 );
190 * Sets the uniform value
191 * @param [in] location of uniform
192 * @param [in] value0 as float
194 void SetUniform1f( GLint location, GLfloat value0 );
197 * Sets the uniform value
198 * @param [in] location of uniform
199 * @param [in] value0 as float
200 * @param [in] value1 as float
202 void SetUniform2f( GLint location, GLfloat value0, GLfloat value1 );
205 * Sets the uniform value
206 * @param [in] location of uniform
207 * @param [in] value0 as float
208 * @param [in] value1 as float
209 * @param [in] value2 as float
211 void SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 );
214 * Sets the uniform value
215 * @param [in] location of uniform
216 * @param [in] value0 as float
217 * @param [in] value1 as float
218 * @param [in] value2 as float
219 * @param [in] value3 as float
221 void SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 );
224 * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
225 * so make sure your matrix is in correct order for GL.
226 * @param [in] location Location of uniform
227 * @param [in] count Count of matrices
228 * @param [in] value values as float pointers
230 void SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value );
233 * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
234 * so make sure your matrix is in correct order for GL.
235 * @param [in] location Location of uniform
236 * @param [in] count Count of matrices
237 * @param [in] value values as float pointers
239 void SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value );
242 * Needs to be called when GL context is (re)created
244 void GlContextCreated();
247 * Needs to be called when GL context is destroyed
249 void GlContextDestroyed();
252 * @return true if this program modifies geometry
254 bool ModifiesGeometry();
257 * Set the projection matrix that has currently been sent
258 * @param matrix to set
260 void SetProjectionMatrix( const Matrix* matrix )
262 mProjectionMatrix = matrix;
266 * Get the projection matrix that has currently been sent
267 * @return the matrix that is set
269 const Matrix* GetProjectionMatrix()
271 return mProjectionMatrix;
275 * Set the projection matrix that has currently been sent
276 * @param matrix to set
278 void SetViewMatrix( const Matrix* matrix )
280 mViewMatrix = matrix;
284 * Get the projection matrix that has currently been sent
285 * @return the matrix that is set
287 const Matrix* GetViewMatrix()
292 private: // Implementation
295 * Constructor, private so no direct instantiation
296 * @param[in] cache where the programs are stored
297 * @param[in] shaderData A smart pointer to a data structure containing the program source and binary
298 * @param[in] modifiesGeometry True if the vertex shader changes geometry
300 Program( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry );
305 * Destructor, non virtual as no virtual methods or inheritance
311 // default constructor, not defined
313 // assignment operator, not defined
314 Program& operator=( const Program& );
317 * Load the shader, from a precompiled binary if available, else from source code
328 * @param shaderType vertex or fragment shader
329 * @param shaderId of the shader, returned
330 * @param src of the shader
331 * @return true if the compilation succeeded
333 bool CompileShader(GLenum shaderType, GLuint& shaderId, const char* src);
336 * Links the shaders together to create program
341 * Frees the shader programs
348 void ResetAttribsUniformCache();
352 ProgramCache& mCache; ///< The program cache
353 Integration::GlAbstraction& mGlAbstraction; ///< The OpenGL Abstraction layer
354 const Matrix* mProjectionMatrix; ///< currently set projection matrix
355 const Matrix* mViewMatrix; ///< currently set view matrix
356 bool mLinked; ///< whether the program is linked
357 GLuint mVertexShaderId; ///< GL identifier for vertex shader
358 GLuint mFragmentShaderId; ///< GL identifier for fragment shader
359 GLuint mProgramId; ///< GL identifier for program
360 Integration::ShaderDataPtr mProgramData; ///< Shader program source and binary (when compiled & linked or loaded)
363 GLint mAttribLocations[ ATTRIB_TYPE_LAST ]; ///< attribute location cache
364 std::vector< std::pair< std::string, GLint > > mUniformLocations; ///< uniform location cache
366 // uniform value caching
367 GLint mUniformCacheInt[ MAX_UNIFORM_CACHE_SIZE ]; ///< Value cache for uniforms of single int
368 GLfloat mUniformCacheFloat[ MAX_UNIFORM_CACHE_SIZE ]; ///< Value cache for uniforms of single float
369 GLfloat mUniformCacheFloat4[ MAX_UNIFORM_CACHE_SIZE ][4]; ///< Value cache for uniforms of four float
370 bool mModifiesGeometry; ///< True if the program changes geometry
374 } // namespace Internal
378 #endif // __DALI_INTERNAL_PROGRAM_H__