Merge "Move shader program creation from update side to render side" into 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) 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 class ProgramCache;
43
44 namespace SceneGraph
45 {
46 class SceneController;
47 class Shader;
48 class NodeDataProvider;
49
50 class RenderInstruction; //for relfection effect
51 } // namespace SceneGraph
52
53 namespace Render
54 {
55 /**
56  * Renderers are used to render meshes
57  * These objects are used during RenderManager::Render(), so properties modified during
58  * the Update must either be double-buffered, or set via a message added to the RenderQueue.
59  */
60 class Renderer : public GlResourceOwner
61 {
62 public:
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, int stencilFunctionReference, int stencilMask, StencilOperation::Type stencilOperationOnFail, StencilOperation::Type stencilOperationOnZFail, StencilOperation::Type stencilOperationOnZPass)
69     : stencilFunctionMask(stencilFunctionMask),
70       stencilFunctionReference(stencilFunctionReference),
71       stencilMask(stencilMask),
72       renderMode(renderMode),
73       stencilFunction(stencilFunction),
74       stencilOperationOnFail(stencilOperationOnFail),
75       stencilOperationOnZFail(stencilOperationOnZFail),
76       stencilOperationOnZPass(stencilOperationOnZPass)
77     {
78     }
79
80     int                    stencilFunctionMask;         ///< The stencil function mask
81     int                    stencilFunctionReference;    ///< The stencil function reference
82     int                    stencilMask;                 ///< The stencil mask
83     RenderMode::Type       renderMode : 4;              ///< The render mode
84     StencilFunction::Type  stencilFunction : 4;         ///< The stencil function
85     StencilOperation::Type stencilOperationOnFail : 4;  ///< The stencil operation for stencil test fail
86     StencilOperation::Type stencilOperationOnZFail : 4; ///< The stencil operation for depth test fail
87     StencilOperation::Type stencilOperationOnZPass : 4; ///< The stencil operation for depth test pass
88   };
89
90   /**
91    * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
92    */
93   void GlContextDestroyed() override;
94
95   /**
96    * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
97    */
98   void GlCleanup() override;
99
100   /**
101    * Create a new renderer instance
102    * @param[in] dataProviders The data providers for the renderer
103    * @param[in] geometry The geometry for the renderer
104    * @param[in] blendingBitmask A bitmask of blending options.
105    * @param[in] blendColor The blend color to pass to GL
106    * @param[in] faceCullingMode The face-culling mode.
107    * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
108    * @param[in] depthWriteMode Depth buffer write mode
109    * @param[in] depthTestMode Depth buffer test mode
110    * @param[in] depthFunction Depth function
111    * @param[in] stencilParameters Struct containing all stencil related options
112    */
113   static Renderer* New(SceneGraph::RenderDataProvider* dataProviders,
114                        Render::Geometry*               geometry,
115                        uint32_t                        blendingBitmask,
116                        const Vector4&                  blendColor,
117                        FaceCullingMode::Type           faceCullingMode,
118                        bool                            preMultipliedAlphaEnabled,
119                        DepthWriteMode::Type            depthWriteMode,
120                        DepthTestMode::Type             depthTestMode,
121                        DepthFunction::Type             depthFunction,
122                        StencilParameters&              stencilParameters);
123
124   /**
125    * Constructor.
126    * @param[in] dataProviders The data providers for the renderer
127    * @param[in] geometry The geometry for the renderer
128    * @param[in] blendingBitmask A bitmask of blending options.
129    * @param[in] blendColor The blend color to pass to GL
130    * @param[in] faceCullingMode The face-culling mode.
131    * @param[in] preMultipliedAlphaEnabled whether alpha is pre-multiplied.
132    * @param[in] depthWriteMode Depth buffer write mode
133    * @param[in] depthTestMode Depth buffer test mode
134    * @param[in] depthFunction Depth function
135    * @param[in] stencilParameters Struct containing all stencil related options
136    */
137   Renderer(SceneGraph::RenderDataProvider* dataProviders,
138            Render::Geometry*               geometry,
139            uint32_t                        blendingBitmask,
140            const Vector4&                  blendColor,
141            FaceCullingMode::Type           faceCullingMode,
142            bool                            preMultipliedAlphaEnabled,
143            DepthWriteMode::Type            depthWriteMode,
144            DepthTestMode::Type             depthTestMode,
145            DepthFunction::Type             depthFunction,
146            StencilParameters&              stencilParameters);
147
148   /**
149    * Change the geometry used by the renderer
150    * @param[in] geometry The new geometry
151    */
152   void SetGeometry(Render::Geometry* geometry);
153
154   void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
155
156   /**
157    * @brief Returns a reference to an array of draw commands
158    * @return Valid array of draw commands (may be empty)
159    */
160   const std::vector<Dali::DevelRenderer::DrawCommand>& GetDrawCommands() const
161   {
162     return mDrawCommands;
163   }
164
165   /**
166    * Second-phase construction.
167    * This is called when the renderer is inside render thread
168    * @param[in] context Context used by the renderer
169    * @param[in] programCache Cache of program objects
170    */
171   void Initialize(Context& context, ProgramCache& programCache);
172
173   /**
174    * Destructor
175    */
176   ~Renderer() override;
177
178   /**
179    * Set the face-culling mode.
180    * @param[in] mode The face-culling mode.
181    */
182   void SetFaceCullingMode(FaceCullingMode::Type mode);
183
184   /**
185    * Set the bitmask for blending options
186    * @param[in] bitmask A bitmask of blending options.
187    */
188   void SetBlendingBitMask(uint32_t bitmask);
189
190   /**
191    * Set the blend color for blending options
192    * @param[in] blendColor The blend color to pass to GL
193    */
194   void SetBlendColor(const Vector4& color);
195
196   /**
197    * Set the first element index to draw by the indexed draw
198    * @param[in] firstElement index of first element to draw
199    */
200   void SetIndexedDrawFirstElement(uint32_t firstElement);
201
202   /**
203    * Set the number of elements to draw by the indexed draw
204    * @param[in] elementsCount number of elements to draw
205    */
206   void SetIndexedDrawElementsCount(uint32_t elementsCount);
207
208   /**
209    * @brief Set whether the Pre-multiplied Alpha Blending is required
210    *
211    * @param[in] preMultipled whether alpha is pre-multiplied.
212    */
213   void EnablePreMultipliedAlpha(bool preMultipled);
214
215   /**
216    * Sets the depth write mode
217    * @param[in] depthWriteMode The depth write mode
218    */
219   void SetDepthWriteMode(DepthWriteMode::Type depthWriteMode);
220
221   /**
222    * Query the Renderer's depth write mode
223    * @return The renderer depth write mode
224    */
225   DepthWriteMode::Type GetDepthWriteMode() const;
226
227   /**
228    * Sets the depth test mode
229    * @param[in] depthTestMode The depth test mode
230    */
231   void SetDepthTestMode(DepthTestMode::Type depthTestMode);
232
233   /**
234    * Query the Renderer's depth test mode
235    * @return The renderer depth test mode
236    */
237   DepthTestMode::Type GetDepthTestMode() const;
238
239   /**
240    * Sets the depth function
241    * @param[in] depthFunction The depth function
242    */
243   void SetDepthFunction(DepthFunction::Type depthFunction);
244
245   /**
246    * Query the Renderer's depth function
247    * @return The renderer depth function
248    */
249   DepthFunction::Type GetDepthFunction() const;
250
251   /**
252    * Sets the render mode
253    * @param[in] renderMode The render mode
254    */
255   void SetRenderMode(RenderMode::Type mode);
256
257   /**
258    * Gets the render mode
259    * @return The render mode
260    */
261   RenderMode::Type GetRenderMode() const;
262
263   /**
264    * Sets the stencil function
265    * @param[in] stencilFunction The stencil function
266    */
267   void SetStencilFunction(StencilFunction::Type stencilFunction);
268
269   /**
270    * Gets the stencil function
271    * @return The stencil function
272    */
273   StencilFunction::Type GetStencilFunction() const;
274
275   /**
276    * Sets the stencil function mask
277    * @param[in] stencilFunctionMask The stencil function mask
278    */
279   void SetStencilFunctionMask(int stencilFunctionMask);
280
281   /**
282    * Gets the stencil function mask
283    * @return The stencil function mask
284    */
285   int GetStencilFunctionMask() const;
286
287   /**
288    * Sets the stencil function reference
289    * @param[in] stencilFunctionReference The stencil function reference
290    */
291   void SetStencilFunctionReference(int stencilFunctionReference);
292
293   /**
294    * Gets the stencil function reference
295    * @return The stencil function reference
296    */
297   int GetStencilFunctionReference() const;
298
299   /**
300    * Sets the stencil mask
301    * @param[in] stencilMask The stencil mask
302    */
303   void SetStencilMask(int stencilMask);
304
305   /**
306    * Gets the stencil mask
307    * @return The stencil mask
308    */
309   int GetStencilMask() const;
310
311   /**
312    * Sets the stencil operation for when the stencil test fails
313    * @param[in] stencilOperationOnFail The stencil operation
314    */
315   void SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail);
316
317   /**
318    * Gets the stencil operation for when the stencil test fails
319    * @return The stencil operation
320    */
321   StencilOperation::Type GetStencilOperationOnFail() const;
322
323   /**
324    * Sets the stencil operation for when the depth test fails
325    * @param[in] stencilOperationOnZFail The stencil operation
326    */
327   void SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail);
328
329   /**
330    * Gets the stencil operation for when the depth test fails
331    * @return The stencil operation
332    */
333   StencilOperation::Type GetStencilOperationOnZFail() const;
334
335   /**
336    * Sets the stencil operation for when the depth test passes
337    * @param[in] stencilOperationOnZPass The stencil operation
338    */
339   void SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass);
340
341   /**
342    * Gets the stencil operation for when the depth test passes
343    * @return The stencil operation
344    */
345   StencilOperation::Type GetStencilOperationOnZPass() const;
346
347   /**
348    * Called to upload during RenderManager::Render().
349    * @param[in] context The context used for uploading
350    */
351   void Upload(Context& context);
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<GLuint>&                                      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, Vector<GLuint>& boundTextures);
444
445 private:
446   OwnerPointer<SceneGraph::RenderDataProvider> mRenderDataProvider;
447
448   Context*          mContext;
449   Render::Geometry* mGeometry;
450
451   ProgramCache* mProgramCache;
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