Skip to content

Commit c7484b7

Browse files
committed
fixing put pixel, adding scene change checker
1 parent 5945549 commit c7484b7

File tree

28 files changed

+302
-201
lines changed

28 files changed

+302
-201
lines changed

runtime/Includes/Core/Enums.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ namespace mlx
1919

2020
enum class Event
2121
{
22-
DescriptorPoolResetEventCode = 55,
2322
ResizeEventCode = 56,
2423
FrameBeginEventCode = 57,
2524
FatalErrorEventCode = 168,

runtime/Includes/Core/Graphics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ namespace mlx
4444
std::shared_ptr<Window> p_window;
4545
std::unique_ptr<Scene> p_scene;
4646

47+
std::uint64_t m_draw_layer = 0;
48+
4749
int m_id;
4850

4951
bool m_has_window;

runtime/Includes/Core/Graphics.inl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace mlx
99
p_scene->ResetSprites();
1010
m_put_pixel_manager.ResetRenderData();
1111
m_insert_new_pixel_put_texture = true;
12+
m_draw_layer = 0;
1213
}
1314

1415
void GraphicsSupport::PixelPut(int x, int y, std::uint32_t color) noexcept
@@ -19,6 +20,7 @@ namespace mlx
1920
{
2021
Sprite& new_sprite = p_scene->CreateSprite(texture);
2122
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
23+
m_draw_layer++;
2224
}
2325
m_insert_new_pixel_put_texture = false;
2426
}
@@ -42,8 +44,12 @@ namespace mlx
4244
new_sprite.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
4345
m_insert_new_pixel_put_texture = true;
4446
}
45-
else
47+
else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer))
48+
{
4649
p_scene->BringToFront(std::move(sprite));
50+
m_insert_new_pixel_put_texture = true;
51+
}
52+
m_draw_layer++;
4753
}
4854

4955
void GraphicsSupport::LoadFont(const std::filesystem::path& filepath, float scale)

runtime/Includes/Graphics/Mesh.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ namespace mlx
4848
private:
4949
std::vector<SubMesh> m_sub_meshes;
5050
};
51+
52+
class MeshRegistry
53+
{
54+
public:
55+
MeshRegistry() = default;
56+
57+
inline void RegisterMesh(std::shared_ptr<Mesh> mesh);
58+
inline void UnregisterMesh(std::shared_ptr<Mesh> mesh);
59+
inline bool IsMeshKnown(std::shared_ptr<Mesh> mesh);
60+
61+
~MeshRegistry() = default;
62+
63+
private:
64+
std::unordered_set<std::shared_ptr<Mesh>> m_mesh_registry;
65+
};
5166
}
5267

5368
#endif

runtime/Includes/Graphics/Scene.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,25 @@
77

88
namespace mlx
99
{
10-
struct SceneDescriptor
11-
{
12-
NonOwningPtr<Renderer> renderer;
13-
// More description may come in future
14-
};
15-
1610
class Scene
1711
{
1812
public:
19-
Scene(SceneDescriptor desc);
13+
Scene() = default;
2014

2115
Sprite& CreateSprite(NonOwningPtr<class Texture> texture) noexcept;
2216
NonOwningPtr<Sprite> GetSpriteFromTextureAndPosition(NonOwningPtr<Texture> texture, const Vec2f& position) const;
2317
void BringToFront(NonOwningPtr<Sprite> sprite);
2418
void TryEraseSpriteFromTexture(NonOwningPtr<Texture> texture);
19+
bool IsTextureAtGivenDrawLayer(NonOwningPtr<Texture> texture, std::uint64_t draw_layer) const;
2520

2621
inline void ResetSprites() { m_sprites.clear(); }
2722

2823
[[nodiscard]] MLX_FORCEINLINE const std::vector<std::shared_ptr<Sprite>>& GetSprites() const noexcept { return m_sprites; }
29-
[[nodiscard]] MLX_FORCEINLINE const SceneDescriptor& GetDescription() const noexcept { return m_descriptor; }
3024
[[nodiscard]] MLX_FORCEINLINE ViewerData& GetViewerData() noexcept { return m_viewer_data; }
3125

3226
~Scene() = default;
3327

3428
private:
35-
SceneDescriptor m_descriptor;
3629
std::vector<std::shared_ptr<Sprite>> m_sprites;
3730
ViewerData m_viewer_data;
3831
};

runtime/Includes/Graphics/Sprite.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace mlx
1414
friend class Render2DPass;
1515

1616
public:
17-
Sprite(class Renderer& renderer, NonOwningPtr<Texture> texture);
17+
Sprite(NonOwningPtr<Texture> texture);
1818

1919
inline void SetColor(Vec4f color) noexcept { m_color = color; }
2020
inline void SetPosition(Vec2f position) noexcept { m_position = position; }
@@ -24,25 +24,27 @@ namespace mlx
2424
[[nodiscard]] MLX_FORCEINLINE std::shared_ptr<Mesh> GetMesh() const { return p_mesh; }
2525
[[nodiscard]] MLX_FORCEINLINE NonOwningPtr<Texture> GetTexture() const { return p_texture; }
2626

27-
~Sprite() = default;
27+
inline ~Sprite() { if(p_set) p_set->ReturnDescriptorSetToPool(); }
2828

2929
private:
30-
[[nodiscard]] inline bool IsSetInit() const noexcept { return m_set.IsInit(); }
31-
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t frame_index) const noexcept { return m_set.GetSet(frame_index); }
30+
[[nodiscard]] inline bool IsSetInit() const noexcept { return p_set && p_set->IsInit(); }
31+
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t frame_index) const noexcept { return p_set ? p_set->GetSet(frame_index) : VK_NULL_HANDLE; }
3232

