Path: blob/master/servers/physics_server_3d_wrap_mt.h
9887 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_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 {57mutable PhysicsServer3D *physics_server_3d = nullptr;5859mutable CommandQueueMT command_queue;6061Thread::ID server_thread = Thread::UNASSIGNED_ID;62WorkerThreadPool::TaskID server_task_id = WorkerThreadPool::INVALID_TASK_ID;63bool exit = false;64bool create_thread = false;65SafeFlag doing_sync;6667void _assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id);68void _thread_exit();69void _thread_step(real_t p_delta);70void _thread_loop();71void _thread_sync();7273public:74#define ServerName PhysicsServer3D75#define ServerNameWrapMT PhysicsServer3DWrapMT76#define server_name physics_server_3d77#define WRITE_ACTION7879#include "servers/server_wrap_mt_common.h"8081//FUNC1RID(shape,ShapeType); todo fix82FUNCRID(world_boundary_shape)83FUNCRID(separation_ray_shape)84FUNCRID(sphere_shape)85FUNCRID(box_shape)86FUNCRID(capsule_shape)87FUNCRID(cylinder_shape)88FUNCRID(convex_polygon_shape)89FUNCRID(concave_polygon_shape)90FUNCRID(heightmap_shape)91FUNCRID(custom_shape)9293FUNC2(shape_set_data, RID, const Variant &);94FUNC2(shape_set_custom_solver_bias, RID, real_t);9596FUNC2(shape_set_margin, RID, real_t)97FUNC1RC(real_t, shape_get_margin, RID)9899FUNC1RC(ShapeType, shape_get_type, RID);100FUNC1RC(Variant, shape_get_data, RID);101FUNC1RC(real_t, shape_get_custom_solver_bias, RID);102#if 0103//these work well, but should be used from the main thread only104bool 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) {105ERR_FAIL_COND_V(!Thread::is_main_thread(), false);106return 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);107}108#endif109/* SPACE API */110111FUNCRID(space);112FUNC2(space_set_active, RID, bool);113FUNC1RC(bool, space_is_active, RID);114115FUNC3(space_set_param, RID, SpaceParameter, real_t);116FUNC2RC(real_t, space_get_param, RID, SpaceParameter);117118// this function only works on physics process, errors and returns null otherwise119PhysicsDirectSpaceState3D *space_get_direct_state(RID p_space) override {120ERR_FAIL_COND_V(!Thread::is_main_thread(), nullptr);121return physics_server_3d->space_get_direct_state(p_space);122}123124FUNC2(space_set_debug_contacts, RID, int);125virtual Vector<Vector3> space_get_contacts(RID p_space) const override {126ERR_FAIL_COND_V(!Thread::is_main_thread(), Vector<Vector3>());127return physics_server_3d->space_get_contacts(p_space);128}129130virtual int space_get_contact_count(RID p_space) const override {131ERR_FAIL_COND_V(!Thread::is_main_thread(), 0);132return physics_server_3d->space_get_contact_count(p_space);133}134135/* AREA API */136137//FUNC0RID(area);138FUNCRID(area);139140FUNC2(area_set_space, RID, RID);141FUNC1RC(RID, area_get_space, RID);142143FUNC4(area_add_shape, RID, RID, const Transform3D &, bool);144FUNC3(area_set_shape, RID, int, RID);145FUNC3(area_set_shape_transform, RID, int, const Transform3D &);146FUNC3(area_set_shape_disabled, RID, int, bool);147148FUNC1RC(int, area_get_shape_count, RID);149FUNC2RC(RID, area_get_shape, RID, int);150FUNC2RC(Transform3D, area_get_shape_transform, RID, int);151FUNC2(area_remove_shape, RID, int);152FUNC1(area_clear_shapes, RID);153154FUNC2(area_attach_object_instance_id, RID, ObjectID);155FUNC1RC(ObjectID, area_get_object_instance_id, RID);156157FUNC3(area_set_param, RID, AreaParameter, const Variant &);158FUNC2(area_set_transform, RID, const Transform3D &);159160FUNC2RC(Variant, area_get_param, RID, AreaParameter);161FUNC1RC(Transform3D, area_get_transform, RID);162163FUNC2(area_set_collision_layer, RID, uint32_t);164FUNC1RC(uint32_t, area_get_collision_layer, RID);165166FUNC2(area_set_collision_mask, RID, uint32_t);167FUNC1RC(uint32_t, area_get_collision_mask, RID);168169FUNC2(area_set_monitorable, RID, bool);170FUNC2(area_set_ray_pickable, RID, bool);171172FUNC2(area_set_monitor_callback, RID, const Callable &);173FUNC2(area_set_area_monitor_callback, RID, const Callable &);174175/* BODY API */176177//FUNC2RID(body,BodyMode,bool);178FUNCRID(body)179180FUNC2(body_set_space, RID, RID);181FUNC1RC(RID, body_get_space, RID);182183FUNC2(body_set_mode, RID, BodyMode);184FUNC1RC(BodyMode, body_get_mode, RID);185186FUNC4(body_add_shape, RID, RID, const Transform3D &, bool);187FUNC3(body_set_shape, RID, int, RID);188FUNC3(body_set_shape_transform, RID, int, const Transform3D &);189190FUNC1RC(int, body_get_shape_count, RID);191FUNC2RC(Transform3D, body_get_shape_transform, RID, int);192FUNC2RC(RID, body_get_shape, RID, int);193194FUNC3(body_set_shape_disabled, RID, int, bool);195196FUNC2(body_remove_shape, RID, int);197FUNC1(body_clear_shapes, RID);198199FUNC2(body_attach_object_instance_id, RID, ObjectID);200FUNC1RC(ObjectID, body_get_object_instance_id, RID);201202FUNC2(body_set_enable_continuous_collision_detection, RID, bool);203FUNC1RC(bool, body_is_continuous_collision_detection_enabled, RID);204205FUNC2(body_set_collision_layer, RID, uint32_t);206FUNC1RC(uint32_t, body_get_collision_layer, RID);207208FUNC2(body_set_collision_mask, RID, uint32_t);209FUNC1RC(uint32_t, body_get_collision_mask, RID);210211FUNC2(body_set_collision_priority, RID, real_t);212FUNC1RC(real_t, body_get_collision_priority, RID);213214FUNC2(body_set_user_flags, RID, uint32_t);215FUNC1RC(uint32_t, body_get_user_flags, RID);216217FUNC3(body_set_param, RID, BodyParameter, const Variant &);218FUNC2RC(Variant, body_get_param, RID, BodyParameter);219220FUNC1(body_reset_mass_properties, RID);221222FUNC3(body_set_state, RID, BodyState, const Variant &);223FUNC2RC(Variant, body_get_state, RID, BodyState);224225FUNC2(body_apply_torque_impulse, RID, const Vector3 &);226FUNC2(body_apply_central_impulse, RID, const Vector3 &);227FUNC3(body_apply_impulse, RID, const Vector3 &, const Vector3 &);228229FUNC2(body_apply_central_force, RID, const Vector3 &);230FUNC3(body_apply_force, RID, const Vector3 &, const Vector3 &);231FUNC2(body_apply_torque, RID, const Vector3 &);232233FUNC3(soft_body_apply_point_impulse, RID, int, const Vector3 &);234FUNC3(soft_body_apply_point_force, RID, int, const Vector3 &);235FUNC2(soft_body_apply_central_impulse, RID, const Vector3 &);236FUNC2(soft_body_apply_central_force, RID, const Vector3 &);237238FUNC2(body_add_constant_central_force, RID, const Vector3 &);239FUNC3(body_add_constant_force, RID, const Vector3 &, const Vector3 &);240FUNC2(body_add_constant_torque, RID, const Vector3 &);241242FUNC2(body_set_constant_force, RID, const Vector3 &);243FUNC1RC(Vector3, body_get_constant_force, RID);244245FUNC2(body_set_constant_torque, RID, const Vector3 &);246FUNC1RC(Vector3, body_get_constant_torque, RID);247248FUNC2(body_set_axis_velocity, RID, const Vector3 &);249250FUNC3(body_set_axis_lock, RID, BodyAxis, bool);251FUNC2RC(bool, body_is_axis_locked, RID, BodyAxis);252253FUNC2(body_add_collision_exception, RID, RID);254FUNC2(body_remove_collision_exception, RID, RID);255FUNC2S(body_get_collision_exceptions, RID, List<RID> *);256257FUNC2(body_set_max_contacts_reported, RID, int);258FUNC1RC(int, body_get_max_contacts_reported, RID);259260FUNC2(body_set_contacts_reported_depth_threshold, RID, real_t);261FUNC1RC(real_t, body_get_contacts_reported_depth_threshold, RID);262263FUNC2(body_set_omit_force_integration, RID, bool);264FUNC1RC(bool, body_is_omitting_force_integration, RID);265266FUNC2(body_set_state_sync_callback, RID, const Callable &);267FUNC3(body_set_force_integration_callback, RID, const Callable &, const Variant &);268269FUNC2(body_set_ray_pickable, RID, bool);270271bool body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result = nullptr) override {272ERR_FAIL_COND_V(!Thread::is_main_thread(), false);273return physics_server_3d->body_test_motion(p_body, p_parameters, r_result);274}275276// this function only works on physics process, errors and returns null otherwise277PhysicsDirectBodyState3D *body_get_direct_state(RID p_body) override {278ERR_FAIL_COND_V(!Thread::is_main_thread(), nullptr);279return physics_server_3d->body_get_direct_state(p_body);280}281282/* SOFT BODY API */283284FUNCRID(soft_body)285286FUNC2(soft_body_update_rendering_server, RID, PhysicsServer3DRenderingServerHandler *)287288FUNC2(soft_body_set_space, RID, RID)289FUNC1RC(RID, soft_body_get_space, RID)290291FUNC2(soft_body_set_ray_pickable, RID, bool);292293FUNC2(soft_body_set_collision_layer, RID, uint32_t)294FUNC1RC(uint32_t, soft_body_get_collision_layer, RID)295296FUNC2(soft_body_set_collision_mask, RID, uint32_t)297FUNC1RC(uint32_t, soft_body_get_collision_mask, RID)298299FUNC2(soft_body_add_collision_exception, RID, RID)300FUNC2(soft_body_remove_collision_exception, RID, RID)301FUNC2S(soft_body_get_collision_exceptions, RID, List<RID> *)302303FUNC3(soft_body_set_state, RID, BodyState, const Variant &);304FUNC2RC(Variant, soft_body_get_state, RID, BodyState);305306FUNC2(soft_body_set_transform, RID, const Transform3D &);307308FUNC2(soft_body_set_simulation_precision, RID, int);309FUNC1RC(int, soft_body_get_simulation_precision, RID);310311FUNC2(soft_body_set_total_mass, RID, real_t);312FUNC1RC(real_t, soft_body_get_total_mass, RID);313314FUNC2(soft_body_set_linear_stiffness, RID, real_t);315FUNC1RC(real_t, soft_body_get_linear_stiffness, RID);316317FUNC2(soft_body_set_shrinking_factor, RID, real_t);318FUNC1RC(real_t, soft_body_get_shrinking_factor, RID);319320FUNC2(soft_body_set_pressure_coefficient, RID, real_t);321FUNC1RC(real_t, soft_body_get_pressure_coefficient, RID);322323FUNC2(soft_body_set_damping_coefficient, RID, real_t);324FUNC1RC(real_t, soft_body_get_damping_coefficient, RID);325326FUNC2(soft_body_set_drag_coefficient, RID, real_t);327FUNC1RC(real_t, soft_body_get_drag_coefficient, RID);328329FUNC2(soft_body_set_mesh, RID, RID);330331FUNC1RC(AABB, soft_body_get_bounds, RID);332333FUNC3(soft_body_move_point, RID, int, const Vector3 &);334FUNC2RC(Vector3, soft_body_get_point_global_position, RID, int);335336FUNC1(soft_body_remove_all_pinned_points, RID);337FUNC3(soft_body_pin_point, RID, int, bool);338FUNC2RC(bool, soft_body_is_point_pinned, RID, int);339340/* JOINT API */341342FUNCRID(joint)343344FUNC1(joint_clear, RID)345346FUNC5(joint_make_pin, RID, RID, const Vector3 &, RID, const Vector3 &)347348FUNC3(pin_joint_set_param, RID, PinJointParam, real_t)349FUNC2RC(real_t, pin_joint_get_param, RID, PinJointParam)350351FUNC2(pin_joint_set_local_a, RID, const Vector3 &)352FUNC1RC(Vector3, pin_joint_get_local_a, RID)353354FUNC2(pin_joint_set_local_b, RID, const Vector3 &)355FUNC1RC(Vector3, pin_joint_get_local_b, RID)356357FUNC5(joint_make_hinge, RID, RID, const Transform3D &, RID, const Transform3D &)358FUNC7(joint_make_hinge_simple, RID, RID, const Vector3 &, const Vector3 &, RID, const Vector3 &, const Vector3 &)359360FUNC3(hinge_joint_set_param, RID, HingeJointParam, real_t)361FUNC2RC(real_t, hinge_joint_get_param, RID, HingeJointParam)362363FUNC3(hinge_joint_set_flag, RID, HingeJointFlag, bool)364FUNC2RC(bool, hinge_joint_get_flag, RID, HingeJointFlag)365366FUNC5(joint_make_slider, RID, RID, const Transform3D &, RID, const Transform3D &)367368FUNC3(slider_joint_set_param, RID, SliderJointParam, real_t)369FUNC2RC(real_t, slider_joint_get_param, RID, SliderJointParam)370371FUNC5(joint_make_cone_twist, RID, RID, const Transform3D &, RID, const Transform3D &)372373FUNC3(cone_twist_joint_set_param, RID, ConeTwistJointParam, real_t)374FUNC2RC(real_t, cone_twist_joint_get_param, RID, ConeTwistJointParam)375376FUNC5(joint_make_generic_6dof, RID, RID, const Transform3D &, RID, const Transform3D &)377378FUNC4(generic_6dof_joint_set_param, RID, Vector3::Axis, G6DOFJointAxisParam, real_t)379FUNC3RC(real_t, generic_6dof_joint_get_param, RID, Vector3::Axis, G6DOFJointAxisParam)380381FUNC4(generic_6dof_joint_set_flag, RID, Vector3::Axis, G6DOFJointAxisFlag, bool)382FUNC3RC(bool, generic_6dof_joint_get_flag, RID, Vector3::Axis, G6DOFJointAxisFlag)383384FUNC1RC(JointType, joint_get_type, RID);385386FUNC2(joint_set_solver_priority, RID, int);387FUNC1RC(int, joint_get_solver_priority, RID);388389FUNC2(joint_disable_collisions_between_bodies, RID, bool);390FUNC1RC(bool, joint_is_disabled_collisions_between_bodies, RID);391392/* MISC */393394FUNC1(free, RID);395FUNC1(set_active, bool);396397virtual void init() override;398virtual void step(real_t p_step) override;399virtual void sync() override;400virtual void end_sync() override;401virtual void flush_queries() override;402virtual void finish() override;403404virtual bool is_flushing_queries() const override {405return physics_server_3d->is_flushing_queries();406}407408int get_process_info(ProcessInfo p_info) override {409return physics_server_3d->get_process_info(p_info);410}411412PhysicsServer3DWrapMT(PhysicsServer3D *p_contained, bool p_create_thread);413~PhysicsServer3DWrapMT();414415#undef ServerNameWrapMT416#undef ServerName417#undef server_name418#undef WRITE_ACTION419};420421#ifdef DEBUG_SYNC422#undef DEBUG_SYNC423#endif424#undef SYNC_DEBUG425426#ifdef DEBUG_ENABLED427#undef MAIN_THREAD_SYNC_WARN428#endif429430431