source: opengl-game/vulkan-utils.hpp@ e1f88a9

feature/imgui-sdl
Last change on this file since e1f88a9 was b8d4456, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 4 years ago

Add x and y coordinates to mouse events

  • Property mode set to 100644
File size: 6.3 KB
Line 
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#include <SDL2/SDL.h>
11#include <SDL2/SDL_vulkan.h>
12
13using namespace std;
14
15struct QueueFamilyIndices {
16 optional<uint32_t> graphicsFamily;
17 optional<uint32_t> presentFamily;
18
19 bool isComplete() {
20 return graphicsFamily.has_value() && presentFamily.has_value();
21 }
22};
23
24struct SwapChainSupportDetails {
25 VkSurfaceCapabilitiesKHR capabilities;
26 vector<VkSurfaceFormatKHR> formats;
27 vector<VkPresentModeKHR> presentModes;
28};
29
30struct VulkanImage {
31 VkImage image;
32 VkDeviceMemory imageMemory;
33 VkImageView imageView;
34};
35
36class VulkanUtils {
37 public:
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 SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice physicalDevice,
53 VkSurfaceKHR surface);
54 static VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats);
55 static VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes);
56 static VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, int width, int height);
57 static VkImageView createImageView(VkDevice device, VkImage image, VkFormat format,
58 VkImageAspectFlags aspectFlags);
59 static VkFormat findSupportedFormat(VkPhysicalDevice physicalDevice, const vector<VkFormat>& candidates,
60 VkImageTiling tiling, VkFormatFeatureFlags features);
61 static void createBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkDeviceSize size,
62 VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer,
63 VkDeviceMemory& bufferMemory);
64 static uint32_t findMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter,
65 VkMemoryPropertyFlags properties);
66
67 static void createVulkanImageFromFile(VkDevice device, VkPhysicalDevice physicalDevice,
68 VkCommandPool commandPool, string filename, VulkanImage& image, VkQueue graphicsQueue);
69 static void createVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice,
70 SDL_Texture* texture, VulkanImage& image);
71 static void populateVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice,
72 VkCommandPool commandPool, SDL_Texture* texture, SDL_Renderer* renderer, VulkanImage& image,
73 VkQueue graphicsQueue);
74 static void createDepthImage(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
75 VkFormat depthFormat, VkExtent2D extent, VulkanImage& image, VkQueue graphicsQueue);
76 static void createImage(VkDevice device, VkPhysicalDevice physicalDevice, uint32_t width, uint32_t height,
77 VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties,
78 VulkanImage& image);
79
80 static void transitionImageLayout(VkDevice device, VkCommandPool commandPool, VkImage image,
81 VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout, VkQueue graphicsQueue);
82 static VkCommandBuffer beginSingleTimeCommands(VkDevice device, VkCommandPool commandPool);
83 static void endSingleTimeCommands(VkDevice device, VkCommandPool commandPool,
84 VkCommandBuffer commandBuffer, VkQueue graphicsQueue);
85 static void copyBufferToImage(VkDevice device, VkCommandPool commandPool, VkBuffer buffer, VkImage image,
86 uint32_t width, uint32_t height, VkQueue graphicsQueue);
87
88 template<class DataType>
89 static void copyDataToBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
90 const vector<DataType>& srcData, VkBuffer dstBuffer, size_t dstVertexOffset, VkQueue graphicsQueue);
91
92 static void copyBuffer(VkDevice device, VkCommandPool commandPool, VkBuffer srcBuffer, VkBuffer dstBuffer,
93 VkDeviceSize srcOffset, VkDeviceSize dstOffset, VkDeviceSize size, VkQueue graphicsQueue);
94
95 template<class DataType>
96 static void copyDataToMemory(VkDevice device, VkDeviceMemory bufferMemory, VkDeviceSize offset,
97 const DataType& srcData);
98
99 static bool hasStencilComponent(VkFormat format);
100
101 static void destroyVulkanImage(VkDevice& device, VulkanImage& image);
102};
103
104template<class DataType>
105void 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
127template<class DataType>
128void VulkanUtils::copyDataToMemory(VkDevice device, VkDeviceMemory bufferMemory, VkDeviceSize offset,
129 const DataType& srcData) {
130 void* data;
131
132 vkMapMemory(device, bufferMemory, offset * sizeof(DataType), sizeof(DataType), 0, &data);
133 memcpy(data, &srcData, sizeof(DataType));
134 vkUnmapMemory(device, bufferMemory);
135}
136
137#endif // _VULKAN_UTILS_H
Note: See TracBrowser for help on using the repository browser.