diff --git a/CopiumEngine/CopiumEngine.vcxproj b/CopiumEngine/CopiumEngine.vcxproj index 5c9d73d..188f45f 100644 --- a/CopiumEngine/CopiumEngine.vcxproj +++ b/CopiumEngine/CopiumEngine.vcxproj @@ -173,7 +173,6 @@ - @@ -242,6 +241,7 @@ + diff --git a/CopiumEngine/CopiumEngine.vcxproj.filters b/CopiumEngine/CopiumEngine.vcxproj.filters index 18f0ce7..5257914 100644 --- a/CopiumEngine/CopiumEngine.vcxproj.filters +++ b/CopiumEngine/CopiumEngine.vcxproj.filters @@ -192,9 +192,6 @@ Source Files - - Source Files - Source Files @@ -467,5 +464,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/CopiumEngine/src/copium/asset/Asset.cpp b/CopiumEngine/src/copium/asset/Asset.cpp index d8b0560..4f054c3 100644 --- a/CopiumEngine/src/copium/asset/Asset.cpp +++ b/CopiumEngine/src/copium/asset/Asset.cpp @@ -5,11 +5,6 @@ namespace Copium Asset::Asset() = default; Asset::~Asset() = default; - AssetHandle Asset::GetHandle() const - { - return metaData.handle; - } - const std::string& Asset::GetName() const { return metaData.name; @@ -20,13 +15,13 @@ namespace Copium return metaData.uuid; } - bool Asset::isRuntime() const + bool Asset::IsRuntime() const { return metaData.isRuntime; } - Asset::operator AssetHandle() const + AssetId Asset::GetId() const { - return metaData.handle; + return metaData.id; } } \ No newline at end of file diff --git a/CopiumEngine/src/copium/asset/Asset.h b/CopiumEngine/src/copium/asset/Asset.h index fef046a..38a5141 100644 --- a/CopiumEngine/src/copium/asset/Asset.h +++ b/CopiumEngine/src/copium/asset/Asset.h @@ -16,12 +16,10 @@ namespace Copium Asset(); virtual ~Asset(); - AssetHandle GetHandle() const; const std::string& GetName() const; Uuid GetUuid() const; - bool isRuntime() const; - - operator AssetHandle() const; + bool IsRuntime() const; + AssetId GetId() const; private: AssetMeta metaData; diff --git a/CopiumEngine/src/copium/asset/AssetHandle.h b/CopiumEngine/src/copium/asset/AssetHandle.h new file mode 100644 index 0000000..851423f --- /dev/null +++ b/CopiumEngine/src/copium/asset/AssetHandle.h @@ -0,0 +1,59 @@ +#pragma once + +#include "copium/asset/AssetMeta.h" +#include "copium/asset/AssetManager.h" + +namespace Copium +{ + template + 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&& 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(id); + } + + void UnloadAsset() + { + AssetManager::UnloadAsset(id); + id = NULL_ASSET_ID; + } + + bool Valid() const + { + return id != NULL_ASSET_ID; + } + private: + AssetId id; + }; +} diff --git a/CopiumEngine/src/copium/asset/AssetManager.cpp b/CopiumEngine/src/copium/asset/AssetManager.cpp index 4eb17ed..8301b8d 100644 --- a/CopiumEngine/src/copium/asset/AssetManager.cpp +++ b/CopiumEngine/src/copium/asset/AssetManager.cpp @@ -13,12 +13,12 @@ namespace Copium { std::vector AssetManager::assetDirs; std::map AssetManager::assetTypes; - std::map> AssetManager::assets; - std::map AssetManager::pathToAssetCache; - std::map AssetManager::nameToAssetCache; + std::map> AssetManager::assets; + std::map AssetManager::pathToAssetCache; + std::map AssetManager::nameToAssetCache; std::vector AssetManager::cachedAssetFiles; - AssetHandle AssetManager::assetHandle = 1; - AssetHandle AssetManager::runtimeAssetHandle = (1 << 31) + 1; + AssetId AssetManager::assetId = 1; + AssetId AssetManager::runtimeAssetId = (1 << 31) + 1; 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"); 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 } - AssetHandle AssetManager::DuplicateAsset(AssetHandle handle) + AssetId AssetManager::DuplicateAsset(AssetId id) { - auto it = assets.find(handle); - CP_ASSERT(it != assets.end(), "Failed to find asset with handle=%d", handle); + auto it = assets.find(id); + CP_ASSERT(it != assets.end(), "Failed to find asset with id=%d", id); CP_DEBUG("Duplicating asset: %s", it->second->GetName().c_str()); 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()) { CP_WARN("Asset not loaded"); @@ -124,7 +124,7 @@ namespace Copium if (it->second->metaData.loadCount > 0) return; - if (it->second->isRuntime()) + if (it->second->IsRuntime()) nameToAssetCache.erase(it->second->GetName()); else pathToAssetCache.erase(it->second->GetName()); @@ -143,7 +143,7 @@ namespace Copium CP_WARN("Performing auto clean up of %d non unloaded assets", assets.size()); 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(pathToAssetCache.empty(), "Path To Asset Cache not empty after full unload"); @@ -156,13 +156,13 @@ namespace Copium auto it = nameToAssetCache.find(name); CP_ASSERT(it == nameToAssetCache.end(), "Asset already exists: %s", name); - AssetHandle handle = runtimeAssetHandle++; - Asset* asset2 = assets.emplace(handle, std::move(asset)).first->second.get(); - asset2->metaData.handle = handle; + AssetId id = runtimeAssetId++; + Asset* asset2 = assets.emplace(id, std::move(asset)).first->second.get(); + asset2->metaData.id = id; asset2->metaData.name = name; asset2->metaData.uuid = Uuid(); asset2->metaData.isRuntime = true; - nameToAssetCache.emplace(name, handle); + nameToAssetCache.emplace(name, id); return *asset2; } diff --git a/CopiumEngine/src/copium/asset/AssetManager.h b/CopiumEngine/src/copium/asset/AssetManager.h index 8eca434..8155560 100644 --- a/CopiumEngine/src/copium/asset/AssetManager.h +++ b/CopiumEngine/src/copium/asset/AssetManager.h @@ -17,22 +17,22 @@ namespace Copium using CreateAssetFunc = std::function; static std::map assetTypes; static std::vector assetDirs; - static std::map> assets; + static std::map> assets; - static std::map pathToAssetCache; - static std::map nameToAssetCache; - static AssetHandle assetHandle; - static AssetHandle runtimeAssetHandle; + static std::map pathToAssetCache; + static std::map nameToAssetCache; + static AssetId assetId; + static AssetId runtimeAssetId; static std::vector cachedAssetFiles; // TODO: Make a set? public: static void RegisterAssetDir(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 Uuid& uuid); - static AssetHandle DuplicateAsset(AssetHandle handle); - static void UnloadAsset(AssetHandle handle); + static AssetId DuplicateAsset(AssetId id); + static void UnloadAsset(AssetId id); static Asset& RegisterRuntimeAsset(const std::string& name, std::unique_ptr&& asset); static const std::vector& GetAssetFiles(); static void Cleanup(); @@ -61,9 +61,9 @@ namespace Copium } template - static AssetT& GetAsset(AssetHandle handle) + static AssetT& GetAsset(AssetId id) { - Asset& asset = GetAsset(handle); + Asset& asset = GetAsset(id); AssetT* assetT = dynamic_cast(&asset); CP_ASSERT(assetT, "Invalid Asset cast"); return *assetT; @@ -83,10 +83,10 @@ namespace Copium template static Asset& CreateAsset(const MetaFile& metaFile, const std::string& metaFileClass) { - AssetHandle handle = assetHandle++; - pathToAssetCache.emplace(metaFile.GetFilePath(), handle); - Asset& asset = *assets.emplace(handle, std::make_unique(metaFile)).first->second.get(); - asset.metaData.handle = handle; + AssetId id = assetId++; + pathToAssetCache.emplace(metaFile.GetFilePath(), id); + Asset& asset = *assets.emplace(id, std::make_unique(metaFile)).first->second.get(); + asset.metaData.id = id; asset.metaData.name = metaFile.GetFilePath(); asset.metaData.uuid = Uuid{metaFile.GetMetaClass(metaFileClass).GetValue("uuid")}; asset.metaData.isRuntime = false; diff --git a/CopiumEngine/src/copium/asset/AssetMeta.h b/CopiumEngine/src/copium/asset/AssetMeta.h index c6186b7..1f3f735 100644 --- a/CopiumEngine/src/copium/asset/AssetMeta.h +++ b/CopiumEngine/src/copium/asset/AssetMeta.h @@ -7,12 +7,12 @@ namespace Copium { - using AssetHandle = uint64_t; - static constexpr int NULL_ASSET_HANDLE = 0; + static constexpr int NULL_ASSET_ID = 0; + using AssetId = uint32_t; struct AssetMeta { - AssetHandle handle; + AssetId id; std::string name; Uuid uuid; bool isRuntime = false; diff --git a/CopiumEngine/src/copium/asset/AssetRef.cpp b/CopiumEngine/src/copium/asset/AssetRef.cpp deleted file mode 100644 index c7d60a1..0000000 --- a/CopiumEngine/src/copium/asset/AssetRef.cpp +++ /dev/null @@ -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(new AssetHandle{handle}, AssetHandleUnloader{})} - {} - - AssetRef::operator AssetHandle() const - { - return *handle; - } -} diff --git a/CopiumEngine/src/copium/asset/AssetRef.h b/CopiumEngine/src/copium/asset/AssetRef.h index d5101f3..678e159 100644 --- a/CopiumEngine/src/copium/asset/AssetRef.h +++ b/CopiumEngine/src/copium/asset/AssetRef.h @@ -1,19 +1,63 @@ #pragma once -#include "copium/asset/AssetMeta.h" +#include "copium/asset/AssetManager.h" namespace Copium { + template class AssetRef { - private: - std::shared_ptr handle; - public: - AssetRef(); - AssetRef(AssetHandle handle); + AssetRef() + : id{std::shared_ptr(new AssetId(NULL_ASSET_ID), AssetIdUnloader{})} + {} - operator AssetHandle() const; + AssetRef(const std::string& assetName) + : id{std::shared_ptr(new AssetId(AssetManager::LoadAsset(assetName).GetId()), AssetIdUnloader{})} + {} + + AssetRef(const Uuid& uuid) + : id{std::shared_ptr(new AssetId(AssetManager::LoadAsset(uuid).GetId()), AssetIdUnloader{})} + {} + + AssetRef(AssetType& asset) + : id{std::shared_ptr(new AssetId(AssetManager::DuplicateAsset(asset.GetId())), AssetIdUnloader{})} + {} + + AssetRef(const std::string& name, std::unique_ptr&& runtimeAsset) + : id{std::shared_ptr(new AssetId(AssetManager::RegisterRuntimeAsset(name, std::move(runtimeAsset)).GetId()), AssetIdUnloader{})} + {} + + AssetRef(AssetId id) + : id{std::shared_ptr(new AssetId(AssetManager::DuplicateAsset(id)), AssetIdUnloader{})} + {} + + AssetId GetId() const + { + return *id; + } + + AssetType& GetAsset() const + { + return AssetManager::GetAsset(*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 id; }; - } diff --git a/CopiumEngine/src/copium/buffer/Framebuffer.cpp b/CopiumEngine/src/copium/buffer/Framebuffer.cpp index 3de8ec6..7095ed7 100644 --- a/CopiumEngine/src/copium/buffer/Framebuffer.cpp +++ b/CopiumEngine/src/copium/buffer/Framebuffer.cpp @@ -10,8 +10,8 @@ namespace Copium Framebuffer::Framebuffer(const MetaFile& metaFile) { const MetaFileClass& metaClass = metaFile.GetMetaClass("Framebuffer"); - ColorAttachment& attachment = AssetManager::LoadAsset(Uuid{metaClass.GetValue("rendertexture-uuid")}); - colorAttachment = attachment; + colorAttachment = AssetRef(Uuid{metaClass.GetValue("rendertexture-uuid")}); + ColorAttachment& attachment = colorAttachment.GetAsset(); width = attachment.GetWidth(); height = attachment.GetHeight(); InitializeDepthBuffer(); @@ -33,7 +33,6 @@ namespace Copium for (auto& framebuffer : framebuffers) vkDestroyFramebuffer(Vulkan::GetDevice(), framebuffer, nullptr); vkDestroyRenderPass(Vulkan::GetDevice(), renderPass, nullptr); - AssetManager::UnloadAsset(colorAttachment); } void Framebuffer::Resize(uint32_t width, uint32_t height) @@ -43,7 +42,7 @@ namespace Copium this->height = height; for (auto&& framebuffer : framebuffers) vkDestroyFramebuffer(Vulkan::GetDevice(), framebuffer, nullptr); - AssetManager::GetAsset(colorAttachment).Resize(width, height); + colorAttachment.GetAsset().Resize(width, height); depthAttachment->Resize(width, height); InitializeFramebuffers(); } @@ -96,7 +95,7 @@ namespace Copium const ColorAttachment& Framebuffer::GetColorAttachment() const { - return AssetManager::GetAsset(colorAttachment); + return colorAttachment.GetAsset(); } uint32_t Framebuffer::GetWidth() const @@ -186,7 +185,7 @@ namespace Copium void Framebuffer::InitializeFramebuffers() { framebuffers.resize(SwapChain::MAX_FRAMES_IN_FLIGHT); - const ColorAttachment& attachment = AssetManager::GetAsset(colorAttachment); + const ColorAttachment& attachment = colorAttachment.GetAsset(); for (size_t i = 0; i < framebuffers.size(); ++i) { std::vector attachments{attachment.GetImageView(i), depthAttachment->GetImageView()}; diff --git a/CopiumEngine/src/copium/buffer/Framebuffer.h b/CopiumEngine/src/copium/buffer/Framebuffer.h index 1a5f36f..92cb9b2 100644 --- a/CopiumEngine/src/copium/buffer/Framebuffer.h +++ b/CopiumEngine/src/copium/buffer/Framebuffer.h @@ -2,6 +2,7 @@ #include "copium/asset/Asset.h" #include "copium/asset/AssetMeta.h" +#include "copium/asset/AssetRef.h" #include "copium/buffer/CommandBuffer.h" #include "copium/sampler/ColorAttachment.h" #include "copium/sampler/DepthAttachment.h" @@ -15,7 +16,7 @@ namespace Copium { CP_DELETE_COPY_AND_MOVE_CTOR(Framebuffer); private: - AssetHandle colorAttachment; + AssetRef colorAttachment; std::unique_ptr depthAttachment; std::vector framebuffers; VkRenderPass renderPass; diff --git a/CopiumEngine/src/copium/core/Vulkan.cpp b/CopiumEngine/src/copium/core/Vulkan.cpp index 65b9a4e..1dd02c8 100644 --- a/CopiumEngine/src/copium/core/Vulkan.cpp +++ b/CopiumEngine/src/copium/core/Vulkan.cpp @@ -1,11 +1,11 @@ #include "copium/core/Vulkan.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/Font.h" -#include "copium/pipeline/Pipeline.h" -#include "copium/buffer/Framebuffer.h" +#include "copium/sampler/Texture2D.h" #include "copium/util/Timer.h" namespace Copium @@ -15,7 +15,7 @@ namespace Copium std::unique_ptr Vulkan::device; std::unique_ptr Vulkan::swapChain; std::unique_ptr Vulkan::imGuiInstance; - AssetHandle Vulkan::emptyTexture2D; + AssetHandle Vulkan::emptyTexture2D; void Vulkan::Initialize() { @@ -38,13 +38,13 @@ namespace Copium // 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) AssetManager::RegisterAssetDir("assets/"); - emptyTexture2D = AssetManager::RegisterRuntimeAsset("empty_texture2d", std::make_unique(std::vector{255, 0, 255, 255}, 1, 1, SamplerCreator{})); + emptyTexture2D = AssetHandle{"empty_texture2d", std::make_unique(std::vector{255, 0, 255, 255}, 1, 1, SamplerCreator{})}; CP_INFO("Initialized AssetManager in %f seconds", timer.Elapsed()); } void Vulkan::Destroy() { - AssetManager::UnloadAsset(emptyTexture2D); + emptyTexture2D.UnloadAsset(); AssetManager::UnregisterAssetDir("assets/"); AssetManager::Cleanup(); imGuiInstance.reset(); @@ -79,7 +79,7 @@ namespace Copium return *imGuiInstance; } - AssetHandle Vulkan::GetEmptyTexture2D() + AssetHandle Vulkan::GetEmptyTexture2D() { return emptyTexture2D; } diff --git a/CopiumEngine/src/copium/core/Vulkan.h b/CopiumEngine/src/copium/core/Vulkan.h index ff06bfb..87c5e2c 100644 --- a/CopiumEngine/src/copium/core/Vulkan.h +++ b/CopiumEngine/src/copium/core/Vulkan.h @@ -1,11 +1,13 @@ #pragma once +#include "copium/asset/AssetHandle.h" #include "copium/core/Device.h" #include "copium/core/Instance.h" #include "copium/core/SwapChain.h" #include "copium/core/Window.h" #include "copium/core/ImGuiInstance.h" #include "copium/util/Common.h" +#include "copium/sampler/Texture2D.h" #include @@ -21,7 +23,7 @@ namespace Copium static std::unique_ptr swapChain; static std::unique_ptr imGuiInstance; - static AssetHandle emptyTexture2D; + static AssetHandle emptyTexture2D; public: static void Initialize(); static void Destroy(); @@ -31,6 +33,6 @@ namespace Copium static SwapChain& GetSwapChain(); static ImGuiInstance& GetImGuiInstance(); static bool Valid(); - static AssetHandle GetEmptyTexture2D(); + static AssetHandle GetEmptyTexture2D(); }; } \ No newline at end of file diff --git a/CopiumEngine/src/copium/pipeline/Pipeline.cpp b/CopiumEngine/src/copium/pipeline/Pipeline.cpp index c316637..a1b7f4e 100644 --- a/CopiumEngine/src/copium/pipeline/Pipeline.cpp +++ b/CopiumEngine/src/copium/pipeline/Pipeline.cpp @@ -18,9 +18,9 @@ namespace Copium VkRenderPass renderPass; if (metaFileClass.HasValue("framebuffer-uuid")) { - Framebuffer& fb = AssetManager::LoadAsset(Uuid{metaFileClass.GetValue("framebuffer-uuid")}); + framebuffer = AssetRef(Uuid{metaFileClass.GetValue("framebuffer-uuid")}); + Framebuffer& fb = framebuffer.GetAsset(); renderPass = fb.GetRenderPass(); - framebuffer = fb; } else { @@ -62,10 +62,6 @@ namespace Copium { vkDestroyDescriptorSetLayout(Vulkan::GetDevice(), descriptorSetLayout, nullptr); } - if (framebuffer != NULL_ASSET_HANDLE) - { - AssetManager::UnloadAsset(framebuffer); - } } void Pipeline::Bind(const CommandBuffer& commandBuffer) diff --git a/CopiumEngine/src/copium/pipeline/Pipeline.h b/CopiumEngine/src/copium/pipeline/Pipeline.h index 8d11b88..e588ff2 100644 --- a/CopiumEngine/src/copium/pipeline/Pipeline.h +++ b/CopiumEngine/src/copium/pipeline/Pipeline.h @@ -1,7 +1,9 @@ #pragma once #include "copium/asset/Asset.h" +#include "copium/asset/AssetRef.h" #include "copium/buffer/CommandBuffer.h" +#include "copium/buffer/Framebuffer.h" #include "copium/pipeline/DescriptorSet.h" #include "copium/pipeline/PipelineCreator.h" #include "copium/util/Common.h" @@ -20,7 +22,7 @@ namespace Copium std::vector boundDescriptorSets; VkPipelineLayout pipelineLayout; VkPipeline graphicsPipeline; - AssetHandle framebuffer; + AssetRef framebuffer; public: diff --git a/CopiumEngine/src/copium/renderer/Batch.cpp b/CopiumEngine/src/copium/renderer/Batch.cpp index c11446d..558579c 100644 --- a/CopiumEngine/src/copium/renderer/Batch.cpp +++ b/CopiumEngine/src/copium/renderer/Batch.cpp @@ -5,9 +5,9 @@ namespace Copium { - Batch::Batch(AssetHandle pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector samplers) + Batch::Batch(AssetRef& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector samplers) : vertexBuffer{RendererVertex::GetDescriptor(), vertexCount}, - descriptorSet{AssetManager::GetAsset(pipeline).CreateDescriptorSet(descriptorPool, 0)} + descriptorSet{pipeline.GetAsset().CreateDescriptorSet(descriptorPool, 0)} {} RendererVertexBuffer& Batch::GetVertexBuffer() diff --git a/CopiumEngine/src/copium/renderer/Batch.h b/CopiumEngine/src/copium/renderer/Batch.h index 7104d9c..a3e87b5 100644 --- a/CopiumEngine/src/copium/renderer/Batch.h +++ b/CopiumEngine/src/copium/renderer/Batch.h @@ -1,6 +1,7 @@ #pragma once #include "copium/asset/AssetMeta.h" +#include "copium/asset/AssetRef.h" #include "copium/buffer/RendererVertexBuffer.h" #include "copium/pipeline/DescriptorSet.h" #include "copium/pipeline/Pipeline.h" @@ -15,7 +16,7 @@ namespace Copium RendererVertexBuffer vertexBuffer; std::unique_ptr descriptorSet; public: - Batch(AssetHandle pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector samplers); + Batch(AssetRef& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector samplers); RendererVertexBuffer& GetVertexBuffer(); DescriptorSet& GetDescriptorSet(); }; diff --git a/CopiumEngine/src/copium/renderer/Renderer.cpp b/CopiumEngine/src/copium/renderer/Renderer.cpp index 8c7832c..1fd6c93 100644 --- a/CopiumEngine/src/copium/renderer/Renderer.cpp +++ b/CopiumEngine/src/copium/renderer/Renderer.cpp @@ -15,15 +15,14 @@ namespace Copium Renderer::Renderer() : descriptorPool{}, ibo{MAX_NUM_INDICES}, - samplers{MAX_NUM_TEXTURES, &AssetManager::GetAsset(Vulkan::GetEmptyTexture2D())} + pipeline{"renderer.meta"}, // TODO: should be a runtime renderer pipeline or passed in constructor + samplers{MAX_NUM_TEXTURES, &Vulkan::GetEmptyTexture2D().GetAsset()} { InitializeIndexBuffer(); - InitializeGraphicsPipeline(); } Renderer::~Renderer() { - AssetManager::UnloadAsset(pipeline); } 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) { - AssetManager::GetAsset(pipeline).Bind(commandBuffer); + pipeline.GetAsset().Bind(commandBuffer); ibo.Bind(commandBuffer); batchIndex = -1; NextBatch(); @@ -108,12 +107,12 @@ namespace Copium Pipeline& Renderer::GetGraphicsPipeline() { - return AssetManager::GetAsset(pipeline); + return pipeline.GetAsset(); } void Renderer::SetDescriptorSet(const DescriptorSet& descriptorSet) { - AssetManager::GetAsset(pipeline).SetDescriptorSet(descriptorSet); + pipeline.GetAsset().SetDescriptorSet(descriptorSet); } void Renderer::InitializeIndexBuffer() @@ -134,11 +133,6 @@ namespace Copium ibo.UpdateStaging(indices.data()); } - void Renderer::InitializeGraphicsPipeline() - { - pipeline = AssetManager::LoadAsset("renderer.meta"); - } - int Renderer::AllocateSampler(const Sampler& sampler) { for (size_t i = 0; i < textureCount; i++) @@ -174,15 +168,16 @@ namespace Copium { batches[batchIndex]->GetVertexBuffer().Unmap(); batches[batchIndex]->GetVertexBuffer().Bind(*currentCommandBuffer); - AssetManager::GetAsset(pipeline).SetDescriptorSet(batches[batchIndex]->GetDescriptorSet()); - AssetManager::GetAsset(pipeline).BindDescriptorSets(*currentCommandBuffer); + Pipeline& p = pipeline.GetAsset(); + p.SetDescriptorSet(batches[batchIndex]->GetDescriptorSet()); + p.BindDescriptorSets(*currentCommandBuffer); ibo.Draw(*currentCommandBuffer, quadCount * 6); } void Renderer::NextBatch() { batchIndex++; - std::fill(samplers.begin(), samplers.end(), &AssetManager::GetAsset(Vulkan::GetEmptyTexture2D())); + std::fill(samplers.begin(), samplers.end(), &Vulkan::GetEmptyTexture2D().GetAsset()); if (batchIndex >= batches.size()) { batches.emplace_back(std::make_unique(pipeline, descriptorPool, MAX_NUM_VERTICES, samplers)); diff --git a/CopiumEngine/src/copium/renderer/Renderer.h b/CopiumEngine/src/copium/renderer/Renderer.h index 64cb3d9..bdbc327 100644 --- a/CopiumEngine/src/copium/renderer/Renderer.h +++ b/CopiumEngine/src/copium/renderer/Renderer.h @@ -1,6 +1,7 @@ #pragma once #include "copium/asset/AssetMeta.h" +#include "copium/asset/AssetRef.h" #include "copium/buffer/CommandBuffer.h" #include "copium/buffer/IndexBuffer.h" #include "copium/buffer/RendererVertexBuffer.h" @@ -21,7 +22,7 @@ namespace Copium private: DescriptorPool descriptorPool; IndexBuffer ibo; - AssetHandle pipeline; + AssetRef pipeline; std::vector> batches; // Temporary data during a render @@ -47,7 +48,6 @@ namespace Copium void SetDescriptorSet(const DescriptorSet& descriptorSet); private: void InitializeIndexBuffer(); - void InitializeGraphicsPipeline(); int AllocateSampler(const Sampler& sampler); void AllocateQuad(); diff --git a/CopiumEngine/src/copium/sampler/Font.h b/CopiumEngine/src/copium/sampler/Font.h index 1360a6c..026648f 100644 --- a/CopiumEngine/src/copium/sampler/Font.h +++ b/CopiumEngine/src/copium/sampler/Font.h @@ -28,6 +28,7 @@ namespace Copium float GetBaseHeight() const; BoundingBox GetTextBoundingBox(const std::string& str, float size) const; + private: void InitializeTextureImageFromData(const uint8_t* rgbaData, int width, int height); }; diff --git a/CopiumEngine/src/copium/sampler/Texture2D.h b/CopiumEngine/src/copium/sampler/Texture2D.h index 29d2dbc..eff509c 100644 --- a/CopiumEngine/src/copium/sampler/Texture2D.h +++ b/CopiumEngine/src/copium/sampler/Texture2D.h @@ -18,6 +18,7 @@ namespace Copium ~Texture2D() override; VkDescriptorImageInfo GetDescriptorImageInfo(int index) const override; + private: void InitializeTextureImageFromFile(const std::string& filename); void InitializeTextureImageFromData(const uint8_t* rgbaData, int width, int height);