Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/physics_server_3d_wrap_mt.h
9887 views
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
31
#pragma once
32
33
#include "core/config/project_settings.h"
34
#include "core/object/worker_thread_pool.h"
35
#include "core/os/thread.h"
36
#include "core/templates/command_queue_mt.h"
37
#include "servers/physics_server_3d.h"
38
39
#define ASYNC_COND_PUSH (Thread::get_caller_id() != server_thread)
40
#define ASYNC_COND_PUSH_AND_RET (Thread::get_caller_id() != server_thread && !(doing_sync.is_set() && Thread::is_main_thread()))
41
#define ASYNC_COND_PUSH_AND_SYNC (Thread::get_caller_id() != server_thread && !(doing_sync.is_set() && Thread::is_main_thread()))
42
43
#ifdef DEBUG_SYNC
44
#define SYNC_DEBUG print_line("sync on: " + String(__FUNCTION__));
45
#else
46
#define SYNC_DEBUG
47
#endif
48
49
#ifdef DEBUG_ENABLED
50
#ifdef DEV_ENABLED
51
#define MAIN_THREAD_SYNC_WARN WARN_PRINT("Call to " + String(__FUNCTION__) + " causing PhysicsServer3D synchronizations on every frame. This significantly affects performance.");
52
#else
53
#define MAIN_THREAD_SYNC_WARN
54
#endif
55
#endif
56
57
class PhysicsServer3DWrapMT : public PhysicsServer3D {
58
mutable PhysicsServer3D *physics_server_3d = nullptr;
59
60
mutable CommandQueueMT command_queue;
61
62
Thread::ID server_thread = Thread::UNASSIGNED_ID;
63
WorkerThreadPool::TaskID server_task_id = WorkerThreadPool::INVALID_TASK_ID;
64
bool exit = false;
65
bool create_thread = false;
66
SafeFlag doing_sync;
67
68
void _assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id);
69
void _thread_exit();
70
void _thread_step(real_t p_delta);
71
void _thread_loop();
72
void _thread_sync();
73
74
public:
75
#define ServerName PhysicsServer3D
76
#define ServerNameWrapMT PhysicsServer3DWrapMT
77
#define server_name physics_server_3d
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(sphere_shape)
86
FUNCRID(box_shape)
87
FUNCRID(capsule_shape)
88
FUNCRID(cylinder_shape)
89
FUNCRID(convex_polygon_shape)
90
FUNCRID(concave_polygon_shape)
91
FUNCRID(heightmap_shape)
92
FUNCRID(custom_shape)
93
94
FUNC2(shape_set_data, RID, const Variant &);
95
FUNC2(shape_set_custom_solver_bias, RID, real_t);
96
97
FUNC2(shape_set_margin, RID, real_t)
98
FUNC1RC(real_t, shape_get_margin, RID)
99
100
FUNC1RC(ShapeType, shape_get_type, RID);
101
FUNC1RC(Variant, shape_get_data, RID);
102
FUNC1RC(real_t, shape_get_custom_solver_bias, RID);
103
#if 0
104
//these work well, but should be used from the main thread only
105
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) {
106
ERR_FAIL_COND_V(!Thread::is_main_thread(), false);
107
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);
108
}
109
#endif
110
/* SPACE API */
111
112
FUNCRID(space);
113
FUNC2(space_set_active, RID, bool);
114
FUNC1RC(bool, space_is_active, RID);
115
116
FUNC3(space_set_param, RID, SpaceParameter, real_t);
117
FUNC2RC(real_t, space_get_param, RID, SpaceParameter);
118
119
// this function only works on physics process, errors and returns null otherwise
120
PhysicsDirectSpaceState3D *space_get_direct_state(RID p_space) override {
121
ERR_FAIL_COND_V(!Thread::is_main_thread(), nullptr);
122
return physics_server_3d->space_get_direct_state(p_space);
123
}
124
125
FUNC2(space_set_debug_contacts, RID, int);
126
virtual Vector<Vector3> space_get_contacts(RID p_space) const override {
127
ERR_FAIL_COND_V(!Thread::is_main_thread(), Vector<Vector3>());
128
return physics_server_3d->space_get_contacts(p_space);
129
}
130
131
virtual int space_get_contact_count(RID p_space) const override {
132
ERR_FAIL_COND_V(!Thread::is_main_thread(), 0);
133
return physics_server_3d->space_get_contact_count(p_space);
134
}
135
136
/* AREA API */
137
138
//FUNC0RID(area);
139
FUNCRID(area);
140
141
FUNC2(area_set_space, RID, RID);
142
FUNC1RC(RID, area_get_space, RID);
143
144
FUNC4(area_add_shape, RID, RID, const Transform3D &, bool);
145
FUNC3(area_set_shape, RID, int, RID);
146
FUNC3(area_set_shape_transform, RID, int, const Transform3D &);
147
FUNC3(area_set_shape_disabled, RID, int, bool);
148
149
FUNC1RC(int, area_get_shape_count, RID);
150
FUNC2RC(RID, area_get_shape, RID, int);
151
FUNC2RC(Transform3D, area_get_shape_transform, RID, int);
152
FUNC2(area_remove_shape, RID, int);
153
FUNC1(area_clear_shapes, RID);
154
155
FUNC2(area_attach_object_instance_id, RID, ObjectID);
156
FUNC1RC(ObjectID, area_get_object_instance_id, RID);
157
158
FUNC3(area_set_param, RID, AreaParameter, const Variant &);
159
FUNC2(area_set_transform, RID, const Transform3D &);
160
161
FUNC2RC(Variant, area_get_param, RID, AreaParameter);
162
FUNC1RC(Transform3D, area_get_transform, RID);
163
164
FUNC2(area_set_collision_layer, RID, uint32_t);
165
FUNC1RC(uint32_t, area_get_collision_layer, RID);
166
167
FUNC2(area_set_collision_mask, RID, uint32_t);
168
FUNC1RC(uint32_t, area_get_collision_mask, RID);
169
170
FUNC2(area_set_monitorable, RID, bool);
171
FUNC2(area_set_ray_pickable, RID, bool);
172
173
FUNC2(area_set_monitor_callback, RID, const Callable &);
174
FUNC2(area_set_area_monitor_callback, RID, const Callable &);
175
176
/* BODY API */
177
178
//FUNC2RID(body,BodyMode,bool);
179
FUNCRID(body)
180
181
FUNC2(body_set_space, RID, RID);
182
FUNC1RC(RID, body_get_space, RID);
183
184
FUNC2(body_set_mode, RID, BodyMode);
185
FUNC1RC(BodyMode, body_get_mode, RID);
186
187
FUNC4(body_add_shape, RID, RID, const Transform3D &, bool);
188
FUNC3(body_set_shape, RID, int, RID);
189
FUNC3(body_set_shape_transform, RID, int, const Transform3D &);
190
191
FUNC1RC(int, body_get_shape_count, RID);
192
FUNC2RC(Transform3D, body_get_shape_transform, RID, int);
193
FUNC2RC(RID, body_get_shape, RID, int);
194
195
FUNC3(body_set_shape_disabled, RID, int, bool);
196
197
FUNC2(body_remove_shape, RID, int);
198
FUNC1(body_clear_shapes, RID);
199
200
FUNC2(body_attach_object_instance_id, RID, ObjectID);
201
FUNC1RC(ObjectID, body_get_object_instance_id, RID);
202
203
FUNC2(body_set_enable_continuous_collision_detection, RID, bool);
204
FUNC1RC(bool, body_is_continuous_collision_detection_enabled, RID);
205
206
FUNC2(body_set_collision_layer, RID, uint32_t);
207
FUNC1RC(uint32_t, body_get_collision_layer, RID);
208
209
FUNC2(body_set_collision_mask, RID, uint32_t);
210
FUNC1RC(uint32_t, body_get_collision_mask, RID);
211
212
FUNC2(body_set_collision_priority, RID, real_t);
213
FUNC1RC(real_t, body_get_collision_priority, RID);
214
215
FUNC2(body_set_user_flags, RID, uint32_t);
216
FUNC1RC(uint32_t, body_get_user_flags, RID);
217
218
FUNC3(body_set_param, RID, BodyParameter, const Variant &);
219
FUNC2RC(Variant, body_get_param, RID, BodyParameter);
220
221
FUNC1(body_reset_mass_properties, RID);
222
223
FUNC3(body_set_state, RID, BodyState, const Variant &);
224
FUNC2RC(Variant, body_get_state, RID, BodyState);
225
226
FUNC2(body_apply_torque_impulse, RID, const Vector3 &);
227
FUNC2(body_apply_central_impulse, RID, const Vector3 &);
228
FUNC3(body_apply_impulse, RID, const Vector3 &, const Vector3 &);
229
230
FUNC2(body_apply_central_force, RID, const Vector3 &);
231
FUNC3(body_apply_force, RID, const Vector3 &, const Vector3 &);
232
FUNC2(body_apply_torque, RID, const Vector3 &);
233
234
FUNC3(soft_body_apply_point_impulse, RID, int, const Vector3 &);
235
FUNC3(soft_body_apply_point_force, RID, int, const Vector3 &);
236
FUNC2(soft_body_apply_central_impulse, RID, const Vector3 &);
237
FUNC2(soft_body_apply_central_force, RID, const Vector3 &);
238
239
FUNC2(body_add_constant_central_force, RID, const Vector3 &);
240
FUNC3(body_add_constant_force, RID, const Vector3 &, const Vector3 &);
241
FUNC2(body_add_constant_torque, RID, const Vector3 &);
242
243
FUNC2(body_set_constant_force, RID, const Vector3 &);
244
FUNC1RC(Vector3, body_get_constant_force, RID);
245
246
FUNC2(body_set_constant_torque, RID, const Vector3 &);
247
FUNC1RC(Vector3, body_get_constant_torque, RID);
248
249
FUNC2(body_set_axis_velocity, RID, const Vector3 &);
250
251
FUNC3(body_set_axis_lock, RID, BodyAxis, bool);
252
FUNC2RC(bool, body_is_axis_locked, RID, BodyAxis);
253
254
FUNC2(body_add_collision_exception, RID, RID);
255
FUNC2(body_remove_collision_exception, RID, RID);
256
FUNC2S(body_get_collision_exceptions, RID, List<RID> *);
257
258
FUNC2(body_set_max_contacts_reported, RID, int);
259
FUNC1RC(int, body_get_max_contacts_reported, RID);
260
261
FUNC2(body_set_contacts_reported_depth_threshold, RID, real_t);
262
FUNC1RC(real_t, body_get_contacts_reported_depth_threshold, RID);
263
264
FUNC2(body_set_omit_force_integration, RID, bool);
265
FUNC1RC(bool, body_is_omitting_force_integration, RID);
266
267
FUNC2(body_set_state_sync_callback, RID, const Callable &);
268
FUNC3(body_set_force_integration_callback, RID, const Callable &, const Variant &);
269
270
FUNC2(body_set_ray_pickable, RID, bool);
271
272
bool body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result = nullptr) override {
273
ERR_FAIL_COND_V(!Thread::is_main_thread(), false);
274
return physics_server_3d->body_test_motion(p_body, p_parameters, r_result);
275
}
276
277
// this function only works on physics process, errors and returns null otherwise
278
PhysicsDirectBodyState3D *body_get_direct_state(RID p_body) override {
279
ERR_FAIL_COND_V(!Thread::is_main_thread(), nullptr);
280
return physics_server_3d->body_get_direct_state(p_body);
281
}
282
283
/* SOFT BODY API */
284
285
FUNCRID(soft_body)
286
287
FUNC2(soft_body_update_rendering_server, RID, PhysicsServer3DRenderingServerHandler *)
288
289
FUNC2(soft_body_set_space, RID, RID)
290
FUNC1RC(RID, soft_body_get_space, RID)
291
292
FUNC2(soft_body_set_ray_pickable, RID, bool);
293
294
FUNC2(soft_body_set_collision_layer, RID, uint32_t)
295
FUNC1RC(uint32_t, soft_body_get_collision_layer, RID)
296
297
FUNC2(soft_body_set_collision_mask, RID, uint32_t)
298
FUNC1RC(uint32_t, soft_body_get_collision_mask, RID)
299
300
FUNC2(soft_body_add_collision_exception, RID, RID)
301
FUNC2(soft_body_remove_collision_exception, RID, RID)
302
FUNC2S(soft_body_get_collision_exceptions, RID, List<RID> *)
303
304
FUNC3(soft_body_set_state, RID, BodyState, const Variant &);
305
FUNC2RC(Variant, soft_body_get_state, RID, BodyState);
306
307
FUNC2(soft_body_set_transform, RID, const Transform3D &);
308
309
FUNC2(soft_body_set_simulation_precision, RID, int);
310
FUNC1RC(int, soft_body_get_simulation_precision, RID);
311
312
FUNC2(soft_body_set_total_mass, RID, real_t);
313
FUNC1RC(real_t, soft_body_get_total_mass, RID);
314
315
FUNC2(soft_body_set_linear_stiffness, RID, real_t);
316
FUNC1RC(real_t, soft_body_get_linear_stiffness, RID);
317
318
FUNC2(soft_body_set_shrinking_factor, RID, real_t);
319
FUNC1RC(real_t, soft_body_get_shrinking_factor, RID);
320
321
FUNC2(soft_body_set_pressure_coefficient, RID, real_t);
322
FUNC1RC(real_t, soft_body_get_pressure_coefficient, RID);
323
324
FUNC2(soft_body_set_damping_coefficient, RID, real_t);
325
FUNC1RC(real_t, soft_body_get_damping_coefficient, RID);
326
327
FUNC2(soft_body_set_drag_coefficient, RID, real_t);
328
FUNC1RC(real_t, soft_body_get_drag_coefficient, RID);
329
330
FUNC2(soft_body_set_mesh, RID, RID);
331
332
FUNC1RC(AABB, soft_body_get_bounds, RID);
333
334
FUNC3(soft_body_move_point, RID, int, const Vector3 &);
335
FUNC2RC(Vector3, soft_body_get_point_global_position, RID, int);
336
337
FUNC1(soft_body_remove_all_pinned_points, RID);
338
FUNC3(soft_body_pin_point, RID, int, bool);
339
FUNC2RC(bool, soft_body_is_point_pinned, RID, int);
340
341
/* JOINT API */
342
343
FUNCRID(joint)
344
345
FUNC1(joint_clear, RID)
346
347
FUNC5(joint_make_pin, RID, RID, const Vector3 &, RID, const Vector3 &)
348
349
FUNC3(pin_joint_set_param, RID, PinJointParam, real_t)
350
FUNC2RC(real_t, pin_joint_get_param, RID, PinJointParam)
351
352
FUNC2(pin_joint_set_local_a, RID, const Vector3 &)
353
FUNC1RC(Vector3, pin_joint_get_local_a, RID)
354
355
FUNC2(pin_joint_set_local_b, RID, const Vector3 &)
356
FUNC1RC(Vector3, pin_joint_get_local_b, RID)
357
358
FUNC5(joint_make_hinge, RID, RID, const Transform3D &, RID, const Transform3D &)
359
FUNC7(joint_make_hinge_simple, RID, RID, const Vector3 &, const Vector3 &, RID, const Vector3 &, const Vector3 &)
360
361
FUNC3(hinge_joint_set_param, RID, HingeJointParam, real_t)
362
FUNC2RC(real_t, hinge_joint_get_param, RID, HingeJointParam)
363
364
FUNC3(hinge_joint_set_flag, RID, HingeJointFlag, bool)
365
FUNC2RC(bool, hinge_joint_get_flag, RID, HingeJointFlag)
366
367
FUNC5(joint_make_slider, RID, RID, const Transform3D &, RID, const Transform3D &)
368
369
FUNC3(slider_joint_set_param, RID, SliderJointParam, real_t)
370
FUNC2RC(real_t, slider_joint_get_param, RID, SliderJointParam)
371
372
FUNC5(joint_make_cone_twist, RID, RID, const Transform3D &, RID, const Transform3D &)
373
374
FUNC3(cone_twist_joint_set_param, RID, ConeTwistJointParam, real_t)
375
FUNC2RC(real_t, cone_twist_joint_get_param, RID, ConeTwistJointParam)
376
377
FUNC5(joint_make_generic_6dof, RID, RID, const Transform3D &, RID, const Transform3D &)
378
379
FUNC4(generic_6dof_joint_set_param, RID, Vector3::Axis, G6DOFJointAxisParam, real_t)
380
FUNC3RC(real_t, generic_6dof_joint_get_param, RID, Vector3::Axis, G6DOFJointAxisParam)
381
382
FUNC4(generic_6dof_joint_set_flag, RID, Vector3::Axis, G6DOFJointAxisFlag, bool)
383
FUNC3RC(bool, generic_6dof_joint_get_flag, RID, Vector3::Axis, G6DOFJointAxisFlag)
384
385
FUNC1RC(JointType, joint_get_type, RID);
386
387
FUNC2(joint_set_solver_priority, RID, int);
388
FUNC1RC(int, joint_get_solver_priority, RID);
389
390
FUNC2(joint_disable_collisions_between_bodies, RID, bool);
391
FUNC1RC(bool, joint_is_disabled_collisions_between_bodies, RID);
392
393
/* MISC */
394
395
FUNC1(free, RID);
396
FUNC1(set_active, bool);
397
398
virtual void init() override;
399
virtual void step(real_t p_step) override;
400
virtual void sync() override;
401
virtual void end_sync() override;
402
virtual void flush_queries() override;
403
virtual void finish() override;
404
405
virtual bool is_flushing_queries() const override {
406
return physics_server_3d->is_flushing_queries();
407
}
408
409
int get_process_info(ProcessInfo p_info) override {
410
return physics_server_3d->get_process_info(p_info);
411
}
412
413
PhysicsServer3DWrapMT(PhysicsServer3D *p_contained, bool p_create_thread);
414
~PhysicsServer3DWrapMT();
415
416
#undef ServerNameWrapMT
417
#undef ServerName
418
#undef server_name
419
#undef WRITE_ACTION
420
};
421
422
#ifdef DEBUG_SYNC
423
#undef DEBUG_SYNC
424
#endif
425
#undef SYNC_DEBUG
426
427
#ifdef DEBUG_ENABLED
428
#undef MAIN_THREAD_SYNC_WARN
429
#endif
430
431