begenning the refactor

This commit is contained in:
2024-03-27 23:03:54 +01:00
parent e5ff232065
commit 6bbf1e196d
131 changed files with 2135 additions and 1192 deletions

66
runtime/Includes/Renderer/Buffers/Buffer.h git.filemode.normal_file
View File

@@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Buffer.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 23:18:52 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:09:07 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_BUFFER__
#define __MLX_VK_BUFFER__
#include <Renderer/Enums.h>
#include <Renderer/Core/RenderCore.h>
#include <Renderer/Command/CommandResource.h>
namespace mlx
{
class Buffer : public CommandResource
{
public:
Buffer() = default;
void Create(BufferType type, VkDeviceSize size, VkBufferUsageFlags usage, const char* name, const void* data = nullptr);
void Destroy() noexcept;
inline void MapMem(void** data) noexcept { Render_Core::get().getAllocator().mapMemory(m_allocation, data); m_is_mapped = true; }
inline bool IsMapped() const noexcept { return m_is_mapped; }
inline void UnmapMem() noexcept { Render_Core::get().getAllocator().unmapMemory(m_allocation); m_is_mapped = false; }
void Flush(VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0);
bool CopyFromBuffer(const Buffer& buffer) noexcept;
inline VkBuffer& operator()() noexcept { return m_buffer; }
inline VkBuffer& Get() noexcept { return m_buffer; }
inline VkDeviceSize GetSize() const noexcept { return m_size; }
inline VkDeviceSize GetOffset() const noexcept { return m_offset; }
~Buffer() = default;
protected:
void PushToGPU() noexcept;
void Swap(Buffer& buffer) noexcept;
protected:
VmaAllocation m_allocation;
VkBuffer m_buffer = VK_NULL_HANDLE;
VkDeviceSize m_offset = 0;
VkDeviceSize m_size = 0;
private:
void CreateBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size, const char* name);
private:
#ifdef DEBUG
std::string m_name;
#endif
VkBufferUsageFlags m_usage = 0;
bool m_is_mapped = false;
};
}
#endif

View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* IndexBuffer.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/25 15:05:05 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:11:57 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __VK_IBO__
#define __VK_IBO__
#include <Renderer/Buffer/Buffer.h>
#include <Renderer/Renderer.h>
namespace mlx
{
class C_IBO : public Buffer
{
public:
inline void Create(std::uint32_t size, const std::uint16_t* data, const char* name) { Buffer::Create(BufferType::Constant, size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, name, data); }
inline void Bind(Renderer& renderer) noexcept { renderer.GetActiveCmdBuffer().BindIndexBuffer(*this); }
};
}
#endif

View File

@@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* UniformBuffer.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:45:29 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:15:23 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_UBO__
#define __MLX_VK_UBO__
#include <Renderer/Buffer/Buffer.h>
namespace mlx
{
class UniformBuffer
{
public:
UniformBuffer() = default;
void Create(NonOwningPtr<class Renderer> renderer, std::uint32_t size, const char* name);
void Destroy() noexcept;
void SetData(std::uint32_t size, const void* data);
VkDeviceSize GetSize() noexcept;
VkDeviceSize GetOffset() noexcept;
VkDeviceMemory GetDeviceMemory() noexcept;
VkBuffer& operator()() noexcept;
VkBuffer& Get() noexcept;
inline VkDeviceSize GetSize(int i) noexcept { return m_buffers[i].getSize(); }
inline VkDeviceSize GetOffset(int i) noexcept { return m_buffers[i].getOffset(); }
inline VkBuffer& operator()(int i) noexcept { return m_buffers[i].get(); }
inline VkBuffer& Get(int i) noexcept { return m_buffers[i].get(); }
~UniformBuffer() = default;
private:
std::array<Buffer, MAX_FRAMES_IN_FLIGHT> m_buffers;
std::array<void*, MAX_FRAMES_IN_FLIGHT> m_maps;
NonOwningPtr<class Renderer> m_renderer;
};
}
#endif // __MLX_VK_UBO__

View File

@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* VertexBuffer.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:27:38 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:18:23 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_VBO__
#define __MLX_VK_VBO__
#include <Renderer/Enums.h>
#include <Renderer/Buffer/Buffer.h>
#include <Renderer/Renderer.h>
namespace mlx
{
class VertexBuffer : public Buffer
{
public:
inline void Create(std::uint32_t size, const void* data, const char* name) { Buffer::Create(BufferType::HighDynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name, data); }
void SetData(std::uint32_t size, const void* data);
inline void Bind(Renderer& renderer) noexcept { renderer.GetActiveCmdBuffer().BindVertexBuffer(*this); }
};
class DeviceVertexBuffer : public Buffer
{
public:
inline void create(std::uint32_t size, const void* data, const char* name) { Buffer::Create(BufferType::LowDynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name, data); }
void SetData(std::uint32_t size, const void* data);
inline void Bind(Renderer& renderer) noexcept { renderer.GetActiveCmdBuffer().BindVertexBuffer(*this); }
};
class ConstantVertexBuffer : public Buffer
{
public:
inline void Create(std::uint32_t size, const void* data, const char* name) { Buffer::Create(BufferType::Constant, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name, data); }
inline void Bind(Renderer& renderer) noexcept { renderer.GetActiveCmdBuffer().BindVertexBuffer(*this); }
};
}
#endif // __MLX_VK_VBO__

View File

