1 | #ifndef _VULKAN_UTILS_H
|
---|
2 | #define _VULKAN_UTILS_H
|
---|
3 |
|
---|
4 | #include <optional>
|
---|
5 | #include <sstream>
|
---|
6 | #include <stdexcept>
|
---|
7 | #include <string>
|
---|
8 | #include <vector>
|
---|
9 |
|
---|
10 | #include <vulkan/vulkan.h>
|
---|
11 |
|
---|
12 | // TODO: Ideally, vulkan-utils should not have things speciic to windowing apis (glfw, sdl, sfml, etc.).
|
---|
13 | // Check what these inclydes are for and if that functionality can be moved
|
---|
14 | #include <SDL2/SDL.h>
|
---|
15 | #include <SDL2/SDL_vulkan.h>
|
---|
16 |
|
---|
17 | using namespace std;
|
---|
18 |
|
---|
19 | struct QueueFamilyIndices {
|
---|
20 | optional<uint32_t> graphicsFamily;
|
---|
21 | optional<uint32_t> presentFamily;
|
---|
22 |
|
---|
23 | bool isComplete() {
|
---|
24 | return graphicsFamily.has_value() && presentFamily.has_value();
|
---|
25 | }
|
---|
26 | };
|
---|
27 |
|
---|
28 | struct VulkanImage {
|
---|
29 | VkImage image;
|
---|
30 | VkDeviceMemory imageMemory;
|
---|
31 | VkImageView imageView;
|
---|
32 | };
|
---|
33 |
|
---|
34 | class VulkanUtils {
|
---|
35 | public:
|
---|
36 | static string resultString(VkResult result);
|
---|
37 |
|
---|
38 | static bool checkValidationLayerSupport(const vector<const char*> &validationLayers);
|
---|
39 |
|
---|
40 | static VkResult createDebugUtilsMessengerEXT(VkInstance instance,
|
---|
41 | const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
|
---|
42 | const VkAllocationCallbacks* pAllocator,
|
---|
43 | VkDebugUtilsMessengerEXT* pDebugMessenger);
|
---|
44 |
|
---|
45 | static void destroyDebugUtilsMessengerEXT(VkInstance instance,
|
---|
46 | VkDebugUtilsMessengerEXT debugMessenger,
|
---|
47 | const VkAllocationCallbacks* pAllocator);
|
---|
48 |
|
---|
49 | static QueueFamilyIndices findQueueFamilies(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface);
|
---|
50 | static bool checkDeviceExtensionSupport(VkPhysicalDevice physicalDevice,
|
---|
51 | const vector<const char*>& deviceExtensions);
|
---|
52 | static VkSurfaceCapabilitiesKHR querySwapChainCapabilities(VkPhysicalDevice physicalDevice,
|
---|
53 | VkSurfaceKHR surface);
|
---|
54 | static vector<VkSurfaceFormatKHR> querySwapChainFormats(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface);
|
---|
55 | static vector<VkPresentModeKHR> querySwapChainPresentModes(VkPhysicalDevice physicalDevice,
|
---|
56 | VkSurfaceKHR surface);
|
---|
57 | static VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats,
|
---|
58 | const vector<VkFormat>& requestedFormats, VkColorSpaceKHR requestedColorSpace);
|
---|
59 | static VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes,
|
---|
60 | const vector<VkPresentModeKHR>& requestedPresentModes);
|
---|
61 | static VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, int width, int height);
|
---|
62 | static VkImageView createImageView(VkDevice device, VkImage image, VkFormat format,
|
---|
63 | VkImageAspectFlags aspectFlags);
|
---|
64 | static VkFormat findSupportedFormat(VkPhysicalDevice physicalDevice, const vector<VkFormat>& candidates,
|
---|
65 | VkImageTiling tiling, VkFormatFeatureFlags features);
|
---|
66 | static void createBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkDeviceSize size,
|
---|
67 | VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer,
|
---|
68 | VkDeviceMemory& bufferMemory);
|
---|
69 | static uint32_t findMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter,
|
---|
70 | VkMemoryPropertyFlags properties);
|
---|
71 |
|
---|
72 | static void createVulkanImageFromFile(VkDevice device, VkPhysicalDevice physicalDevice,
|
---|
73 | VkCommandPool commandPool, string filename, VulkanImage& image,
|
---|
74 | VkQueue graphicsQueue);
|
---|
75 | static void createVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice,
|
---|
76 | SDL_Texture* texture, VulkanImage& image);
|
---|
77 | static void populateVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice,
|
---|
78 | VkCommandPool commandPool, SDL_Texture* texture,
|
---|
79 | SDL_Renderer* renderer, VulkanImage& image, VkQueue graphicsQueue);
|
---|
80 | static void createDepthImage(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
|
---|
81 | VkFormat depthFormat, VkExtent2D extent, VulkanImage& image, VkQueue graphicsQueue);
|
---|
82 | static void createImage(VkDevice device, VkPhysicalDevice physicalDevice, uint32_t width, uint32_t height,
|
---|
83 | VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage,
|
---|
84 | VkMemoryPropertyFlags properties, VulkanImage& image);
|
---|
85 |
|
---|
86 | static void transitionImageLayout(VkDevice device, VkCommandPool commandPool, VkImage image,
|
---|
87 | VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout,
|
---|
88 | VkQueue graphicsQueue);
|
---|
89 | static VkCommandBuffer beginSingleTimeCommands(VkDevice device, VkCommandPool commandPool);
|
---|
90 | static void endSingleTimeCommands(VkDevice device, VkCommandPool commandPool,
|
---|
91 | VkCommandBuffer commandBuffer, VkQueue graphicsQueue);
|
---|
92 | static void copyBufferToImage(VkDevice device, VkCommandPool commandPool, VkBuffer buffer, VkImage image,
|
---|
93 | uint32_t width, uint32_t height, VkQueue graphicsQueue);
|
---|
94 |
|
---|
95 | template<class DataType>
|
---|
96 | static void copyDataToBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
|
---|
97 | const vector<DataType>& srcData, VkBuffer dstBuffer, size_t dstVertexOffset,
|
---|
98 | VkQueue graphicsQueue);
|
---|
99 |
|
---|
100 | static void copyBuffer(VkDevice device, VkCommandPool commandPool, VkBuffer srcBuffer, VkBuffer dstBuffer,
|
---|
101 | VkDeviceSize srcOffset, VkDeviceSize dstOffset, VkDeviceSize size,
|
---|
102 | VkQueue graphicsQueue);
|
---|
103 |
|
---|
104 | template<class DataType>
|
---|
105 | static void copyDataToMemory(VkDevice device, const DataType& srcData, VkDeviceMemory bufferMemory,
|
---|
106 | VkDeviceSize offset);
|
---|
107 |
|
---|
108 | template<class DataType>
|
---|
109 | static void copyDataToMemory(VkDevice device, const DataType& srcData, VkDeviceMemory bufferMemory,
|
---|
110 | VkDeviceSize offset, VkDeviceSize size);
|
---|
111 |
|
---|
112 | static bool hasStencilComponent(VkFormat format);
|
---|
113 |
|
---|
114 | static void destroyVulkanImage(VkDevice& device, VulkanImage& image);
|
---|
115 | };
|
---|
116 |
|
---|
117 | template<class DataType>
|
---|
118 | void VulkanUtils::copyDataToBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
|
---|
119 | const vector<DataType>& srcData, VkBuffer dstBuffer, size_t dstVertexOffset,
|
---|
120 | VkQueue graphicsQueue) {
|
---|
121 | VkDeviceSize srcDataSize = srcData.size() * sizeof(DataType);
|
---|
122 |
|
---|
123 | VkBuffer stagingBuffer;
|
---|
124 | VkDeviceMemory stagingBufferMemory;
|
---|
125 | createBuffer(device, physicalDevice, srcDataSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
---|
126 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
---|
127 | stagingBuffer, stagingBufferMemory);
|
---|
128 |
|
---|
129 | void* data;
|
---|
130 | vkMapMemory(device, stagingBufferMemory, 0, srcDataSize, 0, &data);
|
---|
131 | memcpy(data, srcData.data(), (size_t)srcDataSize);
|
---|
132 | vkUnmapMemory(device, stagingBufferMemory);
|
---|
133 |
|
---|
134 | copyBuffer(device, commandPool, stagingBuffer, dstBuffer, 0, dstVertexOffset * sizeof(DataType), srcDataSize,
|
---|
135 | graphicsQueue);
|
---|
136 |
|
---|
137 | vkDestroyBuffer(device, stagingBuffer, nullptr);
|
---|
138 | vkFreeMemory(device, stagingBufferMemory, nullptr);
|
---|
139 | }
|
---|
140 |
|
---|
141 | template<class DataType>
|
---|
142 | void VulkanUtils::copyDataToMemory(VkDevice device, const DataType& srcData, VkDeviceMemory bufferMemory,
|
---|
143 | VkDeviceSize offset) {
|
---|
144 | copyDataToMemory(device, srcData, bufferMemory, offset, sizeof(DataType));
|
---|
145 | }
|
---|
146 |
|
---|
147 | // TODO: This would be used when the GPU memory is host-coherent. If it it not, I also need to use vkFlushMappedMemoryRanges
|
---|
148 | // I should create a variant that supports non-coherent memory
|
---|
149 | template<class DataType>
|
---|
150 | void VulkanUtils::copyDataToMemory(VkDevice device, const DataType& srcData, VkDeviceMemory bufferMemory,
|
---|
151 | VkDeviceSize offset, VkDeviceSize size) {
|
---|
152 | void* data;
|
---|
153 |
|
---|
154 | vkMapMemory(device, bufferMemory, offset, size, 0, &data);
|
---|
155 | memcpy(data, &srcData, size);
|
---|
156 | vkUnmapMemory(device, bufferMemory);
|
---|
157 | }
|
---|
158 |
|
---|
159 | // TODO: Use this in vulkan-utils itself as well
|
---|
160 | #define VKUTIL_CHECK_RESULT(f, msg) { \
|
---|
161 | VkResult res = (f); \
|
---|
162 | \
|
---|
163 | if (res != VK_SUCCESS) { \
|
---|
164 | ostringstream oss; \
|
---|
165 | oss << msg << " VkResult is \"" << VulkanUtils::resultString(res) << "\" in " << __FILE__ << " at line " << __LINE__;\
|
---|
166 | \
|
---|
167 | if (res < 0) { \
|
---|
168 | throw runtime_error("Fatal: " + oss.str()); \
|
---|
169 | } else { \
|
---|
170 | cerr << oss.str(); \
|
---|
171 | } \
|
---|
172 | } \
|
---|
173 | }
|
---|
174 |
|
---|
175 | #endif // _VULKAN_UTILS_H
|
---|