device/instance.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | /// @file instance.h | ||
| 3 | /// @brief The Vulkan instance: the loader, optional validation and the messages it reports. | ||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include "ernie/device/vulkan.h" | ||
| 7 | |||
| 8 | #include <mutex> | ||
| 9 | #include <string> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | namespace ernie { | ||
| 13 | |||
| 14 | 82 | struct InstanceOptions { | |
| 15 | /// The Khronos validation layer with synchronization validation, when installed. | ||
| 16 | bool validation = false; | ||
| 17 | /// Extensions a window system needs. | ||
| 18 | std::vector<std::string> extensions; | ||
| 19 | }; | ||
| 20 | |||
| 21 | /// @brief A Vulkan 1.3 instance. Each owns its messages: two contexts in one process keep theirs | ||
| 22 | /// apart. | ||
| 23 | class Instance { | ||
| 24 | public: | ||
| 25 | /// @throws DeviceError when no Vulkan loader or no Vulkan 1.3 driver is installed. | ||
| 26 | explicit Instance(const InstanceOptions& options = {}); | ||
| 27 | ~Instance(); | ||
| 28 | Instance(const Instance&) = delete; | ||
| 29 | Instance& operator=(const Instance&) = delete; | ||
| 30 | |||
| 31 | 79 | VkInstance Handle() const { | |
| 32 | 79 | return instance_; | |
| 33 | } | ||
| 34 | std::vector<VkPhysicalDevice> PhysicalDevices() const; | ||
| 35 | /// @return Whether the validation layer was found and enabled. | ||
| 36 | bool Validating() const { | ||
| 37 | return messenger_ != VK_NULL_HANDLE; | ||
| 38 | } | ||
| 39 | /// @return The validation layer's warnings and errors since the last call. | ||
| 40 | std::vector<std::string> TakeMessages(); | ||
| 41 | /// @brief Names a device's object, when validating; otherwise does nothing. | ||
| 42 | void Name(VkDevice device, VkObjectType type, std::uint64_t handle, | ||
| 43 | std::string_view name) const; | ||
| 44 | /// @brief Opens a named region of commands for captures and validation messages, when | ||
| 45 | /// validating; otherwise does nothing. | ||
| 46 | void BeginLabel(VkCommandBuffer commands, const char* name) const; | ||
| 47 | void EndLabel(VkCommandBuffer commands) const; | ||
| 48 | |||
| 49 | private: | ||
| 50 | static VKAPI_ATTR VkBool32 VKAPI_CALL Report(VkDebugUtilsMessageSeverityFlagBitsEXT severity, | ||
| 51 | VkDebugUtilsMessageTypeFlagsEXT types, | ||
| 52 | const VkDebugUtilsMessengerCallbackDataEXT* data, | ||
| 53 | void* self); | ||
| 54 | |||
| 55 | VkInstance instance_ = VK_NULL_HANDLE; | ||
| 56 | VkDebugUtilsMessengerEXT messenger_ = VK_NULL_HANDLE; | ||
| 57 | // This instance's own: volk's globals hold whichever instance loaded last, and only those | ||
| 58 | // with validation have these. | ||
| 59 | PFN_vkDestroyDebugUtilsMessengerEXT destroyMessenger_ = nullptr; | ||
| 60 | PFN_vkSetDebugUtilsObjectNameEXT setName_ = nullptr; | ||
| 61 | PFN_vkCmdBeginDebugUtilsLabelEXT beginLabel_ = nullptr; | ||
| 62 | PFN_vkCmdEndDebugUtilsLabelEXT endLabel_ = nullptr; | ||
| 63 | std::mutex mutex_; | ||
| 64 | std::vector<std::string> messages_; | ||
| 65 | }; | ||
| 66 | |||
| 67 | } // namespace ernie | ||
| 68 |