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