[WIP] Move to shared_ptr.

This commit is contained in:
Anish Bhobe 2025-04-06 19:31:12 +02:00
parent 98660a11fa
commit 1bee73e46f
9 changed files with 24 additions and 68 deletions

View File

@ -65,6 +65,11 @@ struct Buffer
} }
void Destroy(); void Destroy();
~Buffer()
{
Destroy();
}
void Write(usize offset, usize size, const void *data); void Write(usize offset, usize size, const void *data);
void Allocate(const Device *device, usize size, vk::BufferUsageFlags bufferUsage, void Allocate(const Device *device, usize size, vk::BufferUsageFlags bufferUsage,

View File

@ -222,4 +222,4 @@ struct fmt::formatter<eastl::fixed_string<TType, TCount, TOverflow>> : nested_fo
}; };
template <concepts::Manageable T> template <concepts::Manageable T>
using Ref = eastl::intrusive_ptr<T>; using Ref = std::shared_ptr<T>;

View File

@ -106,6 +106,10 @@ struct Image
} }
void Destroy(); void Destroy();
~Image()
{
Destroy();
}
static bool static bool
Conforms(const Image &) Conforms(const Image &)

View File

@ -56,7 +56,7 @@ struct View
[[nodiscard]] bool [[nodiscard]] bool
IsValid() const IsValid() const
{ {
return m_Image; return Cast<bool>(m_Image);
} }
void void
@ -67,6 +67,11 @@ struct View
m_Image->DestroyView(Take(m_View)); m_Image->DestroyView(Take(m_View));
} }
~View()
{
Destroy();
}
}; };
struct ImageView : View<Image> struct ImageView : View<Image>

View File

@ -82,8 +82,6 @@ class CommitManager
}; };
}; };
static_assert(sizeof(Entry) == 24);
eastl::vector<Entry> m_Data; eastl::vector<Entry> m_Data;
FreeList<Entry> m_FreeList; FreeList<Entry> m_FreeList;
eastl::intrusive_hash_map<typename Entry::key_type, Entry, 31, typename Entry::Hash> m_InUse; eastl::intrusive_hash_map<typename Entry::key_type, Entry, 31, typename Entry::Hash> m_InUse;

View File

@ -18,7 +18,7 @@ static Ref<TTo>
CastImage(const concepts::ImageRef auto &from) CastImage(const concepts::ImageRef auto &from)
{ {
assert(TTo::Conforms(*from.get())); assert(TTo::Conforms(*from.get()));
return Recast<TTo *>(from.get()); return std::reinterpret_pointer_cast<TTo>(from);
} }
struct Texture2DCreateInfo struct Texture2DCreateInfo

View File

@ -27,66 +27,19 @@ class Manager
/** /**
* Constructor for the Manager class template. * Constructor for the Manager class template.
* @param device Device with which resources are created. * @param device Device with which resources are created.
* @param maxCount Max number of resources that can be created (maxCount <= Handle::INDEX_MASK)
*/ */
explicit Manager(const Device *device, const u32 maxCount) explicit Manager(const Device *device, const u32)
: m_Data{maxCount} : m_Device{device}
, m_Device{device}
{ {
for (auto &element : m_Data)
{
m_FreeList.Push(element);
}
} }
virtual ~Manager() virtual ~Manager()
{ {
if constexpr (concepts::DeviceDestructible<Type>)
{
for (auto &element : m_Data)
{
element.Destroy(m_Device);
}
}
m_Device = nullptr; m_Device = nullptr;
} }
// TODO: Work on deletion!!
void
Sweep()
requires concepts::DeviceDestructible<Type>
{
for (i64 i = m_Data.size() - 1; i >= 0; --i)
{
if (auto *pIter = &m_Data[i]; !pIter->IsReferenced())
{
pIter->Destroy(m_Device);
m_FreeList.Push(*pIter);
}
}
}
void
Sweep()
requires concepts::SelfDestructible<Type>
{
for (i64 i = m_Data.size() - 1; i >= 0; --i)
{
if (auto *pIter = &m_Data[i]; !pIter->IsValid())
{
pIter->Destroy();
m_FreeList.Push(*pIter);
}
}
}
PIN_MEMORY(Manager); PIN_MEMORY(Manager);
private:
eastl::vector<Type> m_Data; // Data also keeps the freelist during 'not use'.
FreeList<Type> m_FreeList;
protected: protected:
const Device *m_Device; const Device *m_Device;
@ -97,15 +50,9 @@ class Manager
[[nodiscard]] Handle [[nodiscard]] Handle
Alloc() Alloc()
{ {
ERROR_IF(m_FreeList.Empty(), "Max buffers allocated.") THEN_ABORT(-1); auto object = std::make_shared<Type>();
object->m_Device = m_Device;
Type &pAlloc = m_FreeList.Pop(); return object;
memset(&pAlloc, 0, sizeof pAlloc);
if constexpr (concepts::SelfDestructible<Type>)
{
pAlloc.m_Device = m_Device;
}
return {&pAlloc};
} }
}; };
} // namespace systems } // namespace systems

View File

@ -138,10 +138,7 @@ class ResourceManager
void void
Update() Update()
{ {
m_Views.Sweep(); // TODO: Remove
m_Images.Sweep();
m_Buffers.Sweep();
m_Samplers.Sweep();
} }
~ResourceManager() = default; ~ResourceManager() = default;

View File

@ -20,7 +20,7 @@ static Ref<TTo>
CastView(const concepts::ImageViewRef auto &from) CastView(const concepts::ImageViewRef auto &from)
{ {
assert(TTo::ImageType::Conforms(*from->m_Image.get())); assert(TTo::ImageType::Conforms(*from->m_Image.get()));
return Recast<TTo *>(from.get()); return std::reinterpret_pointer_cast<TTo>(from);
} }
template <concepts::Image TImage = Image> template <concepts::Image TImage = Image>