[Bf-blender-cvs] [c38aebbafb8] viewport-compositor: Viewport Compositor: Port Channel Key node
Omar Emara
noreply at git.blender.org
Tue Dec 28 20:02:45 CET 2021
Commit: c38aebbafb8be024aa7fa4ac60ad2af3b342e28d
Author: Omar Emara
Date: Mon Nov 15 13:07:52 2021 +0200
Branches: viewport-compositor
https://developer.blender.org/rBc38aebbafb8be024aa7fa4ac60ad2af3b342e28d
Viewport Compositor: Port Channel Key node
This patch ports the Channel 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_channel_matte.glsl
M source/blender/nodes/composite/nodes/node_composite_channelMatte.cc
===================================================================
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index 344f33b74bf..a301b38974c 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -286,6 +286,7 @@ data_to_c_simple(shaders/common/gpu_shader_common_hash.glsl SRC)
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_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 c90dffbc98d..ce86868de31 100644
--- a/source/blender/gpu/intern/gpu_material_library.c
+++ b/source/blender/gpu/intern/gpu_material_library.c
@@ -52,6 +52,7 @@ extern char datatoc_gpu_shader_common_hash_glsl[];
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_color_balance_glsl[];
extern char datatoc_gpu_shader_composite_color_correction_glsl[];
extern char datatoc_gpu_shader_composite_color_to_luminance_glsl[];
@@ -205,6 +206,11 @@ static GPUMaterialLibrary gpu_shader_composite_bright_contrast_library = {
NULL},
};
+static GPUMaterialLibrary gpu_shader_composite_channel_matte_library = {
+ .code = datatoc_gpu_shader_composite_channel_matte_glsl,
+ .dependencies = {&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,
@@ -797,6 +803,7 @@ static GPUMaterialLibrary *gpu_material_libraries[] = {
&gpu_shader_composite_alpha_over_library,
&gpu_shader_composite_box_mask_library,
&gpu_shader_composite_bright_contrast_library,
+ &gpu_shader_composite_channel_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/composite/gpu_shader_composite_channel_matte.glsl b/source/blender/gpu/shaders/composite/gpu_shader_composite_channel_matte.glsl
new file mode 100644
index 00000000000..1b7e8357dc6
--- /dev/null
+++ b/source/blender/gpu/shaders/composite/gpu_shader_composite_channel_matte.glsl
@@ -0,0 +1,50 @@
+#define CMP_NODE_CHANNEL_MATTE_CS_RGB 1.0
+#define CMP_NODE_CHANNEL_MATTE_CS_HSV 2.0
+#define CMP_NODE_CHANNEL_MATTE_CS_YUV 3.0
+#define CMP_NODE_CHANNEL_MATTE_CS_YCC 4.0
+
+void node_composite_channel_matte(vec4 color,
+ const float color_space,
+ const float matte_channel,
+ const vec2 limit_channels,
+ float max_limit,
+ float min_limit,
+ out vec4 result,
+ out float matte)
+{
+ vec4 channels;
+ if (color_space == CMP_NODE_CHANNEL_MATTE_CS_HSV) {
+ rgb_to_hsv(color, channels);
+ }
+ else if (color_space == CMP_NODE_CHANNEL_MATTE_CS_YUV) {
+ rgba_to_yuva_itu_709(color, channels);
+ }
+ else if (color_space == CMP_NODE_CHANNEL_MATTE_CS_YCC) {
+ rgba_to_ycca_itu_709(color, channels);
+ }
+ else {
+ channels = color;
+ }
+
+ float matte_value = channels[int(matte_channel)];
+ float limit_value = max(channels[int(limit_channels.x)], channels[int(limit_channels.y)]);
+
+ float alpha = 1.0 - (matte_value - limit_value);
+ if (alpha > max_limit) {
+ alpha = color.a;
+ }
+ else if (alpha < min_limit) {
+ alpha = 0.0;
+ }
+ else {
+ alpha = (alpha - min_limit) / (max_limit - min_limit);
+ }
+
+ matte = min(alpha, color.a);
+ result = color * matte;
+}
+
+#undef CMP_NODE_CHANNEL_MATTE_CS_RGB
+#undef CMP_NODE_CHANNEL_MATTE_CS_HSV
+#undef CMP_NODE_CHANNEL_MATTE_CS_YUV
+#undef CMP_NODE_CHANNEL_MATTE_CS_YCC
diff --git a/source/blender/nodes/composite/nodes/node_composite_channelMatte.cc b/source/blender/nodes/composite/nodes/node_composite_channelMatte.cc
index e211bc45b17..484c4d83584 100644
--- a/source/blender/nodes/composite/nodes/node_composite_channelMatte.cc
+++ b/source/blender/nodes/composite/nodes/node_composite_channelMatte.cc
@@ -50,6 +50,41 @@ static void node_composit_init_channel_matte(bNodeTree *UNUSED(ntree), bNode *no
node->custom2 = 2; /* Green Channel. */
}
+static int node_composite_gpu_channel_matte(GPUMaterial *mat,
+ bNode *node,
+ bNodeExecData *UNUSED(execdata),
+ GPUNodeStack *in,
+ GPUNodeStack *out)
+{
+ const NodeChroma *data = (NodeChroma *)node->storage;
+
+ const float color_space = (float)node->custom1;
+ const float matte_channel = (float)(node->custom2 - 1);
+
+ /* Always assume the limit algorithm is Max, if it is a single limit channel, store it in both
+ * limit channels. */
+ float limit_channels[2];
+ if (data->algorithm == 1) {
+ limit_channels[0] = (float)(node->custom2 % 3);
+ limit_channels[1] = (float)((node->custom2 + 1) % 3);
+ }
+ else {
+ limit_channels[0] = (float)(data->channel - 1);
+ limit_channels[1] = (float)(data->channel - 1);
+ }
+
+ return GPU_stack_link(mat,
+ node,
+ "node_composite_channel_matte",
+ in,
+ out,
+ GPU_constant(&color_space),
+ GPU_constant(&matte_channel),
+ GPU_constant(limit_channels),
+ GPU_uniform(&data->t1),
+ GPU_uniform(&data->t2));
+}
+
void register_node_type_cmp_channel_matte(void)
{
static bNodeType ntype;
@@ -59,6 +94,7 @@ void register_node_type_cmp_channel_matte(void)
node_type_socket_templates(&ntype, cmp_node_channel_matte_in, cmp_node_channel_matte_out);
node_type_init(&ntype, node_composit_init_channel_matte);
node_type_storage(&ntype, "NodeChroma", node_free_standard_storage, node_copy_standard_storage);
+ node_type_gpu(&ntype, node_composite_gpu_channel_matte);
nodeRegisterType(&ntype);
}
More information about the Bf-blender-cvs
mailing list