Cycles: Cleanup, comments and dead code
[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 isec_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
244                         __m256 inv_den_256 = _mm256_rcp_ps(den_256);
245                         U_256 = _mm256_mul_ps(U_256, inv_den_256);
246                         V_256 = _mm256_mul_ps(V_256, inv_den_256);
247                         T_256 = _mm256_mul_ps(T_256, inv_den_256);
248
249                         _mm256_store_ps(U8, U_256);
250                         _mm256_store_ps(V8, V_256);
251                         _mm256_store_ps(T8, T_256);
252
253                         /* NOTE: Here we assume visibility for all triangles in the node is
254                          * the same. */
255
256                         (*isect)->u = U8[i];
257                         (*isect)->v = V8[i];
258                         (*isect)->t = T8[i];
259
260                         (*isect)->prim = (prim_addr + i);
261                         (*isect)->object = object;
262                         (*isect)->type = PRIMITIVE_TRIANGLE;
263
264                         return true;
265                 }
266         else {
267                         _mm256_store_ps(den8, den_256);
268                         _mm256_store_ps(U8, U_256);
269                         _mm256_store_ps(V8, V_256);
270                         _mm256_store_ps(T8, T_256);
271
272                         _mm256_store_ps(sign_T8, sign_T_256);
273                         _mm256_store_ps(xor_signmask8, xor_signmask_256);
274                         _mm256_store_si256((__m256i*)mask_minmaxUVW8, mask_minmaxUVW_256);
275
276                         int ret = false;
277
278                         if(visibility == PATH_RAY_SHADOW) {
279                                 for(int i = 0; i < prim_num; i++) {
280                                         if(!mask_minmaxUVW8[i]) {
281 #ifdef __VISIBILITY_FLAG__
282                                                 if(kernel_tex_fetch(__prim_visibility, (prim_addr + i)) & visibility)
283 #endif
284                                                 {
285                                                         if((sign_T8[i] >= 0.0f) &&
286                                                            (sign_T8[i] <= (*isect)->t * xor_signmask8[i]))
287                                                         {
288                                                                 if(den8[i]) {
289                                                                         const float inv_den = 1.0f / den8[i];
290
291                                                                         (*isect)->u = U8[i] * inv_den;
292                                                                         (*isect)->v = V8[i] * inv_den;
293                                                                         (*isect)->t = T8[i] * inv_den;
294
295                                                                         (*isect)->prim = (prim_addr + i);
296                                                                         (*isect)->object = object;
297                                                                         (*isect)->type = PRIMITIVE_TRIANGLE;
298
299                                                                         int prim = kernel_tex_fetch(__prim_index, (*isect)->prim);
300                                                                         int shader = 0;
301
302 #ifdef __HAIR__
303                                                                         if(kernel_tex_fetch(__prim_type, (*isect)->prim) & PRIMITIVE_ALL_TRIANGLE)
304 #endif
305                                                                         {
306                                                                                 shader = kernel_tex_fetch(__tri_shader, prim);
307                                                                         }
308 #ifdef __HAIR__
309                                                                         else {
310                                                                                 float4 str = kernel_tex_fetch(__curves, prim);
311                                                                                 shader = __float_as_int(str.z);
312                                                                         }
313 #endif
314                                                                         int flag = kernel_tex_fetch(__shaders, (shader & SHADER_MASK)).flags;
315
316                                                                         /* if no transparent shadows, all light is blocked. */
317                                                                         if(!(flag & SD_HAS_TRANSPARENT_SHADOW)) {
318                                                                                 return 2;
319                                                                         }
320                                                                         /* if maximum number of hits reached, block all light. */
321                                                                         else if(*num_hits == max_hits) {
322                                                                                 return 2;
323                                                                         }
324                                                                         /* move on to next entry in intersections array */
325                                                                         ret = true;
326
327                                                                         (*isect)++;
328                                                                         (*num_hits)++;
329
330                                                                         (*num_hits_in_instance)++;
331
332                                                                         (*isect)->t = isec_t;
333
334                                                                 } //den
335                                                         } //if sign
336                                                 } //vis
337                                         }//if mask
338                                 } //for
339                 }
340                 else { //default case
341                         for(int i = 0; i < prim_num; i++) {
342                                 if(!mask_minmaxUVW8[i]) {
343 #ifdef __VISIBILITY_FLAG__
344                                         if(kernel_tex_fetch(__prim_visibility, (prim_addr + i)) & visibility)
345 #endif
346                                         {
347                                                 if((sign_T8[i] >= 0.0f) &&
348                                                    (sign_T8[i] <= (*isect)->t * xor_signmask8[i]))
349                                                 {
350                                                         if(den8[i]) {
351                                                                 const float inv_den = 1.0f / den8[i];
352
353                                                                 (*isect)->u = U8[i] * inv_den;
354                                                                 (*isect)->v = V8[i] * inv_den;
355                                                                 (*isect)->t = T8[i] * inv_den;
356
357                                                                 (*isect)->prim = (prim_addr + i);
358                                                                 (*isect)->object = object;
359                                                                 (*isect)->type = PRIMITIVE_TRIANGLE;
360
361                                                                 ret = true;
362                                                         } //den
363                                                 } //if sign
364                                         } //vis
365                                 }//if mask
366                         } //for
367                 } //default
368                 return ret;
369         }// else PATH_RAY_SHADOW_OPAQUE
370 }
371
372 ccl_device_inline int triangle_intersect8(
373         KernelGlobals *kg,
374         Intersection **isect,
375         float3 P,
376         float3 dir,
377         uint visibility,
378         int object,
379         int prim_addr,
380         int prim_num,
381         uint *num_hits,
382         uint max_hits,
383         int *num_hits_in_instance,
384         float isec_t)
385  {
386         __m128 tri_a[8], tri_b[8], tri_c[8];
387         __m256  tritmp[12], tri[12];
388         __m256  triA[3], triB[3], triC[3];
389
390         int i, r;
391
392         uint tri_vindex = kernel_tex_fetch(__prim_tri_index, prim_addr);
393         for(i = 0; i < prim_num; i++) {
394                 tri_a[i] = *(__m128*)&kg->__prim_tri_verts.data[tri_vindex++];
395                 tri_b[i] = *(__m128*)&kg->__prim_tri_verts.data[tri_vindex++];
396                 tri_c[i] = *(__m128*)&kg->__prim_tri_verts.data[tri_vindex++];
397          }
398         //create 9 or  12 placeholders
399         tri[0] = _mm256_castps128_ps256(tri_a[0]);    //_mm256_zextps128_ps256
400         tri[1] = _mm256_castps128_ps256(tri_b[0]);//_mm256_zextps128_ps256
401         tri[2] = _mm256_castps128_ps256(tri_c[0]);//_mm256_zextps128_ps256
402
403         tri[3] = _mm256_castps128_ps256(tri_a[1]);    //_mm256_zextps128_ps256
404         tri[4] = _mm256_castps128_ps256(tri_b[1]);//_mm256_zextps128_ps256
405         tri[5] = _mm256_castps128_ps256(tri_c[1]);//_mm256_zextps128_ps256
406
407         tri[6] = _mm256_castps128_ps256(tri_a[2]);    //_mm256_zextps128_ps256
408         tri[7] = _mm256_castps128_ps256(tri_b[2]);//_mm256_zextps128_ps256
409         tri[8] = _mm256_castps128_ps256(tri_c[2]);//_mm256_zextps128_ps256
410
411         if(prim_num > 3) {
412                 tri[9] =  _mm256_castps128_ps256(tri_a[3]);    //_mm256_zextps128_ps256
413                 tri[10] = _mm256_castps128_ps256(tri_b[3]);//_mm256_zextps128_ps256
414                 tri[11] = _mm256_castps128_ps256(tri_c[3]);//_mm256_zextps128_ps256
415         }
416
417         for(i = 4, r = 0; i < prim_num; i ++, r += 3) {
418                 tri[r] =     _mm256_insertf128_ps(tri[r] , tri_a[i], 1);
419                 tri[r + 1] = _mm256_insertf128_ps(tri[r + 1], tri_b[i], 1);
420                 tri[r + 2] = _mm256_insertf128_ps(tri[r + 2], tri_c[i], 1);
421          }
422
423         //------------------------------------------------
424         //0!  Xa0 Ya0 Za0 1 Xa4 Ya4 Za4  1
425         //1!  Xb0 Yb0 Zb0 1 Xb4 Yb4 Zb4 1
426         //2!  Xc0 Yc0 Zc0 1 Xc4 Yc4 Zc4 1
427
428         //3!  Xa1 Ya1 Za1 1 Xa5 Ya5 Za5 1
429         //4!  Xb1 Yb1 Zb1 1 Xb5 Yb5 Zb5  1
430         //5!  Xc1 Yc1 Zc1 1 Xc5 Yc5 Zc5 1
431
432         //6!  Xa2 Ya2 Za2 1 Xa6 Ya6 Za6 1
433         //7!  Xb2 Yb2 Zb2 1 Xb6 Yb6 Zb6  1
434         //8!  Xc2 Yc2 Zc2 1 Xc6 Yc6 Zc6 1
435
436         //9!  Xa3 Ya3 Za3 1 Xa7 Ya7 Za7  1
437         //10! Xb3 Yb3 Zb3 1 Xb7 Yb7 Zb7  1
438         //11! Xc3 Yc3 Zc3 1 Xc7 Yc7 Zc7  1
439
440         //"transpose"
441         tritmp[0] = _mm256_unpacklo_ps(tri[0], tri[3]);   //0!  Xa0 Xa1 Ya0 Ya1 Xa4 Xa5 Ya4 Ya5
442         tritmp[1] = _mm256_unpackhi_ps(tri[0], tri[3]);   //1!  Za0 Za1 1   1   Za4 Za5  1   1
443
444         tritmp[2] = _mm256_unpacklo_ps(tri[6], tri[9]);   //2!  Xa2 Xa3 Ya2 Ya3 Xa6 Xa7 Ya6 Ya7
445         tritmp[3] = _mm256_unpackhi_ps(tri[6], tri[9]);   //3!  Za2 Za3  1   1  Za6 Za7  1   1
446
447         tritmp[4] = _mm256_unpacklo_ps(tri[1], tri[4]);   //4!  Xb0 Xb1 Yb0 Yb1 Xb4 Xb5 Yb4 Yb5
448         tritmp[5] = _mm256_unpackhi_ps(tri[1], tri[4]);   //5!  Zb0 Zb1  1  1   Zb4 Zb5  1   1
449
450         tritmp[6] = _mm256_unpacklo_ps(tri[7], tri[10]);  //6!  Xb2 Xb3 Yb2 Yb3 Xb6 Xb7 Yb6 Yb7
451         tritmp[7] = _mm256_unpackhi_ps(tri[7], tri[10]);  //7!  Zb2 Zb3  1    1 Zb6 Zb7  1   1
452
453         tritmp[8] = _mm256_unpacklo_ps(tri[2], tri[5]);   //8!  Xc0 Xc1 Yc0 Yc1 Xc4 Xc5 Yc4 Yc5
454         tritmp[9] = _mm256_unpackhi_ps(tri[2], tri[5]);   //9!  Zc0 Zc1  1   1  Zc4 Zc5  1   1
455
456         tritmp[10] = _mm256_unpacklo_ps(tri[8], tri[11]); //10! Xc2 Xc3 Yc2 Yc3 Xc6 Xc7 Yc6 Yc7
457         tritmp[11] = _mm256_unpackhi_ps(tri[8], tri[11]); //11! Zc2 Zc3  1   1  Zc6 Zc7  1   1
458
459                                 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
460         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
461         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
462         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
463
464         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
465         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
466         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
467
468         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
469         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
470         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
471
472                           /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
473
474         int result = ray_triangle_intersect8(kg, P,
475                                              dir,
476                                              isect,
477                                              visibility, object,
478                                              triA,
479                                              triB,
480                                              triC,
481                                              prim_addr,
482                                              prim_num,
483                                              num_hits,
484                                              max_hits,
485                                              num_hits_in_instance,
486                                              isec_t);
487         return result;
488 }
489
490 #endif  /* __KERNEL_AVX2__ */
491
492 /* Special ray intersection routines for subsurface scattering. In that case we
493  * only want to intersect with primitives in the same object, and if case of
494  * multiple hits we pick a single random primitive as the intersection point.
495  * Returns whether traversal should be stopped.
496  */
497
498 #ifdef __BVH_LOCAL__
499 ccl_device_inline bool triangle_intersect_local(
500         KernelGlobals *kg,
501         LocalIntersection *local_isect,
502         float3 P,
503         float3 dir,
504         int object,
505         int local_object,
506         int prim_addr,
507         float tmax,
508         uint *lcg_state,
509         int max_hits)
510 {
511         /* Only intersect with matching object, for instanced objects we
512          * already know we are only intersecting the right object. */
513         if(object == OBJECT_NONE) {
514                 if(kernel_tex_fetch(__prim_object, prim_addr) != local_object) {
515                         return false;
516                 }
517         }
518
519         const uint tri_vindex = kernel_tex_fetch(__prim_tri_index, prim_addr);
520 #if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
521         const ssef *ssef_verts = (ssef*)&kg->__prim_tri_verts.data[tri_vindex];
522 #else
523         const float3 tri_a = float4_to_float3(kernel_tex_fetch(__prim_tri_verts, tri_vindex+0)),
524                      tri_b = float4_to_float3(kernel_tex_fetch(__prim_tri_verts, tri_vindex+1)),
525                      tri_c = float4_to_float3(kernel_tex_fetch(__prim_tri_verts, tri_vindex+2));
526 #endif
527         float t, u, v;
528         if(!ray_triangle_intersect(P,
529                                    dir,
530                                    tmax,
531 #if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
532                                    ssef_verts,
533 #else
534                                    tri_a, tri_b, tri_c,
535 #endif
536                                    &u, &v, &t))
537         {
538                 return false;
539         }
540
541         /* If no actual hit information is requested, just return here. */
542         if(max_hits == 0) {
543                 return true;
544         }
545
546         int hit;
547         if(lcg_state) {
548                 /* Record up to max_hits intersections. */
549                 for(int i = min(max_hits, local_isect->num_hits) - 1; i >= 0; --i) {
550                         if(local_isect->hits[i].t == t) {
551                                 return false;
552                         }
553                 }
554
555                 local_isect->num_hits++;
556
557                 if(local_isect->num_hits <= max_hits) {
558                         hit = local_isect->num_hits - 1;
559                 }
560                 else {
561                         /* reservoir sampling: if we are at the maximum number of
562                          * hits, randomly replace element or skip it */
563                         hit = lcg_step_uint(lcg_state) % local_isect->num_hits;
564
565                         if(hit >= max_hits)
566                                 return false;
567                 }
568         }
569         else {
570                 /* Record closest intersection only. */
571                 if(local_isect->num_hits && t > local_isect->hits[0].t) {
572                         return false;
573                 }
574
575                 hit = 0;
576                 local_isect->num_hits = 1;
577         }
578
579         /* Record intersection. */
580         Intersection *isect = &local_isect->hits[hit];
581         isect->prim = prim_addr;
582         isect->object = object;
583         isect->type = PRIMITIVE_TRIANGLE;
584         isect->u = u;
585         isect->v = v;
586         isect->t = t;
587
588         /* Record geometric normal. */
589 #if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
590         const float3 tri_a = float4_to_float3(kernel_tex_fetch(__prim_tri_verts, tri_vindex+0)),
591                      tri_b = float4_to_float3(kernel_tex_fetch(__prim_tri_verts, tri_vindex+1)),
592                      tri_c = float4_to_float3(kernel_tex_fetch(__prim_tri_verts, tri_vindex+2));
593 #endif
594         local_isect->Ng[hit] = normalize(cross(tri_b - tri_a, tri_c - tri_a));
595
596         return false;
597 }
598 #endif  /* __BVH_LOCAL__ */
599
600 /* Refine triangle intersection to more precise hit point. For rays that travel
601  * far the precision is often not so good, this reintersects the primitive from
602  * a closer distance. */
603
604 /* Reintersections uses the paper:
605  *
606  * Tomas Moeller
607  * Fast, minimum storage ray/triangle intersection
608  * http://www.cs.virginia.edu/~gfx/Courses/2003/ImageSynthesis/papers/Acceleration/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf
609  */
610
611 ccl_device_inline float3 triangle_refine(KernelGlobals *kg,
612                                          ShaderData *sd,
613                                          const Intersection *isect,
614                                          const Ray *ray)
615 {
616         float3 P = ray->P;
617         float3 D = ray->D;
618         float t = isect->t;
619
620 #ifdef __INTERSECTION_REFINE__
621         if(isect->object != OBJECT_NONE) {
622                 if(UNLIKELY(t == 0.0f)) {
623                         return P;
624                 }
625 #  ifdef __OBJECT_MOTION__
626                 Transform tfm = sd->ob_itfm;
627 #  else
628                 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_INVERSE_TRANSFORM);
629 #  endif
630
631                 P = transform_point(&tfm, P);
632                 D = transform_direction(&tfm, D*t);
633                 D = normalize_len(D, &t);
634         }
635
636         P = P + D*t;
637
638         const uint tri_vindex = kernel_tex_fetch(__prim_tri_index, isect->prim);
639         const float4 tri_a = kernel_tex_fetch(__prim_tri_verts, tri_vindex+0),
640                      tri_b = kernel_tex_fetch(__prim_tri_verts, tri_vindex+1),
641                      tri_c = kernel_tex_fetch(__prim_tri_verts, tri_vindex+2);
642         float3 edge1 = make_float3(tri_a.x - tri_c.x, tri_a.y - tri_c.y, tri_a.z - tri_c.z);
643         float3 edge2 = make_float3(tri_b.x - tri_c.x, tri_b.y - tri_c.y, tri_b.z - tri_c.z);
644         float3 tvec = make_float3(P.x - tri_c.x, P.y - tri_c.y, P.z - tri_c.z);
645         float3 qvec = cross(tvec, edge1);
646         float3 pvec = cross(D, edge2);
647         float det = dot(edge1, pvec);
648         if(det != 0.0f) {
649                 /* If determinant is zero it means ray lies in the plane of
650                  * the triangle. It is possible in theory due to watertight
651                  * nature of triangle intersection. For such cases we simply
652                  * don't refine intersection hoping it'll go all fine.
653                  */
654                 float rt = dot(edge2, qvec) / det;
655                 P = P + D*rt;
656         }
657
658         if(isect->object != OBJECT_NONE) {
659 #  ifdef __OBJECT_MOTION__
660                 Transform tfm = sd->ob_tfm;
661 #  else
662                 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_TRANSFORM);
663 #  endif
664
665                 P = transform_point(&tfm, P);
666         }
667
668         return P;
669 #else
670         return P + D*t;
671 #endif
672 }
673
674 /* Same as above, except that isect->t is assumed to be in object space for
675  * instancing.
676  */
677 ccl_device_inline float3 triangle_refine_local(KernelGlobals *kg,
678                                                ShaderData *sd,
679                                                const Intersection *isect,
680                                                const Ray *ray)
681 {
682         float3 P = ray->P;
683         float3 D = ray->D;
684         float t = isect->t;
685
686         if(isect->object != OBJECT_NONE) {
687 #ifdef __OBJECT_MOTION__
688                 Transform tfm = sd->ob_itfm;
689 #else
690                 Transform tfm = object_fetch_transform(kg,
691                                                        isect->object,
692                                                        OBJECT_INVERSE_TRANSFORM);
693 #endif
694
695                 P = transform_point(&tfm, P);
696                 D = transform_direction(&tfm, D);
697                 D = normalize(D);
698         }
699
700         P = P + D*t;
701
702 #ifdef __INTERSECTION_REFINE__
703         const uint tri_vindex = kernel_tex_fetch(__prim_tri_index, isect->prim);
704         const float4 tri_a = kernel_tex_fetch(__prim_tri_verts, tri_vindex+0),
705                      tri_b = kernel_tex_fetch(__prim_tri_verts, tri_vindex+1),
706                      tri_c = kernel_tex_fetch(__prim_tri_verts, tri_vindex+2);
707         float3 edge1 = make_float3(tri_a.x - tri_c.x, tri_a.y - tri_c.y, tri_a.z - tri_c.z);
708         float3 edge2 = make_float3(tri_b.x - tri_c.x, tri_b.y - tri_c.y, tri_b.z - tri_c.z);
709         float3 tvec = make_float3(P.x - tri_c.x, P.y - tri_c.y, P.z - tri_c.z);
710         float3 qvec = cross(tvec, edge1);
711         float3 pvec = cross(D, edge2);
712         float det = dot(edge1, pvec);
713         if(det != 0.0f) {
714                 /* If determinant is zero it means ray lies in the plane of
715                  * the triangle. It is possible in theory due to watertight
716                  * nature of triangle intersection. For such cases we simply
717                  * don't refine intersection hoping it'll go all fine.
718                  */
719                 float rt = dot(edge2, qvec) / det;
720                 P = P + D*rt;
721         }
722 #endif  /* __INTERSECTION_REFINE__ */
723
724         if(isect->object != OBJECT_NONE) {
725 #ifdef __OBJECT_MOTION__
726                 Transform tfm = sd->ob_tfm;
727 #else
728                 Transform tfm = object_fetch_transform(kg,
729                                                        isect->object,
730                                                        OBJECT_TRANSFORM);
731 #endif
732
733                 P = transform_point(&tfm, P);
734         }
735
736         return P;
737 }
738
739 CCL_NAMESPACE_END