diff options
31 files changed, 885 insertions, 244 deletions
diff --git a/COPYRIGHT.txt b/COPYRIGHT.txt index 078aac1f13..3bb9c916fd 100644 --- a/COPYRIGHT.txt +++ b/COPYRIGHT.txt @@ -89,6 +89,8 @@ Files: ./servers/physics_3d/gjk_epa.cpp ./servers/physics_3d/joints/slider_joint_3d_sw.h ./servers/physics_3d/soft_body_3d_sw.cpp ./servers/physics_3d/soft_body_3d_sw.h + ./servers/physics_3d/shape_3d_sw.cpp + ./servers/physics_3d/shape_3d_sw.h Comment: Bullet Continuous Collision Detection and Physics Library Copyright: 2003-2008, Erwin Coumans 2007-2021, Juan Linietsky, Ariel Manzur. diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 61f3f7d82e..7f83e27dfe 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -143,36 +143,6 @@ static _FORCE_INLINE_ void vc_ptrcall(void (T::*method)(P...) const, void *p_bas } template <class R, class T, class... P> -static _FORCE_INLINE_ void vc_change_return_type(R (T::*method)(P...), Variant *v) { - VariantTypeAdjust<R>::adjust(v); -} - -template <class R, class T, class... P> -static _FORCE_INLINE_ void vc_change_return_type(R (T::*method)(P...) const, Variant *v) { - VariantTypeAdjust<R>::adjust(v); -} - -template <class T, class... P> -static _FORCE_INLINE_ void vc_change_return_type(void (T::*method)(P...), Variant *v) { - VariantInternal::clear(v); -} - -template <class T, class... P> -static _FORCE_INLINE_ void vc_change_return_type(void (T::*method)(P...) const, Variant *v) { - VariantInternal::clear(v); -} - -template <class R, class... P> -static _FORCE_INLINE_ void vc_change_return_type(R (*method)(P...), Variant *v) { - VariantTypeAdjust<R>::adjust(v); -} - -template <class... P> -static _FORCE_INLINE_ void vc_change_return_type(void (*method)(P...), Variant *v) { - VariantInternal::clear(v); -} - -template <class R, class T, class... P> static _FORCE_INLINE_ int vc_get_argument_count(R (T::*method)(P...)) { return sizeof...(P); } @@ -333,7 +303,6 @@ static _FORCE_INLINE_ Variant::Type vc_get_base_type(void (T::*method)(P...) con vc_method_call(m_method_ptr, base, p_args, p_argcount, r_ret, p_defvals, r_error); \ } \ static void validated_call(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret) { \ - vc_change_return_type(m_method_ptr, r_ret); \ vc_validated_call(m_method_ptr, base, p_args, r_ret); \ } \ static void ptrcall(void *p_base, const void **p_args, void *r_ret, int p_argcount) { \ @@ -384,7 +353,6 @@ static _FORCE_INLINE_ void vc_static_ptrcall(void (*method)(P...), const void ** vc_static_method_call(m_method_ptr, p_args, p_argcount, r_ret, p_defvals, r_error); \ } \ static void validated_call(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret) { \ - vc_change_return_type(m_method_ptr, r_ret); \ vc_validated_static_call(m_method_ptr, p_args, r_ret); \ } \ static void ptrcall(void *p_base, const void **p_args, void *r_ret, int p_argcount) { \ @@ -435,7 +403,6 @@ static _FORCE_INLINE_ void vc_ptrcall(void (*method)(T *, P...), void *p_base, c vc_method_call_static(m_method_ptr, base, p_args, p_argcount, r_ret, p_defvals, r_error); \ } \ static void validated_call(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret) { \ - vc_change_return_type(m_method_ptr, r_ret); \ vc_validated_call_static(m_method_ptr, base, p_args, r_ret); \ } \ static void ptrcall(void *p_base, const void **p_args, void *r_ret, int p_argcount) { \ diff --git a/doc/classes/AStar.xml b/doc/classes/AStar.xml index e975b8ed28..533ecbd279 100644 --- a/doc/classes/AStar.xml +++ b/doc/classes/AStar.xml @@ -289,6 +289,7 @@ </argument> <description> Returns an array with the points that are in the path found by AStar between the given points. The array is ordered from the starting point to the ending point of the path. + [b]Note:[/b] This method is not thread-safe. If called from a [Thread], it will return an empty [PackedVector2Array] and will print an error message. </description> </method> <method name="get_point_position" qualifiers="const"> diff --git a/doc/classes/AStar2D.xml b/doc/classes/AStar2D.xml index 2a51678209..3efd2f604c 100644 --- a/doc/classes/AStar2D.xml +++ b/doc/classes/AStar2D.xml @@ -258,6 +258,7 @@ </argument> <description> Returns an array with the points that are in the path found by AStar2D between the given points. The array is ordered from the starting point to the ending point of the path. + [b]Note:[/b] This method is not thread-safe. If called from a [Thread], it will return an empty [PackedVector2Array] and will print an error message. </description> </method> <method name="get_point_position" qualifiers="const"> diff --git a/doc/classes/AnimationNodeTimeSeek.xml b/doc/classes/AnimationNodeTimeSeek.xml index eb5335c792..171d65fbe0 100644 --- a/doc/classes/AnimationNodeTimeSeek.xml +++ b/doc/classes/AnimationNodeTimeSeek.xml @@ -4,7 +4,27 @@ A time-seeking animation node to be used with [AnimationTree]. </brief_description> <description> - This node can be used to cause a seek command to happen to any sub-children of the graph. After setting the time, this value returns to -1. + This node can be used to cause a seek command to happen to any sub-children of the animation graph. Use this node type to play an [Animation] from the start or a certain playback position inside the [AnimationNodeBlendTree]. After setting the time and changing the animation playback, the seek node automatically goes into sleep mode on the next process frame by setting its [code]seek_position[/code] value to [code]-1.0[/code]. + [codeblocks] + [gdscript] + # Play child animation from the start. + animation_tree.set("parameters/Seek/seek_position", 0.0) + # Alternative syntax (same result as above). + animation_tree["parameters/Seek/seek_position"] = 0.0 + + # Play child animation from 12 second timestamp. + animation_tree.set("parameters/Seek/seek_position", 12.0) + # Alternative syntax (same result as above). + animation_tree["parameters/Seek/seek_position"] = 12.0 + [/gdscript] + [csharp] + // Play child animation from the start. + animationTree.Set("parameters/Seek/seek_position", 0.0); + + // Play child animation from 12 second timestamp. + animationTree.Set("parameters/Seek/seek_position", 12.0); + [/csharp] + [/codeblocks] </description> <tutorials> <link title="AnimationTree">https://docs.godotengine.org/en/latest/tutorials/animation/animation_tree.html</link> diff --git a/doc/classes/Geometry2D.xml b/doc/classes/Geometry2D.xml index 2c0d9b54d1..13354ec19e 100644 --- a/doc/classes/Geometry2D.xml +++ b/doc/classes/Geometry2D.xml @@ -184,7 +184,7 @@ </argument> <description> Merges (combines) [code]polygon_a[/code] and [code]polygon_b[/code] and returns an array of merged polygons. This performs [constant OPERATION_UNION] between polygons. - The operation may result in an outer polygon (boundary) and inner polygon (hole) produced which could be distinguished by calling [method is_polygon_clockwise]. + The operation may result in an outer polygon (boundary) and multiple inner polygons (holes) produced which could be distinguished by calling [method is_polygon_clockwise]. </description> </method> <method name="offset_polygon"> diff --git a/doc/classes/HeightMapShape3D.xml b/doc/classes/HeightMapShape3D.xml index 6d230bdab8..f6f2a27891 100644 --- a/doc/classes/HeightMapShape3D.xml +++ b/doc/classes/HeightMapShape3D.xml @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8" ?> <class name="HeightMapShape3D" inherits="Shape3D" version="4.0"> <brief_description> - Height map shape for 3D physics (Bullet only). + Height map shape for 3D physics. </brief_description> <description> Height map shape resource, which can be added to a [PhysicsBody3D] or [Area3D]. diff --git a/doc/classes/VideoPlayer.xml b/doc/classes/VideoPlayer.xml index b2ab356b0d..d905ce4054 100644 --- a/doc/classes/VideoPlayer.xml +++ b/doc/classes/VideoPlayer.xml @@ -74,6 +74,7 @@ </member> <member name="stream_position" type="float" setter="set_stream_position" getter="get_stream_position"> The current position of the stream, in seconds. + [b]Note:[/b] Changing this value won't have any effect as seeking is not implemented yet, except in video formats implemented by a GDNative add-on. </member> <member name="volume" type="float" setter="set_volume" getter="get_volume"> Audio volume as a linear value. diff --git a/drivers/vulkan/vulkan_context.cpp b/drivers/vulkan/vulkan_context.cpp index 504e63392f..e759e53288 100644 --- a/drivers/vulkan/vulkan_context.cpp +++ b/drivers/vulkan/vulkan_context.cpp @@ -41,6 +41,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <vector> #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) #define APP_SHORT_NAME "GodotEngine" @@ -193,7 +194,7 @@ VKAPI_ATTR VkBool32 VKAPI_CALL VulkanContext::_debug_report_callback( return VK_FALSE; } -VkBool32 VulkanContext::_check_layers(uint32_t check_count, const char **check_names, uint32_t layer_count, VkLayerProperties *layers) { +VkBool32 VulkanContext::_check_layers(uint32_t check_count, const char *const *check_names, uint32_t layer_count, VkLayerProperties *layers) { for (uint32_t i = 0; i < check_count; i++) { VkBool32 found = 0; for (uint32_t j = 0; j < layer_count; j++) { @@ -210,57 +211,55 @@ VkBool32 VulkanContext::_check_layers(uint32_t check_count, const char **check_n return 1; } -Error VulkanContext::_create_validation_layers() { - VkResult err; - const char *instance_validation_layers_alt1[] = { "VK_LAYER_KHRONOS_validation" }; - const char *instance_validation_layers_alt2[] = { "VK_LAYER_LUNARG_standard_validation" }; - const char *instance_validation_layers_alt3[] = { "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation", "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation", "VK_LAYER_GOOGLE_unique_objects" }; +Error VulkanContext::_get_preferred_validation_layers(uint32_t *count, const char *const **names) { + static const std::vector<std::vector<const char *>> instance_validation_layers_alt{ + // Preferred set of validation layers + { "VK_LAYER_KHRONOS_validation" }, - uint32_t instance_layer_count = 0; - err = vkEnumerateInstanceLayerProperties(&instance_layer_count, nullptr); - ERR_FAIL_COND_V(err, ERR_CANT_CREATE); + // Alternative (deprecated, removed in SDK 1.1.126.0) set of validation layers + { "VK_LAYER_LUNARG_standard_validation" }, - VkBool32 validation_found = 0; - uint32_t validation_layer_count = 0; - const char **instance_validation_layers = nullptr; - if (instance_layer_count > 0) { - VkLayerProperties *instance_layers = (VkLayerProperties *)malloc(sizeof(VkLayerProperties) * instance_layer_count); - err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers); - if (err) { - free(instance_layers); - ERR_FAIL_V(ERR_CANT_CREATE); - } + // Alternative (deprecated, removed in SDK 1.1.121.1) set of validation layers + { "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation", "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation", "VK_LAYER_GOOGLE_unique_objects" } + }; - validation_layer_count = ARRAY_SIZE(instance_validation_layers_alt1); - instance_validation_layers = instance_validation_layers_alt1; - validation_found = _check_layers(validation_layer_count, instance_validation_layers, instance_layer_count, instance_layers); + // Clear out-arguments + *count = 0; + if (names != nullptr) { + *names = nullptr; + } - // use alternative (deprecated, removed in SDK 1.1.126.0) set of validation layers - if (!validation_found) { - validation_layer_count = ARRAY_SIZE(instance_validation_layers_alt2); - instance_validation_layers = instance_validation_layers_alt2; - validation_found = _check_layers(validation_layer_count, instance_validation_layers, instance_layer_count, instance_layers); - } + VkResult err; + uint32_t instance_layer_count; - // use alternative (deprecated, removed in SDK 1.1.121.1) set of validation layers - if (!validation_found) { - validation_layer_count = ARRAY_SIZE(instance_validation_layers_alt3); - instance_validation_layers = instance_validation_layers_alt3; - validation_found = _check_layers(validation_layer_count, instance_validation_layers, instance_layer_count, instance_layers); - } + err = vkEnumerateInstanceLayerProperties(&instance_layer_count, nullptr); + if (err) { + ERR_FAIL_V(ERR_CANT_CREATE); + } + + if (instance_layer_count < 1) { + return OK; + } + VkLayerProperties *instance_layers = (VkLayerProperties *)malloc(sizeof(VkLayerProperties) * instance_layer_count); + err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers); + if (err) { free(instance_layers); + ERR_FAIL_V(ERR_CANT_CREATE); } - if (validation_found) { - enabled_layer_count = validation_layer_count; - for (uint32_t i = 0; i < validation_layer_count; i++) { - enabled_layers[i] = instance_validation_layers[i]; + for (uint32_t i = 0; i < instance_validation_layers_alt.size(); i++) { + if (_check_layers(instance_validation_layers_alt[i].size(), instance_validation_layers_alt[i].data(), instance_layer_count, instance_layers)) { + *count = instance_validation_layers_alt[i].size(); + if (names != nullptr) { + *names = instance_validation_layers_alt[i].data(); + } + break; } - } else { - return ERR_CANT_CREATE; } + free(instance_layers); + return OK; } @@ -301,7 +300,6 @@ Error VulkanContext::_initialize_extensions() { uint32_t instance_extension_count = 0; enabled_extension_count = 0; - enabled_layer_count = 0; enabled_debug_utils = false; enabled_debug_report = false; /* Look for instance extensions */ @@ -330,7 +328,7 @@ Error VulkanContext::_initialize_extensions() { extension_names[enabled_extension_count++] = _get_platform_surface_extension(); } if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) { - if (use_validation_layers) { + if (_use_validation_layers()) { extension_names[enabled_extension_count++] = VK_EXT_DEBUG_REPORT_EXTENSION_NAME; enabled_debug_report = true; } @@ -542,11 +540,6 @@ Error VulkanContext::_create_physical_device() { /* obtain version */ _obtain_vulkan_version(); - /* Look for validation layers */ - if (use_validation_layers) { - _create_validation_layers(); - } - /* initialise extensions */ { Error err = _initialize_extensions(); @@ -567,16 +560,14 @@ Error VulkanContext::_create_physical_device() { /*engineVersion*/ 0, /*apiVersion*/ VK_MAKE_VERSION(vulkan_major, vulkan_minor, 0) }; - VkInstanceCreateInfo inst_info = { - /*sType*/ VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, - /*pNext*/ nullptr, - /*flags*/ 0, - /*pApplicationInfo*/ &app, - /*enabledLayerCount*/ enabled_layer_count, - /*ppEnabledLayerNames*/ (const char *const *)enabled_layers, - /*enabledExtensionCount*/ enabled_extension_count, - /*ppEnabledExtensionNames*/ (const char *const *)extension_names, - }; + VkInstanceCreateInfo inst_info{}; + inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; + inst_info.pApplicationInfo = &app; + inst_info.enabledExtensionCount = enabled_extension_count; + inst_info.ppEnabledExtensionNames = (const char *const *)extension_names; + if (_use_validation_layers()) { + _get_preferred_validation_layers(&inst_info.enabledLayerCount, &inst_info.ppEnabledLayerNames); + } /* * This is info for a temp callback to use during CreateInstance. @@ -1077,6 +1068,10 @@ Error VulkanContext::_create_semaphores() { return OK; } +bool VulkanContext::_use_validation_layers() { + return Engine::get_singleton()->is_validation_layers_enabled(); +} + Error VulkanContext::_window_create(DisplayServer::WindowID p_window_id, VkSurfaceKHR p_surface, int p_width, int p_height) { ERR_FAIL_COND_V(windows.has(p_window_id), ERR_INVALID_PARAMETER); @@ -2008,8 +2003,6 @@ String VulkanContext::get_device_pipeline_cache_uuid() const { } VulkanContext::VulkanContext() { - use_validation_layers = Engine::get_singleton()->is_validation_layers_enabled(); - command_buffer_queue.resize(1); // First one is always the setup command. command_buffer_queue.write[0] = nullptr; } diff --git a/drivers/vulkan/vulkan_context.h b/drivers/vulkan/vulkan_context.h index b788181ab9..88e4f26bb1 100644 --- a/drivers/vulkan/vulkan_context.h +++ b/drivers/vulkan/vulkan_context.h @@ -151,9 +151,6 @@ private: */ bool enabled_debug_report = false; - uint32_t enabled_layer_count = 0; - const char *enabled_layers[MAX_LAYERS]; - PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT; PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT; PFN_vkSubmitDebugUtilsMessageEXT SubmitDebugUtilsMessageEXT; @@ -180,11 +177,10 @@ private: VkDebugReportCallbackEXT dbg_debug_report = VK_NULL_HANDLE; Error _obtain_vulkan_version(); - Error _create_validation_layers(); Error _initialize_extensions(); Error _check_capabilities(); - VkBool32 _check_layers(uint32_t check_count, const char **check_names, uint32_t layer_count, VkLayerProperties *layers); + VkBool32 _check_layers(uint32_t check_count, const char *const *check_names, uint32_t layer_count, VkLayerProperties *layers); static VKAPI_ATTR VkBool32 VKAPI_CALL _debug_messenger_callback( VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageType, @@ -217,11 +213,12 @@ private: protected: virtual const char *_get_platform_surface_extension() const = 0; - // Enabled via command line argument. - bool use_validation_layers = false; - virtual Error _window_create(DisplayServer::WindowID p_window_id, VkSurfaceKHR p_surface, int p_width, int p_height); + virtual bool _use_validation_layers(); + + Error _get_preferred_validation_layers(uint32_t *count, const char *const **names); + VkInstance _get_instance() { return inst; } diff --git a/editor/project_settings_editor.cpp b/editor/project_settings_editor.cpp index de7996eaa2..faec3355ac 100644 --- a/editor/project_settings_editor.cpp +++ b/editor/project_settings_editor.cpp @@ -102,10 +102,9 @@ void ProjectSettingsEditor::_add_setting() { String setting = _get_setting_name(); // Initialize the property with the default value for the given type. - // The type list starts at 1 (as we exclude Nil), so add 1 to the selected value. Callable::CallError ce; Variant value; - Variant::construct(Variant::Type(type->get_selected() + 1), value, nullptr, 0, ce); + Variant::construct(Variant::Type(type->get_selected_id()), value, nullptr, 0, ce); undo_redo->create_action(TTR("Add Project Setting")); undo_redo->add_do_property(ps, setting, value); @@ -584,7 +583,7 @@ ProjectSettingsEditor::ProjectSettingsEditor(EditorData *p_data) { // There's no point in adding Nil types, and Object types // can't be serialized correctly in the project settings. if (i != Variant::NIL && i != Variant::OBJECT) { - type->add_item(Variant::get_type_name(Variant::Type(i))); + type->add_item(Variant::get_type_name(Variant::Type(i)), i); } } diff --git a/modules/bullet/shape_bullet.cpp b/modules/bullet/shape_bullet.cpp index 471b154813..40e785d699 100644 --- a/modules/bullet/shape_bullet.cpp +++ b/modules/bullet/shape_bullet.cpp @@ -142,7 +142,7 @@ btScaledBvhTriangleMeshShape *ShapeBullet::create_shape_concave(btBvhTriangleMes } } -btHeightfieldTerrainShape *ShapeBullet::create_shape_height_field(Vector<real_t> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height) { +btHeightfieldTerrainShape *ShapeBullet::create_shape_height_field(Vector<float> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height) { const btScalar ignoredHeightScale(1); const int YAxis = 1; // 0=X, 1=Y, 2=Z const bool flipQuadEdges = false; @@ -480,17 +480,10 @@ void HeightMapShapeBullet::set_data(const Variant &p_data) { ERR_FAIL_COND_MSG(l_width < 2, "Map width must be at least 2."); ERR_FAIL_COND_MSG(l_depth < 2, "Map depth must be at least 2."); - // TODO This code will need adjustments if real_t is set to `double`, - // because that precision is unnecessary for a heightmap and Bullet doesn't support it... - - Vector<real_t> l_heights; + Vector<float> l_heights; Variant l_heights_v = d["heights"]; -#ifdef REAL_T_IS_DOUBLE - if (l_heights_v.get_type() == Variant::PACKED_FLOAT64_ARRAY) { -#else if (l_heights_v.get_type() == Variant::PACKED_FLOAT32_ARRAY) { -#endif // Ready-to-use heights can be passed l_heights = l_heights_v; @@ -511,9 +504,9 @@ void HeightMapShapeBullet::set_data(const Variant &p_data) { l_heights.resize(l_image->get_width() * l_image->get_height()); - real_t *w = l_heights.ptrw(); + float *w = l_heights.ptrw(); const uint8_t *r = im_data.ptr(); - real_t *rp = (real_t *)r; + float *rp = (float *)r; // At this point, `rp` could be used directly for Bullet, but I don't know how safe it would be. for (int i = 0; i < l_heights.size(); ++i) { @@ -521,11 +514,7 @@ void HeightMapShapeBullet::set_data(const Variant &p_data) { } } else { -#ifdef REAL_T_IS_DOUBLE - ERR_FAIL_MSG("Expected PackedFloat64Array or float Image."); -#else ERR_FAIL_MSG("Expected PackedFloat32Array or float Image."); -#endif } ERR_FAIL_COND(l_width <= 0); @@ -534,11 +523,11 @@ void HeightMapShapeBullet::set_data(const Variant &p_data) { // Compute min and max heights if not specified. if (!d.has("min_height") && !d.has("max_height")) { - const real_t *r = l_heights.ptr(); + const float *r = l_heights.ptr(); int heights_size = l_heights.size(); for (int i = 0; i < heights_size; ++i) { - real_t h = r[i]; + float h = r[i]; if (h < l_min_height) { l_min_height = h; @@ -559,7 +548,7 @@ PhysicsServer3D::ShapeType HeightMapShapeBullet::get_type() const { return PhysicsServer3D::SHAPE_HEIGHTMAP; } -void HeightMapShapeBullet::setup(Vector<real_t> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height) { +void HeightMapShapeBullet::setup(Vector<float> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height) { // TODO cell size must be tweaked using localScaling, which is a shared property for all Bullet shapes // If this array is resized outside of here, it should be preserved due to CoW diff --git a/modules/bullet/shape_bullet.h b/modules/bullet/shape_bullet.h index bfd95747eb..5080d13d99 100644 --- a/modules/bullet/shape_bullet.h +++ b/modules/bullet/shape_bullet.h @@ -89,7 +89,7 @@ public: /// IMPORTANT: Remember to delete the shape interface by calling: delete my_shape->getMeshInterface(); static class btConvexPointCloudShape *create_shape_convex(btAlignedObjectArray<btVector3> &p_vertices, const btVector3 &p_local_scaling = btVector3(1, 1, 1)); static class btScaledBvhTriangleMeshShape *create_shape_concave(btBvhTriangleMeshShape *p_mesh_shape, const btVector3 &p_local_scaling = btVector3(1, 1, 1)); - static class btHeightfieldTerrainShape *create_shape_height_field(Vector<real_t> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height); + static class btHeightfieldTerrainShape *create_shape_height_field(Vector<float> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height); static class btRayShape *create_shape_ray(real_t p_length, bool p_slips_on_slope); }; @@ -212,7 +212,7 @@ private: class HeightMapShapeBullet : public ShapeBullet { public: - Vector<real_t> heights; + Vector<float> heights; int width = 0; int depth = 0; real_t min_height = 0.0; @@ -226,7 +226,7 @@ public: virtual btCollisionShape *create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge = 0); private: - void setup(Vector<real_t> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height); + void setup(Vector<float> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height); }; class RayShapeBullet : public ShapeBullet { diff --git a/modules/gdscript/gdscript_byte_codegen.cpp b/modules/gdscript/gdscript_byte_codegen.cpp index af2bfc33a7..89c5f5482b 100644 --- a/modules/gdscript/gdscript_byte_codegen.cpp +++ b/modules/gdscript/gdscript_byte_codegen.cpp @@ -47,7 +47,8 @@ uint32_t GDScriptByteCodeGenerator::add_parameter(const StringName &p_name, bool } uint32_t GDScriptByteCodeGenerator::add_local(const StringName &p_name, const GDScriptDataType &p_type) { - int stack_pos = increase_stack(); + int stack_pos = locals.size() + RESERVED_STACK; + locals.push_back(StackSlot(p_type.builtin_type)); add_stack_identifier(p_name, stack_pos); return stack_pos; } @@ -66,25 +67,87 @@ uint32_t GDScriptByteCodeGenerator::add_or_get_name(const StringName &p_name) { return get_name_map_pos(p_name); } -uint32_t GDScriptByteCodeGenerator::add_temporary() { - current_temporaries++; - int idx = increase_stack(); -#ifdef DEBUG_ENABLED - temp_stack.push_back(idx); -#endif - return idx; +uint32_t GDScriptByteCodeGenerator::add_temporary(const GDScriptDataType &p_type) { + Variant::Type temp_type = Variant::NIL; + if (p_type.has_type) { + if (p_type.kind == GDScriptDataType::BUILTIN) { + switch (p_type.builtin_type) { + case Variant::NIL: + case Variant::BOOL: + case Variant::INT: + case Variant::FLOAT: + case Variant::STRING: + case Variant::VECTOR2: + case Variant::VECTOR2I: + case Variant::RECT2: + case Variant::RECT2I: + case Variant::VECTOR3: + case Variant::VECTOR3I: + case Variant::TRANSFORM2D: + case Variant::PLANE: + case Variant::QUAT: + case Variant::AABB: + case Variant::BASIS: + case Variant::TRANSFORM: + case Variant::COLOR: + case Variant::STRING_NAME: + case Variant::NODE_PATH: + case Variant::RID: + case Variant::OBJECT: + case Variant::CALLABLE: + case Variant::SIGNAL: + case Variant::DICTIONARY: + case Variant::ARRAY: + temp_type = p_type.builtin_type; + break; + case Variant::PACKED_BYTE_ARRAY: + case Variant::PACKED_INT32_ARRAY: + case Variant::PACKED_INT64_ARRAY: + case Variant::PACKED_FLOAT32_ARRAY: + case Variant::PACKED_FLOAT64_ARRAY: + case Variant::PACKED_STRING_ARRAY: + case Variant::PACKED_VECTOR2_ARRAY: + case Variant::PACKED_VECTOR3_ARRAY: + case Variant::PACKED_COLOR_ARRAY: + case Variant::VARIANT_MAX: + // Packed arrays are reference counted, so we don't use the pool for them. + temp_type = Variant::NIL; + break; + } + } else { + temp_type = Variant::OBJECT; + } + } + + if (!temporaries_pool.has(temp_type)) { + temporaries_pool[temp_type] = List<int>(); + } + + List<int> &pool = temporaries_pool[temp_type]; + if (pool.is_empty()) { + StackSlot new_temp(temp_type); + int idx = temporaries.size(); + pool.push_back(idx); + temporaries.push_back(new_temp); + + // First time using this, so adjust to the proper type. + if (temp_type != Variant::NIL) { + Address addr(Address::TEMPORARY, idx, p_type); + write_type_adjust(addr, temp_type); + } + } + int slot = pool.front()->get(); + pool.pop_front(); + used_temporaries.push_back(slot); + return slot; } void GDScriptByteCodeGenerator::pop_temporary() { - ERR_FAIL_COND(current_temporaries == 0); - current_stack_size--; -#ifdef DEBUG_ENABLED - if (temp_stack.back()->get() != current_stack_size) { - ERR_PRINT("Mismatched popping of temporary value"); - } - temp_stack.pop_back(); -#endif - current_temporaries--; + ERR_FAIL_COND(used_temporaries.is_empty()); + int slot_idx = used_temporaries.back()->get(); + const StackSlot &slot = temporaries[slot_idx]; + temporaries_pool[slot.type].push_back(slot_idx); + used_temporaries.pop_back(); } void GDScriptByteCodeGenerator::start_parameters() { @@ -119,12 +182,18 @@ void GDScriptByteCodeGenerator::write_start(GDScript *p_script, const StringName GDScriptFunction *GDScriptByteCodeGenerator::write_end() { #ifdef DEBUG_ENABLED - if (current_temporaries != 0) { - ERR_PRINT("Non-zero temporary variables at end of function: " + itos(current_temporaries)); + if (!used_temporaries.is_empty()) { + ERR_PRINT("Non-zero temporary variables at end of function: " + itos(used_temporaries.size())); } #endif append(GDScriptFunction::OPCODE_END, 0); + for (int i = 0; i < temporaries.size(); i++) { + for (int j = 0; j < temporaries[i].bytecode_indices.size(); j++) { + opcodes.write[temporaries[i].bytecode_indices[j]] = (i + max_locals + RESERVED_STACK) | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); + } + } + if (constant_map.size()) { function->_constant_count = constant_map.size(); function->constants.resize(constant_map.size()); @@ -317,7 +386,7 @@ GDScriptFunction *GDScriptByteCodeGenerator::write_end() { if (debug_stack) { function->stack_debug = stack_debug; } - function->_stack_size = stack_max; + function->_stack_size = RESERVED_STACK + max_locals + temporaries.size(); function->_instruction_args_size = instr_args_max; function->_ptrcall_args_size = ptrcall_max; @@ -341,6 +410,117 @@ void GDScriptByteCodeGenerator::set_initial_line(int p_line) { #define IS_BUILTIN_TYPE(m_var, m_type) \ (m_var.type.has_type && m_var.type.kind == GDScriptDataType::BUILTIN && m_var.type.builtin_type == m_type) +void GDScriptByteCodeGenerator::write_type_adjust(const Address &p_target, Variant::Type p_new_type) { + switch (p_new_type) { + case Variant::BOOL: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_BOOL, 1); + break; + case Variant::INT: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_INT, 1); + break; + case Variant::FLOAT: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_FLOAT, 1); + break; + case Variant::STRING: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_STRING, 1); + break; + case Variant::VECTOR2: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR2, 1); + break; + case Variant::VECTOR2I: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR2I, 1); + break; + case Variant::RECT2: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_RECT2, 1); + break; + case Variant::RECT2I: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_RECT2I, 1); + break; + case Variant::VECTOR3: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3, 1); + break; + case Variant::VECTOR3I: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_VECTOR3I, 1); + break; + case Variant::TRANSFORM2D: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_TRANSFORM2D, 1); + break; + case Variant::PLANE: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_PLANE, 1); + break; + case Variant::QUAT: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_QUAT, 1); + break; + case Variant::AABB: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_AABB, 1); + break; + case Variant::BASIS: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_BASIS, 1); + break; + case Variant::TRANSFORM: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_TRANSFORM, 1); + break; + case Variant::COLOR: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_COLOR, 1); + break; + case Variant::STRING_NAME: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_STRING_NAME, 1); + break; + case Variant::NODE_PATH: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_NODE_PATH, 1); + break; + case Variant::RID: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_RID, 1); + break; + case Variant::OBJECT: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_OBJECT, 1); + break; + case Variant::CALLABLE: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_CALLABLE, 1); + break; + case Variant::SIGNAL: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_SIGNAL, 1); + break; + case Variant::DICTIONARY: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_DICTIONARY, 1); + break; + case Variant::ARRAY: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_ARRAY, 1); + break; + case Variant::PACKED_BYTE_ARRAY: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_BYTE_ARRAY, 1); + break; + case Variant::PACKED_INT32_ARRAY: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_INT32_ARRAY, 1); + break; + case Variant::PACKED_INT64_ARRAY: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_INT64_ARRAY, 1); + break; + case Variant::PACKED_FLOAT32_ARRAY: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_FLOAT32_ARRAY, 1); + break; + case Variant::PACKED_FLOAT64_ARRAY: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_FLOAT64_ARRAY, 1); + break; + case Variant::PACKED_STRING_ARRAY: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_STRING_ARRAY, 1); + break; + case Variant::PACKED_VECTOR2_ARRAY: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_VECTOR2_ARRAY, 1); + break; + case Variant::PACKED_VECTOR3_ARRAY: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_VECTOR3_ARRAY, 1); + break; + case Variant::PACKED_COLOR_ARRAY: + append(GDScriptFunction::OPCODE_TYPE_ADJUST_PACKED_COLOR_ARRAY, 1); + break; + case Variant::NIL: + case Variant::VARIANT_MAX: + return; + } + append(p_target); +} + void GDScriptByteCodeGenerator::write_unary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand) { if (HAS_BUILTIN_TYPE(p_left_operand)) { // Gather specific operator. @@ -806,6 +986,14 @@ void GDScriptByteCodeGenerator::write_call_builtin_type(const Address &p_target, return; } + if (p_target.mode == Address::TEMPORARY) { + Variant::Type result_type = Variant::get_builtin_method_return_type(p_type, p_method); + Variant::Type temp_type = temporaries[p_target.address].type; + if (result_type != temp_type) { + write_type_adjust(p_target, result_type); + } + } + append(GDScriptFunction::OPCODE_CALL_BUILTIN_TYPE_VALIDATED, 2 + p_arguments.size()); for (int i = 0; i < p_arguments.size(); i++) { diff --git a/modules/gdscript/gdscript_byte_codegen.h b/modules/gdscript/gdscript_byte_codegen.h index 4b196ed420..17d681d7bb 100644 --- a/modules/gdscript/gdscript_byte_codegen.h +++ b/modules/gdscript/gdscript_byte_codegen.h @@ -37,6 +37,17 @@ #include "gdscript_utility_functions.h" class GDScriptByteCodeGenerator : public GDScriptCodeGenerator { + struct StackSlot { + Variant::Type type = Variant::NIL; + Vector<int> bytecode_indices; + + StackSlot() = default; + StackSlot(Variant::Type p_type) : + type(p_type) {} + }; + + const static int RESERVED_STACK = 3; // For self, class, and nil. + bool ended = false; GDScriptFunction *function = nullptr; bool debug_stack = false; @@ -47,15 +58,17 @@ class GDScriptByteCodeGenerator : public GDScriptCodeGenerator { List<int> stack_identifiers_counts; Map<StringName, int> local_constants; + Vector<StackSlot> locals; + Vector<StackSlot> temporaries; + List<int> used_temporaries; + Map<Variant::Type, List<int>> temporaries_pool; + List<GDScriptFunction::StackDebug> stack_debug; List<Map<StringName, int>> block_identifier_stack; Map<StringName, int> block_identifiers; - int current_stack_size = 3; // First 3 spots are reserved for self, class, and nil. - int current_temporaries = 0; - int current_locals = 0; + int max_locals = 0; int current_line = 0; - int stack_max = 3; int instr_args_max = 0; int ptrcall_max = 0; @@ -102,7 +115,9 @@ class GDScriptByteCodeGenerator : public GDScriptCodeGenerator { List<List<int>> match_continues_to_patch; void add_stack_identifier(const StringName &p_id, int p_stackpos) { - current_locals++; + if (locals.size() > max_locals) { + max_locals = locals.size(); + } stack_identifiers[p_id] = p_stackpos; if (debug_stack) { block_identifiers[p_id] = p_stackpos; @@ -116,7 +131,7 @@ class GDScriptByteCodeGenerator : public GDScriptCodeGenerator { } void push_stack_identifiers() { - stack_identifiers_counts.push_back(current_locals); + stack_identifiers_counts.push_back(locals.size()); stack_id_stack.push_back(stack_identifiers); if (debug_stack) { Map<StringName, int> block_ids(block_identifiers); @@ -126,17 +141,16 @@ class GDScriptByteCodeGenerator : public GDScriptCodeGenerator { } void pop_stack_identifiers() { - current_locals = stack_identifiers_counts.back()->get(); + int current_locals = stack_identifiers_counts.back()->get(); stack_identifiers_counts.pop_back(); stack_identifiers = stack_id_stack.back()->get(); stack_id_stack.pop_back(); #ifdef DEBUG_ENABLED - if (current_temporaries != 0) { - ERR_PRINT("Leaving block with non-zero temporary variables: " + itos(current_temporaries)); + if (!used_temporaries.is_empty()) { + ERR_PRINT("Leaving block with non-zero temporary variables: " + itos(used_temporaries.size())); } #endif - current_stack_size = current_locals + 3; // Keep the 3 reserved slots for self, class, and nil. - + locals.resize(current_locals); if (debug_stack) { for (Map<StringName, int>::Element *E = block_identifiers.front(); E; E = E->next()) { GDScriptFunction::StackDebug sd; @@ -279,18 +293,6 @@ class GDScriptByteCodeGenerator : public GDScriptCodeGenerator { return pos; } - void alloc_stack(int p_level) { - if (p_level >= stack_max) { - stack_max = p_level + 1; - } - } - - int increase_stack() { - int top = current_stack_size++; - alloc_stack(current_stack_size); - return top; - } - void alloc_ptrcall(int p_params) { if (p_params >= ptrcall_max) { ptrcall_max = p_params; @@ -308,9 +310,11 @@ class GDScriptByteCodeGenerator : public GDScriptCodeGenerator { case Address::CONSTANT: return p_address.address | (GDScriptFunction::ADDR_TYPE_CONSTANT << GDScriptFunction::ADDR_BITS); case Address::LOCAL_VARIABLE: - case Address::TEMPORARY: case Address::FUNCTION_PARAMETER: return p_address.address | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS); + case Address::TEMPORARY: + temporaries.write[p_address.address].bytecode_indices.push_back(opcodes.size()); + return -1; case Address::NIL: return GDScriptFunction::ADDR_NIL; } @@ -392,7 +396,7 @@ public: virtual uint32_t add_local_constant(const StringName &p_name, const Variant &p_constant) override; virtual uint32_t add_or_get_constant(const Variant &p_constant) override; virtual uint32_t add_or_get_name(const StringName &p_name) override; - virtual uint32_t add_temporary() override; + virtual uint32_t add_temporary(const GDScriptDataType &p_type) override; virtual void pop_temporary() override; virtual void start_parameters() override; @@ -409,6 +413,7 @@ public: #endif virtual void set_initial_line(int p_line) override; + virtual void write_type_adjust(const Address &p_target, Variant::Type p_new_type) override; virtual void write_unary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand) override; virtual void write_binary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) override; virtual void write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) override; diff --git a/modules/gdscript/gdscript_codegen.h b/modules/gdscript/gdscript_codegen.h index cce4e856c7..b377beefdb 100644 --- a/modules/gdscript/gdscript_codegen.h +++ b/modules/gdscript/gdscript_codegen.h @@ -71,7 +71,7 @@ public: virtual uint32_t add_local_constant(const StringName &p_name, const Variant &p_constant) = 0; virtual uint32_t add_or_get_constant(const Variant &p_constant) = 0; virtual uint32_t add_or_get_name(const StringName &p_name) = 0; - virtual uint32_t add_temporary() = 0; + virtual uint32_t add_temporary(const GDScriptDataType &p_type) = 0; virtual void pop_temporary() = 0; virtual void start_parameters() = 0; @@ -80,9 +80,6 @@ public: virtual void start_block() = 0; virtual void end_block() = 0; - // virtual int get_max_stack_level() = 0; - // virtual int get_max_function_arguments() = 0; - virtual void write_start(GDScript *p_script, const StringName &p_function_name, bool p_static, MultiplayerAPI::RPCMode p_rpc_mode, const GDScriptDataType &p_return_type) = 0; virtual GDScriptFunction *write_end() = 0; @@ -91,9 +88,7 @@ public: #endif virtual void set_initial_line(int p_line) = 0; - // virtual void alloc_stack(int p_level) = 0; // Is this needed? - // virtual void alloc_call(int p_arg_count) = 0; // This might be automatic from other functions. - + virtual void write_type_adjust(const Address &p_target, Variant::Type p_new_type) = 0; virtual void write_unary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand) = 0; virtual void write_binary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) = 0; virtual void write_type_test(const Address &p_target, const Address &p_source, const Address &p_type) = 0; @@ -138,7 +133,6 @@ public: virtual void write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) = 0; virtual void write_await(const Address &p_target, const Address &p_operand) = 0; virtual void write_if(const Address &p_condition) = 0; - // virtual void write_elseif(const Address &p_condition) = 0; This kind of makes things more difficult for no real benefit. virtual void write_else() = 0; virtual void write_endif() = 0; virtual void start_for(const GDScriptDataType &p_iterator_type, const GDScriptDataType &p_list_type) = 0; diff --git a/modules/gdscript/gdscript_compiler.cpp b/modules/gdscript/gdscript_compiler.cpp index abbca899bd..7429e3cc0b 100644 --- a/modules/gdscript/gdscript_compiler.cpp +++ b/modules/gdscript/gdscript_compiler.cpp @@ -711,7 +711,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code case GDScriptParser::Node::UNARY_OPERATOR: { const GDScriptParser::UnaryOpNode *unary = static_cast<const GDScriptParser::UnaryOpNode *>(p_expression); - GDScriptCodeGenerator::Address result = codegen.add_temporary(); + GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(unary->get_datatype())); GDScriptCodeGenerator::Address operand = _parse_expression(codegen, r_error, unary->operand); if (r_error) { @@ -729,7 +729,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code case GDScriptParser::Node::BINARY_OPERATOR: { const GDScriptParser::BinaryOpNode *binary = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression); - GDScriptCodeGenerator::Address result = codegen.add_temporary(); + GDScriptCodeGenerator::Address result = codegen.add_temporary(_gdtype_from_datatype(binary->get_datatype())); switch (binary->operation) { case GDScriptParser::BinaryOpNode::OP_LOGIC_AND: { diff --git a/modules/gdscript/gdscript_compiler.h b/modules/gdscript/gdscript_compiler.h index 1b0beec0d4..c405eadb07 100644 --- a/modules/gdscript/gdscript_compiler.h +++ b/modules/gdscript/gdscript_compiler.h @@ -66,7 +66,7 @@ class GDScriptCompiler { } GDScriptCodeGenerator::Address add_temporary(const GDScriptDataType &p_type = GDScriptDataType()) { - uint32_t addr = generator->add_temporary(); + uint32_t addr = generator->add_temporary(p_type); return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::TEMPORARY, addr, p_type); } diff --git a/modules/gdscript/gdscript_disassembler.cpp b/modules/gdscript/gdscript_disassembler.cpp index 74da0ee232..0d0afcc741 100644 --- a/modules/gdscript/gdscript_disassembler.cpp +++ b/modules/gdscript/gdscript_disassembler.cpp @@ -894,6 +894,51 @@ void GDScriptFunction::disassemble(const Vector<String> &p_code_lines) const { incr += 2; } break; + +#define DISASSEMBLE_TYPE_ADJUST(m_v_type) \ + case OPCODE_TYPE_ADJUST_##m_v_type: { \ + text += "type adjust ("; \ + text += #m_v_type; \ + text += ") "; \ + text += DADDR(1); \ + incr += 2; \ + } break + + DISASSEMBLE_TYPE_ADJUST(BOOL); + DISASSEMBLE_TYPE_ADJUST(INT); + DISASSEMBLE_TYPE_ADJUST(FLOAT); + DISASSEMBLE_TYPE_ADJUST(STRING); + DISASSEMBLE_TYPE_ADJUST(VECTOR2); + DISASSEMBLE_TYPE_ADJUST(VECTOR2I); + DISASSEMBLE_TYPE_ADJUST(RECT2); + DISASSEMBLE_TYPE_ADJUST(RECT2I); + DISASSEMBLE_TYPE_ADJUST(VECTOR3); + DISASSEMBLE_TYPE_ADJUST(VECTOR3I); + DISASSEMBLE_TYPE_ADJUST(TRANSFORM2D); + DISASSEMBLE_TYPE_ADJUST(PLANE); + DISASSEMBLE_TYPE_ADJUST(QUAT); + DISASSEMBLE_TYPE_ADJUST(AABB); + DISASSEMBLE_TYPE_ADJUST(BASIS); + DISASSEMBLE_TYPE_ADJUST(TRANSFORM); + DISASSEMBLE_TYPE_ADJUST(COLOR); + DISASSEMBLE_TYPE_ADJUST(STRING_NAME); + DISASSEMBLE_TYPE_ADJUST(NODE_PATH); + DISASSEMBLE_TYPE_ADJUST(RID); + DISASSEMBLE_TYPE_ADJUST(OBJECT); + DISASSEMBLE_TYPE_ADJUST(CALLABLE); + DISASSEMBLE_TYPE_ADJUST(SIGNAL); + DISASSEMBLE_TYPE_ADJUST(DICTIONARY); + DISASSEMBLE_TYPE_ADJUST(ARRAY); + DISASSEMBLE_TYPE_ADJUST(PACKED_BYTE_ARRAY); + DISASSEMBLE_TYPE_ADJUST(PACKED_INT32_ARRAY); + DISASSEMBLE_TYPE_ADJUST(PACKED_INT64_ARRAY); + DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT32_ARRAY); + DISASSEMBLE_TYPE_ADJUST(PACKED_FLOAT64_ARRAY); + DISASSEMBLE_TYPE_ADJUST(PACKED_STRING_ARRAY); + DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR2_ARRAY); + DISASSEMBLE_TYPE_ADJUST(PACKED_VECTOR3_ARRAY); + DISASSEMBLE_TYPE_ADJUST(PACKED_COLOR_ARRAY); + case OPCODE_ASSERT: { text += "assert ("; text += DADDR(1); diff --git a/modules/gdscript/gdscript_function.h b/modules/gdscript/gdscript_function.h index 414dfab2e7..fbec734a28 100644 --- a/modules/gdscript/gdscript_function.h +++ b/modules/gdscript/gdscript_function.h @@ -351,6 +351,40 @@ public: OPCODE_ITERATE_PACKED_COLOR_ARRAY, OPCODE_ITERATE_OBJECT, OPCODE_STORE_NAMED_GLOBAL, + OPCODE_TYPE_ADJUST_BOOL, + OPCODE_TYPE_ADJUST_INT, + OPCODE_TYPE_ADJUST_FLOAT, + OPCODE_TYPE_ADJUST_STRING, + OPCODE_TYPE_ADJUST_VECTOR2, + OPCODE_TYPE_ADJUST_VECTOR2I, + OPCODE_TYPE_ADJUST_RECT2, + OPCODE_TYPE_ADJUST_RECT2I, + OPCODE_TYPE_ADJUST_VECTOR3, + OPCODE_TYPE_ADJUST_VECTOR3I, + OPCODE_TYPE_ADJUST_TRANSFORM2D, + OPCODE_TYPE_ADJUST_PLANE, + OPCODE_TYPE_ADJUST_QUAT, + OPCODE_TYPE_ADJUST_AABB, + OPCODE_TYPE_ADJUST_BASIS, + OPCODE_TYPE_ADJUST_TRANSFORM, + OPCODE_TYPE_ADJUST_COLOR, + OPCODE_TYPE_ADJUST_STRING_NAME, + OPCODE_TYPE_ADJUST_NODE_PATH, + OPCODE_TYPE_ADJUST_RID, + OPCODE_TYPE_ADJUST_OBJECT, + OPCODE_TYPE_ADJUST_CALLABLE, + OPCODE_TYPE_ADJUST_SIGNAL, + OPCODE_TYPE_ADJUST_DICTIONARY, + OPCODE_TYPE_ADJUST_ARRAY, + OPCODE_TYPE_ADJUST_PACKED_BYTE_ARRAY, + OPCODE_TYPE_ADJUST_PACKED_INT32_ARRAY, + OPCODE_TYPE_ADJUST_PACKED_INT64_ARRAY, + OPCODE_TYPE_ADJUST_PACKED_FLOAT32_ARRAY, + OPCODE_TYPE_ADJUST_PACKED_FLOAT64_ARRAY, + OPCODE_TYPE_ADJUST_PACKED_STRING_ARRAY, + OPCODE_TYPE_ADJUST_PACKED_VECTOR2_ARRAY, + OPCODE_TYPE_ADJUST_PACKED_VECTOR3_ARRAY, + OPCODE_TYPE_ADJUST_PACKED_COLOR_ARRAY, OPCODE_ASSERT, OPCODE_BREAKPOINT, OPCODE_LINE, diff --git a/modules/gdscript/gdscript_vm.cpp b/modules/gdscript/gdscript_vm.cpp index 8bf6a8b08b..b47a4eb992 100644 --- a/modules/gdscript/gdscript_vm.cpp +++ b/modules/gdscript/gdscript_vm.cpp @@ -282,6 +282,40 @@ String GDScriptFunction::_get_call_error(const Callable::CallError &p_err, const &&OPCODE_ITERATE_PACKED_COLOR_ARRAY, \ &&OPCODE_ITERATE_OBJECT, \ &&OPCODE_STORE_NAMED_GLOBAL, \ + &&OPCODE_TYPE_ADJUST_BOOL, \ + &&OPCODE_TYPE_ADJUST_INT, \ + &&OPCODE_TYPE_ADJUST_FLOAT, \ + &&OPCODE_TYPE_ADJUST_STRING, \ + &&OPCODE_TYPE_ADJUST_VECTOR2, \ + &&OPCODE_TYPE_ADJUST_VECTOR2I, \ + &&OPCODE_TYPE_ADJUST_RECT2, \ + &&OPCODE_TYPE_ADJUST_RECT2I, \ + &&OPCODE_TYPE_ADJUST_VECTOR3, \ + &&OPCODE_TYPE_ADJUST_VECTOR3I, \ + &&OPCODE_TYPE_ADJUST_TRANSFORM2D, \ + &&OPCODE_TYPE_ADJUST_PLANE, \ + &&OPCODE_TYPE_ADJUST_QUAT, \ + &&OPCODE_TYPE_ADJUST_AABB, \ + &&OPCODE_TYPE_ADJUST_BASIS, \ + &&OPCODE_TYPE_ADJUST_TRANSFORM, \ + &&OPCODE_TYPE_ADJUST_COLOR, \ + &&OPCODE_TYPE_ADJUST_STRING_NAME, \ + &&OPCODE_TYPE_ADJUST_NODE_PATH, \ + &&OPCODE_TYPE_ADJUST_RID, \ + &&OPCODE_TYPE_ADJUST_OBJECT, \ + &&OPCODE_TYPE_ADJUST_CALLABLE, \ + &&OPCODE_TYPE_ADJUST_SIGNAL, \ + &&OPCODE_TYPE_ADJUST_DICTIONARY, \ + &&OPCODE_TYPE_ADJUST_ARRAY, \ + &&OPCODE_TYPE_ADJUST_PACKED_BYTE_ARRAY, \ + &&OPCODE_TYPE_ADJUST_PACKED_INT32_ARRAY, \ + &&OPCODE_TYPE_ADJUST_PACKED_INT64_ARRAY, \ + &&OPCODE_TYPE_ADJUST_PACKED_FLOAT32_ARRAY, \ + &&OPCODE_TYPE_ADJUST_PACKED_FLOAT64_ARRAY, \ + &&OPCODE_TYPE_ADJUST_PACKED_STRING_ARRAY, \ + &&OPCODE_TYPE_ADJUST_PACKED_VECTOR2_ARRAY, \ + &&OPCODE_TYPE_ADJUST_PACKED_VECTOR3_ARRAY, \ + &&OPCODE_TYPE_ADJUST_PACKED_COLOR_ARRAY, \ &&OPCODE_ASSERT, \ &&OPCODE_BREAKPOINT, \ &&OPCODE_LINE, \ @@ -2973,6 +3007,50 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a } DISPATCH_OPCODE; +#define OPCODE_TYPE_ADJUST(m_v_type, m_c_type) \ + OPCODE(OPCODE_TYPE_ADJUST_##m_v_type) { \ + CHECK_SPACE(2); \ + GET_INSTRUCTION_ARG(arg, 0); \ + VariantTypeAdjust<m_c_type>::adjust(arg); \ + ip += 2; \ + } \ + DISPATCH_OPCODE + + OPCODE_TYPE_ADJUST(BOOL, bool); + OPCODE_TYPE_ADJUST(INT, int64_t); + OPCODE_TYPE_ADJUST(FLOAT, double); + OPCODE_TYPE_ADJUST(STRING, String); + OPCODE_TYPE_ADJUST(VECTOR2, Vector2); + OPCODE_TYPE_ADJUST(VECTOR2I, Vector2i); + OPCODE_TYPE_ADJUST(RECT2, Rect2); + OPCODE_TYPE_ADJUST(RECT2I, Rect2i); + OPCODE_TYPE_ADJUST(VECTOR3, Vector3); + OPCODE_TYPE_ADJUST(VECTOR3I, Vector3i); + OPCODE_TYPE_ADJUST(TRANSFORM2D, Transform2D); + OPCODE_TYPE_ADJUST(PLANE, Plane); + OPCODE_TYPE_ADJUST(QUAT, Quat); + OPCODE_TYPE_ADJUST(AABB, AABB); + OPCODE_TYPE_ADJUST(BASIS, Basis); + OPCODE_TYPE_ADJUST(TRANSFORM, Transform); + OPCODE_TYPE_ADJUST(COLOR, Color); + OPCODE_TYPE_ADJUST(STRING_NAME, StringName); + OPCODE_TYPE_ADJUST(NODE_PATH, NodePath); + OPCODE_TYPE_ADJUST(RID, RID); + OPCODE_TYPE_ADJUST(OBJECT, Object *); + OPCODE_TYPE_ADJUST(CALLABLE, Callable); + OPCODE_TYPE_ADJUST(SIGNAL, Signal); + OPCODE_TYPE_ADJUST(DICTIONARY, Dictionary); + OPCODE_TYPE_ADJUST(ARRAY, Array); + OPCODE_TYPE_ADJUST(PACKED_BYTE_ARRAY, PackedByteArray); + OPCODE_TYPE_ADJUST(PACKED_INT32_ARRAY, PackedInt32Array); + OPCODE_TYPE_ADJUST(PACKED_INT64_ARRAY, PackedInt64Array); + OPCODE_TYPE_ADJUST(PACKED_FLOAT32_ARRAY, PackedFloat32Array); + OPCODE_TYPE_ADJUST(PACKED_FLOAT64_ARRAY, PackedFloat64Array); + OPCODE_TYPE_ADJUST(PACKED_STRING_ARRAY, PackedStringArray); + OPCODE_TYPE_ADJUST(PACKED_VECTOR2_ARRAY, PackedVector2Array); + OPCODE_TYPE_ADJUST(PACKED_VECTOR3_ARRAY, PackedVector3Array); + OPCODE_TYPE_ADJUST(PACKED_COLOR_ARRAY, PackedColorArray); + OPCODE(OPCODE_ASSERT) { CHECK_SPACE(3); diff --git a/modules/theora/video_stream_theora.cpp b/modules/theora/video_stream_theora.cpp index c5f6dc0d99..19f26c87cd 100644 --- a/modules/theora/video_stream_theora.cpp +++ b/modules/theora/video_stream_theora.cpp @@ -603,6 +603,7 @@ float VideoStreamPlaybackTheora::get_playback_position() const { }; void VideoStreamPlaybackTheora::seek(float p_time) { + WARN_PRINT_ONCE("Seeking in Theora and WebM videos is not implemented yet (it's only supported for GDNative-provided video streams)."); } void VideoStreamPlaybackTheora::set_mix_callback(AudioMixCallback p_callback, void *p_userdata) { diff --git a/modules/webm/video_stream_webm.cpp b/modules/webm/video_stream_webm.cpp index 101001cba0..a6b64b342e 100644 --- a/modules/webm/video_stream_webm.cpp +++ b/modules/webm/video_stream_webm.cpp @@ -194,7 +194,7 @@ float VideoStreamPlaybackWebm::get_playback_position() const { } void VideoStreamPlaybackWebm::seek(float p_time) { - //Not implemented + WARN_PRINT_ONCE("Seeking in Theora and WebM videos is not implemented yet (it's only supported for GDNative-provided video streams)."); } void VideoStreamPlaybackWebm::set_audio_track(int p_idx) { diff --git a/platform/android/java/app/build.gradle b/platform/android/java/app/build.gradle index f103f22db2..1b1fb47bd8 100644 --- a/platform/android/java/app/build.gradle +++ b/platform/android/java/app/build.gradle @@ -157,7 +157,7 @@ android { aidl.srcDirs = ['aidl'] assets.srcDirs = ['assets'] } - debug.jniLibs.srcDirs = ['libs/debug'] + debug.jniLibs.srcDirs = ['libs/debug', 'libs/debug/vulkan_validation_layers'] release.jniLibs.srcDirs = ['libs/release'] } diff --git a/platform/android/vulkan/vulkan_context_android.cpp b/platform/android/vulkan/vulkan_context_android.cpp index 1bf85f07f1..63f2026fae 100644 --- a/platform/android/vulkan/vulkan_context_android.cpp +++ b/platform/android/vulkan/vulkan_context_android.cpp @@ -52,10 +52,10 @@ int VulkanContextAndroid::window_create(ANativeWindow *p_window, int p_width, in return _window_create(DisplayServer::MAIN_WINDOW_ID, surface, p_width, p_height); } -VulkanContextAndroid::VulkanContextAndroid() { - // TODO: fix validation layers - use_validation_layers = false; -} +bool VulkanContextAndroid::_use_validation_layers() { + uint32_t count = 0; + _get_preferred_validation_layers(&count, nullptr); -VulkanContextAndroid::~VulkanContextAndroid() { + // On Android, we use validation layers automatically if they were explicitly linked with the app. + return count > 0; } diff --git a/platform/android/vulkan/vulkan_context_android.h b/platform/android/vulkan/vulkan_context_android.h index c608f2d665..5a84eaf8f3 100644 --- a/platform/android/vulkan/vulkan_context_android.h +++ b/platform/android/vulkan/vulkan_context_android.h @@ -36,13 +36,16 @@ struct ANativeWindow; class VulkanContextAndroid : public VulkanContext { - virtual const char *_get_platform_surface_extension() const; + virtual const char *_get_platform_surface_extension() const override; public: int window_create(ANativeWindow *p_window, int p_width, int p_height); - VulkanContextAndroid(); - ~VulkanContextAndroid(); + VulkanContextAndroid() = default; + ~VulkanContextAndroid() override = default; + +protected: + bool _use_validation_layers() override; }; #endif // VULKAN_CONTEXT_ANDROID_H diff --git a/platform/uwp/export/export.cpp b/platform/uwp/export/export.cpp index 2a0bc78440..800a728033 100644 --- a/platform/uwp/export/export.cpp +++ b/platform/uwp/export/export.cpp @@ -1177,6 +1177,8 @@ public: } virtual Error export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags = 0) override { + ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags); + String src_appx; EditorProgress ep("export", "Exporting for UWP", 7, true); diff --git a/scene/resources/height_map_shape_3d.cpp b/scene/resources/height_map_shape_3d.cpp index 5593bb766f..de5da944bc 100644 --- a/scene/resources/height_map_shape_3d.cpp +++ b/scene/resources/height_map_shape_3d.cpp @@ -41,10 +41,10 @@ Vector<Vector3> HeightMapShape3D::get_debug_mesh_lines() const { Vector2 size(map_width - 1, map_depth - 1); Vector2 start = size * -0.5; - const real_t *r = map_data.ptr(); + const float *r = map_data.ptr(); // reserve some memory for our points.. - points.resize(((map_width - 1) * map_depth * 2) + (map_width * (map_depth - 1) * 2)); + points.resize(((map_width - 1) * map_depth * 2) + (map_width * (map_depth - 1) * 2) + ((map_width - 1) * (map_depth - 1) * 2)); // now set our points int r_offset = 0; @@ -65,6 +65,11 @@ Vector<Vector3> HeightMapShape3D::get_debug_mesh_lines() const { points.write[w_offset++] = Vector3(height.x, r[r_offset + map_width - 1], height.z + 1.0); } + if ((w != map_width - 1) && (d != map_depth - 1)) { + points.write[w_offset++] = Vector3(height.x + 1.0, r[r_offset], height.z); + points.write[w_offset++] = Vector3(height.x, r[r_offset + map_width - 1], height.z + 1.0); + } + height.x += 1.0; } @@ -100,7 +105,7 @@ void HeightMapShape3D::set_map_width(int p_new) { int new_size = map_width * map_depth; map_data.resize(map_width * map_depth); - real_t *w = map_data.ptrw(); + float *w = map_data.ptrw(); while (was_size < new_size) { w[was_size++] = 0.0; } @@ -124,7 +129,7 @@ void HeightMapShape3D::set_map_depth(int p_new) { int new_size = map_width * map_depth; map_data.resize(new_size); - real_t *w = map_data.ptrw(); + float *w = map_data.ptrw(); while (was_size < new_size) { w[was_size++] = 0.0; } @@ -146,8 +151,8 @@ void HeightMapShape3D::set_map_data(PackedFloat32Array p_new) { } // copy - real_t *w = map_data.ptrw(); - const real_t *r = p_new.ptr(); + float *w = map_data.ptrw(); + const float *r = p_new.ptr(); for (int i = 0; i < size; i++) { float val = r[i]; w[i] = val; @@ -189,7 +194,7 @@ void HeightMapShape3D::_bind_methods() { HeightMapShape3D::HeightMapShape3D() : Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_HEIGHTMAP)) { map_data.resize(map_width * map_depth); - real_t *w = map_data.ptrw(); + float *w = map_data.ptrw(); w[0] = 0.0; w[1] = 0.0; w[2] = 0.0; diff --git a/scene/resources/height_map_shape_3d.h b/scene/resources/height_map_shape_3d.h index 6fc88cff90..1219791c56 100644 --- a/scene/resources/height_map_shape_3d.h +++ b/scene/resources/height_map_shape_3d.h @@ -39,8 +39,8 @@ class HeightMapShape3D : public Shape3D { int map_width = 2; int map_depth = 2; PackedFloat32Array map_data; - float min_height = 0.0; - float max_height = 0.0; + real_t min_height = 0.0; + real_t max_height = 0.0; protected: static void _bind_methods(); diff --git a/servers/physics_3d/shape_3d_sw.cpp b/servers/physics_3d/shape_3d_sw.cpp index 4c14cb3162..ccd37ca742 100644 --- a/servers/physics_3d/shape_3d_sw.cpp +++ b/servers/physics_3d/shape_3d_sw.cpp @@ -30,10 +30,28 @@ #include "shape_3d_sw.h" +#include "core/io/image.h" #include "core/math/geometry_3d.h" #include "core/math/quick_hull.h" #include "core/templates/sort_array.h" +// HeightMapShape3DSW is based on Bullet btHeightfieldTerrainShape. + +/* +Bullet Continuous Collision Detection and Physics Library +Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org + +This software is provided 'as-is', without any express or implied warranty. +In no event will the authors be held liable for any damages arising from the use of this software. +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it freely, +subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. +2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. +3. This notice may not be removed or altered from any source distribution. +*/ + #define _EDGE_IS_VALID_SUPPORT_THRESHOLD 0.0002 #define _FACE_IS_VALID_SUPPORT_THRESHOLD 0.9998 @@ -1617,7 +1635,7 @@ ConcavePolygonShape3DSW::ConcavePolygonShape3DSW() { /* HEIGHT MAP SHAPE */ -Vector<real_t> HeightMapShape3DSW::get_heights() const { +Vector<float> HeightMapShape3DSW::get_heights() const { return heights; } @@ -1629,10 +1647,6 @@ int HeightMapShape3DSW::get_depth() const { return depth; } -real_t HeightMapShape3DSW::get_cell_size() const { - return cell_size; -} - void HeightMapShape3DSW::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const { //not very useful, but not very used either p_transform.xform(get_aabb()).project_range_in_plane(Plane(p_normal, 0), r_min, r_max); @@ -1643,7 +1657,198 @@ Vector3 HeightMapShape3DSW::get_support(const Vector3 &p_normal) const { return get_aabb().get_support(p_normal); } +struct _HeightmapSegmentCullParams { + Vector3 from; + Vector3 to; + Vector3 dir; + + Vector3 result; + Vector3 normal; + + const HeightMapShape3DSW *heightmap = nullptr; + FaceShape3DSW *face = nullptr; +}; + +_FORCE_INLINE_ bool _heightmap_face_cull_segment(_HeightmapSegmentCullParams &p_params) { + Vector3 res; + Vector3 normal; + if (p_params.face->intersect_segment(p_params.from, p_params.to, res, normal)) { + p_params.result = res; + p_params.normal = normal; + return true; + } + + return false; +} + +_FORCE_INLINE_ bool _heightmap_cell_cull_segment(_HeightmapSegmentCullParams &p_params, int p_x, int p_z) { + // First triangle. + p_params.heightmap->_get_point(p_x, p_z, p_params.face->vertex[0]); + p_params.heightmap->_get_point(p_x + 1, p_z, p_params.face->vertex[1]); + p_params.heightmap->_get_point(p_x, p_z + 1, p_params.face->vertex[2]); + p_params.face->normal = Plane(p_params.face->vertex[0], p_params.face->vertex[1], p_params.face->vertex[2]).normal; + if (_heightmap_face_cull_segment(p_params)) { + return true; + } + + // Second triangle. + p_params.face->vertex[0] = p_params.face->vertex[1]; + p_params.heightmap->_get_point(p_x + 1, p_z + 1, p_params.face->vertex[1]); + p_params.face->normal = Plane(p_params.face->vertex[0], p_params.face->vertex[1], p_params.face->vertex[2]).normal; + if (_heightmap_face_cull_segment(p_params)) { + return true; + } + + return false; +} + bool HeightMapShape3DSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const { + if (heights.is_empty()) { + return false; + } + + Vector3 local_begin = p_begin + local_origin; + Vector3 local_end = p_end + local_origin; + + FaceShape3DSW face; + face.backface_collision = false; + + _HeightmapSegmentCullParams params; + params.from = p_begin; + params.to = p_end; + params.dir = (p_end - p_begin).normalized(); + params.heightmap = this; + params.face = &face; + + // Quantize the ray begin/end. + int begin_x = floor(local_begin.x); + int begin_z = floor(local_begin.z); + int end_x = floor(local_end.x); + int end_z = floor(local_end.z); + + if ((begin_x == end_x) && (begin_z == end_z)) { + // Simple case for rays that don't traverse the grid horizontally. + // Just perform a test on the given cell. + int x = CLAMP(begin_x, 0, width - 2); + int z = CLAMP(begin_z, 0, depth - 2); + if (_heightmap_cell_cull_segment(params, x, z)) { + r_point = params.result; + r_normal = params.normal; + return true; + } + } else { + // Perform grid query from projected ray. + Vector2 ray_dir_proj(local_end.x - local_begin.x, local_end.z - local_begin.z); + real_t ray_dist_proj = ray_dir_proj.length(); + + if (ray_dist_proj < CMP_EPSILON) { + ray_dir_proj = Vector2(); + } else { + ray_dir_proj /= ray_dist_proj; + } + + const int x_step = (ray_dir_proj.x > CMP_EPSILON) ? 1 : ((ray_dir_proj.x < -CMP_EPSILON) ? -1 : 0); + const int z_step = (ray_dir_proj.y > CMP_EPSILON) ? 1 : ((ray_dir_proj.y < -CMP_EPSILON) ? -1 : 0); + + const real_t infinite = 1e20; + const real_t delta_x = (x_step != 0) ? 1.f / Math::abs(ray_dir_proj.x) : infinite; + const real_t delta_z = (z_step != 0) ? 1.f / Math::abs(ray_dir_proj.y) : infinite; + + real_t cross_x; // At which value of `param` we will cross a x-axis lane? + real_t cross_z; // At which value of `param` we will cross a z-axis lane? + + // X initialization. + if (x_step != 0) { + if (x_step == 1) { + cross_x = (ceil(local_begin.x) - local_begin.x) * delta_x; + } else { + cross_x = (local_begin.x - floor(local_begin.x)) * delta_x; + } + } else { + cross_x = infinite; // Will never cross on X. + } + + // Z initialization. + if (z_step != 0) { + if (z_step == 1) { + cross_z = (ceil(local_begin.z) - local_begin.z) * delta_z; + } else { + cross_z = (local_begin.z - floor(local_begin.z)) * delta_z; + } + } else { + cross_z = infinite; // Will never cross on Z. + } + + int x = floor(local_begin.x); + int z = floor(local_begin.z); + + // Workaround cases where the ray starts at an integer position. + if (Math::abs(cross_x) < CMP_EPSILON) { + cross_x += delta_x; + // If going backwards, we should ignore the position we would get by the above flooring, + // because the ray is not heading in that direction. + if (x_step == -1) { + x -= 1; + } + } + + if (Math::abs(cross_z) < CMP_EPSILON) { + cross_z += delta_z; + if (z_step == -1) { + z -= 1; + } + } + + // Start inside the grid. + int x_start = CLAMP(x, 0, width - 2); + int z_start = CLAMP(z, 0, depth - 2); + + // Adjust initial cross values. + cross_x += delta_x * x_step * (x_start - x); + cross_z += delta_z * z_step * (z_start - z); + + x = x_start; + z = z_start; + + if (_heightmap_cell_cull_segment(params, x, z)) { + r_point = params.result; + r_normal = params.normal; + return true; + } + + real_t dist = 0.0; + while (true) { + if (cross_x < cross_z) { + // X lane. + x += x_step; + // Assign before advancing the param, + // to be in sync with the initialization step. + dist = cross_x; + cross_x += delta_x; + } else { + // Z lane. + z += z_step; + dist = cross_z; + cross_z += delta_z; + } + + // Stop when outside the grid. + if ((x < 0) || (z < 0) || (x >= width - 1) || (z >= depth - 1)) { + break; + } + + if (_heightmap_cell_cull_segment(params, x, z)) { + r_point = params.result; + r_normal = params.normal; + return true; + } + + if (dist > ray_dist_proj) { + break; + } + } + } + return false; } @@ -1655,7 +1860,66 @@ Vector3 HeightMapShape3DSW::get_closest_point_to(const Vector3 &p_point) const { return Vector3(); } +void HeightMapShape3DSW::_get_cell(const Vector3 &p_point, int &r_x, int &r_y, int &r_z) const { + const AABB &aabb = get_aabb(); + + Vector3 pos_local = aabb.position + local_origin; + + Vector3 clamped_point(p_point); + clamped_point.x = CLAMP(p_point.x, pos_local.x, pos_local.x + aabb.size.x); + clamped_point.y = CLAMP(p_point.y, pos_local.y, pos_local.y + aabb.size.y); + clamped_point.z = CLAMP(p_point.z, pos_local.z, pos_local.x + aabb.size.z); + + r_x = (clamped_point.x < 0.0) ? (clamped_point.x - 0.5) : (clamped_point.x + 0.5); + r_y = (clamped_point.y < 0.0) ? (clamped_point.y - 0.5) : (clamped_point.y + 0.5); + r_z = (clamped_point.z < 0.0) ? (clamped_point.z - 0.5) : (clamped_point.z + 0.5); +} + void HeightMapShape3DSW::cull(const AABB &p_local_aabb, Callback p_callback, void *p_userdata) const { + if (heights.is_empty()) { + return; + } + + AABB local_aabb = p_local_aabb; + local_aabb.position += local_origin; + + // Quantize the aabb, and adjust the start/end ranges. + int aabb_min[3]; + int aabb_max[3]; + _get_cell(local_aabb.position, aabb_min[0], aabb_min[1], aabb_min[2]); + _get_cell(local_aabb.position + local_aabb.size, aabb_max[0], aabb_max[1], aabb_max[2]); + + // Expand the min/max quantized values. + // This is to catch the case where the input aabb falls between grid points. + for (int i = 0; i < 3; ++i) { + aabb_min[i]--; + aabb_max[i]++; + } + + int start_x = MAX(0, aabb_min[0]); + int end_x = MIN(width - 1, aabb_max[0]); + int start_z = MAX(0, aabb_min[2]); + int end_z = MIN(depth - 1, aabb_max[2]); + + FaceShape3DSW face; + face.backface_collision = true; + + for (int z = start_z; z < end_z; z++) { + for (int x = start_x; x < end_x; x++) { + // First triangle. + _get_point(x, z, face.vertex[0]); + _get_point(x + 1, z, face.vertex[1]); + _get_point(x, z + 1, face.vertex[2]); + face.normal = Plane(face.vertex[0], face.vertex[2], face.vertex[1]).normal; + p_callback(p_userdata, &face); + + // Second triangle. + face.vertex[0] = face.vertex[1]; + _get_point(x + 1, z + 1, face.vertex[1]); + face.normal = Plane(face.vertex[0], face.vertex[2], face.vertex[1]).normal; + p_callback(p_userdata, &face); + } + } } Vector3 HeightMapShape3DSW::get_moment_of_inertia(real_t p_mass) const { @@ -1668,58 +1932,102 @@ Vector3 HeightMapShape3DSW::get_moment_of_inertia(real_t p_mass) const { (p_mass / 3.0) * (extents.x * extents.x + extents.y * extents.y)); } -void HeightMapShape3DSW::_setup(Vector<real_t> p_heights, int p_width, int p_depth, real_t p_cell_size) { +void HeightMapShape3DSW::_setup(const Vector<float> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height) { heights = p_heights; width = p_width; depth = p_depth; - cell_size = p_cell_size; - - const real_t *r = heights.ptr(); + // Initialize aabb. AABB aabb; + aabb.position = Vector3(0.0, p_min_height, 0.0); + aabb.size = Vector3(p_width - 1, p_max_height - p_min_height, p_depth - 1); - for (int i = 0; i < depth; i++) { - for (int j = 0; j < width; j++) { - real_t h = r[i * width + j]; + // Initialize origin as the aabb center. + local_origin = aabb.position + 0.5 * aabb.size; + local_origin.y = 0.0; - Vector3 pos(j * cell_size, h, i * cell_size); - if (i == 0 || j == 0) { - aabb.position = pos; - } else { - aabb.expand_to(pos); - } - } - } + aabb.position -= local_origin; configure(aabb); } void HeightMapShape3DSW::set_data(const Variant &p_data) { ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY); + Dictionary d = p_data; ERR_FAIL_COND(!d.has("width")); ERR_FAIL_COND(!d.has("depth")); - ERR_FAIL_COND(!d.has("cell_size")); ERR_FAIL_COND(!d.has("heights")); int width = d["width"]; int depth = d["depth"]; - real_t cell_size = d["cell_size"]; - Vector<real_t> heights = d["heights"]; - ERR_FAIL_COND(width <= 0); - ERR_FAIL_COND(depth <= 0); - ERR_FAIL_COND(cell_size <= CMP_EPSILON); - ERR_FAIL_COND(heights.size() != (width * depth)); - _setup(heights, width, depth, cell_size); + ERR_FAIL_COND(width <= 0.0); + ERR_FAIL_COND(depth <= 0.0); + + Variant heights_variant = d["heights"]; + Vector<float> heights_buffer; + if (heights_variant.get_type() == Variant::PACKED_FLOAT32_ARRAY) { + // Ready-to-use heights can be passed. + heights_buffer = heights_variant; + } else if (heights_variant.get_type() == Variant::OBJECT) { + // If an image is passed, we have to convert it. + // This would be expensive to do with a script, so it's nice to have it here. + Ref<Image> image = heights_variant; + ERR_FAIL_COND(image.is_null()); + ERR_FAIL_COND(image->get_format() != Image::FORMAT_RF); + + PackedByteArray im_data = image->get_data(); + heights_buffer.resize(image->get_width() * image->get_height()); + + float *w = heights_buffer.ptrw(); + float *rp = (float *)im_data.ptr(); + for (int i = 0; i < heights_buffer.size(); ++i) { + w[i] = rp[i]; + } + } else { + ERR_FAIL_MSG("Expected PackedFloat32Array or float Image."); + } + + // Compute min and max heights or use precomputed values. + real_t min_height = 0.0; + real_t max_height = 0.0; + if (d.has("min_height") && d.has("max_height")) { + min_height = d["min_height"]; + max_height = d["max_height"]; + } else { + int heights_size = heights.size(); + for (int i = 0; i < heights_size; ++i) { + float h = heights[i]; + if (h < min_height) { + min_height = h; + } else if (h > max_height) { + max_height = h; + } + } + } + + ERR_FAIL_COND(min_height > max_height); + + ERR_FAIL_COND(heights_buffer.size() != (width * depth)); + + // If specified, min and max height will be used as precomputed values. + _setup(heights_buffer, width, depth, min_height, max_height); } Variant HeightMapShape3DSW::get_data() const { - ERR_FAIL_V(Variant()); + Dictionary d; + d["width"] = width; + d["depth"] = depth; + + const AABB &aabb = get_aabb(); + d["min_height"] = aabb.position.y; + d["max_height"] = aabb.position.y + aabb.size.y; + + d["heights"] = heights; + + return d; } HeightMapShape3DSW::HeightMapShape3DSW() { - width = 0; - depth = 0; - cell_size = 0; } diff --git a/servers/physics_3d/shape_3d_sw.h b/servers/physics_3d/shape_3d_sw.h index 988e76c699..4d2b6ffbed 100644 --- a/servers/physics_3d/shape_3d_sw.h +++ b/servers/physics_3d/shape_3d_sw.h @@ -81,7 +81,7 @@ public: virtual PhysicsServer3D::ShapeType get_type() const = 0; - _FORCE_INLINE_ AABB get_aabb() const { return aabb; } + _FORCE_INLINE_ const AABB &get_aabb() const { return aabb; } _FORCE_INLINE_ bool is_configured() const { return configured; } virtual bool is_concave() const { return false; } @@ -389,21 +389,29 @@ public: }; struct HeightMapShape3DSW : public ConcaveShape3DSW { - Vector<real_t> heights; - int width; - int depth; - real_t cell_size; + Vector<float> heights; + int width = 0; + int depth = 0; + Vector3 local_origin; - //void _cull_segment(int p_idx,_SegmentCullParams *p_params) const; - //void _cull(int p_idx,_CullParams *p_params) const; + _FORCE_INLINE_ float _get_height(int p_x, int p_z) const { + return heights[(p_z * width) + p_x]; + } + + _FORCE_INLINE_ void _get_point(int p_x, int p_z, Vector3 &r_point) const { + r_point.x = p_x - 0.5 * (width - 1.0); + r_point.y = _get_height(p_x, p_z); + r_point.z = p_z - 0.5 * (depth - 1.0); + } + + void _get_cell(const Vector3 &p_point, int &r_x, int &r_y, int &r_z) const; - void _setup(Vector<real_t> p_heights, int p_width, int p_depth, real_t p_cell_size); + void _setup(const Vector<float> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height); public: - Vector<real_t> get_heights() const; + Vector<float> get_heights() const; int get_width() const; int get_depth() const; - real_t get_cell_size() const; virtual PhysicsServer3D::ShapeType get_type() const { return PhysicsServer3D::SHAPE_HEIGHTMAP; } |