[Bf-blender-cvs] [c85eb8fc7cc] viewport-compositor: Viewport Compositor: Port Bright And Contrast node

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


Commit: c85eb8fc7cca865b58f29691855dd9949b7488ba
Author: Omar Emara
Date:   Tue Nov 2 13:24:21 2021 +0200
Branches: viewport-compositor
https://developer.blender.org/rBc85eb8fc7cca865b58f29691855dd9949b7488ba

Viewport Compositor: Port Bright And Contrast node

This patch ports the Bright And Contrast node to the viewport
compositor. The shader is a straightforward port of the compositor code.
The (un)premultiply_alpha functions were adjusted to retain the original
alpha for compatibility with the compositor. This has no effect on
materials because alpha is implicitly discarded.

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

M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/intern/gpu_material_library.c
M	source/blender/gpu/shaders/common/gpu_shader_common_color_util.glsl
M	source/blender/gpu/shaders/common/gpu_shader_common_math_util.glsl
A	source/blender/gpu/shaders/composite/gpu_shader_composite_bright_contrast.glsl
M	source/blender/nodes/composite/nodes/node_composite_brightness.cc

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

diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index 68a49830203..0a3a14db15b 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -280,6 +280,7 @@ data_to_c_simple(shaders/common/gpu_shader_common_math_util.glsl SRC)
 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_bright_contrast.glsl SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_composite.glsl SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_image.glsl SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_invert.glsl SRC)
diff --git a/source/blender/gpu/intern/gpu_material_library.c b/source/blender/gpu/intern/gpu_material_library.c
index 80455e3dc74..e8e6e7699fb 100644
--- a/source/blender/gpu/intern/gpu_material_library.c
+++ b/source/blender/gpu/intern/gpu_material_library.c
@@ -46,6 +46,7 @@ extern char datatoc_gpu_shader_common_math_util_glsl[];
 extern char datatoc_gpu_shader_common_hash_glsl[];
 
 extern char datatoc_gpu_shader_composite_alpha_over_glsl[];
+extern char datatoc_gpu_shader_composite_bright_contrast_glsl[];
 extern char datatoc_gpu_shader_composite_composite_glsl[];
 extern char datatoc_gpu_shader_composite_image_glsl[];
 extern char datatoc_gpu_shader_composite_invert_glsl[];
@@ -158,6 +159,13 @@ static GPUMaterialLibrary gpu_shader_composite_alpha_over_library = {
     .dependencies = {NULL},
 };
 