33-
inline void UpdateDescriptorSet(const DescriptorSet& set)
33+
inline void UpdateDescriptorSet(std::shared_ptr<DescriptorSet> set)
3434
{
35-
m_set = set.Duplicate();
35+
p_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(set->GetShaderLayout(), set->GetShaderType());
3636
}
3737

3838
inline void Bind(std::size_t frame_index, VkCommandBuffer cmd)
3939
{
40-
m_set.SetImage(frame_index, 0, *p_texture);
41-
m_set.Update(frame_index, cmd);
40+
if(!p_set)
41+
return;
42+
p_set->SetImage(frame_index, 0, *p_texture);
43+
p_set->Update(frame_index, cmd);
4244
}
4345

4446
private:
45-
DescriptorSet m_set;
47+
std::shared_ptr<DescriptorSet> p_set;
4648
NonOwningPtr<Texture> p_texture;
4749
std::shared_ptr<Mesh> p_mesh;
4850
Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f };

runtime/Includes/Renderer/Descriptor.h

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ namespace mlx
1313
NonOwningPtr<class GPUBuffer> uniform_buffer_ptr;
1414
NonOwningPtr<class Image> image_ptr;
1515
VkDescriptorType type;
16-
ShaderType shader_type;
1716
std::uint32_t binding;
1817
};
1918

@@ -25,17 +24,18 @@ namespace mlx
2524
void Init() noexcept;
2625
void Destroy() noexcept;
2726

28-
VkDescriptorSet AllocateDescriptorSet(std::uint32_t frame_index, VkDescriptorSetLayout layout);
27+
std::shared_ptr<class DescriptorSet> RequestDescriptorSet(const ShaderSetLayout& layout, ShaderType shader_type);
28+
void ReturnDescriptorSet(std::shared_ptr<class DescriptorSet> set);
2929

30-
void ResetPoolFromFrameIndex(std::size_t frame_index);
31-
32-
[[nodiscard]] inline VkDescriptorPool Get(std::uint32_t index) const noexcept { return m_pools[index]; }
30+
[[nodiscard]] inline VkDescriptorPool Get() const noexcept { return m_pool; }
3331
[[nodiscard]] MLX_FORCEINLINE std::size_t GetNumberOfSetsAllocated() const noexcept { return m_allocation_count; }
3432

3533
~DescriptorPool() = default;
3634

3735
private:
38-
std::array<VkDescriptorPool, MAX_FRAMES_IN_FLIGHT> m_pools;
36+
std::vector<std::shared_ptr<class DescriptorSet>> m_free_sets;
37+
std::vector<std::shared_ptr<class DescriptorSet>> m_used_sets;
38+
VkDescriptorPool m_pool;
3939
std::size_t m_allocation_count = 0;
4040
};
4141

@@ -44,42 +44,45 @@ namespace mlx
4444
public:
4545
DescriptorPoolManager() = default;
4646

47-
void ResetPoolsFromFrameIndex(std::size_t frame_index);
4847
DescriptorPool& GetAvailablePool();
4948
void Destroy();
5049

5150
~DescriptorPoolManager() = default;
5251

5352
private:
54-
std::list<DescriptorPool> m_pools;
53+
std::vector<DescriptorPool> m_pools;
5554
};
5655

