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/integration-api/resource-cache.h>
46 * A program contains a vertex & fragment shader.
48 * A program will contain vertex attributes and uniform variables
49 * E.g. inside the code for our text fragment shaders we have the line:
51 * uniform lowp vec4 uColor
54 * This describes a variable used to color text as it is drawn.
56 * uColor is set to the value specified by Actor::SetColor and is
57 * animatable through the property Actor::COLOR
64 * Size of the uniform cache per program
65 * GLES specification states that minimum uniform count for fragment shader
66 * is 16 and for vertex shader 128. We're caching the 16 common ones for now
68 static const int MAX_UNIFORM_CACHE_SIZE = 16;
71 * Constant for uniform / attribute not found
73 static const int NOT_FOUND = -1;
91 * Common shader uniform names
95 UNIFORM_NOT_QUERIED = -2,
98 UNIFORM_MODELVIEW_MATRIX,
99 UNIFORM_PROJECTION_MATRIX,
100 UNIFORM_MODEL_MATRIX,
102 UNIFORM_NORMAL_MATRIX,
104 UNIFORM_CUSTOM_TEXTURE_COORDS,
106 UNIFORM_SAMPLER_RECT,
107 UNIFORM_EFFECT_SAMPLER,
108 UNIFORM_EFFECT_SAMPLER_RECT,
110 UNIFORM_SAMPLER_OPACITY,
111 UNIFORM_SAMPLER_NORMAL_MAP,
116 UNIFORM_OUTLINE_COLOR,
120 UNIFORM_SHADOW_COLOR,
121 UNIFORM_SHADOW_SMOOTHING,
122 UNIFORM_GRADIENT_COLOR,
123 UNIFORM_GRADIENT_LINE,
124 UNIFORM_INVERSE_TEXT_SIZE,
130 * Creates a new program, or returns a copy of an existing program in the program cache
131 * maintained by Context
132 * @param [in] resourceId ResourceManager resourceId for the shader source and binary.
133 * Used as a lookup key in the program cache
134 * @param[in] shaderData A pointer to a data structure containing the program source
135 * and optionally precompiled binary. If the binary is empty the program bytecode
136 * is copied into it after compilation and linking)
137 * @param [in] context GL context
138 * @return pointer to the program
140 static Program* New( const Integration::ResourceId& resourceId, Integration::ShaderData* shaderData, Context& context );
143 * Takes this program into use
148 * @return true if this program is used currently
153 * @param [in] type of the attribute
154 * @return the index of the attribute
156 GLint GetAttribLocation( AttribType type );
159 * Register a uniform name in our local cache
160 * @param [in] name uniform name
161 * @return the index of the uniform name in local cache
163 unsigned int RegisterExternalUniform( const std::string& name );
166 * Register a uniform name in our local cache
167 * @param [in] name uniform name
168 * @return the index of the uniform name in local cache
170 unsigned int RegisterUniform( const char* name );
173 * Gets the location of a pre-registered uniform.
174 * Uniforms in list UniformType are always registered and in the order of the enumeration
175 * @param [in] uniformIndex of the uniform in local cache
176 * @return the index of the uniform in the GL program
178 GLint GetUniformLocation( unsigned int uniformIndex );
181 * Sets the uniform value
182 * @param [in] location of uniform
183 * @param [in] value0 as int
185 void SetUniform1i( GLint location, GLint value0 );
188 * Sets the uniform value
189 * @param [in] location of uniform
190 * @param [in] value0 as int
191 * @param [in] value1 as int
192 * @param [in] value2 as int
193 * @param [in] value3 as int
195 void SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 );
198 * Sets the uniform value
199 * @param [in] location of uniform
200 * @param [in] value0 as float
202 void SetUniform1f( GLint location, GLfloat value0 );
205 * Sets the uniform value
206 * @param [in] location of uniform
207 * @param [in] value0 as float
208 * @param [in] value1 as float
210 void SetUniform2f( GLint location, GLfloat value0, GLfloat value1 );
213 * Sets the uniform value
214 * @param [in] location of uniform
215 * @param [in] value0 as float
216 * @param [in] value1 as float
217 * @param [in] value2 as float
219 void SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 );
222 * Sets the uniform value
223 * @param [in] location of uniform
224 * @param [in] value0 as float
225 * @param [in] value1 as float
226 * @param [in] value2 as float
227 * @param [in] value3 as float
229 void SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 );
232 * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
233 * so make sure your matrix is in correct order for GL.
234 * @param [in] location Location of uniform
235 * @param [in] count Count of matrices
236 * @param [in] value values as float pointers
238 void SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value );
241 * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
242 * so make sure your matrix is in correct order for GL.
243 * @param [in] location Location of uniform
244 * @param [in] count Count of matrices
245 * @param [in] value values as float pointers
247 void SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value );
250 * Needs to be called when GL context is (re)created
252 void GlContextCreated();
255 * Needs to be called when GL context is destroyed
257 void GlContextDestroyed();
259 private: // Implementation
262 * Constructor, private so no direct instantiation
263 * @param[in] shaderData A pointer to a data structure containing the program source and binary
264 * @param[in] context The GL context state cache.
266 Program( Integration::ShaderData* shaderData, Context& context );
271 * Destructor, non virtual as no virtual methods or inheritance
277 // default constructor, not defined
279 // assignment operator, not defined
280 Program& operator=( const Program& );
283 * Load the shader, from a precompiled binary if available, else from source code
294 * @param shaderType vertex or fragment shader
295 * @param shaderId of the shader, returned
296 * @param src of the shader
297 * @return true if the compilation succeeded
299 bool CompileShader(GLenum shaderType, GLuint& shaderId, const char* src);
302 * Links the shaders together to create program
307 * Frees the shader programs
314 void ResetAttribsUniforms();
318 Context& mContext; ///< The GL context state cache
319 Integration::GlAbstraction& mGlAbstraction; ///< The OpenGL Abstraction layer
320 bool mLinked; ///< whether the program is linked
321 GLuint mVertexShaderId; ///< GL identifier for vertex shader
322 GLuint mFragmentShaderId; ///< GL identifier for fragment shader
323 GLuint mProgramId; ///< GL identifier for program
324 Integration::ShaderData* mProgramData; ///< Shader program source and binary (when compiled & linked or loaded)
326 // uniform location caches
327 GLint mAttribLocations[ ATTRIB_TYPE_LAST ]; ///< attribute location cache
328 GLint mBuiltinUniformLocations[ UNIFORM_TYPE_LAST ]; ///< uniform location cache for built in uniforms
329 std::vector< std::pair< const char *, GLint > > mCustomUniformLocations; ///< uniform location cache for custom uniforms
330 static std::set< std::string > mExternalUniformNames; /// < names for externally specified uniforms, set to avoid duplicates
332 // uniform value caching
333 GLint mUniformCacheInt[ MAX_UNIFORM_CACHE_SIZE ]; ///< Value cache for uniforms of single int
334 GLfloat mUniformCacheFloat[ MAX_UNIFORM_CACHE_SIZE ]; ///< Value cache for uniforms of single float
335 GLfloat mUniformCacheFloat4[ MAX_UNIFORM_CACHE_SIZE ][4]; ///< Value cache for uniforms of four float
339 } // namespace Internal
343 #endif // __DALI_INTERNAL_PROGRAM_H__