425 lines
12 KiB
C++
425 lines
12 KiB
C++
#include "VulkanUtils.hpp"
|
|
|
|
#include <stdexcept>
|
|
|
|
uint32_t findMemoryType(
|
|
VkPhysicalDevice physicalDevice,
|
|
uint32_t typeFilter,
|
|
VkMemoryPropertyFlags properties)
|
|
{
|
|
VkPhysicalDeviceMemoryProperties memProperties;
|
|
|
|
vkGetPhysicalDeviceMemoryProperties(
|
|
physicalDevice,
|
|
&memProperties
|
|
);
|
|
|
|
for (uint32_t i = 0; i < memProperties.memoryTypeCount; ++i)
|
|
{
|
|
if ((typeFilter & (1 << i)) &&
|
|
(memProperties.memoryTypes[i].propertyFlags & properties) == properties)
|
|
{
|
|
return i;
|
|
}
|
|
}
|
|
|
|
throw std::runtime_error("Failed to find suitable memory type");
|
|
}
|
|
|
|
uint32_t findMemoryTypeWithPreferred(
|
|
VkPhysicalDevice physicalDevice,
|
|
uint32_t typeFilter,
|
|
VkMemoryPropertyFlags requiredProperties,
|
|
VkMemoryPropertyFlags preferredProperties,
|
|
VkMemoryPropertyFlags& selectedProperties)
|
|
{
|
|
VkPhysicalDeviceMemoryProperties memProperties;
|
|
|
|
vkGetPhysicalDeviceMemoryProperties(
|
|
physicalDevice,
|
|
&memProperties
|
|
);
|
|
|
|
uint32_t fallbackIndex = UINT32_MAX;
|
|
VkMemoryPropertyFlags fallbackProperties = 0;
|
|
|
|
for (uint32_t i = 0; i < memProperties.memoryTypeCount; ++i)
|
|
{
|
|
if ((typeFilter & (1 << i)) == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
const VkMemoryPropertyFlags flags =
|
|
memProperties.memoryTypes[i].propertyFlags;
|
|
|
|
if ((flags & requiredProperties) != requiredProperties)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if ((flags & preferredProperties) == preferredProperties)
|
|
{
|
|
selectedProperties = flags;
|
|
return i;
|
|
}
|
|
|
|
if (fallbackIndex == UINT32_MAX)
|
|
{
|
|
fallbackIndex = i;
|
|
fallbackProperties = flags;
|
|
}
|
|
}
|
|
|
|
if (fallbackIndex != UINT32_MAX)
|
|
{
|
|
selectedProperties = fallbackProperties;
|
|
return fallbackIndex;
|
|
}
|
|
|
|
throw std::runtime_error("Failed to find suitable memory type");
|
|
}
|
|
|
|
void createBuffer(
|
|
VkDevice device,
|
|
VkPhysicalDevice physicalDevice,
|
|
VkDeviceSize size,
|
|
VkBufferUsageFlags usage,
|
|
VkMemoryPropertyFlags properties,
|
|
VkBuffer& buffer,
|
|
VkDeviceMemory& bufferMemory)
|
|
{
|
|
VkBufferCreateInfo bufferInfo{};
|
|
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
|
bufferInfo.size = size;
|
|
bufferInfo.usage = usage;
|
|
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS)
|
|
{
|
|
throw std::runtime_error("Failed to create buffer");
|
|
}
|
|
|
|
VkMemoryRequirements memRequirements;
|
|
vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
|
|
|
|
VkMemoryAllocateInfo allocInfo{};
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
allocInfo.allocationSize = memRequirements.size;
|
|
allocInfo.memoryTypeIndex = findMemoryType(
|
|
physicalDevice,
|
|
memRequirements.memoryTypeBits,
|
|
properties
|
|
);
|
|
|
|
if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS)
|
|
{
|
|
throw std::runtime_error("Failed to allocate buffer memory");
|
|
}
|
|
|
|
vkBindBufferMemory(device, buffer, bufferMemory, 0);
|
|
}
|
|
|
|
void createBufferWithPreferredMemory(
|
|
VkDevice device,
|
|
VkPhysicalDevice physicalDevice,
|
|
VkDeviceSize size,
|
|
VkBufferUsageFlags usage,
|
|
VkMemoryPropertyFlags requiredProperties,
|
|
VkMemoryPropertyFlags preferredProperties,
|
|
VkBuffer& buffer,
|
|
VkDeviceMemory& bufferMemory,
|
|
VkMemoryPropertyFlags& selectedProperties)
|
|
{
|
|
VkBufferCreateInfo bufferInfo{};
|
|
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
|
bufferInfo.size = size;
|
|
bufferInfo.usage = usage;
|
|
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS)
|
|
{
|
|
throw std::runtime_error("Failed to create buffer");
|
|
}
|
|
|
|
VkMemoryRequirements memRequirements;
|
|
vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
|
|
|
|
VkMemoryAllocateInfo allocInfo{};
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
allocInfo.allocationSize = memRequirements.size;
|
|
allocInfo.memoryTypeIndex = findMemoryTypeWithPreferred(
|
|
physicalDevice,
|
|
memRequirements.memoryTypeBits,
|
|
requiredProperties,
|
|
preferredProperties,
|
|
selectedProperties
|
|
);
|
|
|
|
if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS)
|
|
{
|
|
throw std::runtime_error("Failed to allocate buffer memory");
|
|
}
|
|
|
|
vkBindBufferMemory(device, buffer, bufferMemory, 0);
|
|
}
|
|
|
|
VkCommandBuffer beginSingleTimeCommands(
|
|
VkDevice device,
|
|
VkCommandPool commandPool)
|
|
{
|
|
VkCommandBufferAllocateInfo allocInfo{};
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
|
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
|
allocInfo.commandPool = commandPool;
|
|
allocInfo.commandBufferCount = 1;
|
|
|
|
VkCommandBuffer commandBuffer;
|
|
vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer);
|
|
|
|
VkCommandBufferBeginInfo beginInfo{};
|
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
|
|
|
vkBeginCommandBuffer(commandBuffer, &beginInfo);
|
|
|
|
return commandBuffer;
|
|
}
|
|
|
|
void endSingleTimeCommands(
|
|
VkDevice device,
|
|
VkCommandPool commandPool,
|
|
VkQueue graphicsQueue,
|
|
VkCommandBuffer commandBuffer)
|
|
{
|
|
vkEndCommandBuffer(commandBuffer);
|
|
|
|
VkSubmitInfo submitInfo{};
|
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
submitInfo.commandBufferCount = 1;
|
|
submitInfo.pCommandBuffers = &commandBuffer;
|
|
|
|
vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
|
|
vkQueueWaitIdle(graphicsQueue);
|
|
|
|
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
|
|
}
|
|
|
|
void createImage(
|
|
VkDevice device,
|
|
VkPhysicalDevice physicalDevice,
|
|
uint32_t width,
|
|
uint32_t height,
|
|
VkFormat format,
|
|
VkImageTiling tiling,
|
|
VkImageUsageFlags usage,
|
|
VkMemoryPropertyFlags properties,
|
|
VkImage& image,
|
|
VkDeviceMemory& imageMemory)
|
|
{
|
|
VkImageCreateInfo imageInfo{};
|
|
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
|
imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
|
imageInfo.extent.width = width;
|
|
imageInfo.extent.height = height;
|
|
imageInfo.extent.depth = 1;
|
|
imageInfo.mipLevels = 1;
|
|
imageInfo.arrayLayers = 1;
|
|
imageInfo.format = format;
|
|
imageInfo.tiling = tiling;
|
|
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
imageInfo.usage = usage;
|
|
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
if (vkCreateImage(device, &imageInfo, nullptr, &image) != VK_SUCCESS)
|
|
{
|
|
throw std::runtime_error("Failed to create image");
|
|
}
|
|
|
|
VkMemoryRequirements memRequirements;
|
|
vkGetImageMemoryRequirements(device, image, &memRequirements);
|
|
|
|
VkMemoryAllocateInfo allocInfo{};
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
allocInfo.allocationSize = memRequirements.size;
|
|
allocInfo.memoryTypeIndex = findMemoryType(
|
|
physicalDevice,
|
|
memRequirements.memoryTypeBits,
|
|
properties
|
|
);
|
|
|
|
if (vkAllocateMemory(device, &allocInfo, nullptr, &imageMemory) != VK_SUCCESS)
|
|
{
|
|
throw std::runtime_error("Failed to allocate image memory");
|
|
}
|
|
|
|
vkBindImageMemory(device, image, imageMemory, 0);
|
|
}
|
|
|
|
void recordImageLayoutTransition(
|
|
VkCommandBuffer commandBuffer,
|
|
VkImage image,
|
|
VkFormat format,
|
|
VkImageLayout oldLayout,
|
|
VkImageLayout newLayout)
|
|
{
|
|
(void)format;
|
|
|
|
VkImageMemoryBarrier barrier{};
|
|
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
|
barrier.oldLayout = oldLayout;
|
|
barrier.newLayout = newLayout;
|
|
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
|
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
|
barrier.image = image;
|
|
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
barrier.subresourceRange.baseMipLevel = 0;
|
|
barrier.subresourceRange.levelCount = 1;
|
|
barrier.subresourceRange.baseArrayLayer = 0;
|
|
barrier.subresourceRange.layerCount = 1;
|
|
|
|
VkPipelineStageFlags sourceStage;
|
|
VkPipelineStageFlags destinationStage;
|
|
|
|
if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED &&
|
|
newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL)
|
|
{
|
|
barrier.srcAccessMask = 0;
|
|
barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
|
|
sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
|
destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
|
}
|
|
else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED &&
|
|
newLayout == VK_IMAGE_LAYOUT_GENERAL)
|
|
{
|
|
barrier.srcAccessMask = 0;
|
|
barrier.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
|
|
|
sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
|
destinationStage = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
|
|
}
|
|
else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL &&
|
|
newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
|
|
{
|
|
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
|
|
sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
|
destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
|
|
}
|
|
else if (oldLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL &&
|
|
newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL)
|
|
{
|
|
barrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
|
|
sourceStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
|
|
destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
|
}
|
|
else
|
|
{
|
|
throw std::runtime_error("Unsupported layout transition");
|
|
}
|
|
|
|
vkCmdPipelineBarrier(
|
|
commandBuffer,
|
|
sourceStage,
|
|
destinationStage,
|
|
0,
|
|
0,
|
|
nullptr,
|
|
0,
|
|
nullptr,
|
|
1,
|
|
&barrier
|
|
);
|
|
}
|
|
|
|
void transitionImageLayout(
|
|
VkDevice device,
|
|
VkCommandPool commandPool,
|
|
VkQueue graphicsQueue,
|
|
VkImage image,
|
|
VkFormat format,
|
|
VkImageLayout oldLayout,
|
|
VkImageLayout newLayout)
|
|
{
|
|
VkCommandBuffer commandBuffer =
|
|
beginSingleTimeCommands(device, commandPool);
|
|
|
|
recordImageLayoutTransition(
|
|
commandBuffer,
|
|
image,
|
|
format,
|
|
oldLayout,
|
|
newLayout
|
|
);
|
|
|
|
endSingleTimeCommands(
|
|
device,
|
|
commandPool,
|
|
graphicsQueue,
|
|
commandBuffer
|
|
);
|
|
}
|
|
|
|
void recordCopyBufferToImage(
|
|
VkCommandBuffer commandBuffer,
|
|
VkBuffer buffer,
|
|
VkImage image,
|
|
uint32_t width,
|
|
uint32_t height)
|
|
{
|
|
VkBufferImageCopy region{};
|
|
region.bufferOffset = 0;
|
|
region.bufferRowLength = 0;
|
|
region.bufferImageHeight = 0;
|
|
|
|
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
region.imageSubresource.mipLevel = 0;
|
|
region.imageSubresource.baseArrayLayer = 0;
|
|
region.imageSubresource.layerCount = 1;
|
|
|
|
region.imageOffset = {0, 0, 0};
|
|
region.imageExtent = {width, height, 1};
|
|
|
|
vkCmdCopyBufferToImage(
|
|
commandBuffer,
|
|
buffer,
|
|
image,
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
1,
|
|
®ion
|
|
);
|
|
}
|
|
|
|
void copyBufferToImage(
|
|
VkDevice device,
|
|
VkCommandPool commandPool,
|
|
VkQueue graphicsQueue,
|
|
VkBuffer buffer,
|
|
VkImage image,
|
|
uint32_t width,
|
|
uint32_t height)
|
|
{
|
|
VkCommandBuffer commandBuffer =
|
|
beginSingleTimeCommands(device, commandPool);
|
|
|
|
recordCopyBufferToImage(
|
|
commandBuffer,
|
|
buffer,
|
|
image,
|
|
width,
|
|
height
|
|
);
|
|
|
|
endSingleTimeCommands(
|
|
device,
|
|
commandPool,
|
|
graphicsQueue,
|
|
commandBuffer
|
|
);
|
|
}
|