SeparateRGB and CombineRGB nodes for Cycles materials
authorDalai Felinto <dfelinto@gmail.com>
Thu, 1 Dec 2011 21:46:10 +0000 (21:46 +0000)
committerDalai Felinto <dfelinto@gmail.com>
Thu, 1 Dec 2011 21:46:10 +0000 (21:46 +0000)
reviewed and approved by Brecht

my first OpenCL code \o/

intern/cycles/app/cycles_xml.cpp
intern/cycles/blender/blender_shader.cpp
intern/cycles/kernel/CMakeLists.txt
intern/cycles/kernel/osl/nodes/CMakeLists.txt
intern/cycles/kernel/osl/nodes/node_sepcomb_rgb.osl [new file with mode: 0644]
intern/cycles/kernel/svm/svm.h
intern/cycles/kernel/svm/svm_sepcomb_rgb.h [new file with mode: 0644]
intern/cycles/kernel/svm/svm_types.h
intern/cycles/render/nodes.cpp
intern/cycles/render/nodes.h
source/blender/nodes/shader/nodes/node_shader_sepcombRGB.c

index 5f9e1d7a5ffbb87c6012cc5e500b640abdc0ef8c..d89c30619b6a7631b63bb65fca480e6d529a65fc 100644 (file)
@@ -471,6 +471,12 @@ static void xml_read_shader_graph(const XMLReadState& state, Shader *shader, pug
                        xml_read_enum(&mix->type, MixNode::type_enum, node, "type");
                        snode = mix;
                }
