ref: gl: slight refactoring, split large R_RenderBrushPoly to smaller functions

* Use R_RenderDetailsForSurface in R_AddSurfToVBO instead of copypaste
* Use existing VBO's R_CheckLightMap in R_RenderLightmapForSurface
This commit is contained in:
Alibek Omarov 2024-09-05 02:21:23 +03:00
parent 5c2ab150b3
commit ac50c762d7

View file

@ -1135,43 +1135,21 @@ static void R_RenderDetails( int passes )
GL_ResetFogColor(); GL_ResetFogColor();
} }
/* static void R_RenderFullbrightForSurface( msurface_t *fa, texture_t *t )
================
R_RenderBrushPoly
================
*/
static void R_RenderBrushPoly( msurface_t *fa, int cull_type )
{ {
qboolean is_dynamic = false; if( !t->fb_texturenum )
int maps;
texture_t *t;
r_stats.c_world_polys++;
if( fa->flags & SURF_DRAWSKY )
return; // already handled
t = R_TextureAnimation( fa );
if( FBitSet( fa->flags, SURF_DRAWTURB ))
{
R_UploadRipples( t );
// warp texture, no lightmaps
EmitWaterPolys( fa, (cull_type == CULL_BACKSIDE));
return; return;
}
else GL_Bind( XASH_TEXTURE0, t->gl_texturenum );
if( t->fb_texturenum )
{
fa->info->lumachain = fullbright_surfaces[t->fb_texturenum]; fa->info->lumachain = fullbright_surfaces[t->fb_texturenum];
fullbright_surfaces[t->fb_texturenum] = fa->info; fullbright_surfaces[t->fb_texturenum] = fa->info;
draw_fullbrights = true; draw_fullbrights = true;
} }
if( r_detailtextures.value ) static void R_RenderDetailsForSurface( msurface_t *fa, texture_t *t )
{ {
if( !r_detailtextures.value )
return;
if( glState.isFogEnabled ) if( glState.isFogEnabled )
{ {
// don't apply detail textures for windows in the fog // don't apply detail textures for windows in the fog
@ -1199,8 +1177,8 @@ static void R_RenderBrushPoly( msurface_t *fa, int cull_type )
} }
} }
DrawGLPoly( fa->polys, 0.0f, 0.0f ); static void R_RenderDecalsForSurface( msurface_t *fa, int cull_type )
{
if( RI.currententity->curstate.rendermode == kRenderNormal ) if( RI.currententity->curstate.rendermode == kRenderNormal )
{ {
// batch decals to draw later // batch decals to draw later
@ -1212,9 +1190,12 @@ static void R_RenderBrushPoly( msurface_t *fa, int cull_type )
// if rendermode != kRenderNormal draw decals sequentially // if rendermode != kRenderNormal draw decals sequentially
DrawSurfaceDecals( fa, true, (cull_type == CULL_BACKSIDE)); DrawSurfaceDecals( fa, true, (cull_type == CULL_BACKSIDE));
} }
}
if( FBitSet( fa->flags, SURF_DRAWTILED )) static qboolean R_CheckLightMap( msurface_t *fa )
return; // no lightmaps anyway {
qboolean is_dynamic = false;
int maps;
// check for lightmap modification // check for lightmap modification
for( maps = 0; maps < MAXLIGHTMAPS && fa->styles[maps] != 255; maps++ ) for( maps = 0; maps < MAXLIGHTMAPS && fa->styles[maps] != 255; maps++ )
@ -1228,12 +1209,15 @@ static void R_RenderBrushPoly( msurface_t *fa, int cull_type )
{ {
dynamic: dynamic:
// NOTE: at this point we have only valid textures // NOTE: at this point we have only valid textures
if( r_dynamic->value ) is_dynamic = true; if( r_dynamic->value )
is_dynamic = true;
} }
if( is_dynamic ) if( is_dynamic )
{ {
if(( maps < MAXLIGHTMAPS ) && ( fa->styles[maps] >= 32 || fa->styles[maps] == 0 || fa->styles[maps] == 20 ) && ( fa->dlightframe != tr.framecount )) const int style = fa->styles[maps];
if( maps < MAXLIGHTMAPS && ( style >= 32 || style == 0 || style == 20 ) && fa->dlightframe != tr.framecount )
{ {
byte temp[132*132*4]; byte temp[132*132*4];
mextrasurf_t *info = fa->info; mextrasurf_t *info = fa->info;
@ -1244,23 +1228,48 @@ dynamic:
smax = ( info->lightextents[0] / sample_size ) + 1; smax = ( info->lightextents[0] / sample_size ) + 1;
tmax = ( info->lightextents[1] / sample_size ) + 1; tmax = ( info->lightextents[1] / sample_size ) + 1;
if( smax < 132 && tmax < 132 )
R_BuildLightMap( fa, temp, smax * 4, true ); R_BuildLightMap( fa, temp, smax * 4, true );
else
{
smax = Q_min( smax, 132 );
tmax = Q_min( tmax, 132 );
memset( temp, 255, sizeof( temp ));
//Host_MapDesignError( "%s: bad surface extents: %d %d", __func__, fa->extents[0], fa->extents[1] );
}
R_SetCacheState( fa ); R_SetCacheState( fa );
#ifdef XASH_WES
GL_Bind( XASH_TEXTURE1, tr.lightmapTextures[fa->lightmaptexturenum] );
pglTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE );
#else
GL_Bind( XASH_TEXTURE0, tr.lightmapTextures[fa->lightmaptexturenum] ); GL_Bind( XASH_TEXTURE0, tr.lightmapTextures[fa->lightmaptexturenum] );
#endif
pglTexSubImage2D( GL_TEXTURE_2D, 0, fa->light_s, fa->light_t, smax, tmax, pglTexSubImage2D( GL_TEXTURE_2D, 0, fa->light_s, fa->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, temp );
GL_RGBA, GL_UNSIGNED_BYTE, temp );
fa->info->lightmapchain = gl_lms.lightmap_surfaces[fa->lightmaptexturenum]; #ifdef XASH_WES
gl_lms.lightmap_surfaces[fa->lightmaptexturenum] = fa; GL_SelectTexture( XASH_TEXTURE0 );
#endif
} }
else else
return true; // add to dynamic chain
}
return false; // updated
}
static void R_RenderLightmapForSurface( msurface_t *fa )
{
if( !fa->polys || FBitSet( fa->flags, SURF_DRAWTILED ))
return;
if( R_CheckLightMap( fa ))
{ {
fa->info->lightmapchain = gl_lms.dynamic_surfaces; fa->info->lightmapchain = gl_lms.dynamic_surfaces;
gl_lms.dynamic_surfaces = fa; gl_lms.dynamic_surfaces = fa;
} }
}
else else
{ {
fa->info->lightmapchain = gl_lms.lightmap_surfaces[fa->lightmaptexturenum]; fa->info->lightmapchain = gl_lms.lightmap_surfaces[fa->lightmaptexturenum];
@ -1268,6 +1277,39 @@ dynamic:
} }
} }
/*
================
R_RenderBrushPoly
================
*/
static void R_RenderBrushPoly( msurface_t *fa, int cull_type )
{
texture_t *t;
r_stats.c_world_polys++;
if( fa->flags & SURF_DRAWSKY )
return; // already handled
t = R_TextureAnimation( fa );
if( FBitSet( fa->flags, SURF_DRAWTURB ))
{
R_UploadRipples( t );
// warp texture, no lightmaps
EmitWaterPolys( fa, (cull_type == CULL_BACKSIDE));
return;
}
else GL_Bind( XASH_TEXTURE0, t->gl_texturenum );
R_RenderFullbrightForSurface( fa, t );
R_RenderDetailsForSurface( fa, t );
DrawGLPoly( fa->polys, 0.0f, 0.0f );
R_RenderDecalsForSurface( fa, cull_type );
R_RenderLightmapForSurface( fa );
}
/* /*
================ ================
R_DrawTextureChains R_DrawTextureChains
@ -3115,88 +3157,21 @@ void R_DrawVBO( qboolean drawlightmap, qboolean drawtextures )
vbos.maxtexture = 0; vbos.maxtexture = 0;
} }
/*
================
R_CheckLightMap
update surface's lightmap if needed and return true if it is dynamic
================
*/
static qboolean R_CheckLightMap( msurface_t *fa )
{
int maps;
qboolean is_dynamic = false;
// check for lightmap modification
for( maps = 0; maps < MAXLIGHTMAPS && fa->styles[maps] != 255; maps++ )
{
if( tr.lightstylevalue[fa->styles[maps]] != fa->cached_light[maps] )
{
is_dynamic = true;
break;
}
}
// already up to date
if( !is_dynamic && ( fa->dlightframe != tr.framecount ))
return false;
// build lightmap
if(( maps < MAXLIGHTMAPS ) && ( fa->styles[maps] >= 32 || fa->styles[maps] == 0 ) && ( fa->dlightframe != tr.framecount ))
{
byte temp[132*132*4];
int smax, tmax;
int sample_size;
mextrasurf_t *info;
info = fa->info;
sample_size = gEngfuncs.Mod_SampleSizeForFace( fa );
smax = ( info->lightextents[0] / sample_size ) + 1;
tmax = ( info->lightextents[1] / sample_size ) + 1;
if( smax < 132 && tmax < 132 )
{
R_BuildLightMap( fa, temp, smax * 4, true );
}
else
{
smax = Q_min( smax, 132 );
tmax = Q_min( tmax, 132 );
//Host_MapDesignError( "R_RenderBrushPoly: bad surface extents: %d %d", fa->extents[0], fa->extents[1] );
memset( temp, 255, sizeof( temp ) );
}
R_SetCacheState( fa );
#ifdef XASH_WES
GL_Bind( XASH_TEXTURE1, tr.lightmapTextures[fa->lightmaptexturenum] );
pglTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE );
#else
GL_Bind( XASH_TEXTURE0, tr.lightmapTextures[fa->lightmaptexturenum] );
#endif
pglTexSubImage2D( GL_TEXTURE_2D, 0, fa->light_s, fa->light_t, smax, tmax,
GL_RGBA, GL_UNSIGNED_BYTE, temp );
#ifdef XASH_WES
GL_SelectTexture( XASH_TEXTURE0 );
#endif
}
// add to dynamic chain
else
return true;
// updated
return false;
}
qboolean R_AddSurfToVBO( msurface_t *surf, qboolean buildlightmap ) qboolean R_AddSurfToVBO( msurface_t *surf, qboolean buildlightmap )
{ {
if( r_vbo.value && vbos.surfdata[surf - WORLDMODEL->surfaces].vbotexture ) const int idx = surf - WORLDMODEL->surfaces;
{ vbotexture_t *vbotex;
int texturenum;
if( !r_vbo.value )
return false;
// find vbotexture_t assotiated with this surface // find vbotexture_t assotiated with this surface
int idx = surf - WORLDMODEL->surfaces; vbotex = vbos.surfdata[idx].vbotexture;
vbotexture_t *vbotex = vbos.surfdata[idx].vbotexture; texturenum = vbos.surfdata[idx].texturenum;
int texturenum = vbos.surfdata[idx].texturenum;
if( !vbotex )
return false;
if( !surf->polys ) if( !surf->polys )
return true; return true;
@ -3212,40 +3187,9 @@ qboolean R_AddSurfToVBO( msurface_t *surf, qboolean buildlightmap )
buildlightmap &= !r_fullbright->value && !!WORLDMODEL->lightdata; buildlightmap &= !r_fullbright->value && !!WORLDMODEL->lightdata;
/* draw details in regular way */ // draw details in regular way
if( r_vbo_detail.value == 0 ) if( r_vbo_detail.value == 0 && surf->texinfo )
{ R_RenderDetailsForSurface( surf, surf->texinfo->texture );
if( r_detailtextures.value && surf->texinfo && surf->texinfo )
{
texture_t *t = surf->texinfo->texture;
if( glState.isFogEnabled )
{
// don't apply detail textures for windows in the fog
if( RI.currententity->curstate.rendermode != kRenderTransTexture )
{
if( t->dt_texturenum )
{
surf->info->detailchain = detail_surfaces[t->dt_texturenum];
detail_surfaces[t->dt_texturenum] = surf->info;
}
else
{
// draw stub detail texture for underwater surfaces
surf->info->detailchain = detail_surfaces[tr.grayTexture];
detail_surfaces[tr.grayTexture] = surf->info;
}
draw_details = true;
}
}
else if( t->dt_texturenum )
{
surf->info->detailchain = detail_surfaces[t->dt_texturenum];
detail_surfaces[t->dt_texturenum] = surf->info;
draw_details = true;
}
}
}
if( buildlightmap && R_CheckLightMap( surf )) if( buildlightmap && R_CheckLightMap( surf ))
{ {
@ -3279,8 +3223,6 @@ qboolean R_AddSurfToVBO( msurface_t *surf, qboolean buildlightmap )
return true; return true;
} }
return false;
}
/* /*
============================================================= =============================================================