summaryrefslogtreecommitdiff
path: root/servers/physics_server_3d_wrap_mt.h
blob: 1d5fa95bfba2888287e17686087f5ade45018879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*************************************************************************/
/*  physics_server_3d_wrap_mt.h                                          */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#ifndef PHYSICS_SERVER_3D_WRAP_MT_H
#define PHYSICS_SERVER_3D_WRAP_MT_H

#include "core/config/project_settings.h"
#include "core/os/thread.h"
#include "core/templates/command_queue_mt.h"
#include "servers/physics_server_3d.h"

#ifdef DEBUG_SYNC
#define SYNC_DEBUG print_line("sync on: " + String(__FUNCTION__));
#else
#define SYNC_DEBUG
#endif

class PhysicsServer3DWrapMT : public PhysicsServer3D {
	mutable PhysicsServer3D *physics_server_3d;

	mutable CommandQueueMT command_queue;

	static void _thread_callback(void *_instance);
	void thread_loop();

	Thread::ID server_thread;
	Thread::ID main_thread;
	volatile bool exit = false;
	Thread thread;
	volatile bool step_thread_up = false;
	bool create_thread = false;

	Semaphore step_sem;
	void thread_step(real_t p_delta);

	void thread_exit();

	bool first_frame = true;

	Mutex alloc_mutex;
	int pool_max_size = 0;

public:
#define ServerName PhysicsServer3D
#define ServerNameWrapMT PhysicsServer3DWrapMT
#define server_name physics_server_3d
#define WRITE_ACTION

#include "servers/server_wrap_mt_common.h"

	//FUNC1RID(shape,ShapeType); todo fix
	FUNCRID(world_boundary_shape)
	FUNCRID(separation_ray_shape)
	FUNCRID(sphere_shape)
	FUNCRID(box_shape)
	FUNCRID(capsule_shape)
	FUNCRID(cylinder_shape)
	FUNCRID(convex_polygon_shape)
	FUNCRID(concave_polygon_shape)
	FUNCRID(heightmap_shape)
	FUNCRID(custom_shape)

	FUNC2(shape_set_data, RID, const Variant &);
	FUNC2(shape_set_custom_solver_bias, RID, real_t);

	FUNC2(shape_set_margin, RID, real_t)
	FUNC1RC(real_t, shape_get_margin, RID)

	FUNC1RC(ShapeType, shape_get_type, RID);
	FUNC1RC(Variant, shape_get_data, RID);
	FUNC1RC(real_t, shape_get_custom_solver_bias, RID);
#if 0
	//these work well, but should be used from the main thread only
	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) {
		ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), false);
		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);
	}
