Commit: 9717f4fabd68c94a945bbcf79210a3d6138e6d14
Author: Omar Emara
Date:   Fri Feb 4 18:40:50 2022 +0200
Branches: temp-viewport-compositor-compiler
https://developer.blender.org/rB9717f4fabd68c94a945bbcf79210a3d6138e6d14

Viewport Compositor: Add execute function interface

This patch proposes an initial interface for the compositor execute
function of nodes, as well as an engine implementation for the needed
context functionalities.

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

M       source/blender/blenkernel/BKE_node.h
M       source/blender/draw/engines/compositor/compositor_engine.cc
M       source/blender/nodes/CMakeLists.txt
A       source/blender/nodes/NOD_compositor_execute.hh
M       source/blender/nodes/composite/node_composite_util.hh

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

diff --git a/source/blender/blenkernel/BKE_node.h 
b/source/blender/blenkernel/BKE_node.h
index 16d8ba2e6dd..87e204e1c27 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -116,6 +116,7 @@ typedef struct bNodeSocketTemplate {
 #ifdef __cplusplus
 namespace blender {
 namespace nodes {
+class CompositorContext;
 class NodeMultiFunctionBuilder;
 class GeoNodeExecParams;
 class NodeDeclarationBuilder;
@@ -128,6 +129,7 @@ class MFDataType;
 }  // namespace blender
 
 using CPPTypeHandle = blender::fn::CPPType;
+using NodeCompositorExecuteFunction = void 
(*)(blender::nodes::CompositorContext &context);
 using NodeMultiFunctionBuildFunction = void 
(*)(blender::nodes::NodeMultiFunctionBuilder &builder);
 using NodeGeometryExecFunction = void (*)(blender::nodes::GeoNodeExecParams 
params);
 using NodeDeclareFunction = void (*)(blender::nodes::NodeDeclarationBuilder 
&builder);
@@ -140,6 +142,7 @@ using NodeGatherSocketLinkOperationsFunction =
     void (*)(blender::nodes::GatherLinkSearchOpParams &params);
 
 #else
+typedef void *NodeCompositorExecuteFunction;
 typedef void *NodeMultiFunctionBuildFunction;
 typedef void *NodeGeometryExecFunction;
 typedef void *NodeDeclareFunction;
@@ -322,6 +325,9 @@ typedef struct bNodeType {
   /* gpu */
   NodeGPUExecFunction gpu_fn;
 
+  /* Execute a compositor node. */
+  NodeCompositorExecuteFunction compositor_execute;
+
   /* Build a multi-function for this node. */
   NodeMultiFunctionBuildFunction build_multi_function;
 
diff --git a/source/blender/draw/engines/compositor/compositor_engine.cc 
b/source/blender/draw/engines/compositor/compositor_engine.cc
index c2f5dd62de1..4892204c84b 100644
--- a/source/blender/draw/engines/compositor/compositor_engine.cc
+++ b/source/blender/draw/engines/compositor/compositor_engine.cc
@@ -25,22 +25,76 @@
 #include "DRW_render.h"
 
 #include "BLI_map.hh"
+#include "BLI_string_ref.hh"
 #include "BLI_utildefines.h"
 #include "BLI_vector.hh"
 #include "BLI_vector_set.hh"
 
 #include "DNA_node_types.h"
+#include "DNA_scene_types.h"
+
+#include "GPU_texture.h"
 
 #include "IMB_colormanagement.h"
 
+#include "NOD_compositor_execute.hh"
 #include "NOD_derived_node_tree.hh"
 
 #include "compositor_shader.hh"
 
 namespace blender::compositor {
 
+using nodes::CompositorContext;
 using namespace nodes::derived_node_tree_types;
 
+class DRWCompositorContext : public CompositorContext {
+ private:
+  /* The node currently being executed. */
+  DNode node_;
+  /* A map associating output sockets with the textures storing their 
contents. The map only stores
+   * the textures that were already computed by a dependency node and are 
still needed by one or
+   * more dependent nodes, so the node currently executing can get its inputs 
and outputs from this
+   * member. See get_input_texture and get_output_texture. */
+  const Map<DSocket, GPUTexture *> &allocated_textures_;
+
+ public:
+  DRWCompositorContext(DNode node, const Map<DSocket, GPUTexture *> 
&allocated_textures)
+      : node_(node), allocated_textures_(allocated_textures)
+  {
+  }
+
+  const GPUTexture *get_input_texture(StringRef identifier) override
+  {
+    /* Find the output socket connected to the input with the given input 
identifier and return its
+     * allocated texture. If the input is not linked, return nullptr. */
+    GPUTexture *texture = nullptr;
+    node_.input_by_identifier(identifier).foreach_origin_socket([&](const 
DSocket origin) {
+      texture = allocated_textures_.lookup(origin);
+    });
+    return texture;
+  }
+
+  const GPUTexture *get_output_texture(StringRef identifier) override
+  {
+    return allocated_textures_.lookup(node_.output_by_identifier(identifier));
+  }
+
+  const GPUTexture *get_viewport_texture() override
+  {
+    return DRW_viewport_texture_list_get()->color;
+  }
+
+  const GPUTexture *get_pass_texture(int view_layer, eScenePassType pass_type) 
override
+  {
+    return DRW_render_pass_find(DRW_context_state_get()->scene, view_layer, 
pass_type)->pass_tx;
+  }
+
+  const bNode &node() override
+  {
+    return *node_->bnode();
+  }
+};
+
 class Compiler {
  public:
  private:
diff --git a/source/blender/nodes/CMakeLists.txt 
b/source/blender/nodes/CMakeLists.txt
index c3a41a1650b..ad47ddd0106 100644
--- a/source/blender/nodes/CMakeLists.txt
+++ b/source/blender/nodes/CMakeLists.txt
@@ -72,6 +72,7 @@ set(SRC
 
   NOD_common.h
   NOD_composite.h
+  NOD_compositor_execute.hh
   NOD_derived_node_tree.hh
   NOD_function.h
   NOD_geometry.h
diff --git a/source/blender/nodes/NOD_compositor_execute.hh 
b/source/blender/nodes/NOD_compositor_execute.hh
new file mode 100644
index 00000000000..c76c6aa5772
--- /dev/null
+++ b/source/blender/nodes/NOD_compositor_execute.hh
@@ -0,0 +1,52 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#pragma once
+
+#include "BLI_string_ref.hh"
+
+#include "DNA_node_types.h"
+#include "DNA_scene_types.h"
+
+#include "GPU_texture.h"
+
+namespace blender::nodes {
+
+/* This abstract class is passed to the execution function of the compositor. 
The compositor engine
+ * should implement it to provide the necessary functionality needed by the 
currently execuing
+ * node. */
+class CompositorContext {
+ public:
+  /* Get the input texture for the input socket with the given identifier. 
Returns nullptr if the
+   * socket is not linked. */
+  virtual const GPUTexture *get_input_texture(StringRef identifier) = 0;
+
+  /* Get the output texture for the output socket with the given identifier. */
+  virtual const GPUTexture *get_output_texture(StringRef identifier) = 0;
+
+  /* Get the texture representing the viewport where the result of the 
compositor should be
+   * written. This should be called by output nodes to get their target 
texture. */
+  virtual const GPUTexture *get_viewport_texture() = 0;
+
+  /* Get the texture where the given render pass is stored. This should be 
called by the Render
+   * Layer node to populate its outputs. */
+  virtual const GPUTexture *get_pass_texture(int view_layer, eScenePassType 
pass_type) = 0;
+
+  /* Get the node currently being executed. */
+  virtual const bNode &node() = 0;
+};
+
+}  // namespace blender::nodes
diff --git a/source/blender/nodes/composite/node_composite_util.hh 
b/source/blender/nodes/composite/node_composite_util.hh
index 2a479034263..27a18592ebb 100644
--- a/source/blender/nodes/composite/node_composite_util.hh
+++ b/source/blender/nodes/composite/node_composite_util.hh
@@ -45,6 +45,7 @@
 #include "RE_pipeline.h"
 
 #include "NOD_composite.h"
+#include "NOD_compositor_execute.hh"
 #include "NOD_socket.h"
 #include "NOD_socket_declarations.hh"

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
List details, subscription details or unsubscribe:
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to