[dali_1.1.39] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / devel-api / rendering / shader.h
1 #ifndef DALI_SHADER_H
2 #define DALI_SHADER_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 // EXTERNAL INCLUDES
22 #include <string> // std::string
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/object/handle.h> // Dali::Handle
26 #include <dali/public-api/object/property-index-ranges.h> // DEFAULT_DERIVED_HANDLE_PROPERTY_START_INDEX
27
28 /**
29  * @brief DALI_COMPOSE_SHADER macro provides a convenient way to write shader source code.
30  *
31  * We normally use double quotation marks to write a string such as "Hello World".
32  * However many symbols are needed to add multiple lines of string.
33  * We don't need to write quotation marks using this macro at every line.
34  *
35  * [An example of double quotation marks usage]
36  * <pre>
37  * const string FRAGMENT_SHADER_SOURCE = \
38  * "  void main()\n"
39  * "  {\n"
40  * "    gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n"
41  * "  }\n";
42  * </pre><br/>
43  * [An example of DALI_COMPOSE_SHADER usage]
44  * <pre>
45  * const string VERTEX_SHADER_SOURCE = DALI_COMPOSE_SHADER (
46  *   void main()
47  *   {
48  *     gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);
49  *     vTexCoord = aTexCoord;
50  *   }
51  * );
52  * </pre>
53  */
54 #define DALI_COMPOSE_SHADER(STR) #STR
55
56 namespace Dali
57 {
58
59 namespace Internal DALI_INTERNAL
60 {
61 class Shader;
62 }
63
64 /**
65  * @brief Shaders allows custom vertex and color transformations in the GPU
66  */
67 class DALI_IMPORT_API Shader : public Handle
68 {
69 public:
70   /**
71    * @brief Hints for rendering/subdividing geometry.
72    */
73   enum ShaderHints
74   {
75     HINT_NONE                     = 0x00, ///< no hints
76     HINT_OUTPUT_IS_TRANSPARENT    = 0x01, ///< Might generate transparent alpha from opaque inputs
77     HINT_MODIFIES_GEOMETRY        = 0x02, ///< Might change position of vertices,
78                                           ///< this option disables any culling optimizations
79   };
80
81   /**
82    * @brief An enumeration of properties belonging to the Shader class.
83    */
84   struct Property
85   {
86     enum
87     {
88       PROGRAM = DEFAULT_OBJECT_PROPERTY_START_INDEX  ///< name "program",      type MAP; {"vertex":"","fragment":"",hints:"",
89                                                      ///                                  "vertexPrefix":"","fragmentPrefix":""}
90     };
91   };
92
93   /**
94    * @brief Create Shader.
95    *
96    * @param vertexShader code for the effect. If you pass in an empty string, the default version will be used
97    * @param fragmentShader code for the effect. If you pass in an empty string, the default version will be used
98    * @param hints GeometryHints to define the geometry of the rendered object
99    * @return A handle to a shader effect
100    */
101   static Shader New( const std::string& vertexShader,
102                      const std::string& fragmentShader,
103                      ShaderHints hints = ShaderHints(HINT_NONE) );
104
105   /**
106    * @brief Default constructor, creates an empty handle
107    */
108   Shader();
109
110   /**
111    * @brief Destructor
112    *
113    * This is non-virtual since derived Handle types must not contain data or virtual methods.
114    */
115   ~Shader();
116
117   /**
118    * @brief Copy constructor
119    *
120    * @param handle A handle to a Shader object
121    */
122   Shader( const Shader& handle );
123
124   /**
125    * @brief Downcast to a shader handle.
126    *
127    * If not a shader the returned shader handle is left uninitialized.
128    * @param[in] handle to an object
129    * @return shader handle or an uninitialized handle
130    */
131   static Shader DownCast( BaseHandle handle );
132
133   /**
134    * @brief Assignment operator, changes this handle to point at the same object
135    *
136    * @param[in] handle Handle to an object
137    * @return Reference to the assigned object
138    */
139   Shader& operator=( const Shader& handle );
140
141 public: // Not intended for application developers
142   /**
143    * @brief This constructor is used by Dali New() methods.
144    * @param [in] effect A pointer to a newly allocated Dali resource.
145    */
146   explicit DALI_INTERNAL Shader( Internal::Shader* effect );
147 };
148
149 } // namespace Dali
150
151 #endif // DALI_SHADER_H