@@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* CommandBuffer.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:25:42 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:44:58 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_CMD_BUFFER__
#define __MLX_VK_CMD_BUFFER__
#include <Renderer/Core/Fence.h>
#include <Renderer/Enums.h>
namespace mlx
{
class Buffer;
class Image;
class CommandBuffer
{
public:
void Init(CommandBufferType type, NonOwningPtr<class CommandManager> manager);
void Init(CommandBufferType type, NonOwningPtr<class CommandPool> pool);
void Destroy() noexcept;
void BeginRecord(VkCommandBufferUsageFlags usage = 0);
void Submit(class Semaphore* semaphores) noexcept;
void SubmitIdle(bool shouldWaitForExecution = true) noexcept; // TODO : handle `shouldWaitForExecution` as false by default (needs to modify CmdResources lifetimes to do so)
void UpdateSubmitState() noexcept;
inline void WaitForExecution() noexcept { m_fence.wait(); UpdateSubmitState(); m_state = CommandBufferState::Ready; }
inline void Reset() noexcept { vkResetCommandBuffer(m_cmd_buffer, 0); }
void EndRecord();
void BindVertexBuffer(Buffer& buffer) noexcept;
void BindIndexBuffer(Buffer& buffer) noexcept;
void CopyBuffer(Buffer& dst, Buffer& src) noexcept;
void CopyBufferToImage(Buffer& buffer, Image& image) noexcept;
void CopyImagetoBuffer(Image& image, Buffer& buffer) noexcept;
void TransitionImageLayout(Image& image, VkImageLayout new_layout) noexcept;
inline bool IsInit() const noexcept { return m_state != CommandBufferState::Uninit; }
inline bool IsReadyToBeUsed() const noexcept { return m_state == CommandBufferState::Ready; }
inline bool IsRecording() const noexcept { return m_state == CommandBufferState::Recording; }
inline bool HasBeenSubmitted() const noexcept { return m_state == CommandBufferState::Submitted; }
inline CommandBufferState GetCurrentState() const noexcept { return m_state; }
inline VkCommandBuffer& operator()() noexcept { return m_cmd_buffer; }
inline VkCommandBuffer& Get() noexcept { return m_cmd_buffer; }
inline Fence& GetFence() noexcept { return m_fence; }
private:
void PreTransferBarrier() noexcept;
void PostTransferBarrier() noexcept;
private:
std::vector<class CmdResource*> m_cmd_resources;
Fence m_fence;
VkCommandBuffer m_cmd_buffer = VK_NULL_HANDLE;
NonOwningPtr<class CmdPool> m_pool;
CommandBufferState m_state = CommandBufferState::Uninit;
CommandBufferType m_type;
};
}
#endif // __MLX_VK_CMD_BUFFER__

View File

@@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* CommandManager.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 17:48:52 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:20:53 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_COMMAND_MANAGER__
#define __MLX_COMMAND_MANAGER__
#include <Renderer/Core/RenderCore.h>
#include <Renderer/Command/CommandPool.h>
#include <Renderer/Command/CommandBuffer.h>
namespace mlx
{
class CommandManager
{
public:
CommandManager() = default;
void Init() noexcept;
void BeginRecord(int active_image_index);
void EndRecord(int active_image_index);
void Destroy() noexcept;
inline CommandPool& GetCmdPool() noexcept { return m_cmd_pool; }
inline CommandBuffer& GetCmdBuffer(int i) noexcept { return m_cmd_buffers[i]; }
~CommandManager() = default;
private:
std::array<CommandBuffer, MAX_FRAMES_IN_FLIGHT> m_cmd_buffers;
CommandPool m_cmd_pool;
};
}
#endif

View File

@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* CommandPool.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:24:12 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:33:15 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_CMD_POOL__
#define __MLX_VK_CMD_POOL__
namespace mlx
{
class CommandPool
{
public:
CommandPool() = default;
void Init();
void Destroy() noexcept;
inline VkCommandPool& operator()() noexcept { return m_cmd_pool; }
inline VkCommandPool& Get() noexcept { return m_cmd_pool; }
~CommandPool() = default;
private:
VkCommandPool m_cmd_pool = VK_NULL_HANDLE;
};
}
#endif // __MLX_VK_CMD_POOL__

View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* CommandResource.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/16 20:44:29 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:37:06 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_COMMAND_RESOURCE__
#define __MLX_COMMAND_RESOURCE__
#include <Renderer/Enums.h>
#include <Core/UUID.h>
namespace mlx
{
class CommandResource
{
friend class SingleTimeCmdManager;
public:
CommandResource() : m_uuid() {}
inline void RecordedInCmdBuffer() noexcept { m_state = CommandResourceState::Held; }
inline void RemovedFromCmdBuffer() noexcept { m_state = CommandResourceState::Free; }
inline UUID GetUUID() const noexcept { return m_uuid; }
virtual ~CommandResource() = default;
private:
UUID m_uuid;
CommandResourceState m_state = CommandResourceState::Free;
};
}
#endif

View File

@@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* SingleTimeCmdManager.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/15 18:25:57 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:46:48 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_SINGLE_TIME_CMD_MANAGER__
#define __MLX_SINGLE_TIME_CMD_MANAGER__
#include <Renderer/Command/CommandBuffer.h>
#include <Renderer/Command/CommandPool.h>
namespace mlx
{
class CommandBuffer;
class SingleTimeCmdManager
{
friend class RenderCore;
public:
SingleTimeCmdManager() = default;
void Init() noexcept;
void Destroy() noexcept;
void UpdateSingleTimesCmdBuffersSubmitState() noexcept;
void WaitForAllExecutions() noexcept;
inline CommandPool& GetCmdPool() noexcept { return m_pool; }
CommanddBuffer& GetCmdBuffer() noexcept;
~SingleTimeCmdManager() = default;
inline static constexpr const std::uint8_t BASE_POOL_SIZE = 16;
private:
std::vector<CommanddBuffer> m_buffers;
CommanddPool m_pool;
};
}
#endif

40
runtime/Includes/Renderer/Core/Device.h git.filemode.normal_file
View File

@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Device.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:13:42 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:47:21 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_DEVICE__
#define __MLX_VK_DEVICE__
namespace mlx
{
class Device
{
public:
void Init();
void Destroy() noexcept;
inline VkDevice& operator()() noexcept { return m_device; }
inline VkDevice& Get() noexcept { return m_device; }
inline VkPhysicalDevice& GetPhysicalDevice() noexcept { return m_physical_device; }
private:
void PickPhysicalDevice();
bool CheckDeviceExtensionSupport(VkPhysicalDevice device);
int DeviceScore(VkPhysicalDevice device);
private:
VkPhysicalDevice m_physical_device = VK_NULL_HANDLE;
VkDevice m_device = VK_NULL_HANDLE;
};
}
#endif // __MLX_VK_DEVICE__

View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* DrawableResource.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/10 21:00:37 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:47:32 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_DRAWABLE_RESOURCE__
#define __MLX_DRAWABLE_RESOURCE__
namespace mlx
{
class DrawableResource
{
public:
DrawableResource() = default;
virtual void Render(std::array<VkDescriptorSet, 2>& sets, class Renderer& renderer) = 0;
virtual void ResetUpdate() {}
virtual ~DrawableResource() = default;
};
}
#endif

40
runtime/Includes/Renderer/Core/Fence.h git.filemode.normal_file
View File

@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Fence.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 17:52:09 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:48:31 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_FENCE__
#define __MLX_VK_FENCE__
namespace mlx
{
class Fence
{
public:
Fence() = default;
void Init();
inline VkFence& Get() noexcept { return m_fence; }
void Wait() noexcept;
void Reset() noexcept;
bool IsReady() const noexcept;
MLX_FORCEINLINE void WaitAndReset() noexcept { Wait(); Reset(); }
void Destroy() noexcept;
~Fence() = default;
private:
VkFence m_fence = VK_NULL_HANDLE;
};
}
#endif

