Cycles: add ColorRamp node.

This commit is contained in:
Brecht Van Lommel
2012-03-26 12:45:14 +00:00
parent 56baed13da
commit bbc3d820f4
13 changed files with 191 additions and 5 deletions

View File

@@ -105,7 +105,6 @@ static ShaderNode *add_node(BL::BlendData b_data, ShaderGraph *graph, BL::Shader
switch(b_node.type()) { switch(b_node.type()) {
/* not supported */ /* not supported */
case BL::ShaderNode::type_CURVE_RGB: break;
case BL::ShaderNode::type_CURVE_VEC: break; case BL::ShaderNode::type_CURVE_VEC: break;
case BL::ShaderNode::type_GEOMETRY: break; case BL::ShaderNode::type_GEOMETRY: break;
case BL::ShaderNode::type_MATERIAL: 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_SCRIPT: break;
case BL::ShaderNode::type_SQUEEZE: break; case BL::ShaderNode::type_SQUEEZE: break;
case BL::ShaderNode::type_TEXTURE: break; case BL::ShaderNode::type_TEXTURE: break;
case BL::ShaderNode::type_VALTORGB: break;
/* handled outside this function */ /* handled outside this function */
case BL::ShaderNode::type_GROUP: break; case BL::ShaderNode::type_GROUP: break;
/* existing blender nodes */ /* 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: { case BL::ShaderNode::type_RGB: {
ColorNode *color = new ColorNode(); ColorNode *color = new ColorNode();
color->value = get_node_output_rgba(b_node, "Color"); color->value = get_node_output_rgba(b_node, "Color");

View File

@@ -50,6 +50,7 @@ void engine_tag_redraw(void *engine);
void engine_tag_update(void *engine); void engine_tag_update(void *engine);
int rna_Object_is_modified(void *ob, void *scene, int settings); int rna_Object_is_modified(void *ob, void *scene, int settings);
void BLI_timestr(double _time, char *str); 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); 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) static inline void object_remove_mesh(BL::BlendData data, BL::Mesh mesh)
{ {
rna_Main_meshes_remove(data.ptr.data, NULL, mesh.ptr.data); rna_Main_meshes_remove(data.ptr.data, NULL, mesh.ptr.data);

View File

@@ -78,6 +78,7 @@ set(SRC_SVM_HEADERS
svm/svm_noise.h svm/svm_noise.h
svm/svm_noisetex.h svm/svm_noisetex.h
svm/svm_normal.h svm/svm_normal.h
svm/svm_ramp.h
svm/svm_sepcomb_rgb.h svm/svm_sepcomb_rgb.h
svm/svm_sky.h svm/svm_sky.h
svm/svm_tex_coord.h svm/svm_tex_coord.h

View File

@@ -29,6 +29,7 @@ CCL_NAMESPACE_BEGIN
#define OBJECT_SIZE 16 #define OBJECT_SIZE 16
#define LIGHT_SIZE 4 #define LIGHT_SIZE 4
#define FILTER_TABLE_SIZE 256 #define FILTER_TABLE_SIZE 256
#define RAMP_TABLE_SIZE 256
/* device capabilities */ /* device capabilities */
#ifdef __KERNEL_CPU__ #ifdef __KERNEL_CPU__

View File

@@ -104,6 +104,12 @@ __device_inline float4 read_node_float(KernelGlobals *kg, int *offset)
return f; 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) __device_inline void decode_node_uchar4(uint i, uint *x, uint *y, uint *z, uint *w)
{ {
if(x) *x = (i & 0xFF); if(x) *x = (i & 0xFF);
@@ -140,6 +146,7 @@ CCL_NAMESPACE_END
#include "svm_wave.h" #include "svm_wave.h"
#include "svm_math.h" #include "svm_math.h"
#include "svm_mix.h" #include "svm_mix.h"
#include "svm_ramp.h"
#include "svm_sepcomb_rgb.h" #include "svm_sepcomb_rgb.h"
#include "svm_musgrave.h" #include "svm_musgrave.h"
#include "svm_sky.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: case NODE_EMISSION_SET_WEIGHT_TOTAL:
svm_node_emission_set_weight_total(kg, sd, node.y, node.z, node.w); svm_node_emission_set_weight_total(kg, sd, node.y, node.z, node.w);
break; 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: case NODE_END:
default: default:
#ifndef __MULTI_CLOSURE__ #ifndef __MULTI_CLOSURE__

View File

@@ -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__ */

View File

@@ -88,7 +88,9 @@ typedef enum NodeType {
NODE_NORMAL = 5500, NODE_NORMAL = 5500,
NODE_GAMMA = 5600, NODE_GAMMA = 5600,
NODE_TEX_CHECKER = 5700, NODE_TEX_CHECKER = 5700,
NODE_BRIGHTCONTRAST = 5800 NODE_BRIGHTCONTRAST = 5800,
NODE_RGB_RAMP = 5900,
NODE_RGB_CURVES = 6000
} NodeType; } NodeType;
typedef enum NodeAttributeType { typedef enum NodeAttributeType {

View File

@@ -2352,5 +2352,60 @@ void BumpNode::compile(OSLCompiler& compiler)
compiler.add(this, "node_bump"); 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 CCL_NAMESPACE_END

View File

@@ -390,6 +390,18 @@ public:
SHADER_NODE_CLASS(BumpNode) 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 CCL_NAMESPACE_END
#endif /* __NODES_H__ */ #endif /* __NODES_H__ */

View File

@@ -326,6 +326,12 @@ void SVMCompiler::add_node(const float4& f)
__float_as_int(f.w))); __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) uint SVMCompiler::attribute(ustring name)
{ {
return shader_manager->get_attribute_id(name); return shader_manager->get_attribute_id(name);

View File

@@ -67,6 +67,7 @@ public:
void add_node(int a = 0, int b = 0, int c = 0, int d = 0); 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(NodeType type, const float3& f);
void add_node(const float4& f); void add_node(const float4& f);
void add_array(float4 *f, int num);
uint attribute(ustring name); uint attribute(ustring name);
uint attribute(Attribute::Standard std); uint attribute(Attribute::Standard std);
uint encode_uchar4(uint x, uint y = 0, uint z = 0, uint w = 0); uint encode_uchar4(uint x, uint y = 0, uint z = 0, uint w = 0);

View 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); do_colorband(coba, position, color);
} }

View File

@@ -76,7 +76,7 @@ void register_node_type_sh_valtorgb(bNodeTreeType *ttype)
static bNodeType ntype; static bNodeType ntype;
node_type_base(ttype, &ntype, SH_NODE_VALTORGB, "ColorRamp", NODE_CLASS_CONVERTOR, NODE_OPTIONS); 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_socket_templates(&ntype, sh_node_valtorgb_in, sh_node_valtorgb_out);
node_type_size(&ntype, 240, 200, 300); node_type_size(&ntype, 240, 200, 300);
node_type_init(&ntype, node_shader_init_valtorgb); node_type_init(&ntype, node_shader_init_valtorgb);