mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-11 14:43:34 +00:00
yes
This commit is contained in:
@@ -1,19 +1,6 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */
|
||||
/* Updated: 2024/10/03 06:39:01 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
||||
@@ -177,41 +163,3 @@ int main(void)
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -44,11 +44,11 @@ namespace mlx
|
||||
std::shared_ptr<Window> p_window;
|
||||
std::unique_ptr<Scene> p_scene;
|
||||
|
||||
std::uint64_t m_current_depth = 0;
|
||||
|
||||
int m_id;
|
||||
|
||||
bool m_has_window;
|
||||
|
||||
bool m_insert_new_pixel_put_texture = false;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
#include <Core/Graphics.h>
|
||||
#include <iostream>
|
||||
|
||||
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> texture = m_put_pixel_manager.DrawPixel(x, y, m_current_depth, color);
|
||||
NonOwningPtr<Texture> 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<float>(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<float>(x), static_cast<float>(y), static_cast<float>(m_current_depth) });
|
||||
m_current_depth++;
|
||||
new_sprite.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
|
||||
m_insert_new_pixel_put_texture = true;
|
||||
}
|
||||
else
|
||||
sprite->SetPosition(Vec3f{ static_cast<float>(x), static_cast<float>(y), static_cast<float>(m_current_depth) });
|
||||
p_scene->BringToFront(std::move(sprite));
|
||||
}
|
||||
|
||||
void GraphicsSupport::LoadFont(const std::filesystem::path& filepath, float scale)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -11,13 +11,13 @@ namespace mlx
|
||||
PutPixelManager(NonOwningPtr<class Renderer> renderer) : p_renderer(renderer) {}
|
||||
|
||||
// Return a valid pointer when a new texture has been created
|
||||
NonOwningPtr<Texture> DrawPixel(int x, int y, std::uint64_t z, std::uint32_t color);
|
||||
NonOwningPtr<Texture> DrawPixel(int x, int y, bool insert_new_texture, std::uint32_t color);
|
||||
void ResetRenderData();
|
||||
|
||||
~PutPixelManager();
|
||||
|
||||
private:
|
||||
std::map<std::uint64_t, Texture> m_textures;
|
||||
std::list<Texture> m_textures;
|
||||
NonOwningPtr<class Renderer> p_renderer;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -20,21 +20,20 @@ namespace mlx
|
||||
|
||||
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);
|
||||
|
||||
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 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<std::shared_ptr<Sprite>> m_sprites;
|
||||
DepthImage m_depth;
|
||||
ViewerData m_viewer_data;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -17,10 +17,10 @@ namespace mlx
|
||||
Sprite(class Renderer& renderer, NonOwningPtr<Texture> 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<Mesh> GetMesh() const { return p_mesh; }
|
||||
[[nodiscard]] MLX_FORCEINLINE NonOwningPtr<Texture> GetTexture() const { return p_texture; }
|
||||
|
||||
@@ -46,7 +46,7 @@ namespace mlx
|
||||
NonOwningPtr<Texture> p_texture;
|
||||
std::shared_ptr<Mesh> 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 };
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@
|
||||
#include <string_view>
|
||||
#include <sstream>
|
||||
#include <ostream>
|
||||
#include <ranges>
|
||||
|
||||
#ifndef MLX_PLAT_WINDOWS
|
||||
#include <dlfcn.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<VkFormat> 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:
|
||||
|
||||
@@ -14,7 +14,6 @@ namespace mlx
|
||||
std::shared_ptr<Shader> fragment_shader;
|
||||
std::vector<NonOwningPtr<Texture>> color_attachments;
|
||||
NonOwningPtr<class Renderer> renderer = nullptr;
|
||||
NonOwningPtr<DepthImage> 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<class Renderer> p_renderer;
|
||||
NonOwningPtr<DepthImage> p_depth;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -5,18 +5,22 @@
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
NonOwningPtr<Texture> PutPixelManager::DrawPixel(int x, int y, std::uint64_t z, std::uint32_t color)
|
||||
NonOwningPtr<Texture> 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());
|
||||
if(insert_new_texture)
|
||||
{
|
||||
#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));
|
||||
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
|
||||
auto res = m_textures.try_emplace(z, CPUBuffer{}, swapchain_extent.width, swapchain_extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, std::string_view{});
|
||||
Texture& texture = m_textures.emplace_back(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);
|
||||
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()
|
||||
|
||||
@@ -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> texture) noexcept
|
||||
@@ -31,6 +30,18 @@ namespace mlx
|
||||
return (it != m_sprites.end() ? it->get() : nullptr);
|
||||
}
|
||||
|
||||
void Scene::BringToFront(NonOwningPtr<Sprite> sprite)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
auto it = std::find_if(m_sprites.begin(), m_sprites.end(), [&sprite](std::shared_ptr<Sprite> 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> texture)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
@@ -44,9 +55,4 @@ namespace mlx
|
||||
m_sprites.erase(it);
|
||||
} while(it != m_sprites.end());
|
||||
}
|
||||
|
||||
Scene::~Scene()
|
||||
{
|
||||
m_depth.Destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
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);
|
||||
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<Texture> image : m_attachments)
|
||||
{
|
||||
if(!image->IsInit())
|
||||
|
||||
@@ -41,15 +41,13 @@ namespace mlx
|
||||
}
|
||||
|
||||
std::size_t i = 0;
|
||||
std::vector<VkPushConstantRange> 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++;
|
||||
}
|
||||
|
||||
@@ -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<VkDescriptorSet, 2> 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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
8
third_party/kvf.h
vendored
8
third_party/kvf.h
vendored
@@ -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 };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user