35
runtime/Includes/Renderer/Core/Instance.h git.filemode.normal_file
View File

@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Instance.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:03:04 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:48:55 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_INSTANCE__
#define __MLX_VK_INSTANCE__
namespace mlx
{
class Instance
{
public:
void Init();
void Destroy() noexcept;
inline VkInstance& operator()() noexcept { return m_instance; }
inline VkInstance& Get() noexcept { return m_instance; }
private:
std::vector<const char*> GetRequiredExtensions();
private:
VkInstance m_instance = VK_NULL_HANDLE;
};
}
#endif // __MLX_VK_INSTANCE__

48
runtime/Includes/Renderer/Core/Memory.h git.filemode.normal_file
View File

@@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Memory.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/20 02:13:03 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:49:57 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_MEMORY__
#define __MLX_VK_MEMORY__
namespace mlx
{
class GPUallocator
{
public:
GPUallocator() = default;
void Init() noexcept;
void Destroy() noexcept;
VmaAllocation CreateBuffer(const VkBufferCreateInfo* binfo, const VmaAllocationCreateInfo* vinfo, VkBuffer& buffer, const char* name = nullptr) noexcept;
void DestroyBuffer(VmaAllocation allocation, VkBuffer buffer) noexcept;
VmaAllocation CreateImage(const VkImageCreateInfo* iminfo, const VmaAllocationCreateInfo* vinfo, VkImage& image, const char* name = nullptr) noexcept;
void DestroyImage(VmaAllocation allocation, VkImage image) noexcept;
void MapMemory(VmaAllocation allocation, void** data) noexcept;
void UnmapMemory(VmaAllocation allocation) noexcept;
void DumpMemoryToJson();
void Flush(VmaAllocation allocation, VkDeviceSize size, VkDeviceSize offset) noexcept;
~GPUallocator() = default;
private:
VmaAllocator m_allocator;
std::int32_t m_active_buffers_allocations = 0;
std::int32_t m_active_images_allocations = 0;
};
}
#endif

51
runtime/Includes/Renderer/Core/Queues.h git.filemode.normal_file
View File

@@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Queues.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:01:49 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:50:52 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_QUEUES__
#define __MLX_VK_QUEUES__
namespace mlx
{
class Queues
{
public:
struct QueueFamilyIndices
{
std::optional<std::uint32_t> graphics_family;
std::optional<std::uint32_t> present_family;
inline bool isComplete() { return graphics_family.has_value() && present_family.has_value(); }
};
public:
QueueFamilyIndices FindQueueFamilies(VkPhysicalDevice device);
void Init();
inline VkQueue& GetGraphic() noexcept { return _graphics_queue; }
inline VkQueue& GetPresent() noexcept { return _present_queue; }
inline QueueFamilyIndices GetFamilies() noexcept
{
if(m_families.has_value())
return *m_families;
FatalError("Vulkan : cannot get queue families, not init");
return {}; // just to avoid warnings
}
private:
VkQueue m_graphics_queue;
VkQueue m_present_queue;
std::optional<QueueFamilyIndices> m_families;
};
}
#endif // __MLX_VK_QUEUES__

78
runtime/Includes/Renderer/Core/RenderCore.h git.filemode.normal_file
View File

@@ -0,0 +1,78 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RenderCore.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 19:16:32 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:55:43 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_RENDER_CORE__
#define __MLX_RENDER_CORE__
#include <Renderer/Command/SingleTimeCmdManager.h>
#include <Renderer/Descriptors/DescriptorPoolManager.h>
#include <Renderer/Descriptors/DescriptorPool.h>
#include <Renderer/Core/Queues.h>
#include <Renderer/Core/Device.h>
#include <Renderer/Core/Instance.h>
#include <Renderer/Core/ValidationLayers.h>
#include <Renderer/Core/memory.h>
#include <Utils/Singleton.h>
namespace mlx
{
const char* VerbaliseVkResult(VkResult result);
VkPipelineStageFlags AccessFlagsToPipelineStage(VkAccessFlags access_flags, VkPipelineStageFlags stage_flags);
#ifdef DEBUG
constexpr const bool enable_validation_layers = true;
#else
constexpr const bool enable_validation_layers = false;
#endif
const std::vector<const char*> validation_layers = { "VK_LAYER_KHRONOS_validation" };
constexpr const int MAX_FRAMES_IN_FLIGHT = 3;
constexpr const int MAX_SETS_PER_POOL = 512;
constexpr const int NUMBER_OF_UNIFORM_BUFFERS = 1; // change this if for wathever reason more than one uniform buffer is needed
class RenderCore : public Singleton<RenderCore>
{
friend class Singleton<RenderCore>;
public:
void Init();
void Destroy();
inline bool IsInit() const noexcept { return m_is_init; }
inline Instance& GetInstance() noexcept { return m_instance; }
inline Device& GetDevice() noexcept { return m_device; }
inline Queues& GetQueue() noexcept { return m_queues; }
inline GPUallocator& GetAllocator() noexcept { return m_allocator; }
inline ValidationLayers& GetLayers() noexcept { return m_layers; }
inline CommandBuffer& GetSingleTimeCmdBuffer() noexcept { return m_cmd_manager.GetCmdBuffer(); }
inline SingleTimeCmdManager& GetSingleTimeCmdManager() noexcept { return m_cmd_manager; }
inline DescriptorPool& GetDescriptorPool() { return m_pool_manager.GetAvailablePool(); }
private:
RenderCore() = default;
~RenderCore() = default;
private:
ValidationLayers m_layers;
SingleTimeCmdManager m_cmd_manager;
Queues m_queues;
DescriptorPoolManager m_pool_manager;
Device m_device;
Instance m_instance;
GPUallocator m_allocator;
bool m_is_init = false;
};
}
#endif // __MLX_RENDER_CORE__

31
runtime/Includes/Renderer/Core/Semaphore.h git.filemode.normal_file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Semaphore.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 18:59:38 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:56:51 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_SEMAPHORE__
#define __MLX_VK_SEMAPHORE__
namespace mlx
{
class Semaphore
{
public:
void Init();
void Destroy() noexcept;
inline VkSemaphore& Get() noexcept { return m_semaphore; }
private:
VkSemaphore m_semaphore = VK_NULL_HANDLE;
};
}
#endif // __MLX_VK_SEMAPHORE__

