lightmap_raycaster_embree.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**************************************************************************/
  2. /* lightmap_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. #ifdef TOOLS_ENABLED
  31. #include "lightmap_raycaster_embree.h"
  32. #ifdef __SSE2__
  33. #include <pmmintrin.h>
  34. #endif
  35. LightmapRaycaster *LightmapRaycasterEmbree::create_embree_raycaster() {
  36. return memnew(LightmapRaycasterEmbree);
  37. }
  38. void LightmapRaycasterEmbree::make_default_raycaster() {
  39. create_function = create_embree_raycaster;
  40. }
  41. void LightmapRaycasterEmbree::filter_function(const struct RTCFilterFunctionNArguments *p_args) {
  42. RTCHit *hit = (RTCHit *)p_args->hit;
  43. unsigned int geomID = hit->geomID;
  44. float u = hit->u;
  45. float v = hit->v;
  46. LightmapRaycasterEmbree *scene = (LightmapRaycasterEmbree *)p_args->geometryUserPtr;
  47. RTCGeometry geom = rtcGetGeometry(scene->embree_scene, geomID);
  48. rtcInterpolate0(geom, hit->primID, hit->u, hit->v, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, &hit->u, 2);
  49. if (scene->alpha_textures.has(geomID)) {
  50. const AlphaTextureData &alpha_texture = scene->alpha_textures[geomID];
  51. if (alpha_texture.sample(hit->u, hit->v) < 128) {
  52. p_args->valid[0] = 0;
  53. return;
  54. }
  55. }
  56. rtcInterpolate0(geom, hit->primID, u, v, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, &hit->Ng_x, 3);
  57. }
  58. bool LightmapRaycasterEmbree::intersect(Ray &r_ray) {
  59. RTCRayQueryContext context;
  60. rtcInitRayQueryContext(&context);
  61. RTCIntersectArguments args;
  62. rtcInitIntersectArguments(&args);
  63. args.context = &context;
  64. rtcIntersect1(embree_scene, (RTCRayHit *)&r_ray, &args);
  65. return r_ray.geomID != RTC_INVALID_GEOMETRY_ID;
  66. }
  67. void LightmapRaycasterEmbree::intersect(Vector<Ray> &r_rays) {
  68. Ray *rays = r_rays.ptrw();
  69. for (int i = 0; i < r_rays.size(); ++i) {
  70. intersect(rays[i]);
  71. }
  72. }
  73. void LightmapRaycasterEmbree::set_mesh_alpha_texture(Ref<Image> p_alpha_texture, unsigned int p_id) {
  74. if (p_alpha_texture.is_valid() && p_alpha_texture->get_size() != Vector2i()) {
  75. AlphaTextureData tex;
  76. tex.size = p_alpha_texture->get_size();
  77. tex.data = p_alpha_texture->get_data();
  78. alpha_textures.insert(p_id, tex);
  79. }
  80. }
  81. float blerp(float c00, float c10, float c01, float c11, float tx, float ty) {
  82. return Math::lerp(Math::lerp(c00, c10, tx), Math::lerp(c01, c11, tx), ty);
  83. }
  84. uint8_t LightmapRaycasterEmbree::AlphaTextureData::sample(float u, float v) const {
  85. float x = u * size.x;
  86. float y = v * size.y;
  87. int xi = (int)x;
  88. int yi = (int)y;
  89. uint8_t texels[4];
  90. for (int i = 0; i < 4; ++i) {
  91. int sample_x = CLAMP(xi + i % 2, 0, size.x - 1);
  92. int sample_y = CLAMP(yi + i / 2, 0, size.y - 1);
  93. texels[i] = data[sample_y * size.x + sample_x];
  94. }
  95. return Math::round(blerp(texels[0], texels[1], texels[2], texels[3], x - xi, y - yi));
  96. }
  97. void LightmapRaycasterEmbree::add_mesh(const Vector<Vector3> &p_vertices, const Vector<Vector3> &p_normals, const Vector<Vector2> &p_uv2s, unsigned int p_id) {
  98. RTCGeometry embree_mesh = rtcNewGeometry(embree_device, RTC_GEOMETRY_TYPE_TRIANGLE);
  99. rtcSetGeometryVertexAttributeCount(embree_mesh, 2);
  100. int vertex_count = p_vertices.size();
  101. ERR_FAIL_COND(vertex_count % 3 != 0);
  102. ERR_FAIL_COND(vertex_count != p_uv2s.size());
  103. ERR_FAIL_COND(!p_normals.is_empty() && vertex_count != p_normals.size());
  104. Vector3 *embree_vertices = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count);
  105. memcpy(embree_vertices, p_vertices.ptr(), sizeof(Vector3) * vertex_count);
  106. Vector2 *embree_light_uvs = (Vector2 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, RTC_FORMAT_FLOAT2, sizeof(Vector2), vertex_count);
  107. memcpy(embree_light_uvs, p_uv2s.ptr(), sizeof(Vector2) * vertex_count);
  108. uint32_t *embree_triangles = (uint32_t *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(uint32_t) * 3, vertex_count / 3);
  109. for (int i = 0; i < vertex_count; i++) {
  110. embree_triangles[i] = i;
  111. }
  112. if (!p_normals.is_empty()) {
  113. Vector3 *embree_normals = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count);
  114. memcpy(embree_normals, p_normals.ptr(), sizeof(Vector3) * vertex_count);
  115. }
  116. rtcCommitGeometry(embree_mesh);
  117. rtcSetGeometryIntersectFilterFunction(embree_mesh, filter_function);
  118. rtcSetGeometryUserData(embree_mesh, this);
  119. rtcAttachGeometryByID(embree_scene, embree_mesh, p_id);
  120. rtcReleaseGeometry(embree_mesh);
  121. }
  122. void LightmapRaycasterEmbree::commit() {
  123. rtcCommitScene(embree_scene);
  124. }
  125. void LightmapRaycasterEmbree::set_mesh_filter(const HashSet<int> &p_mesh_ids) {
  126. for (const int &E : p_mesh_ids) {
  127. rtcDisableGeometry(rtcGetGeometry(embree_scene, E));
  128. }
  129. rtcCommitScene(embree_scene);
  130. filter_meshes = p_mesh_ids;
  131. }
  132. void LightmapRaycasterEmbree::clear_mesh_filter() {
  133. for (const int &E : filter_meshes) {
  134. rtcEnableGeometry(rtcGetGeometry(embree_scene, E));
  135. }
  136. rtcCommitScene(embree_scene);
  137. filter_meshes.clear();
  138. }
  139. void embree_lm_error_handler(void *p_user_data, RTCError p_code, const char *p_str) {
  140. print_error("Embree error: " + String(p_str));
  141. }
  142. LightmapRaycasterEmbree::LightmapRaycasterEmbree() {
  143. #ifdef __SSE2__
  144. _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
  145. _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
  146. #endif
  147. embree_device = rtcNewDevice(nullptr);
  148. rtcSetDeviceErrorFunction(embree_device, &embree_lm_error_handler, nullptr);
  149. embree_scene = rtcNewScene(embree_device);
  150. }
  151. LightmapRaycasterEmbree::~LightmapRaycasterEmbree() {
  152. if (embree_scene != nullptr) {
  153. rtcReleaseScene(embree_scene);
  154. }
  155. if (embree_device != nullptr) {
  156. rtcReleaseDevice(embree_device);
  157. }
  158. }
  159. #endif // TOOLS_ENABLED