[Bf-blender-cvs] [2f1d60a481e] temp-geometry-nodes-evaluator-refactor: improve compute context docs

Jacques Lucke noreply at git.blender.org
Thu Sep 8 17:22:30 CEST 2022


Commit: 2f1d60a481e2099b5c92f4e23a9ab22f94ed07cf
Author: Jacques Lucke
Date:   Thu Sep 8 10:35:43 2022 +0200
Branches: temp-geometry-nodes-evaluator-refactor
https://developer.blender.org/rB2f1d60a481e2099b5c92f4e23a9ab22f94ed07cf

improve compute context docs

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

R056	source/blender/blenkernel/BKE_context_stack.hh	source/blender/blenkernel/BKE_compute_contexts.hh
M	source/blender/blenkernel/CMakeLists.txt
A	source/blender/blenkernel/intern/compute_contexts.cc
D	source/blender/blenkernel/intern/context_stack.cc
A	source/blender/blenlib/BLI_compute_context.hh
D	source/blender/blenlib/BLI_context_stack.hh
M	source/blender/blenlib/CMakeLists.txt
R055	source/blender/blenlib/intern/context_stack.cc	source/blender/blenlib/intern/compute_context.cc
M	source/blender/editors/space_node/node_draw.cc
M	source/blender/editors/space_spreadsheet/spreadsheet_data_source_geometry.cc
M	source/blender/modifiers/intern/MOD_nodes.cc
M	source/blender/nodes/NOD_geometry_exec.hh
M	source/blender/nodes/NOD_geometry_nodes_log.hh
M	source/blender/nodes/NOD_geometry_nodes_to_lazy_function_graph.hh
M	source/blender/nodes/intern/geometry_nodes_log.cc
M	source/blender/nodes/intern/geometry_nodes_to_lazy_function_graph.cc

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

diff --git a/source/blender/blenkernel/BKE_context_stack.hh b/source/blender/blenkernel/BKE_compute_contexts.hh
similarity index 56%
rename from source/blender/blenkernel/BKE_context_stack.hh
rename to source/blender/blenkernel/BKE_compute_contexts.hh
index 1415a5e21b7..bb01aee788e 100644
--- a/source/blender/blenkernel/BKE_context_stack.hh
+++ b/source/blender/blenkernel/BKE_compute_contexts.hh
@@ -2,34 +2,35 @@
 
 #pragma once
 
