From 42b72a61667d94ed095f768fddec8b600a72f920 Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Fri, 5 Aug 2022 10:38:15 -0700 Subject: [PATCH] [impeller] [vulkan] Support textures backed by `vk::Image`s (flutter/engine#35163) --- .../renderer/backend/vulkan/allocator_vk.cc | 44 +++++++++++++- .../renderer/backend/vulkan/formats_vk.h | 28 +++++++++ .../renderer/backend/vulkan/texture_vk.cc | 59 ++++++++++++++++++- .../renderer/backend/vulkan/texture_vk.h | 15 ++++- 4 files changed, 143 insertions(+), 3 deletions(-) diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/allocator_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/allocator_vk.cc index c572363e6d..ade9dd215f 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/allocator_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/allocator_vk.cc @@ -10,6 +10,8 @@ _Pragma("GCC diagnostic ignored \"-Wthread-safety-analysis\""); #define VMA_IMPLEMENTATION #include "impeller/renderer/backend/vulkan/allocator_vk.h" #include "impeller/renderer/backend/vulkan/device_buffer_vk.h" +#include "impeller/renderer/backend/vulkan/formats_vk.h" +#include "impeller/renderer/backend/vulkan/texture_vk.h" #include @@ -61,7 +63,47 @@ bool AllocatorVK::IsValid() const { std::shared_ptr AllocatorVK::CreateTexture( StorageMode mode, const TextureDescriptor& desc) { - FML_UNREACHABLE(); + auto image_create_info = vk::ImageCreateInfo{}; + image_create_info.imageType = vk::ImageType::e2D; + image_create_info.format = ToVKImageFormat(desc.format); + image_create_info.extent.width = desc.size.width; + image_create_info.extent.height = desc.size.height; + image_create_info.samples = ToVKSampleCount(desc.sample_count); + image_create_info.mipLevels = desc.mip_count; + + // TODO (kaushikiska): should we read these from desc? + image_create_info.extent.depth = 1; + image_create_info.arrayLayers = 1; + + image_create_info.tiling = vk::ImageTiling::eOptimal; + image_create_info.initialLayout = vk::ImageLayout::eUndefined; + image_create_info.usage = vk::ImageUsageFlagBits::eSampled | + vk::ImageUsageFlagBits::eColorAttachment; + + VmaAllocationCreateInfo alloc_create_info = {}; + alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO; + // docs recommend using `VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT` for image + // allocations, but setting them to be host visible for now. + alloc_create_info.flags = + VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | + VMA_ALLOCATION_CREATE_MAPPED_BIT; + + auto create_info_native = + static_cast(image_create_info); + + VkImage img; + VmaAllocation allocation; + VmaAllocationInfo allocation_info; + auto result = vk::Result{vmaCreateImage(allocator_, &create_info_native, + &alloc_create_info, &img, &allocation, + &allocation_info)}; + if (result != vk::Result::eSuccess) { + VALIDATION_LOG << "Unable to allocate an image"; + return nullptr; + } + + return std::make_shared(desc, context_, allocator_, img, + allocation, allocation_info); } // |Allocator| diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/formats_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/formats_vk.h index 64becdf10d..a3412715d4 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/formats_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/formats_vk.h @@ -132,4 +132,32 @@ constexpr std::optional ToVKShaderStageFlagBits( FML_UNREACHABLE(); } +constexpr vk::Format ToVKImageFormat(PixelFormat format) { + switch (format) { + case PixelFormat::kUnknown: + return vk::Format::eUndefined; + case PixelFormat::kA8UNormInt: + return vk::Format::eA8B8G8R8UnormPack32; + case PixelFormat::kR8G8B8A8UNormInt: + return vk::Format::eR8G8B8A8Unorm; + case PixelFormat::kR8G8B8A8UNormIntSRGB: + return vk::Format::eR8G8B8A8Srgb; + case PixelFormat::kB8G8R8A8UNormInt: + return vk::Format::eB8G8R8A8Unorm; + case PixelFormat::kB8G8R8A8UNormIntSRGB: + return vk::Format::eB8G8R8A8Srgb; + case PixelFormat::kS8UInt: + return vk::Format::eS8Uint; + } +} + +constexpr vk::SampleCountFlagBits ToVKSampleCount(SampleCount sample_count) { + switch (sample_count) { + case SampleCount::kCount1: + return vk::SampleCountFlagBits::e1; + case SampleCount::kCount4: + return vk::SampleCountFlagBits::e4; + } +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.cc index 7d29259d81..8d46611667 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.cc @@ -6,6 +6,63 @@ namespace impeller { -// +TextureVK::TextureVK(TextureDescriptor desc, + ContextVK& context, + const VmaAllocator& allocator, + VkImage image, + VmaAllocation allocation, + VmaAllocationInfo allocation_info) + : Texture(desc), + context_(context), + allocator_(allocator), + image_(image), + allocation_(allocation), + allocation_info_(allocation_info) {} + +TextureVK::~TextureVK() { + if (image_) { + vmaDestroyImage(allocator_, image_, allocation_); + } +} + +void TextureVK::SetLabel(std::string_view label) { + context_.SetDebugName(vk::Image{image_}, label); +} + +bool TextureVK::OnSetContents(const uint8_t* contents, + size_t length, + size_t slice) { + if (!image_ || !contents) { + return false; + } + + const auto& desc = GetTextureDescriptor(); + + // Out of bounds access. + if (length != desc.GetByteSizeOfBaseMipLevel()) { + VALIDATION_LOG << "illegal to set contents for invalid size"; + return false; + } + + // currently we are only supporting 2d textures, no cube textures etc. + memcpy(allocation_info_.pMappedData, contents, length); + + return true; +} + +bool TextureVK::OnSetContents(std::shared_ptr mapping, + size_t slice) { + // Vulkan has no threading restrictions. So we can pass this data along to the + // client rendering API immediately. + return OnSetContents(mapping->GetMapping(), mapping->GetSize(), slice); +} + +bool TextureVK::IsValid() const { + return image_; +} + +ISize TextureVK::GetSize() const { + return GetTextureDescriptor().size; +} } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.h index f4bc6ad4f8..0bc53f7dc6 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/texture_vk.h @@ -6,18 +6,31 @@ #include "flutter/fml/macros.h" #include "impeller/base/backend_cast.h" +#include "impeller/renderer/backend/vulkan/context_vk.h" +#include "impeller/renderer/backend/vulkan/vk.h" #include "impeller/renderer/texture.h" namespace impeller { class TextureVK final : public Texture, public BackendCast { public: - TextureVK(TextureDescriptor desc); + TextureVK(TextureDescriptor desc, + ContextVK& context, + const VmaAllocator& allocator, + VkImage image, + VmaAllocation allocation, + VmaAllocationInfo allocation_info); // |Texture| ~TextureVK() override; private: + ContextVK& context_; + const VmaAllocator& allocator_; + VkImage image_; + VmaAllocation allocation_; + VmaAllocationInfo allocation_info_; + // |Texture| void SetLabel(std::string_view label) override;