79d0fb6ddbecc59b191e1210a2b0d55cd58014af
[blender.git] / intern / cycles / kernel / svm / svm_bevel.h
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 CCL_NAMESPACE_BEGIN
18
19 /* Bevel shader averaging normals from nearby surfaces.
20  *
21  * Sampling strategy from: BSSRDF Importance Sampling, SIGGRAPH 2013
22  * http://library.imageworks.com/pdfs/imageworks-library-BSSRDF-sampling.pdf
23  */
24
25 ccl_device_noinline float3 svm_bevel(
26         KernelGlobals *kg,
27         ShaderData *sd,
28         ccl_addr_space PathState *state,
29         float radius,
30         int num_samples)
31 {
32         /* Early out if no sampling needed. */
33         if(radius <= 0.0f || num_samples < 1 || sd->object == OBJECT_NONE) {
34                 return sd->N;
35         }
36
37         /* Don't bevel for blurry indirect rays. */
38         if(state->min_ray_pdf < 8.0f) {
39                 return sd->N;
40         }
41
42         /* Setup for multi intersection. */
43         LocalIntersection isect;
44         uint lcg_state = lcg_state_init_addrspace(state, 0x64c6a40e);
45
46         /* Sample normals from surrounding points on surface. */
47         float3 sum_N = make_float3(0.0f, 0.0f, 0.0f);
48
49         for(int sample = 0; sample < num_samples; sample++) {
50                 float disk_u, disk_v;
51                 path_branched_rng_2D(kg, state->rng_hash, state, sample, num_samples,
52                                      PRNG_BEVEL_U, &disk_u, &disk_v);
53
54                 /* Pick random axis in local frame and point on disk. */
55                 float3 disk_N, disk_T, disk_B;
56                 float pick_pdf_N, pick_pdf_T, pick_pdf_B;
57
58                 disk_N = sd->Ng;
59                 make_orthonormals(disk_N, &disk_T, &disk_B);
60
61                 float axisu = disk_u;
62
63                 if(axisu < 0.5f) {
64                         pick_pdf_N = 0.5f;
65                         pick_pdf_T = 0.25f;
66                         pick_pdf_B = 0.25f;
67                         disk_u *= 2.0f;
68                 }
69                 else if(axisu < 0.75f) {
70                         float3 tmp = disk_N;
71                         disk_N = disk_T;
72                         disk_T = tmp;
73                         pick_pdf_N = 0.25f;
74                         pick_pdf_T = 0.5f;
75                         pick_pdf_B = 0.25f;
76                         disk_u = (disk_u - 0.5f)*4.0f;
77                 }
78                 else {
79                         float3 tmp = disk_N;
80                         disk_N = disk_B;
81                         disk_B = tmp;
82                         pick_pdf_N = 0.25f;
83                         pick_pdf_T = 0.25f;
84                         pick_pdf_B = 0.5f;
85                         disk_u = (disk_u - 0.75f)*4.0f;
86                 }
87
88                 /* Sample point on disk. */
89                 float phi = M_2PI_F * disk_u;
90                 float disk_r = disk_v;
91                 float disk_height;
92
93                 /* Perhaps find something better than Cubic BSSRDF, but happens to work well. */
94                 bssrdf_cubic_sample(radius, 0.0f, disk_r, &disk_r, &disk_height);
95
96                 float3 disk_P = (disk_r*cosf(phi)) * disk_T + (disk_r*sinf(phi)) * disk_B;
97
98                 /* Create ray. */
99                 Ray *ray = &isect.ray;
100                 ray->P = sd->P + disk_N*disk_height + disk_P;
101                 ray->D = -disk_N;
102                 ray->t = 2.0f*disk_height;
103                 ray->dP = sd->dP;
104                 ray->dD = differential3_zero();
105                 ray->time = sd->time;
106
107                 /* Intersect with the same object. if multiple intersections are found it
108                  * will use at most LOCAL_MAX_HITS hits, a random subset of all hits. */
109                 scene_intersect_local(kg,
110                                       *ray,
111                                       &isect,
112                                       sd->object,
113                                       &lcg_state,
114                                       LOCAL_MAX_HITS);
115
116                 int num_eval_hits = min(isect.num_hits, LOCAL_MAX_HITS);
117
118                 for(int hit = 0; hit < num_eval_hits; hit++) {
119                         /* Quickly retrieve P and Ng without setting up ShaderData. */
120                         float3 hit_P;
121                         if(sd->type & PRIMITIVE_TRIANGLE) {
122                                 hit_P = triangle_refine_local(kg,
123                                                               sd,
124                                                               &isect.hits[hit],
125                                                               ray);
126                         }
127 #ifdef __OBJECT_MOTION__
128                         else  if(sd->type & PRIMITIVE_MOTION_TRIANGLE) {
129                                 float3 verts[3];
130                                 motion_triangle_vertices(
131                                         kg,
132                                         sd->object,
133                                         kernel_tex_fetch(__prim_index, isect.hits[hit].prim),
134                                         sd->time,
135                                         verts);
136                                 hit_P = motion_triangle_refine_local(kg,
137                                                                      sd,
138                                                                      &isect.hits[hit],
139                                                                      ray,
140                                                                      verts);
141                         }
142 #endif  /* __OBJECT_MOTION__ */
143
144                         float3 hit_Ng = isect.Ng[hit];
145
146                         /* Compute smooth normal. */
147                         float3 N = hit_Ng;
148                         int prim = kernel_tex_fetch(__prim_index, isect.hits[hit].prim);
149                         int shader = kernel_tex_fetch(__tri_shader, prim);
150
151                         if(shader & SHADER_SMOOTH_NORMAL) {
152                                 float u = isect.hits[hit].u;
153                                 float v = isect.hits[hit].v;
154
155                                 if(sd->type & PRIMITIVE_TRIANGLE) {
156                                         N = triangle_smooth_normal(kg, N, prim, u, v);
157                                 }
158 #ifdef __OBJECT_MOTION__
159                                 else if(sd->type & PRIMITIVE_MOTION_TRIANGLE) {
160                                         N = motion_triangle_smooth_normal(kg, N, sd->object, prim, u, v, sd->time);
161                                 }
162 #endif  /* __OBJECT_MOTION__ */
163                         }
164
165                         /* Transform normals to world space. */
166                         if(isect.hits[hit].object != OBJECT_NONE) {
167                                 object_normal_transform(kg, sd, &N);
168                                 object_normal_transform(kg, sd, &hit_Ng);
169                         }
170
171                         /* Probability densities for local frame axes. */
172                         float pdf_N = pick_pdf_N * fabsf(dot(disk_N, hit_Ng));
173                         float pdf_T = pick_pdf_T * fabsf(dot(disk_T, hit_Ng));
174                         float pdf_B = pick_pdf_B * fabsf(dot(disk_B, hit_Ng));
175
176                         /* Multiple importance sample between 3 axes, power heuristic
177                          * found to be slightly better than balance heuristic. pdf_N
178                          * in the MIS weight and denominator cancelled out. */
179                         float w = pdf_N / (sqr(pdf_N) + sqr(pdf_T) + sqr(pdf_B));
180                         if(isect.num_hits > LOCAL_MAX_HITS) {
181                                 w *= isect.num_hits/(float)LOCAL_MAX_HITS;
182                         }
183
184                         /* Real distance to sampled point. */
185                         float r = len(hit_P - sd->P);
186
187                         /* Compute weight. */
188                         float pdf = bssrdf_cubic_pdf(radius, 0.0f, r);
189                         float disk_pdf = bssrdf_cubic_pdf(radius, 0.0f, disk_r);
190
191                         w *= pdf / disk_pdf;
192
193                         /* Sum normal and weight. */
194                         sum_N += w * N;
195                 }
196         }
197
198         /* Normalize. */
199         float3 N = safe_normalize(sum_N);
200         return is_zero(N) ? sd->N : (sd->flag & SD_BACKFACING) ? -N : N;
201 }
202
203 ccl_device void svm_node_bevel(
204         KernelGlobals *kg,
205         ShaderData *sd,
206         ccl_addr_space PathState *state,
207         float *stack,
208         uint4 node)
209 {
210         uint num_samples, radius_offset, normal_offset, out_offset;
211         decode_node_uchar4(node.y, &num_samples, &radius_offset, &normal_offset, &out_offset);
212
213         float radius = stack_load_float(stack, radius_offset);
214         float3 bevel_N = svm_bevel(kg, sd, state, radius, num_samples);
215
216         if(stack_valid(normal_offset)) {
217                 /* Preserve input normal. */
218                 float3 ref_N = stack_load_float3(stack, normal_offset);
219                 bevel_N = normalize(ref_N + (bevel_N - sd->N));
220         }
221
222         stack_store_float3(stack, out_offset, bevel_N);
223 }
224
225 CCL_NAMESPACE_END