diff --git a/runtime/Includes/Core/Application.inl b/runtime/Includes/Core/Application.inl index 3193aee..f00c206 100644 --- a/runtime/Includes/Core/Application.inl +++ b/runtime/Includes/Core/Application.inl @@ -7,7 +7,7 @@ Error("invalid window ptr (NULL)"); \ return; \ } \ - else if(*static_cast(win) < 0 || *static_cast(win) > static_cast(_graphics.size()))\ + else if(*static_cast(win) < 0 || *static_cast(win) > static_cast(m_graphics.size()))\ { \ Error("invalid window ptr"); \ return; \ @@ -33,7 +33,7 @@ namespace mlx *y = p_in->GetY(); } - void Application::MouseMove(void* win, int x, int y) noexcept + void Application::MouseMove(Handle win, int x, int y) noexcept { CHECK_WINDOW_PTR(win); if(!m_graphics[*static_cast(win)]->HasWindow()) @@ -43,7 +43,7 @@ namespace mlx } } - void Application::OnEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept + void Application::OnEvent(Handle win, int event, int (*funct_ptr)(int, void*), void* param) noexcept { CHECK_WINDOW_PTR(win); if(!m_graphics[*static_cast(win)]->HasWindow()) @@ -54,7 +54,7 @@ namespace mlx m_in.OnEvent(m_graphics[*static_cast(win)]->GetWindow()->GetID(), event, funct_ptr, param); } - void Application::GetScreenSize(void* win, int* w, int* h) noexcept + void Application::GetScreenSize(Handle win, int* w, int* h) noexcept { CHECK_WINDOW_PTR(win); *w = 0; @@ -88,28 +88,28 @@ namespace mlx return static_cast(&m_graphics.back()->GetID()); } - void Application::ClearGraphicsSupport(void* win) + void Application::ClearGraphicsSupport(Handle win) { MLX_PROFILE_FUNCTION(); CHECK_WINDOW_PTR(win); m_graphics[*static_cast(win)]->ClearRenderData(); } - void Application::DestroyGraphicsSupport(void* win) + void Application::DestroyGraphicsSupport(Handle win) { MLX_PROFILE_FUNCTION(); CHECK_WINDOW_PTR(win); m_graphics[*static_cast(win)].reset(); } - void Application::PixelPut(void* win, int x, int y, std::uint32_t color) const noexcept + void Application::PixelPut(Handle win, int x, int y, std::uint32_t color) const noexcept { MLX_PROFILE_FUNCTION(); CHECK_WINDOW_PTR(win); m_graphics[*static_cast(win)]->PixelPut(x, y, color); } - void Application::StringPut(void* win, int x, int y, std::uint32_t color, char* str) + void Application::StringPut(Handle win, int x, int y, std::uint32_t color, char* str) { MLX_PROFILE_FUNCTION(); CHECK_WINDOW_PTR(win); @@ -126,14 +126,14 @@ namespace mlx m_graphics[*static_cast(win)]->StringPut(x, y, color, str); } - void Application::LoadFont(void* win, const std::filesystem::path& filepath, float scale) + void Application::LoadFont(Handle win, const std::filesystem::path& filepath, float scale) { MLX_PROFILE_FUNCTION(); CHECK_WINDOW_PTR(win); m_graphics[*static_cast(win)]->LoadFont(filepath, scale); } - void Application::TexturePut(void* win, void* img, int x, int y) + void Application::TexturePut(Handle win, Handle img, int x, int y) { MLX_PROFILE_FUNCTION(); CHECK_WINDOW_PTR(win); @@ -145,7 +145,7 @@ namespace mlx m_graphics[*static_cast(win)]->TexturePut(texture, x, y); } - int Application::GetTexturePixel(void* img, int x, int y) + int Application::GetTexturePixel(Handle img, int x, int y) { MLX_PROFILE_FUNCTION(); CHECK_IMAGE_PTR(img, return 0); @@ -158,7 +158,7 @@ namespace mlx return texture->GetPixel(x, y); } - void Application::setTexturePixel(void* img, int x, int y, std::uint32_t color) + void Application::SetTexturePixel(Handle img, int x, int y, std::uint32_t color) { MLX_PROFILE_FUNCTION(); CHECK_IMAGE_PTR(img, return); diff --git a/runtime/Includes/Core/Enums.h b/runtime/Includes/Core/Enums.h index 082373e..7eb6984 100644 --- a/runtime/Includes/Core/Enums.h +++ b/runtime/Includes/Core/Enums.h @@ -19,10 +19,10 @@ namespace mlx enum class Event { + DescriptorPoolResetEventCode = 55, ResizeEventCode = 56, FrameBeginEventCode = 57, FatalErrorEventCode = 168, - QuitEventCode = 168, EndEnum }; diff --git a/runtime/Includes/Core/EventBase.h b/runtime/Includes/Core/EventBase.h index 7f4464a..5e9aa02 100644 --- a/runtime/Includes/Core/EventBase.h +++ b/runtime/Includes/Core/EventBase.h @@ -5,7 +5,7 @@ namespace mlx { struct EventBase { - virtual std::uint32_t What() const = 0; + virtual Event What() const = 0; }; } diff --git a/runtime/Includes/Core/Graphics.h b/runtime/Includes/Core/Graphics.h index 842c2c9..e97ea9d 100644 --- a/runtime/Includes/Core/Graphics.h +++ b/runtime/Includes/Core/Graphics.h @@ -3,12 +3,10 @@ #include #include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include namespace mlx { @@ -18,36 +16,31 @@ namespace mlx GraphicsSupport(std::size_t w, std::size_t h, NonOwningPtr render_target, int id); GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id); - inline int& GetID() noexcept; - inline std::shared_ptr GetWindow(); + [[nodiscard]] MLX_FORCEINLINE int& GetID() noexcept { return m_id; } + [[nodiscard]] inline std::shared_ptr GetWindow() { return p_window; } void Render() noexcept; - inline void ClearRenderData() noexcept; + inline void ResetRenderData() noexcept; + inline void PixelPut(int x, int y, std::uint32_t color) noexcept; inline void StringPut(int x, int y, std::uint32_t color, std::string str); inline void TexturePut(NonOwningPtr texture, int x, int y); + inline void LoadFont(const std::filesystem::path& filepath, float scale); - inline void TryEraseTextureFromManager(NonOwningPtr texture) noexcept; - inline bool HasWindow() const noexcept { return m_has_window; } + inline void TryEraseTextureFromRegistry(NonOwningPtr texture) noexcept; - inline Renderer& GetRenderer() { return m_renderer; } + [[nodiscard]] MLX_FORCEINLINE bool HasWindow() const noexcept { return m_has_window; } + [[nodiscard]] MLX_FORCEINLINE Renderer& GetRenderer() { return m_renderer; } ~GraphicsSupport(); private: Renderer m_renderer; - PixelPutPipeline m_pixel_put_pipeline; - - std::vector> m_drawlist; - - TextManager m_text_manager; - TextureRegistry m_texture_registry; - - glm::mat4 m_proj = glm::mat4(1.0); - + SceneRenderer m_scene_renderer; std::shared_ptr p_window; + std::unique_ptr p_scene; std::size_t m_width = 0; std::size_t m_height = 0; diff --git a/runtime/Includes/Core/Graphics.inl b/runtime/Includes/Core/Graphics.inl index 10286cc..7d866bb 100644 --- a/runtime/Includes/Core/Graphics.inl +++ b/runtime/Includes/Core/Graphics.inl @@ -3,66 +3,35 @@ namespace mlx { - int& GraphicsSupport::GetID() noexcept { return m_id; } - std::shared_ptr GraphicsSupport::GetWindow() { return p_window; } - - void GraphicsSupport::ClearRenderData() noexcept + void GraphicsSupport::ResetRenderData() noexcept { MLX_PROFILE_FUNCTION(); - m_drawlist.clear(); - m_pixel_put_pipeline.Clear(); - m_text_manager.Clear(); - m_texture_registry.Clear(); + p_scene->ResetSprites(); + m_images_registry.Clear(); } void GraphicsSupport::PixelPut(int x, int y, std::uint32_t color) noexcept { MLX_PROFILE_FUNCTION(); - m_pixel_put_pipeline.SetPixel(x, y, color); } void GraphicsSupport::StringPut(int x, int y, std::uint32_t color, std::string str) { MLX_PROFILE_FUNCTION(); - std::pair, bool> res = m_text_manager.RegisterText(x, y, color, str); - if(!res.second) // if this is not a completly new text draw - { - auto it = std::find(m_drawlist.begin(), m_drawlist.end(), res.first); - if(it != m_drawlist.end()) - m_drawlist.erase(it); - } - m_drawlist.push_back(res.first); } void GraphicsSupport::TexturePut(NonOwningPtr texture, int x, int y) { MLX_PROFILE_FUNCTION(); - auto res = m_texture_registry.RegisterTexture(texture, x, y); - if(!res.second) // if this is not a completly new texture draw - { - auto it = std::find(m_drawlist.begin(), m_drawlist.end(), res.first); - if(it != m_drawlist.end()) - m_drawlist.erase(it); - } - m_drawlist.push_back(res.first); } void GraphicsSupport::LoadFont(const std::filesystem::path& filepath, float scale) { MLX_PROFILE_FUNCTION(); - m_text_manager.LoadFont(m_renderer, filepath, scale); } - void GraphicsSupport::TryEraseTextureFromManager(NonOwningPtr texture) noexcept + void GraphicsSupport::TryEraseTextureFromRegistry(NonOwningPtr texture) noexcept { MLX_PROFILE_FUNCTION(); - for(auto it = m_drawlist.begin(); it != m_drawlist.end();) - { - if(m_texture_registry.IsTextureKnown(texture)) - it = m_drawlist.erase(it); - else - ++it; - } - m_texture_registry.EraseTextures(texture); } } diff --git a/runtime/Includes/Embedded/2DVertex.nzsl b/runtime/Includes/Embedded/2DVertex.nzsl index ec65918..67f7e09 100644 --- a/runtime/Includes/Embedded/2DVertex.nzsl +++ b/runtime/Includes/Embedded/2DVertex.nzsl @@ -4,9 +4,7 @@ module; struct VertIn { [location(0)] pos: vec4[f32], - [location(1)] color: vec4[f32], // unused - [location(2)] normal: vec4[f32], // unused - [location(3)] uv: vec2[f32] + [location(1)] uv: vec2[f32] } struct VertOut @@ -24,13 +22,13 @@ struct ViewerData struct SpriteData { color: vec4[f32], - position: vec2[f32] + position: vec4[f32] } external { [set(0), binding(0)] viewer_data: uniform[ViewerData], - model : push_constant[SpriteData] + model: push_constant[SpriteData] } [entry(vert)] @@ -40,6 +38,6 @@ fn main(input: VertIn) -> VertOut let output: VertOut; output.uv = input.uv; output.color = model.color; - output.pos = viewer_data.projection_matrix * vec4[f32](input.pos.xy + model.position, 0.0, 1.0); + output.pos = viewer_data.projection_matrix * (input.pos + model.position); return output; } diff --git a/runtime/Includes/Embedded/2DVertex.spv.h b/runtime/Includes/Embedded/2DVertex.spv.h index 94cf250..5a4f7f8 100644 --- a/runtime/Includes/Embedded/2DVertex.spv.h +++ b/runtime/Includes/Embedded/2DVertex.spv.h @@ -1,29 +1,29 @@ -3,2,35,7,0,0,1,0,39,0,0,0,77,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,14,0, -3,0,0,0,0,0,1,0,0,0,15,0,12,0,0,0,0,0,37,0,0,0,109,97,105,110,0,0,0,0, -14,0,0,0,18,0,0,0,20,0,0,0,23,0,0,0,29,0,0,0,31,0,0,0,32,0,0,0,3,0, +3,2,35,7,0,0,1,0,39,0,0,0,73,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,14,0, +3,0,0,0,0,0,1,0,0,0,15,0,12,0,0,0,0,0,36,0,0,0,109,97,105,110,0,0,0,0, +13,0,0,0,17,0,0,0,19,0,0,0,23,0,0,0,29,0,0,0,31,0,0,0,32,0,0,0,3,0, 3,0,0,0,0,0,100,0,0,0,5,0,5,0,4,0,0,0,86,105,101,119,101,114,68,97,116,97,0,0, 6,0,8,0,4,0,0,0,0,0,0,0,112,114,111,106,101,99,116,105,111,110,95,109,97,116,114,105,120,0, -0,0,5,0,5,0,8,0,0,0,83,112,114,105,116,101,68,97,116,97,0,0,6,0,5,0,8,0,0,0, -0,0,0,0,99,111,108,111,114,0,0,0,6,0,6,0,8,0,0,0,1,0,0,0,112,111,115,105,116,105, +0,0,5,0,5,0,7,0,0,0,83,112,114,105,116,101,68,97,116,97,0,0,6,0,5,0,7,0,0,0, +0,0,0,0,99,111,108,111,114,0,0,0,6,0,6,0,7,0,0,0,1,0,0,0,112,111,115,105,116,105, 111,110,0,0,0,0,5,0,4,0,26,0,0,0,86,101,114,116,73,110,0,0,6,0,4,0,26,0,0,0, 0,0,0,0,112,111,115,0,6,0,5,0,26,0,0,0,1,0,0,0,99,111,108,111,114,0,0,0,6,0, 5,0,26,0,0,0,2,0,0,0,110,111,114,109,97,108,0,0,6,0,4,0,26,0,0,0,3,0,0,0, 117,118,0,0,5,0,4,0,33,0,0,0,86,101,114,116,79,117,116,0,6,0,5,0,33,0,0,0,0,0, 0,0,99,111,108,111,114,0,0,0,6,0,4,0,33,0,0,0,1,0,0,0,117,118,0,0,6,0,4,0, 33,0,0,0,2,0,0,0,112,111,115,0,5,0,5,0,6,0,0,0,118,105,101,119,101,114,95,100,97,116, -97,0,5,0,4,0,10,0,0,0,109,111,100,101,108,0,0,0,5,0,3,0,14,0,0,0,112,111,115,0, -5,0,4,0,18,0,0,0,99,111,108,111,114,0,0,0,5,0,4,0,20,0,0,0,110,111,114,109,97,108, +97,0,5,0,4,0,9,0,0,0,109,111,100,101,108,0,0,0,5,0,3,0,13,0,0,0,112,111,115,0, +5,0,4,0,17,0,0,0,99,111,108,111,114,0,0,0,5,0,4,0,19,0,0,0,110,111,114,109,97,108, 0,0,5,0,3,0,23,0,0,0,117,118,0,0,5,0,4,0,29,0,0,0,99,111,108,111,114,0,0,0, 5,0,3,0,31,0,0,0,117,118,0,0,5,0,5,0,32,0,0,0,112,111,115,105,116,105,111,110,0,0, -0,0,5,0,4,0,37,0,0,0,109,97,105,110,0,0,0,0,71,0,4,0,6,0,0,0,33,0,0,0, +0,0,5,0,4,0,36,0,0,0,109,97,105,110,0,0,0,0,71,0,4,0,6,0,0,0,33,0,0,0, 0,0,0,0,71,0,4,0,6,0,0,0,34,0,0,0,0,0,0,0,71,0,4,0,32,0,0,0,11,0, -0,0,0,0,0,0,71,0,4,0,14,0,0,0,30,0,0,0,0,0,0,0,71,0,4,0,18,0,0,0, -30,0,0,0,1,0,0,0,71,0,4,0,20,0,0,0,30,0,0,0,2,0,0,0,71,0,4,0,23,0, +0,0,0,0,0,0,71,0,4,0,13,0,0,0,30,0,0,0,0,0,0,0,71,0,4,0,17,0,0,0, +30,0,0,0,1,0,0,0,71,0,4,0,19,0,0,0,30,0,0,0,2,0,0,0,71,0,4,0,23,0, 0,0,30,0,0,0,3,0,0,0,71,0,4,0,29,0,0,0,30,0,0,0,0,0,0,0,71,0,4,0, 31,0,0,0,30,0,0,0,1,0,0,0,71,0,3,0,4,0,0,0,2,0,0,0,72,0,4,0,4,0, 0,0,0,0,0,0,5,0,0,0,72,0,5,0,4,0,0,0,0,0,0,0,7,0,0,0,16,0,0,0, -72,0,5,0,4,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,71,0,3,0,8,0,0,0,2,0, -0,0,72,0,5,0,8,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,72,0,5,0,8,0,0,0, +72,0,5,0,4,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,71,0,3,0,7,0,0,0,2,0, +0,0,72,0,5,0,7,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,72,0,5,0,7,0,0,0, 1,0,0,0,35,0,0,0,16,0,0,0,72,0,5,0,26,0,0,0,0,0,0,0,35,0,0,0,0,0, 0,0,72,0,5,0,26,0,0,0,1,0,0,0,35,0,0,0,16,0,0,0,72,0,5,0,26,0,0,0, 2,0,0,0,35,0,0,0,32,0,0,0,72,0,5,0,26,0,0,0,3,0,0,0,35,0,0,0,48,0, @@ -31,50 +31,47 @@ 1,0,0,0,35,0,0,0,16,0,0,0,72,0,5,0,33,0,0,0,2,0,0,0,35,0,0,0,32,0, 0,0,22,0,3,0,1,0,0,0,32,0,0,0,23,0,4,0,2,0,0,0,1,0,0,0,4,0,0,0, 24,0,4,0,3,0,0,0,2,0,0,0,4,0,0,0,30,0,3,0,4,0,0,0,3,0,0,0,32,0, -4,0,5,0,0,0,2,0,0,0,4,0,0,0,23,0,4,0,7,0,0,0,1,0,0,0,2,0,0,0, -30,0,4,0,8,0,0,0,2,0,0,0,7,0,0,0,32,0,4,0,9,0,0,0,9,0,0,0,8,0, -0,0,19,0,2,0,11,0,0,0,33,0,3,0,12,0,0,0,11,0,0,0,32,0,4,0,13,0,0,0, -1,0,0,0,2,0,0,0,21,0,4,0,15,0,0,0,32,0,0,0,1,0,0,0,43,0,4,0,15,0, -0,0,16,0,0,0,0,0,0,0,32,0,4,0,17,0,0,0,7,0,0,0,2,0,0,0,43,0,4,0, -15,0,0,0,19,0,0,0,1,0,0,0,43,0,4,0,15,0,0,0,21,0,0,0,2,0,0,0,32,0, -4,0,22,0,0,0,1,0,0,0,7,0,0,0,43,0,4,0,15,0,0,0,24,0,0,0,3,0,0,0, -32,0,4,0,25,0,0,0,7,0,0,0,7,0,0,0,30,0,6,0,26,0,0,0,2,0,0,0,2,0, -0,0,2,0,0,0,7,0,0,0,32,0,4,0,27,0,0,0,7,0,0,0,26,0,0,0,32,0,4,0, -28,0,0,0,3,0,0,0,2,0,0,0,32,0,4,0,30,0,0,0,3,0,0,0,7,0,0,0,30,0, -5,0,33,0,0,0,2,0,0,0,7,0,0,0,2,0,0,0,43,0,4,0,1,0,0,0,34,0,0,0, -0,0,128,63,32,0,4,0,35,0,0,0,7,0,0,0,33,0,0,0,43,0,4,0,1,0,0,0,36,0, -0,0,0,0,0,0,32,0,4,0,51,0,0,0,7,0,0,0,1,0,0,0,32,0,4,0,56,0,0,0, -9,0,0,0,2,0,0,0,32,0,4,0,60,0,0,0,2,0,0,0,3,0,0,0,32,0,4,0,66,0, -0,0,9,0,0,0,7,0,0,0,59,0,4,0,5,0,0,0,6,0,0,0,2,0,0,0,59,0,4,0, -9,0,0,0,10,0,0,0,9,0,0,0,59,0,4,0,13,0,0,0,14,0,0,0,1,0,0,0,59,0, -4,0,13,0,0,0,18,0,0,0,1,0,0,0,59,0,4,0,13,0,0,0,20,0,0,0,1,0,0,0, -59,0,4,0,22,0,0,0,23,0,0,0,1,0,0,0,59,0,4,0,28,0,0,0,29,0,0,0,3,0, -0,0,59,0,4,0,30,0,0,0,31,0,0,0,3,0,0,0,59,0,4,0,28,0,0,0,32,0,0,0, -3,0,0,0,54,0,5,0,11,0,0,0,37,0,0,0,0,0,0,0,12,0,0,0,248,0,2,0,38,0, -0,0,59,0,4,0,35,0,0,0,39,0,0,0,7,0,0,0,59,0,4,0,27,0,0,0,40,0,0,0, -7,0,0,0,65,0,5,0,17,0,0,0,41,0,0,0,40,0,0,0,16,0,0,0,63,0,3,0,41,0, -0,0,14,0,0,0,65,0,5,0,17,0,0,0,42,0,0,0,40,0,0,0,19,0,0,0,63,0,3,0, -42,0,0,0,18,0,0,0,65,0,5,0,17,0,0,0,43,0,0,0,40,0,0,0,21,0,0,0,63,0, -3,0,43,0,0,0,20,0,0,0,65,0,5,0,25,0,0,0,44,0,0,0,40,0,0,0,24,0,0,0, -63,0,3,0,44,0,0,0,23,0,0,0,65,0,5,0,25,0,0,0,45,0,0,0,40,0,0,0,24,0, -0,0,61,0,4,0,7,0,0,0,46,0,0,0,45,0,0,0,81,0,5,0,1,0,0,0,47,0,0,0, -46,0,0,0,0,0,0,0,127,0,4,0,1,0,0,0,48,0,0,0,34,0,0,0,133,0,5,0,1,0, -0,0,49,0,0,0,47,0,0,0,48,0,0,0,65,0,5,0,25,0,0,0,50,0,0,0,40,0,0,0, -24,0,0,0,65,0,5,0,51,0,0,0,52,0,0,0,50,0,0,0,16,0,0,0,62,0,3,0,52,0, -0,0,49,0,0,0,65,0,5,0,25,0,0,0,53,0,0,0,40,0,0,0,24,0,0,0,61,0,4,0, -7,0,0,0,54,0,0,0,53,0,0,0,65,0,5,0,25,0,0,0,55,0,0,0,39,0,0,0,19,0, -0,0,62,0,3,0,55,0,0,0,54,0,0,0,65,0,5,0,56,0,0,0,57,0,0,0,10,0,0,0, -16,0,0,0,61,0,4,0,2,0,0,0,58,0,0,0,57,0,0,0,65,0,5,0,17,0,0,0,59,0, -0,0,39,0,0,0,16,0,0,0,62,0,3,0,59,0,0,0,58,0,0,0,65,0,5,0,60,0,0,0, -61,0,0,0,6,0,0,0,16,0,0,0,61,0,4,0,3,0,0,0,62,0,0,0,61,0,0,0,65,0, -5,0,17,0,0,0,63,0,0,0,40,0,0,0,16,0,0,0,61,0,4,0,2,0,0,0,64,0,0,0, -63,0,0,0,79,0,7,0,7,0,0,0,65,0,0,0,64,0,0,0,64,0,0,0,0,0,0,0,1,0, -0,0,65,0,5,0,66,0,0,0,67,0,0,0,10,0,0,0,19,0,0,0,61,0,4,0,7,0,0,0, -68,0,0,0,67,0,0,0,129,0,5,0,7,0,0,0,69,0,0,0,65,0,0,0,68,0,0,0,80,0, -6,0,2,0,0,0,70,0,0,0,69,0,0,0,36,0,0,0,34,0,0,0,145,0,5,0,2,0,0,0, -71,0,0,0,62,0,0,0,70,0,0,0,65,0,5,0,17,0,0,0,72,0,0,0,39,0,0,0,21,0, -0,0,62,0,3,0,72,0,0,0,71,0,0,0,61,0,4,0,33,0,0,0,73,0,0,0,39,0,0,0, -81,0,5,0,2,0,0,0,74,0,0,0,73,0,0,0,0,0,0,0,62,0,3,0,29,0,0,0,74,0, -0,0,81,0,5,0,7,0,0,0,75,0,0,0,73,0,0,0,1,0,0,0,62,0,3,0,31,0,0,0, -75,0,0,0,81,0,5,0,2,0,0,0,76,0,0,0,73,0,0,0,2,0,0,0,62,0,3,0,32,0, -0,0,76,0,0,0,253,0,1,0,56,0,1,0 +4,0,5,0,0,0,2,0,0,0,4,0,0,0,30,0,4,0,7,0,0,0,2,0,0,0,2,0,0,0, +32,0,4,0,8,0,0,0,9,0,0,0,7,0,0,0,19,0,2,0,10,0,0,0,33,0,3,0,11,0, +0,0,10,0,0,0,32,0,4,0,12,0,0,0,1,0,0,0,2,0,0,0,21,0,4,0,14,0,0,0, +32,0,0,0,1,0,0,0,43,0,4,0,14,0,0,0,15,0,0,0,0,0,0,0,32,0,4,0,16,0, +0,0,7,0,0,0,2,0,0,0,43,0,4,0,14,0,0,0,18,0,0,0,1,0,0,0,43,0,4,0, +14,0,0,0,20,0,0,0,2,0,0,0,23,0,4,0,21,0,0,0,1,0,0,0,2,0,0,0,32,0, +4,0,22,0,0,0,1,0,0,0,21,0,0,0,43,0,4,0,14,0,0,0,24,0,0,0,3,0,0,0, +32,0,4,0,25,0,0,0,7,0,0,0,21,0,0,0,30,0,6,0,26,0,0,0,2,0,0,0,2,0, +0,0,2,0,0,0,21,0,0,0,32,0,4,0,27,0,0,0,7,0,0,0,26,0,0,0,32,0,4,0, +28,0,0,0,3,0,0,0,2,0,0,0,32,0,4,0,30,0,0,0,3,0,0,0,21,0,0,0,30,0, +5,0,33,0,0,0,2,0,0,0,21,0,0,0,2,0,0,0,43,0,4,0,1,0,0,0,34,0,0,0, +0,0,128,63,32,0,4,0,35,0,0,0,7,0,0,0,33,0,0,0,32,0,4,0,50,0,0,0,7,0, +0,0,1,0,0,0,32,0,4,0,55,0,0,0,9,0,0,0,2,0,0,0,32,0,4,0,59,0,0,0, +2,0,0,0,3,0,0,0,59,0,4,0,5,0,0,0,6,0,0,0,2,0,0,0,59,0,4,0,8,0, +0,0,9,0,0,0,9,0,0,0,59,0,4,0,12,0,0,0,13,0,0,0,1,0,0,0,59,0,4,0, +12,0,0,0,17,0,0,0,1,0,0,0,59,0,4,0,12,0,0,0,19,0,0,0,1,0,0,0,59,0, +4,0,22,0,0,0,23,0,0,0,1,0,0,0,59,0,4,0,28,0,0,0,29,0,0,0,3,0,0,0, +59,0,4,0,30,0,0,0,31,0,0,0,3,0,0,0,59,0,4,0,28,0,0,0,32,0,0,0,3,0, +0,0,54,0,5,0,10,0,0,0,36,0,0,0,0,0,0,0,11,0,0,0,248,0,2,0,37,0,0,0, +59,0,4,0,35,0,0,0,38,0,0,0,7,0,0,0,59,0,4,0,27,0,0,0,39,0,0,0,7,0, +0,0,65,0,5,0,16,0,0,0,40,0,0,0,39,0,0,0,15,0,0,0,63,0,3,0,40,0,0,0, +13,0,0,0,65,0,5,0,16,0,0,0,41,0,0,0,39,0,0,0,18,0,0,0,63,0,3,0,41,0, +0,0,17,0,0,0,65,0,5,0,16,0,0,0,42,0,0,0,39,0,0,0,20,0,0,0,63,0,3,0, +42,0,0,0,19,0,0,0,65,0,5,0,25,0,0,0,43,0,0,0,39,0,0,0,24,0,0,0,63,0, +3,0,43,0,0,0,23,0,0,0,65,0,5,0,25,0,0,0,44,0,0,0,39,0,0,0,24,0,0,0, +61,0,4,0,21,0,0,0,45,0,0,0,44,0,0,0,81,0,5,0,1,0,0,0,46,0,0,0,45,0, +0,0,0,0,0,0,127,0,4,0,1,0,0,0,47,0,0,0,34,0,0,0,133,0,5,0,1,0,0,0, +48,0,0,0,46,0,0,0,47,0,0,0,65,0,5,0,25,0,0,0,49,0,0,0,39,0,0,0,24,0, +0,0,65,0,5,0,50,0,0,0,51,0,0,0,49,0,0,0,15,0,0,0,62,0,3,0,51,0,0,0, +48,0,0,0,65,0,5,0,25,0,0,0,52,0,0,0,39,0,0,0,24,0,0,0,61,0,4,0,21,0, +0,0,53,0,0,0,52,0,0,0,65,0,5,0,25,0,0,0,54,0,0,0,38,0,0,0,18,0,0,0, +62,0,3,0,54,0,0,0,53,0,0,0,65,0,5,0,55,0,0,0,56,0,0,0,9,0,0,0,15,0, +0,0,61,0,4,0,2,0,0,0,57,0,0,0,56,0,0,0,65,0,5,0,16,0,0,0,58,0,0,0, +38,0,0,0,15,0,0,0,62,0,3,0,58,0,0,0,57,0,0,0,65,0,5,0,59,0,0,0,60,0, +0,0,6,0,0,0,15,0,0,0,61,0,4,0,3,0,0,0,61,0,0,0,60,0,0,0,65,0,5,0, +16,0,0,0,62,0,0,0,39,0,0,0,15,0,0,0,61,0,4,0,2,0,0,0,63,0,0,0,62,0, +0,0,65,0,5,0,55,0,0,0,64,0,0,0,9,0,0,0,18,0,0,0,61,0,4,0,2,0,0,0, +65,0,0,0,64,0,0,0,129,0,5,0,2,0,0,0,66,0,0,0,63,0,0,0,65,0,0,0,145,0, +5,0,2,0,0,0,67,0,0,0,61,0,0,0,66,0,0,0,65,0,5,0,16,0,0,0,68,0,0,0, +38,0,0,0,20,0,0,0,62,0,3,0,68,0,0,0,67,0,0,0,61,0,4,0,33,0,0,0,69,0, +0,0,38,0,0,0,81,0,5,0,2,0,0,0,70,0,0,0,69,0,0,0,0,0,0,0,62,0,3,0, +29,0,0,0,70,0,0,0,81,0,5,0,21,0,0,0,71,0,0,0,69,0,0,0,1,0,0,0,62,0, +3,0,31,0,0,0,71,0,0,0,81,0,5,0,2,0,0,0,72,0,0,0,69,0,0,0,2,0,0,0, +62,0,3,0,32,0,0,0,72,0,0,0,253,0,1,0,56,0,1,0 diff --git a/runtime/Includes/Graphics/Scene.h b/runtime/Includes/Graphics/Scene.h index 32f06bf..760c6fb 100644 --- a/runtime/Includes/Graphics/Scene.h +++ b/runtime/Includes/Graphics/Scene.h @@ -2,6 +2,7 @@ #define __MLX_SCENE__ #include +#include namespace mlx { @@ -18,14 +19,20 @@ namespace mlx Sprite& CreateSprite(std::shared_ptr texture) noexcept; - [[nodiscard]] inline const std::vector>& GetSprites() const noexcept { return m_sprites; } - [[nodiscard]] inline const SceneDescriptor& GetDescription() const noexcept { return m_descriptor; } + inline void ResetSprites() { m_sprites.clear(); } + + [[nodiscard]] MLX_FORCEINLINE const std::vector>& GetSprites() const noexcept { return m_sprites; } + [[nodiscard]] MLX_FORCEINLINE const SceneDescriptor& GetDescription() const noexcept { return m_descriptor; } + [[nodiscard]] MLX_FORCEINLINE DepthImage& GetDepth() noexcept { return m_depth; } + [[nodiscard]] MLX_FORCEINLINE ViewerData& GetViewerData() noexcept { return m_viewer_data; } ~Scene() = default; private: SceneDescriptor m_descriptor; std::vector> m_sprites; + DepthImage m_depth; + ViewerData m_viewer_data; }; } diff --git a/runtime/Includes/Graphics/Sprite.h b/runtime/Includes/Graphics/Sprite.h index 1720f6a..3f4b426 100644 --- a/runtime/Includes/Graphics/Sprite.h +++ b/runtime/Includes/Graphics/Sprite.h @@ -1,7 +1,7 @@ #ifndef __MLX_SPRITE__ #define __MLX_SPRITE__ -#include +#include #include #include #include @@ -17,10 +17,10 @@ namespace mlx Sprite(std::shared_ptr texture); inline void SetColor(Vec4f color) noexcept { m_color = color; } - inline void SetPosition(Vec2ui position) noexcept { m_position = position; } + inline void SetPosition(Vec3f position) noexcept { m_position = position; } [[nodiscard]] inline const Vec4f& GetColor() const noexcept { return m_color; } - [[nodiscard]] inline const Vec2ui& GetPosition() const noexcept { return m_position; } + [[nodiscard]] inline const Vec3f& GetPosition() const noexcept { return m_position; } [[nodiscard]] inline std::shared_ptr GetMesh() const { return p_mesh; } [[nodiscard]] inline std::shared_ptr GetTexture() const { return p_texture; } @@ -46,7 +46,7 @@ namespace mlx std::shared_ptr p_texture; std::shared_ptr p_mesh; Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f }; - Vec2ui m_position = Vec2ui{ 0, 0 }; + Vec3f m_position = Vec4f{ 0.0f, 0.0f, 0.0f }; }; } diff --git a/runtime/Includes/Renderer/Descriptor.h b/runtime/Includes/Renderer/Descriptor.h index 9a28ee3..b99fbfa 100644 --- a/runtime/Includes/Renderer/Descriptor.h +++ b/runtime/Includes/Renderer/Descriptor.h @@ -28,6 +28,7 @@ namespace mlx 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() noexcept; [[nodiscard]] inline VkDescriptorSet GetSet(std::size_t i) const noexcept { return m_set[i]; } [[nodiscard]] inline DescriptorSet Duplicate() const { return DescriptorSet{ m_set_layout, m_descriptors }; } diff --git a/runtime/Includes/Renderer/Enums.h b/runtime/Includes/Renderer/Enums.h index 684f4e0..ddc0487 100644 --- a/runtime/Includes/Renderer/Enums.h +++ b/runtime/Includes/Renderer/Enums.h @@ -17,6 +17,7 @@ namespace mlx enum class ImageType { Color = 0, + Depth, EndEnum }; diff --git a/runtime/Includes/Renderer/Image.h b/runtime/Includes/Renderer/Image.h index 2c3809e..fb9952e 100644 --- a/runtime/Includes/Renderer/Image.h +++ b/runtime/Includes/Renderer/Image.h @@ -23,7 +23,7 @@ namespace mlx m_layout = layout; } - void Init(ImageType type, std::uint32_t width, std::uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, bool is_multisampled = false); + void Init(ImageType type, std::uint32_t width, std::uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, bool is_multisampled = false); void CreateImageView(VkImageViewType type, VkImageAspectFlags aspectFlags, int layer_count = 1) noexcept; void CreateSampler() noexcept; void TransitionLayout(VkImageLayout new_layout, VkCommandBuffer cmd = VK_NULL_HANDLE); @@ -34,7 +34,6 @@ namespace mlx virtual void Destroy() noexcept; [[nodiscard]] MLX_FORCEINLINE VkImage Get() const noexcept { return m_image; } - [[nodiscard]] MLX_FORCEINLINE VkImage operator()() const noexcept { return m_image; } [[nodiscard]] MLX_FORCEINLINE VkDeviceMemory GetDeviceMemory() const noexcept { return m_memory.memory; } [[nodiscard]] MLX_FORCEINLINE VkImageView GetImageView() const noexcept { return m_image_view; } [[nodiscard]] MLX_FORCEINLINE VkFormat GetFormat() const noexcept { return m_format; } @@ -62,6 +61,21 @@ namespace mlx bool m_is_multisampled = false; }; + class DepthImage : public Image + { + public: + DepthImage() = default; + inline void Init(std::uint32_t width, std::uint32_t height, bool is_multisampled = false) + { + std::vector candidates = { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT }; + VkFormat format = kvfFindSupportFormatInCandidates(RenderCore::Get().GetDevice(), candidates.data(), candidates.size(), VK_IMAGE_TILING_OPTIMAL, VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT); + Image::Init(ImageType::Depth, width, height, format, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, is_multisampled); + Image::CreateImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_DEPTH_BIT); + Image::TransitionLayout(VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); + } + ~DepthImage() = default; + }; + class Texture : public Image { public: @@ -72,7 +86,7 @@ namespace mlx } inline void Init(CPUBuffer pixels, std::uint32_t width, std::uint32_t height, VkFormat format = VK_FORMAT_R8G8B8A8_SRGB, bool is_multisampled = false) { - Image::Init(ImageType::Color, width, height, format, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, is_multisampled); + Image::Init(ImageType::Color, width, height, format, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, is_multisampled); Image::CreateImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT); Image::CreateSampler(); if(pixels) diff --git a/runtime/Includes/Renderer/Pipelines/Graphics.h b/runtime/Includes/Renderer/Pipelines/Graphics.h index 027aaf4..46304bf 100644 --- a/runtime/Includes/Renderer/Pipelines/Graphics.h +++ b/runtime/Includes/Renderer/Pipelines/Graphics.h @@ -14,6 +14,7 @@ namespace mlx std::shared_ptr fragment_shader; std::vector> color_attachments; NonOwningPtr renderer = nullptr; + NonOwningPtr depth = nullptr; bool clear_color_attachments = true; bool no_vertex_inputs = false; }; @@ -51,6 +52,7 @@ namespace mlx VkPipeline m_pipeline = VK_NULL_HANDLE; VkPipelineLayout m_pipeline_layout = VK_NULL_HANDLE; NonOwningPtr p_renderer; + NonOwningPtr p_depth; }; } diff --git a/runtime/Includes/Renderer/Vertex.h b/runtime/Includes/Renderer/Vertex.h index bcc501e..d815a3e 100644 --- a/runtime/Includes/Renderer/Vertex.h +++ b/runtime/Includes/Renderer/Vertex.h @@ -8,15 +8,14 @@ namespace mlx { struct Vertex { - alignas(16) Vec2f position = Vec4f{ 0.0f, 0.0f }; - alignas(16) Vec4f color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f }; + alignas(16) Vec4f position = Vec4f{ 0.0f, 0.0f, 0.0f, 1.0f }; alignas(16) Vec2f uv = Vec2f{ 0.0f, 0.0f }; Vertex() = default; - Vertex(Vec2f p, Vec4f c, Vec2f u) : position(std::move(p)), color(std::move(c)), uv(std::move(u)) {} + Vertex(Vec4f p, Vec2f u) : position(std::move(p)), uv(std::move(u)) {} [[nodiscard]] inline static VkVertexInputBindingDescription GetBindingDescription(); - [[nodiscard]] inline static std::array GetAttributeDescriptions(); + [[nodiscard]] inline static std::array GetAttributeDescriptions(); }; } diff --git a/runtime/Includes/Renderer/Vertex.inl b/runtime/Includes/Renderer/Vertex.inl index 0c6b9ea..f23eb77 100644 --- a/runtime/Includes/Renderer/Vertex.inl +++ b/runtime/Includes/Renderer/Vertex.inl @@ -12,24 +12,19 @@ namespace mlx return binding_description; } - std::array Vertex::GetAttributeDescriptions() + std::array Vertex::GetAttributeDescriptions() { std::array attribute_descriptions; attribute_descriptions[0].binding = 0; attribute_descriptions[0].location = 0; - attribute_descriptions[0].format = VK_FORMAT_R32G32_SFLOAT; + attribute_descriptions[0].format = VK_FORMAT_R32G32B32A32_SFLOAT; attribute_descriptions[0].offset = offsetof(Vertex, position); attribute_descriptions[1].binding = 0; attribute_descriptions[1].location = 1; - attribute_descriptions[1].format = VK_FORMAT_R32G32B32A32_SFLOAT; - attribute_descriptions[1].offset = offsetof(Vertex, color); - - attribute_descriptions[2].binding = 0; - attribute_descriptions[2].location = 2; - attribute_descriptions[2].format = VK_FORMAT_R32G32_SFLOAT; - attribute_descriptions[2].offset = offsetof(Vertex, uv); + attribute_descriptions[1].format = VK_FORMAT_R32G32_SFLOAT; + attribute_descriptions[1].offset = offsetof(Vertex, uv); return attribute_descriptions; } diff --git a/runtime/Sources/Core/Graphics.cpp b/runtime/Sources/Core/Graphics.cpp index 52b4419..2f9c720 100644 --- a/runtime/Sources/Core/Graphics.cpp +++ b/runtime/Sources/Core/Graphics.cpp @@ -1,5 +1,4 @@ #include - #include namespace mlx @@ -14,8 +13,11 @@ namespace mlx MLX_PROFILE_FUNCTION(); m_renderer.SetWindow(nullptr); m_renderer.Init(render_target); - m_pixel_put_pipeline.Init(w, h, m_renderer); - m_text_manager.Init(m_renderer); + m_scene_renderer.Init(); + + SceneDescriptor descriptor{}; + descriptor.renderer = &m_renderer; + p_scene = std::make_unique(std::move(descriptor)); } GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id) : @@ -28,29 +30,21 @@ namespace mlx MLX_PROFILE_FUNCTION(); m_renderer.SetWindow(p_window.get()); m_renderer.Init(nullptr); - m_pixel_put_pipeline.Init(w, h, m_renderer); - m_text_manager.Init(m_renderer); + m_scene_renderer.Init(); + + SceneDescriptor descriptor{}; + descriptor.renderer = &m_renderer; + p_scene = std::make_unique(std::move(descriptor)); } void GraphicsSupport::Render() noexcept { MLX_PROFILE_FUNCTION(); - if(!m_renderer.BeginFrame()) - return; - m_proj = glm::ortho(0, m_width, 0, m_height); - m_renderer.GetUniformBuffer()->SetData(sizeof(m_proj), &m_proj); - - m_renderer.getVertDescriptorSet().Bind(); - - for(auto& data : m_drawlist) - data->Render(m_renderer); - - m_pixel_put_pipeline.Render(m_renderer); - - m_renderer.EndFrame(); - - for(auto& data : _drawlist) - data->ResetUpdate(); + if(m_renderer.BeginFrame()) + { + m_scene_renderer.Render(*p_scene, m_renderer); + m_renderer.EndFrame(); + } #ifdef GRAPHICS_MEMORY_DUMP // dump memory to file every two seconds @@ -67,9 +61,9 @@ namespace mlx GraphicsSupport::~GraphicsSupport() { MLX_PROFILE_FUNCTION(); - vkDeviceWaitIdle(RenderCore::Get().GetDevice().Get()); - m_text_manager.Destroy(); - m_pixel_put_pipeline.Destroy(); + RenderCore::Get().WaitDeviceIdle(); + p_scene.reset(); + m_scene_renderer.Destroy(); m_renderer->Destroy(); if(p_window) p_window->Destroy(); diff --git a/runtime/Sources/Core/SDLManager.cpp b/runtime/Sources/Core/SDLManager.cpp index c4ce300..f397e82 100644 --- a/runtime/Sources/Core/SDLManager.cpp +++ b/runtime/Sources/Core/SDLManager.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include namespace mlx { @@ -101,6 +101,8 @@ namespace mlx default: break; } + + return 0; }, &watcher_data); } diff --git a/runtime/Sources/Graphics/Scene.cpp b/runtime/Sources/Graphics/Scene.cpp index 547e641..c95fee7 100644 --- a/runtime/Sources/Graphics/Scene.cpp +++ b/runtime/Sources/Graphics/Scene.cpp @@ -3,11 +3,13 @@ #include #include -namespace Scop +namespace mlx { Scene::Scene(SceneDescriptor desc) : m_descriptor(std::move(desc)) { + Verify((bool)m_descriptor.renderer, "invalid renderer"); + m_depth.Init(m_descriptor.renderer->GetSwapchainImages().back().GetWidth(), m_descriptor.renderer->GetSwapchainImages().back().GetHeight()); } Sprite& Scene::CreateSprite(std::shared_ptr texture) noexcept diff --git a/runtime/Sources/Graphics/Sprite.cpp b/runtime/Sources/Graphics/Sprite.cpp index 8a65924..dbc5e3d 100644 --- a/runtime/Sources/Graphics/Sprite.cpp +++ b/runtime/Sources/Graphics/Sprite.cpp @@ -40,5 +40,12 @@ namespace mlx Verify((bool)texture, "Sprite: invalid texture"); p_mesh = CreateQuad(0, 0, texture->GetWidth(), texture->GetHeight()); p_texture = texture; + + func::function functor = [this](const EventBase& event) + { + if(event.What() == Event::DescriptorPoolResetEventCode) + m_set.Reallocate(); + }; + EventBus::RegisterListener({ functor, "__Sprite" + std::to_string(reinterpret_cast(this)) }); } } diff --git a/runtime/Sources/Renderer/Descriptor.cpp b/runtime/Sources/Renderer/Descriptor.cpp index 8d4ee93..2fa6d56 100644 --- a/runtime/Sources/Renderer/Descriptor.cpp +++ b/runtime/Sources/Renderer/Descriptor.cpp @@ -138,4 +138,10 @@ namespace mlx } vkUpdateDescriptorSets(RenderCore::Get().GetDevice(), writes.size(), writes.data(), 0, nullptr); } + + void Descriptor::Reallocate() noexcept + { + for(std::size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) + m_set[i] = kvfAllocateDescriptorSet(RenderCore::Get().GetDevice(), m_set_layout); + } } diff --git a/runtime/Sources/Renderer/Image.cpp b/runtime/Sources/Renderer/Image.cpp index 5ee95d0..971e7df 100644 --- a/runtime/Sources/Renderer/Image.cpp +++ b/runtime/Sources/Renderer/Image.cpp @@ -5,7 +5,7 @@ namespace mlx { - void Image::Init(ImageType type, std::uint32_t width, std::uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, bool is_multisampled) + void Image::Init(ImageType type, std::uint32_t width, std::uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, bool is_multisampled) { m_type = type; m_width = width; @@ -55,6 +55,8 @@ namespace mlx switch(m_type) { case ImageType::Color: kvf_type = KVF_IMAGE_COLOR; break; + case ImageType::Depth: kvf_type = KVF_IMAGE_DEPTH; break; + default: break; } kvfTransitionImageLayout(RenderCore::Get().GetDevice(), m_image, kvf_type, cmd, m_format, m_layout, new_layout, is_single_time_cmd_buffer); diff --git a/runtime/Sources/Renderer/Pipelines/Graphics.cpp b/runtime/Sources/Renderer/Pipelines/Graphics.cpp index 81bee86..e85d7f5 100644 --- a/runtime/Sources/Renderer/Pipelines/Graphics.cpp +++ b/runtime/Sources/Renderer/Pipelines/Graphics.cpp @@ -16,6 +16,7 @@ namespace Scop p_vertex_shader = descriptor.vertex_shader; p_fragment_shader = descriptor.fragment_shader; p_renderer = descriptor.renderer; + p_depth = descriptor.depth; std::vector push_constants; std::vector set_layouts; @@ -37,7 +38,10 @@ namespace Scop kvfGPipelineBuilderSetInputTopology(builder, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST); kvfGPipelineBuilderSetCullMode(builder, VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE); kvfGPipelineBuilderEnableAlphaBlending(builder); - kvfGPipelineBuilderDisableDepthTest(builder); + if(p_depth) + kvfGPipelineBuilderEnableDepthTest(builder, (descriptor.depth_test_equal ? VK_COMPARE_OP_EQUAL : VK_COMPARE_OP_LESS), true); + else + kvfGPipelineBuilderDisableDepthTest(builder); kvfGPipelineBuilderSetPolygonMode(builder, VK_POLYGON_MODE_FILL, 1.0f); if(features.sampleRateShading) kvfGPipelineBuilderSetMultisamplingShading(builder, VK_SAMPLE_COUNT_1_BIT, 0.25f); @@ -84,6 +88,9 @@ namespace Scop m_clears[i].color.float32[3] = clear[3]; } + if(p_depth) + m_clears.back().depthStencil = VkClearDepthStencilValue{ 1.0f, 0 }; + kvfBeginRenderPass(m_renderpass, command_buffer, fb, fb_extent, m_clears.data(), m_clears.size()); vkCmdBindPipeline(command_buffer, GetPipelineBindPoint(), GetPipeline()); return true; @@ -127,10 +134,16 @@ namespace Scop for(NonOwningPtr image : render_targets) { - attachments.push_back(kvfBuildAttachmentDescription(KVF_IMAGE_COLOR, image->GetFormat(), image->GetLayout(), image->GetLayout(), clear_attachments, VK_SAMPLE_COUNT_1_BIT)); + attachments.push_back(kvfBuildAttachmentDescription((kvfIsDepthFormat(image->GetFormat()) ? KVF_IMAGE_DEPTH : KVF_IMAGE_COLOR), image->GetFormat(), image->GetLayout(), image->GetLayout(), clear_attachments, VK_SAMPLE_COUNT_1_BIT)); attachment_views.push_back(image->GetImageView()); } + if(p_depth) + { + attachments.push_back(kvfBuildAttachmentDescription((kvfIsDepthFormat(p_depth->GetFormat()) ? KVF_IMAGE_DEPTH : KVF_IMAGE_COLOR), p_depth->GetFormat(), p_depth->GetLayout(), p_depth->GetLayout(), clear_attachments, VK_SAMPLE_COUNT_1_BIT)); + attachment_views.push_back(p_depth->GetImageView()); + } + m_renderpass = kvfCreateRenderPass(RenderCore::Get().GetDevice(), attachments.data(), attachments.size(), GetPipelineBindPoint()); m_clears.clear(); m_clears.resize(attachments.size()); @@ -154,6 +167,9 @@ namespace Scop void GraphicPipeline::TransitionAttachments(VkCommandBuffer cmd) { + if(p_depth) + p_depth->TransitionLayout(VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, cmd); + for(NonOwningPtr image : m_attachments) { if(!image->IsInit()) diff --git a/runtime/Sources/Renderer/RenderPasses/2DPass.cpp b/runtime/Sources/Renderer/RenderPasses/2DPass.cpp index 28f199e..bea3c70 100644 --- a/runtime/Sources/Renderer/RenderPasses/2DPass.cpp +++ b/runtime/Sources/Renderer/RenderPasses/2DPass.cpp @@ -11,7 +11,7 @@ namespace mlx struct SpriteData { Vec4f color; - Vec2f position; + Vec4f position; }; void Render2DPass::Init() @@ -43,10 +43,20 @@ namespace mlx }; p_fragment_shader = std::make_shared(fragment_shader, ShaderType::Fragment, std::move(fragment_shader_layout)); - std::function functor = [this](const EventBase& event) + func::function functor = [this](const EventBase& event) { if(event.What() == Event::ResizeEventCode) m_pipeline.Destroy(); + if(event.What() == Event::DescriptorPoolResetEventCode) + { + p_texture_set->Reallocate(); + p_viewer_data_set.Reallocate(); + for(std::size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) + { + p_viewer_data_set->SetUniformBuffer(i, 0, p_viewer_data_buffer->Get(i)); + p_viewer_data_set->Update(i); + } + } }; EventBus::RegisterListener({ functor, "__ScopRender2DPass" }); @@ -70,6 +80,7 @@ namespace mlx pipeline_descriptor.vertex_shader = p_vertex_shader; pipeline_descriptor.fragment_shader = p_fragment_shader; pipeline_descriptor.color_attachments = { &render_target }; + pipeline_descriptor.depth = scene.GetDepth(); pipeline_descriptor.clear_color_attachments = false; m_pipeline.Init(pipeline_descriptor); } @@ -87,7 +98,7 @@ namespace mlx for(auto sprite : scene.GetSprites()) { SpriteData sprite_data; - sprite_data.position = Vec2f{ static_cast(sprite->GetPosition().x), static_cast(sprite->GetPosition().y) }; + sprite_data.position = Vec4f{ sprite->GetPosition(), 1.0f }; sprite_data.color = sprite->GetColor(); if(!sprite->IsSetInit()) sprite->UpdateDescriptorSet(*p_texture_set); diff --git a/runtime/Sources/Renderer/RenderPasses/FinalPass.cpp b/runtime/Sources/Renderer/RenderPasses/FinalPass.cpp index d3b5cbe..5c2c410 100644 --- a/runtime/Sources/Renderer/RenderPasses/FinalPass.cpp +++ b/runtime/Sources/Renderer/RenderPasses/FinalPass.cpp @@ -30,10 +30,12 @@ namespace mlx }; p_fragment_shader = std::make_shared(fragment_shader_code, ShaderType::Fragment, std::move(fragment_shader_layout)); - std::function functor = [this](const EventBase& event) + func::function functor = [this](const EventBase& event) { if(event.What() == Event::ResizeEventCode) m_pipeline.Destroy(); + if(event.What() == Event::DescriptorPoolResetEventCode) + p_set->Reallocate(); }; EventBus::RegisterListener({ functor, "__ScopFinalPass" }); diff --git a/runtime/Sources/Renderer/RenderPasses/Passes.cpp b/runtime/Sources/Renderer/RenderPasses/Passes.cpp index a2a3e45..e905280 100644 --- a/runtime/Sources/Renderer/RenderPasses/Passes.cpp +++ b/runtime/Sources/Renderer/RenderPasses/Passes.cpp @@ -15,7 +15,7 @@ namespace mlx { if(!m_main_render_texture.IsInit()) { - std::function functor = [this, renderer](const EventBase& event) + func::function functor = [this, renderer](const EventBase& event) { if(event.What() == Event::ResizeEventCode) { @@ -31,6 +31,7 @@ namespace mlx } m_main_render_texture.Clear(renderer.GetActiveCommandBuffer(), Vec4f{ 0.0f, 0.0f, 0.0f, 1.0f }); + scene.GetDepth().Clear(renderer.GetActiveCommandBuffer(), {}); m_2Dpass.Pass(scene, renderer, m_main_render_texture); m_final.Pass(scene, renderer, m_main_render_texture); diff --git a/runtime/Sources/Renderer/Renderer.cpp b/runtime/Sources/Renderer/Renderer.cpp index 00f09b2..8aab412 100644 --- a/runtime/Sources/Renderer/Renderer.cpp +++ b/runtime/Sources/Renderer/Renderer.cpp @@ -18,11 +18,16 @@ namespace mlx { Event What() const override { return Event::FrameBeginEventCode; } }; + + struct DescriptorPoolResetEventBroadcast : public EventBase + { + Event What() const override { return Event::DescriptorPoolResetEventCode; } + }; } void Renderer::Init(NonOwningPtr window) { - std::function functor = [this](const EventBase& event) + func::function functor = [this](const EventBase& event) { if(event.What() == Event::ResizeEventCode) this->RequireFramebufferResize(); @@ -86,6 +91,7 @@ namespace mlx } m_current_frame_index = (m_current_frame_index + 1) % MAX_FRAMES_IN_FLIGHT; kvfResetDeviceDescriptorPools(RenderCore::Get().GetDevice()); + EventBus::SendBroadcast(Internal::DescriptorPoolResetEventBroadcast{}); } void Renderer::CreateSwapchain()