Commit: 4516e86668d1c26798d827de323232308d9d5add
Author: Omar Emara
Date:   Wed Nov 17 13:29:58 2021 +0200
Branches: viewport-compositor
https://developer.blender.org/rB4516e86668d1c26798d827de323232308d9d5add

Viewport Compositor: Port Color Key node

This patch ports the Color 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
A       
source/blender/gpu/shaders/composite/gpu_shader_composite_color_matte.glsl
M       source/blender/nodes/composite/nodes/node_composite_colorMatte.cc

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

diff --git a/source/blender/gpu/CMakeLists.txt 
b/source/blender/gpu/CMakeLists.txt
index f218305759f..4bb1a508afc 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -290,6 +290,7 @@ 
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_matte.glsl SRC)
 
data_to_c_simple(shaders/composite/gpu_shader_composite_color_to_luminance.glsl 
SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_composite.glsl SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_ellipse_mask.glsl SRC)
diff --git a/source/blender/gpu/intern/gpu_material_library.c 
b/source/blender/gpu/intern/gpu_material_library.c
index a2df675d45d..9b7588c9912 100644
--- a/source/blender/gpu/intern/gpu_material_library.c
+++ b/source/blender/gpu/intern/gpu_material_library.c
@@ -56,6 +56,7 @@ 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_matte_glsl[];
 extern char datatoc_gpu_shader_composite_color_to_luminance_glsl[];
 extern char datatoc_gpu_shader_composite_composite_glsl[];
 extern char datatoc_gpu_shader_composite_ellipse_mask_glsl[];
@@ -233,6 +234,11 @@ static GPUMaterialLibrary 
gpu_shader_composite_color_correction_library = {
                      NULL},
 };
 
+static GPUMaterialLibrary gpu_shader_composite_color_matte_library = {
+    .code = datatoc_gpu_shader_composite_color_matte_glsl,
+    .dependencies = {&gpu_shader_common_color_util_library, NULL},
+};
+
 static GPUMaterialLibrary gpu_shader_composite_color_to_luminance_library = {
     .code = datatoc_gpu_shader_composite_color_to_luminance_glsl,
     .dependencies = {&gpu_shader_common_color_util_library, NULL},
@@ -815,6 +821,7 @@ static GPUMaterialLibrary *gpu_material_libraries[] = {
     &gpu_shader_composite_chroma_matte_library,
     &gpu_shader_composite_color_balance_library,
     &gpu_shader_composite_color_correction_library,
+    &gpu_shader_composite_color_matte_library,
     &gpu_shader_composite_color_to_luminance_library,
     &gpu_shader_composite_composite_library,
     &gpu_shader_composite_ellipse_mask_library,
diff --git 
a/source/blender/gpu/shaders/composite/gpu_shader_composite_color_matte.glsl 
b/source/blender/gpu/shaders/composite/gpu_shader_composite_color_matte.glsl
new file mode 100644
index 00000000000..745c39f048f
--- /dev/null
+++ b/source/blender/gpu/shaders/composite/gpu_shader_composite_color_matte.glsl
@@ -0,0 +1,25 @@
+void node_composite_color_matte(vec4 color,
+                                vec4 key,
+                                float hue_epsilon,
+                                float saturation_epsilon,
+                                float value_epsilon,
+                                out vec4 result,
+                                out float matte)
+
+{
+  vec4 color_hsva;
+  rgb_to_hsv(color, color_hsva);
+  vec4 key_hsva;
+  rgb_to_hsv(key, key_hsva);
+
+  bool is_within_saturation = distance(color_hsva.y, key_hsva.y) < 
saturation_epsilon;
+  bool is_within_value = distance(color_hsva.z, key_hsva.z) < value_epsilon;
+  bool is_within_hue = distance(color_hsva.x, key_hsva.x) < hue_epsilon;
+  /* Hue wraps around, so check the distance around the boundary. */
+  float min_hue = min(color_hsva.x, key_hsva.x);
+  float max_hue = max(color_hsva.x, key_hsva.x);
+  is_within_hue = is_within_hue || ((min_hue + (1.0 - max_hue)) < hue_epsilon);
+
+  matte = (is_within_hue && is_within_saturation && is_within_value) ? 0.0 : 
color.a;
+  result = color * matte;
+}
diff --git a/source/blender/nodes/composite/nodes/node_composite_colorMatte.cc 
b/source/blender/nodes/composite/nodes/node_composite_colorMatte.cc
index fc9a0075b14..457cb3fba6b 100644
--- a/source/blender/nodes/composite/nodes/node_composite_colorMatte.cc
+++ b/source/blender/nodes/composite/nodes/node_composite_colorMatte.cc
@@ -47,6 +47,27 @@ static void node_composit_init_color_matte(bNodeTree 
*UNUSED(ntree), bNode *node
   c->fstrength = 1.0f;
 }
 
+static int node_composite_gpu_color_matte(GPUMaterial *mat,
+                                          bNode *node,
+                                          bNodeExecData *UNUSED(execdata),
+                                          GPUNodeStack *in,
+                                          GPUNodeStack *out)
+{
+  const NodeChroma *data = (NodeChroma *)node->storage;
+
+  /* Because the Hue wraps around. */
+  const float hue_epsilon = data->t1 / 2.0f;
+
+  return GPU_stack_link(mat,
+                        node,
+                        "node_composite_color_matte",
+                        in,
+                        out,
+                        GPU_uniform(&hue_epsilon),
+                        GPU_uniform(&data->t2),
+                        GPU_uniform(&data->t3));
+}
+
 void register_node_type_cmp_color_matte(void)
 {
   static bNodeType ntype;
@@ -55,6 +76,7 @@ void register_node_type_cmp_color_matte(void)
   node_type_socket_templates(&ntype, cmp_node_color_in, cmp_node_color_out);
   node_type_init(&ntype, node_composit_init_color_matte);
   node_type_storage(&ntype, "NodeChroma", node_free_standard_storage, 
node_copy_standard_storage);
+  node_type_gpu(&ntype, node_composite_gpu_color_matte);
 
   nodeRegisterType(&ntype);
 }

_______________________________________________
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