[Bf-blender-cvs] [7bd95ae5730] soc-2019-cycles-procedural: Implement constant folding for mapping node.

OmarSquircleArt noreply at git.blender.org
Tue Jun 4 13:24:11 CEST 2019


Commit: 7bd95ae57309248bab10f8e3f14f4917f9b8c0e1
Author: OmarSquircleArt
Date:   Tue Jun 4 13:24:51 2019 +0200
Branches: soc-2019-cycles-procedural
https://developer.blender.org/rB7bd95ae57309248bab10f8e3f14f4917f9b8c0e1

Implement constant folding for mapping node.

===================================================================

M	intern/cycles/kernel/CMakeLists.txt
M	intern/cycles/kernel/svm/svm.h
M	intern/cycles/kernel/svm/svm_mapping.h
A	intern/cycles/kernel/svm/svm_mapping_util.h
M	intern/cycles/render/constant_fold.cpp
M	intern/cycles/render/constant_fold.h
M	intern/cycles/render/nodes.cpp
M	intern/cycles/render/nodes.h

===================================================================

diff --git a/intern/cycles/kernel/CMakeLists.txt b/intern/cycles/kernel/CMakeLists.txt
index 8a8fee108ae..c1bc73257eb 100644
--- a/intern/cycles/kernel/CMakeLists.txt
+++ b/intern/cycles/kernel/CMakeLists.txt
@@ -199,6 +199,7 @@ set(SRC_SVM_HEADERS
   svm/svm_light_path.h
   svm/svm_magic.h
   svm/svm_mapping.h
+  svm/svm_mapping_util.h
   svm/svm_math.h
   svm/svm_math_util.h
   svm/svm_mix.h
diff --git a/intern/cycles/kernel/svm/svm.h b/intern/cycles/kernel/svm/svm.h
index 5c609e8fee5..5eb8331b046 100644
--- a/intern/cycles/kernel/svm/svm.h
+++ b/intern/cycles/kernel/svm/svm.h
@@ -153,6 +153,7 @@ CCL_NAMESPACE_END
 
 #include "kernel/svm/svm_color_util.h"
 #include "kernel/svm/svm_math_util.h"
+#include "kernel/svm/svm_mapping_util.h"
 
 #include "kernel/svm/svm_attribute.h"
 #include "kernel/svm/svm_gradient.h"
diff --git a/intern/cycles/kernel/svm/svm_mapping.h b/intern/cycles/kernel/svm/svm_mapping.h
index a0889e32ed6..c59db8b7390 100644
--- a/intern/cycles/kernel/svm/svm_mapping.h
+++ b/intern/cycles/kernel/svm/svm_mapping.h
@@ -49,30 +49,7 @@ ccl_device void svm_node_mapping(KernelGlobals *kg,
   float3 size = stack_load_float3(stack, node1.z);
 
   float3 r;
-  Transform rot_t;
-  if (vec_type == NODE_MAPPING_TYPE_TEXTURE) {
-    rot_t = euler_to_transform(-rot);
-  }
-  else {
-    rot_t = euler_to_transform(rot);
-  }
-
-  switch (vec_type) {
-    case NODE_MAPPING_TYPE_POINT:
-      r = transform_direction(&rot_t, (vec * size)) + loc;
-      break;
-    case NODE_MAPPING_TYPE_TEXTURE:
-      r = safe_divide_float3(transform_direction(&rot_t, (vec - loc)), size);
-      break;
-    case NODE_MAPPING_TYPE_VECTOR:
-      r = transform_direction(&rot_t, (vec * size));
-      break;
-    case NODE_MAPPING_TYPE_NORMAL:
-      r = safe_normalize(transform_direction(&rot_t, safe_divide_float3(vec, size)));
-      break;
-    default:
-      r = make_float3(0.0f, 0.0f, 0.0f);
-  }
+  svm_mapping(&r, vec_type, vec, loc, rot, size);
   stack_store_float3(stack, node1.w, r);
 }
 
diff --git a/intern/cycles/kernel/svm/svm_mapping_util.h b/intern/cycles/kernel/svm/svm_mapping_util.h
new file mode 100644
index 00000000000..29218b275f9
--- /dev/null
+++ b/intern/cycles/kernel/svm/svm_mapping_util.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2011-2014 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 void svm_mapping(
+    float3 *outVec, NodeMappingType vecType, float3 vecIn, float3 loc, float3 rot, float3 size)
+{
+  Transform rot_t;
+  if (vecType == NODE_MAPPING_TYPE_TEXTURE) {
+    rot_t = euler_to_transform(-rot);
+  }
+  else {
+    rot_t = euler_to_transform(rot);
+  }
+
+  switch (vecType) {
+    case NODE_MAPPING_TYPE_POINT:
+      *outVec = transform_direction(&rot_t, (vecIn * size)) + loc;
+      break;
+    case NODE_MAPPING_TYPE_TEXTURE:
+      *outVec = safe_divide_float3(transform_direction(&rot_t, (vecIn - loc)), size);
+      break;
+    case NODE_MAPPING_TYPE_VECTOR:
+      *outVec = transform_direction(&rot_t, (vecIn * size));
+      break;
+    case NODE_MAPPING_TYPE_NORMAL:
+      *outVec = safe_normalize(transform_direction(&rot_t, safe_divide_float3(vecIn, size)));
+      break;
+    default:
+      *outVec = make_float3(0.0f, 0.0f, 0.0f);
+  }
+}
+
+CCL_NAMESPACE_END
diff --git a/intern/cycles/render/constant_fold.cpp b/intern/cycles/render/constant_fold.cpp
index 482441a8433..b3647a1e671 100644
--- a/intern/cycles/render/constant_fold.cpp
+++ b/intern/cycles/render/constant_fold.cpp
@@ -429,4 +429,21 @@ void ConstantFolder::fold_vector_math(NodeVectorMath type) const
   }
 }
 
