static_raycaster_embree.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**************************************************************************/
  2. /* static_raycaster_embree.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "static_raycaster_embree.h"
  31. #ifdef TOOLS_ENABLED
  32. #ifdef __SSE2__
  33. #include <pmmintrin.h>
  34. #endif
  35. RTCDevice StaticRaycasterEmbree::embree_device;
  36. StaticRaycaster *StaticRaycasterEmbree::create_embree_raycaster() {
  37. return memnew(StaticRaycasterEmbree);
  38. }
  39. void StaticRaycasterEmbree::make_default_raycaster() {
  40. create_function = create_embree_raycaster;
  41. }
  42. void StaticRaycasterEmbree::free() {
  43. if (embree_device) {
  44. rtcReleaseDevice(embree_device);
  45. }
  46. }
  47. bool StaticRaycasterEmbree::intersect(Ray &r_ray) {
  48. RTCRayQueryContext context;
  49. rtcInitRayQueryContext(&context);
  50. RTCIntersectArguments args;
  51. rtcInitIntersectArguments(&args);
  52. args.context = &context;
  53. rtcIntersect1(embree_scene, (RTCRayHit *)&r_ray, &args);
  54. return r_ray.geomID != RTC_INVALID_GEOMETRY_ID;
  55. }
  56. void StaticRaycasterEmbree::intersect(Vector<Ray> &r_rays) {
  57. Ray *rays = r_rays.ptrw();
  58. for (int i = 0; i < r_rays.size(); ++i) {
  59. intersect(rays[i]);
  60. }
  61. }
  62. void StaticRaycasterEmbree::add_mesh(const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices, unsigned int p_id) {
  63. RTCGeometry embree_mesh = rtcNewGeometry(embree_device, RTC_GEOMETRY_TYPE_TRIANGLE);
  64. int vertex_count = p_vertices.size();
  65. Vector3 *embree_vertices = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count);
  66. memcpy(embree_vertices, p_vertices.ptr(), sizeof(Vector3) * vertex_count);
  67. if (p_indices.is_empty()) {
  68. ERR_FAIL_COND(vertex_count % 3 != 0);
  69. uint32_t *embree_triangles = (uint32_t *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(uint32_t) * 3, vertex_count / 3);
  70. for (int i = 0; i < vertex_count; i++) {
  71. embree_triangles[i] = i;
  72. }
  73. } else {
  74. uint32_t *embree_triangles = (uint32_t *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(uint32_t) * 3, p_indices.size() / 3);
  75. memcpy(embree_triangles, p_indices.ptr(), sizeof(uint32_t) * p_indices.size());
  76. }
  77. rtcCommitGeometry(embree_mesh);
  78. rtcAttachGeometryByID(embree_scene, embree_mesh, p_id);
  79. rtcReleaseGeometry(embree_mesh);
  80. }
  81. void StaticRaycasterEmbree::commit() {
  82. rtcCommitScene(embree_scene);
  83. }
  84. void StaticRaycasterEmbree::set_mesh_filter(const HashSet<int> &p_mesh_ids) {
  85. for (const int &E : p_mesh_ids) {
  86. rtcDisableGeometry(rtcGetGeometry(embree_scene, E));
  87. }
  88. rtcCommitScene(embree_scene);
  89. filter_meshes = p_mesh_ids;
  90. }
  91. void StaticRaycasterEmbree::clear_mesh_filter() {
  92. for (const int &E : filter_meshes) {
  93. rtcEnableGeometry(rtcGetGeometry(embree_scene, E));
  94. }
  95. rtcCommitScene(embree_scene);
  96. filter_meshes.clear();
  97. }
  98. void embree_error_handler(void *p_user_data, RTCError p_code, const char *p_str) {
  99. print_error("Embree error: " + String(p_str));
  100. }
  101. StaticRaycasterEmbree::StaticRaycasterEmbree() {
  102. #ifdef __SSE2__
  103. _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
  104. _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
  105. #endif
  106. if (!embree_device) {
  107. embree_device = rtcNewDevice(nullptr);
  108. rtcSetDeviceErrorFunction(embree_device, &embree_error_handler, nullptr);
  109. }
  110. embree_scene = rtcNewScene(embree_device);
  111. }
  112. StaticRaycasterEmbree::~StaticRaycasterEmbree() {
  113. if (embree_scene != nullptr) {
  114. rtcReleaseScene(embree_scene);
  115. }
  116. }
  117. #endif // TOOLS_ENABLED