Asset bakers now support multiple extensions if necessary

This commit is contained in:
antopilo
2025-01-21 00:47:18 -05:00
parent 1728b20cda
commit 8faaef0ec6
9 changed files with 64 additions and 19 deletions

View File

@@ -74,7 +74,7 @@ FileType File::GetFileType() const
return FileType::Solution;
}
if (ext == ".mesh")
if (ext == ".nkmesh")
{
return FileType::Mesh;
}

View File

@@ -17,7 +17,25 @@ std::string FileSystem::Root = "";
Ref<Directory> FileSystem::RootDirectory;
Ref<filewatch::FileWatch<std::string>> FileSystem::RootFileWatch;
void FileSystem::ForeachFile(std::function<void(Ref<File>)> func)
{
ForeachFile(func, RootDirectory);
}
void FileSystem::ForeachFile(std::function<void(Ref<File>)> func, Ref<Directory> dir)
{
for(auto& file : RootDirectory->Files)
{
func(file);
}
for(auto& dir : RootDirectory->Directories)
{
ForeachFile(func, dir);
}
}
void FileSystem::ScanDirectory(Ref<Directory> directory)
{
directory->Files.clear();

View File

@@ -2,6 +2,8 @@
#include "src/Core/Core.h"
#include "FileTypes.h"
#include <functional>
namespace filewatch
{
template<typename T>
@@ -21,7 +23,13 @@ namespace Nuake
static Ref<filewatch::FileWatch<std::string>> RootFileWatch;
static void SetRootDirectory(const std::string path);
// Starts from root
static void ForeachFile(std::function<void(Ref<File>)> func);
// Strats from specified folder
static void ForeachFile(std::function<void(Ref<File>)> func, Ref<Directory> dir);
static void Scan();
static std::string AbsoluteToRelative(const std::string& path);
static std::string RelativeToAbsolute(const std::string& path);

View File

@@ -6,7 +6,8 @@
using namespace Nuake;
VkMesh::VkMesh(const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices)
VkMesh::VkMesh(const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices) :
DescriptorLayout(nullptr)
{
// First we allocate the buffers on the GPU
const size_t vertexBufferSize = vertices.size() * sizeof(Vertex);
@@ -55,13 +56,16 @@ void VkMesh::UploadToGPU(const std::vector<Vertex>& vertices, const std::vector<
void VkMesh::CreateDescriptorSet()
{
DescriptorLayoutBuilder builder;
builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
auto& vk = VkRenderer::Get();
auto device = vk.GetDevice();
DescriptorLayout = builder.Build(device, VK_SHADER_STAGE_ALL_GRAPHICS);
DescriptorSet = vk.GetDescriptorAllocator().Allocate(device, DescriptorLayout);
if (!DescriptorLayout)
{
DescriptorLayoutBuilder builder;
builder.AddBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
DescriptorLayout = builder.Build(device, VK_SHADER_STAGE_ALL_GRAPHICS);
DescriptorSet = vk.GetDescriptorAllocator().Allocate(device, DescriptorLayout);
}
VkDescriptorBufferInfo bufferInfo{};
bufferInfo.buffer = GetVertexBuffer()->GetBuffer();

View File

@@ -80,9 +80,9 @@ void VkRenderer::Initialize()
// Init global pool
std::vector<DescriptorAllocator::PoolSizeRatio> sizes =
{
{ VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 8 },
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 },
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 50 }
{ VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 4096 },
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 4096 },
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1024 }
};
GlobalDescriptorAllocator.InitPool(Device, 1000, sizes);
@@ -883,5 +883,7 @@ VkDescriptorSet DescriptorAllocator::Allocate(VkDevice device, VkDescriptorSetLa
VkDescriptorSet ds;
VK_CALL(vkAllocateDescriptorSets(device, &allocInfo, &ds));
NumAllocation++;
return ds;
}

View File

@@ -103,6 +103,7 @@ namespace Nuake
struct DescriptorAllocator
{
uint32_t NumAllocation = 0;
struct PoolSizeRatio
{
VkDescriptorType type;
@@ -244,7 +245,7 @@ namespace Nuake
void EndDraw();
DescriptorAllocator GetDescriptorAllocator()
DescriptorAllocator& GetDescriptorAllocator()
{
return GlobalDescriptorAllocator;
}

View File

@@ -22,7 +22,11 @@ namespace Nuake
void RegisterBaker(Ref<IAssetBaker> baker)
{
Bakers[baker->GetExtension()] = baker;
// Latest registered baker overwrites previous
for(const auto& extension : baker->GetExtensions())
{
Bakers[extension] = baker;
}
}
Ref<File> Bake(const Ref<File>& file)

View File

@@ -30,7 +30,7 @@ namespace Nuake
class GLTFBaker : public IAssetBaker
{
public:
GLTFBaker() : IAssetBaker(".glb") {}
GLTFBaker() : IAssetBaker({".glb", ".gltf", "fbx"}) {}
Ref<File> Bake(const Ref<File>& file) override;

View File

@@ -3,6 +3,8 @@
#include "src/Core/Core.h"
#include "src/FileSystem/File.h"
#include <set>
#include <span>
#include <string>
namespace Nuake
@@ -10,14 +12,20 @@ namespace Nuake
class IAssetBaker
{
private:
std::string Extension;
std::set<std::string> Extensions;
public:
IAssetBaker(const std::string& extension) : Extension(extension) {}
IAssetBaker(const std::string& extension) : Extensions { extension } {}
IAssetBaker(std::initializer_list<std::string> extension) : Extensions(extension.begin(), extension.end()) {}
virtual ~IAssetBaker() = default;
std::string GetExtension() const { return Extension; }
std::set<std::string> GetExtensions() const { return Extensions; }
inline bool CanBakeFile(const Ref<File>& file) const
{
return Extensions.find(file->GetExtension()) != Extensions.end();
}
public:
virtual Ref<File> Bake(const Ref<File>& file) = 0;
};