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
This commit is contained in:
Thraix
2025-08-19 20:14:04 +02:00
parent 3c4365083f
commit 71e02f3015
5 changed files with 61 additions and 27 deletions
@@ -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)
Asset& AssetManager::RegisterRuntimeAsset(const std::string& name, const Uuid& uuid, std::unique_ptr<Asset>&& 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;
+15 -3
View File
@@ -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>&& asset);
static Asset& RegisterRuntimeAsset(const std::string& name, const Uuid& uuid, std::unique_ptr<Asset>&& asset);
static const std::vector<AssetFile>& GetAssetFiles();
static void Cleanup();
@@ -70,13 +70,25 @@ namespace Copium
}
template <typename AssetT>
static AssetT& RegisterRuntimeAsset(const std::string& name, std::unique_ptr<AssetT>&& assetT)
static AssetT& RegisterRuntimeAsset(const std::string& name, const Uuid& uuid, std::unique_ptr<AssetT>&& assetT)
{
AssetT* ptr = assetT.release();
Asset& asset = RegisterRuntimeAsset(name, std::unique_ptr<Asset>((Asset*)ptr));
Asset& asset = RegisterRuntimeAsset(name, uuid, std::unique_ptr<Asset>((Asset*)ptr));
return *(AssetT*)&asset;
}
template <typename AssetT>
static AssetT& RegisterRuntimeAsset(const std::string& name, std::unique_ptr<AssetT>&& assetT)
{
return RegisterRuntimeAsset(name, Uuid{}, std::move(assetT));
}
template <typename AssetT>
static AssetT& RegisterRuntimeAsset(const Uuid& uuid, std::unique_ptr<AssetT>&& assetT)
{
return RegisterRuntimeAsset(uuid.ToString(), uuid, std::move(assetT));
}
private:
static Asset& LoadAssetFromPath(const std::string& filepath);
+15 -3
View File
@@ -12,22 +12,34 @@ namespace Copium
: id{std::shared_ptr<AssetId>(new AssetId(NULL_ASSET_ID), AssetIdUnloader{})}
{}
AssetRef(const std::string& assetName)
explicit AssetRef(const std::string& assetName)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::LoadAsset(assetName).GetId()), AssetIdUnloader{})}
{}
AssetRef(const Uuid& uuid)
explicit AssetRef(const Uuid& uuid)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::LoadAsset(uuid).GetId()), AssetIdUnloader{})}
{}
AssetRef(AssetType& asset)
explicit AssetRef(AssetType& asset)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::DuplicateAsset(asset.GetId())), AssetIdUnloader{})}
{}
explicit AssetRef(std::unique_ptr<AssetType>&& runtimeAsset)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::RegisterRuntimeAsset(Uuid{}, std::move(runtimeAsset)).GetId()), AssetIdUnloader{})}
{}
AssetRef(const Uuid& uuid, std::unique_ptr<AssetType>&& runtimeAsset)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::RegisterRuntimeAsset(uuid, std::move(runtimeAsset)).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(const std::string& name, const Uuid& uuid, std::unique_ptr<AssetType>&& runtimeAsset)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::RegisterRuntimeAsset(name, uuid, std::move(runtimeAsset)).GetId()), AssetIdUnloader{})}
{}
AssetRef(AssetId id)
: id{std::shared_ptr<AssetId>(new AssetId(AssetManager::DuplicateAsset(id)), AssetIdUnloader{})}
{}
+21 -11
View File
@@ -12,17 +12,24 @@ namespace Copium
const MetaFileClass& metaClass = metaFile.GetMetaClass("Framebuffer");
colorAttachment = AssetRef<ColorAttachment>(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<uint32_t>(width), static_cast<uint32_t>(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<uint32_t>(width), static_cast<uint32_t>(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<ColorAttachment>(width, height, SamplerCreator{}));
colorAttachment = AssetRef(std::make_unique<ColorAttachment>(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");
}
}
}
}
+7 -7
View File
@@ -20,25 +20,25 @@ namespace Copium
std::vector<VkFramebuffer> 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();