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 {
44 {
45 public:
46 VkSampler getOrCreate(VkDevice device, VkSamplerAddressMode addressMode);
47 void cleanup(VkDevice device);
48 private:
49 std::unordered_map<VkSamplerAddressMode, VkSampler> cache_;
50 std::mutex mutex_;
51 };
52
57 {
58 public:
59 ApplicationContext() = default;
61
67 void createDevice( VkSurfaceKHR mirrorSurface );
68
74 [[nodiscard]] XrInstance getXrInstance() const;
75
81 [[nodiscard]] XrSystemId getXrSystemId() const;
82
88 [[nodiscard]] VkInstance getVkInstance() const;
89
98 [[nodiscard]] const VkDevice getVkDevice() const;
99
108 [[nodiscard]] const VkQueue & getPresentQueue() const;
109
118 [[nodiscard]] const VkQueue & getGraphicsQueue() const;
119
128 [[nodiscard]] const VkQueue & getTransferQueue() const;
129
138 [[nodiscard]] VkSampleCountFlagBits getMultisampleCount() const;
139
148 [[nodiscard]] const VkPhysicalDevice & getVkPhysicalDevice() const;
149
158 [[nodiscard]] uint32_t getVkGraphicsQueueFamilyIndex() const;
159
168 [[nodiscard]] uint32_t getVkTransferQueueFamilyIndex() const;
169
178 [[nodiscard]] VkDeviceSize getUniformBufferOffsetAlignment() const;
179
188 [[nodiscard]] uint32_t getMaxPushConstantsSize() const;
189
198 [[nodiscard]] const XrViewConfigurationType getXrViewType() const;
199
204
208 void cleanup();
209
218 [[nodiscard]] bool usesDedicatedTransferQueue() const;
219
228 [[nodiscard]] VmaAllocator getVmaAllocator() const;
229
230 [[nodiscard]] SamplerCache& getSamplerCache() { return samplerCache_; }
231
232 [[nodiscard]] bool isMeshShaderSupported() const;
233 [[nodiscard]] bool isFbColorSpaceSupported() const { return fbColorSpaceSupported_; }
234
235 private:
236 static void validateFeature( VkBool32 feature, const std::string & errorMessage );
237
252 VkPhysicalDevice physicalDevice,
253 VkSurfaceKHR surface,
254 uint32_t & outGraphicsIndex,
255 uint32_t & outPresentIndex,
256 uint32_t & outTransferIndex
257 );
258
271 std::optional<std::filesystem::path> customOpenXrRuntimePath =
272 std::optional<std::filesystem::path>()
273 );
274
279 PFN_xrGetVulkanInstanceExtensionsKHR xrGetVulkanInstanceExtensionsKHR = nullptr;
280 PFN_xrGetVulkanGraphicsDevice2KHR xrGetVulkanGraphicsDevice2KHR = nullptr;
281 PFN_xrGetVulkanGraphicsRequirements2KHR xrGetVulkanGraphicsRequirements2KHR = nullptr;
282
283 PFN_xrCreateVulkanInstanceKHR xrCreateVulkanInstanceKHR = nullptr;
284 PFN_xrCreateVulkanDeviceKHR xrCreateVulkanDeviceKHR = nullptr;
285
286#ifdef IS_IN_DEBUG
287 PFN_xrCreateDebugUtilsMessengerEXT xrCreateDebugUtilsMessengerEXT = nullptr;
288 PFN_xrDestroyDebugUtilsMessengerEXT xrDestroyDebugUtilsMessengerEXT = nullptr;
289
290 void createOpenXrDebugMessenger();
291#endif
292
300
308
316
324
332
336 std::vector<XrExtensionProperties> supportedOpenXRInstanceExtensions;
337
341 std::vector<XrApiLayerProperties> supportedOpenXRApiLayers;
342
346 XrInstance xrInstance = XR_NULL_HANDLE;
347
348#ifdef IS_IN_DEBUG
352 XrDebugUtilsMessengerEXT xrDebugMessenger = XR_NULL_HANDLE;
353#endif
354
358 XrSystemId xrSystemId = XR_NULL_SYSTEM_ID;
359
363 std::vector<VkExtensionProperties> supportedVulkanInstanceExtensions;
364
368 std::vector<const char *> vulkanInstanceExtensions;
369
373 VkInstance vkInstance = VK_NULL_HANDLE;
374
378 VkPhysicalDevice vkPhysicalDevice = VK_NULL_HANDLE;
379
383 std::vector<VkExtensionProperties> supportedVulkanDeviceExtensions;
387 std::vector<const char *> vulkanDeviceExtensions;
395 VkSampleCountFlagBits multisampleCount = VK_SAMPLE_COUNT_1_BIT;
399 uint32_t maxPushConstantsSize = 128u;
400
402 {
403 struct Family
404 {
405 uint32_t index = 0u;
406 VkQueue queue = VK_NULL_HANDLE;
407 };
408
413
414 VkDevice vkDevice = VK_NULL_HANDLE;
415
419 VmaAllocator vmaAllocator_ = VK_NULL_HANDLE;
420
422
424 };
425 }
426} // 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
std::unordered_map< VkSamplerAddressMode, VkSampler > cache_
VkSampler getOrCreate(VkDevice device, VkSamplerAddressMode addressMode)
void cleanup(VkDevice device)
Environment(const std::string &message)
Environment(const std::stringstream &message)
Exception(const std::string &message)
Definition Exceptions.h:10
MissingVulkanFeature(const std::string &message)
MissingVulkanFeature(const std::stringstream &message)