Cycles: add folding for redundant A to B to A conversions.
authorAlexander Gavrilov <angavrilov@gmail.com>
Tue, 2 Aug 2016 09:22:43 +0000 (12:22 +0300)
committerAlexander Gavrilov <angavrilov@gmail.com>
Tue, 2 Aug 2016 15:41:15 +0000 (18:41 +0300)
As a result of other folding simplifications it may happen that
two type conversion nodes end up directly connected. In some
cases it may be possible to then remove both. A realistic case
might be an optimized out Mix RGB node used to blend vectors.

It seems it's safe to optimize when B is a float3 type
(color, vector), and A is float3 or float.

Reviewers: #cycles, sergey

Reviewed By: #cycles, sergey

Subscribers: sergey

Differential Revision: https://developer.blender.org/D2134

intern/cycles/render/nodes.cpp
intern/cycles/test/render_graph_finalize_test.cpp

index 9009d71da2fc95289d9c049fad784d76f7c4e3fc..4f54b86fe4a1268ba2daddd2d3fabdf7557e971e 100644 (file)
@@ -1689,6 +1689,19 @@ void ConvertNode::constant_fold(const ConstantFolder& folder)
                        }
                }
        }
+       else {
+               ShaderInput *in = inputs[0];
+               ShaderNode *prev = in->link->parent;
+
+               /* no-op conversion of A to B to A */
+               if(prev->type == node_types[to][from]) {
+                       ShaderInput *prev_in = prev->inputs[0];
+
+                       if(SocketType::is_float3(from) && (to == SocketType::FLOAT || SocketType::is_float3(to)) && prev_in->link) {
+                               folder.bypass(prev_in->link);
+                       }
+               }
+       }
 }
 
 void ConvertNode::compile(SVMCompiler& compiler)
index cf4083c23dbf9534a96a3065301a396cf4833a6b..f84ddc3ff894b6cf8f59c6ce7b7703c3666f3c37 100644 (file)
@@ -1460,4 +1460,72 @@ TEST(render_graph, constant_fold_rgb_ramp_flat)
        graph.finalize(&scene);
 }
 
+/*
+ * Tests:
+ *  - Folding of redundant conversion of float to color to float.
+ */
+TEST(render_graph, constant_fold_convert_float_color_float)
+{
+       DEFINE_COMMON_VARIABLES(builder, log);
+
+       EXPECT_ANY_MESSAGE(log);
+       CORRECT_INFO_MESSAGE(log, "Folding Invert::Color to socket convert_float_to_color::value_color.");
+       CORRECT_INFO_MESSAGE(log, "Folding convert_color_to_float::value_float to socket Attribute::Fac.");
+
+       builder
+               .add_attribute("Attribute")
+               .add_node(ShaderNodeBuilder<InvertNode>("Invert")
+                         .set("Fac", 0.0f))
+               .add_connection("Attribute::Fac", "Invert::Color")
+               .output_value("Invert::Color");
+
+       graph.finalize(&scene);
+}
+
+/*
+ * Tests:
+ *  - Folding of redundant conversion of color to vector to color.
+ */
+TEST(render_graph, constant_fold_convert_color_vector_color)
+{
+       DEFINE_COMMON_VARIABLES(builder, log);
+
+       EXPECT_ANY_MESSAGE(log);
+       CORRECT_INFO_MESSAGE(log, "Folding VecAdd::Vector to socket convert_color_to_vector::value_vector.");
+       CORRECT_INFO_MESSAGE(log, "Folding convert_vector_to_color::value_color to socket Attribute::Color.");
+
+       builder
+               .add_attribute("Attribute")
+               .add_node(ShaderNodeBuilder<VectorMathNode>("VecAdd")
+                         .set(&VectorMathNode::type, NODE_VECTOR_MATH_ADD)
+                         .set("Vector2", make_float3(0,0,0)))
+               .add_connection("Attribute::Color", "VecAdd::Vector1")
+               .output_color("VecAdd::Vector");
+
+       graph.finalize(&scene);
+}
+
+/*
+ * Tests:
+ *  - NOT folding conversion of color to float to color.
+ */
+TEST(render_graph, constant_fold_convert_color_float_color)
+{
+       DEFINE_COMMON_VARIABLES(builder, log);
+
+       EXPECT_ANY_MESSAGE(log);
+       CORRECT_INFO_MESSAGE(log, "Folding MathAdd::Value to socket convert_color_to_float::value_float.");
+       INVALID_INFO_MESSAGE(log, "Folding convert_float_to_color::");
+
+       builder
+               .add_attribute("Attribute")
+               .add_node(ShaderNodeBuilder<MathNode>("MathAdd")
+                         .set(&MathNode::type, NODE_MATH_ADD)
+                         .set("Value2", 0.0f))
+               .add_connection("Attribute::Color", "MathAdd::Value1")
+               .output_color("MathAdd::Value");
+
+       graph.finalize(&scene);
+}
+
 CCL_NAMESPACE_END