Add additional Asset types

- Add Framebuffer Asset
- Add Pipeline Asset
- Add RenderTexture Asset
This commit is contained in:
Thraix
2023-04-22 21:00:34 +02:00
parent 0246e89039
commit 1731bb1dd5
34 changed files with 322 additions and 171 deletions
-10
View File
@@ -261,16 +261,6 @@
<ClInclude Include="src\copium\util\VulkanException.h" />
<ClInclude Include="src\copium\mesh\VertexPassthrough.h" />
</ItemGroup>
<ItemGroup>
<None Include="assets\fox.meta" />
<None Include="assets\fox2.meta" />
<None Include="res\shaders\passthrough.frag" />
<None Include="res\shaders\passthrough.vert" />
<None Include="res\shaders\renderer.frag" />
<None Include="res\shaders\renderer.vert" />
<None Include="res\shaders\shader.frag" />
<None Include="res\shaders\shader.vert" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
-10
View File
@@ -294,14 +294,4 @@
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="res\shaders\shader.frag" />
<None Include="res\shaders\shader.vert" />
<None Include="res\shaders\passthrough.frag" />
<None Include="res\shaders\passthrough.vert" />
<None Include="res\shaders\renderer.frag" />
<None Include="res\shaders\renderer.vert" />
<None Include="assets\fox.meta" />
<None Include="assets\fox2.meta" />
</ItemGroup>
</Project>
+3
View File
@@ -0,0 +1,3 @@
[Framebuffer]
rendertexture-uuid=e59a549e-bb14-991c-0d95-a8e9351fa074
uuid=91455834-b62b-354e-ee3b-50f7dbe74d28
+5
View File
@@ -0,0 +1,5 @@
[Pipeline]
vert-filepath=res/shaders/passthrough.vert
frag-filepath=res/shaders/passthrough.frag
type=Passthrough
uuid=8fdcfe12-5c69-cf29-2f31-e177d6267e4e
+6
View File
@@ -0,0 +1,6 @@
[Pipeline]
vert-filepath=res/shaders/shader.vert
frag-filepath=res/shaders/shader.frag
framebuffer-uuid=91455834-b62b-354e-ee3b-50f7dbe74d28
type=Mesh
uuid=37082163-6f99-bded-1617-6220c84e3c2a
+6
View File
@@ -0,0 +1,6 @@
[Pipeline]
vert-filepath=res/shaders/renderer.vert
frag-filepath=res/shaders/renderer.frag
framebuffer-uuid=91455834-b62b-354e-ee3b-50f7dbe74d28
type=Renderer
uuid=59c56410-000b-70ca-1ae1-4568fb7856f7
+2
View File
@@ -0,0 +1,2 @@
[RenderTexture]
uuid=e59a549e-bb14-991c-0d95-a8e9351fa074
+1 -1
View File
@@ -6,6 +6,6 @@ layout(location = 0) out vec2 outTexCoord;
void main()
{
gl_Position = vec4(inPosition * 0.5, 0.999, 1.0);
gl_Position = vec4(inPosition, 0.0, 1.0);
outTexCoord = inPosition * 0.5 + 0.5;
}
+1 -10
View File
@@ -2,11 +2,7 @@
namespace Copium
{
Asset::Asset(AssetType type)
{
metaData.type = type;
}
Asset::Asset() = default;
Asset::~Asset() = default;
AssetHandle Asset::GetHandle() const
@@ -14,11 +10,6 @@ namespace Copium
return metaData.handle;
}
AssetType Asset::GetType() const
{
return metaData.type;
}
const std::string& Asset::GetName() const
{
return metaData.name;
+1 -2
View File
@@ -13,11 +13,10 @@ namespace Copium
{
friend class AssetManager;
public:
Asset(AssetType type);
Asset();
virtual ~Asset();
AssetHandle GetHandle() const;
AssetType GetType() const;
const std::string& GetName() const;
UUID GetUUID() const;
bool isRuntime() const;
+12 -7
View File
@@ -4,6 +4,8 @@
namespace Copium
{
std::vector<std::string> AssetFile::assetTypes;
AssetFile::AssetFile(const std::string& path)
: path{path}
{
@@ -17,17 +19,16 @@ namespace Copium
void AssetFile::Load()
{
const std::vector<std::pair<std::string, AssetType>> strToType{{"Texture2D", AssetType::Texture2D}};
MetaFile metaFile{path};
for (auto&& [str, type] : strToType)
for (auto&& assetType : assetTypes)
{
if (!metaFile.HasMetaClass(str))
if (!metaFile.HasMetaClass(assetType))
continue;
Load(metaFile, str, type);
Load(metaFile, assetType);
return;
}
CP_ABORT("Unknown Asset type");
CP_WARN("Unknown Asset type in file: %s", metaFile.GetFilePath().c_str());
}
const std::string& AssetFile::GetPath() const
@@ -40,11 +41,15 @@ namespace Copium
return uuid;
}
void AssetFile::Load(const MetaFile& metaFile, const std::string& className, AssetType assetType)
void AssetFile::Load(const MetaFile& metaFile, const std::string& className)
{
const MetaFileClass& metaClass = metaFile.GetMetaClass(className);
uuid = UUID{metaClass.GetValue("uuid")};
type = assetType;
dateModified = FileSystem::DateModified(path);
}
void AssetFile::RegisterAssetType(const std::string& assetType)
{
assetTypes.emplace_back(assetType);
}
}
+4 -2
View File
@@ -8,9 +8,10 @@ namespace Copium
{
class AssetFile
{
friend class AssetManager;
private:
static std::vector<std::string> assetTypes;
std::string path;
AssetType type;
UUID uuid;
int64_t dateModified;
@@ -23,6 +24,7 @@ namespace Copium
const std::string& GetPath() const;
UUID GetUUID() const;
private:
void Load(const MetaFile& metaFile, const std::string& className, AssetType assetType);
void Load(const MetaFile& metaFile, const std::string& className);
static void RegisterAssetType(const std::string& assetType);
};
}
+52 -42
View File
@@ -1,5 +1,7 @@
#include "copium/asset/AssetManager.h"
#include "copium/buffer/Framebuffer.h"
#include "copium/sampler/ColorAttachment.h"
#include "copium/sampler/Texture2D.h"
#include "copium/util/Common.h"
#include "copium/util/MetaFile.h"
@@ -10,6 +12,7 @@
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;
@@ -27,10 +30,17 @@ namespace Copium
if (std::filesystem::is_directory(it->path()))
continue;
std::filesystem::path assetDirPath{assetDir};
cachedAssetFiles.emplace_back(assetDir + "/" + std::filesystem::absolute(it->path()).string().substr(std::filesystem::absolute(assetDirPath).string().size()).c_str());
std::string assetPath = assetDir + "/" + std::filesystem::absolute(it->path()).string().substr(std::filesystem::absolute(assetDirPath).string().size() + 1).c_str();
try
{
CP_DEBUG("Registering Asset: %s", assetPath.c_str());
cachedAssetFiles.emplace_back(assetPath);
}
catch (RuntimeException& exception)
{
CP_ERR("Failed to register Asset: %s", assetPath.c_str());
}
}
UUID uuid{};
CP_INFO(uuid.ToString().c_str());
}
void AssetManager::UnregisterAssetDir(std::string assetDir)
@@ -62,20 +72,12 @@ namespace Copium
for (auto& dir : assetDirs)
{
std::string path = dir + "/" + assetPath;
auto it = pathToAssetCache.find(path);
if (it != pathToAssetCache.end())
return *assets.find(it->second)->second.get();
std::ifstream file{path};
if (!file.good())
continue;
file.close();
MetaFile metaFile{path};
if (metaFile.HasMetaClass("Texture2D"))
{
return CreateAsset<Texture2D>(metaFile, "Texture2D");
}
CP_ABORT("Unknown Asset type: %s/%s", dir.c_str(), assetPath.c_str());
return LoadAssetFromPath(path);
}
CP_ABORT("Unknown Asset: %s", assetPath.c_str());
}
@@ -86,26 +88,13 @@ namespace Copium
CP_DEBUG("Loading uuid Asset: %s", uuid.ToString().c_str());
for (auto&& assetFile : cachedAssetFiles)
{
if (assetFile.GetUUID() != uuid)
continue;
if (assetFile.NeedReload())
assetFile.Load();
if (assetFile.GetUUID() != uuid)
continue;
CP_DEBUG("Loading Asset: %s", assetFile.GetPath().c_str());
auto it = pathToAssetCache.find(assetFile.GetPath());
if (it != pathToAssetCache.end())
return *assets.find(it->second)->second.get();
MetaFile metaFile{assetFile.GetPath()};
if (metaFile.HasMetaClass("Texture2D"))
{
return CreateAsset<Texture2D>(metaFile, "Texture2D");
}
CP_ABORT("Unknown Asset type: %s", assetFile.GetPath().c_str());
return LoadAssetFromPath(assetFile.GetPath());
}
CP_ABORT("Asset not found with uuid=%s", uuid.ToString().c_str());
// TODO: Reload the assetCache to see if a new file has appeared with that uuid
@@ -114,7 +103,16 @@ namespace Copium
void AssetManager::UnloadAsset(AssetHandle handle)
{
auto it = assets.find(handle);
CP_ASSERT(it != assets.end(), "Asset not loaded");
if (it == assets.end())
{
CP_WARN("Asset not loaded");
return;
}
CP_DEBUG("Unloading Asset: %s", it->second->GetName().c_str());
it->second->metaData.loadCount--;
if (it->second->metaData.loadCount > 0)
return;
if (it->second->isRuntime())
nameToAssetCache.erase(it->second->GetName());
@@ -127,14 +125,19 @@ namespace Copium
{
if (assets.empty())
return;
CP_WARN("Cleaning up %d loaded assets", assets.size());
assets.clear();
nameToAssetCache.clear();
pathToAssetCache.clear();
CP_WARN("Performing auto clean up of %d non unloaded assets", assets.size());
while (!assets.empty())
{
UnloadAsset(assets.begin()->second->GetHandle());
}
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");
}
Asset& AssetManager::RegisterRuntimeAsset(const std::string& name, 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);
@@ -148,16 +151,23 @@ namespace Copium
return *asset2;
}
template <typename T>
Asset& AssetManager::CreateAsset(const MetaFile& metaFile, const std::string& metaFileClass)
Asset& AssetManager::LoadAssetFromPath(const std::string& filepath)
{
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;
asset.metaData.name = metaFile.GetFilePath();
asset.metaData.uuid = UUID{metaFile.GetMetaClass(metaFileClass).GetValue("uuid")};
asset.metaData.isRuntime = false;
return asset;
CP_DEBUG("Loading Asset: %s", filepath.c_str());
auto it = pathToAssetCache.find(filepath);
if (it != pathToAssetCache.end())
{
auto itAsset = assets.find(it->second);
itAsset->second->metaData.loadCount++;
return *itAsset->second.get();
}
MetaFile metaFile{filepath};
for (auto& assetType : assetTypes)
{
if(metaFile.HasMetaClass(assetType.first))
return assetType.second(metaFile, assetType.first);
}
CP_ABORT("Unknown Asset type: %s", filepath.c_str());
}
}
+23 -1
View File
@@ -4,6 +4,7 @@
#include "copium/asset/AssetFile.h"
#include "copium/util/Common.h"
#include <functional>
#include <map>
#include <vector>
@@ -13,6 +14,8 @@ namespace Copium
{
CP_STATIC_CLASS(AssetManager);
private:
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;
@@ -32,6 +35,13 @@ namespace Copium
static Asset& RegisterRuntimeAsset(const std::string& name, std::unique_ptr<Asset>&& asset);
static void Cleanup();
template <typename AssetType>
static void RegisterAssetType(const std::string& assetType)
{
CP_ASSERT(assetTypes.emplace(assetType, &AssetManager::CreateAsset<AssetType>).second, "Asset type already exists: %s", assetType.c_str());
AssetFile::RegisterAssetType(assetType);
}
template <typename AssetT>
static AssetT& LoadAsset(const std::string& assetPath)
{
@@ -66,7 +76,19 @@ namespace Copium
}
private:
static Asset& LoadAssetFromPath(const std::string& filepath);
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++;
pathToAssetCache.emplace(metaFile.GetFilePath(), handle);
Asset& asset = *assets.emplace(handle, std::make_unique<T>(metaFile)).first->second.get();
asset.metaData.handle = handle;
asset.metaData.name = metaFile.GetFilePath();
asset.metaData.uuid = UUID{metaFile.GetMetaClass(metaFileClass).GetValue("uuid")};
asset.metaData.isRuntime = false;
return asset;
}
};
}
+3 -8
View File
@@ -7,20 +7,15 @@
namespace Copium
{
enum class AssetType
{
Pipeline,
Texture2D,
Sound,
};
using AssetHandle = uint64_t;
static constexpr int NULL_ASSET_HANDLE = 0;
struct AssetMeta
{
AssetHandle handle;
AssetType type;
std::string name;
UUID uuid;
bool isRuntime;
bool isRuntime = false;
int loadCount = 1;
};
}
+20 -7
View File
@@ -1,11 +1,24 @@
#include "copium/buffer/Framebuffer.h"
#include "copium/asset/AssetManager.h"
#include "copium/buffer/CommandBuffer.h"
#include "copium/core/Vulkan.h"
#include "copium/sampler/Image.h"
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;
width = attachment.GetWidth();
height = attachment.GetHeight();
InitializeDepthBuffer();
InitializeRenderPass();
InitializeFramebuffers();
}
Framebuffer::Framebuffer(uint32_t width, uint32_t height)
: width{width}, height{height}
{
@@ -20,6 +33,7 @@ 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)
@@ -27,12 +41,10 @@ namespace Copium
vkDeviceWaitIdle(Vulkan::GetDevice());
this->width = width;
this->height = height;
colorAttachment.reset();
depthAttachment.reset();
for (auto&& framebuffer : framebuffers)
vkDestroyFramebuffer(Vulkan::GetDevice(), framebuffer, nullptr);
InitializeImage();
InitializeDepthBuffer();
AssetManager::GetAsset<ColorAttachment>(colorAttachment).Resize(width, height);
depthAttachment->Resize(width, height);
InitializeFramebuffers();
}
@@ -84,7 +96,7 @@ namespace Copium
const ColorAttachment& Framebuffer::GetColorAttachment() const
{
return *colorAttachment;
return AssetManager::GetAsset<ColorAttachment>(colorAttachment);
}
uint32_t Framebuffer::GetWidth() const
@@ -99,7 +111,7 @@ namespace Copium
void Framebuffer::InitializeImage()
{
colorAttachment = std::make_unique<ColorAttachment>(width, height);
colorAttachment = AssetManager::RegisterRuntimeAsset("Framebuffer::ColorAttachment", std::make_unique<ColorAttachment>(width, height));
}
void Framebuffer::InitializeDepthBuffer()
@@ -174,9 +186,10 @@ namespace Copium
void Framebuffer::InitializeFramebuffers()
{
framebuffers.resize(SwapChain::MAX_FRAMES_IN_FLIGHT);
const ColorAttachment& attachment = AssetManager::GetAsset<ColorAttachment>(colorAttachment);
for (size_t i = 0; i < framebuffers.size(); ++i)
{
std::vector<VkImageView> attachments{colorAttachment->GetImageView(i), depthAttachment->GetImageView()};
std::vector<VkImageView> attachments{attachment.GetImageView(i), depthAttachment->GetImageView()};
VkFramebufferCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
+5 -2
View File
@@ -1,5 +1,7 @@
#pragma once
#include "copium/asset/Asset.h"
#include "copium/asset/AssetMeta.h"
#include "copium/buffer/CommandBuffer.h"
#include "copium/sampler/ColorAttachment.h"
#include "copium/sampler/DepthAttachment.h"
@@ -9,11 +11,11 @@
namespace Copium
{
class Framebuffer final
class Framebuffer final : public Asset
{
CP_DELETE_COPY_AND_MOVE_CTOR(Framebuffer);
private:
std::unique_ptr<ColorAttachment> colorAttachment;
AssetHandle colorAttachment;
std::unique_ptr<DepthAttachment> depthAttachment;
std::vector<VkFramebuffer> framebuffers;
VkRenderPass renderPass;
@@ -21,6 +23,7 @@ namespace Copium
uint32_t width;
uint32_t height;
public:
Framebuffer(const MetaFile& metaFile);
Framebuffer(uint32_t width, uint32_t height);
~Framebuffer();
+25 -29
View File
@@ -52,16 +52,13 @@ namespace Copium
vkDeviceWaitIdle(Vulkan::GetDevice());
AssetManager::UnloadAsset(texture2D);
AssetManager::UnloadAsset(texture2D2);
AssetManager::UnloadAsset(graphicsPipeline);
AssetManager::UnloadAsset(graphicsPipelinePassthrough);
AssetManager::UnloadAsset(framebuffer);
}
bool Application::Update()
{
if (framebuffer->GetWidth() != Vulkan::GetSwapChain().GetExtent().width || framebuffer->GetHeight() != Vulkan::GetSwapChain().GetExtent().height)
{
framebuffer->Resize(Vulkan::GetSwapChain().GetExtent().width / 8, Vulkan::GetSwapChain().GetExtent().height / 8);
descriptorSetPassthrough->SetSampler(framebuffer->GetColorAttachment(), 0);
}
if (!Vulkan::GetSwapChain().BeginPresent())
return true;
@@ -74,42 +71,37 @@ namespace Copium
void Application::InitializeFrameBuffer()
{
framebuffer = std::make_unique<Framebuffer>(Vulkan::GetSwapChain().GetExtent().width, Vulkan::GetSwapChain().GetExtent().height);
framebuffer = AssetManager::LoadAsset<Framebuffer>("framebuffer.meta");
}
void Application::InitializeRenderer()
{
renderer = std::make_unique<Renderer>(framebuffer->GetRenderPass());
renderer = std::make_unique<Renderer>();
}
void Application::InitializeTextureSampler()
{
texture2D = AssetManager::LoadAsset("fox.meta");
texture2D2 = AssetManager::LoadAsset("fox2.meta");
texture2D = AssetManager::LoadAsset<Texture2D>("fox.meta");
texture2D2 = AssetManager::LoadAsset<Texture2D>("fox2.meta");
}
void Application::InitializeDescriptorSets()
{
descriptorPool = std::make_unique<DescriptorPool>();
descriptorSet = graphicsPipeline->CreateDescriptorSet(*descriptorPool, 0);
descriptorSet = AssetManager::GetAsset<Pipeline>(graphicsPipeline).CreateDescriptorSet(*descriptorPool, 0);
descriptorSet->SetSampler(AssetManager::GetAsset<Texture2D>(texture2D), 1);
descriptorSetPassthrough = graphicsPipelinePassthrough->CreateDescriptorSet(*descriptorPool, 0);
descriptorSetPassthrough->SetSampler(framebuffer->GetColorAttachment(), 0);
descriptorSetPassthrough = AssetManager::GetAsset<Pipeline>(graphicsPipelinePassthrough).CreateDescriptorSet(*descriptorPool, 0);
descriptorSetPassthrough->SetSampler(AssetManager::GetAsset<Framebuffer>(framebuffer).GetColorAttachment(), 0);
descriptorSetRenderer = renderer->GetGraphicsPipeline().CreateDescriptorSet(*descriptorPool, 1);
}
void Application::InitializeGraphicsPipeline()
{
PipelineCreator creator{framebuffer->GetRenderPass(), "res/shaders/shader.vert", "res/shaders/shader.frag"};
creator.SetVertexDescriptor(Vertex::GetDescriptor());
graphicsPipeline = std::make_unique<Pipeline>(creator);
PipelineCreator creatorPassthrough{Vulkan::GetSwapChain().GetRenderPass(), "res/shaders/passthrough.vert", "res/shaders/passthrough.frag"};
creatorPassthrough.SetVertexDescriptor(VertexPassthrough::GetDescriptor());
graphicsPipelinePassthrough = std::make_unique<Pipeline>(creatorPassthrough);
graphicsPipeline = AssetManager::LoadAsset<Pipeline>("pipeline.meta");
graphicsPipelinePassthrough = AssetManager::LoadAsset<Pipeline>("passthrough.meta");
}
void Application::InitializeMesh()
@@ -127,13 +119,15 @@ namespace Copium
{
commandBuffer->Begin();
framebuffer->Bind(*commandBuffer);
graphicsPipeline->Bind(*commandBuffer);
Framebuffer& fb = AssetManager::GetAsset<Framebuffer>(framebuffer);
Pipeline& pl = AssetManager::GetAsset<Pipeline>(graphicsPipeline);
fb.Bind(*commandBuffer);
pl.Bind(*commandBuffer);
UpdateUniformBuffer();
graphicsPipeline->SetDescriptorSet(*descriptorSet);
graphicsPipeline->BindDescriptorSets(*commandBuffer);
pl.SetDescriptorSet(*descriptorSet);
pl.BindDescriptorSets(*commandBuffer);
mesh->Bind(*commandBuffer);
mesh->Render(*commandBuffer);
@@ -151,13 +145,14 @@ namespace Copium
renderer->Quad(glm::vec2{ 0.1, -0.4}, glm::vec2{0.8, 0.8}, AssetManager::GetAsset<Texture2D>(texture2D2));
renderer->End();
framebuffer->Unbind(*commandBuffer);
fb.Unbind(*commandBuffer);
Vulkan::GetSwapChain().BeginFrameBuffer(*commandBuffer);
graphicsPipelinePassthrough->Bind(*commandBuffer);
graphicsPipelinePassthrough->SetDescriptorSet(*descriptorSetPassthrough);
graphicsPipelinePassthrough->BindDescriptorSets(*commandBuffer);
Pipeline& plPassthrough = AssetManager::GetAsset<Pipeline>(graphicsPipelinePassthrough);
plPassthrough.Bind(*commandBuffer);
plPassthrough.SetDescriptorSet(*descriptorSetPassthrough);
plPassthrough.BindDescriptorSets(*commandBuffer);
meshPassthrough->Bind(*commandBuffer);
meshPassthrough->Render(*commandBuffer);
@@ -171,7 +166,8 @@ namespace Copium
static Timer startTimer;
float time = startTimer.Elapsed();
float aspect = framebuffer->GetWidth() / (float)framebuffer->GetHeight();
Framebuffer& fb = AssetManager::GetAsset<Framebuffer>(framebuffer);
float aspect = fb.GetWidth() / (float)fb.GetHeight();
{
glm::mat4 projection = glm::perspective(glm::radians(45.0f), aspect, 0.1f, 10.0f);
+3 -3
View File
@@ -15,15 +15,15 @@ namespace Copium
CP_DELETE_COPY_AND_MOVE_CTOR(Application);
private:
std::unique_ptr<Renderer> renderer;
std::unique_ptr<Framebuffer> framebuffer;
AssetHandle framebuffer;
AssetHandle texture2D;
AssetHandle texture2D2;
AssetHandle graphicsPipeline;
AssetHandle graphicsPipelinePassthrough;
std::unique_ptr<DescriptorPool> descriptorPool;
std::unique_ptr<DescriptorSet> descriptorSet;
std::unique_ptr<DescriptorSet> descriptorSetPassthrough;
std::unique_ptr<DescriptorSet> descriptorSetRenderer;
std::unique_ptr<Pipeline> graphicsPipeline;
std::unique_ptr<Pipeline> graphicsPipelinePassthrough;
std::unique_ptr<Mesh> mesh;
std::unique_ptr<Mesh> meshPassthrough;
std::unique_ptr<CommandBuffer> commandBuffer;
+9
View File
@@ -1,6 +1,10 @@
#include "copium/core/Vulkan.h"
#include "copium/asset/AssetManager.h"
#include "copium/sampler/Texture2D.h"
#include "copium/sampler/ColorAttachment.h"
#include "copium/pipeline/Pipeline.h"
#include "copium/buffer/Framebuffer.h"
namespace Copium
{
@@ -16,6 +20,11 @@ namespace Copium
device = std::make_unique<Device>();
swapChain = std::make_unique<SwapChain>();
AssetManager::RegisterAssetType<Texture2D>("Texture2D");
AssetManager::RegisterAssetType<ColorAttachment>("RenderTexture");
AssetManager::RegisterAssetType<Pipeline>("Pipeline");
AssetManager::RegisterAssetType<Framebuffer>("Framebuffer");
// 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/");
-4
View File
@@ -8,10 +8,6 @@
int main(int argc, char** argv)
{
CP_ASSERT(glfwInit() == GLFW_TRUE, "Failed to initialize the glfw context");
for (int i = 0; i < argc; i++)
{
CP_INFO(argv[i]);
}
Copium::Vulkan::Initialize();
{
@@ -1,11 +1,51 @@
#include "copium/pipeline/Pipeline.h"
#include "copium/asset/AssetManager.h"
#include "copium/buffer/Framebuffer.h"
#include "copium/core/Vulkan.h"
#include "copium/pipeline/Shader.h"
#include "copium/renderer/RendererVertex.h"
#include "copium/mesh/VertexPassthrough.h"
#include "copium/mesh/Vertex.h"
#include "copium/util/FileSystem.h"
namespace Copium
{
Pipeline::Pipeline(const MetaFile& metaFile)
: shaderReflector{ShaderReflector{metaFile.GetMetaClass("Pipeline").GetValue("vert-filepath"), metaFile.GetMetaClass("Pipeline").GetValue("frag-filepath")}}
{
const MetaFileClass& metaFileClass = metaFile.GetMetaClass("Pipeline");
VkRenderPass renderPass;
if (metaFileClass.HasValue("framebuffer-uuid"))
{
Framebuffer& fb = AssetManager::LoadAsset<Framebuffer>(UUID{metaFileClass.GetValue("framebuffer-uuid")});
renderPass = fb.GetRenderPass();
framebuffer = fb;
}
else
{
renderPass = Vulkan::GetSwapChain().GetRenderPass();
}
PipelineCreator creator{renderPass, metaFileClass.GetValue("vert-filepath"), metaFileClass.GetValue("frag-filepath")};
std::string type = metaFileClass.GetValue("type");
if (type == "Renderer")
{
creator.SetVertexDescriptor(RendererVertex::GetDescriptor());
creator.SetDepthTest(false);
}
else if (type == "Passthrough")
{
creator.SetVertexDescriptor(VertexPassthrough::GetDescriptor());
creator.SetDepthTest(false);
}
else if (type == "Mesh")
{
creator.SetVertexDescriptor(Vertex::GetDescriptor());
}
InitializeDescriptorSetLayout(creator);
InitializePipeline(creator);
}
Pipeline::Pipeline(PipelineCreator creator)
: shaderReflector{creator.shaderReflector}
{
@@ -21,6 +61,10 @@ namespace Copium
{
vkDestroyDescriptorSetLayout(Vulkan::GetDevice(), descriptorSetLayout, nullptr);
}
if (framebuffer != NULL_ASSET_HANDLE)
{
AssetManager::UnloadAsset(framebuffer);
}
}
void Pipeline::Bind(const CommandBuffer& commandBuffer)
+5 -1
View File
@@ -1,5 +1,6 @@
#pragma once
#include "copium/asset/Asset.h"
#include "copium/buffer/CommandBuffer.h"
#include "copium/pipeline/DescriptorSet.h"
#include "copium/pipeline/PipelineCreator.h"
@@ -10,7 +11,7 @@
namespace Copium
{
class Pipeline final
class Pipeline final : public Asset
{
CP_DELETE_COPY_AND_MOVE_CTOR(Pipeline);
private:
@@ -19,8 +20,11 @@ namespace Copium
std::vector<VkDescriptorSet> boundDescriptorSets;
VkPipelineLayout pipelineLayout;
VkPipeline graphicsPipeline;
AssetHandle framebuffer;
public:
Pipeline(const MetaFile& metaFile);
Pipeline(PipelineCreator creator);
~Pipeline();
void Bind(const CommandBuffer& commandBuffer);
+4 -4
View File
@@ -1,13 +1,13 @@
#include "copium/renderer/Batch.h"
#include "copium/asset/AssetManager.h"
#include "copium/renderer/RendererVertex.h"
namespace Copium
{
Batch::Batch(Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers)
: pipeline{pipeline},
vertexBuffer{RendererVertex::GetDescriptor(), vertexCount},
descriptorSet{pipeline.CreateDescriptorSet(descriptorPool, 0)}
Batch::Batch(AssetHandle pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers)
: vertexBuffer{RendererVertex::GetDescriptor(), vertexCount},
descriptorSet{AssetManager::GetAsset<Pipeline>(pipeline).CreateDescriptorSet(descriptorPool, 0)}
{
descriptorSet->SetSamplers(samplers, 0);
}
+2 -3
View File
@@ -1,5 +1,6 @@
#pragma once
#include "copium/asset/AssetMeta.h"
#include "copium/buffer/RendererVertexBuffer.h"
#include "copium/pipeline/DescriptorSet.h"
#include "copium/pipeline/Pipeline.h"
@@ -11,12 +12,10 @@ namespace Copium
{
CP_DELETE_COPY_AND_MOVE_CTOR(Batch);
private:
Pipeline& pipeline;
RendererVertexBuffer vertexBuffer;
std::unique_ptr<DescriptorSet> descriptorSet;
public:
Batch(Pipeline& pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers);
Batch(AssetHandle pipeline, DescriptorPool& descriptorPool, int vertexCount, const std::vector<const Sampler*> samplers);
RendererVertexBuffer& GetVertexBuffer();
DescriptorSet& GetDescriptorSet();
};
+12 -14
View File
@@ -12,19 +12,20 @@ namespace Copium
static constexpr int MAX_NUM_INDICES = 6 * MAX_NUM_QUADS;
static constexpr int MAX_NUM_TEXTURES = 32;
Renderer::Renderer(VkRenderPass renderPass)
Renderer::Renderer()
: descriptorPool{},
ibo{MAX_NUM_INDICES},
emptyTexture{AssetManager::RegisterRuntimeAsset("empty", std::make_unique<Texture2D>(std::vector<uint8_t>{0, 0, 0, 255}, 1, 1))},
samplers{MAX_NUM_TEXTURES, &AssetManager::GetAsset<Texture2D>(emptyTexture)}
{
InitializeIndexBuffer();
InitializeGraphicsPipeline(renderPass);
InitializeGraphicsPipeline();
}
Renderer::~Renderer()
{
AssetManager::UnloadAsset(emptyTexture);
AssetManager::UnloadAsset(pipeline);
}
void Renderer::Quad(const glm::vec2& pos, const glm::vec2& size, const glm::vec3& color)
@@ -59,7 +60,7 @@ namespace Copium
void Renderer::Begin(CommandBuffer& commandBuffer)
{
graphicsPipeline->Bind(commandBuffer);
AssetManager::GetAsset<Pipeline>(pipeline).Bind(commandBuffer);
ibo.Bind(commandBuffer);
batchIndex = -1;
NextBatch();
@@ -73,12 +74,12 @@ namespace Copium
Pipeline& Renderer::GetGraphicsPipeline()
{
return *graphicsPipeline;
return AssetManager::GetAsset<Pipeline>(pipeline);
}
void Renderer::SetDescriptorSet(const DescriptorSet& descriptorSet)
{
graphicsPipeline->SetDescriptorSet(descriptorSet);
AssetManager::GetAsset<Pipeline>(pipeline).SetDescriptorSet(descriptorSet);
}
void Renderer::InitializeIndexBuffer()
@@ -99,19 +100,16 @@ namespace Copium
ibo.UpdateStaging(indices.data());
}
void Renderer::InitializeGraphicsPipeline(VkRenderPass renderPass)
void Renderer::InitializeGraphicsPipeline()
{
PipelineCreator creator{renderPass, "res/shaders/renderer.vert", "res/shaders/renderer.frag"};
creator.SetVertexDescriptor(RendererVertex::GetDescriptor());
creator.SetDepthTest(false);
graphicsPipeline = std::make_unique<Pipeline>(creator);
pipeline = AssetManager::LoadAsset<Pipeline>("renderer.meta");
}
int Renderer::AllocateSampler(const Sampler& sampler)
{
for (size_t i = 0; i < textureCount; i++)
{
if (*samplers[i] == sampler)
if (*samplers[i] == (VkSampler)sampler)
{
return i;
}
@@ -142,8 +140,8 @@ namespace Copium
{
batches[batchIndex]->GetVertexBuffer().Unmap();
batches[batchIndex]->GetVertexBuffer().Bind(*currentCommandBuffer);
graphicsPipeline->SetDescriptorSet(batches[batchIndex]->GetDescriptorSet());
graphicsPipeline->BindDescriptorSets(*currentCommandBuffer);
AssetManager::GetAsset<Pipeline>(pipeline).SetDescriptorSet(batches[batchIndex]->GetDescriptorSet());
AssetManager::GetAsset<Pipeline>(pipeline).BindDescriptorSets(*currentCommandBuffer);
ibo.Draw(*currentCommandBuffer, quadCount * 6);
}
@@ -153,7 +151,7 @@ namespace Copium
std::fill(samplers.begin(), samplers.end(), &AssetManager::GetAsset<Texture2D>(emptyTexture));
if (batchIndex >= batches.size())
{
batches.emplace_back(std::make_unique<Batch>(*graphicsPipeline, descriptorPool, MAX_NUM_VERTICES, samplers));
batches.emplace_back(std::make_unique<Batch>(pipeline, descriptorPool, MAX_NUM_VERTICES, samplers));
}
mappedVertexBuffer = (char*)batches[batchIndex]->GetVertexBuffer().Map() + batches[batchIndex]->GetVertexBuffer().GetPosition(Vulkan::GetSwapChain().GetFlightIndex());
quadCount = 0;
+4 -3
View File
@@ -1,5 +1,6 @@
#pragma once
#include "copium/asset/AssetMeta.h"
#include "copium/buffer/CommandBuffer.h"
#include "copium/buffer/IndexBuffer.h"
#include "copium/buffer/RendererVertexBuffer.h"
@@ -20,7 +21,7 @@ namespace Copium
DescriptorPool descriptorPool;
IndexBuffer ibo;
AssetHandle emptyTexture;
std::unique_ptr<Pipeline> graphicsPipeline;
AssetHandle pipeline;
std::vector<std::unique_ptr<Batch>> batches;
// Temporary data during a render
@@ -31,7 +32,7 @@ namespace Copium
int textureCount;
void* mappedVertexBuffer;
public:
Renderer(VkRenderPass renderPass);
Renderer();
~Renderer();
void Quad(const glm::vec2& from, const glm::vec2& to, const glm::vec3& color = glm::vec3{1, 1, 1});
@@ -44,7 +45,7 @@ namespace Copium
void SetDescriptorSet(const DescriptorSet& descriptorSet);
private:
void InitializeIndexBuffer();
void InitializeGraphicsPipeline(VkRenderPass renderPass);
void InitializeGraphicsPipeline();
int AllocateSampler(const Sampler& sampler);
void AllocateQuad();
@@ -5,8 +5,32 @@
namespace Copium
{
ColorAttachment::ColorAttachment(const MetaFile& metaFile)
{
const MetaFileClass& metaClass = metaFile.GetMetaClass("RenderTexture");
if (metaClass.HasValue("width"))
{
char* endPtr;
width = std::strtol(metaClass.GetValue("width").c_str(), &endPtr, 10);
}
else
{
width = Vulkan::GetSwapChain().GetExtent().width;
}
if (metaClass.HasValue("height"))
{
char* endPtr;
height = std::strtol(metaClass.GetValue("height").c_str(), &endPtr, 10);
}
else
{
height = Vulkan::GetSwapChain().GetExtent().height;
}
InitializeColorAttachment(width, height);
}
ColorAttachment::ColorAttachment(int width, int height)
: Sampler{}
{
InitializeColorAttachment(width, height);
}
@@ -21,6 +45,28 @@ namespace Copium
vkDestroyImageView(Vulkan::GetDevice(), imageView, nullptr);
}
void ColorAttachment::Resize(int width, int height)
{
for (auto&& image : images)
vkDestroyImage(Vulkan::GetDevice(), image, nullptr);
for (auto&& imageMemory : imageMemories)
vkFreeMemory(Vulkan::GetDevice(), imageMemory, nullptr);
for (auto&& imageView : imageViews)
vkDestroyImageView(Vulkan::GetDevice(), imageView, nullptr);
InitializeColorAttachment(width, height);
}
int ColorAttachment::GetWidth() const
{
return width;
}
int ColorAttachment::GetHeight() const
{
return height;
}
VkDescriptorImageInfo ColorAttachment::GetDescriptorImageInfo(int index) const
{
CP_ASSERT(index >= 0 && index < imageViews.size(), "index out of bound for color attachment");
@@ -32,7 +78,7 @@ namespace Copium
return imageInfo;
}
VkImageView ColorAttachment::GetImageView(int index)
VkImageView ColorAttachment::GetImageView(int index) const
{
CP_ASSERT(index >= 0 && index < imageViews.size(), "Index out of bound");
@@ -14,12 +14,19 @@ namespace Copium
std::vector<VkImage> images;
std::vector<VkDeviceMemory> imageMemories;
std::vector<VkImageView> imageViews;
int width;
int height;
public:
ColorAttachment(const MetaFile& metaFile);
ColorAttachment(int width, int height);
~ColorAttachment() override;
void Resize(int width, int height);
int GetWidth() const;
int GetHeight() const;
VkDescriptorImageInfo GetDescriptorImageInfo(int index) const override;
VkImageView GetImageView(int index);
VkImageView GetImageView(int index) const;
private:
void InitializeColorAttachment(int width, int height);
@@ -18,6 +18,15 @@ namespace Copium
vkDestroyImageView(Vulkan::GetDevice(), imageView, nullptr);
}
void DepthAttachment::Resize(int width, int height)
{
vkDestroyImage(Vulkan::GetDevice(), image, nullptr);
vkFreeMemory(Vulkan::GetDevice(), imageMemory, nullptr);
vkDestroyImageView(Vulkan::GetDevice(), imageView, nullptr);
InitializeDepthAttachment(width, height);
}
VkDescriptorImageInfo DepthAttachment::GetDescriptorImageInfo(int index) const
{
VkDescriptorImageInfo imageInfo{};
@@ -18,6 +18,8 @@ namespace Copium
DepthAttachment(int width, int height);
~DepthAttachment() override;
void Resize(int width, int height);
VkDescriptorImageInfo GetDescriptorImageInfo(int index) const override;
VkImageView GetImageView() const;
+2 -1
View File
@@ -1,12 +1,13 @@
#pragma once
#include "copium/asset/Asset.h"
#include "copium/util/Common.h"
#include <vulkan/vulkan.hpp>
namespace Copium
{
class Sampler
class Sampler : public Asset
{
CP_DELETE_COPY_AND_MOVE_CTOR(Sampler);
protected:
@@ -10,7 +10,6 @@
namespace Copium
{
Texture2D::Texture2D(const MetaFile& metaFile)
: Sampler{}, Asset{AssetType::Texture2D}
{
const std::string& filepath = metaFile.GetMetaClass("Texture2D").GetValue("filepath");
CP_DEBUG("Loading texture file: %s", filepath.c_str());
@@ -18,7 +17,6 @@ namespace Copium
}
Texture2D::Texture2D(const std::vector<uint8_t>& rgbaData, int width, int height)
: Sampler{}, Asset{AssetType::Texture2D}
{
CP_ASSERT(rgbaData.size() == width * height * 4, "rgbaData has invalid size, should be equal to width * height * 4 (%d) actually is %d", width * height * 4, rgbaData.size());
InitializeTextureImageFromData((void*)rgbaData.data(), width, height);
+1 -2
View File
@@ -1,6 +1,5 @@
#pragma once
#include "copium/asset/Asset.h"
#include "copium/buffer/CommandBufferScoped.h"
#include "copium/sampler/Image.h"
#include "copium/sampler/Sampler.h"
@@ -10,7 +9,7 @@
namespace Copium
{
class Texture2D final : public Sampler, public Asset
class Texture2D final : public Sampler
{
CP_DELETE_COPY_AND_MOVE_CTOR(Texture2D);
private: