Skip to content

Commit

Permalink
vk: allow dynamic surface/material patching
Browse files Browse the repository at this point in the history
Refactor NewMap and patch loading a bit.
  • Loading branch information
w23 committed Apr 13, 2023
1 parent 90591cf commit 77f9594
Showing 1 changed file with 79 additions and 34 deletions.
113 changes: 79 additions & 34 deletions ref/vk/vk_scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,93 @@ static void loadLights( const model_t *const map ) {
RT_LightsLoadEnd();
}

// Clears all old map data
static void mapLoadBegin( const model_t *const map ) {
// TODO should we do something like VK_BrushBeginLoad?
VK_BrushStatsClear();

R_GeometryBuffer_MapClear();

VK_ClearLightmap();

// This is to ensure that we have computed lightstyles properly
VK_RunLightStyles();

if (vk_core.rtx)
VK_RayNewMap();

RT_LightsNewMap(map);
}

static void mapLoadEnd(const model_t *const map) {
// TODO should we do something like VK_BrushEndLoad?
VK_UploadLightmap();
}

static void loadBrushModels( void ) {
const int num_models = gEngine.EngineGetParm( PARM_NUMMODELS, 0 );

// Load all models at once
gEngine.Con_Reportf( "Num models: %d:\n", num_models );
for( int i = 0; i < num_models; i++ )
{
model_t *m;
if(( m = gEngine.pfnGetModelByIndex( i + 1 )) == NULL )
continue;

gEngine.Con_Reportf( " %d: name=%s, type=%d, submodels=%d, nodes=%d, surfaces=%d, nummodelsurfaces=%d\n", i, m->name, m->type, m->numsubmodels, m->numnodes, m->numsurfaces, m->nummodelsurfaces);

if( m->type != mod_brush )
continue;

if (!VK_BrushModelLoad(m))
gEngine.Host_Error( "Couldn't load model %s\n", m->name );
}
}

// Only used when reloading patches. In norma circumstances models get destroyed by the engine
static void destroyBrushModels( void ) {
const int num_models = gEngine.EngineGetParm( PARM_NUMMODELS, 0 );
gEngine.Con_Printf("Destroying %d models\n", num_models);

for( int i = 0; i < num_models; i++ ) {
model_t *m;
if(( m = gEngine.pfnGetModelByIndex( i + 1 )) == NULL )
continue;

if( m->type != mod_brush )
continue;

VK_BrushModelDestroy(m);
}
}

static void reloadMaterials( void ) {
gEngine.Con_Printf("Reloading materials\n");

R_VkStagingFlushSync();

XVK_CHECK(vkDeviceWaitIdle( vk_core.device ));

destroyBrushModels();

const model_t *const map = gEngine.pfnGetModelByIndex( 1 );
mapLoadBegin(map);

// Materials do affect patching, as new materials can be referenced in patch data
// So we must do the full sequence
XVK_ParseMapEntities();
XVK_ReloadMaterials();
XVK_ParseMapPatches();

// Assumes that the map has been loaded
loadBrushModels();

// Might have loaded new patch data, need to reload lighting data just in case
const model_t *const map = gEngine.pfnGetModelByIndex( 1 );
loadLights(map);
mapLoadEnd(map);

R_VkStagingFlushSync();
}

// Same as the above, but avoids reloading heavy materials data
Expand Down Expand Up @@ -165,7 +240,6 @@ int R_FIXME_GetEntityRenderMode( cl_entity_t *ent )

// tell the renderer what new map is started
void R_NewMap( void ) {
const int num_models = gEngine.EngineGetParm( PARM_NUMMODELS, 0 );
const model_t *const map = gEngine.pfnGetModelByIndex( 1 );

// Existence of cache.data for the world means that we've already have loaded this map
Expand All @@ -178,20 +252,9 @@ void R_NewMap( void ) {
if (is_save_load)
return;

// TODO should we do something like VK_BrushBeginLoad?
VK_BrushStatsClear();

R_GeometryBuffer_MapClear();

VK_ClearLightmap();

// This is to ensure that we have computed lightstyles properly
VK_RunLightStyles();

XVK_SetupSky( gEngine.pfnGetMoveVars()->skyName );

if (vk_core.rtx)
VK_RayNewMap();
mapLoadBegin(map);

// Load light entities and patch data prior to loading map brush model
XVK_ParseMapEntities();
Expand All @@ -203,28 +266,10 @@ void R_NewMap( void ) {
// Depends on loaded materials. Must preceed loading brush models.
XVK_ParseMapPatches();

// Load all models at once
gEngine.Con_Reportf( "Num models: %d:\n", num_models );
for( int i = 0; i < num_models; i++ )
{
model_t *m;
if(( m = gEngine.pfnGetModelByIndex( i + 1 )) == NULL )
continue;

gEngine.Con_Reportf( " %d: name=%s, type=%d, submodels=%d, nodes=%d, surfaces=%d, nummodelsurfaces=%d\n", i, m->name, m->type, m->numsubmodels, m->numnodes, m->numsurfaces, m->nummodelsurfaces);
loadBrushModels();

if( m->type != mod_brush )
continue;

if (!VK_BrushModelLoad(m))
gEngine.Host_Error( "Couldn't load model %s\n", m->name );
}

RT_LightsNewMap(map);
loadLights(map);

// TODO should we do something like VK_BrushEndLoad?
VK_UploadLightmap();
mapLoadEnd(map);
}

qboolean R_AddEntity( struct cl_entity_s *clent, int type )
Expand Down

0 comments on commit 77f9594

Please sign in to comment.