[impeller] [vulkan] Support textures backed by vk::Images (flutter/engine#35163)

This commit is contained in:
Kaushik Iska
2022-08-05 10:38:15 -07:00
committed by GitHub
parent bfd0781318
commit 42b72a6166
4 changed files with 143 additions and 3 deletions

View File

@@ -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 <memory>
@@ -61,7 +63,47 @@ bool AllocatorVK::IsValid() const {
std::shared_ptr<Texture> 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<vk::ImageCreateInfo::NativeType>(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<TextureVK>(desc, context_, allocator_, img,
allocation, allocation_info);
}
// |Allocator|

View File

@@ -132,4 +132,32 @@ constexpr std::optional<vk::ShaderStageFlagBits> 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

View File

@@ -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<const fml::Mapping> 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

View File

@@ -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<TextureVK, Texture> {
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;