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#include "material/TextureFilteringPreference.h"
14
15// External.
16#include "VulkanMemoryAllocator/include/vk_mem_alloc.h"
17#include "ktxvulkan.h"
18
19namespace ne {
20 class VulkanResourceManager;
21 class Renderer;
22
24 class VulkanResource : public GpuResource {
25 // Only resource manager can create this resource
26 // (simply because only manager has a memory allocator object).
27 friend class VulkanResourceManager;
28
29 public:
30 VulkanResource() = delete;
31 VulkanResource(const VulkanResource&) = delete;
32 VulkanResource& operator=(const VulkanResource&) = delete;
33
34 virtual ~VulkanResource() override;
35
46 inline VkBuffer getInternalBufferResource() const { return pBufferResource; };
47
58 inline VkImageView getInternalImageView() const { return pImageView; }
59
72 inline VkImageView getInternalCubemapImageView(size_t iCubemapFaceIndex = 0) {
73 // Make sure we won't access out of bounds.
74 if (iCubemapFaceIndex >= vCubeMapViews.size()) [[unlikely]] { // <- remove if no log
75 // Log an error because this case is marked as unlikely.
76 // ! REMOVE [[unlikely]] IF REMOVING LOG !
77 Logger::get().error(std::format(
78 "cubemap view was requested on resource \"{}\" with an out of bounds index "
79 "{} while cubemap view count is {}",
81 iCubemapFaceIndex,
82 vCubeMapViews.size()));
83 return nullptr;
84 }
85
86 return vCubeMapViews[iCubemapFaceIndex];
87 }
88
99 inline VkImageView getInternalImageViewDepthAspect() const { return pDepthAspectImageView; }
100
111 inline VkImage getInternalImage() const { return pImageResource; }
112
118 bool isStorageResource() const;
119
126 VkSampler getTextureSamplerForThisImage() const;
127
137 inline std::pair<std::recursive_mutex, VmaAllocation>* getInternalResourceMemory() {
138 // Self check:
139 if (optionalKtxTexture.has_value()) [[unlikely]] {
140 Error error(std::format(
141 "failed to query VmaAllocation of resource \"{}\" because "
142 "this resource is a KTX texture that was loaded via external library "
143 "(accessing VmaAllocation of such object is complicated, if you want "
144 "to access VkDeviceMemory it's a good time to implement such a getter "
145 "because VkDeviceMemory is available for KTX textures)",
146 getResourceName()));
147 error.showError();
148 throw std::runtime_error(error.getFullErrorMessage());
149 }
150
151 return &mtxResourceMemory;
152 }
153
154 private:
169 VulkanResourceManager* pResourceManager,
170 const std::string& sResourceName,
171 std::variant<VkBuffer, VkImage> pInternalResource,
173 VmaAllocation pResourceMemory,
174 unsigned int iElementSizeInBytes,
175 unsigned int iElementCount);
176
187 VulkanResourceManager* pResourceManager,
188 const std::string& sResourceName,
189 ktxVulkanTexture ktxTexture,
190 TextureFilteringPreference filteringPreference);
191
207 static std::variant<std::unique_ptr<VulkanResource>, Error> create(
208 VulkanResourceManager* pResourceManager,
209 const std::string& sResourceName,
210 VmaAllocator pMemoryAllocator,
211 const VkBufferCreateInfo& bufferInfo,
212 const VmaAllocationCreateInfo& allocationInfo,
213 unsigned int iElementSizeInBytes,
214 unsigned int iElementCount);
215
230 static std::variant<std::unique_ptr<VulkanResource>, Error> create(
231 VulkanResourceManager* pResourceManager,
232 const std::string& sResourceName,
233 VmaAllocator pMemoryAllocator,
234 const VkImageCreateInfo& imageInfo,
235 const VmaAllocationCreateInfo& allocationInfo,
236 std::optional<VkImageAspectFlags> viewDescription,
237 bool bIsCubeMapView = false);
238
250 static std::variant<std::unique_ptr<VulkanResource>, Error> create(
251 VulkanResourceManager* pResourceManager,
252 const std::string& sResourceName,
253 ktxVulkanTexture ktxTexture,
254 TextureFilteringPreference filteringPreference);
255
257 std::optional<ktxVulkanTexture> optionalKtxTexture;
258
264 VkBuffer pBufferResource = nullptr;
265
271 VkImage pImageResource = nullptr;
272
274 VkImageView pImageView = nullptr;
275
281 VkImageView pDepthAspectImageView = nullptr;
282
284 std::vector<VkImageView> vCubeMapViews;
285
291 std::pair<std::recursive_mutex, VmaAllocation> mtxResourceMemory;
292
294 const TextureFilteringPreference textureFilteringPreference;
295
297 const bool isUsedAsStorageResource = false;
298 };
299} // 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:24
std::pair< std::recursive_mutex, VmaAllocation > * getInternalResourceMemory()
Definition: VulkanResource.h:137
bool isStorageResource() const
Definition: VulkanResource.cpp:117
std::pair< std::recursive_mutex, VmaAllocation > mtxResourceMemory
Definition: VulkanResource.h:291
const TextureFilteringPreference textureFilteringPreference
Definition: VulkanResource.h:294
VkImageView pDepthAspectImageView
Definition: VulkanResource.h:281
VkImage getInternalImage() const
Definition: VulkanResource.h:111
VkBuffer getInternalBufferResource() const
Definition: VulkanResource.h:46
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:174
std::vector< VkImageView > vCubeMapViews
Definition: VulkanResource.h:284
std::optional< ktxVulkanTexture > optionalKtxTexture
Definition: VulkanResource.h:257
const bool isUsedAsStorageResource
Definition: VulkanResource.h:297
VkImage pImageResource
Definition: VulkanResource.h:271
VkSampler getTextureSamplerForThisImage() const
Definition: VulkanResource.cpp:119
VkImageView getInternalImageView() const
Definition: VulkanResource.h:58
VkBuffer pBufferResource
Definition: VulkanResource.h:264
VkImageView pImageView
Definition: VulkanResource.h:274
VkImageView getInternalCubemapImageView(size_t iCubemapFaceIndex=0)
Definition: VulkanResource.h:72
VkImageView getInternalImageViewDepthAspect() const
Definition: VulkanResource.h:99