From 49fcf4ffad4172c2e9dd2249222b628e420c3cc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Mon, 26 Sep 2022 13:03:44 +0200 Subject: Style: Cleanup header guards for consistency Fix file names for {Static,Lightmap}RaycasterEmbree. --- core/math/convex_hull.h | 6 +- core/string/ustring.h | 4 +- core/templates/pooled_list.h | 5 +- editor/debugger/editor_debugger_tree.h | 4 +- misc/scripts/header_guards.sh | 17 ++ modules/gltf/register_types.h | 5 + modules/mono/utils/macos_utils.h | 4 +- modules/raycast/lightmap_raycaster.cpp | 196 --------------------- modules/raycast/lightmap_raycaster.h | 77 -------- modules/raycast/lightmap_raycaster_embree.cpp | 196 +++++++++++++++++++++ modules/raycast/lightmap_raycaster_embree.h | 82 +++++++++ modules/raycast/register_types.cpp | 4 +- modules/raycast/register_types.h | 5 + modules/raycast/static_raycaster.cpp | 137 -------------- modules/raycast/static_raycaster.h | 64 ------- modules/raycast/static_raycaster_embree.cpp | 137 ++++++++++++++ modules/raycast/static_raycaster_embree.h | 69 ++++++++ platform/android/java_godot_io_wrapper.h | 3 - platform/android/java_godot_wrapper.h | 3 - platform/ios/os_ios.h | 6 +- platform/linuxbsd/detect_prime_x11.h | 8 +- platform/macos/export/codesign.h | 6 +- platform/macos/export/lipo.h | 4 +- platform/macos/export/macho.h | 4 +- platform/macos/export/plist.h | 4 +- scene/animation/easing_equations.h | 6 +- scene/resources/particle_process_material.h | 6 +- scene/resources/sky_material.h | 6 +- .../physics_3d/joints/godot_cone_twist_joint_3d.h | 6 +- .../joints/godot_generic_6dof_joint_3d.h | 6 +- servers/physics_3d/joints/godot_hinge_joint_3d.h | 6 +- .../physics_3d/joints/godot_jacobian_entry_3d.h | 6 +- servers/physics_3d/joints/godot_pin_joint_3d.h | 6 +- servers/physics_3d/joints/godot_slider_joint_3d.h | 6 +- 34 files changed, 570 insertions(+), 534 deletions(-) delete mode 100644 modules/raycast/lightmap_raycaster.cpp delete mode 100644 modules/raycast/lightmap_raycaster.h create mode 100644 modules/raycast/lightmap_raycaster_embree.cpp create mode 100644 modules/raycast/lightmap_raycaster_embree.h delete mode 100644 modules/raycast/static_raycaster.cpp delete mode 100644 modules/raycast/static_raycaster.h create mode 100644 modules/raycast/static_raycaster_embree.cpp create mode 100644 modules/raycast/static_raycaster_embree.h diff --git a/core/math/convex_hull.h b/core/math/convex_hull.h index bd86fe0eba..cc41a794bd 100644 --- a/core/math/convex_hull.h +++ b/core/math/convex_hull.h @@ -28,6 +28,9 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifndef CONVEX_HULL_H +#define CONVEX_HULL_H + /* Copyright (c) 2011 Ole Kniemeyer, MAXON, www.maxon.net This software is provided 'as-is', without any express or implied warranty. @@ -40,9 +43,6 @@ subject to the following restrictions: 3. This notice may not be removed or altered from any source distribution. */ -#ifndef CONVEX_HULL_H -#define CONVEX_HULL_H - #include "core/math/geometry_3d.h" #include "core/math/vector3.h" #include "core/templates/local_vector.h" diff --git a/core/string/ustring.h b/core/string/ustring.h index b8ae3c2392..4b6568a502 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -28,11 +28,11 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -// Note: _GODOT suffix added to avoid conflict with ICU header with the same guard. - #ifndef USTRING_GODOT_H #define USTRING_GODOT_H +// Note: _GODOT suffix added to header guard to avoid conflict with ICU header. + #include "core/string/char_utils.h" #include "core/templates/cowdata.h" #include "core/templates/vector.h" diff --git a/core/templates/pooled_list.h b/core/templates/pooled_list.h index f13156b292..08f53572e7 100644 --- a/core/templates/pooled_list.h +++ b/core/templates/pooled_list.h @@ -28,7 +28,8 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#pragma once +#ifndef POOLED_LIST_H +#define POOLED_LIST_H // Simple template to provide a pool with O(1) allocate and free. // The freelist could alternatively be a linked list placed within the unused elements @@ -206,3 +207,5 @@ private: LocalVector _active_map; LocalVector _active_list; }; + +#endif // POOLED_LIST_H diff --git a/editor/debugger/editor_debugger_tree.h b/editor/debugger/editor_debugger_tree.h index 5b2df8abd5..5af3a0d84a 100644 --- a/editor/debugger/editor_debugger_tree.h +++ b/editor/debugger/editor_debugger_tree.h @@ -28,11 +28,11 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#include "scene/gui/tree.h" - #ifndef EDITOR_DEBUGGER_TREE_H #define EDITOR_DEBUGGER_TREE_H +#include "scene/gui/tree.h" + class SceneDebuggerTree; class EditorFileDialog; diff --git a/misc/scripts/header_guards.sh b/misc/scripts/header_guards.sh index 9a830f3ad2..9fdc864f8c 100755 --- a/misc/scripts/header_guards.sh +++ b/misc/scripts/header_guards.sh @@ -5,11 +5,15 @@ if [ ! -f "version.py" ]; then echo "Some of the paths checks may not work as intended from a different folder." fi +files_invalid_guard="" + for file in $(find -name "thirdparty" -prune -o -name "*.h" -print); do # Skip *.gen.h and *-so_wrap.h, they're generated. if [[ "$file" == *".gen.h" || "$file" == *"-so_wrap.h" ]]; then continue; fi # Has important define before normal header guards. if [[ "$file" == *"thread.h" || "$file" == *"platform_config.h" ]]; then continue; fi + # Obj-C files don't use header guards. + if grep -q "#import " "$file"; then continue; fi bname=$(basename $file .h) @@ -43,8 +47,21 @@ for file in $(find -name "thirdparty" -prune -o -name "*.h" -print); do sed -i $file -e "$ s/#endif.*/\n#endif \/\/ $guard/" # Removes redundant \n added before, if they weren't needed. sed -i $file -e "/^$/N;/^\n$/D" + + # Check that first ifndef (should be header guard) is at the expected position. + # If not it can mean we have some code before the guard that should be after. + # "31" is the expected line with the copyright header. + first_ifndef=$(grep -n -m 1 "ifndef" $file | sed 's/\([0-9]*\).*/\1/') + if [[ "$first_ifndef" != "31" ]]; then + files_invalid_guard+="$file\n" + fi done +if [[ ! -z "$files_invalid_guard" ]]; then + echo -e "The following files were found to have potentially invalid header guard:\n" + echo -e "$files_invalid_guard" +fi + diff=$(git diff --color) # If no diff has been generated all is OK, clean up, and exit. diff --git a/modules/gltf/register_types.h b/modules/gltf/register_types.h index 90b9a83c88..bf2730d2ef 100644 --- a/modules/gltf/register_types.h +++ b/modules/gltf/register_types.h @@ -28,7 +28,12 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifndef GLTF_REGISTER_TYPES_H +#define GLTF_REGISTER_TYPES_H + #include "modules/register_module_types.h" void initialize_gltf_module(ModuleInitializationLevel p_level); void uninitialize_gltf_module(ModuleInitializationLevel p_level); + +#endif // GLTF_REGISTER_TYPES_H diff --git a/modules/mono/utils/macos_utils.h b/modules/mono/utils/macos_utils.h index ca4957f5a7..0b74114685 100644 --- a/modules/mono/utils/macos_utils.h +++ b/modules/mono/utils/macos_utils.h @@ -28,13 +28,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#include "core/string/ustring.h" - #ifndef MONO_MACOS_UTILS_H #define MONO_MACOS_UTILS_H #ifdef MACOS_ENABLED +#include "core/string/ustring.h" + bool macos_is_app_bundle_installed(const String &p_bundle_id); #endif diff --git a/modules/raycast/lightmap_raycaster.cpp b/modules/raycast/lightmap_raycaster.cpp deleted file mode 100644 index 9b35b5616e..0000000000 --- a/modules/raycast/lightmap_raycaster.cpp +++ /dev/null @@ -1,196 +0,0 @@ -/*************************************************************************/ -/* lightmap_raycaster.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifdef TOOLS_ENABLED - -#include "lightmap_raycaster.h" - -#ifdef __SSE2__ -#include -#endif - -LightmapRaycaster *LightmapRaycasterEmbree::create_embree_raycaster() { - return memnew(LightmapRaycasterEmbree); -} - -void LightmapRaycasterEmbree::make_default_raycaster() { - create_function = create_embree_raycaster; -} - -void LightmapRaycasterEmbree::filter_function(const struct RTCFilterFunctionNArguments *p_args) { - RTCHit *hit = (RTCHit *)p_args->hit; - - unsigned int geomID = hit->geomID; - float u = hit->u; - float v = hit->v; - - LightmapRaycasterEmbree *scene = (LightmapRaycasterEmbree *)p_args->geometryUserPtr; - RTCGeometry geom = rtcGetGeometry(scene->embree_scene, geomID); - - rtcInterpolate0(geom, hit->primID, hit->u, hit->v, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, &hit->u, 2); - - if (scene->alpha_textures.has(geomID)) { - const AlphaTextureData &alpha_texture = scene->alpha_textures[geomID]; - - if (alpha_texture.sample(hit->u, hit->v) < 128) { - p_args->valid[0] = 0; - return; - } - } - - rtcInterpolate0(geom, hit->primID, u, v, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, &hit->Ng_x, 3); -} - -bool LightmapRaycasterEmbree::intersect(Ray &r_ray) { - RTCIntersectContext context; - - rtcInitIntersectContext(&context); - - rtcIntersect1(embree_scene, &context, (RTCRayHit *)&r_ray); - return r_ray.geomID != RTC_INVALID_GEOMETRY_ID; -} - -void LightmapRaycasterEmbree::intersect(Vector &r_rays) { - Ray *rays = r_rays.ptrw(); - for (int i = 0; i < r_rays.size(); ++i) { - intersect(rays[i]); - } -} - -void LightmapRaycasterEmbree::set_mesh_alpha_texture(Ref p_alpha_texture, unsigned int p_id) { - if (p_alpha_texture.is_valid() && p_alpha_texture->get_size() != Vector2i()) { - AlphaTextureData tex; - tex.size = p_alpha_texture->get_size(); - tex.data = p_alpha_texture->get_data(); - alpha_textures.insert(p_id, tex); - } -} - -float blerp(float c00, float c10, float c01, float c11, float tx, float ty) { - return Math::lerp(Math::lerp(c00, c10, tx), Math::lerp(c01, c11, tx), ty); -} - -uint8_t LightmapRaycasterEmbree::AlphaTextureData::sample(float u, float v) const { - float x = u * size.x; - float y = v * size.y; - int xi = (int)x; - int yi = (int)y; - - uint8_t texels[4]; - - for (int i = 0; i < 4; ++i) { - int sample_x = CLAMP(xi + i % 2, 0, size.x - 1); - int sample_y = CLAMP(yi + i / 2, 0, size.y - 1); - texels[i] = data[sample_y * size.x + sample_x]; - } - - return Math::round(blerp(texels[0], texels[1], texels[2], texels[3], x - xi, y - yi)); -} - -void LightmapRaycasterEmbree::add_mesh(const Vector &p_vertices, const Vector &p_normals, const Vector &p_uv2s, unsigned int p_id) { - RTCGeometry embree_mesh = rtcNewGeometry(embree_device, RTC_GEOMETRY_TYPE_TRIANGLE); - - rtcSetGeometryVertexAttributeCount(embree_mesh, 2); - - int vertex_count = p_vertices.size(); - - ERR_FAIL_COND(vertex_count % 3 != 0); - ERR_FAIL_COND(vertex_count != p_uv2s.size()); - ERR_FAIL_COND(!p_normals.is_empty() && vertex_count != p_normals.size()); - - Vector3 *embree_vertices = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count); - memcpy(embree_vertices, p_vertices.ptr(), sizeof(Vector3) * vertex_count); - - Vector2 *embree_light_uvs = (Vector2 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, RTC_FORMAT_FLOAT2, sizeof(Vector2), vertex_count); - memcpy(embree_light_uvs, p_uv2s.ptr(), sizeof(Vector2) * vertex_count); - - uint32_t *embree_triangles = (uint32_t *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(uint32_t) * 3, vertex_count / 3); - for (int i = 0; i < vertex_count; i++) { - embree_triangles[i] = i; - } - - if (!p_normals.is_empty()) { - Vector3 *embree_normals = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count); - memcpy(embree_normals, p_normals.ptr(), sizeof(Vector3) * vertex_count); - } - - rtcCommitGeometry(embree_mesh); - rtcSetGeometryIntersectFilterFunction(embree_mesh, filter_function); - rtcSetGeometryUserData(embree_mesh, this); - rtcAttachGeometryByID(embree_scene, embree_mesh, p_id); - rtcReleaseGeometry(embree_mesh); -} - -void LightmapRaycasterEmbree::commit() { - rtcCommitScene(embree_scene); -} - -void LightmapRaycasterEmbree::set_mesh_filter(const HashSet &p_mesh_ids) { - for (const int &E : p_mesh_ids) { - rtcDisableGeometry(rtcGetGeometry(embree_scene, E)); - } - rtcCommitScene(embree_scene); - filter_meshes = p_mesh_ids; -} - -void LightmapRaycasterEmbree::clear_mesh_filter() { - for (const int &E : filter_meshes) { - rtcEnableGeometry(rtcGetGeometry(embree_scene, E)); - } - rtcCommitScene(embree_scene); - filter_meshes.clear(); -} - -void embree_lm_error_handler(void *p_user_data, RTCError p_code, const char *p_str) { - print_error("Embree error: " + String(p_str)); -} - -LightmapRaycasterEmbree::LightmapRaycasterEmbree() { -#ifdef __SSE2__ - _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); - _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); -#endif - - embree_device = rtcNewDevice(nullptr); - rtcSetDeviceErrorFunction(embree_device, &embree_lm_error_handler, nullptr); - embree_scene = rtcNewScene(embree_device); -} - -LightmapRaycasterEmbree::~LightmapRaycasterEmbree() { - if (embree_scene != nullptr) { - rtcReleaseScene(embree_scene); - } - - if (embree_device != nullptr) { - rtcReleaseDevice(embree_device); - } -} - -#endif diff --git a/modules/raycast/lightmap_raycaster.h b/modules/raycast/lightmap_raycaster.h deleted file mode 100644 index 2e9f59dda4..0000000000 --- a/modules/raycast/lightmap_raycaster.h +++ /dev/null @@ -1,77 +0,0 @@ -/*************************************************************************/ -/* lightmap_raycaster.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifdef TOOLS_ENABLED - -#include "core/io/image.h" -#include "core/object/object.h" -#include "scene/3d/lightmapper.h" - -#include - -class LightmapRaycasterEmbree : public LightmapRaycaster { - GDCLASS(LightmapRaycasterEmbree, LightmapRaycaster); - -private: - struct AlphaTextureData { - Vector data; - Vector2i size; - - uint8_t sample(float u, float v) const; - }; - - RTCDevice embree_device; - RTCScene embree_scene; - - static void filter_function(const struct RTCFilterFunctionNArguments *p_args); - - HashMap alpha_textures; - HashSet filter_meshes; - -public: - virtual bool intersect(Ray &p_ray) override; - - virtual void intersect(Vector &r_rays) override; - - virtual void add_mesh(const Vector &p_vertices, const Vector &p_normals, const Vector &p_uv2s, unsigned int p_id) override; - virtual void set_mesh_alpha_texture(Ref p_alpha_texture, unsigned int p_id) override; - virtual void commit() override; - - virtual void set_mesh_filter(const HashSet &p_mesh_ids) override; - virtual void clear_mesh_filter() override; - - static LightmapRaycaster *create_embree_raycaster(); - static void make_default_raycaster(); - - LightmapRaycasterEmbree(); - ~LightmapRaycasterEmbree(); -}; - -#endif // LIGHTMAP_RAYCASTER_H diff --git a/modules/raycast/lightmap_raycaster_embree.cpp b/modules/raycast/lightmap_raycaster_embree.cpp new file mode 100644 index 0000000000..e6a579bd3a --- /dev/null +++ b/modules/raycast/lightmap_raycaster_embree.cpp @@ -0,0 +1,196 @@ +/*************************************************************************/ +/* lightmap_raycaster_embree.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifdef TOOLS_ENABLED + +#include "lightmap_raycaster_embree.h" + +#ifdef __SSE2__ +#include +#endif + +LightmapRaycaster *LightmapRaycasterEmbree::create_embree_raycaster() { + return memnew(LightmapRaycasterEmbree); +} + +void LightmapRaycasterEmbree::make_default_raycaster() { + create_function = create_embree_raycaster; +} + +void LightmapRaycasterEmbree::filter_function(const struct RTCFilterFunctionNArguments *p_args) { + RTCHit *hit = (RTCHit *)p_args->hit; + + unsigned int geomID = hit->geomID; + float u = hit->u; + float v = hit->v; + + LightmapRaycasterEmbree *scene = (LightmapRaycasterEmbree *)p_args->geometryUserPtr; + RTCGeometry geom = rtcGetGeometry(scene->embree_scene, geomID); + + rtcInterpolate0(geom, hit->primID, hit->u, hit->v, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, &hit->u, 2); + + if (scene->alpha_textures.has(geomID)) { + const AlphaTextureData &alpha_texture = scene->alpha_textures[geomID]; + + if (alpha_texture.sample(hit->u, hit->v) < 128) { + p_args->valid[0] = 0; + return; + } + } + + rtcInterpolate0(geom, hit->primID, u, v, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, &hit->Ng_x, 3); +} + +bool LightmapRaycasterEmbree::intersect(Ray &r_ray) { + RTCIntersectContext context; + + rtcInitIntersectContext(&context); + + rtcIntersect1(embree_scene, &context, (RTCRayHit *)&r_ray); + return r_ray.geomID != RTC_INVALID_GEOMETRY_ID; +} + +void LightmapRaycasterEmbree::intersect(Vector &r_rays) { + Ray *rays = r_rays.ptrw(); + for (int i = 0; i < r_rays.size(); ++i) { + intersect(rays[i]); + } +} + +void LightmapRaycasterEmbree::set_mesh_alpha_texture(Ref p_alpha_texture, unsigned int p_id) { + if (p_alpha_texture.is_valid() && p_alpha_texture->get_size() != Vector2i()) { + AlphaTextureData tex; + tex.size = p_alpha_texture->get_size(); + tex.data = p_alpha_texture->get_data(); + alpha_textures.insert(p_id, tex); + } +} + +float blerp(float c00, float c10, float c01, float c11, float tx, float ty) { + return Math::lerp(Math::lerp(c00, c10, tx), Math::lerp(c01, c11, tx), ty); +} + +uint8_t LightmapRaycasterEmbree::AlphaTextureData::sample(float u, float v) const { + float x = u * size.x; + float y = v * size.y; + int xi = (int)x; + int yi = (int)y; + + uint8_t texels[4]; + + for (int i = 0; i < 4; ++i) { + int sample_x = CLAMP(xi + i % 2, 0, size.x - 1); + int sample_y = CLAMP(yi + i / 2, 0, size.y - 1); + texels[i] = data[sample_y * size.x + sample_x]; + } + + return Math::round(blerp(texels[0], texels[1], texels[2], texels[3], x - xi, y - yi)); +} + +void LightmapRaycasterEmbree::add_mesh(const Vector &p_vertices, const Vector &p_normals, const Vector &p_uv2s, unsigned int p_id) { + RTCGeometry embree_mesh = rtcNewGeometry(embree_device, RTC_GEOMETRY_TYPE_TRIANGLE); + + rtcSetGeometryVertexAttributeCount(embree_mesh, 2); + + int vertex_count = p_vertices.size(); + + ERR_FAIL_COND(vertex_count % 3 != 0); + ERR_FAIL_COND(vertex_count != p_uv2s.size()); + ERR_FAIL_COND(!p_normals.is_empty() && vertex_count != p_normals.size()); + + Vector3 *embree_vertices = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count); + memcpy(embree_vertices, p_vertices.ptr(), sizeof(Vector3) * vertex_count); + + Vector2 *embree_light_uvs = (Vector2 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, RTC_FORMAT_FLOAT2, sizeof(Vector2), vertex_count); + memcpy(embree_light_uvs, p_uv2s.ptr(), sizeof(Vector2) * vertex_count); + + uint32_t *embree_triangles = (uint32_t *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(uint32_t) * 3, vertex_count / 3); + for (int i = 0; i < vertex_count; i++) { + embree_triangles[i] = i; + } + + if (!p_normals.is_empty()) { + Vector3 *embree_normals = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count); + memcpy(embree_normals, p_normals.ptr(), sizeof(Vector3) * vertex_count); + } + + rtcCommitGeometry(embree_mesh); + rtcSetGeometryIntersectFilterFunction(embree_mesh, filter_function); + rtcSetGeometryUserData(embree_mesh, this); + rtcAttachGeometryByID(embree_scene, embree_mesh, p_id); + rtcReleaseGeometry(embree_mesh); +} + +void LightmapRaycasterEmbree::commit() { + rtcCommitScene(embree_scene); +} + +void LightmapRaycasterEmbree::set_mesh_filter(const HashSet &p_mesh_ids) { + for (const int &E : p_mesh_ids) { + rtcDisableGeometry(rtcGetGeometry(embree_scene, E)); + } + rtcCommitScene(embree_scene); + filter_meshes = p_mesh_ids; +} + +void LightmapRaycasterEmbree::clear_mesh_filter() { + for (const int &E : filter_meshes) { + rtcEnableGeometry(rtcGetGeometry(embree_scene, E)); + } + rtcCommitScene(embree_scene); + filter_meshes.clear(); +} + +void embree_lm_error_handler(void *p_user_data, RTCError p_code, const char *p_str) { + print_error("Embree error: " + String(p_str)); +} + +LightmapRaycasterEmbree::LightmapRaycasterEmbree() { +#ifdef __SSE2__ + _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); + _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); +#endif + + embree_device = rtcNewDevice(nullptr); + rtcSetDeviceErrorFunction(embree_device, &embree_lm_error_handler, nullptr); + embree_scene = rtcNewScene(embree_device); +} + +LightmapRaycasterEmbree::~LightmapRaycasterEmbree() { + if (embree_scene != nullptr) { + rtcReleaseScene(embree_scene); + } + + if (embree_device != nullptr) { + rtcReleaseDevice(embree_device); + } +} + +#endif // TOOLS_ENABLED diff --git a/modules/raycast/lightmap_raycaster_embree.h b/modules/raycast/lightmap_raycaster_embree.h new file mode 100644 index 0000000000..0c3371f07c --- /dev/null +++ b/modules/raycast/lightmap_raycaster_embree.h @@ -0,0 +1,82 @@ +/*************************************************************************/ +/* lightmap_raycaster_embree.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef LIGHTMAP_RAYCASTER_EMBREE_H +#define LIGHTMAP_RAYCASTER_EMBREE_H + +#ifdef TOOLS_ENABLED + +#include "core/io/image.h" +#include "core/object/object.h" +#include "scene/3d/lightmapper.h" + +#include + +class LightmapRaycasterEmbree : public LightmapRaycaster { + GDCLASS(LightmapRaycasterEmbree, LightmapRaycaster); + +private: + struct AlphaTextureData { + Vector data; + Vector2i size; + + uint8_t sample(float u, float v) const; + }; + + RTCDevice embree_device; + RTCScene embree_scene; + + static void filter_function(const struct RTCFilterFunctionNArguments *p_args); + + HashMap alpha_textures; + HashSet filter_meshes; + +public: + virtual bool intersect(Ray &p_ray) override; + + virtual void intersect(Vector &r_rays) override; + + virtual void add_mesh(const Vector &p_vertices, const Vector &p_normals, const Vector &p_uv2s, unsigned int p_id) override; + virtual void set_mesh_alpha_texture(Ref p_alpha_texture, unsigned int p_id) override; + virtual void commit() override; + + virtual void set_mesh_filter(const HashSet &p_mesh_ids) override; + virtual void clear_mesh_filter() override; + + static LightmapRaycaster *create_embree_raycaster(); + static void make_default_raycaster(); + + LightmapRaycasterEmbree(); + ~LightmapRaycasterEmbree(); +}; + +#endif // TOOLS_ENABLED + +#endif // LIGHTMAP_RAYCASTER_EMBREE_H diff --git a/modules/raycast/register_types.cpp b/modules/raycast/register_types.cpp index 42de1d971d..a8380b00ba 100644 --- a/modules/raycast/register_types.cpp +++ b/modules/raycast/register_types.cpp @@ -30,9 +30,9 @@ #include "register_types.h" -#include "lightmap_raycaster.h" +#include "lightmap_raycaster_embree.h" #include "raycast_occlusion_cull.h" -#include "static_raycaster.h" +#include "static_raycaster_embree.h" RaycastOcclusionCull *raycast_occlusion_cull = nullptr; diff --git a/modules/raycast/register_types.h b/modules/raycast/register_types.h index a917285390..25a6c346b9 100644 --- a/modules/raycast/register_types.h +++ b/modules/raycast/register_types.h @@ -28,7 +28,12 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifndef RAYCAST_REGISTER_TYPES_H +#define RAYCAST_REGISTER_TYPES_H + #include "modules/register_module_types.h" void initialize_raycast_module(ModuleInitializationLevel p_level); void uninitialize_raycast_module(ModuleInitializationLevel p_level); + +#endif // RAYCAST_REGISTER_TYPES_H diff --git a/modules/raycast/static_raycaster.cpp b/modules/raycast/static_raycaster.cpp deleted file mode 100644 index 7659eea27f..0000000000 --- a/modules/raycast/static_raycaster.cpp +++ /dev/null @@ -1,137 +0,0 @@ -/*************************************************************************/ -/* static_raycaster.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifdef TOOLS_ENABLED - -#include "static_raycaster.h" - -#ifdef __SSE2__ -#include -#endif - -RTCDevice StaticRaycasterEmbree::embree_device; - -StaticRaycaster *StaticRaycasterEmbree::create_embree_raycaster() { - return memnew(StaticRaycasterEmbree); -} - -void StaticRaycasterEmbree::make_default_raycaster() { - create_function = create_embree_raycaster; -} - -void StaticRaycasterEmbree::free() { - if (embree_device) { - rtcReleaseDevice(embree_device); - } -} - -bool StaticRaycasterEmbree::intersect(Ray &r_ray) { - RTCIntersectContext context; - rtcInitIntersectContext(&context); - rtcIntersect1(embree_scene, &context, (RTCRayHit *)&r_ray); - return r_ray.geomID != RTC_INVALID_GEOMETRY_ID; -} - -void StaticRaycasterEmbree::intersect(Vector &r_rays) { - Ray *rays = r_rays.ptrw(); - for (int i = 0; i < r_rays.size(); ++i) { - intersect(rays[i]); - } -} - -void StaticRaycasterEmbree::add_mesh(const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices, unsigned int p_id) { - RTCGeometry embree_mesh = rtcNewGeometry(embree_device, RTC_GEOMETRY_TYPE_TRIANGLE); - - int vertex_count = p_vertices.size(); - - Vector3 *embree_vertices = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count); - memcpy(embree_vertices, p_vertices.ptr(), sizeof(Vector3) * vertex_count); - - if (p_indices.is_empty()) { - ERR_FAIL_COND(vertex_count % 3 != 0); - uint32_t *embree_triangles = (uint32_t *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(uint32_t) * 3, vertex_count / 3); - for (int i = 0; i < vertex_count; i++) { - embree_triangles[i] = i; - } - } else { - 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); - memcpy(embree_triangles, p_indices.ptr(), sizeof(uint32_t) * p_indices.size()); - } - - rtcCommitGeometry(embree_mesh); - rtcAttachGeometryByID(embree_scene, embree_mesh, p_id); - rtcReleaseGeometry(embree_mesh); -} - -void StaticRaycasterEmbree::commit() { - rtcCommitScene(embree_scene); -} - -void StaticRaycasterEmbree::set_mesh_filter(const HashSet &p_mesh_ids) { - for (const int &E : p_mesh_ids) { - rtcDisableGeometry(rtcGetGeometry(embree_scene, E)); - } - rtcCommitScene(embree_scene); - filter_meshes = p_mesh_ids; -} - -void StaticRaycasterEmbree::clear_mesh_filter() { - for (const int &E : filter_meshes) { - rtcEnableGeometry(rtcGetGeometry(embree_scene, E)); - } - rtcCommitScene(embree_scene); - filter_meshes.clear(); -} - -void embree_error_handler(void *p_user_data, RTCError p_code, const char *p_str) { - print_error("Embree error: " + String(p_str)); -} - -StaticRaycasterEmbree::StaticRaycasterEmbree() { -#ifdef __SSE2__ - _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); - _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); -#endif - - if (!embree_device) { - embree_device = rtcNewDevice(nullptr); - rtcSetDeviceErrorFunction(embree_device, &embree_error_handler, nullptr); - } - - embree_scene = rtcNewScene(embree_device); -} - -StaticRaycasterEmbree::~StaticRaycasterEmbree() { - if (embree_scene != nullptr) { - rtcReleaseScene(embree_scene); - } -} - -#endif diff --git a/modules/raycast/static_raycaster.h b/modules/raycast/static_raycaster.h deleted file mode 100644 index 607a392683..0000000000 --- a/modules/raycast/static_raycaster.h +++ /dev/null @@ -1,64 +0,0 @@ -/*************************************************************************/ -/* static_raycaster.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifdef TOOLS_ENABLED - -#include "core/math/static_raycaster.h" - -#include - -class StaticRaycasterEmbree : public StaticRaycaster { - GDCLASS(StaticRaycasterEmbree, StaticRaycaster); - -private: - static RTCDevice embree_device; - RTCScene embree_scene; - - HashSet filter_meshes; - -public: - virtual bool intersect(Ray &p_ray) override; - virtual void intersect(Vector &r_rays) override; - - virtual void add_mesh(const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices, unsigned int p_id) override; - virtual void commit() override; - - virtual void set_mesh_filter(const HashSet &p_mesh_ids) override; - virtual void clear_mesh_filter() override; - - static StaticRaycaster *create_embree_raycaster(); - static void make_default_raycaster(); - static void free(); - - StaticRaycasterEmbree(); - ~StaticRaycasterEmbree(); -}; - -#endif // STATIC_RAYCASTER_H diff --git a/modules/raycast/static_raycaster_embree.cpp b/modules/raycast/static_raycaster_embree.cpp new file mode 100644 index 0000000000..b5a4ab42d4 --- /dev/null +++ b/modules/raycast/static_raycaster_embree.cpp @@ -0,0 +1,137 @@ +/*************************************************************************/ +/* static_raycaster_embree.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifdef TOOLS_ENABLED + +#include "static_raycaster_embree.h" + +#ifdef __SSE2__ +#include +#endif + +RTCDevice StaticRaycasterEmbree::embree_device; + +StaticRaycaster *StaticRaycasterEmbree::create_embree_raycaster() { + return memnew(StaticRaycasterEmbree); +} + +void StaticRaycasterEmbree::make_default_raycaster() { + create_function = create_embree_raycaster; +} + +void StaticRaycasterEmbree::free() { + if (embree_device) { + rtcReleaseDevice(embree_device); + } +} + +bool StaticRaycasterEmbree::intersect(Ray &r_ray) { + RTCIntersectContext context; + rtcInitIntersectContext(&context); + rtcIntersect1(embree_scene, &context, (RTCRayHit *)&r_ray); + return r_ray.geomID != RTC_INVALID_GEOMETRY_ID; +} + +void StaticRaycasterEmbree::intersect(Vector &r_rays) { + Ray *rays = r_rays.ptrw(); + for (int i = 0; i < r_rays.size(); ++i) { + intersect(rays[i]); + } +} + +void StaticRaycasterEmbree::add_mesh(const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices, unsigned int p_id) { + RTCGeometry embree_mesh = rtcNewGeometry(embree_device, RTC_GEOMETRY_TYPE_TRIANGLE); + + int vertex_count = p_vertices.size(); + + Vector3 *embree_vertices = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count); + memcpy(embree_vertices, p_vertices.ptr(), sizeof(Vector3) * vertex_count); + + if (p_indices.is_empty()) { + ERR_FAIL_COND(vertex_count % 3 != 0); + uint32_t *embree_triangles = (uint32_t *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(uint32_t) * 3, vertex_count / 3); + for (int i = 0; i < vertex_count; i++) { + embree_triangles[i] = i; + } + } else { + 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); + memcpy(embree_triangles, p_indices.ptr(), sizeof(uint32_t) * p_indices.size()); + } + + rtcCommitGeometry(embree_mesh); + rtcAttachGeometryByID(embree_scene, embree_mesh, p_id); + rtcReleaseGeometry(embree_mesh); +} + +void StaticRaycasterEmbree::commit() { + rtcCommitScene(embree_scene); +} + +void StaticRaycasterEmbree::set_mesh_filter(const HashSet &p_mesh_ids) { + for (const int &E : p_mesh_ids) { + rtcDisableGeometry(rtcGetGeometry(embree_scene, E)); + } + rtcCommitScene(embree_scene); + filter_meshes = p_mesh_ids; +} + +void StaticRaycasterEmbree::clear_mesh_filter() { + for (const int &E : filter_meshes) { + rtcEnableGeometry(rtcGetGeometry(embree_scene, E)); + } + rtcCommitScene(embree_scene); + filter_meshes.clear(); +} + +void embree_error_handler(void *p_user_data, RTCError p_code, const char *p_str) { + print_error("Embree error: " + String(p_str)); +} + +StaticRaycasterEmbree::StaticRaycasterEmbree() { +#ifdef __SSE2__ + _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); + _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); +#endif + + if (!embree_device) { + embree_device = rtcNewDevice(nullptr); + rtcSetDeviceErrorFunction(embree_device, &embree_error_handler, nullptr); + } + + embree_scene = rtcNewScene(embree_device); +} + +StaticRaycasterEmbree::~StaticRaycasterEmbree() { + if (embree_scene != nullptr) { + rtcReleaseScene(embree_scene); + } +} + +#endif // TOOLS_ENABLED diff --git a/modules/raycast/static_raycaster_embree.h b/modules/raycast/static_raycaster_embree.h new file mode 100644 index 0000000000..4d631e3ca0 --- /dev/null +++ b/modules/raycast/static_raycaster_embree.h @@ -0,0 +1,69 @@ +/*************************************************************************/ +/* static_raycaster_embree.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef STATIC_RAYCASTER_EMBREE_H +#define STATIC_RAYCASTER_EMBREE_H + +#ifdef TOOLS_ENABLED + +#include "core/math/static_raycaster.h" + +#include + +class StaticRaycasterEmbree : public StaticRaycaster { + GDCLASS(StaticRaycasterEmbree, StaticRaycaster); + +private: + static RTCDevice embree_device; + RTCScene embree_scene; + + HashSet filter_meshes; + +public: + virtual bool intersect(Ray &p_ray) override; + virtual void intersect(Vector &r_rays) override; + + virtual void add_mesh(const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices, unsigned int p_id) override; + virtual void commit() override; + + virtual void set_mesh_filter(const HashSet &p_mesh_ids) override; + virtual void clear_mesh_filter() override; + + static StaticRaycaster *create_embree_raycaster(); + static void make_default_raycaster(); + static void free(); + + StaticRaycasterEmbree(); + ~StaticRaycasterEmbree(); +}; + +#endif // TOOLS_ENABLED + +#endif // STATIC_RAYCASTER_EMBREE_H diff --git a/platform/android/java_godot_io_wrapper.h b/platform/android/java_godot_io_wrapper.h index 9a1a877b6f..24995147d4 100644 --- a/platform/android/java_godot_io_wrapper.h +++ b/platform/android/java_godot_io_wrapper.h @@ -28,9 +28,6 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -// note, swapped java and godot around in the file name so all the java -// wrappers are together - #ifndef JAVA_GODOT_IO_WRAPPER_H #define JAVA_GODOT_IO_WRAPPER_H diff --git a/platform/android/java_godot_wrapper.h b/platform/android/java_godot_wrapper.h index a6c7853107..0e96a4e1f3 100644 --- a/platform/android/java_godot_wrapper.h +++ b/platform/android/java_godot_wrapper.h @@ -28,9 +28,6 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -// note, swapped java and godot around in the file name so all the java -// wrappers are together - #ifndef JAVA_GODOT_WRAPPER_H #define JAVA_GODOT_WRAPPER_H diff --git a/platform/ios/os_ios.h b/platform/ios/os_ios.h index 00d91da771..400040875f 100644 --- a/platform/ios/os_ios.h +++ b/platform/ios/os_ios.h @@ -28,11 +28,11 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifdef IOS_ENABLED - #ifndef OS_IOS_H #define OS_IOS_H +#ifdef IOS_ENABLED + #include "drivers/coreaudio/audio_driver_coreaudio.h" #include "drivers/unix/os_unix.h" #include "ios.h" @@ -124,6 +124,6 @@ public: void on_focus_in(); }; -#endif // OS_IOS_H +#endif // IOS_ENABLED #endif // OS_IOS_H diff --git a/platform/linuxbsd/detect_prime_x11.h b/platform/linuxbsd/detect_prime_x11.h index 21ebaead32..7eb7064cc5 100644 --- a/platform/linuxbsd/detect_prime_x11.h +++ b/platform/linuxbsd/detect_prime_x11.h @@ -28,11 +28,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifdef X11_ENABLED -#if defined(GLES3_ENABLED) +#ifndef DETECT_PRIME_X11_H +#define DETECT_PRIME_X11_H + +#if defined(X11_ENABLED) && defined(GLES3_ENABLED) int detect_prime(); -#endif +#endif // X11_ENABLED && GLES3_ENABLED #endif // DETECT_PRIME_X11_H diff --git a/platform/macos/export/codesign.h b/platform/macos/export/codesign.h index fea7b117d0..01e97bca81 100644 --- a/platform/macos/export/codesign.h +++ b/platform/macos/export/codesign.h @@ -28,6 +28,9 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifndef MACOS_CODESIGN_H +#define MACOS_CODESIGN_H + // macOS code signature creation utility. // // Current implementation has the following limitation: @@ -38,9 +41,6 @@ // - Requirements code generator is not implemented (only hard-coded requirements for the ad-hoc signing is supported). // - RFC5652/CMS blob generation is not implemented, supports ad-hoc signing only. -#ifndef MACOS_CODESIGN_H -#define MACOS_CODESIGN_H - #include "core/crypto/crypto_core.h" #include "core/io/dir_access.h" #include "core/io/file_access.h" diff --git a/platform/macos/export/lipo.h b/platform/macos/export/lipo.h index 516ef99860..6378f9899c 100644 --- a/platform/macos/export/lipo.h +++ b/platform/macos/export/lipo.h @@ -28,11 +28,11 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -// Universal / Universal 2 fat binary file creator and extractor. - #ifndef MACOS_LIPO_H #define MACOS_LIPO_H +// Universal / Universal 2 fat binary file creator and extractor. + #include "core/io/file_access.h" #include "core/object/ref_counted.h" #include "modules/modules_enabled.gen.h" // For regex. diff --git a/platform/macos/export/macho.h b/platform/macos/export/macho.h index 7ef0d9067e..0d42a7013f 100644 --- a/platform/macos/export/macho.h +++ b/platform/macos/export/macho.h @@ -28,11 +28,11 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -// Mach-O binary object file format parser and editor. - #ifndef MACOS_MACHO_H #define MACOS_MACHO_H +// Mach-O binary object file format parser and editor. + #include "core/crypto/crypto.h" #include "core/crypto/crypto_core.h" #include "core/io/file_access.h" diff --git a/platform/macos/export/plist.h b/platform/macos/export/plist.h index 79cb928d0a..b3c51a9635 100644 --- a/platform/macos/export/plist.h +++ b/platform/macos/export/plist.h @@ -28,11 +28,11 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -// Property list file format (application/x-plist) parser, property list ASN-1 serialization. - #ifndef MACOS_PLIST_H #define MACOS_PLIST_H +// Property list file format (application/x-plist) parser, property list ASN-1 serialization. + #include "core/crypto/crypto_core.h" #include "core/io/file_access.h" #include "modules/modules_enabled.gen.h" // For regex. diff --git a/scene/animation/easing_equations.h b/scene/animation/easing_equations.h index 094829e406..03d9e16454 100644 --- a/scene/animation/easing_equations.h +++ b/scene/animation/easing_equations.h @@ -28,6 +28,9 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifndef EASING_EQUATIONS_H +#define EASING_EQUATIONS_H + /* * Derived from Robert Penner's easing equations: http://robertpenner.com/easing/ * @@ -52,9 +55,6 @@ * SOFTWARE. */ -#ifndef EASING_EQUATIONS_H -#define EASING_EQUATIONS_H - namespace linear { static real_t in(real_t t, real_t b, real_t c, real_t d) { return c * t / d + b; diff --git a/scene/resources/particle_process_material.h b/scene/resources/particle_process_material.h index fe4741d6e5..9430e5797d 100644 --- a/scene/resources/particle_process_material.h +++ b/scene/resources/particle_process_material.h @@ -28,12 +28,12 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#include "core/templates/rid.h" -#include "scene/resources/material.h" - #ifndef PARTICLE_PROCESS_MATERIAL_H #define PARTICLE_PROCESS_MATERIAL_H +#include "core/templates/rid.h" +#include "scene/resources/material.h" + /* TODO: -Path following diff --git a/scene/resources/sky_material.h b/scene/resources/sky_material.h index fbb202d8d8..3de1a4b26f 100644 --- a/scene/resources/sky_material.h +++ b/scene/resources/sky_material.h @@ -28,12 +28,12 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#include "core/templates/rid.h" -#include "scene/resources/material.h" - #ifndef SKY_MATERIAL_H #define SKY_MATERIAL_H +#include "core/templates/rid.h" +#include "scene/resources/material.h" + class ProceduralSkyMaterial : public Material { GDCLASS(ProceduralSkyMaterial, Material); diff --git a/servers/physics_3d/joints/godot_cone_twist_joint_3d.h b/servers/physics_3d/joints/godot_cone_twist_joint_3d.h index fdcc2ceea3..1651d34916 100644 --- a/servers/physics_3d/joints/godot_cone_twist_joint_3d.h +++ b/servers/physics_3d/joints/godot_cone_twist_joint_3d.h @@ -28,6 +28,9 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifndef GODOT_CONE_TWIST_JOINT_3D_H +#define GODOT_CONE_TWIST_JOINT_3D_H + /* Adapted to Godot from the Bullet library. */ @@ -49,9 +52,6 @@ subject to the following restrictions: Written by: Marcus Hennix */ -#ifndef GODOT_CONE_TWIST_JOINT_3D_H -#define GODOT_CONE_TWIST_JOINT_3D_H - #include "servers/physics_3d/godot_joint_3d.h" #include "servers/physics_3d/joints/godot_jacobian_entry_3d.h" diff --git a/servers/physics_3d/joints/godot_generic_6dof_joint_3d.h b/servers/physics_3d/joints/godot_generic_6dof_joint_3d.h index bcf2d18647..6a0ab81461 100644 --- a/servers/physics_3d/joints/godot_generic_6dof_joint_3d.h +++ b/servers/physics_3d/joints/godot_generic_6dof_joint_3d.h @@ -28,13 +28,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifndef GODOT_GENERIC_6DOF_JOINT_3D_H +#define GODOT_GENERIC_6DOF_JOINT_3D_H + /* Adapted to Godot from the Bullet library. */ -#ifndef GODOT_GENERIC_6DOF_JOINT_3D_H -#define GODOT_GENERIC_6DOF_JOINT_3D_H - #include "servers/physics_3d/godot_joint_3d.h" #include "servers/physics_3d/joints/godot_jacobian_entry_3d.h" diff --git a/servers/physics_3d/joints/godot_hinge_joint_3d.h b/servers/physics_3d/joints/godot_hinge_joint_3d.h index b934540e8d..f1187771af 100644 --- a/servers/physics_3d/joints/godot_hinge_joint_3d.h +++ b/servers/physics_3d/joints/godot_hinge_joint_3d.h @@ -28,13 +28,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifndef GODOT_HINGE_JOINT_3D_H +#define GODOT_HINGE_JOINT_3D_H + /* Adapted to Godot from the Bullet library. */ -#ifndef GODOT_HINGE_JOINT_3D_H -#define GODOT_HINGE_JOINT_3D_H - #include "servers/physics_3d/godot_joint_3d.h" #include "servers/physics_3d/joints/godot_jacobian_entry_3d.h" diff --git a/servers/physics_3d/joints/godot_jacobian_entry_3d.h b/servers/physics_3d/joints/godot_jacobian_entry_3d.h index 0fe15751d5..a46ce830ec 100644 --- a/servers/physics_3d/joints/godot_jacobian_entry_3d.h +++ b/servers/physics_3d/joints/godot_jacobian_entry_3d.h @@ -28,13 +28,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifndef GODOT_JACOBIAN_ENTRY_3D_H +#define GODOT_JACOBIAN_ENTRY_3D_H + /* Adapted to Godot from the Bullet library. */ -#ifndef GODOT_JACOBIAN_ENTRY_3D_H -#define GODOT_JACOBIAN_ENTRY_3D_H - /* Bullet Continuous Collision Detection and Physics Library Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/ diff --git a/servers/physics_3d/joints/godot_pin_joint_3d.h b/servers/physics_3d/joints/godot_pin_joint_3d.h index eeeaa650bd..b3e2389d5a 100644 --- a/servers/physics_3d/joints/godot_pin_joint_3d.h +++ b/servers/physics_3d/joints/godot_pin_joint_3d.h @@ -28,13 +28,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifndef GODOT_PIN_JOINT_3D_H +#define GODOT_PIN_JOINT_3D_H + /* Adapted to Godot from the Bullet library. */ -#ifndef GODOT_PIN_JOINT_3D_H -#define GODOT_PIN_JOINT_3D_H - #include "servers/physics_3d/godot_joint_3d.h" #include "servers/physics_3d/joints/godot_jacobian_entry_3d.h" diff --git a/servers/physics_3d/joints/godot_slider_joint_3d.h b/servers/physics_3d/joints/godot_slider_joint_3d.h index f596c9ff75..29d19be0d7 100644 --- a/servers/physics_3d/joints/godot_slider_joint_3d.h +++ b/servers/physics_3d/joints/godot_slider_joint_3d.h @@ -28,13 +28,13 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#ifndef GODOT_SLIDER_JOINT_3D_H +#define GODOT_SLIDER_JOINT_3D_H + /* Adapted to Godot from the Bullet library. */ -#ifndef GODOT_SLIDER_JOINT_3D_H -#define GODOT_SLIDER_JOINT_3D_H - #include "servers/physics_3d/godot_joint_3d.h" #include "servers/physics_3d/joints/godot_jacobian_entry_3d.h" -- cgit v1.2.3