vis: code style updates

This commit is contained in:
Eric Wasylishen 2023-11-11 12:59:45 -07:00
parent 37fc62ca0e
commit e3c6ace7ad
2 changed files with 45 additions and 68 deletions

View File

@ -2,7 +2,6 @@
#include <vis/leafbits.hh>
#include <common/log.hh>
#include <common/parallel.hh>
#include <atomic>
/*
==============
@ -27,28 +26,21 @@
static void ClipToSeparators(visstats_t &stats, const viswinding_t *source, const qplane3d src_pl, const viswinding_t *pass,
viswinding_t *&target, unsigned int test, pstack_t &stack)
{
size_t i, j, k, l;
qplane3d sep;
qvec3d v1, v2;
vec_t d;
int count;
bool fliptest;
vec_t len_sq;
// check all combinations
for (i = 0; i < source->size(); i++) {
l = (i + 1) % source->size();
v1 = source->at(l) - source->at(i);
for (size_t i = 0; i < source->size(); i++) {
const size_t l = (i + 1) % source->size();
const qvec3d v1 = source->at(l) - source->at(i);
// find a vertex of pass that makes a plane that puts all of the
// vertexes of pass on the front side and all of the vertexes of
// source on the back side
for (j = 0; j < pass->size(); j++) {
for (size_t j = 0; j < pass->size(); j++) {
// Which side of the source portal is this point?
// This also tells us which side of the separating plane has
// the source portal.
d = src_pl.distance_to(pass->at(j));
bool fliptest;
vec_t d = src_pl.distance_to(pass->at(j));
if (d < -VIS_ON_EPSILON)
fliptest = true;
else if (d > VIS_ON_EPSILON)
@ -57,9 +49,10 @@ static void ClipToSeparators(visstats_t &stats, const viswinding_t *source, cons
continue; // Point lies in source plane
// Make a plane with the three points
v2 = pass->at(j) - source->at(i);
qplane3d sep;
const qvec3d v2 = pass->at(j) - source->at(i);
sep.normal = qv::cross(v1, v2);
len_sq = qv::length2(sep.normal);
const vec_t len_sq = qv::length2(sep.normal);
// If points don't make a valid plane, skip it.
if (len_sq < VIS_ON_EPSILON)
@ -78,8 +71,9 @@ static void ClipToSeparators(visstats_t &stats, const viswinding_t *source, cons
// if all of the pass portal points are now on the positive side,
// this is the separating plane
//
count = 0;
for (k = 0; k < pass->size(); k++) {
int count = 0;
size_t k = 0;
for (; k < pass->size(); k++) {
if (k == j)
continue;
d = sep.distance_to(pass->at(k));
@ -120,9 +114,7 @@ static void ClipToSeparators(visstats_t &stats, const viswinding_t *source, cons
static int CheckStack(leaf_t *leaf, threaddata_t *thread)
{
pstack_t *p;
for (p = thread->pstack_head.next; p; p = p->next)
for (pstack_t *p = thread->pstack_head.next; p; p = p->next)
if (p->leaf == leaf)
return 1;
return 0;

View File

@ -1,16 +1,16 @@
// vis.c
#include <vis/vis.hh>
#include <vis/leafbits.hh>
#include <common/log.hh>
#include <common/bsputils.hh>
#include <common/fs.hh>
#include <common/parallel.hh>
#include <climits>
#include <cstdint>
#include <bit> // for std::countr_zero
#include <numeric> // for std::accumulate
#include <vis/leafbits.hh>
#include <vis/vis.hh>
#include <common/log.hh>
#include <common/bsputils.hh>
#include <common/fs.hh>
#include <common/parallel.hh>
#include <fmt/chrono.h>
/*
@ -116,8 +116,7 @@ viswinding_t *ClipStackWinding(visstats_t &stats, viswinding_t *in, pstack_t &st
{
vec_t dists[MAX_WINDING + 1];
int sides[MAX_WINDING + 1];
int counts[3];
size_t i, j;
size_t i;
/* Fast test first */
vec_t dot = split.distance_to(in->origin);
@ -131,7 +130,7 @@ viswinding_t *ClipStackWinding(visstats_t &stats, viswinding_t *in, pstack_t &st
if (in->size() > MAX_WINDING)
FError("in->numpoints > MAX_WINDING ({} > {})", in->size(), MAX_WINDING);
counts[0] = counts[1] = counts[2] = 0;
int counts[3] = {0, 0, 0};
/* determine sides for each point */
for (i = 0; i < in->size(); i++) {
@ -191,7 +190,7 @@ viswinding_t *ClipStackWinding(visstats_t &stats, viswinding_t *in, pstack_t &st
const qvec3d &p2 = (*in)[(i + 1) % in->size()];
qvec3d mid;
vec_t fraction = dists[i] / (dists[i] - dists[i + 1]);
for (j = 0; j < 3; j++) {
for (size_t j = 0; j < 3; j++) {
/* avoid round off error when possible */
if (split.normal[j] == 1)
mid[j] = split.dist;
@ -295,11 +294,6 @@ static void UpdateMightsee(visstats_t &stats, const leaf_t &source, const leaf_t
*/
static void PortalCompleted(visstats_t &stats, visportal_t *completed)
{
int i, j, k, bit, numblocks;
int leafnum;
const visportal_t *p, *p2;
uint32_t changed;
portal_mutex.lock();
completed->status = pstat_done;
@ -309,16 +303,16 @@ static void PortalCompleted(visstats_t &stats, visportal_t *completed)
* mightsee during the full vis so far.
*/
const leaf_t &myleaf = leafs[completed->leaf];
for (i = 0; i < myleaf.numportals; i++) {
p = myleaf.portals[i];
for (int i = 0; i < myleaf.numportals; i++) {
const visportal_t *p = myleaf.portals[i];
if (p->status != pstat_done)
continue;
auto might = p->mightsee.data();
auto vis = p->visbits.data();
numblocks = (portalleafs + leafbits_t::mask) >> leafbits_t::shift;
for (j = 0; j < numblocks; j++) {
changed = might[j] & ~vis[j];
int numblocks = (portalleafs + leafbits_t::mask) >> leafbits_t::shift;
for (int j = 0; j < numblocks; j++) {
uint32_t changed = might[j] & ~vis[j];
if (!changed)
continue;
@ -326,10 +320,10 @@ static void PortalCompleted(visstats_t &stats, visportal_t *completed)
* If any of these changed bits are still visible from another
* portal, we can't update yet.
*/
for (k = 0; k < myleaf.numportals; k++) {
for (int k = 0; k < myleaf.numportals; k++) {
if (k == i)
continue;
p2 = myleaf.portals[k];
const visportal_t *p2 = myleaf.portals[k];
if (p2->status == pstat_done)
changed &= ~p2->visbits.data()[j];
else
@ -342,9 +336,9 @@ static void PortalCompleted(visstats_t &stats, visportal_t *completed)
* Update mightsee for any of the changed bits that survived
*/
while (changed) {
bit = std::countr_zero(changed);
int bit = std::countr_zero(changed);
changed &= ~nth_bit(bit);
leafnum = (j << leafbits_t::shift) + bit;
int leafnum = (j << leafbits_t::shift) + bit;
UpdateMightsee(stats, leafs[leafnum], myleaf);
}
}
@ -363,8 +357,6 @@ static duration stateinterval;
*/
static visstats_t LeafThread()
{
visportal_t *p;
portal_mutex.lock();
/* Save state if sufficient time has elapsed */
auto now = I_FloatTime();
@ -374,7 +366,7 @@ static visstats_t LeafThread()
}
portal_mutex.unlock();
p = GetNextPortal();
visportal_t *p = GetNextPortal();
if (!p)
return {};
@ -401,22 +393,16 @@ static std::vector<uint8_t> compressed;
static void ClusterFlow(int clusternum, leafbits_t &buffer, mbsp_t *bsp)
{
leaf_t *leaf;
uint8_t *outbuffer;
int i, j;
int numvis, numblocks;
const visportal_t *p;
/*
* Collect visible bits from all portals into buffer
*/
leaf = &leafs[clusternum];
numblocks = (portalleafs + leafbits_t::mask) >> leafbits_t::shift;
for (i = 0; i < leaf->numportals; i++) {
p = leaf->portals[i];
leaf_t *leaf = &leafs[clusternum];
int numblocks = (portalleafs + leafbits_t::mask) >> leafbits_t::shift;
for (int i = 0; i < leaf->numportals; i++) {
const visportal_t *p = leaf->portals[i];
if (p->status != pstat_done)
FError("portal not done");
for (j = 0; j < numblocks; j++)
for (int j = 0; j < numblocks; j++)
buffer.data()[j] |= p->visbits.data()[j];
}
@ -430,11 +416,12 @@ static void ClusterFlow(int clusternum, leafbits_t &buffer, mbsp_t *bsp)
/*
* Now expand the clusters into the full leaf visibility map
*/
numvis = 0;
int numvis = 0;
uint8_t *outbuffer;
if (bsp->loadversion->game->id == GAME_QUAKE_II) {
outbuffer = uncompressed.data() + clusternum * leafbytes;
for (i = 0; i < portalleafs; i++) {
for (int i = 0; i < portalleafs; i++) {
if (buffer[i]) {
outbuffer[i >> 3] |= nth_bit(i & 7);
numvis++;
@ -442,7 +429,7 @@ static void ClusterFlow(int clusternum, leafbits_t &buffer, mbsp_t *bsp)
}
} else {
outbuffer = uncompressed.data() + clusternum * leafbytes_real;
for (i = 0; i < portalleafs_real; i++) {
for (int i = 0; i < portalleafs_real; i++) {
if (buffer[bsp->dleafs[i + 1].cluster]) {
outbuffer[i >> 3] |= nth_bit(i & 7);
numvis++;
@ -463,7 +450,7 @@ static void ClusterFlow(int clusternum, leafbits_t &buffer, mbsp_t *bsp)
// FIXME: not sure what this is supposed to be?
totalvis += numvis;
} else {
for (i = 0; i < portalleafs_real; i++) {
for (int i = 0; i < portalleafs_real; i++) {
if (bsp->dleafs[i + 1].cluster == clusternum) {
totalvis += numvis;
}
@ -486,7 +473,7 @@ static void ClusterFlow(int clusternum, leafbits_t &buffer, mbsp_t *bsp)
// Set pointers
if (bsp->loadversion->game->id != GAME_QUAKE_II) {
for (i = 0; i < portalleafs_real; i++) {
for (int i = 0; i < portalleafs_real; i++) {
if (bsp->dleafs[i + 1].cluster == clusternum) {
bsp->dleafs[i + 1].visofs = visofs;
}
@ -552,8 +539,6 @@ visstats_t CalcPortalVis(const mbsp_t *bsp)
*/
visstats_t CalcVis(mbsp_t *bsp)
{
int i;
if (LoadVisState()) {
logging::print("Loaded previous state. Resuming progress...\n");
} else {
@ -569,7 +554,7 @@ visstats_t CalcVis(mbsp_t *bsp)
//
logging::print("Expanding clusters...\n");
leafbits_t buffer(portalleafs);
for (i = 0; i < portalleafs; i++) {
for (int i = 0; i < portalleafs; i++) {
ClusterFlow(i, buffer, bsp);
buffer.clear();
}