physics_2d_server_wrap_mt.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*************************************************************************/
  2. /* physics_2d_server_wrap_mt.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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 PHYSICS2DSERVERWRAPMT_H
  31. #define PHYSICS2DSERVERWRAPMT_H
  32. #include "core/command_queue_mt.h"
  33. #include "core/os/thread.h"
  34. #include "core/project_settings.h"
  35. #include "servers/physics_2d_server.h"
  36. #ifdef DEBUG_SYNC
  37. #define SYNC_DEBUG print_line("sync on: " + String(__FUNCTION__));
  38. #else
  39. #define SYNC_DEBUG
  40. #endif
  41. class Physics2DServerWrapMT : public Physics2DServer {
  42. mutable Physics2DServer *physics_2d_server;
  43. mutable CommandQueueMT command_queue;
  44. static void _thread_callback(void *_instance);
  45. void thread_loop();
  46. Thread::ID server_thread;
  47. Thread::ID main_thread;
  48. volatile bool exit;
  49. Thread *thread;
  50. volatile bool step_thread_up;
  51. bool create_thread;
  52. Semaphore *step_sem;
  53. int step_pending;
  54. void thread_step(real_t p_delta);
  55. void thread_flush();
  56. void thread_exit();
  57. bool first_frame;
  58. Mutex *alloc_mutex;
  59. int pool_max_size;
  60. public:
  61. #define ServerName Physics2DServer
  62. #define ServerNameWrapMT Physics2DServerWrapMT
  63. #define server_name physics_2d_server
  64. #include "servers/server_wrap_mt_common.h"
  65. //FUNC1RID(shape,ShapeType); todo fix
  66. FUNCRID(line_shape)
  67. FUNCRID(ray_shape)
  68. FUNCRID(segment_shape)
  69. FUNCRID(circle_shape)
  70. FUNCRID(rectangle_shape)
  71. FUNCRID(capsule_shape)
  72. FUNCRID(convex_polygon_shape)
  73. FUNCRID(concave_polygon_shape)
  74. FUNC2(shape_set_data, RID, const Variant &);
  75. FUNC2(shape_set_custom_solver_bias, RID, real_t);
  76. FUNC1RC(ShapeType, shape_get_type, RID);
  77. FUNC1RC(Variant, shape_get_data, RID);
  78. FUNC1RC(real_t, shape_get_custom_solver_bias, RID);
  79. //these work well, but should be used from the main thread only
  80. 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) {
  81. ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), false);
  82. return physics_2d_server->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);
  83. }
  84. /* SPACE API */
  85. FUNCRID(space);
  86. FUNC2(space_set_active, RID, bool);
  87. FUNC1RC(bool, space_is_active, RID);
  88. FUNC3(space_set_param, RID, SpaceParameter, real_t);
  89. FUNC2RC(real_t, space_get_param, RID, SpaceParameter);
  90. // this function only works on physics process, errors and returns null otherwise
  91. Physics2DDirectSpaceState *space_get_direct_state(RID p_space) {
  92. ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), NULL);
  93. return physics_2d_server->space_get_direct_state(p_space);
  94. }
  95. FUNC2(space_set_debug_contacts, RID, int);
  96. virtual Vector<Vector2> space_get_contacts(RID p_space) const {
  97. ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), Vector<Vector2>());
  98. return physics_2d_server->space_get_contacts(p_space);
  99. }
  100. virtual int space_get_contact_count(RID p_space) const {
  101. ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), 0);
  102. return physics_2d_server->space_get_contact_count(p_space);
  103. }
  104. /* AREA API */
  105. //FUNC0RID(area);
  106. FUNCRID(area);
  107. FUNC2(area_set_space, RID, RID);
  108. FUNC1RC(RID, area_get_space, RID);
  109. FUNC2(area_set_space_override_mode, RID, AreaSpaceOverrideMode);
  110. FUNC1RC(AreaSpaceOverrideMode, area_get_space_override_mode, RID);
  111. FUNC3(area_add_shape, RID, RID, const Transform2D &);
  112. FUNC3(area_set_shape, RID, int, RID);
  113. FUNC3(area_set_shape_transform, RID, int, const Transform2D &);
  114. FUNC3(area_set_shape_disabled, RID, int, bool);
  115. FUNC1RC(int, area_get_shape_count, RID);
  116. FUNC2RC(RID, area_get_shape, RID, int);
  117. FUNC2RC(Transform2D, area_get_shape_transform, RID, int);
  118. FUNC2(area_remove_shape, RID, int);
  119. FUNC1(area_clear_shapes, RID);
  120. FUNC2(area_attach_object_instance_id, RID, ObjectID);
  121. FUNC1RC(ObjectID, area_get_object_instance_id, RID);
  122. FUNC2(area_attach_canvas_instance_id, RID, ObjectID);
  123. FUNC1RC(ObjectID, area_get_canvas_instance_id, RID);
  124. FUNC3(area_set_param, RID, AreaParameter, const Variant &);
  125. FUNC2(area_set_transform, RID, const Transform2D &);
  126. FUNC2RC(Variant, area_get_param, RID, AreaParameter);
  127. FUNC1RC(Transform2D, area_get_transform, RID);
  128. FUNC2(area_set_collision_mask, RID, uint32_t);
  129. FUNC2(area_set_collision_layer, RID, uint32_t);
  130. FUNC2(area_set_monitorable, RID, bool);
  131. FUNC2(area_set_pickable, RID, bool);
  132. FUNC3(area_set_monitor_callback, RID, Object *, const StringName &);
  133. FUNC3(area_set_area_monitor_callback, RID, Object *, const StringName &);
  134. /* BODY API */
  135. //FUNC2RID(body,BodyMode,bool);
  136. FUNCRID(body)
  137. FUNC2(body_set_space, RID, RID);
  138. FUNC1RC(RID, body_get_space, RID);
  139. FUNC2(body_set_mode, RID, BodyMode);
  140. FUNC1RC(BodyMode, body_get_mode, RID);
  141. FUNC3(body_add_shape, RID, RID, const Transform2D &);
  142. FUNC3(body_set_shape, RID, int, RID);
  143. FUNC3(body_set_shape_transform, RID, int, const Transform2D &);
  144. FUNC3(body_set_shape_metadata, RID, int, const Variant &);
  145. FUNC1RC(int, body_get_shape_count, RID);
  146. FUNC2RC(Transform2D, body_get_shape_transform, RID, int);
  147. FUNC2RC(Variant, body_get_shape_metadata, RID, int);
  148. FUNC2RC(RID, body_get_shape, RID, int);
  149. FUNC3(body_set_shape_disabled, RID, int, bool);
  150. FUNC3(body_set_shape_as_one_way_collision, RID, int, bool);
  151. FUNC2(body_remove_shape, RID, int);
  152. FUNC1(body_clear_shapes, RID);
  153. FUNC2(body_attach_object_instance_id, RID, uint32_t);
  154. FUNC1RC(uint32_t, body_get_object_instance_id, RID);
  155. FUNC2(body_attach_canvas_instance_id, RID, uint32_t);
  156. FUNC1RC(uint32_t, body_get_canvas_instance_id, RID);
  157. FUNC2(body_set_continuous_collision_detection_mode, RID, CCDMode);
  158. FUNC1RC(CCDMode, body_get_continuous_collision_detection_mode, 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. FUNC3(body_set_param, RID, BodyParameter, real_t);
  164. FUNC2RC(real_t, body_get_param, RID, BodyParameter);
  165. FUNC3(body_set_state, RID, BodyState, const Variant &);
  166. FUNC2RC(Variant, body_get_state, RID, BodyState);
  167. FUNC2(body_set_applied_force, RID, const Vector2 &);
  168. FUNC1RC(Vector2, body_get_applied_force, RID);
  169. FUNC2(body_set_applied_torque, RID, real_t);
  170. FUNC1RC(real_t, body_get_applied_torque, RID);
  171. FUNC2(body_add_central_force, RID, const Vector2 &);
  172. FUNC3(body_add_force, RID, const Vector2 &, const Vector2 &);
  173. FUNC2(body_add_torque, RID, real_t);
  174. FUNC2(body_apply_central_impulse, RID, const Vector2 &);
  175. FUNC2(body_apply_torque_impulse, RID, real_t);
  176. FUNC3(body_apply_impulse, RID, const Vector2 &, const Vector2 &);
  177. FUNC2(body_set_axis_velocity, RID, const Vector2 &);
  178. FUNC2(body_add_collision_exception, RID, RID);
  179. FUNC2(body_remove_collision_exception, RID, RID);
  180. FUNC2S(body_get_collision_exceptions, RID, List<RID> *);
  181. FUNC2(body_set_max_contacts_reported, RID, int);
  182. FUNC1RC(int, body_get_max_contacts_reported, RID);
  183. FUNC2(body_set_contacts_reported_depth_threshold, RID, real_t);
  184. FUNC1RC(real_t, body_get_contacts_reported_depth_threshold, RID);
  185. FUNC2(body_set_omit_force_integration, RID, bool);
  186. FUNC1RC(bool, body_is_omitting_force_integration, RID);
  187. FUNC4(body_set_force_integration_callback, RID, Object *, const StringName &, const Variant &);
  188. 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) {
  189. return physics_2d_server->body_collide_shape(p_body, p_body_shape, p_shape, p_shape_xform, p_motion, r_results, p_result_max, r_result_count);
  190. }
  191. FUNC2(body_set_pickable, RID, bool);
  192. bool body_test_motion(RID p_body, const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia, real_t p_margin = 0.001, MotionResult *r_result = NULL, bool p_exclude_raycast_shapes = true) {
  193. ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), false);
  194. return physics_2d_server->body_test_motion(p_body, p_from, p_motion, p_infinite_inertia, p_margin, r_result, p_exclude_raycast_shapes);
  195. }
  196. int body_test_ray_separation(RID p_body, const Transform2D &p_transform, bool p_infinite_inertia, Vector2 &r_recover_motion, SeparationResult *r_results, int p_result_max, float p_margin = 0.001) {
  197. ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), false);
  198. return physics_2d_server->body_test_ray_separation(p_body, p_transform, p_infinite_inertia, r_recover_motion, r_results, p_result_max, p_margin);
  199. }
  200. // this function only works on physics process, errors and returns null otherwise
  201. Physics2DDirectBodyState *body_get_direct_state(RID p_body) {
  202. ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), NULL);
  203. return physics_2d_server->body_get_direct_state(p_body);
  204. }
  205. /* JOINT API */
  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. FUNC3R(RID, pin_joint_create, const Vector2 &, RID, RID);
  215. FUNC5R(RID, groove_joint_create, const Vector2 &, const Vector2 &, const Vector2 &, RID, RID);
  216. FUNC4R(RID, damped_spring_joint_create, 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(damped_string_joint_set_param, RID, DampedStringParam, real_t);
  220. FUNC2RC(real_t, damped_string_joint_get_param, RID, DampedStringParam);
  221. FUNC1RC(JointType, joint_get_type, RID);
  222. /* MISC */
  223. FUNC1(free, RID);
  224. FUNC1(set_active, bool);
  225. virtual void init();
  226. virtual void step(real_t p_step);
  227. virtual void sync();
  228. virtual void end_sync();
  229. virtual void flush_queries();
  230. virtual void finish();
  231. virtual bool is_flushing_queries() const {
  232. return physics_2d_server->is_flushing_queries();
  233. }
  234. int get_process_info(ProcessInfo p_info) {
  235. return physics_2d_server->get_process_info(p_info);
  236. }
  237. Physics2DServerWrapMT(Physics2DServer *p_contained, bool p_create_thread);
  238. ~Physics2DServerWrapMT();
  239. template <class T>
  240. static Physics2DServer *init_server() {
  241. int tm = GLOBAL_DEF("physics/2d/thread_model", 1);
  242. if (tm == 0) //single unsafe
  243. return memnew(T);
  244. else if (tm == 1) //single saef
  245. return memnew(Physics2DServerWrapMT(memnew(T), false));
  246. else //single unsafe
  247. return memnew(Physics2DServerWrapMT(memnew(T), true));
  248. }
  249. #undef ServerNameWrapMT
  250. #undef ServerName
  251. #undef server_name
  252. };
  253. #ifdef DEBUG_SYNC
  254. #undef DEBUG_SYNC
  255. #endif
  256. #undef SYNC_DEBUG
  257. #endif // PHYSICS2DSERVERWRAPMT_H