lightmap_raycaster.cpp 7.8 KB

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