[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [43666] trunk/blender: Brightness/ Contrast Node for Cycles

Dalai Felinto dfelinto at gmail.com
Tue Jan 24 17:32:41 CET 2012


Revision: 43666
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=43666
Author:   dfelinto
Date:     2012-01-24 16:32:31 +0000 (Tue, 24 Jan 2012)
Log Message:
-----------
Brightness/Contrast Node for Cycles

Contrast helps to adjust IBL (HDR images used for background lighting).
Note: In the UI we are caling it Bright instead of Brightness. This copy what Blender composite is doing.
Note2: the algorithm we are using produces pure black when contrast is 100. I'm not a fan of that, but it's a division by zero. I would like to look at other algorithms (what gimp does for example). But that would be only after 2.62.

Modified Paths:
--------------
    trunk/blender/intern/cycles/app/cycles_xml.cpp
    trunk/blender/intern/cycles/blender/blender_shader.cpp
    trunk/blender/intern/cycles/kernel/CMakeLists.txt
    trunk/blender/intern/cycles/kernel/osl/nodes/CMakeLists.txt
    trunk/blender/intern/cycles/kernel/svm/svm.h
    trunk/blender/intern/cycles/kernel/svm/svm_types.h
    trunk/blender/intern/cycles/render/nodes.cpp
    trunk/blender/intern/cycles/render/nodes.h
    trunk/blender/source/blender/blenkernel/BKE_node.h
    trunk/blender/source/blender/blenkernel/intern/node.c
    trunk/blender/source/blender/makesrna/intern/rna_nodetree_types.h
    trunk/blender/source/blender/nodes/CMakeLists.txt
    trunk/blender/source/blender/nodes/NOD_shader.h
    trunk/blender/source/blender/nodes/shader/nodes/node_shader_gamma.c

Added Paths:
-----------
    trunk/blender/intern/cycles/kernel/osl/nodes/node_brightness.osl
    trunk/blender/intern/cycles/kernel/svm/svm_brightness.h
    trunk/blender/source/blender/nodes/shader/nodes/node_shader_brightness.c

Modified: trunk/blender/intern/cycles/app/cycles_xml.cpp
===================================================================
--- trunk/blender/intern/cycles/app/cycles_xml.cpp	2012-01-24 16:18:20 UTC (rev 43665)
+++ trunk/blender/intern/cycles/app/cycles_xml.cpp	2012-01-24 16:32:31 UTC (rev 43666)
@@ -462,6 +462,9 @@
 		else if(string_iequals(node.name(), "gamma")) {
 			snode = new GammaNode();
 		}
+		else if(string_iequals(node.name(), "brightness")) {
+			snode = new BrightContrastNode();
+		}
 		else if(string_iequals(node.name(), "combine_rgb")) {
 			snode = new CombineRGBNode();
 		}

Modified: trunk/blender/intern/cycles/blender/blender_shader.cpp
===================================================================
--- trunk/blender/intern/cycles/blender/blender_shader.cpp	2012-01-24 16:18:20 UTC (rev 43665)
+++ trunk/blender/intern/cycles/blender/blender_shader.cpp	2012-01-24 16:32:31 UTC (rev 43666)
@@ -142,6 +142,10 @@
 			node = new GammaNode();
 			break;
 		}
