Foundation
Loading...
Searching...
No Matches
Details.hpp
Go to the documentation of this file.
1#pragma once
2#include <Core/AtomicPool.hpp>
6namespace Foundation::RHI
7{
9 constexpr static Handle kInvalidHandle = static_cast<Handle>(-1);
16 {
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>
42 {
43 public:
45 Factory* mFactory{nullptr};
52 template <typename U = T>
53 [[nodiscard]] U* Get() const
54 {
55 if (!IsValid())
56 throw std::runtime_error("RHIHandle::Get called on an invalid handle");
58 if (!ptr)
59 throw std::runtime_error("RHIHandle::Get got nullptr");
60 return static_cast<U*>(ptr);
61 }
62 T* operator->() const { return Get(); }
63
64 constexpr Handle operator()() const { return mHandle; }
65 constexpr operator bool() const noexcept { return IsValid(); }
66 constexpr T* operator*() noexcept { return Get(); }
67 bool operator==(const RHIHandle& other) const { return mFactory == other.mFactory && mHandle == other.mHandle; }
68
74 [[nodiscard]] bool IsValid() const { return mFactory != nullptr && mHandle != kInvalidHandle; }
79 void Invalidate() { mFactory = nullptr, mHandle = kInvalidHandle; }
80 };
84 template <typename Factory, typename T>
85 class RHIScopedHandle : public RHIHandle<Factory, T>
86 {
87 public:
90 using RHIHandle<Factory, T>::Get;
93
94 RHIScopedHandle() = default;
97 {
98 other.Invalidate();
99 }
101 {
102 if (this != &other)
103 {
104 mFactory = other.mFactory;
105 mHandle = other.mHandle;
106 other.Invalidate();
107 }
108 return *this;
109 }
113 [[nodiscard]] RHIHandle<Factory, T> View() const { return *this; }
119 {
120 RHIHandle<Factory, T> handle = *this;
121 Invalidate();
122 return handle;
123 }
127 void Reset()
128 {
129 if (IsValid())
130 {
132 Invalidate();
133 }
134 }
142 };
143 static constexpr size_t kRHIObjectPoolMaxSize = 65536;
147 template <typename Base = RHIObject>
149 {
151
154
155 public:
161 template <typename U, typename... Args>
163 {
164 Base* obj = Core::ConstructBase<Base, U>(mAllocator, std::forward<Args>(args)...);
166 return reinterpret_cast<Handle>(pointer);
167 }
173 template <typename U = Base>
174 static U* GetObjectPtr(Handle handle) noexcept
175 {
176 PointerType& pointer = *reinterpret_cast<PointerType*>(handle);
177 Base* obj = pointer.get();
178 return reinterpret_cast<U*>(obj);
179 }
184 {
185 auto* pointer = reinterpret_cast<PointerType*>(handle);
187 }
191 void Destroy()
192 {
193 mPool.Collect();
194 }
195 };
196} // namespace Foundation::RHI
197
198
199namespace Foundation::RHI
200{
201 class RHIApplication;
202 template <typename T>
204 template <typename T>
206
207 class RHIDevice;
208 template <typename T>
210 template <typename T>
212} // namespace Foundation::RHI
General Purpose Allocator (GPA) interface.
Definition Allocator.hpp:24
Atomic, bounded object pool of fixed allocation sizes. Being a sibling to AtomicStack - key differenc...
Definition AtomicPool.hpp:18
void Destruct(T *ptr)
Destructs the object pointed to by ptr and returns it to the pool.
Definition AtomicPool.hpp:99
T * Construct(Args &&... args)
Constructs an object of type T in the pool with the given arguments.
Definition AtomicPool.hpp:87
void Collect()
Destruct all allocated objects in the pool, collecting garbage.
Definition AtomicPool.hpp:132
Definition Device.hpp:188
Handle type for RHI Objects.
Definition Details.hpp:42
bool IsValid() const
Check if the handle is valid (i.e. associated with a Factory and not kInvalidHandle)
Definition Details.hpp:74
constexpr Handle operator()() const
Definition Details.hpp:64
bool operator==(const RHIHandle &other) const
Definition Details.hpp:67
U * Get() const
Retrieves the underlying RHIObject pointer. It is undefined behavior to use the returned pointer afte...
Definition Details.hpp:53
T * operator->() const
Definition Details.hpp:62
void Invalidate()
Resets the handle to an invalid state. After calling this, the handle is no longer associated with an...
Definition Details.hpp:79
Handle mHandle
Definition Details.hpp:46
Factory * mFactory
Definition Details.hpp:45
constexpr T * operator*() noexcept
Definition Details.hpp:66
Thread-safe type-erased handle dereference facility for RHI Objects.
Definition Details.hpp:149
Core::AtomicPool< PointerType > mPool
Definition Details.hpp:153
RHIObjectPool(Core::Allocator *allocator)
Definition Details.hpp:156
static U * GetObjectPtr(Handle handle) noexcept
Retrieves the raw pointer to the object within the storage.
Definition Details.hpp:174
Core::UniquePtr< Base > PointerType
Definition Details.hpp:150
Core::Allocator * mAllocator
Definition Details.hpp:152
void Destroy()
Destroys all objects in the pool.
Definition Details.hpp:191
Handle CreateObject(Args &&... args)
Creates specified RHIObject of derived type T and retrieves its handle.
Definition Details.hpp:162
void DestroyObject(Handle handle)
Destroys the object associated with the given handle, and frees the handle for reuse.
Definition Details.hpp:183
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:86
RHIScopedHandle & operator=(RHIScopedHandle &&other) noexcept
Definition Details.hpp:100
RHIScopedHandle(const RHIScopedHandle &)=delete
RHIScopedHandle(RHIScopedHandle &&other) noexcept
Definition Details.hpp:96
void Reset()
Destructs the underlying RHIObject, and invalidates the scoped handle.
Definition Details.hpp:127
RHIScopedHandle(Factory *factory, Handle handle)
Definition Details.hpp:95
~RHIScopedHandle()
Definition Details.hpp:137
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:118
RHIHandle< Factory, T > View() const
Returns a non-owning view of the underlying RHIHandle.
Definition Details.hpp:113
void * pointer
Definition Allocator.hpp:7
std::unique_ptr< T, Deleter > UniquePtr
std::unique_ptr with custom deleter that uses a Foundation::Core::Allocator to deallocate memory.
Definition Allocator.hpp:166
T * Construct(Allocator *resource, Args &&...args)
Convenience placement new with object of type T using a Foundation::Core::Allocator.
Definition Allocator.hpp:149
Low-level Rendering Hardware Interface (RHI) abstractions.
Definition Application.hpp:4
uintptr_t Handle
Definition Details.hpp:8
static constexpr size_t kRHIObjectPoolMaxSize
Definition Details.hpp:143
static constexpr Handle kInvalidHandle
Definition Details.hpp:9
Custom deleter for Foundation::Core::UniquePtr and Foundation::Core::SharedPtr that uses a Foundation...
Definition Allocator.hpp:117
Provides type traits for types derived from RHIObject.
Definition Details.hpp:30