[Bf-blender-cvs] [782cc8a7953] viewport-compositor: Viewport Compositor: Port Chroma Key node

Omar Emara noreply at git.blender.org
Tue Dec 28 20:02:45 CET 2021


Commit: 782cc8a79537b26b3d618a7c0855fdd4a675fa1a
Author: Omar Emara
Date:   Tue Nov 16 07:18:09 2021 +0200
Branches: viewport-compositor
https://developer.blender.org/rB782cc8a79537b26b3d618a7c0855fdd4a675fa1a

Viewport Compositor: Port Chroma Key node

This patch ports the Chroma Key node to the viewport compositor. The
shader is a straightforward port of the compositor code.

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

M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/intern/gpu_material_library.c
M	source/blender/gpu/shaders/common/gpu_shader_common_math_util.glsl
A	source/blender/gpu/shaders/composite/gpu_shader_composite_chroma_matte.glsl
M	source/blender/nodes/composite/nodes/node_composite_chromaMatte.cc

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

diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index a301b38974c..f218305759f 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -287,6 +287,7 @@ data_to_c_simple(shaders/composite/gpu_shader_composite_alpha_over.glsl SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_box_mask.glsl SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_bright_contrast.glsl SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_channel_matte.glsl SRC)
+data_to_c_simple(shaders/composite/gpu_shader_composite_chroma_matte.glsl SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_color_balance.glsl SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_color_correction.glsl SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_color_to_luminance.glsl SRC)
diff --git a/source/blender/gpu/intern/gpu_material_library.c b/source/blender/gpu/intern/gpu_material_library.c
index ce86868de31..a2df675d45d 100644
--- a/source/blender/gpu/intern/gpu_material_library.c
+++ b/source/blender/gpu/intern/gpu_material_library.c
@@ -53,6 +53,7 @@ extern char datatoc_gpu_shader_composite_alpha_over_glsl[];
 extern char datatoc_gpu_shader_composite_box_mask_glsl[];
 extern char datatoc_gpu_shader_composite_bright_contrast_glsl[];
 extern char datatoc_gpu_shader_composite_channel_matte_glsl[];
+extern char datatoc_gpu_shader_composite_chroma_matte_glsl[];
 extern char datatoc_gpu_shader_composite_color_balance_glsl[];
 extern char datatoc_gpu_shader_composite_color_correction_glsl[];
 extern char datatoc_gpu_shader_composite_color_to_luminance_glsl[];
@@ -211,6 +212,13 @@ static GPUMaterialLibrary gpu_shader_composite_channel_matte_library = {
     .dependencies = {&gpu_shader_common_color_util_library, NULL},
 };
 
