physics_server_3d_wrap_mt.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. #define MAIN_THREAD_SYNC_WARN WARN_PRINT("Call to " + String(__FUNCTION__) + " causing PhysicsServer3D synchronizations on every frame. This significantly affects performance.");
  44. #endif
  45. class PhysicsServer3DWrapMT : public PhysicsServer3D {
  46. mutable PhysicsServer3D *physics_server_3d = nullptr;
  47. mutable CommandQueueMT command_queue;
  48. Thread::ID server_thread = Thread::UNASSIGNED_ID;
  49. WorkerThreadPool::TaskID server_task_id = WorkerThreadPool::INVALID_TASK_ID;
  50. bool exit = false;
  51. bool create_thread = false;
  52. void _assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id);
  53. void _thread_exit();
  54. void _thread_step(real_t p_delta);
  55. void _thread_loop();
  56. public:
  57. #define ServerName PhysicsServer3D
  58. #define ServerNameWrapMT PhysicsServer3DWrapMT
  59. #define server_name physics_server_3d
  60. #define WRITE_ACTION
  61. #include "servers/server_wrap_mt_common.h"
  62. //FUNC1RID(shape,ShapeType); todo fix
  63. FUNCRID(world_boundary_shape)
  64. FUNCRID(separation_ray_shape)
  65. FUNCRID(sphere_shape)
  66. FUNCRID(box_shape)
  67. FUNCRID(capsule_shape)
  68. FUNCRID(cylinder_shape)
  69. FUNCRID(convex_polygon_shape)
  70. FUNCRID(concave_polygon_shape)
  71. FUNCRID(heightmap_shape)
  72. FUNCRID(custom_shape)
  73. FUNC2(shape_set_data, RID, const Variant &);
  74. FUNC2(shape_set_custom_solver_bias, RID, real_t);
  75. FUNC2(shape_set_margin, RID, real_t)
  76. FUNC1RC(real_t, shape_get_margin, RID)
  77. FUNC1RC(ShapeType, shape_get_type, RID);
  78. FUNC1RC(Variant, shape_get_data, RID);
  79. FUNC1RC(real_t, shape_get_custom_solver_bias, RID);
  80. #if 0
  81. //these work well, but should be used from the main thread only
  82. 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) {
  83. ERR_FAIL_COND_V(!Thread::is_main_thread(), false);
  84. 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);
  85. }
  86. #endif
  87. /* SPACE API */
  88. FUNCRID(space);
  89. FUNC2(space_set_active, RID, bool);
  90. FUNC1RC(bool, space_is_active, RID);
  91. FUNC3(space_set_param, RID, SpaceParameter, real_t);
  92. FUNC2RC(real_t, space_get_param, RID, SpaceParameter);
  93. // this function only works on physics process, errors and returns null otherwise
  94. PhysicsDirectSpaceState3D *space_get_direct_state(RID p_space) override {
  95. ERR_FAIL_COND_V(!Thread::is_main_thread(), nullptr);
  96. return physics_server_3d->space_get_direct_state(p_space);
  97. }
  98. FUNC2(space_set_debug_contacts, RID, int);
  99. virtual Vector<Vector3> space_get_contacts(RID p_space) const override {
  100. ERR_FAIL_COND_V(!Thread::is_main_thread(), Vector<Vector3>());
  101. return physics_server_3d->space_get_contacts(p_space);
  102. }
  103. virtual int space_get_contact_count(RID p_space) const override {
  104. ERR_FAIL_COND_V(!Thread::is_main_thread(), 0);
  105. return physics_server_3d->space_get_contact_count(p_space);
  106. }
  107. /* AREA API */
  108. //FUNC0RID(area);
  109. FUNCRID(area);
  110. FUNC2(area_set_space, RID, RID);
  111. FUNC1RC(RID, area_get_space, RID);
  112. FUNC4(area_add_shape, RID, RID, const Transform3D &, bool);
  113. FUNC3(area_set_shape, RID, int, RID);
  114. FUNC3(area_set_shape_transform, RID, int, const Transform3D &);
  115. FUNC3(area_set_shape_disabled, RID, int, bool);
  116. FUNC1RC(int, area_get_shape_count, RID);
  117. FUNC2RC(RID, area_get_shape, RID, int);
  118. FUNC2RC(Transform3D, area_get_shape_transform, RID, int);
  119. FUNC2(area_remove_shape, RID, int);
  120. FUNC1(area_clear_shapes, RID);
  121. FUNC2(area_attach_object_instance_id, RID, ObjectID);
  122. FUNC1RC(ObjectID, area_get_object_instance_id, RID);
  123. FUNC3(area_set_param, RID, AreaParameter, const Variant &);
  124. FUNC2(area_set_transform, RID, const Transform3D &);
  125. FUNC2RC(Variant, area_get_param, RID, AreaParameter);
  126. FUNC1RC(Transform3D, area_get_transform, RID);
  127. FUNC2(area_set_collision_layer, RID, uint32_t);
  128. FUNC1RC(uint32_t, area_get_collision_layer, RID);
  129. FUNC2(area_set_collision_mask, RID, uint32_t);
  130. FUNC1RC(uint32_t, area_get_collision_mask, RID);
  131. FUNC2(area_set_monitorable, RID, bool);
  132. FUNC2(area_set_ray_pickable, RID, bool);
  133. FUNC2(area_set_monitor_callback, RID, const Callable &);
  134. FUNC2(area_set_area_monitor_callback, RID, const Callable &);
  135. /* BODY API */
  136. //FUNC2RID(body,BodyMode,bool);
  137. FUNCRID(body)
  138. FUNC2(body_set_space, RID, RID);
  139. FUNC1RC(RID, body_get_space, RID);
  140. FUNC2(body_set_mode, RID, BodyMode);
  141. FUNC1RC(BodyMode, body_get_mode, RID);
  142. FUNC4(body_add_shape, RID, RID, const Transform3D &, bool);
  143. FUNC3(body_set_shape, RID, int, RID);
  144. FUNC3(body_set_shape_transform, RID, int, const Transform3D &);
  145. FUNC1RC(int, body_get_shape_count, RID);
  146. FUNC2RC(Transform3D, body_get_shape_transform, RID, int);
  147. FUNC2RC(RID, body_get_shape, RID, int);
  148. FUNC3(body_set_shape_disabled, RID, int, bool);
  149. FUNC2(body_remove_shape, RID, int);
  150. FUNC1(body_clear_shapes, RID);
  151. FUNC2(body_attach_object_instance_id, RID, ObjectID);
  152. FUNC1RC(ObjectID, body_get_object_instance_id, RID);
  153. FUNC2(body_set_enable_continuous_collision_detection, RID, bool);
  154. FUNC1RC(bool, body_is_continuous_collision_detection_enabled, RID);
  155. FUNC2(body_set_collision_layer, RID, uint32_t);
  156. FUNC1RC(uint32_t, body_get_collision_layer, RID);
  157. FUNC2(body_set_collision_mask, RID, uint32_t);
  158. FUNC1RC(uint32_t, body_get_collision_mask, RID);
  159. FUNC2(body_set_collision_priority, RID, real_t);
  160. FUNC1RC(real_t, body_get_collision_priority, RID);
  161. FUNC2(body_set_user_flags, RID, uint32_t);
  162. FUNC1RC(uint32_t, body_get_user_flags, RID);
  163. FUNC3(body_set_param, RID, BodyParameter, const Variant &);
  164. FUNC2RC(Variant, body_get_param, RID, BodyParameter);
  165. FUNC1(body_reset_mass_properties, RID);
  166. FUNC3(body_set_state, RID, BodyState, const Variant &);
  167. FUNC2RC(Variant, body_get_state, RID, BodyState);
  168. FUNC2(body_apply_torque_impulse, RID, const Vector3 &);
  169. FUNC2(body_apply_central_impulse, RID, const Vector3 &);
  170. FUNC3(body_apply_impulse, RID, const Vector3 &, const Vector3 &);
  171. FUNC2(body_apply_central_force, RID, const Vector3 &);
  172. FUNC3(body_apply_force, RID, const Vector3 &, const Vector3 &);
  173. FUNC2(body_apply_torque, RID, const Vector3 &);
  174. FUNC2(body_add_constant_central_force, RID, const Vector3 &);
  175. FUNC3(body_add_constant_force, RID, const Vector3 &, const Vector3 &);
  176. FUNC2(body_add_constant_torque, RID, const Vector3 &);
  177. FUNC2(body_set_constant_force, RID, const Vector3 &);
  178. FUNC1RC(Vector3, body_get_constant_force, RID);
  179. FUNC2(body_set_constant_torque, RID, const Vector3 &);
  180. FUNC1RC(Vector3, body_get_constant_torque, RID);
  181. FUNC2(body_set_axis_velocity, RID, const Vector3 &);
  182. FUNC3(body_set_axis_lock, RID, BodyAxis, bool);
  183. FUNC2RC(bool, body_is_axis_locked, RID, BodyAxis);
  184. FUNC2(body_add_collision_exception, RID, RID);
  185. FUNC2(body_remove_collision_exception, RID, RID);
  186. FUNC2S(body_get_collision_exceptions, RID, List<RID> *);
  187. FUNC2(body_set_max_contacts_reported, RID, int);
  188. FUNC1RC(int, body_get_max_contacts_reported, RID);
  189. FUNC2(body_set_contacts_reported_depth_threshold, RID, real_t);
  190. FUNC1RC(real_t, body_get_contacts_reported_depth_threshold, RID);
  191. FUNC2(body_set_omit_force_integration, RID, bool);
  192. FUNC1RC(bool, body_is_omitting_force_integration, RID);
  193. FUNC2(body_set_state_sync_callback, RID, const Callable &);
  194. FUNC3(body_set_force_integration_callback, RID, const Callable &, const Variant &);
  195. FUNC2(body_set_ray_pickable, RID, bool);
  196. bool body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result = nullptr) override {
  197. ERR_FAIL_COND_V(!Thread::is_main_thread(), false);
  198. return physics_server_3d->body_test_motion(p_body, p_parameters, r_result);
  199. }
  200. // this function only works on physics process, errors and returns null otherwise
  201. PhysicsDirectBodyState3D *body_get_direct_state(RID p_body) override {
  202. ERR_FAIL_COND_V(!Thread::is_main_thread(), nullptr);
  203. return physics_server_3d->body_get_direct_state(p_body);
  204. }
  205. /* SOFT BODY API */
  206. FUNCRID(soft_body)
  207. FUNC2(soft_body_update_rendering_server, RID, PhysicsServer3DRenderingServerHandler *)
  208. FUNC2(soft_body_set_space, RID, RID)
  209. FUNC1RC(RID, soft_body_get_space, RID)
  210. FUNC2(soft_body_set_ray_pickable, RID, bool);
  211. FUNC2(soft_body_set_collision_layer, RID, uint32_t)
  212. FUNC1RC(uint32_t, soft_body_get_collision_layer, RID)
  213. FUNC2(soft_body_set_collision_mask, RID, uint32_t)
  214. FUNC1RC(uint32_t, soft_body_get_collision_mask, RID)
  215. FUNC2(soft_body_add_collision_exception, RID, RID)
  216. FUNC2(soft_body_remove_collision_exception, RID, RID)
  217. FUNC2S(soft_body_get_collision_exceptions, RID, List<RID> *)
  218. FUNC3(soft_body_set_state, RID, BodyState, const Variant &);
  219. FUNC2RC(Variant, soft_body_get_state, RID, BodyState);
  220. FUNC2(soft_body_set_transform, RID, const Transform3D &);
  221. FUNC2(soft_body_set_simulation_precision, RID, int);
  222. FUNC1RC(int, soft_body_get_simulation_precision, RID);
  223. FUNC2(soft_body_set_total_mass, RID, real_t);
  224. FUNC1RC(real_t, soft_body_get_total_mass, RID);
  225. FUNC2(soft_body_set_linear_stiffness, RID, real_t);
  226. FUNC1RC(real_t, soft_body_get_linear_stiffness, RID);
  227. FUNC2(soft_body_set_pressure_coefficient, RID, real_t);
  228. FUNC1RC(real_t, soft_body_get_pressure_coefficient, RID);
  229. FUNC2(soft_body_set_damping_coefficient, RID, real_t);
  230. FUNC1RC(real_t, soft_body_get_damping_coefficient, RID);
  231. FUNC2(soft_body_set_drag_coefficient, RID, real_t);
  232. FUNC1RC(real_t, soft_body_get_drag_coefficient, RID);
  233. FUNC2(soft_body_set_mesh, RID, RID);
  234. FUNC1RC(AABB, soft_body_get_bounds, RID);
  235. FUNC3(soft_body_move_point, RID, int, const Vector3 &);
  236. FUNC2RC(Vector3, soft_body_get_point_global_position, RID, int);
  237. FUNC1(soft_body_remove_all_pinned_points, RID);
  238. FUNC3(soft_body_pin_point, RID, int, bool);
  239. FUNC2RC(bool, soft_body_is_point_pinned, RID, int);
  240. /* JOINT API */
  241. FUNCRID(joint)
  242. FUNC1(joint_clear, RID)
  243. FUNC5(joint_make_pin, RID, RID, const Vector3 &, RID, const Vector3 &)
  244. FUNC3(pin_joint_set_param, RID, PinJointParam, real_t)
  245. FUNC2RC(real_t, pin_joint_get_param, RID, PinJointParam)
  246. FUNC2(pin_joint_set_local_a, RID, const Vector3 &)
  247. FUNC1RC(Vector3, pin_joint_get_local_a, RID)
  248. FUNC2(pin_joint_set_local_b, RID, const Vector3 &)
  249. FUNC1RC(Vector3, pin_joint_get_local_b, RID)
  250. FUNC5(joint_make_hinge, RID, RID, const Transform3D &, RID, const Transform3D &)
  251. FUNC7(joint_make_hinge_simple, RID, RID, const Vector3 &, const Vector3 &, RID, const Vector3 &, const Vector3 &)
  252. FUNC3(hinge_joint_set_param, RID, HingeJointParam, real_t)
  253. FUNC2RC(real_t, hinge_joint_get_param, RID, HingeJointParam)
  254. FUNC3(hinge_joint_set_flag, RID, HingeJointFlag, bool)
  255. FUNC2RC(bool, hinge_joint_get_flag, RID, HingeJointFlag)
  256. FUNC5(joint_make_slider, RID, RID, const Transform3D &, RID, const Transform3D &)
  257. FUNC3(slider_joint_set_param, RID, SliderJointParam, real_t)
  258. FUNC2RC(real_t, slider_joint_get_param, RID, SliderJointParam)
  259. FUNC5(joint_make_cone_twist, RID, RID, const Transform3D &, RID, const Transform3D &)
  260. FUNC3(cone_twist_joint_set_param, RID, ConeTwistJointParam, real_t)
  261. FUNC2RC(real_t, cone_twist_joint_get_param, RID, ConeTwistJointParam)
  262. FUNC5(joint_make_generic_6dof, RID, RID, const Transform3D &, RID, const Transform3D &)
  263. FUNC4(generic_6dof_joint_set_param, RID, Vector3::Axis, G6DOFJointAxisParam, real_t)
  264. FUNC3RC(real_t, generic_6dof_joint_get_param, RID, Vector3::Axis, G6DOFJointAxisParam)
  265. FUNC4(generic_6dof_joint_set_flag, RID, Vector3::Axis, G6DOFJointAxisFlag, bool)
  266. FUNC3RC(bool, generic_6dof_joint_get_flag, RID, Vector3::Axis, G6DOFJointAxisFlag)
  267. FUNC1RC(JointType, joint_get_type, RID);
  268. FUNC2(joint_set_solver_priority, RID, int);
  269. FUNC1RC(int, joint_get_solver_priority, RID);
  270. FUNC2(joint_disable_collisions_between_bodies, RID, bool);
  271. FUNC1RC(bool, joint_is_disabled_collisions_between_bodies, RID);
  272. /* MISC */
  273. FUNC1(free, RID);
  274. FUNC1(set_active, bool);
  275. virtual void init() override;
  276. virtual void step(real_t p_step) override;
  277. virtual void sync() override;
  278. virtual void end_sync() override;
  279. virtual void flush_queries() override;
  280. virtual void finish() override;
  281. virtual bool is_flushing_queries() const override {
  282. return physics_server_3d->is_flushing_queries();
  283. }
  284. int get_process_info(ProcessInfo p_info) override {
  285. return physics_server_3d->get_process_info(p_info);
  286. }
  287. PhysicsServer3DWrapMT(PhysicsServer3D *p_contained, bool p_create_thread);
  288. ~PhysicsServer3DWrapMT();
  289. #undef ServerNameWrapMT
  290. #undef ServerName
  291. #undef server_name
  292. #undef WRITE_ACTION
  293. };
  294. #ifdef DEBUG_SYNC
  295. #undef DEBUG_SYNC
  296. #endif
  297. #undef SYNC_DEBUG
  298. #ifdef DEBUG_ENABLED
  299. #undef MAIN_THREAD_SYNC_WARN
  300. #endif
  301. #endif // PHYSICS_SERVER_3D_WRAP_MT_H