[Bf-blender-cvs] [dd8bfa0] master: Code refactor: reduce special node types, use generic constant folding.
Brecht Van Lommel
noreply at git.blender.org
Thu May 5 21:44:27 CEST 2016
Commit: dd8bfa0929f9f1ae940e7da0468fbdd6744e50bb
Author: Brecht Van Lommel
Date: Mon May 2 00:05:16 2016 +0200
Branches: master
https://developer.blender.org/rBdd8bfa0929f9f1ae940e7da0468fbdd6744e50bb
Code refactor: reduce special node types, use generic constant folding.
===================================================================
M intern/cycles/blender/blender_shader.cpp
M intern/cycles/render/graph.cpp
M intern/cycles/render/graph.h
M intern/cycles/render/mesh.cpp
M intern/cycles/render/nodes.cpp
M intern/cycles/render/nodes.h
M intern/cycles/render/osl.cpp
M intern/cycles/render/shader.cpp
M intern/cycles/render/svm.cpp
===================================================================
diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp
index 3f919bc..6e4ca66 100644
--- a/intern/cycles/blender/blender_shader.cpp
+++ b/intern/cycles/blender/blender_shader.cpp
@@ -32,7 +32,7 @@ CCL_NAMESPACE_BEGIN
typedef map<void*, ShaderInput*> PtrInputMap;
typedef map<void*, ShaderOutput*> PtrOutputMap;
-typedef map<std::string, ProxyNode*> ProxyMap;
+typedef map<std::string, ConvertNode*> ProxyMap;
/* Find */
@@ -321,10 +321,6 @@ static ShaderNode *add_node(Scene *scene,
BL::ShaderNodeMixRGB b_mix_node(b_node);
MixNode *mix = new MixNode();
mix->type = MixNode::type_enum[b_mix_node.blend_type()];
- /* Tag if it's Mix */
- if(b_mix_node.blend_type() == 0)
- mix->special_type = SHADER_SPECIAL_TYPE_MIX_RGB;
-
mix->use_clamp = b_mix_node.use_clamp();
node = mix;
}
@@ -1029,7 +1025,8 @@ static void add_nodes(Scene *scene,
BL::Node::internal_links_iterator b_link;
for(b_node->internal_links.begin(b_link); b_link != b_node->internal_links.end(); ++b_link) {
BL::NodeSocket to_socket(b_link->to_socket());
- ProxyNode *proxy = new ProxyNode(convert_socket_type(to_socket));
+ ShaderSocketType to_socket_type = convert_socket_type(to_socket);
+ ConvertNode *proxy = new ConvertNode(to_socket_type, to_socket_type, true);
input_map[b_link->from_socket().ptr.data] = proxy->inputs[0];
output_map[b_link->to_socket().ptr.data] = proxy->outputs[0];
@@ -1051,7 +1048,8 @@ static void add_nodes(Scene *scene,
* so that links have something to connect to and assert won't fail.
*/
for(b_node->inputs.begin(b_input); b_input != b_node->inputs.end(); ++b_input) {
- ProxyNode *proxy = new ProxyNode(convert_socket_type(*b_input));
+ ShaderSocketType input_type = convert_socket_type(*b_input);
+ ConvertNode *proxy = new ConvertNode(input_type, input_type, true);
graph->add(proxy);
/* register the proxy node for internal binding */
@@ -1062,7 +1060,8 @@ static void add_nodes(Scene *scene,
set_default_value(proxy->inputs[0], *b_input, b_data, b_ntree);
}
for(b_node->outputs.begin(b_output); b_output != b_node->outputs.end(); ++b_output) {
- ProxyNode *proxy = new ProxyNode(convert_socket_type(*b_output));
+ ShaderSocketType output_type = convert_socket_type(*b_output);
+ ConvertNode *proxy = new ConvertNode(output_type, output_type, true);
graph->add(proxy);
/* register the proxy node for internal binding */
@@ -1088,7 +1087,7 @@ static void add_nodes(Scene *scene,
for(b_node->outputs.begin(b_output); b_output != b_node->outputs.end(); ++b_output) {
ProxyMap::const_iterator proxy_it = proxy_input_map.find(b_output->identifier());
if(proxy_it != proxy_input_map.end()) {
- ProxyNode *proxy = proxy_it->second;
+ ConvertNode *proxy = proxy_it->second;
output_map[b_output->ptr.data] = proxy->outputs[0];
}
@@ -1102,7 +1101,7 @@ static void add_nodes(Scene *scene,
for(b_node->inputs.begin(b_input); b_input != b_node->inputs.end(); ++b_input) {
ProxyMap::const_iterator proxy_it = proxy_output_map.find(b_input->identifier());
if(proxy_it != proxy_output_map.end()) {
- ProxyNode *proxy = proxy_it->second;
+ ConvertNode *proxy = proxy_it->second;
input_map[b_input->ptr.data] = proxy->inputs[0];
diff --git a/intern/cycles/render/graph.cpp b/intern/cycles/render/graph.cpp
index 4a9b2f1..b23fd2a 100644
--- a/intern/cycles/render/graph.cpp
+++ b/intern/cycles/render/graph.cpp
@@ -290,18 +290,21 @@ void ShaderGraph::disconnect(ShaderInput *to)
from->links.erase(remove(from->links.begin(), from->links.end(), to), from->links.end());
}
-void ShaderGraph::relink(vector<ShaderInput*> inputs, vector<ShaderInput*> outputs, ShaderOutput *output)
+void ShaderGraph::relink(ShaderNode *node, ShaderOutput *from, ShaderOutput *to)
{
- /* Remove nodes and re-link if output isn't NULL. */
- foreach(ShaderInput *sock, inputs) {
+ /* Copy because disconnect modifies this list */
+ vector<ShaderInput*> outputs = from->links;
+
+ /* Bypass node by moving all links from "from" to "to" */
+ foreach(ShaderInput *sock, node->inputs) {
if(sock->link)
disconnect(sock);
}
foreach(ShaderInput *sock, outputs) {
disconnect(sock);
- if(output)
- connect(output, sock);
+ if(to)
+ connect(to, sock);
}
}
@@ -411,39 +414,29 @@ void ShaderGraph::copy_nodes(ShaderNodeSet& nodes, ShaderNodeMap& nnodemap)
/* Graph simplification */
/* ******************** */
-/* Step 1: Remove unused nodes.
- * Remove nodes which are not needed in the graph, such as proxies,
- * mix nodes with a factor of 0 or 1, emission shaders without contribution...
+/* Step 1: Remove proxy nodes.
+ * These only exists temporarily when exporting groups, and we must remove them
+ * early so that node->attributes() and default links do not see them.
*/
-void ShaderGraph::remove_unneeded_nodes()
+void ShaderGraph::remove_proxy_nodes()
{
vector<bool> removed(num_node_ids, false);
bool any_node_removed = false;
- ShaderNode *geom = NULL;
-
- /* find and unlink proxy nodes */
foreach(ShaderNode *node, nodes) {
if(node->special_type == SHADER_SPECIAL_TYPE_PROXY) {
- ProxyNode *proxy = static_cast<ProxyNode*>(node);
+ ConvertNode *proxy = static_cast<ConvertNode*>(node);
ShaderInput *input = proxy->inputs[0];
ShaderOutput *output = proxy->outputs[0];
- /* temp. copy of the output links list.
- * output->links is modified when we disconnect!
- */
- vector<ShaderInput*> links(output->links);
- ShaderOutput *from = input->link;
-
/* bypass the proxy node */
- if(from) {
- disconnect(input);
- foreach(ShaderInput *to, links) {
- disconnect(to);
- connect(from, to);
- }
+ if(input->link) {
+ relink(proxy, output, input->link);
}
else {
+ /* Copy because disconnect modifies this list */
+ vector<ShaderInput*> links(output->links);
+
foreach(ShaderInput *to, links) {
/* remove any autoconvert nodes too if they lead to
* sockets with an automatically set default value */
@@ -465,132 +458,16 @@ void ShaderGraph::remove_unneeded_nodes()
}
disconnect(to);
-
+
/* transfer the default input value to the target socket */
to->set(input->value);
to->set(input->value_string);
}
}
-
+
removed[proxy->id] = true;
any_node_removed = true;
}
- else if(node->special_type == SHADER_SPECIAL_TYPE_BACKGROUND) {
- BackgroundNode *bg = static_cast<BackgroundNode*>(node);
-
- if(bg->outputs[0]->links.size()) {
- /* Black color or zero strength, remove node */
- if((!bg->inputs[0]->link && bg->inputs[0]->value == make_float3(0.0f, 0.0f, 0.0f)) ||
- (!bg->inputs[1]->link && bg->inputs[1]->value.x == 0.0f))
- {
- vector<ShaderInput*> inputs = bg->outputs[0]->links;
-
- relink(bg->inputs, inputs, NULL);
- removed[bg->id] = true;
- any_node_removed = true;
- }
- }
- }
- else if(node->special_type == SHADER_SPECIAL_TYPE_EMISSION) {
- EmissionNode *em = static_cast<EmissionNode*>(node);
-
- if(em->outputs[0]->links.size()) {
- /* Black color or zero strength, remove node */
- if((!em->inputs[0]->link && em->inputs[0]->value == make_float3(0.0f, 0.0f, 0.0f)) ||
- (!em->inputs[1]->link && em->inputs[1]->value.x == 0.0f))
- {
- vector<ShaderInput*> inputs = em->outputs[0]->links;
-
- relink(em->inputs, inputs, NULL);
- removed[em->id] = true;
- any_node_removed = true;
- }
- }
- }
- else if(node->special_type == SHADER_SPECIAL_TYPE_BUMP) {
- BumpNode *bump = static_cast<BumpNode*>(node);
-
- if(bump->outputs[0]->links.size()) {
- /* Height inputs is not connected. */
- /* TODO(sergey): Ignore bump with zero strength. */
- if(bump->inputs[0]->link == NULL) {
- vector<ShaderInput*> inputs = bump->outputs[0]->links;
- if(bump->inputs[4]->link == NULL) {
- if(geom == NULL) {
- geom = new GeometryNode();
- }
- relink(bump->inputs, inputs, geom->output("Normal"));
- }
- else {
- relink(bump->inputs, inputs, bump->input("Normal")->link);
- }
- removed[bump->id] = true;
- any_node_removed = true;
- }
- }
- }
- else if(node->special_type == SHADER_SPECIAL_TYPE_MIX_CLOSURE) {
- MixClosureNode *mix = static_cast<MixClosureNode*>(node);
-
- /* remove useless mix closures nodes */
- if(mix->outputs[0]->links.size() && mix->inputs[1]->link == mix->inputs[2]->link) {
- ShaderOutput *output = mix->inputs[1]->link;
- vector<ShaderInput*> inputs = mix->outputs[0]->links;
-
- relink(mix->inputs, inputs, output);
- removed[mix->id] = true;
- any_node_removed = true;
- }
-
- /* remove unused mix closure input when factor is 0.0 or 1.0 */
- /* check for closure links and make sure factor link is disconnected */
- if(mix->outputs[0]->links.size() && mix->inputs[1]->link && mix->inputs[2]->link && !mix->inputs[0]->link) {
- /* factor 0.0 */
- if(mix->inputs[0]->value.x == 0.0f) {
- ShaderOutput *output = mix->inputs[1]->link;
- vector<ShaderInput*> inputs = mix->outputs[0]->links;
-
- relink(mix->inputs, inputs, output);
- removed[mix->id] = true;
- any_node_removed = true;
- }
- /* factor 1.0 */
- else if(mix->inputs[0]->value.x == 1.0f) {
- ShaderOutput *output = mix->inputs[2]->link;
- vector<ShaderInput*> inputs = mix->outputs[0]->links;
-
- relink(mix->inputs, inputs, output);
- removed[mix->id] = true;
- any_node_removed = true;
- }
- }
- }
- else if(node->special_type == SHADER_SPECIAL_TYPE_MIX_RGB) {
- MixNode *mix = static_cast<MixNode*>(node);
-
- /* remove unused Mix RGB inputs when factor is 0.0 or 1.0 */
- /* check for color links and make sure factor link is disconnected */
- if(mix->outputs[0]->links.size() && mix->inputs[1]->link && mix->inputs[2]->link && !mix->inputs[0]->link) {
- /* factor 0.0 */
- if(mix->inputs[0]->value.x == 0.0f) {
- ShaderOutput *output = mix->inputs[1]->link;
- vector<ShaderInput*> inputs = mix->outputs[0]->links;
-
- relink(mix->inputs, inputs, output);
- removed[mix-
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list