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 | |
31 | #ifndef PHYSICS_SERVER_2D_WRAP_MT_H |
32 | #define PHYSICS_SERVER_2D_WRAP_MT_H |
33 | |
34 | #include "core/config/project_settings.h" |
35 | #include "core/os/thread.h" |
36 | #include "core/templates/command_queue_mt.h" |
37 | #include "core/templates/safe_refcount.h" |
38 | #include "servers/physics_server_2d.h" |
39 | |
40 | #ifdef DEBUG_SYNC |
41 | #define SYNC_DEBUG print_line("sync on: " + String(__FUNCTION__)); |
42 | #else |
43 | #define SYNC_DEBUG |
44 | #endif |
45 | |
46 | class PhysicsServer2DWrapMT : public PhysicsServer2D { |
47 | mutable PhysicsServer2D *physics_server_2d; |
48 | |
49 | mutable CommandQueueMT command_queue; |
50 | |
51 | static void _thread_callback(void *_instance); |
52 | void thread_loop(); |
53 | |
54 | Thread::ID server_thread; |
55 | Thread::ID main_thread; |
56 | SafeFlag exit; |
57 | Thread thread; |
58 | SafeFlag step_thread_up; |
59 | bool create_thread = false; |
60 | |
61 | Semaphore step_sem; |
62 | void thread_step(real_t p_delta); |
63 | |
64 | void thread_exit(); |
65 | |
66 | bool first_frame = true; |
67 | |
68 | Mutex alloc_mutex; |
69 | int pool_max_size = 0; |
70 | |
71 | public: |
72 | #define ServerName PhysicsServer2D |
73 | #define ServerNameWrapMT PhysicsServer2DWrapMT |
74 | #define server_name physics_server_2d |
75 | #define WRITE_ACTION |
76 | |
77 | #include "servers/server_wrap_mt_common.h" |
78 | |
79 | //FUNC1RID(shape,ShapeType); todo fix |
80 | FUNCRID(world_boundary_shape) |
81 | FUNCRID(separation_ray_shape) |
82 | FUNCRID(segment_shape) |
83 | FUNCRID(circle_shape) |
84 | FUNCRID(rectangle_shape) |
85 | FUNCRID(capsule_shape) |
86 | FUNCRID(convex_polygon_shape) |
87 | FUNCRID(concave_polygon_shape) |
88 | |
89 | FUNC2(shape_set_data, RID, const Variant &); |
90 | FUNC2(shape_set_custom_solver_bias, RID, real_t); |
91 | |
92 | FUNC1RC(ShapeType, shape_get_type, RID); |
93 | FUNC1RC(Variant, shape_get_data, RID); |
94 | FUNC1RC(real_t, shape_get_custom_solver_bias, RID); |
95 | |
96 | //these work well, but should be used from the main thread only |
97 | 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 { |
98 | ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), false); |
99 | 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); |
100 | } |
101 | |
102 | /* SPACE API */ |
103 | |
104 | FUNCRID(space); |
105 | FUNC2(space_set_active, RID, bool); |
106 | FUNC1RC(bool, space_is_active, RID); |
107 | |
108 | FUNC3(space_set_param, RID, SpaceParameter, real_t); |
109 | FUNC2RC(real_t, space_get_param, RID, SpaceParameter); |
110 | |
111 | // this function only works on physics process, errors and returns null otherwise |
112 | PhysicsDirectSpaceState2D *space_get_direct_state(RID p_space) override { |
113 | ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), nullptr); |
114 | return physics_server_2d->space_get_direct_state(p_space); |
115 | } |
116 | |
117 | FUNC2(space_set_debug_contacts, RID, int); |
118 | virtual Vector<Vector2> space_get_contacts(RID p_space) const override { |
119 | ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), Vector<Vector2>()); |
120 | return physics_server_2d->space_get_contacts(p_space); |
121 | } |
122 | |
123 | virtual int space_get_contact_count(RID p_space) const override { |
124 | ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), 0); |
125 | return physics_server_2d->space_get_contact_count(p_space); |
126 | } |
127 | |
128 | /* AREA API */ |
129 | |
130 | //FUNC0RID(area); |
131 | FUNCRID(area); |
132 | |
133 | FUNC2(area_set_space, RID, RID); |
134 | FUNC1RC(RID, area_get_space, RID); |
135 | |
136 | FUNC4(area_add_shape, RID, RID, const Transform2D &, bool); |
137 | FUNC3(area_set_shape, RID, int, RID); |
138 | FUNC3(area_set_shape_transform, RID, int, const Transform2D &); |
139 | FUNC3(area_set_shape_disabled, RID, int, bool); |
140 | |
141 | FUNC1RC(int, area_get_shape_count, RID); |
142 | FUNC2RC(RID, area_get_shape, RID, int); |
143 | FUNC2RC(Transform2D, area_get_shape_transform, RID, int); |
144 | FUNC2(area_remove_shape, RID, int); |
145 | FUNC1(area_clear_shapes, RID); |
146 | |
147 | FUNC2(area_attach_object_instance_id, RID, ObjectID); |
148 | FUNC1RC(ObjectID, area_get_object_instance_id, RID); |
149 | |
150 | FUNC2(area_attach_canvas_instance_id, RID, ObjectID); |
151 | FUNC1RC(ObjectID, area_get_canvas_instance_id, RID); |
152 | |
153 | FUNC3(area_set_param, RID, AreaParameter, const Variant &); |
154 | FUNC2(area_set_transform, RID, const Transform2D &); |
155 | |
156 | FUNC2RC(Variant, area_get_param, RID, AreaParameter); |
157 | FUNC1RC(Transform2D, area_get_transform, RID); |
158 | |
159 | FUNC2(area_set_collision_layer, RID, uint32_t); |
160 | FUNC1RC(uint32_t, area_get_collision_layer, RID); |
161 | |
162 | FUNC2(area_set_collision_mask, RID, uint32_t); |
163 | FUNC1RC(uint32_t, area_get_collision_mask, RID); |
164 | |
165 | FUNC2(area_set_monitorable, RID, bool); |
166 | FUNC2(area_set_pickable, RID, bool); |
167 | |
168 | FUNC2(area_set_monitor_callback, RID, const Callable &); |
169 | FUNC2(area_set_area_monitor_callback, RID, const Callable &); |
170 | |
171 | /* BODY API */ |
172 | |
173 | //FUNC2RID(body,BodyMode,bool); |
174 | FUNCRID(body) |
175 | |
176 | FUNC2(body_set_space, RID, RID); |
177 | FUNC1RC(RID, body_get_space, RID); |
178 | |
179 | FUNC2(body_set_mode, RID, BodyMode); |
180 | FUNC1RC(BodyMode, body_get_mode, RID); |
181 | |
182 | FUNC4(body_add_shape, RID, RID, const Transform2D &, bool); |
183 | FUNC3(body_set_shape, RID, int, RID); |
184 | FUNC3(body_set_shape_transform, RID, int, const Transform2D &); |
185 | |
186 | FUNC1RC(int, body_get_shape_count, RID); |
187 | FUNC2RC(Transform2D, body_get_shape_transform, RID, int); |
188 | FUNC2RC(RID, body_get_shape, RID, int); |
189 | |
190 | FUNC3(body_set_shape_disabled, RID, int, bool); |
191 | FUNC4(body_set_shape_as_one_way_collision, RID, int, bool, real_t); |
192 | |
193 | FUNC2(body_remove_shape, RID, int); |
194 | FUNC1(body_clear_shapes, RID); |
195 | |
196 | FUNC2(body_attach_object_instance_id, RID, ObjectID); |
197 | FUNC1RC(ObjectID, body_get_object_instance_id, RID); |
198 | |
199 | FUNC2(body_attach_canvas_instance_id, RID, ObjectID); |
200 | FUNC1RC(ObjectID, body_get_canvas_instance_id, RID); |
201 | |
202 | FUNC2(body_set_continuous_collision_detection_mode, RID, CCDMode); |
203 | FUNC1RC(CCDMode, body_get_continuous_collision_detection_mode, RID); |
204 | |
205 | FUNC2(body_set_collision_layer, RID, uint32_t); |
206 | FUNC1RC(uint32_t, body_get_collision_layer, RID); |
207 | |
208 | FUNC2(body_set_collision_mask, RID, uint32_t); |
209 | FUNC1RC(uint32_t, body_get_collision_mask, RID); |
210 | |
211 | FUNC2(body_set_collision_priority, RID, real_t); |
212 | FUNC1RC(real_t, body_get_collision_priority, RID); |
213 | |
214 | FUNC3(body_set_param, RID, BodyParameter, const Variant &); |
215 | FUNC2RC(Variant, body_get_param, RID, BodyParameter); |
216 | |
217 | FUNC1(body_reset_mass_properties, RID); |
218 | |
219 | FUNC3(body_set_state, RID, BodyState, const Variant &); |
220 | FUNC2RC(Variant, body_get_state, RID, BodyState); |
221 | |
222 | FUNC2(body_apply_central_impulse, RID, const Vector2 &); |
223 | FUNC2(body_apply_torque_impulse, RID, real_t); |
224 | FUNC3(body_apply_impulse, RID, const Vector2 &, const Vector2 &); |
225 | |
226 | FUNC2(body_apply_central_force, RID, const Vector2 &); |
227 | FUNC3(body_apply_force, RID, const Vector2 &, const Vector2 &); |
228 | FUNC2(body_apply_torque, RID, real_t); |
229 | |
230 | FUNC2(body_add_constant_central_force, RID, const Vector2 &); |
231 | FUNC3(body_add_constant_force, RID, const Vector2 &, const Vector2 &); |
232 | FUNC2(body_add_constant_torque, RID, real_t); |
233 | |
234 | FUNC2(body_set_constant_force, RID, const Vector2 &); |
235 | FUNC1RC(Vector2, body_get_constant_force, RID); |
236 | |
237 | FUNC2(body_set_constant_torque, RID, real_t); |
238 | FUNC1RC(real_t, body_get_constant_torque, RID); |
239 | |
240 | FUNC2(body_set_axis_velocity, RID, const Vector2 &); |
241 | |
242 | FUNC2(body_add_collision_exception, RID, RID); |
243 | FUNC2(body_remove_collision_exception, RID, RID); |
244 | FUNC2S(body_get_collision_exceptions, RID, List<RID> *); |
245 | |
246 | FUNC2(body_set_max_contacts_reported, RID, int); |
247 | FUNC1RC(int, body_get_max_contacts_reported, RID); |
248 | |
249 | FUNC2(body_set_contacts_reported_depth_threshold, RID, real_t); |
250 | FUNC1RC(real_t, body_get_contacts_reported_depth_threshold, RID); |
251 | |
252 | FUNC2(body_set_omit_force_integration, RID, bool); |
253 | FUNC1RC(bool, body_is_omitting_force_integration, RID); |
254 | |
255 | FUNC2(body_set_state_sync_callback, RID, const Callable &); |
256 | FUNC3(body_set_force_integration_callback, RID, const Callable &, const Variant &); |
257 | |
258 | 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 { |
259 | 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); |
260 | } |
261 | |
262 | FUNC2(body_set_pickable, RID, bool); |
263 | |
264 | bool body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result = nullptr) override { |
265 | ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), false); |
266 | return physics_server_2d->body_test_motion(p_body, p_parameters, r_result); |
267 | } |
268 | |
269 | // this function only works on physics process, errors and returns null otherwise |
270 | PhysicsDirectBodyState2D *body_get_direct_state(RID p_body) override { |
271 | ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), nullptr); |
272 | return physics_server_2d->body_get_direct_state(p_body); |
273 | } |
274 | |
275 | /* JOINT API */ |
276 | |
277 | FUNCRID(joint) |
278 | |
279 | FUNC1(joint_clear, RID) |
280 | |
281 | FUNC3(joint_set_param, RID, JointParam, real_t); |
282 | FUNC2RC(real_t, joint_get_param, RID, JointParam); |
283 | |
284 | FUNC2(joint_disable_collisions_between_bodies, RID, const bool); |
285 | FUNC1RC(bool, joint_is_disabled_collisions_between_bodies, RID); |
286 | |
287 | ///FUNC3RID(pin_joint,const Vector2&,RID,RID); |
288 | ///FUNC5RID(groove_joint,const Vector2&,const Vector2&,const Vector2&,RID,RID); |
289 | ///FUNC4RID(damped_spring_joint,const Vector2&,const Vector2&,RID,RID); |
290 | |
291 | //TODO need to convert this to FUNCRID, but it's a hassle.. |
292 | |
293 | FUNC4(joint_make_pin, RID, const Vector2 &, RID, RID); |
294 | FUNC6(joint_make_groove, RID, const Vector2 &, const Vector2 &, const Vector2 &, RID, RID); |
295 | FUNC5(joint_make_damped_spring, RID, const Vector2 &, const Vector2 &, RID, RID); |
296 | |
297 | FUNC3(pin_joint_set_param, RID, PinJointParam, real_t); |
298 | FUNC2RC(real_t, pin_joint_get_param, RID, PinJointParam); |
299 | |
300 | FUNC3(damped_spring_joint_set_param, RID, DampedSpringParam, real_t); |
301 | FUNC2RC(real_t, damped_spring_joint_get_param, RID, DampedSpringParam); |
302 | |
303 | FUNC1RC(JointType, joint_get_type, RID); |
304 | |
305 | /* MISC */ |
306 | |
307 | FUNC1(free, RID); |
308 | FUNC1(set_active, bool); |
309 | |
310 | virtual void init() override; |
311 | virtual void step(real_t p_step) override; |
312 | virtual void sync() override; |
313 | virtual void end_sync() override; |
314 | virtual void flush_queries() override; |
315 | virtual void finish() override; |
316 | |
317 | virtual bool is_flushing_queries() const override { |
318 | return physics_server_2d->is_flushing_queries(); |
319 | } |
320 | |
321 | int get_process_info(ProcessInfo p_info) override { |
322 | return physics_server_2d->get_process_info(p_info); |
323 | } |
324 | |
325 | PhysicsServer2DWrapMT(PhysicsServer2D *p_contained, bool p_create_thread); |
326 | ~PhysicsServer2DWrapMT(); |
327 | |
328 | #undef ServerNameWrapMT |
329 | #undef ServerName |
330 | #undef server_name |
331 | #undef WRITE_ACTION |
332 | }; |
333 | |
334 | #ifdef DEBUG_SYNC |
335 | #undef DEBUG_SYNC |
336 | #endif |
337 | #undef SYNC_DEBUG |
338 | |
339 | #endif // PHYSICS_SERVER_2D_WRAP_MT_H |
340 | |