AssetMeta MetaSerializer::LoadOrCreateMeta(const std::filesystem::path& assetPath)
{
AssetMeta meta;
if (MetaFileExists(assetPath)) meta = LoadMeta(assetPath);
else meta = CreateMeta(assetPath);
std::filesystem::path path = PathManager::GetArtifactsPath();
std::string guidStr = meta.guid.ToString();
path /= guidStr.substr(0, 2);
path /= guidStr;
meta.exportedPath = path;
return meta;
}
class IAssetType
{
public:
virtual ImportedData Import(const AssetMeta& file) = 0;
virtual ProcessedData Process(const ImportedData& file) { return file; }
virtual void Serialize(BinaryWriter& writer, const ProcessedData& data) = 0;
virtual void Load(BinaryReader& reader, const AssetMeta& meta, AssetData* data) = 0;
virtual void LoadFromBinary(BinaryReader& reader, const AssetMeta& meta, AssetData* data) {}
virtual bool NeedsProcessing() const { return false; }
};
For a texture, Import decodes the image with stb_image and
Serialize writes the width, height, channel count, raw pixels and
sprite rectangles. At load time the pixels go straight to the GPU, with no image decoding at runtime.
if (std::filesystem::exists(meta.exportedPath) &&
std::filesystem::last_write_time(meta.fullPath) < std::filesystem::last_write_time(meta.exportedPath))
return;
ImportedData imported = type->Import(meta);
BinaryWriter writer(meta.guid);
if (type->NeedsProcessing())
type->Serialize(writer, type->Process(imported));
else
type->Serialize(writer, imported);
Adding a new kind of asset means writing one class and registering it. The rest of the pipeline
doesn't change.
AssetData* data = AssetCache::Get(guid);
if (data == nullptr)
{
BinaryReader reader(guid);
const AssetMeta& meta = database.GetProcessedFile(guid);
data = AssetCache::Store(guid, T::CreateNewData());
AssetTypeRegistry::GetType(meta.type)->Load(reader, meta, data);
}
return data;
When the count reaches zero, the asset isn't deleted right away. It's put in a queue that is
processed at the end of the frame, and only assets that are still unused get freed. If a
scene drops a texture and picks it up again in the same frame, which happens a lot when objects are
destroyed and recreated, the texture stays in memory and isn't loaded twice.
void AssetDeletionQueue::ProcessPendingForDelete()
{
for (GUID& guid : pendingForDelete)
{
AssetData* data = AssetCache::Get(guid);
if (data == nullptr) continue;
if (data->refCount <= 0)
AssetCache::Delete(guid);
}
pendingForDelete.clear();
}
void AssetManager::AddFileChanged(const Editor::FileWatcherEvent& e)
{
std::lock_guard<std::mutex> lock(fileChangesMutex);
fileChanges.push_back(e);
}
At the end of each frame the main thread takes the whole list in one short lock and processes it
with the lock released, so the watcher thread is never blocked while files are re-imported:
std::vector<Editor::FileWatcherEvent> localChanges;
{
std::lock_guard<std::mutex> lock(fileChangesMutex);
localChanges = std::move(fileChanges);
fileChanges.clear();
}
for (auto& e : localChanges)
{
const AssetMeta& meta = database.ProcessFile(e.path, e.root);
AssetImporter::ImportFile(meta);
AssetCache::MarkForReload(meta.guid);
}
The reload writes the new data into the existing cache entry instead of creating a new one.
Every handle already points at that entry, so every sprite, material and sound using the asset shows
the new version immediately, without anyone having to look it up again.
struct Header {
Assets::GUID guid;
size_t size;
size_t offset;
AssetType type;
char name[32];
};
The game only needs to read the header table into its asset database at startup. When an asset is
needed, the reader jumps straight to that asset's offset in the bundle, using the same load code the
editor uses:
void TextureAssetType::LoadFromBinary(BinaryReader& reader, const AssetMeta& meta, AssetData* data)
{
reader.SetRead(meta.offset);
Load(reader, meta, data);
}