Commit: df192657da1b249217ae25d001f7373bd50fdc37
Author: Omar Emara
Date:   Sun Nov 14 21:15:18 2021 +0200
Branches: viewport-compositor
https://developer.blender.org/rBdf192657da1b249217ae25d001f7373bd50fdc37

Viewport Compositor: Port Ellipse Mask node

This patch ports the Ellipse Mask node to the viewport compositor.

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

M       source/blender/gpu/CMakeLists.txt
M       source/blender/gpu/intern/gpu_material_library.c
A       
source/blender/gpu/shaders/composite/gpu_shader_composite_ellipse_mask.glsl
M       source/blender/nodes/composite/nodes/node_composite_ellipsemask.cc

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

diff --git a/source/blender/gpu/CMakeLists.txt 
b/source/blender/gpu/CMakeLists.txt
index 1d2b5b1c8dc..344f33b74bf 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_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)
 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)
 data_to_c_simple(shaders/composite/gpu_shader_composite_exposure.glsl SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_gamma.glsl SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_hue_correct.glsl SRC)
diff --git a/source/blender/gpu/intern/gpu_material_library.c 
b/source/blender/gpu/intern/gpu_material_library.c
index 3cb9e586d12..c90dffbc98d 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_color_balance_glsl[];
 extern char datatoc_gpu_shader_composite_color_correction_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[];
 extern char datatoc_gpu_shader_composite_exposure_glsl[];
 extern char datatoc_gpu_shader_composite_gamma_glsl[];
 extern char datatoc_gpu_shader_composite_hue_correct_glsl[];
@@ -228,6 +229,11 @@ static GPUMaterialLibrary 
gpu_shader_composite_composite_library = {
     .dependencies = {NULL},
 };
 
+static GPUMaterialLibrary gpu_shader_composite_ellipse_mask_library = {
+    .code = datatoc_gpu_shader_composite_ellipse_mask_glsl,
+    .dependencies = {NULL},
+};
+
 static GPUMaterialLibrary gpu_shader_composite_exposure_library = {
     .code = datatoc_gpu_shader_composite_exposure_glsl,
     .dependencies = {NULL},
@@ -795,6 +801,7 @@ static GPUMaterialLibrary *gpu_material_libraries[] = {
     &gpu_shader_composite_color_correction_library,
     &gpu_shader_composite_color_to_luminance_library,
     &gpu_shader_composite_composite_library,
+    &gpu_shader_composite_ellipse_mask_library,
     &gpu_shader_composite_exposure_library,
     &gpu_shader_composite_gamma_library,
     &gpu_shader_composite_hue_correct_library,
diff --git 
a/source/blender/gpu/shaders/composite/gpu_shader_composite_ellipse_mask.glsl 
b/source/blender/gpu/shaders/composite/gpu_shader_composite_ellipse_mask.glsl
new file mode 100644
index 00000000000..7c596cabdcb
--- /dev/null
+++ 
b/source/blender/gpu/shaders/composite/gpu_shader_composite_ellipse_mask.glsl
@@ -0,0 +1,40 @@
+#define CMP_NODE_MASKTYPE_ADD 0.0
+#define CMP_NODE_MASKTYPE_SUBTRACT 1.0
+#define CMP_NODE_MASKTYPE_MULTIPLY 2.0
+#define CMP_NODE_MASKTYPE_NOT 3.0
+
+void node_composite_ellipse_mask(float in_mask,
+                                 float value,
+                                 const float mask_type,
+                                 float x_location,
+                                 float y_location,
+                                 float half_width,
+                                 float half_height,
+                                 float cos_angle,
+                                 float sin_angle,
+                                 out float out_mask)
+{
+  vec2 uv = g_data.uv_render_layer.xy;
+  uv -= vec2(x_location, y_location);
+  uv.y *= ViewportSize.y / ViewportSize.x;
+  uv = mat2(cos_angle, -sin_angle, sin_angle, cos_angle) * uv;
+  bool is_inside = length(uv / vec2(half_width, half_height)) < 1.0;
+
+  if (mask_type == CMP_NODE_MASKTYPE_ADD) {
+    out_mask = is_inside ? max(in_mask, value) : in_mask;
+  }
+  else if (mask_type == CMP_NODE_MASKTYPE_SUBTRACT) {
+    out_mask = is_inside ? clamp(in_mask - value, 0.0, 1.0) : in_mask;
+  }
+  else if (mask_type == CMP_NODE_MASKTYPE_MULTIPLY) {
+    out_mask = is_inside ? in_mask * value : 0.0;
+  }
+  else if (mask_type == CMP_NODE_MASKTYPE_NOT) {
+    out_mask = is_inside ? (in_mask > 0.0 ? 0.0 : value) : in_mask;
+  }
+}
+
+#undef CMP_NODE_MASKTYPE_ADD
+#undef CMP_NODE_MASKTYPE_SUBTRACT
+#undef CMP_NODE_MASKTYPE_MULTIPLY
+#undef CMP_NODE_MASKTYPE_NOT
diff --git a/source/blender/nodes/composite/nodes/node_composite_ellipsemask.cc 
b/source/blender/nodes/composite/nodes/node_composite_ellipsemask.cc
index 67196fb0d35..adb62b3e488 100644
--- a/source/blender/nodes/composite/nodes/node_composite_ellipsemask.cc
+++ b/source/blender/nodes/composite/nodes/node_composite_ellipsemask.cc
@@ -44,6 +44,34 @@ static void node_composit_init_ellipsemask(bNodeTree 
*UNUSED(ntree), bNode *node
   node->storage = data;
 }
 
+static int node_composite_gpu_ellipsemask(GPUMaterial *mat,
+                                          bNode *node,
+                                          bNodeExecData *UNUSED(execdata),
+                                          GPUNodeStack *in,
+                                          GPUNodeStack *out)
+{
+  const NodeEllipseMask *data = (NodeEllipseMask *)node->storage;
+
+  const float mask_type = (float)node->custom1;
+  const float cos_angle = std::cos(data->rotation);
+  const float sin_angle = std::sin(data->rotation);
+  const float half_width = data->width / 2.0;
+  const float half_height = data->height / 2.0;
+
+  return GPU_stack_link(mat,
+                        node,
+                        "node_composite_ellipse_mask",
+                        in,
+                        out,
+                        GPU_constant(&mask_type),
+                        GPU_uniform(&data->x),
+                        GPU_uniform(&data->y),
+                        GPU_uniform(&half_width),
+                        GPU_uniform(&half_height),
+                        GPU_uniform(&cos_angle),
+                        GPU_uniform(&sin_angle));
+}
+
 void register_node_type_cmp_ellipsemask(void)
 {
   static bNodeType ntype;
@@ -54,6 +82,7 @@ void register_node_type_cmp_ellipsemask(void)
   node_type_init(&ntype, node_composit_init_ellipsemask);
   node_type_storage(
       &ntype, "NodeEllipseMask", node_free_standard_storage, 
node_copy_standard_storage);
+  node_type_gpu(&ntype, node_composite_gpu_ellipsemask);
 
   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