1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __CONSTANT_FOLD_H__
18 #define __CONSTANT_FOLD_H__
19 
20 #include "kernel/svm/svm_types.h"
21 #include "util/util_types.h"
22 
23 CCL_NAMESPACE_BEGIN
24 
25 class Scene;
26 class ShaderGraph;
27 class ShaderInput;
28 class ShaderNode;
29 class ShaderOutput;
30 
31 class ConstantFolder {
32  public:
33   ShaderGraph *const graph;
34   ShaderNode *const node;
35   ShaderOutput *const output;
36 
37   Scene *scene;
38 
39   ConstantFolder(ShaderGraph *graph, ShaderNode *node, ShaderOutput *output, Scene *scene);
40 
41   bool all_inputs_constant() const;
42 
43   /* Constant folding helpers */
44   void make_constant(float value) const;
45   void make_constant(float3 value) const;
46   void make_constant_clamp(float value, bool clamp) const;
47   void make_constant_clamp(float3 value, bool clamp) const;
48   void make_zero() const;
49   void make_one() const;
50 
51   /* Bypass node, relinking to another output socket. */
52   void bypass(ShaderOutput *output) const;
53 
54   /* For closure nodes, discard node entirely or bypass to one of its inputs. */
55   void discard() const;
56   void bypass_or_discard(ShaderInput *input) const;
57 
58   /* Bypass or make constant, unless we can't due to clamp being true. */
59   bool try_bypass_or_make_constant(ShaderInput *input, bool clamp = false) const;
60 
61   /* Test if shader inputs of the current nodes have fixed values. */
62   bool is_zero(ShaderInput *input) const;
63   bool is_one(ShaderInput *input) const;
64 
65   /* Specific nodes. */
66   void fold_mix(NodeMix type, bool clamp) const;
67   void fold_math(NodeMathType type) const;
68   void fold_vector_math(NodeVectorMathType type) const;
69   void fold_mapping(NodeMappingType type) const;
70 };
71 
72 CCL_NAMESPACE_END
73 
74 #endif /* __CONSTANT_FOLD_H__ */
75