Merge branch 'master' into blender2.8
[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
56 class CBSSRDFClosure : public CClosurePrimitive {
57 public:
58         Bssrdf params;
59         ustring method;
60
61         CBSSRDFClosure()
62         {
63                 params.texture_blur = 0.0f;
64                 params.sharpness = 0.0f;
65                 params.roughness = 0.0f;
66         }
67
68         void setup(ShaderData *sd, int path_flag, float3 weight)
69         {
70                 if (method == u_cubic) {
71                         alloc(sd, path_flag, weight, CLOSURE_BSSRDF_CUBIC_ID);
72                 }
73                 else if (method == u_gaussian) {
74                         alloc(sd, path_flag, weight, CLOSURE_BSSRDF_GAUSSIAN_ID);
75                 }
76                 else if (method == u_burley) {
77                         alloc(sd, path_flag, weight, CLOSURE_BSSRDF_BURLEY_ID);
78                 }
79                 else if (method == u_principled) {
80                         alloc(sd, path_flag, weight, CLOSURE_BSSRDF_PRINCIPLED_ID);
81                 }
82         }
83
84         void alloc(ShaderData *sd, int path_flag, float3 weight, ClosureType type)
85         {
86                 Bssrdf *bssrdf = bssrdf_alloc(sd, weight);
87
88                 if(bssrdf) {
89                         /* disable in case of diffuse ancestor, can't see it well then and
90                          * adds considerably noise due to probabilities of continuing path
91                          * getting lower and lower */
92                         if(path_flag & PATH_RAY_DIFFUSE_ANCESTOR) {
93                                 params.radius = make_float3(0.0f, 0.0f, 0.0f);
94                         }
95
96                         /* create one closure per color channel */
97                         bssrdf->radius = params.radius;
98                         bssrdf->albedo = params.albedo;
99                         bssrdf->texture_blur = params.texture_blur;
100                         bssrdf->sharpness = params.sharpness;
101                         bssrdf->N = params.N;
102                         bssrdf->roughness = params.roughness;
103                         sd->flag |= bssrdf_setup(sd, bssrdf, (ClosureType)type);
104                 }
105         }
106 };
107
108 ClosureParam *closure_bssrdf_params()
109 {
110         static ClosureParam params[] = {
111                 CLOSURE_STRING_PARAM(CBSSRDFClosure, method),
112                 CLOSURE_FLOAT3_PARAM(CBSSRDFClosure, params.N),
113                 CLOSURE_FLOAT3_PARAM(CBSSRDFClosure, params.radius),
114                 CLOSURE_FLOAT3_PARAM(CBSSRDFClosure, params.albedo),
115                 CLOSURE_FLOAT_KEYPARAM(CBSSRDFClosure, params.texture_blur, "texture_blur"),
116                 CLOSURE_FLOAT_KEYPARAM(CBSSRDFClosure, params.sharpness, "sharpness"),
117                 CLOSURE_FLOAT_KEYPARAM(CBSSRDFClosure, params.roughness, "roughness"),
118                 CLOSURE_STRING_KEYPARAM(CBSSRDFClosure, label, "label"),
119                 CLOSURE_FINISH_PARAM(CBSSRDFClosure)
120         };
121         return params;
122 }
123
124 CCLOSURE_PREPARE(closure_bssrdf_prepare, CBSSRDFClosure)
125
126 CCL_NAMESPACE_END
127