finxing memory usages

This commit is contained in:
2024-12-16 01:24:25 +01:00
parent 5b726fe74a
commit feb3fcbd1f
24 changed files with 315 additions and 79 deletions

View File

@@ -109,12 +109,7 @@ namespace mlx
Application::~Application()
{
for(auto& window : m_graphics)
{
if(window && window->GetWindow()->GetName() == "让我们在月光下做爱吧")
window.reset();
}
m_mesh_registry.Reset();
m_font_registry.Reset();
p_render_core.reset();
p_sdl_manager.reset();

View File

@@ -1,3 +1,4 @@
#include "mlx_extended.h"
#include <PreCompiled.h>
#include <Core/Application.h>
@@ -274,7 +275,7 @@ extern "C"
mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
if(!texture)
return;
gs->TexturePut(texture, x, y, 1.0f, 0.0f);
gs->TexturePut(texture, x, y, 1.0f, 1.0f, 0.0f);
}
void mlx_string_put(mlx_context mlx, mlx_window win, int x, int y, int color, char* str)
@@ -299,7 +300,7 @@ extern "C"
void mlx_set_font(mlx_context mlx, char* filepath)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if (filepath == nullptr)
if(filepath == nullptr)
{
mlx::Error("Font loader: filepath is NULL");
return;
@@ -319,7 +320,7 @@ extern "C"
void mlx_set_font_scale(mlx_context mlx, char* filepath, float scale)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if (filepath == nullptr)
if(filepath == nullptr)
{
mlx::Error("Font loader: filepath is NULL");
return;
@@ -332,4 +333,87 @@ extern "C"
}
mlx->app->LoadFont(file, scale);
}
// Extended
void mlx_set_window_max_size(mlx_context mlx, mlx_window win, int x, int y)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
gs->GetWindow()->SetMaxSize(x, y);
}
void mlx_set_window_min_size(mlx_context mlx, mlx_window win, int x, int y)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
gs->GetWindow()->SetMinSize(x, y);
}
void mlx_maximise_window(mlx_context mlx, mlx_window win)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
gs->GetWindow()->Maximize();
}
void mlx_minimize_window(mlx_context mlx, mlx_window win)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
gs->GetWindow()->Minimize();
}
void mlx_restore_window(mlx_context mlx, mlx_window win)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
gs->GetWindow()->Restore();
}
void mlx_pixel_put_array(mlx_context mlx, mlx_window win, int x, int y, int* pixels)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
}
void mlx_get_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, int* dst)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
if(!texture)
return;
}
void mlx_set_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, int* pixels)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
if(!texture)
return;
}
void mlx_put_transformed_image_to_window(mlx_context mlx, mlx_window win, mlx_image image, int x, int y, float scale_x, float scale_y, float angle)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
if(!texture)
return;
gs->TexturePut(texture, x, y, scale_x, scale_y, angle);
}
}

View File

@@ -68,15 +68,16 @@ namespace mlx
#ifdef MLX_COMPILER_MSVC
void* ptr2 = _aligned_realloc(ptr, alignment, size);
if(it != s_blocks.end())
s_blocks.erase(it);
#else
void* ptr2 = AlignedMalloc(alignment, size);
if(it != s_blocks.end())
{
std::memcpy(ptr2, ptr, it->size);
Free(ptr);
}
#endif
if(it != s_blocks.end())
s_blocks.erase(it);
if(ptr2 != nullptr)
s_blocks.emplace_back(ptr, size, true);
return ptr2;

View File

