lightmap_raycaster_embree.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. RTCIntersectContext context;
  60. rtcInitIntersectContext(&context);
  61. rtcIntersect1(embree_scene, &context, (RTCRayHit *)&r_ray);
  62. return r_ray.geomID != RTC_INVALID_GEOMETRY_ID;
  63. }
  64. void LightmapRaycasterEmbree::intersect(Vector<Ray> &r_rays) {
  65. Ray *rays = r_rays.ptrw();
  66. for (int i = 0; i < r_rays.size(); ++i) {
  67. intersect(rays[i]);
  68. }
  69. }
  70. void LightmapRaycasterEmbree::set_mesh_alpha_texture(Ref<Image> p_alpha_texture, unsigned int p_id) {
  71. if (p_alpha_texture.is_valid() && p_alpha_texture->get_size() != Vector2i()) {
  72. AlphaTextureData tex;
  73. tex.size = p_alpha_texture->get_size();
  74. tex.data = p_alpha_texture->get_data();
  75. alpha_textures.insert(p_id, tex);
  76. }
  77. }
  78. float blerp(float c00, float c10, float c01, float c11, float tx, float ty) {
  79. return Math::lerp(Math::lerp(c00, c10, tx), Math::lerp(c01, c11, tx), ty);
  80. }
  81. uint8_t LightmapRaycasterEmbree::AlphaTextureData::sample(float u, float v) const {
  82. float x = u * size.x;
  83. float y = v * size.y;
  84. int xi = (int)x;
  85. int yi = (int)y;
  86. uint8_t texels[4];
  87. for (int i = 0; i < 4; ++i) {
  88. int sample_x = CLAMP(xi + i % 2, 0, size.x - 1);
  89. int sample_y = CLAMP(yi + i / 2, 0, size.y - 1);
  90. texels[i] = data[sample_y * size.x + sample_x];
  91. }
  92. return Math::round(blerp(texels[0], texels[1], texels[2], texels[3], x - xi, y - yi));
  93. }
  94. void LightmapRaycasterEmbree::add_mesh(const Vector<Vector3> &p_vertices, const Vector<Vector3> &p_normals, const Vector<Vector2> &p_uv2s, unsigned int p_id) {
  95. RTCGeometry embree_mesh = rtcNewGeometry(embree_device, RTC_GEOMETRY_TYPE_TRIANGLE);
  96. rtcSetGeometryVertexAttributeCount(embree_mesh, 2);
  97. int vertex_count = p_vertices.size();
  98. ERR_FAIL_COND(vertex_count % 3 != 0);
  99. ERR_FAIL_COND(vertex_count != p_uv2s.size());
  100. ERR_FAIL_COND(!p_normals.is_empty() && vertex_count != p_normals.size());
  101. Vector3 *embree_vertices = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count);
  102. memcpy(embree_vertices, p_vertices.ptr(), sizeof(Vector3) * vertex_count);
  103. Vector2 *embree_light_uvs = (Vector2 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, RTC_FORMAT_FLOAT2, sizeof(Vector2), vertex_count);
  104. memcpy(embree_light_uvs, p_uv2s.ptr(), sizeof(Vector2) * vertex_count);
  105. uint32_t *embree_triangles = (uint32_t *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(uint32_t) * 3, vertex_count / 3);
  106. for (int i = 0; i < vertex_count; i++) {
  107. embree_triangles[i] = i;
  108. }
  109. if (!p_normals.is_empty()) {
  110. Vector3 *embree_normals = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count);
  111. memcpy(embree_normals, p_normals.ptr(), sizeof(Vector3) * vertex_count);
  112. }
  113. rtcCommitGeometry(embree_mesh);
  114. rtcSetGeometryIntersectFilterFunction(embree_mesh, filter_function);
  115. rtcSetGeometryUserData(embree_mesh, this);
  116. rtcAttachGeometryByID(embree_scene, embree_mesh, p_id);
  117. rtcReleaseGeometry(embree_mesh);
  118. }
  119. void LightmapRaycasterEmbree::commit() {
  120. rtcCommitScene(embree_scene);
  121. }
  122. void LightmapRaycasterEmbree::set_mesh_filter(const HashSet<int> &p_mesh_ids) {
  123. for (const int &E : p_mesh_ids) {
  124. rtcDisableGeometry(rtcGetGeometry(embree_scene, E));
  125. }
  126. rtcCommitScene(embree_scene);
  127. filter_meshes = p_mesh_ids;
  128. }
  129. void LightmapRaycasterEmbree::clear_mesh_filter() {
  130. for (const int &E : filter_meshes) {
  131. rtcEnableGeometry(rtcGetGeometry(embree_scene, E));
  132. }
  133. rtcCommitScene(embree_scene);
  134. filter_meshes.clear();
  135. }
  136. void embree_lm_error_handler(void *p_user_data, RTCError p_code, const char *p_str) {
  137. print_error("Embree error: " + String(p_str));
  138. }
  139. LightmapRaycasterEmbree::LightmapRaycasterEmbree() {
  140. #ifdef __SSE2__
  141. _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
  142. _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
  143. #endif
  144. embree_device = rtcNewDevice(nullptr);
  145. rtcSetDeviceErrorFunction(embree_device, &embree_lm_error_handler, nullptr);
  146. embree_scene = rtcNewScene(embree_device);
  147. }
  148. LightmapRaycasterEmbree::~LightmapRaycasterEmbree() {
  149. if (embree_scene != nullptr) {
  150. rtcReleaseScene(embree_scene);
  151. }
  152. if (embree_device != nullptr) {
  153. rtcReleaseDevice(embree_device);
  154. }
  155. }
  156. #endif // TOOLS_ENABLED