From 71e02f3015d7153974955374365a9339ecfa884a Mon Sep 17 00:00:00 2001 From: Thraix Date: Tue, 19 Aug 2025 20:14:04 +0200 Subject: [PATCH] Fix multiple creation of runtime framebuffers - Framebuffer names are now generated based on Uuids, as such we can create multiple runtime Framebuffers - Framebuffers now uses ints instead of uint32_t, to avoid issues where we do "-framebuffer.GetWidth()", which will return an odd uint32_t value - Add some additional AssetRef constructors --- .../src/copium/asset/AssetManager.cpp | 6 ++-- CopiumEngine/src/copium/asset/AssetManager.h | 18 +++++++++-- CopiumEngine/src/copium/asset/AssetRef.h | 18 +++++++++-- .../src/copium/buffer/Framebuffer.cpp | 32 ++++++++++++------- CopiumEngine/src/copium/buffer/Framebuffer.h | 14 ++++---- 5 files changed, 61 insertions(+), 27 deletions(-) diff --git a/CopiumEngine/src/copium/asset/AssetManager.cpp b/CopiumEngine/src/copium/asset/AssetManager.cpp index d3712f4..46ce986 100644 --- a/CopiumEngine/src/copium/asset/AssetManager.cpp +++ b/CopiumEngine/src/copium/asset/AssetManager.cpp @@ -146,18 +146,18 @@ namespace Copium CP_ASSERT(pathToAssetCache.empty(), "Path To Asset Cache not empty after full unload"); } - Asset& AssetManager::RegisterRuntimeAsset(const std::string& name, std::unique_ptr&& asset) + Asset& AssetManager::RegisterRuntimeAsset(const std::string& name, const Uuid& uuid, std::unique_ptr&& asset) { CP_DEBUG("Registering Runtime Asset: %s", name.c_str()); 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.c_str()); 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.uuid = uuid; asset2->metaData.isRuntime = true; nameToAssetCache.emplace(name, id); return *asset2; diff --git a/CopiumEngine/src/copium/asset/AssetManager.h b/CopiumEngine/src/copium/asset/AssetManager.h index 8155560..e9bad4b 100644 --- a/CopiumEngine/src/copium/asset/AssetManager.h +++ b/CopiumEngine/src/copium/asset/AssetManager.h @@ -33,7 +33,7 @@ namespace Copium static Asset& LoadAsset(const Uuid& uuid); static AssetId DuplicateAsset(AssetId id); static void UnloadAsset(AssetId id); - static Asset& RegisterRuntimeAsset(const std::string& name, std::unique_ptr&& asset); + static Asset& RegisterRuntimeAsset(const std::string& name, const Uuid& uuid, std::unique_ptr&& asset); static const std::vector& GetAssetFiles(); static void Cleanup(); @@ -70,13 +70,25 @@ namespace Copium } template - static AssetT& RegisterRuntimeAsset(const std::string& name, std::unique_ptr&& assetT) + static AssetT& RegisterRuntimeAsset(const std::string& name, const Uuid& uuid, std::unique_ptr&& assetT) { AssetT* ptr = assetT.release(); - Asset& asset = RegisterRuntimeAsset(name, std::unique_ptr((Asset*)ptr)); + Asset& asset = RegisterRuntimeAsset(name, uuid, std::unique_ptr((Asset*)ptr)); return *(AssetT*)&asset; } + template + static AssetT& RegisterRuntimeAsset(const std::string& name, std::unique_ptr&& assetT) + { + return RegisterRuntimeAsset(name, Uuid{}, std::move(assetT)); + } + + template + static AssetT& RegisterRuntimeAsset(const Uuid& uuid, std::unique_ptr&& assetT) + { + return RegisterRuntimeAsset(uuid.ToString(), uuid, std::move(assetT)); + } + private: static Asset& LoadAssetFromPath(const std::string& filepath); diff --git a/CopiumEngine/src/copium/asset/AssetRef.h b/CopiumEngine/src/copium/asset/AssetRef.h index 678e159..22a7598 100644 --- a/CopiumEngine/src/copium/asset/AssetRef.h +++ b/CopiumEngine/src/copium/asset/AssetRef.h @@ -12,22 +12,34 @@ namespace Copium : id{std::shared_ptr(new AssetId(NULL_ASSET_ID), AssetIdUnloader{})} {} - AssetRef(const std::string& assetName) + explicit AssetRef(const std::string& assetName) : id{std::shared_ptr(new AssetId(AssetManager::LoadAsset(assetName).GetId()), AssetIdUnloader{})} {} - AssetRef(const Uuid& uuid) + explicit AssetRef(const Uuid& uuid) : id{std::shared_ptr(new AssetId(AssetManager::LoadAsset(uuid).GetId()), AssetIdUnloader{})} {} - AssetRef(AssetType& asset) + explicit AssetRef(AssetType& asset) : id{std::shared_ptr(new AssetId(AssetManager::DuplicateAsset(asset.GetId())), AssetIdUnloader{})} {} + explicit AssetRef(std::unique_ptr&& runtimeAsset) + : id{std::shared_ptr(new AssetId(AssetManager::RegisterRuntimeAsset(Uuid{}, std::move(runtimeAsset)).GetId()), AssetIdUnloader{})} + {} + + AssetRef(const Uuid& uuid, std::unique_ptr&& runtimeAsset) + : id{std::shared_ptr(new AssetId(AssetManager::RegisterRuntimeAsset(uuid, std::move(runtimeAsset)).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(const std::string& name, const Uuid& uuid, std::unique_ptr&& runtimeAsset) + : id{std::shared_ptr(new AssetId(AssetManager::RegisterRuntimeAsset(name, uuid, std::move(runtimeAsset)).GetId()), AssetIdUnloader{})} + {} + AssetRef(AssetId id) : id{std::shared_ptr(new AssetId(AssetManager::DuplicateAsset(id)), AssetIdUnloader{})} {} diff --git a/CopiumEngine/src/copium/buffer/Framebuffer.cpp b/CopiumEngine/src/copium/buffer/Framebuffer.cpp index fdbae99..bdd7e37 100644 --- a/CopiumEngine/src/copium/buffer/Framebuffer.cpp +++ b/CopiumEngine/src/copium/buffer/Framebuffer.cpp @@ -12,17 +12,24 @@ namespace Copium const MetaFileClass& metaClass = metaFile.GetMetaClass("Framebuffer"); colorAttachment = AssetRef(Uuid{metaClass.GetValue("rendertexture-uuid")}); ColorAttachment& attachment = colorAttachment.GetAsset(); + width = attachment.GetWidth(); height = attachment.GetHeight(); + CP_ASSERT(width > 0, "Width of framebuffer is less than 1: %d", width); + CP_ASSERT(height > 0, "Height of framebuffer is less than 1: %d", height); + InitializeDepthBuffer(); InitializeRenderPass(); InitializeFramebuffers(); } - Framebuffer::Framebuffer(uint32_t width, uint32_t height) + Framebuffer::Framebuffer(int width, int height, const SamplerCreator& samplerCreator) : width{width}, height{height} { - InitializeImage(); + CP_ASSERT(width > 0, "Width of framebuffer is less than 1: %d", width); + CP_ASSERT(height > 0, "Height of framebuffer is less than 1: %d", height); + + InitializeImage(samplerCreator); InitializeDepthBuffer(); InitializeRenderPass(); InitializeFramebuffers(); @@ -39,8 +46,11 @@ namespace Copium }); } - void Framebuffer::Resize(uint32_t width, uint32_t height) + void Framebuffer::Resize(int width, int height) { + CP_ASSERT(width > 0, "Width of framebuffer is less than 1: %d", width); + CP_ASSERT(height > 0, "Height of framebuffer is less than 1: %d", height); + Vulkan::GetDevice().WaitIdle(); this->width = width; this->height = height; @@ -61,9 +71,9 @@ namespace Copium renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; renderPassBeginInfo.renderPass = renderPass; renderPassBeginInfo.framebuffer = framebuffers[Vulkan::GetSwapChain().GetFlightIndex()]; -; + renderPassBeginInfo.renderArea.offset = {0, 0}; - renderPassBeginInfo.renderArea.extent = {width, height}; + renderPassBeginInfo.renderArea.extent = {static_cast(width), static_cast(height)}; renderPassBeginInfo.clearValueCount = clearValues.size(); renderPassBeginInfo.pClearValues = clearValues.data(); vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); @@ -78,7 +88,7 @@ namespace Copium vkCmdSetViewport(commandBuffer, 0, 1, &viewport); VkRect2D scissor{}; scissor.offset = {0, 0}; - scissor.extent = {width, height}; + scissor.extent = {static_cast(width), static_cast(height)}; vkCmdSetScissor(commandBuffer, 0, 1, &scissor); } @@ -102,19 +112,19 @@ namespace Copium return colorAttachment.GetAsset(); } - uint32_t Framebuffer::GetWidth() const + int Framebuffer::GetWidth() const { return width; } - uint32_t Framebuffer::GetHeight() const + int Framebuffer::GetHeight() const { return height; } - void Framebuffer::InitializeImage() + void Framebuffer::InitializeImage(const SamplerCreator& samplerCreator) { - colorAttachment = AssetManager::RegisterRuntimeAsset("Framebuffer::ColorAttachment", std::make_unique(width, height, SamplerCreator{})); + colorAttachment = AssetRef(std::make_unique(width, height, samplerCreator)); } void Framebuffer::InitializeDepthBuffer() @@ -206,4 +216,4 @@ namespace Copium CP_VK_ASSERT(vkCreateFramebuffer(Vulkan::GetDevice(), &createInfo, nullptr, &framebuffers[i]), "Failed to initialize framebuffer"); } } -} \ No newline at end of file +} diff --git a/CopiumEngine/src/copium/buffer/Framebuffer.h b/CopiumEngine/src/copium/buffer/Framebuffer.h index c3b874b..9718b2d 100644 --- a/CopiumEngine/src/copium/buffer/Framebuffer.h +++ b/CopiumEngine/src/copium/buffer/Framebuffer.h @@ -20,25 +20,25 @@ namespace Copium std::vector framebuffers; VkRenderPass renderPass; - uint32_t width; - uint32_t height; + int width; + int height; public: Framebuffer(const MetaFile& metaFile); - Framebuffer(uint32_t width, uint32_t height); + Framebuffer(int width, int height, const SamplerCreator& samplerCreator); ~Framebuffer(); - void Resize(uint32_t width, uint32_t height); + void Resize(int width, int height); void Bind(const CommandBuffer& commandBuffer); void Unbind(const CommandBuffer& commandBuffer); VkRenderPass GetRenderPass() const; VkFramebuffer GetFramebuffer() const; const ColorAttachment& GetColorAttachment() const; - uint32_t GetWidth() const; - uint32_t GetHeight() const; + int GetWidth() const; + int GetHeight() const; private: - void InitializeImage(); + void InitializeImage(const SamplerCreator& samplerCreator); void InitializeDepthBuffer(); void InitializeRenderPass(); void InitializeFramebuffers();