physics_server_3d_wrap_mt.h 15 KB

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