34
runtime/Includes/Renderer/Core/Surface.h git.filemode.normal_file
View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Surface.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 18:57:55 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:58:15 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_SURFACE__
#define __MLX_VK_SURFACE__
namespace mlx
{
class Surface
{
public:
void Create(class Renderer& renderer);
void Destroy() noexcept;
VkSurfaceFormatKHR ChooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& available_formats);
inline VkSurfaceKHR& operator()() noexcept { return m_surface; }
inline VkSurfaceKHR& Get() noexcept { return m_surface; }
private:
VkSurfaceKHR m_surface = VK_NULL_HANDLE;
};
}
#endif // __MLX_VK_SURFACE__

View File

@@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ValidationLayers.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/19 14:04:25 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:59:00 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __VK_VALIDATION_LAYERS__
#define __VK_VALIDATION_LAYERS__
namespace mlx
{
class ValidationLayers
{
public:
ValidationLayers() = default;
void Init();
void Destroy();
bool CheckValidationLayerSupport();
void PopulateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& create_info);
VkResult SetDebugUtilsObjectNameEXT(VkObjectType object_type, std::uint64_t object_handle, const char* object_name);
~ValidationLayers() = default;
private:
VkResult CreateDebugUtilsMessengerEXT(const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator);
static VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity, VkDebugUtilsMessageTypeFlagsEXT message_type, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* pUserData);
void DestroyDebugUtilsMessengerEXT(const VkAllocationCallbacks* pAllocator);
private:
VkDebugUtilsMessengerEXT m_debug_messenger;
PFN_vkSetDebugUtilsObjectNameEXT f_vkSetDebugUtilsObjectNameEXT = nullptr;
};
}
#endif

View File

