Cycles: add ColorRamp node.
authorBrecht Van Lommel <brechtvanlommel@pandora.be>
Mon, 26 Mar 2012 12:45:14 +0000 (12:45 +0000)
committerBrecht Van Lommel <brechtvanlommel@pandora.be>
Mon, 26 Mar 2012 12:45:14 +0000 (12:45 +0000)
13 files changed:
intern/cycles/blender/blender_shader.cpp
intern/cycles/blender/blender_util.h
intern/cycles/kernel/CMakeLists.txt
intern/cycles/kernel/kernel_types.h
intern/cycles/kernel/svm/svm.h
intern/cycles/kernel/svm/svm_ramp.h [new file with mode: 0644]
intern/cycles/kernel/svm/svm_types.h
intern/cycles/render/nodes.cpp
intern/cycles/render/nodes.h
intern/cycles/render/svm.cpp
intern/cycles/render/svm.h
source/blender/makesrna/intern/rna_color.c
source/blender/nodes/shader/nodes/node_shader_valToRgb.c

index 5c26a4d216398bdae8afc16a517cd08a63ec444d..bea434bb7b66fb5cf2b9d7a139496c2164bd1cee 100644 (file)
@@ -105,7 +105,6 @@ static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Shader
 
        switch(b_node.type()) {
                /* not supported */
-               case BL::ShaderNode::type_CURVE_RGB: break;
                case BL::ShaderNode::type_CURVE_VEC: break;
                case BL::ShaderNode::type_GEOMETRY: break;
                case BL::ShaderNode::type_MATERIAL: break;
@@ -114,10 +113,21 @@ static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Shader
                case BL::ShaderNode::type_SCRIPT: break;
                case BL::ShaderNode::type_SQUEEZE: break;
                case BL::ShaderNode::type_TEXTURE: break;
-               case BL::ShaderNode::type_VALTORGB: break;
                /* handled outside this function */
                case BL::ShaderNode::type_GROUP: break;
                /* existing blender nodes */
+               case BL::ShaderNode::type_CURVE_RGB: {
+                       RGBCurvesNode *ramp = new RGBCurvesNode();
+                       node = ramp;
+                       break;
+               }
+               case BL::ShaderNode::type_VALTORGB: {
+                       RGBRampNode *ramp = new RGBRampNode();
+                       BL::ShaderNodeValToRGB b_ramp_node(b_node);
+                       colorramp_to_array(b_ramp_node.color_ramp(), ramp->ramp, RAMP_TABLE_SIZE);
+                       node = ramp;
+                       break;
+               }
                case BL::ShaderNode::type_RGB: {
                        ColorNode *color = new ColorNode();
                        color->value = get_node_output_rgba(b_node, "Color");
index ff6d55c6f3ed663ad486e79d66e91ca9232c2a90..67f3a3ab7d90672c2ec89eb96edc981e90610124 100644 (file)
@@ -50,6 +50,7 @@ void engine_tag_redraw(void *engine);
 void engine_tag_update(void *engine);
 int rna_Object_is_modified(void *ob, void *scene, int settings);
 void BLI_timestr(double _time, char *str);
+void rna_ColorRamp_eval(void *coba, float position, float color[4]);
 
 }
 
@@ -63,6 +64,16 @@ static inline BL::Mesh object_to_mesh(BL::Object self, BL::Scene scene, bool app
        return BL::Mesh(ptr);
 }
 
+static inline void colorramp_to_array(BL::ColorRamp ramp, float4 *data, int size)
+{
+       for(int i = 0; i < size; i++) {
+               float color[4];
+
+               rna_ColorRamp_eval(ramp.ptr.data, i/(float)(size-1), color);
+               data[i] = make_float4(color[0], color[1], color[2], color[3]);
+       }
+}
+
 static inline void object_remove_mesh(BL::BlendData data, BL::Mesh mesh)
 {
        rna_Main_meshes_remove(data.ptr.data, NULL, mesh.ptr.data);
index bef5c7af789ca01bf3ec3d789f05a066cd390ff9..e9820010b6360e2fa6f1af8286fbe9b9b144015c 100644 (file)
@@ -78,6 +78,7 @@ set(SRC_SVM_HEADERS
        svm/svm_noise.h
        svm/svm_noisetex.h
        svm/svm_normal.h
+       svm/svm_ramp.h
        svm/svm_sepcomb_rgb.h
        svm/svm_sky.h
        svm/svm_tex_coord.h
index e5e03cedc1813ee23cf95c273484234a2319967e..c198ee1d24e83a5309362225e3cab2de15b4bd82 100644 (file)
@@ -29,6 +29,7 @@ CCL_NAMESPACE_BEGIN
 #define OBJECT_SIZE            16
 #define LIGHT_SIZE                     4
 #define FILTER_TABLE_SIZE      256
+#define RAMP_TABLE_SIZE                256
 
 /* device capabilities */
 #ifdef __KERNEL_CPU__
index f1bae9c8d9f9f88dcc24f29fa6a2640825ddf520..50181c0cf2c29c4341aaa4d7d27b9a193b62bd91 100644 (file)
@@ -104,6 +104,12 @@ __device_inline float4 read_node_float(KernelGlobals *kg, int *offset)
        return f;
 }
 
+__device_inline float4 fetch_node_float(KernelGlobals *kg, int offset)
+{
+       uint4 node = kernel_tex_fetch(__svm_nodes, offset);
+       return make_float4(__int_as_float(node.x), __int_as_float(node.y), __int_as_float(node.z), __int_as_float(node.w));
+}
+
 __device_inline void decode_node_uchar4(uint i, uint *x, uint *y, uint *z, uint *w)
 {
        if(x) *x = (i & 0xFF);
@@ -140,6 +146,7 @@ CCL_NAMESPACE_END
 #include "svm_wave.h"
 #include "svm_math.h"
 #include "svm_mix.h"
+#include "svm_ramp.h"
 #include "svm_sepcomb_rgb.h"
 #include "svm_musgrave.h"
 #include "svm_sky.h"
@@ -331,6 +338,12 @@ __device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ShaderT
                        case NODE_EMISSION_SET_WEIGHT_TOTAL:
                                svm_node_emission_set_weight_total(kg, sd, node.y, node.z, node.w);
                                break;
+                       case NODE_RGB_RAMP:
+                               svm_node_rgb_ramp(kg, sd, stack, node, &offset);
+                               break;
+                       case NODE_RGB_CURVES:
+                               svm_node_rgb_curves(kg, sd, stack, node, &offset);
+                               break;
                        case NODE_END:
                        default:
 #ifndef __MULTI_CLOSURE__
diff --git a/intern/cycles/kernel/svm/svm_ramp.h b/intern/cycles/kernel/svm/svm_ramp.h
new file mode 100644 (file)
index 0000000..0654240
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * 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.
+ */
+
+#ifndef __SVM_RAMP_H__
+#define __SVM_RAMP_H__
+
+CCL_NAMESPACE_BEGIN
+
+__device float4 rgb_ramp_lookup(KernelGlobals *kg, int offset, float f)
+{
+       f = clamp(f, 0.0f, 1.0f)*(RAMP_TABLE_SIZE-1);
+
+       int i = (int)f;
+       float t = f - (float)i;
+
+       float4 a = fetch_node_float(kg, offset+i);
+
+       if(t > 0.0f)
+               a = (1.0f - t)*a + t*fetch_node_float(kg, offset+i+1);
+
+       return a;
+}
+
+__device void svm_node_rgb_ramp(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
+{
+       uint fac_offset = node.y;
+       uint color_offset = node.z;
+
+       float fac = stack_load_float(stack, fac_offset);
+       float4 color = rgb_ramp_lookup(kg, *offset, fac);
+
+       stack_store_float3(stack, color_offset, float4_to_float3(color));
+
+       *offset += RAMP_TABLE_SIZE;
+}
+
+__device void svm_node_rgb_curves(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
+{
+       uint fac_offset = node.y;
+       uint color_offset = node.z;
+       uint out_offset = node.w;
+
+       float fac = stack_load_float(stack, fac_offset);
+       float3 color = stack_load_float3(stack, color_offset);
+
+       float r = rgb_ramp_lookup(kg, *offset, rgb_ramp_lookup(kg, *offset, color.x).w).x;
+       float g = rgb_ramp_lookup(kg, *offset, rgb_ramp_lookup(kg, *offset, color.y).w).y;
+       float b = rgb_ramp_lookup(kg, *offset, rgb_ramp_lookup(kg, *offset, color.z).w).z;
+
+       color = (1.0f - fac)*color + fac*make_float3(r, g, b);
+       stack_store_float3(stack, out_offset, color);
+
+       *offset += RAMP_TABLE_SIZE;
+}
+
+CCL_NAMESPACE_END
+
+#endif /* __SVM_RAMP_H__ */
+
index 533a2944557defa0ffde284646d498658a776f77..13d72307765ee2e8f07e86c9ea2563f51c5b97f4 100644 (file)
@@ -88,7 +88,9 @@ typedef enum NodeType {
        NODE_NORMAL = 5500,
        NODE_GAMMA = 5600,
        NODE_TEX_CHECKER = 5700,
-       NODE_BRIGHTCONTRAST = 5800
+       NODE_BRIGHTCONTRAST = 5800,
+       NODE_RGB_RAMP = 5900,
+       NODE_RGB_CURVES = 6000
 } NodeType;
 
 typedef enum NodeAttributeType {
index a98352aa85a7accb3a302c288efa7b9e297f1932..45ba18469d69e912b122287d998dfbe6807aed11 100644 (file)
@@ -2352,5 +2352,60 @@ void BumpNode::compile(OSLCompiler& compiler)
        compiler.add(this, "node_bump");
 }
 
+/* RGBCurvesNode */
+
+RGBCurvesNode::RGBCurvesNode()
+: ShaderNode("rgb_curves")
+{
+       add_input("Fac", SHADER_SOCKET_FLOAT);
+       add_input("Color", SHADER_SOCKET_COLOR);
+       add_output("Color", SHADER_SOCKET_COLOR);
+}
+
+void RGBCurvesNode::compile(SVMCompiler& compiler)
+{
+       ShaderInput *fac_in = input("Fac");
+       ShaderInput *color_in = input("Color");
+       ShaderOutput *color_out = output("Color");
+
+       compiler.stack_assign(fac_in);
+       compiler.stack_assign(color_in);
+       compiler.stack_assign(color_out);
+
+       compiler.add_node(NODE_RGB_CURVES, fac_in->stack_offset, color_in->stack_offset, color_out->stack_offset);
+       compiler.add_array(curves, RAMP_TABLE_SIZE);
+}
+
+void RGBCurvesNode::compile(OSLCompiler& compiler)
+{
+       compiler.add(this, "node_rgb_curves");
+}
+
+/* RGBRampNode */
+
+RGBRampNode::RGBRampNode()
+: ShaderNode("rgb_ramp")
+{
+       add_input("Fac", SHADER_SOCKET_FLOAT);
+       add_output("Color", SHADER_SOCKET_COLOR);
+}
+
+void RGBRampNode::compile(SVMCompiler& compiler)
+{
+       ShaderInput *fac_in = input("Fac");
+       ShaderOutput *color_out = output("Color");
+
+       compiler.stack_assign(fac_in);
+       compiler.stack_assign(color_out);
+
+       compiler.add_node(NODE_RGB_RAMP, fac_in->stack_offset, color_out->stack_offset);
+       compiler.add_array(ramp, RAMP_TABLE_SIZE);
+}
+
+void RGBRampNode::compile(OSLCompiler& compiler)
+{
+       compiler.add(this, "node_rgb_ramp");
+}
+
 CCL_NAMESPACE_END
 
index e0329cb9b1d156ed25e78eeb07a9ac34a9016d24..364209f8c5ee1c81c5296bf3fa4e2131a99d8645 100644 (file)
@@ -390,6 +390,18 @@ public:
        SHADER_NODE_CLASS(BumpNode)
 };
 
+class RGBCurvesNode : public ShaderNode {
+public:
+       SHADER_NODE_CLASS(RGBCurvesNode)
+       float4 curves[RAMP_TABLE_SIZE];
+};
+
+class RGBRampNode : public ShaderNode {
+public:
+       SHADER_NODE_CLASS(RGBRampNode)
+       float4 ramp[RAMP_TABLE_SIZE];
+};
+
 CCL_NAMESPACE_END
 
 #endif /* __NODES_H__ */
index 46c6149ab32da03c0af7093d38c31dbc1f24b066..a52e30c6030f364536197b01cc72ae469fa99a06 100644 (file)
@@ -326,6 +326,12 @@ void SVMCompiler::add_node(const float4& f)
                __float_as_int(f.w)));
 }
 
+void SVMCompiler::add_array(float4 *f, int num)
+{
+       for(int i = 0; i < num; i++)
+               add_node(f[i]);
+}
+
 uint SVMCompiler::attribute(ustring name)
 {
        return shader_manager->get_attribute_id(name);
index d8a1b14d637f6df7e4e6b2b22986e3636a09d72f..56c930f6217a111446d57c46837be38097cbab82 100644 (file)
@@ -67,6 +67,7 @@ public:
        void add_node(int a = 0, int b = 0, int c = 0, int d = 0);
        void add_node(NodeType type, const float3& f);
        void add_node(const float4& f);
+       void add_array(float4 *f, int num);
        uint attribute(ustring name);
        uint attribute(Attribute::Standard std);
        uint encode_uchar4(uint x, uint y = 0, uint z = 0, uint w = 0);
index 8ced52bd63c683ade23ad8b2a2c366a8aa5b0e03..6ea59dc12e5255bf0d06079b26edcb55e3fe6957 100644 (file)
@@ -276,7 +276,7 @@ static void rna_ColorRamp_update(Main *bmain, Scene *UNUSED(scene), PointerRNA *
        }
 }
 
-static void rna_ColorRamp_eval(struct ColorBand *coba, float position, float color[4])
+void rna_ColorRamp_eval(struct ColorBand *coba, float position, float color[4])
 {
        do_colorband(coba, position, color);
 }
index a9513eec6ea67431ff2aa99c3ee453014dfe44fc..7e513135203fb15e7c25672bdc7e44ca271c8905 100644 (file)
@@ -76,7 +76,7 @@ void register_node_type_sh_valtorgb(bNodeTreeType *ttype)
        static bNodeType ntype;
 
        node_type_base(ttype, &ntype, SH_NODE_VALTORGB, "ColorRamp", 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_valtorgb_in, sh_node_valtorgb_out);
        node_type_size(&ntype, 240, 200, 300);
        node_type_init(&ntype, node_shader_init_valtorgb);