ref: refactor R_BuildLightMap

This commit is contained in:
Alibek Omarov 2024-11-02 22:06:19 +03:00
parent 9608da5bf9
commit b39d660189
2 changed files with 58 additions and 85 deletions

View file

@ -729,38 +729,40 @@ Combine and scale multiple lightmaps into the floating
format in r_blocklights format in r_blocklights
================= =================
*/ */
static void R_BuildLightMap( msurface_t *surf, byte *dest, int stride, qboolean dynamic ) static void R_BuildLightMap( const msurface_t *surf, byte *dest, int stride, qboolean dynamic )
{ {
int smax, tmax; int map, t;
uint *bl, scale; const mextrasurf_t *info = surf->info;
int i, map, size, s, t;
int sample_size;
mextrasurf_t *info = surf->info;
color24 *lm;
int lightscale; int lightscale;
sample_size = gEngfuncs.Mod_SampleSizeForFace( surf ); const int sample_size = gEngfuncs.Mod_SampleSizeForFace( surf );
smax = ( info->lightextents[0] / sample_size ) + 1; const int smax = ( info->lightextents[0] / sample_size ) + 1;
tmax = ( info->lightextents[1] / sample_size ) + 1; const int tmax = ( info->lightextents[1] / sample_size ) + 1;
size = smax * tmax; const int size = smax * tmax;
if( gl_overbright.value ) if( gl_overbright.value )
lightscale = ( R_HasEnabledVBO() && !r_vbo_overbrightmode.value) ? 171 : 256; lightscale = ( R_HasEnabledVBO() && !r_vbo_overbrightmode.value) ? 171 : 256;
else lightscale = ( pow( 2.0f, 1.0f / v_lightgamma->value ) * 256 ) + 0.5; else lightscale = ( pow( 2.0f, 1.0f / v_lightgamma->value ) * 256 ) + 0.5;
lm = surf->samples;
memset( r_blocklights, 0, sizeof( uint ) * size * 3 ); memset( r_blocklights, 0, sizeof( uint ) * size * 3 );
// add all the lightmaps // add all the lightmaps
for( map = 0; map < MAXLIGHTMAPS && surf->styles[map] != 255 && lm; map++ ) for( map = 0; map < MAXLIGHTMAPS && surf->samples; map++ )
{ {
const color24 *lm = &surf->samples[map * size];
uint scale;
int i;
if( surf->styles[map] >= 255 )
break;
scale = tr.lightstylevalue[surf->styles[map]]; scale = tr.lightstylevalue[surf->styles[map]];
for( i = 0, bl = r_blocklights; i < size; i++, bl += 3, lm++ ) for( i = 0; i < size; i++ )
{ {
bl[0] += lm->r * scale; r_blocklights[i * 3 + 0] += lm[i].r * scale;
bl[1] += lm->g * scale; r_blocklights[i * 3 + 1] += lm[i].g * scale;
bl[2] += lm->b * scale; r_blocklights[i * 3 + 2] += lm[i].b * scale;
} }
} }
@ -768,15 +770,16 @@ static void R_BuildLightMap( msurface_t *surf, byte *dest, int stride, qboolean
if( surf->dlightframe == tr.framecount && dynamic ) if( surf->dlightframe == tr.framecount && dynamic )
R_AddDynamicLights( surf ); R_AddDynamicLights( surf );
// Put into texture format for( t = 0; t < tmax; t++ )
stride -= (smax << 2);
bl = r_blocklights;
for( t = 0; t < tmax; t++, dest += stride )
{ {
int s;
for( s = 0; s < smax; s++ ) for( s = 0; s < smax; s++ )
{ {
const uint *bl = &r_blocklights[(s + (t * smax)) * 3];
byte *dst = &dest[(t * stride) + (s * 4)];
int i; int i;
for( i = 0; i < 3; i++ ) for( i = 0; i < 3; i++ )
{ {
int t = bl[i] * lightscale >> 14; int t = bl[i] * lightscale >> 14;
@ -784,12 +787,9 @@ static void R_BuildLightMap( msurface_t *surf, byte *dest, int stride, qboolean
if( t > 1023 ) if( t > 1023 )
t = 1023; t = 1023;
dest[i] = gEngfuncs.LightToTexGammaEx( t ) >> 2; dst[i] = gEngfuncs.LightToTexGammaEx( t ) >> 2;
} }
dest[3] = 255; dst[3] = 255;
bl += 3;
dest += 4;
} }
} }
} }

View file

@ -180,25 +180,15 @@ format in r_blocklights
*/ */
static void R_BuildLightMap( void ) static void R_BuildLightMap( void )
{ {
int smax, tmax; int map, t, i;
uint *bl, scale; const msurface_t *surf = r_drawsurf.surf;
int i, map, size, s, t; const mextrasurf_t *info = surf->info;
int sample_size; const int sample_size = gEngfuncs.Mod_SampleSizeForFace( surf );
msurface_t *surf = r_drawsurf.surf; int smax = ( info->lightextents[0] / sample_size ) + 1;
mextrasurf_t *info = surf->info; int tmax = ( info->lightextents[1] / sample_size ) + 1;
color24 *lm; int size = smax * tmax;
qboolean dynamic = 0;
sample_size = gEngfuncs.Mod_SampleSizeForFace( surf ); if( FBitSet( surf->flags, SURF_CONVEYOR ))
smax = ( info->lightextents[0] / sample_size ) + 1;
tmax = ( info->lightextents[1] / sample_size ) + 1;
//smax = (surf->extents[0]>>4)+1;
//tmax = (surf->extents[1]>>4)+1;
size = smax * tmax;
if( surf->flags & SURF_CONVEYOR )
{ {
smax = ( info->lightextents[0] * 3 / sample_size ) + 1; smax = ( info->lightextents[0] * 3 / sample_size ) + 1;
size = smax * tmax; size = smax * tmax;
@ -206,61 +196,44 @@ static void R_BuildLightMap( void )
return; return;
} }
lm = surf->samples;
memset( blocklights, 0, sizeof( uint ) * size ); memset( blocklights, 0, sizeof( uint ) * size );
// add all the lightmaps // add all the lightmaps
for( map = 0; map < MAXLIGHTMAPS && surf->styles[map] != 255; map++ ) for( map = 0; map < MAXLIGHTMAPS && surf->samples; map++ )
{ {
const color24 *lm = &surf->samples[map * size];
uint scale;
int i;
if( surf->styles[map] >= 255 )
break;
scale = tr.lightstylevalue[surf->styles[map]]; scale = tr.lightstylevalue[surf->styles[map]];
for( i = 0, bl = blocklights; i < size; i++, bl += 1, lm++ ) for( i = 0; i < size; i++ )
{ blocklights[i] += ( lm[i].r + lm[i].g + lm[i].b ) * scale;
bl[0] += ( lm->r + lm->g + lm->b ) * scale;
}
} }
// add all the dynamic lights // add all the dynamic lights
if( surf->dlightframe == tr.framecount ) if( surf->dlightframe == tr.framecount )
R_AddDynamicLights( surf ); R_AddDynamicLights( surf );
// Put into texture format
//stride -= (smax << 2);
//bl = blocklights;
/*for( t = 0; t < tmax; t++, dest += stride )
{
for( s = 0; s < smax; s++ )
{
dest[0] = Q_min((bl[0] >> 7), 255 );
//dest[1] = Q_min((bl[1] >> 7), 255 );
//dest[2] = Q_min((bl[2] >> 7), 255 );
//dest[3] = 255;
bl += 3;
dest += 4;
}
}*/
// bound, invert, and shift // bound, invert, and shift
for (i=0 ; i<size ; i++) for( i = 0; i < size; i++ )
{ {
if( blocklights[i] < 65280 ) if( blocklights[i] < 65280 )
t = gEngfuncs.LightToTexGammaEx( blocklights[i] >> 6 ) << 6; t = gEngfuncs.LightToTexGammaEx( blocklights[i] >> 6 ) << 6;
else t = (int)blocklights[i]; else t = (int)blocklights[i];
if (t < 0) t = bound( 0, t, 65535 * 3 );
t = 0; t = t / 2048 / 3;//(255*256 - t) >> (8 - VID_CBITS);
if( t > 65535 * 3 )
t = 65535 * 3;
t = t / 2048 / 3;//(255*256 - t) >> (8 - VID_CBITS);
//if (t < (1 << 6)) //if (t < (1 << 6))
//t = (1 << 6); //t = (1 << 6);
t = t << 8; t = t << 8;
blocklights[i] = t; blocklights[i] = t;
} }
} }
#else #else