Skip to content

Commit

Permalink
Implement resource stock for caching model configs.
Browse files Browse the repository at this point in the history
- Add "MISF" chunk from Serious Engine 1.50 to model instance serialization.

#10
  • Loading branch information
DreamyCecil committed Jan 15, 2025
1 parent 5fa4a67 commit bc59f51
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 44 deletions.
49 changes: 38 additions & 11 deletions Sources/Engine/Base/ReplaceFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <Engine/Templates/Stock_CSkeleton.h>
#include <Engine/Templates/Stock_CAnimSet.h>
#include <Engine/Templates/Stock_CTextureData.h>
#include <Engine/Templates/Stock_CModelConfig.h> // [Cecil]
#include <Engine/Templates/DynamicContainer.cpp>
//#include <Engine/Templates/Stock_CShader.h>

#include <Engine/Base/ListIterator.inl>

Expand Down Expand Up @@ -475,13 +475,20 @@ void WriteOffsetAndChildren(CTStream &strm, CModelInstance &mi)
for(INDEX icmi=0;icmi<ctcmi;icmi++) {
CModelInstance &cmi = mi.mi_cmiChildren[icmi];
// save child model instance
WriteModelInstance_t(strm, cmi);
WriteModelInstance_t(strm, cmi, FALSE); // [Cecil] Don't bother caching as they are a part of the main model
}
}

void WriteModelInstance_t(CTStream &strm, CModelInstance &mi)
void WriteModelInstance_t(CTStream &strm, CModelInstance &mi, BOOL bFromStock)
{
strm.WriteID_t("MI03"); // model instance 03

// [Cecil] Write "Model Instance Source File" if this model has been loaded from the stock
if (bFromStock && mi.mi_pInStock != NULL) {
strm.WriteID_t("MISF");
strm.WriteFileName(mi.mi_pInStock->ser_FileName);
}

// write model instance name
strm<<mi.GetName();
// write index of current colision box
Expand Down Expand Up @@ -630,7 +637,7 @@ void ReadModelInstanceOld_t(CTStream &strm, CModelInstance &mi)
// add as child to parent model isntance
mi.mi_cmiChildren.Add(pcmi);
// read child model instance
ReadModelInstance_t(strm, *pcmi);
ReadModelInstance_t(strm, *pcmi, FALSE); // [Cecil] Configs of child models aren't cached
}
}

Expand Down Expand Up @@ -785,13 +792,32 @@ void ReadOffsetAndChildren_t(CTStream &strm, CModelInstance &mi)
// add as child to parent model isntance
mi.mi_cmiChildren.Add(pcmi);
// read child model instance
ReadModelInstance_t(strm, *pcmi);
ReadModelInstance_t(strm, *pcmi, FALSE); // [Cecil] Configs of child models aren't cached
}
}

void ReadModelInstanceNew_t(CTStream &strm, CModelInstance &mi)
// [Cecil] Stock flag
void ReadModelInstanceNew_t(CTStream &strm, CModelInstance &mi, BOOL bMarkInStock)
{
strm.ExpectID_t("MI03"); // model instance 02
strm.ExpectID_t("MI03"); // model instance 03

// [Cecil] Read optional "Model Instance Source File" chunk
if (strm.PeekID_t() == CChunkID("MISF")) {
strm.ExpectID_t("MISF");

CTString fnmSourceFile;
strm.ReadFileName(fnmSourceFile);

ASSERT(fnmSourceFile != "");
ASSERT(mi.mi_pInStock == NULL);

// Retrieve it from the stock, if needed
if (bMarkInStock) {
mi.mi_pInStock = _pModelConfigStock->Obtain_t(fnmSourceFile);
ASSERT(mi.mi_pInStock != NULL);
}
}

// Read model instance name
CTString strModelInstanceName;
strm>>strModelInstanceName;
Expand All @@ -810,17 +836,17 @@ void ReadModelInstanceNew_t(CTStream &strm, CModelInstance &mi)
ReadAnimQueue_t(strm, mi);
ReadColisionBoxes_t(strm, mi);
ReadOffsetAndChildren_t(strm, mi);
strm.ExpectID_t("ME03"); // model instance end 02
strm.ExpectID_t("ME03"); // model instance end 03
}

