From 632117a3a3ae3a32e0ed2107e42784cbdc60b4ed Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Sat, 19 Oct 2024 00:43:39 +0200 Subject: [PATCH] yes --- example/main.c | 74 +++---------------- runtime/Includes/Core/Graphics.h | 4 +- runtime/Includes/Core/Graphics.inl | 16 ++-- runtime/Includes/Embedded/2DFragment.spv.h | 26 +++---- runtime/Includes/Graphics/PutPixelManager.h | 4 +- runtime/Includes/Graphics/Scene.h | 5 +- runtime/Includes/Graphics/Sprite.h | 6 +- runtime/Includes/PreCompiled.h | 1 + runtime/Includes/Renderer/Image.h | 16 ---- .../Includes/Renderer/Pipelines/Graphics.h | 2 - runtime/Sources/Core/SDLManager.cpp | 2 +- runtime/Sources/Graphics/PutPixelManager.cpp | 20 +++-- runtime/Sources/Graphics/Scene.cpp | 18 +++-- runtime/Sources/Renderer/Image.cpp | 19 ++++- .../Sources/Renderer/Pipelines/Graphics.cpp | 29 +------- runtime/Sources/Renderer/Pipelines/Shader.cpp | 4 +- .../Sources/Renderer/RenderPasses/2DPass.cpp | 28 ++++--- .../Sources/Renderer/RenderPasses/Passes.cpp | 1 - third_party/kvf.h | 8 +- 19 files changed, 109 insertions(+), 174 deletions(-) diff --git a/example/main.c b/example/main.c index 3b27ec7..f243767 100644 --- a/example/main.c +++ b/example/main.c @@ -1,19 +1,6 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* main.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */ -/* Updated: 2024/10/03 06:39:01 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - #include #include "../includes/mlx.h" -/* typedef struct { void* mlx; @@ -24,8 +11,6 @@ typedef struct void* img; } mlx_t; -//void* img = NULL; - int update(void* param) { static int i = 0; @@ -34,34 +19,35 @@ int update(void* param) if(i == 200) mlx_clear_window(mlx->mlx, mlx->win); -// if(img) -// mlx_destroy_image(mlx->mlx,img); -// img = mlx_new_image(mlx->mlx, 800, 800); -// mlx_set_image_pixel(mlx->mlx, img, 4, 4, 0xFF00FF00); -// mlx_put_image_to_window(mlx->mlx, mlx->win, img, 0, 0); - if(i >= 250) mlx_set_font_scale(mlx->mlx, mlx->win, "default", 16.f); else mlx_set_font_scale(mlx->mlx, mlx->win, "default", 6.f); + mlx_string_put(mlx->mlx, mlx->win, 160, 120, 0xFFFF2066, "this text should be hidden"); mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_png, 100, 100); - mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_jpg, 210, 150); mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_bmp, 220, 40); mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->img, 150, 60); mlx_set_font(mlx->mlx, mlx->win, "default"); mlx_string_put(mlx->mlx, mlx->win, 20, 50, 0xFFFFFFFF, "that's a text"); - int color = 0; - for(int j = 0; j < 400; j++) + for(int j = 0, color = 0; j < 400; j++) { mlx_pixel_put(mlx->mlx, mlx->win, j, j, 0xFFFF0000 + color); mlx_pixel_put(mlx->mlx, mlx->win, 399 - j, j, 0xFF0000FF); color += (color < 255); } + mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_jpg, 210, 150); + + for(int j = 0; j < 20; j++) + { + for(int k = 0; k < 20; k++) + mlx_pixel_put(mlx->mlx, mlx->win, 220 + j, 160 + k, 0xFFFF0000); + } + i++; return 0; } @@ -174,44 +160,6 @@ int main(void) mlx_destroy_image(mlx.mlx, mlx.img); mlx_destroy_window(mlx.mlx, mlx.win); mlx_destroy_display(mlx.mlx); - - return 0; -} -*/ - -int window_hook(int event, void* param) -{ - if(event == 0) - mlx_loop_end(param); - return 0; -} - -int main(void) -{ - void* mlx; - void* win; - - mlx = mlx_init(); - win = mlx_new_window(mlx, 400, 400, "My window"); - - mlx_set_fps_goal(mlx, 60); - - mlx_on_event(mlx, win, MLX_WINDOW_EVENT, window_hook, mlx); - - int dummy; - void* logo_png = mlx_png_file_to_image(mlx, "42_logo.png", &dummy, &dummy); - - mlx_put_image_to_window(mlx, win, logo_png, 10, 190); - mlx_put_image_to_window(mlx, win, logo_png, 11, 190); - mlx_put_image_to_window(mlx, win, logo_png, 10, 190); - mlx_put_image_to_window(mlx, win, logo_png, 100, 190); - mlx_put_image_to_window(mlx, win, logo_png, 10, 190); - - mlx_loop(mlx); - - mlx_destroy_image(mlx, logo_png); - mlx_destroy_window(mlx, win); - mlx_destroy_display(mlx); - + return 0; } diff --git a/runtime/Includes/Core/Graphics.h b/runtime/Includes/Core/Graphics.h index 5c8de28..c2be4c0 100644 --- a/runtime/Includes/Core/Graphics.h +++ b/runtime/Includes/Core/Graphics.h @@ -44,11 +44,11 @@ namespace mlx std::shared_ptr p_window; std::unique_ptr p_scene; - std::uint64_t m_current_depth = 0; - int m_id; bool m_has_window; + + bool m_insert_new_pixel_put_texture = false; }; } diff --git a/runtime/Includes/Core/Graphics.inl b/runtime/Includes/Core/Graphics.inl index 93a7760..2ba4a09 100644 --- a/runtime/Includes/Core/Graphics.inl +++ b/runtime/Includes/Core/Graphics.inl @@ -1,6 +1,5 @@ #pragma once #include -#include namespace mlx { @@ -9,20 +8,19 @@ namespace mlx MLX_PROFILE_FUNCTION(); p_scene->ResetSprites(); m_put_pixel_manager.ResetRenderData(); - m_current_depth = 0; + m_insert_new_pixel_put_texture = true; } void GraphicsSupport::PixelPut(int x, int y, std::uint32_t color) noexcept { MLX_PROFILE_FUNCTION(); - /* - NonOwningPtr texture = m_put_pixel_manager.DrawPixel(x, y, m_current_depth, color); + NonOwningPtr texture = m_put_pixel_manager.DrawPixel(x, y, m_insert_new_pixel_put_texture, color); if(texture) { Sprite& new_sprite = p_scene->CreateSprite(texture); - new_sprite.SetPosition(Vec3f{ 0.0f, 0.0f, static_cast(m_current_depth) }); + new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f }); } - */ + m_insert_new_pixel_put_texture = false; } void GraphicsSupport::StringPut(int x, int y, std::uint32_t color, std::string str) @@ -41,11 +39,11 @@ namespace mlx if(!sprite) { Sprite& new_sprite = p_scene->CreateSprite(texture); - new_sprite.SetPosition(Vec3f{ static_cast(x), static_cast(y), static_cast(m_current_depth) }); - m_current_depth++; + new_sprite.SetPosition(Vec2f{ static_cast(x), static_cast(y) }); + m_insert_new_pixel_put_texture = true; } else - sprite->SetPosition(Vec3f{ static_cast(x), static_cast(y), static_cast(m_current_depth) }); + p_scene->BringToFront(std::move(sprite)); } void GraphicsSupport::LoadFont(const std::filesystem::path& filepath, float scale) diff --git a/runtime/Includes/Embedded/2DFragment.spv.h b/runtime/Includes/Embedded/2DFragment.spv.h index 5792158..f71f131 100644 --- a/runtime/Includes/Embedded/2DFragment.spv.h +++ b/runtime/Includes/Embedded/2DFragment.spv.h @@ -1,4 +1,4 @@ -3,2,35,7,0,0,1,0,39,0,0,0,51,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,14,0, +3,2,35,7,0,0,1,0,39,0,0,0,48,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,8,0,4,0,0,0,28,0,0,0,109,97,105,110,0,0,0,0, 10,0,0,0,16,0,0,0,22,0,0,0,16,0,3,0,28,0,0,0,7,0,0,0,3,0,3,0,0,0, 0,0,100,0,0,0,5,0,4,0,19,0,0,0,86,101,114,116,79,117,116,0,6,0,5,0,19,0,0,0, @@ -29,16 +29,14 @@ 7,0,0,0,248,0,2,0,29,0,0,0,59,0,4,0,24,0,0,0,30,0,0,0,7,0,0,0,59,0, 4,0,20,0,0,0,31,0,0,0,7,0,0,0,65,0,5,0,13,0,0,0,32,0,0,0,31,0,0,0, 12,0,0,0,63,0,3,0,32,0,0,0,10,0,0,0,65,0,5,0,18,0,0,0,33,0,0,0,31,0, -0,0,17,0,0,0,63,0,3,0,33,0,0,0,16,0,0,0,65,0,5,0,13,0,0,0,34,0,0,0, -31,0,0,0,12,0,0,0,61,0,4,0,8,0,0,0,35,0,0,0,34,0,0,0,61,0,4,0,3,0, -0,0,36,0,0,0,5,0,0,0,65,0,5,0,18,0,0,0,37,0,0,0,31,0,0,0,17,0,0,0, -61,0,4,0,14,0,0,0,38,0,0,0,37,0,0,0,87,0,5,0,8,0,0,0,39,0,0,0,36,0, -0,0,38,0,0,0,133,0,5,0,8,0,0,0,40,0,0,0,35,0,0,0,39,0,0,0,65,0,5,0, -13,0,0,0,41,0,0,0,30,0,0,0,12,0,0,0,62,0,3,0,41,0,0,0,40,0,0,0,65,0, -5,0,13,0,0,0,45,0,0,0,30,0,0,0,12,0,0,0,61,0,4,0,8,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,3,0,0,0,180,0,5,0,27,0, -0,0,48,0,0,0,47,0,0,0,26,0,0,0,247,0,3,0,42,0,0,0,0,0,0,0,250,0,4,0, -48,0,0,0,43,0,0,0,44,0,0,0,248,0,2,0,43,0,0,0,252,0,1,0,248,0,2,0,44,0, -0,0,249,0,2,0,42,0,0,0,248,0,2,0,42,0,0,0,61,0,4,0,23,0,0,0,49,0,0,0, -30,0,0,0,81,0,5,0,8,0,0,0,50,0,0,0,49,0,0,0,0,0,0,0,62,0,3,0,22,0, -0,0,50,0,0,0,253,0,1,0,56,0,1,0 +0,0,17,0,0,0,63,0,3,0,33,0,0,0,16,0,0,0,61,0,4,0,3,0,0,0,34,0,0,0, +5,0,0,0,65,0,5,0,18,0,0,0,35,0,0,0,31,0,0,0,17,0,0,0,61,0,4,0,14,0, +0,0,36,0,0,0,35,0,0,0,87,0,5,0,8,0,0,0,37,0,0,0,34,0,0,0,36,0,0,0, +65,0,5,0,13,0,0,0,38,0,0,0,30,0,0,0,12,0,0,0,62,0,3,0,38,0,0,0,37,0, +0,0,65,0,5,0,13,0,0,0,42,0,0,0,30,0,0,0,12,0,0,0,61,0,4,0,8,0,0,0, +43,0,0,0,42,0,0,0,81,0,5,0,1,0,0,0,44,0,0,0,43,0,0,0,3,0,0,0,180,0, +5,0,27,0,0,0,45,0,0,0,44,0,0,0,26,0,0,0,247,0,3,0,39,0,0,0,0,0,0,0, +250,0,4,0,45,0,0,0,40,0,0,0,41,0,0,0,248,0,2,0,40,0,0,0,252,0,1,0,248,0, +2,0,41,0,0,0,249,0,2,0,39,0,0,0,248,0,2,0,39,0,0,0,61,0,4,0,23,0,0,0, +46,0,0,0,30,0,0,0,81,0,5,0,8,0,0,0,47,0,0,0,46,0,0,0,0,0,0,0,62,0, +3,0,22,0,0,0,47,0,0,0,253,0,1,0,56,0,1,0 diff --git a/runtime/Includes/Graphics/PutPixelManager.h b/runtime/Includes/Graphics/PutPixelManager.h index 70a9039..5249045 100644 --- a/runtime/Includes/Graphics/PutPixelManager.h +++ b/runtime/Includes/Graphics/PutPixelManager.h @@ -11,13 +11,13 @@ namespace mlx PutPixelManager(NonOwningPtr renderer) : p_renderer(renderer) {} // Return a valid pointer when a new texture has been created - NonOwningPtr DrawPixel(int x, int y, std::uint64_t z, std::uint32_t color); + NonOwningPtr DrawPixel(int x, int y, bool insert_new_texture, std::uint32_t color); void ResetRenderData(); ~PutPixelManager(); private: - std::map m_textures; + std::list m_textures; NonOwningPtr p_renderer; }; } diff --git a/runtime/Includes/Graphics/Scene.h b/runtime/Includes/Graphics/Scene.h index e4bd110..4c7f6a7 100644 --- a/runtime/Includes/Graphics/Scene.h +++ b/runtime/Includes/Graphics/Scene.h @@ -20,21 +20,20 @@ namespace mlx Sprite& CreateSprite(NonOwningPtr texture) noexcept; NonOwningPtr GetSpriteFromTextureAndPosition(NonOwningPtr texture, const Vec2f& position) const; + void BringToFront(NonOwningPtr sprite); void TryEraseSpriteFromTexture(NonOwningPtr texture); 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(); + ~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 3221852..757e30d 100644 --- a/runtime/Includes/Graphics/Sprite.h +++ b/runtime/Includes/Graphics/Sprite.h @@ -17,10 +17,10 @@ namespace mlx Sprite(class Renderer& renderer, NonOwningPtr texture); inline void SetColor(Vec4f color) noexcept { m_color = color; } - inline void SetPosition(Vec3f position) noexcept { m_position = position; } + inline void SetPosition(Vec2f position) noexcept { m_position = position; } [[nodiscard]] MLX_FORCEINLINE const Vec4f& GetColor() const noexcept { return m_color; } - [[nodiscard]] MLX_FORCEINLINE const Vec3f& GetPosition() const noexcept { return m_position; } + [[nodiscard]] MLX_FORCEINLINE const Vec2f& GetPosition() const noexcept { return m_position; } [[nodiscard]] MLX_FORCEINLINE std::shared_ptr GetMesh() const { return p_mesh; } [[nodiscard]] MLX_FORCEINLINE NonOwningPtr GetTexture() const { return p_texture; } @@ -46,7 +46,7 @@ namespace mlx NonOwningPtr p_texture; std::shared_ptr p_mesh; Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f }; - Vec3f m_position = Vec3f{ 0.0f, 0.0f, 0.0f }; + Vec2f m_position = Vec2f{ 0.0f, 0.0f }; }; } diff --git a/runtime/Includes/PreCompiled.h b/runtime/Includes/PreCompiled.h index bc3efbc..fde27c8 100644 --- a/runtime/Includes/PreCompiled.h +++ b/runtime/Includes/PreCompiled.h @@ -48,6 +48,7 @@ #include #include #include +#include #ifndef MLX_PLAT_WINDOWS #include diff --git a/runtime/Includes/Renderer/Image.h b/runtime/Includes/Renderer/Image.h index 55840ed..c857089 100644 --- a/runtime/Includes/Renderer/Image.h +++ b/runtime/Includes/Renderer/Image.h @@ -67,22 +67,6 @@ 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, [[maybe_unused]] std::string_view debug_name) - { - MLX_PROFILE_FUNCTION(); - 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 | VK_IMAGE_USAGE_TRANSFER_DST_BIT, is_multisampled, std::move(debug_name)); - 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: diff --git a/runtime/Includes/Renderer/Pipelines/Graphics.h b/runtime/Includes/Renderer/Pipelines/Graphics.h index 09aa939..246494a 100644 --- a/runtime/Includes/Renderer/Pipelines/Graphics.h +++ b/runtime/Includes/Renderer/Pipelines/Graphics.h @@ -14,7 +14,6 @@ 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; }; @@ -55,7 +54,6 @@ 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/Sources/Core/SDLManager.cpp b/runtime/Sources/Core/SDLManager.cpp index 0c5c20f..1e0b873 100644 --- a/runtime/Sources/Core/SDLManager.cpp +++ b/runtime/Sources/Core/SDLManager.cpp @@ -101,7 +101,7 @@ namespace mlx #endif #ifdef VK_USE_PLATFORM_WAYLAND_KHR - extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME); + // extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME); #endif #ifdef VK_USE_PLATFORM_WIN32_KHR diff --git a/runtime/Sources/Graphics/PutPixelManager.cpp b/runtime/Sources/Graphics/PutPixelManager.cpp index b4727cf..929e504 100644 --- a/runtime/Sources/Graphics/PutPixelManager.cpp +++ b/runtime/Sources/Graphics/PutPixelManager.cpp @@ -5,18 +5,22 @@ namespace mlx { - NonOwningPtr PutPixelManager::DrawPixel(int x, int y, std::uint64_t z, std::uint32_t color) + NonOwningPtr PutPixelManager::DrawPixel(int x, int y, bool insert_new_texture, std::uint32_t color) { Verify((bool)p_renderer, "invalid renderer pointer"); VkExtent2D swapchain_extent = kvfGetSwapchainImagesSize(p_renderer->GetSwapchain()); - #ifdef DEBUG - auto res = m_textures.try_emplace(z, CPUBuffer{}, swapchain_extent.width, swapchain_extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_put_pixel_layer_" + std::to_string(z)); - #else - auto res = m_textures.try_emplace(z, CPUBuffer{}, swapchain_extent.width, swapchain_extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, std::string_view{}); - #endif - res.first->second.SetPixel(x, y, color); - return (res.second ? &res.first->second : nullptr); + if(insert_new_texture) + { + #ifdef DEBUG + Texture& texture = m_textures.emplace_back(CPUBuffer{}, swapchain_extent.width, swapchain_extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_put_pixel_layer_" + std::to_string(m_textures.size())); + #else + Texture& texture = m_textures.emplace_back(CPUBuffer{}, swapchain_extent.width, swapchain_extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, std::string_view{}); + #endif + texture.Clear(VK_NULL_HANDLE, Vec4f{ 0.0f }); + } + m_textures.back().SetPixel(x, y, color); + return (insert_new_texture ? &m_textures.back() : nullptr); } void PutPixelManager::ResetRenderData() diff --git a/runtime/Sources/Graphics/Scene.cpp b/runtime/Sources/Graphics/Scene.cpp index c508553..e1bf59b 100644 --- a/runtime/Sources/Graphics/Scene.cpp +++ b/runtime/Sources/Graphics/Scene.cpp @@ -10,7 +10,6 @@ namespace mlx { MLX_PROFILE_FUNCTION(); Verify((bool)m_descriptor.renderer, "invalid renderer"); - m_depth.Init(m_descriptor.renderer->GetSwapchainImages().back().GetWidth(), m_descriptor.renderer->GetSwapchainImages().back().GetHeight(), false, "mlx_scene_depth"); } Sprite& Scene::CreateSprite(NonOwningPtr texture) noexcept @@ -31,6 +30,18 @@ namespace mlx return (it != m_sprites.end() ? it->get() : nullptr); } + void Scene::BringToFront(NonOwningPtr sprite) + { + MLX_PROFILE_FUNCTION(); + auto it = std::find_if(m_sprites.begin(), m_sprites.end(), [&sprite](std::shared_ptr sprite_ptr) + { + return sprite_ptr.get() == sprite.Get(); + }); + if(it == m_sprites.end()) + return; + std::rotate(it, it + 1, m_sprites.end()); + } + void Scene::TryEraseSpriteFromTexture(NonOwningPtr texture) { MLX_PROFILE_FUNCTION(); @@ -44,9 +55,4 @@ namespace mlx m_sprites.erase(it); } while(it != m_sprites.end()); } - - Scene::~Scene() - { - m_depth.Destroy(); - } } diff --git a/runtime/Sources/Renderer/Image.cpp b/runtime/Sources/Renderer/Image.cpp index 173523a..9b8a0b7 100644 --- a/runtime/Sources/Renderer/Image.cpp +++ b/runtime/Sources/Renderer/Image.cpp @@ -100,6 +100,13 @@ namespace mlx subresource_range.levelCount = 1; subresource_range.baseArrayLayer = 0; + bool is_single_time_cmd_buffer = (cmd == VK_NULL_HANDLE); + if(is_single_time_cmd_buffer) + { + cmd = kvfCreateCommandBuffer(RenderCore::Get().GetDevice()); + kvfBeginCommandBuffer(cmd, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT); + } + VkImageLayout old_layout = m_layout; TransitionLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, cmd); if(m_type == ImageType::Color) @@ -115,6 +122,14 @@ namespace mlx RenderCore::Get().vkCmdClearDepthStencilImage(cmd, m_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clear_depth_stencil, 1, &subresource_range); } TransitionLayout(old_layout, cmd); + + if(is_single_time_cmd_buffer) + { + kvfEndCommandBuffer(cmd); + VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice()); + kvfSubmitSingleTimeCommandBuffer(RenderCore::Get().GetDevice(), cmd, KVF_GRAPHICS_QUEUE, fence); + kvfDestroyFence(RenderCore::Get().GetDevice(), fence); + } } void Image::DestroySampler() noexcept @@ -165,7 +180,7 @@ namespace mlx kvfBeginCommandBuffer(cmd, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT); TransitionLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, cmd); kvfCopyBufferToImage(cmd, Image::Get(), staging_buffer.Get(), staging_buffer.GetOffset(), VK_IMAGE_ASPECT_COLOR_BIT, { width, height, 1 }); - RenderCore::Get().vkEndCommandBuffer(cmd); + kvfEndCommandBuffer(cmd); VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice()); kvfSubmitSingleTimeCommandBuffer(RenderCore::Get().GetDevice(), cmd, KVF_GRAPHICS_QUEUE, fence); kvfDestroyFence(RenderCore::Get().GetDevice(), fence); @@ -237,7 +252,7 @@ namespace mlx TransitionLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, cmd); kvfCopyImageToBuffer(cmd, m_staging_buffer->Get(), m_image, m_staging_buffer->GetOffset(), VK_IMAGE_ASPECT_COLOR_BIT, { m_width, m_height, 1 }); TransitionLayout(old_layout, cmd); - RenderCore::Get().vkEndCommandBuffer(cmd); + kvfEndCommandBuffer(cmd); VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice()); kvfSubmitSingleTimeCommandBuffer(RenderCore::Get().GetDevice(), cmd, KVF_GRAPHICS_QUEUE, fence); kvfDestroyFence(RenderCore::Get().GetDevice(), fence); diff --git a/runtime/Sources/Renderer/Pipelines/Graphics.cpp b/runtime/Sources/Renderer/Pipelines/Graphics.cpp index ecab564..7013dfd 100644 --- a/runtime/Sources/Renderer/Pipelines/Graphics.cpp +++ b/runtime/Sources/Renderer/Pipelines/Graphics.cpp @@ -17,7 +17,6 @@ namespace mlx p_vertex_shader = descriptor.vertex_shader; p_fragment_shader = descriptor.fragment_shader; p_renderer = descriptor.renderer; - p_depth = descriptor.depth; #ifdef DEBUG m_debug_name = debug_name; @@ -34,24 +33,15 @@ namespace mlx TransitionAttachments(); CreateFramebuffers(m_attachments, descriptor.clear_color_attachments); - VkPhysicalDeviceFeatures features{}; - RenderCore::Get().vkGetPhysicalDeviceFeatures(RenderCore::Get().GetPhysicalDevice(), &features); - KvfGraphicsPipelineBuilder* builder = kvfCreateGPipelineBuilder(); kvfGPipelineBuilderAddShaderStage(builder, p_vertex_shader->GetShaderStage(), p_vertex_shader->GetShaderModule(), "main"); kvfGPipelineBuilderAddShaderStage(builder, p_fragment_shader->GetShaderStage(), p_fragment_shader->GetShaderModule(), "main"); kvfGPipelineBuilderSetInputTopology(builder, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST); kvfGPipelineBuilderSetCullMode(builder, VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE); - kvfGPipelineBuilderEnableAlphaBlending(builder); - if(p_depth) - kvfGPipelineBuilderEnableDepthTest(builder, VK_COMPARE_OP_LESS, true); - else - kvfGPipelineBuilderDisableDepthTest(builder); + kvfGPipelineBuilderDisableDepthTest(builder); kvfGPipelineBuilderSetPolygonMode(builder, VK_POLYGON_MODE_FILL, 1.0f); - if(features.sampleRateShading) - kvfGPipelineBuilderSetMultisamplingShading(builder, VK_SAMPLE_COUNT_1_BIT, 0.25f); - else - kvfGPipelineBuilderSetMultisampling(builder, VK_SAMPLE_COUNT_1_BIT); + kvfGPipelineBuilderSetMultisampling(builder, VK_SAMPLE_COUNT_1_BIT); + kvfGPipelineBuilderEnableAlphaBlending(builder); if(!descriptor.no_vertex_inputs) { @@ -122,9 +112,6 @@ namespace mlx 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()); RenderCore::Get().vkCmdBindPipeline(command_buffer, GetPipelineBindPoint(), GetPipeline()); return true; @@ -177,12 +164,6 @@ namespace mlx 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()); @@ -208,10 +189,6 @@ namespace mlx void GraphicPipeline::TransitionAttachments(VkCommandBuffer cmd) { MLX_PROFILE_FUNCTION(); - if(p_depth) - p_depth->TransitionLayout(VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, cmd); - - #pragma omp parallel for for(NonOwningPtr image : m_attachments) { if(!image->IsInit()) diff --git a/runtime/Sources/Renderer/Pipelines/Shader.cpp b/runtime/Sources/Renderer/Pipelines/Shader.cpp index 7dd4691..0e527f2 100644 --- a/runtime/Sources/Renderer/Pipelines/Shader.cpp +++ b/runtime/Sources/Renderer/Pipelines/Shader.cpp @@ -41,15 +41,13 @@ namespace mlx } std::size_t i = 0; - std::vector push_constants(layout.push_constants.size()); m_pipeline_layout_part.push_constants.resize(layout.push_constants.size()); - for(auto& pc : layout.push_constants) + for(const auto& pc : layout.push_constants) { VkPushConstantRange push_constant_range = {}; push_constant_range.offset = pc.offset; push_constant_range.size = pc.size; push_constant_range.stageFlags = m_stage; - push_constants[i] = push_constant_range; m_pipeline_layout_part.push_constants[i] = push_constant_range; i++; } diff --git a/runtime/Sources/Renderer/RenderPasses/2DPass.cpp b/runtime/Sources/Renderer/RenderPasses/2DPass.cpp index f2f5ed8..642804e 100644 --- a/runtime/Sources/Renderer/RenderPasses/2DPass.cpp +++ b/runtime/Sources/Renderer/RenderPasses/2DPass.cpp @@ -17,6 +17,7 @@ namespace mlx void Render2DPass::Init(Renderer& renderer) { MLX_PROFILE_FUNCTION(); + ShaderLayout vertex_shader_layout( { { 0, @@ -81,7 +82,6 @@ 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; #ifdef DEBUG m_pipeline.Init(pipeline_descriptor, "mlx_2D_pass"); @@ -93,28 +93,38 @@ namespace mlx std::uint32_t frame_index = renderer.GetCurrentFrameIndex(); ViewerData viewer_data; - viewer_data.projection_matrix = Mat4f::Ortho(0.0f, render_target.GetWidth(), render_target.GetHeight(), 0.0f, -1.0f, 100'000.0f); + viewer_data.projection_matrix = Mat4f::Ortho(0.0f, render_target.GetWidth(), render_target.GetHeight(), 0.0f, -1.0f, 1.0f); static CPUBuffer buffer(sizeof(ViewerData)); std::memcpy(buffer.GetData(), &viewer_data, buffer.GetSize()); p_viewer_data_buffer->SetData(buffer, frame_index); VkCommandBuffer cmd = renderer.GetActiveCommandBuffer(); - m_pipeline.BindPipeline(cmd, 0, {}); - #pragma omp parallel for - for(auto sprite : scene.GetSprites()) + const auto& sprites = scene.GetSprites(); + + for(auto sprite : sprites) { - SpriteData sprite_data; - sprite_data.position = Vec4f{ sprite->GetPosition(), 1.0f }; - sprite_data.color = sprite->GetColor(); + // Check every textures and update modified ones to GPU before starting the render pass if(!sprite->IsSetInit()) sprite->UpdateDescriptorSet(*p_texture_set); Verify((bool)sprite->GetTexture(), "a sprite has no texture attached (internal mlx issue, please report to the devs)"); sprite->GetTexture()->Update(cmd); + } + + m_pipeline.BindPipeline(cmd, 0, {}); + for(auto sprite : sprites) + { + SpriteData sprite_data; + sprite_data.position = Vec4f{ sprite->GetPosition(), 0.0f, 1.0f }; + sprite_data.color = sprite->GetColor(); + sprite->Bind(frame_index, cmd); + std::array sets = { p_viewer_data_set->GetSet(frame_index), sprite->GetSet(frame_index) }; - RenderCore::Get().vkCmdBindDescriptorSets(cmd, m_pipeline.GetPipelineBindPoint(), m_pipeline.GetPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr); + RenderCore::Get().vkCmdPushConstants(cmd, m_pipeline.GetPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(SpriteData), &sprite_data); + RenderCore::Get().vkCmdBindDescriptorSets(cmd, m_pipeline.GetPipelineBindPoint(), m_pipeline.GetPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr); + sprite->GetMesh()->Draw(cmd, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef()); } m_pipeline.EndPipeline(cmd); diff --git a/runtime/Sources/Renderer/RenderPasses/Passes.cpp b/runtime/Sources/Renderer/RenderPasses/Passes.cpp index b23ebc8..ce31c2c 100644 --- a/runtime/Sources/Renderer/RenderPasses/Passes.cpp +++ b/runtime/Sources/Renderer/RenderPasses/Passes.cpp @@ -37,7 +37,6 @@ namespace mlx void RenderPasses::Pass(Scene& scene, Renderer& renderer) { 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/third_party/kvf.h b/third_party/kvf.h index 8256b24..6bc5e75 100755 --- a/third_party/kvf.h +++ b/third_party/kvf.h @@ -2974,10 +2974,10 @@ VkPipeline kvfCreateGraphicsPipeline(VkDevice device, VkPipelineLayout layout, K color_blending.logicOp = VK_LOGIC_OP_COPY; color_blending.attachmentCount = 1; color_blending.pAttachments = &builder->color_blend_attachment_state; - color_blending.blendConstants[0] = 0.0f; - color_blending.blendConstants[1] = 0.0f; - color_blending.blendConstants[2] = 0.0f; - color_blending.blendConstants[3] = 0.0f; + color_blending.blendConstants[0] = 1.0f; + color_blending.blendConstants[1] = 1.0f; + color_blending.blendConstants[2] = 1.0f; + color_blending.blendConstants[3] = 1.0f; VkDynamicState states[] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };