[Bf-blender-cvs] [85ce4882981] master: Realtime Compositor: Implement static cache manager

Omar Emara noreply at git.blender.org
Fri Nov 4 15:15:33 CET 2022


Commit: 85ce4882981943b5a306090f03482bd5397c503d
Author: Omar Emara
Date:   Fri Nov 4 16:14:22 2022 +0200
Branches: master
https://developer.blender.org/rB85ce4882981943b5a306090f03482bd5397c503d

Realtime Compositor: Implement static cache manager

This patch introduces the concept of a Cached Resource that can be
cached across compositor evaluations as well as used by multiple
operations in the same evaluation. Additionally, this patch implements a
new structure for the realtime compositor, the Static Cache Manager,
that manages all the cached resources and deletes them when they are no
longer needed.

This improves responsiveness while adjusting compositor node trees and
also conserves memory usage.

Differential Revision: https://developer.blender.org/D16357

Reviewed By: Clement Foucault

===================================================================

M	source/blender/compositor/realtime_compositor/CMakeLists.txt
M	source/blender/compositor/realtime_compositor/COM_context.hh
A	source/blender/compositor/realtime_compositor/COM_static_cache_manager.hh
A	source/blender/compositor/realtime_compositor/cached_resources/COM_cached_resource.hh
A	source/blender/compositor/realtime_compositor/cached_resources/COM_morphological_distance_feather_weights.hh
A	source/blender/compositor/realtime_compositor/cached_resources/COM_symmetric_blur_weights.hh
A	source/blender/compositor/realtime_compositor/cached_resources/COM_symmetric_separable_blur_weights.hh
A	source/blender/compositor/realtime_compositor/cached_resources/intern/morphological_distance_feather_weights.cc
A	source/blender/compositor/realtime_compositor/cached_resources/intern/symmetric_blur_weights.cc
A	source/blender/compositor/realtime_compositor/cached_resources/intern/symmetric_separable_blur_weights.cc
M	source/blender/compositor/realtime_compositor/intern/context.cc
M	source/blender/compositor/realtime_compositor/intern/evaluator.cc
A	source/blender/compositor/realtime_compositor/intern/static_cache_manager.cc
M	source/blender/draw/CMakeLists.txt
M	source/blender/nodes/composite/CMakeLists.txt
M	source/blender/nodes/composite/nodes/node_composite_blur.cc
M	source/blender/nodes/composite/nodes/node_composite_dilate.cc

===================================================================

diff --git a/source/blender/compositor/realtime_compositor/CMakeLists.txt b/source/blender/compositor/realtime_compositor/CMakeLists.txt
index 2402adcadaf..b4352248b5b 100644
--- a/source/blender/compositor/realtime_compositor/CMakeLists.txt
+++ b/source/blender/compositor/realtime_compositor/CMakeLists.txt
@@ -3,6 +3,7 @@
 set(INC
   .
   algorithms
+  cached_resources
   ../../blenkernel
   ../../blenlib
   ../../gpu
@@ -10,6 +11,7 @@ set(INC
   ../../makesdna
   ../../makesrna
   ../../nodes
+  ../../render
   ../../gpu/intern
   ../../../../intern/guardedalloc
 )
@@ -31,6 +33,7 @@ set(SRC
   intern/shader_node.cc
   intern/shader_operation.cc
   intern/simple_operation.cc
+  intern/static_cache_manager.cc
   intern/static_shader_manager.cc
   intern/texture_pool.cc
   intern/utilities.cc
@@ -51,6 +54,7 @@ set(SRC
   COM_shader_node.hh
   COM_shader_operation.hh
   COM_simple_operation.hh
+  COM_static_cache_manager.hh
   COM_static_shader_manager.hh
   COM_texture_pool.hh
   COM_utilities.hh
@@ -58,12 +62,22 @@ set(SRC
   algorithms/intern/algorithm_parallel_reduction.cc
 
   algorithms/COM_algorithm_parallel_reduction.hh
+
+  cached_resources/intern/morphological_distance_feather_weights.cc
+  cached_resources/intern/symmetric_blur_weights.cc
+  cached_resources/intern/symmetric_separable_blur_weights.cc
+
+  cached_resources/COM_cached_resource.hh
+  cached_resources/COM_morphological_distance_feather_weights.hh
+  cached_resources/COM_symmetric_blur_weights.hh
+  cached_resources/COM_symmetric_separable_blur_weights.hh
 )
 
 set(LIB
   bf_gpu
   bf_nodes
   bf_imbuf
+  bf_render
   bf_blenlib
   bf_blenkernel
 )
diff --git a/source/blender/compositor/realtime_compositor/COM_context.hh b/source/blender/compositor/realtime_compositor/COM_context.hh
index b5c8cea641f..80fb4f70ca4 100644
--- a/source/blender/compositor/realtime_compositor/COM_context.hh
+++ b/source/blender/compositor/realtime_compositor/COM_context.hh
@@ -9,6 +9,7 @@
 
 #include "GPU_texture.h"
 
+#include "COM_static_cache_manager.hh"
 #include "COM_static_shader_manager.hh"
 #include "COM_texture_pool.hh"
 
@@ -22,14 +23,17 @@ namespace blender::realtime_compositor {
  * providing input data like render passes and the active scene, as well as references to the data
  * where the output of the evaluator will be written. The class also provides a reference to the
  * texture pool which should be implemented by the caller and provided during construction.
- * Finally, the class have an instance of a static shader manager for convenient shader
- * acquisition. */
+ * Finally, the class have an instance of a static shader manager and a static resource manager
+ * for acquiring cached shaders and resources efficiently. */
 class Context {
  private:
   /* A texture pool that can be used to allocate textures for the compositor efficiently. */
   TexturePool &texture_pool_;
   /* A static shader manager that can be used to acquire shaders for the compositor efficiently. */
   StaticShaderManager shader_manager_;
+  /* A static cache manager that can be used to acquire cached resources for the compositor
+   * efficiently. */
+  StaticCacheManager cache_manager_;
 
  public:
   Context(TexturePool &texture_pool);
@@ -67,6 +71,9 @@ class Context {
 
   /* Get a reference to the static shader manager of this context. */
   StaticShaderManager &shader_manager();
+
+  /* Get a reference to the static cache manager of this context. */
+  StaticCacheManager &cache_manager();
 };
 
 }  // namespace blender::realtime_compositor
diff --git a/source/blender/compositor/realtime_compositor/COM_static_cache_manager.hh b/source/blender/compositor/realtime_compositor/COM_static_cache_manager.hh
new file mode 100644
index 00000000000..20fbb156879
--- /dev/null
+++ b/source/blender/compositor/realtime_compositor/COM_static_cache_manager.hh
@@ -0,0 +1,77 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#pragma once
+
+#include <memory>
+
+#include "BLI_map.hh"
+#include "BLI_math_vec_types.hh"
+
+#include "COM_morphological_distance_feather_weights.hh"
+#include "COM_symmetric_blur_weights.hh"
+#include "COM_symmetric_separable_blur_weights.hh"
+
+namespace blender::realtime_compositor {
+
+/* -------------------------------------------------------------------------------------------------
+ * Static Cache Manager
+ *
+ * A static cache manager is a collection of cached resources that can be retrieved when needed and
+ * created if not already available. In particular, each cached resource type has its own Map in
+ * the class, where all instances of that cached resource type are stored and tracked. See the
+ * CachedResource class for more information.
+ *
+ * The manager deletes the cached resources that are no longer needed. A cached resource is said to
+ * be not needed when it was not used in the previous evaluation. This is done through the
+ * following mechanism:
+ *
+ * - Before every evaluation, do the following:
+ *     1. All resources whose CachedResource::needed flag is false are deleted.
+ *     2. The CachedResource::needed flag of all remaining resources is set to false.
+ * - During evaluation, when retrieving any cached resource, set its CachedResource::needed flag to
+ *   true.
+ *
+ * In effect, any resource that was used in the previous evaluation but was not used in the current
+ * evaluation will be deleted before the next evaluation. This mechanism is implemented in the
+ * reset() method of the class, which should be called before every evaluation. */
+class StaticCacheManager {
+ private:
+  /* A map that stores all SymmetricBlurWeights cached resources. */
+  Map<SymmetricBlurWeightsKey, std::unique_ptr<SymmetricBlurWeights>> symmetric_blur_weights_;
+
+  /* A map that stores all SymmetricSeparableBlurWeights cached resources. */
+  Map<SymmetricSeparableBlurWeightsKey, std::unique_ptr<SymmetricSeparableBlurWeights>>
+      symmetric_separable_blur_weights_;
+
+  /* A map that stores all MorphologicalDistanceFeatherWeights cached resources. */
+  Map<MorphologicalDistanceFeatherWeightsKey, std::unique_ptr<MorphologicalDistanceFeatherWeights>>
+      morphological_distance_feather_weights_;
+
+ public:
+  /* Reset the cache manager by deleting the cached resources that are no longer needed because
+   * they weren't used in the last evaluation and prepare the remaining cached resources to track
+   * their needed status in the next evaluation. See the class description for more information.
+   * This should be called before every evaluation. */
+  void reset();
+
+  /* Check if there is an available SymmetricBlurWeights cached resource with the given parameters
+   * in the manager, if one exists, return it, otherwise, return a newly created one and add it to
+   * the manager. In both cases, tag the cached resource as needed to keep it cached for the next
+   * evaluation. */
+  SymmetricBlurWeights &get_symmetric_blur_weights(int type, float2 radius);
+
+  /* Check if there is an available SymmetricSeparableBlurWeights cached resource with the given
+   * parameters in the manager, if one exists, return it, otherwise, return a newly created one and
+   * add it to the manager. In both cases, tag the cached resource as needed to keep it cached for
+   * the next evaluation. */
+  SymmetricSeparableBlurWeights &get_symmetric_separable_blur_weights(int type, float radius);
+
+  /* Check if there is an available MorphologicalDistanceFeatherWeights cached resource with the
+   * given parameters in the manager, if one exists, return it, otherwise, return a newly created
+   * one and add it to the manager. In both cases, tag the cached resource as needed to keep it
+   * cached for the next evaluation. */
+  MorphologicalDistanceFeatherWeights &get_morphological_distance_feather_weights(int type,
+                                                                                  int radius);
+};
+
+}  // namespace blender::realtime_compositor
diff --git a/source/blender/compositor/realtime_compositor/cached_resources/COM_cached_resource.hh b/source/blender/compositor/realtime_compositor/cached_resources/COM_cached_resource.hh
new file mode 100644
index 00000000000..fe3158ef52d
--- /dev/null
+++ b/source/blender/compositor/realtime_compositor/cached_resources/COM_cached_resource.hh
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#pragma once
+
+namespace blender::realtime_compositor {
+
+/* -------------------------------------------------------------------------------------------------
+ * Cached Resource.
+ *
+ * A cached resource is any resource that can be cached across compositor evaluations and across
+ * multiple operations. Cached resources are managed by an instance of a StaticCacheManager and are
+ * freed when they are no longer needed, a state which is represented by the `needed` member in the
+ * class. For more information on the caching mechanism, see the StaticCacheManager class.
+ *
+ * To add a new cached resource:
+ *
+ * - Create a derived class from CachedResource to represent the resource.
+ * - Create a key class that can be used in a Map to identify the resource.
+ * - Add a new Map to StaticCacheManager mapping the key to the resource.
+ * - Reset the contents of the added map in StaticCacheManager::reset.
+ * - Add an appropriate getter method in StaticCacheManager.
+ *
+ * See the existing cached resources for reference. */
+class CachedResource {
+ public:
+  /* A flag that represents the needed status of the cached resource. See the StaticCacheManager
+   * class for more information on how this member is utilized in the caching mechanism. */
+  bool needed = true;
+};
+
+}  // namespace blender::realtime_compositor
diff --git a/source/blender/compositor/realtime_compositor/cached_resources/COM_morphological_distance_feather_weights.hh b/source/blender/compositor/realtime_compositor/cached_resources/COM_morphological_distance_feather_weights.hh
new file mode 100644
index 00000000000..cd6827bdd6b
--- /dev/null
+++ b/source/blender/compositor/realtime_compositor/cached_resources/COM_morphological_distance_feather_weights.hh
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#pragma once
+
+#include <cstd

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list