Use UniformBufferView memory pool 86/322586/1
authorEunki, Hong <eunkiki.hong@samsung.com>
Fri, 11 Apr 2025 06:14:08 +0000 (15:14 +0900)
committerEunki, Hong <eunkiki.hong@samsung.com>
Fri, 11 Apr 2025 06:44:57 +0000 (15:44 +0900)
Per each Renderer::Render time, we create UniformBufferView the number of uniform blocks,
and destroy at that time.

Since we create & destroy UboView very frequency every frame, we'd better
keep this memory as pool system.

Moreover, let we remove some useless values and operations relative with
UBO View.

Let we make UboView.Write's offset parameter is relative of
View itself's offset, instead of UboBuffer's offset.

It will reduce useless offset getter calling

Change-Id: I583728ec1bd4cf0a1a34f6002a647f4f15a32e12
Signed-off-by: Eunki, Hong <eunkiki.hong@samsung.com>
dali/internal/common/memory-pool-object-allocator.h
dali/internal/render/common/render-manager.cpp
dali/internal/render/renderers/render-renderer.cpp
dali/internal/render/renderers/render-uniform-block.cpp
dali/internal/render/renderers/uniform-buffer-manager.cpp
dali/internal/render/renderers/uniform-buffer-view.cpp
dali/internal/render/renderers/uniform-buffer-view.h

index 72ed2f646185514036fb96f2f1aa024064b3861c..3e43f681fc60bccaf078dc46093ff72ee883816c 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTERNAL_MEMORY_POOL_OBJECT_ALLOCATOR_H
 
 /*
- * Copyright (c) 2024 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2025 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.
@@ -19,6 +19,7 @@
  */
 
 // EXTERNAL INCLUDES
+#include <dali/public-api/common/dali-common.h>
 #include <memory> ///< for std::unique_ptr
 
 // INTERNAL INCLUDES
index 0037f3b86573d0bcbc13b50960223c7407eb7c28..42249d5a2db408d193f6804cdd3e5da3f0391620 100644 (file)
@@ -377,6 +377,7 @@ RenderManager::~RenderManager()
   // Ensure to release memory pool
   Render::Renderer::ResetMemoryPool();
   Render::Texture::ResetMemoryPool();
+  Render::UniformBufferView::ResetMemoryPool();
 }
 
 void RenderManager::ContextDestroyed()
