[cb01aff] | 1 | #include "vulkan-utils.hpp"
|
---|
| 2 |
|
---|
[fe5c3ba] | 3 | #include <algorithm>
|
---|
[90a424f] | 4 | #include <set>
|
---|
[fe5c3ba] | 5 | #include <stdexcept>
|
---|
[b794178] | 6 |
|
---|
| 7 | #define STB_IMAGE_IMPLEMENTATION
|
---|
| 8 | #include "stb_image.h" // TODO: Probably switch to SDL_image
|
---|
| 9 |
|
---|
| 10 | // TODO: Remove all instances of auto
|
---|
[90a424f] | 11 |
|
---|
[cb01aff] | 12 | bool VulkanUtils::checkValidationLayerSupport(const vector<const char*> &validationLayers) {
|
---|
| 13 | uint32_t layerCount;
|
---|
| 14 | vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
|
---|
| 15 |
|
---|
| 16 | vector<VkLayerProperties> availableLayers(layerCount);
|
---|
| 17 | vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
|
---|
| 18 |
|
---|
| 19 | for (const char* layerName : validationLayers) {
|
---|
| 20 | bool layerFound = false;
|
---|
| 21 |
|
---|
| 22 | for (const auto& layerProperties : availableLayers) {
|
---|
| 23 | if (strcmp(layerName, layerProperties.layerName) == 0) {
|
---|
| 24 | layerFound = true;
|
---|
| 25 | break;
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | if (!layerFound) {
|
---|
| 30 | return false;
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | return true;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | VkResult VulkanUtils::createDebugUtilsMessengerEXT(VkInstance instance,
|
---|
| 38 | const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
|
---|
| 39 | const VkAllocationCallbacks* pAllocator,
|
---|
| 40 | VkDebugUtilsMessengerEXT* pDebugMessenger) {
|
---|
[c205c3a] | 41 | PFN_vkCreateDebugUtilsMessengerEXT func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance,
|
---|
| 42 | "vkCreateDebugUtilsMessengerEXT");
|
---|
[cb01aff] | 43 |
|
---|
| 44 | if (func != nullptr) {
|
---|
| 45 | return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
|
---|
| 46 | } else {
|
---|
| 47 | return VK_ERROR_EXTENSION_NOT_PRESENT;
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | void VulkanUtils::destroyDebugUtilsMessengerEXT(VkInstance instance,
|
---|
| 52 | VkDebugUtilsMessengerEXT debugMessenger,
|
---|
| 53 | const VkAllocationCallbacks* pAllocator) {
|
---|
[c205c3a] | 54 | PFN_vkDestroyDebugUtilsMessengerEXT func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance,
|
---|
| 55 | "vkDestroyDebugUtilsMessengerEXT");
|
---|
[cb01aff] | 56 |
|
---|
| 57 | if (func != nullptr) {
|
---|
| 58 | func(instance, debugMessenger, pAllocator);
|
---|
| 59 | }
|
---|
[90a424f] | 60 | }
|
---|
| 61 |
|
---|
[fe5c3ba] | 62 | QueueFamilyIndices VulkanUtils::findQueueFamilies(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface) {
|
---|
[90a424f] | 63 | QueueFamilyIndices indices;
|
---|
| 64 |
|
---|
| 65 | uint32_t queueFamilyCount = 0;
|
---|
[fe5c3ba] | 66 | vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, nullptr);
|
---|
[90a424f] | 67 |
|
---|
| 68 | vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
|
---|
[fe5c3ba] | 69 | vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, queueFamilies.data());
|
---|
[90a424f] | 70 |
|
---|
| 71 | int i = 0;
|
---|
| 72 | for (const auto& queueFamily : queueFamilies) {
|
---|
| 73 | if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
|
---|
| 74 | indices.graphicsFamily = i;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | VkBool32 presentSupport = false;
|
---|
[fe5c3ba] | 78 | vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, i, surface, &presentSupport);
|
---|
[90a424f] | 79 |
|
---|
| 80 | if (queueFamily.queueCount > 0 && presentSupport) {
|
---|
| 81 | indices.presentFamily = i;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | if (indices.isComplete()) {
|
---|
| 85 | break;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | i++;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | return indices;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[fe5c3ba] | 94 | bool VulkanUtils::checkDeviceExtensionSupport(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions) {
|
---|
[90a424f] | 95 | uint32_t extensionCount;
|
---|
[fe5c3ba] | 96 | vkEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &extensionCount, nullptr);
|
---|
[90a424f] | 97 |
|
---|
| 98 | vector<VkExtensionProperties> availableExtensions(extensionCount);
|
---|
[fe5c3ba] | 99 | vkEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &extensionCount, availableExtensions.data());
|
---|
[90a424f] | 100 |
|
---|
| 101 | set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
|
---|
| 102 |
|
---|
| 103 | for (const auto& extension : availableExtensions) {
|
---|
| 104 | requiredExtensions.erase(extension.extensionName);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | return requiredExtensions.empty();
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[fe5c3ba] | 110 | SwapChainSupportDetails VulkanUtils::querySwapChainSupport(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface) {
|
---|
[90a424f] | 111 | SwapChainSupportDetails details;
|
---|
| 112 |
|
---|
[fe5c3ba] | 113 | vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &details.capabilities);
|
---|
[90a424f] | 114 |
|
---|
| 115 | uint32_t formatCount;
|
---|
[fe5c3ba] | 116 | vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, nullptr);
|
---|
[90a424f] | 117 |
|
---|
| 118 | if (formatCount != 0) {
|
---|
| 119 | details.formats.resize(formatCount);
|
---|
[fe5c3ba] | 120 | vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, details.formats.data());
|
---|
[90a424f] | 121 | }
|
---|
| 122 |
|
---|
| 123 | uint32_t presentModeCount;
|
---|
[fe5c3ba] | 124 | vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, nullptr);
|
---|
[90a424f] | 125 |
|
---|
| 126 | if (presentModeCount != 0) {
|
---|
| 127 | details.presentModes.resize(presentModeCount);
|
---|
[fe5c3ba] | 128 | vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, details.presentModes.data());
|
---|
[90a424f] | 129 | }
|
---|
| 130 |
|
---|
| 131 | return details;
|
---|
[502bd0b] | 132 | }
|
---|
| 133 |
|
---|
| 134 | VkSurfaceFormatKHR VulkanUtils::chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) {
|
---|
| 135 | for (const auto& availableFormat : availableFormats) {
|
---|
| 136 | if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
|
---|
| 137 | return availableFormat;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | return availableFormats[0];
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | VkPresentModeKHR VulkanUtils::chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes) {
|
---|
| 145 | VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR;
|
---|
| 146 |
|
---|
[c324d6a] | 147 | /* This functions effectively selects present modes in this order, which allows for unlimited framerate:
|
---|
| 148 | * { VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_FIFO_KHR }
|
---|
| 149 | *
|
---|
| 150 | * To cap the framerate (I assume to the monitor refresh rate), just use:
|
---|
| 151 | * { VK_PRESENT_MODE_FIFO_KHR }
|
---|
| 152 | *
|
---|
| 153 | * Would be better to make a more generic function that takes a list of prefered modes ordered by preference.
|
---|
| 154 | * Example code:
|
---|
| 155 | *
|
---|
| 156 | * for (int request_i = 0; request_i < request_modes_count; request_i++)
|
---|
| 157 | * for (uint32_t avail_i = 0; avail_i < avail_count; avail_i++)
|
---|
| 158 | * if (request_modes[request_i] == avail_modes[avail_i])
|
---|
| 159 | * return request_modes[request_i];
|
---|
| 160 | */
|
---|
| 161 |
|
---|
[502bd0b] | 162 | for (const auto& availablePresentMode : availablePresentModes) {
|
---|
| 163 | if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
|
---|
| 164 | return availablePresentMode;
|
---|
[c324d6a] | 165 | } else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
|
---|
[502bd0b] | 166 | bestMode = availablePresentMode;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | return bestMode;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | VkExtent2D VulkanUtils::chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, int width, int height) {
|
---|
| 174 | if (capabilities.currentExtent.width != numeric_limits<uint32_t>::max()) {
|
---|
| 175 | return capabilities.currentExtent;
|
---|
| 176 | } else {
|
---|
| 177 | VkExtent2D actualExtent = {
|
---|
| 178 | static_cast<uint32_t>(width),
|
---|
| 179 | static_cast<uint32_t>(height)
|
---|
| 180 | };
|
---|
| 181 |
|
---|
| 182 | actualExtent.width = std::max(capabilities.minImageExtent.width, std::min(capabilities.maxImageExtent.width, actualExtent.width));
|
---|
| 183 | actualExtent.height = std::max(capabilities.minImageExtent.height, std::min(capabilities.maxImageExtent.height, actualExtent.height));
|
---|
| 184 |
|
---|
| 185 | return actualExtent;
|
---|
| 186 | }
|
---|
[f94eea9] | 187 | }
|
---|
| 188 |
|
---|
| 189 | VkImageView VulkanUtils::createImageView(VkDevice device, VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) {
|
---|
| 190 | VkImageViewCreateInfo viewInfo = {};
|
---|
| 191 | viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
---|
| 192 | viewInfo.image = image;
|
---|
| 193 | viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
---|
| 194 | viewInfo.format = format;
|
---|
| 195 |
|
---|
| 196 | viewInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
|
---|
| 197 | viewInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
|
---|
| 198 | viewInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
|
---|
| 199 | viewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
|
---|
| 200 |
|
---|
| 201 | viewInfo.subresourceRange.aspectMask = aspectFlags;
|
---|
| 202 | viewInfo.subresourceRange.baseMipLevel = 0;
|
---|
| 203 | viewInfo.subresourceRange.levelCount = 1;
|
---|
| 204 | viewInfo.subresourceRange.baseArrayLayer = 0;
|
---|
| 205 | viewInfo.subresourceRange.layerCount = 1;
|
---|
| 206 |
|
---|
| 207 | VkImageView imageView;
|
---|
| 208 | if (vkCreateImageView(device, &viewInfo, nullptr, &imageView) != VK_SUCCESS) {
|
---|
| 209 | throw runtime_error("failed to create image view!");
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | return imageView;
|
---|
[6fc24c7] | 213 | }
|
---|
| 214 |
|
---|
| 215 | VkFormat VulkanUtils::findSupportedFormat(VkPhysicalDevice physicalDevice, const vector<VkFormat>& candidates,
|
---|
| 216 | VkImageTiling tiling, VkFormatFeatureFlags features) {
|
---|
| 217 | for (VkFormat format : candidates) {
|
---|
| 218 | VkFormatProperties props;
|
---|
| 219 | vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &props);
|
---|
| 220 |
|
---|
| 221 | if (tiling == VK_IMAGE_TILING_LINEAR &&
|
---|
| 222 | (props.linearTilingFeatures & features) == features) {
|
---|
| 223 | return format;
|
---|
| 224 | } else if (tiling == VK_IMAGE_TILING_OPTIMAL &&
|
---|
| 225 | (props.optimalTilingFeatures & features) == features) {
|
---|
| 226 | return format;
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | throw runtime_error("failed to find supported format!");
|
---|
[b794178] | 231 | }
|
---|
| 232 |
|
---|
| 233 | void VulkanUtils::createBuffer(VkDevice device, VkPhysicalDevice physicalDevice, VkDeviceSize size, VkBufferUsageFlags usage,
|
---|
| 234 | VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory) {
|
---|
| 235 | VkBufferCreateInfo bufferInfo = {};
|
---|
| 236 | bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
---|
| 237 | bufferInfo.size = size;
|
---|
| 238 | bufferInfo.usage = usage;
|
---|
| 239 | bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
---|
| 240 |
|
---|
| 241 | if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) {
|
---|
| 242 | throw runtime_error("failed to create buffer!");
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | VkMemoryRequirements memRequirements;
|
---|
| 246 | vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
|
---|
| 247 |
|
---|
| 248 | VkMemoryAllocateInfo allocInfo = {};
|
---|
| 249 | allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
---|
| 250 | allocInfo.allocationSize = memRequirements.size;
|
---|
| 251 | allocInfo.memoryTypeIndex = findMemoryType(physicalDevice, memRequirements.memoryTypeBits, properties);
|
---|
| 252 |
|
---|
| 253 | if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
|
---|
| 254 | throw runtime_error("failed to allocate buffer memory!");
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | vkBindBufferMemory(device, buffer, bufferMemory, 0);
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | uint32_t VulkanUtils::findMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter, VkMemoryPropertyFlags properties) {
|
---|
| 261 | VkPhysicalDeviceMemoryProperties memProperties;
|
---|
| 262 | vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
|
---|
| 263 |
|
---|
| 264 | for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
|
---|
| 265 | if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
|
---|
| 266 | return i;
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | throw runtime_error("failed to find suitable memory type!");
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | void VulkanUtils::createVulkanImageFromFile(VkDevice device, VkPhysicalDevice physicalDevice,
|
---|
| 274 | VkCommandPool commandPool, string filename, VulkanImage& image, VkQueue graphicsQueue) {
|
---|
[4a9416a] | 275 | // TODO: Since the image loaded here will be used as a texture, display a warning if it has
|
---|
| 276 | // non power-of-two dimensions
|
---|
[b794178] | 277 | int texWidth, texHeight, texChannels;
|
---|
| 278 |
|
---|
| 279 | stbi_uc* pixels = stbi_load(filename.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
|
---|
| 280 | VkDeviceSize imageSize = texWidth * texHeight * 4;
|
---|
| 281 |
|
---|
| 282 | if (!pixels) {
|
---|
| 283 | throw runtime_error("failed to load texture image!");
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | VkBuffer stagingBuffer;
|
---|
| 287 | VkDeviceMemory stagingBufferMemory;
|
---|
| 288 |
|
---|
[d2d9286] | 289 | createBuffer(device, physicalDevice, imageSize,
|
---|
| 290 | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
---|
[b794178] | 291 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
---|
| 292 | stagingBuffer, stagingBufferMemory);
|
---|
| 293 |
|
---|
| 294 | void* data;
|
---|
| 295 |
|
---|
| 296 | vkMapMemory(device, stagingBufferMemory, 0, imageSize, 0, &data);
|
---|
| 297 | memcpy(data, pixels, static_cast<size_t>(imageSize));
|
---|
| 298 | vkUnmapMemory(device, stagingBufferMemory);
|
---|
| 299 |
|
---|
| 300 | stbi_image_free(pixels);
|
---|
| 301 |
|
---|
| 302 | createImage(device, physicalDevice, texWidth, texHeight, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL,
|
---|
| 303 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image);
|
---|
| 304 |
|
---|
| 305 | transitionImageLayout(device, commandPool, image.image, VK_FORMAT_R8G8B8A8_UNORM,
|
---|
| 306 | VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, graphicsQueue);
|
---|
| 307 | copyBufferToImage(device, commandPool, stagingBuffer, image.image,
|
---|
| 308 | static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight), graphicsQueue);
|
---|
| 309 | transitionImageLayout(device, commandPool, image.image, VK_FORMAT_R8G8B8A8_UNORM,
|
---|
| 310 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, graphicsQueue);
|
---|
| 311 |
|
---|
| 312 | vkDestroyBuffer(device, stagingBuffer, nullptr);
|
---|
| 313 | vkFreeMemory(device, stagingBufferMemory, nullptr);
|
---|
| 314 |
|
---|
| 315 | image.imageView = createImageView(device, image.image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | void VulkanUtils::createVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice,
|
---|
| 319 | SDL_Texture* texture, VulkanImage& image) {
|
---|
| 320 | int a, w, h;
|
---|
| 321 |
|
---|
| 322 | // I only need this here for the width and height, which are constants, so just use those instead
|
---|
| 323 | SDL_QueryTexture(texture, nullptr, &a, &w, &h);
|
---|
| 324 |
|
---|
| 325 | createImage(device, physicalDevice, w, h, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL,
|
---|
| 326 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image);
|
---|
| 327 |
|
---|
| 328 | image.imageView = createImageView(device, image.image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
|
---|
| 329 | }
|
---|
| 330 |
|
---|
[d2d9286] | 331 | void VulkanUtils::populateVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice,
|
---|
| 332 | VkCommandPool commandPool, SDL_Texture* texture, SDL_Renderer* renderer, VulkanImage& image,
|
---|
| 333 | VkQueue graphicsQueue) {
|
---|
| 334 | int a, w, h;
|
---|
| 335 |
|
---|
| 336 | SDL_QueryTexture(texture, nullptr, &a, &w, &h);
|
---|
| 337 |
|
---|
| 338 | VkDeviceSize imageSize = w * h * 4;
|
---|
| 339 | unsigned char* pixels = new unsigned char[imageSize];
|
---|
| 340 |
|
---|
| 341 | SDL_RenderReadPixels(renderer, nullptr, SDL_PIXELFORMAT_ABGR8888, pixels, w * 4);
|
---|
| 342 |
|
---|
| 343 | VkBuffer stagingBuffer;
|
---|
| 344 | VkDeviceMemory stagingBufferMemory;
|
---|
| 345 |
|
---|
| 346 | createBuffer(device, physicalDevice, imageSize,
|
---|
| 347 | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
---|
| 348 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
|
---|
| 349 | stagingBuffer, stagingBufferMemory);
|
---|
| 350 |
|
---|
| 351 | void* data;
|
---|
| 352 |
|
---|
| 353 | vkMapMemory(device, stagingBufferMemory, 0, VK_WHOLE_SIZE, 0, &data);
|
---|
| 354 | memcpy(data, pixels, static_cast<size_t>(imageSize));
|
---|
| 355 |
|
---|
| 356 | VkMappedMemoryRange mappedMemoryRange = {};
|
---|
| 357 | mappedMemoryRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
|
---|
| 358 | mappedMemoryRange.memory = stagingBufferMemory;
|
---|
| 359 | mappedMemoryRange.offset = 0;
|
---|
| 360 | mappedMemoryRange.size = VK_WHOLE_SIZE;
|
---|
| 361 |
|
---|
| 362 | // TODO: Should probably check that the function succeeded
|
---|
| 363 | vkFlushMappedMemoryRanges(device, 1, &mappedMemoryRange);
|
---|
| 364 | vkUnmapMemory(device, stagingBufferMemory);
|
---|
| 365 |
|
---|
| 366 | delete[] pixels;
|
---|
| 367 |
|
---|
| 368 | transitionImageLayout(device, commandPool, image.image, VK_FORMAT_R8G8B8A8_UNORM,
|
---|
| 369 | VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, graphicsQueue);
|
---|
| 370 | copyBufferToImage(device, commandPool, stagingBuffer, image.image,
|
---|
| 371 | static_cast<uint32_t>(w), static_cast<uint32_t>(h), graphicsQueue);
|
---|
| 372 | transitionImageLayout(device, commandPool, image.image, VK_FORMAT_R8G8B8A8_UNORM,
|
---|
| 373 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, graphicsQueue);
|
---|
| 374 |
|
---|
| 375 | vkDestroyBuffer(device, stagingBuffer, nullptr);
|
---|
| 376 | vkFreeMemory(device, stagingBufferMemory, nullptr);
|
---|
| 377 | }
|
---|
| 378 |
|
---|
[603b5bc] | 379 | void VulkanUtils::createDepthImage(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
|
---|
| 380 | VkFormat depthFormat, VkExtent2D extent, VulkanImage& image, VkQueue graphicsQueue) {
|
---|
| 381 | createImage(device, physicalDevice, extent.width, extent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL,
|
---|
| 382 | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image);
|
---|
| 383 | image.imageView = createImageView(device, image.image, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
|
---|
| 384 |
|
---|
| 385 | transitionImageLayout(device, commandPool, image.image, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED,
|
---|
| 386 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, graphicsQueue);
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[b794178] | 389 | void VulkanUtils::createImage(VkDevice device, VkPhysicalDevice physicalDevice, uint32_t width, uint32_t height,
|
---|
| 390 | VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties,
|
---|
| 391 | VulkanImage& image) {
|
---|
| 392 | VkImageCreateInfo imageInfo = {};
|
---|
| 393 | imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
---|
| 394 | imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
---|
| 395 | imageInfo.extent.width = width;
|
---|
| 396 | imageInfo.extent.height = height;
|
---|
| 397 | imageInfo.extent.depth = 1;
|
---|
| 398 | imageInfo.mipLevels = 1;
|
---|
| 399 | imageInfo.arrayLayers = 1;
|
---|
| 400 | imageInfo.format = format;
|
---|
| 401 | imageInfo.tiling = tiling;
|
---|
| 402 | imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
---|
| 403 | imageInfo.usage = usage;
|
---|
| 404 | imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
---|
| 405 | imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
---|
| 406 |
|
---|
| 407 | if (vkCreateImage(device, &imageInfo, nullptr, &image.image) != VK_SUCCESS) {
|
---|
| 408 | throw runtime_error("failed to create image!");
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | VkMemoryRequirements memRequirements;
|
---|
| 412 | vkGetImageMemoryRequirements(device, image.image, &memRequirements);
|
---|
| 413 |
|
---|
| 414 | VkMemoryAllocateInfo allocInfo = {};
|
---|
| 415 | allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
---|
| 416 | allocInfo.allocationSize = memRequirements.size;
|
---|
| 417 | allocInfo.memoryTypeIndex = findMemoryType(physicalDevice, memRequirements.memoryTypeBits, properties);
|
---|
| 418 |
|
---|
| 419 | if (vkAllocateMemory(device, &allocInfo, nullptr, &image.imageMemory) != VK_SUCCESS) {
|
---|
| 420 | throw runtime_error("failed to allocate image memory!");
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | vkBindImageMemory(device, image.image, image.imageMemory, 0);
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | void VulkanUtils::transitionImageLayout(VkDevice device, VkCommandPool commandPool, VkImage image,
|
---|
| 427 | VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout, VkQueue graphicsQueue) {
|
---|
| 428 | VkCommandBuffer commandBuffer = beginSingleTimeCommands(device, commandPool);
|
---|
| 429 |
|
---|
| 430 | VkImageMemoryBarrier barrier = {};
|
---|
| 431 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
---|
| 432 | barrier.oldLayout = oldLayout;
|
---|
| 433 | barrier.newLayout = newLayout;
|
---|
| 434 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
---|
| 435 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
---|
| 436 | barrier.image = image;
|
---|
| 437 |
|
---|
| 438 | if (newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
|
---|
| 439 | barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
|
---|
| 440 |
|
---|
| 441 | if (hasStencilComponent(format)) {
|
---|
| 442 | barrier.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
|
---|
| 443 | }
|
---|
| 444 | } else {
|
---|
| 445 | barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 | barrier.subresourceRange.baseMipLevel = 0;
|
---|
| 449 | barrier.subresourceRange.levelCount = 1;
|
---|
| 450 | barrier.subresourceRange.baseArrayLayer = 0;
|
---|
| 451 | barrier.subresourceRange.layerCount = 1;
|
---|
| 452 |
|
---|
| 453 | VkPipelineStageFlags sourceStage;
|
---|
| 454 | VkPipelineStageFlags destinationStage;
|
---|
| 455 |
|
---|
| 456 | if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
|
---|
| 457 | barrier.srcAccessMask = 0;
|
---|
| 458 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
---|
| 459 |
|
---|
| 460 | sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
---|
| 461 | destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
---|
| 462 | } else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
|
---|
| 463 | barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
---|
| 464 | barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
---|
| 465 |
|
---|
| 466 | sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
---|
| 467 | destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
|
---|
| 468 | } else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
|
---|
| 469 | barrier.srcAccessMask = 0;
|
---|
| 470 | barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
---|
| 471 |
|
---|
| 472 | sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
---|
| 473 | destinationStage = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
|
---|
| 474 | } else {
|
---|
| 475 | throw invalid_argument("unsupported layout transition!");
|
---|
| 476 | }
|
---|
| 477 |
|
---|
| 478 | vkCmdPipelineBarrier(
|
---|
| 479 | commandBuffer,
|
---|
| 480 | sourceStage, destinationStage,
|
---|
| 481 | 0,
|
---|
| 482 | 0, nullptr,
|
---|
| 483 | 0, nullptr,
|
---|
| 484 | 1, &barrier
|
---|
| 485 | );
|
---|
| 486 |
|
---|
| 487 | endSingleTimeCommands(device, commandPool, commandBuffer, graphicsQueue);
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 | VkCommandBuffer VulkanUtils::beginSingleTimeCommands(VkDevice device, VkCommandPool commandPool) {
|
---|
| 491 | VkCommandBufferAllocateInfo allocInfo = {};
|
---|
| 492 | allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
---|
| 493 | allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
---|
| 494 | allocInfo.commandPool = commandPool;
|
---|
| 495 | allocInfo.commandBufferCount = 1;
|
---|
| 496 |
|
---|
| 497 | VkCommandBuffer commandBuffer;
|
---|
| 498 | vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer);
|
---|
| 499 |
|
---|
| 500 | VkCommandBufferBeginInfo beginInfo = {};
|
---|
| 501 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
---|
| 502 | beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
---|
| 503 |
|
---|
| 504 | vkBeginCommandBuffer(commandBuffer, &beginInfo);
|
---|
| 505 |
|
---|
| 506 | return commandBuffer;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | void VulkanUtils::endSingleTimeCommands(VkDevice device, VkCommandPool commandPool,
|
---|
| 510 | VkCommandBuffer commandBuffer, VkQueue graphicsQueue) {
|
---|
| 511 | vkEndCommandBuffer(commandBuffer);
|
---|
| 512 |
|
---|
| 513 | VkSubmitInfo submitInfo = {};
|
---|
| 514 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
---|
| 515 | submitInfo.commandBufferCount = 1;
|
---|
| 516 | submitInfo.pCommandBuffers = &commandBuffer;
|
---|
| 517 |
|
---|
| 518 | vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
|
---|
| 519 | vkQueueWaitIdle(graphicsQueue);
|
---|
| 520 |
|
---|
| 521 | vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | void VulkanUtils::copyBufferToImage(VkDevice device, VkCommandPool commandPool, VkBuffer buffer,
|
---|
| 525 | VkImage image, uint32_t width, uint32_t height, VkQueue graphicsQueue) {
|
---|
| 526 | VkCommandBuffer commandBuffer = beginSingleTimeCommands(device, commandPool);
|
---|
| 527 |
|
---|
| 528 | VkBufferImageCopy region = {};
|
---|
| 529 | region.bufferOffset = 0;
|
---|
| 530 | region.bufferRowLength = 0;
|
---|
| 531 | region.bufferImageHeight = 0;
|
---|
| 532 | region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
---|
| 533 | region.imageSubresource.mipLevel = 0;
|
---|
| 534 | region.imageSubresource.baseArrayLayer = 0;
|
---|
| 535 | region.imageSubresource.layerCount = 1;
|
---|
| 536 | region.imageOffset = { 0, 0, 0 };
|
---|
| 537 | region.imageExtent = { width, height, 1 };
|
---|
| 538 |
|
---|
| 539 | vkCmdCopyBufferToImage(
|
---|
| 540 | commandBuffer,
|
---|
| 541 | buffer,
|
---|
| 542 | image,
|
---|
| 543 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
---|
| 544 | 1,
|
---|
| 545 | ®ion
|
---|
| 546 | );
|
---|
| 547 |
|
---|
| 548 | endSingleTimeCommands(device, commandPool, commandBuffer, graphicsQueue);
|
---|
| 549 | }
|
---|
| 550 |
|
---|
[87c8f1a] | 551 | void VulkanUtils::copyBuffer(VkDevice device, VkCommandPool commandPool, VkBuffer srcBuffer,
|
---|
| 552 | VkBuffer dstBuffer, VkDeviceSize srcOffset, VkDeviceSize dstOffset, VkDeviceSize size,
|
---|
| 553 | VkQueue graphicsQueue) {
|
---|
| 554 | VkCommandBuffer commandBuffer = beginSingleTimeCommands(device, commandPool);
|
---|
| 555 |
|
---|
| 556 | VkBufferCopy copyRegion = { srcOffset, dstOffset, size };
|
---|
| 557 | vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, ©Region);
|
---|
| 558 |
|
---|
| 559 | endSingleTimeCommands(device, commandPool, commandBuffer, graphicsQueue);
|
---|
| 560 | }
|
---|
| 561 |
|
---|
[b794178] | 562 | bool VulkanUtils::hasStencilComponent(VkFormat format) {
|
---|
| 563 | return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;
|
---|
[e83b155] | 564 | }
|
---|
| 565 |
|
---|
| 566 | void VulkanUtils::destroyVulkanImage(VkDevice& device, VulkanImage& image) {
|
---|
| 567 | vkDestroyImageView(device, image.imageView, nullptr);
|
---|
| 568 | vkDestroyImage(device, image.image, nullptr);
|
---|
| 569 | vkFreeMemory(device, image.imageMemory, nullptr);
|
---|
[cb01aff] | 570 | } |
---|