diff options
author | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2016-05-29 14:26:41 +0300 |
---|---|---|
committer | Jeroen Bakker <j.bakker@atmind.nl> | 2016-06-08 22:45:40 +0300 |
commit | 10d3acacf6bc630141796137beb506cd2478eccd (patch) | |
tree | cafc37d8d58ee35a1948531c7aa42bb4cf11d354 | |
parent | 693b15f553799ce8744f148639eca954b48a9600 (diff) |
Code refactor: pass ShaderInput to constant_fold, so it supports arbitrary types.
-rw-r--r-- | intern/cycles/blender/blender_shader.cpp | 18 | ||||
-rw-r--r-- | intern/cycles/render/graph.cpp | 25 | ||||
-rw-r--r-- | intern/cycles/render/graph.h | 7 | ||||
-rw-r--r-- | intern/cycles/render/nodes.cpp | 114 | ||||
-rw-r--r-- | intern/cycles/render/nodes.h | 26 | ||||
-rw-r--r-- | intern/cycles/render/shader.cpp | 6 |
6 files changed, 88 insertions, 108 deletions
diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp index 7aa099e9e2b..5f16c1b6738 100644 --- a/intern/cycles/blender/blender_shader.cpp +++ b/intern/cycles/blender/blender_shader.cpp @@ -179,25 +179,25 @@ static void set_default_value(ShaderInput *input, /* copy values for non linked inputs */ switch(input->type()) { case SocketType::FLOAT: { - input->value_float() = get_float(b_sock.ptr, "default_value"); + input->set(get_float(b_sock.ptr, "default_value")); break; } case SocketType::INT: { - input->value_float() = (float)get_int(b_sock.ptr, "default_value"); + input->set(get_int(b_sock.ptr, "default_value")); break; } case SocketType::COLOR: { - input->value() = float4_to_float3(get_float4(b_sock.ptr, "default_value")); + input->set(float4_to_float3(get_float4(b_sock.ptr, "default_value"))); break; } case SocketType::NORMAL: case SocketType::POINT: case SocketType::VECTOR: { - input->value() = get_float3(b_sock.ptr, "default_value"); + input->set(get_float3(b_sock.ptr, "default_value")); break; } case SocketType::STRING: { - input->value_string() = (ustring)blender_absolute_path(b_data, b_id, get_string(b_sock.ptr, "default_value")); + input->set((ustring)blender_absolute_path(b_data, b_id, get_string(b_sock.ptr, "default_value"))); break; } default: @@ -1225,7 +1225,7 @@ void BlenderSync::sync_materials(bool update_all) ShaderNode *closure, *out; closure = graph->add(new DiffuseBsdfNode()); - closure->input("Color")->value() = get_float3(b_mat->diffuse_color()); + closure->input("Color")->set(get_float3(b_mat->diffuse_color())); out = graph->output(); graph->connect(closure->output("BSDF"), out->input("Surface")); @@ -1274,7 +1274,7 @@ void BlenderSync::sync_world(bool update_all) ShaderNode *closure, *out; closure = graph->add(new BackgroundNode()); - closure->input("Color")->value() = get_float3(b_world.horizon_color()); + closure->input("Color")->set(get_float3(b_world.horizon_color())); out = graph->output(); graph->connect(closure->output("Background"), out->input("Surface")); @@ -1367,8 +1367,8 @@ void BlenderSync::sync_lamps(bool update_all) } closure = graph->add(new EmissionNode()); - closure->input("Color")->value() = get_float3(b_lamp->color()); - closure->input("Strength")->value_float() = strength; + closure->input("Color")->set(get_float3(b_lamp->color())); + closure->input("Strength")->set(strength); out = graph->output(); graph->connect(closure->output("Emission"), out->input("Surface")); diff --git a/intern/cycles/render/graph.cpp b/intern/cycles/render/graph.cpp index 62998b0166f..29c0eec9b97 100644 --- a/intern/cycles/render/graph.cpp +++ b/intern/cycles/render/graph.cpp @@ -470,8 +470,8 @@ void ShaderGraph::remove_proxy_nodes() disconnect(to); /* transfer the default input value to the target socket */ - to->value() = input->value(); - to->value_string() = input->value_string(); + to->set(input->value()); + to->set(input->value_string()); } } @@ -537,14 +537,13 @@ void ShaderGraph::constant_fold() } } /* Optimize current node. */ - float3 optimized_value = make_float3(0.0f, 0.0f, 0.0f); - if(node->constant_fold(this, output, &optimized_value)) { - /* Apply optimized value to connected sockets. */ + if(node->constant_fold(this, output, output->links[0])) { + /* Apply optimized value to other connected sockets and disconnect. */ vector<ShaderInput*> links(output->links); - foreach(ShaderInput *input, links) { - /* Assign value and disconnect the optimizedinput. */ - input->value() = optimized_value; - disconnect(input); + for(size_t i = 0; i < links.size(); i++) { + if(i > 0) + links[i]->set(links[0]->value()); + disconnect(links[i]); } } } @@ -935,7 +934,7 @@ void ShaderGraph::transform_multi_closure(ShaderNode *node, ShaderOutput *weight if(fin->link) connect(fin->link, fac_in); else - fac_in->value_float() = fin->value_float(); + fac_in->set(fin->value_float()); if(weight_out) connect(weight_out, weight_in); @@ -970,12 +969,12 @@ void ShaderGraph::transform_multi_closure(ShaderNode *node, ShaderOutput *weight if(weight_in->link) connect(weight_in->link, value1_in); else - value1_in->value() = weight_in->value(); + value1_in->set(weight_in->value_float()); if(weight_out) connect(weight_out, value2_in); else - value2_in->value_float() = 1.0f; + value2_in->set(1.0f); weight_out = math_node->output("Value"); if(weight_in->link) @@ -986,7 +985,7 @@ void ShaderGraph::transform_multi_closure(ShaderNode *node, ShaderOutput *weight if(weight_out) connect(weight_out, weight_in); else - weight_in->value_float() += 1.0f; + weight_in->set(weight_in->value_float() + 1.0f); } } diff --git a/intern/cycles/render/graph.h b/intern/cycles/render/graph.h index 760b19f447a..882e495df20 100644 --- a/intern/cycles/render/graph.h +++ b/intern/cycles/render/graph.h @@ -85,6 +85,11 @@ public: int flags() { return flags_; } SocketType::Type type() { return type_; } + void set(float f) { value_.x = f; } + void set(float3 f) { value_ = f; } + void set(int i) { value_.x = (float)i; } + void set(ustring s) { value_string_ = s; } + float3& value() { return value_; } float& value_float() { return value_.x; } ustring& value_string() { return value_string_; } @@ -148,7 +153,7 @@ public: /* ** Node optimization ** */ /* Check whether the node can be replaced with single constant. */ - virtual bool constant_fold(ShaderGraph * /*graph*/, ShaderOutput * /*socket*/, float3 * /*optimized_value*/) { return false; } + virtual bool constant_fold(ShaderGraph * /*graph*/, ShaderOutput * /*socket*/, ShaderInput * /*optimized*/) { return false; } /* Simplify settings used by artists to the ones which are simpler to * evaluate in the kernel but keep the final result unchanged. diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp index 359225cf56a..ea68a671029 100644 --- a/intern/cycles/render/nodes.cpp +++ b/intern/cycles/render/nodes.cpp @@ -1545,12 +1545,10 @@ RGBToBWNode::RGBToBWNode() add_output("Val", SocketType::FLOAT); } -bool RGBToBWNode::constant_fold(ShaderGraph * /*graph*/, - ShaderOutput * /*socket*/, - float3 *optimized_value) +bool RGBToBWNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized) { if(inputs[0]->link == NULL) { - optimized_value->x = linear_rgb_to_gray(inputs[0]->value()); + optimized->set(linear_rgb_to_gray(inputs[0]->value())); return true; } @@ -1624,9 +1622,7 @@ ConvertNode::ConvertNode(SocketType::Type from_, SocketType::Type to_, bool auto assert(0); } -bool ConvertNode::constant_fold(ShaderGraph * /*graph*/, - ShaderOutput * /*socket*/, - float3 *optimized_value) +bool ConvertNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized) { ShaderInput *in = inputs[0]; float3 value = in->value(); @@ -1635,42 +1631,24 @@ bool ConvertNode::constant_fold(ShaderGraph * /*graph*/, if(in->link == NULL) { if(from == SocketType::FLOAT) { - if(to == SocketType::INT) - /* float to int */ - return false; - else - /* float to float3 */ - *optimized_value = make_float3(value.x, value.x, value.x); - } - else if(from == SocketType::INT) { - if(to == SocketType::FLOAT) - /* int to float */ - return false; - else - /* int to vector/point/normal */ - return false; - } - else if(to == SocketType::FLOAT) { - if(from == SocketType::COLOR) - /* color to float */ - optimized_value->x = linear_rgb_to_gray(value); - else - /* vector/point/normal to float */ - optimized_value->x = average(value); - } - else if(to == SocketType::INT) { - if(from == SocketType::COLOR) - /* color to int */ - return false; - else - /* vector/point/normal to int */ - return false; + if(SocketType::is_float3(to)) { + optimized->set(make_float3(value.x, value.x, value.x)); + return true; + } } - else { - *optimized_value = value; + else if(SocketType::is_float3(from)) { + if(to == SocketType::FLOAT) { + if(from == SocketType::COLOR) + optimized->set(linear_rgb_to_gray(value)); + else + optimized->set(average(value)); + return true; + } + else if(SocketType::is_float3(to)) { + optimized->set(value); + return true; + } } - - return true; } return false; @@ -2254,7 +2232,7 @@ void EmissionNode::compile(OSLCompiler& compiler) compiler.add(this, "node_emission"); } -bool EmissionNode::constant_fold(ShaderGraph * /*graph*/, ShaderOutput * /*socket*/, float3 * /*optimized_value*/) +bool EmissionNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *) { ShaderInput *color_in = input("Color"); ShaderInput *strength_in = input("Strength"); @@ -2296,7 +2274,7 @@ void BackgroundNode::compile(OSLCompiler& compiler) compiler.add(this, "node_background"); } -bool BackgroundNode::constant_fold(ShaderGraph * /*graph*/, ShaderOutput * /*socket*/, float3 * /*optimized_value*/) +bool BackgroundNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *) { ShaderInput *color_in = input("Color"); ShaderInput *strength_in = input("Strength"); @@ -3147,10 +3125,9 @@ ValueNode::ValueNode() add_output("Value", SocketType::FLOAT); } -bool ValueNode::constant_fold(ShaderGraph * /*graph*/, ShaderOutput * /*socket*/, - float3 *optimized_value) +bool ValueNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized) { - *optimized_value = make_float3(value, value, value); + optimized->set(value); return true; } @@ -3177,10 +3154,9 @@ ColorNode::ColorNode() add_output("Color", SocketType::COLOR); } -bool ColorNode::constant_fold(ShaderGraph * /*graph*/, ShaderOutput * /*socket*/, - float3 *optimized_value) +bool ColorNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized) { - *optimized_value = value; + optimized->set(value); return true; } @@ -3246,7 +3222,7 @@ void MixClosureNode::compile(OSLCompiler& compiler) compiler.add(this, "node_mix_closure"); } -bool MixClosureNode::constant_fold(ShaderGraph *graph, ShaderOutput * /*socket*/, float3 * /*optimized_value*/) +bool MixClosureNode::constant_fold(ShaderGraph *graph, ShaderOutput *, ShaderInput *) { ShaderInput *fac_in = input("Fac"); ShaderInput *closure1_in = input("Closure1"); @@ -3404,7 +3380,7 @@ void MixNode::compile(OSLCompiler& compiler) compiler.add(this, "node_mix"); } -bool MixNode::constant_fold(ShaderGraph *graph, ShaderOutput * /*socket*/, float3 * optimized_value) +bool MixNode::constant_fold(ShaderGraph *graph, ShaderOutput *, ShaderInput *optimized) { if(type != ustring("Mix")) { return false; @@ -3428,7 +3404,7 @@ bool MixNode::constant_fold(ShaderGraph *graph, ShaderOutput * /*socket*/, float if(color1_in->link) graph->relink(this, color_out, color1_in->link); else - *optimized_value = color1_in->value(); + optimized->set(color1_in->value()); return true; } /* factor 1.0 */ @@ -3436,7 +3412,7 @@ bool MixNode::constant_fold(ShaderGraph *graph, ShaderOutput * /*socket*/, float if(color2_in->link) graph->relink(this, color_out, color2_in->link); else - *optimized_value = color2_in->value(); + optimized->set(color2_in->value()); return true; } } @@ -3553,15 +3529,15 @@ GammaNode::GammaNode() add_output("Color", SocketType::COLOR); } -bool GammaNode::constant_fold(ShaderGraph * /*graph*/, ShaderOutput *socket, float3 *optimized_value) +bool GammaNode::constant_fold(ShaderGraph *, ShaderOutput *socket, ShaderInput *optimized) { ShaderInput *color_in = input("Color"); ShaderInput *gamma_in = input("Gamma"); if(socket == output("Color")) { if(color_in->link == NULL && gamma_in->link == NULL) { - *optimized_value = svm_math_gamma_color(color_in->value(), - gamma_in->value_float()); + optimized->set(svm_math_gamma_color(color_in->value(), + gamma_in->value_float())); return true; } } @@ -4013,13 +3989,13 @@ BlackbodyNode::BlackbodyNode() add_output("Color", SocketType::COLOR); } -bool BlackbodyNode::constant_fold(ShaderGraph * /*graph*/, ShaderOutput *socket, float3 *optimized_value) +bool BlackbodyNode::constant_fold(ShaderGraph *, ShaderOutput *socket, ShaderInput *optimized) { ShaderInput *temperature_in = input("Temperature"); if(socket == output("Color")) { if(temperature_in->link == NULL) { - *optimized_value = svm_math_blackbody_color(temperature_in->value_float()); + optimized->set(svm_math_blackbody_color(temperature_in->value_float())); return true; } } @@ -4119,21 +4095,23 @@ static NodeEnum math_type_init() NodeEnum MathNode::type_enum = math_type_init(); -bool MathNode::constant_fold(ShaderGraph * /*graph*/, ShaderOutput *socket, float3 *optimized_value) +bool MathNode::constant_fold(ShaderGraph *, ShaderOutput *socket, ShaderInput *optimized) { ShaderInput *value1_in = input("Value1"); ShaderInput *value2_in = input("Value2"); if(socket == output("Value")) { if(value1_in->link == NULL && value2_in->link == NULL) { - optimized_value->x = svm_math((NodeMath)type_enum[type], - value1_in->value_float(), - value2_in->value_float()); + float value = svm_math((NodeMath)type_enum[type], + value1_in->value_float(), + value2_in->value_float()); if(use_clamp) { - optimized_value->x = saturate(optimized_value->x); + value = saturate(value); } + optimized->set(value); + return true; } } @@ -4195,7 +4173,7 @@ static NodeEnum vector_math_type_init() NodeEnum VectorMathNode::type_enum = vector_math_type_init(); -bool VectorMathNode::constant_fold(ShaderGraph * /*graph*/, ShaderOutput *socket, float3 *optimized_value) +bool VectorMathNode::constant_fold(ShaderGraph *, ShaderOutput *socket, ShaderInput *optimized) { ShaderInput *vector1_in = input("Vector1"); ShaderInput *vector2_in = input("Vector2"); @@ -4211,11 +4189,11 @@ bool VectorMathNode::constant_fold(ShaderGraph * /*graph*/, ShaderOutput *socket vector2_in->value()); if(socket == output("Value")) { - optimized_value->x = value; + optimized->set(value); return true; } else if(socket == output("Vector")) { - *optimized_value = vector; + optimized->set(vector); return true; } } @@ -4357,9 +4335,7 @@ void BumpNode::compile(OSLCompiler& compiler) compiler.add(this, "node_bump"); } -bool BumpNode::constant_fold(ShaderGraph *graph, - ShaderOutput * /*socket*/, - float3 * /*optimized_value*/) +bool BumpNode::constant_fold(ShaderGraph *graph, ShaderOutput *, ShaderInput *) { ShaderInput *height_in = input("Height"); ShaderInput *normal_in = input("Normal"); diff --git a/intern/cycles/render/nodes.h b/intern/cycles/render/nodes.h index bb77cc452dc..43dc1dd26fe 100644 --- a/intern/cycles/render/nodes.h +++ b/intern/cycles/render/nodes.h @@ -366,7 +366,7 @@ class RGBToBWNode : public ShaderNode { public: SHADER_NODE_CLASS(RGBToBWNode) - bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, float3 *optimized_value); + bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized); }; class ConvertNode : public ShaderNode { @@ -374,7 +374,7 @@ public: ConvertNode(SocketType::Type from, SocketType::Type to, bool autoconvert = false); SHADER_NODE_BASE_CLASS(ConvertNode) - bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, float3 *optimized_value); + bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized); SocketType::Type from, to; @@ -491,7 +491,7 @@ public: class EmissionNode : public ShaderNode { public: SHADER_NODE_CLASS(EmissionNode) - bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, float3 *optimized_value); + bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized); virtual ClosureType get_closure_type() { return CLOSURE_EMISSION_ID; } bool has_surface_emission() { return true; } @@ -500,7 +500,7 @@ public: class BackgroundNode : public ShaderNode { public: SHADER_NODE_CLASS(BackgroundNode) - bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, float3 *optimized_value); + bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized); virtual ClosureType get_closure_type() { return CLOSURE_BACKGROUND_ID; } }; @@ -646,7 +646,7 @@ class ValueNode : public ShaderNode { public: SHADER_NODE_CLASS(ValueNode) - bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, float3 *optimized_value); + bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized); float value; @@ -661,7 +661,7 @@ class ColorNode : public ShaderNode { public: SHADER_NODE_CLASS(ColorNode) - bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, float3 *optimized_value); + bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized); float3 value; @@ -680,7 +680,7 @@ public: class MixClosureNode : public ShaderNode { public: SHADER_NODE_CLASS(MixClosureNode) - bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, float3 *optimized_value); + bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized); }; class MixClosureWeightNode : public ShaderNode { @@ -698,7 +698,7 @@ public: class MixNode : public ShaderNode { public: SHADER_NODE_CLASS(MixNode) - bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, float3 *optimized_value); + bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized); virtual int get_group() { return NODE_GROUP_LEVEL_3; } @@ -741,7 +741,7 @@ class GammaNode : public ShaderNode { public: SHADER_NODE_CLASS(GammaNode) - bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, float3 *optimized_value); + bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized); virtual int get_group() { return NODE_GROUP_LEVEL_1; } }; @@ -832,7 +832,7 @@ public: class BlackbodyNode : public ShaderNode { public: SHADER_NODE_CLASS(BlackbodyNode) - bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, float3 *optimized_value); + bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized); virtual int get_group() { return NODE_GROUP_LEVEL_3; } }; @@ -841,7 +841,7 @@ class MathNode : public ShaderNode { public: SHADER_NODE_CLASS(MathNode) virtual int get_group() { return NODE_GROUP_LEVEL_1; } - bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, float3 *optimized_value); + bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized); bool use_clamp; @@ -876,7 +876,7 @@ class VectorMathNode : public ShaderNode { public: SHADER_NODE_CLASS(VectorMathNode) virtual int get_group() { return NODE_GROUP_LEVEL_1; } - bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, float3 *optimized_value); + bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized); ustring type; static NodeEnum type_enum; @@ -914,7 +914,7 @@ public: class BumpNode : public ShaderNode { public: SHADER_NODE_CLASS(BumpNode) - bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, float3 *optimized_value); + bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized); bool has_spatial_varying() { return true; } virtual int get_feature() { return NODE_FEATURE_BUMP; diff --git a/intern/cycles/render/shader.cpp b/intern/cycles/render/shader.cpp index f03e8e54bcc..708eeef3b50 100644 --- a/intern/cycles/render/shader.cpp +++ b/intern/cycles/render/shader.cpp @@ -456,7 +456,7 @@ void ShaderManager::add_default(Scene *scene) ShaderGraph *graph = new ShaderGraph(); closure = graph->add(new DiffuseBsdfNode()); - closure->input("Color")->value() = make_float3(0.8f, 0.8f, 0.8f); + closure->input("Color")->set(make_float3(0.8f, 0.8f, 0.8f)); out = graph->output(); graph->connect(closure->output("BSDF"), out->input("Surface")); @@ -473,8 +473,8 @@ void ShaderManager::add_default(Scene *scene) ShaderGraph *graph = new ShaderGraph(); closure = graph->add(new EmissionNode()); - closure->input("Color")->value() = make_float3(0.8f, 0.8f, 0.8f); - closure->input("Strength")->value_float() = 0.0f; + closure->input("Color")->set(make_float3(0.8f, 0.8f, 0.8f)); + closure->input("Strength")->set(0.0f); out = graph->output(); graph->connect(closure->output("Emission"), out->input("Surface")); |