Merge "Add descriptions and example codes" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-renderer.h
1 #ifndef DALI_INTERNAL_RENDER_RENDERER_H
2 #define DALI_INTERNAL_RENDER_RENDERER_H
3
4 /*
5  * Copyright (c) 2016 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/math/matrix.h>
23 #include <dali/public-api/math/vector4.h>
24 #include <dali/public-api/rendering/texture-set.h>
25 #include <dali/internal/common/blending-options.h>
26 #include <dali/internal/common/message.h>
27 #include <dali/internal/common/type-abstraction-enums.h>
28 #include <dali/internal/event/common/property-input-impl.h>
29 #include <dali/internal/event/effects/shader-declarations.h>
30 #include <dali/internal/render/data-providers/render-data-provider.h>
31 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
32 #include <dali/internal/render/renderers/render-geometry.h>
33 #include <dali/internal/update/manager/prepare-render-instructions.h>
34 #include <dali/integration-api/debug.h>
35
36 namespace Dali
37 {
38
39 namespace Internal
40 {
41 class Context;
42 class Texture;
43 class Program;
44
45 namespace SceneGraph
46 {
47 class SceneController;
48 class Shader;
49 class TextureCache;
50 class NodeDataProvider;
51 }
52
53
54 namespace Render
55 {
56
57 /**
58  * Renderers are used to render meshes
59  * These objects are used during RenderManager::Render(), so properties modified during
60  * the Update must either be double-buffered, or set via a message added to the RenderQueue.
61  */
62 class Renderer : public GlResourceOwner
63 {
64 public:
65
66   /**
67    * @brief Struct to encapsulate stencil parameters required for control of the stencil buffer.
68    */
69   struct StencilParameters
70   {
71     StencilParameters( StencilMode::Type stencilMode, StencilFunction::Type stencilFunction, int stencilFunctionMask,
72                        int stencilFunctionReference, int stencilMask, StencilOperation::Type stencilOperationOnFail,
73                        StencilOperation::Type stencilOperationOnZFail, StencilOperation::Type stencilOperationOnZPass )
74     : stencilFunctionMask      ( stencilFunctionMask      ),
75       stencilFunctionReference ( stencilFunctionReference ),
76       stencilMask              ( stencilMask              ),
77       stencilFunction          ( stencilFunction          ),
78       stencilOperationOnFail   ( stencilOperationOnFail   ),
79       stencilOperationOnZFail  ( stencilOperationOnZFail  ),
80       stencilOperationOnZPass  ( stencilOperationOnZPass  ),
81       stencilMode              ( stencilMode              )
82     {
83     }
84
85     int stencilFunctionMask;                          ///< The stencil function mask
86     int stencilFunctionReference;                     ///< The stencil function reference
87     int stencilMask;                                  ///< The stencil mask
88     StencilFunction::Type stencilFunction:3;          ///< The stencil function
89     StencilOperation::Type stencilOperationOnFail:3;  ///< The stencil operation for stencil test fail
90     StencilOperation::Type stencilOperationOnZFail:3; ///< The stencil operation for depth test fail
91     StencilOperation::Type stencilOperationOnZPass:3; ///< The stencil operation for depth test pass
92     StencilMode::Type stencilMode:2;                  ///< The stencil mode
93   };
94
95   /**
96    * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
97    */
98   void GlContextDestroyed();
99
100   /**
101    * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
102    */
103   void GlCleanup();
104
105   /**
106    * Create a new renderer instance
107    * @param[in] dataProviders The data providers for the renderer
108    * @param[in] geometry The geometry for the renderer
109    * @param[in] blendingBitmask A bitmask of blending options.
110    * @param[in] blendColor The blend color to pass to GL
111    * @param[in] faceCullingMode The face-culling mode.
112    * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
113    * @param[in] depthWriteMode Depth buffer write mode
114    * @param[in] depthTestMode Depth buffer test mode
115    * @param[in] depthFunction Depth function
116    * @param[in] stencilParameters Struct containing all stencil related options
117    * @param[in] writeToColorBuffer Set to True to write to the color buffer
118    */
119   static Renderer* New( SceneGraph::RenderDataProvider* dataProviders,
120                         Render::Geometry* geometry,
121                         unsigned int blendingBitmask,
122                         const Vector4* blendColor,
123                         FaceCullingMode::Type faceCullingMode,
124                         bool preMultipliedAlphaEnabled,
125                         DepthWriteMode::Type depthWriteMode,
126                         DepthTestMode::Type depthTestMode,
127                         DepthFunction::Type depthFunction,
128                         StencilParameters& stencilParameters,
129                         bool writeToColorBuffer );
130
131   /**
132    * Constructor.
133    * @param[in] dataProviders The data providers for the renderer
134    * @param[in] geometry The geometry for the renderer
135    * @param[in] blendingBitmask A bitmask of blending options.
136    * @param[in] blendColor The blend color to pass to GL
137    * @param[in] faceCullingMode The face-culling mode.
138    * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
139    * @param[in] depthWriteMode Depth buffer write mode
140    * @param[in] depthTestMode Depth buffer test mode
141    * @param[in] depthFunction Depth function
142    * @param[in] stencilParameters Struct containing all stencil related options
143    * @param[in] writeToColorBuffer Set to True to write to the color buffer
144    */
145   Renderer( SceneGraph::RenderDataProvider* dataProviders,
146             Render::Geometry* geometry,
147             unsigned int blendingBitmask,
148             const Vector4* blendColor,
149             FaceCullingMode::Type faceCullingMode,
150             bool preMultipliedAlphaEnabled,
151             DepthWriteMode::Type depthWriteMode,
152             DepthTestMode::Type depthTestMode,
153             DepthFunction::Type depthFunction,
154             StencilParameters& stencilParameters,
155             bool writeToColorBuffer );
156
157   /**
158    * Change the data providers of the renderer
159    * @param[in] dataProviders The data providers
160    */
161   void SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProviders );
162
163   /**
164    * Change the geometry used by the renderer
165    * @param[in] geometry The new geometry
166    */
167   void SetGeometry( Render::Geometry* geometry );
168
169   /**
170    * Retrieves the geometry used by the renderer
171    * @return The geometry used by the renderer
172    */
173   Render::Geometry* GetGeometry() const
174   {
175     return mGeometry;
176   }
177
178   /**
179    * Second-phase construction.
180    * This is called when the renderer is inside render thread
181    * @param[in] context Context used by the renderer
182    * @param[in] textureCache The texture cache to use
183    */
184   void Initialize( Context& context, SceneGraph::TextureCache& textureCache );
185
186   /**
187    * Destructor
188    */
189   ~Renderer();
190
191   /**
192    * Set the face-culling mode.
193    * @param[in] mode The face-culling mode.
194    */
195   void SetFaceCullingMode( FaceCullingMode::Type mode );
196
197   /**
198    * Set the bitmask for blending options
199    * @param[in] bitmask A bitmask of blending options.
200    */
201   void SetBlendingBitMask( unsigned int bitmask );
202
203   /**
204    * Set the blend color for blending options
205    * @param[in] blendColor The blend color to pass to GL
206    */
207   void SetBlendColor( const Vector4* color );
208
209   /**
210    * Set the first element index to draw by the indexed draw
211    * @param[in] firstElement index of first element to draw
212    */
213   void SetIndexedDrawFirstElement( size_t firstElement );
214
215   /**
216    * Set the number of elements to draw by the indexed draw
217    * @param[in] elementsCount number of elements to draw
218    */
219   void SetIndexedDrawElementsCount( size_t elementsCount );
220
221   /**
222    * @brief Set whether the Pre-multiplied Alpha Blending is required
223    *
224    * @param[in] preMultipled whether alpha is pre-multiplied.
225    */
226   void EnablePreMultipliedAlpha( bool preMultipled );
227
228   /**
229    * Sets the depth write mode
230    * @param[in] depthWriteMode The depth write mode
231    */
232   void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
233
234   /**
235    * Query the Renderer's depth write mode
236    * @return The renderer depth write mode
237    */
238   DepthWriteMode::Type GetDepthWriteMode() const;
239
240   /**
241    * Sets the depth test mode
242    * @param[in] depthTestMode The depth test mode
243    */
244   void SetDepthTestMode( DepthTestMode::Type depthTestMode );
245
246   /**
247    * Query the Renderer's depth test mode
248    * @return The renderer depth test mode
249    */
250   DepthTestMode::Type GetDepthTestMode() const;
251
252   /**
253    * Sets the depth function
254    * @param[in] depthFunction The depth function
255    */
256   void SetDepthFunction( DepthFunction::Type depthFunction );
257
258   /**
259    * Query the Renderer's depth function
260    * @return The renderer depth function
261    */
262   DepthFunction::Type GetDepthFunction() const;
263
264   /**
265    * Sets the stencil mode
266    * @param[in] stencilMode The stencil function
267    */
268   void SetStencilMode( StencilMode::Type stencilMode );
269
270   /**
271    * Gets the stencil mode
272    * @return The stencil function
273    */
274   StencilMode::Type GetStencilMode() const;
275
276   /**
277    * Sets the stencil function
278    * @param[in] stencilFunction The stencil function
279    */
280   void SetStencilFunction( StencilFunction::Type stencilFunction );
281
282   /**
283    * Gets the stencil function
284    * @return The stencil function
285    */
286   StencilFunction::Type GetStencilFunction() const;
287
288   /**
289    * Sets the stencil function mask
290    * @param[in] stencilFunctionMask The stencil function mask
291    */
292   void SetStencilFunctionMask( int stencilFunctionMask );
293
294   /**
295    * Gets the stencil function mask
296    * @return The stencil function mask
297    */
298   int GetStencilFunctionMask() const;
299
300   /**
301    * Sets the stencil function reference
302    * @param[in] stencilFunctionReference The stencil function reference
303    */
304   void SetStencilFunctionReference( int stencilFunctionReference );
305
306   /**
307    * Gets the stencil function reference
308    * @return The stencil function reference
309    */
310   int GetStencilFunctionReference() const;
311
312   /**
313    * Sets the stencil mask
314    * @param[in] stencilMask The stencil mask
315    */
316   void SetStencilMask( int stencilMask );
317
318   /**
319    * Gets the stencil mask
320    * @return The stencil mask
321    */
322   int GetStencilMask() const;
323
324   /**
325    * Sets the stencil operation for when the stencil test fails
326    * @param[in] stencilOperationOnFail The stencil operation
327    */
328   void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
329
330   /**
331    * Gets the stencil operation for when the stencil test fails
332    * @return The stencil operation
333    */
334   StencilOperation::Type GetStencilOperationOnFail() const;
335
336   /**
337    * Sets the stencil operation for when the depth test fails
338    * @param[in] stencilOperationOnZFail The stencil operation
339    */
340   void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
341
342   /**
343    * Gets the stencil operation for when the depth test fails
344    * @return The stencil operation
345    */
346   StencilOperation::Type GetStencilOperationOnZFail() const;
347
348   /**
349    * Sets the stencil operation for when the depth test passes
350    * @param[in] stencilOperationOnZPass The stencil operation
351    */
352   void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
353
354   /**
355    * Gets the stencil operation for when the depth test passes
356    * @return The stencil operation
357    */
358   StencilOperation::Type GetStencilOperationOnZPass() const;
359
360   /**
361    * Sets whether or not to write to the color buffer
362    * @param[in] writeToColorBuffer True to write to the color buffer
363    */
364   void SetWriteToColorBuffer( bool writeToColorBuffer );
365
366   /**
367    * Gets whether or not to write to the color buffer
368    * @return True to write to the color buffer
369    */
370   bool GetWriteToColorBuffer() const;
371
372   /**
373    * Sets batching mode on the renderer
374    * @param[in] batchingEnabled batching state
375    */
376   void SetBatchingEnabled( bool batchingEnabled );
377
378   /**
379    * Called to render during RenderManager::Render().
380    * @param[in] context The context used for rendering
381    * @param[in] textureCache The texture cache used to get textures
382    * @param[in] bufferIndex The index of the previous update buffer.
383    * @param[in] node The node using this renderer
384    * @param[in] defaultShader in case there is no custom shader
385    * @param[in] modelViewMatrix The model-view matrix.
386    * @param[in] viewMatrix The view matrix.
387    * @param[in] projectionMatrix The projection matrix.
388    * @param[in] size Size of the render item
389    * @param[in] externalGeometry Optional external geometry, if set the original geometry is ignored. If NULL, original geometry will be drawn as normal.
390    * @param[in] blend If true, blending is enabled
391    */
392   void Render( Context& context,
393                SceneGraph::TextureCache& textureCache,
394                BufferIndex bufferIndex,
395                const SceneGraph::NodeDataProvider& node,
396                SceneGraph::Shader& defaultShader,
397                const Matrix& modelMatrix,
398                const Matrix& modelViewMatrix,
399                const Matrix& viewMatrix,
400                const Matrix& projectionMatrix,
401                const Vector3& size,
402                Render::Geometry* externalGeometry,
403                bool blend);
404
405   /**
406    * Write the renderer's sort attributes to the passed in reference
407    *
408    * @param[in] bufferIndex The current update buffer index.
409    * @param[out] sortAttributes
410    */
411   void SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const;
412
413 private:
414
415   struct UniformIndexMap;
416
417   // Undefined
418   Renderer( const Renderer& );
419
420   // Undefined
421   Renderer& operator=( const Renderer& rhs );
422
423   /**
424    * Sets blending options
425    * @param context to use
426    * @param blend Wheter blending should be enabled or not
427    */
428   void SetBlending( Context& context, bool blend );
429
430   /**
431    * Set the uniforms from properties according to the uniform map
432    * @param[in] bufferIndex The index of the previous update buffer.
433    * @param[in] node The node using the renderer
434    * @param[in] size The size of the renderer
435    * @param[in] program The shader program on which to set the uniforms.
436    */
437   void SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program );
438
439   /**
440    * Set the program uniform in the map from the mapped property
441    * @param[in] bufferIndex The index of the previous update buffer.
442    * @param[in] program The shader program
443    * @param[in] map The uniform
444    */
445   void SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map );
446
447   /**
448    * Bind the textures and setup the samplers
449    * @param[in] context The GL context
450    * @param[in] textureCache The texture cache
451    * @param[in] program The shader program
452    * @return False if create or bind failed, true if success.
453    */
454   bool BindTextures( Context& context, SceneGraph::TextureCache& textureCache, Program& program );
455
456 private:
457
458   OwnerPointer< SceneGraph::RenderDataProvider > mRenderDataProvider;
459
460   Context*                     mContext;
461   SceneGraph::TextureCache*    mTextureCache;
462   Render::Geometry*            mGeometry;
463
464   struct UniformIndexMap
465   {
466     unsigned int               uniformIndex;  ///< The index of the cached location in the Program
467     const PropertyInputImpl*   propertyValue;
468   };
469
470   typedef Dali::Vector< UniformIndexMap > UniformIndexMappings;
471   UniformIndexMappings         mUniformIndexMap;
472   Vector<GLint>                mAttributesLocation;
473
474   StencilParameters            mStencilParameters;          ///< Struct containing all stencil related options
475   BlendingOptions              mBlendingOptions;            ///< Blending options including blend color, blend func and blend equation
476
477   size_t                       mIndexedDrawFirstElement;    ///< Offset of first element to draw
478   size_t                       mIndexedDrawElementsCount;   ///< Number of elements to draw
479
480   DepthFunction::Type          mDepthFunction:3;            ///< The depth function
481   FaceCullingMode::Type        mFaceCullingMode:2;          ///< The mode of face culling
482   BlendMode::Type              mBlendMode:2;                ///< The mode of blending
483   DepthWriteMode::Type         mDepthWriteMode:2;           ///< The depth write mode
484   DepthTestMode::Type          mDepthTestMode:2;            ///< The depth test mode
485   bool                         mWriteToColorBuffer:1;       ///< True if we are writing to the color buffer
486   bool                         mUpdateAttributesLocation:1; ///< Indicates attribute locations have changed
487   bool                         mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
488   bool mBatchingEnabled:1;                ///< Flag indicating if the renderer is batchable
489 };
490
491 } // namespace SceneGraph
492
493 } // namespace Internal
494
495 } // namespace Dali
496
497 #endif // DALI_INTERNAL_RENDER_RENDERER_H