Cycles: add Displacement node.
authorBrecht Van Lommel <brechtvanlommel@gmail.com>
Sat, 13 Jan 2018 12:11:03 +0000 (13:11 +0100)
committerBrecht Van Lommel <brechtvanlommel@gmail.com>
Tue, 23 Jan 2018 10:12:26 +0000 (11:12 +0100)
This converts object space height to world space displacement, to be
linked to the new vector displacement material output.

Differential Revision: https://developer.blender.org/D3015

16 files changed:
intern/cycles/blender/blender_shader.cpp
intern/cycles/kernel/shaders/CMakeLists.txt
intern/cycles/kernel/shaders/node_displacement.osl [new file with mode: 0644]
intern/cycles/kernel/svm/svm.h
intern/cycles/kernel/svm/svm_displace.h
intern/cycles/kernel/svm/svm_types.h
intern/cycles/render/nodes.cpp
intern/cycles/render/nodes.h
release/scripts/startup/nodeitems_builtins.py
source/blender/blenkernel/BKE_node.h
source/blender/blenkernel/intern/node.c
source/blender/gpu/shaders/gpu_shader_material.glsl
source/blender/nodes/CMakeLists.txt
source/blender/nodes/NOD_shader.h
source/blender/nodes/NOD_static_types.h
source/blender/nodes/shader/nodes/node_shader_displacement.c [new file with mode: 0644]

index 67845bb5561db83d170a8623cef205a685c3b7de..45633b2920947cb8c8d56c0e64f13fbf9671b618 100644 (file)
@@ -872,6 +872,9 @@ static ShaderNode *add_node(Scene *scene,
                bevel->samples = b_bevel_node.samples();
                node = bevel;
        }
