[Bf-blender-cvs] [09bebf1fedb] temp-viewport-compositor-compiler: Viewport Compositor: Implement Split Viewer node

Omar Emara noreply at git.blender.org
Thu Mar 31 11:09:45 CEST 2022


Commit: 09bebf1fedb2d1ef23c0e9bf027e4ef163c9973f
Author: Omar Emara
Date:   Thu Mar 31 11:08:05 2022 +0200
Branches: temp-viewport-compositor-compiler
https://developer.blender.org/rB09bebf1fedb2d1ef23c0e9bf027e4ef163c9973f

Viewport Compositor: Implement Split Viewer node

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

M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/GPU_shader.h
M	source/blender/gpu/intern/gpu_shader.cc
A	source/blender/gpu/shaders/compositor/compositor_split_viewer.glsl
A	source/blender/gpu/shaders/compositor/infos/compositor_split_viewer_info.hh
M	source/blender/nodes/composite/nodes/node_composite_split_viewer.cc

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

diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index 427f7449cde..349c5e57f34 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -311,6 +311,7 @@ set(GLSL_SRC
 
   shaders/compositor/compositor_convert.glsl
   shaders/compositor/compositor_realize_on_domain.glsl
+  shaders/compositor/compositor_split_viewer.glsl
 
   shaders/material/gpu_shader_material_add_shader.glsl
   shaders/material/gpu_shader_material_ambient_occlusion.glsl
@@ -495,6 +496,7 @@ set(SHADER_CREATE_INFOS
 
   shaders/compositor/infos/compositor_convert_info.hh
   shaders/compositor/infos/compositor_realize_on_domain_info.hh
+  shaders/compositor/infos/compositor_split_viewer_info.hh
 )
 
 set(SHADER_CREATE_INFOS_CONTENT "")
diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h
index 0b5f5fd8242..5d2292fdf02 100644
--- a/source/blender/gpu/GPU_shader.h
+++ b/source/blender/gpu/GPU_shader.h
@@ -192,6 +192,7 @@ void GPU_shader_uniform_float(GPUShader *shader, int location, float value);
 void GPU_shader_uniform_int(GPUShader *shader, int location, int value);
 
 void GPU_shader_uniform_1i(GPUShader *sh, const char *name, int value);
+void GPU_shader_uniform_2iv(GPUShader *sh, const char *name, const int data[2]);
 void GPU_shader_uniform_1b(GPUShader *sh, const char *name, bool value);
 void GPU_shader_uniform_1f(GPUShader *sh, const char *name, float value);
 void GPU_shader_uniform_2f(GPUShader *sh, const char *name, float x, float y);
diff --git a/source/blender/gpu/intern/gpu_shader.cc b/source/blender/gpu/intern/gpu_shader.cc
index 0e7cbbcda44..e80c8f41c51 100644
--- a/source/blender/gpu/intern/gpu_shader.cc
+++ b/source/blender/gpu/intern/gpu_shader.cc
@@ -672,6 +672,12 @@ void GPU_shader_uniform_1i(GPUShader *sh, const char *name, int value)
   GPU_shader_uniform_int(sh, loc, value);
 }
 