+static GPUMaterialLibrary gpu_shader_composite_chroma_matte_library = {
+    .code = datatoc_gpu_shader_composite_chroma_matte_glsl,
+    .dependencies = {&gpu_shader_common_math_util_library,
+                     &gpu_shader_common_color_util_library,
+                     NULL},
+};
+
 static GPUMaterialLibrary gpu_shader_composite_color_balance_library = {
     .code = datatoc_gpu_shader_composite_color_balance_glsl,
     .dependencies = {&gpu_shader_common_math_util_library,
@@ -804,6 +812,7 @@ static GPUMaterialLibrary *gpu_material_libraries[] = {
     &gpu_shader_composite_box_mask_library,
     &gpu_shader_composite_bright_contrast_library,
     &gpu_shader_composite_channel_matte_library,
+    &gpu_shader_composite_chroma_matte_library,
     &gpu_shader_composite_color_balance_library,
     &gpu_shader_composite_color_correction_library,
     &gpu_shader_composite_color_to_luminance_library,
diff --git a/source/blender/gpu/shaders/common/gpu_shader_common_math_util.glsl b/source/blender/gpu/shaders/common/gpu_shader_common_math_util.glsl
index 5b4ae8b69fa..17451624ef0 100644
--- a/source/blender/gpu/shaders/common/gpu_shader_common_math_util.glsl
+++ b/source/blender/gpu/shaders/common/gpu_shader_common_math_util.glsl
@@ -133,6 +133,15 @@ vec3 fallback_pow(vec3 a, float b, vec3 fallback)
 
 /* Matirx Math */
 
+/* Return a 2D rotation matrix with the angle that the input 2D vector makes with the x axis. */
+mat2 vector_to_rotation_matrix(vec2 vector)
+{
+  vec2 normalized_vector = normalize(vector);
+  float cos_angle = normalized_vector.x;
+  float sin_angle = normalized_vector.y;
+  return mat2(cos_angle, sin_angle, -sin_angle, cos_angle);
+}
+
 mat3 euler_to_mat3(vec3 euler)
 {
   float cx = cos(euler.x);
diff --git a/source/blender/gpu/shaders/composite/gpu_shader_composite_chroma_matte.glsl b/source/blender/gpu/shaders/composite/gpu_shader_composite_chroma_matte.glsl
new file mode 100644
index 00000000000..6c7add496bf
--- /dev/null
+++ b/source/blender/gpu/shaders/composite/gpu_shader_composite_chroma_matte.glsl
@@ -0,0 +1,40 @@
+/* Algorithm from the book Video Demystified. Chapter 7. Chroma Keying. */
+void node_composite_chroma_matte(vec4 color,
+                                 vec4 key,
+                                 float acceptance,
+                                 float cutoff,
+                                 float falloff,
+                                 out vec4 result,
+                                 out float matte)
+{
+  vec4 color_ycca;
+  rgba_to_ycca_itu_709(color, color_ycca);
+  vec4 key_ycca;
+  rgba_to_ycca_itu_709(key, key_ycca);
+
+  /* Normalize the CrCb components into the [-1, 1] range. */
+  vec2 color_cc = color_ycca.yz * 2.0 - 1.0;
+  vec2 key_cc = key_ycca.yz * 2.0 - 1.0;
+
+  /* Rotate the color onto the space of the key such that x axis of the color space passes through
+   * the key color. */
+  color_cc = vector_to_rotation_matrix(key_cc * vec2(1.0, -1.0)) * color_cc;
+
+  /* Compute foreground key. If positive, the value is in the [0, 1] range. */
+  float foreground_key = color_cc.x - (abs(color_cc.y) / acceptance);
+
+  /* Negative foreground key values retain the original alpha. Positive values are scaled by the
+   * falloff, while colors that make an angle less than the cutoff angle get a zero alpha. */
+  float alpha = color.a;
+  if (foreground_key > 0.0) {
+    alpha = 1.0 - (foreground_key / falloff);
+
+    if (abs(atan(color_cc.y, color_cc.x)) < (cutoff / 2.0)) {
+      alpha = 0.0;
+    }
+  }
+
+  /* Compute output. */
+  matte = min(alpha, color.a);
+  result = color * matte;
+}
diff --git a/source/blender/nodes/composite/nodes/node_composite_chromaMatte.cc b/source/blender/nodes/composite/nodes/node_composite_chromaMatte.cc
index 990778160df..c3f09bf21f6 100644
--- a/source/blender/nodes/composite/nodes/node_composite_chromaMatte.cc
+++ b/source/blender/nodes/composite/nodes/node_composite_chromaMatte.cc
@@ -21,6 +21,8 @@
  * \ingroup cmpnodes
  */
 
+#include <cmath>
+
 #include "node_composite_util.hh"
 
 /* ******************* Chroma Key ********************************************************** */
@@ -47,6 +49,26 @@ static void node_composit_init_chroma_matte(bNodeTree *UNUSED(ntree), bNode *nod
   c->fstrength = 1.0f;
 }
 
+static int node_composite_gpu_chroma_matte(GPUMaterial *mat,
+                                           bNode *node,
+                                           bNodeExecData *UNUSED(execdata),
+                                           GPUNodeStack *in,
+                                           GPUNodeStack *out)
+{
+  const NodeChroma *data = (NodeChroma *)node->storage;
+
+  const float acceptance = std::tan(data->t1) / 2.0f;
+
+  return GPU_stack_link(mat,
+                        node,
+                        "node_composite_chroma_matte",
+                        in,
+                        out,
+                        GPU_uniform(&acceptance),
+                        GPU_uniform(&data->t2),
+                        GPU_uniform(&data->fstrength));
+}
+
 void register_node_type_cmp_chroma_matte(void)
 {
   static bNodeType ntype;
@@ -55,6 +77,7 @@ void register_node_type_cmp_chroma_matte(void)
   node_type_socket_templates(&ntype, cmp_node_chroma_in, cmp_node_chroma_out);
   node_type_init(&ntype, node_composit_init_chroma_matte);
   node_type_storage(&ntype, "NodeChroma", node_free_standard_storage, node_copy_standard_storage);
+  node_type_gpu(&ntype, node_composite_gpu_chroma_matte);
 
   nodeRegisterType(&ntype);
 }



More information about the Bf-blender-cvs mailing list