Converted GPU buffers for geometry to new API
[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    */
351   void Upload();
352
353   /**
354    * Called to render during RenderManager::Render().
355    * @param[in] context The context used for rendering
356    * @param[in] bufferIndex The index of the previous update buffer.
357    * @param[in] node The node using this renderer
358    * @param[in] modelViewMatrix The model-view matrix.
359    * @param[in] viewMatrix The view matrix.
360    * @param[in] projectionMatrix The projection matrix.
361    * @param[in] size Size of the render item
362    * @param[in] blend If true, blending is enabled
363    * @param[in] boundTextures The textures bound for rendering
364    * @param[in] instruction. for use case like reflection where CullFace needs to be adjusted
365
366    */
367   void Render(Context&                                             context,
368               BufferIndex                                          bufferIndex,
369               const SceneGraph::NodeDataProvider&                  node,
370               const Matrix&                                        modelMatrix,
371               const Matrix&                                        modelViewMatrix,
372               const Matrix&                                        viewMatrix,
373               const Matrix&                                        projectionMatrix,
374               const Vector3&                                       size,
375               bool                                                 blend,
376               Vector<Graphics::Texture*>&                          boundTextures,
377               const Dali::Internal::SceneGraph::RenderInstruction& instruction,
378               uint32_t                                             queueIndex);
379
380   /**
381    * Write the renderer's sort attributes to the passed in reference
382    *
383    * @param[in] bufferIndex The current update buffer index.
384    * @param[out] sortAttributes
385    */
386   void SetSortAttributes(BufferIndex bufferIndex, SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes) const;
387
388   /**
389    * Sets the flag indicating whether shader changed.
390    *
391    * @param[in] value True if shader changed
392    */
393   void SetShaderChanged(bool value);
394
395   /**
396    * Check if the renderer attributes/uniforms are updated and returns the flag
397    *
398    * @param[in] bufferIndex The current update buffer index.
399    * @param[in] node The node using this renderer
400    */
401   bool Updated(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider* node);
402
403 private:
404   struct UniformIndexMap;
405
406   // Undefined
407   Renderer(const Renderer&);
408
409   // Undefined
410   Renderer& operator=(const Renderer& rhs);
411
412   /**
413    * Sets blending options
414    * @param context to use
415    * @param blend Wheter blending should be enabled or not
416    */
417   void SetBlending(Context& context, bool blend);
418
419   /**
420    * Set the uniforms from properties according to the uniform map
421    * @param[in] bufferIndex The index of the previous update buffer.
422    * @param[in] node The node using the renderer
423    * @param[in] size The size of the renderer
424    * @param[in] program The shader program on which to set the uniforms.
425    */
426   void SetUniforms(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program);
427
428   /**
429    * Set the program uniform in the map from the mapped property
430    * @param[in] bufferIndex The index of the previous update buffer.
431    * @param[in] program The shader program
432    * @param[in] map The uniform
433    */
434   void SetUniformFromProperty(BufferIndex bufferIndex, Program& program, UniformIndexMap& map);
435
436   /**
437    * Bind the textures and setup the samplers
438    * @param[in] context The GL context
439    * @param[in] program The shader program
440    * @param[in] boundTextures The textures bound for rendering
441    * @return False if create or bind failed, true if success.
442    */
443   bool BindTextures(Context& context, Program& program, Graphics::CommandBuffer& commandBuffer, Vector<Graphics::Texture*>& boundTextures);
444
445 private:
446   Graphics::Controller*                        mGraphicsController;
447   OwnerPointer<SceneGraph::RenderDataProvider> mRenderDataProvider;
448
449   Context*          mContext;
450   Render::Geometry* mGeometry;
451
452   struct UniformIndexMap
453   {
454     uint32_t                 uniformIndex; ///< The index of the cached location in the Program
455     const PropertyInputImpl* propertyValue;
456   };
457
458   using UniformIndexMappings = Dali::Vector<UniformIndexMap>;
459
460   UniformIndexMappings mUniformIndexMap;
461   Vector<GLint>        mAttributesLocation;
462
463   uint64_t mUniformsHash;
464
465   StencilParameters mStencilParameters; ///< Struct containing all stencil related options
466   BlendingOptions   mBlendingOptions;   ///< Blending options including blend color, blend func and blend equation
467
468   uint32_t mIndexedDrawFirstElement;  ///< Offset of first element to draw
469   uint32_t mIndexedDrawElementsCount; ///< Number of elements to draw
470
471   DepthFunction::Type   mDepthFunction : 4;            ///< The depth function
472   FaceCullingMode::Type mFaceCullingMode : 3;          ///< The mode of face culling
473   DepthWriteMode::Type  mDepthWriteMode : 3;           ///< The depth write mode
474   DepthTestMode::Type   mDepthTestMode : 3;            ///< The depth test mode
475   bool                  mUpdateAttributesLocation : 1; ///< Indicates attribute locations have changed
476   bool                  mPremultipledAlphaEnabled : 1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
477   bool                  mShaderChanged : 1;            ///< Flag indicating the shader changed and uniform maps have to be updated
478   bool                  mUpdated : 1;
479
480   std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands; // Devel stuff
481 };
482
483 } // namespace Render
484
485 } // namespace Internal
486
487 } // namespace Dali
488
489 #endif // DALI_INTERNAL_RENDER_RENDERER_H