Cycles: Shader Graph Optimization for Mix RGB nodes.

Basically the same as AC2c58e96685e8, but for Mix RGB Shaders, in case we use the Mix type. This way the node can be used as texture switch for example, setting the Factor to 0.0 or 1.0, without wasting extra memory / render time.
This commit is contained in:
Thomas Dinges 2014-09-24 12:52:19 +02:00
parent 6dae643450
commit cbffc7499e
3 changed files with 51 additions and 4 deletions

@ -229,6 +229,10 @@ static ShaderNode *add_node(Scene *scene, BL::BlendData b_data, BL::Scene b_scen
BL::ShaderNodeMixRGB b_mix_node(b_node);
MixNode *mix = new MixNode();
mix->type = MixNode::type_enum[b_mix_node.blend_type()];
/* Tag if it's Mix */
if(b_mix_node.blend_type() == 0)
mix->special_type = SHADER_SPECIAL_TYPE_MIX_RGB;
mix->use_clamp = b_mix_node.use_clamp();
node = mix;
}

@ -420,6 +420,48 @@ void ShaderGraph::remove_unneeded_nodes()
ShaderOutput *output = mix->inputs[2]->link;
vector<ShaderInput*> inputs = mix->outputs[0]->links;
foreach(ShaderInput *sock, mix->inputs)
if(sock->link)
disconnect(sock);
foreach(ShaderInput *input, inputs) {
disconnect(input);
if(output)
connect(output, input);
}
removed[mix->id] = true;
any_node_removed = true;
}
}
}
else if(node->special_type == SHADER_SPECIAL_TYPE_MIX_RGB) {
MixNode *mix = static_cast<MixNode*>(node);
/* remove unused Mix RGB inputs when factor is 0.0 or 1.0 */
/* check for color links and make sure factor link is disconnected */
if(mix->outputs[0]->links.size() && mix->inputs[1]->link && mix->inputs[2]->link && !mix->inputs[0]->link) {
/* factor 0.0 */
if(mix->inputs[0]->value.x == 0.0f) {
ShaderOutput *output = mix->inputs[1]->link;
vector<ShaderInput*> inputs = mix->outputs[0]->links;
foreach(ShaderInput *sock, mix->inputs)
if(sock->link)
disconnect(sock);
foreach(ShaderInput *input, inputs) {
disconnect(input);
if(output)
connect(output, input);
}
removed[mix->id] = true;
any_node_removed = true;
}
/* factor 1.0 */
else if(mix->inputs[0]->value.x == 1.0f) {
ShaderOutput *output = mix->inputs[2]->link;
vector<ShaderInput*> inputs = mix->outputs[0]->links;
foreach(ShaderInput *sock, mix->inputs)
if(sock->link)
disconnect(sock);

@ -76,6 +76,7 @@ enum ShaderNodeSpecialType {
SHADER_SPECIAL_TYPE_NONE,
SHADER_SPECIAL_TYPE_PROXY,
SHADER_SPECIAL_TYPE_MIX_CLOSURE,
SHADER_SPECIAL_TYPE_MIX_RGB, /* Only Mix subtype */
SHADER_SPECIAL_TYPE_AUTOCONVERT,
SHADER_SPECIAL_TYPE_GEOMETRY,
SHADER_SPECIAL_TYPE_SCRIPT