Cleaned up image layouts, pass subresourcerange, use undefined image layout for optimal tiled images (refs #187)

This commit is contained in:
saschawillems 2016-06-21 20:36:54 +02:00
parent 2e101fdd1e
commit 82242e61de

View file

@ -114,54 +114,58 @@ public:
// Create an image memory barrier for changing the layout of // Create an image memory barrier for changing the layout of
// an image and put it into an active command buffer // an image and put it into an active command buffer
void setImageLayout(VkCommandBuffer cmdBuffer, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout oldImageLayout, VkImageLayout newImageLayout, uint32_t mipLevel, uint32_t mipLevelCount) void setImageLayout(VkCommandBuffer cmdBuffer, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout oldImageLayout, VkImageLayout newImageLayout, VkImageSubresourceRange subresourceRange)
{ {
// Create an image barrier object // Create an image barrier object
VkImageMemoryBarrier imageMemoryBarrier = vkTools::initializers::imageMemoryBarrier();; VkImageMemoryBarrier imageMemoryBarrier = vkTools::initializers::imageMemoryBarrier();;
imageMemoryBarrier.oldLayout = oldImageLayout; imageMemoryBarrier.oldLayout = oldImageLayout;
imageMemoryBarrier.newLayout = newImageLayout; imageMemoryBarrier.newLayout = newImageLayout;
imageMemoryBarrier.image = image; imageMemoryBarrier.image = image;
imageMemoryBarrier.subresourceRange.aspectMask = aspectMask; imageMemoryBarrier.subresourceRange = subresourceRange;
imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel;
imageMemoryBarrier.subresourceRange.levelCount = mipLevelCount;
imageMemoryBarrier.subresourceRange.layerCount = 1;
// Only sets masks for layouts used in this example // Only sets masks for layouts used in this example
// For a more complete version that can be used with // For a more complete version that can be used with other layouts see vkTools::setImageLayout
// other layouts see vkTools::setImageLayout
// Source layouts (new) // Source layouts (old)
switch (oldImageLayout)
if (oldImageLayout == VK_IMAGE_LAYOUT_PREINITIALIZED)
{ {
imageMemoryBarrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT; case VK_IMAGE_LAYOUT_UNDEFINED:
// Only valid as initial layout, memory contents are not preserved
// Can be accessed directly, no source dependency required
imageMemoryBarrier.srcAccessMask = 0;
break;
case VK_IMAGE_LAYOUT_PREINITIALIZED:
// Only valid as initial layout for linear images, preserves memory contents
// Make sure host writes to the image have been finished
imageMemoryBarrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
break;
case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
// Old layout is transfer destination
// Make sure any writes to the image have been finished
imageMemoryBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
break;
} }
// Target layouts (new) // Target layouts (new)
switch (newImageLayout)
// New layout is transfer destination (copy, blit)
// Make sure any reads from and writes to the image have been finished
if (newImageLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL)
{ {
imageMemoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_HOST_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT; case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
} // Transfer source (copy, blit)
// Make sure any reads from the image have been finished
// New layout is shader read (sampler, input attachment) imageMemoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
// Make sure any writes to the image have been finished break;
if (newImageLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
{ // Transfer destination (copy, blit)
imageMemoryBarrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT; // Make sure any writes to the image have been finished
imageMemoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
break;
case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
// Shader read (sampler, input attachment)
imageMemoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; imageMemoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
break;
} }
// New layout is transfer source (copy, blit) // Put barrier on top of pipeline
// Make sure any reads from and writes to the image have been finished
if (newImageLayout == VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL)
{
imageMemoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_HOST_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
}
// Put barrier on top
VkPipelineStageFlags srcStageFlags = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; VkPipelineStageFlags srcStageFlags = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
VkPipelineStageFlags destStageFlags = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; VkPipelineStageFlags destStageFlags = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
@ -284,7 +288,8 @@ public:
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL; imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageCreateInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT; imageCreateInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED; // Set initial layout of the image to undefined
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageCreateInfo.extent = { texture.width, texture.height, 1 }; imageCreateInfo.extent = { texture.width, texture.height, 1 };
imageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; imageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
@ -300,16 +305,28 @@ public:
VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
// Image barrier for optimal image (target) // Image barrier for optimal image
// Optimal image will be used as destination for the copy
// The sub resource range describes the regions of the image we will be transition
VkImageSubresourceRange subresourceRange = {};
// Image only contains color data
subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
// Start at first mip level
subresourceRange.baseMipLevel = 0;
// We will transition on all mip levels
subresourceRange.levelCount = texture.mipLevels;
// The 2D texture only has one layer
subresourceRange.layerCount = 1;
// Optimal image will be used as destination for the copy, so we must transfer from our
// initial undefined image layout to the transfer destination layout
setImageLayout( setImageLayout(
copyCmd, copyCmd,
texture.image, texture.image,
VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_ASPECT_COLOR_BIT,
VK_IMAGE_LAYOUT_PREINITIALIZED, VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
0, subresourceRange);
texture.mipLevels);
// Copy mip levels from staging buffer // Copy mip levels from staging buffer
vkCmdCopyBufferToImage( vkCmdCopyBufferToImage(
@ -318,8 +335,7 @@ public:
texture.image, texture.image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
bufferCopyRegions.size(), bufferCopyRegions.size(),
bufferCopyRegions.data() bufferCopyRegions.data());
);
// Change texture image layout to shader read after all mip levels have been copied // Change texture image layout to shader read after all mip levels have been copied
texture.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; texture.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
@ -329,8 +345,7 @@ public:
VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_ASPECT_COLOR_BIT,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
texture.imageLayout, texture.imageLayout,
0, subresourceRange);
texture.mipLevels);
VulkanExampleBase::flushCommandBuffer(copyCmd, queue, true); VulkanExampleBase::flushCommandBuffer(copyCmd, queue, true);
@ -405,14 +420,25 @@ public:
VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
// Setup image memory barrier transfer image to shader read layout // Setup image memory barrier transfer image to shader read layout
// The sub resource range describes the regions of the image we will be transition
VkImageSubresourceRange subresourceRange = {};
// Image only contains color data
subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
// Start at first mip level
subresourceRange.baseMipLevel = 0;
// Only one mip level, most implementations won't support more for linear tiled images
subresourceRange.levelCount = 1;
// The 2D texture only has one layer
subresourceRange.layerCount = 1;
setImageLayout( setImageLayout(
copyCmd, copyCmd,
texture.image, texture.image,
VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_ASPECT_COLOR_BIT,
VK_IMAGE_LAYOUT_PREINITIALIZED, VK_IMAGE_LAYOUT_PREINITIALIZED,
texture.imageLayout, texture.imageLayout,
0, subresourceRange);
1);
VulkanExampleBase::flushCommandBuffer(copyCmd, queue, true); VulkanExampleBase::flushCommandBuffer(copyCmd, queue, true);
} }