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