diff options
author | Dalai Felinto <dfelinto@gmail.com> | 2012-01-24 20:32:31 +0400 |
---|---|---|
committer | Dalai Felinto <dfelinto@gmail.com> | 2012-01-24 20:32:31 +0400 |
commit | 335ffb0ff3df6ee52f525448d09ae6448b75e158 (patch) | |
tree | 460fc93c618d05f0585405c61dcc5152ee906798 /intern | |
parent | 1f9e25ac1a7851ab2503b88564c0d480b9e125cf (diff) |
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.
Diffstat (limited to 'intern')
-rw-r--r-- | intern/cycles/app/cycles_xml.cpp | 3 | ||||
-rw-r--r-- | intern/cycles/blender/blender_shader.cpp | 4 | ||||
-rw-r--r-- | intern/cycles/kernel/CMakeLists.txt | 1 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/nodes/CMakeLists.txt | 1 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/nodes/node_brightness.osl | 50 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm.h | 4 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm_brightness.h | 57 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm_types.h | 3 | ||||
-rw-r--r-- | intern/cycles/render/nodes.cpp | 32 | ||||
-rw-r--r-- | intern/cycles/render/nodes.h | 5 |
10 files changed, 159 insertions, 1 deletions
diff --git a/intern/cycles/app/cycles_xml.cpp b/intern/cycles/app/cycles_xml.cpp index af52520d87c..5e4b3da071d 100644 --- a/intern/cycles/app/cycles_xml.cpp +++ b/intern/cycles/app/cycles_xml.cpp @@ -462,6 +462,9 @@ static void xml_read_shader_graph(const XMLReadState& state, Shader *shader, pug 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(); } diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp index 1ce134f3094..f7b750e6dab 100644 --- a/intern/cycles/blender/blender_shader.cpp +++ b/intern/cycles/blender/blender_shader.cpp @@ -142,6 +142,10 @@ static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Shader 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(); diff --git a/intern/cycles/kernel/CMakeLists.txt b/intern/cycles/kernel/CMakeLists.txt index a4e0c0ddb56..b6e749f3fcb 100644 --- a/intern/cycles/kernel/CMakeLists.txt +++ b/intern/cycles/kernel/CMakeLists.txt @@ -61,6 +61,7 @@ set(SRC_SVM_HEADERS 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 diff --git a/intern/cycles/kernel/osl/nodes/CMakeLists.txt b/intern/cycles/kernel/osl/nodes/CMakeLists.txt index 1a207d6c139..d3a1cf59a37 100644 --- a/intern/cycles/kernel/osl/nodes/CMakeLists.txt +++ b/intern/cycles/kernel/osl/nodes/CMakeLists.txt @@ -20,6 +20,7 @@ set(SRC_OSL node_environment_texture.osl node_fresnel.osl node_gamma.osl + node_brightness.osl node_geometry.osl node_glass_bsdf.osl node_glossy_bsdf.osl diff --git a/intern/cycles/kernel/osl/nodes/node_brightness.osl b/intern/cycles/kernel/osl/nodes/node_brightness.osl new file mode 100644 index 00000000000..a93ff06d443 --- /dev/null +++ b/intern/cycles/kernel/osl/nodes/node_brightness.osl @@ -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; +} + diff --git a/intern/cycles/kernel/svm/svm.h b/intern/cycles/kernel/svm/svm.h index d22c9181a84..f1bae9c8d9f 100644 --- a/intern/cycles/kernel/svm/svm.h +++ b/intern/cycles/kernel/svm/svm.h @@ -131,6 +131,7 @@ CCL_NAMESPACE_END #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 @@ __device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ShaderT 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; diff --git a/intern/cycles/kernel/svm/svm_brightness.h b/intern/cycles/kernel/svm/svm_brightness.h new file mode 100644 index 00000000000..698899ffd60 --- /dev/null +++ b/intern/cycles/kernel/svm/svm_brightness.h @@ -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 diff --git a/intern/cycles/kernel/svm/svm_types.h b/intern/cycles/kernel/svm/svm_types.h index 8203ed4c15e..ff6e9b94a0f 100644 --- a/intern/cycles/kernel/svm/svm_types.h +++ b/intern/cycles/kernel/svm/svm_types.h @@ -87,7 +87,8 @@ typedef enum NodeType { NODE_INVERT = 5400, NODE_NORMAL = 5500, NODE_GAMMA = 5600, - NODE_TEX_CHECKER = 5700 + NODE_TEX_CHECKER = 5700, + NODE_BRIGHTCONTRAST = 5800 } NodeType; typedef enum NodeAttributeType { diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp index 7397c9c09af..982521b31f2 100644 --- a/intern/cycles/render/nodes.cpp +++ b/intern/cycles/render/nodes.cpp @@ -1820,6 +1820,38 @@ void GammaNode::compile(OSLCompiler& compiler) 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") diff --git a/intern/cycles/render/nodes.h b/intern/cycles/render/nodes.h index c2f9ead5924..338af4ef48a 100644 --- a/intern/cycles/render/nodes.h +++ b/intern/cycles/render/nodes.h @@ -320,6 +320,11 @@ public: SHADER_NODE_CLASS(GammaNode) }; +class BrightContrastNode : public ShaderNode { +public: + SHADER_NODE_CLASS(BrightContrastNode) +}; + class SeparateRGBNode : public ShaderNode { public: SHADER_NODE_CLASS(SeparateRGBNode) |