+               else if(string_iequals(node.name(), "combine_rgb")) {
+                       snode = new CombineRGBNode();
+               }
+               else if(string_iequals(node.name(), "separate_rgb")) {
+                       snode = new SeparateRGBNode();
+               }
                else if(string_iequals(node.name(), "attribute")) {
                        AttributeNode *attr = new AttributeNode();
                        xml_read_ustring(&attr->attribute, node, "attribute");
index cf8527b9760dc36a89cde5a65145dd03a1fb7172..dfca9d7173a77255fd24ef5d23d366f0671a1d09 100644 (file)
@@ -128,7 +128,6 @@ static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Node *
        switch(b_node.type()) {
                /* not supported */
                case BL::ShaderNode::type_CAMERA: break;
-               case BL::ShaderNode::type_COMBRGB: break;
                case BL::ShaderNode::type_CURVE_RGB: break;
                case BL::ShaderNode::type_CURVE_VEC: break;
                case BL::ShaderNode::type_GEOMETRY: break;
@@ -139,7 +138,6 @@ static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Node *
                case BL::ShaderNode::type_NORMAL: break;
                case BL::ShaderNode::type_OUTPUT: break;
                case BL::ShaderNode::type_SCRIPT: break;
-               case BL::ShaderNode::type_SEPRGB: break;
                case BL::ShaderNode::type_SQUEEZE: break;
                case BL::ShaderNode::type_TEXTURE: break;
                case BL::ShaderNode::type_VALTORGB: break;
@@ -165,6 +163,14 @@ static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Node *
                        node = mix;
                        break;
                }
+               case BL::ShaderNode::type_SEPRGB: {
+                       node = new SeparateRGBNode();
+                       break;
+               }
+               case BL::ShaderNode::type_COMBRGB: {
+                       node = new CombineRGBNode();
+                       break;
+               }
                case BL::ShaderNode::type_RGBTOBW: {
                        node = new ConvertNode(SHADER_SOCKET_COLOR, SHADER_SOCKET_FLOAT);
                        break;
index 73425486be1f90cc675a007bcc86a9c3641e44bc..abe81103d8064795288531848644f5b3af199379 100644 (file)
@@ -69,6 +69,7 @@ set(SRC_SVM_HEADERS
        svm/svm_musgrave.h
        svm/svm_noise.h
        svm/svm_noisetex.h
+       svm/svm_sepcomb_rgb.h
        svm/svm_sky.h
        svm/svm_tex_coord.h
        svm/svm_texture.h
index 1b8b81eb6f6f948694d91be56b0107a7a1fd15ef..34729145f2809a675e8be7a5428b8c7fa2197a9d 100644 (file)
@@ -35,6 +35,7 @@ set(SRC_OSL
        node_output_displacement.osl
        node_output_surface.osl
        node_output_volume.osl
+       node_sepcomb_rgb.osl
        node_sky_texture.osl
        node_stucci_texture.osl
        node_texture_coordinate.osl
diff --git a/intern/cycles/kernel/osl/nodes/node_sepcomb_rgb.osl b/intern/cycles/kernel/osl/nodes/node_sepcomb_rgb.osl
new file mode 100644 (file)
index 0000000..a02f31f
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * 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_separate_rgb(
+       color Image = color(0.8, 0.8, 0.8),
+       output float R = 0.0,
+       output float G = 0.0,
+       output float B = 0.0)
+{
+               R = Image[0];
+               G = Image[1];
+               B = Image[2];
+}
+
+shader node_combine_rgb(
+       float R = 0.0,
+       float G = 0.0,
+       float B = 0.0,
+       output color Image = color(0.8, 0.8, 0.8)
+{
+       Image = color(R, G, B)
+}
+
index 2615272691c66b56d2172b1ae9f14046741cd30a..3073aefc27299e037e188ee15dc79a7b29c31563 100644 (file)
@@ -134,6 +134,7 @@ CCL_NAMESPACE_END
 #include "svm_wave.h"
 #include "svm_math.h"
 #include "svm_mix.h"
+#include "svm_sepcomb_rgb.h"
 #include "svm_musgrave.h"
 #include "svm_sky.h"
 #include "svm_tex_coord.h"
@@ -254,6 +255,12 @@ __device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ShaderT
                        case NODE_MIX:
                                svm_node_mix(kg, sd, stack, node.y, node.z, node.w, &offset);
                                break;
+                       case NODE_SEPARATE_RGB:
+                               svm_node_separate_rgb(sd, stack, node.y, node.z, node.w);
+                               break;
+                       case NODE_COMBINE_RGB:
+                               svm_node_combine_rgb(sd, stack, node.y, node.z, node.w);
+                               break;
                        case NODE_ATTR:
                                svm_node_attr(kg, sd, stack, node);
                                break;
diff --git a/intern/cycles/kernel/svm/svm_sepcomb_rgb.h b/intern/cycles/kernel/svm/svm_sepcomb_rgb.h
new file mode 100644 (file)
index 0000000..5d3afef
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * 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_combine_rgb(ShaderData *sd, float *stack, uint in_offset, uint color_index, uint out_offset)
+{
+       float color = stack_load_float(stack, in_offset);
+
+       if (stack_valid(out_offset))
+               stack_store_float(stack, out_offset+color_index, color);
+}
+
+__device void svm_node_separate_rgb(ShaderData *sd, float *stack, uint icolor_offset, uint color_index, uint out_offset)
+{
+       float3 color = stack_load_float3(stack, icolor_offset);
+
+       if (stack_valid(out_offset))
+               stack_store_float(stack, out_offset, color[color_index]); 
+}
+
+CCL_NAMESPACE_END
+
index 071477a83c70924bb6e7bfcfd463aa75d147c6d1..cde5e662fa7b4a997eedf77a8bf818ddb35b10ed 100644 (file)
@@ -79,7 +79,9 @@ typedef enum NodeType {
        NODE_TEX_ENVIRONMENT = 4600,
        NODE_CLOSURE_HOLDOUT = 4700,
        NODE_LAYER_WEIGHT = 4800,
-       NODE_CLOSURE_VOLUME = 4900
+       NODE_CLOSURE_VOLUME = 4900,
+       NODE_SEPARATE_RGB = 5000,
+       NODE_COMBINE_RGB = 5100
 } NodeType;
 
 typedef enum NodeAttributeType {
index d7bd74c9ec7eb14d9be3b5301c6cbaa5f7009383..0020a3c5d1b9423203b60f3346a7dde223dc3f5c 100644 (file)
@@ -1629,6 +1629,74 @@ void MixNode::compile(OSLCompiler& compiler)
        compiler.add(this, "node_mix");
 }
 
+/* Combine RGB */
+CombineRGBNode::CombineRGBNode()
+: ShaderNode("combine_rgb")
+{
+       add_input("R", SHADER_SOCKET_FLOAT);
+       add_input("G", SHADER_SOCKET_FLOAT);
+       add_input("B", SHADER_SOCKET_FLOAT);
+       add_output("Image", SHADER_SOCKET_COLOR);
+}
+
+void CombineRGBNode::compile(SVMCompiler& compiler)
+{
+       ShaderInput *red_in = input("R");
+       ShaderInput *green_in = input("G");
+       ShaderInput *blue_in = input("B");
+       ShaderOutput *color_out = output("Image");
+
+       compiler.stack_assign(color_out);
+
+       compiler.stack_assign(red_in);
+       compiler.add_node(NODE_COMBINE_RGB, red_in->stack_offset, 0, color_out->stack_offset);
+
+       compiler.stack_assign(green_in);
+       compiler.add_node(NODE_COMBINE_RGB, green_in->stack_offset, 1, color_out->stack_offset);
+
+       compiler.stack_assign(blue_in);
+       compiler.add_node(NODE_COMBINE_RGB, blue_in->stack_offset, 2, color_out->stack_offset);
+}
+
+void CombineRGBNode::compile(OSLCompiler& compiler)
+{
+       compiler.add(this, "node_combine_rgb");
+}
+
+/* Separate RGB */
+SeparateRGBNode::SeparateRGBNode()
+: ShaderNode("separate_rgb")
+{
+       add_input("Image", SHADER_SOCKET_COLOR);
+       add_output("R", SHADER_SOCKET_FLOAT);
+       add_output("G", SHADER_SOCKET_FLOAT);
+       add_output("B", SHADER_SOCKET_FLOAT);
+}
+
+void SeparateRGBNode::compile(SVMCompiler& compiler)
+{
+       ShaderInput *color_in = input("Image");
+       ShaderOutput *red_out = output("R");
+       ShaderOutput *green_out = output("G");
+       ShaderOutput *blue_out = output("B");
+
+       compiler.stack_assign(color_in);
+
+       compiler.stack_assign(red_out);
+       compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 0, red_out->stack_offset);
+
+       compiler.stack_assign(green_out);
+       compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 1, green_out->stack_offset);
+
+       compiler.stack_assign(blue_out);
+       compiler.add_node(NODE_SEPARATE_RGB, color_in->stack_offset, 2, blue_out->stack_offset);
+}
+
+void SeparateRGBNode::compile(OSLCompiler& compiler)
+{
+       compiler.add(this, "node_separate_rgb");
+}
+
 /* Attribute */
 
 AttributeNode::AttributeNode()
index d9cbd7ff5897c791a8109a043c7eb77828ad16c9..4cc76b0f71ea6b37b436199a904db2587341b88f 100644 (file)
@@ -292,6 +292,16 @@ public:
        static ShaderEnum type_enum;
 };
 
+class CombineRGBNode : public ShaderNode {
+public:
+       SHADER_NODE_CLASS(CombineRGBNode)
+};
+
+class SeparateRGBNode : public ShaderNode {
+public:
+       SHADER_NODE_CLASS(SeparateRGBNode)
+};
+
 class AttributeNode : public ShaderNode {
 public:
        SHADER_NODE_CLASS(AttributeNode)
index 09371a8a5ae03cd80ee4eef4a91301c6e505805e..bd4f084d15636aa16915b368bb081fcc761b8857 100644 (file)
@@ -61,7 +61,7 @@ void register_node_type_sh_seprgb(bNodeTreeType *ttype)
        static bNodeType ntype;
 
        node_type_base(ttype, &ntype, SH_NODE_SEPRGB, "Separate RGB", NODE_CLASS_CONVERTOR, 0);
-       node_type_compatibility(&ntype, NODE_OLD_SHADING);
+       node_type_compatibility(&ntype, NODE_OLD_SHADING|NODE_NEW_SHADING);
        node_type_socket_templates(&ntype, sh_node_seprgb_in, sh_node_seprgb_out);
        node_type_size(&ntype, 80, 40, 140);
        node_type_exec(&ntype, node_shader_exec_seprgb);
@@ -101,7 +101,7 @@ void register_node_type_sh_combrgb(bNodeTreeType *ttype)
        static bNodeType ntype;
 
        node_type_base(ttype, &ntype, SH_NODE_COMBRGB, "Combine RGB", NODE_CLASS_CONVERTOR, NODE_OPTIONS);
-       node_type_compatibility(&ntype, NODE_OLD_SHADING);
+       node_type_compatibility(&ntype, NODE_OLD_SHADING|NODE_NEW_SHADING);
        node_type_socket_templates(&ntype, sh_node_combrgb_in, sh_node_combrgb_out);
        node_type_size(&ntype, 80, 40, 140);
        node_type_exec(&ntype, node_shader_exec_combrgb);