Commit: 88494adecb1491276e3137c2989d29aec70b7e9b
Author: Omar Emara
Date:   Thu Nov 4 18:58:47 2021 +0200
Branches: viewport-compositor
https://developer.blender.org/rB88494adecb1491276e3137c2989d29aec70b7e9b

Viewport Compositor: Port Mix RGB node

This patch ports the Mix RGB node to the viewport compositor. The
material Mix RGB node code was moved into a common directory to be
utilized by both the material and the compositor nodes. Additionally,
some of the operations were adapted to work with the compositor, in
particular, the linear and soft light operations now write the alpha to
the result, this has no effect on materials but is consistent with the
compositor.

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

M       source/blender/gpu/CMakeLists.txt
M       source/blender/gpu/intern/gpu_material_library.c
R096    source/blender/gpu/shaders/material/gpu_shader_material_mix_rgb.glsl    
source/blender/gpu/shaders/common/gpu_shader_common_mix_rgb.glsl
M       source/blender/nodes/composite/nodes/node_composite_mixrgb.cc
M       source/blender/nodes/shader/nodes/node_shader_mixRgb.cc

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

diff --git a/source/blender/gpu/CMakeLists.txt 
b/source/blender/gpu/CMakeLists.txt
index 36766b34b92..683cb8225ee 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -277,6 +277,7 @@ data_to_c_simple(shaders/gpu_shader_geometry.glsl SRC)
 
 data_to_c_simple(shaders/common/gpu_shader_common_color_util.glsl SRC)
 data_to_c_simple(shaders/common/gpu_shader_common_math_util.glsl SRC)
+data_to_c_simple(shaders/common/gpu_shader_common_mix_rgb.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)
@@ -327,7 +328,6 @@ 
data_to_c_simple(shaders/material/gpu_shader_material_light_path.glsl SRC)
 data_to_c_simple(shaders/material/gpu_shader_material_mapping.glsl SRC)
 data_to_c_simple(shaders/material/gpu_shader_material_map_range.glsl SRC)
 data_to_c_simple(shaders/material/gpu_shader_material_math.glsl SRC)
-data_to_c_simple(shaders/material/gpu_shader_material_mix_rgb.glsl SRC)
 data_to_c_simple(shaders/material/gpu_shader_material_mix_shader.glsl SRC)
 data_to_c_simple(shaders/material/gpu_shader_material_noise.glsl SRC)
 data_to_c_simple(shaders/material/gpu_shader_material_normal.glsl SRC)
diff --git a/source/blender/gpu/intern/gpu_material_library.c 
b/source/blender/gpu/intern/gpu_material_library.c
index 3d734b801b3..967e04543db 100644
--- a/source/blender/gpu/intern/gpu_material_library.c
+++ b/source/blender/gpu/intern/gpu_material_library.c
@@ -43,6 +43,7 @@
 
 extern char datatoc_gpu_shader_common_color_util_glsl[];
 extern char datatoc_gpu_shader_common_math_util_glsl[];
+extern char datatoc_gpu_shader_common_mix_rgb_glsl[];
 extern char datatoc_gpu_shader_common_hash_glsl[];
 
 extern char datatoc_gpu_shader_composite_alpha_over_glsl[];
@@ -93,7 +94,6 @@ extern char datatoc_gpu_shader_material_light_path_glsl[];
 extern char datatoc_gpu_shader_material_mapping_glsl[];
 extern char datatoc_gpu_shader_material_map_range_glsl[];
 extern char datatoc_gpu_shader_material_math_glsl[];
-extern char datatoc_gpu_shader_material_mix_rgb_glsl[];
 extern char datatoc_gpu_shader_material_mix_shader_glsl[];
 extern char datatoc_gpu_shader_material_noise_glsl[];
 extern char datatoc_gpu_shader_material_normal_glsl[];
@@ -155,6 +155,11 @@ static GPUMaterialLibrary 
gpu_shader_common_color_util_library = {
     .dependencies = {NULL},
 };
 
