[qfvis] Use cluster sphere culling for base vis

While this doesn't give as much of a boost as does basic sphere culling
(since it's just culling sphere tests), it took ad_tears' base vis from
1000s to 720s on my machine.
This commit is contained in:
Bill Currie 2021-07-30 18:52:47 +09:00
parent 9d819254d4
commit ca8dcf3fa9
3 changed files with 153 additions and 65 deletions

View file

@ -126,6 +126,7 @@ typedef struct passage_s {
typedef struct cluster_s {
int numportals;
int visofs;
vspheref_t sphere;
passage_t *passages;
portal_t *portals;
} cluster_t;
@ -181,6 +182,7 @@ typedef struct threaddata_s {
typedef struct {
set_t *portalsee;
unsigned clustercull; ///< number of portals culled by cluster sphere
unsigned spherecull; ///< number of portals culled by sphere tests
unsigned windingcull; ///< number of portals culled by winding tests
int clustersee;

View file

@ -103,75 +103,132 @@ test_sphere (const vspheref_t *sphere, vec4f_t plane)
#endif
}
static int __attribute__ ((pure))
test_winding_front (const winding_t *winding, vec4f_t plane)
{
for (unsigned i = 0; i < winding->numpoints; i++) {
vec4f_t d = dotf (winding->points[i], plane);
if (d[0] > ON_EPSILON)
return 1;
}
return 0;
}
static int __attribute__ ((pure))
test_winding_back (const winding_t *winding, vec4f_t plane)
{
for (unsigned i = 0; i < winding->numpoints; i++) {
vec4f_t d = dotf (winding->points[i], plane);
if (d[0] < -ON_EPSILON)
return 1;
}
return 0;
}
void
PortalBase (basethread_t *thread, portal_t *portal)
{
unsigned i, j, k;
vec4f_t d;
portal_t *tp;
winding_t *winding;
cluster_t *cluster;
int tp_side, portal_side;
i = portal - portals;
for (cluster = clusters; cluster - clusters < portalclusters; cluster++) {
int side = test_sphere (&cluster->sphere, portal->plane);
for (j = 0, tp = portals; j < numportals * 2; j++, tp++) {
if (j == i)
continue;
// If the target portal is behind the portals's plane, then it
// can't possibly be seen by the portal.
//
// If the portal is in front of the target's plane, then the target
// is of no interest as it is facing counter to the flow of
// visibility.
// First check using the bounding spheres of the two portals.
tp_side = test_sphere (&tp->sphere, portal->plane);
if (tp_side < 0) {
// The test portal definitely is entirely behind the portal's
// plane.
thread->spherecull++;
continue; // entirely behind
}
portal_side = test_sphere (&portal->sphere, tp->plane);
if (portal_side > 0) {
// The portal definitely is entirely in front of the test
// portal's plane.
thread->spherecull++;
continue; // entirely in front
}
if (tp_side == 0) {
// The test portal's sphere touches the portal's plane, so
// do a more refined check.
winding = tp->winding;
for (k = 0; k < winding->numpoints; k++) {
d = dotf (winding->points[k], portal->plane);
if (d[0] > ON_EPSILON)
break;
if (side < 0) {
// The cluster is entirely behind the portal's plane, thus every
// portal in the cluster is also behind the portal's plane and
// cannot be seen at all.
thread->clustercull += cluster->numportals;
} else if (side > 0) {
// The cluster is entirely in front of the portal's plane, thus
// every portal in the cluster is also in front of the portal's
// plane and may be seen. However, as portals are one-way (ie,
// can see out the portal along its plane normal, but not into
// the portal against its plane normal), for a cluster's portal
// to be considered visible, the current portal must be behind
// the cluster's portal, or straddle its plane.
for (int i = 0; i < cluster->numportals; i++) {
tp = cluster->portals + i;
if (tp == portal) {
continue;
}
portal_side = test_sphere (&portal->sphere, tp->plane);
if (portal_side > 0) {
// The portal definitely is entirely in front of the test
// portal's plane. (cannot see into a portal from its
// front side)
thread->spherecull++;
} else if (portal_side < 0) {
// The portal's sphere is behind the test portal's plane,
// so the portal itself is entirely behind the plane
// thus the test portal is potentially visible.
set_add (thread->portalsee, tp - portals);
} else {
// The portal's sphere straddle's the test portal's
// plane, so need to do a more refined check.
if (test_winding_back (portal->winding, tp->plane)) {
// The portal is at least partially behind the test
// portal's plane, so the test portal is potentially
// visible.
set_add (thread->portalsee, tp - portals);
} else {
thread->windingcull++;
}
}
}
if (k == winding->numpoints) {
thread->windingcull++;
continue; // no points on front
} else {
// The cluster's sphere straddle's the portal's plane, thus each
// portal in the cluster must be tested individually.
for (int i = 0; i < cluster->numportals; i++) {
tp = cluster->portals + i;
if (tp == portal) {
continue;
}
// If the target portal is behind the portals's plane, then
// it can't possibly be seen by the portal.
//
// If the portal is in front of the target's plane, then the
// target is of no interest as it is facing counter to the
// flow of visibility.
// First check using the bounding spheres of the two portals.
tp_side = test_sphere (&tp->sphere, portal->plane);
if (tp_side < 0) {
// The test portal definitely is entirely behind the
// portal's plane.
thread->spherecull++;
continue; // entirely behind
}
portal_side = test_sphere (&portal->sphere, tp->plane);
if (portal_side > 0) {
// The portal definitely is entirely in front of the
// test portal's plane. (cannot see into a portal from
// its front side)
thread->spherecull++;
continue; // entirely in front
}
if (tp_side == 0) {
// The test portal's sphere touches the portal's plane,
// so do a more refined check.
if (!test_winding_front (tp->winding, portal->plane)) {
thread->windingcull++;
continue;
}
}
if (portal_side == 0) {
// The portal's sphere touches the test portal's plane,
// so do a more refined check.
if (!test_winding_back (portal->winding, tp->plane)) {
thread->windingcull++;
continue;
}
}
set_add (thread->portalsee, tp - portals);
}
}
if (portal_side == 0) {
// The portal's sphere touches the test portal's plane, so
// do a more refined check.
winding = portal->winding;
for (k = 0; k < winding->numpoints; k++) {
d = dotf (winding->points[k], tp->plane);
if (d[0] < -ON_EPSILON)
break;
}
if (k == winding->numpoints) {
thread->windingcull++;
continue; // no points on front
}
}
set_add (thread->portalsee, j);
}
SimpleFlood (thread, portal, portal->cluster);

View file

@ -75,9 +75,10 @@ options_t options;
static threaddata_t main_thread;
static visstat_t global_stats;
int base_mightsee;
unsigned base_spherecull;
unsigned base_windingcull;
unsigned long base_mightsee;
unsigned long base_clustercull;
unsigned long base_spherecull;
unsigned long base_windingcull;
static unsigned portal_count;
unsigned numportals;
@ -635,6 +636,7 @@ BaseVisThread (void *_thread)
} while (1);
WRLOCK (stats_lock);
base_clustercull += data.clustercull;
base_spherecull += data.spherecull;
base_windingcull += data.windingcull;
base_mightsee += num_mightsee;
@ -896,9 +898,10 @@ BasePortalVis (void)
end = Sys_DoubleTime ();
if (options.verbosity >= 1) {
printf ("base_mightsee: %d %gs\n", base_mightsee, end - start);
printf ("sphere cull: %u winding cull %u\n",
base_spherecull, base_windingcull);
printf ("base_mightsee: %lu %gs\n", base_mightsee, end - start);
printf ("cluster cull: %lu\n", base_clustercull);
printf (" sphere cull: %lu\n", base_spherecull);
printf ("winding cull: %lu\n", base_windingcull);
}
}
@ -978,6 +981,31 @@ CalcVis (void)
if (options.verbosity >= 0)
printf ("average clusters visible: %u\n", totalvis / portalclusters);
}
static void
CalcClusterSphers (void)
{
memhunk_t *hunk = main_thread.hunk;
for (unsigned i = 0; i < portalclusters; i++) {
cluster_t *cluster = &clusters[i];
int vertcount = 0;
for (int j = 0; j < cluster->numportals; j++) {
vertcount += cluster->portals[j].winding->numpoints;
}
size_t mark = Hunk_LowMark (hunk);
vec4f_t *verts = Hunk_RawAlloc (hunk, vertcount * sizeof (vec4f_t));
vertcount = 0;
for (int j = 0; j < cluster->numportals; j++) {
memcpy (verts + vertcount, cluster->portals[j].winding->points,
cluster->portals[j].winding->numpoints * sizeof (vec4f_t));
vertcount += cluster->portals[j].winding->numpoints;
}
cluster->sphere = SmallestEnclosingBall_vf (verts, vertcount);
Hunk_RawFreeToLowMark (hunk, mark);
}
}
#if 0
static qboolean
PlaneCompare (plane_t *p1, plane_t *p2)
@ -1388,6 +1416,7 @@ generate_pvs (void)
uncompressed = calloc (bitbytes_l, portalclusters);
CalcClusterSphers ();
CalcVis ();
if (options.verbosity >= 1)