use std::vector for flagging in SmoothNormals()

functions run about 10% faster due to cheaper access to a flag
This commit is contained in:
Garux 2021-03-13 11:16:07 +03:00
parent 1d4424f4c6
commit c7fedadcd1
2 changed files with 20 additions and 38 deletions

View File

@ -159,19 +159,15 @@ Vector3b ColorToBytes( const Vector3& color, float scale ){
void SmoothNormals( void ){ void SmoothNormals( void ){
int i, j, k, f, fOld, start; int i, j, k, f, fOld, start;
float shadeAngle, defaultShadeAngle, maxShadeAngle; float shadeAngle, defaultShadeAngle, maxShadeAngle;
bspDrawSurface_t *ds;
shaderInfo_t *si;
float *shadeAngles;
byte *smoothed;
int indexes[ MAX_SAMPLES ]; int indexes[ MAX_SAMPLES ];
Vector3 votes[ MAX_SAMPLES ]; Vector3 votes[ MAX_SAMPLES ];
/* allocate shade angle table */ /* allocate shade angle table */
shadeAngles = safe_calloc( numBSPDrawVerts * sizeof( float ) ); std::vector<float> shadeAngles( numBSPDrawVerts, 0 );
/* allocate smoothed table */ /* allocate smoothed table */
smoothed = safe_calloc( ( numBSPDrawVerts / 8 ) + 1 ); std::vector<std::uint8_t> smoothed( numBSPDrawVerts, false );
/* set default shade angle */ /* set default shade angle */
defaultShadeAngle = degrees_to_radians( shadeAngleDegrees ); defaultShadeAngle = degrees_to_radians( shadeAngleDegrees );
@ -182,10 +178,10 @@ void SmoothNormals( void ){
for ( i = 0; i < numBSPDrawSurfaces; i++ ) for ( i = 0; i < numBSPDrawSurfaces; i++ )
{ {
/* get drawsurf */ /* get drawsurf */
ds = &bspDrawSurfaces[ i ]; bspDrawSurface_t& ds = bspDrawSurfaces[ i ];
/* get shader for shade angle */ /* get shader for shade angle */
si = surfaceInfos[ i ].si; const shaderInfo_t *si = surfaceInfos[ i ].si;
if ( si->shadeAngleDegrees ) { if ( si->shadeAngleDegrees ) {
shadeAngle = degrees_to_radians( si->shadeAngleDegrees ); shadeAngle = degrees_to_radians( si->shadeAngleDegrees );
} }
@ -195,26 +191,24 @@ void SmoothNormals( void ){
value_maximize( maxShadeAngle, shadeAngle ); value_maximize( maxShadeAngle, shadeAngle );
/* flag its verts */ /* flag its verts */
for ( j = 0; j < ds->numVerts; j++ ) for ( j = 0; j < ds.numVerts; j++ )
{ {
f = ds->firstVert + j; f = ds.firstVert + j;
shadeAngles[ f ] = shadeAngle; shadeAngles[ f ] = shadeAngle;
if ( ds->surfaceType == MST_TRIANGLE_SOUP ) { if ( ds.surfaceType == MST_TRIANGLE_SOUP ) {
smoothed[ f >> 3 ] |= ( 1 << ( f & 7 ) ); smoothed[ f ] = true;
} }
} }
/* ydnar: optional force-to-trisoup */ /* ydnar: optional force-to-trisoup */
if ( trisoup && ds->surfaceType == MST_PLANAR ) { if ( trisoup && ds.surfaceType == MST_PLANAR ) {
ds->surfaceType = MST_TRIANGLE_SOUP; ds.surfaceType = MST_TRIANGLE_SOUP;
ds->lightmapNum[ 0 ] = -3; ds.lightmapNum[ 0 ] = -3;
} }
} }
/* bail if no surfaces have a shade angle */ /* bail if no surfaces have a shade angle */
if ( maxShadeAngle == 0 ) { if ( maxShadeAngle == 0 ) {
free( shadeAngles );
free( smoothed );
return; return;
} }
@ -233,7 +227,7 @@ void SmoothNormals( void ){
} }
/* already smoothed? */ /* already smoothed? */
if ( smoothed[ i >> 3 ] & ( 1 << ( i & 7 ) ) ) { if ( smoothed[ i ] ) {
continue; continue;
} }
@ -246,7 +240,7 @@ void SmoothNormals( void ){
for ( j = i; j < numBSPDrawVerts && numVerts < MAX_SAMPLES; j++ ) for ( j = i; j < numBSPDrawVerts && numVerts < MAX_SAMPLES; j++ )
{ {
/* already smoothed? */ /* already smoothed? */
if ( smoothed[ j >> 3 ] & ( 1 << ( j & 7 ) ) ) { if ( smoothed[ j ] ) {
continue; continue;
} }
@ -270,7 +264,7 @@ void SmoothNormals( void ){
indexes[ numVerts++ ] = j; indexes[ numVerts++ ] = j;
/* flag vertex */ /* flag vertex */
smoothed[ j >> 3 ] |= ( 1 << ( j & 7 ) ); smoothed[ j ] = true;
/* see if this normal has already been voted */ /* see if this normal has already been voted */
for ( k = 0; k < numVotes; k++ ) for ( k = 0; k < numVotes; k++ )
@ -301,10 +295,6 @@ void SmoothNormals( void ){
} }
} }
/* free the tables */
free( shadeAngles );
free( smoothed );
/* print time */ /* print time */
Sys_Printf( " (%i)\n", (int) ( I_FloatTime() - start ) ); Sys_Printf( " (%i)\n", (int) ( I_FloatTime() - start ) );
} }

View File

@ -1098,8 +1098,6 @@ void SmoothMetaTriangles( void ){
int i, j, k, f, fOld, start, numSmoothed; int i, j, k, f, fOld, start, numSmoothed;
float shadeAngle, defaultShadeAngle, maxShadeAngle; float shadeAngle, defaultShadeAngle, maxShadeAngle;
metaTriangle_t *tri; metaTriangle_t *tri;
float *shadeAngles;
byte *smoothed;
int indexes[ MAX_SAMPLES ]; int indexes[ MAX_SAMPLES ];
Vector3 votes[ MAX_SAMPLES ]; Vector3 votes[ MAX_SAMPLES ];
@ -1107,10 +1105,10 @@ void SmoothMetaTriangles( void ){
Sys_FPrintf( SYS_VRB, "--- SmoothMetaTriangles ---\n" ); Sys_FPrintf( SYS_VRB, "--- SmoothMetaTriangles ---\n" );
/* allocate shade angle table */ /* allocate shade angle table */
shadeAngles = safe_calloc( numMetaVerts * sizeof( float ) ); std::vector<float> shadeAngles( numMetaVerts, 0 );
/* allocate smoothed table */ /* allocate smoothed table */
smoothed = safe_calloc( ( numMetaVerts / 8 ) + 1 ); std::vector<std::uint8_t> smoothed( numMetaVerts, false );
/* set default shade angle */ /* set default shade angle */
defaultShadeAngle = degrees_to_radians( npDegrees ); defaultShadeAngle = degrees_to_radians( npDegrees );
@ -1142,7 +1140,7 @@ void SmoothMetaTriangles( void ){
{ {
shadeAngles[ tri->indexes[ j ] ] = shadeAngle; shadeAngles[ tri->indexes[ j ] ] = shadeAngle;
if ( shadeAngle <= 0 ) { if ( shadeAngle <= 0 ) {
smoothed[ tri->indexes[ j ] >> 3 ] |= ( 1 << ( tri->indexes[ j ] & 7 ) ); smoothed[ tri->indexes[ j ] ] = true;
} }
} }
} }
@ -1150,8 +1148,6 @@ void SmoothMetaTriangles( void ){
/* bail if no surfaces have a shade angle */ /* bail if no surfaces have a shade angle */
if ( maxShadeAngle <= 0 ) { if ( maxShadeAngle <= 0 ) {
Sys_FPrintf( SYS_VRB, "No smoothing angles specified, aborting\n" ); Sys_FPrintf( SYS_VRB, "No smoothing angles specified, aborting\n" );
free( shadeAngles );
free( smoothed );
return; return;
} }
@ -1171,7 +1167,7 @@ void SmoothMetaTriangles( void ){
} }
/* already smoothed? */ /* already smoothed? */
if ( smoothed[ i >> 3 ] & ( 1 << ( i & 7 ) ) ) { if ( smoothed[ i ] ) {
continue; continue;
} }
@ -1184,7 +1180,7 @@ void SmoothMetaTriangles( void ){
for ( j = i; j < numMetaVerts && numVerts < MAX_SAMPLES; j++ ) for ( j = i; j < numMetaVerts && numVerts < MAX_SAMPLES; j++ )
{ {
/* already smoothed? */ /* already smoothed? */
if ( smoothed[ j >> 3 ] & ( 1 << ( j & 7 ) ) ) { if ( smoothed[ j ] ) {
continue; continue;
} }
@ -1206,7 +1202,7 @@ void SmoothMetaTriangles( void ){
indexes[ numVerts++ ] = j; indexes[ numVerts++ ] = j;
/* flag vertex */ /* flag vertex */
smoothed[ j >> 3 ] |= ( 1 << ( j & 7 ) ); smoothed[ j ] = true;
/* see if this normal has already been voted */ /* see if this normal has already been voted */
for ( k = 0; k < numVotes; k++ ) for ( k = 0; k < numVotes; k++ )
@ -1238,10 +1234,6 @@ void SmoothMetaTriangles( void ){
} }
} }
/* free the tables */
free( shadeAngles );
free( smoothed );
/* print time */ /* print time */
Sys_FPrintf( SYS_VRB, " (%d)\n", (int) ( I_FloatTime() - start ) ); Sys_FPrintf( SYS_VRB, " (%d)\n", (int) ( I_FloatTime() - start ) );