LiteFX 0.3.1.2022
Computer Graphics Engine
vulkan_api.hpp
1#pragma once
2
3#if !defined (LITEFX_VULKAN_API)
4# if defined(LiteFX_Backends_Vulkan_EXPORTS) && (defined _WIN32 || defined WINCE)
5# define LITEFX_VULKAN_API __declspec(dllexport)
6# elif (defined(LiteFX_Backends_Vulkan_EXPORTS) || defined(__APPLE__)) && defined __GNUC__ && __GNUC__ >= 4
7# define LITEFX_VULKAN_API __attribute__ ((visibility ("default")))
8# elif !defined(LiteFX_Backends_Vulkan_EXPORTS) && (defined _WIN32 || defined WINCE)
9# define LITEFX_VULKAN_API __declspec(dllimport)
10# endif
11#endif
12
13#ifndef LITEFX_VULKAN_API
14# define LITEFX_VULKAN_API
15#endif
16
17#if (defined _WIN32 || defined WINCE)
18# define VK_USE_PLATFORM_WIN32_KHR
19#else
20# pragma message ("Vulkan: No supported surface platform detected.")
21#endif
22
23#include <litefx/config.h>
24#include <litefx/rendering.hpp>
25#include <vulkan/vulkan.h>
26
28 using namespace LiteFX::Math;
29 using namespace LiteFX::Rendering;
30
31 constexpr char VULKAN_LOG[] = "Backend::Vulkan";
32
33 // Forward declarations.
46 class VulkanRasterizer;
51 class VulkanRenderPass;
53 class VulkanSwapChain;
54 class VulkanQueue;
56 class VulkanDevice;
57 class VulkanBackend;
58
59 // Interface declarations.
60 class IVulkanBuffer;
63 class IVulkanImage;
64 class IVulkanSampler;
65
66#if defined(BUILD_DEFINE_BUILDERS)
67 // Builder declarations.
68 class VulkanVertexBufferLayoutBuilder;
69 class VulkanDescriptorSetLayoutBuilder;
70 class VulkanPushConstantsLayoutBuilder;
71 class VulkanPipelineLayoutBuilder;
72 class VulkanShaderProgramBuilder;
73 class VulkanInputAssemblerBuilder;
74 class VulkanRasterizerBuilder;
75 class VulkanRenderPipelineBuilder;
76 class VulkanComputePipelineBuilder;
77 class VulkanRenderPassBuilder;
78#endif // defined(BUILD_DEFINE_BUILDERS)
79
83 namespace Vk
84 {
88 Format LITEFX_VULKAN_API getFormat(const VkFormat& format);
89
93 VkFormat LITEFX_VULKAN_API getFormat(const Format& format);
94
98 //BufferFormat LITEFX_VULKAN_API getFormat(const VkFormat& format);
99
103 VkFormat LITEFX_VULKAN_API getFormat(const BufferFormat& format);
104
108 PolygonMode LITEFX_VULKAN_API getPolygonMode(const VkPolygonMode& mode);
109
113 VkPolygonMode LITEFX_VULKAN_API getPolygonMode(const PolygonMode& mode);
114
118 CullMode LITEFX_VULKAN_API getCullMode(const VkCullModeFlags& mode);
119
123 VkCullModeFlags LITEFX_VULKAN_API getCullMode(const CullMode& mode);
124
128 PrimitiveTopology LITEFX_VULKAN_API getPrimitiveTopology(const VkPrimitiveTopology& topology);
129
133 VkPrimitiveTopology LITEFX_VULKAN_API getPrimitiveTopology(const PrimitiveTopology& topology);
134
138 ShaderStage LITEFX_VULKAN_API getShaderStage(const VkShaderStageFlagBits& shaderType);
139
143 VkShaderStageFlagBits LITEFX_VULKAN_API getShaderStage(const ShaderStage& shaderType);
144
148 MultiSamplingLevel LITEFX_VULKAN_API getSamples(const VkSampleCountFlagBits& samples);
149
153 VkImageType LITEFX_VULKAN_API getImageType(const ImageDimensions& dimension);
154
158 VkImageViewType LITEFX_VULKAN_API getImageViewType(const ImageDimensions& dimension, const UInt32& layers = 1);
159
163 VkSampleCountFlagBits LITEFX_VULKAN_API getSamples(const MultiSamplingLevel& samples);
164
168 VkCompareOp LITEFX_VULKAN_API getCompareOp(const CompareOperation& compareOp);
169
173 VkStencilOp LITEFX_VULKAN_API getStencilOp(const StencilOperation& stencilOp);
174
178 VkBlendFactor LITEFX_VULKAN_API getBlendFactor(const BlendFactor& blendFactor);
179
183 VkBlendOp LITEFX_VULKAN_API getBlendOperation(const BlendOperation& blendOperation);
184
188 VkImageLayout LITEFX_VULKAN_API getImageLayout(const ResourceState& resourceState);
189
193 VkAccessFlags LITEFX_VULKAN_API getAccessFlags(const ResourceState& resourceState);
194 }
195
199 class LITEFX_VULKAN_API VulkanGraphicsAdapter : public IGraphicsAdapter, public Resource<VkPhysicalDevice> {
200 LITEFX_IMPLEMENTATION(VulkanGraphicsAdapterImpl);
201
202 public:
207 explicit VulkanGraphicsAdapter(VkPhysicalDevice adapter);
210 virtual ~VulkanGraphicsAdapter() noexcept;
211
212 public:
214 virtual String name() const noexcept override;
215
217 virtual UInt64 uniqueId() const noexcept override;
218
220 virtual UInt32 vendorId() const noexcept override;
221
223 virtual UInt32 deviceId() const noexcept override;
224
226 virtual GraphicsAdapterType type() const noexcept override;
227
229 virtual UInt32 driverVersion() const noexcept override;
230
232 virtual UInt32 apiVersion() const noexcept override;
233
235 virtual UInt64 dedicatedMemory() const noexcept override;
236
237 public:
242 VkPhysicalDeviceLimits limits() const noexcept;
243
249 bool validateDeviceExtensions(Span<const String> extensions) const noexcept;
250
256 Array<String> getAvailableDeviceExtensions() const noexcept;
257
263 bool validateDeviceLayers(const Span<const String> validationLayers) const noexcept;
264
270 Array<String> deviceValidationLayers() const noexcept;
271 };
272
276 class LITEFX_VULKAN_API VulkanSurface : public ISurface, public Resource<VkSurfaceKHR> {
277 LITEFX_IMPLEMENTATION(VulkanSurfaceImpl)
278
279 public:
280#ifdef VK_USE_PLATFORM_WIN32_KHR
287 VulkanSurface(const VkSurfaceKHR& surface, const VkInstance& instance, const HWND hwnd);
288#else
294 VulkanSurface(const VkSurfaceKHR& surface, const VkInstance& instance);
295#endif // VK_USE_PLATFORM_WIN32_KHR
296
297 VulkanSurface(const VulkanSurface&) = delete;
299 virtual ~VulkanSurface() noexcept;
300
301 public:
306 const VkInstance& instance() const noexcept;
307
308#ifdef VK_USE_PLATFORM_WIN32_KHR
314 const HWND windowHandle() const noexcept;
315#endif // VK_USE_PLATFORM_WIN32_KHR
316 };
317
318 DEFINE_EXCEPTION(VulkanPlatformException, std::runtime_error);
319
328 template <typename TException, typename ...TArgs>
329 inline void raiseIfFailed(VkResult result, StringView message, TArgs&&... args) {
330 if (result == VK_SUCCESS) [[likely]]
331 return;
332
333 throw TException(VulkanPlatformException("Result: {0}", result), fmt::format(fmt::runtime(message), std::forward<TArgs>(args)...));
334 }
335}
Represents the base interface for a Vulkan buffer implementation.
Definition: vulkan.hpp:92
Represents a Vulkan sampled image or the base interface for a texture.
Definition: vulkan.hpp:125
Represents a Vulkan index buffer.
Definition: vulkan.hpp:112
Represents a Vulkan sampler.
Definition: vulkan.hpp:167
Represents a Vulkan vertex buffer.
Definition: vulkan.hpp:102
Defines a rendering backend that creates a Vulkan device.
Definition: vulkan.hpp:1593
Records commands for a VulkanCommandQueue
Definition: vulkan.hpp:802
Implements a Vulkan ComputePipeline.
Definition: vulkan.hpp:954
Implements a Vulkan IDescriptorLayout
Definition: vulkan.hpp:386
Implements a Vulkan DescriptorSet.
Definition: vulkan.hpp:343
Implements a Vulkan DescriptorSetLayout.
Definition: vulkan.hpp:439
Implements a Vulkan graphics device.
Definition: vulkan.hpp:1469
Implements a Vulkan frame buffer.
Definition: vulkan.hpp:999
Represents a Vulkan IGraphicsAdapter.
Definition: vulkan_api.hpp:199
VulkanGraphicsAdapter(const VulkanGraphicsAdapter &)=delete
VulkanGraphicsAdapter(VulkanGraphicsAdapter &&)=delete
A graphics factory that produces objects for a VulkanDevice.
Definition: vulkan.hpp:1409
Implements a Vulkan index buffer layout.
Definition: vulkan.hpp:55
Implements the Vulkan input assembler state.
Definition: vulkan.hpp:694
Implements a IInputAttachmentMapping.
Definition: vulkan.hpp:1168
Implements a Vulkan PipelineLayout.
Definition: vulkan.hpp:648
Defines the base class for Vulkan pipeline state objects.
Definition: vulkan.hpp:778
Implements the Vulkan PushConstantsLayout.
Definition: vulkan.hpp:596
Implements the Vulkan IPushConstantsRange.
Definition: vulkan.hpp:556
Implements a Vulkan command queue.
Definition: vulkan.hpp:1278
Implements a Vulkan IRasterizer.
Definition: vulkan.hpp:734
Implements a Vulkan render pass.
Definition: vulkan.hpp:1067
Implements a Vulkan RenderPipeline.
Definition: vulkan.hpp:881
Implements a Vulkan IShaderModule.
Definition: vulkan.hpp:247
Implements a Vulkan ShaderProgram.
Definition: vulkan.hpp:304
Represents a Vulkan ISurface.
Definition: vulkan_api.hpp:276
VulkanSurface(VulkanSurface &&)=delete
VulkanSurface(const VulkanSurface &)=delete
Implements a Vulkan swap chain.
Definition: vulkan.hpp:1222
Implements a Vulkan vertex buffer layout.
Definition: vulkan.hpp:18
Represents a physical graphics adapter.
Definition: rendering_api.hpp:1754
Represents a surface to render to.
Definition: rendering_api.hpp:1815
Implements the IResource interface.
Definition: containers.hpp:371
Definition: math.hpp:30
uint32_t UInt32
Definition: math.hpp:37
uint64_t UInt64
Definition: math.hpp:39
Format LITEFX_VULKAN_API getFormat(const VkFormat &format)
Definition: convert.cpp:5
VkImageLayout LITEFX_VULKAN_API getImageLayout(const ResourceState &resourceState)
Definition: convert.cpp:956
MultiSamplingLevel LITEFX_VULKAN_API getSamples(const VkSampleCountFlagBits &samples)
Definition: convert.cpp:809
VkImageViewType LITEFX_VULKAN_API getImageViewType(const ImageDimensions &dimension, const UInt32 &layers=1)
Definition: convert.cpp:848
VkCompareOp LITEFX_VULKAN_API getCompareOp(const CompareOperation &compareOp)
Definition: convert.cpp:888
ShaderStage LITEFX_VULKAN_API getShaderStage(const VkShaderStageFlagBits &shaderType)
Definition: convert.cpp:766
PolygonMode LITEFX_VULKAN_API getPolygonMode(const VkPolygonMode &mode)
Definition: convert.cpp:664
VkBlendFactor LITEFX_VULKAN_API getBlendFactor(const BlendFactor &blendFactor)
Definition: convert.cpp:918
VkStencilOp LITEFX_VULKAN_API getStencilOp(const StencilOperation &stencilOp)
Definition: convert.cpp:903
PrimitiveTopology LITEFX_VULKAN_API getPrimitiveTopology(const VkPrimitiveTopology &topology)
Definition: convert.cpp:728
VkBlendOp LITEFX_VULKAN_API getBlendOperation(const BlendOperation &blendOperation)
Definition: convert.cpp:944
CullMode LITEFX_VULKAN_API getCullMode(const VkCullModeFlags &mode)
Definition: convert.cpp:694
VkImageType LITEFX_VULKAN_API getImageType(const ImageDimensions &dimension)
Definition: convert.cpp:832
VkAccessFlags LITEFX_VULKAN_API getAccessFlags(const ResourceState &resourceState)
Definition: convert.cpp:974
Definition: dx12.hpp:8
void raiseIfFailed(HRESULT hr, StringView message, TArgs &&... args)
Definition: dx12_api.hpp:266
DEFINE_EXCEPTION(DX12PlatformException, std::runtime_error)
constexpr char VULKAN_LOG[]
Definition: vulkan_api.hpp:31
Definition: dx12.hpp:8
std::string String
Definition: string.hpp:19
std::vector< T > Array
Represents a dynamic array.
Definition: containers.hpp:58
std::span< T > Span
Represents a view of an array.
Definition: containers.hpp:72
std::string_view StringView
Definition: string.hpp:21
auto format(LiteFX::Platform t, FormatContext &ctx) const
Definition: app_formatters.hpp:8