diff options
author | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2012-03-26 16:45:14 +0400 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2012-03-26 16:45:14 +0400 |
commit | bbc3d820f44ca62810c1800cf7caa3905b6116a6 (patch) | |
tree | 3cb45928cc5e8e758b3cabd18ebe79a763f162d5 /intern/cycles | |
parent | 56baed13da0e3cf1206afd17f65bba00ec4a287c (diff) |
Cycles: add ColorRamp node.
Diffstat (limited to 'intern/cycles')
-rw-r--r-- | intern/cycles/blender/blender_shader.cpp | 14 | ||||
-rw-r--r-- | intern/cycles/blender/blender_util.h | 11 | ||||
-rw-r--r-- | intern/cycles/kernel/CMakeLists.txt | 1 | ||||
-rw-r--r-- | intern/cycles/kernel/kernel_types.h | 1 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm.h | 13 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm_ramp.h | 74 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm_types.h | 4 | ||||
-rw-r--r-- | intern/cycles/render/nodes.cpp | 55 | ||||
-rw-r--r-- | intern/cycles/render/nodes.h | 12 | ||||
-rw-r--r-- | intern/cycles/render/svm.cpp | 6 | ||||
-rw-r--r-- | intern/cycles/render/svm.h | 1 |
11 files changed, 189 insertions, 3 deletions
diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp index 5c26a4d2163..bea434bb7b6 100644 --- a/intern/cycles/blender/blender_shader.cpp +++ b/intern/cycles/blender/blender_shader.cpp @@ -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"); diff --git a/intern/cycles/blender/blender_util.h b/intern/cycles/blender/blender_util.h index ff6d55c6f3e..67f3a3ab7d9 100644 --- a/intern/cycles/blender/blender_util.h +++ b/intern/cycles/blender/blender_util.h @@ -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); diff --git a/intern/cycles/kernel/CMakeLists.txt b/intern/cycles/kernel/CMakeLists.txt index bef5c7af789..e9820010b63 100644 --- a/intern/cycles/kernel/CMakeLists.txt +++ b/intern/cycles/kernel/CMakeLists.txt @@ -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 diff --git a/intern/cycles/kernel/kernel_types.h b/intern/cycles/kernel/kernel_types.h index e5e03cedc18..c198ee1d24e 100644 --- a/intern/cycles/kernel/kernel_types.h +++ b/intern/cycles/kernel/kernel_types.h @@ -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__ diff --git a/intern/cycles/kernel/svm/svm.h b/intern/cycles/kernel/svm/svm.h index f1bae9c8d9f..50181c0cf2c 100644 --- a/intern/cycles/kernel/svm/svm.h +++ b/intern/cycles/kernel/svm/svm.h @@ -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 index 00000000000..0654240c21b --- /dev/null +++ b/intern/cycles/kernel/svm/svm_ramp.h @@ -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__ */ + diff --git a/intern/cycles/kernel/svm/svm_types.h b/intern/cycles/kernel/svm/svm_types.h index 533a2944557..13d72307765 100644 --- a/intern/cycles/kernel/svm/svm_types.h +++ b/intern/cycles/kernel/svm/svm_types.h @@ -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 { diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp index a98352aa85a..45ba18469d6 100644 --- a/intern/cycles/render/nodes.cpp +++ b/intern/cycles/render/nodes.cpp @@ -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 diff --git a/intern/cycles/render/nodes.h b/intern/cycles/render/nodes.h index e0329cb9b1d..364209f8c5e 100644 --- a/intern/cycles/render/nodes.h +++ b/intern/cycles/render/nodes.h @@ -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__ */ diff --git a/intern/cycles/render/svm.cpp b/intern/cycles/render/svm.cpp index 46c6149ab32..a52e30c6030 100644 --- a/intern/cycles/render/svm.cpp +++ b/intern/cycles/render/svm.cpp @@ -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); diff --git a/intern/cycles/render/svm.h b/intern/cycles/render/svm.h index d8a1b14d637..56c930f6217 100644 --- a/intern/cycles/render/svm.h +++ b/intern/cycles/render/svm.h @@ -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); |