Path: blob/master/servers/physics_server_2d_wrap_mt.h
9887 views
/**************************************************************************/1/* physics_server_2d_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/object/worker_thread_pool.h"33#include "core/os/thread.h"34#include "core/templates/command_queue_mt.h"35#include "servers/physics_server_2d.h"3637#define ASYNC_COND_PUSH (Thread::get_caller_id() != server_thread)38#define ASYNC_COND_PUSH_AND_RET (Thread::get_caller_id() != server_thread && !(doing_sync.is_set() && Thread::is_main_thread()))39#define ASYNC_COND_PUSH_AND_SYNC (Thread::get_caller_id() != server_thread && !(doing_sync.is_set() && Thread::is_main_thread()))4041#ifdef DEBUG_SYNC42#define SYNC_DEBUG print_line("sync on: " + String(__FUNCTION__));43#else44#define SYNC_DEBUG45#endif4647#ifdef DEBUG_ENABLED48#ifdef DEV_ENABLED49#define MAIN_THREAD_SYNC_WARN WARN_PRINT("Call to " + String(__FUNCTION__) + " causing PhysicsServer2D synchronizations on every frame. This significantly affects performance.");50#else51#define MAIN_THREAD_SYNC_WARN52#endif53#endif5455class PhysicsServer2DWrapMT : public PhysicsServer2D {56mutable PhysicsServer2D *physics_server_2d = nullptr;5758mutable CommandQueueMT command_queue;5960Thread::ID server_thread = Thread::UNASSIGNED_ID;61WorkerThreadPool::TaskID server_task_id = WorkerThreadPool::INVALID_TASK_ID;62bool exit = false;63bool create_thread = false;64SafeFlag doing_sync;6566void _assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id);67void _thread_exit();68void _thread_loop();69void _thread_sync();7071public:72#define ServerName PhysicsServer2D73#define ServerNameWrapMT PhysicsServer2DWrapMT74#define server_name physics_server_2d75#define WRITE_ACTION7677#include "servers/server_wrap_mt_common.h"7879//FUNC1RID(shape,ShapeType); todo fix80FUNCRID(world_boundary_shape)81FUNCRID(separation_ray_shape)82FUNCRID(segment_shape)83FUNCRID(circle_shape)84FUNCRID(rectangle_shape)85FUNCRID(capsule_shape)86FUNCRID(convex_polygon_shape)87FUNCRID(concave_polygon_shape)8889FUNC2(shape_set_data, RID, const Variant &);90FUNC2(shape_set_custom_solver_bias, RID, real_t);9192FUNC1RC(ShapeType, shape_get_type, RID);93FUNC1RC(Variant, shape_get_data, RID);94FUNC1RC(real_t, shape_get_custom_solver_bias, RID);9596//these work well, but should be used from the main thread only97bool shape_collide(RID p_shape_A, const Transform2D &p_xform_A, const Vector2 &p_motion_A, RID p_shape_B, const Transform2D &p_xform_B, const Vector2 &p_motion_B, Vector2 *r_results, int p_result_max, int &r_result_count) override {98ERR_FAIL_COND_V(!Thread::is_main_thread(), false);99return physics_server_2d->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);100}101102/* SPACE API */103104FUNCRID(space);105FUNC2(space_set_active, RID, bool);106FUNC1RC(bool, space_is_active, RID);107108FUNC3(space_set_param, RID, SpaceParameter, real_t);109FUNC2RC(real_t, space_get_param, RID, SpaceParameter);110111// this function only works on physics process, errors and returns null otherwise112PhysicsDirectSpaceState2D *space_get_direct_state(RID p_space) override {113ERR_FAIL_COND_V(!Thread::is_main_thread(), nullptr);114return physics_server_2d->space_get_direct_state(p_space);115}116117FUNC2(space_set_debug_contacts, RID, int);118virtual Vector<Vector2> space_get_contacts(RID p_space) const override {119ERR_FAIL_COND_V(!Thread::is_main_thread(), Vector<Vector2>());120return physics_server_2d->space_get_contacts(p_space);121}122123virtual int space_get_contact_count(RID p_space) const override {124ERR_FAIL_COND_V(!Thread::is_main_thread(), 0);125return physics_server_2d->space_get_contact_count(p_space);126}127128/* AREA API */129130//FUNC0RID(area);131FUNCRID(area);132133FUNC2(area_set_space, RID, RID);134FUNC1RC(RID, area_get_space, RID);135136FUNC4(area_add_shape, RID, RID, const Transform2D &, bool);137FUNC3(area_set_shape, RID, int, RID);138FUNC3(area_set_shape_transform, RID, int, const Transform2D &);139FUNC3(area_set_shape_disabled, RID, int, bool);140141FUNC1RC(int, area_get_shape_count, RID);142FUNC2RC(RID, area_get_shape, RID, int);143FUNC2RC(Transform2D, area_get_shape_transform, RID, int);144FUNC2(area_remove_shape, RID, int);145FUNC1(area_clear_shapes, RID);146147FUNC2(area_attach_object_instance_id, RID, ObjectID);148FUNC1RC(ObjectID, area_get_object_instance_id, RID);149150FUNC2(area_attach_canvas_instance_id, RID, ObjectID);151FUNC1RC(ObjectID, area_get_canvas_instance_id, RID);152153FUNC3(area_set_param, RID, AreaParameter, const Variant &);154FUNC2(area_set_transform, RID, const Transform2D &);155156FUNC2RC(Variant, area_get_param, RID, AreaParameter);157FUNC1RC(Transform2D, area_get_transform, RID);158159FUNC2(area_set_collision_layer, RID, uint32_t);160FUNC1RC(uint32_t, area_get_collision_layer, RID);161162FUNC2(area_set_collision_mask, RID, uint32_t);163FUNC1RC(uint32_t, area_get_collision_mask, RID);164165FUNC2(area_set_monitorable, RID, bool);166FUNC2(area_set_pickable, RID, bool);167168FUNC2(area_set_monitor_callback, RID, const Callable &);169FUNC2(area_set_area_monitor_callback, RID, const Callable &);170171/* BODY API */172173//FUNC2RID(body,BodyMode,bool);174FUNCRID(body)175176FUNC2(body_set_space, RID, RID);177FUNC1RC(RID, body_get_space, RID);178179FUNC2(body_set_mode, RID, BodyMode);180FUNC1RC(BodyMode, body_get_mode, RID);181182FUNC4(body_add_shape, RID, RID, const Transform2D &, bool);183FUNC3(body_set_shape, RID, int, RID);184FUNC3(body_set_shape_transform, RID, int, const Transform2D &);185186FUNC1RC(int, body_get_shape_count, RID);187FUNC2RC(Transform2D, body_get_shape_transform, RID, int);188FUNC2RC(RID, body_get_shape, RID, int);189190FUNC3(body_set_shape_disabled, RID, int, bool);191FUNC4(body_set_shape_as_one_way_collision, RID, int, bool, real_t);192193FUNC2(body_remove_shape, RID, int);194FUNC1(body_clear_shapes, RID);195196FUNC2(body_attach_object_instance_id, RID, ObjectID);197FUNC1RC(ObjectID, body_get_object_instance_id, RID);198199FUNC2(body_attach_canvas_instance_id, RID, ObjectID);200FUNC1RC(ObjectID, body_get_canvas_instance_id, RID);201202FUNC2(body_set_continuous_collision_detection_mode, RID, CCDMode);203FUNC1RC(CCDMode, body_get_continuous_collision_detection_mode, 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);213214FUNC3(body_set_param, RID, BodyParameter, const Variant &);215FUNC2RC(Variant, body_get_param, RID, BodyParameter);216217FUNC1(body_reset_mass_properties, RID);218219FUNC3(body_set_state, RID, BodyState, const Variant &);220FUNC2RC(Variant, body_get_state, RID, BodyState);221222FUNC2(body_apply_central_impulse, RID, const Vector2 &);223FUNC2(body_apply_torque_impulse, RID, real_t);224FUNC3(body_apply_impulse, RID, const Vector2 &, const Vector2 &);225226FUNC2(body_apply_central_force, RID, const Vector2 &);227FUNC3(body_apply_force, RID, const Vector2 &, const Vector2 &);228FUNC2(body_apply_torque, RID, real_t);229230FUNC2(body_add_constant_central_force, RID, const Vector2 &);231FUNC3(body_add_constant_force, RID, const Vector2 &, const Vector2 &);232FUNC2(body_add_constant_torque, RID, real_t);233234FUNC2(body_set_constant_force, RID, const Vector2 &);235FUNC1RC(Vector2, body_get_constant_force, RID);236237FUNC2(body_set_constant_torque, RID, real_t);238FUNC1RC(real_t, body_get_constant_torque, RID);239240FUNC2(body_set_axis_velocity, RID, const Vector2 &);241242FUNC2(body_add_collision_exception, RID, RID);243FUNC2(body_remove_collision_exception, RID, RID);244FUNC2S(body_get_collision_exceptions, RID, List<RID> *);245246FUNC2(body_set_max_contacts_reported, RID, int);247FUNC1RC(int, body_get_max_contacts_reported, RID);248249FUNC2(body_set_contacts_reported_depth_threshold, RID, real_t);250FUNC1RC(real_t, body_get_contacts_reported_depth_threshold, RID);251252FUNC2(body_set_omit_force_integration, RID, bool);253FUNC1RC(bool, body_is_omitting_force_integration, RID);254255FUNC2(body_set_state_sync_callback, RID, const Callable &);256FUNC3(body_set_force_integration_callback, RID, const Callable &, const Variant &);257258bool body_collide_shape(RID p_body, int p_body_shape, RID p_shape, const Transform2D &p_shape_xform, const Vector2 &p_motion, Vector2 *r_results, int p_result_max, int &r_result_count) override {259return physics_server_2d->body_collide_shape(p_body, p_body_shape, p_shape, p_shape_xform, p_motion, r_results, p_result_max, r_result_count);260}261262FUNC2(body_set_pickable, RID, bool);263264bool body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result = nullptr) override {265ERR_FAIL_COND_V(!Thread::is_main_thread(), false);266return physics_server_2d->body_test_motion(p_body, p_parameters, r_result);267}268269// this function only works on physics process, errors and returns null otherwise270PhysicsDirectBodyState2D *body_get_direct_state(RID p_body) override {271ERR_FAIL_COND_V(!Thread::is_main_thread(), nullptr);272return physics_server_2d->body_get_direct_state(p_body);273}274275/* JOINT API */276277FUNCRID(joint)278279FUNC1(joint_clear, RID)280281FUNC3(joint_set_param, RID, JointParam, real_t);282FUNC2RC(real_t, joint_get_param, RID, JointParam);283284FUNC2(joint_disable_collisions_between_bodies, RID, const bool);285FUNC1RC(bool, joint_is_disabled_collisions_between_bodies, RID);286287///FUNC3RID(pin_joint,const Vector2&,RID,RID);288///FUNC5RID(groove_joint,const Vector2&,const Vector2&,const Vector2&,RID,RID);289///FUNC4RID(damped_spring_joint,const Vector2&,const Vector2&,RID,RID);290291//TODO need to convert this to FUNCRID, but it's a hassle..292293FUNC4(joint_make_pin, RID, const Vector2 &, RID, RID);294FUNC6(joint_make_groove, RID, const Vector2 &, const Vector2 &, const Vector2 &, RID, RID);295FUNC5(joint_make_damped_spring, RID, const Vector2 &, const Vector2 &, RID, RID);296297FUNC3(pin_joint_set_param, RID, PinJointParam, real_t);298FUNC2RC(real_t, pin_joint_get_param, RID, PinJointParam);299300FUNC3(pin_joint_set_flag, RID, PinJointFlag, bool);301FUNC2RC(bool, pin_joint_get_flag, RID, PinJointFlag);302303FUNC3(damped_spring_joint_set_param, RID, DampedSpringParam, real_t);304FUNC2RC(real_t, damped_spring_joint_get_param, RID, DampedSpringParam);305306FUNC1RC(JointType, joint_get_type, RID);307308/* MISC */309310FUNC1(free, RID);311FUNC1(set_active, bool);312313virtual void init() override;314virtual void step(real_t p_step) override;315virtual void sync() override;316virtual void end_sync() override;317virtual void flush_queries() override;318virtual void finish() override;319320virtual bool is_flushing_queries() const override {321return physics_server_2d->is_flushing_queries();322}323324int get_process_info(ProcessInfo p_info) override {325return physics_server_2d->get_process_info(p_info);326}327328PhysicsServer2DWrapMT(PhysicsServer2D *p_contained, bool p_create_thread);329~PhysicsServer2DWrapMT();330331#undef ServerNameWrapMT332#undef ServerName333#undef server_name334#undef WRITE_ACTION335};336337#ifdef DEBUG_SYNC338#undef DEBUG_SYNC339#endif340#undef SYNC_DEBUG341342#ifdef DEBUG_ENABLED343#undef MAIN_THREAD_SYNC_WARN344#endif345346347