Cycles: add random walk subsurface scattering to Principled BSDF.
[blender.git] / intern / cycles / kernel / osl / osl_bssrdf.cpp
1 /*
2  * Adapted from Open Shading Language with this license:
3  *
4  * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
5  * All Rights Reserved.
6  *
7  * Modifications Copyright 2011, Blender Foundation.
8  * 
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are
11  * met:
12  * * Redistributions of source code must retain the above copyright
13  *   notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in the
16  *   documentation and/or other materials provided with the distribution.
17  * * Neither the name of Sony Pictures Imageworks nor the names of its
18  *   contributors may be used to endorse or promote products derived from
19  *   this software without specific prior written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <OSL/genclosure.h>
34
35 #include "kernel/kernel_compat_cpu.h"
36 #include "kernel/osl/osl_closures.h"
37
38 #include "kernel/kernel_types.h"
39 #include "kernel/kernel_montecarlo.h"
40
41 #include "kernel/closure/alloc.h"
42 #include "kernel/closure/bsdf_util.h"
43 #include "kernel/closure/bsdf_diffuse.h"
44 #include "kernel/closure/bsdf_principled_diffuse.h"
45 #include "kernel/closure/bssrdf.h"
46
47 CCL_NAMESPACE_BEGIN
48
49 using namespace OSL;
50
51 static ustring u_cubic("cubic");
52 static ustring u_gaussian("gaussian");
53 static ustring u_burley("burley");
54 static ustring u_principled("principled");
55 static ustring u_random_walk("random_walk");
56 static ustring u_principled_random_walk("principled_random_walk");
57
58 class CBSSRDFClosure : public CClosurePrimitive {
59 public:
60         Bssrdf params;
61         ustring method;
62
63         CBSSRDFClosure()
64         {
65                 params.texture_blur = 0.0f;
66                 params.sharpness = 0.0f;
67                 params.roughness = 0.0f;
68         }
69
70         void setup(ShaderData *sd, int path_flag, float3 weight)
71         {
72                 if (method == u_cubic) {
73                         alloc(sd, path_flag, weight, CLOSURE_BSSRDF_CUBIC_ID);
74                 }
75                 else if (method == u_gaussian) {
76                         alloc(sd, path_flag, weight, CLOSURE_BSSRDF_GAUSSIAN_ID);
77                 }
78                 else if (method == u_burley) {
79                         alloc(sd, path_flag, weight, CLOSURE_BSSRDF_BURLEY_ID);
80                 }
81                 else if (method == u_principled) {
82                         alloc(sd, path_flag, weight, CLOSURE_BSSRDF_PRINCIPLED_ID);
83                 }
84                 else if (method == u_random_walk) {
85                         alloc(sd, path_flag, weight, CLOSURE_BSSRDF_RANDOM_WALK_ID);
86                 }
87                 else if (method == u_principled_random_walk) {
88                         alloc(sd, path_flag, weight, CLOSURE_BSSRDF_PRINCIPLED_RANDOM_WALK_ID);
89                 }
90         }
91
92         void alloc(ShaderData *sd, int path_flag, float3 weight, ClosureType type)
93         {
94                 Bssrdf *bssrdf = bssrdf_alloc(sd, weight);
95
96                 if(bssrdf) {
97                         /* disable in case of diffuse ancestor, can't see it well then and
98                          * adds considerably noise due to probabilities of continuing path
99                          * getting lower and lower */
100                         if(path_flag & PATH_RAY_DIFFUSE_ANCESTOR) {
101                                 params.radius = make_float3(0.0f, 0.0f, 0.0f);
102                         }
103
104                         /* create one closure per color channel */
105                         bssrdf->radius = params.radius;
106                         bssrdf->albedo = params.albedo;
107                         bssrdf->texture_blur = params.texture_blur;
108                         bssrdf->sharpness = params.sharpness;
109                         bssrdf->N = params.N;
110                         bssrdf->roughness = params.roughness;
111                         sd->flag |= bssrdf_setup(sd, bssrdf, (ClosureType)type);
112                 }
113         }
114 };
115
116 ClosureParam *closure_bssrdf_params()
117 {
118         static ClosureParam params[] = {
119                 CLOSURE_STRING_PARAM(CBSSRDFClosure, method),
120                 CLOSURE_FLOAT3_PARAM(CBSSRDFClosure, params.N),
121                 CLOSURE_FLOAT3_PARAM(CBSSRDFClosure, params.radius),
122                 CLOSURE_FLOAT3_PARAM(CBSSRDFClosure, params.albedo),
123                 CLOSURE_FLOAT_KEYPARAM(CBSSRDFClosure, params.texture_blur, "texture_blur"),
124                 CLOSURE_FLOAT_KEYPARAM(CBSSRDFClosure, params.sharpness, "sharpness"),
125                 CLOSURE_FLOAT_KEYPARAM(CBSSRDFClosure, params.roughness, "roughness"),
126                 CLOSURE_STRING_KEYPARAM(CBSSRDFClosure, label, "label"),
127                 CLOSURE_FINISH_PARAM(CBSSRDFClosure)
128         };
129         return params;
130 }
131
132 CCLOSURE_PREPARE(closure_bssrdf_prepare, CBSSRDFClosure)
133
134 CCL_NAMESPACE_END
135