index 6db90d0636ed315be97c2a691d426f76c4427e83..4d943a5b32b8dc79302ca2d9586ceb61256dc768 100644 (file)
@@ -937,8 +937,8 @@ void Renderer::WriteUniform(Render::UniformBufferView& ubo, const Graphics::Unif
 template<>
 void Renderer::WriteUniform<Matrix3>(Render::UniformBufferView& ubo, const Graphics::UniformInfo& uniformInfo, const Matrix3& matrix)
 {
-  auto     dst       = ubo.GetOffset() + uniformInfo.offset;
-  uint32_t rowStride = 3 * sizeof(float); // Gles2 standalone buffer is tightly packed
+  const auto dst       = uniformInfo.offset;
+  uint32_t   rowStride = 3 * sizeof(float); // Gles2 standalone buffer is tightly packed
   if(uniformInfo.bufferIndex > 0)
   {
     rowStride = uniformInfo.matrixStride; // Gles3/Vulkan uniform block, mat3 row is padded to vec4
@@ -953,7 +953,7 @@ void Renderer::WriteUniform<Matrix3>(Render::UniformBufferView& ubo, const Graph
 
 void Renderer::WriteUniform(Render::UniformBufferView& ubo, const Graphics::UniformInfo& uniformInfo, const void* data, uint32_t size)
 {
-  ubo.Write(data, size, ubo.GetOffset() + uniformInfo.offset);
+  ubo.Write(data, size, uniformInfo.offset);
 }
 
 void Renderer::FillUniformBuffer(Program&                                                       program,
@@ -1010,9 +1010,7 @@ void Renderer::WriteDynUniform(
     return;
   }
 
-  int        arrayIndex = uniform.arrayIndex;
-  auto       dst        = ubo->GetOffset() + uniform.uniformOffset;
-  const auto dest       = dst + uniform.arrayElementStride * arrayIndex;
+  const auto dest = uniform.uniformOffset + uniform.arrayElementStride * uniform.arrayIndex;
 
   const auto valueAddress = propertyValue->GetValueAddress(updateBufferIndex);
 
index ba947705402c5769e26096fe6b632a8a6c367273..f592bd785963d3a631e62cd9ce22bf8811e04719 100644 (file)
@@ -99,9 +99,7 @@ void UniformBlock::WriteDynUniform(
   UniformBufferView&       ubo,
   BufferIndex              renderBufferIndex)
 {
-  int        arrayIndex = uniform.arrayIndex;
-  auto       dst        = ubo.GetOffset() + uniform.uniformOffset;
-  const auto dest       = dst + uniform.arrayElementStride * arrayIndex;
+  const auto dest = uniform.uniformOffset + uniform.arrayElementStride * uniform.arrayIndex;
 
   const auto valueAddress = propertyValue->GetValueAddress(renderBufferIndex);
 
index f45d18dcd5457c61c67e1d08d9628bd40329cfbc..e9b5b485b58fbed413d4adf319c8d8177dbbb985 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2024 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2025 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.
@@ -75,7 +75,7 @@ Graphics::UniquePtr<UniformBufferView> UniformBufferManager::CreateUniformBuffer
 
   // Use current offset and increment it after
   auto offset = ubo->GetCurrentOffset();
-  auto retval = Graphics::UniquePtr<UniformBufferView>(new UniformBufferView(*ubo.get(), offset, size));
+  auto retval = Graphics::UniquePtr<UniformBufferView>(UniformBufferView::New(*ubo.get(), offset));
 
   // make sure new offset will meet alignment requirements
   uint32_t alignedSize = ubo->AlignSize(size);
index 1b1bf18f4a64680d9efedb5cea9eebb20b8bc648..87fe74ea461734e2aedf827eb8d85636b1daaf0d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2025 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.
 #include <dali/internal/render/renderers/uniform-buffer-view.h>
 
 // INTERNAL INCLUDES
+#include <dali/internal/common/memory-pool-object-allocator.h>
 #include <dali/internal/render/renderers/uniform-buffer.h>
 
 namespace Dali::Internal::Render
 {
-UniformBufferView::UniformBufferView(UniformBufferV2& ubo, uint32_t offset, size_t size)
+namespace
+{
+// Memory pool used to allocate new uniform buffer view. Memory used by this pool will be released when process dies
+Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::Render::UniformBufferView>& GetUboViewMemoryPool()
+{
+  static Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::Render::UniformBufferView> gUboViewMemoryPool;
+  return gUboViewMemoryPool;
+}
+} // namespace
+
+UniformBufferView* UniformBufferView::New(UniformBufferV2& ubo, uint32_t offset)
+{
+  return new(GetUboViewMemoryPool().AllocateRaw()) UniformBufferView(ubo, offset);
+}
+
+void UniformBufferView::ResetMemoryPool()
+{
+  GetUboViewMemoryPool().ResetMemoryPool();
+}
+
+UniformBufferView::UniformBufferView(UniformBufferV2& ubo, uint32_t offset)
 : mUniformBuffer(&ubo),
-  mOffset(offset),
-  mSize(size)
+  mOffset(offset)
 {
 }
 
 UniformBufferView::~UniformBufferView() = default;
 
+void UniformBufferView::operator delete(void* ptr)
+{
+  GetUboViewMemoryPool().Free(static_cast<UniformBufferView*>(ptr));
+}
+
 void UniformBufferView::Write(const void* data, uint32_t size, uint32_t offset)
 {
   // Write into mapped buffer
-  mUniformBuffer->Write(data, size, offset);
+  mUniformBuffer->Write(data, size, offset + mOffset);
 }
 
 Graphics::Buffer* UniformBufferView::GetBuffer() const
index 028e546d62771134c932802960c8cc51952a560f..1f3f90d2f39d4cc4af7e12f22cddcc9e36e6aa88 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTERNAL_UNIFORM_BUFFER_VIEW_H
 
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2025 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.
@@ -48,10 +48,30 @@ class UniformBufferV2;
 class UniformBufferView
 {
 public:
-  UniformBufferView(UniformBufferV2& ubo, uint32_t offset, size_t size);
+  /**
+   * Construct a new UniformBufferView.
+   * @param[in] ubo The buffer for this view
+   * @param[in] offset The offset of this view from ubo
+   * @return A new UniformBufferView
+   */
+  static UniformBufferView* New(UniformBufferV2& ubo, uint32_t offset);
+
+  /**
+   * Clear memory pool of UniformBufferView.
+   * This should be called at the begin of Core.
+   * (Since Core could be recreated, we need to reset the memory pool.)
+   * After this API call, all UniformBufferView classes are invalid.
+   */
+  static void ResetMemoryPool();
 
   ~UniformBufferView();
 
+  /**
+   * Overriden delete operator
+   * Deletes the UniformBufferView from its global memory pool
+   */
+  void operator delete(void* ptr);
+
   /**
    * @brief Writes data into the current uniform buffer view.
    * @note We prefer to call UniformBuffer::ReadyToLockUniformBuffer before call Write API.
@@ -59,20 +79,10 @@ public:
    *
    * @param[in] data pointer to the source data
    * @param[in] size size of source data
-   * @param[in] offset destination offset
+   * @param[in] offset destination offset from the offset of this view
    */
   void Write(const void* data, uint32_t size, uint32_t offset);
 
-  /**
-   * @brief Returns the size of the view
-   *
-   * @return size of view
-   */
-  [[nodiscard]] uint32_t GetSize() const
-  {
-    return mSize;
-  }
-
   /**
    * @brief Returns the offset within the UBO
    * @return Offset
@@ -89,10 +99,15 @@ public:
    */
   [[nodiscard]] Graphics::Buffer* GetBuffer() const;
 
+protected:
+  /**
+   * Protected constructor. See New()
+   */
+  UniformBufferView(UniformBufferV2& ubo, uint32_t offset);
+
 private:
   UniformBufferV2* mUniformBuffer{nullptr}; ///< UniformBuffer that the view views
   uint32_t         mOffset{0u};             ///< Offset within the buffer
-  size_t           mSize{0u};               ///< Size of view
 };
 } // Namespace Internal::Render
 } // Namespace Dali