Cycles: Cleanup, reduce indentation level
[blender.git] / intern / cycles / kernel / geom / geom_triangle_intersect.h
1                         /*
2  * Copyright 2014, 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 /* Triangle/Ray intersections.
18  *
19  * For BVH ray intersection we use a precomputed triangle storage to accelerate
20  * intersection at the cost of more memory usage.
21  */
22
23 CCL_NAMESPACE_BEGIN
24
25 ccl_device_inline bool triangle_intersect(KernelGlobals *kg,
26                                           Intersection *isect,
27                                           float3 P,
28                                           float3 dir,
29                                           uint visibility,
30                                           int object,
31                                           int prim_addr)
32 {
33         const uint tri_vindex = kernel_tex_fetch(__prim_tri_index, prim_addr);
34 #if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
35         const ssef *ssef_verts = (ssef*)&kg->__prim_tri_verts.data[tri_vindex];
36 #else
37         const float4 tri_a = kernel_tex_fetch(__prim_tri_verts, tri_vindex+0),
38                      tri_b = kernel_tex_fetch(__prim_tri_verts, tri_vindex+1),
39                      tri_c = kernel_tex_fetch(__prim_tri_verts, tri_vindex+2);
40 #endif
41         float t, u, v;
42         if(ray_triangle_intersect(P,
43                                   dir,
44                                   isect->t,
45 #if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
46                                   ssef_verts,
47 #else
48                                   float4_to_float3(tri_a),
49                                   float4_to_float3(tri_b),
50                                   float4_to_float3(tri_c),
51 #endif
52                                   &u, &v, &t))
53         {
54 #ifdef __VISIBILITY_FLAG__
55                 /* Visibility flag test. we do it here under the assumption
56                  * that most triangles are culled by node flags.
57                  */
58                 if(kernel_tex_fetch(__prim_visibility, prim_addr) & visibility)
59 #endif
60                 {
61                         isect->prim = prim_addr;
62                         isect->object = object;
63                         isect->type = PRIMITIVE_TRIANGLE;
64                         isect->u = u;
65                         isect->v = v;
66                         isect->t = t;
67                         return true;
68                 }
69         }
70         return false;
71 }
72
73 #ifdef __KERNEL_AVX2__
74 #define cross256(A,B, C,D) _mm256_fmsub_ps(A,B, _mm256_mul_ps(C,D))
75 ccl_device_inline int ray_triangle_intersect8(
76             KernelGlobals *kg,
77             float3 ray_P,
78             float3 ray_dir,
79             Intersection **isect,
80             uint visibility,
81             int object,
82             __m256 *triA,
83             __m256 *triB,
84             __m256 *triC,
85             int prim_addr,
86             int prim_num,
87             uint *num_hits,
88             uint max_hits,
89             int *num_hits_in_instance,
90             float isect_t)
91 {
92
93         const unsigned char prim_num_mask = (1 << prim_num) - 1;
94
95         const __m256i zero256 = _mm256_setzero_si256();
96
97         const __m256 Px256 = _mm256_set1_ps(ray_P.x);
98         const __m256 Py256 = _mm256_set1_ps(ray_P.y);
99         const __m256 Pz256 = _mm256_set1_ps(ray_P.z);
100
101         const __m256 dirx256 = _mm256_set1_ps(ray_dir.x);
102         const __m256 diry256 = _mm256_set1_ps(ray_dir.y);
103         const __m256 dirz256 = _mm256_set1_ps(ray_dir.z);
104
105         /* Calculate vertices relative to ray origin. */
106         __m256 v0_x_256 = _mm256_sub_ps(triC[0], Px256);
107         __m256 v0_y_256 = _mm256_sub_ps(triC[1], Py256);
108         __m256 v0_z_256 = _mm256_sub_ps(triC[2], Pz256);
109
110         __m256 v1_x_256 = _mm256_sub_ps(triA[0], Px256);
111         __m256 v1_y_256 = _mm256_sub_ps(triA[1], Py256);
112         __m256 v1_z_256 = _mm256_sub_ps(triA[2], Pz256);
113
114         __m256 v2_x_256 = _mm256_sub_ps(triB[0], Px256);
115         __m256 v2_y_256 = _mm256_sub_ps(triB[1], Py256);
116         __m256 v2_z_256 = _mm256_sub_ps(triB[2], Pz256);
117
118         __m256 v0_v1_x_256 = _mm256_add_ps(v0_x_256, v1_x_256);
119         __m256 v0_v1_y_256 = _mm256_add_ps(v0_y_256, v1_y_256);
120         __m256 v0_v1_z_256 = _mm256_add_ps(v0_z_256, v1_z_256);
121
122         __m256 v0_v2_x_256 = _mm256_add_ps(v0_x_256, v2_x_256);
123         __m256 v0_v2_y_256 = _mm256_add_ps(v0_y_256, v2_y_256);
124         __m256 v0_v2_z_256 = _mm256_add_ps(v0_z_256, v2_z_256);
125
126         __m256 v1_v2_x_256 = _mm256_add_ps(v1_x_256, v2_x_256);
127         __m256 v1_v2_y_256 = _mm256_add_ps(v1_y_256, v2_y_256);
128         __m256 v1_v2_z_256 = _mm256_add_ps(v1_z_256, v2_z_256);
129
130         /* Calculate triangle edges. */
131         __m256 e0_x_256 = _mm256_sub_ps(v2_x_256, v0_x_256);
132         __m256 e0_y_256 = _mm256_sub_ps(v2_y_256, v0_y_256);
133         __m256 e0_z_256 = _mm256_sub_ps(v2_z_256, v0_z_256);
134
135         __m256 e1_x_256 = _mm256_sub_ps(v0_x_256, v1_x_256);
136         __m256 e1_y_256 = _mm256_sub_ps(v0_y_256, v1_y_256);
137         __m256 e1_z_256 = _mm256_sub_ps(v0_z_256, v1_z_256);
138
139         __m256 e2_x_256 = _mm256_sub_ps(v1_x_256, v2_x_256);
140         __m256 e2_y_256 = _mm256_sub_ps(v1_y_256, v2_y_256);
141         __m256 e2_z_256 = _mm256_sub_ps(v1_z_256, v2_z_256);
142
143         /* Perform edge tests. */
144         /* cross (AyBz - AzBy, AzBx -AxBz,  AxBy - AyBx) */
145         __m256 U_x_256 = cross256(v0_v2_y_256, e0_z_256, v0_v2_z_256, e0_y_256);
146         __m256 U_y_256 = cross256(v0_v2_z_256, e0_x_256, v0_v2_x_256, e0_z_256);
147         __m256 U_z_256 = cross256(v0_v2_x_256, e0_y_256, v0_v2_y_256, e0_x_256);
148         /* vertical dot */
149         __m256 U_256 = _mm256_mul_ps(U_x_256, dirx256);
150         U_256 = _mm256_fmadd_ps(U_y_256, diry256, U_256);
151         U_256 = _mm256_fmadd_ps(U_z_256, dirz256, U_256);
152
153         __m256 V_x_256 = cross256(v0_v1_y_256, e1_z_256, v0_v1_z_256, e1_y_256);
154         __m256 V_y_256 = cross256(v0_v1_z_256, e1_x_256, v0_v1_x_256, e1_z_256);
155         __m256 V_z_256 = cross256(v0_v1_x_256, e1_y_256, v0_v1_y_256, e1_x_256);
156         /* vertical dot */
157         __m256 V_256 = _mm256_mul_ps(V_x_256, dirx256);
158         V_256 = _mm256_fmadd_ps(V_y_256, diry256, V_256);
159         V_256 = _mm256_fmadd_ps(V_z_256, dirz256, V_256);
160
161         __m256 W_x_256 = cross256(v1_v2_y_256, e2_z_256, v1_v2_z_256, e2_y_256);
162         __m256 W_y_256 = cross256(v1_v2_z_256, e2_x_256, v1_v2_x_256, e2_z_256);
163         __m256 W_z_256 = cross256(v1_v2_x_256, e2_y_256, v1_v2_y_256, e2_x_256);
164         /* vertical dot */
165         __m256 W_256 = _mm256_mul_ps(W_x_256, dirx256);
166         W_256 = _mm256_fmadd_ps(W_y_256, diry256,W_256);
167         W_256 = _mm256_fmadd_ps(W_z_256, dirz256,W_256);
168
169         __m256i U_256_1 = _mm256_srli_epi32(_mm256_castps_si256(U_256), 31);
170         __m256i V_256_1 = _mm256_srli_epi32(_mm256_castps_si256(V_256), 31);
171         __m256i W_256_1 = _mm256_srli_epi32(_mm256_castps_si256(W_256), 31);
172         __m256i UVW_256_1 = _mm256_add_epi32(_mm256_add_epi32(U_256_1, V_256_1), W_256_1);
173
174         const __m256i one256 = _mm256_set1_epi32(1);
175         const __m256i two256 = _mm256_set1_epi32(2);
176
177         __m256i mask_minmaxUVW_256 = _mm256_or_si256(
178                 _mm256_cmpeq_epi32(one256, UVW_256_1),
179                 _mm256_cmpeq_epi32(two256, UVW_256_1));
180
181         unsigned char mask_minmaxUVW_pos = _mm256_movemask_ps(_mm256_castsi256_ps(mask_minmaxUVW_256));
182         if((mask_minmaxUVW_pos & prim_num_mask) == prim_num_mask) { //all bits set
183                 return false;
184         }
185
186         /* Calculate geometry normal and denominator. */
187         __m256 Ng1_x_256 = cross256(e1_y_256, e0_z_256, e1_z_256, e0_y_256);
188         __m256 Ng1_y_256 = cross256(e1_z_256, e0_x_256, e1_x_256, e0_z_256);
189         __m256 Ng1_z_256 = cross256(e1_x_256, e0_y_256, e1_y_256, e0_x_256);
190
191         Ng1_x_256 = _mm256_add_ps(Ng1_x_256, Ng1_x_256);
192         Ng1_y_256 = _mm256_add_ps(Ng1_y_256, Ng1_y_256);
193         Ng1_z_256 = _mm256_add_ps(Ng1_z_256, Ng1_z_256);
194
195         /* vertical dot */
196         __m256 den_256 = _mm256_mul_ps(Ng1_x_256, dirx256);
197         den_256 = _mm256_fmadd_ps(Ng1_y_256, diry256,den_256);
198         den_256 = _mm256_fmadd_ps(Ng1_z_256, dirz256,den_256);
199
200         /* Perform depth test. */
201         __m256 T_256 = _mm256_mul_ps(Ng1_x_256, v0_x_256);
202         T_256 = _mm256_fmadd_ps(Ng1_y_256, v0_y_256,T_256);
203         T_256 = _mm256_fmadd_ps(Ng1_z_256, v0_z_256,T_256);
204
205         const __m256i c0x80000000 = _mm256_set1_epi32(0x80000000);
206         __m256i sign_den_256 = _mm256_and_si256(_mm256_castps_si256(den_256), c0x80000000);
207
208         __m256 sign_T_256 = _mm256_castsi256_ps(_mm256_xor_si256(_mm256_castps_si256(T_256), sign_den_256));
209
210         unsigned char mask_sign_T = _mm256_movemask_ps(sign_T_256);
211         if(((mask_minmaxUVW_pos | mask_sign_T) & prim_num_mask) == prim_num_mask) {
212                 return false;
213         } 
214
215         __m256 xor_signmask_256 = _mm256_castsi256_ps(_mm256_xor_si256(_mm256_castps_si256(den_256), sign_den_256));
216
217         ccl_align(32) float den8[8], U8[8], V8[8], T8[8], sign_T8[8], xor_signmask8[8];
218         ccl_align(32) unsigned int mask_minmaxUVW8[8];
219
220         if(visibility == PATH_RAY_SHADOW_OPAQUE) {
221                 __m256i mask_final_256 = _mm256_cmpeq_epi32(mask_minmaxUVW_256, zero256);
222                 __m256i maskden256 = _mm256_cmpeq_epi32(_mm256_castps_si256(den_256), zero256);
223                 __m256i mask0 = _mm256_cmpgt_epi32(zero256, _mm256_castps_si256(sign_T_256));
224                 __m256 rayt_256 = _mm256_set1_ps((*isect)->t);
225                 __m256i mask1 = _mm256_cmpgt_epi32(_mm256_castps_si256(sign_T_256),
226                         _mm256_castps_si256(
227                                 _mm256_mul_ps(_mm256_castsi256_ps(_mm256_xor_si256(_mm256_castps_si256(den_256), sign_den_256)), rayt_256)
228                         )
229                 );
230                 mask0 = _mm256_or_si256(mask1, mask0);
231                 mask_final_256 = _mm256_andnot_si256(mask0, mask_final_256); //(~mask_minmaxUVW_pos) &(~mask)
232                 mask_final_256 = _mm256_andnot_si256(maskden256, mask_final_256); //(~mask_minmaxUVW_pos) &(~mask) & (~maskden)
233                 unsigned char mask_final = _mm256_movemask_ps(_mm256_castsi256_ps(mask_final_256));
234                 if((mask_final & prim_num_mask) == 0) {
235                         return false;
236                 }
237                 unsigned long i = 0;
238 #if defined(_MSC_VER)
239                 unsigned char res = _BitScanForward(&i, (unsigned long)mask_final);
240 #else
241                 i = __builtin_ffs(mask_final)-1;
242 #endif
243                 __m256 inv_den_256 = _mm256_rcp_ps(den_256);
244                 U_256 = _mm256_mul_ps(U_256, inv_den_256);
245                 V_256 = _mm256_mul_ps(V_256, inv_den_256);
246                 T_256 = _mm256_mul_ps(T_256, inv_den_256);
247                 _mm256_store_ps(U8, U_256);
248                 _mm256_store_ps(V8, V_256);
249                 _mm256_store_ps(T8, T_256);
250                 /* NOTE: Here we assume visibility for all triangles in the node is
251                  * the same. */
252                 (*isect)->u = U8[i];
253                 (*isect)->v = V8[i];
254                 (*isect)->t = T8[i];
255                 (*isect)->prim = (prim_addr + i);
256                 (*isect)->object = object;
257                 (*isect)->type = PRIMITIVE_TRIANGLE;
258                 return true;
259         }
260         else {
261                 _mm256_store_ps(den8, den_256);
262                 _mm256_store_ps(U8, U_256);
263                 _mm256_store_ps(V8, V_256);
264                 _mm256_store_ps(T8, T_256);
265
266                 _mm256_store_ps(sign_T8, sign_T_256);
267                 _mm256_store_ps(xor_signmask8, xor_signmask_256);
268                 _mm256_store_si256((__m256i*)mask_minmaxUVW8, mask_minmaxUVW_256);
269
270                 int ret = false;
271
272                 if(visibility == PATH_RAY_SHADOW) {
273                         for(int i = 0; i < prim_num; i++) {
274                                 if(mask_minmaxUVW8[i]) {
275                                         continue;
276                                 }
277 #ifdef __VISIBILITY_FLAG__
278                                 if((kernel_tex_fetch(__prim_visibility, (prim_addr + i)) & visibility) == 0) {
279                                         continue;
280                                 }
281 #endif
282                                 if((sign_T8[i] < 0.0f) ||
283                                    (sign_T8[i] > (*isect)->t * xor_signmask8[i]))
284                                 {
285                                         continue;
286                                 }
287                                 if(!den8[i]) {
288                                         continue;
289                                 }
290                                 const float inv_den = 1.0f / den8[i];
291                                 (*isect)->u = U8[i] * inv_den;
292                                 (*isect)->v = V8[i] * inv_den;
293                                 (*isect)->t = T8[i] * inv_den;
294                                 (*isect)->prim = (prim_addr + i);
295                                 (*isect)->object = object;
296                                 (*isect)->type = PRIMITIVE_TRIANGLE;
297                                 const int prim = kernel_tex_fetch(__prim_index, (*isect)->prim);
298                                 int shader = 0;
299 #ifdef __HAIR__
300                                 if(kernel_tex_fetch(__prim_type, (*isect)->prim) & PRIMITIVE_ALL_TRIANGLE)
301 #endif
302                                 {
303                                         shader = kernel_tex_fetch(__tri_shader, prim);
304                                 }
305 #ifdef __HAIR__
306                                 else {
307                                         float4 str = kernel_tex_fetch(__curves, prim);
308                                         shader = __float_as_int(str.z);
309                                 }
310 #endif
311                                 const int flag = kernel_tex_fetch(__shaders, (shader & SHADER_MASK)).flags;
312                                 /* If no transparent shadows, all light is blocked. */
313                                 if(!(flag & SD_HAS_TRANSPARENT_SHADOW)) {
314                                         return 2;
315                                 }
316                                 /* If maximum number of hits reached, block all light. */
317                                 else if(*num_hits == max_hits) {
318                                         return 2;
319                                 }
320                                 /* Move on to next entry in intersections array. */
321                                 ret = true;
322                                 (*isect)++;
323                                 (*num_hits)++;
324                                 (*num_hits_in_instance)++;
325                                 (*isect)->t = isect_t;
326                         }
327                 }
328                 else {
329                         for(int i = 0; i < prim_num; i++) {
330                                 if(mask_minmaxUVW8[i]) {
331                                         continue;
332                                 }
333 #ifdef __VISIBILITY_FLAG__
334                                 if((kernel_tex_fetch(__prim_visibility, (prim_addr + i)) & visibility) == 0) {
335                                         continue;
336                                 }
337 #endif
338                                 if((sign_T8[i] < 0.0f) ||
339                                    (sign_T8[i] > (*isect)->t * xor_signmask8[i]))
340                                 {
341                                         continue;
342                                 }
343                                 if(!den8[i]) {
344                                         continue;
345                                 }
346                                 const float inv_den = 1.0f / den8[i];
347                                 (*isect)->u = U8[i] * inv_den;
348                                 (*isect)->v = V8[i] * inv_den;
349                                 (*isect)->t = T8[i] * inv_den;
350                                 (*isect)->prim = (prim_addr + i);
351                                 (*isect)->object = object;
352                                 (*isect)->type = PRIMITIVE_TRIANGLE;
353                                 ret = true;
354                         }
355                 }
356                 return ret;
357         }
358 }
359
360 ccl_device_inline int triangle_intersect8(
361         KernelGlobals *kg,
362         Intersection **isect,
363         float3 P,
364         float3 dir,
365         uint visibility,
366         int object,
367         int prim_addr,
368         int prim_num,
369         uint *num_hits,
370         uint max_hits,
371         int *num_hits_in_instance,
372         float isect_t)
373  {
374         __m128 tri_a[8], tri_b[8], tri_c[8];
375         __m256  tritmp[12], tri[12];
376         __m256  triA[3], triB[3], triC[3];
377
378         int i, r;
379
380         uint tri_vindex = kernel_tex_fetch(__prim_tri_index, prim_addr);
381         for(i = 0; i < prim_num; i++) {
382                 tri_a[i] = *(__m128*)&kg->__prim_tri_verts.data[tri_vindex++];
383                 tri_b[i] = *(__m128*)&kg->__prim_tri_verts.data[tri_vindex++];
384                 tri_c[i] = *(__m128*)&kg->__prim_tri_verts.data[tri_vindex++];
385          }
386         //create 9 or  12 placeholders
387         tri[0] = _mm256_castps128_ps256(tri_a[0]);    //_mm256_zextps128_ps256
388         tri[1] = _mm256_castps128_ps256(tri_b[0]);//_mm256_zextps128_ps256
389         tri[2] = _mm256_castps128_ps256(tri_c[0]);//_mm256_zextps128_ps256
390
391         tri[3] = _mm256_castps128_ps256(tri_a[1]);    //_mm256_zextps128_ps256
392         tri[4] = _mm256_castps128_ps256(tri_b[1]);//_mm256_zextps128_ps256
393         tri[5] = _mm256_castps128_ps256(tri_c[1]);//_mm256_zextps128_ps256
394
395         tri[6] = _mm256_castps128_ps256(tri_a[2]);    //_mm256_zextps128_ps256
396         tri[7] = _mm256_castps128_ps256(tri_b[2]);//_mm256_zextps128_ps256
397         tri[8] = _mm256_castps128_ps256(tri_c[2]);//_mm256_zextps128_ps256
398
399         if(prim_num > 3) {
400                 tri[9] =  _mm256_castps128_ps256(tri_a[3]);    //_mm256_zextps128_ps256
401                 tri[10] = _mm256_castps128_ps256(tri_b[3]);//_mm256_zextps128_ps256
402                 tri[11] = _mm256_castps128_ps256(tri_c[3]);//_mm256_zextps128_ps256
403         }
404
405         for(i = 4, r = 0; i < prim_num; i ++, r += 3) {
406                 tri[r] =     _mm256_insertf128_ps(tri[r] , tri_a[i], 1);
407                 tri[r + 1] = _mm256_insertf128_ps(tri[r + 1], tri_b[i], 1);
408                 tri[r + 2] = _mm256_insertf128_ps(tri[r + 2], tri_c[i], 1);
409          }
410
411         //------------------------------------------------
412         //0!  Xa0 Ya0 Za0 1 Xa4 Ya4 Za4  1
413         //1!  Xb0 Yb0 Zb0 1 Xb4 Yb4 Zb4 1
414         //2!  Xc0 Yc0 Zc0 1 Xc4 Yc4 Zc4 1
415
416         //3!  Xa1 Ya1 Za1 1 Xa5 Ya5 Za5 1
417         //4!  Xb1 Yb1 Zb1 1 Xb5 Yb5 Zb5  1
418         //5!  Xc1 Yc1 Zc1 1 Xc5 Yc5 Zc5 1
419
420         //6!  Xa2 Ya2 Za2 1 Xa6 Ya6 Za6 1
421         //7!  Xb2 Yb2 Zb2 1 Xb6 Yb6 Zb6  1
422         //8!  Xc2 Yc2 Zc2 1 Xc6 Yc6 Zc6 1
423
424         //9!  Xa3 Ya3 Za3 1 Xa7 Ya7 Za7  1
425         //10! Xb3 Yb3 Zb3 1 Xb7 Yb7 Zb7  1
426         //11! Xc3 Yc3 Zc3 1 Xc7 Yc7 Zc7  1
427
428         //"transpose"
429         tritmp[0] = _mm256_unpacklo_ps(tri[0], tri[3]);   //0!  Xa0 Xa1 Ya0 Ya1 Xa4 Xa5 Ya4 Ya5
430         tritmp[1] = _mm256_unpackhi_ps(tri[0], tri[3]);   //1!  Za0 Za1 1   1   Za4 Za5  1   1
431
432         tritmp[2] = _mm256_unpacklo_ps(tri[6], tri[9]);   //2!  Xa2 Xa3 Ya2 Ya3 Xa6 Xa7 Ya6 Ya7
433         tritmp[3] = _mm256_unpackhi_ps(tri[6], tri[9]);   //3!  Za2 Za3  1   1  Za6 Za7  1   1
434
435         tritmp[4] = _mm256_unpacklo_ps(tri[1], tri[4]);   //4!  Xb0 Xb1 Yb0 Yb1 Xb4 Xb5 Yb4 Yb5
436         tritmp[5] = _mm256_unpackhi_ps(tri[1], tri[4]);   //5!  Zb0 Zb1  1  1   Zb4 Zb5  1   1
437
438         tritmp[6] = _mm256_unpacklo_ps(tri[7], tri[10]);  //6!  Xb2 Xb3 Yb2 Yb3 Xb6 Xb7 Yb6 Yb7
439         tritmp[7] = _mm256_unpackhi_ps(tri[7], tri[10]);  //7!  Zb2 Zb3  1    1 Zb6 Zb7  1   1
440
441         tritmp[8] = _mm256_unpacklo_ps(tri[2], tri[5]);   //8!  Xc0 Xc1 Yc0 Yc1 Xc4 Xc5 Yc4 Yc5
442         tritmp[9] = _mm256_unpackhi_ps(tri[2], tri[5]);   //9!  Zc0 Zc1  1   1  Zc4 Zc5  1   1
443
444         tritmp[10] = _mm256_unpacklo_ps(tri[8], tri[11]); //10! Xc2 Xc3 Yc2 Yc3 Xc6 Xc7 Yc6 Yc7
445         tritmp[11] = _mm256_unpackhi_ps(tri[8], tri[11]); //11! Zc2 Zc3  1   1  Zc6 Zc7  1   1
446
447                                 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
448         triA[0] = _mm256_castpd_ps(_mm256_unpacklo_pd(_mm256_castps_pd(tritmp[0]), _mm256_castps_pd(tritmp[2])));       //  Xa0 Xa1 Xa2 Xa3 Xa4 Xa5 Xa6 Xa7
449         triA[1] = _mm256_castpd_ps(_mm256_unpackhi_pd(_mm256_castps_pd(tritmp[0]), _mm256_castps_pd(tritmp[2])));   //  Ya0 Ya1 Ya2 Ya3 Ya4 Ya5 Ya6 Ya7
450         triA[2] = _mm256_castpd_ps(_mm256_unpacklo_pd(_mm256_castps_pd(tritmp[1]), _mm256_castps_pd(tritmp[3])));   //  Za0 Za1 Za2 Za3 Za4 Za5 Za6 Za7
451
452         triB[0] = _mm256_castpd_ps(_mm256_unpacklo_pd(_mm256_castps_pd(tritmp[4]), _mm256_castps_pd(tritmp[6])));       //  Xb0 Xb1  Xb2 Xb3 Xb4 Xb5 Xb5 Xb7
453         triB[1] = _mm256_castpd_ps(_mm256_unpackhi_pd(_mm256_castps_pd(tritmp[4]), _mm256_castps_pd(tritmp[6])));   //  Yb0 Yb1  Yb2 Yb3 Yb4 Yb5 Yb5 Yb7
454         triB[2] = _mm256_castpd_ps(_mm256_unpacklo_pd(_mm256_castps_pd(tritmp[5]), _mm256_castps_pd(tritmp[7]))); //    Zb0 Zb1  Zb2 Zb3 Zb4 Zb5 Zb5 Zb7
455
456         triC[0] = _mm256_castpd_ps(_mm256_unpacklo_pd(_mm256_castps_pd(tritmp[8]), _mm256_castps_pd(tritmp[10])));     //Xc0 Xc1 Xc2 Xc3 Xc4 Xc5 Xc6 Xc7
457         triC[1] = _mm256_castpd_ps(_mm256_unpackhi_pd(_mm256_castps_pd(tritmp[8]), _mm256_castps_pd(tritmp[10])));     //Yc0 Yc1 Yc2 Yc3 Yc4 Yc5 Yc6 Yc7
458         triC[2] = _mm256_castpd_ps(_mm256_unpacklo_pd(_mm256_castps_pd(tritmp[9]), _mm256_castps_pd(tritmp[11])));     //Zc0 Zc1 Zc2 Zc3 Zc4 Zc5 Zc6 Zc7
459
460                           /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
461
462         int result = ray_triangle_intersect8(kg, P,
463                                              dir,
464                                              isect,
465                                              visibility, object,
466                                              triA,
467                                              triB,
468                                              triC,
469                                              prim_addr,
470                                              prim_num,
471                                              num_hits,
472                                              max_hits,
473                                              num_hits_in_instance,
474                                              isect_t);
475         return result;
476 }
477
478 #endif  /* __KERNEL_AVX2__ */
479
480 /* Special ray intersection routines for subsurface scattering. In that case we
481  * only want to intersect with primitives in the same object, and if case of
482  * multiple hits we pick a single random primitive as the intersection point.
483  * Returns whether traversal should be stopped.
484  */
485
486 #ifdef __BVH_LOCAL__
487 ccl_device_inline bool triangle_intersect_local(
488         KernelGlobals *kg,
489         LocalIntersection *local_isect,
490         float3 P,
491         float3 dir,
492         int object,
493         int local_object,
494         int prim_addr,
495         float tmax,
496         uint *lcg_state,
497         int max_hits)
498 {
499         /* Only intersect with matching object, for instanced objects we
500          * already know we are only intersecting the right object. */
501         if(object == OBJECT_NONE) {
502                 if(kernel_tex_fetch(__prim_object, prim_addr) != local_object) {
503                         return false;
504                 }
505         }
506
507         const uint tri_vindex = kernel_tex_fetch(__prim_tri_index, prim_addr);
508 #if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
509         const ssef *ssef_verts = (ssef*)&kg->__prim_tri_verts.data[tri_vindex];
510 #else
511         const float3 tri_a = float4_to_float3(kernel_tex_fetch(__prim_tri_verts, tri_vindex+0)),
512                      tri_b = float4_to_float3(kernel_tex_fetch(__prim_tri_verts, tri_vindex+1)),
513                      tri_c = float4_to_float3(kernel_tex_fetch(__prim_tri_verts, tri_vindex+2));
514 #endif
515         float t, u, v;
516         if(!ray_triangle_intersect(P,
517                                    dir,
518                                    tmax,
519 #if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
520                                    ssef_verts,
521 #else
522                                    tri_a, tri_b, tri_c,
523 #endif
524                                    &u, &v, &t))
525         {
526                 return false;
527         }
528
529         /* If no actual hit information is requested, just return here. */
530         if(max_hits == 0) {
531                 return true;
532         }
533
534         int hit;
535         if(lcg_state) {
536                 /* Record up to max_hits intersections. */
537                 for(int i = min(max_hits, local_isect->num_hits) - 1; i >= 0; --i) {
538                         if(local_isect->hits[i].t == t) {
539                                 return false;
540                         }
541                 }
542
543                 local_isect->num_hits++;
544
545                 if(local_isect->num_hits <= max_hits) {
546                         hit = local_isect->num_hits - 1;
547                 }
548                 else {
549                         /* reservoir sampling: if we are at the maximum number of
550                          * hits, randomly replace element or skip it */
551                         hit = lcg_step_uint(lcg_state) % local_isect->num_hits;
552
553                         if(hit >= max_hits)
554                                 return false;
555                 }
556         }
557         else {
558                 /* Record closest intersection only. */
559                 if(local_isect->num_hits && t > local_isect->hits[0].t) {
560                         return false;
561                 }
562
563                 hit = 0;
564                 local_isect->num_hits = 1;
565         }
566
567         /* Record intersection. */
568         Intersection *isect = &local_isect->hits[hit];
569         isect->prim = prim_addr;
570         isect->object = object;
571         isect->type = PRIMITIVE_TRIANGLE;
572         isect->u = u;
573         isect->v = v;
574         isect->t = t;
575
576         /* Record geometric normal. */
577 #if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
578         const float3 tri_a = float4_to_float3(kernel_tex_fetch(__prim_tri_verts, tri_vindex+0)),
579                      tri_b = float4_to_float3(kernel_tex_fetch(__prim_tri_verts, tri_vindex+1)),
580                      tri_c = float4_to_float3(kernel_tex_fetch(__prim_tri_verts, tri_vindex+2));
581 #endif
582         local_isect->Ng[hit] = normalize(cross(tri_b - tri_a, tri_c - tri_a));
583
584         return false;
585 }
586 #endif  /* __BVH_LOCAL__ */
587
588 /* Refine triangle intersection to more precise hit point. For rays that travel
589  * far the precision is often not so good, this reintersects the primitive from
590  * a closer distance. */
591
592 /* Reintersections uses the paper:
593  *
594  * Tomas Moeller
595  * Fast, minimum storage ray/triangle intersection
596  * http://www.cs.virginia.edu/~gfx/Courses/2003/ImageSynthesis/papers/Acceleration/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf
597  */
598
599 ccl_device_inline float3 triangle_refine(KernelGlobals *kg,
600                                          ShaderData *sd,
601                                          const Intersection *isect,
602                                          const Ray *ray)
603 {
604         float3 P = ray->P;
605         float3 D = ray->D;
606         float t = isect->t;
607
608 #ifdef __INTERSECTION_REFINE__
609         if(isect->object != OBJECT_NONE) {
610                 if(UNLIKELY(t == 0.0f)) {
611                         return P;
612                 }
613 #  ifdef __OBJECT_MOTION__
614                 Transform tfm = sd->ob_itfm;
615 #  else
616                 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_INVERSE_TRANSFORM);
617 #  endif
618
619                 P = transform_point(&tfm, P);
620                 D = transform_direction(&tfm, D*t);
621                 D = normalize_len(D, &t);
622         }
623
624         P = P + D*t;
625
626         const uint tri_vindex = kernel_tex_fetch(__prim_tri_index, isect->prim);
627         const float4 tri_a = kernel_tex_fetch(__prim_tri_verts, tri_vindex+0),
628                      tri_b = kernel_tex_fetch(__prim_tri_verts, tri_vindex+1),
629                      tri_c = kernel_tex_fetch(__prim_tri_verts, tri_vindex+2);
630         float3 edge1 = make_float3(tri_a.x - tri_c.x, tri_a.y - tri_c.y, tri_a.z - tri_c.z);
631         float3 edge2 = make_float3(tri_b.x - tri_c.x, tri_b.y - tri_c.y, tri_b.z - tri_c.z);
632         float3 tvec = make_float3(P.x - tri_c.x, P.y - tri_c.y, P.z - tri_c.z);
633         float3 qvec = cross(tvec, edge1);
634         float3 pvec = cross(D, edge2);
635         float det = dot(edge1, pvec);
636         if(det != 0.0f) {
637                 /* If determinant is zero it means ray lies in the plane of
638                  * the triangle. It is possible in theory due to watertight
639                  * nature of triangle intersection. For such cases we simply
640                  * don't refine intersection hoping it'll go all fine.
641                  */
642                 float rt = dot(edge2, qvec) / det;
643                 P = P + D*rt;
644         }
645
646         if(isect->object != OBJECT_NONE) {
647 #  ifdef __OBJECT_MOTION__
648                 Transform tfm = sd->ob_tfm;
649 #  else
650                 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_TRANSFORM);
651 #  endif
652
653                 P = transform_point(&tfm, P);
654         }
655
656         return P;
657 #else
658         return P + D*t;
659 #endif
660 }
661
662 /* Same as above, except that isect->t is assumed to be in object space for
663  * instancing.
664  */
665 ccl_device_inline float3 triangle_refine_local(KernelGlobals *kg,
666                                                ShaderData *sd,
667                                                const Intersection *isect,
668                                                const Ray *ray)
669 {
670         float3 P = ray->P;
671         float3 D = ray->D;
672         float t = isect->t;
673
674         if(isect->object != OBJECT_NONE) {
675 #ifdef __OBJECT_MOTION__
676                 Transform tfm = sd->ob_itfm;
677 #else
678                 Transform tfm = object_fetch_transform(kg,
679                                                        isect->object,
680                                                        OBJECT_INVERSE_TRANSFORM);
681 #endif
682
683                 P = transform_point(&tfm, P);
684                 D = transform_direction(&tfm, D);
685                 D = normalize(D);
686         }
687
688         P = P + D*t;
689
690 #ifdef __INTERSECTION_REFINE__
691         const uint tri_vindex = kernel_tex_fetch(__prim_tri_index, isect->prim);
692         const float4 tri_a = kernel_tex_fetch(__prim_tri_verts, tri_vindex+0),
693                      tri_b = kernel_tex_fetch(__prim_tri_verts, tri_vindex+1),
694                      tri_c = kernel_tex_fetch(__prim_tri_verts, tri_vindex+2);
695         float3 edge1 = make_float3(tri_a.x - tri_c.x, tri_a.y - tri_c.y, tri_a.z - tri_c.z);
696         float3 edge2 = make_float3(tri_b.x - tri_c.x, tri_b.y - tri_c.y, tri_b.z - tri_c.z);
697         float3 tvec = make_float3(P.x - tri_c.x, P.y - tri_c.y, P.z - tri_c.z);
698         float3 qvec = cross(tvec, edge1);
699         float3 pvec = cross(D, edge2);
700         float det = dot(edge1, pvec);
701         if(det != 0.0f) {
702                 /* If determinant is zero it means ray lies in the plane of
703                  * the triangle. It is possible in theory due to watertight
704                  * nature of triangle intersection. For such cases we simply
705                  * don't refine intersection hoping it'll go all fine.
706                  */
707                 float rt = dot(edge2, qvec) / det;
708                 P = P + D*rt;
709         }
710 #endif  /* __INTERSECTION_REFINE__ */
711
712         if(isect->object != OBJECT_NONE) {
713 #ifdef __OBJECT_MOTION__
714                 Transform tfm = sd->ob_tfm;
715 #else
716                 Transform tfm = object_fetch_transform(kg,
717                                                        isect->object,
718                                                        OBJECT_TRANSFORM);
719 #endif
720
721                 P = transform_point(&tfm, P);
722         }
723
724         return P;
725 }
726
727 CCL_NAMESPACE_END