Skip to content

Commit bdde909

Browse files
authored
Threading Framework & Threaded Collisions (scp-fs2open#6814)
* Dispatch ship weapon collisions and defer handling * multithread ship weapon collision * properly spin * proper memory order * externalize generic threadpool settings * Wait for threads to shut down * Add result pipelining * cleanup * incorrect nullopt type * randomrange todo * parallelize ship ship collisions * bogus debug return * remove warnings * clang tidy
1 parent 6921f55 commit bdde909

14 files changed

Lines changed: 827 additions & 412 deletions

code/cmdline/cmdline.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ cmdline_parm luadev_arg("-luadev", "Make lua errors non-fatal", AT_NONE); // Cmd
534534
cmdline_parm override_arg("-override_data", "Enable override directory", AT_NONE); // Cmdline_override_data
535535
cmdline_parm imgui_debug_arg("-imgui_debug", nullptr, AT_NONE);
536536
cmdline_parm vulkan("-vulkan", nullptr, AT_NONE);
537+
cmdline_parm multithreading("-threads", nullptr, AT_INT);
537538

538539
char *Cmdline_start_mission = NULL;
539540
int Cmdline_dis_collisions = 0;
@@ -572,6 +573,7 @@ bool Cmdline_lua_devmode = false;
572573
bool Cmdline_override_data = false;
573574
bool Cmdline_show_imgui_debug = false;
574575
bool Cmdline_vulkan = false;
576+
int Cmdline_multithreading = 1;
575577

576578
// Other
577579
cmdline_parm get_flags_arg(GET_FLAGS_STRING, "Output the launcher flags file", AT_STRING);
@@ -2407,6 +2409,14 @@ bool SetCmdlineParams()
24072409
}
24082410
}
24092411

2412+
if (multithreading.found()) {
2413+
Cmdline_multithreading = abs(multithreading.get_int());
2414+
if (Cmdline_multithreading < 1) {
2415+
Cmdline_multithreading = 1;
2416+
Warning(LOCATION,"-threads must be an integer greater or equal to 1. Invalid thread count will be disregarded.");
2417+
}
2418+
}
2419+
24102420
return true;
24112421
}
24122422

code/cmdline/cmdline.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ extern bool Cmdline_lua_devmode;
159159
extern bool Cmdline_override_data;
160160
extern bool Cmdline_show_imgui_debug;
161161
extern bool Cmdline_vulkan;
162+
extern int Cmdline_multithreading;
162163

163164
enum class WeaponSpewType { NONE = 0, STANDARD, ALL };
164165
extern WeaponSpewType Cmdline_spew_weapon_stats;

code/model/modelcollide.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,24 @@
3030
// checking a collision rather than passing a bunch of parameters around. These are
3131
// not persistant between calls to model_collide
3232

33-
static mc_info *Mc; // The mc_info passed into model_collide
34-
35-
static polymodel *Mc_pm; // The polygon model we're checking
36-
static int Mc_submodel; // The current submodel we're checking
33+
thread_local static mc_info *Mc; // The mc_info passed into model_collide
34+
35+
thread_local static polymodel *Mc_pm; // The polygon model we're checking
36+
thread_local static int Mc_submodel; // The current submodel we're checking
3737

38-
static polymodel_instance *Mc_pmi;
38+
thread_local static polymodel_instance *Mc_pmi;
3939

40-
static matrix Mc_orient; // A matrix to rotate a world point into the current
40+
thread_local static matrix Mc_orient; // A matrix to rotate a world point into the current
4141
// submodel's frame of reference.
42-
static vec3d Mc_base; // A point used along with Mc_orient.
42+
thread_local static vec3d Mc_base; // A point used along with Mc_orient.
4343

44-
static vec3d Mc_p0; // The ray origin rotated into the current submodel's frame of reference
45-
static vec3d Mc_p1; // The ray end rotated into the current submodel's frame of reference
46-
static float Mc_mag; // The length of the ray
47-
static vec3d Mc_direction; // A vector from the ray's origin to its end, in the current submodel's frame of reference
44+
thread_local static vec3d Mc_p0; // The ray origin rotated into the current submodel's frame of reference
45+
thread_local static vec3d Mc_p1; // The ray end rotated into the current submodel's frame of reference
46+
thread_local static float Mc_mag; // The length of the ray
47+
thread_local static vec3d Mc_direction; // A vector from the ray's origin to its end, in the current submodel's frame of reference
4848

49-
static vec3d **Mc_point_list = NULL; // A pointer to the current submodel's vertex list
49+
thread_local static vec3d **Mc_point_list = nullptr; // A pointer to the current submodel's vertex list
5050

51-
static float Mc_edge_time;
5251

5352

5453
void model_collide_free_point_list()
@@ -1135,7 +1134,6 @@ int model_collide(mc_info *mc_info_obj)
11351134
Mc_orient = *Mc->orient;
11361135
Mc_base = *Mc->pos;
11371136
Mc_mag = vm_vec_dist( Mc->p0, Mc->p1 );
1138-
Mc_edge_time = FLT_MAX;
11391137

11401138
if ( Mc->model_instance_num >= 0 ) {
11411139
Mc_pmi = model_get_instance(Mc->model_instance_num);

0 commit comments

Comments
 (0)