+static GPUMaterialLibrary gpu_shader_common_mix_rgb_library = {
+    .code = datatoc_gpu_shader_common_mix_rgb_glsl,
+    .dependencies = {&gpu_shader_common_color_util_library, NULL},
+};
+
 static GPUMaterialLibrary gpu_shader_common_hash_library = {
     .code = datatoc_gpu_shader_common_hash_glsl,
     .dependencies = {NULL},
@@ -411,11 +416,6 @@ static GPUMaterialLibrary gpu_shader_material_math_library 
= {
     .dependencies = {&gpu_shader_common_math_util_library, NULL},
 };
 
-static GPUMaterialLibrary gpu_shader_material_mix_rgb_library = {
-    .code = datatoc_gpu_shader_material_mix_rgb_glsl,
-    .dependencies = {&gpu_shader_common_color_util_library, NULL},
-};
-
 static GPUMaterialLibrary gpu_shader_material_mix_shader_library = {
     .code = datatoc_gpu_shader_material_mix_shader_glsl,
     .dependencies = {NULL},
@@ -659,6 +659,7 @@ static GPUMaterialLibrary 
gpu_shader_material_world_normals_library = {
 static GPUMaterialLibrary *gpu_material_libraries[] = {
     &gpu_shader_common_math_util_library,
     &gpu_shader_common_color_util_library,
+    &gpu_shader_common_mix_rgb_library,
     &gpu_shader_common_hash_library,
     &gpu_shader_material_noise_library,
     &gpu_shader_material_fractal_noise_library,
@@ -698,7 +699,6 @@ static GPUMaterialLibrary *gpu_material_libraries[] = {
     &gpu_shader_material_mapping_library,
     &gpu_shader_material_map_range_library,
     &gpu_shader_material_math_library,
-    &gpu_shader_material_mix_rgb_library,
     &gpu_shader_material_mix_shader_library,
     &gpu_shader_material_normal_library,
     &gpu_shader_material_normal_map_library,
diff --git 
a/source/blender/gpu/shaders/material/gpu_shader_material_mix_rgb.glsl 
b/source/blender/gpu/shaders/common/gpu_shader_common_mix_rgb.glsl
similarity index 96%
rename from source/blender/gpu/shaders/material/gpu_shader_material_mix_rgb.glsl
rename to source/blender/gpu/shaders/common/gpu_shader_common_mix_rgb.glsl
index e089aec1d92..5ff5beff131 100644
--- a/source/blender/gpu/shaders/material/gpu_shader_material_mix_rgb.glsl
+++ b/source/blender/gpu/shaders/common/gpu_shader_common_mix_rgb.glsl
@@ -276,6 +276,7 @@ void mix_soft(float fac, vec4 col1, vec4 col2, out vec4 
outcol)
   vec4 one = vec4(1.0);
   vec4 scr = one - (one - col2) * (one - col1);
   outcol = facm * col1 + fac * ((one - col1) * col2 * col1 + col1 * scr);
+  outcol.a = col1.a;
 }
 
 void mix_linear(float fac, vec4 col1, vec4 col2, out vec4 outcol)
@@ -283,9 +284,15 @@ void mix_linear(float fac, vec4 col1, vec4 col2, out vec4 
outcol)
   fac = clamp(fac, 0.0, 1.0);
 
   outcol = col1 + fac * (2.0 * (col2 - vec4(0.5)));
+  outcol.a = col1.a;
 }
 
-void clamp_color(vec3 vec, vec3 min, vec3 max, out vec3 out_vec)
+void clamp_color(vec4 vec, const vec4 min, const vec4 max, out vec4 out_vec)
 {
   out_vec = clamp(vec, min, max);
 }
+
+void multiply_by_alpha(float factor, vec4 color, out float result)
+{
+  result = factor * color.a;
+}
diff --git a/source/blender/nodes/composite/nodes/node_composite_mixrgb.cc 
b/source/blender/nodes/composite/nodes/node_composite_mixrgb.cc
index 4f2a9c2717c..ca34b2ed24c 100644
--- a/source/blender/nodes/composite/nodes/node_composite_mixrgb.cc
+++ b/source/blender/nodes/composite/nodes/node_composite_mixrgb.cc
@@ -21,6 +21,7 @@
  * \ingroup cmpnodes
  */
 
+#include "DNA_material_types.h"
 #include "node_composite_util.hh"
 
 /* **************** MIX RGB ******************** */
@@ -37,6 +38,86 @@ static void cmp_node_mixrgb_declare(NodeDeclarationBuilder 
&b)
 
 }  // namespace blender::nodes
 
+static const char *gpu_shader_get_name(int mode)
+{
+  switch (mode) {
+    case MA_RAMP_BLEND:
+      return "mix_blend";
+    case MA_RAMP_ADD:
+      return "mix_add";
+    case MA_RAMP_MULT:
+      return "mix_mult";
+    case MA_RAMP_SUB:
+      return "mix_sub";
+    case MA_RAMP_SCREEN:
+      return "mix_screen";
+    case MA_RAMP_DIV:
+      return "mix_div";
+    case MA_RAMP_DIFF:
+      return "mix_diff";
+    case MA_RAMP_DARK:
+      return "mix_dark";
+    case MA_RAMP_LIGHT:
+      return "mix_light";
+    case MA_RAMP_OVERLAY:
+      return "mix_overlay";
+    case MA_RAMP_DODGE:
+      return "mix_dodge";
+    case MA_RAMP_BURN:
+      return "mix_burn";
+    case MA_RAMP_HUE:
+      return "mix_hue";
+    case MA_RAMP_SAT:
+      return "mix_sat";
+    case MA_RAMP_VAL:
+      return "mix_val";
+    case MA_RAMP_COLOR:
+      return "mix_color";
+    case MA_RAMP_SOFT:
+      return "mix_soft";
+    case MA_RAMP_LINEAR:
+      return "mix_linear";
+  }
+
+  return nullptr;
+}
+
+static int node_composite_gpu_mix_rgb(GPUMaterial *mat,
+                                      bNode *node,
+                                      bNodeExecData *UNUSED(execdata),
+                                      GPUNodeStack *in,
+                                      GPUNodeStack *out)
+{
+  const char *name = gpu_shader_get_name(node->custom1);
+  if (name == nullptr) {
+    return 0;
+  }
+
+  bool valid = GPU_stack_link(mat, node, name, in, out);
+  if (!valid) {
+    return 0;
+  }
+
+  if (node->custom2 & SHD_MIXRGB_USE_ALPHA) {
+    bool valid = GPU_link(mat, "multiply_by_alpha", in[0].link, in[1].link, 
&in[0].link);
+    if (!valid) {
+      return 0;
+    }
+  }
+
+  if (node->custom2 & SHD_MIXRGB_CLAMP) {
+    const float min[4] = {0.0f, 0.0f, 0.0f, 0.0f};
+    const float max[4] = {1.0f, 1.0f, 1.0f, 1.0f};
+    valid = GPU_link(
+        mat, "clamp_color", out[0].link, GPU_constant(min), GPU_constant(max), 
&out[0].link);
+    if (!valid) {
+      return 0;
+    }
+  }
+
+  return 1;
+}
+
 /* custom1 = mix type */
 void register_node_type_cmp_mix_rgb(void)
 {
@@ -45,6 +126,7 @@ void register_node_type_cmp_mix_rgb(void)
   cmp_node_type_base(&ntype, CMP_NODE_MIX_RGB, "Mix", NODE_CLASS_OP_COLOR, 
NODE_PREVIEW);
   ntype.declare = blender::nodes::cmp_node_mixrgb_declare;
   node_type_label(&ntype, node_blend_label);
+  node_type_gpu(&ntype, node_composite_gpu_mix_rgb);
 
   nodeRegisterType(&ntype);
 }
diff --git a/source/blender/nodes/shader/nodes/node_shader_mixRgb.cc 
b/source/blender/nodes/shader/nodes/node_shader_mixRgb.cc
index d4d02e80ada..51126349d71 100644
--- a/source/blender/nodes/shader/nodes/node_shader_mixRgb.cc
+++ b/source/blender/nodes/shader/nodes/node_shader_mixRgb.cc
@@ -117,8 +117,8 @@ static int gpu_shader_mix_rgb(GPUMaterial *mat,
   if (name != nullptr) {
     int ret = GPU_stack_link(mat, node, name, in, out);
     if (ret && node->custom2 & SHD_MIXRGB_CLAMP) {
-      const float min[3] = {0.0f, 0.0f, 0.0f};
-      const float max[3] = {1.0f, 1.0f, 1.0f};
+      const float min[4] = {0.0f, 0.0f, 0.0f, 0.0f};
+      const float max[4] = {1.0f, 1.0f, 1.0f, 0.0f};
       GPU_link(
           mat, "clamp_color", out[0].link, GPU_constant(min), 
GPU_constant(max), &out[0].link);
     }

_______________________________________________
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