@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* DescriptorPool.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/23 18:32:43 by maldavid #+# #+# */
/* Updated: 2024/03/27 23:00:29 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __VK_DESCRIPTOR_POOL__
#define __VK_DESCRIPTOR_POOL__
namespace mlx
{
class DescriptorPool
{
public:
DescriptorPool() = default;
void Init(std::size_t n, NonOwningPtr<VkDescriptorPoolSize> size);
void FreeDescriptor(const class DescriptorSet& set);
void Destroy() noexcept;
inline VkDescriptorPool& operator()() noexcept { return m_pool; }
inline VkDescriptorPool& Get() noexcept { return m_pool; }
inline std::size_t GetNumberOfSetsAllocated() const noexcept { return m_allocated_sets; }
inline bool IsInit() const noexcept { return m_pool != VK_NULL_HANDLE; }
~DescriptorPool() = default;
private:
VkDescriptorPool m_pool = VK_NULL_HANDLE;
std::size_t m_allocated_sets = 0;
};
}
#endif

View File

@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* DescriptorPoolManager.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/20 06:26:26 by maldavid #+# #+# */
/* Updated: 2024/03/27 23:00:56 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_DESCRIPTOR_POOL_MANAGER__
#define __MLX_DESCRIPTOR_POOL_MANAGER__
#include <Renderer/Descriptors/DescriptorPool.h>
namespace mlx
{
class DescriptorPoolManager
{
public:
DescriptorPoolManager() = default;
DescriptorPool& GetAvailablePool(); // assumes the pool is for only one set allocation, may cause some issues if this is for more than one
void DestroyAllPools();
~DescriptorPoolManager() = default;
private:
std::list<DescriptorPool> m_pools;
};
}
#endif

View File

@@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* DescriptorSet.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/23 18:39:36 by maldavid #+# #+# */
/* Updated: 2024/03/27 23:02:38 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __VK_DESCRIPTOR_SET__
#define __VK_DESCRIPTOR_SET__
#include <Renderer/Core/RenderCore.h>
namespace mlx
{
class DescriptorSet
{
public:
DescriptorSet() = default;
void Init(class Renderer* renderer, class DescriptorPool* pool, class DescriptorSetLayout* layout);
void WriteDescriptor(int binding, NonOwningPtr<class UniformBuffer> ubo) const noexcept;
void WriteDescriptor(int binding, const class Image& image) const noexcept;
inline bool IsInit() const noexcept { return m_pool != nullptr && m_renderer != nullptr; }
DescriptorSet Duplicate();
VkDescriptorSet& operator()() noexcept;
VkDescriptorSet& Get() noexcept;
inline const std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT>& GetAllFramesDescriptorSets() const { return m_desc_set; }
void Destroy() noexcept;
~DescriptorSet() = default;
private:
std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT> m_desc_set;
NonOwningPtr<class DescriptorPool> p_pool;
NonOwningPtr<class DescriptorSetLayout> p_layout;
NonOwningPtr<class Renderer> p_renderer;
};
}
#endif

View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* DescriptorSetLayout.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/23 18:36:22 by maldavid #+# #+# */
/* Updated: 2024/03/27 23:03:04 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __VK_DESCRIPTOR_SET_LAYOUT__
#define __VK_DESCRIPTOR_SET_LAYOUT__
namespace mlx
{
class DescriptorSetLayout
{
public:
DescriptorSetLayout() = default;
void Init(std::vector<std::pair<int, VkDescriptorType>> binds, VkShaderStageFlagBits stage);
void Destroy() noexcept;
inline VkDescriptorSetLayout& operator()() noexcept { return m_layout; }
inline VkDescriptorSetLayout& Get() noexcept { return m_layout; }
inline const std::vector<std::pair<int, VkDescriptorType>>& GetBindings() const noexcept { return m_bindings; }
~DescriptorSetLayout() = default;
private:
std::vector<std::pair<int, VkDescriptorType>> m_bindings;
VkDescriptorSetLayout m_layout = VK_NULL_HANDLE;
};
}
#endif

59
runtime/Includes/Renderer/Enums.h git.filemode.normal_file
View File

@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Enums.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 22:02:58 by maldavid #+# #+# */
/* Updated: 2024/03/27 22:39:31 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_RENDERER_ENUMS__
#define __MLX_RENDERER_ENUMS__
namespace mlx
{
enum class BufferType
{
Constant = 0,
HighDynamic, // typically stored in RAM
LowDynamic, // typically stored in VRAM
EndEnum
};
constexpr std::size_t BufferTypeCount = static_cast<std::size_t>(BufferType::EndEnum);
enum class CommandResourceState
{
Held = 0,
Free,
EndEnum
};
constexpr std::size_t CommandResourceStateCount = static_cast<std::size_t>(CommandResourceState::EndEnum);
enum class CommandBufferState
{
Uninit = 0, // buffer not initialized or destroyed
Ready, // buffer ready to be used after having been submitted
Idle, // buffer has recorded informations but has not been submitted
Recording, // buffer is currently recording
Submitted, // buffer has been submitted
EndEnum
};
constexpr std::size_t CommandBufferStateCount = static_cast<std::size_t>(CommandBufferState::EndEnum);
enum class CommandBufferType
{
SingleTime = 0,
LongTime,
EndEnum
};
constexpr std::size_t CommandBufferTypeCount = static_cast<std::size_t>(CommandBufferType::EndEnum);
}
#endif

88
runtime/Includes/Renderer/Images/Image.h git.filemode.normal_file
View File

@@ -0,0 +1,88 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vk_image.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/25 11:54:21 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:09:40 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_IMAGE__
#define __MLX_VK_IMAGE__
#include <renderer/core/cmd_resource.h>
#include <renderer/command/vk_cmd_buffer.h>
#include <renderer/command/vk_cmd_pool.h>
#ifdef DEBUG
#include <string>
#endif
namespace mlx
{
std::uint32_t formatSize(VkFormat format);
bool isStencilFormat(VkFormat format);
bool isDepthFormat(VkFormat format);
VkFormat bitsToFormat(std::uint32_t bits);
VkPipelineStageFlags layoutToAccessMask(VkImageLayout layout, bool isDestination);
class Image : public CmdResource
{
friend class SwapChain;
public:
Image() = default;
inline void create(VkImage image, VkFormat format, std::uint32_t width, std::uint32_t height, VkImageLayout layout = VK_IMAGE_LAYOUT_UNDEFINED) noexcept
{
_image = image;
_format = format;
_width = width;
_height = height;
_layout = layout;
}
void create(std::uint32_t width, std::uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, const char* name, bool decated_memory = false);
void createImageView(VkImageViewType type, VkImageAspectFlags aspectFlags) noexcept;
void createSampler() noexcept;
void copyFromBuffer(class Buffer& buffer);
void copyToBuffer(class Buffer& buffer);
void transitionLayout(VkImageLayout new_layout, CmdBuffer* cmd = nullptr);
virtual void destroy() noexcept;
inline VkImage get() noexcept { return _image; }
inline VkImage operator()() noexcept { return _image; }
inline VkImageView getImageView() const noexcept { return _image_view; }
inline VkFormat getFormat() const noexcept { return _format; }
inline VkImageTiling getTiling() const noexcept { return _tiling; }
inline VkImageLayout getLayout() const noexcept { return _layout; }
inline VkSampler getSampler() const noexcept { return _sampler; }
inline std::uint32_t getWidth() const noexcept { return _width; }
inline std::uint32_t getHeight() const noexcept { return _height; }
inline bool isInit() const noexcept { return _image != VK_NULL_HANDLE; }
virtual ~Image() = default;
private:
void destroySampler() noexcept;
void destroyImageView() noexcept;
private:
VmaAllocation _allocation;
VkImage _image = VK_NULL_HANDLE;
VkImageView _image_view = VK_NULL_HANDLE;
VkSampler _sampler = VK_NULL_HANDLE;
#ifdef DEBUG
std::string _name;
#endif
VkFormat _format;
VkImageTiling _tiling;
VkImageLayout _layout = VK_IMAGE_LAYOUT_UNDEFINED;
std::uint32_t _width = 0;
std::uint32_t _height = 0;
};
}
#endif

63
runtime/Includes/Renderer/Images/Texture.h git.filemode.normal_file
View File

@@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* texture.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 02:24:58 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:09:56 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_TEXTURE__
#define __MLX_TEXTURE__
#include <renderer/images/vk_image.h>
#include <renderer/descriptors/vk_descriptor_set.h>
#include <renderer/buffers/vk_ibo.h>
#include <renderer/buffers/vk_vbo.h>
namespace mlx
{
class Texture : public Image
{
public:
Texture() = default;
void create(std::uint8_t* pixels, std::uint32_t width, std::uint32_t height, VkFormat format, const char* name, bool dedicated_memory = false);
void render(std::array<VkDescriptorSet, 2>& sets, class Renderer& renderer, int x, int y);
void destroy() noexcept override;
void setPixel(int x, int y, std::uint32_t color) noexcept;
int getPixel(int x, int y) noexcept;
inline void setDescriptor(DescriptorSet&& set) noexcept { _set = set; }
inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; }
inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, *this); _has_set_been_updated = true; }
inline bool hasBeenUpdated() const noexcept { return _has_set_been_updated; }
inline constexpr void resetUpdate() noexcept { _has_set_been_updated = false; }
~Texture() = default;
private:
void openCPUmap();
private:
C_VBO _vbo;
C_IBO _ibo;
#ifdef DEBUG
std::string _name;
#endif
DescriptorSet _set;
std::vector<std::uint32_t> _cpu_map;
std::optional<Buffer> _buf_map = std::nullopt;
void* _map = nullptr;
bool _has_been_modified = false;
bool _has_set_been_updated = false;
};
Texture stbTextureLoad(std::filesystem::path file, int* w, int* h);
}
#endif

View File

@@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* texture_atlas.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/07 16:36:33 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:09:50 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_TEXTURE_ATLAS__
#define __MLX_TEXTURE_ATLAS__
#include <renderer/images/texture.h>
namespace mlx
{
class TextureAtlas : public Image
{
public:
TextureAtlas() = default;
void create(std::uint8_t* pixels, std::uint32_t width, std::uint32_t height, VkFormat format, const char* name, bool dedicated_memory = false);
void render(class Renderer& renderer, int x, int y, std::uint32_t ibo_size) const;
void destroy() noexcept override;
inline void setDescriptor(DescriptorSet&& set) noexcept { _set = set; }
inline VkDescriptorSet getVkSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; }
inline DescriptorSet getSet() noexcept { return _set; }
inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, *this); _has_been_updated = true; }
inline bool hasBeenUpdated() const noexcept { return _has_been_updated; }
inline constexpr void resetUpdate() noexcept { _has_been_updated = false; }
~TextureAtlas() = default;
private:
DescriptorSet _set;
bool _has_been_updated = false;
};
}
#endif

View File

@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* texture_descriptor.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 01:00:13 by maldavid #+# #+# */
/* Updated: 2024/01/11 01:21:52 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_TEXTURE_DESCRIPTOR__
#define __MLX_TEXTURE_DESCRIPTOR__
#include <renderer/images/texture.h>
#include <renderer/core/drawable_resource.h>
#include <utils/combine_hash.h>
namespace mlx
{
struct TextureRenderDescriptor : public DrawableResource
{
Texture* texture;
int x;
int y;
TextureRenderDescriptor(Texture* _texture, int _x, int _y) : texture(_texture), x(_x), y(_y) {}
inline bool operator==(const TextureRenderDescriptor& rhs) const { return texture == rhs.texture && x == rhs.x && y == rhs.y; }
inline void render(std::array<VkDescriptorSet, 2>& sets, class Renderer& renderer) override
{
if(!texture->isInit())
return;
texture->render(sets, renderer, x, y);
}
inline void resetUpdate() override
{
if(!texture->isInit())
return;
texture->resetUpdate();
}
};
}
namespace std
{
template <>
struct hash<mlx::TextureRenderDescriptor>
{
std::size_t operator()(const mlx::TextureRenderDescriptor& d) const noexcept
{
std::size_t hash = 0;
mlx::hashCombine(hash, d.texture, d.x, d.y);
return hash;
}
};
}
#endif

View File

@@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* texture_manager.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 00:56:15 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:09:45 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_TEXTURE_MANAGER__
#define __MLX_TEXTURE_MANAGER__
#include <renderer/images/texture_descriptor.h>
#include <core/profiler.h>
namespace mlx
{
class TextureManager
{
public:
TextureManager() = default;
inline void clear() { _texture_descriptors.clear(); }
inline std::pair<DrawableResource*, bool> registerTexture(Texture* texture, int x, int y)
{
MLX_PROFILE_FUNCTION();
auto res = _texture_descriptors.emplace(texture, x, y);
return std::make_pair(static_cast<DrawableResource*>(&const_cast<TextureRenderDescriptor&>(*res.first)), res.second);
}
inline bool isTextureKnown(Texture* texture) noexcept
{
MLX_PROFILE_FUNCTION();
for(const auto& desc : _texture_descriptors)
{
if(desc.texture == texture)
return true;
}
return false;
}
inline void eraseTextures(Texture* texture)
{
MLX_PROFILE_FUNCTION();
for(auto it = _texture_descriptors.begin(); it != _texture_descriptors.end();)
{
if(it->texture == texture)
it = _texture_descriptors.erase(it);
else
++it;
}
}
~TextureManager() = default;
private:
std::unordered_set<TextureRenderDescriptor> _texture_descriptors;
};
}
#endif

View File

@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipeline.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/18 21:23:52 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:09:01 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __PIPELINE__
#define __PIPELINE__
#include <renderer/command/vk_cmd_buffer.h>
namespace mlx
{
class GraphicPipeline
{
public:
void init(class Renderer& renderer);
void destroy() noexcept;
inline void bindPipeline(CmdBuffer& command_buffer) noexcept { vkCmdBindPipeline(command_buffer.get(), VK_PIPELINE_BIND_POINT_GRAPHICS, _graphics_pipeline); }
inline const VkPipeline& getPipeline() const noexcept { return _graphics_pipeline; }
inline const VkPipelineLayout& getPipelineLayout() const noexcept { return _pipeline_layout; }
private:
VkPipeline _graphics_pipeline = VK_NULL_HANDLE;
VkPipelineLayout _pipeline_layout = VK_NULL_HANDLE;
};
}
#endif

48
runtime/Includes/Renderer/PixelPut.h git.filemode.normal_file
View File

@@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pixel_put.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/31 13:18:50 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:07:56 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_PIXEL_PUT__
#define __MLX_PIXEL_PUT__
#include <renderer/images/texture.h>
#include <renderer/descriptors/vk_descriptor_set.h>
namespace mlx
{
class PixelPutPipeline
{
public:
PixelPutPipeline() = default;
void init(std::uint32_t width, std::uint32_t height, class Renderer& renderer) noexcept;
void setPixel(int x, int y, std::uint32_t color) noexcept;
void render(std::array<VkDescriptorSet, 2>& sets, class Renderer& renderer) noexcept;
void clear();
void destroy() noexcept;
~PixelPutPipeline();
private:
Texture _texture;
Buffer _buffer;
// using vector as CPU map and not directly writting to mapped buffer to improve performances
std::vector<std::uint32_t> _cpu_map;
void* _buffer_map = nullptr;
std::uint32_t _width = 0;
std::uint32_t _height = 0;
bool _has_been_modified = true;
};
}
#endif

142
runtime/Includes/Renderer/Renderer.h git.filemode.normal_file
View File

@@ -0,0 +1,142 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* renderer.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/18 17:14:45 by maldavid #+# #+# */
/* Updated: 2024/03/27 00:31:28 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __RENDERER__
#define __RENDERER__
#include <renderer/buffers/vk_ubo.h>
#include <renderer/core/vk_surface.h>
#include <renderer/core/render_core.h>
#include <renderer/core/vk_semaphore.h>
#include <renderer/pipeline/pipeline.h>
#include <renderer/command/cmd_manager.h>
#include <renderer/swapchain/vk_swapchain.h>
#include <renderer/renderpass/vk_render_pass.h>
#include <renderer/renderpass/vk_framebuffer.h>
#include <renderer/descriptors/vk_descriptor_set.h>
#include <renderer/descriptors/vk_descriptor_pool.h>
#include <renderer/descriptors/vk_descriptor_set_layout.h>
#include <core/errors.h>
#include <mlx_profile.h>
#include <glm/glm.hpp>
namespace mlx
{
struct Vertex
{
glm::vec2 pos;
glm::vec4 color;
glm::vec2 uv;
Vertex(glm::vec2 _pos, glm::vec4 _color, glm::vec2 _uv) : pos(std::move(_pos)), color(std::move(_color)), uv(std::move(_uv)) {}
static VkVertexInputBindingDescription getBindingDescription()
{
VkVertexInputBindingDescription bindingDescription{};
bindingDescription.binding = 0;
bindingDescription.stride = sizeof(Vertex);
bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
return bindingDescription;
}
static std::array<VkVertexInputAttributeDescription, 3> getAttributeDescriptions()
{
std::array<VkVertexInputAttributeDescription, 3> attributeDescriptions;
attributeDescriptions[0].binding = 0;
attributeDescriptions[0].location = 0;
attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT;
attributeDescriptions[0].offset = offsetof(Vertex, pos);
attributeDescriptions[1].binding = 0;
attributeDescriptions[1].location = 1;
attributeDescriptions[1].format = VK_FORMAT_R32G32B32A32_SFLOAT;
attributeDescriptions[1].offset = offsetof(Vertex, color);
attributeDescriptions[2].binding = 0;
attributeDescriptions[2].location = 2;
attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT;
attributeDescriptions[2].offset = offsetof(Vertex, uv);
return attributeDescriptions;
}
};
class Renderer
{
public:
Renderer() = default;
void init(class Texture* render_target);
bool beginFrame();
void endFrame();
void destroy();
inline class Window* getWindow() { return _window; }
inline void setWindow(class Window* window) { _window = window; }
inline Surface& getSurface() noexcept { return _surface; }
inline CmdPool& getCmdPool() noexcept { return _cmd.getCmdPool(); }
inline UBO* getUniformBuffer() noexcept { return _uniform_buffer.get(); }
inline SwapChain& getSwapChain() noexcept { return _swapchain; }
inline Semaphore& getSemaphore(int i) noexcept { return _semaphores[i]; }
inline RenderPass& getRenderPass() noexcept { return _pass; }
inline GraphicPipeline& getPipeline() noexcept { return _pipeline; }
inline CmdBuffer& getCmdBuffer(int i) noexcept { return _cmd.getCmdBuffer(i); }
inline CmdBuffer& getActiveCmdBuffer() noexcept { return _cmd.getCmdBuffer(_current_frame_index); }
inline FrameBuffer& getFrameBuffer(int i) noexcept { return _framebuffers[i]; }
inline DescriptorSet& getVertDescriptorSet() noexcept { return _vert_set; }
inline DescriptorSet& getFragDescriptorSet() noexcept { return _frag_set; }
inline DescriptorSetLayout& getVertDescriptorSetLayout() noexcept { return _vert_layout; }
inline DescriptorSetLayout& getFragDescriptorSetLayout() noexcept { return _frag_layout; }
inline std::uint32_t getActiveImageIndex() noexcept { return _current_frame_index; }
inline std::uint32_t getImageIndex() noexcept { return _image_index; }
constexpr inline void requireFrameBufferResize() noexcept { _framebuffer_resized = true; }
~Renderer() = default;
private:
void recreateRenderData();
private:
GraphicPipeline _pipeline;
CmdManager _cmd;
RenderPass _pass;
Surface _surface;
SwapChain _swapchain;
std::array<Semaphore, MAX_FRAMES_IN_FLIGHT> _semaphores;
std::vector<FrameBuffer> _framebuffers;
DescriptorSetLayout _vert_layout;
DescriptorSetLayout _frag_layout;
DescriptorSet _vert_set;
DescriptorSet _frag_set;
std::unique_ptr<UBO> _uniform_buffer;
class Window* _window = nullptr;
class Texture* _render_target = nullptr;
std::uint32_t _current_frame_index = 0;
std::uint32_t _image_index = 0;
bool _framebuffer_resized = false;
};
}
#endif

View File

@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vk_framebuffer.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:19:44 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:08:37 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_FRAMEBUFFER__
#define __MLX_VK_FRAMEBUFFER__
namespace mlx
{
class FrameBuffer
{
public:
void init(class RenderPass& renderpass, class Image& image);
void destroy() noexcept;
inline VkFramebuffer& operator()() noexcept { return _framebuffer; }
inline VkFramebuffer& get() noexcept { return _framebuffer; }
inline std::uint32_t getWidth() const noexcept { return _width; }
inline std::uint32_t getHeight() const noexcept { return _height; }
private:
VkFramebuffer _framebuffer = VK_NULL_HANDLE;
std::uint32_t _width = 0;
std::uint32_t _height = 0;
};
}
#endif // __MLX_VK_FRAMEBUFFER__

View File

@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vk_render_pass.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:22:00 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:08:30 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_RENDER_PASS__
#define __MLX_VK_RENDER_PASS__
namespace mlx
{
class RenderPass
{
public:
void init(VkFormat attachement_format, VkImageLayout layout);
void destroy() noexcept;
void begin(class CmdBuffer& cmd, class FrameBuffer& fb);
void end(class CmdBuffer& cmd);
inline VkRenderPass& operator()() noexcept { return _render_pass; }
inline VkRenderPass& get() noexcept { return _render_pass; }
private:
VkRenderPass _render_pass = VK_NULL_HANDLE;
bool _is_running = false;
};
}
#endif // __MLX_VK_RENDER_PASS__

View File

@@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vk_swapchain.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 18:23:27 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:08:26 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_VK_SWAPCHAIN__
#define __MLX_VK_SWAPCHAIN__
#include <renderer/images/vk_image.h>
namespace mlx
{
class SwapChain
{
friend class GraphicPipeline;
friend class RenderPass;
friend class Renderer;
public:
struct SwapChainSupportDetails
{
VkSurfaceCapabilitiesKHR capabilities;
std::vector<VkSurfaceFormatKHR> formats;
std::vector<VkPresentModeKHR> present_modes;
};
public:
SwapChain() = default;
void init(class Renderer* renderer);
void recreate();
void destroy() noexcept;
SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device);
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities);
VkPresentModeKHR chooseSwapPresentMode([[maybe_unused]] const std::vector<VkPresentModeKHR> &availablePresentModes);
inline VkSwapchainKHR get() noexcept { return _swapchain; }
inline VkSwapchainKHR operator()() noexcept { return _swapchain; }
inline std::size_t getImagesNumber() const noexcept { return _images.size(); }
inline Image& getImage(std::size_t i) noexcept { return _images[i]; }
inline SwapChainSupportDetails getSupport() noexcept { return _swapchain_support; }
inline VkExtent2D getExtent() noexcept { return _extent; }
inline VkFormat getImagesFormat() const noexcept { return _swapchain_image_format; }
~SwapChain() = default;
private:
SwapChainSupportDetails _swapchain_support;
VkSwapchainKHR _swapchain;
std::vector<Image> _images;
VkFormat _swapchain_image_format;
VkExtent2D _extent;
class Renderer* _renderer = nullptr;
};
}
#endif // __MLX_VK_SWAPCHAIN__

53
runtime/Includes/Renderer/Texts/Font.h git.filemode.normal_file
View File

@@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* font.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 21:17:04 by kbz_8 #+# #+# */
/* Updated: 2024/03/25 19:08:21 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_FONT__
#define __MLX_FONT__
#include <renderer/images/texture_atlas.h>
#include <utils/combine_hash.h>
namespace mlx
{
class Font
{
friend class FontLibrary;
public:
Font() = delete;
Font(class Renderer& renderer, const std::filesystem::path& path, float scale);
Font(class Renderer& renderer, const std::string& name, const std::vector<std::uint8_t>& ttf_data, float scale);
inline const std::string& getName() const { return _name; }
inline float getScale() const noexcept { return _scale; }
inline const std::array<stbtt_packedchar, 96>& getCharData() const { return _cdata; }
inline const TextureAtlas& getAtlas() const noexcept { return _atlas; }
inline bool operator==(const Font& rhs) const { return rhs._name == _name && rhs._scale == _scale; }
inline bool operator!=(const Font& rhs) const { return rhs._name != _name || rhs._scale != _scale; }
void destroy();
~Font();
private:
void buildFont();
private:
std::array<stbtt_packedchar, 96> _cdata;
TextureAtlas _atlas;
std::variant<std::filesystem::path, std::vector<std::uint8_t>> _build_data;
std::string _name;
class Renderer& _renderer;
float _scale = 0;
bool _is_init = false;
};
}
#endif

47
runtime/Includes/Renderer/Texts/FontLibrary.h git.filemode.normal_file
View File

@@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* font_library.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 09:26:03 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:08:18 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_FONT_LIBRARY__
#define __MLX_FONT_LIBRARY__
#include <renderer/texts/font.h>
#include <renderer/core/render_core.h>
#include <utils/singleton.h>
namespace mlx
{
using FontID = std::uint32_t;
constexpr FontID nullfont = 0;
class FontLibrary : public Singleton<FontLibrary>
{
friend class Singleton<FontLibrary>;
public:
std::shared_ptr<class Font> getFontData(FontID id);
FontID addFontToLibrary(std::shared_ptr<Font> font);
void removeFontFromLibrary(FontID id);
void clearLibrary();
private:
FontLibrary() = default;
~FontLibrary() = default;
private:
std::unordered_map<FontID, std::shared_ptr<class Font>> _cache;
std::vector<FontID> _invalid_ids;
FontID _current_id = 1;
};
}
#endif

49
runtime/Includes/Renderer/Texts/Text.h git.filemode.normal_file
View File

@@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* text.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 00:09:04 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:08:15 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_TEXT__
#define __MLX_TEXT__
#include <renderer/texts/font.h>
#include <renderer/texts/font_library.h>
#include <renderer/buffers/vk_ibo.h>
#include <renderer/buffers/vk_vbo.h>
namespace mlx
{
class Text
{
public:
Text() = default;
void init(std::string text, FontID font, std::uint32_t color, std::vector<Vertex> vbo_data, std::vector<std::uint16_t> ibo_data);
void bind(class Renderer& renderer) noexcept;
inline FontID getFontInUse() const noexcept { return _font; }
void updateVertexData(int frame, std::vector<Vertex> vbo_data);
inline std::uint32_t getIBOsize() noexcept { return _ibo.getSize(); }
inline const std::string& getText() const { return _text; }
inline std::uint32_t getColor() const noexcept { return _color; }
void destroy() noexcept;
~Text();
private:
std::array<D_VBO, MAX_FRAMES_IN_FLIGHT> _vbo;
C_IBO _ibo;
std::string _text;
std::uint32_t _color;
FontID _font = nullfont;
bool _is_init = false;
};
}
#endif

View File

@@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* text_descriptor.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 00:13:34 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:08:11 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_TEXT_DESCRIPTOR__
#define __MLX_TEXT_DESCRIPTOR__
#include <utils/combine_hash.h>
#include <renderer/core/drawable_resource.h>
#include <renderer/texts/text_library.h>
#include <renderer/texts/font_library.h>
namespace mlx
{
class TextDrawDescriptor : public DrawableResource
{
friend class std::hash<TextDrawDescriptor>;
public:
TextID id;
std::uint32_t color;
int x;
int y;
public:
TextDrawDescriptor(std::string text, std::uint32_t _color, int _x, int _y);
void init(FontID font) noexcept;
bool operator==(const TextDrawDescriptor& rhs) const { return _text == rhs._text && x == rhs.x && y == rhs.y && color == rhs.color; }
void render(std::array<VkDescriptorSet, 2>& sets, Renderer& renderer) override;
void resetUpdate() override;
TextDrawDescriptor() = default;
private:
std::string _text;
};
}
namespace std
{
template <>
struct hash<mlx::TextDrawDescriptor>
{
std::size_t operator()(const mlx::TextDrawDescriptor& d) const noexcept
{
std::size_t hash = 0;
mlx::hashCombine(hash, d.x, d.y, d.color, d._text);
return hash;
}
};
}
#endif

48
runtime/Includes/Renderer/Texts/TextLibrary.h git.filemode.normal_file
View File

@@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* text_library.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/10 11:52:30 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:08:03 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_TEXT_LIBRARY__
#define __MLX_TEXT_LIBRARY__
#include <renderer/buffers/vk_vbo.h>
#include <renderer/buffers/vk_ibo.h>
#include <renderer/texts/font.h>
#include <renderer/core/render_core.h>
#include <utils/singleton.h>
namespace mlx
{
using TextID = std::uint32_t;
constexpr TextID nulltext = 0;
class TextLibrary : public Singleton<TextLibrary>
{
friend class Singleton<TextLibrary>;
public:
std::shared_ptr<class Text> getTextData(TextID id);
TextID addTextToLibrary(std::shared_ptr<Text> text);
void removeTextFromLibrary(TextID id);
void clearLibrary();
private:
TextLibrary() = default;
~TextLibrary() = default;
private:
std::unordered_map<TextID, std::shared_ptr<class Text>> _cache;
TextID _current_id = 1;
};
}
#endif

43
runtime/Includes/Renderer/Texts/TextManager.h git.filemode.normal_file
View File

@@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* text_manager.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/06 16:24:11 by maldavid #+# #+# */
/* Updated: 2024/03/25 19:08:00 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_TEXT_MANAGER__
#define __MLX_TEXT_MANAGER__
#include <renderer/renderer.h>
#include <renderer/images/texture_atlas.h>
#include <renderer/texts/text_descriptor.h>
#include <renderer/texts/text_library.h>
#include <renderer/texts/font_library.h>
namespace mlx
{
class TextManager
{
public:
TextManager() = default;
void init(Renderer& renderer) noexcept;
std::pair<DrawableResource*, bool> registerText(int x, int y, std::uint32_t color, std::string str);
inline void clear() { _text_descriptors.clear(); }
void loadFont(Renderer& renderer, const std::filesystem::path& filepath, float scale);
void destroy() noexcept;
~TextManager() = default;
private:
std::unordered_set<TextDrawDescriptor> _text_descriptors;
FontID _font_in_use = nullfont;
};
}
#endif