+		case BL::ShaderNode::type_BRIGHTCONTRAST: {
+			node = new BrightContrastNode();
+			break;
+		}
 		case BL::ShaderNode::type_MIX_RGB: {
 			BL::ShaderNodeMixRGB b_mix_node(b_node);
 			MixNode *mix = new MixNode();

Modified: trunk/blender/intern/cycles/kernel/CMakeLists.txt
===================================================================
--- trunk/blender/intern/cycles/kernel/CMakeLists.txt	2012-01-24 16:18:20 UTC (rev 43665)
+++ trunk/blender/intern/cycles/kernel/CMakeLists.txt	2012-01-24 16:32:31 UTC (rev 43666)
@@ -61,6 +61,7 @@
 	svm/svm_displace.h
 	svm/svm_fresnel.h
 	svm/svm_gamma.h
+	svm/svm_brightness.h
 	svm/svm_geometry.h
 	svm/svm_gradient.h
 	svm/svm_hsv.h

Modified: trunk/blender/intern/cycles/kernel/osl/nodes/CMakeLists.txt
===================================================================
--- trunk/blender/intern/cycles/kernel/osl/nodes/CMakeLists.txt	2012-01-24 16:18:20 UTC (rev 43665)
+++ trunk/blender/intern/cycles/kernel/osl/nodes/CMakeLists.txt	2012-01-24 16:32:31 UTC (rev 43666)
@@ -20,6 +20,7 @@
 	node_environment_texture.osl
 	node_fresnel.osl
 	node_gamma.osl
+	node_brightness.osl
 	node_geometry.osl
 	node_glass_bsdf.osl
 	node_glossy_bsdf.osl

Added: trunk/blender/intern/cycles/kernel/osl/nodes/node_brightness.osl
===================================================================
--- trunk/blender/intern/cycles/kernel/osl/nodes/node_brightness.osl	                        (rev 0)
+++ trunk/blender/intern/cycles/kernel/osl/nodes/node_brightness.osl	2012-01-24 16:32:31 UTC (rev 43666)
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2011, Blender Foundation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "stdosl.h"
+
+shader node_brightness(
+	color ColorIn = color(0.8, 0.8, 0.8),
+	float Bright = 0.0,
+	float Contrast = 0.0,
+	output ColorOut = color(0.8, 0.8, 0.8)
+{
+	float delta = Contrast * (1.0/200.0);
+	float a = 1.0 - delta * 2.0;
+	float b;
+
+	Bright *= 1.0/100.0;
+
+	/*
+	* The algorithm is by Werner D. Streidt
+	* (http://visca.com/ffactory/archives/5-99/msg00021.html)
+	* Extracted of OpenCV demhist.c
+	*/
+
+	if (Contrast > 0.0) {
+		a = (a < 0.0 ? 1.0/a : 0.0);
+		b = a * (Brightness - delta);
+	}
+	else {
+		delta *= -1.0;
+		b = a * (Brightness + delta);
+	}
+
+	ColorOut = a * ColorIn + b;
+}
+


Property changes on: trunk/blender/intern/cycles/kernel/osl/nodes/node_brightness.osl
___________________________________________________________________
Added: svn:eol
   + native

Modified: trunk/blender/intern/cycles/kernel/svm/svm.h
===================================================================
--- trunk/blender/intern/cycles/kernel/svm/svm.h	2012-01-24 16:18:20 UTC (rev 43665)
+++ trunk/blender/intern/cycles/kernel/svm/svm.h	2012-01-24 16:32:31 UTC (rev 43666)
@@ -131,6 +131,7 @@
 #include "svm_hsv.h"
 #include "svm_image.h"
 #include "svm_gamma.h"
+#include "svm_brightness.h"
 #include "svm_invert.h"
 #include "svm_light_path.h"
 #include "svm_magic.h"
@@ -270,6 +271,9 @@
 			case NODE_GAMMA:
 				svm_node_gamma(sd, stack, node.y, node.z, node.w);
 				break;
+			case NODE_BRIGHTCONTRAST:
+				svm_node_brightness(sd, stack, node.y, node.z, node.w);
+				break;
 			case NODE_MIX:
 				svm_node_mix(kg, sd, stack, node.y, node.z, node.w, &offset);
 				break;

Added: trunk/blender/intern/cycles/kernel/svm/svm_brightness.h
===================================================================
--- trunk/blender/intern/cycles/kernel/svm/svm_brightness.h	                        (rev 0)
+++ trunk/blender/intern/cycles/kernel/svm/svm_brightness.h	2012-01-24 16:32:31 UTC (rev 43666)
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2011, Blender Foundation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+CCL_NAMESPACE_BEGIN
+
+__device void svm_node_brightness(ShaderData *sd, float *stack, uint in_color, uint out_color, uint node)
+{
+	uint bright_offset, contrast_offset;
+	float3 color = stack_load_float3(stack, in_color);
+
+	decode_node_uchar4(node, &bright_offset, &contrast_offset, NULL, NULL);
+	float brightness = stack_load_float(stack, bright_offset);
+	float contrast  = stack_load_float(stack, contrast_offset);
+
+	brightness *= 1.0f/100.0f;
+	float delta = contrast * (1.0f/200.0f);
+	float a = 1.0f - delta * 2.0f;
+	float b;
+
+	/*
+	* The algorithm is by Werner D. Streidt
+	* (http://visca.com/ffactory/archives/5-99/msg00021.html)
+	* Extracted of OpenCV demhist.c
+	*/
+	if (contrast > 0.0f) {
+		a = (a > 0.0f? (1.0f / a): 0.0f);
+		b = a * (brightness - delta);
+	}
+	else {
+		delta *= -1.0f;
+		b = a * (brightness + delta);
+	}
+
+	color.x = a*color.x + b;
+	color.y = a*color.y + b;
+	color.z = a*color.z + b;
+
+	if (stack_valid(out_color))
+		stack_store_float3(stack, out_color, color);
+}
+
+CCL_NAMESPACE_END


Property changes on: trunk/blender/intern/cycles/kernel/svm/svm_brightness.h
___________________________________________________________________
Added: svn:eol
   + native

Modified: trunk/blender/intern/cycles/kernel/svm/svm_types.h
===================================================================
--- trunk/blender/intern/cycles/kernel/svm/svm_types.h	2012-01-24 16:18:20 UTC (rev 43665)
+++ trunk/blender/intern/cycles/kernel/svm/svm_types.h	2012-01-24 16:32:31 UTC (rev 43666)
@@ -87,7 +87,8 @@
 	NODE_INVERT = 5400,
 	NODE_NORMAL = 5500,
 	NODE_GAMMA = 5600,
-	NODE_TEX_CHECKER = 5700
+	NODE_TEX_CHECKER = 5700,
+	NODE_BRIGHTCONTRAST = 5800
 } NodeType;
 
 typedef enum NodeAttributeType {

Modified: trunk/blender/intern/cycles/render/nodes.cpp
===================================================================
--- trunk/blender/intern/cycles/render/nodes.cpp	2012-01-24 16:18:20 UTC (rev 43665)
+++ trunk/blender/intern/cycles/render/nodes.cpp	2012-01-24 16:32:31 UTC (rev 43666)
@@ -1820,6 +1820,38 @@
 	compiler.add(this, "node_gamma");
 }
 
+/* Bright Contrast */
+BrightContrastNode::BrightContrastNode()
+: ShaderNode("brightness")
+{
+	add_input("Color", SHADER_SOCKET_COLOR);
+	add_input("Bright", SHADER_SOCKET_FLOAT);
+	add_input("Contrast", SHADER_SOCKET_FLOAT);
+	add_output("Color", SHADER_SOCKET_COLOR);
+}
+
+void BrightContrastNode::compile(SVMCompiler& compiler)
+{
+	ShaderInput *color_in = input("Color");
+	ShaderInput *bright_in = input("Bright");
+	ShaderInput *contrast_in = input("Contrast");
+	ShaderOutput *color_out = output("Color");
+
+	compiler.stack_assign(color_in);
+	compiler.stack_assign(bright_in);
+	compiler.stack_assign(contrast_in);
+	compiler.stack_assign(color_out);
+
+	compiler.add_node(NODE_BRIGHTCONTRAST,
+		color_in->stack_offset, color_out->stack_offset,
+		compiler.encode_uchar4(bright_in->stack_offset, contrast_in->stack_offset));
+}
+
+void BrightContrastNode::compile(OSLCompiler& compiler)
+{
+	compiler.add(this, "node_brightness");
+}
+
 /* Separate RGB */
 SeparateRGBNode::SeparateRGBNode()
 : ShaderNode("separate_rgb")

Modified: trunk/blender/intern/cycles/render/nodes.h
===================================================================
--- trunk/blender/intern/cycles/render/nodes.h	2012-01-24 16:18:20 UTC (rev 43665)
+++ trunk/blender/intern/cycles/render/nodes.h	2012-01-24 16:32:31 UTC (rev 43666)
@@ -320,6 +320,11 @@
 	SHADER_NODE_CLASS(GammaNode)
 };
 
+class BrightContrastNode : public ShaderNode {
+public:
+	SHADER_NODE_CLASS(BrightContrastNode)
+};
+
 class SeparateRGBNode : public ShaderNode {
 public:
 	SHADER_NODE_CLASS(SeparateRGBNode)

Modified: trunk/blender/source/blender/blenkernel/BKE_node.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_node.h	2012-01-24 16:18:20 UTC (rev 43665)
+++ trunk/blender/source/blender/blenkernel/BKE_node.h	2012-01-24 16:32:31 UTC (rev 43666)
@@ -523,6 +523,7 @@
 #define SH_NODE_VOLUME_ISOTROPIC		162
 #define SH_NODE_GAMMA				163
 #define SH_NODE_TEX_CHECKER			164
+#define SH_NODE_BRIGHTCONTRAST			165
 
 /* custom defines options for Material node */
 #define SH_NODE_MAT_DIFF   1

Modified: trunk/blender/source/blender/blenkernel/intern/node.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/node.c	2012-01-24 16:18:20 UTC (rev 43665)

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-blender-cvs mailing list