slight speed boost on large maps by merging this loop into one (saves calculating metrics twice)

This commit is contained in:
Jonathan 2022-01-24 15:10:26 -05:00
parent 1764afb475
commit 6dadb1d8f6
1 changed files with 23 additions and 27 deletions

View File

@ -270,8 +270,10 @@ The clipping hull BSP doesn't worry about avoiding splits
static std::list<surface_t>::iterator ChooseMidPlaneFromList(std::list<surface_t> &surfaces, const aabb3d &bounds) static std::list<surface_t>::iterator ChooseMidPlaneFromList(std::list<surface_t> &surfaces, const aabb3d &bounds)
{ {
/* pick the plane that splits the least */ /* pick the plane that splits the least */
vec_t bestmetric = VECT_MAX; vec_t bestaxialmetric = VECT_MAX;
std::list<surface_t>::iterator bestsurface = surfaces.end(); std::list<surface_t>::iterator bestaxialsurface = surfaces.end();
vec_t bestanymetric = VECT_MAX;
std::list<surface_t>::iterator bestanysurface = surfaces.end();
for (int pass = 0; pass < 2; pass++) { for (int pass = 0; pass < 2; pass++) {
for (auto surf = surfaces.begin(); surf != surfaces.end(); surf++) { for (auto surf = surfaces.begin(); surf != surfaces.end(); surf++) {
@ -283,44 +285,38 @@ static std::list<surface_t>::iterator ChooseMidPlaneFromList(std::list<surface_t
if (!surf->has_struct && !pass) if (!surf->has_struct && !pass)
continue; continue;
/* check for axis aligned surfaces */
const qbsp_plane_t &plane = map.planes[surf->planenum]; const qbsp_plane_t &plane = map.planes[surf->planenum];
if (!(plane.type < 3)) bool axial = false;
continue;
/* check for axis aligned surfaces */
if (plane.type < 3) {
axial = true;
}
/* calculate the split metric, smaller values are better */ /* calculate the split metric, smaller values are better */
const vec_t metric = SplitPlaneMetric(plane, bounds); const vec_t metric = SplitPlaneMetric(plane, bounds);
if (metric < bestmetric) {
bestmetric = metric; if (metric < bestanymetric) {
bestsurface = surf; bestanymetric = metric;
bestanysurface = surf;
} }
}
if (bestsurface == surfaces.end()) { if (axial) {
// TODO: we can probably merge this into the loop above if (metric < bestaxialmetric) {
/* Choose based on spatial subdivision only */ bestaxialmetric = metric;
for (auto surf = surfaces.begin(); surf != surfaces.end(); surf++) { bestaxialsurface = surf;
if (surf->onnode)
continue;
if (surf->has_struct && pass)
continue;
if (!surf->has_struct && !pass)
continue;
const qbsp_plane_t &plane = map.planes[surf->planenum];
const vec_t metric = SplitPlaneMetric(plane, bounds);
if (metric < bestmetric) {
bestmetric = metric;
bestsurface = surf;
} }
} }
} }
if (bestsurface != surfaces.end()) if (bestanysurface != surfaces.end() || bestaxialsurface != surfaces.end()) {
break; break;
}
} }
// prefer the axial split
auto bestsurface = (bestaxialsurface == surfaces.end()) ? bestanysurface : bestaxialsurface;
if (bestsurface == surfaces.end()) if (bestsurface == surfaces.end())
FError("No valid planes in surface list"); FError("No valid planes in surface list");