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