57-
class DescriptorSet
56+
class DescriptorSet : public std::enable_shared_from_this<DescriptorSet>
5857
{
59-
public:
60-
DescriptorSet() { m_set.fill(VK_NULL_HANDLE); }
61-
DescriptorSet(DescriptorPoolManager& pools_manager, const ShaderSetLayout& layout, VkDescriptorSetLayout vklayout, ShaderType shader_type);
58+
friend DescriptorPool;
6259

60+
public:
6361
void SetImage(std::size_t i, std::uint32_t binding, class Image& image);
6462
void SetStorageBuffer(std::size_t i, std::uint32_t binding, class GPUBuffer& buffer);
6563
void SetUniformBuffer(std::size_t i, std::uint32_t binding, class GPUBuffer& buffer);
6664
void Update(std::size_t i, VkCommandBuffer cmd = VK_NULL_HANDLE) noexcept;
67-
void Reallocate(std::size_t frame_index) noexcept;
6865

69-
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t i) const noexcept { return m_set[i]; }
70-
[[nodiscard]] inline DescriptorSet Duplicate() const { return DescriptorSet{ *p_pools_manager, m_set_layout, m_descriptors }; }
71-
[[nodiscard]] inline bool IsInit() const noexcept { return m_set[0] != VK_NULL_HANDLE; }
66+
void ReturnDescriptorSetToPool();
67+
68+
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t i) const noexcept { return m_sets[i]; }
69+
[[nodiscard]] MLX_FORCEINLINE bool IsInit() const noexcept { return m_sets[0] != VK_NULL_HANDLE; }
70+
[[nodiscard]] MLX_FORCEINLINE VkDescriptorSetLayout GetVulkanLayout() const noexcept { return m_set_layout; }
71+
[[nodiscard]] MLX_FORCEINLINE const ShaderSetLayout& GetShaderLayout() const { return m_shader_layout; }
72+
[[nodiscard]] MLX_FORCEINLINE ShaderType GetShaderType() const noexcept { return m_shader_type; }
7273

7374
~DescriptorSet() = default;
7475

7576
private:
76-
DescriptorSet(DescriptorPoolManager& pools_manager, VkDescriptorSetLayout layout, const std::vector<Descriptor>& descriptors);
77+
DescriptorSet(DescriptorPool& pool, VkDescriptorSetLayout vulkan_layout, const ShaderSetLayout& layout, std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT> vulkan_sets, ShaderType shader_type);
7778

7879
private:
80+
ShaderSetLayout m_shader_layout;
7981
std::vector<Descriptor> m_descriptors;
80-
std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT> m_set;
82+
std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT> m_sets;
8183
VkDescriptorSetLayout m_set_layout;
82-
NonOwningPtr<DescriptorPoolManager> p_pools_manager;
84+
ShaderType m_shader_type;
85+
DescriptorPool& m_pool;
8386
};
8487
}
8588

runtime/Includes/Renderer/Enums.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ namespace mlx
1717
enum class ImageType
1818
{
1919
Color = 0,
20-
Depth,
2120

2221
EndEnum
2322
};
2423
constexpr std::size_t ImageTypeCount = static_cast<std::size_t>(ImageType::EndEnum);
24+
25+
enum class ShaderType
26+
{
27+
Vertex,
28+
Fragment
29+
};
2530
}
2631

2732
#endif

runtime/Includes/Renderer/Image.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ namespace mlx
7777
}
7878

7979
void Init(CPUBuffer pixels, std::uint32_t width, std::uint32_t height, VkFormat format, bool is_multisampled, [[maybe_unused]] std::string_view debug_name);
80+
void Destroy() noexcept override;
8081

8182
void SetPixel(int x, int y, std::uint32_t color) noexcept;
8283
int GetPixel(int x, int y) noexcept;

runtime/Includes/Renderer/Pipelines/Shader.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#ifndef __MLX_SHADER__
22
#define __MLX_SHADER__
33

