Welcome to mirror list, hosted at ThFree Co, Russian Federation.

constant_fold.cpp « render « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1fee6b2c0813ac92b89d8d95099991691c17f63a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * 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.
 */

#include "constant_fold.h"
#include "graph.h"

#include "util_foreach.h"

CCL_NAMESPACE_BEGIN

ConstantFolder::ConstantFolder(ShaderGraph *graph, ShaderNode *node, ShaderOutput *output)
: graph(graph), node(node), output(output)
{
}

bool ConstantFolder::all_inputs_constant() const
{
	foreach(ShaderInput *input, node->inputs) {
		if(input->link) {
			return false;
		}
	}

	return true;
}

void ConstantFolder::make_constant(float value) const
{
	foreach(ShaderInput *sock, output->links) {
		sock->set(value);
	}

	graph->disconnect(output);
}

void ConstantFolder::make_constant(float3 value) const
{
	foreach(ShaderInput *sock, output->links) {
		sock->set(value);
	}

	graph->disconnect(output);
}

void ConstantFolder::make_constant_clamp(float value, bool clamp) const
{
	make_constant(clamp ? saturate(value) : value);
}

void ConstantFolder::make_constant_clamp(float3 value, bool clamp) const
{
	if (clamp) {
		value.x = saturate(value.x);
		value.y = saturate(value.y);
		value.z = saturate(value.z);
	}

	make_constant(value);
}

void ConstantFolder::bypass(ShaderOutput *new_output) const
{
	assert(new_output);

	/* Remove all outgoing links from socket and connect them to new_output instead.
	 * The graph->relink method affects node inputs, so it's not safe to use in constant
	 * folding if the node has multiple outputs and will thus be folded multiple times. */
	vector<ShaderInput*> outputs = output->links;

	graph->disconnect(output);

	foreach(ShaderInput *sock, outputs) {
		graph->connect(new_output, sock);
	}
}

void ConstantFolder::discard() const
{
	assert(output->type() == SocketType::CLOSURE);
	graph->disconnect(output);
}

void ConstantFolder::bypass_or_discard(ShaderInput *input) const
{
	assert(input->type() == SocketType::CLOSURE);

	if (input->link) {
		bypass(input->link);
	}
	else {
		discard();
	}
}

bool ConstantFolder::try_bypass_or_make_constant(ShaderInput *input, float3 input_value, bool clamp) const
{
	if(!input->link) {
		make_constant_clamp(input_value, clamp);
		return true;
	}
	else if(!clamp) {
		bypass(input->link);
		return true;
	}

	return false;
}

CCL_NAMESPACE_END