Avoid creating unnecessary strings for built in shader uniforms
[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 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
10 //
11 //     http://floralicense.org/license/
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 // EXTERNAL INCLUDES
21 #include <string>
22
23 // INTERNAL INCLUDES
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/internal/render/gl-resources/context-observer.h>
28 #include <dali/integration-api/resource-cache.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 Context;
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 : public ContextObserver
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_TYPE_LAST
128   };
129
130   /**
131    * Creates a new program, or returns a copy of an existing program in the program cache
132    * maintained by Context
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    * @return pointer to the program
140    */
141   static Program* New( const Integration::ResourceId& resourceId, Integration::ShaderData* shaderData, Context& context );
142
143   /**
144    * Takes this program into use
145    */
146   void Use();
147
148   /**
149    * @return true if this program is used currently
150    */
151   bool IsUsed();
152
153   /**
154    * @param [in] type of the attribute
155    * @return the index of the attribute
156    */
157   GLint GetAttribLocation( AttribType type );
158
159   /**
160    * Register a uniform name in our local cache
161    * @param [in] name uniform name
162    * @return the index of the uniform name in local cache
163    */
164   unsigned int RegisterExternalUniform( const std::string& name );
165
166   /**
167    * Register a uniform name in our local cache
168    * @param [in] name uniform name
169    * @return the index of the uniform name in local cache
170    */
171   unsigned int RegisterUniform( const char* name );
172
173   /**
174    * Gets the location of a pre-registered uniform.
175    * Uniforms in list UniformType are always registered and in the order of the enumeration
176    * @param [in] uniformIndex of the uniform in local cache
177    * @return the index of the uniform in the GL program
178    */
179   GLint GetUniformLocation( unsigned int uniformIndex );
180
181   /**
182    * Sets the uniform value
183    * @param [in] location of uniform
184    * @param [in] value0 as int
185    */
186   void SetUniform1i( GLint location, GLint value0 );
187
188   /**
189    * Sets the uniform value
190    * @param [in] location of uniform
191    * @param [in] value0 as int
192    * @param [in] value1 as int
193    * @param [in] value2 as int
194    * @param [in] value3 as int
195    */
196   void SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 );
197
198   /**
199    * Sets the uniform value
200    * @param [in] location of uniform
201    * @param [in] value0 as float
202    */
203   void SetUniform1f( GLint location, GLfloat value0 );
204
205   /**
206    * Sets the uniform value
207    * @param [in] location of uniform
208    * @param [in] value0 as float
209    * @param [in] value1 as float
210    */
211   void SetUniform2f( GLint location, GLfloat value0, GLfloat value1 );
212
213   /**
214    * Sets the uniform value
215    * @param [in] location of uniform
216    * @param [in] value0 as float
217    * @param [in] value1 as float
218    * @param [in] value2 as float
219    */
220   void SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 );
221
222   /**
223    * Sets the uniform value
224    * @param [in] location of uniform
225    * @param [in] value0 as float
226    * @param [in] value1 as float
227    * @param [in] value2 as float
228    * @param [in] value3 as float
229    */
230   void SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 );
231
232   /**
233    * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
234    * so make sure your matrix is in correct order for GL.
235    * @param [in] location Location of uniform
236    * @param [in] count Count of matrices
237    * @param [in] value values as float pointers
238    */
239   void SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value );
240
241   /**
242    * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
243    * so make sure your matrix is in correct order for GL.
244    * @param [in] location Location of uniform
245    * @param [in] count Count of matrices
246    * @param [in] value values as float pointers
247    */
248   void SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value );
249
250 public: // From ContextObserver
251
252   /**
253    * @copydoc ContextObserver::GlContextCreated
254    */
255   virtual void GlContextCreated();          // From ContextObserver
256
257   /**
258    * @copydoc ContextObserver::GlContextToBeDestroyed
259    */
260   virtual void GlContextToBeDestroyed();   // From ContextObserver
261
262 private: // Implementation
263
264   /**
265    * Constructor, private so no direct instantiation
266    * @param[in] shaderData A pointer to a data structure containing the program source and binary
267    * @param[in] context    The GL context state cache.
268    */
269   Program( Integration::ShaderData* shaderData, Context& context );
270
271 public:
272
273   /**
274    * Destructor
275    */
276   virtual ~Program();
277
278 private:
279
280   // default constructor, not defined
281   Program();
282   // assignment operator, not defined
283   Program& operator=( const Program& );
284
285   /**
286    * Load the shader, from a precompiled binary if available, else from source code
287    */
288   void Load();
289
290   /**
291    * Unload the shader
292    */
293   void Unload();
294
295   /**
296    * Compile the shader
297    * @param shaderType vertex or fragment shader
298    * @param shaderId of the shader, returned
299    * @param src of the shader
300    * @return true if the compilation succeeded
301    */
302   bool CompileShader(GLenum shaderType, GLuint& shaderId, const char* src);
303
304   /**
305    * Links the shaders together to create program
306    */
307   void Link();
308
309   /**
310    * Frees the shader programs
311    */
312   void FreeShaders();
313
314   /**
315    * Resets caches
316    */
317   void ResetAttribsUniforms();
318
319 private:  // Data
320
321   Context& mContext;                          ///< The GL context state cache
322   Integration::GlAbstraction& mGlAbstraction; ///< The OpenGL Abstraction layer
323   bool mLinked;                               ///< whether the program is linked
324   GLuint mVertexShaderId;                     ///< GL identifier for vertex shader
325   GLuint mFragmentShaderId;                   ///< GL identifier for fragment shader
326   GLuint mProgramId;                          ///< GL identifier for program
327   Integration::ShaderData* mProgramData;      ///< Shader program source and binary (when compiled & linked or loaded)
328
329   // uniform location caches
330   GLint mAttribLocations[ ATTRIB_TYPE_LAST ]; ///< attribute location cache
331   GLint mBuiltinUniformLocations[ UNIFORM_TYPE_LAST ];  ///< uniform location cache for built in uniforms
332   std::vector< std::pair< const char *, GLint > > mCustomUniformLocations; ///< uniform location cache for custom uniforms
333   static std::set< std::string > mExternalUniformNames; /// < names for externally specified uniforms, set to avoid duplicates
334
335   // uniform value caching
336   GLint mUniformCacheInt[ MAX_UNIFORM_CACHE_SIZE ];         ///< Value cache for uniforms of single int
337   GLfloat mUniformCacheFloat[ MAX_UNIFORM_CACHE_SIZE ];     ///< Value cache for uniforms of single float
338   GLfloat mUniformCacheFloat4[ MAX_UNIFORM_CACHE_SIZE ][4]; ///< Value cache for uniforms of four float
339
340 };
341
342 } // namespace Internal
343
344 } // namespace Dali
345
346 #endif // __DALI_INTERNAL_PROGRAM_H__