Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
ApplicationContext.h
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:
29 explicit Environment(const std::string& message) : Exception(message) {}
30
34 explicit Environment(const std::stringstream& message) : Exception(message) {}
35 };
36
41 {
42 public:
46 explicit MissingVulkanFeature(const std::string& message) : Exception(message) {}
47
51 explicit MissingVulkanFeature(const std::stringstream& message) : Exception(message) {}
52 };
53 }
54
55 namespace Core
56 {
61 {
62 public:
69 VkSampler getOrCreate( VkDevice device, VkSamplerAddressMode addressMode );
70
75 void cleanup(VkDevice device);
76
77 private:
78 std::unordered_map<VkSamplerAddressMode, VkSampler> cache_;
79 std::mutex mutex_;
80 };
81
86 {
87 public:
91 ApplicationContext() = default;
92
97
102 void createDevice( VkSurfaceKHR mirrorSurface );
103
108 [[nodiscard]] XrInstance getXrInstance() const;
109
114 [[nodiscard]] XrSystemId getXrSystemId() const;
115
120 [[nodiscard]] VkInstance getVkInstance() const;
121
126 [[nodiscard]] VkDevice getVkDevice() const;
127
132 [[nodiscard]] const VkQueue & getPresentQueue() const;
133
138 [[nodiscard]] const VkQueue & getGraphicsQueue() const;
139
144 [[nodiscard]] const VkQueue & getTransferQueue() const;
145
150 [[nodiscard]] VkSampleCountFlagBits getMultisampleCount() const;
151
156 [[nodiscard]] const VkPhysicalDevice & getVkPhysicalDevice() const;
157
162 [[nodiscard]] uint32_t getVkGraphicsQueueFamilyIndex() const;
163
168 [[nodiscard]] uint32_t getVkTransferQueueFamilyIndex() const;
169
174 [[nodiscard]] VkDeviceSize getUniformBufferOffsetAlignment() const;
175
180 [[nodiscard]] uint32_t getMaxPushConstantsSize() const;
181
186 [[nodiscard]] XrViewConfigurationType getXrViewType() const;
187
192
196 void cleanup();
197
206 [[nodiscard]] bool usesDedicatedTransferQueue() const;
207
212 [[nodiscard]] VmaAllocator getVmaAllocator() const;
213
218 [[nodiscard]] SamplerCache& getSamplerCache() { return samplerCache_; }
219
224 [[nodiscard]] bool isMeshShaderSupported() const;
225
230 [[nodiscard]] bool isFbColorSpaceSupported() const { return fbColorSpaceSupported_; }
231
232 private:
233 static void validateFeature( VkBool32 feature, const std::string & errorMessage );
234
246 VkPhysicalDevice physicalDevice,
247 VkSurfaceKHR surface,
248 uint32_t & outGraphicsIndex,
249 uint32_t & outPresentIndex,
250 uint32_t & outTransferIndex
251 );
252
261 std::optional<std::filesystem::path> customOpenXrRuntimePath =
262 std::optional<std::filesystem::path>()
263 );
264
269 PFN_xrGetVulkanGraphicsDevice2KHR xrGetVulkanGraphicsDevice2KHR = nullptr;
270 PFN_xrGetVulkanGraphicsRequirements2KHR xrGetVulkanGraphicsRequirements2KHR = nullptr;
271
272 PFN_xrCreateVulkanInstanceKHR xrCreateVulkanInstanceKHR = nullptr;
273 PFN_xrCreateVulkanDeviceKHR xrCreateVulkanDeviceKHR = nullptr;
274
275#ifdef IS_IN_DEBUG
276 PFN_xrCreateDebugUtilsMessengerEXT xrCreateDebugUtilsMessengerEXT = nullptr;
277 PFN_xrDestroyDebugUtilsMessengerEXT xrDestroyDebugUtilsMessengerEXT = nullptr;
278
279 void createOpenXrDebugMessenger();
280#endif
281
286
291
296
301
306
310 std::vector<XrExtensionProperties> supportedOpenXRInstanceExtensions;
311
315 std::vector<XrApiLayerProperties> supportedOpenXRApiLayers;
316
320 XrInstance xrInstance = XR_NULL_HANDLE;
321
322#ifdef IS_IN_DEBUG
326 XrDebugUtilsMessengerEXT xrDebugMessenger = XR_NULL_HANDLE;
327#endif
328
332 XrSystemId xrSystemId = XR_NULL_SYSTEM_ID;
333
337 std::vector<VkExtensionProperties> supportedVulkanInstanceExtensions;
338
342 std::vector<const char *> vulkanInstanceExtensions;
343
347 VkInstance vkInstance = VK_NULL_HANDLE;
348
352 VkPhysicalDevice vkPhysicalDevice = VK_NULL_HANDLE;
353
357 std::vector<VkExtensionProperties> supportedVulkanDeviceExtensions;
361 std::vector<const char *> vulkanDeviceExtensions;
369 VkSampleCountFlagBits multisampleCount = VK_SAMPLE_COUNT_1_BIT;
373 uint32_t maxPushConstantsSize = 128u;
374
379 {
383 struct Family
384 {
388 uint32_t index = 0u;
389
393 VkQueue queue = VK_NULL_HANDLE;
394 };
395
400
401 VkDevice vkDevice = VK_NULL_HANDLE;
402
406 VmaAllocator vmaAllocator_ = VK_NULL_HANDLE;
407
409
411 };
412 }
413} // 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.
PFN_xrCreateVulkanDeviceKHR xrCreateVulkanDeviceKHR
XrViewConfigurationType getXrViewType() const
Gets xr view type which should be stereo for any typical VR application.
std::vector< XrExtensionProperties > supportedOpenXRInstanceExtensions
The supported open xr instance extensions.
const VkQueue & getGraphicsQueue() const
Gets draw queue.
SamplerCache & getSamplerCache()
Gets the Vulkan sampler cache owned by the context.
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.
ApplicationContext()=default
Creates an empty application context.
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.
bool isFbColorSpaceSupported() const
Checks whether the framebuffer color space required by the engine is supported.
bool isMeshShaderSupported() const
Checks whether mesh shaders are supported by the selected device.
~ApplicationContext()
Destroys the context object without performing Vulkan or OpenXR cleanup.
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.
VkDevice getVkDevice() const
Gets the vulkan device.
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.
PFN_xrGetVulkanGraphicsRequirements2KHR xrGetVulkanGraphicsRequirements2KHR
std::vector< const char * > vulkanDeviceExtensions
(Immutable) the vulkan device extensions
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)
Creates an environment exception from message text.
Environment(const std::stringstream &message)
Creates an environment exception from a formatted message stream.
Exception(const std::string &message)
Creates an engine exception from message text.
Definition Exceptions.h:16
MissingVulkanFeature(const std::string &message)
Creates a missing Vulkan feature exception from message text.
MissingVulkanFeature(const std::stringstream &message)
Creates a missing Vulkan feature exception from a formatted message stream.
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.