341abb91ac8674d4a1dac4be05c12815b5c859a6
[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/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>
31
32 namespace Dali
33 {
34
35 class Matrix;
36
37 namespace Integration
38 {
39 class GlAbstraction;
40 class ShaderData;
41 }
42
43 namespace Internal
44 {
45
46 class Context;
47
48 /*
49  * A program contains a vertex & fragment shader.
50  *
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:
53  * \code
54  * uniform lowp vec4  uColor
55  * \endcode
56  *
57  * This describes a variable used to color text as it is drawn.
58  *
59  * uColor is set to the value specified by Actor::SetColor and is
60  * animatable through the property Actor::COLOR
61  */
62 class Program
63 {
64 public:
65
66   /**
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
70    */
71   static const int MAX_UNIFORM_CACHE_SIZE = 16;
72
73   /**
74    * Constant for uniform / attribute not found
75    */
76   static const int NOT_FOUND = -1;
77
78   /**
79    * Vertex attributes
80    */
81   enum AttribType
82   {
83     ATTRIB_UNKNOWN = -1,
84     ATTRIB_POSITION,
85     ATTRIB_NORMAL,
86     ATTRIB_TEXCOORD,
87     ATTRIB_COLOR,
88     ATTRIB_BONE_WEIGHTS,
89     ATTRIB_BONE_INDICES,
90     ATTRIB_TYPE_LAST
91   };
92
93   /**
94    * Common shader uniform names
95    */
96   enum UniformType
97   {
98     UNIFORM_NOT_QUERIED = -2,
99     UNIFORM_UNKNOWN = -1,
100     UNIFORM_MVP_MATRIX,
101     UNIFORM_MODELVIEW_MATRIX,
102     UNIFORM_PROJECTION_MATRIX,
103     UNIFORM_MODEL_MATRIX,
104     UNIFORM_VIEW_MATRIX,
105     UNIFORM_NORMAL_MATRIX,
106     UNIFORM_COLOR,
107     UNIFORM_CUSTOM_TEXTURE_COORDS,
108     UNIFORM_SAMPLER,
109     UNIFORM_SAMPLER_RECT,
110     UNIFORM_EFFECT_SAMPLER,
111     UNIFORM_EFFECT_SAMPLER_RECT,
112     UNIFORM_TIME_DELTA,
113     UNIFORM_SAMPLER_OPACITY,
114     UNIFORM_SAMPLER_NORMAL_MAP,
115
116     UNIFORM_TEXT_COLOR,
117     UNIFORM_SMOOTHING,
118     UNIFORM_OUTLINE,
119     UNIFORM_OUTLINE_COLOR,
120     UNIFORM_GLOW,
121     UNIFORM_GLOW_COLOR,
122     UNIFORM_SHADOW,
123     UNIFORM_SHADOW_COLOR,
124     UNIFORM_SHADOW_SMOOTHING,
125     UNIFORM_GRADIENT_COLOR,
126     UNIFORM_GRADIENT_LINE,
127     UNIFORM_INVERSE_TEXT_SIZE,
128
129     UNIFORM_TYPE_LAST
130   };
131
132   /**
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
142    */
143   static Program* New( const Integration::ResourceId& resourceId, Integration::ShaderData* shaderData, Context& context, bool modifiesGeometry );
144
145   /**
146    * Takes this program into use
147    */
148   void Use();
149
150   /**
151    * @return true if this program is used currently
152    */
153   bool IsUsed();
154
155   /**
156    * @param [in] type of the attribute
157    * @return the index of the attribute
158    */
159   GLint GetAttribLocation( AttribType type );
160
161   /**
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
165    */
166   unsigned int RegisterUniform( const std::string& name );
167
168   /**
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
173    */
174   GLint GetUniformLocation( unsigned int uniformIndex );
175
176   /**
177    * Sets the uniform value
178    * @param [in] location of uniform
179    * @param [in] value0 as int
180    */
181   void SetUniform1i( GLint location, GLint value0 );
182
183   /**
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
190    */
191   void SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 );
192
193   /**
194    * Sets the uniform value
195    * @param [in] location of uniform
196    * @param [in] value0 as float
197    */
198   void SetUniform1f( GLint location, GLfloat value0 );
199
200   /**
201    * Sets the uniform value
202    * @param [in] location of uniform
203    * @param [in] value0 as float
204    * @param [in] value1 as float
205    */
206   void SetUniform2f( GLint location, GLfloat value0, GLfloat value1 );
207
208   /**
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
214    */
215   void SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 );
216
217   /**
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
224    */
225   void SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 );
226
227   /**
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
233    */
234   void SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value );
235
236   /**
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
242    */
243   void SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value );
244
245   /**
246    * Needs to be called when GL context is (re)created
247    */
248   void GlContextCreated();
249
250   /**
251    * Needs to be called when GL context is destroyed
252    */
253   void GlContextDestroyed();
254
255   /**
256    * @return true if this program modifies geometry
257    */
258   bool ModifiesGeometry();
259
260   /**
261    * Set the projection matrix that has currently been sent
262    * @param matrix to set
263    */
264   void SetProjectionMatrix( const Matrix* matrix )
265   {
266     mProjectionMatrix = matrix;
267   }
268
269   /**
270    * Get the projection matrix that has currently been sent
271    * @return the matrix that is set
272    */
273   const Matrix* GetProjectionMatrix()
274   {
275     return mProjectionMatrix;
276   }
277
278   /**
279    * Set the projection matrix that has currently been sent
280    * @param matrix to set
281    */
282   void SetViewMatrix( const Matrix* matrix )
283   {
284     mViewMatrix = matrix;
285   }
286
287   /**
288    * Get the projection matrix that has currently been sent
289    * @return the matrix that is set
290    */
291   const Matrix* GetViewMatrix()
292   {
293     return mViewMatrix;
294   }
295
296 private: // Implementation
297
298   /**
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
303    */
304   Program( Integration::ShaderData* shaderData, Context& context, bool modifiesGeometry );
305
306 public:
307
308   /**
309    * Destructor, non virtual as no virtual methods or inheritance
310    */
311   ~Program();
312
313 private:
314
315   // default constructor, not defined
316   Program();
317   // assignment operator, not defined
318   Program& operator=( const Program& );
319
320   /**
321    * Load the shader, from a precompiled binary if available, else from source code
322    */
323   void Load();
324
325   /**
326    * Unload the shader
327    */
328   void Unload();
329
330   /**
331    * Compile the shader
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
336    */
337   bool CompileShader(GLenum shaderType, GLuint& shaderId, const char* src);
338
339   /**
340    * Links the shaders together to create program
341    */
342   void Link();
343
344   /**
345    * Frees the shader programs
346    */
347   void FreeShaders();
348
349   /**
350    * Resets caches
351    */
352   void ResetAttribsUniforms();
353
354 private:  // Data
355
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)
365
366   // location caches
367   GLint mAttribLocations[ ATTRIB_TYPE_LAST ]; ///< attribute location cache
368   std::vector< std::pair< std::string, GLint > > mUniformLocations; ///< uniform location cache
369
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
375 };
376
377 } // namespace Internal
378
379 } // namespace Dali
380
381 #endif // __DALI_INTERNAL_PROGRAM_H__