-#include "BLI_context_stack.hh"
+/**
+ * This file implements some specific compute contexts for concepts in Blender.
+ */
+
+#include "BLI_compute_context.hh"
 
 namespace blender::bke {
 
-class ModifierContextStack : public ContextStack {
+class ModifierComputeContext : public ComputeContext {
  private:
   static constexpr const char *s_static_type = "MODIFIER";
 
   std::string modifier_name_;
 
  public:
-  ModifierContextStack(const ContextStack *parent, std::string modifier_name);
+  ModifierComputeContext(const ComputeContext *parent, std::string modifier_name);
 
  private:
   void print_current_in_line(std::ostream &stream) const override;
 };
 
-class NodeGroupContextStack : public ContextStack {
+class NodeGroupComputeContext : public ComputeContext {
  private:
   static constexpr const char *s_static_type = "NODE_GROUP";
 
   std::string node_name_;
-  std::string debug_group_name_;
 
  public:
-  NodeGroupContextStack(const ContextStack *parent,
-                        std::string node_name,
-                        std::string debug_group_name = "<unknown>");
+  NodeGroupComputeContext(const ComputeContext *parent, std::string node_name);
 
   StringRefNull node_name() const;
 
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 228902f5d02..f8fb609f1c3 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -98,9 +98,9 @@ set(SRC
   intern/collision.c
   intern/colorband.c
   intern/colortools.c
+  intern/compute_contexts.cc
   intern/constraint.c
   intern/context.c
-  intern/context_stack.cc
   intern/crazyspace.cc
   intern/cryptomatte.cc
   intern/curve.cc
@@ -353,9 +353,9 @@ set(SRC
   BKE_collision.h
   BKE_colorband.h
   BKE_colortools.h
+  BKE_compute_contexts.hh
   BKE_constraint.h
   BKE_context.h
-  BKE_context_stack.hh
   BKE_crazyspace.h
   BKE_crazyspace.hh
   BKE_cryptomatte.h
diff --git a/source/blender/blenkernel/intern/compute_contexts.cc b/source/blender/blenkernel/intern/compute_contexts.cc
new file mode 100644
index 00000000000..026706d363e
--- /dev/null
+++ b/source/blender/blenkernel/intern/compute_contexts.cc
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#include "BKE_compute_contexts.hh"
+
+namespace blender::bke {
+
+ModifierComputeContext::ModifierComputeContext(const ComputeContext *parent,
+                                               std::string modifier_name)
+    : ComputeContext(s_static_type, parent), modifier_name_(std::move(modifier_name))
+{
+  hash_.mix_in(s_static_type, strlen(s_static_type));
+  hash_.mix_in(modifier_name_.data(), modifier_name_.size());
+}
+
+void ModifierComputeContext::print_current_in_line(std::ostream &stream) const
+{
+  stream << "Modifier: " << modifier_name_;
+}
+
+NodeGroupComputeContext::NodeGroupComputeContext(const ComputeContext *parent,
+                                                 std::string node_name)
+    : ComputeContext(s_static_type, parent), node_name_(std::move(node_name))
+{
+  hash_.mix_in(s_static_type, strlen(s_static_type));
+  hash_.mix_in(node_name_.data(), node_name_.size());
+}
+
+StringRefNull NodeGroupComputeContext::node_name() const
+{
+  return node_name_;
+}
+
+void NodeGroupComputeContext::print_current_in_line(std::ostream &stream) const
+{
+  stream << "Node: " << node_name_;
+}
+
+}  // namespace blender::bke
diff --git a/source/blender/blenkernel/intern/context_stack.cc b/source/blender/blenkernel/intern/context_stack.cc
deleted file mode 100644
index 3e45bebb52a..00000000000
--- a/source/blender/blenkernel/intern/context_stack.cc
+++ /dev/null
@@ -1,40 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-or-later */
-
-#include "BKE_context_stack.hh"
-
-namespace blender::bke {
-
-ModifierContextStack::ModifierContextStack(const ContextStack *parent, std::string modifier_name)
-    : ContextStack(s_static_type, parent), modifier_name_(std::move(modifier_name))
-{
-  hash_.mix_in(s_static_type, strlen(s_static_type));
-  hash_.mix_in(modifier_name_.data(), modifier_name_.size());
-}
-
-void ModifierContextStack::print_current_in_line(std::ostream &stream) const
-{
-  stream << "Modifier: " << modifier_name_;
-}
-
-NodeGroupContextStack::NodeGroupContextStack(const ContextStack *parent,
-                                             std::string node_name,
-                                             std::string debug_group_name)
-    : ContextStack(s_static_type, parent),
-      node_name_(std::move(node_name)),
-      debug_group_name_(std::move(debug_group_name))
-{
-  hash_.mix_in(s_static_type, strlen(s_static_type));
-  hash_.mix_in(node_name_.data(), node_name_.size());
-}
-
-StringRefNull NodeGroupContextStack::node_name() const
-{
-  return node_name_;
-}
-
-void NodeGroupContextStack::print_current_in_line(std::ostream &stream) const
-{
-  stream << "Node Group: " << debug_group_name_ << " \t Node Name: " << node_name_;
-}
-
-}  // namespace blender::bke
diff --git a/source/blender/blenlib/BLI_compute_context.hh b/source/blender/blenlib/BLI_compute_context.hh
new file mode 100644
index 00000000000..fc6858ad6a9
--- /dev/null
+++ b/source/blender/blenlib/BLI_compute_context.hh
@@ -0,0 +1,173 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+#pragma once
+
+/** \file
+ * \ingroup bli
+ *
+ * When logging computed values, we generally want to know where the value was computed. For
+ * example, geometry nodes logs socket values so that they can be displayed in the ui. For that we
+ * can combine the logged value with a `ComputeContext`, which identifies the place where the value
+ * was computed.
+ *
+ * This is not a trivial problem because e.g. just storing storing a pointer to the socket a value
+ * belongs to is not enough. That's because the same socket may correspond to many different values
+ * when the socket is used in a node group that is used multiple times. In this case, not only does
+ * the socket has to be stored but also the entire nested node group path that led to the
+ * evaluation of the socket.
+ *
+ * Storing the entire "context path" for every logged value is not feasible, because that path can
+ * become quite long. So that would need much more memory, more compute overhead and makes it
+ * complicated to compare if two contexts are the same. If the identifier for a compute context
+ * would have a variable size, it would also be much harder to create a map from context to values.
+ *
+ * The solution implemented below uses the following key ideas:
+ * - Every compute context can be hashed to a unique fixed size value (`ComputeContextHash`). While
+ *   technically there could be hash collisions, the hashing algorithm has to be chosen to make
+ *   that practically impossible. This way an entire context path, possibly consisting of many
+ *   nested contexts, is represented by a single value that can be stored easily.
+ * - A nested compute context is build as singly linked list, where every compute context has a
+ *   pointer to the parent compute context. Note that a link in the other direction is not possible
+ *   because the same parent compute context may be used by many different children which possibly
+ *   run on different threads.
+ */
+
+#include "BLI_array.hh"
+#include "BLI_linear_allocator.hh"
+#include "BLI_stack.hh"
+#include "BLI_string_ref.hh"
+
+namespace blender {
+
+/**
+ * A hash that uniquely identifies a specific (non-fixed-size) compute context. The hash has to
+ * have enough bits to make collisions practically impossible.
+ */
+struct ComputeContextHash {
+  static constexpr int64_t HashSizeInBytes = 16;
+  uint64_t v1 = 0;
+  uint64_t v2 = 0;
+
+  uint64_t hash() const
+  {
+    return v1;
+  }
+
+  friend bool operator==(const ComputeContextHash &a, const ComputeContextHash &b)
+  {
+    return a.v1 == b.v1 && a.v2 == b.v2;
+  }
+
+  void mix_in(const void *data, int64_t len);
+
+  friend std::ostream &operator<<(std::ostream &stream, const ComputeContextHash &hash);
+};
+
+static_assert(sizeof(ComputeContextHash) == ComputeContextHash::HashSizeInBytes);
+
+/**
+ * Identifies the context in which a computation happens. This context can be used to identify
+ * values logged during the computation. For more details, see the comment at the top of the file.
+ *
+ * This class should be subclassed to implement specific contexts.
+ */
+class ComputeContext {
+ private:
+  /**
+   * Only used for debugging currently.
+   */
+  const char *static_type_;
+  /**
+   * Pointer to the context that this context is child of. That allows nesting compute contexts.
+   */
+  const ComputeContext *parent_ = nullptr;
+
+ protected:
+  /**
+   * The hash that uniquely identifies this context. It's a combined hash of this context as well
+   * as all the parent contexts.
+   */
+  ComputeContextHash hash_;
+
+ public:
+  ComputeContext(const char *static_type, const ComputeContext *parent)
+      : static_type_(static_type), parent_(parent)
+  {
+    if (parent != nullptr) {
+      hash_ = parent_->hash_;
+    }
+  }
+  virtual ~ComputeContext() = default;
+
+  const ComputeContextHash &hash() const
+  {
+    return hash_;
+  }
+
+  const char *static_type() const
+  {
+    return static_type_;
+  }
+
+  const ComputeContext *parent() const
+  {
+    return parent_;
+  }
+
+  /**
+   * Print the entire nested context stack.
+   */
+  void print_stack(std::ostream &stream, StringRef name) const;
+
+  /**
+   * Print information about this specific context. This has to be implemented by each subclass.
+   */
+  virtual void print_current_in_line(std::ostream &stream) const = 0;
+
+  friend std::ostream &operator<<(std::ostream &stream, const ComputeContext &compute_context);
+};
+
+/**
+ * Utility class to build a context stack in one place. T

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list