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