Foundation
Loading...
Searching...
No Matches
Device.hpp
Go to the documentation of this file.
1#pragma once
2#include "PipelineState.hpp"
3#include "Shader.hpp"
4#include "Swapchain.hpp"
5#include "Command.hpp"
6#include "Resource.hpp"
7#include "Descriptor.hpp"
8namespace Foundation::RHI {
9 class RHIApplication;
10 class RHIDevice;
11 class RHIDeviceSemaphore;
12 class RHIDeviceFence;
13 class RHIDeviceQueue : public RHIObject {
14 protected:
16 public:
18
19 virtual void WaitIdle() const = 0;
21 struct SubmitDesc {
22 // Semaphore(s) and the minimum values to wait on
24 // Semaphore(s) and the values to signal
26 // Binary semaphore(s) to wait when the command lists are done
28 // Stages the semaphore waits occur in [timeline_waits..., waits...]
30 // Binary semaphore(s) to signal when the command lists are done
32 // Command lists to submit
34 };
35 virtual void Submit(Span<const SubmitDesc> desc, RHIDeviceFence* completionFence = nullptr) const = 0;
36
43 virtual void Present(PresentDesc const& desc) const = 0;
44 [[nodiscard]] virtual uint32_t GetQueueIndex() const = 0;
45 virtual void DebugSetObjectName(const char* name) = 0;
46 };
47 // https://docs.vulkan.org/samples/latest/samples/extensions/timeline_semaphore/README.html
49 protected:
51 public:
52 const bool mIsTimeline;
54
55 virtual void DebugSetObjectName(const char* name) = 0;
56 };
57 class RHIDeviceFence : public RHIObject {
58 protected:
60 public:
62
63 virtual void DebugSetObjectName(const char* name) = 0;
64 };
66 struct Binding {
67 uint32_t count{ 1 }; // Array size for array access
68 RHIShaderStage stage{ RHIShaderStageBits::All }; // Stage this binding is used in
69 RHIDescriptorType type; // Type of this binding
70 };
71 // Bindings that make up this layout
73 // Allow updating descriptors after being bound to a command buffer when
74 // they are not used
75 bool updateAfterBind{ false };
76 };
78 protected:
80 public:
85
86 virtual void DebugSetObjectName(const char* name) = 0;
87 };
88 class RHIDeviceSampler : public RHIObject {
89 protected:
91 public:
92 struct SamplerDesc {
93 struct Anisotropy {
94 bool enable{ false }; // Enable anisotropic filtering
95 float maxLevel{ 16.0f }; // Max anisotropy level
97 struct AddressMode {
98 enum Mode {
104 } u{ Repeat }, v{ Repeat }, w{ Repeat }; // Address modes for U, V, W coordinates
106 struct Mipmap {
109 Nearest
110 } mipmapMode{ Linear }; // Mipmap mode;
111 float bias{ 0.0f }; // Mipmap LOD bias
113 struct Filter {
114 enum Type {
117 Cubic
118 } minFilter{ Linear }, magFilter{ Linear }; // Minification and magnification filters
120 struct LOD {
121 float min{ 0.0f }; // Minimum level of detail
122 float max{ 16.0f }; // Maximum level of detail
123 } lod; // Level of detail settings;
124 } const mDesc;
126 : mDevice(device), mDesc(desc) {
127 }
128
129 virtual void DebugSetObjectName(const char* name) = 0;
130 };
131 class RHIDevice : public RHIObject {
132 protected:
134 public:
135 struct DeviceDesc {
137 const char* name;
138 };
140
144 [[nodiscard]] virtual RHISwapchain* GetSwapchain(Handle handle) const = 0;
145 virtual void DestroySwapchain(Handle handle) = 0;
146
148 [[nodiscard]] virtual RHIPipelineState* GetPipelineState(Handle handle) const = 0;
149 virtual void DestroyPipelineState(Handle handle) = 0;
150
152 [[nodiscard]] virtual RHIShaderModule* GetShaderModule(Handle handle) const = 0;
153 virtual void DestroyShaderModule(Handle handle) = 0;
154
156 [[nodiscard]] virtual RHICommandPool* GetCommandPool(Handle handle) const = 0;
157 virtual void DestroyCommandPool(Handle handle) = 0;
158
160
162 [[nodiscard]] virtual RHIDeviceSemaphore* GetSemaphore(Handle handle) const = 0;
163 virtual void DestroySemaphore(Handle handle) = 0;
164
166 [[nodiscard]] virtual RHIDeviceFence* GetFence(Handle handle) const = 0;
167 virtual void DestroyFence(Handle handle) = 0;
168
170 [[nodiscard]] virtual RHIBuffer* GetBuffer(Handle handle) const = 0;
171 virtual void DestroyBuffer(Handle handle) = 0;
172
174 [[nodiscard]] virtual RHITexture* GetImage(Handle handle) const = 0;
175 virtual void DestroyImage(Handle handle) = 0;
176
179 virtual void DestroyDescriptorSetLayout(Handle handle) = 0;
180
182 RHIDeviceDescriptorPool::PoolDesc const& desc) = 0;
184 virtual void DestroyDescriptorPool(Handle handle) = 0;
185
187 RHIDeviceSampler::SamplerDesc const& desc) = 0;
188 [[nodiscard]] virtual RHIDeviceSampler* GetSampler(Handle handle) const = 0;
189 virtual void DestroySampler(Handle handle) = 0;
190
193
196
197 virtual void WaitIdle() const = 0;
198
199 virtual void DebugSetObjectName(const char* name) = 0;
200 };
205 {
206 RHIDevice const* mDevice { nullptr };
209 void WaitIdle() const
210 {
211 if (mDevice) mDevice->WaitIdle();
212 }
216
217 };
218
220 static RHISwapchain* Get(RHIDevice const* device, Handle handle) {
221 return device->GetSwapchain(handle);
222 }
223 static void Destroy(RHIDevice* device, Handle handle) {
224 device->DestroySwapchain(handle);
225 }
226 };
228 static RHIPipelineState* Get(RHIDevice const* device, Handle handle) {
229 return device->GetPipelineState(handle);
230 }
231 static void Destroy(RHIDevice* device, Handle handle) {
232 device->DestroyPipelineState(handle);
233 }
234 };
236 static RHIShaderModule* Get(RHIDevice const* device, Handle handle) {
237 return device->GetShaderModule(handle);
238 }
239 static void Destroy(RHIDevice* device, Handle handle) {
240 device->DestroyShaderModule(handle);
241 }
242 };
244 static RHICommandPool* Get(RHIDevice const* device, Handle handle) {
245 return device->GetCommandPool(handle);
246 }
247 static void Destroy(RHIDevice* device, Handle handle) {
248 device->DestroyCommandPool(handle);
249 }
250 };
252 static RHIDeviceSemaphore* Get(RHIDevice const* device, Handle handle) {
253 return device->GetSemaphore(handle);
254 }
255 static void Destroy(RHIDevice* device, Handle handle) {
256 device->DestroySemaphore(handle);
257 }
258 };
260 static RHIDeviceFence* Get(RHIDevice const* device, Handle handle) {
261 return device->GetFence(handle);
262 }
263 static void Destroy(RHIDevice* device, Handle handle) {
264 device->DestroyFence(handle);
265 }
266 };
267 template<> struct RHIObjectTraits<RHIDevice, RHIBuffer> {
268 static RHIBuffer* Get(RHIDevice const* device, Handle handle) {
269 return device->GetBuffer(handle);
270 }
271 static void Destroy(RHIDevice* device, Handle handle) {
272 device->DestroyBuffer(handle);
273 }
274 };
276 static RHITexture* Get(RHIDevice const* device, Handle handle) {
277 return device->GetImage(handle);
278 }
279 static void Destroy(RHIDevice* device, Handle handle) {
280 device->DestroyImage(handle);
281 }
282 };
285 return device->GetDescriptorSetLayout(handle);
286 }
287 static void Destroy(RHIDevice* device, Handle handle) {
288 device->DestroyDescriptorSetLayout(handle);
289 }
290 };
293 return device->GetDescriptorPool(handle);
294 }
295 static void Destroy(RHIDevice* device, Handle handle) {
296 device->DestroyDescriptorPool(handle);
297 }
298 };
300 static RHIDeviceSampler* Get(RHIDevice const* device, Handle handle) {
301 return device->GetSampler(handle);
302 }
303 static void Destroy(RHIDevice* device, Handle handle) {
304 device->DestroySampler(handle);
305 }
306 };
307}
std::span with relaxed constructors for pointer-aliasing types and common containers.
Definition Container.hpp:59
The root object of everything RHI. Implementation of this class inherently defines the RHI backend.
Definition Application.hpp:11
Definition Resource.hpp:30
Definition Command.hpp:13
Definition Descriptor.hpp:43
const RHIDeviceDescriptorSetLayoutDesc mDesc
Definition Device.hpp:81
RHIDeviceDescriptorSetLayout(RHIDevice const &device, RHIDeviceDescriptorSetLayoutDesc const &desc)
Definition Device.hpp:82
const RHIDevice & mDevice
Definition Device.hpp:79
virtual void DebugSetObjectName(const char *name)=0
Definition Device.hpp:57
const RHIDevice & mDevice
Definition Device.hpp:59
RHIDeviceFence(RHIDevice const &device)
Definition Device.hpp:61
virtual void DebugSetObjectName(const char *name)=0
Definition Device.hpp:13
virtual uint32_t GetQueueIndex() const =0
RHIDeviceQueue(RHIDevice const &device)
Definition Device.hpp:17
virtual void Submit(Span< const SubmitDesc > desc, RHIDeviceFence *completionFence=nullptr) const =0
virtual void DebugSetObjectName(const char *name)=0
Pair< RHIDeviceSemaphore *, size_t > TimelinePair
Definition Device.hpp:20
virtual void WaitIdle() const =0
virtual void Present(PresentDesc const &desc) const =0
const RHIDevice & mDevice
Definition Device.hpp:15
Definition Device.hpp:88
struct Foundation::RHI::RHIDeviceSampler::SamplerDesc mDesc
const RHIDevice & mDevice
Definition Device.hpp:90
RHIDeviceSampler(RHIDevice const &device, SamplerDesc const &desc)
Definition Device.hpp:125
virtual void DebugSetObjectName(const char *name)=0
Definition Device.hpp:48
RHIDeviceSemaphore(RHIDevice const &device, bool is_timeline)
Definition Device.hpp:53
const RHIDevice & mDevice
Definition Device.hpp:50
const bool mIsTimeline
Definition Device.hpp:52
virtual void DebugSetObjectName(const char *name)=0
Definition Device.hpp:131
virtual RHIBuffer * GetBuffer(Handle handle) const =0
virtual RHIDeviceDescriptorPool * GetDescriptorPool(Handle handle) const =0
virtual void ResetFences(Span< const RHIDeviceObjectHandle< RHIDeviceFence > > fences)=0
virtual RHIDeviceScopedObjectHandle< RHICommandPool > CreateCommandPool(RHICommandPool::PoolDesc type)=0
virtual RHIDeviceSampler * GetSampler(Handle handle) const =0
virtual RHIPipelineState * GetPipelineState(Handle handle) const =0
virtual void WaitForFences(Span< const RHIDeviceObjectHandle< RHIDeviceFence > > fences, bool wait_all, size_t timeout)=0
virtual RHITexture * GetImage(Handle handle) const =0
virtual Span< RHISwapchainPresentMode const > GetSwapchainSupportedPresentModes() const =0
virtual RHIDeviceScopedObjectHandle< RHIDeviceSampler > CreateSampler(RHIDeviceSampler::SamplerDesc const &desc)=0
virtual RHIDeviceFence * GetFence(Handle handle) const =0
virtual void SignalTimelineSemaphores(Span< const Pair< RHIDeviceObjectHandle< RHIDeviceSemaphore >, size_t > > semaphores)=0
virtual RHISwapchain * GetSwapchain(Handle handle) const =0
virtual RHIDeviceScopedObjectHandle< RHIDeviceDescriptorSetLayout > CreateDescriptorSetLayout(RHIDeviceDescriptorSetLayoutDesc const &desc)=0
virtual void DestroyDescriptorPool(Handle handle)=0
virtual RHIDeviceScopedObjectHandle< RHIDeviceDescriptorPool > CreateDescriptorPool(RHIDeviceDescriptorPool::PoolDesc const &desc)=0
virtual void DestroyPipelineState(Handle handle)=0
const RHIApplication & mApp
Definition Device.hpp:133
virtual Span< RHIResourceFormat const > GetSwapchainSupportedFormats() const =0
virtual void DestroyShaderModule(Handle handle)=0
virtual RHIDeviceScopedObjectHandle< RHIPipelineState > CreatePipelineState(RHIPipelineState::PipelineStateDesc const &desc)=0
virtual RHIDeviceScopedObjectHandle< RHITexture > CreateTexture(RHITextureDesc const &desc)=0
virtual void DestroySemaphore(Handle handle)=0
virtual void DebugSetObjectName(const char *name)=0
virtual void DestroySampler(Handle handle)=0
virtual RHIDeviceScopedObjectHandle< RHIDeviceSemaphore > CreateSemaphore(bool is_timeline=false)=0
virtual void WaitIdle() const =0
virtual RHIDeviceSemaphore * GetSemaphore(Handle handle) const =0
virtual RHIShaderModule * GetShaderModule(Handle handle) const =0
virtual void DestroyImage(Handle handle)=0
virtual RHIDeviceScopedObjectHandle< RHIShaderModule > CreateShaderModule(RHIShaderModule::ShaderModuleDesc const &desc)=0
virtual RHIDeviceScopedObjectHandle< RHISwapchain > CreateSwapchain(RHISwapchain::SwapchainDesc const &desc)=0
virtual void DestroyBuffer(Handle handle)=0
virtual RHIDeviceScopedObjectHandle< RHIDeviceFence > CreateFence(bool signaled=true)=0
virtual void DestroyFence(Handle handle)=0
virtual RHICommandPool * GetCommandPool(Handle handle) const =0
virtual void DestroyDescriptorSetLayout(Handle handle)=0
virtual void DestroySwapchain(Handle handle)=0
virtual RHIDeviceDescriptorSetLayout * GetDescriptorSetLayout(Handle handle) const =0
virtual RHIDeviceQueue * GetDeviceQueue(RHIDeviceQueueType type) const =0
virtual RHIDeviceScopedObjectHandle< RHIBuffer > CreateBuffer(RHIBufferDesc const &desc)=0
virtual void DestroyCommandPool(Handle handle)=0
RHIDevice(RHIApplication const &app)
Definition Device.hpp:139
virtual void WaitForTimelineSemaphores(Span< const Pair< RHIDeviceObjectHandle< RHIDeviceSemaphore >, size_t > > semaphores, size_t timeout)=0
Base class for all RHI objects.
Definition Details.hpp:16
Definition PipelineState.hpp:7
Definition Shader.hpp:5
Definition Swapchain.hpp:19
Definition Resource.hpp:146
std::pair< First, Second > Pair
Alias for std::pair
Definition Container.hpp:30
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
RHIDescriptorType
Definition Common.hpp:116
RHIDeviceQueueType
Definition Common.hpp:69
Definition Resource.hpp:21
RHIDescriptorType type
Definition Device.hpp:69
RHIShaderStage stage
Definition Device.hpp:68
Span< const Binding > bindings
Definition Device.hpp:72
bool updateAfterBind
Definition Device.hpp:75
RAII guard to wait for device idle on destruction.
Definition Device.hpp:205
~RHIDeviceIdleGuard()
Definition Device.hpp:213
void WaitIdle() const
Definition Device.hpp:209
RHIDeviceIdleGuard(RHIDevice const *device)
Definition Device.hpp:208
RHIDevice const * mDevice
Definition Device.hpp:206
RHISwapchain * swapchain
Definition Device.hpp:39
Span< RHIDeviceSemaphore *const > waits
Definition Device.hpp:41
uint32_t imageIndex
Definition Device.hpp:38
Span< RHICommandList *const > cmdLists
Definition Device.hpp:33
Span< RHIDeviceSemaphore *const > signals
Definition Device.hpp:31
Span< const TimelinePair > timelineSignals
Definition Device.hpp:25
Span< RHIDeviceSemaphore *const > waits
Definition Device.hpp:27
Span< const TimelinePair > timelineWaits
Definition Device.hpp:23
Span< const RHIPipelineStage > waitsStages
Definition Device.hpp:29
enum Foundation::RHI::RHIDeviceSampler::SamplerDesc::AddressMode::Mode Repeat
enum Foundation::RHI::RHIDeviceSampler::SamplerDesc::Filter::Type Linear
enum Foundation::RHI::RHIDeviceSampler::SamplerDesc::Mipmap::MipmapMode Linear
struct Foundation::RHI::RHIDeviceSampler::SamplerDesc::LOD lod
struct Foundation::RHI::RHIDeviceSampler::SamplerDesc::Mipmap mipmap
struct Foundation::RHI::RHIDeviceSampler::SamplerDesc::Anisotropy anisotropy
struct Foundation::RHI::RHIDeviceSampler::SamplerDesc::AddressMode addressMode
struct Foundation::RHI::RHIDeviceSampler::SamplerDesc::Filter filter
const char * name
Definition Device.hpp:137
uint32_t id
Definition Device.hpp:136
static void Destroy(RHIDevice *device, Handle handle)
Definition Device.hpp:271
static RHIBuffer * Get(RHIDevice const *device, Handle handle)
Definition Device.hpp:268
static void Destroy(RHIDevice *device, Handle handle)
Definition Device.hpp:247
static RHICommandPool * Get(RHIDevice const *device, Handle handle)
Definition Device.hpp:244
static void Destroy(RHIDevice *device, Handle handle)
Definition Device.hpp:295
static RHIDeviceDescriptorPool * Get(RHIDevice const *device, Handle handle)
Definition Device.hpp:292
static RHIDeviceDescriptorSetLayout * Get(RHIDevice const *device, Handle handle)
Definition Device.hpp:284
static void Destroy(RHIDevice *device, Handle handle)
Definition Device.hpp:287
static RHIDeviceFence * Get(RHIDevice const *device, Handle handle)
Definition Device.hpp:260
static void Destroy(RHIDevice *device, Handle handle)
Definition Device.hpp:263
static void Destroy(RHIDevice *device, Handle handle)
Definition Device.hpp:303
static RHIDeviceSampler * Get(RHIDevice const *device, Handle handle)
Definition Device.hpp:300
static RHIDeviceSemaphore * Get(RHIDevice const *device, Handle handle)
Definition Device.hpp:252
static void Destroy(RHIDevice *device, Handle handle)
Definition Device.hpp:255
static RHIPipelineState * Get(RHIDevice const *device, Handle handle)
Definition Device.hpp:228
static void Destroy(RHIDevice *device, Handle handle)
Definition Device.hpp:231
static RHIShaderModule * Get(RHIDevice const *device, Handle handle)
Definition Device.hpp:236
static void Destroy(RHIDevice *device, Handle handle)
Definition Device.hpp:239
static void Destroy(RHIDevice *device, Handle handle)
Definition Device.hpp:223
static RHISwapchain * Get(RHIDevice const *device, Handle handle)
Definition Device.hpp:220
static RHITexture * Get(RHIDevice const *device, Handle handle)
Definition Device.hpp:276
static void Destroy(RHIDevice *device, Handle handle)
Definition Device.hpp:279
Provides type traits for types derived from RHIObject.
Definition Details.hpp:30
Definition Resource.hpp:79