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\AssetFile.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\Buffer.cpp" />
<ClCompile Include="src\copium\core\Device.cpp" />
@@ -242,6 +241,7 @@
<ItemGroup>
<ClInclude Include="src\copium\asset\Asset.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\AssetMeta.h" />
<ClInclude Include="src\copium\asset\AssetRef.h" />
+3 -3
View File
@@ -192,9 +192,6 @@
<ClCompile Include="src\copium\ecs\Entity.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\copium\asset\AssetRef.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\copium\ecs\SystemPool.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@@ -467,5 +464,8 @@
<ClInclude Include="src\copium\event\ViewportResize.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\copium\asset\AssetHandle.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
+3 -8
View File
@@ -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;
}
}
+2 -4
View File
@@ -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;
@@ -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::map<std::string, AssetManager::CreateAssetFunc> AssetManager::assetTypes;
std::map<AssetHandle, std::unique_ptr<Asset>> AssetManager::assets;
std::map<std::string, AssetHandle> AssetManager::pathToAssetCache;
std::map<std::string, AssetHandle> AssetManager::nameToAssetCache;
std::map<AssetId, std::unique_ptr<Asset>> AssetManager::assets;
std::map<std::string, AssetId> AssetManager::pathToAssetCache;
std::map<std::string, AssetId> AssetManager::nameToAssetCache;
std::vector<AssetFile> 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;
}
+14 -14
View File
@@ -17,22 +17,22 @@ namespace Copium
using CreateAssetFunc = std::function<Asset&(const MetaFile& metaFile, const std::string& str)>;
static std::map<std::string, CreateAssetFunc> assetTypes;
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, AssetHandle> nameToAssetCache;
static AssetHandle assetHandle;
static AssetHandle runtimeAssetHandle;
static std::map<std::string, AssetId> pathToAssetCache;
static std::map<std::string, AssetId> nameToAssetCache;
static AssetId assetId;
static AssetId runtimeAssetId;
static std::vector<AssetFile> 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>&& asset);
static const std::vector<AssetFile>& GetAssetFiles();
static void Cleanup();
@@ -61,9 +61,9 @@ namespace Copium
}
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);
CP_ASSERT(assetT, "Invalid Asset cast");
return *assetT;
@@ -83,10 +83,10 @@ namespace Copium
template <typename T>
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<T>(metaFile)).first->second.get();
asset.metaData.handle = handle;
AssetId id = assetId++;
pathToAssetCache.emplace(metaFile.GetFilePath(), id);
Asset& asset = *assets.emplace(id, std::make_unique<T>(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;
+3 -3
View File
@@ -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;
@@ -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
#include "copium/asset/AssetMeta.h"
#include "copium/asset/AssetManager.h"
namespace Copium
{
template <typename AssetType>
class AssetRef
{
private:
std::shared_ptr<AssetHandle> handle;
public:
AssetRef();
AssetRef(AssetHandle handle);
AssetRef()
: 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)
{
const MetaFileClass& metaClass = metaFile.GetMetaClass("Framebuffer");
ColorAttachment& attachment = AssetManager::LoadAsset<ColorAttachment>(Uuid{metaClass.GetValue("rendertexture-uuid")});
colorAttachment = attachment;
colorAttachment = AssetRef<ColorAttachment>(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>(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>(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>(colorAttachment);
const ColorAttachment& attachment = colorAttachment.GetAsset();
for (size_t i = 0; i < framebuffers.size(); ++i)
{
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/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> colorAttachment;
std::unique_ptr<DepthAttachment> depthAttachment;
std::vector<VkFramebuffer> framebuffers;
VkRenderPass renderPass;
+7 -7
View File
@@ -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<Device> Vulkan::device;
std::unique_ptr<SwapChain> Vulkan::swapChain;
std::unique_ptr<ImGuiInstance> Vulkan::imGuiInstance;
AssetHandle Vulkan::emptyTexture2D;
AssetHandle<Texture2D> 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<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());
}
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<Texture2D> Vulkan::GetEmptyTexture2D()
{
return emptyTexture2D;
}
+4 -2
View File
@@ -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 <memory>
@@ -21,7 +23,7 @@ namespace Copium
static std::unique_ptr<SwapChain> swapChain;
static std::unique_ptr<ImGuiInstance> imGuiInstance;
static AssetHandle emptyTexture2D;
static AssetHandle<Texture2D> 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<Texture2D> GetEmptyTexture2D();
};
}
@@ -18,9 +18,9 @@ namespace Copium
VkRenderPass renderPass;
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();
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)
+3 -1
View File
@@ -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<VkDescriptorSet> boundDescriptorSets;
VkPipelineLayout pipelineLayout;
VkPipeline graphicsPipeline;
AssetHandle framebuffer;
AssetRef<Framebuffer> framebuffer;
public:
+2 -2
View File
@@ -5,9 +5,9 @@
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},
descriptorSet{AssetManager::GetAsset<Pipeline>(pipeline).CreateDescriptorSet(descriptorPool, 0)}
descriptorSet{pipeline.GetAsset().CreateDescriptorSet(descriptorPool, 0)}
{}
RendererVertexBuffer& Batch::GetVertexBuffer()
+2 -1
View File
@@ -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> descriptorSet;
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();
DescriptorSet& GetDescriptorSet();
};
+9 -14
View File
@@ -15,15 +15,14 @@ namespace Copium
Renderer::Renderer()
: descriptorPool{},
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();
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>(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>(pipeline);
return pipeline.GetAsset();
}
void Renderer::SetDescriptorSet(const DescriptorSet& descriptorSet)
{
AssetManager::GetAsset<Pipeline>(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<Pipeline>("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>(pipeline).SetDescriptorSet(batches[batchIndex]->GetDescriptorSet());
AssetManager::GetAsset<Pipeline>(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<Texture2D>(Vulkan::GetEmptyTexture2D()));
std::fill(samplers.begin(), samplers.end(), &Vulkan::GetEmptyTexture2D().GetAsset());
if (batchIndex >= batches.size())
{
batches.emplace_back(std::make_unique<Batch>(pipeline, descriptorPool, MAX_NUM_VERTICES, samplers));
+2 -2
View File
@@ -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> pipeline;
std::vector<std::unique_ptr<Batch>> 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();
+1
View File
@@ -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);
};
@@ -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);