Refactor asset handling

This commit is contained in:
Thraix
2024-10-01 19:48:21 +02:00
parent b256b90abb
commit 9133bce7fc
22 changed files with 196 additions and 125 deletions
+1 -1
View File
@@ -173,7 +173,6 @@
<ClCompile Include="src\copium\asset\Asset.cpp" /> <ClCompile Include="src\copium\asset\Asset.cpp" />
<ClCompile Include="src\copium\asset\AssetFile.cpp" /> <ClCompile Include="src\copium\asset\AssetFile.cpp" />
<ClCompile Include="src\copium\asset\AssetManager.cpp" /> <ClCompile Include="src\copium\asset\AssetManager.cpp" />
<ClCompile Include="src\copium\asset\AssetRef.cpp" />
<ClCompile Include="src\copium\buffer\RendererVertexBuffer.cpp" /> <ClCompile Include="src\copium\buffer\RendererVertexBuffer.cpp" />
<ClCompile Include="src\copium\buffer\Buffer.cpp" /> <ClCompile Include="src\copium\buffer\Buffer.cpp" />
<ClCompile Include="src\copium\core\Device.cpp" /> <ClCompile Include="src\copium\core\Device.cpp" />
@@ -242,6 +241,7 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="src\copium\asset\Asset.h" /> <ClInclude Include="src\copium\asset\Asset.h" />
<ClInclude Include="src\copium\asset\AssetFile.h" /> <ClInclude Include="src\copium\asset\AssetFile.h" />
<ClInclude Include="src\copium\asset\AssetHandle.h" />
<ClInclude Include="src\copium\asset\AssetManager.h" /> <ClInclude Include="src\copium\asset\AssetManager.h" />
<ClInclude Include="src\copium\asset\AssetMeta.h" /> <ClInclude Include="src\copium\asset\AssetMeta.h" />
<ClInclude Include="src\copium\asset\AssetRef.h" /> <ClInclude Include="src\copium\asset\AssetRef.h" />
+3 -3
View File
@@ -192,9 +192,6 @@
<ClCompile Include="src\copium\ecs\Entity.cpp"> <ClCompile Include="src\copium\ecs\Entity.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\copium\asset\AssetRef.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\copium\ecs\SystemPool.cpp"> <ClCompile Include="src\copium\ecs\SystemPool.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
@@ -467,5 +464,8 @@
<ClInclude Include="src\copium\event\ViewportResize.h"> <ClInclude Include="src\copium\event\ViewportResize.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\copium\asset\AssetHandle.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>
+3 -8
View File
@@ -5,11 +5,6 @@ namespace Copium
Asset::Asset() = default; Asset::Asset() = default;
Asset::~Asset() = default; Asset::~Asset() = default;
AssetHandle Asset::GetHandle() const
{
return metaData.handle;
}
const std::string& Asset::GetName() const const std::string& Asset::GetName() const
{ {
return metaData.name; return metaData.name;
@@ -20,13 +15,13 @@ namespace Copium
return metaData.uuid; return metaData.uuid;
} }
bool Asset::isRuntime() const bool Asset::IsRuntime() const
{ {
return metaData.isRuntime; return metaData.isRuntime;
} }
Asset::operator AssetHandle() const AssetId Asset::GetId() const
{ {
return metaData.handle; return metaData.id;
} }
} }
+2 -4
View File
@@ -16,12 +16,10 @@ namespace Copium
Asset(); Asset();
virtual ~Asset(); virtual ~Asset();
AssetHandle GetHandle() const;
const std::string& GetName() const; const std::string& GetName() const;
Uuid GetUuid() const; Uuid GetUuid() const;
bool isRuntime() const; bool IsRuntime() const;
AssetId GetId() const;
operator AssetHandle() const;
private: private:
AssetMeta metaData; AssetMeta metaData;
@@ -0,0 +1,59 @@
#pragma once
#include "copium/asset/AssetMeta.h"
#include "copium/asset/AssetManager.h"
namespace Copium
{
template <typename AssetType>
class AssetHandle
{
public:
AssetHandle()
: id{NULL_ASSET_ID}
{}
AssetHandle(const std::string& assetName)
: id{AssetManager::LoadAsset(assetName).GetId()}
{}
AssetHandle(const Uuid& uuid)
: id{AssetManager::LoadAsset(uuid).GetId()}
{}
AssetHandle(AssetType& asset)
: id{AssetManager::DuplicateAsset(asset.GetId())}
{}
AssetHandle(const std::string& name, std::unique_ptr<AssetType>&& runtimeAsset)
: id{AssetManager::RegisterRuntimeAsset(name, std::move(runtimeAsset)).GetId()}
{}
AssetHandle(AssetId id)
: id{AssetManager::DuplicateAsset(id)}
{}
AssetId GetId() const
{
return id;
}
AssetType& GetAsset() const
{
return AssetManager::GetAsset<AssetType>(id);
}
void UnloadAsset()
{
AssetManager::UnloadAsset(id);
id = NULL_ASSET_ID;
}
bool Valid() const
{
return id != NULL_ASSET_ID;
}
private:
AssetId id;
};
}
+19 -19
View File
@@ -13,12 +13,12 @@ namespace Copium
{ {
std::vector<std::string> AssetManager::assetDirs; std::vector<std::string> AssetManager::assetDirs;
std::map<std::string, AssetManager::CreateAssetFunc> AssetManager::assetTypes; std::map<std::string, AssetManager::CreateAssetFunc> AssetManager::assetTypes;
std::map<AssetHandle, std::unique_ptr<Asset>> AssetManager::assets; std::map<AssetId, std::unique_ptr<Asset>> AssetManager::assets;
std::map<std::string, AssetHandle> AssetManager::pathToAssetCache; std::map<std::string, AssetId> AssetManager::pathToAssetCache;
std::map<std::string, AssetHandle> AssetManager::nameToAssetCache; std::map<std::string, AssetId> AssetManager::nameToAssetCache;
std::vector<AssetFile> AssetManager::cachedAssetFiles; std::vector<AssetFile> AssetManager::cachedAssetFiles;
AssetHandle AssetManager::assetHandle = 1; AssetId AssetManager::assetId = 1;
AssetHandle AssetManager::runtimeAssetHandle = (1 << 31) + 1; AssetId AssetManager::runtimeAssetId = (1 << 31) + 1;
void AssetManager::RegisterAssetDir(std::string assetDir) void AssetManager::RegisterAssetDir(std::string assetDir)
{ {
@@ -58,9 +58,9 @@ namespace Copium
} }
} }
Asset& AssetManager::GetAsset(AssetHandle handle) Asset& AssetManager::GetAsset(AssetId id)
{ {
auto it = assets.find(handle); auto it = assets.find(id);
CP_ASSERT(it != assets.end(), "Asset not loaded"); CP_ASSERT(it != assets.end(), "Asset not loaded");
return *it->second.get(); return *it->second.get();
} }
@@ -100,19 +100,19 @@ namespace Copium
// TODO: Reload the assetCache to see if a new file has appeared with that uuid // TODO: Reload the assetCache to see if a new file has appeared with that uuid
} }
AssetHandle AssetManager::DuplicateAsset(AssetHandle handle) AssetId AssetManager::DuplicateAsset(AssetId id)
{ {
auto it = assets.find(handle); auto it = assets.find(id);
CP_ASSERT(it != assets.end(), "Failed to find asset with handle=%d", handle); CP_ASSERT(it != assets.end(), "Failed to find asset with id=%d", id);
CP_DEBUG("Duplicating asset: %s", it->second->GetName().c_str()); CP_DEBUG("Duplicating asset: %s", it->second->GetName().c_str());
it->second->metaData.loadCount++; it->second->metaData.loadCount++;
return handle; return id;
} }
void AssetManager::UnloadAsset(AssetHandle handle) void AssetManager::UnloadAsset(AssetId id)
{ {
auto it = assets.find(handle); auto it = assets.find(id);
if (it == assets.end()) if (it == assets.end())
{ {
CP_WARN("Asset not loaded"); CP_WARN("Asset not loaded");
@@ -124,7 +124,7 @@ namespace Copium
if (it->second->metaData.loadCount > 0) if (it->second->metaData.loadCount > 0)
return; return;
if (it->second->isRuntime()) if (it->second->IsRuntime())
nameToAssetCache.erase(it->second->GetName()); nameToAssetCache.erase(it->second->GetName());
else else
pathToAssetCache.erase(it->second->GetName()); pathToAssetCache.erase(it->second->GetName());
@@ -143,7 +143,7 @@ namespace Copium
CP_WARN("Performing auto clean up of %d non unloaded assets", assets.size()); CP_WARN("Performing auto clean up of %d non unloaded assets", assets.size());
while (!assets.empty()) while (!assets.empty())
{ {
UnloadAsset(assets.begin()->second->GetHandle()); UnloadAsset(assets.begin()->second->GetId());
} }
CP_ASSERT(nameToAssetCache.empty(), "Name To Asset Cache not empty after full unload"); CP_ASSERT(nameToAssetCache.empty(), "Name To Asset Cache not empty after full unload");
CP_ASSERT(pathToAssetCache.empty(), "Path To Asset Cache not empty after full unload"); CP_ASSERT(pathToAssetCache.empty(), "Path To Asset Cache not empty after full unload");
@@ -156,13 +156,13 @@ namespace Copium
auto it = nameToAssetCache.find(name); auto it = nameToAssetCache.find(name);
CP_ASSERT(it == nameToAssetCache.end(), "Asset already exists: %s", name); CP_ASSERT(it == nameToAssetCache.end(), "Asset already exists: %s", name);
AssetHandle handle = runtimeAssetHandle++; AssetId id = runtimeAssetId++;
Asset* asset2 = assets.emplace(handle, std::move(asset)).first->second.get(); Asset* asset2 = assets.emplace(id, std::move(asset)).first->second.get();
asset2->metaData.handle = handle; asset2->metaData.id = id;
asset2->metaData.name = name; asset2->metaData.name = name;
asset2->metaData.uuid = Uuid(); asset2->metaData.uuid = Uuid();
asset2->metaData.isRuntime = true; asset2->metaData.isRuntime = true;
nameToAssetCache.emplace(name, handle); nameToAssetCache.emplace(name, id);
return *asset2; return *asset2;
} }
+14 -14
View File
@@ -17,22 +17,22 @@ namespace Copium
using CreateAssetFunc = std::function<Asset&(const MetaFile& metaFile, const std::string& str)>; using CreateAssetFunc = std::function<Asset&(const MetaFile& metaFile, const std::string& str)>;
static std::map<std::string, CreateAssetFunc> assetTypes; static std::map<std::string, CreateAssetFunc> assetTypes;
static std::vector<std::string> assetDirs; static std::vector<std::string> assetDirs;
static std::map<AssetHandle, std::unique_ptr<Asset>> assets; static std::map<AssetId, std::unique_ptr<Asset>> assets;
static std::map<std::string, AssetHandle> pathToAssetCache; static std::map<std::string, AssetId> pathToAssetCache;
static std::map<std::string, AssetHandle> nameToAssetCache; static std::map<std::string, AssetId> nameToAssetCache;
static AssetHandle assetHandle; static AssetId assetId;
static AssetHandle runtimeAssetHandle; static AssetId runtimeAssetId;
static std::vector<AssetFile> cachedAssetFiles; // TODO: Make a set? static std::vector<AssetFile> cachedAssetFiles; // TODO: Make a set?
public: public:
static void RegisterAssetDir(std::string assetDir); static void RegisterAssetDir(std::string assetDir);
static void UnregisterAssetDir(std::string assetDir); static void UnregisterAssetDir(std::string assetDir);
static Asset& GetAsset(AssetHandle handle); static Asset& GetAsset(AssetId id);
static Asset& LoadAsset(const std::string& assetPath); static Asset& LoadAsset(const std::string& assetPath);
static Asset& LoadAsset(const Uuid& uuid); static Asset& LoadAsset(const Uuid& uuid);
static AssetHandle DuplicateAsset(AssetHandle handle); static AssetId DuplicateAsset(AssetId id);
static void UnloadAsset(AssetHandle handle); static void UnloadAsset(AssetId id);
static Asset& RegisterRuntimeAsset(const std::string& name, std::unique_ptr<Asset>&& asset); static Asset& RegisterRuntimeAsset(const std::string& name, std::unique_ptr<Asset>&& asset);
static const std::vector<AssetFile>& GetAssetFiles(); static const std::vector<AssetFile>& GetAssetFiles();
static void Cleanup(); static void Cleanup();
@@ -61,9 +61,9 @@ namespace Copium
} }
template <typename AssetT> template <typename AssetT>
static AssetT& GetAsset(AssetHandle handle) static AssetT& GetAsset(AssetId id)
{ {
Asset& asset = GetAsset(handle); Asset& asset = GetAsset(id);
AssetT* assetT = dynamic_cast<AssetT*>(&asset); AssetT* assetT = dynamic_cast<AssetT*>(&asset);
CP_ASSERT(assetT, "Invalid Asset cast"); CP_ASSERT(assetT, "Invalid Asset cast");
return *assetT; return *assetT;
@@ -83,10 +83,10 @@ namespace Copium
template <typename T> template <typename T>
static Asset& CreateAsset(const MetaFile& metaFile, const std::string& metaFileClass) static Asset& CreateAsset(const MetaFile& metaFile, const std::string& metaFileClass)
{ {
AssetHandle handle = assetHandle++; AssetId id = assetId++;
pathToAssetCache.emplace(metaFile.GetFilePath(), handle); pathToAssetCache.emplace(metaFile.GetFilePath(), id);
Asset& asset = *assets.emplace(handle, std::make_unique<T>(metaFile)).first->second.get(); Asset& asset = *assets.emplace(id, std::make_unique<T>(metaFile)).first->second.get();
asset.metaData.handle = handle; asset.metaData.id = id;
asset.metaData.name = metaFile.GetFilePath(); asset.metaData.name = metaFile.GetFilePath();
asset.metaData.uuid = Uuid{metaFile.GetMetaClass(metaFileClass).GetValue("uuid")}; asset.metaData.uuid = Uuid{metaFile.GetMetaClass(metaFileClass).GetValue("uuid")};
asset.metaData.isRuntime = false; asset.metaData.isRuntime = false;
+3 -3
View File
@@ -7,12 +7,12 @@
namespace Copium namespace Copium
{ {
using AssetHandle = uint64_t; static constexpr int NULL_ASSET_ID = 0;
static constexpr int NULL_ASSET_HANDLE = 0; using AssetId = uint32_t;
struct AssetMeta struct AssetMeta
{ {
AssetHandle handle; AssetId id;
std::string name; std::string name;
Uuid uuid; Uuid uuid;
bool isRuntime = false; bool isRuntime = false;
@@ -1,23 +0,0 @@
#include "copium/asset/AssetRef.h"
#include "copium/asset/AssetManager.h"
namespace Copium
{
struct AssetHandleUnloader {
void operator()(AssetHandle* handle) {
AssetManager::UnloadAsset(*handle);
}
};
AssetRef::AssetRef() = default;
AssetRef::AssetRef(AssetHandle handle)
: handle{std::shared_ptr<AssetHandle>(new AssetHandle{handle}, AssetHandleUnloader{})}
{}
AssetRef::operator AssetHandle() const
{
return *handle;
}
}
+52 -8
View File
@@ -1,19 +1,63 @@
#pragma once #pragma once
#include "copium/asset/AssetMeta.h" #include "copium/asset/AssetManager.h"
namespace Copium namespace Copium
{ {
template <typename AssetType>
class AssetRef class AssetRef
{ {
private:
std::shared_ptr<AssetHandle> handle;
public: public:
AssetRef(); AssetRef()
AssetRef(AssetHandle handle); : id{std::shared_ptr<AssetId>(new AssetId(NULL_ASSET_ID), AssetIdUnloader{})}
{}
operator AssetHandle() const; AssetRef(const std::string& assetName)
}; : id{std::shared_ptr<AssetId>(new AssetId(AssetManager::LoadAsset(assetName).GetId()), AssetIdUnloader{})}
{}
AssetRef(const Uuid& uuid)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::LoadAsset(uuid).GetId()), AssetIdUnloader{})}
{}
AssetRef(AssetType& asset)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::DuplicateAsset(asset.GetId())), AssetIdUnloader{})}
{}
AssetRef(const std::string& name, std::unique_ptr<AssetType>&& runtimeAsset)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::RegisterRuntimeAsset(name, std::move(runtimeAsset)).GetId()), AssetIdUnloader{})}
{}
AssetRef(AssetId id)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::DuplicateAsset(id)), AssetIdUnloader{})}
{}
AssetId GetId() const
{
return *id;
}
AssetType& GetAsset() const
{
return AssetManager::GetAsset<AssetType>(*id);
}
bool Valid() const
{
return *id != NULL_ASSET_ID;
}
private:
struct AssetIdUnloader {
void operator()(AssetId* id)
{
if (*id != NULL_ASSET_ID)
{
AssetManager::UnloadAsset(*id);
}
}
};
private:
std::shared_ptr<AssetId> id;
};
} }
@@ -10,8 +10,8 @@ namespace Copium
Framebuffer::Framebuffer(const MetaFile& metaFile) Framebuffer::Framebuffer(const MetaFile& metaFile)
{ {
const MetaFileClass& metaClass = metaFile.GetMetaClass("Framebuffer"); const MetaFileClass& metaClass = metaFile.GetMetaClass("Framebuffer");
ColorAttachment& attachment = AssetManager::LoadAsset<ColorAttachment>(Uuid{metaClass.GetValue("rendertexture-uuid")}); colorAttachment = AssetRef<ColorAttachment>(Uuid{metaClass.GetValue("rendertexture-uuid")});
colorAttachment = attachment; ColorAttachment& attachment = colorAttachment.GetAsset();
width = attachment.GetWidth(); width = attachment.GetWidth();
height = attachment.GetHeight(); height = attachment.GetHeight();
InitializeDepthBuffer(); InitializeDepthBuffer();
@@ -33,7 +33,6 @@ namespace Copium
for (auto& framebuffer : framebuffers) for (auto& framebuffer : framebuffers)
vkDestroyFramebuffer(Vulkan::GetDevice(), framebuffer, nullptr); vkDestroyFramebuffer(Vulkan::GetDevice(), framebuffer, nullptr);
vkDestroyRenderPass(Vulkan::GetDevice(), renderPass, nullptr); vkDestroyRenderPass(Vulkan::GetDevice(), renderPass, nullptr);
AssetManager::UnloadAsset(colorAttachment);
} }
void Framebuffer::Resize(uint32_t width, uint32_t height) void Framebuffer::Resize(uint32_t width, uint32_t height)
@@ -43,7 +42,7 @@ namespace Copium
this->height = height; this->height = height;
for (auto&& framebuffer : framebuffers) for (auto&& framebuffer : framebuffers)
vkDestroyFramebuffer(Vulkan::GetDevice(), framebuffer, nullptr); vkDestroyFramebuffer(Vulkan::GetDevice(), framebuffer, nullptr);
AssetManager::GetAsset<ColorAttachment>(colorAttachment).Resize(width, height); colorAttachment.GetAsset().Resize(width, height);
depthAttachment->Resize(width, height); depthAttachment->Resize(width, height);
InitializeFramebuffers(); InitializeFramebuffers();
} }
@@ -96,7 +95,7 @@ namespace Copium
const ColorAttachment& Framebuffer::GetColorAttachment() const const ColorAttachment& Framebuffer::GetColorAttachment() const
{ {
return AssetManager::GetAsset<ColorAttachment>(colorAttachment); return colorAttachment.GetAsset();
} }
uint32_t Framebuffer::GetWidth() const uint32_t Framebuffer::GetWidth() const
@@ -186,7 +185,7 @@ namespace Copium
void Framebuffer::InitializeFramebuffers() void Framebuffer::InitializeFramebuffers()
{ {
framebuffers.resize(SwapChain::MAX_FRAMES_IN_FLIGHT); framebuffers.resize(SwapChain::MAX_FRAMES_IN_FLIGHT);
const ColorAttachment& attachment = AssetManager::GetAsset<ColorAttachment>(colorAttachment); const ColorAttachment& attachment = colorAttachment.GetAsset();
for (size_t i = 0; i < framebuffers.size(); ++i) for (size_t i = 0; i < framebuffers.size(); ++i)
{ {
std::vector<VkImageView> attachments{attachment.GetImageView(i), depthAttachment->GetImageView()}; std::vector<VkImageView> attachments{attachment.GetImageView(i), depthAttachment->GetImageView()};
+2 -1
View File
@@ -2,6 +2,7 @@
#include "copium/asset/Asset.h" #include "copium/asset/Asset.h"
#include "copium/asset/AssetMeta.h" #include "copium/asset/AssetMeta.h"
#include "copium/asset/AssetRef.h"
#include "copium/buffer/CommandBuffer.h" #include "copium/buffer/CommandBuffer.h"
#include "copium/sampler/ColorAttachment.h" #include "copium/sampler/ColorAttachment.h"
#include "copium/sampler/DepthAttachment.h" #include "copium/sampler/DepthAttachment.h"
@@ -15,7 +16,7 @@ namespace Copium
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(Framebuffer); CP_DELETE_COPY_AND_MOVE_CTOR(Framebuffer);
private: private:
AssetHandle colorAttachment; AssetRef<ColorAttachment> colorAttachment;
std::unique_ptr<DepthAttachment> depthAttachment; std::unique_ptr<DepthAttachment> depthAttachment;
std::vector<VkFramebuffer> framebuffers; std::vector<VkFramebuffer> framebuffers;
VkRenderPass renderPass; VkRenderPass renderPass;
+7 -7
View File
@@ -1,11 +1,11 @@
#include "copium/core/Vulkan.h" #include "copium/core/Vulkan.h"
#include "copium/asset/AssetManager.h" #include "copium/asset/AssetManager.h"
#include "copium/sampler/Texture2D.h" #include "copium/buffer/Framebuffer.h"
#include "copium/pipeline/Pipeline.h"
#include "copium/sampler/ColorAttachment.h" #include "copium/sampler/ColorAttachment.h"
#include "copium/sampler/Font.h" #include "copium/sampler/Font.h"
#include "copium/pipeline/Pipeline.h" #include "copium/sampler/Texture2D.h"
#include "copium/buffer/Framebuffer.h"
#include "copium/util/Timer.h" #include "copium/util/Timer.h"
namespace Copium namespace Copium
@@ -15,7 +15,7 @@ namespace Copium
std::unique_ptr<Device> Vulkan::device; std::unique_ptr<Device> Vulkan::device;
std::unique_ptr<SwapChain> Vulkan::swapChain; std::unique_ptr<SwapChain> Vulkan::swapChain;
std::unique_ptr<ImGuiInstance> Vulkan::imGuiInstance; std::unique_ptr<ImGuiInstance> Vulkan::imGuiInstance;
AssetHandle Vulkan::emptyTexture2D; AssetHandle<Texture2D> Vulkan::emptyTexture2D;
void Vulkan::Initialize() void Vulkan::Initialize()
{ {
@@ -38,13 +38,13 @@ namespace Copium
// TODO: Make the working directory always be relative to the assets folder // TODO: Make the working directory always be relative to the assets folder
// By looking at where the executable is, since that should always be in the bin folder (it currently isn't though) // By looking at where the executable is, since that should always be in the bin folder (it currently isn't though)
AssetManager::RegisterAssetDir("assets/"); AssetManager::RegisterAssetDir("assets/");
emptyTexture2D = AssetManager::RegisterRuntimeAsset("empty_texture2d", std::make_unique<Texture2D>(std::vector<uint8_t>{255, 0, 255, 255}, 1, 1, SamplerCreator{})); emptyTexture2D = AssetHandle<Texture2D>{"empty_texture2d", std::make_unique<Texture2D>(std::vector<uint8_t>{255, 0, 255, 255}, 1, 1, SamplerCreator{})};
CP_INFO("Initialized AssetManager in %f seconds", timer.Elapsed()); CP_INFO("Initialized AssetManager in %f seconds", timer.Elapsed());
} }
void Vulkan::Destroy() void Vulkan::Destroy()
{ {
AssetManager::UnloadAsset(emptyTexture2D); emptyTexture2D.UnloadAsset();
AssetManager::UnregisterAssetDir("assets/"); AssetManager::UnregisterAssetDir("assets/");
AssetManager::Cleanup(); AssetManager::Cleanup();
imGuiInstance.reset(); imGuiInstance.reset();
@@ -79,7 +79,7 @@ namespace Copium
return *imGuiInstance; return *imGuiInstance;
} }
AssetHandle Vulkan::GetEmptyTexture2D() AssetHandle<Texture2D> Vulkan::GetEmptyTexture2D()
{ {
return emptyTexture2D; return emptyTexture2D;
} }
+4 -2
View File
@@ -1,11 +1,13 @@
#pragma once #pragma once
#include "copium/asset/AssetHandle.h"
#include "copium/core/Device.h" #include "copium/core/Device.h"
#include "copium/core/Instance.h" #include "copium/core/Instance.h"
#include "copium/core/SwapChain.h" #include "copium/core/SwapChain.h"
#include "copium/core/Window.h" #include "copium/core/Window.h"
#include "copium/core/ImGuiInstance.h" #include "copium/core/ImGuiInstance.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
#include "copium/sampler/Texture2D.h"
#include <memory> #include <memory>
@@ -21,7 +23,7 @@ namespace Copium
static std::unique_ptr<SwapChain> swapChain; static std::unique_ptr<SwapChain> swapChain;
static std::unique_ptr<ImGuiInstance> imGuiInstance; static std::unique_ptr<ImGuiInstance> imGuiInstance;
static AssetHandle emptyTexture2D; static AssetHandle<Texture2D> emptyTexture2D;
public: public:
static void Initialize(); static void Initialize();
static void Destroy(); static void Destroy();
@@ -31,6 +33,6 @@ namespace Copium
static SwapChain& GetSwapChain(); static SwapChain& GetSwapChain();
static ImGuiInstance& GetImGuiInstance(); static ImGuiInstance& GetImGuiInstance();
static bool Valid(); static bool Valid();
static AssetHandle GetEmptyTexture2D(); static AssetHandle<Texture2D> GetEmptyTexture2D();
}; };
} }
@@ -18,9 +18,9 @@ namespace Copium
VkRenderPass renderPass; VkRenderPass renderPass;
if (metaFileClass.HasValue("framebuffer-uuid")) if (metaFileClass.HasValue("framebuffer-uuid"))
{ {
Framebuffer& fb = AssetManager::LoadAsset<Framebuffer>(Uuid{metaFileClass.GetValue("framebuffer-uuid")}); framebuffer = AssetRef<Framebuffer>(Uuid{metaFileClass.GetValue("framebuffer-uuid")});
Framebuffer& fb = framebuffer.GetAsset();
renderPass = fb.GetRenderPass(); renderPass = fb.GetRenderPass();
framebuffer = fb;
} }
else else
{ {
@@ -62,10 +62,6 @@ namespace Copium
{ {
vkDestroyDescriptorSetLayout(Vulkan::GetDevice(), descriptorSetLayout, nullptr); vkDestroyDescriptorSetLayout(Vulkan::GetDevice(), descriptorSetLayout, nullptr);
} }
if (framebuffer != NULL_ASSET_HANDLE)
{
AssetManager::UnloadAsset(framebuffer);
}
} }
void Pipeline::Bind(const CommandBuffer& commandBuffer) void Pipeline::Bind(const CommandBuffer& commandBuffer)
+3 -1
View File
@@ -1,7 +1,9 @@
#pragma once #pragma once
#include "copium/asset/Asset.h" #include "copium/asset/Asset.h"
#include "copium/asset/AssetRef.h"
#include "copium/buffer/CommandBuffer.h" #include "copium/buffer/CommandBuffer.h"
#include "copium/buffer/Framebuffer.h"
#include "copium/pipeline/DescriptorSet.h" #include "copium/pipeline/DescriptorSet.h"
#include "copium/pipeline/PipelineCreator.h" #include "copium/pipeline/PipelineCreator.h"
#include "copium/util/Common.h" #include "copium/util/Common.h"
@@ -20,7 +22,7 @@ namespace Copium
std::vector<VkDescriptorSet> boundDescriptorSets; std::vector<VkDescriptorSet> boundDescriptorSets;
VkPipelineLayout pipelineLayout; VkPipelineLayout pipelineLayout;
VkPipeline graphicsPipeline; VkPipeline graphicsPipeline;
AssetHandle framebuffer; AssetRef<Framebuffer> framebuffer;
public: public:
+2 -2
View File
@@ -5,9 +5,9 @@
namespace Copium namespace Copium
{ {
Batch::Batch(AssetHandle pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers) Batch::Batch(AssetRef<Pipeline>& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers)
: vertexBuffer{RendererVertex::GetDescriptor(), vertexCount}, : vertexBuffer{RendererVertex::GetDescriptor(), vertexCount},
descriptorSet{AssetManager::GetAsset<Pipeline>(pipeline).CreateDescriptorSet(descriptorPool, 0)} descriptorSet{pipeline.GetAsset().CreateDescriptorSet(descriptorPool, 0)}
{} {}
RendererVertexBuffer& Batch::GetVertexBuffer() RendererVertexBuffer& Batch::GetVertexBuffer()
+2 -1
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "copium/asset/AssetMeta.h" #include "copium/asset/AssetMeta.h"
#include "copium/asset/AssetRef.h"
#include "copium/buffer/RendererVertexBuffer.h" #include "copium/buffer/RendererVertexBuffer.h"
#include "copium/pipeline/DescriptorSet.h" #include "copium/pipeline/DescriptorSet.h"
#include "copium/pipeline/Pipeline.h" #include "copium/pipeline/Pipeline.h"
@@ -15,7 +16,7 @@ namespace Copium
RendererVertexBuffer vertexBuffer; RendererVertexBuffer vertexBuffer;
std::unique_ptr<DescriptorSet> descriptorSet; std::unique_ptr<DescriptorSet> descriptorSet;
public: public:
Batch(AssetHandle pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers); Batch(AssetRef<Pipeline>& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers);
RendererVertexBuffer& GetVertexBuffer(); RendererVertexBuffer& GetVertexBuffer();
DescriptorSet& GetDescriptorSet(); DescriptorSet& GetDescriptorSet();
}; };
+9 -14
View File
@@ -15,15 +15,14 @@ namespace Copium
Renderer::Renderer() Renderer::Renderer()
: descriptorPool{}, : descriptorPool{},
ibo{MAX_NUM_INDICES}, ibo{MAX_NUM_INDICES},
samplers{MAX_NUM_TEXTURES, &AssetManager::GetAsset<Texture2D>(Vulkan::GetEmptyTexture2D())} pipeline{"renderer.meta"}, // TODO: should be a runtime renderer pipeline or passed in constructor
samplers{MAX_NUM_TEXTURES, &Vulkan::GetEmptyTexture2D().GetAsset()}
{ {
InitializeIndexBuffer(); InitializeIndexBuffer();
InitializeGraphicsPipeline();
} }
Renderer::~Renderer() Renderer::~Renderer()
{ {
AssetManager::UnloadAsset(pipeline);
} }
void Renderer::Quad(const glm::vec2& pos, const glm::vec2& size, const glm::vec3& color) void Renderer::Quad(const glm::vec2& pos, const glm::vec2& size, const glm::vec3& color)
@@ -94,7 +93,7 @@ namespace Copium
void Renderer::Begin(CommandBuffer& commandBuffer) void Renderer::Begin(CommandBuffer& commandBuffer)
{ {
AssetManager::GetAsset<Pipeline>(pipeline).Bind(commandBuffer); pipeline.GetAsset().Bind(commandBuffer);
ibo.Bind(commandBuffer); ibo.Bind(commandBuffer);
batchIndex = -1; batchIndex = -1;
NextBatch(); NextBatch();
@@ -108,12 +107,12 @@ namespace Copium
Pipeline& Renderer::GetGraphicsPipeline() Pipeline& Renderer::GetGraphicsPipeline()
{ {
return AssetManager::GetAsset<Pipeline>(pipeline); return pipeline.GetAsset();
} }
void Renderer::SetDescriptorSet(const DescriptorSet& descriptorSet) void Renderer::SetDescriptorSet(const DescriptorSet& descriptorSet)
{ {
AssetManager::GetAsset<Pipeline>(pipeline).SetDescriptorSet(descriptorSet); pipeline.GetAsset().SetDescriptorSet(descriptorSet);
} }
void Renderer::InitializeIndexBuffer() void Renderer::InitializeIndexBuffer()
@@ -134,11 +133,6 @@ namespace Copium
ibo.UpdateStaging(indices.data()); ibo.UpdateStaging(indices.data());
} }
void Renderer::InitializeGraphicsPipeline()
{
pipeline = AssetManager::LoadAsset<Pipeline>("renderer.meta");
}
int Renderer::AllocateSampler(const Sampler& sampler) int Renderer::AllocateSampler(const Sampler& sampler)
{ {
for (size_t i = 0; i < textureCount; i++) for (size_t i = 0; i < textureCount; i++)
@@ -174,15 +168,16 @@ namespace Copium
{ {
batches[batchIndex]->GetVertexBuffer().Unmap(); batches[batchIndex]->GetVertexBuffer().Unmap();
batches[batchIndex]->GetVertexBuffer().Bind(*currentCommandBuffer); batches[batchIndex]->GetVertexBuffer().Bind(*currentCommandBuffer);
AssetManager::GetAsset<Pipeline>(pipeline).SetDescriptorSet(batches[batchIndex]->GetDescriptorSet()); Pipeline& p = pipeline.GetAsset();
AssetManager::GetAsset<Pipeline>(pipeline).BindDescriptorSets(*currentCommandBuffer); p.SetDescriptorSet(batches[batchIndex]->GetDescriptorSet());
p.BindDescriptorSets(*currentCommandBuffer);
ibo.Draw(*currentCommandBuffer, quadCount * 6); ibo.Draw(*currentCommandBuffer, quadCount * 6);
} }
void Renderer::NextBatch() void Renderer::NextBatch()
{ {
batchIndex++; batchIndex++;
std::fill(samplers.begin(), samplers.end(), &AssetManager::GetAsset<Texture2D>(Vulkan::GetEmptyTexture2D())); std::fill(samplers.begin(), samplers.end(), &Vulkan::GetEmptyTexture2D().GetAsset());
if (batchIndex >= batches.size()) if (batchIndex >= batches.size())
{ {
batches.emplace_back(std::make_unique<Batch>(pipeline, descriptorPool, MAX_NUM_VERTICES, samplers)); batches.emplace_back(std::make_unique<Batch>(pipeline, descriptorPool, MAX_NUM_VERTICES, samplers));
+2 -2
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "copium/asset/AssetMeta.h" #include "copium/asset/AssetMeta.h"
#include "copium/asset/AssetRef.h"
#include "copium/buffer/CommandBuffer.h" #include "copium/buffer/CommandBuffer.h"
#include "copium/buffer/IndexBuffer.h" #include "copium/buffer/IndexBuffer.h"
#include "copium/buffer/RendererVertexBuffer.h" #include "copium/buffer/RendererVertexBuffer.h"
@@ -21,7 +22,7 @@ namespace Copium
private: private:
DescriptorPool descriptorPool; DescriptorPool descriptorPool;
IndexBuffer ibo; IndexBuffer ibo;
AssetHandle pipeline; AssetRef<Pipeline> pipeline;
std::vector<std::unique_ptr<Batch>> batches; std::vector<std::unique_ptr<Batch>> batches;
// Temporary data during a render // Temporary data during a render
@@ -47,7 +48,6 @@ namespace Copium
void SetDescriptorSet(const DescriptorSet& descriptorSet); void SetDescriptorSet(const DescriptorSet& descriptorSet);
private: private:
void InitializeIndexBuffer(); void InitializeIndexBuffer();
void InitializeGraphicsPipeline();
int AllocateSampler(const Sampler& sampler); int AllocateSampler(const Sampler& sampler);
void AllocateQuad(); void AllocateQuad();
+1
View File
@@ -28,6 +28,7 @@ namespace Copium
float GetBaseHeight() const; float GetBaseHeight() const;
BoundingBox GetTextBoundingBox(const std::string& str, float size) const; BoundingBox GetTextBoundingBox(const std::string& str, float size) const;
private: private:
void InitializeTextureImageFromData(const uint8_t* rgbaData, int width, int height); void InitializeTextureImageFromData(const uint8_t* rgbaData, int width, int height);
}; };
@@ -18,6 +18,7 @@ namespace Copium
~Texture2D() override; ~Texture2D() override;
VkDescriptorImageInfo GetDescriptorImageInfo(int index) const override; VkDescriptorImageInfo GetDescriptorImageInfo(int index) const override;
private: private:
void InitializeTextureImageFromFile(const std::string& filename); void InitializeTextureImageFromFile(const std::string& filename);
void InitializeTextureImageFromData(const uint8_t* rgbaData, int width, int height); void InitializeTextureImageFromData(const uint8_t* rgbaData, int width, int height);