Lazy and right-to-left evaluation in the node tree

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












As someone with a programming background, here's a fundamental thing about the Cycles node tree I've been thinking about. Let's say you have a node setup like this:



enter image description here



Here we have two computationally intensive shaders, called "Gigantic Shader A" and "Gigantic Shader B", and a Mix Shader is used to switch between them using some black-and-white mask.



In programming terms, this setup can be thought of as an if-else construct, with shader A and B being the two execution branches of the if-else construct.



Now, in basically every sane programming language, the two if-else branches are evaluated "lazily", meaning that either only the first branch or only the second branch is evaluated, depending on whether the if condition evaluates to true or false.



So here are my questions:



(1) Does Cycles have this lazy evaluation optimization as well? More specifically, is Cycles smart enough to figure out that if Fac=0 or Fac=1 in the above Mix Shader, it can completely ignore either shader B or shader A?



(2) If Cycles does have this optimization, then is it also true that the node tree is actually evaluated from right to left, starting at the Material Output node, and not from left to right, as one would naively assume? Because I can't think of any other way to implement lazy evaluation than going from right to left.










share|improve this question



























    up vote
    1
    down vote

    favorite












    As someone with a programming background, here's a fundamental thing about the Cycles node tree I've been thinking about. Let's say you have a node setup like this:



    enter image description here



    Here we have two computationally intensive shaders, called "Gigantic Shader A" and "Gigantic Shader B", and a Mix Shader is used to switch between them using some black-and-white mask.



    In programming terms, this setup can be thought of as an if-else construct, with shader A and B being the two execution branches of the if-else construct.



    Now, in basically every sane programming language, the two if-else branches are evaluated "lazily", meaning that either only the first branch or only the second branch is evaluated, depending on whether the if condition evaluates to true or false.



    So here are my questions:



    (1) Does Cycles have this lazy evaluation optimization as well? More specifically, is Cycles smart enough to figure out that if Fac=0 or Fac=1 in the above Mix Shader, it can completely ignore either shader B or shader A?



    (2) If Cycles does have this optimization, then is it also true that the node tree is actually evaluated from right to left, starting at the Material Output node, and not from left to right, as one would naively assume? Because I can't think of any other way to implement lazy evaluation than going from right to left.










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      As someone with a programming background, here's a fundamental thing about the Cycles node tree I've been thinking about. Let's say you have a node setup like this:



      enter image description here



      Here we have two computationally intensive shaders, called "Gigantic Shader A" and "Gigantic Shader B", and a Mix Shader is used to switch between them using some black-and-white mask.



      In programming terms, this setup can be thought of as an if-else construct, with shader A and B being the two execution branches of the if-else construct.



      Now, in basically every sane programming language, the two if-else branches are evaluated "lazily", meaning that either only the first branch or only the second branch is evaluated, depending on whether the if condition evaluates to true or false.



      So here are my questions:



      (1) Does Cycles have this lazy evaluation optimization as well? More specifically, is Cycles smart enough to figure out that if Fac=0 or Fac=1 in the above Mix Shader, it can completely ignore either shader B or shader A?



      (2) If Cycles does have this optimization, then is it also true that the node tree is actually evaluated from right to left, starting at the Material Output node, and not from left to right, as one would naively assume? Because I can't think of any other way to implement lazy evaluation than going from right to left.










      share|improve this question













      As someone with a programming background, here's a fundamental thing about the Cycles node tree I've been thinking about. Let's say you have a node setup like this:



      enter image description here



      Here we have two computationally intensive shaders, called "Gigantic Shader A" and "Gigantic Shader B", and a Mix Shader is used to switch between them using some black-and-white mask.



      In programming terms, this setup can be thought of as an if-else construct, with shader A and B being the two execution branches of the if-else construct.



      Now, in basically every sane programming language, the two if-else branches are evaluated "lazily", meaning that either only the first branch or only the second branch is evaluated, depending on whether the if condition evaluates to true or false.



      So here are my questions:



      (1) Does Cycles have this lazy evaluation optimization as well? More specifically, is Cycles smart enough to figure out that if Fac=0 or Fac=1 in the above Mix Shader, it can completely ignore either shader B or shader A?



      (2) If Cycles does have this optimization, then is it also true that the node tree is actually evaluated from right to left, starting at the Material Output node, and not from left to right, as one would naively assume? Because I can't think of any other way to implement lazy evaluation than going from right to left.







      cycles nodes






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 4 hours ago









      Nam-Quang Tran

      836




      836




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote













          When executing shaders, a special optimization is applied to Mix Shader nodes. If Factor evaluates to 0 or 1, any nodes that are only reachable via the unused branch of the mix are not evaluated.



          Taken from this page:



          https://docs.blender.org/manual/en/dev/render/cycles/optimizations/nodes.html



          This page also describes how the node tree is evaluated.






          share|improve this answer




















          • Hmm... the page only mentions the Mix Shader under Run-Time Optimizations. So I guess there's no such optimization for the MixRGB node, which can also be used for if-else branching? :-/
            – Nam-Quang Tran
            3 hours ago






          • 1




            @Nam-QuangTran It works with MixRGB node the same. Pls read the link zzubnik provided.
            – Jaroslav Jerryno Novotny
            2 hours ago










          • @Jaroslav Jerryno Novotny: I've read the page, and the MixRGB node is indeed mentioned there, but only under "Node Optimizations". This means, in my interpretation, that the MixRGB node is only optimized during the compile-time tree simplication step, not during run-time, unlike the Mix Shader node. Either that, or the run-time section of the documentation is incomplete.
            – Nam-Quang Tran
            2 hours ago











          • @Nam-QuangTran The tree is evaluated and optimized before every frame that renders. The MixRGB node would change values in single frame time-frame only when motion-blur sub-sampling. I'd have to check the source code or test to see if there is a difference in sub-sampling, I doubt it. Maybe it's only the documentation wording like you say.
            – Jaroslav Jerryno Novotny
            1 hour ago










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "502"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fblender.stackexchange.com%2fquestions%2f119826%2flazy-and-right-to-left-evaluation-in-the-node-tree%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote













          When executing shaders, a special optimization is applied to Mix Shader nodes. If Factor evaluates to 0 or 1, any nodes that are only reachable via the unused branch of the mix are not evaluated.



          Taken from this page:



          https://docs.blender.org/manual/en/dev/render/cycles/optimizations/nodes.html



          This page also describes how the node tree is evaluated.






          share|improve this answer




















          • Hmm... the page only mentions the Mix Shader under Run-Time Optimizations. So I guess there's no such optimization for the MixRGB node, which can also be used for if-else branching? :-/
            – Nam-Quang Tran
            3 hours ago






          • 1




            @Nam-QuangTran It works with MixRGB node the same. Pls read the link zzubnik provided.
            – Jaroslav Jerryno Novotny
            2 hours ago










          • @Jaroslav Jerryno Novotny: I've read the page, and the MixRGB node is indeed mentioned there, but only under "Node Optimizations". This means, in my interpretation, that the MixRGB node is only optimized during the compile-time tree simplication step, not during run-time, unlike the Mix Shader node. Either that, or the run-time section of the documentation is incomplete.
            – Nam-Quang Tran
            2 hours ago











          • @Nam-QuangTran The tree is evaluated and optimized before every frame that renders. The MixRGB node would change values in single frame time-frame only when motion-blur sub-sampling. I'd have to check the source code or test to see if there is a difference in sub-sampling, I doubt it. Maybe it's only the documentation wording like you say.
            – Jaroslav Jerryno Novotny
            1 hour ago














          up vote
          4
          down vote













          When executing shaders, a special optimization is applied to Mix Shader nodes. If Factor evaluates to 0 or 1, any nodes that are only reachable via the unused branch of the mix are not evaluated.



          Taken from this page:



          https://docs.blender.org/manual/en/dev/render/cycles/optimizations/nodes.html



          This page also describes how the node tree is evaluated.






          share|improve this answer




















          • Hmm... the page only mentions the Mix Shader under Run-Time Optimizations. So I guess there's no such optimization for the MixRGB node, which can also be used for if-else branching? :-/
            – Nam-Quang Tran
            3 hours ago






          • 1




            @Nam-QuangTran It works with MixRGB node the same. Pls read the link zzubnik provided.
            – Jaroslav Jerryno Novotny
            2 hours ago










          • @Jaroslav Jerryno Novotny: I've read the page, and the MixRGB node is indeed mentioned there, but only under "Node Optimizations". This means, in my interpretation, that the MixRGB node is only optimized during the compile-time tree simplication step, not during run-time, unlike the Mix Shader node. Either that, or the run-time section of the documentation is incomplete.
            – Nam-Quang Tran
            2 hours ago











          • @Nam-QuangTran The tree is evaluated and optimized before every frame that renders. The MixRGB node would change values in single frame time-frame only when motion-blur sub-sampling. I'd have to check the source code or test to see if there is a difference in sub-sampling, I doubt it. Maybe it's only the documentation wording like you say.
            – Jaroslav Jerryno Novotny
            1 hour ago












          up vote
          4
          down vote










          up vote
          4
          down vote









          When executing shaders, a special optimization is applied to Mix Shader nodes. If Factor evaluates to 0 or 1, any nodes that are only reachable via the unused branch of the mix are not evaluated.



          Taken from this page:



          https://docs.blender.org/manual/en/dev/render/cycles/optimizations/nodes.html



          This page also describes how the node tree is evaluated.






          share|improve this answer












          When executing shaders, a special optimization is applied to Mix Shader nodes. If Factor evaluates to 0 or 1, any nodes that are only reachable via the unused branch of the mix are not evaluated.



          Taken from this page:



          https://docs.blender.org/manual/en/dev/render/cycles/optimizations/nodes.html



          This page also describes how the node tree is evaluated.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 4 hours ago









          zzubnik

          814




          814











          • Hmm... the page only mentions the Mix Shader under Run-Time Optimizations. So I guess there's no such optimization for the MixRGB node, which can also be used for if-else branching? :-/
            – Nam-Quang Tran
            3 hours ago






          • 1




            @Nam-QuangTran It works with MixRGB node the same. Pls read the link zzubnik provided.
            – Jaroslav Jerryno Novotny
            2 hours ago










          • @Jaroslav Jerryno Novotny: I've read the page, and the MixRGB node is indeed mentioned there, but only under "Node Optimizations". This means, in my interpretation, that the MixRGB node is only optimized during the compile-time tree simplication step, not during run-time, unlike the Mix Shader node. Either that, or the run-time section of the documentation is incomplete.
            – Nam-Quang Tran
            2 hours ago











          • @Nam-QuangTran The tree is evaluated and optimized before every frame that renders. The MixRGB node would change values in single frame time-frame only when motion-blur sub-sampling. I'd have to check the source code or test to see if there is a difference in sub-sampling, I doubt it. Maybe it's only the documentation wording like you say.
            – Jaroslav Jerryno Novotny
            1 hour ago
















          • Hmm... the page only mentions the Mix Shader under Run-Time Optimizations. So I guess there's no such optimization for the MixRGB node, which can also be used for if-else branching? :-/
            – Nam-Quang Tran
            3 hours ago






          • 1




            @Nam-QuangTran It works with MixRGB node the same. Pls read the link zzubnik provided.
            – Jaroslav Jerryno Novotny
            2 hours ago










          • @Jaroslav Jerryno Novotny: I've read the page, and the MixRGB node is indeed mentioned there, but only under "Node Optimizations". This means, in my interpretation, that the MixRGB node is only optimized during the compile-time tree simplication step, not during run-time, unlike the Mix Shader node. Either that, or the run-time section of the documentation is incomplete.
            – Nam-Quang Tran
            2 hours ago











          • @Nam-QuangTran The tree is evaluated and optimized before every frame that renders. The MixRGB node would change values in single frame time-frame only when motion-blur sub-sampling. I'd have to check the source code or test to see if there is a difference in sub-sampling, I doubt it. Maybe it's only the documentation wording like you say.
            – Jaroslav Jerryno Novotny
            1 hour ago















          Hmm... the page only mentions the Mix Shader under Run-Time Optimizations. So I guess there's no such optimization for the MixRGB node, which can also be used for if-else branching? :-/
          – Nam-Quang Tran
          3 hours ago




          Hmm... the page only mentions the Mix Shader under Run-Time Optimizations. So I guess there's no such optimization for the MixRGB node, which can also be used for if-else branching? :-/
          – Nam-Quang Tran
          3 hours ago




          1




          1




          @Nam-QuangTran It works with MixRGB node the same. Pls read the link zzubnik provided.
          – Jaroslav Jerryno Novotny
          2 hours ago




          @Nam-QuangTran It works with MixRGB node the same. Pls read the link zzubnik provided.
          – Jaroslav Jerryno Novotny
          2 hours ago












          @Jaroslav Jerryno Novotny: I've read the page, and the MixRGB node is indeed mentioned there, but only under "Node Optimizations". This means, in my interpretation, that the MixRGB node is only optimized during the compile-time tree simplication step, not during run-time, unlike the Mix Shader node. Either that, or the run-time section of the documentation is incomplete.
          – Nam-Quang Tran
          2 hours ago





          @Jaroslav Jerryno Novotny: I've read the page, and the MixRGB node is indeed mentioned there, but only under "Node Optimizations". This means, in my interpretation, that the MixRGB node is only optimized during the compile-time tree simplication step, not during run-time, unlike the Mix Shader node. Either that, or the run-time section of the documentation is incomplete.
          – Nam-Quang Tran
          2 hours ago













          @Nam-QuangTran The tree is evaluated and optimized before every frame that renders. The MixRGB node would change values in single frame time-frame only when motion-blur sub-sampling. I'd have to check the source code or test to see if there is a difference in sub-sampling, I doubt it. Maybe it's only the documentation wording like you say.
          – Jaroslav Jerryno Novotny
          1 hour ago




          @Nam-QuangTran The tree is evaluated and optimized before every frame that renders. The MixRGB node would change values in single frame time-frame only when motion-blur sub-sampling. I'd have to check the source code or test to see if there is a difference in sub-sampling, I doubt it. Maybe it's only the documentation wording like you say.
          – Jaroslav Jerryno Novotny
          1 hour ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fblender.stackexchange.com%2fquestions%2f119826%2flazy-and-right-to-left-evaluation-in-the-node-tree%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          List of Gilmore Girls characters

          What does second last employer means? [closed]

          One-line joke