geom_motion_triangle_intersect.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright 2011-2016 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. /* Motion Triangle Primitive
  17. *
  18. * These are stored as regular triangles, plus extra positions and normals at
  19. * times other than the frame center. Computing the triangle vertex positions
  20. * or normals at a given ray time is a matter of interpolation of the two steps
  21. * between which the ray time lies.
  22. *
  23. * The extra positions and normals are stored as ATTR_STD_MOTION_VERTEX_POSITION
  24. * and ATTR_STD_MOTION_VERTEX_NORMAL mesh attributes.
  25. */
  26. CCL_NAMESPACE_BEGIN
  27. /* Refine triangle intersection to more precise hit point. For rays that travel
  28. * far the precision is often not so good, this reintersects the primitive from
  29. * a closer distance.
  30. */
  31. ccl_device_inline float3 motion_triangle_refine(
  32. KernelGlobals *kg, ShaderData *sd, const Intersection *isect, const Ray *ray, float3 verts[3])
  33. {
  34. float3 P = ray->P;
  35. float3 D = ray->D;
  36. float t = isect->t;
  37. #ifdef __INTERSECTION_REFINE__
  38. if (isect->object != OBJECT_NONE) {
  39. if (UNLIKELY(t == 0.0f)) {
  40. return P;
  41. }
  42. # ifdef __OBJECT_MOTION__
  43. Transform tfm = sd->ob_itfm;
  44. # else
  45. Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_INVERSE_TRANSFORM);
  46. # endif
  47. P = transform_point(&tfm, P);
  48. D = transform_direction(&tfm, D * t);
  49. D = normalize_len(D, &t);
  50. }
  51. P = P + D * t;
  52. /* Compute refined intersection distance. */
  53. const float3 e1 = verts[0] - verts[2];
  54. const float3 e2 = verts[1] - verts[2];
  55. const float3 s1 = cross(D, e2);
  56. const float invdivisor = 1.0f / dot(s1, e1);
  57. const float3 d = P - verts[2];
  58. const float3 s2 = cross(d, e1);
  59. float rt = dot(e2, s2) * invdivisor;
  60. /* Compute refined position. */
  61. P = P + D * rt;
  62. if (isect->object != OBJECT_NONE) {
  63. # ifdef __OBJECT_MOTION__
  64. Transform tfm = sd->ob_tfm;
  65. # else
  66. Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_TRANSFORM);
  67. # endif
  68. P = transform_point(&tfm, P);
  69. }
  70. return P;
  71. #else
  72. return P + D * t;
  73. #endif
  74. }
  75. /* Same as above, except that isect->t is assumed to be in object space
  76. * for instancing.
  77. */
  78. #ifdef __BVH_LOCAL__
  79. # if defined(__KERNEL_CUDA__) && (defined(i386) || defined(_M_IX86))
  80. ccl_device_noinline
  81. # else
  82. ccl_device_inline
  83. # endif
  84. float3
  85. motion_triangle_refine_local(KernelGlobals *kg,
  86. ShaderData *sd,
  87. const Intersection *isect,
  88. const Ray *ray,
  89. float3 verts[3])
  90. {
  91. float3 P = ray->P;
  92. float3 D = ray->D;
  93. float t = isect->t;
  94. # ifdef __INTERSECTION_REFINE__
  95. if (isect->object != OBJECT_NONE) {
  96. # ifdef __OBJECT_MOTION__
  97. Transform tfm = sd->ob_itfm;
  98. # else
  99. Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_INVERSE_TRANSFORM);
  100. # endif
  101. P = transform_point(&tfm, P);
  102. D = transform_direction(&tfm, D);
  103. D = normalize(D);
  104. }
  105. P = P + D * t;
  106. /* compute refined intersection distance */
  107. const float3 e1 = verts[0] - verts[2];
  108. const float3 e2 = verts[1] - verts[2];
  109. const float3 s1 = cross(D, e2);
  110. const float invdivisor = 1.0f / dot(s1, e1);
  111. const float3 d = P - verts[2];
  112. const float3 s2 = cross(d, e1);
  113. float rt = dot(e2, s2) * invdivisor;
  114. P = P + D * rt;
  115. if (isect->object != OBJECT_NONE) {
  116. # ifdef __OBJECT_MOTION__
  117. Transform tfm = sd->ob_tfm;
  118. # else
  119. Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_TRANSFORM);
  120. # endif
  121. P = transform_point(&tfm, P);
  122. }
  123. return P;
  124. # else /* __INTERSECTION_REFINE__ */
  125. return P + D * t;
  126. # endif /* __INTERSECTION_REFINE__ */
  127. }
  128. #endif /* __BVH_LOCAL__ */
  129. /* Ray intersection. We simply compute the vertex positions at the given ray
  130. * time and do a ray intersection with the resulting triangle.
  131. */
  132. ccl_device_inline bool motion_triangle_intersect(KernelGlobals *kg,
  133. Intersection *isect,
  134. float3 P,
  135. float3 dir,
  136. float time,
  137. uint visibility,
  138. int object,
  139. int prim_addr)
  140. {
  141. /* Primitive index for vertex location lookup. */
  142. int prim = kernel_tex_fetch(__prim_index, prim_addr);
  143. int fobject = (object == OBJECT_NONE) ? kernel_tex_fetch(__prim_object, prim_addr) : object;
  144. /* Get vertex locations for intersection. */
  145. float3 verts[3];
  146. motion_triangle_vertices(kg, fobject, prim, time, verts);
  147. /* Ray-triangle intersection, unoptimized. */
  148. float t, u, v;
  149. if (ray_triangle_intersect(P,
  150. dir,
  151. isect->t,
  152. #if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
  153. (ssef *)verts,
  154. #else
  155. verts[0],
  156. verts[1],
  157. verts[2],
  158. #endif
  159. &u,
  160. &v,
  161. &t)) {
  162. #ifdef __VISIBILITY_FLAG__
  163. /* Visibility flag test. we do it here under the assumption
  164. * that most triangles are culled by node flags.
  165. */
  166. if (kernel_tex_fetch(__prim_visibility, prim_addr) & visibility)
  167. #endif
  168. {
  169. isect->t = t;
  170. isect->u = u;
  171. isect->v = v;
  172. isect->prim = prim_addr;
  173. isect->object = object;
  174. isect->type = PRIMITIVE_MOTION_TRIANGLE;
  175. return true;
  176. }
  177. }
  178. return false;
  179. }
  180. /* Special ray intersection routines for local intersections. In that case we
  181. * only want to intersect with primitives in the same object, and if case of
  182. * multiple hits we pick a single random primitive as the intersection point.
  183. * Returns whether traversal should be stopped.
  184. */
  185. #ifdef __BVH_LOCAL__
  186. ccl_device_inline bool motion_triangle_intersect_local(KernelGlobals *kg,
  187. LocalIntersection *local_isect,
  188. float3 P,
  189. float3 dir,
  190. float time,
  191. int object,
  192. int local_object,
  193. int prim_addr,
  194. float tmax,
  195. uint *lcg_state,
  196. int max_hits)
  197. {
  198. /* Only intersect with matching object, for instanced objects we
  199. * already know we are only intersecting the right object. */
  200. if (object == OBJECT_NONE) {
  201. if (kernel_tex_fetch(__prim_object, prim_addr) != local_object) {
  202. return false;
  203. }
  204. }
  205. /* Primitive index for vertex location lookup. */
  206. int prim = kernel_tex_fetch(__prim_index, prim_addr);
  207. /* Get vertex locations for intersection. */
  208. float3 verts[3];
  209. motion_triangle_vertices(kg, local_object, prim, time, verts);
  210. /* Ray-triangle intersection, unoptimized. */
  211. float t, u, v;
  212. if (!ray_triangle_intersect(P,
  213. dir,
  214. tmax,
  215. # if defined(__KERNEL_SSE2__) && defined(__KERNEL_SSE__)
  216. (ssef *)verts,
  217. # else
  218. verts[0],
  219. verts[1],
  220. verts[2],
  221. # endif
  222. &u,
  223. &v,
  224. &t)) {
  225. return false;
  226. }
  227. /* If no actual hit information is requested, just return here. */
  228. if (max_hits == 0) {
  229. return true;
  230. }
  231. int hit;
  232. if (lcg_state) {
  233. /* Record up to max_hits intersections. */
  234. for (int i = min(max_hits, local_isect->num_hits) - 1; i >= 0; --i) {
  235. if (local_isect->hits[i].t == t) {
  236. return false;
  237. }
  238. }
  239. local_isect->num_hits++;
  240. if (local_isect->num_hits <= max_hits) {
  241. hit = local_isect->num_hits - 1;
  242. }
  243. else {
  244. /* Reservoir sampling: if we are at the maximum number of
  245. * hits, randomly replace element or skip it.
  246. */
  247. hit = lcg_step_uint(lcg_state) % local_isect->num_hits;
  248. if (hit >= max_hits)
  249. return false;
  250. }
  251. }
  252. else {
  253. /* Record closest intersection only. */
  254. if (local_isect->num_hits && t > local_isect->hits[0].t) {
  255. return false;
  256. }
  257. hit = 0;
  258. local_isect->num_hits = 1;
  259. }
  260. /* Record intersection. */
  261. Intersection *isect = &local_isect->hits[hit];
  262. isect->t = t;
  263. isect->u = u;
  264. isect->v = v;
  265. isect->prim = prim_addr;
  266. isect->object = object;
  267. isect->type = PRIMITIVE_MOTION_TRIANGLE;
  268. /* Record geometric normal. */
  269. local_isect->Ng[hit] = normalize(cross(verts[1] - verts[0], verts[2] - verts[0]));
  270. return false;
  271. }
  272. #endif /* __BVH_LOCAL__ */
  273. CCL_NAMESPACE_END