Skip to content

Commit

Permalink
Vulkan/Texture: Fix incorrect upload image layout
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Feb 5, 2023
1 parent 2dd374d commit 1371dcf
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/common/vulkan/swap_chain.cpp
Expand Up @@ -715,7 +715,8 @@ bool SwapChain::SetupSwapChainImages()

// Create texture object, which creates a view of the backbuffer
if (!image.texture.Adopt(image.image, VK_IMAGE_VIEW_TYPE_2D, m_window_info.surface_width,
m_window_info.surface_height, 1, 1, m_surface_format.format, VK_SAMPLE_COUNT_1_BIT))
m_window_info.surface_height, 1, 1, m_surface_format.format, VK_SAMPLE_COUNT_1_BIT,
VK_IMAGE_LAYOUT_UNDEFINED))
{
return false;
}
Expand Down
13 changes: 9 additions & 4 deletions src/common/vulkan/texture.cpp
Expand Up @@ -163,14 +163,15 @@ bool Vulkan::Texture::Create(u32 width, u32 height, u32 levels, u32 layers, VkFo
m_samples = static_cast<u8>(samples);
m_format = LookupBaseFormat(format);
m_view_type = view_type;
m_layout = VK_IMAGE_LAYOUT_UNDEFINED;
m_image = image;
m_allocation = allocation;
m_view = view;
return true;
}

bool Vulkan::Texture::Adopt(VkImage existing_image, VkImageViewType view_type, u32 width, u32 height, u32 levels,
u32 layers, VkFormat format, VkSampleCountFlagBits samples,
u32 layers, VkFormat format, VkSampleCountFlagBits samples, VkImageLayout layout,
const VkComponentMapping* swizzle /* = nullptr */)
{
// Only need to create the image view, this is mainly for swap chains.
Expand Down Expand Up @@ -205,6 +206,7 @@ bool Vulkan::Texture::Adopt(VkImage existing_image, VkImageViewType view_type, u
m_format = LookupBaseFormat(format);
m_samples = static_cast<u8>(samples);
m_view_type = view_type;
m_layout = layout;
m_image = existing_image;
m_view = view;
return true;
Expand Down Expand Up @@ -395,8 +397,11 @@ VkFramebuffer Vulkan::Texture::CreateFramebuffer(VkRenderPass render_pass)
void Vulkan::Texture::UpdateFromBuffer(VkCommandBuffer cmdbuf, u32 level, u32 layer, u32 x, u32 y, u32 width,
u32 height, VkBuffer buffer, u32 buffer_offset, u32 row_length)
{
// If we're previously undefined, don't leave any images in this layout.
const VkImageLayout old_layout = m_layout;
if (old_layout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL)
if (old_layout == VK_IMAGE_LAYOUT_UNDEFINED)
TransitionToLayout(cmdbuf, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
else if (old_layout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL)
TransitionSubresourcesToLayout(cmdbuf, level, 1, layer, 1, old_layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);

const VkBufferImageCopy bic = {static_cast<VkDeviceSize>(buffer_offset),
Expand All @@ -406,9 +411,9 @@ void Vulkan::Texture::UpdateFromBuffer(VkCommandBuffer cmdbuf, u32 level, u32 la
{static_cast<int32_t>(x), static_cast<int32_t>(y), 0},
{width, height, 1u}};

vkCmdCopyBufferToImage(cmdbuf, buffer, m_image, m_layout, 1, &bic);
vkCmdCopyBufferToImage(cmdbuf, buffer, m_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &bic);

if (old_layout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL)
if (old_layout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && old_layout != VK_IMAGE_LAYOUT_UNDEFINED)
TransitionSubresourcesToLayout(cmdbuf, level, 1, layer, 1, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, old_layout);
}

Expand Down
3 changes: 2 additions & 1 deletion src/common/vulkan/texture.h
Expand Up @@ -46,7 +46,8 @@ class Texture final : public GPUTexture
const VkComponentMapping* swizzle = nullptr);

bool Adopt(VkImage existing_image, VkImageViewType view_type, u32 width, u32 height, u32 levels, u32 layers,
VkFormat format, VkSampleCountFlagBits samples, const VkComponentMapping* swizzle = nullptr);
VkFormat format, VkSampleCountFlagBits samples, VkImageLayout layout,
const VkComponentMapping* swizzle = nullptr);

void Destroy(bool defer = true);

Expand Down
1 change: 1 addition & 0 deletions src/frontend-common/imgui_impl_vulkan.cpp
Expand Up @@ -413,6 +413,7 @@ bool ImGui_ImplVulkan_CreateFontsTexture()

// Store our identifier
bd->FontTexture.Update(0, 0, width, height, 0, 0, pixels, sizeof(u32) * width);
bd->FontTexture.TransitionToLayout(g_vulkan_context->GetCurrentCommandBuffer(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
io.Fonts->SetTexID((ImTextureID)&bd->FontTexture);
return true;
}
Expand Down

0 comments on commit 1371dcf

Please sign in to comment.