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