Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
ApplicationContext.h
Go to the documentation of this file.
1#pragma once
2#include "Exceptions.h"
3
4#include <filesystem>
5#include <mutex>
6#include <openxr/openxr.h>
7#include <vulkan/vulkan_core.h>
8#include <vk_mem_alloc.h>
9#include <openxr/openxr_platform.h>
10#include <optional>
11#include <string>
12#include <unordered_map>
13#include <utility>
14#include <vector>
15
16namespace Engine
17{
18 namespace Exceptions
19 {
24 class Environment : public Exception {
25 public:
26 explicit Environment(const std::string& message) : Exception(message) {}
27 explicit Environment(const std::stringstream& message) : Exception(message) {}
28 };
29
34 {
35 public:
36 explicit MissingVulkanFeature(const std::string& message) : Exception(message) {}
37 explicit MissingVulkanFeature(const std::stringstream& message) : Exception(message) {}
38 };
39 }
40
41 namespace Core
42 {
47 {
48 public:
55 VkSampler getOrCreate( VkDevice device, VkSamplerAddressMode addressMode );
56
61 void cleanup( VkDevice device );
62
63 private:
64 std::unordered_map<VkSamplerAddressMode, VkSampler> cache_;
65 std::mutex mutex_;
66 };
67
72 {
73 public:
74 ApplicationContext() = default;
76
81 void createDevice( VkSurfaceKHR mirrorSurface );
82
87 [[nodiscard]] XrInstance getXrInstance() const;
88
93 [[nodiscard]] XrSystemId getXrSystemId() const;
94
99 [[nodiscard]] VkInstance getVkInstance() const;
100
105 [[nodiscard]] const VkDevice getVkDevice() const;
106
111 [[nodiscard]] const VkQueue & getPresentQueue() const;
112
117 [[nodiscard]] const VkQueue & getGraphicsQueue() const;
118
123 [[nodiscard]] const VkQueue & getTransferQueue() const;
124
129 [[nodiscard]] VkSampleCountFlagBits getMultisampleCount() const;
130
135 [[nodiscard]] const VkPhysicalDevice & getVkPhysicalDevice() const;
136
141 [[nodiscard]] uint32_t getVkGraphicsQueueFamilyIndex() const;
142
147 [[nodiscard]] uint32_t getVkTransferQueueFamilyIndex() const;
148
153 [[nodiscard]] VkDeviceSize getUniformBufferOffsetAlignment() const;
154
159 [[nodiscard]] uint32_t getMaxPushConstantsSize() const;
160
165 [[nodiscard]] const XrViewConfigurationType getXrViewType() const;
166
171
175 void cleanup();
176
185 [[nodiscard]] bool usesDedicatedTransferQueue() const;
186
191 [[nodiscard]] VmaAllocator getVmaAllocator() const;
192
193 [[nodiscard]] SamplerCache& getSamplerCache() { return samplerCache_; }
194
195 [[nodiscard]] bool isMeshShaderSupported() const;
196 [[nodiscard]] bool isFbColorSpaceSupported() const { return fbColorSpaceSupported_; }
197
198 private:
199 static void validateFeature( VkBool32 feature, const std::string & errorMessage );
200
212 VkPhysicalDevice physicalDevice,
213 VkSurfaceKHR surface,
214 uint32_t & outGraphicsIndex,
215 uint32_t & outPresentIndex,
216 uint32_t & outTransferIndex
217 );
218
227 std::optional<std::filesystem::path> customOpenXrRuntimePath =
228 std::optional<std::filesystem::path>()
229 );
230
235 PFN_xrGetVulkanInstanceExtensionsKHR xrGetVulkanInstanceExtensionsKHR = nullptr;
236 PFN_xrGetVulkanGraphicsDevice2KHR xrGetVulkanGraphicsDevice2KHR = nullptr;
237 PFN_xrGetVulkanGraphicsRequirements2KHR xrGetVulkanGraphicsRequirements2KHR = nullptr;
238
239 PFN_xrCreateVulkanInstanceKHR xrCreateVulkanInstanceKHR = nullptr;
240 PFN_xrCreateVulkanDeviceKHR xrCreateVulkanDeviceKHR = nullptr;
241
242#ifdef IS_IN_DEBUG
243 PFN_xrCreateDebugUtilsMessengerEXT xrCreateDebugUtilsMessengerEXT = nullptr;
244 PFN_xrDestroyDebugUtilsMessengerEXT xrDestroyDebugUtilsMessengerEXT = nullptr;
245
246 void createOpenXrDebugMessenger();
247#endif
248
253
258
263
268
273
277 std::vector<XrExtensionProperties> supportedOpenXRInstanceExtensions;
278
282 std::vector<XrApiLayerProperties> supportedOpenXRApiLayers;
283
287 XrInstance xrInstance = XR_NULL_HANDLE;
288
289#ifdef IS_IN_DEBUG
293 XrDebugUtilsMessengerEXT xrDebugMessenger = XR_NULL_HANDLE;
294#endif
295
299 XrSystemId xrSystemId = XR_NULL_SYSTEM_ID;
300
304 std::vector<VkExtensionProperties> supportedVulkanInstanceExtensions;
305
309 std::vector<const char *> vulkanInstanceExtensions;
310
314 VkInstance vkInstance = VK_NULL_HANDLE;
315
319 VkPhysicalDevice vkPhysicalDevice = VK_NULL_HANDLE;
320
324 std::vector<VkExtensionProperties> supportedVulkanDeviceExtensions;
328 std::vector<const char *> vulkanDeviceExtensions;
336 VkSampleCountFlagBits multisampleCount = VK_SAMPLE_COUNT_1_BIT;
340 uint32_t maxPushConstantsSize = 128u;
341
346 {
350 struct Family
351 {
355 uint32_t index = 0u;
356
360 VkQueue queue = VK_NULL_HANDLE;
361 };
362
367
368 VkDevice vkDevice = VK_NULL_HANDLE;
369
373 VmaAllocator vmaAllocator_ = VK_NULL_HANDLE;
374
376
378 };
379 }
380} // namespace EngineCore
const VkQueue & getPresentQueue() const
Gets the present queue.
VkDeviceSize uniformBufferOffsetAlignment
The uniform buffer offset alignment.
const VkPhysicalDevice & getVkPhysicalDevice() const
Gets vulkan physical device.
void logVulkanRequirements()
Logs vulkan requirements.
VkSampleCountFlagBits multisampleCount
Number of multisamples.
std::vector< XrApiLayerProperties > supportedOpenXRApiLayers
The supported open xr API layers.
const VkDevice getVkDevice() const
Gets the vulkan device.
PFN_xrCreateVulkanDeviceKHR xrCreateVulkanDeviceKHR
std::vector< XrExtensionProperties > supportedOpenXRInstanceExtensions
The supported open xr instance extensions.
const VkQueue & getGraphicsQueue() const
Gets draw queue.
PFN_xrCreateVulkanInstanceKHR xrCreateVulkanInstanceKHR
void createOpenXrInstance(std::optional< std::filesystem::path > customOpenXrRuntimePath=std::optional< std::filesystem::path >())
Queries supported openxr instance extensions, openxr api layers and creates the XrInstance.
VkPhysicalDevice vkPhysicalDevice
The vulkan physical device.
struct Engine::Core::ApplicationContext::QueueFamily queueFamily
XrInstance getXrInstance() const
Gets xr instance.
uint32_t getVkGraphicsQueueFamilyIndex() const
Gets the zero-based index of the vulkan draw queue family.
XrInstance xrInstance
The xr instance.
std::vector< VkExtensionProperties > supportedVulkanInstanceExtensions
The supported vulkan instance extensions.
VmaAllocator getVmaAllocator() const
Gets the VMA allocator for memory management.
bool usesDedicatedTransferQueue() const
Check if the transfer queue index is its own, dedicated queue.
const VkQueue & getTransferQueue() const
Gets the transfer queue.
PFN_xrGetVulkanGraphicsDevice2KHR xrGetVulkanGraphicsDevice2KHR
VkInstance getVkInstance() const
Gets vulkan instance.
void retrieveXrSystemId()
Gets the XrSystemId.
VkDeviceSize getUniformBufferOffsetAlignment() const
Gets uniform buffer offset alignment.
void createResources()
creates all openxr and vulkan resources
static void validateFeature(VkBool32 feature, const std::string &errorMessage)
void pickQueueFamilies(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface, uint32_t &outGraphicsIndex, uint32_t &outPresentIndex, uint32_t &outTransferIndex)
gets the device queues needed
void LoadOpenXrExtensionFunctions()
Loads all dynamically loaded xr functions.
void getVulkanInstanceExtensions()
Gets vulkan instance extensions.
VkSampleCountFlagBits getMultisampleCount() const
Gets multisample count.
void createVulkanInstance()
Creates a vulkan instance.
void checkRequiredEnvironmentBlendModeAvailability()
Gets the environment blend mode.
std::vector< const char * > vulkanInstanceExtensions
(Immutable) the vulkan instance extensions which should get enabled
uint32_t getVkTransferQueueFamilyIndex() const
gets the transfer queue family index
XrSystemId getXrSystemId() const
Gets xr system identifier.
void cleanup()
performs the cleanup by removing all vulkan and openxr pointers
VmaAllocator vmaAllocator_
VMA allocator for efficient GPU memory management.
VkInstance vkInstance
The vulkan instance.
void createDevice(VkSurfaceKHR mirrorSurface)
Creates the vulkan device and all necessary components for it.
std::vector< VkExtensionProperties > supportedVulkanDeviceExtensions
The supported vulkan device extensions.
uint32_t getMaxPushConstantsSize() const
Gets max push constants size in bytes.
XrSystemId xrSystemId
Identifier for the xr device. So the identifier for the headset.
uint32_t maxPushConstantsSize
Maximum push constants size in bytes.
const XrViewConfigurationType getXrViewType() const
Gets xr view type which should be stereo for any typical VR application.
PFN_xrGetVulkanGraphicsRequirements2KHR xrGetVulkanGraphicsRequirements2KHR
std::vector< const char * > vulkanDeviceExtensions
(Immutable) the vulkan device extensions
PFN_xrGetVulkanInstanceExtensionsKHR xrGetVulkanInstanceExtensionsKHR
Cache for reusable Vulkan samplers keyed by address mode.
std::unordered_map< VkSamplerAddressMode, VkSampler > cache_
VkSampler getOrCreate(VkDevice device, VkSamplerAddressMode addressMode)
Gets an existing sampler or creates one for the requested address mode.
void cleanup(VkDevice device)
Destroys all cached samplers.
Environment(const std::string &message)
Environment(const std::stringstream &message)
Exception(const std::string &message)
Definition Exceptions.h:13
MissingVulkanFeature(const std::string &message)
MissingVulkanFeature(const std::stringstream &message)
One Vulkan queue family selection and its queue handle.
VkQueue queue
Queue handle retrieved from the logical device.
Queue family indices and queue handles selected for the Vulkan device.