Cycles / SSS Render Passes:
[blender.git] / intern / cycles / kernel / kernel_subsurface.h
1 /*
2  * Copyright 2013, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 CCL_NAMESPACE_BEGIN
20
21 #define BSSRDF_MULTI_EVAL
22 #define BSSRDF_SKIP_NO_HIT
23
24 __device float bssrdf_sample_distance(KernelGlobals *kg, float radius, float refl, float u)
25 {
26         int table_offset = kernel_data.bssrdf.table_offset;
27         float r = lookup_table_read_2D(kg, u, refl, table_offset, BSSRDF_RADIUS_TABLE_SIZE, BSSRDF_REFL_TABLE_SIZE);
28
29         return r*radius;
30 }
31
32 #ifdef BSSRDF_MULTI_EVAL
33 __device float bssrdf_pdf(KernelGlobals *kg, float radius, float refl, float r)
34 {
35         if(r >= radius)
36                 return 0.0f;
37
38         /* todo: when we use the real BSSRDF this will need to be divided by the maximum
39          * radius instead of the average radius */
40         float t = r/radius;
41
42         int table_offset = kernel_data.bssrdf.table_offset + BSSRDF_PDF_TABLE_OFFSET;
43         float pdf = lookup_table_read_2D(kg, t, refl, table_offset, BSSRDF_RADIUS_TABLE_SIZE, BSSRDF_REFL_TABLE_SIZE);
44
45         pdf /= radius;
46
47         return pdf;
48 }
49 #endif
50
51 __device ShaderClosure *subsurface_scatter_pick_closure(KernelGlobals *kg, ShaderData *sd, float *probability)
52 {
53         /* sum sample weights of bssrdf and bsdf */
54         float bsdf_sum = 0.0f;
55         float bssrdf_sum = 0.0f;
56
57         for(int i = 0; i < sd->num_closure; i++) {
58                 ShaderClosure *sc = &sd->closure[i];
59                 
60                 if(CLOSURE_IS_BSDF(sc->type))
61                         bsdf_sum += sc->sample_weight;
62                 else if(CLOSURE_IS_BSSRDF(sc->type))
63                         bssrdf_sum += sc->sample_weight;
64         }
65
66         /* use bsdf or bssrdf? */
67         float r = sd->randb_closure*(bsdf_sum + bssrdf_sum);
68
69         if(r < bsdf_sum) {
70                 /* use bsdf, and adjust randb so we can reuse it for picking a bsdf */
71                 sd->randb_closure = r/bsdf_sum;
72                 *probability = (bsdf_sum > 0.0f)? (bsdf_sum + bssrdf_sum)/bsdf_sum: 1.0f;
73                 return NULL;
74         }
75
76         /* use bssrdf */
77         r -= bsdf_sum;
78         sd->randb_closure = 0.0f; /* not needed anymore */
79
80         float sum = 0.0f;
81
82         for(int i = 0; i < sd->num_closure; i++) {
83                 ShaderClosure *sc = &sd->closure[i];
84                 
85                 if(CLOSURE_IS_BSSRDF(sc->type)) {
86                         sum += sc->sample_weight;
87
88                         if(r <= sum) {
89 #ifdef BSSRDF_MULTI_EVAL
90                                 *probability = (bssrdf_sum > 0.0f)? (bsdf_sum + bssrdf_sum)/bssrdf_sum: 1.0f;
91 #else
92                                 *probability = (bssrdf_sum > 0.0f)? (bsdf_sum + bssrdf_sum)/sc->sample_weight: 1.0f;
93 #endif
94                                 return sc;
95                         }
96                 }
97         }
98
99         /* should never happen */
100         *probability = 1.0f;
101         return NULL;
102 }
103
104 #ifdef BSSRDF_MULTI_EVAL
105 __device float3 subsurface_scatter_multi_eval(KernelGlobals *kg, ShaderData *sd, bool hit, float refl, float *r, int num_r, bool all)
106 {
107         /* compute pdf */
108         float3 eval_sum = make_float3(0.0f, 0.0f, 0.0f);
109         float pdf_sum = 0.0f;
110         float sample_weight_sum = 0.0f;
111         int num_bssrdf = 0;
112
113         for(int i = 0; i < sd->num_closure; i++) {
114                 ShaderClosure *sc = &sd->closure[i];
115                 
116                 if(CLOSURE_IS_BSSRDF(sc->type)) {
117                         float sample_weight = (all)? 1.0f: sc->sample_weight;
118
119                         /* compute pdf */
120                         float pdf = 1.0f;
121                         for(int i = 0; i < num_r; i++)
122                                 pdf *= bssrdf_pdf(kg, sc->data0, refl, r[i]);
123
124                         eval_sum += sc->weight*pdf;
125                         pdf_sum += sample_weight*pdf;
126
127                         sample_weight_sum += sample_weight;
128                         num_bssrdf++;
129                 }
130         }
131
132         float inv_pdf_sum;
133         
134         if(pdf_sum > 0.0f) {
135                 /* in case of non-progressive integrate we sample all bssrdf's once,
136                  * for progressive we pick one, so adjust pdf for that */
137                 if(all)
138                         inv_pdf_sum = 1.0f/pdf_sum;
139                 else
140                         inv_pdf_sum = sample_weight_sum/pdf_sum;
141         }
142         else
143                 inv_pdf_sum = 0.0f;
144
145         float3 weight = eval_sum * inv_pdf_sum;
146
147         return weight;
148 }
149 #endif
150
151 /* replace closures with a single diffuse bsdf closure after scatter step */
152 __device void subsurface_scatter_setup_diffuse_bsdf(ShaderData *sd, float3 weight)
153 {
154         ShaderClosure *sc = &sd->closure[0];
155         sd->num_closure = 1;
156
157         sc->weight = weight;
158         sc->sample_weight = 1.0f;
159         sc->data0 = 0.0f;
160         sc->data1 = 0.0f;
161         sc->N = sd->N;
162         sd->flag &= ~SD_CLOSURE_FLAGS;
163         sd->flag |= bsdf_diffuse_setup(sc);
164         sd->randb_closure = 0.0f;
165
166         /* replace CLOSURE_BSDF_DIFFUSE_ID with this special ID so render passes
167          * can recognize it as not being a regular diffuse closure */
168         sc->type = CLOSURE_BSDF_BSSRDF_ID;
169
170         /* todo: evaluate shading to get blurred textures and bump mapping */
171         /* shader_eval_surface(kg, sd, 0.0f, state_flag, SHADER_CONTEXT_SSS); */
172 }
173
174 /* subsurface scattering step, from a point on the surface to another nearby point on the same object */
175 __device void subsurface_scatter_step(KernelGlobals *kg, ShaderData *sd, int state_flag, ShaderClosure *sc, uint *lcg_state, bool all)
176 {
177         float radius = sc->data0;
178         float refl = max(average(sc->weight)*3.0f, 0.0f);
179         float r = 0.0f;
180         bool hit = false;
181         float3 weight = make_float3(1.0f, 1.0f, 1.0f);
182 #ifdef BSSRDF_MULTI_EVAL
183         float r_attempts[BSSRDF_MAX_ATTEMPTS];
184 #endif
185         int num_attempts;
186
187         /* attempt to find a hit a given number of times before giving up */
188         for(num_attempts = 0; num_attempts < kernel_data.bssrdf.num_attempts; num_attempts++) {
189                 /* random numbers for sampling */
190                 float u1 = lcg_step(lcg_state);
191                 float u2 = lcg_step(lcg_state);
192                 float u3 = lcg_step(lcg_state);
193                 float u4 = lcg_step(lcg_state);
194                 float u5 = lcg_step(lcg_state);
195                 float u6 = lcg_step(lcg_state);
196
197                 r = bssrdf_sample_distance(kg, radius, refl, u5);
198 #ifdef BSSRDF_MULTI_EVAL
199                 r_attempts[num_attempts] = r;
200 #endif
201
202                 float3 p1 = sd->P + sample_uniform_sphere(u1, u2)*r;
203                 float3 p2 = sd->P + sample_uniform_sphere(u3, u4)*r;
204
205                 /* create ray */
206                 Ray ray;
207                 ray.P = p1;
208                 ray.D = normalize_len(p2 - p1, &ray.t);
209                 ray.dP = sd->dP;
210                 ray.dD = differential3_zero();
211                 ray.time = sd->time;
212
213                 /* intersect with the same object. if multiple intersections are
214                  * found it will randomly pick one of them */
215                 Intersection isect;
216                 if(!scene_intersect_subsurface(kg, &ray, &isect, sd->object, u6))
217                         continue;
218
219                 /* setup new shading point */
220                 shader_setup_from_subsurface(kg, sd, &isect, &ray);
221
222                 hit = true;
223                 num_attempts++;
224                 break;
225         }
226
227         /* evaluate subsurface scattering closures */
228 #ifdef BSSRDF_MULTI_EVAL
229         weight *= subsurface_scatter_multi_eval(kg, sd, hit, refl, r_attempts, num_attempts, all);
230 #else
231         weight *= sc->weight;
232 #endif
233
234 #ifdef BSSRDF_SKIP_NO_HIT
235         if(!hit)
236                 weight = make_float3(0.0f, 0.0f, 0.0f);
237 #endif
238
239         /* replace closures with a single diffuse BSDF */
240         subsurface_scatter_setup_diffuse_bsdf(sd, weight);
241 }
242
243 CCL_NAMESPACE_END
244