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