+static GPUMaterialLibrary gpu_shader_composite_bright_contrast_library = {
+    .code = datatoc_gpu_shader_composite_bright_contrast_glsl,
+    .dependencies = {&gpu_shader_common_math_util_library,
+                     &gpu_shader_common_color_util_library,
+                     NULL},
+};
+
 static GPUMaterialLibrary gpu_shader_composite_composite_library = {
     .code = datatoc_gpu_shader_composite_composite_glsl,
     .dependencies = {NULL},
@@ -700,6 +708,7 @@ static GPUMaterialLibrary *gpu_material_libraries[] = {
     &gpu_shader_material_wireframe_library,
     &gpu_shader_material_world_normals_library,
     &gpu_shader_composite_alpha_over_library,
+    &gpu_shader_composite_bright_contrast_library,
     &gpu_shader_composite_composite_library,
     &gpu_shader_composite_image_library,
     &gpu_shader_composite_invert_library,
diff --git a/source/blender/gpu/shaders/common/gpu_shader_common_color_util.glsl b/source/blender/gpu/shaders/common/gpu_shader_common_color_util.glsl
index a5c3a990d90..f1a2f41cca4 100644
--- a/source/blender/gpu/shaders/common/gpu_shader_common_color_util.glsl
+++ b/source/blender/gpu/shaders/common/gpu_shader_common_color_util.glsl
@@ -97,15 +97,15 @@ void color_alpha_clear(vec4 color, out vec4 result)
 
 void color_alpha_premultiply(vec4 color, out vec4 result)
 {
-  result = vec4(color.rgb * color.a, 1.0);
+  result = vec4(color.rgb * color.a, color.a);
 }
 
 void color_alpha_unpremultiply(vec4 color, out vec4 result)
 {
   if (color.a == 0.0 || color.a == 1.0) {
-    result = vec4(color.rgb, 1.0);
+    result = color;
   }
   else {
-    result = vec4(color.rgb / color.a, 1.0);
+    result = vec4(color.rgb / color.a, color.a);
   }
 }
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 523c44fa36a..3c60c566f22 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
@@ -1,3 +1,7 @@
+/* Limits. */
+
+#define FLT_EPSILON 1.192092896e-07F
+
 /* Float Math */
 
 float safe_divide(float a, float b)
diff --git a/source/blender/gpu/shaders/composite/gpu_shader_composite_bright_contrast.glsl b/source/blender/gpu/shaders/composite/gpu_shader_composite_bright_contrast.glsl
new file mode 100644
index 00000000000..24a33c3f84c
--- /dev/null
+++ b/source/blender/gpu/shaders/composite/gpu_shader_composite_bright_contrast.glsl
@@ -0,0 +1,34 @@
+/* The algorithm is by Werner D. Streidt
+ * (http://visca.com/ffactory/archives/5-99/msg00021.html)
+ * Extracted of OpenCV demhist.c
+ */
+
+void node_composite_bright_contrast(
+    vec4 color, float brightness, float contrast, const float use_premultiply, out vec4 result)
+{
+  brightness /= 100.0;
+  float delta = contrast / 200.0;
+
+  float multiplier, offset;
+  if (contrast > 0.0) {
+    multiplier = 1.0 - delta * 2.0;
+    multiplier = 1.0 / max(multiplier, FLT_EPSILON);
+    offset = multiplier * (brightness - delta);
+  }
+  else {
+    delta *= -1.0;
+    multiplier = max(1.0 - delta * 2.0, 0.0);
+    offset = multiplier * brightness + delta;
+  }
+
+  if (use_premultiply != 0.0) {
+    color_alpha_unpremultiply(color, color);
+  }
+
+  result.rgb = color.rgb * multiplier + offset;
+  result.a = color.a;
+
+  if (use_premultiply != 0.0) {
+    color_alpha_premultiply(result, result);
+  }
+}
diff --git a/source/blender/nodes/composite/nodes/node_composite_brightness.cc b/source/blender/nodes/composite/nodes/node_composite_brightness.cc
index ad4b09c69d0..11a511067cc 100644
--- a/source/blender/nodes/composite/nodes/node_composite_brightness.cc
+++ b/source/blender/nodes/composite/nodes/node_composite_brightness.cc
@@ -42,6 +42,18 @@ static void node_composit_init_brightcontrast(bNodeTree *UNUSED(ntree), bNode *n
   node->custom1 = 1;
 }
 
+static int node_composite_gpu_brightcontrast(GPUMaterial *mat,
+                                             bNode *node,
+                                             bNodeExecData *UNUSED(execdata),
+                                             GPUNodeStack *in,
+                                             GPUNodeStack *out)
+{
+  float use_premultiply = node->custom1 ? 1.0f : 0.0f;
+
+  return GPU_stack_link(
+      mat, node, "node_composite_bright_contrast", in, out, GPU_constant(&use_premultiply));
+}
+
 void register_node_type_cmp_brightcontrast(void)
 {
   static bNodeType ntype;
@@ -49,6 +61,7 @@ void register_node_type_cmp_brightcontrast(void)
   cmp_node_type_base(&ntype, CMP_NODE_BRIGHTCONTRAST, "Bright/Contrast", NODE_CLASS_OP_COLOR, 0);
   ntype.declare = blender::nodes::cmp_node_brightcontrast_declare;
   node_type_init(&ntype, node_composit_init_brightcontrast);
+  node_type_gpu(&ntype, node_composite_gpu_brightcontrast);
 
   nodeRegisterType(&ntype);
 }



More information about the Bf-blender-cvs mailing list