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.
51 * uColor is set to the value specified by Actor::SetColor and is
52 * animatable through the property Actor::COLOR
59 * Size of the uniform cache per program
60 * GLES specification states that minimum uniform count for fragment shader
61 * is 16 and for vertex shader 128. We're caching the 16 common ones for now
63 static const int MAX_UNIFORM_CACHE_SIZE = 16;
66 * Constant for uniform / attribute not found
68 static const int NOT_FOUND = -1;
86 * Common shader uniform names
90 UNIFORM_NOT_QUERIED = -2,
93 UNIFORM_MODELVIEW_MATRIX,
94 UNIFORM_PROJECTION_MATRIX,
97 UNIFORM_NORMAL_MATRIX,
99 UNIFORM_CUSTOM_TEXTURE_COORDS,
101 UNIFORM_SAMPLER_RECT,
102 UNIFORM_EFFECT_SAMPLER,
103 UNIFORM_EFFECT_SAMPLER_RECT,
105 UNIFORM_SAMPLER_OPACITY,
106 UNIFORM_SAMPLER_NORMAL_MAP,
112 * Creates a new program, or returns a copy of an existing program in the program cache
113 * @param[in] cache where the programs are stored
114 * @param[in] shaderData A pointer to a data structure containing the program source
115 * and optionally precompiled binary. If the binary is empty the program bytecode
116 * is copied into it after compilation and linking)
117 * @param[in] modifiesGeometry True if the shader modifies geometry
118 * @return pointer to the program
120 static Program* New( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry );
123 * Takes this program into use
128 * @return true if this program is used currently
133 * @param [in] type of the attribute
134 * @return the index of the attribute
136 GLint GetAttribLocation( AttribType type );
139 * Register a uniform name in our local cache
140 * @param [in] name uniform name
141 * @return the index of the uniform name in local cache
143 unsigned int RegisterUniform( const std::string& name );
146 * Gets the location of a pre-registered uniform.
147 * Uniforms in list UniformType are always registered and in the order of the enumeration
148 * @param [in] uniformIndex of the uniform in local cache
149 * @return the index of the uniform in the GL program
151 GLint GetUniformLocation( unsigned int uniformIndex );
154 * Sets the uniform value
155 * @param [in] location of uniform
156 * @param [in] value0 as int
158 void SetUniform1i( GLint location, GLint value0 );
161 * Sets the uniform value
162 * @param [in] location of uniform
163 * @param [in] value0 as int
164 * @param [in] value1 as int
165 * @param [in] value2 as int
166 * @param [in] value3 as int
168 void SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 );
171 * Sets the uniform value
172 * @param [in] location of uniform
173 * @param [in] value0 as float
175 void SetUniform1f( GLint location, GLfloat value0 );
178 * Sets the uniform value
179 * @param [in] location of uniform
180 * @param [in] value0 as float
181 * @param [in] value1 as float
183 void SetUniform2f( GLint location, GLfloat value0, GLfloat value1 );
186 * Sets the uniform value
187 * @param [in] location of uniform
188 * @param [in] value0 as float
189 * @param [in] value1 as float
190 * @param [in] value2 as float
192 void SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 );
195 * Sets the uniform value
196 * @param [in] location of uniform
197 * @param [in] value0 as float
198 * @param [in] value1 as float
199 * @param [in] value2 as float
200 * @param [in] value3 as float
202 void SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 );
205 * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
206 * so make sure your matrix is in correct order for GL.
207 * @param [in] location Location of uniform
208 * @param [in] count Count of matrices
209 * @param [in] value values as float pointers
211 void SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value );
214 * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
215 * so make sure your matrix is in correct order for GL.
216 * @param [in] location Location of uniform
217 * @param [in] count Count of matrices
218 * @param [in] value values as float pointers
220 void SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value );
223 * Needs to be called when GL context is (re)created
225 void GlContextCreated();
228 * Needs to be called when GL context is destroyed
230 void GlContextDestroyed();
233 * @return true if this program modifies geometry
235 bool ModifiesGeometry();
238 * Set the projection matrix that has currently been sent
239 * @param matrix to set
241 void SetProjectionMatrix( const Matrix* matrix )
243 mProjectionMatrix = matrix;
247 * Get the projection matrix that has currently been sent
248 * @return the matrix that is set
250 const Matrix* GetProjectionMatrix()
252 return mProjectionMatrix;
256 * Set the projection matrix that has currently been sent
257 * @param matrix to set
259 void SetViewMatrix( const Matrix* matrix )
261 mViewMatrix = matrix;
265 * Get the projection matrix that has currently been sent
266 * @return the matrix that is set
268 const Matrix* GetViewMatrix()
273 private: // Implementation
276 * Constructor, private so no direct instantiation
277 * @param[in] cache where the programs are stored
278 * @param[in] shaderData A smart pointer to a data structure containing the program source and binary
279 * @param[in] modifiesGeometry True if the vertex shader changes geometry
281 Program( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry );
286 * Destructor, non virtual as no virtual methods or inheritance
292 // default constructor, not defined
294 // assignment operator, not defined
295 Program& operator=( const Program& );
298 * Load the shader, from a precompiled binary if available, else from source code
309 * @param shaderType vertex or fragment shader
310 * @param shaderId of the shader, returned
311 * @param src of the shader
312 * @return true if the compilation succeeded
314 bool CompileShader(GLenum shaderType, GLuint& shaderId, const char* src);
317 * Links the shaders together to create program
322 * Frees the shader programs
329 void ResetAttribsUniformCache();
333 ProgramCache& mCache; ///< The program cache
334 Integration::GlAbstraction& mGlAbstraction; ///< The OpenGL Abstraction layer
335 const Matrix* mProjectionMatrix; ///< currently set projection matrix
336 const Matrix* mViewMatrix; ///< currently set view matrix
337 bool mLinked; ///< whether the program is linked
338 GLuint mVertexShaderId; ///< GL identifier for vertex shader
339 GLuint mFragmentShaderId; ///< GL identifier for fragment shader
340 GLuint mProgramId; ///< GL identifier for program
341 Integration::ShaderDataPtr mProgramData; ///< Shader program source and binary (when compiled & linked or loaded)
344 GLint mAttribLocations[ ATTRIB_TYPE_LAST ]; ///< attribute location cache
345 std::vector< std::pair< std::string, GLint > > mUniformLocations; ///< uniform location cache
347 // uniform value caching
348 GLint mUniformCacheInt[ MAX_UNIFORM_CACHE_SIZE ]; ///< Value cache for uniforms of single int
349 GLfloat mUniformCacheFloat[ MAX_UNIFORM_CACHE_SIZE ]; ///< Value cache for uniforms of single float
350 GLfloat mUniformCacheFloat4[ MAX_UNIFORM_CACHE_SIZE ][4]; ///< Value cache for uniforms of four float
351 bool mModifiesGeometry; ///< True if the program changes geometry
355 } // namespace Internal
359 #endif // __DALI_INTERNAL_PROGRAM_H__