| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/Vulkan/VKTexture.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cstddef> |
| 8 | #include <cstring> |
| 9 | |
| 10 | #include "Common/Align.h" |
| 11 | #include "Common/Assert.h" |
| 12 | #include "Common/CommonTypes.h" |
| 13 | #include "Common/Logging/Log.h" |
| 14 | #include "Common/MsgHandler.h" |
| 15 | |
| 16 | #include "VideoBackends/Vulkan/CommandBufferManager.h" |
| 17 | #include "VideoBackends/Vulkan/ObjectCache.h" |
| 18 | #include "VideoBackends/Vulkan/StagingBuffer.h" |
| 19 | #include "VideoBackends/Vulkan/StateTracker.h" |
| 20 | #include "VideoBackends/Vulkan/VKGfx.h" |
| 21 | #include "VideoBackends/Vulkan/VKStreamBuffer.h" |
| 22 | #include "VideoBackends/Vulkan/VulkanContext.h" |
| 23 | |
| 24 | #include "VideoCommon/DriverDetails.h" |
| 25 | #include "VideoCommon/VideoConfig.h" |
| 26 | |
| 27 | namespace Vulkan |
| 28 | { |
| 29 | VKTexture::VKTexture(const TextureConfig& tex_config, VmaAllocation alloc, VkImage image, |
| 30 | std::string_view name, VkImageLayout layout /* = VK_IMAGE_LAYOUT_UNDEFINED */, |
| 31 | ComputeImageLayout compute_layout /* = ComputeImageLayout::Undefined */) |
| 32 | : AbstractTexture(tex_config), m_alloc(alloc), m_image(image), m_layout(layout), |
| 33 | m_compute_layout(compute_layout), m_name(name) |
| 34 | { |
| 35 | if (!m_name.empty() && g_backend_info.bSupportsSettingObjectNames) |
| 36 | { |
| 37 | VkDebugUtilsObjectNameInfoEXT name_info = {}; |
| 38 | name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT; |
| 39 | name_info.objectType = VK_OBJECT_TYPE_IMAGE; |
| 40 | name_info.objectHandle = reinterpret_cast<uint64_t>(image); |
| 41 | name_info.pObjectName = m_name.c_str(); |
| 42 | vkSetDebugUtilsObjectNameEXT(g_vulkan_context->GetDevice(), &name_info); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | VKTexture::~VKTexture() |
| 47 | { |
| 48 | StateTracker::GetInstance()->UnbindTexture(m_view); |
| 49 | g_command_buffer_mgr->DeferImageViewDestruction(m_view); |
| 50 | |
| 51 | // If we don't have device memory allocated, the image is not owned by us (e.g. swapchain) |
| 52 | if (m_alloc != VK_NULL_HANDLE) |
| 53 | { |
| 54 | g_command_buffer_mgr->DeferImageDestruction(m_image, m_alloc); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | std::unique_ptr<VKTexture> VKTexture::Create(const TextureConfig& tex_config, std::string_view name) |
| 59 | { |
| 60 | // Determine image usage, we need to flag as an attachment if it can be used as a rendertarget. |
| 61 | VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | |
| 62 | VK_IMAGE_USAGE_SAMPLED_BIT; |
| 63 | if (tex_config.IsRenderTarget()) |
| 64 | { |
| 65 | usage |= IsDepthFormat(tex_config.format) ? VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT : |
| 66 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 67 | } |
| 68 | if (tex_config.IsComputeImage()) |
| 69 | usage |= VK_IMAGE_USAGE_STORAGE_BIT; |
| 70 | |
| 71 | VkImageCreateInfo image_info = {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 72 | nullptr, |
| 73 | tex_config.type == AbstractTextureType::Texture_CubeMap ? |
| 74 | VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT : |
| 75 | static_cast<VkImageCreateFlags>(0), |
| 76 | VK_IMAGE_TYPE_2D, |
| 77 | GetVkFormatForHostTextureFormat(tex_config.format), |
| 78 | {tex_config.width, tex_config.height, 1}, |
| 79 | tex_config.levels, |
| 80 | tex_config.layers, |
| 81 | static_cast<VkSampleCountFlagBits>(tex_config.samples), |
| 82 | VK_IMAGE_TILING_OPTIMAL, |
| 83 | usage, |
| 84 | VK_SHARING_MODE_EXCLUSIVE, |
| 85 | 0, |
| 86 | nullptr, |
| 87 | VK_IMAGE_LAYOUT_UNDEFINED}; |
| 88 | |
| 89 | VmaAllocationCreateInfo alloc_create_info = {}; |
| 90 | alloc_create_info.flags = VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT; |
| 91 | alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE; |
| 92 | alloc_create_info.pool = VK_NULL_HANDLE; |
| 93 | alloc_create_info.pUserData = nullptr; |
| 94 | alloc_create_info.priority = |
| 95 | tex_config.IsComputeImage() || tex_config.IsRenderTarget() ? 1.0 : 0.0; |
| 96 | alloc_create_info.requiredFlags = 0; |
| 97 | alloc_create_info.preferredFlags = 0; |
| 98 | |
| 99 | VkImage image = VK_NULL_HANDLE; |
| 100 | VmaAllocation alloc = VK_NULL_HANDLE; |
| 101 | VkResult res = vmaCreateImage(g_vulkan_context->GetMemoryAllocator(), &image_info, |
| 102 | &alloc_create_info, &image, &alloc, nullptr); |
| 103 | if (res != VK_SUCCESS) |
| 104 | { |
| 105 | LOG_VULKAN_ERROR(res, "vmaCreateImage failed: "); |
| 106 | return nullptr; |
| 107 | } |
| 108 | |
| 109 | std::unique_ptr<VKTexture> texture = std::make_unique<VKTexture>( |
| 110 | tex_config, alloc, image, name, VK_IMAGE_LAYOUT_UNDEFINED, ComputeImageLayout::Undefined); |
| 111 | |
| 112 | VkImageViewType image_view_type = VK_IMAGE_VIEW_TYPE_2D_ARRAY; |
| 113 | if (tex_config.type == AbstractTextureType::Texture_CubeMap) |
| 114 | { |
| 115 | image_view_type = VK_IMAGE_VIEW_TYPE_CUBE; |
| 116 | } |
| 117 | else if (tex_config.type == AbstractTextureType::Texture_2D) |
| 118 | { |
| 119 | image_view_type = VK_IMAGE_VIEW_TYPE_2D; |
| 120 | } |
| 121 | else if (tex_config.type == AbstractTextureType::Texture_2DArray) |
| 122 | { |
| 123 | image_view_type = VK_IMAGE_VIEW_TYPE_2D_ARRAY; |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | PanicAlertFmt("Unhandled texture type."); |
| 128 | return nullptr; |
| 129 | } |
| 130 | if (!texture->CreateView(image_view_type)) |
| 131 | return nullptr; |
| 132 | |
| 133 | return texture; |
| 134 | } |
| 135 | |
| 136 | std::unique_ptr<VKTexture> VKTexture::CreateAdopted(const TextureConfig& tex_config, VkImage image, |
| 137 | VkImageViewType view_type, VkImageLayout layout) |
| 138 | { |
| 139 | std::unique_ptr<VKTexture> texture = std::make_unique<VKTexture>( |
| 140 | tex_config, VmaAllocation(VK_NULL_HANDLE), image, "", layout, ComputeImageLayout::Undefined); |
| 141 | if (!texture->CreateView(view_type)) |
| 142 | return nullptr; |
| 143 | |
| 144 | return texture; |
| 145 | } |
| 146 | |
| 147 | bool VKTexture::CreateView(VkImageViewType type) |
| 148 | { |
| 149 | VkImageViewCreateInfo view_info = { |
| 150 | VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
| 151 | nullptr, |
| 152 | 0, |
| 153 | m_image, |
| 154 | type, |
| 155 | GetVkFormat(), |
| 156 | {VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, |
| 157 | VK_COMPONENT_SWIZZLE_IDENTITY}, |
| 158 | {GetImageViewAspectForFormat(GetFormat()), 0, GetLevels(), 0, GetLayers()}}; |
| 159 | |
| 160 | VkResult res = vkCreateImageView(g_vulkan_context->GetDevice(), &view_info, nullptr, &m_view); |
| 161 | if (res != VK_SUCCESS) |
| 162 | { |
| 163 | LOG_VULKAN_ERROR(res, "vkCreateImageView failed: "); |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | VkFormat VKTexture::GetLinearFormat(VkFormat format) |
| 171 | { |
| 172 | switch (format) |
| 173 | { |
| 174 | case VK_FORMAT_R8_SRGB: |
| 175 | return VK_FORMAT_R8_UNORM; |
| 176 | case VK_FORMAT_R8G8_SRGB: |
| 177 | return VK_FORMAT_R8G8_UNORM; |
| 178 | case VK_FORMAT_R8G8B8_SRGB: |
| 179 | return VK_FORMAT_R8G8B8_UNORM; |
| 180 | case VK_FORMAT_R8G8B8A8_SRGB: |
| 181 | return VK_FORMAT_R8G8B8A8_UNORM; |
| 182 | case VK_FORMAT_B8G8R8_SRGB: |
| 183 | return VK_FORMAT_B8G8R8_UNORM; |
| 184 | case VK_FORMAT_B8G8R8A8_SRGB: |
| 185 | return VK_FORMAT_B8G8R8A8_UNORM; |
| 186 | default: |
| 187 | return format; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | VkFormat VKTexture::GetVkFormatForHostTextureFormat(AbstractTextureFormat format) |
| 192 | { |
| 193 | switch (format) |
| 194 | { |
| 195 | case AbstractTextureFormat::DXT1: |
| 196 | return VK_FORMAT_BC1_RGBA_UNORM_BLOCK; |
| 197 | |
| 198 | case AbstractTextureFormat::DXT3: |
| 199 | return VK_FORMAT_BC2_UNORM_BLOCK; |
| 200 | |
| 201 | case AbstractTextureFormat::DXT5: |
| 202 | return VK_FORMAT_BC3_UNORM_BLOCK; |
| 203 | |
| 204 | case AbstractTextureFormat::BPTC: |
| 205 | return VK_FORMAT_BC7_UNORM_BLOCK; |
| 206 | |
| 207 | case AbstractTextureFormat::RGBA8: |
| 208 | return VK_FORMAT_R8G8B8A8_UNORM; |
| 209 | |
| 210 | case AbstractTextureFormat::BGRA8: |
| 211 | return VK_FORMAT_B8G8R8A8_UNORM; |
| 212 | |
| 213 | case AbstractTextureFormat::RGB10_A2: |
| 214 | return VK_FORMAT_A2B10G10R10_UNORM_PACK32; |
| 215 | |
| 216 | case AbstractTextureFormat::RGBA16F: |
| 217 | return VK_FORMAT_R16G16B16A16_SFLOAT; |
| 218 | |
| 219 | case AbstractTextureFormat::R16: |
| 220 | return VK_FORMAT_R16_UNORM; |
| 221 | |
| 222 | case AbstractTextureFormat::D16: |
| 223 | return VK_FORMAT_D16_UNORM; |
| 224 | |
| 225 | case AbstractTextureFormat::D24_S8: |
| 226 | return VK_FORMAT_D24_UNORM_S8_UINT; |
| 227 | |
| 228 | case AbstractTextureFormat::R32F: |
| 229 | return VK_FORMAT_R32_SFLOAT; |
| 230 | |
| 231 | case AbstractTextureFormat::D32F: |
| 232 | return VK_FORMAT_D32_SFLOAT; |
| 233 | |
| 234 | case AbstractTextureFormat::D32F_S8: |
| 235 | return VK_FORMAT_D32_SFLOAT_S8_UINT; |
| 236 | |
| 237 | case AbstractTextureFormat::Undefined: |
| 238 | return VK_FORMAT_UNDEFINED; |
| 239 | |
| 240 | default: |
| 241 | PanicAlertFmt("Unhandled texture format."); |
| 242 | return VK_FORMAT_R8G8B8A8_UNORM; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | VkImageAspectFlags VKTexture::GetImageAspectForFormat(AbstractTextureFormat format) |
| 247 | { |
| 248 | switch (format) |
| 249 | { |
| 250 | case AbstractTextureFormat::D24_S8: |
| 251 | case AbstractTextureFormat::D32F_S8: |
| 252 | return VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; |
| 253 | |
| 254 | case AbstractTextureFormat::D16: |
| 255 | case AbstractTextureFormat::D32F: |
| 256 | return VK_IMAGE_ASPECT_DEPTH_BIT; |
| 257 | |
| 258 | default: |
| 259 | return VK_IMAGE_ASPECT_COLOR_BIT; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | VkImageAspectFlags VKTexture::GetImageViewAspectForFormat(AbstractTextureFormat format) |
| 264 | { |
| 265 | switch (format) |
| 266 | { |
| 267 | case AbstractTextureFormat::D16: |
| 268 | case AbstractTextureFormat::D24_S8: |
| 269 | case AbstractTextureFormat::D32F_S8: |
| 270 | case AbstractTextureFormat::D32F: |
| 271 | return VK_IMAGE_ASPECT_DEPTH_BIT; |
| 272 | |
| 273 | default: |
| 274 | return VK_IMAGE_ASPECT_COLOR_BIT; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void VKTexture::CopyRectangleFromTexture(const AbstractTexture* src, |
| 279 | const MathUtil::Rectangle<int>& src_rect, u32 src_layer, |
| 280 | u32 src_level, const MathUtil::Rectangle<int>& dst_rect, |
| 281 | u32 dst_layer, u32 dst_level) |
| 282 | { |
| 283 | const VKTexture* src_texture = static_cast<const VKTexture*>(src); |
| 284 | |
| 285 | ASSERT_MSG(VIDEO, |
| 286 | static_cast<u32>(src_rect.GetWidth()) <= src_texture->GetWidth() && |
| 287 | static_cast<u32>(src_rect.GetHeight()) <= src_texture->GetHeight(), |
| 288 | "Source rect is too large for CopyRectangleFromTexture"); |
| 289 | |
| 290 | ASSERT_MSG(VIDEO, |
| 291 | static_cast<u32>(dst_rect.GetWidth()) <= m_config.width && |
| 292 | static_cast<u32>(dst_rect.GetHeight()) <= m_config.height, |
| 293 | "Dest rect is too large for CopyRectangleFromTexture"); |
| 294 | |
| 295 | const u32 copy_layer_count = 1; |
| 296 | |
| 297 | VkImageCopy image_copy = { |
| 298 | {VK_IMAGE_ASPECT_COLOR_BIT, src_level, src_layer, copy_layer_count}, |
| 299 | {src_rect.left, src_rect.top, 0}, |
| 300 | {VK_IMAGE_ASPECT_COLOR_BIT, dst_level, dst_layer, copy_layer_count}, |
| 301 | {dst_rect.left, dst_rect.top, 0}, |
| 302 | {static_cast<uint32_t>(src_rect.GetWidth()), static_cast<uint32_t>(src_rect.GetHeight()), 1}}; |
| 303 | |
| 304 | // Must be called outside of a render pass. |
| 305 | StateTracker::GetInstance()->EndRenderPass(); |
| 306 | |
| 307 | const VkImageLayout old_src_layout = src_texture->GetLayout(); |
| 308 | src_texture->TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(), |
| 309 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); |
| 310 | TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(), |
| 311 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); |
| 312 | |
| 313 | vkCmdCopyImage(g_command_buffer_mgr->GetCurrentCommandBuffer(), src_texture->m_image, |
| 314 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, m_image, |
| 315 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &image_copy); |
| 316 | |
| 317 | // Only restore the source layout. Destination is restored by FinishedRendering(). |
| 318 | src_texture->TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(), old_src_layout); |
| 319 | } |
| 320 | |
| 321 | void VKTexture::ResolveFromTexture(const AbstractTexture* src, const MathUtil::Rectangle<int>& rect, |
| 322 | u32 layer, u32 level) |
| 323 | { |
| 324 | const VKTexture* srcentry = static_cast<const VKTexture*>(src); |
| 325 | DEBUG_ASSERT(m_config.samples == 1 && m_config.width == srcentry->m_config.width && |
| 326 | m_config.height == srcentry->m_config.height && srcentry->m_config.samples > 1); |
| 327 | DEBUG_ASSERT(rect.left + rect.GetWidth() <= static_cast<int>(srcentry->m_config.width) && |
| 328 | rect.top + rect.GetHeight() <= static_cast<int>(srcentry->m_config.height)); |
| 329 | |
| 330 | // Resolving is considered to be a transfer operation. |
| 331 | StateTracker::GetInstance()->EndRenderPass(); |
| 332 | VkImageLayout old_src_layout = srcentry->m_layout; |
| 333 | srcentry->TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(), |
| 334 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); |
| 335 | TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(), |
| 336 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); |
| 337 | |
| 338 | VkImageResolve resolve = { |
| 339 | {VK_IMAGE_ASPECT_COLOR_BIT, level, layer, 1}, // srcSubresource |
| 340 | {rect.left, rect.top, 0}, // srcOffset |
| 341 | {VK_IMAGE_ASPECT_COLOR_BIT, level, layer, 1}, // dstSubresource |
| 342 | {rect.left, rect.top, 0}, // dstOffset |
| 343 | {static_cast<u32>(rect.GetWidth()), static_cast<u32>(rect.GetHeight()), 1} // extent |
| 344 | }; |
| 345 | vkCmdResolveImage(g_command_buffer_mgr->GetCurrentCommandBuffer(), srcentry->m_image, |
| 346 | srcentry->m_layout, m_image, m_layout, 1, &resolve); |
| 347 | |
| 348 | srcentry->TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(), old_src_layout); |
| 349 | } |
| 350 | |
| 351 | void VKTexture::Load(u32 level, u32 width, u32 height, u32 row_length, const u8* buffer, |
| 352 | size_t buffer_size, u32 layer) |
| 353 | { |
| 354 | // Can't copy data larger than the texture extents. |
| 355 | width = std::max(1u, std::min(width, GetWidth() >> level)); |
| 356 | height = std::max(1u, std::min(height, GetHeight() >> level)); |
| 357 | |
| 358 | // We don't care about the existing contents of the texture, so we could the image layout to |
| 359 | // VK_IMAGE_LAYOUT_UNDEFINED here. However, under section 2.2.1, Queue Operation of the Vulkan |
| 360 | // specification, it states: |
| 361 | // |
| 362 | // Command buffer submissions to a single queue must always adhere to command order and |
| 363 | // API order, but otherwise may overlap or execute out of order. |
| 364 | // |
| 365 | // Therefore, if a previous frame's command buffer is still sampling from this texture, and we |
| 366 | // overwrite it without a pipeline barrier, a texture sample could occur in parallel with the |
| 367 | // texture upload/copy. I'm not sure if any drivers currently take advantage of this, but we |
| 368 | // should insert an explicit pipeline barrier just in case (done by TransitionToLayout). |
| 369 | // |
| 370 | // We transition to TRANSFER_DST, ready for the image copy, and leave the texture in this state. |
| 371 | // When the last mip level is uploaded, we transition to SHADER_READ_ONLY, ready for use. This is |
| 372 | // because we can't transition in a render pass, and we don't necessarily know when this texture |
| 373 | // is going to be used. |
| 374 | TransitionToLayout(g_command_buffer_mgr->GetCurrentInitCommandBuffer(), |
| 375 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); |
| 376 | |
| 377 | // For unaligned textures, we can save some memory in the transfer buffer by skipping the rows |
| 378 | // that lie outside of the texture's dimensions. |
| 379 | const u32 upload_alignment = static_cast<u32>(g_vulkan_context->GetBufferImageGranularity()); |
| 380 | const u32 block_size = GetBlockSizeForFormat(GetFormat()); |
| 381 | const u32 num_rows = Common::AlignUp(height, block_size) / block_size; |
| 382 | const u32 source_pitch = CalculateStrideForFormat(m_config.format, row_length); |
| 383 | const u32 upload_size = source_pitch * num_rows; |
| 384 | std::unique_ptr<StagingBuffer> temp_buffer; |
| 385 | VkBuffer upload_buffer; |
| 386 | VkDeviceSize upload_buffer_offset; |
| 387 | |
| 388 | // Does this texture data fit within the streaming buffer? |
| 389 | if (upload_size <= STAGING_TEXTURE_UPLOAD_THRESHOLD) |
| 390 | { |
| 391 | StreamBuffer* stream_buffer = g_object_cache->GetTextureUploadBuffer(); |
| 392 | if (!stream_buffer->ReserveMemory(upload_size, upload_alignment)) |
| 393 | { |
| 394 | // Execute the command buffer first. |
| 395 | WARN_LOG_FMT(VIDEO, |
| 396 | "Executing command list while waiting for space in texture upload buffer"); |
| 397 | VKGfx::GetInstance()->ExecuteCommandBuffer(false); |
| 398 | |
| 399 | // Try allocating again. This may cause a fence wait. |
| 400 | if (!stream_buffer->ReserveMemory(upload_size, upload_alignment)) |
| 401 | PanicAlertFmt("Failed to allocate space in texture upload buffer"); |
| 402 | } |
| 403 | // Copy to the streaming buffer. |
| 404 | upload_buffer = stream_buffer->GetBuffer(); |
| 405 | upload_buffer_offset = stream_buffer->GetCurrentOffset(); |
| 406 | std::memcpy(stream_buffer->GetCurrentHostPointer(), buffer, upload_size); |
| 407 | stream_buffer->CommitMemory(upload_size); |
| 408 | } |
| 409 | else |
| 410 | { |
| 411 | // Create a temporary staging buffer that is destroyed after the image is copied. |
| 412 | temp_buffer = StagingBuffer::Create(STAGING_BUFFER_TYPE_UPLOAD, upload_size, |
| 413 | VK_BUFFER_USAGE_TRANSFER_SRC_BIT); |
| 414 | if (!temp_buffer || !temp_buffer->Map()) |
| 415 | { |
| 416 | PanicAlertFmt("Failed to allocate staging texture for large texture upload."); |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | upload_buffer = temp_buffer->GetBuffer(); |
| 421 | upload_buffer_offset = 0; |
| 422 | temp_buffer->Write(0, buffer, upload_size, true); |
| 423 | temp_buffer->Unmap(); |
| 424 | } |
| 425 | |
| 426 | // Copy from the streaming buffer to the actual image. |
| 427 | VkBufferImageCopy image_copy = { |
| 428 | upload_buffer_offset, // VkDeviceSize bufferOffset |
| 429 | row_length, // uint32_t bufferRowLength |
| 430 | 0, // uint32_t bufferImageHeight |
| 431 | {VK_IMAGE_ASPECT_COLOR_BIT, level, layer, 1}, // VkImageSubresourceLayers imageSubresource |
| 432 | {0, 0, 0}, // VkOffset3D imageOffset |
| 433 | {width, height, 1} // VkExtent3D imageExtent |
| 434 | }; |
| 435 | vkCmdCopyBufferToImage(g_command_buffer_mgr->GetCurrentInitCommandBuffer(), upload_buffer, |
| 436 | m_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &image_copy); |
| 437 | |
| 438 | // Preemptively transition to shader read only after uploading the last mip level, as we're |
| 439 | // likely finished with writes to this texture for now. We can't do this in common with a |
| 440 | // FinishedRendering() call because the upload happens in the init command buffer, and we |
| 441 | // don't want to interrupt the render pass with calls which were executed ages before. |
| 442 | if (level == (m_config.levels - 1) && layer == (m_config.layers - 1)) |
| 443 | { |
| 444 | TransitionToLayout(g_command_buffer_mgr->GetCurrentInitCommandBuffer(), |
| 445 | VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | void VKTexture::FinishedRendering() |
| 450 | { |
| 451 | if (m_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) |
| 452 | return; |
| 453 | |
| 454 | StateTracker::GetInstance()->EndRenderPass(); |
| 455 | TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(), |
| 456 | VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); |
| 457 | } |
| 458 | |
| 459 | void VKTexture::OverrideImageLayout(VkImageLayout new_layout) |
| 460 | { |
| 461 | m_layout = new_layout; |
| 462 | } |
| 463 | |
| 464 | void VKTexture::TransitionToLayout(VkCommandBuffer command_buffer, VkImageLayout new_layout) const |
| 465 | { |
| 466 | if (m_layout == new_layout) |
| 467 | return; |
| 468 | |
| 469 | m_written_since_last_layout_change = false; |
| 470 | |
| 471 | VkImageMemoryBarrier barrier = { |
| 472 | VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // VkStructureType sType |
| 473 | nullptr, // const void* pNext |
| 474 | 0, // VkAccessFlags srcAccessMask |
| 475 | 0, // VkAccessFlags dstAccessMask |
| 476 | m_layout, // VkImageLayout oldLayout |
| 477 | new_layout, // VkImageLayout newLayout |
| 478 | VK_QUEUE_FAMILY_IGNORED, // uint32_t srcQueueFamilyIndex |
| 479 | VK_QUEUE_FAMILY_IGNORED, // uint32_t dstQueueFamilyIndex |
| 480 | m_image, // VkImage image |
| 481 | {GetImageAspectForFormat(GetFormat()), 0, GetLevels(), 0, |
| 482 | GetLayers()} // VkImageSubresourceRange subresourceRange |
| 483 | }; |
| 484 | |
| 485 | // srcStageMask -> Stages that must complete before the barrier |
| 486 | // dstStageMask -> Stages that must wait for after the barrier before beginning |
| 487 | VkPipelineStageFlags srcStageMask, dstStageMask; |
| 488 | switch (m_layout) |
| 489 | { |
| 490 | case VK_IMAGE_LAYOUT_UNDEFINED: |
| 491 | // Layout undefined therefore contents undefined, and we don't care what happens to it. |
| 492 | barrier.srcAccessMask = 0; |
| 493 | srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
| 494 | break; |
| 495 | |
| 496 | case VK_IMAGE_LAYOUT_PREINITIALIZED: |
| 497 | // Image has been pre-initialized by the host, so ensure all writes have completed. |
| 498 | barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT; |
| 499 | srcStageMask = VK_PIPELINE_STAGE_HOST_BIT; |
| 500 | break; |
| 501 | |
| 502 | case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: |
| 503 | // Image was being used as a color attachment, so ensure all writes have completed. |
| 504 | barrier.srcAccessMask = |
| 505 | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 506 | srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 507 | break; |
| 508 | |
| 509 | case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: |
| 510 | // Image was being used as a depthstencil attachment, so ensure all writes have completed. |
| 511 | barrier.srcAccessMask = |
| 512 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
| 513 | srcStageMask = |
| 514 | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; |
| 515 | break; |
| 516 | |
| 517 | case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: |
| 518 | // Image was being used as a shader resource, make sure all reads have finished. |
| 519 | barrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 520 | srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; |
| 521 | break; |
| 522 | |
| 523 | case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: |
| 524 | // Image was being used as a copy source, ensure all reads have finished. |
| 525 | barrier.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 526 | srcStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 527 | break; |
| 528 | |
| 529 | case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: |
| 530 | // Image was being used as a copy destination, ensure all writes have finished. |
| 531 | barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 532 | srcStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 533 | break; |
| 534 | |
| 535 | default: |
| 536 | srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
| 537 | break; |
| 538 | } |
| 539 | |
| 540 | switch (new_layout) |
| 541 | { |
| 542 | case VK_IMAGE_LAYOUT_UNDEFINED: |
| 543 | barrier.dstAccessMask = 0; |
| 544 | dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
| 545 | break; |
| 546 | |
| 547 | case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: |
| 548 | barrier.dstAccessMask = |
| 549 | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 550 | dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 551 | break; |
| 552 | |
| 553 | case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: |
| 554 | barrier.dstAccessMask = |
| 555 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
| 556 | dstStageMask = |
| 557 | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; |
| 558 | break; |
| 559 | |
| 560 | case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: |
| 561 | barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 562 | dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; |
| 563 | break; |
| 564 | |
| 565 | case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: |
| 566 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 567 | dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 568 | break; |
| 569 | |
| 570 | case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: |
| 571 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 572 | dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 573 | break; |
| 574 | |
| 575 | case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR: |
| 576 | srcStageMask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; |
| 577 | dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
| 578 | break; |
| 579 | |
| 580 | default: |
| 581 | dstStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 582 | break; |
| 583 | } |
| 584 | |
| 585 | // If we were using a compute layout, the stages need to reflect that |
| 586 | switch (m_compute_layout) |
| 587 | { |
| 588 | case ComputeImageLayout::Undefined: |
| 589 | break; |
| 590 | case ComputeImageLayout::ReadOnly: |
| 591 | barrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 592 | srcStageMask = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 593 | break; |
| 594 | case ComputeImageLayout::WriteOnly: |
| 595 | barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; |
| 596 | srcStageMask = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 597 | break; |
| 598 | case ComputeImageLayout::ReadWrite: |
| 599 | barrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; |
| 600 | srcStageMask = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 601 | break; |
| 602 | } |
| 603 | m_compute_layout = ComputeImageLayout::Undefined; |
| 604 | |
| 605 | vkCmdPipelineBarrier(command_buffer, srcStageMask, dstStageMask, 0, 0, nullptr, 0, nullptr, 1, |
| 606 | &barrier); |
| 607 | |
| 608 | m_layout = new_layout; |
| 609 | } |
| 610 | |
| 611 | void VKTexture::TransitionToLayout(VkCommandBuffer command_buffer, |
| 612 | ComputeImageLayout new_layout) const |
| 613 | { |
| 614 | ASSERT(new_layout != ComputeImageLayout::Undefined); |
| 615 | if (m_compute_layout == new_layout) |
| 616 | return; |
| 617 | |
| 618 | m_written_since_last_layout_change = false; |
| 619 | |
| 620 | VkImageMemoryBarrier barrier = { |
| 621 | VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // VkStructureType sType |
| 622 | nullptr, // const void* pNext |
| 623 | 0, // VkAccessFlags srcAccessMask |
| 624 | 0, // VkAccessFlags dstAccessMask |
| 625 | m_layout, // VkImageLayout oldLayout |
| 626 | VK_IMAGE_LAYOUT_GENERAL, // VkImageLayout newLayout |
| 627 | VK_QUEUE_FAMILY_IGNORED, // uint32_t srcQueueFamilyIndex |
| 628 | VK_QUEUE_FAMILY_IGNORED, // uint32_t dstQueueFamilyIndex |
| 629 | m_image, // VkImage image |
| 630 | {GetImageAspectForFormat(GetFormat()), 0, GetLevels(), 0, |
| 631 | GetLayers()} // VkImageSubresourceRange subresourceRange |
| 632 | }; |
| 633 | |
| 634 | VkPipelineStageFlags srcStageMask, dstStageMask; |
| 635 | switch (m_layout) |
| 636 | { |
| 637 | case VK_IMAGE_LAYOUT_UNDEFINED: |
| 638 | // Layout undefined therefore contents undefined, and we don't care what happens to it. |
| 639 | barrier.srcAccessMask = 0; |
| 640 | srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
| 641 | break; |
| 642 | |
| 643 | case VK_IMAGE_LAYOUT_PREINITIALIZED: |
| 644 | // Image has been pre-initialized by the host, so ensure all writes have completed. |
| 645 | barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT; |
| 646 | srcStageMask = VK_PIPELINE_STAGE_HOST_BIT; |
| 647 | break; |
| 648 | |
| 649 | case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: |
| 650 | // Image was being used as a color attachment, so ensure all writes have completed. |
| 651 | barrier.srcAccessMask = |
| 652 | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 653 | srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 654 | break; |
| 655 | |
| 656 | case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: |
| 657 | // Image was being used as a depthstencil attachment, so ensure all writes have completed. |
| 658 | barrier.srcAccessMask = |
| 659 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
| 660 | srcStageMask = |
| 661 | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; |
| 662 | break; |
| 663 | |
| 664 | case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: |
| 665 | // Image was being used as a shader resource, make sure all reads have finished. |
| 666 | barrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 667 | srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; |
| 668 | break; |
| 669 | |
| 670 | case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: |
| 671 | // Image was being used as a copy source, ensure all reads have finished. |
| 672 | barrier.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 673 | srcStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 674 | break; |
| 675 | |
| 676 | case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: |
| 677 | // Image was being used as a copy destination, ensure all writes have finished. |
| 678 | barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 679 | srcStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 680 | break; |
| 681 | |
| 682 | default: |
| 683 | srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
| 684 | break; |
| 685 | } |
| 686 | |
| 687 | switch (new_layout) |
| 688 | { |
| 689 | case ComputeImageLayout::ReadOnly: |
| 690 | barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; |
| 691 | barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 692 | dstStageMask = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 693 | break; |
| 694 | case ComputeImageLayout::WriteOnly: |
| 695 | barrier.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT; |
| 696 | barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL; |
| 697 | dstStageMask = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 698 | break; |
| 699 | case ComputeImageLayout::ReadWrite: |
| 700 | barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; |
| 701 | barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL; |
| 702 | dstStageMask = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT; |
| 703 | break; |
| 704 | default: |
| 705 | dstStageMask = 0; |
| 706 | break; |
| 707 | } |
| 708 | |
| 709 | m_layout = barrier.newLayout; |
| 710 | m_compute_layout = new_layout; |
| 711 | |
| 712 | vkCmdPipelineBarrier(command_buffer, srcStageMask, dstStageMask, 0, 0, nullptr, 0, nullptr, 1, |
| 713 | &barrier); |
| 714 | } |
| 715 | |
| 716 | void VKTexture::PrepareForRenderPass(VkCommandBuffer command_buffer) const |
| 717 | { |
| 718 | // Should only be used on images that are already in the layout for being rendered to |
| 719 | ASSERT(m_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL || |
| 720 | m_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); |
| 721 | if (m_written_since_last_layout_change) |
| 722 | { |
| 723 | // If the image has already been written, we need a barrier to prevent WaW or RaW hazards. |
| 724 | VkPipelineStageFlags srcStage = 0; |
| 725 | VkPipelineStageFlags dstStage = 0; |
| 726 | VkImageMemoryBarrier barrier = {VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; |
| 727 | barrier.image = m_image; |
| 728 | barrier.oldLayout = m_layout; |
| 729 | barrier.newLayout = m_layout; |
| 730 | barrier.subresourceRange.aspectMask = GetImageAspectForFormat(GetFormat()); |
| 731 | barrier.subresourceRange.layerCount = GetLayers(); |
| 732 | barrier.subresourceRange.levelCount = GetLevels(); |
| 733 | if (barrier.subresourceRange.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) |
| 734 | { |
| 735 | barrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 736 | barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | // |
| 737 | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT; |
| 738 | srcStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 739 | dstStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 740 | } |
| 741 | else |
| 742 | { |
| 743 | barrier.srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
| 744 | barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | |
| 745 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT; |
| 746 | srcStage = VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT; |
| 747 | dstStage = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT; |
| 748 | } |
| 749 | vkCmdPipelineBarrier(command_buffer, srcStage, dstStage, 0, // |
| 750 | 0, nullptr, 0, nullptr, 1, &barrier); |
| 751 | } |
| 752 | else |
| 753 | { |
| 754 | // Otherwise, it's about to be written right now. |
| 755 | m_written_since_last_layout_change = true; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | VKStagingTexture::VKStagingTexture(PrivateTag, StagingTextureType type, const TextureConfig& config, |
| 760 | std::unique_ptr<StagingBuffer> buffer, VkImage linear_image, |
| 761 | VmaAllocation linear_image_alloc) |
| 762 | : AbstractStagingTexture(type, config), m_staging_buffer(std::move(buffer)), |
| 763 | m_linear_image(linear_image), m_linear_image_alloc(linear_image_alloc) |
| 764 | { |
| 765 | } |
| 766 | |
| 767 | VKStagingTexture::~VKStagingTexture() |
| 768 | { |
| 769 | if (m_linear_image != VK_NULL_HANDLE) |
| 770 | { |
| 771 | g_command_buffer_mgr->DeferImageDestruction(m_linear_image, m_linear_image_alloc); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | std::unique_ptr<VKStagingTexture> VKStagingTexture::Create(StagingTextureType type, |
| 776 | const TextureConfig& config) |
| 777 | { |
| 778 | size_t stride = config.GetStride(); |
| 779 | size_t buffer_size = stride * static_cast<size_t>(config.height); |
| 780 | |
| 781 | STAGING_BUFFER_TYPE buffer_type; |
| 782 | VkImageUsageFlags buffer_usage; |
| 783 | if (type == StagingTextureType::Readback) |
| 784 | { |
| 785 | buffer_type = STAGING_BUFFER_TYPE_READBACK; |
| 786 | buffer_usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 787 | } |
| 788 | else if (type == StagingTextureType::Upload) |
| 789 | { |
| 790 | buffer_type = STAGING_BUFFER_TYPE_UPLOAD; |
| 791 | buffer_usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
| 792 | } |
| 793 | else |
| 794 | { |
| 795 | buffer_type = STAGING_BUFFER_TYPE_READBACK; |
| 796 | buffer_usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 797 | } |
| 798 | |
| 799 | VkBuffer buffer; |
| 800 | VmaAllocation alloc; |
| 801 | char* map_ptr; |
| 802 | if (!StagingBuffer::AllocateBuffer(buffer_type, buffer_size, buffer_usage, &buffer, &alloc, |
| 803 | &map_ptr)) |
| 804 | { |
| 805 | return nullptr; |
| 806 | } |
| 807 | |
| 808 | // Linear image |
| 809 | VkImage linear_image = VK_NULL_HANDLE; |
| 810 | VmaAllocation linear_image_alloc = VK_NULL_HANDLE; |
| 811 | if (DriverDetails::HasBug(DriverDetails::BUG_SLOW_OPTIMAL_IMAGE_TO_BUFFER_COPY) && |
| 812 | type == StagingTextureType::Readback && config.samples == 1) |
| 813 | { |
| 814 | std::tie(linear_image, linear_image_alloc) = CreateLinearImage(type, config); |
| 815 | } |
| 816 | |
| 817 | std::unique_ptr<StagingBuffer> staging_buffer = |
| 818 | std::make_unique<StagingBuffer>(buffer_type, buffer, alloc, buffer_size, map_ptr); |
| 819 | std::unique_ptr<VKStagingTexture> staging_tex = std::make_unique<VKStagingTexture>( |
| 820 | PrivateTag{}, type, config, std::move(staging_buffer), linear_image, linear_image_alloc); |
| 821 | |
| 822 | // Use persistent mapping. |
| 823 | if (!staging_tex->m_staging_buffer->Map()) |
| 824 | return nullptr; |
| 825 | staging_tex->m_map_pointer = staging_tex->m_staging_buffer->GetMapPointer(); |
| 826 | staging_tex->m_map_stride = stride; |
| 827 | return staging_tex; |
| 828 | } |
| 829 | |
| 830 | std::pair<VkImage, VmaAllocation> VKStagingTexture::CreateLinearImage(StagingTextureType type, |
| 831 | const TextureConfig& config) |
| 832 | { |
| 833 | // Create a intermediate texture with linear tiling |
| 834 | VkImageCreateInfo image_info = {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 835 | nullptr, |
| 836 | 0, |
| 837 | VK_IMAGE_TYPE_2D, |
| 838 | VKTexture::GetVkFormatForHostTextureFormat(config.format), |
| 839 | {config.width, config.height, 1}, |
| 840 | 1, |
| 841 | 1, |
| 842 | VK_SAMPLE_COUNT_1_BIT, |
| 843 | VK_IMAGE_TILING_LINEAR, |
| 844 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, |
| 845 | VK_SHARING_MODE_EXCLUSIVE, |
| 846 | 0, |
| 847 | nullptr, |
| 848 | VK_IMAGE_LAYOUT_UNDEFINED}; |
| 849 | |
| 850 | VkImageFormatProperties format_properties; |
| 851 | VkResult res = vkGetPhysicalDeviceImageFormatProperties( |
| 852 | g_vulkan_context->GetPhysicalDevice(), image_info.format, image_info.imageType, |
| 853 | image_info.tiling, image_info.usage, image_info.flags, &format_properties); |
| 854 | if (res != VK_SUCCESS) |
| 855 | { |
| 856 | LOG_VULKAN_ERROR(res, "Linear images are not supported for the staging texture: "); |
| 857 | return std::make_pair<VkImage, VmaAllocation>(VK_NULL_HANDLE, VK_NULL_HANDLE); |
| 858 | } |
| 859 | |
| 860 | VmaAllocationCreateInfo alloc_create_info = {}; |
| 861 | alloc_create_info.flags = VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT; |
| 862 | alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE; |
| 863 | alloc_create_info.pool = VK_NULL_HANDLE; |
| 864 | alloc_create_info.pUserData = nullptr; |
| 865 | alloc_create_info.priority = 0.0; |
| 866 | alloc_create_info.requiredFlags = 0; |
| 867 | alloc_create_info.preferredFlags = 0; |
| 868 | |
| 869 | VkImage image; |
| 870 | VmaAllocation alloc; |
| 871 | res = vmaCreateImage(g_vulkan_context->GetMemoryAllocator(), &image_info, &alloc_create_info, |
| 872 | &image, &alloc, nullptr); |
| 873 | if (res != VK_SUCCESS) |
| 874 | { |
| 875 | LOG_VULKAN_ERROR(res, "vmaCreateImage failed: "); |
| 876 | return std::make_pair<VkImage, VmaAllocation>(VK_NULL_HANDLE, VK_NULL_HANDLE); |
| 877 | } |
| 878 | return std::make_pair(image, alloc); |
| 879 | } |
| 880 | |
| 881 | void VKStagingTexture::CopyFromTexture(const AbstractTexture* src, |
| 882 | const MathUtil::Rectangle<int>& src_rect, u32 src_layer, |
| 883 | u32 src_level, const MathUtil::Rectangle<int>& dst_rect) |
| 884 | { |
| 885 | const VKTexture* src_tex = static_cast<const VKTexture*>(src); |
| 886 | ASSERT(m_type == StagingTextureType::Readback || m_type == StagingTextureType::Mutable); |
| 887 | ASSERT(src_rect.GetWidth() == dst_rect.GetWidth() && |
| 888 | src_rect.GetHeight() == dst_rect.GetHeight()); |
| 889 | ASSERT(src_rect.left >= 0 && static_cast<u32>(src_rect.right) <= src_tex->GetWidth() && |
| 890 | src_rect.top >= 0 && static_cast<u32>(src_rect.bottom) <= src_tex->GetHeight()); |
| 891 | ASSERT(dst_rect.left >= 0 && static_cast<u32>(dst_rect.right) <= m_config.width && |
| 892 | dst_rect.top >= 0 && static_cast<u32>(dst_rect.bottom) <= m_config.height); |
| 893 | |
| 894 | StateTracker::GetInstance()->EndRenderPass(); |
| 895 | |
| 896 | VkImageLayout old_layout = src_tex->GetLayout(); |
| 897 | src_tex->TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(), |
| 898 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); |
| 899 | |
| 900 | // Issue the image->buffer copy, but delay it for now. |
| 901 | VkBufferImageCopy image_copy = {}; |
| 902 | const VkImageAspectFlags aspect = VKTexture::GetImageViewAspectForFormat(src_tex->GetFormat()); |
| 903 | image_copy.bufferOffset = |
| 904 | static_cast<VkDeviceSize>(static_cast<size_t>(dst_rect.top) * m_config.GetStride() + |
| 905 | static_cast<size_t>(dst_rect.left) * m_texel_size); |
| 906 | image_copy.bufferRowLength = static_cast<u32>(m_config.width); |
| 907 | image_copy.bufferImageHeight = 0; |
| 908 | image_copy.imageSubresource = {aspect, src_level, src_layer, 1}; |
| 909 | image_copy.imageOffset = {src_rect.left, src_rect.top, 0}; |
| 910 | image_copy.imageExtent = {static_cast<u32>(src_rect.GetWidth()), |
| 911 | static_cast<u32>(src_rect.GetHeight()), 1u}; |
| 912 | |
| 913 | VkImage src_image = src_tex->GetImage(); |
| 914 | if (m_linear_image != VK_NULL_HANDLE) |
| 915 | { |
| 916 | CopyFromTextureToLinearImage(src_tex, src_rect, src_layer, src_level, dst_rect); |
| 917 | src_image = m_linear_image; |
| 918 | image_copy.imageOffset = {0, 0, 0}; |
| 919 | } |
| 920 | |
| 921 | vkCmdCopyImageToBuffer(g_command_buffer_mgr->GetCurrentCommandBuffer(), src_image, |
| 922 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, m_staging_buffer->GetBuffer(), 1, |
| 923 | &image_copy); |
| 924 | |
| 925 | // Restore old source texture layout. |
| 926 | src_tex->TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(), old_layout); |
| 927 | |
| 928 | m_needs_flush = true; |
| 929 | m_flush_fence_counter = g_command_buffer_mgr->GetCurrentFenceCounter(); |
| 930 | } |
| 931 | |
| 932 | void VKStagingTexture::CopyFromTextureToLinearImage(const VKTexture* src_tex, |
| 933 | const MathUtil::Rectangle<int>& src_rect, |
| 934 | u32 src_layer, u32 src_level, |
| 935 | const MathUtil::Rectangle<int>& dst_rect) |
| 936 | { |
| 937 | // The proprietary Qualcomm driver allocates a temporary image when copying from an image |
| 938 | // with optimal tiling (VK_IMAGE_TILING_OPTIMAL) to a buffer. |
| 939 | // That allocation is very slow, so we just do it ourself and reuse the intermediate image. |
| 940 | |
| 941 | const VkImageAspectFlags aspect = VKTexture::GetImageViewAspectForFormat(src_tex->GetFormat()); |
| 942 | |
| 943 | VkImageMemoryBarrier linear_image_barrier = {}; |
| 944 | linear_image_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 945 | linear_image_barrier.pNext = nullptr; |
| 946 | linear_image_barrier.srcAccessMask = 0; |
| 947 | linear_image_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_TRANSFER_READ_BIT; |
| 948 | linear_image_barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 949 | linear_image_barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 950 | linear_image_barrier.image = m_linear_image; |
| 951 | linear_image_barrier.subresourceRange = {aspect, 0, 1, 0, 1}; |
| 952 | vkCmdPipelineBarrier(g_command_buffer_mgr->GetCurrentCommandBuffer(), |
| 953 | VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, |
| 954 | nullptr, 0, nullptr, 1, &linear_image_barrier); |
| 955 | |
| 956 | VkImageBlit blit; |
| 957 | blit.srcSubresource = {aspect, src_level, src_layer, 1}; |
| 958 | blit.dstSubresource.layerCount = 1; |
| 959 | blit.dstSubresource.baseArrayLayer = 0; |
| 960 | blit.dstSubresource.mipLevel = 0; |
| 961 | blit.dstSubresource.aspectMask = linear_image_barrier.subresourceRange.aspectMask; |
| 962 | blit.srcOffsets[0] = {src_rect.left, src_rect.top, 0}; |
| 963 | blit.srcOffsets[1] = {static_cast<s32>(blit.srcOffsets[0].x + src_rect.GetWidth()), |
| 964 | static_cast<s32>(blit.srcOffsets[0].y + src_rect.GetHeight()), 1}; |
| 965 | blit.dstOffsets[0] = {0, 0, 0}; |
| 966 | blit.dstOffsets[1] = {dst_rect.GetWidth(), dst_rect.GetHeight(), 1u}; |
| 967 | |
| 968 | vkCmdBlitImage(g_command_buffer_mgr->GetCurrentCommandBuffer(), src_tex->GetImage(), |
| 969 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, m_linear_image, |
| 970 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit, VK_FILTER_NEAREST); |
| 971 | |
| 972 | linear_image_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 973 | linear_image_barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 974 | linear_image_barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; |
| 975 | |
| 976 | vkCmdPipelineBarrier(g_command_buffer_mgr->GetCurrentCommandBuffer(), |
| 977 | VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, |
| 978 | nullptr, 0, nullptr, 1, &linear_image_barrier); |
| 979 | } |
| 980 | |
| 981 | void VKStagingTexture::CopyToTexture(const MathUtil::Rectangle<int>& src_rect, AbstractTexture* dst, |
| 982 | const MathUtil::Rectangle<int>& dst_rect, u32 dst_layer, |
| 983 | u32 dst_level) |
| 984 | { |
| 985 | const VKTexture* dst_tex = static_cast<const VKTexture*>(dst); |
| 986 | ASSERT(m_type == StagingTextureType::Upload || m_type == StagingTextureType::Mutable); |
| 987 | ASSERT(src_rect.GetWidth() == dst_rect.GetWidth() && |
| 988 | src_rect.GetHeight() == dst_rect.GetHeight()); |
| 989 | ASSERT(src_rect.left >= 0 && static_cast<u32>(src_rect.right) <= m_config.width && |
| 990 | src_rect.top >= 0 && static_cast<u32>(src_rect.bottom) <= m_config.height); |
| 991 | ASSERT(dst_rect.left >= 0 && static_cast<u32>(dst_rect.right) <= dst_tex->GetWidth() && |
| 992 | dst_rect.top >= 0 && static_cast<u32>(dst_rect.bottom) <= dst_tex->GetHeight()); |
| 993 | |
| 994 | // Flush caches before copying. |
| 995 | m_staging_buffer->FlushCPUCache(); |
| 996 | StateTracker::GetInstance()->EndRenderPass(); |
| 997 | |
| 998 | VkImageLayout old_layout = dst_tex->GetLayout(); |
| 999 | dst_tex->TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(), |
| 1000 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); |
| 1001 | |
| 1002 | // Issue the image->buffer copy, but delay it for now. |
| 1003 | VkBufferImageCopy image_copy = {}; |
| 1004 | image_copy.bufferOffset = |
| 1005 | static_cast<VkDeviceSize>(static_cast<size_t>(src_rect.top) * m_config.GetStride() + |
| 1006 | static_cast<size_t>(src_rect.left) * m_texel_size); |
| 1007 | image_copy.bufferRowLength = static_cast<u32>(m_config.width); |
| 1008 | image_copy.bufferImageHeight = 0; |
| 1009 | image_copy.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, dst_level, dst_layer, 1}; |
| 1010 | image_copy.imageOffset = {dst_rect.left, dst_rect.top, 0}; |
| 1011 | image_copy.imageExtent = {static_cast<u32>(dst_rect.GetWidth()), |
| 1012 | static_cast<u32>(dst_rect.GetHeight()), 1u}; |
| 1013 | vkCmdCopyBufferToImage(g_command_buffer_mgr->GetCurrentCommandBuffer(), |
| 1014 | m_staging_buffer->GetBuffer(), dst_tex->GetImage(), |
| 1015 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &image_copy); |
| 1016 | |
| 1017 | // Restore old source texture layout. |
| 1018 | dst_tex->TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(), old_layout); |
| 1019 | |
| 1020 | m_needs_flush = true; |
| 1021 | m_flush_fence_counter = g_command_buffer_mgr->GetCurrentFenceCounter(); |
| 1022 | } |
| 1023 | |
| 1024 | bool VKStagingTexture::Map() |
| 1025 | { |
| 1026 | // Always mapped. |
| 1027 | return true; |
| 1028 | } |
| 1029 | |
| 1030 | void VKStagingTexture::Unmap() |
| 1031 | { |
| 1032 | // Always mapped. |
| 1033 | } |
| 1034 | |
| 1035 | void VKStagingTexture::Flush() |
| 1036 | { |
| 1037 | if (!m_needs_flush) |
| 1038 | return; |
| 1039 | |
| 1040 | // Is this copy in the current command buffer? |
| 1041 | if (g_command_buffer_mgr->GetCurrentFenceCounter() == m_flush_fence_counter) |
| 1042 | { |
| 1043 | // Execute the command buffer and wait for it to finish. |
| 1044 | VKGfx::GetInstance()->ExecuteCommandBuffer(false, true); |
| 1045 | } |
| 1046 | else |
| 1047 | { |
| 1048 | // Wait for the GPU to finish with it. |
| 1049 | g_command_buffer_mgr->WaitForFenceCounter(m_flush_fence_counter); |
| 1050 | } |
| 1051 | |
| 1052 | // For readback textures, invalidate the CPU cache as there is new data there. |
| 1053 | if (m_type == StagingTextureType::Readback || m_type == StagingTextureType::Mutable) |
| 1054 | m_staging_buffer->InvalidateCPUCache(); |
| 1055 | |
| 1056 | m_needs_flush = false; |
| 1057 | } |
| 1058 | |
| 1059 | VKFramebuffer::VKFramebuffer(VKTexture* color_attachment, VKTexture* depth_attachment, |
| 1060 | std::vector<AbstractTexture*> additional_color_attachments, u32 width, |
| 1061 | u32 height, u32 layers, u32 samples, VkFramebuffer fb, |
| 1062 | VkRenderPass load_render_pass, VkRenderPass discard_render_pass, |
| 1063 | VkRenderPass clear_render_pass) |
| 1064 | : AbstractFramebuffer( |
| 1065 | color_attachment, depth_attachment, std::move(additional_color_attachments), |
| 1066 | color_attachment ? color_attachment->GetFormat() : AbstractTextureFormat::Undefined, |
| 1067 | depth_attachment ? depth_attachment->GetFormat() : AbstractTextureFormat::Undefined, |
| 1068 | width, height, layers, samples), |
| 1069 | m_fb(fb), m_load_render_pass(load_render_pass), m_discard_render_pass(discard_render_pass), |
| 1070 | m_clear_render_pass(clear_render_pass) |
| 1071 | { |
| 1072 | } |
| 1073 | |
| 1074 | VKFramebuffer::~VKFramebuffer() |
| 1075 | { |
| 1076 | g_command_buffer_mgr->DeferFramebufferDestruction(m_fb); |
| 1077 | } |
| 1078 | |
| 1079 | std::unique_ptr<VKFramebuffer> |
| 1080 | VKFramebuffer::Create(VKTexture* color_attachment, VKTexture* depth_attachment, |
| 1081 | std::vector<AbstractTexture*> additional_color_attachments) |
| 1082 | { |
| 1083 | if (!ValidateConfig(color_attachment, depth_attachment, additional_color_attachments)) |
| 1084 | return nullptr; |
| 1085 | |
| 1086 | const VkFormat vk_color_format = |
| 1087 | color_attachment ? color_attachment->GetVkFormat() : VK_FORMAT_UNDEFINED; |
| 1088 | const VkFormat vk_depth_format = |
| 1089 | depth_attachment ? depth_attachment->GetVkFormat() : VK_FORMAT_UNDEFINED; |
| 1090 | const VKTexture* either_attachment = color_attachment ? color_attachment : depth_attachment; |
| 1091 | const u32 width = either_attachment->GetWidth(); |
| 1092 | const u32 height = either_attachment->GetHeight(); |
| 1093 | const u32 layers = either_attachment->GetLayers(); |
| 1094 | const u32 samples = either_attachment->GetSamples(); |
| 1095 | |
| 1096 | std::vector<VkImageView> attachment_views; |
| 1097 | if (color_attachment) |
| 1098 | attachment_views.push_back(color_attachment->GetView()); |
| 1099 | |
| 1100 | if (depth_attachment) |
| 1101 | attachment_views.push_back(depth_attachment->GetView()); |
| 1102 | |
| 1103 | for (auto* attachment : additional_color_attachments) |
| 1104 | { |
| 1105 | attachment_views.push_back(static_cast<VKTexture*>(attachment)->GetView()); |
| 1106 | } |
| 1107 | |
| 1108 | VkRenderPass load_render_pass = g_object_cache->GetRenderPass( |
| 1109 | vk_color_format, vk_depth_format, samples, VK_ATTACHMENT_LOAD_OP_LOAD, |
| 1110 | static_cast<u8>(additional_color_attachments.size())); |
| 1111 | VkRenderPass discard_render_pass = g_object_cache->GetRenderPass( |
| 1112 | vk_color_format, vk_depth_format, samples, VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
| 1113 | static_cast<u8>(additional_color_attachments.size())); |
| 1114 | VkRenderPass clear_render_pass = g_object_cache->GetRenderPass( |
| 1115 | vk_color_format, vk_depth_format, samples, VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 1116 | static_cast<u8>(additional_color_attachments.size())); |
| 1117 | if (load_render_pass == VK_NULL_HANDLE || discard_render_pass == VK_NULL_HANDLE || |
| 1118 | clear_render_pass == VK_NULL_HANDLE) |
| 1119 | { |
| 1120 | return nullptr; |
| 1121 | } |
| 1122 | |
| 1123 | VkFramebufferCreateInfo framebuffer_info = {VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, |
| 1124 | nullptr, |
| 1125 | 0, |
| 1126 | load_render_pass, |
| 1127 | static_cast<uint32_t>(attachment_views.size()), |
| 1128 | attachment_views.data(), |
| 1129 | width, |
| 1130 | height, |
| 1131 | layers}; |
| 1132 | |
| 1133 | VkFramebuffer fb; |
| 1134 | VkResult res = |
| 1135 | vkCreateFramebuffer(g_vulkan_context->GetDevice(), &framebuffer_info, nullptr, &fb); |
| 1136 | if (res != VK_SUCCESS) |
| 1137 | { |
| 1138 | LOG_VULKAN_ERROR(res, "vkCreateFramebuffer failed: "); |
| 1139 | return nullptr; |
| 1140 | } |
| 1141 | |
| 1142 | return std::make_unique<VKFramebuffer>( |
| 1143 | color_attachment, depth_attachment, std::move(additional_color_attachments), width, height, |
| 1144 | layers, samples, fb, load_render_pass, discard_render_pass, clear_render_pass); |
| 1145 | } |
| 1146 | |
| 1147 | void VKFramebuffer::Unbind() |
| 1148 | { |
| 1149 | if (m_color_attachment) |
| 1150 | { |
| 1151 | StateTracker::GetInstance()->UnbindTexture( |
| 1152 | static_cast<VKTexture*>(m_color_attachment)->GetView()); |
| 1153 | } |
| 1154 | for (auto* attachment : m_additional_color_attachments) |
| 1155 | { |
| 1156 | StateTracker::GetInstance()->UnbindTexture(static_cast<VKTexture*>(attachment)->GetView()); |
| 1157 | } |
| 1158 | if (m_depth_attachment) |
| 1159 | { |
| 1160 | StateTracker::GetInstance()->UnbindTexture( |
| 1161 | static_cast<VKTexture*>(m_depth_attachment)->GetView()); |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | void VKFramebuffer::TransitionForRender() |
| 1166 | { |
| 1167 | VkCommandBuffer cb = g_command_buffer_mgr->GetCurrentCommandBuffer(); |
| 1168 | if (m_color_attachment) |
| 1169 | { |
| 1170 | static_cast<VKTexture*>(m_color_attachment) |
| 1171 | ->TransitionToLayout(cb, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
| 1172 | } |
| 1173 | for (auto* attachment : m_additional_color_attachments) |
| 1174 | { |
| 1175 | static_cast<VKTexture*>(attachment) |
| 1176 | ->TransitionToLayout(cb, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
| 1177 | } |
| 1178 | |
| 1179 | if (m_depth_attachment) |
| 1180 | { |
| 1181 | static_cast<VKTexture*>(m_depth_attachment) |
| 1182 | ->TransitionToLayout(cb, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | void VKFramebuffer::PrepareForRenderPass() |
| 1187 | { |
| 1188 | VkCommandBuffer cb = g_command_buffer_mgr->GetCurrentCommandBuffer(); |
| 1189 | if (m_color_attachment) |
| 1190 | static_cast<VKTexture*>(m_color_attachment)->PrepareForRenderPass(cb); |
| 1191 | if (m_depth_attachment) |
| 1192 | static_cast<VKTexture*>(m_depth_attachment)->PrepareForRenderPass(cb); |
| 1193 | for (auto* attachment : m_additional_color_attachments) |
| 1194 | static_cast<VKTexture*>(attachment)->PrepareForRenderPass(cb); |
| 1195 | } |
| 1196 | |
| 1197 | void VKFramebuffer::SetAndClear(const VkRect2D& rect, const VkClearValue& color_value, |
| 1198 | const VkClearValue& depth_value) |
| 1199 | { |
| 1200 | std::vector<VkClearValue> clear_values; |
| 1201 | if (GetColorFormat() != AbstractTextureFormat::Undefined) |
| 1202 | { |
| 1203 | clear_values.push_back(color_value); |
| 1204 | } |
| 1205 | if (GetDepthFormat() != AbstractTextureFormat::Undefined) |
| 1206 | { |
| 1207 | clear_values.push_back(depth_value); |
| 1208 | } |
| 1209 | for (std::size_t i = 0; i < m_additional_color_attachments.size(); i++) |
| 1210 | { |
| 1211 | clear_values.push_back(color_value); |
| 1212 | } |
| 1213 | StateTracker::GetInstance()->BeginClearRenderPass(rect, clear_values.data(), |
| 1214 | static_cast<u32>(clear_values.size())); |
| 1215 | } |
| 1216 | } // namespace Vulkan |