Nameless Engine
Loading...
Searching...
No Matches
VulkanResource.h
1#pragma once
2
3// Standard.
4#include <variant>
5#include <memory>
6#include <mutex>
7#include <format>
8#include <string>
9
10// Custom.
11#include "render/general/resource/GpuResource.h"
12#include "io/Logger.h"
13
14// External.
15#include "VulkanMemoryAllocator/include/vk_mem_alloc.h"
16#include "ktxvulkan.h"
17
18namespace ne {
19 class VulkanResourceManager;
20 class Renderer;
21
23 class VulkanResource : public GpuResource {
24 // Only resource manager can create this resource
25 // (simply because only manager has a memory allocator object).
26 friend class VulkanResourceManager;
27
28 public:
29 VulkanResource() = delete;
30 VulkanResource(const VulkanResource&) = delete;
31 VulkanResource& operator=(const VulkanResource&) = delete;
32
33 virtual ~VulkanResource() override;
34
45 inline VkBuffer getInternalBufferResource() const { return pBufferResource; };
46
57 inline VkImageView getInternalImageView() const { return pImageView; }
58
71 inline VkImageView getInternalCubemapImageView(size_t iCubemapFaceIndex = 0) {
72 // Make sure we won't access out of bounds.
73 if (iCubemapFaceIndex >= vCubeMapViews.size()) [[unlikely]] { // <- remove if no log
74 // Log an error because this case is marked as unlikely.
75 // ! REMOVE [[unlikely]] IF REMOVING LOG !
76 Logger::get().error(std::format(
77 "cubemap view was requested on resource \"{}\" with an out of bounds index "
78 "{} while cubemap view count is {}",
80 iCubemapFaceIndex,
81 vCubeMapViews.size()));
82 return nullptr;
83 }
84
85 return vCubeMapViews[iCubemapFaceIndex];
86 }
87
98 inline VkImageView getInternalImageViewDepthAspect() const { return pDepthAspectImageView; }
99
110 inline VkImage getInternalImage() const { return pImageResource; }
111
117 bool isStorageResource() const;
118
128 inline std::pair<std::recursive_mutex, VmaAllocation>* getInternalResourceMemory() {
129 // Self check:
130 if (optionalKtxTexture.has_value()) [[unlikely]] {
131 Error error(std::format(
132 "failed to query VmaAllocation of resource \"{}\" because "
133 "this resource is a KTX texture that was loaded via external library "
134 "(accessing VmaAllocation of such object is complicated, if you want "
135 "to access VkDeviceMemory it's a good time to implement such a getter "
136 "because VkDeviceMemory is available for KTX textures)",
137 getResourceName()));
138 error.showError();
139 throw std::runtime_error(error.getFullErrorMessage());
140 }
141
142 return &mtxResourceMemory;
143 }
144
145 private:
160 VulkanResourceManager* pResourceManager,
161 const std::string& sResourceName,
162 std::variant<VkBuffer, VkImage> pInternalResource,
164 VmaAllocation pResourceMemory,
165 unsigned int iElementSizeInBytes,
166 unsigned int iElementCount);
167
177 VulkanResourceManager* pResourceManager,
178 const std::string& sResourceName,
179 ktxVulkanTexture ktxTexture);
180
196 static std::variant<std::unique_ptr<VulkanResource>, Error> create(
197 VulkanResourceManager* pResourceManager,
198 const std::string& sResourceName,
199 VmaAllocator pMemoryAllocator,
200 const VkBufferCreateInfo& bufferInfo,
201 const VmaAllocationCreateInfo& allocationInfo,
202 unsigned int iElementSizeInBytes,
203 unsigned int iElementCount);
204
219 static std::variant<std::unique_ptr<VulkanResource>, Error> create(
220 VulkanResourceManager* pResourceManager,
221 const std::string& sResourceName,
222 VmaAllocator pMemoryAllocator,
223 const VkImageCreateInfo& imageInfo,
224 const VmaAllocationCreateInfo& allocationInfo,
225 std::optional<VkImageAspectFlags> viewDescription,
226 bool bIsCubeMapView = false);
227
238 static std::variant<std::unique_ptr<VulkanResource>, Error> create(
239 VulkanResourceManager* pResourceManager,
240 const std::string& sResourceName,
241 ktxVulkanTexture ktxTexture);
242
244 std::optional<ktxVulkanTexture> optionalKtxTexture;
245
251 VkBuffer pBufferResource = nullptr;
252
258 VkImage pImageResource = nullptr;
259
261 VkImageView pImageView = nullptr;
262
268 VkImageView pDepthAspectImageView = nullptr;
269
271 std::vector<VkImageView> vCubeMapViews;
272
278 std::pair<std::recursive_mutex, VmaAllocation> mtxResourceMemory;
279
281 const bool isUsedAsStorageResource = false;
282 };
283} // namespace ne
Definition: Error.h:27
std::string getFullErrorMessage() const
Definition: Error.cpp:84
void showError() const
Definition: Error.cpp:102
Definition: GpuResource.h:16
const std::string sResourceName
Definition: GpuResource.h:97
std::string getResourceName() const
Definition: GpuResource.cpp:30
const unsigned int iElementCount
Definition: GpuResource.h:94
const unsigned int iElementSizeInBytes
Definition: GpuResource.h:91
void error(std::string_view sText, const std::source_location location=std::source_location::current()) const
Definition: Logger.cpp:75
static Logger & get()
Definition: Logger.cpp:41
Definition: VulkanResourceManager.h:18
Definition: VulkanResource.h:23
std::pair< std::recursive_mutex, VmaAllocation > * getInternalResourceMemory()
Definition: VulkanResource.h:128
bool isStorageResource() const
Definition: VulkanResource.cpp:114
std::pair< std::recursive_mutex, VmaAllocation > mtxResourceMemory
Definition: VulkanResource.h:278
VkImageView pDepthAspectImageView
Definition: VulkanResource.h:268
VkImage getInternalImage() const
Definition: VulkanResource.h:110
VkBuffer getInternalBufferResource() const
Definition: VulkanResource.h:45
static std::variant< std::unique_ptr< VulkanResource >, Error > create(VulkanResourceManager *pResourceManager, const std::string &sResourceName, VmaAllocator pMemoryAllocator, const VkBufferCreateInfo &bufferInfo, const VmaAllocationCreateInfo &allocationInfo, unsigned int iElementSizeInBytes, unsigned int iElementCount)
Definition: VulkanResource.cpp:116
std::vector< VkImageView > vCubeMapViews
Definition: VulkanResource.h:271
std::optional< ktxVulkanTexture > optionalKtxTexture
Definition: VulkanResource.h:244
const bool isUsedAsStorageResource
Definition: VulkanResource.h:281
VkImage pImageResource
Definition: VulkanResource.h:258
VkImageView getInternalImageView() const
Definition: VulkanResource.h:57
VkBuffer pBufferResource
Definition: VulkanResource.h:251
VkImageView pImageView
Definition: VulkanResource.h:261
VkImageView getInternalCubemapImageView(size_t iCubemapFaceIndex=0)
Definition: VulkanResource.h:71
VkImageView getInternalImageViewDepthAspect() const
Definition: VulkanResource.h:98