bvh_embree.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2018, 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. #include <embree3/rtcore_ray.h>
  17. #include <embree3/rtcore_scene.h>
  18. #include "kernel/kernel_compat_cpu.h"
  19. #include "kernel/split/kernel_split_data_types.h"
  20. #include "kernel/kernel_globals.h"
  21. #include "util/util_vector.h"
  22. CCL_NAMESPACE_BEGIN
  23. struct CCLIntersectContext {
  24. typedef enum {
  25. RAY_REGULAR = 0,
  26. RAY_SHADOW_ALL = 1,
  27. RAY_SSS = 2,
  28. RAY_VOLUME_ALL = 3,
  29. } RayType;
  30. KernelGlobals *kg;
  31. RayType type;
  32. /* for shadow rays */
  33. Intersection *isect_s;
  34. int max_hits;
  35. int num_hits;
  36. /* for SSS Rays: */
  37. LocalIntersection *ss_isect;
  38. int sss_object_id;
  39. uint *lcg_state;
  40. CCLIntersectContext(KernelGlobals *kg_, RayType type_)
  41. {
  42. kg = kg_;
  43. type = type_;
  44. max_hits = 1;
  45. num_hits = 0;
  46. isect_s = NULL;
  47. ss_isect = NULL;
  48. sss_object_id = -1;
  49. lcg_state = NULL;
  50. }
  51. };
  52. class IntersectContext {
  53. public:
  54. IntersectContext(CCLIntersectContext *ctx)
  55. {
  56. rtcInitIntersectContext(&context);
  57. userRayExt = ctx;
  58. }
  59. RTCIntersectContext context;
  60. CCLIntersectContext *userRayExt;
  61. };
  62. ccl_device_inline void kernel_embree_setup_ray(const Ray &ray,
  63. RTCRay &rtc_ray,
  64. const uint visibility)
  65. {
  66. rtc_ray.org_x = ray.P.x;
  67. rtc_ray.org_y = ray.P.y;
  68. rtc_ray.org_z = ray.P.z;
  69. rtc_ray.dir_x = ray.D.x;
  70. rtc_ray.dir_y = ray.D.y;
  71. rtc_ray.dir_z = ray.D.z;
  72. rtc_ray.tnear = 0.0f;
  73. rtc_ray.tfar = ray.t;
  74. rtc_ray.time = ray.time;
  75. rtc_ray.mask = visibility;
  76. }
  77. ccl_device_inline void kernel_embree_setup_rayhit(const Ray &ray,
  78. RTCRayHit &rayhit,
  79. const uint visibility)
  80. {
  81. kernel_embree_setup_ray(ray, rayhit.ray, visibility);
  82. rayhit.hit.geomID = RTC_INVALID_GEOMETRY_ID;
  83. rayhit.hit.primID = RTC_INVALID_GEOMETRY_ID;
  84. }
  85. ccl_device_inline void kernel_embree_convert_hit(KernelGlobals *kg,
  86. const RTCRay *ray,
  87. const RTCHit *hit,
  88. Intersection *isect)
  89. {
  90. bool is_hair = hit->geomID & 1;
  91. isect->u = is_hair ? hit->u : 1.0f - hit->v - hit->u;
  92. isect->v = is_hair ? hit->v : hit->u;
  93. isect->t = ray->tfar;
  94. isect->Ng = make_float3(hit->Ng_x, hit->Ng_y, hit->Ng_z);
  95. if (hit->instID[0] != RTC_INVALID_GEOMETRY_ID) {
  96. RTCScene inst_scene = (RTCScene)rtcGetGeometryUserData(
  97. rtcGetGeometry(kernel_data.bvh.scene, hit->instID[0]));
  98. isect->prim = hit->primID +
  99. (intptr_t)rtcGetGeometryUserData(rtcGetGeometry(inst_scene, hit->geomID)) +
  100. kernel_tex_fetch(__object_node, hit->instID[0] / 2);
  101. isect->object = hit->instID[0] / 2;
  102. }
  103. else {
  104. isect->prim = hit->primID + (intptr_t)rtcGetGeometryUserData(
  105. rtcGetGeometry(kernel_data.bvh.scene, hit->geomID));
  106. isect->object = OBJECT_NONE;
  107. }
  108. isect->type = kernel_tex_fetch(__prim_type, isect->prim);
  109. }
  110. ccl_device_inline void kernel_embree_convert_local_hit(KernelGlobals *kg,
  111. const RTCRay *ray,
  112. const RTCHit *hit,
  113. Intersection *isect,
  114. int local_object_id)
  115. {
  116. isect->u = 1.0f - hit->v - hit->u;
  117. isect->v = hit->u;
  118. isect->t = ray->tfar;
  119. isect->Ng = make_float3(hit->Ng_x, hit->Ng_y, hit->Ng_z);
  120. RTCScene inst_scene = (RTCScene)rtcGetGeometryUserData(
  121. rtcGetGeometry(kernel_data.bvh.scene, local_object_id * 2));
  122. isect->prim = hit->primID +
  123. (intptr_t)rtcGetGeometryUserData(rtcGetGeometry(inst_scene, hit->geomID)) +
  124. kernel_tex_fetch(__object_node, local_object_id);
  125. isect->object = local_object_id;
  126. isect->type = kernel_tex_fetch(__prim_type, isect->prim);
  127. }
  128. CCL_NAMESPACE_END