Overte C++ Documentation
VulkanDevice.h
1 /*
2  * Vulkan device class
3  *
4  * Encapsulates a physical Vulkan device and its logical representation
5  *
6  * Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de
7  *
8  * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
9  */
10 
11 #pragma once
12 
13 #include "VulkanTools.h"
14 #include "Allocation.h"
15 #include "vulkan/vulkan.h"
16 #include <algorithm>
17 #include <assert.h>
18 #include <exception>
19 
20 constexpr int VULKAN_VENDOR_ID_AMD = 0x1002;
21 
22 namespace vks
23 {
24 struct VulkanDevice
25 {
27  VkPhysicalDevice physicalDevice;
29  VkDevice logicalDevice;
31  VkPhysicalDeviceProperties properties;
33  VkPhysicalDeviceFeatures features;
35  VkPhysicalDeviceFeatures enabledFeatures; // TODO: how to initialize this?
37  VkPhysicalDeviceMemoryProperties memoryProperties;
39  std::vector<VkQueueFamilyProperties> queueFamilyProperties;
41  std::vector<std::string> supportedExtensions;
43  VkCommandPool graphicsCommandPool = VK_NULL_HANDLE;
44  VkCommandPool transferCommandPool = VK_NULL_HANDLE;
45  VkCommandPool computeCommandPool = VK_NULL_HANDLE; // VKTODO: this is not assigned yet
47  struct
48  {
49  uint32_t graphics{ VK_QUEUE_FAMILY_IGNORED };
50  uint32_t compute{ VK_QUEUE_FAMILY_IGNORED };
51  uint32_t transfer{ VK_QUEUE_FAMILY_IGNORED };
52  } queueFamilyIndices;
53  operator VkDevice() const
54  {
55  return logicalDevice;
56  };
57  explicit VulkanDevice(VkPhysicalDevice physicalDevice);
58  ~VulkanDevice();
59  uint32_t getMemoryType(uint32_t typeBits, VkMemoryPropertyFlags properties, VkBool32 *memTypeFound = nullptr) const;
60  uint32_t getQueueFamilyIndex(VkQueueFlags queueFlags) const;
61  VkResult createLogicalDevice(VkPhysicalDeviceFeatures enabledFeatures, std::vector<const char *> enabledExtensions, void *pNextChain, bool useSwapChain = true, VkQueueFlags requestedQueueTypes = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT);
62  VkCommandPool createCommandPool(uint32_t queueFamilyIndex, VkCommandPoolCreateFlags createFlags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT);
63  VkCommandBuffer createCommandBuffer(VkCommandPool pool, VkCommandBufferLevel level, bool begin = false);
64  void flushCommandBuffer(VkCommandBuffer commandBuffer, VkQueue queue, VkCommandPool pool, bool free = true);
65  bool extensionSupported(std::string extension);
66 };
67 } // namespace vks