Foundation
Loading...
Searching...
No Matches
Resource.hpp
Go to the documentation of this file.
1#pragma once
2#include "Common.hpp"
3namespace Foundation::RHI {
4 class RHIDevice;
6 bool isAlias{ false }; // If true, the resource is an alias of another resource.
12 bool shared{ false };
15 bool coherent{ false };
19 bool staging{ false };
20 };
25 size_t size{}; // size in bytes
26 };
27 class RHIBuffer;
29 template<typename T> using RHIBufferHandle = RHIHandle<RHIBuffer, T>;
30 class RHIBuffer : public RHIObject {
31 protected:
33 public:
36 : mDevice(device), mDesc(desc) {
37 }
45 virtual void* Map() = 0;
50 virtual void Flush(size_t offset = 0, size_t size = kFullSize) = 0;
56 virtual void Unmap() = 0;
57
65 template<typename T> Span<T> MapSpan(size_t count = kFullSize) {
66 void* p = Map();
67 if (count == kFullSize)
68 count = mDesc.size / sizeof(T);
69 CHECK(count * sizeof(T) <= mDesc.size && "Buffer map range out of bounds");
70 return { static_cast<T*>(p) , count };
71 }
72
73 [[nodiscard]] virtual RHIBufferScopedHandle<RHIBuffer> CreateAliasedBuffer(RHIBufferDesc const& desc, size_t offset = 0) = 0;
74 [[nodiscard]] virtual RHIBuffer* GetAliasedBuffer(Handle handle) const = 0;
75 virtual void DestroyAliasedBuffer(Handle handle) = 0;
76
77 virtual void DebugSetObjectName(const char* name) = 0;
78 };
90 class RHITexture;
91 class RHITextureView;
93 template<typename T> using RHITextureHandle = RHIHandle<RHITexture, T>;
101 // Single mip level and array layer range
103 // Number of mip levels in the range
105 /* @brief Mip level used (inclusive) */
107 return { layer.mipLevel, layer.mipLevel + mipCount - 1 };
108 }
109 /* @brief Array layers used (inclusive) */
123 {
125 .layer = {
126 .aspect = aspect,
127 .mipLevel = base_mip_level,
128 .baseArrayLayer = base_array_layer,
129 .layerCount = layer_count,
130 },
131 .mipCount = mip_count,
132 };
133 CHECK_MSG(res.IsValid(), "Invalid Subresource Range is being created!");
134 return res;
135 }
136 [[nodiscard]] constexpr bool IsValid() const
137 {
138 return layer.aspect.value && mipCount && layer.layerCount;
139 }
140 };
146 class RHITexture : public RHIObject {
147 protected:
149 public:
152 : mDevice(device), mDesc(desc) {
153 }
154
155 virtual void* Map() = 0;
156 virtual void Flush(size_t offset, size_t size) = 0;
157 virtual void Unmap() = 0;
158
160 [[nodiscard]] virtual RHITextureView* GetImageView(Handle handle) const = 0;
161 virtual void DestroyImageView(Handle handle) = 0;
162
164 [[nodiscard]] virtual RHITexture* GetAliasedTexture(Handle handle) const = 0;
165 virtual void DestroyAliasedTexture(Handle handle) = 0;
166
167 virtual void DebugSetObjectName(const char* name) = 0;
168 };
169 class RHITextureView : public RHIObject {
170 protected:
173 public:
175 : mImage(image), mDesc(desc) {
176 }
177 [[nodiscard]] virtual RHITexture* GetTexture() const = 0;
178 virtual void DebugSetObjectName(const char* name) = 0;
179 };
181 static RHITextureView* Get(RHITexture const* image, Handle handle) {
182 return image->GetImageView(handle);
183 }
184 static void Destroy(RHITexture* image, Handle handle) {
185 image->DestroyImageView(handle);
186 }
187 };
188 template<> struct RHIObjectTraits<RHIBuffer, RHIBuffer> {
189 static RHIBuffer* Get(RHIBuffer const* buffer, Handle handle) {
190 return buffer->GetAliasedBuffer(handle);
191 }
192 static void Destroy(RHIBuffer* buffer, Handle handle) {
193 buffer->DestroyAliasedBuffer(handle);
194 }
195 };
197 static RHITexture* Get(RHITexture const* texture, Handle handle) {
198 return texture->GetAliasedTexture(handle);
199 }
200 static void Destroy(RHITexture* texture, Handle handle) {
201 texture->DestroyAliasedTexture(handle);
202 }
203 };
204}
#define CHECK(expr)
Definition Logging.hpp:26
#define CHECK_MSG(expr, format_str,...)
Definition Logging.hpp:31
Definition Resource.hpp:30
virtual void Flush(size_t offset=0, size_t size=kFullSize)=0
Flushes the mapped region to the device. Depending on the implementation, this may be a no-op.
virtual void DestroyAliasedBuffer(Handle handle)=0
virtual void Unmap()=0
Releases or unmaps a previously mapped resource. Implementations MUST guarantee that Unmap() is calle...
Span< T > MapSpan(size_t count=kFullSize)
Creates a span that maps a contiguous region of the buffer to the host memory. Behaviour of the memor...
Definition Resource.hpp:65
const RHIDevice & mDevice
Definition Resource.hpp:32
virtual RHIBufferScopedHandle< RHIBuffer > CreateAliasedBuffer(RHIBufferDesc const &desc, size_t offset=0)=0
virtual void DebugSetObjectName(const char *name)=0
const RHIBufferDesc mDesc
Definition Resource.hpp:34
virtual RHIBuffer * GetAliasedBuffer(Handle handle) const =0
virtual void * Map()=0
Maps the entire buffer to the host memory. Alignment is implementation-defined. Implementations MUST ...
RHIBuffer(RHIDevice const &device, RHIBufferDesc const &desc)
Definition Resource.hpp:35
Definition Device.hpp:131
Base class for all RHI objects.
Definition Details.hpp:16
Definition Resource.hpp:169
const RHITextureViewDesc & mDesc
Definition Resource.hpp:172
virtual void DebugSetObjectName(const char *name)=0
RHITextureView(RHITexture const &image, RHITextureViewDesc const &desc)
Definition Resource.hpp:174
virtual RHITexture * GetTexture() const =0
const RHITexture & mImage
Definition Resource.hpp:171
Definition Resource.hpp:146
const RHITextureDesc mDesc
Definition Resource.hpp:150
virtual void DestroyAliasedTexture(Handle handle)=0
virtual RHITextureScopedHandle< RHITexture > CreateAliasedTexture(RHITextureDesc const &desc, size_t offset=0)=0
virtual void DebugSetObjectName(const char *name)=0
RHITexture(RHIDevice const &device, RHITextureDesc const &desc)
Definition Resource.hpp:151
virtual void DestroyImageView(Handle handle)=0
virtual RHITextureView * GetImageView(Handle handle) const =0
const RHIDevice & mDevice
Definition Resource.hpp:148
virtual RHITextureScopedHandle< RHITextureView > CreateTextureView(RHITextureViewDesc const &desc)=0
virtual RHITexture * GetAliasedTexture(Handle handle) const =0
virtual void * Map()=0
virtual void Flush(size_t offset, size_t size)=0
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
RHITextureLayout
Definition Common.hpp:99
uint64_t Handle
Definition Details.hpp:9
RHIDeviceHeapType
Definition Common.hpp:93
glm::vec< 3, uint32_t > RHIExtent3D
Definition Common.hpp:11
RHIHandle< RHIBuffer, T > RHIBufferHandle
Definition Resource.hpp:29
RHITextureDimension
Definition Common.hpp:135
RHIHandle< RHITexture, T > RHITextureHandle
Definition Resource.hpp:93
RHIResourceFormat
Definition Common.hpp:20
static constexpr size_t kFullSize
Definition Common.hpp:8
RHIScopedHandle< RHIBuffer, T > RHIBufferScopedHandle
Definition Resource.hpp:28
RHIMultisampleCount
Definition Common.hpp:132
RHIResourceHostAccess
Definition Common.hpp:110
RHIScopedHandle< RHITexture, T > RHITextureScopedHandle
Definition Resource.hpp:92
Definition Resource.hpp:21
RHIBufferUsage usage
How the buffer can be used by the device, initially.
Definition Resource.hpp:24
RHIResourceDesc resource
Definition Resource.hpp:22
size_t size
Definition Resource.hpp:25
static RHIBuffer * Get(RHIBuffer const *buffer, Handle handle)
Definition Resource.hpp:189
static void Destroy(RHIBuffer *buffer, Handle handle)
Definition Resource.hpp:192
static RHITextureView * Get(RHITexture const *image, Handle handle)
Definition Resource.hpp:181
static void Destroy(RHITexture *image, Handle handle)
Definition Resource.hpp:184
static void Destroy(RHITexture *texture, Handle handle)
Definition Resource.hpp:200
static RHITexture * Get(RHITexture const *texture, Handle handle)
Definition Resource.hpp:197
Provides type traits for types derived from RHIObject.
Definition Details.hpp:30
Definition Resource.hpp:5
bool coherent
Definition Resource.hpp:15
RHIDeviceHeapType heap
Which heap the resource is allocated in.
Definition Resource.hpp:8
RHIResourceHostAccess hostAccess
How the resource can be accessed by the host (CPU)
Definition Resource.hpp:10
bool staging
Definition Resource.hpp:19
bool shared
Can be shared with other devices.
Definition Resource.hpp:12
bool isAlias
Definition Resource.hpp:6
Definition Resource.hpp:79
RHITextureUsage usage
Definition Resource.hpp:82
RHIExtent3D extent
Definition Resource.hpp:83
RHIResourceDesc resource
Definition Resource.hpp:80
RHIMultisampleCount sampleCount
Definition Resource.hpp:85
RHIResourceFormat format
Definition Resource.hpp:84
RHITextureDimension dimension
Definition Resource.hpp:81
RHITextureLayout initialLayout
Definition Resource.hpp:88
uint32_t mipLevels
Definition Resource.hpp:86
uint32_t arrayLayers
Definition Resource.hpp:87
uint32_t baseArrayLayer
Definition Resource.hpp:97
uint32_t mipLevel
Definition Resource.hpp:96
RHITextureAspectFlag aspect
Definition Resource.hpp:95
uint32_t layerCount
Definition Resource.hpp:98
RHITextureSubresourceLayer layer
Definition Resource.hpp:102
Pair< uint32_t, uint32_t > GetMipLevelRange() const
Definition Resource.hpp:106
static RHITextureSubresourceRange Create(RHITextureAspectFlag aspect=RHITextureAspectFlagBits::Color, uint32_t base_mip_level=0, uint32_t mip_count=1, uint32_t base_array_layer=0, uint32_t layer_count=1)
Helper function to create a Subresource Range with default parameters.
Definition Resource.hpp:122
Pair< uint32_t, uint32_t > GetArrayLayerRange() const
Definition Resource.hpp:110
uint32_t mipCount
Definition Resource.hpp:104
constexpr bool IsValid() const
Definition Resource.hpp:136
Definition Resource.hpp:141
RHIResourceFormat format
Definition Resource.hpp:142
RHITextureDimension dimension
Definition Resource.hpp:143
RHITextureSubresourceRange range
Definition Resource.hpp:144