Foundation
Loading...
Searching...
No Matches
Details.hpp
Go to the documentation of this file.
1#pragma once
2#include <Core/Core.hpp>
3#include <Core/Pool.hpp>
4
8namespace Foundation::RHI {
9 using Handle = uint64_t;
10 constexpr static Handle kInvalidHandle = static_cast<Handle>(-1);
16 class RHIObject {
17 public:
18 RHIObject() = default;
19 RHIObject(RHIObject const&) = delete;
20 RHIObject& operator=(const RHIObject&) = delete;
21 RHIObject(RHIObject&&) = delete;
23
24 virtual ~RHIObject() = default;
25 };
29 template<typename Factory, std::derived_from<RHIObject> T>
40 template<typename Factory, typename T> class RHIHandle {
41 public:
43 Factory* mFactory{ nullptr };
50 template<typename U = T> [[nodiscard]] U* Get() const {
51 CHECK(IsValid() && "RHIHandle::Get called on an invalid handle");
53 CHECK(ptr != nullptr);
54 return static_cast<U*>(ptr);
55 }
56 T* operator->() const {
57 return Get();
58 }
59
60 constexpr Handle operator()() const { return mHandle; }
61 constexpr operator bool() const noexcept { return IsValid(); }
62 bool operator==(const RHIHandle& other) const { return mFactory == other.mFactory && mHandle == other.mHandle; }
63
69 [[nodiscard]] bool IsValid() const { return mFactory != nullptr && mHandle != kInvalidHandle; }
74 void Invalidate() { mFactory = nullptr, mHandle = kInvalidHandle; }
75 };
79 template<typename Factory, typename T> class RHIScopedHandle : public RHIHandle<Factory, T> {
80 public:
83 using RHIHandle<Factory, T>::Get;
86
87 RHIScopedHandle() = default;
90 : RHIHandle<Factory, T>(std::move(other)) {
91 other.Invalidate();
92 }
94 if (this != &other) {
95 mFactory = other.mFactory;
96 mHandle = other.mHandle;
97 other.Invalidate();
98 }
99 return *this;
100 }
105 return *this;
106 }
112 RHIHandle<Factory, T> handle = *this;
113 Invalidate();
114 return handle;
115 }
119 void Reset() {
120 if (IsValid()) {
122 Invalidate();
123 }
124 }
131 };
135 template<typename Base = RHIObject> class RHIObjectPool {
138 public:
146 template<typename U, typename ...Args> Handle CreateObject(Args&&... args) {
147#ifdef _MSC_VER
148 // Motherfucker.
149 // https://stackoverflow.com/questions/77144003/use-of-template-keyword-before-dependent-template-name
150 return mObjects.PopBase<U>(std::forward<Args>(args)...);
151#else
152 return mObjects.template PopBase<U>(std::forward<Args>(args)...);
153#endif
154 }
160 template<typename U = Base> U* GetObjectPtr(Handle handle) const {
161 if (!mObjects.Contains(handle))
162 throw std::out_of_range("invalid handle");
163 return static_cast<U*>(mObjects.At(handle));
164 }
168 void DestroyObject(Handle handle) {
169 mObjects.Free(handle);
170 }
171 };
172}
173
174
175namespace Foundation::RHI {
176 class RHIApplication;
179
180 class RHIDevice;
182 template<typename T> using RHIDeviceObjectHandle = RHIHandle<RHIDevice, T>;
183}
#define CHECK(expr)
Definition Logging.hpp:26
General Purpose Allocator (GPA) interface.
Definition Allocator.hpp:24
Definition Device.hpp:131
Handle type for RHI Objects.
Definition Details.hpp:40
bool IsValid() const
Check if the handle is valid (i.e. associated with a Factory and not kInvalidHandle)
Definition Details.hpp:69
constexpr Handle operator()() const
Definition Details.hpp:60
bool operator==(const RHIHandle &other) const
Definition Details.hpp:62
U * Get() const
Retrieves the underlying RHIObject pointer. It is undefined behavior to use the returned pointer afte...
Definition Details.hpp:50
T * operator->() const
Definition Details.hpp:56
void Invalidate()
Resets the handle to an invalid state. After calling this, the handle is no longer associated with an...
Definition Details.hpp:74
Handle mHandle
Definition Details.hpp:44
Factory * mFactory
Definition Details.hpp:43
Storage/Object dereference facility for RHI Objects.
Definition Details.hpp:135
RHIObjectPool(Core::Allocator *allocator)
Definition Details.hpp:139
Core::Pool< Handle, Base > mObjects
Definition Details.hpp:137
U * GetObjectPtr(Handle handle) const
Retrieves the raw pointer to the object within the storage.
Definition Details.hpp:160
Core::Allocator * mAllocator
Definition Details.hpp:136
Handle CreateObject(Args &&... args)
Creates specified RHIObject of derived type T and retrieves its handle.
Definition Details.hpp:146
void DestroyObject(Handle handle)
Destroys the object associated with the given handle, and frees the handle for reuse.
Definition Details.hpp:168
Base class for all RHI objects.
Definition Details.hpp:16
RHIObject & operator=(const RHIObject &)=delete
RHIObject(RHIObject const &)=delete
virtual ~RHIObject()=default
RHIObject & operator=(RHIObject &&)=delete
RHIObject(RHIObject &&)=delete
Scoped move-only RAII handle wrapper for RHI Objects.
Definition Details.hpp:79
RHIScopedHandle & operator=(RHIScopedHandle &&other) noexcept
Definition Details.hpp:93
RHIScopedHandle(const RHIScopedHandle &)=delete
RHIScopedHandle(RHIScopedHandle &&other) noexcept
Definition Details.hpp:89
void Reset()
Destructs the underlying RHIObject, and invalidates the scoped handle.
Definition Details.hpp:119
RHIScopedHandle(Factory *factory, Handle handle)
Definition Details.hpp:88
~RHIScopedHandle()
Definition Details.hpp:127
RHIScopedHandle & operator=(const RHIScopedHandle &)=delete
RHIHandle< Factory, T > Release()
Releases the underlying RHIHandle, invalidating the scoped handle. NOTE: This may leak the resource i...
Definition Details.hpp:111
RHIHandle< Factory, T > View() const
Returns a non-owning view of the underlying RHIHandle.
Definition Details.hpp:104
std::unique_ptr< T, StlDeleter< T > > UniquePtr
std::unique_ptr with custom deleter that uses a Foundation::Core::Allocator to deallocate memory.
Definition Allocator.hpp:161
Low-level Rendering Hardware Interface (RHI) abstractions.
Definition Application.hpp:5
uint64_t Handle
Definition Details.hpp:9
RHIScopedHandle< RHIApplication, T > RHIApplicationScopedObjectHandle
Definition Details.hpp:177
RHIScopedHandle< RHIDevice, T > RHIDeviceScopedObjectHandle
Definition Details.hpp:181
static constexpr Handle kInvalidHandle
Definition Details.hpp:10
Provides type traits for types derived from RHIObject.
Definition Details.hpp:30