void ReadModelInstance_t(CTStream &strm, CModelInstance &mi)
void ReadModelInstance_t(CTStream &strm, CModelInstance &mi, BOOL bMarkInStock)
{
// is model instance writen in old format
if(strm.PeekID_t() == CChunkID("SKMI")) {
ReadModelInstanceOld_t(strm, mi);
// is model instance writen in new format
} else if(strm.PeekID_t() == CChunkID("MI03")) {
ReadModelInstanceNew_t(strm, mi);
ReadModelInstanceNew_t(strm, mi, bMarkInStock); // [Cecil] Pass flag
// unknown format
} else {
strm.Throw_t("Unknown model instance format");
Expand All @@ -830,8 +856,9 @@ void ReadModelInstance_t(CTStream &strm, CModelInstance &mi)

void SkipModelInstance_t(CTStream &strm)
{
// [Cecil] No need to reference it in the stock if skipping
CModelInstance miDummy;
ReadModelInstance_t(strm,miDummy);
ReadModelInstance_t(strm, miDummy, FALSE);
}

// read an anim object from a file together with its anim data filename
Expand Down
4 changes: 2 additions & 2 deletions Sources/Engine/Base/ReplaceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ void SkipModelObject_t(CTStream &strm);
void WriteModelObject_t(CTStream &strm, CModelObject &mo);

// read/write a ska model from a file
void WriteModelInstance_t(CTStream &strm, CModelInstance &mi);
void ReadModelInstance_t(CTStream &strm, CModelInstance &mi);
void WriteModelInstance_t(CTStream &strm, CModelInstance &mi, BOOL bFromStock = TRUE); // [Cecil] Stock flag
void ReadModelInstance_t(CTStream &strm, CModelInstance &mi, BOOL bMarkInStock = TRUE); // [Cecil] Stock flag
void SkipModelInstance_t(CTStream &strm);


Expand Down
3 changes: 3 additions & 0 deletions Sources/Engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <Engine/Templates/Stock_CSkeleton.h>
#include <Engine/Templates/Stock_CAnimSet.h>
#include <Engine/Templates/Stock_CShader.h>
#include <Engine/Templates/Stock_CModelConfig.h> // [Cecil]
#include <Engine/Templates/StaticArray.cpp>
#include <Engine/Base/IFeel.h>

Expand Down Expand Up @@ -286,6 +287,7 @@ void SE_InitEngine(const SeriousEngineSetup &engineSetup) {
_pSkeletonStock = new CStock_CSkeleton;
_pAnimSetStock = new CStock_CAnimSet;
_pShaderStock = new CStock_CShader;
_pModelConfigStock = new CStock_CModelConfig; // [Cecil]

// init main shell
_pShell = new CShell;
Expand Down Expand Up @@ -558,6 +560,7 @@ void SE_EndEngine(void)
delete _pSkeletonStock; _pSkeletonStock = NULL;
delete _pAnimSetStock; _pAnimSetStock = NULL;
delete _pShaderStock; _pShaderStock = NULL;
delete _pModelConfigStock; _pModelConfigStock = NULL; // [Cecil]

// free all memory used by the crc cache
CRCT_Clear();
Expand Down
3 changes: 3 additions & 0 deletions Sources/Engine/Engine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ del "$(ProjectDir)Ska\smcScan.cpp" /q</Command>
<ClCompile Include="Query\MasterServer.cpp" />
<ClCompile Include="Query\QueryManager.cpp" />
<ClCompile Include="Query\ServerRequest.cpp" />
<ClCompile Include="Ska\ModelConfig.cpp" />
<ClCompile Include="Sound\SoundAPI.cpp" />
<ClCompile Include="Sound\SoundAPI_DSound.cpp" />
<ClCompile Include="Sound\SoundAPI_SDL.cpp" />
Expand Down Expand Up @@ -1141,6 +1142,7 @@ del "$(ProjectDir)Ska\smcScan.cpp" /q</Command>
<ClInclude Include="Query\QueryManager.h" />
<ClInclude Include="Query\ServerRequest.h" />
<ClInclude Include="SE_Config.h" />
<ClInclude Include="Ska\ModelConfig.h" />
<ClInclude Include="Sound\al_functions.h" />
<ClInclude Include="Sound\DSound.h" />
<ClInclude Include="Sound\eax.h" />
Expand Down Expand Up @@ -1183,6 +1185,7 @@ del "$(ProjectDir)Ska\smcScan.cpp" /q</Command>
<ClInclude Include="Templates\Stock_CAnimSet.h" />
<ClInclude Include="Templates\Stock_CEntityClass.h" />
<ClInclude Include="Templates\Stock_CMesh.h" />
<ClInclude Include="Templates\Stock_CModelConfig.h" />
<ClInclude Include="Templates\Stock_CModelData.h" />
<ClInclude Include="Templates\Stock_CShader.h" />
<ClInclude Include="Templates\Stock_CSkeleton.h" />
Expand Down
9 changes: 9 additions & 0 deletions Sources/Engine/Engine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,9 @@
<ClCompile Include="Base\InputJoystick.cpp">
<Filter>Source Files\Base</Filter>
</ClCompile>
<ClCompile Include="Ska\ModelConfig.cpp">
<Filter>Source Files\Ska</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Base\Anim.h">
Expand Down Expand Up @@ -1383,6 +1386,12 @@
<ClInclude Include="OS\Keycodes.h">
<Filter>Header Files\OS Headers</Filter>
</ClInclude>
<ClInclude Include="Ska\ModelConfig.h">
<Filter>Header Files\Ska Headers</Filter>
</ClInclude>
<ClInclude Include="Templates\Stock_CModelConfig.h">
<Filter>Header Files\Templates Headers</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="Base\CTString.inl">
Expand Down
61 changes: 32 additions & 29 deletions Sources/Engine/Entities/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <Engine/Sound/SoundObject.h>
#include <Engine/Graphics/Texture.h>
#include <Engine/Ska/Render.h>
#include <Engine/Ska/ModelConfig.h> // [Cecil]
#include <Engine/Terrain/Terrain.h>
#include <Engine/Terrain/TerrainRayCasting.h>
#include <Engine/Terrain/TerrainMisc.h>
Expand Down Expand Up @@ -2413,47 +2414,49 @@ void CEntity::SetSkaColisionInfo()

void CEntity::SetSkaModel_t(const CTString &fnmModel)
{
ASSERT(en_RenderType==RT_SKAMODEL || en_RenderType==RT_SKAEDITORMODEL);
// if model instance allready exists
if(en_pmiModelInstance!=NULL) {
// release it first
en_pmiModelInstance->Clear();
}
try {
// load the new model data
en_pmiModelInstance = LoadModelInstance_t(fnmModel);
} catch (char *strErrorDefault) {
throw(strErrorDefault);
ASSERT(en_RenderType == RT_SKAMODEL || en_RenderType == RT_SKAEDITORMODEL);

// [Cecil] Create new model instance for the entity, if there's none
if (en_pmiModelInstance == NULL) {
en_pmiModelInstance = CreateModelInstance("");
}

// [Cecil] Obtain a copy of the desired model from the stock
ASSERT(en_pmiModelInstance != NULL);
ObtainModelInstance_t(en_pmiModelInstance, fnmModel);

SetSkaColisionInfo();
}
};

BOOL CEntity::SetSkaModel(const CTString &fnmModel)
{
ASSERT(en_RenderType==RT_SKAMODEL || en_RenderType==RT_SKAEDITORMODEL);
// try to
ASSERT(en_RenderType == RT_SKAMODEL || en_RenderType == RT_SKAEDITORMODEL);

// Try loading the SKA model from a file
try {
SetSkaModel_t(fnmModel);
// if failed
} catch(char *strError) {
(void)strError;
return TRUE;

// If failed
} catch (char *strError) {
WarningMessage("%s\n\rLoading default model.\n", strError);
DECLARE_CTFILENAME(fnmDefault, "Models\\Editor\\Ska\\Axis.smc");
// try to
DECLARE_CTFILENAME(fnmDefault, "Models\\Editor\\Ska\\Axis.smc");

// [Cecil] Try loading the default model from a file
try {
// load the default model data
en_pmiModelInstance = LoadModelInstance_t(fnmDefault);
// if failed
} catch(char *strErrorDefault) {
SetSkaModel_t(fnmDefault);

// Display an error If that failed too
} catch (char *strErrorDefault) {
FatalError(TRANS("Cannot load default model '%s':\n%s"), fnmDefault.ConstData(), strErrorDefault);
}
// set colision info for default model
SetSkaColisionInfo();
return FALSE;
}
return TRUE;
}
// set/get model main blend color

// Couldn't load the desired model
return FALSE;
};

// set/get model main blend color
void CEntity::SetModelColor( const COLOR colBlend)
{
ASSERT(en_RenderType==RT_MODEL || en_RenderType==RT_EDITORMODEL);
Expand Down
8 changes: 7 additions & 1 deletion Sources/Engine/Network/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <Engine/Templates/Stock_CMesh.h>
#include <Engine/Templates/Stock_CShader.h>
#include <Engine/Templates/Stock_CSkeleton.h>
#include <Engine/Templates/Stock_CModelConfig.h> // [Cecil]

#include <Engine/Query/MasterServer.h> // [Cecil]

Expand Down Expand Up @@ -578,6 +579,7 @@ static void StockInfo(void)
const FLOAT fAstBytes = dToMB * _pAnimSetStock->CalculateUsedMemory();
const FLOAT fShaBytes = dToMB * _pShaderStock->CalculateUsedMemory();
const FLOAT fSkaBytes = dToMB * _pSkeletonStock->CalculateUsedMemory();
const FLOAT fCfgBytes = dToMB * _pModelConfigStock->CalculateUsedMemory(); // [Cecil]

CPrintF("\nStock information:\n");
CPrintF(" Textures: %5d (%5.2f MB)\n", _pTextureStock->GetTotalCount(), fTexBytes);
Expand All @@ -597,10 +599,11 @@ static void StockInfo(void)
CPrintF(" Skeletons: %5d (%5.2f MB)\n", _pSkeletonStock->GetTotalCount(), fSkaBytes);
CPrintF(" AnimSets: %5d (%5.2f MB)\n", _pAnimSetStock->GetTotalCount(), fAstBytes);
CPrintF(" Shaders: %5d (%5.2f MB)\n", _pShaderStock->GetTotalCount(), fShaBytes);
CPrintF(" ModelConfigs: %5d (%5.2f MB)\n", _pModelConfigStock->GetTotalCount(), fCfgBytes); // [Cecil]
CPrintF("\n");
CPrintF("CollisionGrid: %.2f MB\n", slCgrBytes*dToMB);
CPrintF("--------------\n");
CPrintF(" Total: %.2f MB\n", fTexBytes+fSndBytes+fMdlBytes+fMshBytes+fSkaBytes+fAstBytes+fShaBytes
CPrintF(" Total: %.2f MB\n", fTexBytes+fSndBytes+fMdlBytes+fMshBytes+fSkaBytes+fAstBytes+fShaBytes+fCfgBytes
+ (slShdBytes+slEntBytes+slSecBytes+slPlnBytes+slEdgBytes+slPlyBytes+slVtxBytes+slLyrBytes+slCgrBytes)*dToMB);
CPrintF("\n");
}
Expand Down Expand Up @@ -632,6 +635,8 @@ static void StockDump(void)
_pShaderStock->DumpMemoryUsage_t(strm);
strm.PutLine_t("Skeletons:");
_pSkeletonStock->DumpMemoryUsage_t(strm);
strm.PutLine_t("Model Configs:"); // [Cecil]
_pModelConfigStock->DumpMemoryUsage_t(strm);

CPrintF("Dumped to '%s'\n", fnm.ConstData());
} catch (char *strError) {
Expand All @@ -655,6 +660,7 @@ static void FreeUnusedStock(void)
_pMeshStock->FreeUnused();
_pShaderStock->FreeUnused();
_pSkeletonStock->FreeUnused();
_pModelConfigStock->FreeUnused(); // [Cecil]
}


Expand Down
Loading

0 comments on commit bc59f51

Please sign in to comment.