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