-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
369 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,6 @@ | ||
using Titan.Core; | ||
using Titan.Core.Threading2; | ||
|
||
namespace Titan.Assets.NewAssets; | ||
|
||
public struct Asset | ||
{ | ||
public AssetState State; | ||
|
||
public Handle<JobApi> JobHandle; | ||
public Handle AssetHandle; | ||
public int ReferenceCount; | ||
|
||
public AssetDescriptor Descriptor; | ||
} | ||
/// <summary> | ||
/// Struct used for the asset handle Handle<Asset> | ||
/// </summary> | ||
public struct Asset{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Titan.Core; | ||
using Titan.Core.Threading2; | ||
using Titan.FileSystem; | ||
|
||
namespace Titan.Assets.NewAssets; | ||
|
||
/// <summary> | ||
/// Struct that contains all information needed for assets. | ||
/// NOTE(Jens): this will consume a bit of memory depending on the amount of assets in the game. Might be fine, but good to keep an eye on. | ||
/// </summary> | ||
internal unsafe struct AssetContext | ||
{ | ||
|
||
public AssetState State; | ||
public Handle<JobApi> JobHandle; | ||
public Handle AssetHandle; | ||
public volatile int ReferenceCount; | ||
|
||
//File loading | ||
public void* FileBuffer; | ||
public FileSystemApi* FileSystemApi; | ||
public FileHandle FileHandle; | ||
|
||
|
||
public AssetDescriptor Descriptor; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using Titan.Core.Logging; | ||
using Titan.FileSystem; | ||
using Titan.Memory; | ||
|
||
namespace Titan.Assets.NewAssets; | ||
|
||
internal unsafe struct AssetFileAccessor | ||
{ | ||
private ManifestToFile* _files; | ||
private int _count; | ||
|
||
private PlatformAllocator* _allocator; | ||
public static bool Create(PlatformAllocator* allocator, FileSystemApi* fileSystem, AssetsConfiguration[] configs, out AssetFileAccessor accessor) | ||
{ | ||
accessor = default; | ||
var files = allocator->Allocate<ManifestToFile>(configs.Length); | ||
if (files == null) | ||
{ | ||
Logger.Error<AssetFileAccessor>($"Failed to allocate a block for {nameof(ManifestToFile)} with size {sizeof(ManifestToFile) * configs.Length} bytes"); | ||
return false; | ||
} | ||
|
||
for (var i = 0; i < configs.Length; ++i) | ||
{ | ||
ref var fileRef = ref files[i]; | ||
fileRef.Id = configs[i].Id; | ||
if (!AssetFile.Open(configs[i].TitanPakFile, fileSystem, out fileRef.File)) | ||
{ | ||
Logger.Error<AssetFileAccessor>($"Failed to open file {configs[i].TitanPakFile}"); | ||
goto Error; | ||
} | ||
Logger.Trace<AssetFileAccessor>($"Opened file {configs[i].TitanPakFile} with size {files[i].File.GetLength()} bytes"); | ||
} | ||
accessor = new AssetFileAccessor | ||
{ | ||
_allocator = allocator, | ||
_files = files, | ||
_count = configs.Length | ||
}; | ||
return true; | ||
Error: | ||
|
||
//NOTE(Jens): if there's an error, close all open file handles. | ||
for (var i = 0; i < configs.Length; ++i) | ||
{ | ||
files[i].File.Close(); | ||
} | ||
return false; | ||
} | ||
|
||
|
||
public int Read(Span<byte> buffer, in AssetDescriptor descriptor) | ||
{ | ||
Debug.Assert(buffer.Length >= (int)descriptor.Reference.Size, $"Buffer size is {buffer.Length} bytes, minimum required is {descriptor.Reference.Size} bytes."); | ||
|
||
var file = GetFileForManifest(descriptor.ManifestId); | ||
Debug.Assert(file != null, $"TitanPak file with ID {descriptor.ManifestId} was not found."); | ||
|
||
return file->Read(buffer[..(int)descriptor.Reference.Size], descriptor.Reference.Offset); | ||
} | ||
|
||
private AssetFile* GetFileForManifest(uint manifestId) | ||
{ | ||
for (var i = 0; i < _count; ++i) | ||
{ | ||
if (_files[i].Id == manifestId) | ||
{ | ||
return &_files[i].File; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
|
||
public void Release() | ||
{ | ||
for (var i = 0; i < _count; ++i) | ||
{ | ||
_files[i].File.Close(); | ||
} | ||
_allocator->Free(_files); | ||
_files = null; | ||
_count = 0; | ||
} | ||
|
||
private struct ManifestToFile | ||
{ | ||
public uint Id; | ||
public AssetFile File; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.