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 // clang-format off
39 #include "kernel/kernel_types.h"
40 #include "kernel/kernel_montecarlo.h"
41 
42 #include "kernel/closure/alloc.h"
43 #include "kernel/closure/bsdf_util.h"
44 #include "kernel/closure/bsdf_diffuse.h"
45 #include "kernel/closure/bsdf_principled_diffuse.h"
46 #include "kernel/closure/bssrdf.h"
47 // clang-format on
48 
49 CCL_NAMESPACE_BEGIN
50 
51 using namespace OSL;
52 
53 static ustring u_cubic("cubic");
54 static ustring u_gaussian("gaussian");
55 static ustring u_burley("burley");
56 static ustring u_principled("principled");
57 static ustring u_random_walk("random_walk");
58 static ustring u_principled_random_walk("principled_random_walk");
59 
60 class CBSSRDFClosure : public CClosurePrimitive {
61  public:
62   Bssrdf params;
63   ustring method;
64 
CBSSRDFClosure()65   CBSSRDFClosure()
66   {
67     params.texture_blur = 0.0f;
68     params.sharpness = 0.0f;
69     params.roughness = 0.0f;
70   }
71 
setup(ShaderData * sd,int path_flag,float3 weight)72   void setup(ShaderData *sd, int path_flag, float3 weight)
73   {
74     if (method == u_cubic) {
75       alloc(sd, path_flag, weight, CLOSURE_BSSRDF_CUBIC_ID);
76     }
77     else if (method == u_gaussian) {
78       alloc(sd, path_flag, weight, CLOSURE_BSSRDF_GAUSSIAN_ID);
79     }
80     else if (method == u_burley) {
81       alloc(sd, path_flag, weight, CLOSURE_BSSRDF_BURLEY_ID);
82     }
83     else if (method == u_principled) {
84       alloc(sd, path_flag, weight, CLOSURE_BSSRDF_PRINCIPLED_ID);
85     }
86     else if (method == u_random_walk) {
87       alloc(sd, path_flag, weight, CLOSURE_BSSRDF_RANDOM_WALK_ID);
88     }
89     else if (method == u_principled_random_walk) {
90       alloc(sd, path_flag, weight, CLOSURE_BSSRDF_PRINCIPLED_RANDOM_WALK_ID);
91     }
92   }
93 
alloc(ShaderData * sd,int path_flag,float3 weight,ClosureType type)94   void alloc(ShaderData *sd, int path_flag, float3 weight, ClosureType type)
95   {
96     Bssrdf *bssrdf = bssrdf_alloc(sd, weight);
97 
98     if (bssrdf) {
99       /* disable in case of diffuse ancestor, can't see it well then and
100        * adds considerably noise due to probabilities of continuing path
101        * getting lower and lower */
102       if (path_flag & PATH_RAY_DIFFUSE_ANCESTOR) {
103         params.radius = make_float3(0.0f, 0.0f, 0.0f);
104       }
105 
106       /* create one closure per color channel */
107       bssrdf->radius = params.radius;
108       bssrdf->albedo = params.albedo;
109       bssrdf->texture_blur = params.texture_blur;
110       bssrdf->sharpness = params.sharpness;
111       bssrdf->N = params.N;
112       bssrdf->roughness = params.roughness;
113       sd->flag |= bssrdf_setup(sd, bssrdf, (ClosureType)type);
114     }
115   }
116 };
117 
closure_bssrdf_params()118 ClosureParam *closure_bssrdf_params()
119 {
120   static ClosureParam params[] = {
121       CLOSURE_STRING_PARAM(CBSSRDFClosure, method),
122       CLOSURE_FLOAT3_PARAM(CBSSRDFClosure, params.N),
123       CLOSURE_FLOAT3_PARAM(CBSSRDFClosure, params.radius),
124       CLOSURE_FLOAT3_PARAM(CBSSRDFClosure, params.albedo),
125       CLOSURE_FLOAT_KEYPARAM(CBSSRDFClosure, params.texture_blur, "texture_blur"),
126       CLOSURE_FLOAT_KEYPARAM(CBSSRDFClosure, params.sharpness, "sharpness"),
127       CLOSURE_FLOAT_KEYPARAM(CBSSRDFClosure, params.roughness, "roughness"),
128       CLOSURE_STRING_KEYPARAM(CBSSRDFClosure, label, "label"),
129       CLOSURE_FINISH_PARAM(CBSSRDFClosure)};
130   return params;
131 }
132 
133 CCLOSURE_PREPARE(closure_bssrdf_prepare, CBSSRDFClosure)
134 
135 CCL_NAMESPACE_END
136