Cycles: add constant folding for more color operation nodes.
authorAlexander Gavrilov <angavrilov@gmail.com>
Sun, 19 Jun 2016 10:28:50 +0000 (13:28 +0300)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Sun, 19 Jun 2016 18:17:28 +0000 (20:17 +0200)
Invert, brightness & constrast, separate/combine and Mix RGB blend modes
and clamping.

intern/cycles/kernel/svm/svm.h
intern/cycles/kernel/svm/svm_brightness.h
intern/cycles/kernel/svm/svm_color_util.h [new file with mode: 0644]
intern/cycles/kernel/svm/svm_mix.h
intern/cycles/render/nodes.cpp
intern/cycles/render/nodes.h

index 543e31bcb3574f4082caaecff0cf1c88f51dfe53..de7e03e5a198366a1dc71eec87187f90302e7d17 100644 (file)
@@ -142,6 +142,7 @@ CCL_NAMESPACE_END
 #include "svm_noise.h"
 #include "svm_texture.h"
 
+#include "svm_color_util.h"
 #include "svm_math_util.h"
 
 #include "svm_attribute.h"
index e4d545a00ae43244cd1081c126a59cd5a6de0c86..d71b0ee0b617dad160f8cc45356f6802905e606b 100644 (file)
@@ -25,12 +25,7 @@ ccl_device void svm_node_brightness(ShaderData *sd, float *stack, uint in_color,
        float brightness = stack_load_float(stack, bright_offset);
        float contrast  = stack_load_float(stack, contrast_offset);
 
-       float a = 1.0f + contrast;
-       float b = brightness - contrast*0.5f;
-
-       color.x = max(a*color.x + b, 0.0f);
-       color.y = max(a*color.y + b, 0.0f);
-       color.z = max(a*color.z + b, 0.0f);
+       color = svm_brightness_contrast(color, brightness, contrast);
 
        if(stack_valid(out_color))
                stack_store_float3(stack, out_color, color);
diff --git a/intern/cycles/kernel/svm/svm_color_util.h b/intern/cycles/kernel/svm/svm_color_util.h
new file mode 100644 (file)
index 0000000..258cdeb
--- /dev/null
@@ -0,0 +1,306 @@
+/*
+ * Copyright 2011-2013 Blender Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+CCL_NAMESPACE_BEGIN
+
+ccl_device float3 svm_mix_blend(float t, float3 col1, float3 col2)
+{
+       return interp(col1, col2, t);
+}
+
+ccl_device float3 svm_mix_add(float t, float3 col1, float3 col2)
+{
+       return interp(col1, col1 + col2, t);
+}
+
+ccl_device float3 svm_mix_mul(float t, float3 col1, float3 col2)
+{
+       return interp(col1, col1 * col2, t);
+}
+
+ccl_device float3 svm_mix_screen(float t, float3 col1, float3 col2)
+{
+       float tm = 1.0f - t;
+       float3 one = make_float3(1.0f, 1.0f, 1.0f);
+       float3 tm3 = make_float3(tm, tm, tm);
+
+       return one - (tm3 + t*(one - col2))*(one - col1);
+}
+
+ccl_device float3 svm_mix_overlay(float t, float3 col1, float3 col2)
+{
+       float tm = 1.0f - t;
+
+       float3 outcol = col1;
+
+       if(outcol.x < 0.5f)
+               outcol.x *= tm + 2.0f*t*col2.x;
+       else
+               outcol.x = 1.0f - (tm + 2.0f*t*(1.0f - col2.x))*(1.0f - outcol.x);
+
+       if(outcol.y < 0.5f)
+               outcol.y *= tm + 2.0f*t*col2.y;
+       else
+               outcol.y = 1.0f - (tm + 2.0f*t*(1.0f - col2.y))*(1.0f - outcol.y);
+
+       if(outcol.z < 0.5f)
+               outcol.z *= tm + 2.0f*t*col2.z;
+       else
+               outcol.z = 1.0f - (tm + 2.0f*t*(1.0f - col2.z))*(1.0f - outcol.z);
+       
+       return outcol;
+}
+
+ccl_device float3 svm_mix_sub(float t, float3 col1, float3 col2)
+{
+       return interp(col1, col1 - col2, t);
+}
+
+ccl_device float3 svm_mix_div(float t, float3 col1, float3 col2)
+{
+       float tm = 1.0f - t;
+
+       float3 outcol = col1;
+
+       if(col2.x != 0.0f) outcol.x = tm*outcol.x + t*outcol.x/col2.x;
+       if(col2.y != 0.0f) outcol.y = tm*outcol.y + t*outcol.y/col2.y;
+       if(col2.z != 0.0f) outcol.z = tm*outcol.z + t*outcol.z/col2.z;
+
+       return outcol;
+}
+
+ccl_device float3 svm_mix_diff(float t, float3 col1, float3 col2)
+{
+       return interp(col1, fabs(col1 - col2), t);
+}
+
+ccl_device float3 svm_mix_dark(float t, float3 col1, float3 col2)
+{
+       return min(col1, col2)*t + col1*(1.0f - t);
+}
+
+ccl_device float3 svm_mix_light(float t, float3 col1, float3 col2)
+{
+       return max(col1, col2*t);
+}
+
+ccl_device float3 svm_mix_dodge(float t, float3 col1, float3 col2)
+{
+       float3 outcol = col1;
+
+       if(outcol.x != 0.0f) {
+               float tmp = 1.0f - t*col2.x;
+               if(tmp <= 0.0f)
+                       outcol.x = 1.0f;
+               else if((tmp = outcol.x/tmp) > 1.0f)
+                       outcol.x = 1.0f;
+               else
+                       outcol.x = tmp;
+       }
+       if(outcol.y != 0.0f) {
+               float tmp = 1.0f - t*col2.y;
+               if(tmp <= 0.0f)
+                       outcol.y = 1.0f;
+               else if((tmp = outcol.y/tmp) > 1.0f)
+                       outcol.y = 1.0f;
+               else
+                       outcol.y = tmp;
+       }
+       if(outcol.z != 0.0f) {
+               float tmp = 1.0f - t*col2.z;
+               if(tmp <= 0.0f)
+                       outcol.z = 1.0f;
+               else if((tmp = outcol.z/tmp) > 1.0f)
+                       outcol.z = 1.0f;
+               else
+                       outcol.z = tmp;
+       }
+
+       return outcol;
+}
+
+ccl_device float3 svm_mix_burn(float t, float3 col1, float3 col2)
+{
+       float tmp, tm = 1.0f - t;
+
+       float3 outcol = col1;
+
+       tmp = tm + t*col2.x;
+       if(tmp <= 0.0f)
+               outcol.x = 0.0f;
+       else if((tmp = (1.0f - (1.0f - outcol.x)/tmp)) < 0.0f)
+               outcol.x = 0.0f;
+       else if(tmp > 1.0f)
+               outcol.x = 1.0f;
+       else
+               outcol.x = tmp;
+
+       tmp = tm + t*col2.y;
+       if(tmp <= 0.0f)
+               outcol.y = 0.0f;
+       else if((tmp = (1.0f - (1.0f - outcol.y)/tmp)) < 0.0f)
+               outcol.y = 0.0f;
+       else if(tmp > 1.0f)
+               outcol.y = 1.0f;
+       else
+               outcol.y = tmp;
+
+       tmp = tm + t*col2.z;
+       if(tmp <= 0.0f)
+               outcol.z = 0.0f;
+       else if((tmp = (1.0f - (1.0f - outcol.z)/tmp)) < 0.0f)
+               outcol.z = 0.0f;
+       else if(tmp > 1.0f)
+               outcol.z = 1.0f;
+       else
+               outcol.z = tmp;
+       
+       return outcol;
+}
+
+ccl_device float3 svm_mix_hue(float t, float3 col1, float3 col2)
+{
+       float3 outcol = col1;
+
+       float3 hsv2 = rgb_to_hsv(col2);
+
+       if(hsv2.y != 0.0f) {
+               float3 hsv = rgb_to_hsv(outcol);
+               hsv.x = hsv2.x;
+               float3 tmp = hsv_to_rgb(hsv); 
+
+               outcol = interp(outcol, tmp, t);
+       }
+
+       return outcol;
+}
+
+ccl_device float3 svm_mix_sat(float t, float3 col1, float3 col2)
+{
+       float tm = 1.0f - t;
+
+       float3 outcol = col1;
+
+       float3 hsv = rgb_to_hsv(outcol);
+
+       if(hsv.y != 0.0f) {
+               float3 hsv2 = rgb_to_hsv(col2);
+
+               hsv.y = tm*hsv.y + t*hsv2.y;
+               outcol = hsv_to_rgb(hsv);
+       }
+
+       return outcol;
+}
+
+ccl_device float3 svm_mix_val(float t, float3 col1, float3 col2)
+{
+       float tm = 1.0f - t;
+
+       float3 hsv = rgb_to_hsv(col1);
+       float3 hsv2 = rgb_to_hsv(col2);
+
+       hsv.z = tm*hsv.z + t*hsv2.z;
+
+       return hsv_to_rgb(hsv);
+}
+
+ccl_device float3 svm_mix_color(float t, float3 col1, float3 col2)
+{
+       float3 outcol = col1;
+       float3 hsv2 = rgb_to_hsv(col2);
+
+       if(hsv2.y != 0.0f) {
+               float3 hsv = rgb_to_hsv(outcol);
+               hsv.x = hsv2.x;
+               hsv.y = hsv2.y;
+               float3 tmp = hsv_to_rgb(hsv); 
+
+               outcol = interp(outcol, tmp, t);
+       }
+
+       return outcol;
+}
+
+ccl_device float3 svm_mix_soft(float t, float3 col1, float3 col2)
+{
+       float tm = 1.0f - t;
+
+       float3 one = make_float3(1.0f, 1.0f, 1.0f);
+       float3 scr = one - (one - col2)*(one - col1);
+
+       return tm*col1 + t*((one - col1)*col2*col1 + col1*scr);
+}
+
+ccl_device float3 svm_mix_linear(float t, float3 col1, float3 col2)
+{
+       return col1 + t*(2.0f*col2 + make_float3(-1.0f, -1.0f, -1.0f));
+}
+
+ccl_device float3 svm_mix_clamp(float3 col)
+{
+       float3 outcol = col;
+
+       outcol.x = saturate(col.x);
+       outcol.y = saturate(col.y);
+       outcol.z = saturate(col.z);
+
+       return outcol;
+}
+
+ccl_device_noinline float3 svm_mix(NodeMix type, float fac, float3 c1, float3 c2)
+{
+       float t = saturate(fac);
+
+       switch(type) {
+               case NODE_MIX_BLEND: return svm_mix_blend(t, c1, c2);
+               case NODE_MIX_ADD: return svm_mix_add(t, c1, c2);
+               case NODE_MIX_MUL: return svm_mix_mul(t, c1, c2);
+               case NODE_MIX_SCREEN: return svm_mix_screen(t, c1, c2);
+               case NODE_MIX_OVERLAY: return svm_mix_overlay(t, c1, c2);
+               case NODE_MIX_SUB: return svm_mix_sub(t, c1, c2);
+               case NODE_MIX_DIV: return svm_mix_div(t, c1, c2);
+               case NODE_MIX_DIFF: return svm_mix_diff(t, c1, c2);
+               case NODE_MIX_DARK: return svm_mix_dark(t, c1, c2);
+               case NODE_MIX_LIGHT: return svm_mix_light(t, c1, c2);
+               case NODE_MIX_DODGE: return svm_mix_dodge(t, c1, c2);
+               case NODE_MIX_BURN: return svm_mix_burn(t, c1, c2);
+               case NODE_MIX_HUE: return svm_mix_hue(t, c1, c2);
+               case NODE_MIX_SAT: return svm_mix_sat(t, c1, c2);
+               case NODE_MIX_VAL: return svm_mix_val (t, c1, c2);
+               case NODE_MIX_COLOR: return svm_mix_color(t, c1, c2);
+               case NODE_MIX_SOFT: return svm_mix_soft(t, c1, c2);
+               case NODE_MIX_LINEAR: return svm_mix_linear(t, c1, c2);
+               case NODE_MIX_CLAMP: return svm_mix_clamp(c1);
+       }
+
+       return make_float3(0.0f, 0.0f, 0.0f);
+}
+
+ccl_device_inline float3 svm_brightness_contrast(float3 color, float brightness, float contrast)
+{
+       float a = 1.0f + contrast;
+       float b = brightness - contrast*0.5f;
+
+       color.x = max(a*color.x + b, 0.0f);
+       color.y = max(a*color.y + b, 0.0f);
+       color.z = max(a*color.z + b, 0.0f);
+
+       return color;
+}
+
+CCL_NAMESPACE_END
+
index 7cbda111d81f82b52541cd64da267ceda71225e4..022a68d19287e0a471bf246170aac861d953af3f 100644 (file)
 
 CCL_NAMESPACE_BEGIN
 
-ccl_device float3 svm_mix_blend(float t, float3 col1, float3 col2)
-{
-       return interp(col1, col2, t);
-}
-
-ccl_device float3 svm_mix_add(float t, float3 col1, float3 col2)
-{
-       return interp(col1, col1 + col2, t);
-}
-
-ccl_device float3 svm_mix_mul(float t, float3 col1, float3 col2)
-{
-       return interp(col1, col1 * col2, t);
-}
-
-ccl_device float3 svm_mix_screen(float t, float3 col1, float3 col2)
-{
-       float tm = 1.0f - t;
-       float3 one = make_float3(1.0f, 1.0f, 1.0f);
-       float3 tm3 = make_float3(tm, tm, tm);
-
-       return one - (tm3 + t*(one - col2))*(one - col1);
-}
-
-ccl_device float3 svm_mix_overlay(float t, float3 col1, float3 col2)
-{
-       float tm = 1.0f - t;
-
-       float3 outcol = col1;
-
-       if(outcol.x < 0.5f)
-               outcol.x *= tm + 2.0f*t*col2.x;
-       else
-               outcol.x = 1.0f - (tm + 2.0f*t*(1.0f - col2.x))*(1.0f - outcol.x);
-
-       if(outcol.y < 0.5f)
-               outcol.y *= tm + 2.0f*t*col2.y;
-       else
-               outcol.y = 1.0f - (tm + 2.0f*t*(1.0f - col2.y))*(1.0f - outcol.y);
-
-       if(outcol.z < 0.5f)
-               outcol.z *= tm + 2.0f*t*col2.z;
-       else
-               outcol.z = 1.0f - (tm + 2.0f*t*(1.0f - col2.z))*(1.0f - outcol.z);
-       
-       return outcol;
-}
-
-ccl_device float3 svm_mix_sub(float t, float3 col1, float3 col2)
-{
-       return interp(col1, col1 - col2, t);
-}
-
-ccl_device float3 svm_mix_div(float t, float3 col1, float3 col2)
-{
-       float tm = 1.0f - t;
-
-       float3 outcol = col1;
-
-       if(col2.x != 0.0f) outcol.x = tm*outcol.x + t*outcol.x/col2.x;
-       if(col2.y != 0.0f) outcol.y = tm*outcol.y + t*outcol.y/col2.y;
-       if(col2.z != 0.0f) outcol.z = tm*outcol.z + t*outcol.z/col2.z;
-
-       return outcol;
-}
-
-ccl_device float3 svm_mix_diff(float t, float3 col1, float3 col2)
-{
-       return interp(col1, fabs(col1 - col2), t);
-}
-
-ccl_device float3 svm_mix_dark(float t, float3 col1, float3 col2)
-{
-       return min(col1, col2)*t + col1*(1.0f - t);
-}
-
-ccl_device float3 svm_mix_light(float t, float3 col1, float3 col2)
-{
-       return max(col1, col2*t);
-}
-
-ccl_device float3 svm_mix_dodge(float t, float3 col1, float3 col2)
-{
-       float3 outcol = col1;
-
-       if(outcol.x != 0.0f) {
-               float tmp = 1.0f - t*col2.x;
-               if(tmp <= 0.0f)
-                       outcol.x = 1.0f;
-               else if((tmp = outcol.x/tmp) > 1.0f)
-                       outcol.x = 1.0f;
-               else
-                       outcol.x = tmp;
-       }
-       if(outcol.y != 0.0f) {
-               float tmp = 1.0f - t*col2.y;
-               if(tmp <= 0.0f)
-                       outcol.y = 1.0f;
-               else if((tmp = outcol.y/tmp) > 1.0f)
-                       outcol.y = 1.0f;
-               else
-                       outcol.y = tmp;
-       }
-       if(outcol.z != 0.0f) {
-               float tmp = 1.0f - t*col2.z;
-               if(tmp <= 0.0f)
-                       outcol.z = 1.0f;
-               else if((tmp = outcol.z/tmp) > 1.0f)
-                       outcol.z = 1.0f;
-               else
-                       outcol.z = tmp;
-       }
-
-       return outcol;
-}
-
-ccl_device float3 svm_mix_burn(float t, float3 col1, float3 col2)
-{
-       float tmp, tm = 1.0f - t;
-
-       float3 outcol = col1;
-
-       tmp = tm + t*col2.x;
-       if(tmp <= 0.0f)
-               outcol.x = 0.0f;
-       else if((tmp = (1.0f - (1.0f - outcol.x)/tmp)) < 0.0f)
-               outcol.x = 0.0f;
-       else if(tmp > 1.0f)
-               outcol.x = 1.0f;
-       else
-               outcol.x = tmp;
-
-       tmp = tm + t*col2.y;
-       if(tmp <= 0.0f)
-               outcol.y = 0.0f;
-       else if((tmp = (1.0f - (1.0f - outcol.y)/tmp)) < 0.0f)
-               outcol.y = 0.0f;
-       else if(tmp > 1.0f)
-               outcol.y = 1.0f;
-       else
-               outcol.y = tmp;
-
-       tmp = tm + t*col2.z;
-       if(tmp <= 0.0f)
-               outcol.z = 0.0f;
-       else if((tmp = (1.0f - (1.0f - outcol.z)/tmp)) < 0.0f)
-               outcol.z = 0.0f;
-       else if(tmp > 1.0f)
-               outcol.z = 1.0f;
-       else
-               outcol.z = tmp;
-       
-       return outcol;
-}
-
-ccl_device float3 svm_mix_hue(float t, float3 col1, float3 col2)
-{
-       float3 outcol = col1;
-
-       float3 hsv2 = rgb_to_hsv(col2);
-
-       if(hsv2.y != 0.0f) {
-               float3 hsv = rgb_to_hsv(outcol);
-               hsv.x = hsv2.x;
-               float3 tmp = hsv_to_rgb(hsv); 
-
-               outcol = interp(outcol, tmp, t);
-       }
-
-       return outcol;
-}
-
-ccl_device float3 svm_mix_sat(float t, float3 col1, float3 col2)
-{
-       float tm = 1.0f - t;
-
-       float3 outcol = col1;
-
-       float3 hsv = rgb_to_hsv(outcol);
-
-       if(hsv.y != 0.0f) {
-               float3 hsv2 = rgb_to_hsv(col2);
-
-               hsv.y = tm*hsv.y + t*hsv2.y;
-               outcol = hsv_to_rgb(hsv);
-       }
-
-       return outcol;
-}
-
-ccl_device float3 svm_mix_val(float t, float3 col1, float3 col2)
-{
-       float tm = 1.0f - t;
-
-       float3 hsv = rgb_to_hsv(col1);
-       float3 hsv2 = rgb_to_hsv(col2);
-
-       hsv.z = tm*hsv.z + t*hsv2.z;
-
-       return hsv_to_rgb(hsv);
-}
-
-ccl_device float3 svm_mix_color(float t, float3 col1, float3 col2)
-{
-       float3 outcol = col1;
-       float3 hsv2 = rgb_to_hsv(col2);
-
-       if(hsv2.y != 0.0f) {
-               float3 hsv = rgb_to_hsv(outcol);
-               hsv.x = hsv2.x;
-               hsv.y = hsv2.y;
-               float3 tmp = hsv_to_rgb(hsv); 
-
-               outcol = interp(outcol, tmp, t);
-       }
-
-       return outcol;
-}
-
-ccl_device float3 svm_mix_soft(float t, float3 col1, float3 col2)
-{
-       float tm = 1.0f - t;
-
-       float3 one = make_float3(1.0f, 1.0f, 1.0f);
-       float3 scr = one - (one - col2)*(one - col1);
-
-       return tm*col1 + t*((one - col1)*col2*col1 + col1*scr);
-}
-
-ccl_device float3 svm_mix_linear(float t, float3 col1, float3 col2)
-{
-       return col1 + t*(2.0f*col2 + make_float3(-1.0f, -1.0f, -1.0f));
-}
-
-ccl_device float3 svm_mix_clamp(float3 col)
-{
-       float3 outcol = col;
-
-       outcol.x = saturate(col.x);
-       outcol.y = saturate(col.y);
-       outcol.z = saturate(col.z);
-
-       return outcol;
-}
-
-ccl_device_noinline float3 svm_mix(NodeMix type, float fac, float3 c1, float3 c2)
-{
-       float t = saturate(fac);
-
-       switch(type) {
-               case NODE_MIX_BLEND: return svm_mix_blend(t, c1, c2);
-               case NODE_MIX_ADD: return svm_mix_add(t, c1, c2);
-               case NODE_MIX_MUL: return svm_mix_mul(t, c1, c2);
-               case NODE_MIX_SCREEN: return svm_mix_screen(t, c1, c2);
-               case NODE_MIX_OVERLAY: return svm_mix_overlay(t, c1, c2);
-               case NODE_MIX_SUB: return svm_mix_sub(t, c1, c2);
-               case NODE_MIX_DIV: return svm_mix_div(t, c1, c2);
-               case NODE_MIX_DIFF: return svm_mix_diff(t, c1, c2);
-               case NODE_MIX_DARK: return svm_mix_dark(t, c1, c2);
-               case NODE_MIX_LIGHT: return svm_mix_light(t, c1, c2);
-               case NODE_MIX_DODGE: return svm_mix_dodge(t, c1, c2);
-               case NODE_MIX_BURN: return svm_mix_burn(t, c1, c2);
-               case NODE_MIX_HUE: return svm_mix_hue(t, c1, c2);
-               case NODE_MIX_SAT: return svm_mix_sat(t, c1, c2);
-               case NODE_MIX_VAL: return svm_mix_val (t, c1, c2);
-               case NODE_MIX_COLOR: return svm_mix_color(t, c1, c2);
-               case NODE_MIX_SOFT: return svm_mix_soft(t, c1, c2);
-               case NODE_MIX_LINEAR: return svm_mix_linear(t, c1, c2);
-               case NODE_MIX_CLAMP: return svm_mix_clamp(c1);
-       }
-
-       return make_float3(0.0f, 0.0f, 0.0f);
-}
-
 /* Node */
 
 ccl_device void svm_node_mix(KernelGlobals *kg, ShaderData *sd, float *stack, uint fac_offset, uint c1_offset, uint c2_offset, int *offset)
index afdec23f3bef2fc82f711726058b865eac74ab0a..5efbf65a198e61c2c88eb237aba6f757a6eb1584 100644 (file)
@@ -19,6 +19,7 @@
 #include "nodes.h"
 #include "scene.h"
 #include "svm.h"
+#include "svm_color_util.h"
 #include "svm_math_util.h"
 #include "osl.h"
 
@@ -3578,6 +3579,28 @@ InvertNode::InvertNode()
 {
 }
 
+bool InvertNode::constant_fold(ShaderGraph *graph, ShaderOutput *, ShaderInput *optimized)
+{
+       ShaderInput *fac_in = input("Fac");
+       ShaderInput *color_in = input("Color");
+       ShaderOutput *color_out = output("Color");
+
+       if(!fac_in->link) {
+               /* evaluate fully constant node */
+               if(!color_in->link) {
+                       optimized->set(interp(color, make_float3(1.0f, 1.0f, 1.0f) - color, fac));
+                       return true;
+               }
+               /* remove no-op node */
+               else if(fac == 0.0f) {
+                       graph->relink(this, color_out, color_in->link);
+                       return true;
+               }
+       }
+
+       return false;
+}
+
 void InvertNode::compile(SVMCompiler& compiler)
 {
        ShaderInput *fac_in = input("Fac");
@@ -3666,37 +3689,54 @@ void MixNode::compile(OSLCompiler& compiler)
 
 bool MixNode::constant_fold(ShaderGraph *graph, ShaderOutput *, ShaderInput *optimized)
 {
-       if(type != NODE_MIX_BLEND) {
-               return false;
-       }
-
        ShaderInput *fac_in = input("Fac");
        ShaderInput *color1_in = input("Color1");
        ShaderInput *color2_in = input("Color2");
        ShaderOutput *color_out = output("Color");
 
+       /* evaluate fully constant node */
+       if(all_inputs_constant()) {
+               float3 result = svm_mix(type, fac, color1, color2);
+               optimized->set(use_clamp ? svm_mix_clamp(result) : result);
+               return true;
+       }
+
+       /* remove no-op node when factor is 0.0 */
+       if(!fac_in->link && fac <= 0.0f) {
+               /* note that some of the modes will clamp out of bounds values even without use_clamp */
+               if(!color1_in->link) {
+                       float3 result = svm_mix(type, 0.0f, color1, color1);
+                       optimized->set(use_clamp ? svm_mix_clamp(result) : result);
+                       return true;
+               }
+               else if(!use_clamp && type != NODE_MIX_LIGHT && type != NODE_MIX_DODGE && type != NODE_MIX_BURN) {
+                       graph->relink(this, color_out, color1_in->link);
+                       return true;
+               }
+       }
+
+       if(type != NODE_MIX_BLEND) {
+               return false;
+       }
+
        /* remove useless mix colors nodes */
-       if(color1_in->link && color1_in->link == color2_in->link) {
+       if(color1_in->link && color1_in->link == color2_in->link && !use_clamp) {
                graph->relink(this, color_out, color1_in->link);
                return true;
        }
+       if(!color1_in->link && !color2_in->link && color1 == color2) {
+               optimized->set(use_clamp ? svm_mix_clamp(color1) : color1);
+               return true;
+       }
 
-       /* remove unused mix color input when factor is 0.0 or 1.0 */
-       if(!fac_in->link) {
-               /* factor 0.0 */
-               if(fac == 0.0f) {
-                       if(color1_in->link)
-                               graph->relink(this, color_out, color1_in->link);
-                       else
-                               optimized->set(color1);
+       /* remove no-op mix color node when factor is 1.0 */
+       if(!fac_in->link && fac >= 1.0f) {
+               if(!color2_in->link) {
+                       optimized->set(use_clamp ? svm_mix_clamp(color2) : color2);
                        return true;
                }
-               /* factor 1.0 */
-               else if(fac == 1.0f) {
-                       if(color2_in->link)
-                               graph->relink(this, color_out, color2_in->link);
-                       else
-                               optimized->set(color2);
+               else if(!use_clamp) {
+                       graph->relink(this, color_out, color2_in->link);
                        return true;
                }
        }
@@ -3724,6 +3764,16 @@ CombineRGBNode::CombineRGBNode()
 {
 }
 
+bool CombineRGBNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
+{
+       if(all_inputs_constant()) {
+               optimized->set(make_float3(r, g, b));
+               return true;
+       }
+
+       return false;
+}
+
 void CombineRGBNode::compile(SVMCompiler& compiler)
 {
        ShaderInput *red_in = input("R");
@@ -3769,6 +3819,16 @@ CombineXYZNode::CombineXYZNode()
 {
 }
 
+bool CombineXYZNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
+{
+       if(all_inputs_constant()) {
+               optimized->set(make_float3(x, y, z));
+               return true;
+       }
+
+       return false;
+}
+
 void CombineXYZNode::compile(SVMCompiler& compiler)
 {
        ShaderInput *x_in = input("X");
@@ -3814,6 +3874,16 @@ CombineHSVNode::CombineHSVNode()
 {
 }
 
+bool CombineHSVNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
+{
+       if(all_inputs_constant()) {
+               optimized->set(hsv_to_rgb(make_float3(h, s, v)));
+               return true;
+       }
+
+       return false;
+}
+
 void CombineHSVNode::compile(SVMCompiler& compiler)
 {
        ShaderInput *hue_in = input("H");
@@ -3899,6 +3969,16 @@ BrightContrastNode::BrightContrastNode()
 {
 }
 
+bool BrightContrastNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimized)
+{
+       if(all_inputs_constant()) {
+               optimized->set(svm_brightness_contrast(color, bright, contrast));
+               return true;
+       }
+
+       return false;
+}
+
 void BrightContrastNode::compile(SVMCompiler& compiler)
 {
        ShaderInput *color_in = input("Color");
@@ -3939,6 +4019,20 @@ SeparateRGBNode::SeparateRGBNode()
 {
 }
 
+bool SeparateRGBNode::constant_fold(ShaderGraph *, ShaderOutput *socket, ShaderInput *optimized)
+{
+       if(all_inputs_constant()) {
+               for(int channel = 0; channel < 3; channel++) {
+                       if(outputs[channel] == socket) {
+                               optimized->set(color[channel]);
+                               return true;
+                       }
+               }
+       }
+
+       return false;
+}
+
 void SeparateRGBNode::compile(SVMCompiler& compiler)
 {
        ShaderInput *color_in = input("Image");
@@ -3984,6 +4078,20 @@ SeparateXYZNode::SeparateXYZNode()
 {
 }
 
+bool SeparateXYZNode::constant_fold(ShaderGraph *, ShaderOutput *socket, ShaderInput *optimized)
+{
+       if(all_inputs_constant()) {
+               for(int channel = 0; channel < 3; channel++) {
+                       if(outputs[channel] == socket) {
+                               optimized->set(vector[channel]);
+                               return true;
+                       }
+               }
+       }
+
+       return false;
+}
+
 void SeparateXYZNode::compile(SVMCompiler& compiler)
 {
        ShaderInput *vector_in = input("Vector");
@@ -4029,6 +4137,22 @@ SeparateHSVNode::SeparateHSVNode()
 {
 }
 
+bool SeparateHSVNode::constant_fold(ShaderGraph *, ShaderOutput *socket, ShaderInput *optimized)
+{
+       if(all_inputs_constant()) {
+               float3 hsv = rgb_to_hsv(color);
+
+               for(int channel = 0; channel < 3; channel++) {
+                       if(outputs[channel] == socket) {
+                               optimized->set(hsv[channel]);
+                               return true;
+                       }
+               }
+       }
+
+       return false;
+}
+
 void SeparateHSVNode::compile(SVMCompiler& compiler)
 {
        ShaderInput *color_in = input("Color");
@@ -4525,13 +4649,7 @@ bool MathNode::constant_fold(ShaderGraph *, ShaderOutput *, ShaderInput *optimiz
 {
        if(all_inputs_constant()) {
                float value = svm_math(type, value1, value2);
-
-               if(use_clamp) {
-                       value = saturate(value);
-               }
-
-               optimized->set(value);
-
+               optimized->set(use_clamp ? saturate(value) : value);
                return true;
        }
 
index 8d73a94abb5500520793c7b840bf15051fc36795..978908f4c78cd7a6cdcee823775673a51c44c026 100644 (file)
@@ -643,7 +643,7 @@ public:
 class InvertNode : public ShaderNode {
 public:
        SHADER_NODE_CLASS(InvertNode)
-
+       bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized);
        virtual int get_group() { return NODE_GROUP_LEVEL_3; }
 
        float fac;
@@ -667,6 +667,7 @@ public:
 class CombineRGBNode : public ShaderNode {
 public:
        SHADER_NODE_CLASS(CombineRGBNode)
+       bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized);
        virtual int get_group() { return NODE_GROUP_LEVEL_3; }
 
        float r, g, b;
@@ -675,6 +676,7 @@ public:
 class CombineHSVNode : public ShaderNode {
 public:
        SHADER_NODE_CLASS(CombineHSVNode)
+       bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized);
        virtual int get_group() { return NODE_GROUP_LEVEL_3; }
 
        float h, s, v;
@@ -683,6 +685,7 @@ public:
 class CombineXYZNode : public ShaderNode {
 public:
        SHADER_NODE_CLASS(CombineXYZNode)
+       bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized);
        virtual int get_group() { return NODE_GROUP_LEVEL_3; }
 
        float x, y, z;
@@ -701,6 +704,7 @@ public:
 class BrightContrastNode : public ShaderNode {
 public:
        SHADER_NODE_CLASS(BrightContrastNode)
+       bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized);
        virtual int get_group() { return NODE_GROUP_LEVEL_1; }
 
        float3 color;
@@ -711,6 +715,7 @@ public:
 class SeparateRGBNode : public ShaderNode {
 public:
        SHADER_NODE_CLASS(SeparateRGBNode)
+       bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized);
        virtual int get_group() { return NODE_GROUP_LEVEL_3; }
 
        float3 color;
@@ -719,6 +724,7 @@ public:
 class SeparateHSVNode : public ShaderNode {
 public:
        SHADER_NODE_CLASS(SeparateHSVNode)
+       bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized);
        virtual int get_group() { return NODE_GROUP_LEVEL_3; }
 
        float3 color;
@@ -727,6 +733,7 @@ public:
 class SeparateXYZNode : public ShaderNode {
 public:
        SHADER_NODE_CLASS(SeparateXYZNode)
+       bool constant_fold(ShaderGraph *graph, ShaderOutput *socket, ShaderInput *optimized);
        virtual int get_group() { return NODE_GROUP_LEVEL_3; }
 
        float3 vector;