Code refactor: small code simplification for Cycles constant folding.
authorBrecht Van Lommel <brechtvanlommel@gmail.com>
Sun, 19 Jun 2016 14:50:25 +0000 (16:50 +0200)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Sun, 19 Jun 2016 18:17:27 +0000 (20:17 +0200)
intern/cycles/render/graph.cpp
intern/cycles/render/graph.h
intern/cycles/render/nodes.cpp

index 9210221a26120ba71dd17877c5f8f66a00539bd7..a1b992a2c317e49ff772893bc3d71134204f6cac 100644 (file)
@@ -126,6 +126,17 @@ ShaderOutput *ShaderNode::output(ustring name)
        return NULL;
 }
 
+bool ShaderNode::all_inputs_constant() const
+{
+       foreach(ShaderInput *input, inputs) {
+               if(input->link) {
+                       return false;
+               }
+       }
+
+       return true;
+}
+
 void ShaderNode::attributes(Shader *shader, AttributeRequestSet *attributes)
 {
        foreach(ShaderInput *input, inputs) {
index dccd8c27b2f4ee9c2c6bfa339ed0c5968e91edde..61100cda60b507f2a6679bc890ec6879e6d3c23a 100644 (file)
@@ -142,6 +142,8 @@ public:
        /* Check whether the node can be replaced with single constant. */
        virtual bool constant_fold(ShaderGraph * /*graph*/, ShaderOutput * /*socket*/, ShaderInput * /*optimized*/) { return false; }
 
+       bool all_inputs_constant() const;
+
        /* Simplify settings used by artists to the ones which are simpler to
         * evaluate in the kernel but keep the final result unchanged.
         */
index 478443f205cade8bbe42084ece43b898a7accb88..afdec23f3bef2fc82f711726058b865eac74ab0a 100644 (file)
@@ -1577,7 +1577,7 @@ RGBToBWNode::RGBToBWNode()
 
 bool RGBToBWNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
 {
-       if(inputs[0]->link == NULL) {
+       if(all_inputs_constant()) {
                optimized->set(linear_rgb_to_gray(color));
                return true;
        }
@@ -1665,11 +1665,9 @@ bool ConvertNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *opti
        /* proxy nodes should have been removed at this point */
        assert(special_type != SHADER_SPECIAL_TYPE_PROXY);
 
-       ShaderInput *in = inputs[0];
-
        /* TODO(DingTo): conversion from/to int is not supported yet, don't fold in that case */
 
-       if(in->link == NULL) {
+       if(all_inputs_constant()) {
                if(from == SocketType::FLOAT) {
                        if(SocketType::is_float3(to)) {
                                optimized->set(make_float3(value_float, value_float, value_float));
@@ -3856,10 +3854,7 @@ GammaNode::GammaNode()
 
 bool GammaNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
 {
-       ShaderInput *color_in = input("Color");
-       ShaderInput *gamma_in = input("Gamma");
-
-       if(color_in->link == NULL && gamma_in->link == NULL) {
+       if(all_inputs_constant()) {
                optimized->set(svm_math_gamma_color(color, gamma));
                return true;
        }
@@ -4419,9 +4414,7 @@ BlackbodyNode::BlackbodyNode()
 
 bool BlackbodyNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
 {
-       ShaderInput *temperature_in = input("Temperature");
-
-       if(temperature_in->link == NULL) {
+       if(all_inputs_constant()) {
                optimized->set(svm_math_blackbody_color(temperature));
                return true;
        }
@@ -4530,10 +4523,7 @@ MathNode::MathNode()
 
 bool MathNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
 {
-       ShaderInput *value1_in = input("Value1");
-       ShaderInput *value2_in = input("Value2");
-
-       if(value1_in->link == NULL && value2_in->link == NULL) {
+       if(all_inputs_constant()) {
                float value = svm_math(type, value1, value2);
 
                if(use_clamp) {
@@ -4601,13 +4591,10 @@ VectorMathNode::VectorMathNode()
 
 bool VectorMathNode::constant_fold(ShaderGraph *, ShaderOutput *socket, ShaderInput *optimized)
 {
-       ShaderInput *vector1_in = input("Vector1");
-       ShaderInput *vector2_in = input("Vector2");
-
        float value;
        float3 vector;
 
-       if(vector1_in->link == NULL && vector2_in->link == NULL) {
+       if(all_inputs_constant()) {
                svm_vector_math(&value,
                                &vector,
                                type,