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