rtcore.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "../../include/embree4/rtcore.h"
  5. RTC_NAMESPACE_USE
  6. namespace embree
  7. {
  8. /*! decoding of intersection flags */
  9. __forceinline bool isCoherent (RTCRayQueryFlags flags) { return (flags & RTC_RAY_QUERY_FLAG_COHERENT) == RTC_RAY_QUERY_FLAG_COHERENT; }
  10. __forceinline bool isIncoherent(RTCRayQueryFlags flags) { return (flags & RTC_RAY_QUERY_FLAG_COHERENT) == RTC_RAY_QUERY_FLAG_INCOHERENT; }
  11. /*! Macros used in the rtcore API implementation */
  12. #if 1
  13. # define RTC_CATCH_BEGIN
  14. # define RTC_CATCH_END(device)
  15. # define RTC_CATCH_END2(scene)
  16. # define RTC_CATCH_END2_FALSE(scene) return false;
  17. #else
  18. #define RTC_CATCH_BEGIN try {
  19. #define RTC_CATCH_END(device) \
  20. } catch (std::bad_alloc&) { \
  21. Device::process_error(device,RTC_ERROR_OUT_OF_MEMORY,"out of memory"); \
  22. } catch (rtcore_error& e) { \
  23. Device::process_error(device,e.error,e.what()); \
  24. } catch (std::exception& e) { \
  25. Device::process_error(device,RTC_ERROR_UNKNOWN,e.what()); \
  26. } catch (...) { \
  27. Device::process_error(device,RTC_ERROR_UNKNOWN,"unknown exception caught"); \
  28. }
  29. #define RTC_CATCH_END2(scene) \
  30. } catch (std::bad_alloc&) { \
  31. Device* device = scene ? scene->device : nullptr; \
  32. Device::process_error(device,RTC_ERROR_OUT_OF_MEMORY,"out of memory"); \
  33. } catch (rtcore_error& e) { \
  34. Device* device = scene ? scene->device : nullptr; \
  35. Device::process_error(device,e.error,e.what()); \
  36. } catch (std::exception& e) { \
  37. Device* device = scene ? scene->device : nullptr; \
  38. Device::process_error(device,RTC_ERROR_UNKNOWN,e.what()); \
  39. } catch (...) { \
  40. Device* device = scene ? scene->device : nullptr; \
  41. Device::process_error(device,RTC_ERROR_UNKNOWN,"unknown exception caught"); \
  42. }
  43. #define RTC_CATCH_END2_FALSE(scene) \
  44. } catch (std::bad_alloc&) { \
  45. Device* device = scene ? scene->device : nullptr; \
  46. Device::process_error(device,RTC_ERROR_OUT_OF_MEMORY,"out of memory"); \
  47. return false; \
  48. } catch (rtcore_error& e) { \
  49. Device* device = scene ? scene->device : nullptr; \
  50. Device::process_error(device,e.error,e.what()); \
  51. return false; \
  52. } catch (std::exception& e) { \
  53. Device* device = scene ? scene->device : nullptr; \
  54. Device::process_error(device,RTC_ERROR_UNKNOWN,e.what()); \
  55. return false; \
  56. } catch (...) { \
  57. Device* device = scene ? scene->device : nullptr; \
  58. Device::process_error(device,RTC_ERROR_UNKNOWN,"unknown exception caught"); \
  59. return false; \
  60. }
  61. #endif
  62. #define RTC_VERIFY_HANDLE(handle) \
  63. if (handle == nullptr) { \
  64. throw_RTCError(RTC_ERROR_INVALID_ARGUMENT,"invalid argument"); \
  65. }
  66. #define RTC_VERIFY_GEOMID(id) \
  67. if (id == RTC_INVALID_GEOMETRY_ID) { \
  68. throw_RTCError(RTC_ERROR_INVALID_ARGUMENT,"invalid argument"); \
  69. }
  70. #define RTC_VERIFY_UPPER(id,upper) \
  71. if (id > upper) { \
  72. throw_RTCError(RTC_ERROR_INVALID_ARGUMENT,"invalid argument"); \
  73. }
  74. #define RTC_VERIFY_RANGE(id,lower,upper) \
  75. if (id < lower || id > upper) \
  76. throw_RTCError(RTC_ERROR_INVALID_OPERATION,"argument out of bounds");
  77. #if 0 // enable to debug print all API calls
  78. #define RTC_TRACE(x) std::cout << #x << std::endl;
  79. #else
  80. #define RTC_TRACE(x)
  81. #endif
  82. #if 0
  83. /*! used to throw embree API errors */
  84. struct rtcore_error : public std::exception
  85. {
  86. __forceinline rtcore_error(RTCError error, const std::string& str)
  87. : error(error), str(str) {}
  88. ~rtcore_error() throw() {}
  89. const char* what () const throw () {
  90. return str.c_str();
  91. }
  92. RTCError error;
  93. std::string str;
  94. };
  95. #endif
  96. #if defined(DEBUG) // only report file and line in debug mode
  97. #define throw_RTCError(error,str) \
  98. printf("%s (%d): %s", __FILE__, __LINE__, std::string(str).c_str()), abort();
  99. //throw rtcore_error(error,std::string(__FILE__) + " (" + toString(__LINE__) + "): " + std::string(str));
  100. #else
  101. #define throw_RTCError(error,str) \
  102. abort();
  103. //throw rtcore_error(error,str);
  104. #endif
  105. #define RTC_BUILD_ARGUMENTS_HAS(settings,member) \
  106. (settings.byteSize > (offsetof(RTCBuildArguments,member)+sizeof(settings.member)))
  107. inline void storeTransform(const AffineSpace3fa& space, RTCFormat format, float* xfm)
  108. {
  109. switch (format)
  110. {
  111. case RTC_FORMAT_FLOAT3X4_ROW_MAJOR:
  112. xfm[ 0] = space.l.vx.x; xfm[ 1] = space.l.vy.x; xfm[ 2] = space.l.vz.x; xfm[ 3] = space.p.x;
  113. xfm[ 4] = space.l.vx.y; xfm[ 5] = space.l.vy.y; xfm[ 6] = space.l.vz.y; xfm[ 7] = space.p.y;
  114. xfm[ 8] = space.l.vx.z; xfm[ 9] = space.l.vy.z; xfm[10] = space.l.vz.z; xfm[11] = space.p.z;
  115. break;
  116. case RTC_FORMAT_FLOAT3X4_COLUMN_MAJOR:
  117. xfm[ 0] = space.l.vx.x; xfm[ 1] = space.l.vx.y; xfm[ 2] = space.l.vx.z;
  118. xfm[ 3] = space.l.vy.x; xfm[ 4] = space.l.vy.y; xfm[ 5] = space.l.vy.z;
  119. xfm[ 6] = space.l.vz.x; xfm[ 7] = space.l.vz.y; xfm[ 8] = space.l.vz.z;
  120. xfm[ 9] = space.p.x; xfm[10] = space.p.y; xfm[11] = space.p.z;
  121. break;
  122. case RTC_FORMAT_FLOAT4X4_COLUMN_MAJOR:
  123. xfm[ 0] = space.l.vx.x; xfm[ 1] = space.l.vx.y; xfm[ 2] = space.l.vx.z; xfm[ 3] = 0.f;
  124. xfm[ 4] = space.l.vy.x; xfm[ 5] = space.l.vy.y; xfm[ 6] = space.l.vy.z; xfm[ 7] = 0.f;
  125. xfm[ 8] = space.l.vz.x; xfm[ 9] = space.l.vz.y; xfm[10] = space.l.vz.z; xfm[11] = 0.f;
  126. xfm[12] = space.p.x; xfm[13] = space.p.y; xfm[14] = space.p.z; xfm[15] = 1.f;
  127. break;
  128. default:
  129. #if !defined(__SYCL_DEVICE_ONLY__)
  130. throw_RTCError(RTC_ERROR_INVALID_OPERATION, "invalid matrix format");
  131. #endif
  132. break;
  133. }
  134. }
  135. }