Purge underscored header file barriers
[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) 2019 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 #include <cstdint> // int32_t, uint32_t
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/common/vector-wrapper.h>
27 #include <dali/public-api/object/ref-object.h>
28 #include <dali/integration-api/gl-abstraction.h>
29 #include <dali/internal/common/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 ProgramCache;
46
47 /**
48  * A program contains a vertex & fragment shader.
49  *
50  * Program caches some of vertex attribute locations and uniform variable values to reduce unnecessary state changes.
51  */
52 class Program
53 {
54 public:
55
56   /**
57    * Size of the uniform cache per program
58    * GLES specification states that minimum uniform count for fragment shader
59    * is 16 and for vertex shader 128. We're caching the 16 common ones for now
60    */
61   static const int32_t MAX_UNIFORM_CACHE_SIZE = 16;
62
63   /**
64    * Constant for uniform / attribute not found
65    */
66   static const int32_t NOT_FOUND = -1;
67
68   /**
69    * Vertex attributes
70    */
71   enum AttribType
72   {
73     ATTRIB_UNKNOWN = -1,
74     ATTRIB_POSITION,
75     ATTRIB_TEXCOORD,
76     ATTRIB_TYPE_LAST
77   };
78
79   /**
80    * Common shader uniform names
81    */
82   enum UniformType
83   {
84     UNIFORM_NOT_QUERIED = -2,
85     UNIFORM_UNKNOWN = -1,
86     UNIFORM_MVP_MATRIX,
87     UNIFORM_MODELVIEW_MATRIX,
88     UNIFORM_PROJECTION_MATRIX,
89     UNIFORM_MODEL_MATRIX,
90     UNIFORM_VIEW_MATRIX,
91     UNIFORM_NORMAL_MATRIX,
92     UNIFORM_COLOR,
93     UNIFORM_SAMPLER,
94     UNIFORM_SAMPLER_RECT,
95     UNIFORM_EFFECT_SAMPLER,
96
97     UNIFORM_SIZE,
98     UNIFORM_TYPE_LAST
99   };
100
101   /**
102    * Creates a new program, or returns a copy of an existing program in the program cache
103    * @param[in] cache where the programs are stored
104    * @param[in] shaderData  A pointer to a data structure containing the program source
105    *                        and optionally precompiled binary. If the binary is empty the program bytecode
106    *                        is copied into it after compilation and linking)
107    * @param[in] modifiesGeometry True if the shader modifies geometry
108    * @return pointer to the program
109    */
110   static Program* New( ProgramCache& cache, Internal::ShaderDataPtr shaderData, bool modifiesGeometry );
111
112   /**
113    * Takes this program into use
114    */
115   void Use();
116
117   /**
118    * @return true if this program is used currently
119    */
120   bool IsUsed();
121
122   /**
123    * @param [in] type of the attribute
124    * @return the index of the attribute
125    */
126   GLint GetAttribLocation( AttribType type );
127
128   /**
129    * Register an attribute name in our local cache
130    * @param [in] name attribute name
131    * @return the index of the attribute name in local cache
132    */
133   uint32_t RegisterCustomAttribute( const std::string& name );
134
135   /**
136    * Gets the location of a pre-registered attribute.
137    * @param [in] attributeIndex of the attribute in local cache
138    * @return the index of the attribute in the GL program
139    */
140   GLint GetCustomAttributeLocation( uint32_t attributeIndex );
141
142   /**
143    * Register a uniform name in our local cache
144    * @param [in] name uniform name
145    * @return the index of the uniform name in local cache
146    */
147   uint32_t RegisterUniform( const std::string& name );
148
149   /**
150    * Gets the location of a pre-registered uniform.
151    * Uniforms in list UniformType are always registered and in the order of the enumeration
152    * @param [in] uniformIndex of the uniform in local cache
153    * @return the index of the uniform in the GL program
154    */
155   GLint GetUniformLocation( uint32_t uniformIndex );
156
157   /**
158    * Introspect the newly loaded shader to get the active sampler locations
159    */
160   void GetActiveSamplerUniforms();
161
162   /**
163    * Gets the uniform location for a sampler
164    * @param [in] index The index of the active sampler
165    * @param [out] location The location of the requested sampler
166    * @return true if the active sampler was found
167    */
168   bool GetSamplerUniformLocation( uint32_t index, GLint& location );
169
170   /**
171    * Get the number of active samplers present in the shader
172    * @return The number of active samplers
173    */
174   uint32_t GetActiveSamplerCount() const;
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    * Special handling for size as we're using uniform geometry so size is passed on to most programs
210    * but it rarely changes so we can cache it
211    * @param [in] location of uniform
212    * @param [in] value0 as float
213    * @param [in] value1 as float
214    * @param [in] value2 as float
215    */
216   void SetSizeUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 );
217
218   /**
219    * Sets the uniform value
220    * @param [in] location of uniform
221    * @param [in] value0 as float
222    * @param [in] value1 as float
223    * @param [in] value2 as float
224    */
225   void SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 );
226
227   /**
228    * Sets the uniform value
229    * @param [in] location of uniform
230    * @param [in] value0 as float
231    * @param [in] value1 as float
232    * @param [in] value2 as float
233    * @param [in] value3 as float
234    */
235   void SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 );
236
237   /**
238    * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
239    * so make sure your matrix is in correct order for GL.
240    * @param [in] location Location of uniform
241    * @param [in] count Count of matrices
242    * @param [in] value values as float pointers
243    */
244   void SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value );
245
246   /**
247    * Sets the uniform value as matrix. NOTE! we never want GPU to transpose
248    * so make sure your matrix is in correct order for GL.
249    * @param [in] location Location of uniform
250    * @param [in] count Count of matrices
251    * @param [in] value values as float pointers
252    */
253   void SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value );
254
255   /**
256    * Needs to be called when GL context is (re)created
257    */
258   void GlContextCreated();
259
260   /**
261    * Needs to be called when GL context is destroyed
262    */
263   void GlContextDestroyed();
264
265   /**
266    * @return true if this program modifies geometry
267    */
268   bool ModifiesGeometry();
269
270   /**
271    * Set the projection matrix that has currently been sent
272    * @param matrix to set
273    */
274   void SetProjectionMatrix( const Matrix* matrix )
275   {
276     mProjectionMatrix = matrix;
277   }
278
279   /**
280    * Get the projection matrix that has currently been sent
281    * @return the matrix that is set
282    */
283   const Matrix* GetProjectionMatrix()
284   {
285     return mProjectionMatrix;
286   }
287
288   /**
289    * Set the projection matrix that has currently been sent
290    * @param matrix to set
291    */
292   void SetViewMatrix( const Matrix* matrix )
293   {
294     mViewMatrix = matrix;
295   }
296
297   /**
298    * Get the projection matrix that has currently been sent
299    * @return the matrix that is set
300    */
301   const Matrix* GetViewMatrix()
302   {
303     return mViewMatrix;
304   }
305
306 private: // Implementation
307
308   /**
309    * Constructor, private so no direct instantiation
310    * @param[in] cache where the programs are stored
311    * @param[in] shaderData A smart pointer to a data structure containing the program source and binary
312    * @param[in] modifiesGeometry True if the vertex shader changes geometry
313    */
314   Program( ProgramCache& cache, Internal::ShaderDataPtr shaderData, bool modifiesGeometry );
315
316 public:
317
318   /**
319    * Destructor, non virtual as no virtual methods or inheritance
320    */
321   ~Program();
322
323 private:
324
325   Program(); ///< default constructor, not defined
326   Program( const Program& ); ///< copy constructor, not defined
327   Program& operator=( const Program& ); ///< assignment operator, not defined
328
329   /**
330    * Load the shader, from a precompiled binary if available, else from source code
331    */
332   void Load();
333
334   /**
335    * Unload the shader
336    */
337   void Unload();
338
339   /**
340    * Compile the shader
341    * @param shaderType vertex or fragment shader
342    * @param shaderId of the shader, returned
343    * @param src of the shader
344    * @return true if the compilation succeeded
345    */
346   bool CompileShader(GLenum shaderType, GLuint& shaderId, const char* src);
347
348   /**
349    * Links the shaders together to create program
350    */
351   void Link();
352
353   /**
354    * Frees the shader programs
355    */
356   void FreeShaders();
357
358   /**
359    * Resets caches
360    */
361   void ResetAttribsUniformCache();
362
363 private:  // Data
364
365   ProgramCache& mCache;                       ///< The program cache
366   Integration::GlAbstraction& mGlAbstraction; ///< The OpenGL Abstraction layer
367   const Matrix* mProjectionMatrix;            ///< currently set projection matrix
368   const Matrix* mViewMatrix;                  ///< currently set view matrix
369   bool mLinked;                               ///< whether the program is linked
370   GLuint mVertexShaderId;                     ///< GL identifier for vertex shader
371   GLuint mFragmentShaderId;                   ///< GL identifier for fragment shader
372   GLuint mProgramId;                          ///< GL identifier for program
373   Internal::ShaderDataPtr mProgramData;       ///< Shader program source and binary (when compiled & linked or loaded)
374
375   // location caches
376   typedef std::pair< std::string, GLint > NameLocationPair;
377   typedef std::vector< NameLocationPair > Locations;
378
379   Locations mAttributeLocations;      ///< attribute location cache
380   Locations mUniformLocations;        ///< uniform location cache
381   std::vector<GLint> mSamplerUniformLocations; ///< sampler uniform location cache
382
383   // uniform value caching
384   GLint mUniformCacheInt[ MAX_UNIFORM_CACHE_SIZE ];         ///< Value cache for uniforms of single int
385   GLfloat mUniformCacheFloat[ MAX_UNIFORM_CACHE_SIZE ];     ///< Value cache for uniforms of single float
386   GLfloat mUniformCacheFloat2[ MAX_UNIFORM_CACHE_SIZE ][2]; ///< Value cache for uniforms of two floats
387   GLfloat mUniformCacheFloat4[ MAX_UNIFORM_CACHE_SIZE ][4]; ///< Value cache for uniforms of four floats
388   Vector3 mSizeUniformCache;                                ///< Cache value for size uniform
389   bool mModifiesGeometry;  ///< True if the program changes geometry
390
391 };
392
393 } // namespace Internal
394
395 } // namespace Dali
396
397 #endif // DALI_INTERNAL_PROGRAM_H