LiteFX 0.4.1.2025
Computer Graphics Engine
Loading...
Searching...
No Matches
vulkan_api.hpp
1#pragma once
2
3#include <litefx/config.h>
4
5#if !defined (LITEFX_VULKAN_API)
6# if defined(LiteFX_Backends_Vulkan_EXPORTS) && (defined _WIN32 || defined WINCE)
7# define LITEFX_VULKAN_API __declspec(dllexport)
8# elif (defined(LiteFX_Backends_Vulkan_EXPORTS) || defined(__APPLE__)) && defined __GNUC__ && __GNUC__ >= 4
9# define LITEFX_VULKAN_API __attribute__ ((visibility ("default")))
10# elif !defined(LiteFX_Backends_Vulkan_EXPORTS) && (defined _WIN32 || defined WINCE)
11# define LITEFX_VULKAN_API __declspec(dllimport)
12# endif
13#endif
14
15#ifndef LITEFX_VULKAN_API
16# define LITEFX_VULKAN_API
17#endif
18
19#if (defined _WIN32 || defined WINCE)
20# define VK_USE_PLATFORM_WIN32_KHR
21#else
22# pragma message ("Vulkan: No supported surface platform detected.")
23#endif
24
25#include <litefx/rendering.hpp>
26#include <vulkan/vulkan.h>
27#include "vulkan_formatters.hpp"
28
30 using namespace LiteFX::Math;
31 using namespace LiteFX::Rendering;
32
33 constexpr StringView VULKAN_LOG = "Backend::Vulkan"sv;
34
35 // Forward declarations.
48 class VulkanRasterizer;
54 class VulkanRenderPass;
55 class VulkanSwapChain;
56 class VulkanQueue;
58 class VulkanDevice;
59 class VulkanBackend;
60
61 // Interface declarations.
62 class IVulkanBuffer;
65 class IVulkanImage;
66 class IVulkanSampler;
70
71#if defined(LITEFX_BUILD_DEFINE_BUILDERS)
72 // Builder declarations.
73 class VulkanVertexBufferLayoutBuilder;
74 class VulkanDescriptorSetLayoutBuilder;
75 class VulkanPushConstantsLayoutBuilder;
76 class VulkanPipelineLayoutBuilder;
77 class VulkanShaderProgramBuilder;
78 class VulkanInputAssemblerBuilder;
79 class VulkanRasterizerBuilder;
80 class VulkanRenderPipelineBuilder;
81 class VulkanComputePipelineBuilder;
82 class VulkanRayTracingPipelineBuilder;
83 class VulkanRenderPassBuilder;
84 class VulkanBarrierBuilder;
85#endif // defined(LITEFX_BUILD_DEFINE_BUILDERS)
86
90 namespace Vk
91 {
95 Format LITEFX_VULKAN_API getFormat(const VkFormat& format);
96
100 VkFormat LITEFX_VULKAN_API getFormat(Format format);
101
105 //BufferFormat LITEFX_VULKAN_API getFormat(const VkFormat& format);
106
110 VkFormat LITEFX_VULKAN_API getFormat(BufferFormat format);
111
115 PolygonMode LITEFX_VULKAN_API getPolygonMode(const VkPolygonMode& mode);
116
120 VkPolygonMode LITEFX_VULKAN_API getPolygonMode(PolygonMode mode);
121
125 CullMode LITEFX_VULKAN_API getCullMode(const VkCullModeFlags& mode);
126
130 VkCullModeFlags LITEFX_VULKAN_API getCullMode(CullMode mode);
131
135 PrimitiveTopology LITEFX_VULKAN_API getPrimitiveTopology(const VkPrimitiveTopology& topology);
136
140 VkPrimitiveTopology LITEFX_VULKAN_API getPrimitiveTopology(PrimitiveTopology topology);
141
145 ShaderStage LITEFX_VULKAN_API getShaderStage(const VkShaderStageFlagBits& shaderType);
146
150 VkShaderStageFlagBits LITEFX_VULKAN_API getShaderStage(ShaderStage shaderType);
151
155 MultiSamplingLevel LITEFX_VULKAN_API getSamples(const VkSampleCountFlagBits& samples);
156
160 VkImageType LITEFX_VULKAN_API getImageType(ImageDimensions dimension);
161
165 VkImageViewType LITEFX_VULKAN_API getImageViewType(ImageDimensions dimension, UInt32 layers = 1);
166
170 VkSampleCountFlagBits LITEFX_VULKAN_API getSamples(MultiSamplingLevel samples);
171
175 VkCompareOp LITEFX_VULKAN_API getCompareOp(CompareOperation compareOp);
176
180 VkStencilOp LITEFX_VULKAN_API getStencilOp(StencilOperation stencilOp);
181
185 VkBlendFactor LITEFX_VULKAN_API getBlendFactor(BlendFactor blendFactor);
186
190 VkBlendOp LITEFX_VULKAN_API getBlendOperation(BlendOperation blendOperation);
191
195 VkPipelineStageFlags2 LITEFX_VULKAN_API getPipelineStage(PipelineStage pipelineStage);
196
200 VkAccessFlags2 LITEFX_VULKAN_API getResourceAccess(ResourceAccess resourceAccess);
201
205 VkImageLayout LITEFX_VULKAN_API getImageLayout(ImageLayout imageLayout);
206
213 template <typename THandle>
214 constexpr UInt64 handleAddress(const THandle handle) noexcept {
215 return reinterpret_cast<std::uintptr_t>(handle); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
216 }
217
223 template <>
224 constexpr UInt64 handleAddress(const UInt64 handle) noexcept {
225 return handle;
226 }
227 }
228
232 class LITEFX_VULKAN_API VulkanGraphicsAdapter final : public IGraphicsAdapter, public Resource<VkPhysicalDevice> {
233 LITEFX_IMPLEMENTATION(VulkanGraphicsAdapterImpl);
235
236 private:
241 explicit VulkanGraphicsAdapter(VkPhysicalDevice adapter);
242
243 private:
245 VulkanGraphicsAdapter(VulkanGraphicsAdapter&&) noexcept = delete;
246
249
251 VulkanGraphicsAdapter& operator=(VulkanGraphicsAdapter&&) noexcept = delete;
252
254 VulkanGraphicsAdapter& operator=(const VulkanGraphicsAdapter&) = delete;
255
256 public:
258 ~VulkanGraphicsAdapter() noexcept override;
259
260 public:
266 static inline auto create(VkPhysicalDevice adapter) {
268 }
269
270 public:
272 String name() const override;
273
275 UInt64 uniqueId() const noexcept override;
276
278 UInt32 vendorId() const noexcept override;
279
281 UInt32 deviceId() const noexcept override;
282
284 GraphicsAdapterType type() const noexcept override;
285
287 UInt64 driverVersion() const noexcept override;
288
290 UInt32 apiVersion() const noexcept override;
291
293 UInt64 dedicatedMemory() const noexcept override;
294
295 public:
300 VkPhysicalDeviceLimits limits() const noexcept;
301
307 bool validateDeviceExtensions(Span<const String> extensions) const;
308
314 Enumerable<String> getAvailableDeviceExtensions() const;
315
321 bool validateDeviceLayers(const Span<const String> validationLayers) const;
322
328 Enumerable<String> deviceValidationLayers() const;
329 };
330
334 class LITEFX_VULKAN_API VulkanSurface final : public ISurface, public Resource<VkSurfaceKHR> {
335 LITEFX_IMPLEMENTATION(VulkanSurfaceImpl)
336
337 public:
338#ifdef VK_USE_PLATFORM_WIN32_KHR
345 VulkanSurface(const VkSurfaceKHR& surface, const VkInstance& instance, const HWND hwnd);
346#else
352 VulkanSurface(const VkSurfaceKHR& surface, const VkInstance& instance);
353#endif // VK_USE_PLATFORM_WIN32_KHR
354
355
358
360 VulkanSurface(const VulkanSurface&) = delete;
361
363 VulkanSurface& operator=(VulkanSurface&&) noexcept;
364
366 VulkanSurface& operator=(const VulkanSurface&) = delete;
367
369 ~VulkanSurface() noexcept override;
370
371 public:
376 const VkInstance& instance() const noexcept;
377
378#ifdef VK_USE_PLATFORM_WIN32_KHR
384 HWND windowHandle() const noexcept;
385#endif // VK_USE_PLATFORM_WIN32_KHR
386 };
387
391 class LITEFX_VULKAN_API VulkanPlatformException : public RuntimeException {
392 private:
393 VkResult m_code;
394
395 public:
400 explicit VulkanPlatformException(VkResult result) :
401 RuntimeException("Operation returned {0}.", result), m_code(result) { }
402
408 explicit VulkanPlatformException(VkResult result, StringView message) :
409 RuntimeException("{1} Operation returned {0}.", result, message), m_code(result) { }
410
417 template <typename ...TArgs>
418 explicit VulkanPlatformException(VkResult result, StringView format, TArgs&&... args) :
419 VulkanPlatformException(result, std::format(format, std::forward<TArgs>(args)...)) { }
420
423
426
428 VulkanPlatformException& operator=(VulkanPlatformException&&) noexcept = default;
429
432
434 ~VulkanPlatformException() noexcept override = default;
435
436 public:
441 VkResult code() const noexcept {
442 return m_code;
443 }
444 };
445
452 template <typename ...TArgs>
453 static inline void raiseIfFailed(VkResult result, StringView message, TArgs&&... args) {
454 if (result == VK_SUCCESS) [[likely]]
455 return;
456 else
457 throw VulkanPlatformException(result, message, std::forward<TArgs>(args)...);
458 }
459}
Represents the base interface for a Vulkan acceleration structure implementation.
Definition vulkan.hpp:288
Represents the base interface for a Vulkan buffer implementation.
Definition vulkan.hpp:183
Represents a Vulkan sampled image or the base interface for a texture.
Definition vulkan.hpp:237
Represents a Vulkan index buffer.
Definition vulkan.hpp:217
Represents a Vulkan sampler.
Definition vulkan.hpp:270
Represents a Vulkan vertex buffer.
Definition vulkan.hpp:200
Defines a rendering backend that creates a Vulkan device.
Definition vulkan.hpp:2752
Implements a Vulkan bottom-level acceleration structure (BLAS).
Definition vulkan.hpp:304
Records commands for a VulkanQueue
Definition vulkan.hpp:1397
Implements a Vulkan ComputePipeline.
Definition vulkan.hpp:1861
Implements a Vulkan IDescriptorLayout
Definition vulkan.hpp:775
Implements a Vulkan DescriptorSet.
Definition vulkan.hpp:716
Implements a Vulkan DescriptorSetLayout.
Definition vulkan.hpp:858
Implements a Vulkan graphics device.
Definition vulkan.hpp:2536
Implements a Vulkan frame buffer.
Definition vulkan.hpp:1998
Represents a Vulkan IGraphicsAdapter.
Definition vulkan_api.hpp:232
A graphics factory that produces objects for a VulkanDevice.
Definition vulkan.hpp:2441
Implements a Vulkan index buffer layout.
Definition vulkan.hpp:112
Implements the Vulkan input assembler state.
Definition vulkan.hpp:1192
Implements a Vulkan PipelineLayout.
Definition vulkan.hpp:1114
Defines the base class for Vulkan pipeline state objects.
Definition vulkan.hpp:1366
An exception that is thrown, if a requested Vulkan operation could not be executed.
Definition vulkan_api.hpp:391
VulkanPlatformException(VulkanPlatformException &&) noexcept=default
VulkanPlatformException(VkResult result, StringView format, TArgs &&... args)
Initializes a new exception.
Definition vulkan_api.hpp:418
VulkanPlatformException(VkResult result, StringView message)
Initializes a new exception.
Definition vulkan_api.hpp:408
VulkanPlatformException(VkResult result)
Initializes a new exception.
Definition vulkan_api.hpp:400
Implements the Vulkan PushConstantsLayout.
Definition vulkan.hpp:1064
Implements the Vulkan IPushConstantsRange.
Definition vulkan.hpp:1012
Implements a Vulkan command queue.
Definition vulkan.hpp:1650
Implements a Vulkan IRasterizer.
Definition vulkan.hpp:1276
Implements a Vulkan RayTracingPipeline.
Definition vulkan.hpp:1919
Implements a Vulkan render pass.
Definition vulkan.hpp:2137
Implements a Vulkan RenderPipeline.
Definition vulkan.hpp:1781
Implements a Vulkan IShaderModule.
Definition vulkan.hpp:568
Implements a Vulkan ShaderProgram.
Definition vulkan.hpp:642
Represents a Vulkan ISurface.
Definition vulkan_api.hpp:334
VulkanSurface(VulkanSurface &&) noexcept
Implements a Vulkan swap chain.
Definition vulkan.hpp:2337
Implements a Vulkan top-level acceleration structure (TLAS).
Definition vulkan.hpp:399
Implements a Vulkan vertex buffer layout.
Definition vulkan.hpp:21
Represents a physical graphics adapter.
Definition rendering_api.hpp:2648
Represents a surface to render to.
Definition rendering_api.hpp:2720
Implements the IResource interface.
Definition containers.hpp:823
An exception that is thrown, if a requested operation could not be executed.
Definition exceptions.hpp:235
static auto create(TArgs &&... args) -> SharedPtr< T >
Generic factory method used to create instances of the shared object.
Definition containers.hpp:1114
Definition math.hpp:30
uint64_t UInt64
A type for an unsigned 64 bit integer.
Definition math.hpp:66
uint32_t UInt32
A type for an unsigned 32 bit integer.
Definition math.hpp:56
Format LITEFX_VULKAN_API getFormat(const VkFormat &format)
Definition convert.cpp:5
VkCompareOp LITEFX_VULKAN_API getCompareOp(CompareOperation compareOp)
Definition convert.cpp:920
VkBlendOp LITEFX_VULKAN_API getBlendOperation(BlendOperation blendOperation)
Definition convert.cpp:976
MultiSamplingLevel LITEFX_VULKAN_API getSamples(const VkSampleCountFlagBits &samples)
Definition convert.cpp:841
constexpr UInt64 handleAddress(const THandle handle) noexcept
Returns the address of a dispatchable handle.
Definition vulkan_api.hpp:214
ShaderStage LITEFX_VULKAN_API getShaderStage(const VkShaderStageFlagBits &shaderType)
Definition convert.cpp:766
VkBlendFactor LITEFX_VULKAN_API getBlendFactor(BlendFactor blendFactor)
Definition convert.cpp:950
VkStencilOp LITEFX_VULKAN_API getStencilOp(StencilOperation stencilOp)
Definition convert.cpp:935
VkAccessFlags2 LITEFX_VULKAN_API getResourceAccess(ResourceAccess resourceAccess)
Definition convert.cpp:1048
VkImageType LITEFX_VULKAN_API getImageType(ImageDimensions dimension)
Definition convert.cpp:864
PolygonMode LITEFX_VULKAN_API getPolygonMode(const VkPolygonMode &mode)
Definition convert.cpp:664
PrimitiveTopology LITEFX_VULKAN_API getPrimitiveTopology(const VkPrimitiveTopology &topology)
Definition convert.cpp:728
VkPipelineStageFlags2 LITEFX_VULKAN_API getPipelineStage(PipelineStage pipelineStage)
Definition convert.cpp:988
VkImageLayout LITEFX_VULKAN_API getImageLayout(ImageLayout imageLayout)
Definition convert.cpp:1106
CullMode LITEFX_VULKAN_API getCullMode(const VkCullModeFlags &mode)
Definition convert.cpp:694
VkImageViewType LITEFX_VULKAN_API getImageViewType(ImageDimensions dimension, UInt32 layers=1)
Definition convert.cpp:880
Definition dx12.hpp:11
constexpr StringView VULKAN_LOG
Definition vulkan_api.hpp:33
Definition dx12.hpp:11
ImageLayout
Specifies the layout of an IImage resource.
Definition rendering_api.hpp:1686
BlendFactor
Specifies a blend factor.
Definition rendering_api.hpp:1307
CullMode
Describes which faces are culled by the Rasterizer stage.
Definition rendering_api.hpp:991
ShaderStage
Describes the valid shader stages of a graphics pipeline.
Definition rendering_api.hpp:799
GraphicsAdapterType
Defines different types of graphics adapters.
Definition rendering_api.hpp:75
CompareOperation
Describes the operation used to compare depth or stencil values during depth/stencil tests.
Definition rendering_api.hpp:1215
BlendOperation
Specifies a blend operation.
Definition rendering_api.hpp:1359
PrimitiveTopology
Describes the topology of a mesh primitive.
Definition rendering_api.hpp:765
PolygonMode
Describes the draw mode for polygons.
Definition rendering_api.hpp:970
ImageDimensions
Describes the dimensions of a image resource, i.e. the dimensions that are required to access a texel...
Definition rendering_api.hpp:1086
StencilOperation
An operation that is applied to the stencil buffer.
Definition rendering_api.hpp:1261
PipelineStage
Defines pipeline stages as points where synchronization may occur.
Definition rendering_api.hpp:1373
BufferFormat
Describes a buffer attribute format.
Definition rendering_api.hpp:339
ResourceAccess
Defines how a IBuffer or IImage resource is accessed.
Definition rendering_api.hpp:1537
Format
Describes a texel format.
Definition rendering_api.hpp:182
MultiSamplingLevel
Describes the number of samples with which a IImage is sampled.
Definition rendering_api.hpp:1111
std::string String
Definition string.hpp:24
std::string_view StringView
Definition string.hpp:26
std::span< T > Span
Represents a view of an array.
Definition containers.hpp:87
An input range over another range, where the returned values of type T are covariants of the values ...
Definition containers.hpp:529
An allocator used to allocate the shared object.
Definition containers.hpp:1098