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