[dali_1.9.23] Merge branch '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) 2019 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 NodeDataProvider;
49
50 class RenderInstruction; //for relfection effect
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:4;              ///< The render mode
88     StencilFunction::Type  stencilFunction:4;         ///< The stencil function
89     StencilOperation::Type stencilOperationOnFail:4;  ///< The stencil operation for stencil test fail
90     StencilOperation::Type stencilOperationOnZFail:4; ///< The stencil operation for depth test fail
91     StencilOperation::Type stencilOperationOnZPass:4; ///< 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                         uint32_t 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             uint32_t 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 geometry used by the renderer
154    * @param[in] geometry The new geometry
155    */
156   void SetGeometry( Render::Geometry* geometry );
157
158   void SetDrawCommands( Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size );
159
160   /**
161    * @brief Returns a reference to an array of draw commands
162    * @return Valid array of draw commands (may be empty)
163    */
164   const std::vector<Dali::DevelRenderer::DrawCommand>& GetDrawCommands() const
165   {
166     return mDrawCommands;
167   }
168
169   /**
170    * Second-phase construction.
171    * This is called when the renderer is inside render thread
172    * @param[in] context Context used by the renderer
173    */
174   void Initialize( Context& context );
175
176   /**
177    * Destructor
178    */
179   ~Renderer();
180
181   /**
182    * Set the face-culling mode.
183    * @param[in] mode The face-culling mode.
184    */
185   void SetFaceCullingMode( FaceCullingMode::Type mode );
186
187   /**
188    * Set the bitmask for blending options
189    * @param[in] bitmask A bitmask of blending options.
190    */
191   void SetBlendingBitMask( uint32_t bitmask );
192
193   /**
194    * Set the blend color for blending options
195    * @param[in] blendColor The blend color to pass to GL
196    */
197   void SetBlendColor( const Vector4& color );
198
199   /**
200    * Set the first element index to draw by the indexed draw
201    * @param[in] firstElement index of first element to draw
202    */
203   void SetIndexedDrawFirstElement( uint32_t firstElement );
204
205   /**
206    * Set the number of elements to draw by the indexed draw
207    * @param[in] elementsCount number of elements to draw
208    */
209   void SetIndexedDrawElementsCount( uint32_t elementsCount );
210
211   /**
212    * @brief Set whether the Pre-multiplied Alpha Blending is required
213    *
214    * @param[in] preMultipled whether alpha is pre-multiplied.
215    */
216   void EnablePreMultipliedAlpha( bool preMultipled );
217
218   /**
219    * Sets the depth write mode
220    * @param[in] depthWriteMode The depth write mode
221    */
222   void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
223
224   /**
225    * Query the Renderer's depth write mode
226    * @return The renderer depth write mode
227    */
228   DepthWriteMode::Type GetDepthWriteMode() const;
229
230   /**
231    * Sets the depth test mode
232    * @param[in] depthTestMode The depth test mode
233    */
234   void SetDepthTestMode( DepthTestMode::Type depthTestMode );
235
236   /**
237    * Query the Renderer's depth test mode
238    * @return The renderer depth test mode
239    */
240   DepthTestMode::Type GetDepthTestMode() const;
241
242   /**
243    * Sets the depth function
244    * @param[in] depthFunction The depth function
245    */
246   void SetDepthFunction( DepthFunction::Type depthFunction );
247
248   /**
249    * Query the Renderer's depth function
250    * @return The renderer depth function
251    */
252   DepthFunction::Type GetDepthFunction() const;
253
254   /**
255    * Sets the render mode
256    * @param[in] renderMode The render mode
257    */
258   void SetRenderMode( RenderMode::Type mode );
259
260   /**
261    * Gets the render mode
262    * @return The render mode
263    */
264   RenderMode::Type GetRenderMode() const;
265
266   /**
267    * Sets the stencil function
268    * @param[in] stencilFunction The stencil function
269    */
270   void SetStencilFunction( StencilFunction::Type stencilFunction );
271
272   /**
273    * Gets the stencil function
274    * @return The stencil function
275    */
276   StencilFunction::Type GetStencilFunction() const;
277
278   /**
279    * Sets the stencil function mask
280    * @param[in] stencilFunctionMask The stencil function mask
281    */
282   void SetStencilFunctionMask( int stencilFunctionMask );
283
284   /**
285    * Gets the stencil function mask
286    * @return The stencil function mask
287    */
288   int GetStencilFunctionMask() const;
289
290   /**
291    * Sets the stencil function reference
292    * @param[in] stencilFunctionReference The stencil function reference
293    */
294   void SetStencilFunctionReference( int stencilFunctionReference );
295
296   /**
297    * Gets the stencil function reference
298    * @return The stencil function reference
299    */
300   int GetStencilFunctionReference() const;
301
302   /**
303    * Sets the stencil mask
304    * @param[in] stencilMask The stencil mask
305    */
306   void SetStencilMask( int stencilMask );
307
308   /**
309    * Gets the stencil mask
310    * @return The stencil mask
311    */
312   int GetStencilMask() const;
313
314   /**
315    * Sets the stencil operation for when the stencil test fails
316    * @param[in] stencilOperationOnFail The stencil operation
317    */
318   void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
319
320   /**
321    * Gets the stencil operation for when the stencil test fails
322    * @return The stencil operation
323    */
324   StencilOperation::Type GetStencilOperationOnFail() const;
325
326   /**
327    * Sets the stencil operation for when the depth test fails
328    * @param[in] stencilOperationOnZFail The stencil operation
329    */
330   void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
331
332   /**
333    * Gets the stencil operation for when the depth test fails
334    * @return The stencil operation
335    */
336   StencilOperation::Type GetStencilOperationOnZFail() const;
337
338   /**
339    * Sets the stencil operation for when the depth test passes
340    * @param[in] stencilOperationOnZPass The stencil operation
341    */
342   void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
343
344   /**
345    * Gets the stencil operation for when the depth test passes
346    * @return The stencil operation
347    */
348   StencilOperation::Type GetStencilOperationOnZPass() const;
349
350   /**
351    * Called to upload during RenderManager::Render().
352    * @param[in] context The context used for uploading
353    */
354   void Upload( Context& context );
355
356   /**
357    * Called to render during RenderManager::Render().
358    * @param[in] context The context used for rendering
359    * @param[in] bufferIndex The index of the previous update buffer.
360    * @param[in] node The node using this renderer
361    * @param[in] modelViewMatrix The model-view matrix.
362    * @param[in] viewMatrix The view matrix.
363    * @param[in] projectionMatrix The projection matrix.
364    * @param[in] size Size of the render item
365    * @param[in] blend If true, blending is enabled
366    * @param[in] boundTextures The textures bound for rendering
367    * @param[in] instruction. for use case like reflection where CullFace needs to be adjusted
368
369    */
370   void Render( Context& context,
371                BufferIndex bufferIndex,
372                const SceneGraph::NodeDataProvider& node,
373                const Matrix& modelMatrix,
374                const Matrix& modelViewMatrix,
375                const Matrix& viewMatrix,
376                const Matrix& projectionMatrix,
377                const Vector3& size,
378                bool blend,
379                Vector<GLuint>& boundTextures,
380                const Dali::Internal::SceneGraph::RenderInstruction& instruction,
381                uint32_t queueIndex );
382
383   /**
384    * Write the renderer's sort attributes to the passed in reference
385    *
386    * @param[in] bufferIndex The current update buffer index.
387    * @param[out] sortAttributes
388    */
389   void SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes ) const;
390
391   /**
392    * Sets the flag indicating whether shader changed.
393    *
394    * @param[in] value True if shader changed
395    */
396   void SetShaderChanged( bool value );
397
398   /**
399    * Check if the renderer attributes/uniforms are updated and returns the flag
400    *
401    * @param[in] bufferIndex The current update buffer index.
402    * @param[in] node The node using this renderer
403    */
404   bool Updated(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider* node);
405
406 private:
407
408   struct UniformIndexMap;
409
410   // Undefined
411   Renderer( const Renderer& );
412
413   // Undefined
414   Renderer& operator=( const Renderer& rhs );
415
416   /**
417    * Sets blending options
418    * @param context to use
419    * @param blend Wheter blending should be enabled or not
420    */
421   void SetBlending( Context& context, bool blend );
422
423   /**
424    * Set the uniforms from properties according to the uniform map
425    * @param[in] bufferIndex The index of the previous update buffer.
426    * @param[in] node The node using the renderer
427    * @param[in] size The size of the renderer
428    * @param[in] program The shader program on which to set the uniforms.
429    */
430   void SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program );
431
432   /**
433    * Set the program uniform in the map from the mapped property
434    * @param[in] bufferIndex The index of the previous update buffer.
435    * @param[in] program The shader program
436    * @param[in] map The uniform
437    */
438   void SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map );
439
440   /**
441    * Bind the textures and setup the samplers
442    * @param[in] context The GL context
443    * @param[in] program The shader program
444    * @param[in] boundTextures The textures bound for rendering
445    * @return False if create or bind failed, true if success.
446    */
447   bool BindTextures( Context& context, Program& program, Vector<GLuint>& boundTextures );
448
449 private:
450
451   OwnerPointer< SceneGraph::RenderDataProvider > mRenderDataProvider;
452
453   Context*                     mContext;
454   Render::Geometry*            mGeometry;
455
456   struct UniformIndexMap
457   {
458     uint32_t                   uniformIndex;                ///< The index of the cached location in the Program
459     const PropertyInputImpl*   propertyValue;
460   };
461
462   typedef Dali::Vector< UniformIndexMap > UniformIndexMappings;
463
464   UniformIndexMappings         mUniformIndexMap;
465   Vector<GLint>                mAttributesLocation;
466
467   uint64_t                     mUniformsHash;
468
469   StencilParameters            mStencilParameters;          ///< Struct containing all stencil related options
470   BlendingOptions              mBlendingOptions;            ///< Blending options including blend color, blend func and blend equation
471
472   uint32_t                     mIndexedDrawFirstElement;    ///< Offset of first element to draw
473   uint32_t                     mIndexedDrawElementsCount;   ///< Number of elements to draw
474
475   DepthFunction::Type          mDepthFunction:4;            ///< The depth function
476   FaceCullingMode::Type        mFaceCullingMode:3;          ///< The mode of face culling
477   DepthWriteMode::Type         mDepthWriteMode:3;           ///< The depth write mode
478   DepthTestMode::Type          mDepthTestMode:3;            ///< The depth test mode
479   bool                         mUpdateAttributesLocation:1; ///< Indicates attribute locations have changed
480   bool                         mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
481   bool                         mShaderChanged:1;            ///< Flag indicating the shader changed and uniform maps have to be updated
482   bool                         mUpdated:1;
483
484
485   std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands; // Devel stuff
486 };
487
488 } // namespace SceneGraph
489
490 } // namespace Internal
491
492 } // namespace Dali
493
494 #endif // DALI_INTERNAL_RENDER_RENDERER_H