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