CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
VKCommon.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
12
13#include "VKLoader.hpp"
14
16
23
28
29#define CE_VK_SAFE_CAST
30
31//#define CE_VK_PROFILING
32#if defined(CE_VK_PROFILING)
33#define CE_VK_PROFILE_FUNC CE_PROFILE_FUNC
34#else
35#define CE_VK_PROFILE_FUNC
36#endif
37
38//#define CE_VK_GPU_PROFILING
39#if defined(CE_VK_GPU_PROFILING)
40namespace tracy {
41 using namespace CeresEngine {}
42
43#include <TracyVulkan.hpp>
44#endif
45
46#if CE_PLATFORM_MAC || CE_PLATFORM_IOS
47#define CE_VK_CHECK_PTR(P) true
48#else
49#define CE_VK_CHECK_PTR(P) (P != nullptr)
50#endif
51
52 namespace CeresEngine {
53
54 // using VKAllocator = FallbackAllocator<AllocatorReference<MemoryPoolCollection<NodePool, BucketDistribution::Log2Buckets>>, DefaultAllocator>;
57
60
65 template<typename T> static VKObjectAllocator VKMakeObjectAllocator(const std::size_t initialSize = 100) {
66 return VKObjectAllocator{VKObjectAllocator::allocator_type{
67 sizeof(T),
68 sizeof(T) * initialSize,
70 }};
71 }
72
73 template<typename T> using VKVector = Vector<T, VKAllocator>;
74
75#define CE_VK_RENDER_API_OBJECT_FORWARD(N) \
76 class VK##N; \
77 using VK##N##Ptr = RC<VK##N>;
79#undef CE_VK_RENDER_API_OBJECT_FORWARD
80
81#if defined(CE_VK_SAFE_CAST)
82#define CE_VK_CAST dynamic_cast
83#else
84#define CE_VK_CAST static_cast
85#endif
86
88 public:
89 static void* operator new(size_t size) noexcept;
90 static void operator delete(void* p, std::size_t sz) noexcept;
91 };
92
93 template<typename ChildTypeName, typename ParentType = void> class VKObject : public VKObjectBase {
94 public:
99 static RC<ChildTypeName> cast(const RC<ParentType>& ptr) { return RC<ChildTypeName>(CE_VK_CAST<ChildTypeName*>(ptr.get())); }
100
105 static ChildTypeName& cast(ParentType& object) { return CE_VK_CAST<ChildTypeName&>(object); }
106
111 static const ChildTypeName& cast(const ParentType& object) { return CE_VK_CAST<const ChildTypeName&>(object); }
112
117 static ChildTypeName* cast(ParentType* object) { return CE_VK_CAST<ChildTypeName*>(object); }
118
123 static const ChildTypeName* cast(const ParentType* object) { return CE_VK_CAST<const ChildTypeName*>(object); }
124 };
125
126 template<typename ChildTypeName> class VKObject<ChildTypeName, void> {};
127
129 public:
132
133 public:
136 explicit VKDeviceObjectBase(VKDevice& device) noexcept;
137
140 };
141
142 template<typename ChildTypeName, typename ParentType = void>
143 class VKDeviceObject : public VKObject<ChildTypeName, ParentType>, public VKDeviceObjectBase {
144 protected:
147 explicit VKDeviceObject(VKDevice& device) noexcept : VKDeviceObjectBase(device) {}
148 };
149
154 template<typename ObjectType, typename OwnerType = VKDevice, typename DescriptorType = typename ObjectType::Descriptor> class VKObjectCache {
155 public:
158 using Descriptor = DescriptorType;
159
161 using Object = ObjectType;
162
165
166 private:
168 OwnerType& mOwner;
169
172
173 public:
176 explicit VKObjectCache(OwnerType& owner);
177
180
181 public:
187 ObjectPtr get(const Descriptor& descriptor);
188
190 ObjectPtr operator[](const Descriptor& descriptor) { return get(descriptor); }
191
193 void release(Object* object);
194 };
195
196 [[nodiscard]] VkShaderStageFlagBits VKMap(ShaderType shaderType);
197 [[nodiscard]] VkFormat VKMap(Format format);
198 [[nodiscard]] VkImageViewType VKMap(GPUImageType imageType);
199 [[nodiscard]] VkPrimitiveTopology VKMap(PrimitiveTopology primitiveTopology);
200 [[nodiscard]] VkPolygonMode VKMap(GPUPolygonMode polygonMode);
201 [[nodiscard]] VkCullModeFlags VKMap(GPUCullMode cullMode);
202 [[nodiscard]] VkCompareOp VKMap(GPUCompareOp compareOp);
203 [[nodiscard]] VkStencilOp VKMap(GPUStencilOp stencilOp);
204 [[nodiscard]] VkLogicOp VKMap(GPULogicOp logicOp);
205 [[nodiscard]] VkBlendFactor VKMap(GPUBlendOp blendOp);
206 [[nodiscard]] VkBlendOp VKMap(GPUBlendArithmetic blendArithmetic);
207 [[nodiscard]] VkSamplerAddressMode VKMap(GPUSamplerAddressMode addressMode);
208 [[nodiscard]] VkDescriptorType VKMap(GPUDescriptorType resourceViewType);
209 [[nodiscard]] VkAttachmentLoadOp VKMap(GPUAttachmentLoadOp loadOp);
210 [[nodiscard]] VkAttachmentStoreOp VKMap(GPUAttachmentStoreOp storeOp);
211 [[nodiscard]] VkImageLayout VKMap(GPUImageLayout layout);
212 [[nodiscard]] VkIndexType VKMap(IndexType type);
213 [[nodiscard]] VkMemoryPropertyFlags VKMap(const GPUMemoryProperties& memoryProperty);
214 [[nodiscard]] VkComponentSwizzle VKMap(GPUImageSwizzle imageSwizzle);
215 [[nodiscard]] VkVertexInputRate VKMap(GPUVertexInputRate vertexInputRate);
216
217 [[nodiscard]] VkImageAspectFlags toVkImageAspectFlags(Format format);
218
219 void convert(VkRect2D& dst, const Viewport& src) noexcept;
220 void convert(VkViewport& dst, const Viewport& src) noexcept;
221 [[nodiscard]] VkViewport convert(const Viewport& src) noexcept;
222
223 void convert(VkRect2D& dst, const Scissor& src) noexcept;
224 [[nodiscard]] VkRect2D convert(const Scissor& src) noexcept;
225
226 void convert(VkOffset3D& dst, const TOffset3<UInt32>& src) noexcept;
227 [[nodiscard]] VkOffset3D convert(const TOffset3<UInt32>& src) noexcept;
228
229 void convert(VkExtent3D& dst, const TExtent3<UInt32>& src) noexcept;
230 [[nodiscard]] VkExtent3D convert(const TExtent3<UInt32>& src) noexcept;
231
232 [[nodiscard]] StringView VKObjectTypeString(VkObjectType objectType) noexcept;
233
234 // Converts the boolean value into a VkBool32 value.
235 [[nodiscard]] inline constexpr VkBool32 VKBoolean(const bool value) noexcept { return (value ? VK_TRUE : VK_FALSE); }
236
237 // Throws an RuntimeError exception if 'result' is not VK_SUCCESS.
238 void VKThrowIfFailed(VkResult result, const char* info);
239
245
250 [[nodiscard]] VKSurfaceSupportDetails VKQuerySurfaceSupport(VkPhysicalDevice device, VkSurfaceKHR surface);
251
260
265 struct FamilyInfo {
268
270 VkQueueFamilyProperties properties;
271
274 UInt32 usageCount = 0;
275 };
276
279
282 explicit VKQueueFamilyAssigner(VkPhysicalDevice device) noexcept;
283
285 [[nodiscard]] Optional<VKPickedQueueFamily> pickFamily(VkQueueFlagBits capabilities) noexcept;
286
289
291 [[nodiscard]] Optional<VKPickedQueueFamily> pickTransferFamily() noexcept;
292
294 [[nodiscard]] Optional<VKPickedQueueFamily> pickComputeFamily() noexcept;
295
297 [[nodiscard]] Optional<VKPickedQueueFamily> pickPresentFamily(VkPhysicalDevice device, VkSurfaceKHR surface) noexcept;
298 };
299
302 [[nodiscard]] Vector<VkLayerProperties> VKQueryInstanceLayerProperties();
303
307 [[nodiscard]] Vector<VkExtensionProperties> VKQueryInstanceExtensionProperties(const char* layerName = nullptr);
308
312 [[nodiscard]] Vector<VkPhysicalDevice> VKQueryPhysicalDevices(VkInstance instance);
313
317 [[nodiscard]] Vector<VkExtensionProperties> VKQueryDeviceExtensionProperties(VkPhysicalDevice device);
318
322 [[nodiscard]] Vector<VkQueueFamilyProperties> VKQueryQueueFamilyProperties(VkPhysicalDevice device);
323
324 template<typename T> struct VKDebugMarkerTypeTrait;
325
326#define DECLARE_DEBUG_MARKER_TYPE_TRAIT(T, C) \
327 template<> struct VKDebugMarkerTypeTrait<T> { static const constexpr VkObjectType type = C; };
328
329 DECLARE_DEBUG_MARKER_TYPE_TRAIT(VkInstance, VK_OBJECT_TYPE_INSTANCE);
330 DECLARE_DEBUG_MARKER_TYPE_TRAIT(VkBuffer, VK_OBJECT_TYPE_BUFFER);
331 DECLARE_DEBUG_MARKER_TYPE_TRAIT(VkImage, VK_OBJECT_TYPE_IMAGE);
332 DECLARE_DEBUG_MARKER_TYPE_TRAIT(VkImageView, VK_OBJECT_TYPE_IMAGE_VIEW);
333 DECLARE_DEBUG_MARKER_TYPE_TRAIT(VkSampler, VK_OBJECT_TYPE_SAMPLER);
334 DECLARE_DEBUG_MARKER_TYPE_TRAIT(VkPipeline, VK_OBJECT_TYPE_PIPELINE);
335 DECLARE_DEBUG_MARKER_TYPE_TRAIT(VkDescriptorSet, VK_OBJECT_TYPE_DESCRIPTOR_SET);
336 DECLARE_DEBUG_MARKER_TYPE_TRAIT(VkCommandBuffer, VK_OBJECT_TYPE_COMMAND_BUFFER);
337 DECLARE_DEBUG_MARKER_TYPE_TRAIT(VkRenderPass, VK_OBJECT_TYPE_RENDER_PASS);
338 DECLARE_DEBUG_MARKER_TYPE_TRAIT(VkShaderModule, VK_OBJECT_TYPE_SHADER_MODULE);
339 DECLARE_DEBUG_MARKER_TYPE_TRAIT(VkFramebuffer, VK_OBJECT_TYPE_FRAMEBUFFER);
340 DECLARE_DEBUG_MARKER_TYPE_TRAIT(VkSemaphore, VK_OBJECT_TYPE_SEMAPHORE);
341
342 template<typename T> VkResult VKSetObjectName(const VkDevice device, T object, StringView name) {
343 if(!CE_VK_CHECK_PTR(vkSetDebugUtilsObjectNameEXT)) {
344 return VK_SUCCESS;
345 }
346
347 const VkDebugUtilsObjectNameInfoEXT objectNameInfo = {
348 VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
349 nullptr,
351 (UInt64)object,
352 name.data(),
353 };
354 return vkSetDebugUtilsObjectNameEXT(device, &objectNameInfo);
355 }
356
357 } // namespace CeresEngine
#define CE_RENDER_API_OBJECT_EACH(F)
A macro that invokes the function-macro F for every RenderAPI object type.
Definition Forward.hpp:18
#define CE_VK_RENDER_API_OBJECT_FORWARD(N)
Definition VKCommon.hpp:75
#define DECLARE_DEBUG_MARKER_TYPE_TRAIT(T, C)
Definition VKCommon.hpp:326
#define CE_VK_CHECK_PTR(P)
Definition VKCommon.hpp:49
A retain-release type of smart pointer.
Definition SmartPtr.hpp:132
Pointer get() const noexcept
Definition SmartPtr.hpp:244
Definition Optional.hpp:17
Definition VKDevice.hpp:79
Definition VKCommon.hpp:128
VKDeviceObjectBase(VKDevice &device) noexcept
Creates a new VKDeviceObjectBase from the given device.
~VKDeviceObjectBase() noexcept
Destroys the VKDeviceObjectBase.
VKDevice & deviceVK
The owning VKDevice instance.
Definition VKCommon.hpp:131
Definition VKCommon.hpp:143
VKDeviceObject(VKDevice &device) noexcept
Creates a new VKDeviceObject from the given device.
Definition VKCommon.hpp:147
Definition VKCommon.hpp:87
A object that caches existing Vulkan objects.
Definition VKCommon.hpp:154
~VKObjectCache()
Destroys the Vulkan device object cache.
void release(Object *object)
Releases a previously cached object.
DescriptorType Descriptor
The type of descriptor used to detect duplicates and create new objects.
Definition VKCommon.hpp:158
ObjectType Object
The type of object managed by the cache object.
Definition VKCommon.hpp:161
VKObjectCache(OwnerType &owner)
Creates a new Vulkan device object cache for the given device.
OwnerType & mOwner
The object owning the cached objects.
Definition VKCommon.hpp:168
HashMap< UInt64, Object * > mCache
A map that contains all cached objects.
Definition VKCommon.hpp:171
ObjectPtr get(const Descriptor &descriptor)
Searches for an existing object that is compatible with the given descriptor.
ObjectPtr operator[](const Descriptor &descriptor)
Searches for an existing object that is compatible with the given descriptor.
Definition VKCommon.hpp:190
Definition VKCommon.hpp:93
static const ChildTypeName * cast(const ParentType *object)
Casts a object of type ParentType to a object of type ChildTypeName.
Definition VKCommon.hpp:123
static const ChildTypeName & cast(const ParentType &object)
Casts a object of type ParentType to a object of type ChildTypeName.
Definition VKCommon.hpp:111
static RC< ChildTypeName > cast(const RC< ParentType > &ptr)
Casts a RC containing a ParentType pointer to a pointer to a pointer to a ChildTypeName.
Definition VKCommon.hpp:99
static ChildTypeName & cast(ParentType &object)
Casts a object of type ParentType to a object of type ChildTypeName.
Definition VKCommon.hpp:105
static ChildTypeName * cast(ParentType *object)
Casts a object of type ParentType to a object of type ChildTypeName.
Definition VKCommon.hpp:117
Definition Application.hpp:19
VKSurfaceSupportDetails VKQuerySurfaceSupport(VkPhysicalDevice device, VkSurfaceKHR surface)
Queries the device for surface support details.
VkResult VKSetObjectName(const VkDevice device, T object, StringView name)
Definition VKCommon.hpp:342
std::uint64_t UInt64
Definition DataTypes.hpp:26
GPUAttachmentLoadOp
Enumeration for render pass attachment load operations.
Definition GPURenderPass.hpp:32
GPUImageSwizzle
Image component swizzle enumeration.
Definition GPUImage.hpp:192
GPUPolygonMode
Polygon filling modes enumeration.
Definition GPUGraphicsPipeline.hpp:143
VkImageAspectFlags toVkImageAspectFlags(Format format)
VKAllocator & gVKAllocator()
GPUCullMode
Polygon culling modes enumeration.
Definition GPUGraphicsPipeline.hpp:153
GPUImageLayout
Definition GPUImage.hpp:65
GPUAttachmentStoreOp
Enumeration for render pass attachment store operations.
Definition GPURenderPass.hpp:48
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
GPUSamplerAddressMode
Technique for resolving image coordinates that are outside of the range [0, 1].
Definition GPUSampler.hpp:29
GPUCompareOp
Compare operations enumeration.
Definition GPUGraphicsPipeline.hpp:37
GPUBlendOp
Blending operations enumeration.
Definition GPUGraphicsPipeline.hpp:84
Format
Definition TextureFormat.hpp:54
std::unordered_map< Key, T, Hash, KeyEqual, ScopedAllocatorAdaptor< StdAllocator< Pair< const Key, T >, RawAllocator > > > HashMap
HashMap is an associative container that contains key-value pairs with unique keys.
Definition Map.hpp:33
void convert(VkRect2D &dst, const Viewport &src) noexcept
GPUBlendArithmetic
Blending arithmetic operations enumeration.
Definition GPUGraphicsPipeline.hpp:129
GPUStencilOp
Stencil operations enumeration.
Definition GPUGraphicsPipeline.hpp:63
GPUImageType
Definition GPUImage.hpp:36
GPUResourceType
An enumeration of supported resource types.
Definition GPUResource.hpp:15
constexpr VkBool32 VKBoolean(const bool value) noexcept
Definition VKCommon.hpp:235
ThreadSafeAllocator< MemoryPool< NodePool, AllocatorReference< VKAllocator > > > VKObjectAllocator
A pool allocator to be used for individual object pools.
Definition VKCommon.hpp:59
Vector< T, VKAllocator > VKVector
Definition VKCommon.hpp:73
void VKThrowIfFailed(VkResult result, const char *info)
std::uint32_t UInt32
Definition DataTypes.hpp:23
VkShaderStageFlagBits VKMap(ShaderType shaderType)
StringView VKObjectTypeString(VkObjectType objectType) noexcept
foonathan::memory::thread_safe_allocator< RawAllocator, Mutex > ThreadSafeAllocator
Definition Allocator.hpp:104
GPULogicOp
Logical pixel operation enumeration.
Definition GPUGraphicsPipeline.hpp:166
Definition Allocator.hpp:66
Specify rate at which vertex attributes are pulled from buffers.
Definition GPUVertexFormat.hpp:63
Definition IndexType.hpp:20
Primitive topology enumeration.
Definition PrimitiveTopology.hpp:21
Definition GPUGraphicsPipeline.hpp:281
Shader type enumeration.
Definition ShaderType.hpp:59
Definition VKCommon.hpp:324
A structure that represents a picked queue family.
Definition VKCommon.hpp:253
UInt32 queueIndex
The index of the queue inside the picked family.
Definition VKCommon.hpp:258
UInt32 familyIndex
The index of picked the queue family.
Definition VKCommon.hpp:255
Struct that contains information about each queue family supported by the device.
Definition VKCommon.hpp:265
VkQueueFamilyProperties properties
A structure that contains properties of the queue family.
Definition VKCommon.hpp:270
UInt32 index
The queue family index.
Definition VKCommon.hpp:267
A helper class helps assigning a queue families.
Definition VKCommon.hpp:262
Vector< FamilyInfo > families
The set of supported families by the device.
Definition VKCommon.hpp:278
VKQueueFamilyAssigner(VkPhysicalDevice device) noexcept
Creates a new VKQueueFamilyAssigner.
Optional< VKPickedQueueFamily > pickFamily(VkQueueFlagBits capabilities) noexcept
Picks a new family that has the requested capabilities.
Optional< VKPickedQueueFamily > pickGraphicsFamily() noexcept
Picks a new queue family that is the most suitable for graphics.
Definition VKCommon.hpp:240
Vector< VkSurfaceFormatKHR > formats
Definition VKCommon.hpp:242
Vector< VkPresentModeKHR > presentModes
Definition VKCommon.hpp:243
VkSurfaceCapabilitiesKHR capabilities
Definition VKCommon.hpp:241
Definition GPUGraphicsPipeline.hpp:214