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