1 | #ifndef _VULKAN_UTILS_H
|
---|
2 | #define _VULKAN_UTILS_H
|
---|
3 |
|
---|
4 | #include <optional>
|
---|
5 | #include <string>
|
---|
6 | #include <vector>
|
---|
7 |
|
---|
8 | #include <vulkan/vulkan.h>
|
---|
9 |
|
---|
10 | // TODO: Ideally, vulkan-utils should not have things speciic to windowing apis (glfw, sdl, sfml, etc.).
|
---|
11 | // Check what these inclydes are for and if that functionality can be moved
|
---|
12 | #include <SDL2/SDL.h>
|
---|
13 | #include <SDL2/SDL_vulkan.h>
|
---|
14 |
|
---|
15 | using namespace std;
|
---|
16 |
|
---|
17 | struct QueueFamilyIndices {
|
---|
18 | optional<uint32_t> graphicsFamily;
|
---|
19 | optional<uint32_t> presentFamily;
|
---|
20 |
|
---|
21 | bool isComplete() {
|
---|
22 | return graphicsFamily.has_value() && presentFamily.has_value();
|
---|
23 | }
|
---|
24 | };
|
---|
25 |
|
---|
26 | struct VulkanImage {
|
---|
27 | VkImage image;
|
---|
28 | VkDeviceMemory imageMemory;
|
---|
29 | VkImageView imageView;
|
---|
30 | };
|
---|
31 |
|
---|
32 | class VulkanUtils {
|
---|
33 | public:
|
---|
34 | static bool checkValidationLayerSupport(const vector<const char*> &validationLayers);
|
---|
35 |
|
---|
36 | static VkResult createDebugUtilsMessengerEXT(VkInstance instance,
|
---|
37 | const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
|
---|
38 | const VkAllocationCallbacks* pAllocator,
|
---|
39 | VkDebugUtilsMessengerEXT* pDebugMessenger);
|
---|
40 |
|
---|
41 | static void destroyDebugUtilsMessengerEXT(VkInstance instance,
|
---|
42 | VkDebugUtilsMessengerEXT debugMessenger,
|
---|
43 | const VkAllocationCallbacks* pAllocator);
|
---|
44 |
|
---|
45 | static QueueFamilyIndices findQueueFamilies(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface);
|
---|
46 | static bool checkDeviceExtensionSupport(VkPhysicalDevice physicalDevice,
|
---|
47 | const vector<const char*>& deviceExtensions);
|
---|
48 | static VkSurfaceCapabilitiesKHR querySwapChainCapabilities(VkPhysicalDevice physicalDevice,
|
---|
49 | VkSurfaceKHR surface);
|
---|
50 | static vector<VkSurfaceFormatKHR> querySwapChainFormats(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface);
|
---|
51 | static vector<VkPresentModeKHR> querySwapChainPresentModes(VkPhysicalDevice physicalDevice,
|
---|
52 | VkSurfaceKHR surface);
|
---|
53 | static VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats,
|
---|
54 | const vector<VkFormat>& requestedFormats, VkColorSpaceKHR requestedColorSpace);
|
---|
55 | static VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes,
|
---|
56 | const vector<VkPresentModeKHR>& requestedPresentModes);
|
---|
57 | static VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, int width, int height);
|
---|
58 | static VkImageView createImageView(VkDevice device, VkImage image, VkFormat format,
|
---|
59 | VkImageAspectFlags aspectFlags);
|
---|
60 | static VkFormat findSupportedFormat(VkPhysicalDevice physicalDevice, const vector<VkFormat>& candidates,
|
---|
61 | VkImageTiling tiling, VkFormatFeatureFlags features);
|
---|
62 | static void createBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkDeviceSize size,
|
---|
63 | VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer,
|
---|
64 | VkDeviceMemory& bufferMemory);
|
---|
65 | static uint32_t findMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter,
|
---|
66 | VkMemoryPropertyFlags properties);
|
---|
67 |
|
---|
68 | static void createVulkanImageFromFile(VkDevice device, VkPhysicalDevice physicalDevice,
|
---|
69 | VkCommandPool commandPool, string filename, VulkanImage& image, VkQueue graphicsQueue);
|
---|
70 | static void createVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice,
|
---|
71 | SDL_Texture* texture, VulkanImage& image);
|
---|
72 | static void populateVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice,
|
---|
73 | VkCommandPool commandPool, SDL_Texture* texture, SDL_Renderer* renderer, VulkanImage& image,
|
---|
74 | VkQueue graphicsQueue);
|
---|
75 | static void createDepthImage(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
|
---|
76 | VkFormat depthFormat, VkExtent2D extent, VulkanImage& image, VkQueue graphicsQueue);
|
---|
77 | static void createImage(VkDevice device, VkPhysicalDevice physicalDevice, uint32_t width, uint32_t height,
|
---|
78 | VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties,
|
---|
79 | VulkanImage& image);
|
---|
80 |
|
---|
81 | static void transitionImageLayout(VkDevice device, VkCommandPool commandPool, VkImage image,
|
---|
82 | VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout, VkQueue graphicsQueue);
|
---|
83 | static VkCommandBuffer beginSingleTimeCommands(VkDevice device, VkCommandPool commandPool);
|
---|
84 | static void endSingleTimeCommands(VkDevice device, VkCommandPool commandPool,
|
---|
85 | VkCommandBuffer commandBuffer, VkQueue graphicsQueue);
|
---|
86 | static void copyBufferToImage(VkDevice device, VkCommandPool commandPool, VkBuffer buffer, VkImage image,
|
---|
87 | uint32_t width, uint32_t height, VkQueue graphicsQueue);
|
---|
88 |
|
---|
89 | template<class DataType>
|
---|
90 | static void copyDataToBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
|
---|
91 | const vector<DataType>& srcData, VkBuffer dstBuffer, size_t dstVertexOffset, VkQueue graphicsQueue);
|
---|
92 |
|
---|
93 | static void copyBuffer(VkDevice device, VkCommandPool commandPool, VkBuffer srcBuffer, VkBuffer dstBuffer,
|
---|
94 | VkDeviceSize srcOffset, VkDeviceSize dstOffset, VkDeviceSize size, VkQueue graphicsQueue);
|
---|
95 |
|
---|
96 | template<class DataType>
|
---|
97 | static void copyDataToMemory(VkDevice device, VkDeviceMemory bufferMemory, VkDeviceSize offset,
|
---|
98 | const DataType& srcData);
|
---|
99 |
|
---|
100 | static bool hasStencilComponent(VkFormat format);
|
---|
101 |
|
---|
102 | static void destroyVulkanImage(VkDevice& device, VulkanImage& image);
|
---|
103 | };
|
---|
104 |
|
---|
105 | template<class DataType>
|
---|
106 | void VulkanUtils::copyDataToBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
|
---|
107 | const vector<DataType>& srcData, VkBuffer dstBuffer, size_t dstVertexOffset, VkQueue graphicsQueue) {
|
---|
108 | VkDeviceSize srcDataSize = srcData.size() * sizeof(DataType);
|
---|
109 |
|
---|
110 | VkBuffer stagingBuffer;
|
---|
111 | VkDeviceMemory stagingBufferMemory;
|
---|
112 | createBuffer(device, physicalDevice, srcDataSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
---|
113 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
---|
114 | stagingBuffer, stagingBufferMemory);
|
---|
115 |
|
---|
116 | void* data;
|
---|
117 | vkMapMemory(device, stagingBufferMemory, 0, srcDataSize, 0, &data);
|
---|
118 | memcpy(data, srcData.data(), (size_t)srcDataSize);
|
---|
119 | vkUnmapMemory(device, stagingBufferMemory);
|
---|
120 |
|
---|
121 | copyBuffer(device, commandPool, stagingBuffer, dstBuffer, 0, dstVertexOffset * sizeof(DataType), srcDataSize,
|
---|
122 | graphicsQueue);
|
---|
123 |
|
---|
124 | vkDestroyBuffer(device, stagingBuffer, nullptr);
|
---|
125 | vkFreeMemory(device, stagingBufferMemory, nullptr);
|
---|
126 | }
|
---|
127 |
|
---|
128 | template<class DataType>
|
---|
129 | void VulkanUtils::copyDataToMemory(VkDevice device, VkDeviceMemory bufferMemory, VkDeviceSize offset,
|
---|
130 | const DataType& srcData) {
|
---|
131 | void* data;
|
---|
132 |
|
---|
133 | vkMapMemory(device, bufferMemory, offset * sizeof(DataType), sizeof(DataType), 0, &data);
|
---|
134 | memcpy(data, &srcData, sizeof(DataType));
|
---|
135 | vkUnmapMemory(device, bufferMemory);
|
---|
136 | }
|
---|
137 |
|
---|
138 | #endif // _VULKAN_UTILS_H
|
---|