physics_server_3d_wrap_mt.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /**************************************************************************/
  2. /* physics_server_3d_wrap_mt.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef PHYSICS_SERVER_3D_WRAP_MT_H
  31. #define PHYSICS_SERVER_3D_WRAP_MT_H
  32. #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"
  37. #ifdef DEBUG_SYNC
  38. #define SYNC_DEBUG print_line("sync on: " + String(__FUNCTION__));
  39. #else
  40. #define SYNC_DEBUG
  41. #endif
  42. #ifdef DEBUG_ENABLED
  43. #ifdef DEV_ENABLED
  44. #define MAIN_THREAD_SYNC_WARN WARN_PRINT("Call to " + String(__FUNCTION__) + " causing PhysicsServer3D synchronizations on every frame. This significantly affects performance.");
  45. #else
  46. #define MAIN_THREAD_SYNC_WARN
  47. #endif
  48. #endif
  49. class PhysicsServer3DWrapMT : public PhysicsServer3D {
  50. mutable PhysicsServer3D *physics_server_3d = nullptr;
  51. mutable CommandQueueMT command_queue;
  52. Thread::ID server_thread = Thread::UNASSIGNED_ID;
  53. WorkerThreadPool::TaskID server_task_id = WorkerThreadPool::INVALID_TASK_ID;
  54. bool exit = false;
  55. bool create_thread = false;
  56. void _assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id);
  57. void _thread_exit();
  58. void _thread_step(real_t p_delta);
  59. void _thread_loop();
  60. public:
  61. #define ServerName PhysicsServer3D
  62. #define ServerNameWrapMT PhysicsServer3DWrapMT
  63. #define server_name physics_server_3d
  64. #define WRITE_ACTION
  65. #include "servers/server_wrap_mt_common.h"
  66. //FUNC1RID(shape,ShapeType); todo fix
  67. FUNCRID(world_boundary_shape)
  68. FUNCRID(separation_ray_shape)
  69. FUNCRID(sphere_shape)
  70. FUNCRID(box_shape)
  71. FUNCRID(capsule_shape)
  72. FUNCRID(cylinder_shape)
  73. FUNCRID(convex_polygon_shape)
  74. FUNCRID(concave_polygon_shape)
  75. FUNCRID(heightmap_shape)
  76. FUNCRID(custom_shape)
  77. FUNC2(shape_set_data, RID, const Variant &);
  78. FUNC2(shape_set_custom_solver_bias, RID, real_t);
  79. FUNC2(shape_set_margin, RID, real_t)
  80. FUNC1RC(real_t, shape_get_margin, RID)
  81. FUNC1RC(ShapeType, shape_get_type, RID);
  82. FUNC1RC(Variant, shape_get_data, RID);
  83. FUNC1RC(real_t, shape_get_custom_solver_bias, RID);
  84. #if 0
  85. //these work well, but should be used from the main thread only
  86. bool 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) {
  87. ERR_FAIL_COND_V(!Thread::is_main_thread(), false);
  88. return 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);
  89. }
  90. #endif
  91. /* SPACE API */
  92. FUNCRID(space);
  93. FUNC2(space_set_active, RID, bool);
  94. FUNC1RC(bool, space_is_active, RID);
  95. FUNC3(space_set_param, RID, SpaceParameter, real_t);
  96. FUNC2RC(real_t, space_get_param, RID, SpaceParameter);
  97. // this function only works on physics process, errors and returns null otherwise
  98. PhysicsDirectSpaceState3D *space_get_direct_state(RID p_space) override {
  99. ERR_FAIL_COND_V(!Thread::is_main_thread(), nullptr);
  100. return physics_server_3d->space_get_direct_state(p_space);
  101. }
  102. FUNC2(space_set_debug_contacts, RID, int);
  103. virtual Vector<Vector3> space_get_contacts(RID p_space) const override {
  104. ERR_FAIL_COND_V(!Thread::is_main_thread(), Vector<Vector3>());
  105. return physics_server_3d->space_get_contacts(p_space);
  106. }
  107. virtual int space_get_contact_count(RID p_space) const override {
  108. ERR_FAIL_COND_V(!Thread::is_main_thread(), 0);
  109. return physics_server_3d->space_get_contact_count(p_space);
  110. }
  111. /* AREA API */
  112. //FUNC0RID(area);
  113. FUNCRID(area);
  114. FUNC2(area_set_space, RID, RID);
  115. FUNC1RC(RID, area_get_space, RID);
  116. FUNC4(area_add_shape, RID, RID, const Transform3D &, bool);
  117. FUNC3(area_set_shape, RID, int, RID);
  118. FUNC3(area_set_shape_transform, RID, int, const Transform3D &);
  119. FUNC3(area_set_shape_disabled, RID, int, bool);
  120. FUNC1RC(int, area_get_shape_count, RID);
  121. FUNC2RC(RID, area_get_shape, RID, int);
  122. FUNC2RC(Transform3D, area_get_shape_transform, RID, int);
  123. FUNC2(area_remove_shape, RID, int);
  124. FUNC1(area_clear_shapes, RID);
  125. FUNC2(area_attach_object_instance_id, RID, ObjectID);
  126. FUNC1RC(ObjectID, area_get_object_instance_id, RID);
  127. FUNC3(area_set_param, RID, AreaParameter, const Variant &);
  128. FUNC2(area_set_transform, RID, const Transform3D &);
  129. FUNC2RC(Variant, area_get_param, RID, AreaParameter);
  130. FUNC1RC(Transform3D, area_get_transform, RID);
  131. FUNC2(area_set_collision_layer, RID, uint32_t);
  132. FUNC1RC(uint32_t, area_get_collision_layer, RID);
  133. FUNC2(area_set_collision_mask, RID, uint32_t);
  134. FUNC1RC(uint32_t, area_get_collision_mask, RID);
  135. FUNC2(area_set_monitorable, RID, bool);
  136. FUNC2(area_set_ray_pickable, RID, bool);
  137. FUNC2(area_set_monitor_callback, RID, const Callable &);
  138. FUNC2(area_set_area_monitor_callback, RID, const Callable &);
  139. /* BODY API */
  140. //FUNC2RID(body,BodyMode,bool);
  141. FUNCRID(body)
  142. FUNC2(body_set_space, RID, RID);
  143. FUNC1RC(RID, body_get_space, RID);
  144. FUNC2(body_set_mode, RID, BodyMode);
  145. FUNC1RC(BodyMode, body_get_mode, RID);
  146. FUNC4(body_add_shape, RID, RID, const Transform3D &, bool);
  147. FUNC3(body_set_shape, RID, int, RID);
  148. FUNC3(body_set_shape_transform, RID, int, const Transform3D &);
  149. FUNC1RC(int, body_get_shape_count, RID);
  150. FUNC2RC(Transform3D, body_get_shape_transform, RID, int);
  151. FUNC2RC(RID, body_get_shape, RID, int);
  152. FUNC3(body_set_shape_disabled, RID, int, bool);
  153. FUNC2(body_remove_shape, RID, int);
  154. FUNC1(body_clear_shapes, RID);
  155. FUNC2(body_attach_object_instance_id, RID, ObjectID);
  156. FUNC1RC(ObjectID, body_get_object_instance_id, RID);
  157. FUNC2(body_set_enable_continuous_collision_detection, RID, bool);
  158. FUNC1RC(bool, body_is_continuous_collision_detection_enabled, RID);
  159. FUNC2(body_set_collision_layer, RID, uint32_t);
  160. FUNC1RC(uint32_t, body_get_collision_layer, RID);
  161. FUNC2(body_set_collision_mask, RID, uint32_t);
  162. FUNC1RC(uint32_t, body_get_collision_mask, RID);
  163. FUNC2(body_set_collision_priority, RID, real_t);
  164. FUNC1RC(real_t, body_get_collision_priority, RID);
  165. FUNC2(body_set_user_flags, RID, uint32_t);
  166. FUNC1RC(uint32_t, body_get_user_flags, RID);
  167. FUNC3(body_set_param, RID, BodyParameter, const Variant &);
  168. FUNC2RC(Variant, body_get_param, RID, BodyParameter);
  169. FUNC1(body_reset_mass_properties, RID);
  170. FUNC3(body_set_state, RID, BodyState, const Variant &);
  171. FUNC2RC(Variant, body_get_state, RID, BodyState);
  172. FUNC2(body_apply_torque_impulse, RID, const Vector3 &);
  173. FUNC2(body_apply_central_impulse, RID, const Vector3 &);
  174. FUNC3(body_apply_impulse, RID, const Vector3 &, const Vector3 &);
  175. FUNC2(body_apply_central_force, RID, const Vector3 &);
  176. FUNC3(body_apply_force, RID, const Vector3 &, const Vector3 &);
  177. FUNC2(body_apply_torque, RID, const Vector3 &);
  178. FUNC2(body_add_constant_central_force, RID, const Vector3 &);
  179. FUNC3(body_add_constant_force, RID, const Vector3 &, const Vector3 &);
  180. FUNC2(body_add_constant_torque, RID, const Vector3 &);
  181. FUNC2(body_set_constant_force, RID, const Vector3 &);
  182. FUNC1RC(Vector3, body_get_constant_force, RID);
  183. FUNC2(body_set_constant_torque, RID, const Vector3 &);
  184. FUNC1RC(Vector3, body_get_constant_torque, RID);
  185. FUNC2(body_set_axis_velocity, RID, const Vector3 &);
  186. FUNC3(body_set_axis_lock, RID, BodyAxis, bool);
  187. FUNC2RC(bool, body_is_axis_locked, RID, BodyAxis);
  188. FUNC2(body_add_collision_exception, RID, RID);
  189. FUNC2(body_remove_collision_exception, RID, RID);
  190. FUNC2S(body_get_collision_exceptions, RID, List<RID> *);
  191. FUNC2(body_set_max_contacts_reported, RID, int);
  192. FUNC1RC(int, body_get_max_contacts_reported, RID);
  193. FUNC2(body_set_contacts_reported_depth_threshold, RID, real_t);
  194. FUNC1RC(real_t, body_get_contacts_reported_depth_threshold, RID);
  195. FUNC2(body_set_omit_force_integration, RID, bool);
  196. FUNC1RC(bool, body_is_omitting_force_integration, RID);
  197. FUNC2(body_set_state_sync_callback, RID, const Callable &);
  198. FUNC3(body_set_force_integration_callback, RID, const Callable &, const Variant &);
  199. FUNC2(body_set_ray_pickable, RID, bool);
  200. bool body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result = nullptr) override {
  201. ERR_FAIL_COND_V(!Thread::is_main_thread(), false);
  202. return physics_server_3d->body_test_motion(p_body, p_parameters, r_result);
  203. }
  204. // this function only works on physics process, errors and returns null otherwise
  205. PhysicsDirectBodyState3D *body_get_direct_state(RID p_body) override {
  206. ERR_FAIL_COND_V(!Thread::is_main_thread(), nullptr);
  207. return physics_server_3d->body_get_direct_state(p_body);
  208. }
  209. /* SOFT BODY API */
  210. FUNCRID(soft_body)
  211. FUNC2(soft_body_update_rendering_server, RID, PhysicsServer3DRenderingServerHandler *)
  212. FUNC2(soft_body_set_space, RID, RID)
  213. FUNC1RC(RID, soft_body_get_space, RID)
  214. FUNC2(soft_body_set_ray_pickable, RID, bool);
  215. FUNC2(soft_body_set_collision_layer, RID, uint32_t)
  216. FUNC1RC(uint32_t, soft_body_get_collision_layer, RID)
  217. FUNC2(soft_body_set_collision_mask, RID, uint32_t)
  218. FUNC1RC(uint32_t, soft_body_get_collision_mask, RID)
  219. FUNC2(soft_body_add_collision_exception, RID, RID)
  220. FUNC2(soft_body_remove_collision_exception, RID, RID)
  221. FUNC2S(soft_body_get_collision_exceptions, RID, List<RID> *)
  222. FUNC3(soft_body_set_state, RID, BodyState, const Variant &);
  223. FUNC2RC(Variant, soft_body_get_state, RID, BodyState);
  224. FUNC2(soft_body_set_transform, RID, const Transform3D &);
  225. FUNC2(soft_body_set_simulation_precision, RID, int);
  226. FUNC1RC(int, soft_body_get_simulation_precision, RID);
  227. FUNC2(soft_body_set_total_mass, RID, real_t);
  228. FUNC1RC(real_t, soft_body_get_total_mass, RID);
  229. FUNC2(soft_body_set_linear_stiffness, RID, real_t);
  230. FUNC1RC(real_t, soft_body_get_linear_stiffness, RID);
  231. FUNC2(soft_body_set_pressure_coefficient, RID, real_t);
  232. FUNC1RC(real_t, soft_body_get_pressure_coefficient, RID);
  233. FUNC2(soft_body_set_damping_coefficient, RID, real_t);
  234. FUNC1RC(real_t, soft_body_get_damping_coefficient, RID);
  235. FUNC2(soft_body_set_drag_coefficient, RID, real_t);
  236. FUNC1RC(real_t, soft_body_get_drag_coefficient, RID);
  237. FUNC2(soft_body_set_mesh, RID, RID);
  238. FUNC1RC(AABB, soft_body_get_bounds, RID);
  239. FUNC3(soft_body_move_point, RID, int, const Vector3 &);
  240. FUNC2RC(Vector3, soft_body_get_point_global_position, RID, int);
  241. FUNC1(soft_body_remove_all_pinned_points, RID);
  242. FUNC3(soft_body_pin_point, RID, int, bool);
  243. FUNC2RC(bool, soft_body_is_point_pinned, RID, int);
  244. /* JOINT API */
  245. FUNCRID(joint)
  246. FUNC1(joint_clear, RID)
  247. FUNC5(joint_make_pin, RID, RID, const Vector3 &, RID, const Vector3 &)
  248. FUNC3(pin_joint_set_param, RID, PinJointParam, real_t)
  249. FUNC2RC(real_t, pin_joint_get_param, RID, PinJointParam)
  250. FUNC2(pin_joint_set_local_a, RID, const Vector3 &)
  251. FUNC1RC(Vector3, pin_joint_get_local_a, RID)
  252. FUNC2(pin_joint_set_local_b, RID, const Vector3 &)
  253. FUNC1RC(Vector3, pin_joint_get_local_b, RID)
  254. FUNC5(joint_make_hinge, RID, RID, const Transform3D &, RID, const Transform3D &)
  255. FUNC7(joint_make_hinge_simple, RID, RID, const Vector3 &, const Vector3 &, RID, const Vector3 &, const Vector3 &)
  256. FUNC3(hinge_joint_set_param, RID, HingeJointParam, real_t)
  257. FUNC2RC(real_t, hinge_joint_get_param, RID, HingeJointParam)
  258. FUNC3(hinge_joint_set_flag, RID, HingeJointFlag, bool)
  259. FUNC2RC(bool, hinge_joint_get_flag, RID, HingeJointFlag)
  260. FUNC5(joint_make_slider, RID, RID, const Transform3D &, RID, const Transform3D &)
  261. FUNC3(slider_joint_set_param, RID, SliderJointParam, real_t)
  262. FUNC2RC(real_t, slider_joint_get_param, RID, SliderJointParam)
  263. FUNC5(joint_make_cone_twist, RID, RID, const Transform3D &, RID, const Transform3D &)
  264. FUNC3(cone_twist_joint_set_param, RID, ConeTwistJointParam, real_t)
  265. FUNC2RC(real_t, cone_twist_joint_get_param, RID, ConeTwistJointParam)
  266. FUNC5(joint_make_generic_6dof, RID, RID, const Transform3D &, RID, const Transform3D &)
  267. FUNC4(generic_6dof_joint_set_param, RID, Vector3::Axis, G6DOFJointAxisParam, real_t)
  268. FUNC3RC(real_t, generic_6dof_joint_get_param, RID, Vector3::Axis, G6DOFJointAxisParam)
  269. FUNC4(generic_6dof_joint_set_flag, RID, Vector3::Axis, G6DOFJointAxisFlag, bool)
  270. FUNC3RC(bool, generic_6dof_joint_get_flag, RID, Vector3::Axis, G6DOFJointAxisFlag)
  271. FUNC1RC(JointType, joint_get_type, RID);
  272. FUNC2(joint_set_solver_priority, RID, int);
  273. FUNC1RC(int, joint_get_solver_priority, RID);
  274. FUNC2(joint_disable_collisions_between_bodies, RID, bool);
  275. FUNC1RC(bool, joint_is_disabled_collisions_between_bodies, RID);
  276. /* MISC */
  277. FUNC1(free, RID);
  278. FUNC1(set_active, bool);
  279. virtual void init() override;
  280. virtual void step(real_t p_step) override;
  281. virtual void sync() override;
  282. virtual void end_sync() override;
  283. virtual void flush_queries() override;
  284. virtual void finish() override;
  285. virtual bool is_flushing_queries() const override {
  286. return physics_server_3d->is_flushing_queries();
  287. }
  288. int get_process_info(ProcessInfo p_info) override {
  289. return physics_server_3d->get_process_info(p_info);
  290. }
  291. PhysicsServer3DWrapMT(PhysicsServer3D *p_contained, bool p_create_thread);
  292. ~PhysicsServer3DWrapMT();
  293. #undef ServerNameWrapMT
  294. #undef ServerName
  295. #undef server_name
  296. #undef WRITE_ACTION
  297. };
  298. #ifdef DEBUG_SYNC
  299. #undef DEBUG_SYNC
  300. #endif
  301. #undef SYNC_DEBUG
  302. #ifdef DEBUG_ENABLED
  303. #undef MAIN_THREAD_SYNC_WARN
  304. #endif
  305. #endif // PHYSICS_SERVER_3D_WRAP_MT_H