[Bf-blender-cvs] [fe5d2448c6e] master: Fix T83494: Eevee clamp node incorrect when min > max.

Jeroen Bakker noreply at git.blender.org
Fri Dec 18 10:30:19 CET 2020


Commit: fe5d2448c6e1348be1f82f10a65d65b992f2477b
Author: Jeroen Bakker
Date:   Fri Dec 18 10:26:02 2020 +0100
Branches: master
https://developer.blender.org/rBfe5d2448c6e1348be1f82f10a65d65b992f2477b

Fix T83494: Eevee clamp node incorrect when min > max.

In glsl the clamp function has undefined behavior when min > max. For
the clamp node this resulted in differences between cycles and eevee.
This patch adds the expected implementation for minmax.

The old clamp function is still used in cases where we know for certain
that the input values are correct (math node clamp option). GPU uses
optimized code and silicon in these cases.

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

M	source/blender/gpu/shaders/material/gpu_shader_material_clamp.glsl
M	source/blender/nodes/shader/nodes/node_shader_clamp.cc

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

diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_clamp.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_clamp.glsl
index b196aed690f..f89608feff1 100644
--- a/source/blender/gpu/shaders/material/gpu_shader_material_clamp.glsl
+++ b/source/blender/gpu/shaders/material/gpu_shader_material_clamp.glsl
@@ -3,6 +3,11 @@ void clamp_value(float value, float min, float max, out float result)
   result = clamp(value, min, max);
 }
 
+void clamp_minmax(float value, float min_allowed, float max_allowed, out float result)
+{
+  result = min(max(value, min_allowed), max_allowed);
+}
+
 void clamp_range(float value, float min, float max, out float result)
 {
   result = (max > min) ? clamp(value, min, max) : clamp(value, max, min);
diff --git a/source/blender/nodes/shader/nodes/node_shader_clamp.cc b/source/blender/nodes/shader/nodes/node_shader_clamp.cc
index d3a893e1d76..4f77421cfe0 100644
--- a/source/blender/nodes/shader/nodes/node_shader_clamp.cc
+++ b/source/blender/nodes/shader/nodes/node_shader_clamp.cc
@@ -46,8 +46,9 @@ static int gpu_shader_clamp(GPUMaterial *mat,
                             GPUNodeStack *in,
                             GPUNodeStack *out)
 {
-  return (node->custom1 == NODE_CLAMP_MINMAX) ? GPU_stack_link(mat, node, "clamp_value", in, out) :
-                                                GPU_stack_link(mat, node, "clamp_range", in, out);
+  return (node->custom1 == NODE_CLAMP_MINMAX) ?
+             GPU_stack_link(mat, node, "clamp_minmax", in, out) :
+             GPU_stack_link(mat, node, "clamp_range", in, out);
 }
 
 static void sh_node_clamp_expand_in_mf_network(blender::nodes::NodeMFNetworkBuilder &builder)



More information about the Bf-blender-cvs mailing list