physics_server_2d_wrap_mt.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**************************************************************************/
  2. /* physics_server_2d_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_2D_WRAP_MT_H
  31. #define PHYSICS_SERVER_2D_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 "core/templates/safe_refcount.h"
  37. #include "servers/physics_server_2d.h"
  38. #ifdef DEBUG_SYNC
  39. #define SYNC_DEBUG print_line("sync on: " + String(__FUNCTION__));
  40. #else
  41. #define SYNC_DEBUG
  42. #endif
  43. #ifdef DEBUG_ENABLED
  44. #define MAIN_THREAD_SYNC_WARN WARN_PRINT("Call to " + String(__FUNCTION__) + " causing PhysicsServer2D synchronizations on every frame. This significantly affects performance.");
  45. #endif
  46. class PhysicsServer2DWrapMT : public PhysicsServer2D {
  47. mutable PhysicsServer2D *physics_server_2d = nullptr;
  48. mutable CommandQueueMT command_queue;
  49. Thread::ID server_thread = Thread::UNASSIGNED_ID;
  50. WorkerThreadPool::TaskID server_task_id = WorkerThreadPool::INVALID_TASK_ID;
  51. bool exit = false;
  52. bool create_thread = false;
  53. void _assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id);
  54. void _thread_exit();
  55. void _thread_loop();
  56. public:
  57. #define ServerName PhysicsServer2D
  58. #define ServerNameWrapMT PhysicsServer2DWrapMT
  59. #define server_name physics_server_2d
  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(segment_shape)
  66. FUNCRID(circle_shape)
  67. FUNCRID(rectangle_shape)
  68. FUNCRID(capsule_shape)
  69. FUNCRID(convex_polygon_shape)
  70. FUNCRID(concave_polygon_shape)
  71. FUNC2(shape_set_data, RID, const Variant &);
  72. FUNC2(shape_set_custom_solver_bias, RID, real_t);
  73. FUNC1RC(ShapeType, shape_get_type, RID);
  74. FUNC1RC(Variant, shape_get_data, RID);
  75. FUNC1RC(real_t, shape_get_custom_solver_bias, RID);
  76. //these work well, but should be used from the main thread only
  77. bool 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 {
  78. ERR_FAIL_COND_V(!Thread::is_main_thread(), false);
  79. return 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);
  80. }
  81. /* SPACE API */
  82. FUNCRID(space);
  83. FUNC2(space_set_active, RID, bool);
  84. FUNC1RC(bool, space_is_active, RID);
  85. FUNC3(space_set_param, RID, SpaceParameter, real_t);
  86. FUNC2RC(real_t, space_get_param, RID, SpaceParameter);
  87. // this function only works on physics process, errors and returns null otherwise
  88. PhysicsDirectSpaceState2D *space_get_direct_state(RID p_space) override {
  89. ERR_FAIL_COND_V(!Thread::is_main_thread(), nullptr);
  90. return physics_server_2d->space_get_direct_state(p_space);
  91. }
  92. FUNC2(space_set_debug_contacts, RID, int);
  93. virtual Vector<Vector2> space_get_contacts(RID p_space) const override {
  94. ERR_FAIL_COND_V(!Thread::is_main_thread(), Vector<Vector2>());
  95. return physics_server_2d->space_get_contacts(p_space);
  96. }
  97. virtual int space_get_contact_count(RID p_space) const override {
  98. ERR_FAIL_COND_V(!Thread::is_main_thread(), 0);
  99. return physics_server_2d->space_get_contact_count(p_space);
  100. }
  101. /* AREA API */
  102. //FUNC0RID(area);
  103. FUNCRID(area);
  104. FUNC2(area_set_space, RID, RID);
  105. FUNC1RC(RID, area_get_space, RID);
  106. FUNC4(area_add_shape, RID, RID, const Transform2D &, bool);
  107. FUNC3(area_set_shape, RID, int, RID);
  108. FUNC3(area_set_shape_transform, RID, int, const Transform2D &);
  109. FUNC3(area_set_shape_disabled, RID, int, bool);
  110. FUNC1RC(int, area_get_shape_count, RID);
  111. FUNC2RC(RID, area_get_shape, RID, int);
  112. FUNC2RC(Transform2D, area_get_shape_transform, RID, int);
  113. FUNC2(area_remove_shape, RID, int);
  114. FUNC1(area_clear_shapes, RID);
  115. FUNC2(area_attach_object_instance_id, RID, ObjectID);
  116. FUNC1RC(ObjectID, area_get_object_instance_id, RID);
  117. FUNC2(area_attach_canvas_instance_id, RID, ObjectID);
  118. FUNC1RC(ObjectID, area_get_canvas_instance_id, RID);
  119. FUNC3(area_set_param, RID, AreaParameter, const Variant &);
  120. FUNC2(area_set_transform, RID, const Transform2D &);
  121. FUNC2RC(Variant, area_get_param, RID, AreaParameter);
  122. FUNC1RC(Transform2D, area_get_transform, RID);
  123. FUNC2(area_set_collision_layer, RID, uint32_t);
  124. FUNC1RC(uint32_t, area_get_collision_layer, RID);
  125. FUNC2(area_set_collision_mask, RID, uint32_t);
  126. FUNC1RC(uint32_t, area_get_collision_mask, RID);
  127. FUNC2(area_set_monitorable, RID, bool);
  128. FUNC2(area_set_pickable, RID, bool);
  129. FUNC2(area_set_monitor_callback, RID, const Callable &);
  130. FUNC2(area_set_area_monitor_callback, RID, const Callable &);
  131. /* BODY API */
  132. //FUNC2RID(body,BodyMode,bool);
  133. FUNCRID(body)
  134. FUNC2(body_set_space, RID, RID);
  135. FUNC1RC(RID, body_get_space, RID);
  136. FUNC2(body_set_mode, RID, BodyMode);
  137. FUNC1RC(BodyMode, body_get_mode, RID);
  138. FUNC4(body_add_shape, RID, RID, const Transform2D &, bool);
  139. FUNC3(body_set_shape, RID, int, RID);
  140. FUNC3(body_set_shape_transform, RID, int, const Transform2D &);
  141. FUNC1RC(int, body_get_shape_count, RID);
  142. FUNC2RC(Transform2D, body_get_shape_transform, RID, int);
  143. FUNC2RC(RID, body_get_shape, RID, int);
  144. FUNC3(body_set_shape_disabled, RID, int, bool);
  145. FUNC4(body_set_shape_as_one_way_collision, RID, int, bool, real_t);
  146. FUNC2(body_remove_shape, RID, int);
  147. FUNC1(body_clear_shapes, RID);
  148. FUNC2(body_attach_object_instance_id, RID, ObjectID);
  149. FUNC1RC(ObjectID, body_get_object_instance_id, RID);
  150. FUNC2(body_attach_canvas_instance_id, RID, ObjectID);
  151. FUNC1RC(ObjectID, body_get_canvas_instance_id, RID);
  152. FUNC2(body_set_continuous_collision_detection_mode, RID, CCDMode);
  153. FUNC1RC(CCDMode, body_get_continuous_collision_detection_mode, RID);
  154. FUNC2(body_set_collision_layer, RID, uint32_t);
  155. FUNC1RC(uint32_t, body_get_collision_layer, RID);
  156. FUNC2(body_set_collision_mask, RID, uint32_t);
  157. FUNC1RC(uint32_t, body_get_collision_mask, RID);
  158. FUNC2(body_set_collision_priority, RID, real_t);
  159. FUNC1RC(real_t, body_get_collision_priority, RID);
  160. FUNC3(body_set_param, RID, BodyParameter, const Variant &);
  161. FUNC2RC(Variant, body_get_param, RID, BodyParameter);
  162. FUNC1(body_reset_mass_properties, RID);
  163. FUNC3(body_set_state, RID, BodyState, const Variant &);
  164. FUNC2RC(Variant, body_get_state, RID, BodyState);
  165. FUNC2(body_apply_central_impulse, RID, const Vector2 &);
  166. FUNC2(body_apply_torque_impulse, RID, real_t);
  167. FUNC3(body_apply_impulse, RID, const Vector2 &, const Vector2 &);
  168. FUNC2(body_apply_central_force, RID, const Vector2 &);
  169. FUNC3(body_apply_force, RID, const Vector2 &, const Vector2 &);
  170. FUNC2(body_apply_torque, RID, real_t);
  171. FUNC2(body_add_constant_central_force, RID, const Vector2 &);
  172. FUNC3(body_add_constant_force, RID, const Vector2 &, const Vector2 &);
  173. FUNC2(body_add_constant_torque, RID, real_t);
  174. FUNC2(body_set_constant_force, RID, const Vector2 &);
  175. FUNC1RC(Vector2, body_get_constant_force, RID);
  176. FUNC2(body_set_constant_torque, RID, real_t);
  177. FUNC1RC(real_t, body_get_constant_torque, RID);
  178. FUNC2(body_set_axis_velocity, RID, const Vector2 &);
  179. FUNC2(body_add_collision_exception, RID, RID);
  180. FUNC2(body_remove_collision_exception, RID, RID);
  181. FUNC2S(body_get_collision_exceptions, RID, List<RID> *);
  182. FUNC2(body_set_max_contacts_reported, RID, int);
  183. FUNC1RC(int, body_get_max_contacts_reported, RID);
  184. FUNC2(body_set_contacts_reported_depth_threshold, RID, real_t);
  185. FUNC1RC(real_t, body_get_contacts_reported_depth_threshold, RID);
  186. FUNC2(body_set_omit_force_integration, RID, bool);
  187. FUNC1RC(bool, body_is_omitting_force_integration, RID);
  188. FUNC2(body_set_state_sync_callback, RID, const Callable &);
  189. FUNC3(body_set_force_integration_callback, RID, const Callable &, const Variant &);
  190. bool 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 {
  191. return 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);
  192. }
  193. FUNC2(body_set_pickable, RID, bool);
  194. bool body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result = nullptr) override {
  195. ERR_FAIL_COND_V(!Thread::is_main_thread(), false);
  196. return physics_server_2d->body_test_motion(p_body, p_parameters, r_result);
  197. }
  198. // this function only works on physics process, errors and returns null otherwise
  199. PhysicsDirectBodyState2D *body_get_direct_state(RID p_body) override {
  200. ERR_FAIL_COND_V(!Thread::is_main_thread(), nullptr);
  201. return physics_server_2d->body_get_direct_state(p_body);
  202. }
  203. /* JOINT API */
  204. FUNCRID(joint)
  205. FUNC1(joint_clear, RID)
  206. FUNC3(joint_set_param, RID, JointParam, real_t);
  207. FUNC2RC(real_t, joint_get_param, RID, JointParam);
  208. FUNC2(joint_disable_collisions_between_bodies, RID, const bool);
  209. FUNC1RC(bool, joint_is_disabled_collisions_between_bodies, RID);
  210. ///FUNC3RID(pin_joint,const Vector2&,RID,RID);
  211. ///FUNC5RID(groove_joint,const Vector2&,const Vector2&,const Vector2&,RID,RID);
  212. ///FUNC4RID(damped_spring_joint,const Vector2&,const Vector2&,RID,RID);
  213. //TODO need to convert this to FUNCRID, but it's a hassle..
  214. FUNC4(joint_make_pin, RID, const Vector2 &, RID, RID);
  215. FUNC6(joint_make_groove, RID, const Vector2 &, const Vector2 &, const Vector2 &, RID, RID);
  216. FUNC5(joint_make_damped_spring, RID, const Vector2 &, const Vector2 &, RID, RID);
  217. FUNC3(pin_joint_set_param, RID, PinJointParam, real_t);
  218. FUNC2RC(real_t, pin_joint_get_param, RID, PinJointParam);
  219. FUNC3(pin_joint_set_flag, RID, PinJointFlag, bool);
  220. FUNC2RC(bool, pin_joint_get_flag, RID, PinJointFlag);
  221. FUNC3(damped_spring_joint_set_param, RID, DampedSpringParam, real_t);
  222. FUNC2RC(real_t, damped_spring_joint_get_param, RID, DampedSpringParam);
  223. FUNC1RC(JointType, joint_get_type, RID);
  224. /* MISC */
  225. FUNC1(free, RID);
  226. FUNC1(set_active, bool);
  227. virtual void init() override;
  228. virtual void step(real_t p_step) override;
  229. virtual void sync() override;
  230. virtual void end_sync() override;
  231. virtual void flush_queries() override;
  232. virtual void finish() override;
  233. virtual bool is_flushing_queries() const override {
  234. return physics_server_2d->is_flushing_queries();
  235. }
  236. int get_process_info(ProcessInfo p_info) override {
  237. return physics_server_2d->get_process_info(p_info);
  238. }
  239. PhysicsServer2DWrapMT(PhysicsServer2D *p_contained, bool p_create_thread);
  240. ~PhysicsServer2DWrapMT();
  241. #undef ServerNameWrapMT
  242. #undef ServerName
  243. #undef server_name
  244. #undef WRITE_ACTION
  245. };
  246. #ifdef DEBUG_SYNC
  247. #undef DEBUG_SYNC
  248. #endif
  249. #undef SYNC_DEBUG
  250. #ifdef DEBUG_ENABLED
  251. #undef MAIN_THREAD_SYNC_WARN
  252. #endif
  253. #endif // PHYSICS_SERVER_2D_WRAP_MT_H