Merge "Remove old pipeline caches" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-renderer.h
index 5583fe0..202277f 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTERNAL_RENDER_RENDERER_H
 
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  */
 
 // INTERNAL INCLUDES
+#include <dali/public-api/common/vector-wrapper.h>
 #include <dali/public-api/math/matrix.h>
 #include <dali/public-api/math/vector4.h>
 #include <dali/public-api/rendering/texture-set.h>
+#include <dali/public-api/signals/render-callback.h>
 
 #include <dali/graphics-api/graphics-controller.h>
 #include <dali/integration-api/debug.h>
@@ -55,9 +57,28 @@ namespace Render
 {
 struct ShaderCache;
 class PipelineCache;
+class PipelineCacheL2;
 class UniformBufferManager;
-class PipelineCache;
+class Renderer;
+
+using RendererKey = MemoryPoolKey<Render::Renderer>;
+} //namespace Render
+} //namespace Internal
+
+// Ensure RendererKey can be used in Dali::Vector
+template<>
+struct TypeTraits<Internal::Render::RendererKey> : public BasicTypes<Internal::Render::RendererKey>
+{
+  enum
+  {
+    IS_TRIVIAL_TYPE = true
+  };
+};
 
+namespace Internal
+{
+namespace Render
+{
 /**
  * Renderers are used to render meshes
  * These objects are used during RenderManager::Render(), so properties modified during
@@ -94,6 +115,11 @@ public:
   };
 
   /**
+   * @brief Global static initialize for Render::Renderer before new CommandBuffer's Render fill start.
+   */
+  static void PrepareCommandBuffer();
+
+  /**
    * Create a new renderer instance
    * @param[in] dataProviders The data providers for the renderer
    * @param[in] geometry The geometry for the renderer
@@ -106,16 +132,16 @@ public:
    * @param[in] depthFunction Depth function
    * @param[in] stencilParameters Struct containing all stencil related options
    */
-  static Renderer* New(SceneGraph::RenderDataProvider* dataProviders,
-                       Render::Geometry*               geometry,
-                       uint32_t                        blendingBitmask,
-                       const Vector4&                  blendColor,
-                       FaceCullingMode::Type           faceCullingMode,
-                       bool                            preMultipliedAlphaEnabled,
-                       DepthWriteMode::Type            depthWriteMode,
-                       DepthTestMode::Type             depthTestMode,
-                       DepthFunction::Type             depthFunction,
-                       StencilParameters&              stencilParameters);
+  static RendererKey NewKey(SceneGraph::RenderDataProvider* dataProviders,
+                            Render::Geometry*               geometry,
+                            uint32_t                        blendingBitmask,
+                            const Vector4&                  blendColor,
+                            FaceCullingMode::Type           faceCullingMode,
+                            bool                            preMultipliedAlphaEnabled,
+                            DepthWriteMode::Type            depthWriteMode,
+                            DepthTestMode::Type             depthTestMode,
+                            DepthFunction::Type             depthFunction,
+                            StencilParameters&              stencilParameters);
 
   /**
    * Constructor.
@@ -142,23 +168,6 @@ public:
            StencilParameters&              stencilParameters);
 
   /**
-   * Change the geometry used by the renderer
-   * @param[in] geometry The new geometry
-   */
-  void SetGeometry(Render::Geometry* geometry);
-
-  void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
-
-  /**
-   * @brief Returns a reference to an array of draw commands
-   * @return Valid array of draw commands (may be empty)
-   */
-  [[nodiscard]] const std::vector<Dali::DevelRenderer::DrawCommand>& GetDrawCommands() const
-  {
-    return mDrawCommands;
-  }
-
-  /**
    * Second-phase construction.
    * This is called when the renderer is inside render thread
    * @param[in] graphicsController The graphics controller to use
@@ -179,6 +188,35 @@ public:
   ~Renderer();
 
   /**
+   * Overriden delete operator
+   * Deletes the renderer from its global memory pool
+   */
+  void operator delete(void* ptr);
+
+  /**
+   * Get a pointer to the object from the given key.
+   * Used by MemoryPoolKey to provide pointer semantics.
+   */
+  static Renderer* Get(RendererKey::KeyType rendererKey);
+
+  /**
+   * Change the geometry used by the renderer
+   * @param[in] geometry The new geometry
+   */
+  void SetGeometry(Render::Geometry* geometry);
+
+  void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
+
+  /**
+   * @brief Returns a reference to an array of draw commands
+   * @return Valid array of draw commands (may be empty)
+   */
+  [[nodiscard]] const std::vector<Dali::DevelRenderer::DrawCommand>& GetDrawCommands() const
+  {
+    return mDrawCommands;
+  }
+
+  /**
    * Set the face-culling mode.
    * @param[in] mode The face-culling mode.
    */
@@ -381,6 +419,36 @@ public:
               uint32_t                                             queueIndex);
 
   /**
+   * Sets RenderCallback object
+   *
+   * @param[in] callback Valid pointer to RenderCallback object
+   */
+  void SetRenderCallback(RenderCallback* callback);
+
+  /**
+   * Returns currently set RenderCallback object
+   *
+   * @return Valid pointer to RenderCallback object or nullptr
+   */
+  RenderCallback* GetRenderCallback()
+  {
+    return mRenderCallback;
+  }
+
+  /**
+   * Returns internal RenderCallbackInput structure
+   * @return Valid reference to the RenderCallbackInput
+   */
+  RenderCallbackInput& GetRenderCallbackInput()
+  {
+    if(!mRenderCallbackInput)
+    {
+      mRenderCallbackInput = std::unique_ptr<RenderCallbackInput>(new RenderCallbackInput);
+    }
+    return *mRenderCallbackInput;
+  }
+
+  /**
    * Write the renderer's sort attributes to the passed in reference
    *
    * @param[out] sortAttributes
@@ -398,30 +466,39 @@ public:
    * Check if the renderer attributes/uniforms are updated and returns the flag
    *
    * @param[in] bufferIndex The current update buffer index.
-   * @param[in] node The node using this renderer
    */
-  bool Updated(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider* node);
+  bool Updated(BufferIndex bufferIndex);
 
   template<class T>
-  bool WriteDefaultUniform(const Graphics::UniformInfo*                       uniformInfo,
-                           Render::UniformBufferView&                         ubo,
-                           const T&                                           data);
+  bool WriteDefaultUniform(const Graphics::UniformInfo* uniformInfo,
+                           Render::UniformBufferView&   ubo,
+                           const T&                     data);
 
   template<class T>
-  void WriteUniform(Render::UniformBufferView&                             ubo,
-                    const Graphics::UniformInfo&                       uniformInfo,
-                    const T&                                           data);
+  void WriteUniform(Render::UniformBufferView&   ubo,
+                    const Graphics::UniformInfo& uniformInfo,
+                    const T&                     data);
 
-  void WriteUniform(Render::UniformBufferView&                             ubo,
-                    const Graphics::UniformInfo&                       uniformInfo,
-                    const void*                                        data,
-                    uint32_t                                           size);
+  void WriteUniform(Render::UniformBufferView&   ubo,
+                    const Graphics::UniformInfo& uniformInfo,
+                    const void*                  data,
+                    uint32_t                     size);
 
   [[nodiscard]] FaceCullingMode::Type GetFaceCullMode() const
   {
     return mFaceCullingMode;
   }
 
+  /**
+   * @brief Gets update area after visual properties applied.
+   *
+   * @param[in] bufferIndex The index of the previous update buffer.
+   * @param[in] originalUpdateArea The original update area before apply the visual properties.
+   *
+   * @return The recalculated update area after visual properties applied.
+   */
+  Vector4 GetVisualTransformedUpdateArea(BufferIndex bufferIndex, const Vector4& originalUpdateArea) const noexcept;
+
 private:
   struct UniformIndexMap;
 
@@ -437,8 +514,10 @@ private:
    * @param[in] node The node using the renderer
    * @param[in] size The size of the renderer
    * @param[in] program The shader program on which to set the uniforms.
+   *
+   * @return the index of the node in change counters store / uniform maps store.
    */
-  void BuildUniformIndexMap(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program);
+  std::size_t BuildUniformIndexMap(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program);
 
   /**
    * Bind the textures and setup the samplers
@@ -473,6 +552,7 @@ private:
    * @param[in] size Size of the render item
    * @param[in] blend If true, blending is enabled
    * @param[in] instruction The render instruction
+   * @param[in] The node index
    */
   void WriteUniformBuffer(BufferIndex                          bufferIndex,
                           Graphics::CommandBuffer&             commandBuffer,
@@ -483,7 +563,8 @@ private:
                           const Matrix&                        modelViewMatrix,
                           const Matrix&                        viewMatrix,
                           const Matrix&                        projectionMatrix,
-                          const Vector3&                       size);
+                          const Vector3&                       size,
+                          std::size_t                          nodeIndex);
 
   /**
    * @brief Fill uniform buffer at index. Writes uniforms into given memory address
@@ -499,11 +580,12 @@ private:
                          Render::UniformBufferView&                    ubo,
                          std::vector<Graphics::UniformBufferBinding>*& outBindings,
                          uint32_t&                                     offset,
-                         BufferIndex                                   updateBufferIndex);
+                         BufferIndex                                   updateBufferIndex,
+                         std::size_t                                   nodeIndex);
 
 private:
-  Graphics::Controller*                        mGraphicsController;
-  OwnerPointer<SceneGraph::RenderDataProvider> mRenderDataProvider;
+  Graphics::Controller*           mGraphicsController;
+  SceneGraph::RenderDataProvider* mRenderDataProvider;
 
   Render::Geometry* mGeometry;
 
@@ -514,10 +596,11 @@ private:
   std::vector<Graphics::UniformBufferBinding> mUniformBufferBindings{};
 
   Render::PipelineCache* mPipelineCache{nullptr};
+  PipelineCacheL2*       mPipeline{nullptr};
 
-  using Hash = unsigned long;
+  using Hash = std::size_t;
 
-  typedef const float&(PropertyInputImpl::*FuncGetter )(BufferIndex) const;
+  typedef const float& (PropertyInputImpl::*FuncGetter)(BufferIndex) const;
 
   struct UniformIndexMap
   {
@@ -527,25 +610,10 @@ private:
     Hash                     uniformNameHashNoArray{0u};
     int32_t                  arrayIndex{-1}; ///< The array index
 
-    int16_t                  uniformLocation{0u};
-    uint16_t                 uniformOffset{0u};
-    uint16_t                 uniformSize{0u};
-    FuncGetter               uniformFunc{0};
-  };
-
-  using UniformIndexMappings = Dali::Vector<UniformIndexMap>;
-
-  UniformIndexMappings mUniformIndexMap;
-  uint64_t             mUniformsHash;
-
-  struct HashedPipeline
-  {
-    uint64_t                                mHash{0u};
-    Graphics::UniquePtr<Graphics::Pipeline> mGraphicsPipeline{nullptr};
-    inline static uint64_t                  GetHash(const void* node, const void* instruction, bool blend)
-    {
-      return (reinterpret_cast<uint64_t>(node) << 32) | ((reinterpret_cast<uint64_t>(instruction) & 0xFFFFFFF) << 1) | blend;
-    }
+    int16_t    uniformLocation{0u};
+    uint16_t   uniformOffset{0u};
+    uint16_t   uniformSize{0u};
+    FuncGetter uniformFunc{0};
   };
 
   StencilParameters mStencilParameters; ///< Struct containing all stencil related options
@@ -554,15 +622,30 @@ private:
   uint32_t mIndexedDrawFirstElement;  ///< Offset of first element to draw
   uint32_t mIndexedDrawElementsCount; ///< Number of elements to draw
 
+  /** Struct to map node to index into mNodeMapCounters and mUniformIndexMaps */
+  struct RenderItemLookup
+  {
+    const SceneGraph::NodeDataProvider* node{nullptr}; ///<Node key. It can be nullptr if this NodeIndex don't need node uniform
+
+    std::size_t index{0};                       ///<Index into mUniformIndexMap
+    std::size_t nodeChangeCounter{0};           ///<The last known change counter for this node's uniform map
+    std::size_t renderItemMapChangeCounter{0u}; ///< Change counter of the renderer & shader collected uniform map for this render item (node/renderer pair)
+  };
+  std::vector<RenderItemLookup> mNodeIndexMap; ///< usually only 1 element.
+  using UniformIndexMappings = std::vector<UniformIndexMap>;
+  std::vector<UniformIndexMappings> mUniformIndexMaps; ///< Cached map per node/renderer/shader.
+
   DepthFunction::Type   mDepthFunction : 4;             ///< The depth function
   FaceCullingMode::Type mFaceCullingMode : 3;           ///< The mode of face culling
   DepthWriteMode::Type  mDepthWriteMode : 3;            ///< The depth write mode
   DepthTestMode::Type   mDepthTestMode : 3;             ///< The depth test mode
   bool                  mPremultipliedAlphaEnabled : 1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
   bool                  mShaderChanged : 1;             ///< Flag indicating the shader changed and uniform maps have to be updated
-  bool                  mUpdated : 1;
 
   std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands; // Devel stuff
+  RenderCallback*                               mRenderCallback{nullptr};
+  std::unique_ptr<RenderCallbackInput>          mRenderCallbackInput{nullptr};
+  std::vector<Graphics::Texture*>               mRenderCallbackTextureBindings{};
 };
 
 } // namespace Render