wrap bit flags operations

This commit is contained in:
Garux 2021-10-28 14:39:58 +03:00
parent 7626f69b02
commit 2b59253b26
5 changed files with 53 additions and 55 deletions

View File

@ -22,6 +22,14 @@ inline void value_minimize( T& value, const T& other ){
}
inline bool bit_is_enabled( const byte *bytes, int bit_index ){
return ( bytes[bit_index >> 3] & ( 1 << ( bit_index & 7 ) ) ) != 0;
}
inline void bit_enable( byte *bytes, int bit_index ){
bytes[bit_index >> 3] |= ( 1 << ( bit_index & 7 ) );
}
template<typename T>
struct MinMax___
{

View File

@ -3019,7 +3019,7 @@ bool ClusterVisible( int a, int b ){
const byte *pvs = bspVisBytes.data() + VIS_HEADER_SIZE + ( a * leafBytes );
/* check */
return ( pvs[ b >> 3 ] & ( 1 << ( b & 7 ) ) );
return bit_is_enabled( pvs, b );
}

View File

@ -1761,9 +1761,6 @@ static bool ApproximateLightmap( rawLightmap_t *lm ){
*/
static bool TestOutLightmapStamp( rawLightmap_t *lm, int lightmapNum, outLightmap_t *olm, int x, int y ){
int sx, sy, ox, oy, offset;
/* bounds check */
if ( x < 0 || y < 0 || ( x + lm->w ) > olm->customWidth || ( y + lm->h ) > olm->customHeight ) {
return false;
@ -1771,17 +1768,16 @@ static bool TestOutLightmapStamp( rawLightmap_t *lm, int lightmapNum, outLightma
/* solid lightmaps test a 1x1 stamp */
if ( lm->solid[ lightmapNum ] ) {
offset = ( y * olm->customWidth ) + x;
if ( olm->lightBits[ offset >> 3 ] & ( 1 << ( offset & 7 ) ) ) {
if ( bit_is_enabled( olm->lightBits, ( y * olm->customWidth ) + x ) ) {
return false;
}
return true;
}
/* test the stamp */
for ( sy = 0; sy < lm->h; sy++ )
for ( int sy = 0; sy < lm->h; ++sy )
{
for ( sx = 0; sx < lm->w; sx++ )
for ( int sx = 0; sx < lm->w; ++sx )
{
/* get luxel */
if ( lm->getBspLuxel( lightmapNum, sx, sy )[ 0 ] < 0.0f ) {
@ -1789,10 +1785,9 @@ static bool TestOutLightmapStamp( rawLightmap_t *lm, int lightmapNum, outLightma
}
/* get bsp lightmap coords and test */
ox = x + sx;
oy = y + sy;
offset = ( oy * olm->customWidth ) + ox;
if ( olm->lightBits[ offset >> 3 ] & ( 1 << ( offset & 7 ) ) ) {
const int ox = x + sx;
const int oy = y + sy;
if ( bit_is_enabled( olm->lightBits, ( oy * olm->customWidth ) + ox ) ) {
return false;
}
}
@ -1856,7 +1851,7 @@ static void SetupOutLightmap( rawLightmap_t *lm, outLightmap_t *olm ){
#define LIGHTMAP_RESERVE_COUNT 1
static void FindOutLightmaps( rawLightmap_t *lm, bool fastAllocate ){
int i, j, k, lightmapNum, xMax, yMax, x = -1, y = -1, sx, sy, ox, oy, offset;
int i, j, k, lightmapNum, xMax, yMax, x = -1, y = -1, sx, sy, ox, oy;
outLightmap_t *olm;
surfaceInfo_t *info;
bool ok;
@ -2140,10 +2135,9 @@ static void FindOutLightmaps( rawLightmap_t *lm, bool fastAllocate ){
/* get bsp lightmap coords */
ox = x + lm->lightmapX[ lightmapNum ];
oy = y + lm->lightmapY[ lightmapNum ];
offset = ( oy * olm->customWidth ) + ox;
/* flag pixel as used */
olm->lightBits[ offset >> 3 ] |= ( 1 << ( offset & 7 ) );
bit_enable( olm->lightBits, ( oy * olm->customWidth ) + ox );
olm->freeLuxels--;
/* store color */
@ -2249,7 +2243,7 @@ void FillOutLightmap( outLightmap_t *olm ){
for ( x = 0; x < olm->customWidth; ++x )
{
ofs = y * olm->customWidth + x;
if ( olm->lightBits[ofs >> 3] & ( 1 << ( ofs & 7 ) ) ) { /* already filled */
if ( bit_is_enabled( olm->lightBits, ofs ) ) { /* already filled */
continue;
}
cnt = 0;
@ -2257,7 +2251,7 @@ void FillOutLightmap( outLightmap_t *olm ){
/* try all four neighbors */
ofs = ( ( y + olm->customHeight - 1 ) % olm->customHeight ) * olm->customWidth + x;
if ( olm->lightBits[ofs >> 3] & ( 1 << ( ofs & 7 ) ) ) { /* already filled */
if ( bit_is_enabled( olm->lightBits, ofs ) ) { /* already filled */
++cnt;
light_sum += olm->bspLightBytes[ofs];
if ( deluxemap ) {
@ -2266,7 +2260,7 @@ void FillOutLightmap( outLightmap_t *olm ){
}
ofs = ( ( y + 1 ) % olm->customHeight ) * olm->customWidth + x;
if ( olm->lightBits[ofs >> 3] & ( 1 << ( ofs & 7 ) ) ) { /* already filled */
if ( bit_is_enabled( olm->lightBits, ofs ) ) { /* already filled */
++cnt;
light_sum += olm->bspLightBytes[ofs];
if ( deluxemap ) {
@ -2275,7 +2269,7 @@ void FillOutLightmap( outLightmap_t *olm ){
}
ofs = y * olm->customWidth + ( x + olm->customWidth - 1 ) % olm->customWidth;
if ( olm->lightBits[ofs >> 3] & ( 1 << ( ofs & 7 ) ) ) { /* already filled */
if ( bit_is_enabled( olm->lightBits, ofs ) ) { /* already filled */
++cnt;
light_sum += olm->bspLightBytes[ofs];
if ( deluxemap ) {
@ -2284,7 +2278,7 @@ void FillOutLightmap( outLightmap_t *olm ){
}
ofs = y * olm->customWidth + ( x + 1 ) % olm->customWidth;
if ( olm->lightBits[ofs >> 3] & ( 1 << ( ofs & 7 ) ) ) { /* already filled */
if ( bit_is_enabled( olm->lightBits, ofs ) ) { /* already filled */
++cnt;
light_sum += olm->bspLightBytes[ofs];
if ( deluxemap ) {
@ -2295,7 +2289,7 @@ void FillOutLightmap( outLightmap_t *olm ){
if ( cnt ) {
++filled;
ofs = y * olm->customWidth + x;
lightBitsNew[ofs >> 3] |= ( 1 << ( ofs & 7 ) );
bit_enable( lightBitsNew, ofs );
lightBytesNew[ofs] = light_sum * ( 1.0 / cnt );
if ( deluxemap ) {
dirBytesNew[ofs] = dir_sum * ( 1.0 / cnt );
@ -2986,7 +2980,7 @@ void StoreSurfaceLightmaps( bool fastAllocate ){
}
else if( lightmapPink ){
for ( x = 0; x < olm->customHeight * olm->customWidth; ++x ){
if ( ( olm->lightBits[x >> 3] & ( 1 << ( x & 7 ) ) ) == 0 ) { /* not filled */
if ( !bit_is_enabled( olm->lightBits, x ) ) { /* not filled */
olm->bspLightBytes[x] = { 255, 0, 255 };
}
}

View File

@ -96,9 +96,9 @@ void SortPortals( void ){
int LeafVectorFromPortalVector( byte *portalbits, byte *leafbits ){
for ( int i = 0; i < numportals * 2; ++i )
{
if ( portalbits[i >> 3] & ( 1 << ( i & 7 ) ) ) {
if ( bit_is_enabled( portalbits, i ) ) {
const vportal_t& p = portals[i];
leafbits[p.leaf >> 3] |= ( 1 << ( p.leaf & 7 ) );
bit_enable( leafbits, p.leaf );
}
}
@ -108,8 +108,8 @@ int LeafVectorFromPortalVector( byte *portalbits, byte *leafbits ){
while ( leafs[leafnum].merged >= 0 )
leafnum = leafs[leafnum].merged;
//if the merged leaf is visible then the original leaf is visible
if ( leafbits[leafnum >> 3] & ( 1 << ( leafnum & 7 ) ) ) {
leafbits[i >> 3] |= ( 1 << ( i & 7 ) );
if ( bit_is_enabled( leafbits, leafnum ) ) {
bit_enable( leafbits, i );
}
}
return CountBits( leafbits, portalclusters ); //c_leafs
@ -148,13 +148,12 @@ void ClusterMerge( int leafnum ){
}
for ( int j = 0; j < portallongs; ++j )
( (long *)portalvector )[j] |= ( (long *)p->portalvis )[j];
const int pnum = p - portals;
portalvector[pnum >> 3] |= 1 << ( pnum & 7 );
bit_enable( portalvector, p - portals );
}
memset( uncompressed, 0, leafbytes );
uncompressed[mergedleafnum >> 3] |= ( 1 << ( mergedleafnum & 7 ) );
bit_enable( uncompressed, mergedleafnum );
// convert portal bits to leaf bits
numvis = LeafVectorFromPortalVector( portalvector, uncompressed );

View File

@ -56,7 +56,7 @@
int CountBits( const byte *bits, int numbits ){
int c = 0;
for ( int i = 0; i < numbits; ++i )
if ( bits[i >> 3] & ( 1 << ( i & 7 ) ) ) {
if ( bit_is_enabled( bits, i ) ) {
c++;
}
@ -380,7 +380,6 @@ void RecursiveLeafFlow( int leafnum, threaddata_t *thread, pstack_t *prevstack )
leaf_t *leaf;
int j, n;
long *test, *might, *prevmight, *vis, more;
int pnum;
thread->c_chains++;
@ -408,7 +407,7 @@ void RecursiveLeafFlow( int leafnum, threaddata_t *thread, pstack_t *prevstack )
if ( p->removed ) {
continue;
}
pnum = p - portals;
const int pnum = p - portals;
/* MrE: portal trace debug code
{
@ -429,7 +428,7 @@ void RecursiveLeafFlow( int leafnum, threaddata_t *thread, pstack_t *prevstack )
}
*/
if ( !( prevstack->mightsee[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) {
if ( !bit_is_enabled( prevstack->mightsee, pnum ) ) {
continue; // can't possibly see it
}
@ -451,7 +450,7 @@ void RecursiveLeafFlow( int leafnum, threaddata_t *thread, pstack_t *prevstack )
}
if ( !more &&
( thread->base->portalvis[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) { // can't see anything new
bit_is_enabled( thread->base->portalvis, pnum ) ) { // can't see anything new
continue;
}
@ -522,7 +521,7 @@ void RecursiveLeafFlow( int leafnum, threaddata_t *thread, pstack_t *prevstack )
if ( !prevstack->pass ) { // the second leaf can only be blocked if coplanar
// mark the portal as visible
thread->base->portalvis[pnum >> 3] |= ( 1 << ( pnum & 7 ) );
bit_enable( thread->base->portalvis, pnum );
RecursiveLeafFlow( p->leaf, thread, &stack );
continue;
@ -574,7 +573,7 @@ void RecursiveLeafFlow( int leafnum, threaddata_t *thread, pstack_t *prevstack )
}
// mark the portal as visible
thread->base->portalvis[pnum >> 3] |= ( 1 << ( pnum & 7 ) );
bit_enable( thread->base->portalvis, pnum );
// flow through it for real
RecursiveLeafFlow( p->leaf, thread, &stack );
@ -641,7 +640,6 @@ void RecursivePassageFlow( vportal_t *portal, threaddata_t *thread, pstack_t *pr
passage_t *passage, *nextpassage;
int i, j;
long *might, *vis, *prevmight, *cansee, *portalvis, more;
int pnum;
leaf = &leafs[portal->leaf];
@ -662,14 +660,14 @@ void RecursivePassageFlow( vportal_t *portal, threaddata_t *thread, pstack_t *pr
continue;
}
nextpassage = passage->next;
pnum = p - portals;
const int pnum = p - portals;
if ( !( prevstack->mightsee[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) {
if ( !bit_is_enabled( prevstack->mightsee, pnum ) ) {
continue; // can't possibly see it
}
// mark the portal as visible
thread->base->portalvis[pnum >> 3] |= ( 1 << ( pnum & 7 ) );
bit_enable( thread->base->portalvis, pnum );
prevmight = (long *)prevstack->mightsee;
cansee = (long *)passage->cansee;
@ -764,7 +762,6 @@ void RecursivePassagePortalFlow( vportal_t *portal, threaddata_t *thread, pstack
passage_t *passage, *nextpassage;
int i, j, n;
long *might, *vis, *prevmight, *cansee, *portalvis, more;
int pnum;
// thread->c_chains++;
@ -795,9 +792,9 @@ void RecursivePassagePortalFlow( vportal_t *portal, threaddata_t *thread, pstack
continue;
}
nextpassage = passage->next;
pnum = p - portals;
const int pnum = p - portals;
if ( !( prevstack->mightsee[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) {
if ( !bit_is_enabled( prevstack->mightsee, pnum ) ) {
continue; // can't possibly see it
}
@ -823,7 +820,7 @@ void RecursivePassagePortalFlow( vportal_t *portal, threaddata_t *thread, pstack
might++;
}
if ( !more && ( thread->base->portalvis[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) { // can't see anything new
if ( !more && bit_is_enabled( thread->base->portalvis, pnum ) ) { // can't see anything new
continue;
}
@ -894,7 +891,7 @@ void RecursivePassagePortalFlow( vportal_t *portal, threaddata_t *thread, pstack
if ( !prevstack->pass ) { // the second leaf can only be blocked if coplanar
// mark the portal as visible
thread->base->portalvis[pnum >> 3] |= ( 1 << ( pnum & 7 ) );
bit_enable( thread->base->portalvis, pnum );
RecursivePassagePortalFlow( p, thread, &stack );
continue;
@ -946,7 +943,7 @@ void RecursivePassagePortalFlow( vportal_t *portal, threaddata_t *thread, pstack
}
// mark the portal as visible
thread->base->portalvis[pnum >> 3] |= ( 1 << ( pnum & 7 ) );
bit_enable( thread->base->portalvis, pnum );
// flow through it for real
RecursivePassagePortalFlow( p, thread, &stack );
@ -1273,10 +1270,10 @@ void CreatePassages( int portalnum ){
if ( p->removed ) {
continue;
}
if ( !( target->portalflood[j >> 3] & ( 1 << ( j & 7 ) ) ) ) {
if ( !bit_is_enabled( target->portalflood, j ) ) {
continue;
}
if ( !( portal->portalflood[j >> 3] & ( 1 << ( j & 7 ) ) ) ) {
if ( !bit_is_enabled( portal->portalflood, j ) ) {
continue;
}
for ( k = 0; k < numseperators; k++ )
@ -1334,7 +1331,7 @@ void CreatePassages( int portalnum ){
if ( k < numseperators ) {
continue;
}
passage->cansee[j >> 3] |= ( 1 << ( j & 7 ) );
bit_enable( passage->cansee, j );
numsee++;
}
}
@ -1418,15 +1415,15 @@ void SimpleFlood( vportal_t *srcportal, int leafnum ){
continue;
}
const int pnum = p - portals;
if ( !( srcportal->portalfront[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) {
if ( !bit_is_enabled( srcportal->portalfront, pnum ) ) {
continue;
}
if ( srcportal->portalflood[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) {
if ( bit_is_enabled( srcportal->portalflood, pnum ) ) {
continue;
}
srcportal->portalflood[pnum >> 3] |= ( 1 << ( pnum & 7 ) );
bit_enable( srcportal->portalflood, pnum );
SimpleFlood( srcportal, p->leaf );
}
@ -1516,7 +1513,7 @@ void BasePortalVis( int portalnum ){
continue; // no points on front
}
p->portalfront[j >> 3] |= ( 1 << ( j & 7 ) );
bit_enable( p->portalfront, j );
}
SimpleFlood( p, p->leaf );
@ -1560,7 +1557,7 @@ void RecursiveLeafBitFlow( int leafnum, byte *mightsee, byte *cansee ){
const int pnum = p - portals;
// if some previous portal can't see it, skip
if ( !( mightsee[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) {
if ( !bit_is_enabled( mightsee, pnum ) ) {
continue;
}
@ -1577,7 +1574,7 @@ void RecursiveLeafBitFlow( int leafnum, byte *mightsee, byte *cansee ){
continue; // can't see anything new
}
cansee[pnum >> 3] |= ( 1 << ( pnum & 7 ) );
bit_enable( cansee, pnum );
RecursiveLeafBitFlow( p->leaf, newmight, cansee );
}