@@ -59,6 +59,8 @@ namespace mlx
flags |= SDL_WINDOW_SHOWN;
if(info->is_resizable)
flags |= SDL_WINDOW_RESIZABLE;
if(info->is_fullscreen)
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
infos->window = SDL_CreateWindow(info->title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, info->width, info->height, flags);
if(!infos->window)
@@ -148,6 +150,31 @@ namespace mlx
SDL_SetWindowFullscreen(static_cast<Internal::WindowInfos*>(window)->window, (enable ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0));
}
void SDLManager::SetWindowMaxSize(Handle window, int x, int y) const noexcept
{
SDL_SetWindowMaximumSize(static_cast<Internal::WindowInfos*>(window)->window, x, y);
}
void SDLManager::SetWindowMinSize(Handle window, int x, int y) const noexcept
{
SDL_SetWindowMinimumSize(static_cast<Internal::WindowInfos*>(window)->window, x, y);
}
void SDLManager::MaximizeWindow(Handle window) const noexcept
{
SDL_MaximizeWindow(static_cast<Internal::WindowInfos*>(window)->window);
}
void SDLManager::MinimizeWindow(Handle window) const noexcept
{
SDL_MinimizeWindow(static_cast<Internal::WindowInfos*>(window)->window);
}
void SDLManager::RestoreWindow(Handle window) const noexcept
{
SDL_RestoreWindow(static_cast<Internal::WindowInfos*>(window)->window);
}
void SDLManager::GetWindowPosition(Handle window, int* x, int* y) const noexcept
{
SDL_GetWindowPosition(static_cast<Internal::WindowInfos*>(window)->window, x, y);

View File

@@ -17,24 +17,43 @@ namespace mlx
else
FatalError("a renderer was created without window nor render target attached (wtf)");
#ifdef DEBUG
auto res = m_textures.try_emplace(draw_layer, CPUBuffer{}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_put_pixel_layer_" + std::to_string(draw_layer));
#else
auto res = m_textures.try_emplace(draw_layer, CPUBuffer{}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, std::string_view{});
#endif
if(res.second)
res.first->second.Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
res.first->second.SetPixel(x, y, color);
return (res.second ? &res.first->second : nullptr);
auto it = m_placements.find(draw_layer);
if(it != m_placements.end())
{
it->second->SetPixel(x, y, color);
return nullptr;
}
bool adjusment = false;
if(m_current_texture_index >= m_textures.size())
{
#ifdef DEBUG
m_textures.push_back(std::make_unique<Texture>(CPUBuffer{}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_put_pixel_layer_" + std::to_string(draw_layer)));
#else
m_textures.push_back(std::make_unique<Texture>(CPUBuffer{}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, std::string_view{}));
#endif
m_current_texture_index++;
adjusment = true;
}
try
{
m_placements[draw_layer] = m_textures.at(m_current_texture_index - adjusment).get();
m_textures.at(m_current_texture_index - adjusment)->Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
m_textures.at(m_current_texture_index - adjusment)->SetPixel(x, y, color);
return m_textures.at(m_current_texture_index - adjusment).get();
}
catch(...)
{
Error("PutPixelManager: invalid texture index; % is not in range of 0-% (internal mlx issue, please report to devs)", m_current_texture_index - 1, m_textures.size());
return nullptr;
}
}
void PutPixelManager::ResetRenderData()
{
m_textures.clear();
}
PutPixelManager::~PutPixelManager()
{
ResetRenderData();
m_placements.clear();
for(auto& texture : m_textures)
texture->Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
m_current_texture_index = 0;
}
}

View File

@@ -27,16 +27,16 @@ namespace mlx
return *sprite;
}
NonOwningPtr<Sprite> Scene::GetSpriteFromTexturePositionScaleRotation(NonOwningPtr<Texture> texture, const Vec2f& position, float scale, float rotation) const
NonOwningPtr<Sprite> Scene::GetSpriteFromTexturePositionScaleRotation(NonOwningPtr<Texture> texture, const Vec2f& position, float scale_x, float scale_y, float rotation) const
{
MLX_PROFILE_FUNCTION();
auto it = std::find_if(m_drawables.begin(), m_drawables.end(), [&texture, &position, scale, rotation](std::shared_ptr<Drawable> drawable)
auto it = std::find_if(m_drawables.begin(), m_drawables.end(), [&texture, &position, scale_x, scale_y, rotation](std::shared_ptr<Drawable> drawable)
{
if(!drawable || drawable->GetType() != DrawableType::Sprite)
return false;
return static_cast<Sprite*>(drawable.get())->GetTexture() == texture &&
drawable->GetPosition() == position &&
drawable->GetScale() == Vec2f{ scale, scale } &&
drawable->GetScale() == Vec2f{ scale_x, scale_y } &&
drawable->GetRotation().ToEulerAngles() == EulerAnglesf{ 0.0f, 0.0f, rotation };
});
return static_cast<Sprite*>(it != m_drawables.end() ? it->get() : nullptr);

View File

@@ -32,7 +32,10 @@ namespace mlx
0,
};
std::shared_ptr<Mesh> mesh = std::make_shared<Mesh>();
std::shared_ptr<Mesh> mesh = MeshRegistry::Get().FindMesh({ Mesh::SubMesh{ data, indices, Mesh::SubMesh::NoBuild{} } });
if(mesh)
return mesh;
mesh = std::make_shared<Mesh>();
mesh->AddSubMesh({ std::move(data), std::move(indices) });
return mesh;
}
@@ -42,6 +45,7 @@ namespace mlx
MLX_PROFILE_FUNCTION();
Verify((bool)texture, "Sprite: invalid texture (internal mlx issue, please report to devs)");
p_mesh = CreateQuad(0, 0, texture->GetWidth(), texture->GetHeight());
MeshRegistry::Get().RegisterMesh(p_mesh);
p_texture = texture;
}

View File

@@ -40,8 +40,13 @@ namespace mlx
index_data.emplace_back(index + 0);
}
std::shared_ptr<Mesh> mesh = std::make_shared<Mesh>();
mesh->AddSubMesh({ std::move(vertex_data), std::move(index_data) });
std::shared_ptr<Mesh> mesh = MeshRegistry::Get().FindMesh({ Mesh::SubMesh{ vertex_data, index_data, Mesh::SubMesh::NoBuild{} } });
if(!mesh)
{
mesh = std::make_shared<Mesh>();
mesh->AddSubMesh({ std::move(vertex_data), std::move(index_data) });
MeshRegistry::Get().RegisterMesh(mesh);
}
Init(text, font, mesh);
}

View File

@@ -79,7 +79,7 @@ namespace mlx
m_surface = p_window->CreateVulkanSurface(RenderCore::Get().GetInstance());
DebugLog("Vulkan: surface created");
m_swapchain = kvfCreateSwapchainKHR(RenderCore::Get().GetDevice(), RenderCore::Get().GetPhysicalDevice(), m_surface, extent, VK_NULL_HANDLE, false);
m_swapchain = kvfCreateSwapchainKHR(RenderCore::Get().GetDevice(), RenderCore::Get().GetPhysicalDevice(), m_surface, extent, m_swapchain, false);
m_images_count = kvfGetSwapchainImagesCount(m_swapchain);
m_min_images_count = kvfGetSwapchainMinImagesCount(m_swapchain);