#endif
	/* SPACE API */

	FUNCRID(space);
	FUNC2(space_set_active, RID, bool);
	FUNC1RC(bool, space_is_active, RID);

	FUNC3(space_set_param, RID, SpaceParameter, real_t);
	FUNC2RC(real_t, space_get_param, RID, SpaceParameter);

	// this function only works on physics process, errors and returns null otherwise
	PhysicsDirectSpaceState3D *space_get_direct_state(RID p_space) override {
		ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), nullptr);
		return physics_server_3d->space_get_direct_state(p_space);
	}

	FUNC2(space_set_debug_contacts, RID, int);
	virtual Vector<Vector3> space_get_contacts(RID p_space) const override {
		ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), Vector<Vector3>());
		return physics_server_3d->space_get_contacts(p_space);
	}

	virtual int space_get_contact_count(RID p_space) const override {
		ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), 0);
		return physics_server_3d->space_get_contact_count(p_space);
	}

	/* AREA API */

	//FUNC0RID(area);
	FUNCRID(area);

	FUNC2(area_set_space, RID, RID);
	FUNC1RC(RID, area_get_space, RID);

	FUNC4(area_add_shape, RID, RID, const Transform3D &, bool);
	FUNC3(area_set_shape, RID, int, RID);
	FUNC3(area_set_shape_transform, RID, int, const Transform3D &);
	FUNC3(area_set_shape_disabled, RID, int, bool);

	FUNC1RC(int, area_get_shape_count, RID);
	FUNC2RC(RID, area_get_shape, RID, int);
	FUNC2RC(Transform3D, area_get_shape_transform, RID, int);
	FUNC2(area_remove_shape, RID, int);
	FUNC1(area_clear_shapes, RID);

	FUNC2(area_attach_object_instance_id, RID, ObjectID);
	FUNC1RC(ObjectID, area_get_object_instance_id, RID);

	FUNC3(area_set_param, RID, AreaParameter, const Variant &);
	FUNC2(area_set_transform, RID, const Transform3D &);

	FUNC2RC(Variant, area_get_param, RID, AreaParameter);
	FUNC1RC(Transform3D, area_get_transform, RID);

	FUNC2(area_set_collision_layer, RID, uint32_t);
	FUNC1RC(uint32_t, area_get_collision_layer, RID);

	FUNC2(area_set_collision_mask, RID, uint32_t);
	FUNC1RC(uint32_t, area_get_collision_mask, RID);

	FUNC2(area_set_monitorable, RID, bool);
	FUNC2(area_set_ray_pickable, RID, bool);

	FUNC2(area_set_monitor_callback, RID, const Callable &);
	FUNC2(area_set_area_monitor_callback, RID, const Callable &);

	/* BODY API */

	//FUNC2RID(body,BodyMode,bool);
	FUNCRID(body)

	FUNC2(body_set_space, RID, RID);
	FUNC1RC(RID, body_get_space, RID);

	FUNC2(body_set_mode, RID, BodyMode);
	FUNC1RC(BodyMode, body_get_mode, RID);

	FUNC4(body_add_shape, RID, RID, const Transform3D &, bool);
	FUNC3(body_set_shape, RID, int, RID);
	FUNC3(body_set_shape_transform, RID, int, const Transform3D &);

	FUNC1RC(int, body_get_shape_count, RID);
	FUNC2RC(Transform3D, body_get_shape_transform, RID, int);
	FUNC2RC(RID, body_get_shape, RID, int);

	FUNC3(body_set_shape_disabled, RID, int, bool);

	FUNC2(body_remove_shape, RID, int);
	FUNC1(body_clear_shapes, RID);

	FUNC2(body_attach_object_instance_id, RID, ObjectID);
	FUNC1RC(ObjectID, body_get_object_instance_id, RID);

	FUNC2(body_set_enable_continuous_collision_detection, RID, bool);
	FUNC1RC(bool, body_is_continuous_collision_detection_enabled, RID);

	FUNC2(body_set_collision_layer, RID, uint32_t);
	FUNC1RC(uint32_t, body_get_collision_layer, RID);

	FUNC2(body_set_collision_mask, RID, uint32_t);
	FUNC1RC(uint32_t, body_get_collision_mask, RID);

	FUNC2(body_set_collision_priority, RID, real_t);
	FUNC1RC(real_t, body_get_collision_priority, RID);

	FUNC2(body_set_user_flags, RID, uint32_t);
	FUNC1RC(uint32_t, body_get_user_flags, RID);

	FUNC3(body_set_param, RID, BodyParameter, const Variant &);
	FUNC2RC(Variant, body_get_param, RID, BodyParameter);

	FUNC1(body_reset_mass_properties, RID);

	FUNC3(body_set_state, RID, BodyState, const Variant &);
	FUNC2RC(Variant, body_get_state, RID, BodyState);

	FUNC2(body_apply_torque_impulse, RID, const Vector3 &);
	FUNC2(body_apply_central_impulse, RID, const Vector3 &);
	FUNC3(body_apply_impulse, RID, const Vector3 &, const Vector3 &);

	FUNC2(body_apply_central_force, RID, const Vector3 &);
	FUNC3(body_apply_force, RID, const Vector3 &, const Vector3 &);
	FUNC2(body_apply_torque, RID, const Vector3 &);

	FUNC2(body_add_constant_central_force, RID, const Vector3 &);
	FUNC3(body_add_constant_force, RID, const Vector3 &, const Vector3 &);
	FUNC2(body_add_constant_torque, RID, const Vector3 &);

	FUNC2(body_set_constant_force, RID, const Vector3 &);
	FUNC1RC(Vector3, body_get_constant_force, RID);

	FUNC2(body_set_constant_torque, RID, const Vector3 &);
	FUNC1RC(Vector3, body_get_constant_torque, RID);

	FUNC2(body_set_axis_velocity, RID, const Vector3 &);

	FUNC3(body_set_axis_lock, RID, BodyAxis, bool);
	FUNC2RC(bool, body_is_axis_locked, RID, BodyAxis);

	FUNC2(body_add_collision_exception, RID, RID);
	FUNC2(body_remove_collision_exception, RID, RID);
	FUNC2S(body_get_collision_exceptions, RID, List<RID> *);

	FUNC2(body_set_max_contacts_reported, RID, int);
	FUNC1RC(int, body_get_max_contacts_reported, RID);

	FUNC2(body_set_contacts_reported_depth_threshold, RID, real_t);
	FUNC1RC(real_t, body_get_contacts_reported_depth_threshold, RID);

	FUNC2(body_set_omit_force_integration, RID, bool);
	FUNC1RC(bool, body_is_omitting_force_integration, RID);

	FUNC2(body_set_state_sync_callback, RID, const Callable &);
	FUNC3(body_set_force_integration_callback, RID, const Callable &, const Variant &);

	FUNC2(body_set_ray_pickable, RID, bool);

	bool body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result = nullptr) override {
		ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), false);
		return physics_server_3d->body_test_motion(p_body, p_parameters, r_result);
	}

	// this function only works on physics process, errors and returns null otherwise
	PhysicsDirectBodyState3D *body_get_direct_state(RID p_body) override {
		ERR_FAIL_COND_V(main_thread != Thread::get_caller_id(), nullptr);
		return physics_server_3d->body_get_direct_state(p_body);
	}

	/* SOFT BODY API */

	FUNCRID(soft_body)

	FUNC2(soft_body_update_rendering_server, RID, PhysicsServer3DRenderingServerHandler *)

	FUNC2(soft_body_set_space, RID, RID)
	FUNC1RC(RID, soft_body_get_space, RID)

	FUNC2(soft_body_set_ray_pickable, RID, bool);

	FUNC2(soft_body_set_collision_layer, RID, uint32_t)
	FUNC1RC(uint32_t, soft_body_get_collision_layer, RID)

	FUNC2(soft_body_set_collision_mask, RID, uint32_t)
	FUNC1RC(uint32_t, soft_body_get_collision_mask, RID)

	FUNC2(soft_body_add_collision_exception, RID, RID)
	FUNC2(soft_body_remove_collision_exception, RID, RID)
	FUNC2S(soft_body_get_collision_exceptions, RID, List<RID> *)

	FUNC3(soft_body_set_state, RID, BodyState, const Variant &);
	FUNC2RC(Variant, soft_body_get_state, RID, BodyState);

	FUNC2(soft_body_set_transform, RID, const Transform3D &);

	FUNC2(soft_body_set_simulation_precision, RID, int);
	FUNC1RC(int, soft_body_get_simulation_precision, RID);

	FUNC2(soft_body_set_total_mass, RID, real_t);
	FUNC1RC(real_t, soft_body_get_total_mass, RID);

	FUNC2(soft_body_set_linear_stiffness, RID, real_t);
	FUNC1RC(real_t, soft_body_get_linear_stiffness, RID);

	FUNC2(soft_body_set_pressure_coefficient, RID, real_t);
	FUNC1RC(real_t, soft_body_get_pressure_coefficient, RID);

	FUNC2(soft_body_set_damping_coefficient, RID, real_t);
	FUNC1RC(real_t, soft_body_get_damping_coefficient, RID);

	FUNC2(soft_body_set_drag_coefficient, RID, real_t);
	FUNC1RC(real_t, soft_body_get_drag_coefficient, RID);

	FUNC2(soft_body_set_mesh, RID, RID);

	FUNC1RC(AABB, soft_body_get_bounds, RID);

	FUNC3(soft_body_move_point, RID, int, const Vector3 &);
	FUNC2RC(Vector3, soft_body_get_point_global_position, RID, int);

	FUNC1(soft_body_remove_all_pinned_points, RID);
	FUNC3(soft_body_pin_point, RID, int, bool);
	FUNC2RC(bool, soft_body_is_point_pinned, RID, int);

	/* JOINT API */

	FUNCRID(joint)

	FUNC1(joint_clear, RID)

	FUNC5(joint_make_pin, RID, RID, const Vector3 &, RID, const Vector3 &)

	FUNC3(pin_joint_set_param, RID, PinJointParam, real_t)
	FUNC2RC(real_t, pin_joint_get_param, RID, PinJointParam)

	FUNC2(pin_joint_set_local_a, RID, const Vector3 &)
	FUNC1RC(Vector3, pin_joint_get_local_a, RID)

	FUNC2(pin_joint_set_local_b, RID, const Vector3 &)
	FUNC1RC(Vector3, pin_joint_get_local_b, RID)

	FUNC5(joint_make_hinge, RID, RID, const Transform3D &, RID, const Transform3D &)
	FUNC7(joint_make_hinge_simple, RID, RID, const Vector3 &, const Vector3 &, RID, const Vector3 &, const Vector3 &)

	FUNC3(hinge_joint_set_param, RID, HingeJointParam, real_t)
	FUNC2RC(real_t, hinge_joint_get_param, RID, HingeJointParam)

	FUNC3(hinge_joint_set_flag, RID, HingeJointFlag, bool)
	FUNC2RC(bool, hinge_joint_get_flag, RID, HingeJointFlag)

	FUNC5(joint_make_slider, RID, RID, const Transform3D &, RID, const Transform3D &)

	FUNC3(slider_joint_set_param, RID, SliderJointParam, real_t)
	FUNC2RC(real_t, slider_joint_get_param, RID, SliderJointParam)

	FUNC5(joint_make_cone_twist, RID, RID, const Transform3D &, RID, const Transform3D &)

	FUNC3(cone_twist_joint_set_param, RID, ConeTwistJointParam, real_t)
	FUNC2RC(real_t, cone_twist_joint_get_param, RID, ConeTwistJointParam)

	FUNC5(joint_make_generic_6dof, RID, RID, const Transform3D &, RID, const Transform3D &)

	FUNC4(generic_6dof_joint_set_param, RID, Vector3::Axis, G6DOFJointAxisParam, real_t)
	FUNC3RC(real_t, generic_6dof_joint_get_param, RID, Vector3::Axis, G6DOFJointAxisParam)

	FUNC4(generic_6dof_joint_set_flag, RID, Vector3::Axis, G6DOFJointAxisFlag, bool)
	FUNC3RC(bool, generic_6dof_joint_get_flag, RID, Vector3::Axis, G6DOFJointAxisFlag)

	FUNC1RC(JointType, joint_get_type, RID);

	FUNC2(joint_set_solver_priority, RID, int);
	FUNC1RC(int, joint_get_solver_priority, RID);

	FUNC2(joint_disable_collisions_between_bodies, RID, bool);
	FUNC1RC(bool, joint_is_disabled_collisions_between_bodies, RID);

	/* MISC */

	FUNC1(free, RID);
	FUNC1(set_active, bool);

	virtual void init() override;
	virtual void step(real_t p_step) override;
	virtual void sync() override;
	virtual void end_sync() override;
	virtual void flush_queries() override;
	virtual void finish() override;

	virtual bool is_flushing_queries() const override {
		return physics_server_3d->is_flushing_queries();
	}

	int get_process_info(ProcessInfo p_info) override {
		return physics_server_3d->get_process_info(p_info);
	}

	PhysicsServer3DWrapMT(PhysicsServer3D *p_contained, bool p_create_thread);
	~PhysicsServer3DWrapMT();

#undef ServerNameWrapMT
#undef ServerName
#undef server_name
#undef WRITE_ACTION
};

#ifdef DEBUG_SYNC
#undef DEBUG_SYNC
#endif
#undef SYNC_DEBUG

#endif // PHYSICS_SERVER_3D_WRAP_MT_H