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