Commit: 6f119700ff2fa91fd5eb6d4cf1b35b0faf806dd5
Author: Omar Emara
Date:   Sat Nov 13 19:48:54 2021 +0200
Branches: viewport-compositor
https://developer.blender.org/rB6f119700ff2fa91fd5eb6d4cf1b35b0faf806dd5

Viewport Compositor: Port Map Range node

This patch ports the Map Range 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_map_value.glsl
M       source/blender/nodes/composite/nodes/node_composite_mapRange.cc

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

diff --git a/source/blender/gpu/CMakeLists.txt 
b/source/blender/gpu/CMakeLists.txt
index 2a8312fc201..87bba8f8e86 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -295,6 +295,7 @@ 
data_to_c_simple(shaders/composite/gpu_shader_composite_hue_correct.glsl SRC)
 
data_to_c_simple(shaders/composite/gpu_shader_composite_hue_saturation_value.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)
+data_to_c_simple(shaders/composite/gpu_shader_composite_map_value.glsl SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_posterize.glsl SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_separate_combine.glsl 
SRC)
 data_to_c_simple(shaders/composite/gpu_shader_composite_set_alpha.glsl SRC)
diff --git a/source/blender/gpu/intern/gpu_material_library.c 
b/source/blender/gpu/intern/gpu_material_library.c
index eac7c8060d5..04c79393ef8 100644
--- a/source/blender/gpu/intern/gpu_material_library.c
+++ b/source/blender/gpu/intern/gpu_material_library.c
@@ -61,6 +61,7 @@ extern char datatoc_gpu_shader_composite_hue_correct_glsl[];
 extern char datatoc_gpu_shader_composite_hue_saturation_value_glsl[];
 extern char datatoc_gpu_shader_composite_image_glsl[];
 extern char datatoc_gpu_shader_composite_invert_glsl[];
+extern char datatoc_gpu_shader_composite_map_value_glsl[];
 extern char datatoc_gpu_shader_composite_posterize_glsl[];
 extern char datatoc_gpu_shader_composite_separate_combine_glsl[];
 extern char datatoc_gpu_shader_composite_set_alpha_glsl[];
@@ -249,6 +250,11 @@ static GPUMaterialLibrary 
gpu_shader_composite_invert_library = {
     .dependencies = {NULL},
 };
 
+static GPUMaterialLibrary gpu_shader_composite_map_value_library = {
+    .code = datatoc_gpu_shader_composite_map_value_glsl,
+    .dependencies = {NULL},
+};
+
 static GPUMaterialLibrary gpu_shader_composite_posterize_library = {
     .code = datatoc_gpu_shader_composite_posterize_glsl,
     .dependencies = {NULL},
@@ -776,6 +782,7 @@ static GPUMaterialLibrary *gpu_material_libraries[] = {
     &gpu_shader_composite_hue_saturation_value_library,
     &gpu_shader_composite_image_library,
     &gpu_shader_composite_invert_library,
+    &gpu_shader_composite_map_value_library,
     &gpu_shader_composite_posterize_library,
     &gpu_shader_composite_separate_combined_library,
     &gpu_shader_composite_set_alpha_library,
diff --git 
a/source/blender/gpu/shaders/composite/gpu_shader_composite_map_value.glsl 
b/source/blender/gpu/shaders/composite/gpu_shader_composite_map_value.glsl
new file mode 100644
index 00000000000..3f8d269339d
--- /dev/null
+++ b/source/blender/gpu/shaders/composite/gpu_shader_composite_map_value.glsl
@@ -0,0 +1,35 @@
+#define BLENDER_ZMAX 10000.0
+
+void node_composite_map_range(float value,
+                              float from_min,
+                              float from_max,
+                              float to_min,
+                              float to_max,
+                              const float should_clamp,
+                              out float result)
+{
+  if (abs(from_max - from_min) < 1e-6) {
+    result = 0.0;
+  }
+  else {
+    if (value >= -BLENDER_ZMAX && value <= BLENDER_ZMAX) {
+      result = (value - from_min) / (from_max - from_min);
+      result = to_min + result * (to_max - to_min);
+    }
+    else if (value > BLENDER_ZMAX) {
+      result = to_max;
+    }
+    else {
+      result = to_min;
+    }
+
+    if (should_clamp != 0.0) {
+      if (to_max > to_min) {
+        result = clamp(result, to_min, to_max);
+      }
+      else {
+        result = clamp(result, to_max, to_min);
+      }
+    }
+  }
+}
diff --git a/source/blender/nodes/composite/nodes/node_composite_mapRange.cc 
b/source/blender/nodes/composite/nodes/node_composite_mapRange.cc
index 808ad538e55..399edf3a659 100644
--- a/source/blender/nodes/composite/nodes/node_composite_mapRange.cc
+++ b/source/blender/nodes/composite/nodes/node_composite_mapRange.cc
@@ -37,12 +37,25 @@ static bNodeSocketTemplate cmp_node_map_range_out[] = {
     {-1, ""},
 };
 
+static int node_composite_gpu_map_range(GPUMaterial *mat,
+                                        bNode *node,
+                                        bNodeExecData *UNUSED(execdata),
+                                        GPUNodeStack *in,
+                                        GPUNodeStack *out)
+{
+  const float should_clamp = node->custom1 ? 1.0f : 0.0f;
+
+  return GPU_stack_link(
+      mat, node, "node_composite_map_range", in, out, 
GPU_constant(&should_clamp));
+}
+
 void register_node_type_cmp_map_range(void)
 {
   static bNodeType ntype;
 
   cmp_node_type_base(&ntype, CMP_NODE_MAP_RANGE, "Map Range", 
NODE_CLASS_OP_VECTOR, 0);
   node_type_socket_templates(&ntype, cmp_node_map_range_in, 
cmp_node_map_range_out);
+  node_type_gpu(&ntype, node_composite_gpu_map_range);
 
   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