Merge remote-tracking branch 'origin/tizen' into new_text
[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  *
51  * uColor is set to the value specified by Actor::SetColor and is
52  * animatable through the property Actor::COLOR
53  */
54 class Program
55 {
56 public:
57
58   /**
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
62    */
63   static const int MAX_UNIFORM_CACHE_SIZE = 16;
64
65   /**
66    * Constant for uniform / attribute not found
67    */
68   static const int NOT_FOUND = -1;
69
70   /**
71    * Vertex attributes
72    */
73   enum AttribType
74   {
75     ATTRIB_UNKNOWN = -1,
76     ATTRIB_POSITION,
77     ATTRIB_NORMAL,
78     ATTRIB_TEXCOORD,
79     ATTRIB_COLOR,
80     ATTRIB_BONE_WEIGHTS,
81     ATTRIB_BONE_INDICES,
82     ATTRIB_TYPE_LAST
83   };
84
85   /**
86    * Common shader uniform names
87    */
88   enum UniformType
89   {
90     UNIFORM_NOT_QUERIED = -2,
91     UNIFORM_UNKNOWN = -1,
92     UNIFORM_MVP_MATRIX,
93     UNIFORM_MODELVIEW_MATRIX,
94     UNIFORM_PROJECTION_MATRIX,
95     UNIFORM_MODEL_MATRIX,
96     UNIFORM_VIEW_MATRIX,
97     UNIFORM_NORMAL_MATRIX,
98     UNIFORM_COLOR,
99     UNIFORM_CUSTOM_TEXTURE_COORDS,
100     UNIFORM_SAMPLER,
101     UNIFORM_SAMPLER_RECT,
102     UNIFORM_EFFECT_SAMPLER,
103     UNIFORM_EFFECT_SAMPLER_RECT,
104     UNIFORM_TIME_DELTA,
105     UNIFORM_SAMPLER_OPACITY,
106     UNIFORM_SAMPLER_NORMAL_MAP,
107
108     UNIFORM_TYPE_LAST
109   };
110
111   /**
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
119    */
120   static Program* New( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry );
121
122   /**
123    * Takes this program into use
124    */
125   void Use();
126
127   /**
128    * @return true if this program is used currently
129    */
130   bool IsUsed();
131
132   /**
133    * @param [in] type of the attribute
134    * @return the index of the attribute
135    */
136   GLint GetAttribLocation( AttribType type );
137
138   /**
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
142    */
143   unsigned int RegisterUniform( const std::string& name );
144
145   /**
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
150    */
151   GLint GetUniformLocation( unsigned int uniformIndex );
152
153   /**
154    * Sets the uniform value
155    * @param [in] location of uniform
156    * @param [in] value0 as int
157    */
158   void SetUniform1i( GLint location, GLint value0 );
159
160   /**
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
167    */
168   void SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 );
169
170   /**
171    * Sets the uniform value
172    * @param [in] location of uniform
173    * @param [in] value0 as float
174    */
175   void SetUniform1f( GLint location, GLfloat value0 );
176
177   /**
178    * Sets the uniform value
179    * @param [in] location of uniform
180    * @param [in] value0 as float
181    * @param [in] value1 as float
182    */
183   void SetUniform2f( GLint location, GLfloat value0, GLfloat value1 );
184
185   /**
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
191    */
192   void SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 );
193
194   /**
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
201    */
202   void SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 );
203
204   /**
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
210    */
211   void SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value );
212
213   /**
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
219    */
220   void SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value );
221
222   /**
223    * Needs to be called when GL context is (re)created
224    */
225   void GlContextCreated();
226
227   /**
228    * Needs to be called when GL context is destroyed
229    */
230   void GlContextDestroyed();
231
232   /**
233    * @return true if this program modifies geometry
234    */
235   bool ModifiesGeometry();
236
237   /**
238    * Set the projection matrix that has currently been sent
239    * @param matrix to set
240    */
241   void SetProjectionMatrix( const Matrix* matrix )
242   {
243     mProjectionMatrix = matrix;
244   }
245
246   /**
247    * Get the projection matrix that has currently been sent
248    * @return the matrix that is set
249    */
250   const Matrix* GetProjectionMatrix()
251   {
252     return mProjectionMatrix;
253   }
254
255   /**
256    * Set the projection matrix that has currently been sent
257    * @param matrix to set
258    */
259   void SetViewMatrix( const Matrix* matrix )
260   {
261     mViewMatrix = matrix;
262   }
263
264   /**
265    * Get the projection matrix that has currently been sent
266    * @return the matrix that is set
267    */
268   const Matrix* GetViewMatrix()
269   {
270     return mViewMatrix;
271   }
272
273 private: // Implementation
274
275   /**
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
280    */
281   Program( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry );
282
283 public:
284
285   /**
286    * Destructor, non virtual as no virtual methods or inheritance
287    */
288   ~Program();
289
290 private:
291
292   // default constructor, not defined
293   Program();
294   // assignment operator, not defined
295   Program& operator=( const Program& );
296
297   /**
298    * Load the shader, from a precompiled binary if available, else from source code
299    */
300   void Load();
301
302   /**
303    * Unload the shader
304    */
305   void Unload();
306
307   /**
308    * Compile the shader
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
313    */
314   bool CompileShader(GLenum shaderType, GLuint& shaderId, const char* src);
315
316   /**
317    * Links the shaders together to create program
318    */
319   void Link();
320
321   /**
322    * Frees the shader programs
323    */
324   void FreeShaders();
325
326   /**
327    * Resets caches
328    */
329   void ResetAttribsUniformCache();
330
331 private:  // Data
332
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)
342
343   // location caches
344   GLint mAttribLocations[ ATTRIB_TYPE_LAST ]; ///< attribute location cache
345   std::vector< std::pair< std::string, GLint > > mUniformLocations; ///< uniform location cache
346
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
352
353 };
354
355 } // namespace Internal
356
357 } // namespace Dali
358
359 #endif // __DALI_INTERNAL_PROGRAM_H__