Skip to content

Commit

Permalink
For now, this is just a big mess
Browse files Browse the repository at this point in the history
  • Loading branch information
gotmachine committed May 19, 2024
1 parent 441f2d7 commit 40bb7fc
Show file tree
Hide file tree
Showing 4 changed files with 536 additions and 36 deletions.
3 changes: 3 additions & 0 deletions GameData/KSPCommunityFixes/Settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

KSP_COMMUNITY_FIXES
{
OnDemandPartTextures = true


// ##########################
// Major bugfixes
// ##########################
Expand Down
1 change: 1 addition & 0 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
<Compile Include="Modding\ReflectionTypeLoadExceptionHandler.cs" />
<Compile Include="Performance\FewerSaves.cs" />
<Compile Include="Performance\ConfigNodePerf.cs" />
<Compile Include="Performance\OnDemandPartTextures.cs" />
<Compile Include="Performance\OptimizedModuleRaycasts.cs" />
<Compile Include="Performance\PQSCoroutineLeak.cs" />
<Compile Include="Performance\PQSUpdateNoMemoryAlloc.cs" />
Expand Down
98 changes: 62 additions & 36 deletions KSPCommunityFixes/Performance/FastLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ static IEnumerator FastAssetLoader(List<ConfigFileType> configFileTypes)
gdb.progressTitle = "Searching assets to load...";
yield return null;

OnDemandPartTextures.GetTextures(out HashSet<string> allPartTextures);

double nextFrameTime = ElapsedTime + minFrameTimeD;

// Files loaded by our custom loaders
Expand Down Expand Up @@ -350,26 +352,29 @@ static IEnumerator FastAssetLoader(List<ConfigFileType> configFileTypes)
}
break;
case FileType.Texture:

bool isOnDemand = allPartTextures.Contains(file.url);

switch (file.fileExtension)
{
case "dds":
textureAssets.Add(new RawAsset(file, RawAsset.AssetType.TextureDDS));
textureAssets.Add(new RawAsset(file, RawAsset.AssetType.TextureDDS, isOnDemand));
break;
case "jpg":
case "jpeg":
textureAssets.Add(new RawAsset(file, RawAsset.AssetType.TextureJPG));
textureAssets.Add(new RawAsset(file, RawAsset.AssetType.TextureJPG, isOnDemand));
break;
case "mbm":
textureAssets.Add(new RawAsset(file, RawAsset.AssetType.TextureMBM));
textureAssets.Add(new RawAsset(file, RawAsset.AssetType.TextureMBM, isOnDemand));
break;
case "png":
textureAssets.Add(new RawAsset(file, RawAsset.AssetType.TexturePNG));
textureAssets.Add(new RawAsset(file, RawAsset.AssetType.TexturePNG, isOnDemand));
break;
case "tga":
textureAssets.Add(new RawAsset(file, RawAsset.AssetType.TextureTGA));
textureAssets.Add(new RawAsset(file, RawAsset.AssetType.TextureTGA, isOnDemand));
break;
case "truecolor":
textureAssets.Add(new RawAsset(file, RawAsset.AssetType.TextureTRUECOLOR));
textureAssets.Add(new RawAsset(file, RawAsset.AssetType.TextureTRUECOLOR, isOnDemand));
break;
default:
unsupportedTextureFiles.Add(file);
Expand Down Expand Up @@ -802,6 +807,12 @@ static void ReadAssetsThread(List<RawAsset> files, Deque<RawAsset> buffer)
{
foreach (RawAsset rawAsset in files)
{
if (rawAsset.IsOnDemand)
{
buffer.AddToFront(rawAsset);
continue;
}

rawAsset.ReadFromDiskWorkerThread();

SpinWait spin = new SpinWait();
Expand Down Expand Up @@ -836,7 +847,7 @@ static void ReadAssetsThread(List<RawAsset> files, Deque<RawAsset> buffer)
/// <summary>
/// Asset wrapper class, actual implementation of the disk reader, individual texture/model formats loaders
/// </summary>
private class RawAsset
internal class RawAsset
{
public enum AssetType
{
Expand Down Expand Up @@ -881,18 +892,21 @@ public enum Result
private BinaryReader binaryReader;
private Result result;
private string resultMessage;
private bool isOnDemand;

public UrlFile File => file;
public Result State => result;
public string Message => resultMessage;
public int DataLength => dataLength;
public string TypeName => assetTypeNames[(int)assetType];
public bool IsOnDemand => isOnDemand;

public RawAsset(UrlFile file, AssetType assetType)
public RawAsset(UrlFile file, AssetType assetType, bool isOnDemand = false)
{
this.result = Result.Valid;
this.file = file;
this.assetType = assetType;
this.isOnDemand = isOnDemand;
}

private void SetError(string message)
Expand Down Expand Up @@ -1002,44 +1016,56 @@ public void LoadAndDisposeMainThread()
if (file.fileType == FileType.Texture)
{
TextureInfo textureInfo;
switch (assetType)

if (isOnDemand)
{
case AssetType.TextureDDS:
textureInfo = LoadDDS();
break;
case AssetType.TextureJPG:
textureInfo = LoadJPG();
break;
case AssetType.TextureMBM:
textureInfo = LoadMBM();
break;
case AssetType.TexturePNG:
textureInfo = LoadPNG();
break;
case AssetType.TexturePNGCached:
textureInfo = LoadPNGCached();
break;
case AssetType.TextureTGA:
textureInfo = LoadTGA();
break;
case AssetType.TextureTRUECOLOR:
textureInfo = LoadTRUECOLOR();
break;
default:
SetError("Unknown texture format");
return;
textureInfo = new OnDemandTextureInfo(file, assetType);
}
else
{
switch (assetType)
{
case AssetType.TextureDDS:
textureInfo = LoadDDS();
break;
case AssetType.TextureJPG:
textureInfo = LoadJPG();
break;
case AssetType.TextureMBM:
textureInfo = LoadMBM();
break;
case AssetType.TexturePNG:
textureInfo = LoadPNG();
break;
case AssetType.TexturePNGCached:
textureInfo = LoadPNGCached();
break;
case AssetType.TextureTGA:
textureInfo = LoadTGA();
break;
case AssetType.TextureTRUECOLOR:
textureInfo = LoadTRUECOLOR();
break;
default:
SetError("Unknown texture format");
return;
}

if (textureInfo.texture.IsNullOrDestroyed())
result = Result.Failed;

if (result == Result.Failed || textureInfo == null || textureInfo.texture.IsNullOrDestroyed())
textureInfo.texture.name = file.url;
textureInfo.name = file.url;
}

if (result == Result.Failed || textureInfo == null)
{
result = Result.Failed;
if (string.IsNullOrEmpty(resultMessage))
resultMessage = $"{TypeName} load error";
}
else
{
textureInfo.name = file.url;
textureInfo.texture.name = file.url;
Instance.databaseTexture.Add(textureInfo);
}
}
Expand Down
Loading

0 comments on commit 40bb7fc

Please sign in to comment.