+void GPU_shader_uniform_2iv(GPUShader *sh, const char *name, const int data[2])
+{
+  const int loc = GPU_shader_get_uniform(sh, name);
+  GPU_shader_uniform_vector_int(sh, loc, 2, 1, data);
+}
+
 void GPU_shader_uniform_1b(GPUShader *sh, const char *name, bool value)
 {
   GPU_shader_uniform_1i(sh, name, value ? 1 : 0);
diff --git a/source/blender/gpu/shaders/compositor/compositor_split_viewer.glsl b/source/blender/gpu/shaders/compositor/compositor_split_viewer.glsl
new file mode 100644
index 00000000000..1b78e094e9f
--- /dev/null
+++ b/source/blender/gpu/shaders/compositor/compositor_split_viewer.glsl
@@ -0,0 +1,13 @@
+#pragma BLENDER_REQUIRE(gpu_shader_compositor_texture_utils.glsl)
+
+void main()
+{
+  ivec2 xy = ivec2(gl_GlobalInvocationID.xy);
+#if defined(SPLIT_HORIZONTAL)
+  bool condition = (view_size.x * split_ratio) < xy.x;
+#elif defined(SPLIT_VERTICAL)
+  bool condition = (view_size.y * split_ratio) < xy.y;
+#endif
+  vec4 color = condition ? load_texture(first_image, xy) : load_texture(second_image, xy);
+  imageStore(output_image, xy, color);
+}
diff --git a/source/blender/gpu/shaders/compositor/infos/compositor_split_viewer_info.hh b/source/blender/gpu/shaders/compositor/infos/compositor_split_viewer_info.hh
new file mode 100644
index 00000000000..771362d0f54
--- /dev/null
+++ b/source/blender/gpu/shaders/compositor/infos/compositor_split_viewer_info.hh
@@ -0,0 +1,39 @@
+/*
+ * 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.
+ *
+ * The Original Code is Copyright (C) 2022 Blender Foundation.
+ * All rights reserved.
+ */
+
+#include "gpu_shader_create_info.hh"
+
+GPU_SHADER_CREATE_INFO(compositor_split_viewer_shared)
+    .local_group_size(16, 16)
+    .push_constant(Type::FLOAT, "split_ratio")
+    .push_constant(Type::IVEC2, "view_size")
+    .sampler(0, ImageType::FLOAT_2D, "first_image")
+    .sampler(1, ImageType::FLOAT_2D, "second_image")
+    .image(0, GPU_RGBA16F, Qualifier::WRITE, ImageType::FLOAT_2D, "output_image")
+    .compute_source("compositor_split_viewer.glsl");
+
+GPU_SHADER_CREATE_INFO(compositor_split_viewer_horizontal)
+    .additional_info("compositor_split_viewer_shared")
+    .define("SPLIT_HORIZONTAL")
+    .do_static_compilation(true);
+
+GPU_SHADER_CREATE_INFO(compositor_split_viewer_vertical)
+    .additional_info("compositor_split_viewer_shared")
+    .define("SPLIT_VERTICAL")
+    .do_static_compilation(true);
diff --git a/source/blender/nodes/composite/nodes/node_composite_split_viewer.cc b/source/blender/nodes/composite/nodes/node_composite_split_viewer.cc
index 222a96fac49..0db40aac613 100644
--- a/source/blender/nodes/composite/nodes/node_composite_split_viewer.cc
+++ b/source/blender/nodes/composite/nodes/node_composite_split_viewer.cc
@@ -27,6 +27,12 @@
 #include "UI_interface.h"
 #include "UI_resources.h"
 
+#include "GPU_compute.h"
+#include "GPU_shader.h"
+#include "GPU_texture.h"
+
+#include "NOD_compositor_execute.hh"
+
 #include "node_composite_util.hh"
 
 /* **************** SPLIT VIEWER ******************** */
@@ -49,22 +55,6 @@ static void node_composit_init_splitviewer(bNodeTree *UNUSED(ntree), bNode *node
   node->id = (ID *)BKE_image_ensure_viewer(G.main, IMA_TYPE_COMPOSITE, "Viewer Node");
 }
 
-static int node_composit_gpu_splitviewer(GPUMaterial *mat,
-                                         bNode *node,
-                                         bNodeExecData *UNUSED(execdata),
-                                         GPUNodeStack *in,
-                                         GPUNodeStack *out)
-{
-  const float split_factor = node->custom1 / 100.0f;
-  const char *function_name = node->custom2 ? "node_composite_split_viewer_y" :
-                                              "node_composite_split_viewer_x";
-  GPUNodeLink *out_link;
-  GPU_stack_link(mat, node, function_name, in, out, GPU_uniform(&split_factor), &out_link);
-  GPU_material_output_surface(mat, out_link);
-
-  return true;
-}
-
 static void node_composit_buts_splitviewer(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
 {
   uiLayout *row, *col;
@@ -75,6 +65,74 @@ static void node_composit_buts_splitviewer(uiLayout *layout, bContext *UNUSED(C)
   uiItemR(col, ptr, "factor", UI_ITEM_R_SPLIT_EMPTY_NAME, nullptr, ICON_NONE);
 }
 
+using namespace blender::viewport_compositor;
+
+class ViewerOperation : public NodeOperation {
+ public:
+  using NodeOperation::NodeOperation;
+
+  void execute() override
+  {
+    GPUShader *shader = get_split_viewer_shader();
+    GPU_shader_bind(shader);
+
+    const int2 size = compute_domain().size;
+
+    GPU_shader_uniform_1f(shader, "split_ratio", get_split_ratio());
+    GPU_shader_uniform_2iv(shader, "view_size", size);
+
+    const Result &first_image = get_input("Image");
+    first_image.bind_as_texture(shader, "first_image");
+    const Result &second_image = get_input("Image_001");
+    second_image.bind_as_texture(shader, "second_image");
+
+    GPUTexture *viewport_texture = context().get_viewport_texture();
+    const int image_unit = GPU_shader_get_texture_binding(shader, "output_image");
+    GPU_texture_image_bind(viewport_texture, image_unit);
+
+    GPU_compute_dispatch(shader, size.x / 16 + 1, size.y / 16 + 1, 1);
+
+    first_image.unbind_as_texture();
+    second_image.unbind_as_texture();
+    GPU_texture_image_unbind(viewport_texture);
+    GPU_shader_unbind();
+    GPU_shader_free(shader);
+  }
+
+  /* The operation domain have the same dimensions of the viewport without any transformations. */
+  Domain compute_domain() override
+  {
+    GPUTexture *viewport_texture = context().get_viewport_texture();
+    return Domain(int2(GPU_texture_width(viewport_texture), GPU_texture_height(viewport_texture)));
+  }
+
+  GPUShader *get_split_viewer_shader()
+  {
+    if (get_split_axis() == 0) {
+      return GPU_shader_create_from_info_name("compositor_split_viewer_horizontal");
+    }
+
+    return GPU_shader_create_from_info_name("compositor_split_viewer_vertical");
+  }
+
+  /* 0 -> Split Horizontal.
+   * 1 -> Split Vertical. */
+  int get_split_axis()
+  {
+    return node().custom2;
+  }
+
+  float get_split_ratio()
+  {
+    return node().custom1 / 100.0f;
+  }
+};
+
+static NodeOperation *get_compositor_operation(Context &context, DNode node)
+{
+  return new ViewerOperation(context, node);
+}
+
 }  // namespace blender::nodes::node_composite_split_viewer_cc
 
 void register_node_type_cmp_splitviewer()
@@ -89,7 +147,7 @@ void register_node_type_cmp_splitviewer()
   ntype.flag |= NODE_PREVIEW;
   node_type_init(&ntype, file_ns::node_composit_init_splitviewer);
   node_type_storage(&ntype, "ImageUser", node_free_standard_storage, node_copy_standard_storage);
-  node_type_gpu(&ntype, file_ns::node_composit_gpu_splitviewer);
+  ntype.get_compositor_operation = file_ns::get_compositor_operation;
 
   ntype.no_muting = true;



More information about the Bf-blender-cvs mailing list