7dd5c2d447529a520ee98f3512142aec8bd64f28
[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         /* pick a random bsdf or bssrdf */
67         float r = sd->randb_closure*(bsdf_sum + bssrdf_sum);
68         float sum = 0.0f;
69
70         int sampled;
71
72         for(sampled = 0; sampled < sd->num_closure; sampled++) {
73                 ShaderClosure *sc = &sd->closure[sampled];
74                 
75                 if(CLOSURE_IS_BSDF(sc->type) || CLOSURE_IS_BSSRDF(sc->type)) {
76                         sum += sc->sample_weight;
77
78                         if(r <= sum) {
79                                 /* if we picked a bssrdf, return the closure. also return probability
80                                  * to adjust throughput depending if we picked a bsdf or bssrdf */
81                                 if(CLOSURE_IS_BSSRDF(sc->type)) {
82                                         sd->randb_closure = 0.0f; /* not needed anymore */
83 #ifdef BSSRDF_MULTI_EVAL
84                                         *probability = (bssrdf_sum > 0.0f)? (bsdf_sum + bssrdf_sum)/bssrdf_sum: 1.0f;
85 #else
86                                         *probability = (bssrdf_sum > 0.0f)? (bsdf_sum + bssrdf_sum)/sc->sample_weight: 1.0f;
87 #endif
88                                         return sc;
89                                 }
90                                 else {
91                                         /* reuse randb for picking a bsdf */
92                                         sd->randb_closure = (sd->randb_closure - sum - sc->sample_weight)/sc->sample_weight;
93                                         *probability = (bsdf_sum > 0.0f)? (bsdf_sum + bssrdf_sum)/bsdf_sum: 1.0f;
94                                         return NULL;
95                                 }
96                         }
97                 }
98         }
99
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)
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
112         for(int i = 0; i < sd->num_closure; i++) {
113                 ShaderClosure *sc = &sd->closure[i];
114                 
115                 if(CLOSURE_IS_BSSRDF(sc->type)) {
116                         float pdf = 1.0f;
117                         for(int i = 0; i < num_r; i++)
118                                 pdf *= bssrdf_pdf(kg, sc->data0, refl, r[i]);
119                         //float pdf = bssrdf_pdf(kg, sc->data0, refl, r[num_r-1]);
120
121                         eval_sum += sc->weight*pdf;
122                         pdf_sum += sc->sample_weight*pdf;
123
124                         sample_weight_sum += sc->sample_weight;
125                 }
126         }
127
128         float inv_pdf_sum = (pdf_sum > 0.0f)? sample_weight_sum/pdf_sum: 0.0f;
129         float3 weight = eval_sum * inv_pdf_sum;
130
131         return weight;
132 }
133 #endif
134
135 /* replace closures with a single diffuse bsdf closure after scatter step */
136 __device void subsurface_scatter_setup_diffuse_bsdf(ShaderData *sd, float3 weight)
137 {
138         ShaderClosure *sc = &sd->closure[0];
139         sd->num_closure = 1;
140
141         sc->weight = weight;
142         sc->sample_weight = 1.0f;
143         sc->data0 = 0.0f;
144         sc->data1 = 0.0f;
145         sc->N = sd->N;
146         sd->flag |= bsdf_diffuse_setup(sc);
147         sd->randb_closure = 0.0f;
148
149         /* todo: evaluate shading to get blurred textures and bump mapping */
150         /* shader_eval_surface(kg, sd, 0.0f, state_flag, SHADER_CONTEXT_SSS); */
151 }
152
153 /* subsurface scattering step, from a point on the surface to another nearby point on the same object */
154 __device void subsurface_scatter_step(KernelGlobals *kg, ShaderData *sd, int state_flag, ShaderClosure *sc, uint *lcg_state)
155 {
156         float radius = sc->data0;
157         float refl = max(average(sc->weight)*3.0f, 0.0f);
158         float r = 0.0f;
159         bool hit = false;
160         float3 weight = make_float3(1.0f, 1.0f, 1.0f);
161 #ifdef BSSRDF_MULTI_EVAL
162         float r_attempts[BSSRDF_MAX_ATTEMPTS];
163 #endif
164         int num_attempts;
165
166         /* attempt to find a hit a given number of times before giving up */
167         for(num_attempts = 0; num_attempts < kernel_data.bssrdf.num_attempts; num_attempts++) {
168                 /* random numbers for sampling */
169                 float u1 = lcg_step(lcg_state);
170                 float u2 = lcg_step(lcg_state);
171                 float u3 = lcg_step(lcg_state);
172                 float u4 = lcg_step(lcg_state);
173                 float u5 = lcg_step(lcg_state);
174                 float u6 = lcg_step(lcg_state);
175
176                 r = bssrdf_sample_distance(kg, radius, refl, u5);
177 #ifdef BSSRDF_MULTI_EVAL
178                 r_attempts[num_attempts] = r;
179 #endif
180
181                 float3 p1 = sd->P + sample_uniform_sphere(u1, u2)*r;
182                 float3 p2 = sd->P + sample_uniform_sphere(u3, u4)*r;
183
184                 /* create ray */
185                 Ray ray;
186                 ray.P = p1;
187                 ray.D = normalize_len(p2 - p1, &ray.t);
188                 ray.dP = sd->dP;
189                 ray.dD.dx = make_float3(0.0f, 0.0f, 0.0f);
190                 ray.dD.dy = make_float3(0.0f, 0.0f, 0.0f);
191                 ray.time = sd->time;
192
193                 /* intersect with the same object. if multiple intersections are
194                  * found it will randomly pick one of them */
195                 Intersection isect;
196                 if(scene_intersect_subsurface(kg, &ray, &isect, sd->object, u6) == 0)
197                         continue;
198
199                 /* setup new shading point */
200                 shader_setup_from_subsurface(kg, sd, &isect, &ray);
201
202                 hit = true;
203                 num_attempts++;
204                 break;
205         }
206
207         /* evaluate subsurface scattering closures */
208 #ifdef BSSRDF_MULTI_EVAL
209         weight *= subsurface_scatter_multi_eval(kg, sd, hit, refl, r_attempts, num_attempts);
210 #else
211         weight *= sc->weight;
212 #endif
213
214 #ifdef BSSRDF_SKIP_NO_HIT
215         if(!hit)
216                 weight = make_float3(0.0f, 0.0f, 0.0f);
217 #endif
218
219         /* replace closures with a single diffuse BSDF */
220         subsurface_scatter_setup_diffuse_bsdf(sd, weight);
221 }
222
223 CCL_NAMESPACE_END
224