Cycles: Make all #include statements relative to cycles source directory
[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_diffuse.h"
43 #include "kernel/closure/bssrdf.h"
44
45 CCL_NAMESPACE_BEGIN
46
47 using namespace OSL;
48
49 class CBSSRDFClosure : public CClosurePrimitive {
50 public:
51         Bssrdf params;
52         float3 radius;
53         float3 albedo;
54
55         void alloc(ShaderData *sd, int path_flag, float3 weight, ClosureType type)
56         {
57                 float sample_weight = fabsf(average(weight));
58
59                 /* disable in case of diffuse ancestor, can't see it well then and
60                  * adds considerably noise due to probabilities of continuing path
61                  * getting lower and lower */
62                 if(path_flag & PATH_RAY_DIFFUSE_ANCESTOR) {
63                         radius = make_float3(0.0f, 0.0f, 0.0f);
64                 }
65
66                 if(sample_weight > CLOSURE_WEIGHT_CUTOFF) {
67                         /* sharpness */
68                         float sharpness = params.sharpness;
69                         /* texture color blur */
70                         float texture_blur = params.texture_blur;
71
72                         /* create one closure per color channel */
73                         Bssrdf *bssrdf = bssrdf_alloc(sd, make_float3(weight.x, 0.0f, 0.0f));
74                         if(bssrdf) {
75                                 bssrdf->sample_weight = sample_weight;
76                                 bssrdf->radius = radius.x;
77                                 bssrdf->texture_blur = texture_blur;
78                                 bssrdf->albedo = albedo.x;
79                                 bssrdf->sharpness = sharpness;
80                                 bssrdf->N = params.N;
81                                 sd->flag |= bssrdf_setup(bssrdf, (ClosureType)type);
82                         }
83
84                         bssrdf = bssrdf_alloc(sd, make_float3(0.0f, weight.y, 0.0f));
85                         if(bssrdf) {
86                                 bssrdf->sample_weight = sample_weight;
87                                 bssrdf->radius = radius.y;
88                                 bssrdf->texture_blur = texture_blur;
89                                 bssrdf->albedo = albedo.y;
90                                 bssrdf->sharpness = sharpness;
91                                 bssrdf->N = params.N;
92                                 sd->flag |= bssrdf_setup(bssrdf, (ClosureType)type);
93                         }
94
95                         bssrdf = bssrdf_alloc(sd, make_float3(0.0f, 0.0f, weight.z));
96                         if(bssrdf) {
97                                 bssrdf->sample_weight = sample_weight;
98                                 bssrdf->radius = radius.z;
99                                 bssrdf->texture_blur = texture_blur;
100                                 bssrdf->albedo = albedo.z;
101                                 bssrdf->sharpness = sharpness;
102                                 bssrdf->N = params.N;
103                                 sd->flag |= bssrdf_setup(bssrdf, (ClosureType)type);
104                         }
105                 }
106         }
107 };
108
109 /* Cubic */
110
111 class CubicBSSRDFClosure : public CBSSRDFClosure {
112 public:
113         void setup(ShaderData *sd, int path_flag, float3 weight)
114         {
115                 alloc(sd, path_flag, weight, CLOSURE_BSSRDF_CUBIC_ID);
116         }
117 };
118
119 ClosureParam *closure_bssrdf_cubic_params()
120 {
121         static ClosureParam params[] = {
122                 CLOSURE_FLOAT3_PARAM(CubicBSSRDFClosure, params.N),
123                 CLOSURE_FLOAT3_PARAM(CubicBSSRDFClosure, radius),
124                 CLOSURE_FLOAT_PARAM(CubicBSSRDFClosure, params.texture_blur),
125                 CLOSURE_FLOAT_PARAM(CubicBSSRDFClosure, params.sharpness),
126                 CLOSURE_STRING_KEYPARAM(CubicBSSRDFClosure, label, "label"),
127                 CLOSURE_FINISH_PARAM(CubicBSSRDFClosure)
128         };
129         return params;
130 }
131
132 CCLOSURE_PREPARE(closure_bssrdf_cubic_prepare, CubicBSSRDFClosure)
133
134 /* Gaussian */
135
136 class GaussianBSSRDFClosure : public CBSSRDFClosure {
137 public:
138         void setup(ShaderData *sd, int path_flag, float3 weight)
139         {
140                 alloc(sd, path_flag, weight, CLOSURE_BSSRDF_GAUSSIAN_ID);
141         }
142 };
143
144 ClosureParam *closure_bssrdf_gaussian_params()
145 {
146         static ClosureParam params[] = {
147                 CLOSURE_FLOAT3_PARAM(GaussianBSSRDFClosure, params.N),
148                 CLOSURE_FLOAT3_PARAM(GaussianBSSRDFClosure, radius),
149                 CLOSURE_FLOAT_PARAM(GaussianBSSRDFClosure, params.texture_blur),
150                 CLOSURE_STRING_KEYPARAM(GaussianBSSRDFClosure, label, "label"),
151                 CLOSURE_FINISH_PARAM(GaussianBSSRDFClosure)
152         };
153         return params;
154 }
155
156 CCLOSURE_PREPARE(closure_bssrdf_gaussian_prepare, GaussianBSSRDFClosure)
157
158 /* Burley */
159
160 class BurleyBSSRDFClosure : public CBSSRDFClosure {
161 public:
162         void setup(ShaderData *sd, int path_flag, float3 weight)
163         {
164                 alloc(sd, path_flag, weight, CLOSURE_BSSRDF_BURLEY_ID);
165         }
166 };
167
168 ClosureParam *closure_bssrdf_burley_params()
169 {
170         static ClosureParam params[] = {
171                 CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, params.N),
172                 CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, radius),
173                 CLOSURE_FLOAT_PARAM(BurleyBSSRDFClosure, params.texture_blur),
174                 CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, albedo),
175                 CLOSURE_STRING_KEYPARAM(BurleyBSSRDFClosure, label, "label"),
176                 CLOSURE_FINISH_PARAM(BurleyBSSRDFClosure)
177         };
178         return params;
179 }
180
181 CCLOSURE_PREPARE(closure_bssrdf_burley_prepare, BurleyBSSRDFClosure)
182
183 CCL_NAMESPACE_END
184