Revert "[Tizen] Add codes for Dali Windows Backend"
[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) 2018 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:3;              ///< The render mode
86     StencilFunction::Type  stencilFunction:3;         ///< The stencil function
87     StencilOperation::Type stencilOperationOnFail:3;  ///< The stencil operation for stencil test fail
88     StencilOperation::Type stencilOperationOnZFail:3; ///< The stencil operation for depth test fail
89     StencilOperation::Type stencilOperationOnZPass:3; ///< 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                         unsigned int 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             unsigned int 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( unsigned int 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( size_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( size_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 render during RenderManager::Render().
339    * @param[in] context The context used for rendering
340    * @param[in] bufferIndex The index of the previous update buffer.
341    * @param[in] node The node using this renderer
342    * @param[in] modelViewMatrix The model-view matrix.
343    * @param[in] viewMatrix The view matrix.
344    * @param[in] projectionMatrix The projection matrix.
345    * @param[in] size Size of the render item
346    * @param[in] blend If true, blending is enabled
347    */
348   void Render( Context& context,
349                BufferIndex bufferIndex,
350                const SceneGraph::NodeDataProvider& node,
351                const Matrix& modelMatrix,
352                const Matrix& modelViewMatrix,
353                const Matrix& viewMatrix,
354                const Matrix& projectionMatrix,
355                const Vector3& size,
356                bool blend );
357
358   /**
359    * Write the renderer's sort attributes to the passed in reference
360    *
361    * @param[in] bufferIndex The current update buffer index.
362    * @param[out] sortAttributes
363    */
364   void SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes ) const;
365
366 private:
367
368   struct UniformIndexMap;
369
370   // Undefined
371   Renderer( const Renderer& );
372
373   // Undefined
374   Renderer& operator=( const Renderer& rhs );
375
376   /**
377    * Sets blending options
378    * @param context to use
379    * @param blend Wheter blending should be enabled or not
380    */
381   void SetBlending( Context& context, bool blend );
382
383   /**
384    * Set the uniforms from properties according to the uniform map
385    * @param[in] bufferIndex The index of the previous update buffer.
386    * @param[in] node The node using the renderer
387    * @param[in] size The size of the renderer
388    * @param[in] program The shader program on which to set the uniforms.
389    */
390   void SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program );
391
392   /**
393    * Set the program uniform in the map from the mapped property
394    * @param[in] bufferIndex The index of the previous update buffer.
395    * @param[in] program The shader program
396    * @param[in] map The uniform
397    */
398   void SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map );
399
400   /**
401    * Bind the textures and setup the samplers
402    * @param[in] context The GL context
403    * @param[in] program The shader program
404    * @return False if create or bind failed, true if success.
405    */
406   bool BindTextures( Context& context, Program& program );
407
408 private:
409
410   OwnerPointer< SceneGraph::RenderDataProvider > mRenderDataProvider;
411
412   Context*                     mContext;
413   Render::Geometry*            mGeometry;
414
415   struct UniformIndexMap
416   {
417     unsigned int               uniformIndex;                ///< The index of the cached location in the Program
418     const PropertyInputImpl*   propertyValue;
419   };
420
421   typedef Dali::Vector< UniformIndexMap > UniformIndexMappings;
422
423   UniformIndexMappings         mUniformIndexMap;
424   Vector<GLint>                mAttributesLocation;
425
426   StencilParameters            mStencilParameters;          ///< Struct containing all stencil related options
427   BlendingOptions              mBlendingOptions;            ///< Blending options including blend color, blend func and blend equation
428
429   size_t                       mIndexedDrawFirstElement;    ///< Offset of first element to draw
430   size_t                       mIndexedDrawElementsCount;   ///< Number of elements to draw
431
432   DepthFunction::Type          mDepthFunction:3;            ///< The depth function
433   FaceCullingMode::Type        mFaceCullingMode:2;          ///< The mode of face culling
434   DepthWriteMode::Type         mDepthWriteMode:2;           ///< The depth write mode
435   DepthTestMode::Type          mDepthTestMode:2;            ///< The depth test mode
436   bool                         mUpdateAttributesLocation:1; ///< Indicates attribute locations have changed
437   bool                         mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
438
439 };
440
441 } // namespace SceneGraph
442
443 } // namespace Internal
444
445 } // namespace Dali
446
447 #endif // DALI_INTERNAL_RENDER_RENDERER_H