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

@@ -12,11 +12,16 @@ namespace mlx
public:
struct SubMesh
{
struct NoBuild {};
VertexBuffer vbo;
IndexBuffer ibo;
std::vector<Vertex> vertex_data;
std::vector<std::uint32_t> index_data;
std::size_t triangle_count = 0;
inline SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices);
inline SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices, NoBuild);
};
public:
@@ -35,6 +40,27 @@ namespace mlx
private:
std::vector<SubMesh> m_sub_meshes;
};
// A registry just to avoid destroying meshes when clearing a window
class MeshRegistry
{
public:
inline MeshRegistry();
inline void RegisterMesh(std::shared_ptr<Mesh> mesh);
inline std::shared_ptr<Mesh> FindMesh(const std::vector<Mesh::SubMesh>& sub_meshes);
inline void UnregisterMesh(std::shared_ptr<Mesh> mesh);
inline void Reset();
inline static bool IsInit() noexcept { return s_instance != nullptr; }
inline static MeshRegistry& Get() noexcept { return *s_instance; }
inline ~MeshRegistry();
private:
inline static MeshRegistry* s_instance = nullptr;
std::unordered_set<std::shared_ptr<Mesh>> m_meshes_registry;
};
}
#include <Graphics/Mesh.inl>

View File

@@ -3,7 +3,7 @@
namespace mlx
{
Mesh::SubMesh::SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices)
Mesh::SubMesh::SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices) : vertex_data(vertices), index_data(indices)
{
CPUBuffer vb(vertices.size() * sizeof(Vertex));
std::memcpy(vb.GetData(), vertices.data(), vb.GetSize());
@@ -17,4 +17,61 @@ namespace mlx
triangle_count = vertices.size() / 3;
}
Mesh::SubMesh::SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices, NoBuild) : vertex_data(vertices), index_data(indices) {}
MeshRegistry::MeshRegistry()
{
s_instance = this;
}
void MeshRegistry::RegisterMesh(std::shared_ptr<Mesh> mesh)
{
m_meshes_registry.insert(mesh);
}
std::shared_ptr<Mesh> MeshRegistry::FindMesh(const std::vector<Mesh::SubMesh>& sub_meshes)
{
for(const std::shared_ptr<Mesh>& mesh : m_meshes_registry)
{
if(mesh->GetSubMeshCount() != sub_meshes.size()) // If the number of submeshes is different than the one we want to find no need to test
continue;
bool found = true;
for(std::size_t i = 0; i < sub_meshes.size(); i++)
{
try
{
const Mesh::SubMesh& registered_sub_mesh = mesh->GetSubMesh(i);
if(registered_sub_mesh.vertex_data != sub_meshes[i].vertex_data || registered_sub_mesh.index_data != sub_meshes[i].index_data)
{
found = false;
break;
}
}
catch(...)
{
found = false;
break;
}
}
if(found)
return mesh;
}
return nullptr;
}
void MeshRegistry::UnregisterMesh(std::shared_ptr<Mesh> mesh)
{
m_meshes_registry.erase(mesh);
}
void MeshRegistry::Reset()
{
m_meshes_registry.clear();
}
MeshRegistry::~MeshRegistry()
{
s_instance = nullptr;
}
}

View File

@@ -10,15 +10,17 @@ namespace mlx
public:
PutPixelManager(NonOwningPtr<class Renderer> renderer) : p_renderer(renderer) {}
// Return a valid pointer when a new texture has been created
// Returns a valid pointer when a new texture has been created
NonOwningPtr<Texture> DrawPixel(int x, int y, std::uint64_t draw_layer, int color);
void ResetRenderData();
~PutPixelManager();
~PutPixelManager() = default;
private:
std::unordered_map<std::uint64_t, Texture> m_textures;
std::unordered_map<std::uint64_t, NonOwningPtr<Texture>> m_placements;
std::vector<std::unique_ptr<Texture>> m_textures;
NonOwningPtr<class Renderer> p_renderer;
std::size_t m_current_texture_index = 0;
};
}

View File

@@ -17,7 +17,7 @@ namespace mlx
Scene() = default;
Sprite& CreateSprite(NonOwningPtr<class Texture> texture) noexcept;
NonOwningPtr<Sprite> GetSpriteFromTexturePositionScaleRotation(NonOwningPtr<Texture> texture, const Vec2f& position, float scale, float rotation) const;
NonOwningPtr<Sprite> GetSpriteFromTexturePositionScaleRotation(NonOwningPtr<Texture> texture, const Vec2f& position, float scale_x, float scale_y, float rotation) const;
void TryEraseSpriteFromTexture(NonOwningPtr<Texture> texture);
bool IsTextureAtGivenDrawLayer(NonOwningPtr<Texture> texture, std::uint64_t draw_layer) const;