[cb01aff] | 1 | #ifndef _VULKAN_UTILS_H
|
---|
| 2 | #define _VULKAN_UTILS_H
|
---|
| 3 |
|
---|
[90a424f] | 4 | #include <optional>
|
---|
[b794178] | 5 | #include <string>
|
---|
[cb01aff] | 6 | #include <vector>
|
---|
| 7 |
|
---|
| 8 | #include <vulkan/vulkan.h>
|
---|
| 9 |
|
---|
[b794178] | 10 | #include <SDL2/SDL.h>
|
---|
| 11 | #include <SDL2/SDL_image.h>
|
---|
| 12 | #include <SDL2/SDL_vulkan.h>
|
---|
| 13 |
|
---|
[cb01aff] | 14 | using namespace std;
|
---|
| 15 |
|
---|
[90a424f] | 16 | struct QueueFamilyIndices {
|
---|
| 17 | optional<uint32_t> graphicsFamily;
|
---|
| 18 | optional<uint32_t> presentFamily;
|
---|
| 19 |
|
---|
| 20 | bool isComplete() {
|
---|
| 21 | return graphicsFamily.has_value() && presentFamily.has_value();
|
---|
| 22 | }
|
---|
| 23 | };
|
---|
| 24 |
|
---|
| 25 | struct SwapChainSupportDetails {
|
---|
| 26 | VkSurfaceCapabilitiesKHR capabilities;
|
---|
| 27 | vector<VkSurfaceFormatKHR> formats;
|
---|
| 28 | vector<VkPresentModeKHR> presentModes;
|
---|
| 29 | };
|
---|
| 30 |
|
---|
[b794178] | 31 | struct VulkanImage {
|
---|
| 32 | VkImage image;
|
---|
| 33 | VkDeviceMemory imageMemory;
|
---|
| 34 | VkImageView imageView;
|
---|
| 35 | };
|
---|
| 36 |
|
---|
[cb01aff] | 37 | class VulkanUtils {
|
---|
| 38 | public:
|
---|
| 39 | static bool checkValidationLayerSupport(const vector<const char*> &validationLayers);
|
---|
| 40 |
|
---|
| 41 | static VkResult createDebugUtilsMessengerEXT(VkInstance instance,
|
---|
| 42 | const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
|
---|
| 43 | const VkAllocationCallbacks* pAllocator,
|
---|
| 44 | VkDebugUtilsMessengerEXT* pDebugMessenger);
|
---|
| 45 |
|
---|
| 46 | static void destroyDebugUtilsMessengerEXT(VkInstance instance,
|
---|
| 47 | VkDebugUtilsMessengerEXT debugMessenger,
|
---|
| 48 | const VkAllocationCallbacks* pAllocator);
|
---|
[90a424f] | 49 |
|
---|
[fe5c3ba] | 50 | static QueueFamilyIndices findQueueFamilies(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface);
|
---|
[b794178] | 51 | static bool checkDeviceExtensionSupport(VkPhysicalDevice physicalDevice,
|
---|
| 52 | const vector<const char*>& deviceExtensions);
|
---|
| 53 | static SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice physicalDevice,
|
---|
| 54 | VkSurfaceKHR surface);
|
---|
[502bd0b] | 55 | static VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats);
|
---|
| 56 | static VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes);
|
---|
| 57 | static VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, int width, int height);
|
---|
[b794178] | 58 | static VkImageView createImageView(VkDevice device, VkImage image, VkFormat format,
|
---|
| 59 | VkImageAspectFlags aspectFlags);
|
---|
[6fc24c7] | 60 | static VkFormat findSupportedFormat(VkPhysicalDevice physicalDevice, const vector<VkFormat>& candidates,
|
---|
| 61 | VkImageTiling tiling, VkFormatFeatureFlags features);
|
---|
[b794178] | 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);
|
---|
[d2d9286] | 72 | static void populateVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice,
|
---|
| 73 | VkCommandPool commandPool, SDL_Texture* texture, SDL_Renderer* renderer, VulkanImage& image,
|
---|
| 74 | VkQueue graphicsQueue);
|
---|
[603b5bc] | 75 | static void createDepthImage(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
|
---|
| 76 | VkFormat depthFormat, VkExtent2D extent, VulkanImage& image, VkQueue graphicsQueue);
|
---|
[b794178] | 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 |
|
---|
[e3bef3a] | 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 |
|
---|
[87c8f1a] | 93 | static void copyBuffer(VkDevice device, VkCommandPool commandPool, VkBuffer srcBuffer, VkBuffer dstBuffer,
|
---|
| 94 | VkDeviceSize srcOffset, VkDeviceSize dstOffset, VkDeviceSize size, VkQueue graphicsQueue);
|
---|
| 95 |
|
---|
[8e02b6b] | 96 | template<class DataType>
|
---|
| 97 | static void copyDataToMemory(VkDevice device, VkDeviceMemory bufferMemory, const DataType& srcData);
|
---|
| 98 |
|
---|
[b794178] | 99 | static bool hasStencilComponent(VkFormat format);
|
---|
[e83b155] | 100 |
|
---|
| 101 | static void destroyVulkanImage(VkDevice& device, VulkanImage& image);
|
---|
[cb01aff] | 102 | };
|
---|
| 103 |
|
---|
[e3bef3a] | 104 | template<class DataType>
|
---|
| 105 | void VulkanUtils::copyDataToBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
|
---|
| 106 | const vector<DataType>& srcData, VkBuffer dstBuffer, size_t dstVertexOffset, VkQueue graphicsQueue) {
|
---|
| 107 | VkDeviceSize srcDataSize = srcData.size() * sizeof(DataType);
|
---|
| 108 |
|
---|
| 109 | VkBuffer stagingBuffer;
|
---|
| 110 | VkDeviceMemory stagingBufferMemory;
|
---|
| 111 | createBuffer(device, physicalDevice, srcDataSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
---|
| 112 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
---|
| 113 | stagingBuffer, stagingBufferMemory);
|
---|
| 114 |
|
---|
| 115 | void* data;
|
---|
| 116 | vkMapMemory(device, stagingBufferMemory, 0, srcDataSize, 0, &data);
|
---|
| 117 | memcpy(data, srcData.data(), (size_t)srcDataSize);
|
---|
| 118 | vkUnmapMemory(device, stagingBufferMemory);
|
---|
| 119 |
|
---|
| 120 | copyBuffer(device, commandPool, stagingBuffer, dstBuffer, 0, dstVertexOffset * sizeof(DataType), srcDataSize,
|
---|
| 121 | graphicsQueue);
|
---|
| 122 |
|
---|
| 123 | vkDestroyBuffer(device, stagingBuffer, nullptr);
|
---|
| 124 | vkFreeMemory(device, stagingBufferMemory, nullptr);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[8e02b6b] | 127 | template<class DataType>
|
---|
| 128 | void VulkanUtils::copyDataToMemory(VkDevice device, VkDeviceMemory bufferMemory, const DataType& srcData) {
|
---|
| 129 | void* data;
|
---|
| 130 | vkMapMemory(device, bufferMemory, 0, sizeof(DataType), 0, &data);
|
---|
| 131 | memcpy(data, &srcData, sizeof(DataType));
|
---|
| 132 | vkUnmapMemory(device, bufferMemory);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[cb01aff] | 135 | #endif // _VULKAN_UTILS_H
|
---|