Merge "Removed some junk from Makefile.am" into devel/master
[platform/core/uifw/dali-core.git] / dali / public-api / shader-effects / shader-effect.h
1 #ifndef __DALI_SHADER_EFFECT_H__
2 #define __DALI_SHADER_EFFECT_H__
3
4 /*
5  * Copyright (c) 2015 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 // INTERNAL INCLUDES
22 #include <dali/public-api/object/handle.h>
23 #include <dali/public-api/object/property-index-ranges.h>
24
25 namespace Dali
26 {
27 /**
28  * @addtogroup dali_core_shader_effects
29  * @{
30  */
31
32 /**
33  * @brief DALI_COMPOSE_SHADER macro provides a convenient way to write shader source code.
34  *
35  * We normally use double quotation marks to write a string such as "Hello World".
36  * However many symbols are needed to add multiple lines of string.
37  * We don't need to write quotation marks using this macro at every line.
38  *
39  * [An example of double quotation marks usage]
40  * const string FRAGMENT_SHADER_SOURCE = \
41  * "  void main()\n"
42  * "  {\n"
43  * "    gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
44  * "  }\n";
45  *
46  * [An example of DALI_COMPOSE_SHADER usage]
47  * const string VERTEX_SHADER_SOURCE = DALI_COMPOSE_SHADER (
48  *   void main()
49  *   {
50  *     gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);
51  *     vTexCoord = aTexCoord;
52  *   }
53  * );
54  */
55 #define DALI_COMPOSE_SHADER(STR) #STR
56
57 class Image;
58 struct Vector2;
59 struct Vector3;
60 struct Vector4;
61
62 namespace Internal DALI_INTERNAL
63 {
64 class ShaderEffect;
65 }
66
67 /**
68  * @deprecated DALi 1.0.47
69  *
70  * @brief Shader effects provide a visual effect for image actors.
71  *
72  * For a Custom shader you can provide the vertex and fragment shader code as strings.
73  * These shader snippets get concatenated with the default attributes and uniforms.
74  * For a vertex shader this part contains the following code:
75  * <pre>
76  * precision highp float;
77  * attribute vec3  aPosition;
78  * attribute vec2  aTexCoord;
79  * uniform   mat4  uMvpMatrix;
80  * uniform   mat4  uModelMatrix;
81  * uniform   mat4  uViewMatrix;
82  * uniform   mat4  uModelView;
83  * uniform   mat3  uNormalMatrix;
84  * uniform   mat4  uProjection;
85  * uniform   vec4  uColor;
86  * varying   vec2  vTexCoord;
87  * </pre>
88  * The custom shader part is expected to output the vertex position and texture coordinate.
89  * A basic custom vertex shader would contain the following code:
90  * <pre>
91  * void main()
92  * {
93  *   gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);
94  *   vTexCoord = aTexCoord;
95  * }
96  * </pre>
97  * For fragment shader the default part for images contains the following code:
98  * <pre>
99  * precision mediump float;
100  * uniform   sampler2D sTexture;
101  * uniform   sampler2D sEffect;
102  * uniform   vec4      uColor;
103  * varying   vec2      vTexCoord;
104  * </pre>
105  * <BR>
106  * <B>
107  * Note: In order for fade and color animations to work, the fragment shader needs to multiply the fragment color
108  * with the uniform color "uColor" of the node
109  * </B>
110  */
111 class DALI_IMPORT_API ShaderEffect : public Handle
112 {
113 public:
114
115   // Default Properties
116   /**
117    * @brief An enumeration of properties belonging to the Path class.
118    * Grid Density defines the spacing of vertex coordinates in world units.
119    * ie a larger actor will have more grids at the same spacing.
120    *
121    *  +---+---+         +---+---+---+
122    *  |   |   |         |   |   |   |
123    *  +---+---+         +---+---+---+
124    *  |   |   |         |   |   |   |
125    *  +---+---+         +---+---+---+
126    *                    |   |   |   |
127    *                    +---+---+---+
128    */
129   struct Property
130   {
131     enum
132     {
133       GRID_DENSITY = DEFAULT_ACTOR_PROPERTY_START_INDEX, ///< name "gridDensity",    type float
134       IMAGE,                                             ///< name "image",          type Map {"filename":"", "loadPolicy":...}
135       PROGRAM,                                           ///< name "program",        type Map {"vertexPrefix":"","fragmentPrefix":"","vertex":"","fragment":""}
136       GEOMETRY_HINTS                                     ///< name "geometryHints",  type int (bitfield) values from enum GeometryHints
137     };
138   };
139
140   static const float DEFAULT_GRID_DENSITY;              ///< The default density is 40 pixels
141
142   /**
143    * @brief Hints for rendering/subdividing geometry.
144    */
145   enum GeometryHints
146   {
147     HINT_NONE           = 0x00,   ///< no hints
148     HINT_GRID_X         = 0x01,   ///< Geometry must be subdivided in X
149     HINT_GRID_Y         = 0x02,   ///< Geometry must be subdivided in Y
150     HINT_GRID           = (HINT_GRID_X | HINT_GRID_Y),
151     HINT_DEPTH_BUFFER   = 0x04,   ///< Needs depth buffering turned on
152     HINT_BLENDING       = 0x08,   ///< Notifies the actor to use blending even if it's fully opaque. Needs actor's blending set to BlendingMode::AUTO
153     HINT_DOESNT_MODIFY_GEOMETRY = 0x10 ///< Notifies that the vertex shader will not change geometry (enables bounding box culling)
154   };
155
156   /**
157    * @brief Coordinate type of the shader uniform.
158    *
159    * Viewport coordinate types will convert from viewport to view space.
160    * Use this coordinate type if your are doing a transformation in view space.
161    * The texture coordinate type converts a value in actor local space to texture coodinates.
162    * This is useful for pixel shaders and accounts for texture atlas.
163    */
164   enum UniformCoordinateType
165   {
166     COORDINATE_TYPE_DEFAULT,           ///< Default, No transformation to be applied
167     COORDINATE_TYPE_VIEWPORT_POSITION, ///< @deprecated Dali 1.1.11 The uniform is a position vector in viewport coordinates that needs to be converted to GL view space coordinates.
168     COORDINATE_TYPE_VIEWPORT_DIRECTION ///< @deprecated Dali 1.1.11 The uniform is a directional vector in viewport coordinates that needs to be converted to GL view space coordinates.
169   };
170
171   /**
172    * @brief Create an empty ShaderEffect.
173    *
174    * This can be initialised with ShaderEffect::New(...)
175    */
176   ShaderEffect();
177
178   /**
179    * @brief Create ShaderEffect.
180    *
181    * @param vertexShader code for the effect. If you pass in an empty string, the default version will be used
182    * @param fragmentShader code for the effect. If you pass in an empty string, the default version will be used
183    * @param hints GeometryHints to define the geometry of the rendered object
184    * @return A handle to a shader effect
185    */
186   static ShaderEffect New( const std::string& vertexShader,
187                            const std::string& fragmentShader,
188                            GeometryHints hints = GeometryHints(HINT_NONE) );
189
190   /**
191    * @brief Create ShaderEffect.
192    * @param vertexShaderPrefix code for the effect. It will be inserted before the default uniforms (ideal for \#defines)
193    * @param vertexShader code for the effect. If you pass in an empty string, the default version will be used
194    * @param fragmentShaderPrefix code for the effect. It will be inserted before the default uniforms (ideal for \#defines)
195    * @param fragmentShader code for the effect. If you pass in an empty string, the default version will be used
196    * @param hints GeometryHints to define the geometry of the rendered object
197    * @return A handle to a shader effect
198    */
199   static ShaderEffect NewWithPrefix(const std::string& vertexShaderPrefix,
200                                     const std::string& vertexShader,
201                                     const std::string& fragmentShaderPrefix,
202                                     const std::string& fragmentShader,
203                                     GeometryHints hints = GeometryHints(HINT_NONE) );
204
205   /**
206    * @brief Downcast an Object handle to ShaderEffect.
207    *
208    * If handle points to a ShaderEffect the downcast produces valid
209    * handle. If not the returned handle is left uninitialized.
210    *
211    * @param[in] handle to An object
212    * @return handle to a ShaderEffect object or an uninitialized handle
213    */
214   static ShaderEffect DownCast( BaseHandle handle );
215
216   /**
217    * @brief Destructor
218    *
219    * This is non-virtual since derived Handle types must not contain data or virtual methods.
220    */
221   ~ShaderEffect();
222
223   /**
224    * @brief Copy constructor
225    *
226    * @param object A reference to a ShaderEffect object
227    */
228   ShaderEffect(const ShaderEffect& object);
229
230   /**
231    * @brief This assignment operator is required for (smart) pointer semantics.
232    *
233    * @param [in] rhs  A reference to the copied handle
234    * @return A reference to this
235    */
236   ShaderEffect& operator=(const ShaderEffect& rhs);
237
238   /**
239    * @brief Sets image for using as effect texture.
240    *
241    * This image texture will be bound to the "sEffect" sampler
242    * so it can be used in fragment shader for effects
243    *
244    * @param[in] image to use as effect texture
245    */
246   void SetEffectImage( Image image );
247
248   /**
249    * @brief Set a uniform value.
250    * This will register a property of type Property::FLOAT; see Object::RegisterProperty() for more details.
251    * If name matches a uniform in the shader source, this value will be uploaded when rendering.
252    * @pre Either the property name is not in use, or a property exists with the correct name & type.
253    * @param name The name of the uniform.
254    * @param value The value to to set.
255    * @param uniformCoordinateType The coordinate type of the uniform.
256    */
257   void SetUniform( const std::string& name,
258                    float value,
259                    UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
260
261   /**
262    * @brief Set a uniform value.
263    *
264    * This will register a property of type Property::VECTOR2; see Object::RegisterProperty() for more details.
265    * If name matches a uniform in the shader source, this value will be uploaded when rendering.
266    * @pre Either the property name is not in use, or a property exists with the correct name & type.
267    * @param name The name of the uniform.
268    * @param value The value to to set.
269    * @param uniformCoordinateType The coordinate type of the uniform.
270    */
271   void SetUniform( const std::string& name,
272                    Vector2 value,
273                    UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
274
275   /**
276    * @brief Set a uniform value.
277    *
278    * This will register a property of type Property::VECTOR3; see Object::RegisterProperty() for more details.
279    * If name matches a uniform in the shader source, this value will be uploaded when rendering.
280    * @pre Either the property name is not in use, or a property exists with the correct name & type.
281    * @param name The name of the uniform.
282    * @param value The value to to set.
283    * @param uniformCoordinateType The coordinate type of the uniform.
284    */
285   void SetUniform( const std::string& name,
286                    Vector3 value,
287                    UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
288
289   /**
290    * @brief Set a uniform value.
291    *
292    * This will register a property of type Property::VECTOR4; see Object::RegisterProperty() for more details.
293    * If name matches a uniform in the shader source, this value will be uploaded when rendering.
294    * @pre Either the property name is not in use, or a property exists with the correct name & type.
295    * @param name The name of the uniform.
296    * @param value The value to to set.
297    * @param uniformCoordinateType The coordinate type of the uniform.
298    */
299   void SetUniform( const std::string& name,
300                    Vector4 value,
301                    UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
302
303   /**
304    * @brief Set a uniform value.
305    *
306    * This will register a property of type Property::MATRIX; see Object::RegisterProperty() for more details.
307    * If name matches a uniform in the shader source, this value will be uploaded when rendering.
308    * @pre Either the property name is not in use, or a property exists with the correct name & type.
309    * @param name The name of the uniform.
310    * @param value The value to to set.
311    * @param uniformCoordinateType The coordinate type of the uniform.
312    */
313   void SetUniform( const std::string& name,
314                    const Matrix& value,
315                    UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
316
317   /**
318    * @brief Set a uniform value.
319    *
320    * This will register a property of type Property::MATRIX3; see Object::RegisterProperty() for more details.
321    * If name matches a uniform in the shader source, this value will be uploaded when rendering.
322    * @pre Either the property name is not in use, or a property exists with the correct name & type.
323    * @param name The name of the uniform.
324    * @param value The value to to set.
325    * @param uniformCoordinateType The coordinate type of the uniform.
326    */
327   void SetUniform( const std::string& name,
328                    const Matrix3& value,
329                    UniformCoordinateType uniformCoordinateType = UniformCoordinateType(COORDINATE_TYPE_DEFAULT) );
330
331 public: // Not intended for application developers
332
333   /**
334    * @brief This constructor is used by Dali New() methods.
335    * @param [in] effect A pointer to a newly allocated Dali resource.
336    */
337   explicit DALI_INTERNAL ShaderEffect(Internal::ShaderEffect* effect);
338 };
339
340 /**
341  * @}
342  */
343 } // namespace Dali
344
345 #endif // __DALI_SHADER_EFFECT_H__