Path: blob/master/servers/physics_3d/physics_server_3d_wrap_mt.h
11322 views
/**************************************************************************/1/* physics_server_3d_wrap_mt.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/config/project_settings.h"33#include "core/object/worker_thread_pool.h"34#include "core/os/thread.h"35#include "core/templates/command_queue_mt.h"36#include "servers/physics_3d/physics_server_3d.h"3738#define ASYNC_COND_PUSH (Thread::get_caller_id() != server_thread)39#define ASYNC_COND_PUSH_AND_RET (Thread::get_caller_id() != server_thread && !(doing_sync.is_set() && Thread::is_main_thread()))40#define ASYNC_COND_PUSH_AND_SYNC (Thread::get_caller_id() != server_thread && !(doing_sync.is_set() && Thread::is_main_thread()))4142#ifdef DEBUG_SYNC43#define SYNC_DEBUG print_line("sync on: " + String(__FUNCTION__));44#else45#define SYNC_DEBUG46#endif4748#ifdef DEBUG_ENABLED49#ifdef DEV_ENABLED50#define MAIN_THREAD_SYNC_WARN WARN_PRINT("Call to " + String(__FUNCTION__) + " causing PhysicsServer3D synchronizations on every frame. This significantly affects performance.");51#else52#define MAIN_THREAD_SYNC_WARN53#endif54#endif5556class PhysicsServer3DWrapMT : public PhysicsServer3D {57GDSOFTCLASS(PhysicsServer3DWrapMT, PhysicsServer3D);5859mutable PhysicsServer3D *physics_server_3d = nullptr;6061mutable CommandQueueMT command_queue;6263Thread::ID server_thread = Thread::UNASSIGNED_ID;64WorkerThreadPool::TaskID server_task_id = WorkerThreadPool::INVALID_TASK_ID;65bool exit = false;66bool create_thread = false;67SafeFlag doing_sync;6869void _assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id);70void _thread_exit();71void _thread_step(real_t p_delta);72void _thread_loop();73void _thread_sync();7475public:76#define ServerName PhysicsServer3D77#define ServerNameWrapMT PhysicsServer3DWrapMT78#define server_name physics_server_3d79#define WRITE_ACTION8081#include "servers/server_wrap_mt_common.h"8283//FUNC1RID(shape,ShapeType); todo fix84FUNCRID(world_boundary_shape)85FUNCRID(separation_ray_shape)86FUNCRID(sphere_shape)87FUNCRID(box_shape)88FUNCRID(capsule_shape)89FUNCRID(cylinder_shape)90FUNCRID(convex_polygon_shape)91FUNCRID(concave_polygon_shape)92FUNCRID(heightmap_shape)93FUNCRID(custom_shape)9495FUNC2(shape_set_data, RID, const Variant &);96FUNC2(shape_set_custom_solver_bias, RID, real_t);9798FUNC2(shape_set_margin, RID, real_t)99FUNC1RC(real_t, shape_get_margin, RID)100101FUNC1RC(ShapeType, shape_get_type, RID);102FUNC1RC(Variant, shape_get_data, RID);103FUNC1RC(real_t, shape_get_custom_solver_bias, RID);104#if 0105//these work well, but should be used from the main thread only106bool shape_collide(RID p_shape_A, const Transform &p_xform_A, const Vector3 &p_motion_A, RID p_shape_B, const Transform &p_xform_B, const Vector3 &p_motion_B, Vector3 *r_results, int p_result_max, int &r_result_count) {107ERR_FAIL_COND_V(!Thread::is_main_thread(), false);108return physics_server_3d->shape_collide(p_shape_A, p_xform_A, p_motion_A, p_shape_B, p_xform_B, p_motion_B, r_results, p_result_max, r_result_count);109}110#endif111/* SPACE API */112113FUNCRID(space);114FUNC2(space_set_active, RID, bool);115FUNC1RC(bool, space_is_active, RID);116117FUNC3(space_set_param, RID, SpaceParameter, real_t);118FUNC2RC(real_t, space_get_param, RID, SpaceParameter);119120// this function only works on physics process, errors and returns null otherwise121PhysicsDirectSpaceState3D *space_get_direct_state(RID p_space) override {122ERR_FAIL_COND_V(!Thread::is_main_thread(), nullptr);123return physics_server_3d->space_get_direct_state(p_space);124}125126FUNC2(space_set_debug_contacts, RID, int);127virtual Vector<Vector3> space_get_contacts(RID p_space) const override {128ERR_FAIL_COND_V(!Thread::is_main_thread(), Vector<Vector3>());129return physics_server_3d->space_get_contacts(p_space);130}131132virtual int space_get_contact_count(RID p_space) const override {133ERR_FAIL_COND_V(!Thread::is_main_thread(), 0);134return physics_server_3d->space_get_contact_count(p_space);135}136137/* AREA API */138139//FUNC0RID(area);140FUNCRID(area);141142FUNC2(area_set_space, RID, RID);143FUNC1RC(RID, area_get_space, RID);144145FUNC4(area_add_shape, RID, RID, const Transform3D &, bool);146FUNC3(area_set_shape, RID, int, RID);147FUNC3(area_set_shape_transform, RID, int, const Transform3D &);148FUNC3(area_set_shape_disabled, RID, int, bool);149150FUNC1RC(int, area_get_shape_count, RID);151FUNC2RC(RID, area_get_shape, RID, int);152FUNC2RC(Transform3D, area_get_shape_transform, RID, int);153FUNC2(area_remove_shape, RID, int);154FUNC1(area_clear_shapes, RID);155156FUNC2(area_attach_object_instance_id, RID, ObjectID);157FUNC1RC(ObjectID, area_get_object_instance_id, RID);158159FUNC3(area_set_param, RID, AreaParameter, const Variant &);160FUNC2(area_set_transform, RID, const Transform3D &);161162FUNC2RC(Variant, area_get_param, RID, AreaParameter);163FUNC1RC(Transform3D, area_get_transform, RID);164165FUNC2(area_set_collision_layer, RID, uint32_t);166FUNC1RC(uint32_t, area_get_collision_layer, RID);167168FUNC2(area_set_collision_mask, RID, uint32_t);169FUNC1RC(uint32_t, area_get_collision_mask, RID);170171FUNC2(area_set_monitorable, RID, bool);172FUNC2(area_set_ray_pickable, RID, bool);173174FUNC2(area_set_monitor_callback, RID, const Callable &);175FUNC2(area_set_area_monitor_callback, RID, const Callable &);176177/* BODY API */178179//FUNC2RID(body,BodyMode,bool);180FUNCRID(body)181182FUNC2(body_set_space, RID, RID);183FUNC1RC(RID, body_get_space, RID);184185FUNC2(body_set_mode, RID, BodyMode);186FUNC1RC(BodyMode, body_get_mode, RID);187188FUNC4(body_add_shape, RID, RID, const Transform3D &, bool);189FUNC3(body_set_shape, RID, int, RID);190FUNC3(body_set_shape_transform, RID, int, const Transform3D &);191192FUNC1RC(int, body_get_shape_count, RID);193FUNC2RC(Transform3D, body_get_shape_transform, RID, int);194FUNC2RC(RID, body_get_shape, RID, int);195196FUNC3(body_set_shape_disabled, RID, int, bool);197198FUNC2(body_remove_shape, RID, int);199FUNC1(body_clear_shapes, RID);200201FUNC2(body_attach_object_instance_id, RID, ObjectID);202FUNC1RC(ObjectID, body_get_object_instance_id, RID);203204FUNC2(body_set_enable_continuous_collision_detection, RID, bool);205FUNC1RC(bool, body_is_continuous_collision_detection_enabled, RID);206207FUNC2(body_set_collision_layer, RID, uint32_t);208FUNC1RC(uint32_t, body_get_collision_layer, RID);209210FUNC2(body_set_collision_mask, RID, uint32_t);211FUNC1RC(uint32_t, body_get_collision_mask, RID);212213FUNC2(body_set_collision_priority, RID, real_t);214FUNC1RC(real_t, body_get_collision_priority, RID);215216FUNC2(body_set_user_flags, RID, uint32_t);217FUNC1RC(uint32_t, body_get_user_flags, RID);218219FUNC3(body_set_param, RID, BodyParameter, const Variant &);220FUNC2RC(Variant, body_get_param, RID, BodyParameter);221222FUNC1(body_reset_mass_properties, RID);223224FUNC3(body_set_state, RID, BodyState, const Variant &);225FUNC2RC(Variant, body_get_state, RID, BodyState);226227FUNC2(body_apply_torque_impulse, RID, const Vector3 &);228FUNC2(body_apply_central_impulse, RID, const Vector3 &);229FUNC3(body_apply_impulse, RID, const Vector3 &, const Vector3 &);230231FUNC2(body_apply_central_force, RID, const Vector3 &);232FUNC3(body_apply_force, RID, const Vector3 &, const Vector3 &);233FUNC2(body_apply_torque, RID, const Vector3 &);234235FUNC3(soft_body_apply_point_impulse, RID, int, const Vector3 &);236FUNC3(soft_body_apply_point_force, RID, int, const Vector3 &);237FUNC2(soft_body_apply_central_impulse, RID, const Vector3 &);238FUNC2(soft_body_apply_central_force, RID, const Vector3 &);239240FUNC2(body_add_constant_central_force, RID, const Vector3 &);241FUNC3(body_add_constant_force, RID, const Vector3 &, const Vector3 &);242FUNC2(body_add_constant_torque, RID, const Vector3 &);243244FUNC2(body_set_constant_force, RID, const Vector3 &);245FUNC1RC(Vector3, body_get_constant_force, RID);246247FUNC2(body_set_constant_torque, RID, const Vector3 &);248FUNC1RC(Vector3, body_get_constant_torque, RID);249250FUNC2(body_set_axis_velocity, RID, const Vector3 &);251252FUNC3(body_set_axis_lock, RID, BodyAxis, bool);253FUNC2RC(bool, body_is_axis_locked, RID, BodyAxis);254255FUNC2(body_add_collision_exception, RID, RID);256FUNC2(body_remove_collision_exception, RID, RID);257FUNC2S(body_get_collision_exceptions, RID, List<RID> *);258259FUNC2(body_set_max_contacts_reported, RID, int);260FUNC1RC(int, body_get_max_contacts_reported, RID);261262FUNC2(body_set_contacts_reported_depth_threshold, RID, real_t);263FUNC1RC(real_t, body_get_contacts_reported_depth_threshold, RID);264265FUNC2(body_set_omit_force_integration, RID, bool);266FUNC1RC(bool, body_is_omitting_force_integration, RID);267268FUNC2(body_set_state_sync_callback, RID, const Callable &);269FUNC3(body_set_force_integration_callback, RID, const Callable &, const Variant &);270271FUNC2(body_set_ray_pickable, RID, bool);272273bool body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result = nullptr) override {274ERR_FAIL_COND_V(!Thread::is_main_thread(), false);275return physics_server_3d->body_test_motion(p_body, p_parameters, r_result);276}277278// this function only works on physics process, errors and returns null otherwise279PhysicsDirectBodyState3D *body_get_direct_state(RID p_body) override {280ERR_FAIL_COND_V(!Thread::is_main_thread(), nullptr);281return physics_server_3d->body_get_direct_state(p_body);282}283284/* SOFT BODY API */285286FUNCRID(soft_body)287288FUNC2(soft_body_update_rendering_server, RID, PhysicsServer3DRenderingServerHandler *)289290FUNC2(soft_body_set_space, RID, RID)291FUNC1RC(RID, soft_body_get_space, RID)292293FUNC2(soft_body_set_ray_pickable, RID, bool);294295FUNC2(soft_body_set_collision_layer, RID, uint32_t)296FUNC1RC(uint32_t, soft_body_get_collision_layer, RID)297298FUNC2(soft_body_set_collision_mask, RID, uint32_t)299FUNC1RC(uint32_t, soft_body_get_collision_mask, RID)300301FUNC2(soft_body_add_collision_exception, RID, RID)302FUNC2(soft_body_remove_collision_exception, RID, RID)303FUNC2S(soft_body_get_collision_exceptions, RID, List<RID> *)304305FUNC3(soft_body_set_state, RID, BodyState, const Variant &);306FUNC2RC(Variant, soft_body_get_state, RID, BodyState);307308FUNC2(soft_body_set_transform, RID, const Transform3D &);309310FUNC2(soft_body_set_simulation_precision, RID, int);311FUNC1RC(int, soft_body_get_simulation_precision, RID);312313FUNC2(soft_body_set_total_mass, RID, real_t);314FUNC1RC(real_t, soft_body_get_total_mass, RID);315316FUNC2(soft_body_set_linear_stiffness, RID, real_t);317FUNC1RC(real_t, soft_body_get_linear_stiffness, RID);318319FUNC2(soft_body_set_shrinking_factor, RID, real_t);320FUNC1RC(real_t, soft_body_get_shrinking_factor, RID);321322FUNC2(soft_body_set_pressure_coefficient, RID, real_t);323FUNC1RC(real_t, soft_body_get_pressure_coefficient, RID);324325FUNC2(soft_body_set_damping_coefficient, RID, real_t);326FUNC1RC(real_t, soft_body_get_damping_coefficient, RID);327328FUNC2(soft_body_set_drag_coefficient, RID, real_t);329FUNC1RC(real_t, soft_body_get_drag_coefficient, RID);330331FUNC2(soft_body_set_mesh, RID, RID);332333FUNC1RC(AABB, soft_body_get_bounds, RID);334335FUNC3(soft_body_move_point, RID, int, const Vector3 &);336FUNC2RC(Vector3, soft_body_get_point_global_position, RID, int);337338FUNC1(soft_body_remove_all_pinned_points, RID);339FUNC3(soft_body_pin_point, RID, int, bool);340FUNC2RC(bool, soft_body_is_point_pinned, RID, int);341342/* JOINT API */343344FUNCRID(joint)345346FUNC1(joint_clear, RID)347348FUNC5(joint_make_pin, RID, RID, const Vector3 &, RID, const Vector3 &)349350FUNC3(pin_joint_set_param, RID, PinJointParam, real_t)351FUNC2RC(real_t, pin_joint_get_param, RID, PinJointParam)352353FUNC2(pin_joint_set_local_a, RID, const Vector3 &)354FUNC1RC(Vector3, pin_joint_get_local_a, RID)355356FUNC2(pin_joint_set_local_b, RID, const Vector3 &)357FUNC1RC(Vector3, pin_joint_get_local_b, RID)358359FUNC5(joint_make_hinge, RID, RID, const Transform3D &, RID, const Transform3D &)360FUNC7(joint_make_hinge_simple, RID, RID, const Vector3 &, const Vector3 &, RID, const Vector3 &, const Vector3 &)361362FUNC3(hinge_joint_set_param, RID, HingeJointParam, real_t)363FUNC2RC(real_t, hinge_joint_get_param, RID, HingeJointParam)364365FUNC3(hinge_joint_set_flag, RID, HingeJointFlag, bool)366FUNC2RC(bool, hinge_joint_get_flag, RID, HingeJointFlag)367368FUNC5(joint_make_slider, RID, RID, const Transform3D &, RID, const Transform3D &)369370FUNC3(slider_joint_set_param, RID, SliderJointParam, real_t)371FUNC2RC(real_t, slider_joint_get_param, RID, SliderJointParam)372373FUNC5(joint_make_cone_twist, RID, RID, const Transform3D &, RID, const Transform3D &)374375FUNC3(cone_twist_joint_set_param, RID, ConeTwistJointParam, real_t)376FUNC2RC(real_t, cone_twist_joint_get_param, RID, ConeTwistJointParam)377378FUNC5(joint_make_generic_6dof, RID, RID, const Transform3D &, RID, const Transform3D &)379380FUNC4(generic_6dof_joint_set_param, RID, Vector3::Axis, G6DOFJointAxisParam, real_t)381FUNC3RC(real_t, generic_6dof_joint_get_param, RID, Vector3::Axis, G6DOFJointAxisParam)382383FUNC4(generic_6dof_joint_set_flag, RID, Vector3::Axis, G6DOFJointAxisFlag, bool)384FUNC3RC(bool, generic_6dof_joint_get_flag, RID, Vector3::Axis, G6DOFJointAxisFlag)385386FUNC1RC(JointType, joint_get_type, RID);387388FUNC2(joint_set_solver_priority, RID, int);389FUNC1RC(int, joint_get_solver_priority, RID);390391FUNC2(joint_disable_collisions_between_bodies, RID, bool);392FUNC1RC(bool, joint_is_disabled_collisions_between_bodies, RID);393394/* MISC */395396FUNC1(free_rid, RID);397FUNC1(set_active, bool);398399virtual void init() override;400virtual void step(real_t p_step) override;401virtual void sync() override;402virtual void end_sync() override;403virtual void flush_queries() override;404virtual void finish() override;405406virtual bool is_flushing_queries() const override {407return physics_server_3d->is_flushing_queries();408}409410int get_process_info(ProcessInfo p_info) override {411return physics_server_3d->get_process_info(p_info);412}413414PhysicsServer3DWrapMT(PhysicsServer3D *p_contained, bool p_create_thread);415~PhysicsServer3DWrapMT();416417#undef ServerNameWrapMT418#undef ServerName419#undef server_name420#undef WRITE_ACTION421};422423#ifdef DEBUG_SYNC424#undef DEBUG_SYNC425#endif426#undef SYNC_DEBUG427428#ifdef DEBUG_ENABLED429#undef MAIN_THREAD_SYNC_WARN430#endif431432433