+       else if(b_node.is_a(&RNA_ShaderNodeDisplacement)) {
+               node = new DisplacementNode();
+       }
 
        if(node) {
                node->name = b_node.name();
index 83cfdbcf8baafbf4c6c48acfb24001615ba268f5..fb983a445795bec851e0d825704ce5af4c21d5ce 100644 (file)
@@ -23,6 +23,7 @@ set(SRC_OSL
        node_convert_from_point.osl
        node_convert_from_vector.osl
        node_diffuse_bsdf.osl
+       node_displacement.osl
        node_emission.osl
        node_environment_texture.osl
        node_fresnel.osl
diff --git a/intern/cycles/kernel/shaders/node_displacement.osl b/intern/cycles/kernel/shaders/node_displacement.osl
new file mode 100644 (file)
index 0000000..fb81533
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * 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 "stdosl.h"
+
+shader node_displacement(
+       float Height = 0.0,
+       float Scale = 1.0,
+       normal Normal = N,
+       output vector Displacement = vector(0.0, 0.0, 0.0))
+{
+       Displacement = normalize(transform("object", Normal));
+       Displacement *= Height * Scale;
+       Displacement = transform("object", "world", Displacement);
+}
+
index d3dac5706d0b8cf5d930d83d700da875c78488d1..cbc603d4645539df035a88cc9dc71f9be30e39cf 100644 (file)
@@ -267,6 +267,9 @@ ccl_device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ccl_a
                        case NODE_SET_DISPLACEMENT:
                                svm_node_set_displacement(kg, sd, stack, node.y);
                                break;
+                       case NODE_DISPLACEMENT:
+                               svm_node_displacement(kg, sd, stack, node);
+                               break;
 #  endif  /* NODES_FEATURE(NODE_FEATURE_BUMP) */
 #  ifdef __TEXTURES__
                        case NODE_TEX_IMAGE:
index 656357be52d474560d9db17e6c5776659bd6e085..1ac2c8fd52e36534ef6a308b7bd0df7790197ea7 100644 (file)
@@ -22,8 +22,8 @@ ccl_device void svm_node_set_bump(KernelGlobals *kg, ShaderData *sd, float *stac
 {
 #ifdef __RAY_DIFFERENTIALS__
        /* get normal input */
-       uint normal_offset, distance_offset, invert, use_object_space;
-       decode_node_uchar4(node.y, &normal_offset, &distance_offset, &invert, &use_object_space);
+       uint normal_offset, scale_offset, invert, use_object_space;
+       decode_node_uchar4(node.y, &normal_offset, &scale_offset, &invert, &use_object_space);
 
        float3 normal_in = stack_valid(normal_offset)? stack_load_float3(stack, normal_offset): sd->N;
 
@@ -55,15 +55,15 @@ ccl_device void svm_node_set_bump(KernelGlobals *kg, ShaderData *sd, float *stac
        float absdet = fabsf(det);
 
        float strength = stack_load_float(stack, strength_offset);
-       float distance = stack_load_float(stack, distance_offset);
+       float scale = stack_load_float(stack, scale_offset);
 
        if(invert)
-               distance *= -1.0f;
+               scale *= -1.0f;
 
        strength = max(strength, 0.0f);
 
        /* compute and output perturbed normal */
-       float3 normal_out = safe_normalize(absdet*normal_in - distance*signf(det)*surfgrad);
+       float3 normal_out = safe_normalize(absdet*normal_in - scale*signf(det)*surfgrad);
        if(is_zero(normal_out)) {
                normal_out = normal_in;
        }
@@ -95,5 +95,22 @@ ccl_device void svm_node_set_displacement(KernelGlobals *kg, ShaderData *sd, flo
        sd->P += dP;
 }
 
+ccl_device void svm_node_displacement(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
+{
+       uint height_offset, scale_offset, normal_offset, displacement_offset;
+       decode_node_uchar4(node.y, &height_offset, &scale_offset, &normal_offset, &displacement_offset);
+
+       float height = stack_load_float(stack, height_offset);
+       float scale = stack_load_float(stack, scale_offset);
+       float3 normal = stack_valid(normal_offset)? stack_load_float3(stack, normal_offset): sd->N;
+
+       float3 dP = normal;
+       object_inverse_normal_transform(kg, sd, &dP);
+       dP *= height * scale;
+       object_dir_transform(kg, sd, &dP);
+
+       stack_store_float3(stack, displacement_offset, dP);
+}
+
 CCL_NAMESPACE_END
 
index c7fe7948422098d263a4043e50f823af558acd57..00c7752da8c24523f0a101bb2b3e6ff0ae8a2ab7 100644 (file)
@@ -133,6 +133,7 @@ typedef enum ShaderNodeType {
        NODE_ENTER_BUMP_EVAL,
        NODE_LEAVE_BUMP_EVAL,
        NODE_BEVEL,
+       NODE_DISPLACEMENT,
 } ShaderNodeType;
 
 typedef enum NodeAttributeType {
index 4452fadaf0240b1c70ee151f2a645b33f8b6ce61..87e8ec3d3ccaa9e4183b7b472b71baf76002dd4f 100644 (file)
@@ -5644,4 +5644,43 @@ void BevelNode::compile(OSLCompiler& compiler)
        compiler.add(this, "node_bevel");
 }
 
+/* Displacement */
+
+NODE_DEFINE(DisplacementNode)
+{
+       NodeType* type = NodeType::add("displacement", create, NodeType::SHADER);
+
+       SOCKET_IN_FLOAT(height, "Height", 0.0f);
+       SOCKET_IN_FLOAT(scale, "Scale", 1.0f);
+       SOCKET_IN_NORMAL(normal, "Normal", make_float3(0.0f, 0.0f, 0.0f), SocketType::LINK_NORMAL);
+
+       SOCKET_OUT_VECTOR(displacement, "Displacement");
+
+       return type;
+}
+
+DisplacementNode::DisplacementNode()
+: ShaderNode(node_type)
+{
+}
+
+void DisplacementNode::compile(SVMCompiler& compiler)
+{
+       ShaderInput *height_in = input("Height");
+       ShaderInput *scale_in = input("Scale");
+       ShaderInput *normal_in = input("Normal");
+       ShaderOutput *displacement_out = output("Displacement");
+
+       compiler.add_node(NODE_DISPLACEMENT,
+               compiler.encode_uchar4(compiler.stack_assign(height_in),
+                                      compiler.stack_assign(scale_in),
+                                      compiler.stack_assign_if_linked(normal_in),
+                                      compiler.stack_assign(displacement_out)));
+}
+
+void DisplacementNode::compile(OSLCompiler& compiler)
+{
+       compiler.add(this, "node_displacement");
+}
+
 CCL_NAMESPACE_END
index 64d7522a23ab295686162fd11312e9a106b36500..d1f32bbf491d454386d446348f93f1c1683174bd 100644 (file)
@@ -1027,6 +1027,18 @@ public:
        int samples;
 };
 
+class DisplacementNode : public ShaderNode {
+public:
+       SHADER_NODE_CLASS(DisplacementNode)
+       virtual int get_feature() {
+               return NODE_FEATURE_BUMP;
+       }
+
+       float height;
+       float scale;
+       float3 normal;
+};
+
 CCL_NAMESPACE_END
 
 #endif /* __NODES_H__ */
index 8a8032e5bfb6584dbf76b9b568cf87f613b57a20..fd458c71b7f5f62df488cf82f1c62cb3d5442d17 100644 (file)
@@ -271,6 +271,7 @@ shader_node_categories = [
     ShaderNewNodeCategory("SH_NEW_OP_VECTOR", "Vector", items=[
         NodeItem("ShaderNodeMapping"),
         NodeItem("ShaderNodeBump"),
+        NodeItem("ShaderNodeDisplacement"),
         NodeItem("ShaderNodeNormalMap"),
         NodeItem("ShaderNodeNormal"),
         NodeItem("ShaderNodeVectorCurve"),
index 30175a42ce55a5b2aa99efb71f9e0b1dc26b0b3a..854a3e64acc8f09f9065b5fcf5e158e70cb90834 100644 (file)
@@ -790,6 +790,7 @@ struct ShadeResult;
 #define SH_NODE_TEX_POINTDENSITY               192
 #define SH_NODE_BSDF_PRINCIPLED         193
 #define SH_NODE_BEVEL                   197
+#define SH_NODE_DISPLACEMENT            198
 
 /* custom defines options for Material node */
 #define SH_NODE_MAT_DIFF   1
index 3bec7cf386aa0465c5661a7877da54d7ac7ffac3..932b8b0c30ade9f9e308d58e4525bed82b0c354a 100644 (file)
@@ -3589,6 +3589,7 @@ static void registerShaderNodes(void)
 
        register_node_type_sh_attribute();
        register_node_type_sh_bevel();
+       register_node_type_sh_displacement();
        register_node_type_sh_geometry();
        register_node_type_sh_light_path();
        register_node_type_sh_light_falloff();
index f98d7b9f5bbac6cab4c9a307520d3f737641bf8e..b89317da84a5d553cd219e5162c0e2ffa3f6d48c 100644 (file)
@@ -3816,6 +3816,11 @@ void node_bevel(float radius, vec3 N, out vec3 result)
        result = N;
 }
 
+void node_displacement(float height, float dist, vec3 N, out vec3 result)
+{
+       result = height * dist * N;
+}
+
 /* output */
 
 void node_output_material(vec4 surface, vec4 volume, float displacement, out vec4 result)
index 21f9afec9031dab176602b3fe5248bf3cba8fc49..f8cbd14a346a5ab85a98c68202b952b6c06e6073 100644 (file)
@@ -193,6 +193,7 @@ set(SRC
        shader/nodes/node_shader_subsurface_scattering.c
        shader/nodes/node_shader_tangent.c
        shader/nodes/node_shader_bevel.c
+       shader/nodes/node_shader_displacement.c
        shader/nodes/node_shader_tex_brick.c
        shader/nodes/node_shader_tex_checker.c
        shader/nodes/node_shader_tex_coord.c
index f7d36e3dbefd4c9a0b06ae562d4d66b85d4bf7ad..9590b0c79448b812f433d1a2389054ce24da305a 100644 (file)
@@ -79,6 +79,7 @@ void register_node_type_sh_tex_pointdensity(void);
 
 void register_node_type_sh_attribute(void);
 void register_node_type_sh_bevel(void);
+void register_node_type_sh_displacement(void);
 void register_node_type_sh_geometry(void);
 void register_node_type_sh_light_path(void);
 void register_node_type_sh_light_falloff(void);
index 13844298c5aa60b6177ba58da2d5c13faf59b371..56a34a92b934b38954e60ec2825a049c805a3a0f 100644 (file)
@@ -127,6 +127,7 @@ DefNode( ShaderNode,     SH_NODE_UVALONGSTROKE,      def_sh_uvalongstroke,   "UV
 DefNode( ShaderNode,     SH_NODE_SEPXYZ,             0,                      "SEPXYZ",             SeparateXYZ,      "Separate XYZ",      ""       )
 DefNode( ShaderNode,     SH_NODE_COMBXYZ,            0,                      "COMBXYZ",            CombineXYZ,       "Combine XYZ",       ""       )
 DefNode( ShaderNode,     SH_NODE_BEVEL,              def_sh_bevel,           "BEVEL",              Bevel,            "Bevel",             ""       )
+DefNode( ShaderNode,     SH_NODE_DISPLACEMENT,       0,                      "DISPLACEMENT",       Displacement,     "Displacement",      ""       )
 
 DefNode( CompositorNode, CMP_NODE_VIEWER,         def_cmp_viewer,         "VIEWER",         Viewer,           "Viewer",            ""              )
 DefNode( CompositorNode, CMP_NODE_RGB,            0,                      "RGB",            RGB,              "RGB",               ""              )
diff --git a/source/blender/nodes/shader/nodes/node_shader_displacement.c b/source/blender/nodes/shader/nodes/node_shader_displacement.c
new file mode 100644 (file)
index 0000000..bcd11b7
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2005 Blender Foundation.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): none yet.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include "../node_shader_util.h"
+
+/* **************** OUTPUT ******************** */
+
+static bNodeSocketTemplate sh_node_displacement_in[] = {
+       {       SOCK_FLOAT, 0, N_("Height"), 0.00f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f},
+       {       SOCK_FLOAT, 0, N_("Scale"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1000.0f},
+       {       SOCK_VECTOR, 1, N_("Normal"), 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
+       {       -1, 0, ""       }
+};
+
+static bNodeSocketTemplate sh_node_displacement_out[] = {
+       {       SOCK_VECTOR, 0, N_("Displacement"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
+       {       -1, 0, ""       }
+};
+
+static int gpu_shader_displacement(GPUMaterial *mat, bNode *UNUSED(node), bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
+{
+       if (!in[2].link) {
+               GPU_link(mat, "direction_transform_m4v3", GPU_builtin(GPU_VIEW_NORMAL), GPU_builtin(GPU_INVERSE_VIEW_MATRIX), &in[2].link);
+       }
+
+       return GPU_stack_link(mat, "node_displacement", in, out);
+}
+
+/* node type definition */
+void register_node_type_sh_displacement(void)
+{
+       static bNodeType ntype;
+
+       sh_node_type_base(&ntype, SH_NODE_DISPLACEMENT, "Displacement", NODE_CLASS_OP_VECTOR, 0);
+       node_type_compatibility(&ntype, NODE_NEW_SHADING);
+       node_type_socket_templates(&ntype, sh_node_displacement_in, sh_node_displacement_out);
+       node_type_storage(&ntype, "", NULL, NULL);
+       node_type_gpu(&ntype, gpu_shader_displacement);
+
+       nodeRegisterType(&ntype);
+}