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/internal/common/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;
82 * Common shader uniform names
86 UNIFORM_NOT_QUERIED = -2,
89 UNIFORM_MODELVIEW_MATRIX,
90 UNIFORM_PROJECTION_MATRIX,
93 UNIFORM_NORMAL_MATRIX,
97 UNIFORM_EFFECT_SAMPLER,
104 * Creates a new program, or returns a copy of an existing program in the program cache
105 * @param[in] cache where the programs are stored
106 * @param[in] shaderData A pointer to a data structure containing the program source
107 * and optionally precompiled binary. If the binary is empty the program bytecode
108 * is copied into it after compilation and linking)
109 * @param[in] modifiesGeometry True if the shader modifies geometry
110 * @return pointer to the program
112 static Program* New( ProgramCache& cache, Internal::ShaderDataPtr shaderData, bool modifiesGeometry );
115 * Takes this program into use
120 * @return true if this program is used currently
125 * @param [in] type of the attribute
126 * @return the index of the attribute
128 GLint GetAttribLocation( AttribType type );
131 * Register an attribute name in our local cache
132 * @param [in] name attribute name
133 * @return the index of the attribute name in local cache
135 unsigned int RegisterCustomAttribute( const std::string& name );
138 * Gets the location of a pre-registered attribute.
139 * @param [in] attributeIndex of the attribute in local cache
140 * @return the index of the attribute in the GL program
142 GLint GetCustomAttributeLocation( unsigned int attributeIndex );
145 * Register a uniform name in our local cache
146 * @param [in] name uniform name
147 * @return the index of the uniform name in local cache
149 unsigned int RegisterUniform( const std::string& name );
152 * Gets the location of a pre-registered uniform.
153 * Uniforms in list UniformType are always registered and in the order of the enumeration
154 * @param [in] uniformIndex of the uniform in local cache
155 * @return the index of the uniform in the GL program
157 GLint GetUniformLocation( unsigned int uniformIndex );
160 * Introspect the newly loaded shader to get the active sampler locations
162 void GetActiveSamplerUniforms();
165 * Gets the uniform location for a sampler
166 * @param [in] index The index of the active sampler
167 * @param [out] location The location of the requested sampler
168 * @return true if the active sampler was found
170 bool GetSamplerUniformLocation( unsigned int index, GLint& location );
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 * Special handling for size as we're using uniform geometry so size is passed on to most programs
206 * but it rarely changes so we can cache it
207 * @param [in] location of uniform
208 * @param [in] value0 as float
209 * @param [in] value1 as float
210 * @param [in] value2 as float
212 void SetSizeUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 );
215 * Sets the uniform value
216 * @param [in] location of uniform
217 * @param [in] value0 as float
218 * @param [in] value1 as float
219 * @param [in] value2 as float
221 void SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 );
224 * Sets the uniform value
225 * @param [in] location of uniform
226 * @param [in] value0 as float
227 * @param [in] value1 as float
228 * @param [in] value2 as float
229 * @param [in] value3 as float
231 void SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 );
234 * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
235 * so make sure your matrix is in correct order for GL.
236 * @param [in] location Location of uniform
237 * @param [in] count Count of matrices
238 * @param [in] value values as float pointers
240 void SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value );
243 * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
244 * so make sure your matrix is in correct order for GL.
245 * @param [in] location Location of uniform
246 * @param [in] count Count of matrices
247 * @param [in] value values as float pointers
249 void SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value );
252 * Needs to be called when GL context is (re)created
254 void GlContextCreated();
257 * Needs to be called when GL context is destroyed
259 void GlContextDestroyed();
262 * @return true if this program modifies geometry
264 bool ModifiesGeometry();
267 * Set the projection matrix that has currently been sent
268 * @param matrix to set
270 void SetProjectionMatrix( const Matrix* matrix )
272 mProjectionMatrix = matrix;
276 * Get the projection matrix that has currently been sent
277 * @return the matrix that is set
279 const Matrix* GetProjectionMatrix()
281 return mProjectionMatrix;
285 * Set the projection matrix that has currently been sent
286 * @param matrix to set
288 void SetViewMatrix( const Matrix* matrix )
290 mViewMatrix = matrix;
294 * Get the projection matrix that has currently been sent
295 * @return the matrix that is set
297 const Matrix* GetViewMatrix()
302 private: // Implementation
305 * Constructor, private so no direct instantiation
306 * @param[in] cache where the programs are stored
307 * @param[in] shaderData A smart pointer to a data structure containing the program source and binary
308 * @param[in] modifiesGeometry True if the vertex shader changes geometry
310 Program( ProgramCache& cache, Internal::ShaderDataPtr shaderData, bool modifiesGeometry );
315 * Destructor, non virtual as no virtual methods or inheritance
321 // default constructor, not defined
323 // assignment operator, not defined
324 Program& operator=( const Program& );
327 * Load the shader, from a precompiled binary if available, else from source code
338 * @param shaderType vertex or fragment shader
339 * @param shaderId of the shader, returned
340 * @param src of the shader
341 * @return true if the compilation succeeded
343 bool CompileShader(GLenum shaderType, GLuint& shaderId, const char* src);
346 * Links the shaders together to create program
351 * Frees the shader programs
358 void ResetAttribsUniformCache();
362 ProgramCache& mCache; ///< The program cache
363 Integration::GlAbstraction& mGlAbstraction; ///< The OpenGL Abstraction layer
364 const Matrix* mProjectionMatrix; ///< currently set projection matrix
365 const Matrix* mViewMatrix; ///< currently set view matrix
366 bool mLinked; ///< whether the program is linked
367 GLuint mVertexShaderId; ///< GL identifier for vertex shader
368 GLuint mFragmentShaderId; ///< GL identifier for fragment shader
369 GLuint mProgramId; ///< GL identifier for program
370 Internal::ShaderDataPtr mProgramData; ///< Shader program source and binary (when compiled & linked or loaded)
373 typedef std::pair< std::string, GLint > NameLocationPair;
374 typedef std::vector< NameLocationPair > Locations;
376 Locations mAttributeLocations; ///< attribute location cache
377 Locations mUniformLocations; ///< uniform location cache
378 std::vector<GLint> mSamplerUniformLocations; ///< sampler uniform location cache
380 // uniform value caching
381 GLint mUniformCacheInt[ MAX_UNIFORM_CACHE_SIZE ]; ///< Value cache for uniforms of single int
382 GLfloat mUniformCacheFloat[ MAX_UNIFORM_CACHE_SIZE ]; ///< Value cache for uniforms of single float
383 GLfloat mUniformCacheFloat2[ MAX_UNIFORM_CACHE_SIZE ][2]; ///< Value cache for uniforms of two floats
384 GLfloat mUniformCacheFloat4[ MAX_UNIFORM_CACHE_SIZE ][4]; ///< Value cache for uniforms of four floats
385 Vector3 mSizeUniformCache; ///< Cache value for size uniform
386 bool mModifiesGeometry; ///< True if the program changes geometry
390 } // namespace Internal
394 #endif // __DALI_INTERNAL_PROGRAM_H__