4+
#include <Renderer/Enums.h>
5+
46
namespace mlx
57
{
68
struct ShaderSetLayout
79
{
8-
std::vector<std::pair<int, VkDescriptorType> > binds;
10+
std::vector<std::pair<int, VkDescriptorType>> binds;
911

1012
ShaderSetLayout(std::vector<std::pair<int, VkDescriptorType> > b) : binds(std::move(b)) {}
13+
14+
inline bool operator==(const ShaderSetLayout& rhs) const { return binds == rhs.binds; }
1115
};
1216

1317
struct ShaderPushConstantLayout
@@ -20,19 +24,12 @@ namespace mlx
2024

2125
struct ShaderLayout
2226
{
23-
std::vector<std::pair<int, ShaderSetLayout> > set_layouts;
27+
std::vector<std::pair<int, ShaderSetLayout>> set_layouts;
2428
std::vector<ShaderPushConstantLayout> push_constants;
2529

2630
ShaderLayout(std::vector<std::pair<int, ShaderSetLayout> > s, std::vector<ShaderPushConstantLayout> pc) : set_layouts(std::move(s)), push_constants(std::move(pc)) {}
2731
};
2832

29-
enum class ShaderType
30-
{
31-
Vertex,
32-
Fragment,
33-
Compute
34-
};
35-
3633
struct ShaderPipelineLayoutPart
3734
{
3835
std::vector<VkPushConstantRange> push_constants;

runtime/Includes/Renderer/RenderCore.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#ifndef __MLX_RENDER_CORE__
22
#define __MLX_RENDER_CORE__
33

4+
constexpr const int MAX_FRAMES_IN_FLIGHT = 3;
5+
46
#include <Renderer/Memory.h>
7+
#include <Renderer/Descriptor.h>
58

69
namespace mlx
710
{
8-
constexpr const int MAX_FRAMES_IN_FLIGHT = 3;
9-
1011
#if defined(DEBUG) && defined(VK_EXT_debug_utils)
1112
#define MLX_HAS_DEBUG_UTILS_FUNCTIONS
1213
#endif
@@ -21,6 +22,7 @@ namespace mlx
2122
[[nodiscard]] MLX_FORCEINLINE VkDevice GetDevice() const noexcept { return m_device; }
2223
[[nodiscard]] MLX_FORCEINLINE VkPhysicalDevice GetPhysicalDevice() const noexcept { return m_physical_device; }
2324
[[nodiscard]] MLX_FORCEINLINE GPUAllocator& GetAllocator() noexcept { return m_allocator; }
25+
[[nodiscard]] inline DescriptorPoolManager& GetDescriptorPoolManager() noexcept { return m_descriptor_pool_manager; }
2426

2527
inline void WaitDeviceIdle() const noexcept { vkDeviceWaitIdle(m_device); }
2628

@@ -45,6 +47,7 @@ namespace mlx
4547
private:
4648
static RenderCore* s_instance;
4749

50+
DescriptorPoolManager m_descriptor_pool_manager;
4851
GPUAllocator m_allocator;
4952
VkInstance m_instance = VK_NULL_HANDLE;
5053
VkDevice m_device = VK_NULL_HANDLE;

runtime/Includes/Renderer/RenderPasses/2DPass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef __MLX_2D_PASS__
22
#define __MLX_2D_PASS__
33

4-
#include <Renderer/Descriptor.h>
4+
#include <Renderer/RenderCore.h>
55
#include <Renderer/Pipelines/Shader.h>
66
#include <Renderer/Pipelines/Graphics.h>
77

runtime/Includes/Renderer/RenderPasses/FinalPass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef __MLX_FINAL_PASS__
22
#define __MLX_FINAL_PASS__
33

4-
#include <Renderer/Descriptor.h>
4+
#include <Renderer/RenderCore.h>
55
#include <Renderer/Pipelines/Shader.h>
66
#include <Renderer/Pipelines/Graphics.h>
77

runtime/Includes/Renderer/Renderer.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <Utils/NonOwningPtr.h>
66
#include <Renderer/RenderCore.h>
77
#include <Renderer/Image.h>
8-
#include <Renderer/Descriptor.h>
98
#include <Core/EventBus.h>
109

1110
namespace mlx
@@ -32,7 +31,6 @@ namespace mlx
3231
[[nodiscard]] inline std::size_t GetSwapchainImageIndex() const noexcept { return m_swapchain_image_index; }
3332
[[nodiscard]] inline std::size_t GetCurrentFrameIndex() const noexcept { return m_current_frame_index; }
3433
[[nodiscard]] inline NonOwningPtr<Window> GetWindow() const noexcept { return p_window; }
35-
[[nodiscard]] inline DescriptorPoolManager& GetDescriptorPoolManager() noexcept { return m_descriptor_pool_manager; }
3634

3735
MLX_FORCEINLINE constexpr void RequireFramebufferResize() noexcept { m_framebuffers_resize = true; }
3836

@@ -45,7 +43,6 @@ namespace mlx
4543
void DestroySwapchain();
4644

4745
private:
48-
DescriptorPoolManager m_descriptor_pool_manager;
4946
std::array<VkSemaphore, MAX_FRAMES_IN_FLIGHT> m_image_available_semaphores;
5047
std::array<VkSemaphore, MAX_FRAMES_IN_FLIGHT> m_render_finished_semaphores;
5148
std::array<VkCommandBuffer, MAX_FRAMES_IN_FLIGHT> m_cmd_buffers;

0 commit comments

Comments
 (0)