Cycles: small code cleanup + fix SSS closure mixed with other closures doing
[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 &= ~SD_CLOSURE_FLAGS;
147         sd->flag |= bsdf_diffuse_setup(sc);
148         sd->randb_closure = 0.0f;
149
150         /* todo: evaluate shading to get blurred textures and bump mapping */
151         /* shader_eval_surface(kg, sd, 0.0f, state_flag, SHADER_CONTEXT_SSS); */
152 }
153
154 /* subsurface scattering step, from a point on the surface to another nearby point on the same object */
155 __device void subsurface_scatter_step(KernelGlobals *kg, ShaderData *sd, int state_flag, ShaderClosure *sc, uint *lcg_state)
156 {
157         float radius = sc->data0;
158         float refl = max(average(sc->weight)*3.0f, 0.0f);
159         float r = 0.0f;
160         bool hit = false;
161         float3 weight = make_float3(1.0f, 1.0f, 1.0f);
162 #ifdef BSSRDF_MULTI_EVAL
163         float r_attempts[BSSRDF_MAX_ATTEMPTS];
164 #endif
165         int num_attempts;
166
167         /* attempt to find a hit a given number of times before giving up */
168         for(num_attempts = 0; num_attempts < kernel_data.bssrdf.num_attempts; num_attempts++) {
169                 /* random numbers for sampling */
170                 float u1 = lcg_step(lcg_state);
171                 float u2 = lcg_step(lcg_state);
172                 float u3 = lcg_step(lcg_state);
173                 float u4 = lcg_step(lcg_state);
174                 float u5 = lcg_step(lcg_state);
175                 float u6 = lcg_step(lcg_state);
176
177                 r = bssrdf_sample_distance(kg, radius, refl, u5);
178 #ifdef BSSRDF_MULTI_EVAL
179                 r_attempts[num_attempts] = r;
180 #endif
181
182                 float3 p1 = sd->P + sample_uniform_sphere(u1, u2)*r;
183                 float3 p2 = sd->P + sample_uniform_sphere(u3, u4)*r;
184
185                 /* create ray */
186                 Ray ray;
187                 ray.P = p1;
188                 ray.D = normalize_len(p2 - p1, &ray.t);
189                 ray.dP = sd->dP;
190                 ray.dD.dx = make_float3(0.0f, 0.0f, 0.0f);
191                 ray.dD.dy = make_float3(0.0f, 0.0f, 0.0f);
192                 ray.time = sd->time;
193
194                 /* intersect with the same object. if multiple intersections are
195                  * found it will randomly pick one of them */
196                 Intersection isect;
197                 if(scene_intersect_subsurface(kg, &ray, &isect, sd->object, u6) == 0)
198                         continue;
199
200                 /* setup new shading point */
201                 shader_setup_from_subsurface(kg, sd, &isect, &ray);
202
203                 hit = true;
204                 num_attempts++;
205                 break;
206         }
207
208         /* evaluate subsurface scattering closures */
209 #ifdef BSSRDF_MULTI_EVAL
210         weight *= subsurface_scatter_multi_eval(kg, sd, hit, refl, r_attempts, num_attempts);
211 #else
212         weight *= sc->weight;
213 #endif
214
215 #ifdef BSSRDF_SKIP_NO_HIT
216         if(!hit)
217                 weight = make_float3(0.0f, 0.0f, 0.0f);
218 #endif
219
220         /* replace closures with a single diffuse BSDF */
221         subsurface_scatter_setup_diffuse_bsdf(sd, weight);
222 }
223
224 CCL_NAMESPACE_END
225