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/common/set-wrapper.h>
27 #include <dali/public-api/object/ref-object.h>
28 #include <dali/internal/render/gl-resources/context.h>
29 #include <dali/integration-api/resource-cache.h>
30 #include <dali/integration-api/shader-data.h>
49 * A program contains a vertex & fragment shader.
51 * A program will contain vertex attributes and uniform variables
52 * E.g. inside the code for our text fragment shaders we have the line:
54 * uniform lowp vec4 uColor
57 * This describes a variable used to color text as it is drawn.
59 * uColor is set to the value specified by Actor::SetColor and is
60 * animatable through the property Actor::COLOR
67 * Size of the uniform cache per program
68 * GLES specification states that minimum uniform count for fragment shader
69 * is 16 and for vertex shader 128. We're caching the 16 common ones for now
71 static const int MAX_UNIFORM_CACHE_SIZE = 16;
74 * Constant for uniform / attribute not found
76 static const int NOT_FOUND = -1;
94 * Common shader uniform names
98 UNIFORM_NOT_QUERIED = -2,
101 UNIFORM_MODELVIEW_MATRIX,
102 UNIFORM_PROJECTION_MATRIX,
103 UNIFORM_MODEL_MATRIX,
105 UNIFORM_NORMAL_MATRIX,
107 UNIFORM_CUSTOM_TEXTURE_COORDS,
109 UNIFORM_SAMPLER_RECT,
110 UNIFORM_EFFECT_SAMPLER,
111 UNIFORM_EFFECT_SAMPLER_RECT,
113 UNIFORM_SAMPLER_OPACITY,
114 UNIFORM_SAMPLER_NORMAL_MAP,
119 UNIFORM_OUTLINE_COLOR,
123 UNIFORM_SHADOW_COLOR,
124 UNIFORM_SHADOW_SMOOTHING,
125 UNIFORM_GRADIENT_COLOR,
126 UNIFORM_GRADIENT_LINE,
127 UNIFORM_INVERSE_TEXT_SIZE,
133 * Creates a new program, or returns a copy of an existing program in the program cache
134 * @param [in] resourceId ResourceManager resourceId for the shader source and binary.
135 * Used as a lookup key in the program cache
136 * @param[in] shaderData A pointer to a data structure containing the program source
137 * and optionally precompiled binary. If the binary is empty the program bytecode
138 * is copied into it after compilation and linking)
139 * @param [in] context GL context
140 * @param [in] modifiesGeometry True if the shader modifies geometry
141 * @return pointer to the program
143 static Program* New( const Integration::ResourceId& resourceId, Integration::ShaderData* shaderData, Context& context, bool modifiesGeometry );
146 * Takes this program into use
151 * @return true if this program is used currently
156 * @param [in] type of the attribute
157 * @return the index of the attribute
159 GLint GetAttribLocation( AttribType type );
162 * Register a uniform name in our local cache
163 * @param [in] name uniform name
164 * @return the index of the uniform name in local cache
166 unsigned int RegisterUniform( const std::string& name );
169 * Gets the location of a pre-registered uniform.
170 * Uniforms in list UniformType are always registered and in the order of the enumeration
171 * @param [in] uniformIndex of the uniform in local cache
172 * @return the index of the uniform in the GL program
174 GLint GetUniformLocation( unsigned int uniformIndex );
177 * Sets the uniform value
178 * @param [in] location of uniform
179 * @param [in] value0 as int
181 void SetUniform1i( GLint location, GLint value0 );
184 * Sets the uniform value
185 * @param [in] location of uniform
186 * @param [in] value0 as int
187 * @param [in] value1 as int
188 * @param [in] value2 as int
189 * @param [in] value3 as int
191 void SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 );
194 * Sets the uniform value
195 * @param [in] location of uniform
196 * @param [in] value0 as float
198 void SetUniform1f( GLint location, GLfloat value0 );
201 * Sets the uniform value
202 * @param [in] location of uniform
203 * @param [in] value0 as float
204 * @param [in] value1 as float
206 void SetUniform2f( GLint location, GLfloat value0, GLfloat value1 );
209 * Sets the uniform value
210 * @param [in] location of uniform
211 * @param [in] value0 as float
212 * @param [in] value1 as float
213 * @param [in] value2 as float
215 void SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 );
218 * Sets the uniform value
219 * @param [in] location of uniform
220 * @param [in] value0 as float
221 * @param [in] value1 as float
222 * @param [in] value2 as float
223 * @param [in] value3 as float
225 void SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 );
228 * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
229 * so make sure your matrix is in correct order for GL.
230 * @param [in] location Location of uniform
231 * @param [in] count Count of matrices
232 * @param [in] value values as float pointers
234 void SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value );
237 * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
238 * so make sure your matrix is in correct order for GL.
239 * @param [in] location Location of uniform
240 * @param [in] count Count of matrices
241 * @param [in] value values as float pointers
243 void SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value );
246 * Needs to be called when GL context is (re)created
248 void GlContextCreated();
251 * Needs to be called when GL context is destroyed
253 void GlContextDestroyed();
256 * @return true if this program modifies geometry
258 bool ModifiesGeometry();
261 * Set the projection matrix that has currently been sent
262 * @param matrix to set
264 void SetProjectionMatrix( const Matrix* matrix )
266 mProjectionMatrix = matrix;
270 * Get the projection matrix that has currently been sent
271 * @return the matrix that is set
273 const Matrix* GetProjectionMatrix()
275 return mProjectionMatrix;
279 * Set the projection matrix that has currently been sent
280 * @param matrix to set
282 void SetViewMatrix( const Matrix* matrix )
284 mViewMatrix = matrix;
288 * Get the projection matrix that has currently been sent
289 * @return the matrix that is set
291 const Matrix* GetViewMatrix()
296 private: // Implementation
299 * Constructor, private so no direct instantiation
300 * @param[in] shaderData A pointer to a data structure containing the program source and binary
301 * @param[in] context The GL context state cache.
302 * @param[in] modifiesGeometry True if the vertex shader changes geometry
304 Program( Integration::ShaderData* shaderData, Context& context, bool modifiesGeometry );
309 * Destructor, non virtual as no virtual methods or inheritance
315 // default constructor, not defined
317 // assignment operator, not defined
318 Program& operator=( const Program& );
321 * Load the shader, from a precompiled binary if available, else from source code
332 * @param shaderType vertex or fragment shader
333 * @param shaderId of the shader, returned
334 * @param src of the shader
335 * @return true if the compilation succeeded
337 bool CompileShader(GLenum shaderType, GLuint& shaderId, const char* src);
340 * Links the shaders together to create program
345 * Frees the shader programs
352 void ResetAttribsUniforms();
356 Context& mContext; ///< The GL context state cache
357 Integration::GlAbstraction& mGlAbstraction; ///< The OpenGL Abstraction layer
358 const Matrix* mProjectionMatrix; ///< currently set projection matrix
359 const Matrix* mViewMatrix; ///< currently set view matrix
360 bool mLinked; ///< whether the program is linked
361 GLuint mVertexShaderId; ///< GL identifier for vertex shader
362 GLuint mFragmentShaderId; ///< GL identifier for fragment shader
363 GLuint mProgramId; ///< GL identifier for program
364 Integration::ShaderDataPtr mProgramData; ///< Shader program source and binary (when compiled & linked or loaded)
367 GLint mAttribLocations[ ATTRIB_TYPE_LAST ]; ///< attribute location cache
368 std::vector< std::pair< std::string, GLint > > mUniformLocations; ///< uniform location cache
370 // uniform value caching
371 GLint mUniformCacheInt[ MAX_UNIFORM_CACHE_SIZE ]; ///< Value cache for uniforms of single int
372 GLfloat mUniformCacheFloat[ MAX_UNIFORM_CACHE_SIZE ]; ///< Value cache for uniforms of single float
373 GLfloat mUniformCacheFloat4[ MAX_UNIFORM_CACHE_SIZE ][4]; ///< Value cache for uniforms of four float
374 bool mModifiesGeometry; ///< True if the program changes geometry
377 } // namespace Internal
381 #endif // __DALI_INTERNAL_PROGRAM_H__