From 2107af0396676ed956d5be9905d9bb58571f4071 Mon Sep 17 00:00:00 2001 From: Colin Blundell Date: Wed, 10 May 2023 09:08:50 +0000 Subject: [PATCH] [Vulkan] Add support for transient attachments This CL adds support for the transient attachment feature and texture usage flag to Vulkan, using the usage flag to allocate textures via lazily-allocated memory if the latter is available. Testing is covered by the MultisampledRenderingWithTransientAttachmentTest now being run on Vulkan due to transient attachments feature now being supported on Vulkan. Bug: dawn:1695 Change-Id: I45a04d21b1b6ea612086a368b9c77a0ff43e93f8 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/130180 Reviewed-by: Loko Kung Kokoro: Kokoro Commit-Queue: Colin Blundell --- src/dawn/native/vulkan/PhysicalDeviceVk.cpp | 1 + .../vulkan/ResourceMemoryAllocatorVk.cpp | 18 +++++++++++++++++- .../native/vulkan/ResourceMemoryAllocatorVk.h | 1 + src/dawn/native/vulkan/TextureVk.cpp | 10 +++++----- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/dawn/native/vulkan/PhysicalDeviceVk.cpp b/src/dawn/native/vulkan/PhysicalDeviceVk.cpp index affb18240a..fafd7a05a9 100644 --- a/src/dawn/native/vulkan/PhysicalDeviceVk.cpp +++ b/src/dawn/native/vulkan/PhysicalDeviceVk.cpp @@ -265,6 +265,7 @@ void PhysicalDevice::InitializeSupportedFeaturesImpl() { #endif // DAWN_PLATFORM_IS(ANDROID) || DAWN_PLATFORM_IS(CHROMEOS) EnableFeature(Feature::SurfaceCapabilities); + EnableFeature(Feature::TransientAttachments); } MaybeError PhysicalDevice::InitializeSupportedLimitsImpl(CombinedLimits* limits) { diff --git a/src/dawn/native/vulkan/ResourceMemoryAllocatorVk.cpp b/src/dawn/native/vulkan/ResourceMemoryAllocatorVk.cpp index ba1816eb26..e8466a7588 100644 --- a/src/dawn/native/vulkan/ResourceMemoryAllocatorVk.cpp +++ b/src/dawn/native/vulkan/ResourceMemoryAllocatorVk.cpp @@ -258,7 +258,23 @@ int ResourceMemoryAllocator::FindBestTypeIndex(VkMemoryRequirements requirements continue; } - // For non-mappable resources, favor device local memory. + // For non-mappable resources that can be lazily allocated, favor lazy + // allocation (note: this is a more important property than that of + // device local memory and hence is checked first). + bool currentLazilyAllocated = + info.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT; + bool bestLazilyAllocated = + info.memoryTypes[bestType].propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT; + if ((kind == MemoryKind::LazilyAllocated) && + (currentLazilyAllocated != bestLazilyAllocated)) { + if (currentLazilyAllocated) { + bestType = static_cast(i); + } + continue; + } + + // For non-mappable, non-lazily-allocated resources, favor device local + // memory. bool currentDeviceLocal = info.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; bool bestDeviceLocal = diff --git a/src/dawn/native/vulkan/ResourceMemoryAllocatorVk.h b/src/dawn/native/vulkan/ResourceMemoryAllocatorVk.h index 2be1895980..452819cf71 100644 --- a/src/dawn/native/vulkan/ResourceMemoryAllocatorVk.h +++ b/src/dawn/native/vulkan/ResourceMemoryAllocatorVk.h @@ -32,6 +32,7 @@ class Device; // Various kinds of memory that influence the result of the allocation. For example, to take // into account mappability and Vulkan's bufferImageGranularity. enum class MemoryKind { + LazilyAllocated, Linear, LinearMappable, Opaque, diff --git a/src/dawn/native/vulkan/TextureVk.cpp b/src/dawn/native/vulkan/TextureVk.cpp index ebd138c6a0..f1bb1ce402 100644 --- a/src/dawn/native/vulkan/TextureVk.cpp +++ b/src/dawn/native/vulkan/TextureVk.cpp @@ -494,8 +494,6 @@ VkImageUsageFlags VulkanImageUsage(wgpu::TextureUsage usage, const Format& forma } else { flags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; } - // TODO(1695): Check for TransientAttachment flag being present and - // convert it into the proper Vulkan flag. } // Choosing Vulkan image usages should not know about kReadOnlyRenderAttachment because that's @@ -745,9 +743,11 @@ MaybeError Texture::InitializeAsInternalTexture(VkImageUsageFlags extraUsages) { Toggle::DisableSubAllocationFor2DTextureWithCopyDstOrRenderAttachment)) && GetDimension() == wgpu::TextureDimension::e2D && (GetInternalUsage() & (wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::RenderAttachment)); - DAWN_TRY_ASSIGN(mMemoryAllocation, - device->GetResourceMemoryAllocator()->Allocate(requirements, MemoryKind::Opaque, - forceDisableSubAllocation)); + auto memoryKind = (GetInternalUsage() & wgpu::TextureUsage::TransientAttachment) + ? MemoryKind::LazilyAllocated + : MemoryKind::Opaque; + DAWN_TRY_ASSIGN(mMemoryAllocation, device->GetResourceMemoryAllocator()->Allocate( + requirements, memoryKind, forceDisableSubAllocation)); DAWN_TRY(CheckVkSuccess( device->fn.BindImageMemory(device->GetVkDevice(), mHandle,