Merge branch 'tizen' of platform/core/uifw/dali-core into devel/new_mesh
[platform/core/uifw/dali-core.git] / dali / internal / render / shaders / program.h
1 #ifndef __DALI_INTERNAL_PROGRAM_H__
2 #define __DALI_INTERNAL_PROGRAM_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <string>
23
24 // INTERNAL INCLUDES
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>
29
30 namespace Dali
31 {
32
33 class Matrix;
34
35 namespace Integration
36 {
37 class GlAbstraction;
38 class ShaderData;
39 }
40
41 namespace Internal
42 {
43
44 class ProgramCache;
45
46 /*
47  * A program contains a vertex & fragment shader.
48  *
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:
51  * \code
52  * uniform lowp vec4  uColor
53  * \endcode
54  *
55  * This describes a variable used to color text as it is drawn.
56  *
57  * uColor is set to the value specified by Actor::SetColor and is
58  * animatable through the property Actor::COLOR
59  */
60 class Program
61 {
62 public:
63
64   /**
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
68    */
69   static const int MAX_UNIFORM_CACHE_SIZE = 16;
70
71   /**
72    * Constant for uniform / attribute not found
73    */
74   static const int NOT_FOUND = -1;
75
76   /**
77    * Vertex attributes
78    */
79   enum AttribType
80   {
81     ATTRIB_UNKNOWN = -1,
82     ATTRIB_POSITION,
83     ATTRIB_NORMAL,
84     ATTRIB_TEXCOORD,
85     ATTRIB_COLOR,
86     ATTRIB_BONE_WEIGHTS,
87     ATTRIB_BONE_INDICES,
88     ATTRIB_TYPE_LAST
89   };
90
91   /**
92    * Common shader uniform names
93    */
94   enum UniformType
95   {
96     UNIFORM_NOT_QUERIED = -2,
97     UNIFORM_UNKNOWN = -1,
98     UNIFORM_MVP_MATRIX,
99     UNIFORM_MODELVIEW_MATRIX,
100     UNIFORM_PROJECTION_MATRIX,
101     UNIFORM_MODEL_MATRIX,
102     UNIFORM_VIEW_MATRIX,
103     UNIFORM_NORMAL_MATRIX,
104     UNIFORM_COLOR,
105     UNIFORM_CUSTOM_TEXTURE_COORDS,
106     UNIFORM_SAMPLER,
107     UNIFORM_SAMPLER_RECT,
108     UNIFORM_EFFECT_SAMPLER,
109     UNIFORM_EFFECT_SAMPLER_RECT,
110     UNIFORM_TIME_DELTA,
111     UNIFORM_SAMPLER_OPACITY,
112     UNIFORM_SAMPLER_NORMAL_MAP,
113
114     UNIFORM_TEXT_COLOR,
115     UNIFORM_SMOOTHING,
116     UNIFORM_OUTLINE,
117     UNIFORM_OUTLINE_COLOR,
118     UNIFORM_GLOW,
119     UNIFORM_GLOW_COLOR,
120     UNIFORM_SHADOW,
121     UNIFORM_SHADOW_COLOR,
122     UNIFORM_SHADOW_SMOOTHING,
123     UNIFORM_GRADIENT_COLOR,
124     UNIFORM_GRADIENT_LINE,
125     UNIFORM_INVERSE_TEXT_SIZE,
126
127     UNIFORM_SIZE,
128     UNIFORM_TYPE_LAST
129   };
130
131   /**
132    * Creates a new program, or returns a copy of an existing program in the program cache
133    * @param[in] cache where the programs are stored
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] modifiesGeometry True if the shader modifies geometry
138    * @return pointer to the program
139    */
140   static Program* New( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry );
141
142   /**
143    * Takes this program into use
144    */
145   void Use();
146
147   /**
148    * @return true if this program is used currently
149    */
150   bool IsUsed();
151
152   /**
153    * @param [in] type of the attribute
154    * @return the index of the attribute
155    */
156   GLint GetAttribLocation( AttribType type );
157
158   /**
159    * Register an attribute name in our local cache
160    * @param [in] name attribute name
161    * @return the index of the attribute name in local cache
162    */
163   unsigned int RegisterCustomAttribute( const std::string& name );
164
165   /**
166    * Gets the location of a pre-registered attribute.
167    * @param [in] attributeIndex of the attribute in local cache
168    * @return the index of the attribute in the GL program
169    */
170   GLint GetCustomAttributeLocation( unsigned int attributeIndex );
171
172   /**
173    * Register a uniform name in our local cache
174    * @param [in] name uniform name
175    * @return the index of the uniform name in local cache
176    */
177   unsigned int RegisterUniform( const std::string& name );
178
179   /**
180    * Gets the location of a pre-registered uniform.
181    * Uniforms in list UniformType are always registered and in the order of the enumeration
182    * @param [in] uniformIndex of the uniform in local cache
183    * @return the index of the uniform in the GL program
184    */
185   GLint GetUniformLocation( unsigned int uniformIndex );
186
187   /**
188    * Sets the uniform value
189    * @param [in] location of uniform
190    * @param [in] value0 as int
191    */
192   void SetUniform1i( GLint location, GLint value0 );
193
194   /**
195    * Sets the uniform value
196    * @param [in] location of uniform
197    * @param [in] value0 as int
198    * @param [in] value1 as int
199    * @param [in] value2 as int
200    * @param [in] value3 as int
201    */
202   void SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 );
203
204   /**
205    * Sets the uniform value
206    * @param [in] location of uniform
207    * @param [in] value0 as float
208    */
209   void SetUniform1f( GLint location, GLfloat value0 );
210
211   /**
212    * Sets the uniform value
213    * @param [in] location of uniform
214    * @param [in] value0 as float
215    * @param [in] value1 as float
216    */
217   void SetUniform2f( GLint location, GLfloat value0, GLfloat value1 );
218
219   /**
220    * Sets the uniform value
221    * @param [in] location of uniform
222    * @param [in] value0 as float
223    * @param [in] value1 as float
224    * @param [in] value2 as float
225    */
226   void SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 );
227
228   /**
229    * Sets the uniform value
230    * @param [in] location of uniform
231    * @param [in] value0 as float
232    * @param [in] value1 as float
233    * @param [in] value2 as float
234    * @param [in] value3 as float
235    */
236   void SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 );
237
238   /**
239    * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
240    * so make sure your matrix is in correct order for GL.
241    * @param [in] location Location of uniform
242    * @param [in] count Count of matrices
243    * @param [in] value values as float pointers
244    */
245   void SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value );
246
247   /**
248    * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
249    * so make sure your matrix is in correct order for GL.
250    * @param [in] location Location of uniform
251    * @param [in] count Count of matrices
252    * @param [in] value values as float pointers
253    */
254   void SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value );
255
256   /**
257    * Needs to be called when GL context is (re)created
258    */
259   void GlContextCreated();
260
261   /**
262    * Needs to be called when GL context is destroyed
263    */
264   void GlContextDestroyed();
265
266   /**
267    * @return true if this program modifies geometry
268    */
269   bool ModifiesGeometry();
270
271   /**
272    * Set the projection matrix that has currently been sent
273    * @param matrix to set
274    */
275   void SetProjectionMatrix( const Matrix* matrix )
276   {
277     mProjectionMatrix = matrix;
278   }
279
280   /**
281    * Get the projection matrix that has currently been sent
282    * @return the matrix that is set
283    */
284   const Matrix* GetProjectionMatrix()
285   {
286     return mProjectionMatrix;
287   }
288
289   /**
290    * Set the projection matrix that has currently been sent
291    * @param matrix to set
292    */
293   void SetViewMatrix( const Matrix* matrix )
294   {
295     mViewMatrix = matrix;
296   }
297
298   /**
299    * Get the projection matrix that has currently been sent
300    * @return the matrix that is set
301    */
302   const Matrix* GetViewMatrix()
303   {
304     return mViewMatrix;
305   }
306
307 private: // Implementation
308
309   /**
310    * Constructor, private so no direct instantiation
311    * @param[in] cache where the programs are stored
312    * @param[in] shaderData A smart pointer to a data structure containing the program source and binary
313    * @param[in] modifiesGeometry True if the vertex shader changes geometry
314    */
315   Program( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry );
316
317 public:
318
319   /**
320    * Destructor, non virtual as no virtual methods or inheritance
321    */
322   ~Program();
323
324 private:
325
326   // default constructor, not defined
327   Program();
328   // assignment operator, not defined
329   Program& operator=( const Program& );
330
331   /**
332    * Load the shader, from a precompiled binary if available, else from source code
333    */
334   void Load();
335
336   /**
337    * Unload the shader
338    */
339   void Unload();
340
341   /**
342    * Compile the shader
343    * @param shaderType vertex or fragment shader
344    * @param shaderId of the shader, returned
345    * @param src of the shader
346    * @return true if the compilation succeeded
347    */
348   bool CompileShader(GLenum shaderType, GLuint& shaderId, const char* src);
349
350   /**
351    * Links the shaders together to create program
352    */
353   void Link();
354
355   /**
356    * Frees the shader programs
357    */
358   void FreeShaders();
359
360   /**
361    * Resets caches
362    */
363   void ResetAttribsUniformCache();
364
365 private:  // Data
366
367   ProgramCache& mCache;                       ///< The program cache
368   Integration::GlAbstraction& mGlAbstraction; ///< The OpenGL Abstraction layer
369   const Matrix* mProjectionMatrix;            ///< currently set projection matrix
370   const Matrix* mViewMatrix;                  ///< currently set view matrix
371   bool mLinked;                               ///< whether the program is linked
372   GLuint mVertexShaderId;                     ///< GL identifier for vertex shader
373   GLuint mFragmentShaderId;                   ///< GL identifier for fragment shader
374   GLuint mProgramId;                          ///< GL identifier for program
375   Integration::ShaderDataPtr mProgramData;    ///< Shader program source and binary (when compiled & linked or loaded)
376
377   // location caches
378   std::vector< std::pair< std::string, GLint > > mAttributeLocations; ///< attribute location cache
379   std::vector< std::pair< std::string, GLint > > mUniformLocations; ///< uniform location cache
380
381   // uniform value caching
382   GLint mUniformCacheInt[ MAX_UNIFORM_CACHE_SIZE ];         ///< Value cache for uniforms of single int
383   GLfloat mUniformCacheFloat[ MAX_UNIFORM_CACHE_SIZE ];     ///< Value cache for uniforms of single float
384   GLfloat mUniformCacheFloat4[ MAX_UNIFORM_CACHE_SIZE ][4]; ///< Value cache for uniforms of four float
385   bool mModifiesGeometry;  ///< True if the program changes geometry
386
387 };
388
389 } // namespace Internal
390
391 } // namespace Dali
392
393 #endif // __DALI_INTERNAL_PROGRAM_H__