mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-11 14:43:34 +00:00
fixing put pixel, adding scene change checker
This commit is contained in:
@@ -19,7 +19,6 @@ namespace mlx
|
||||
|
||||
enum class Event
|
||||
{
|
||||
DescriptorPoolResetEventCode = 55,
|
||||
ResizeEventCode = 56,
|
||||
FrameBeginEventCode = 57,
|
||||
FatalErrorEventCode = 168,
|
||||
|
||||
@@ -44,6 +44,8 @@ namespace mlx
|
||||
std::shared_ptr<Window> p_window;
|
||||
std::unique_ptr<Scene> p_scene;
|
||||
|
||||
std::uint64_t m_draw_layer = 0;
|
||||
|
||||
int m_id;
|
||||
|
||||
bool m_has_window;
|
||||
|
||||
@@ -9,6 +9,7 @@ namespace mlx
|
||||
p_scene->ResetSprites();
|
||||
m_put_pixel_manager.ResetRenderData();
|
||||
m_insert_new_pixel_put_texture = true;
|
||||
m_draw_layer = 0;
|
||||
}
|
||||
|
||||
void GraphicsSupport::PixelPut(int x, int y, std::uint32_t color) noexcept
|
||||
@@ -19,6 +20,7 @@ namespace mlx
|
||||
{
|
||||
Sprite& new_sprite = p_scene->CreateSprite(texture);
|
||||
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
|
||||
m_draw_layer++;
|
||||
}
|
||||
m_insert_new_pixel_put_texture = false;
|
||||
}
|
||||
@@ -42,8 +44,12 @@ namespace mlx
|
||||
new_sprite.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
|
||||
m_insert_new_pixel_put_texture = true;
|
||||
}
|
||||
else
|
||||
else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer))
|
||||
{
|
||||
p_scene->BringToFront(std::move(sprite));
|
||||
m_insert_new_pixel_put_texture = true;
|
||||
}
|
||||
m_draw_layer++;
|
||||
}
|
||||
|
||||
void GraphicsSupport::LoadFont(const std::filesystem::path& filepath, float scale)
|
||||
|
||||
@@ -48,6 +48,21 @@ namespace mlx
|
||||
private:
|
||||
std::vector<SubMesh> m_sub_meshes;
|
||||
};
|
||||
|
||||
class MeshRegistry
|
||||
{
|
||||
public:
|
||||
MeshRegistry() = default;
|
||||
|
||||
inline void RegisterMesh(std::shared_ptr<Mesh> mesh);
|
||||
inline void UnregisterMesh(std::shared_ptr<Mesh> mesh);
|
||||
inline bool IsMeshKnown(std::shared_ptr<Mesh> mesh);
|
||||
|
||||
~MeshRegistry() = default;
|
||||
|
||||
private:
|
||||
std::unordered_set<std::shared_ptr<Mesh>> m_mesh_registry;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -7,32 +7,25 @@
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
struct SceneDescriptor
|
||||
{
|
||||
NonOwningPtr<Renderer> renderer;
|
||||
// More description may come in future
|
||||
};
|
||||
|
||||
class Scene
|
||||
{
|
||||
public:
|
||||
Scene(SceneDescriptor desc);
|
||||
Scene() = default;
|
||||
|
||||
Sprite& CreateSprite(NonOwningPtr<class Texture> texture) noexcept;
|
||||
NonOwningPtr<Sprite> GetSpriteFromTextureAndPosition(NonOwningPtr<Texture> texture, const Vec2f& position) const;
|
||||
void BringToFront(NonOwningPtr<Sprite> sprite);
|
||||
void TryEraseSpriteFromTexture(NonOwningPtr<Texture> texture);
|
||||
bool IsTextureAtGivenDrawLayer(NonOwningPtr<Texture> texture, std::uint64_t draw_layer) const;
|
||||
|
||||
inline void ResetSprites() { m_sprites.clear(); }
|
||||
|
||||
[[nodiscard]] MLX_FORCEINLINE const std::vector<std::shared_ptr<Sprite>>& GetSprites() const noexcept { return m_sprites; }
|
||||
[[nodiscard]] MLX_FORCEINLINE const SceneDescriptor& GetDescription() const noexcept { return m_descriptor; }
|
||||
[[nodiscard]] MLX_FORCEINLINE ViewerData& GetViewerData() noexcept { return m_viewer_data; }
|
||||
|
||||
~Scene() = default;
|
||||
|
||||
private:
|
||||
SceneDescriptor m_descriptor;
|
||||
std::vector<std::shared_ptr<Sprite>> m_sprites;
|
||||
ViewerData m_viewer_data;
|
||||
};
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace mlx
|
||||
friend class Render2DPass;
|
||||
|
||||
public:
|
||||
Sprite(class Renderer& renderer, NonOwningPtr<Texture> texture);
|
||||
Sprite(NonOwningPtr<Texture> texture);
|
||||
|
||||
inline void SetColor(Vec4f color) noexcept { m_color = color; }
|
||||
inline void SetPosition(Vec2f position) noexcept { m_position = position; }
|
||||
@@ -24,25 +24,27 @@ namespace mlx
|
||||
[[nodiscard]] MLX_FORCEINLINE std::shared_ptr<Mesh> GetMesh() const { return p_mesh; }
|
||||
[[nodiscard]] MLX_FORCEINLINE NonOwningPtr<Texture> GetTexture() const { return p_texture; }
|
||||
|
||||
~Sprite() = default;
|
||||
inline ~Sprite() { if(p_set) p_set->ReturnDescriptorSetToPool(); }
|
||||
|
||||
private:
|
||||
[[nodiscard]] inline bool IsSetInit() const noexcept { return m_set.IsInit(); }
|
||||
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t frame_index) const noexcept { return m_set.GetSet(frame_index); }
|
||||
[[nodiscard]] inline bool IsSetInit() const noexcept { return p_set && p_set->IsInit(); }
|
||||
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t frame_index) const noexcept { return p_set ? p_set->GetSet(frame_index) : VK_NULL_HANDLE; }
|
||||
|
||||
inline void UpdateDescriptorSet(const DescriptorSet& set)
|
||||
inline void UpdateDescriptorSet(std::shared_ptr<DescriptorSet> set)
|
||||
{
|
||||
m_set = set.Duplicate();
|
||||
p_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(set->GetShaderLayout(), set->GetShaderType());
|
||||
}
|
||||
|
||||
inline void Bind(std::size_t frame_index, VkCommandBuffer cmd)
|
||||
{
|
||||
m_set.SetImage(frame_index, 0, *p_texture);
|
||||
m_set.Update(frame_index, cmd);
|
||||
if(!p_set)
|
||||
return;
|
||||
p_set->SetImage(frame_index, 0, *p_texture);
|
||||
p_set->Update(frame_index, cmd);
|
||||
}
|
||||
|
||||
private:
|
||||
DescriptorSet m_set;
|
||||
std::shared_ptr<DescriptorSet> p_set;
|
||||
NonOwningPtr<Texture> p_texture;
|
||||
std::shared_ptr<Mesh> p_mesh;
|
||||
Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f };
|
||||
|
||||
@@ -13,7 +13,6 @@ namespace mlx
|
||||
NonOwningPtr<class GPUBuffer> uniform_buffer_ptr;
|
||||
NonOwningPtr<class Image> image_ptr;
|
||||
VkDescriptorType type;
|
||||
ShaderType shader_type;
|
||||
std::uint32_t binding;
|
||||
};
|
||||
|
||||
@@ -25,17 +24,18 @@ namespace mlx
|
||||
void Init() noexcept;
|
||||
void Destroy() noexcept;
|
||||
|
||||
VkDescriptorSet AllocateDescriptorSet(std::uint32_t frame_index, VkDescriptorSetLayout layout);
|
||||
std::shared_ptr<class DescriptorSet> RequestDescriptorSet(const ShaderSetLayout& layout, ShaderType shader_type);
|
||||
void ReturnDescriptorSet(std::shared_ptr<class DescriptorSet> set);
|
||||
|
||||
void ResetPoolFromFrameIndex(std::size_t frame_index);
|
||||
|
||||
[[nodiscard]] inline VkDescriptorPool Get(std::uint32_t index) const noexcept { return m_pools[index]; }
|
||||
[[nodiscard]] inline VkDescriptorPool Get() const noexcept { return m_pool; }
|
||||
[[nodiscard]] MLX_FORCEINLINE std::size_t GetNumberOfSetsAllocated() const noexcept { return m_allocation_count; }
|
||||
|
||||
~DescriptorPool() = default;
|
||||
|
||||
private:
|
||||
std::array<VkDescriptorPool, MAX_FRAMES_IN_FLIGHT> m_pools;
|
||||
std::vector<std::shared_ptr<class DescriptorSet>> m_free_sets;
|
||||
std::vector<std::shared_ptr<class DescriptorSet>> m_used_sets;
|
||||
VkDescriptorPool m_pool;
|
||||
std::size_t m_allocation_count = 0;
|
||||
};
|
||||
|
||||
@@ -44,42 +44,45 @@ namespace mlx
|
||||
public:
|
||||
DescriptorPoolManager() = default;
|
||||
|
||||
void ResetPoolsFromFrameIndex(std::size_t frame_index);
|
||||
DescriptorPool& GetAvailablePool();
|
||||
void Destroy();
|
||||
|
||||
~DescriptorPoolManager() = default;
|
||||
|
||||
private:
|
||||
std::list<DescriptorPool> m_pools;
|
||||
std::vector<DescriptorPool> m_pools;
|
||||
};
|
||||
|
||||
class DescriptorSet
|
||||
class DescriptorSet : public std::enable_shared_from_this<DescriptorSet>
|
||||
{
|
||||
public:
|
||||
DescriptorSet() { m_set.fill(VK_NULL_HANDLE); }
|
||||
DescriptorSet(DescriptorPoolManager& pools_manager, const ShaderSetLayout& layout, VkDescriptorSetLayout vklayout, ShaderType shader_type);
|
||||
friend DescriptorPool;
|
||||
|
||||
public:
|
||||
void SetImage(std::size_t i, std::uint32_t binding, class Image& image);
|
||||
void SetStorageBuffer(std::size_t i, std::uint32_t binding, class GPUBuffer& buffer);
|
||||
void SetUniformBuffer(std::size_t i, std::uint32_t binding, class GPUBuffer& buffer);
|
||||
void Update(std::size_t i, VkCommandBuffer cmd = VK_NULL_HANDLE) noexcept;
|
||||
void Reallocate(std::size_t frame_index) noexcept;
|
||||
|
||||
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t i) const noexcept { return m_set[i]; }
|
||||
[[nodiscard]] inline DescriptorSet Duplicate() const { return DescriptorSet{ *p_pools_manager, m_set_layout, m_descriptors }; }
|
||||
[[nodiscard]] inline bool IsInit() const noexcept { return m_set[0] != VK_NULL_HANDLE; }
|
||||
void ReturnDescriptorSetToPool();
|
||||
|
||||
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t i) const noexcept { return m_sets[i]; }
|
||||
[[nodiscard]] MLX_FORCEINLINE bool IsInit() const noexcept { return m_sets[0] != VK_NULL_HANDLE; }
|
||||
[[nodiscard]] MLX_FORCEINLINE VkDescriptorSetLayout GetVulkanLayout() const noexcept { return m_set_layout; }
|
||||
[[nodiscard]] MLX_FORCEINLINE const ShaderSetLayout& GetShaderLayout() const { return m_shader_layout; }
|
||||
[[nodiscard]] MLX_FORCEINLINE ShaderType GetShaderType() const noexcept { return m_shader_type; }
|
||||
|
||||
~DescriptorSet() = default;
|
||||
|
||||
private:
|
||||
DescriptorSet(DescriptorPoolManager& pools_manager, VkDescriptorSetLayout layout, const std::vector<Descriptor>& descriptors);
|
||||
DescriptorSet(DescriptorPool& pool, VkDescriptorSetLayout vulkan_layout, const ShaderSetLayout& layout, std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT> vulkan_sets, ShaderType shader_type);
|
||||
|
||||
private:
|
||||
ShaderSetLayout m_shader_layout;
|
||||
std::vector<Descriptor> m_descriptors;
|
||||
std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT> m_set;
|
||||
std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT> m_sets;
|
||||
VkDescriptorSetLayout m_set_layout;
|
||||
NonOwningPtr<DescriptorPoolManager> p_pools_manager;
|
||||
ShaderType m_shader_type;
|
||||
DescriptorPool& m_pool;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -17,11 +17,16 @@ namespace mlx
|
||||
enum class ImageType
|
||||
{
|
||||
Color = 0,
|
||||
Depth,
|
||||
|
||||
EndEnum
|
||||
};
|
||||
constexpr std::size_t ImageTypeCount = static_cast<std::size_t>(ImageType::EndEnum);
|
||||
|
||||
enum class ShaderType
|
||||
{
|
||||
Vertex,
|
||||
Fragment
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -77,6 +77,7 @@ namespace mlx
|
||||
}
|
||||
|
||||
void Init(CPUBuffer pixels, std::uint32_t width, std::uint32_t height, VkFormat format, bool is_multisampled, [[maybe_unused]] std::string_view debug_name);
|
||||
void Destroy() noexcept override;
|
||||
|
||||
void SetPixel(int x, int y, std::uint32_t color) noexcept;
|
||||
int GetPixel(int x, int y) noexcept;
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
#ifndef __MLX_SHADER__
|
||||
#define __MLX_SHADER__
|
||||
|
||||
#include <Renderer/Enums.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
struct ShaderSetLayout
|
||||
{
|
||||
std::vector<std::pair<int, VkDescriptorType> > binds;
|
||||
std::vector<std::pair<int, VkDescriptorType>> binds;
|
||||
|
||||
ShaderSetLayout(std::vector<std::pair<int, VkDescriptorType> > b) : binds(std::move(b)) {}
|
||||
|
||||
inline bool operator==(const ShaderSetLayout& rhs) const { return binds == rhs.binds; }
|
||||
};
|
||||
|
||||
struct ShaderPushConstantLayout
|
||||
@@ -20,19 +24,12 @@ namespace mlx
|
||||
|
||||
struct ShaderLayout
|
||||
{
|
||||
std::vector<std::pair<int, ShaderSetLayout> > set_layouts;
|
||||
std::vector<std::pair<int, ShaderSetLayout>> set_layouts;
|
||||
std::vector<ShaderPushConstantLayout> push_constants;
|
||||
|
||||
ShaderLayout(std::vector<std::pair<int, ShaderSetLayout> > s, std::vector<ShaderPushConstantLayout> pc) : set_layouts(std::move(s)), push_constants(std::move(pc)) {}
|
||||
};
|
||||
|
||||
enum class ShaderType
|
||||
{
|
||||
Vertex,
|
||||
Fragment,
|
||||
Compute
|
||||
};
|
||||
|
||||
struct ShaderPipelineLayoutPart
|
||||
{
|
||||
std::vector<VkPushConstantRange> push_constants;
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
#ifndef __MLX_RENDER_CORE__
|
||||
#define __MLX_RENDER_CORE__
|
||||
|
||||
constexpr const int MAX_FRAMES_IN_FLIGHT = 3;
|
||||
|
||||
#include <Renderer/Memory.h>
|
||||
#include <Renderer/Descriptor.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
constexpr const int MAX_FRAMES_IN_FLIGHT = 3;
|
||||
|
||||
#if defined(DEBUG) && defined(VK_EXT_debug_utils)
|
||||
#define MLX_HAS_DEBUG_UTILS_FUNCTIONS
|
||||
#endif
|
||||
@@ -21,6 +22,7 @@ namespace mlx
|
||||
[[nodiscard]] MLX_FORCEINLINE VkDevice GetDevice() const noexcept { return m_device; }
|
||||
[[nodiscard]] MLX_FORCEINLINE VkPhysicalDevice GetPhysicalDevice() const noexcept { return m_physical_device; }
|
||||
[[nodiscard]] MLX_FORCEINLINE GPUAllocator& GetAllocator() noexcept { return m_allocator; }
|
||||
[[nodiscard]] inline DescriptorPoolManager& GetDescriptorPoolManager() noexcept { return m_descriptor_pool_manager; }
|
||||
|
||||
inline void WaitDeviceIdle() const noexcept { vkDeviceWaitIdle(m_device); }
|
||||
|
||||
@@ -45,6 +47,7 @@ namespace mlx
|
||||
private:
|
||||
static RenderCore* s_instance;
|
||||
|
||||
DescriptorPoolManager m_descriptor_pool_manager;
|
||||
GPUAllocator m_allocator;
|
||||
VkInstance m_instance = VK_NULL_HANDLE;
|
||||
VkDevice m_device = VK_NULL_HANDLE;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef __MLX_2D_PASS__
|
||||
#define __MLX_2D_PASS__
|
||||
|
||||
#include <Renderer/Descriptor.h>
|
||||
#include <Renderer/RenderCore.h>
|
||||
#include <Renderer/Pipelines/Shader.h>
|
||||
#include <Renderer/Pipelines/Graphics.h>
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef __MLX_FINAL_PASS__
|
||||
#define __MLX_FINAL_PASS__
|
||||
|
||||
#include <Renderer/Descriptor.h>
|
||||
#include <Renderer/RenderCore.h>
|
||||
#include <Renderer/Pipelines/Shader.h>
|
||||
#include <Renderer/Pipelines/Graphics.h>
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
#include <Utils/NonOwningPtr.h>
|
||||
#include <Renderer/RenderCore.h>
|
||||
#include <Renderer/Image.h>
|
||||
#include <Renderer/Descriptor.h>
|
||||
#include <Core/EventBus.h>
|
||||
|
||||
namespace mlx
|
||||
@@ -32,7 +31,6 @@ namespace mlx
|
||||
[[nodiscard]] inline std::size_t GetSwapchainImageIndex() const noexcept { return m_swapchain_image_index; }
|
||||
[[nodiscard]] inline std::size_t GetCurrentFrameIndex() const noexcept { return m_current_frame_index; }
|
||||
[[nodiscard]] inline NonOwningPtr<Window> GetWindow() const noexcept { return p_window; }
|
||||
[[nodiscard]] inline DescriptorPoolManager& GetDescriptorPoolManager() noexcept { return m_descriptor_pool_manager; }
|
||||
|
||||
MLX_FORCEINLINE constexpr void RequireFramebufferResize() noexcept { m_framebuffers_resize = true; }
|
||||
|
||||
@@ -45,7 +43,6 @@ namespace mlx
|
||||
void DestroySwapchain();
|
||||
|
||||
private:
|
||||
DescriptorPoolManager m_descriptor_pool_manager;
|
||||
std::array<VkSemaphore, MAX_FRAMES_IN_FLIGHT> m_image_available_semaphores;
|
||||
std::array<VkSemaphore, MAX_FRAMES_IN_FLIGHT> m_render_finished_semaphores;
|
||||
std::array<VkCommandBuffer, MAX_FRAMES_IN_FLIGHT> m_cmd_buffers;
|
||||
|
||||
33
runtime/Includes/Utils/CallOnExit.h
git.filemode.normal_file
33
runtime/Includes/Utils/CallOnExit.h
git.filemode.normal_file
@@ -0,0 +1,33 @@
|
||||
#ifndef __MLX_CALL_ON_EXIT__
|
||||
#define __MLX_CALL_ON_EXIT__
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
template <typename F>
|
||||
class CallOnExit
|
||||
{
|
||||
public:
|
||||
CallOnExit() = default;
|
||||
CallOnExit(F&& functor);
|
||||
CallOnExit(const CallOnExit&) = delete;
|
||||
CallOnExit(CallOnExit&&) = delete;
|
||||
|
||||
void CallAndReset();
|
||||
void Reset();
|
||||
|
||||
CallOnExit& operator=(const CallOnExit&) = delete;
|
||||
CallOnExit& operator=(CallOnExit&&) = default;
|
||||
|
||||
~CallOnExit();
|
||||
|
||||
private:
|
||||
std::optional<F> m_functor;
|
||||
};
|
||||
|
||||
template<typename F>
|
||||
CallOnExit(F) -> CallOnExit<F>;
|
||||
}
|
||||
|
||||
#include <Utils/CallOnExit.inl>
|
||||
|
||||
#endif
|
||||
29
runtime/Includes/Utils/CallOnExit.inl
git.filemode.normal_file
29
runtime/Includes/Utils/CallOnExit.inl
git.filemode.normal_file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include <Utils/CallOnExit.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
template<typename F>
|
||||
CallOnExit<F>::CallOnExit(F&& functor) : m_functor(std::move(functor)) {}
|
||||
|
||||
template<typename F>
|
||||
CallOnExit<F>::~CallOnExit()
|
||||
{
|
||||
if(m_functor.has_value())
|
||||
(*m_functor)();
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
void CallOnExit<F>::CallAndReset()
|
||||
{
|
||||
if(m_functor.has_value())
|
||||
(*m_functor)();
|
||||
m_functor.reset();
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
void CallOnExit<F>::Reset()
|
||||
{
|
||||
m_functor.reset();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user