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