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