+void ConstantFolder::fold_mapping(NodeMappingType type) const
+{
+  ShaderInput *vec_in = node->input("Vector");
+  ShaderInput *loc_in = node->input("Location");
+  ShaderInput *rot_in = node->input("Rotation");
+  ShaderInput *size_in = node->input("Scale");
+
+  if (is_zero(size_in)) {
+    make_zero();
+  }
+  else if ((is_zero(loc_in) || type == NODE_MAPPING_TYPE_VECTOR ||
+            type == NODE_MAPPING_TYPE_NORMAL) &&
+           is_zero(rot_in) && is_one(size_in)) {
+    try_bypass_or_make_constant(vec_in);
+  }
+}
+
 CCL_NAMESPACE_END
diff --git a/intern/cycles/render/constant_fold.h b/intern/cycles/render/constant_fold.h
index c14b94868dc..b19afcd92b9 100644
--- a/intern/cycles/render/constant_fold.h
+++ b/intern/cycles/render/constant_fold.h
@@ -66,6 +66,7 @@ class ConstantFolder {
   void fold_mix(NodeMix type, bool clamp) const;
   void fold_math(NodeMath type, bool clamp) const;
   void fold_vector_math(NodeVectorMath type) const;
+  void fold_mapping(NodeMappingType type) const;
 };
 
 CCL_NAMESPACE_END
diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp
index c82e8be08d5..57c1437a6fd 100644
--- a/intern/cycles/render/nodes.cpp
+++ b/intern/cycles/render/nodes.cpp
@@ -25,6 +25,7 @@
 #include "kernel/svm/svm_color_util.h"
 #include "kernel/svm/svm_ramp_util.h"
 #include "kernel/svm/svm_math_util.h"
+#include "kernel/svm/svm_mapping_util.h"
 #include "render/osl.h"
 #include "render/constant_fold.h"
 
@@ -1666,12 +1667,12 @@ NODE_DEFINE(MappingNode)
   type_enum.insert("normal", NODE_MAPPING_TYPE_NORMAL);
   SOCKET_ENUM(vector_type, "Type", type_enum, NODE_MAPPING_TYPE_TEXTURE);
 
-  SOCKET_IN_POINT(vector, "Vector", make_float3(0.0f, 0.0f, 0.0f));
+  SOCKET_IN_POINT(vector_in, "Vector", make_float3(0.0f, 0.0f, 0.0f));
   SOCKET_IN_POINT(location, "Location", make_float3(0.0f, 0.0f, 0.0f));
   SOCKET_IN_POINT(rotation, "Rotation", make_float3(0.0f, 0.0f, 0.0f));
   SOCKET_IN_POINT(scale, "Scale", make_float3(1.0f, 1.0f, 1.0f));
 
-  SOCKET_OUT_POINT(vector, "Vector");
+  SOCKET_OUT_POINT(vector_out, "Vector");
 
   return type;
 }
@@ -1680,6 +1681,19 @@ MappingNode::MappingNode() : ShaderNode(node_type)
 {
 }
 
+void MappingNode::constant_fold(const ConstantFolder &folder)
+{
+  float3 vector_out;
+
+  if (folder.all_inputs_constant()) {
+    svm_mapping(&vector_out, vector_type, vector_in, location, rotation, scale);
+    folder.make_constant(vector_out);
+  }
+  else {
+    folder.fold_mapping((NodeMappingType)vector_type);
+  }
+}
+
 void MappingNode::compile(SVMCompiler &compiler)
 {
   ShaderInput *vector_in = input("Vector");
diff --git a/intern/cycles/render/nodes.h b/intern/cycles/render/nodes.h
index 9ca9f79566e..3916425e486 100644
--- a/intern/cycles/render/nodes.h
+++ b/intern/cycles/render/nodes.h
@@ -377,8 +377,9 @@ class MappingNode : public ShaderNode {
   {
     return NODE_GROUP_LEVEL_2;
   }
+  void constant_fold(const ConstantFolder &folder);
 
-  float3 vector, location, rotation, scale;
+  float3 vector_in, location, rotation, scale;
   NodeMappingType vector_type;
 };



More information about the Bf-blender-cvs mailing list