Flutter Impeller
impeller::AllocatedTextureSourceVK Class Referencefinal
Inheritance diagram for impeller::AllocatedTextureSourceVK:
impeller::TextureSourceVK

Public Member Functions

 AllocatedTextureSourceVK (std::weak_ptr< ResourceManagerVK > resource_manager, const TextureDescriptor &desc, VmaAllocator allocator, vk::Device device, bool supports_memoryless_textures)
 
 ~AllocatedTextureSourceVK ()=default
 
bool IsValid () const
 
vk::Image GetImage () const override
 
vk::ImageView GetImageView () const override
 
- Public Member Functions inherited from impeller::TextureSourceVK
virtual ~TextureSourceVK ()
 
const TextureDescriptorGetTextureDescriptor () const
 
fml::Status SetLayout (const BarrierVK &barrier) const
 
vk::ImageLayout SetLayoutWithoutEncoding (vk::ImageLayout layout) const
 
vk::ImageLayout GetLayout () const
 

Additional Inherited Members

- Protected Member Functions inherited from impeller::TextureSourceVK
 TextureSourceVK (TextureDescriptor desc)
 
- Protected Attributes inherited from impeller::TextureSourceVK
const TextureDescriptor desc_
 

Detailed Description

Definition at line 260 of file allocator_vk.cc.

Constructor & Destructor Documentation

◆ AllocatedTextureSourceVK()

impeller::AllocatedTextureSourceVK::AllocatedTextureSourceVK ( std::weak_ptr< ResourceManagerVK resource_manager,
const TextureDescriptor desc,
VmaAllocator  allocator,
vk::Device  device,
bool  supports_memoryless_textures 
)
inline

Definition at line 262 of file allocator_vk.cc.

267  : TextureSourceVK(desc), resource_(std::move(resource_manager)) {
268  TRACE_EVENT0("impeller", "CreateDeviceTexture");
269  vk::ImageCreateInfo image_info;
270  image_info.flags = ToVKImageCreateFlags(desc.type);
271  image_info.imageType = vk::ImageType::e2D;
272  image_info.format = ToVKImageFormat(desc.format);
273  image_info.extent = VkExtent3D{
274  static_cast<uint32_t>(desc.size.width), // width
275  static_cast<uint32_t>(desc.size.height), // height
276  1u // depth
277  };
278  image_info.samples = ToVKSampleCount(desc.sample_count);
279  image_info.mipLevels = desc.mip_count;
280  image_info.arrayLayers = ToArrayLayerCount(desc.type);
281  image_info.tiling = vk::ImageTiling::eOptimal;
282  image_info.initialLayout = vk::ImageLayout::eUndefined;
283  image_info.usage =
284  ToVKImageUsageFlags(desc.format, desc.usage, desc.storage_mode,
285  supports_memoryless_textures);
286  image_info.sharingMode = vk::SharingMode::eExclusive;
287 
288  VmaAllocationCreateInfo alloc_nfo = {};
289 
290  alloc_nfo.usage = ToVMAMemoryUsage();
291  alloc_nfo.preferredFlags =
292  static_cast<VkMemoryPropertyFlags>(ToVKTextureMemoryPropertyFlags(
293  desc.storage_mode, supports_memoryless_textures));
294  alloc_nfo.flags = ToVmaAllocationCreateFlags(desc.storage_mode);
295 
296  auto create_info_native =
297  static_cast<vk::ImageCreateInfo::NativeType>(image_info);
298 
299  VkImage vk_image = VK_NULL_HANDLE;
300  VmaAllocation allocation = {};
301  VmaAllocationInfo allocation_info = {};
302  {
303  auto result = vk::Result{::vmaCreateImage(allocator, //
304  &create_info_native, //
305  &alloc_nfo, //
306  &vk_image, //
307  &allocation, //
308  &allocation_info //
309  )};
310  if (result != vk::Result::eSuccess) {
311  VALIDATION_LOG << "Unable to allocate Vulkan Image: "
312  << vk::to_string(result)
313  << " Type: " << TextureTypeToString(desc.type)
314  << " Mode: " << StorageModeToString(desc.storage_mode)
315  << " Usage: " << TextureUsageMaskToString(desc.usage)
316  << " [VK]Flags: " << vk::to_string(image_info.flags)
317  << " [VK]Format: " << vk::to_string(image_info.format)
318  << " [VK]Usage: " << vk::to_string(image_info.usage)
319  << " [VK]Mem. Flags: "
320  << vk::to_string(vk::MemoryPropertyFlags(
321  alloc_nfo.preferredFlags));
322  return;
323  }
324  }
325 
326  auto image = vk::Image{vk_image};
327 
328  vk::ImageViewCreateInfo view_info = {};
329  view_info.image = image;
330  view_info.viewType = ToVKImageViewType(desc.type);
331  view_info.format = image_info.format;
332  view_info.subresourceRange.aspectMask = ToVKImageAspectFlags(desc.format);
333  view_info.subresourceRange.levelCount = image_info.mipLevels;
334  view_info.subresourceRange.layerCount = ToArrayLayerCount(desc.type);
335 
336  // Vulkan does not have an image format that is equivalent to
337  // `MTLPixelFormatA8Unorm`, so we use `R8Unorm` instead. Given that the
338  // shaders expect that alpha channel to be set in the cases, we swizzle.
339  // See: https://github.com/flutter/flutter/issues/115461 for more details.
340  if (desc.format == PixelFormat::kA8UNormInt) {
341  view_info.components.a = vk::ComponentSwizzle::eR;
342  view_info.components.r = vk::ComponentSwizzle::eA;
343  }
344 
345  auto [result, image_view] = device.createImageViewUnique(view_info);
346  if (result != vk::Result::eSuccess) {
347  VALIDATION_LOG << "Unable to create an image view for allocation: "
348  << vk::to_string(result);
349  return;
350  }
351  resource_.Swap(ImageResource(ImageVMA{allocator, allocation, image},
352  std::move(image_view)));
353  is_valid_ = true;
354  }

References impeller::TextureDescriptor::format, impeller::TSize< T >::height, impeller::TextureDescriptor::mip_count, impeller::TextureDescriptor::sample_count, impeller::TextureDescriptor::size, impeller::TextureDescriptor::storage_mode, impeller::StorageModeToString(), impeller::TextureTypeToString(), impeller::TextureUsageMaskToString(), impeller::ToArrayLayerCount(), impeller::ToVKImageAspectFlags(), impeller::ToVKImageCreateFlags(), impeller::ToVKImageFormat(), impeller::ToVKImageUsageFlags(), impeller::ToVKImageViewType(), impeller::ToVKSampleCount(), impeller::ToVKTextureMemoryPropertyFlags(), impeller::ToVmaAllocationCreateFlags(), impeller::ToVMAMemoryUsage(), impeller::TextureDescriptor::type, impeller::TextureDescriptor::usage, VALIDATION_LOG, and impeller::TSize< T >::width.

◆ ~AllocatedTextureSourceVK()

impeller::AllocatedTextureSourceVK::~AllocatedTextureSourceVK ( )
default

Member Function Documentation

◆ GetImage()

vk::Image impeller::AllocatedTextureSourceVK::GetImage ( ) const
inlineoverridevirtual

Implements impeller::TextureSourceVK.

Definition at line 360 of file allocator_vk.cc.

360 { return resource_->image.get().image; }

◆ GetImageView()

vk::ImageView impeller::AllocatedTextureSourceVK::GetImageView ( ) const
inlineoverridevirtual

Implements impeller::TextureSourceVK.

Definition at line 362 of file allocator_vk.cc.

362  {
363  return resource_->image_view.get();
364  }

◆ IsValid()

bool impeller::AllocatedTextureSourceVK::IsValid ( ) const
inline

Definition at line 358 of file allocator_vk.cc.

358 { return is_valid_; }

The documentation for this class was generated from the following file:
impeller::ToVKSampleCount
constexpr vk::SampleCountFlagBits ToVKSampleCount(SampleCount sample_count)
Definition: formats_vk.h:204
impeller::TextureSourceVK::TextureSourceVK
TextureSourceVK(TextureDescriptor desc)
Definition: texture_source_vk.cc:9
impeller::ToVKTextureMemoryPropertyFlags
static constexpr vk::Flags< vk::MemoryPropertyFlagBits > ToVKTextureMemoryPropertyFlags(StorageMode mode, bool supports_memoryless_textures)
Definition: allocator_vk.cc:229
impeller::ToArrayLayerCount
constexpr uint32_t ToArrayLayerCount(TextureType type)
Definition: formats_vk.h:591
impeller::UniqueResourceVKT::Swap
void Swap(ResourceType &&other)
Reclaims the existing resource, if any, and replaces it.
Definition: resource_manager_vk.h:178
impeller::ToVKImageCreateFlags
constexpr vk::ImageCreateFlags ToVKImageCreateFlags(TextureType type)
Definition: formats_vk.h:619
impeller::ToVKImageFormat
constexpr vk::Format ToVKImageFormat(PixelFormat format)
Definition: formats_vk.h:136
impeller::StorageModeToString
constexpr const char * StorageModeToString(StorageMode mode)
Definition: formats.h:55
impeller::ToVKImageViewType
constexpr vk::ImageViewType ToVKImageViewType(TextureType type)
Definition: formats_vk.h:605
impeller::ToVKImageAspectFlags
constexpr vk::ImageAspectFlags ToVKImageAspectFlags(PixelFormat format)
Definition: formats_vk.h:565
impeller::TextureUsageMaskToString
std::string TextureUsageMaskToString(TextureUsageMask mask)
Definition: formats.cc:81
impeller::ToVMAMemoryUsage
static constexpr VmaMemoryUsage ToVMAMemoryUsage()
Definition: allocator_vk.cc:224
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
impeller::TextureTypeToString
constexpr const char * TextureTypeToString(TextureType type)
Definition: formats.h:243
impeller::ToVKImageUsageFlags
static constexpr vk::ImageUsageFlags ToVKImageUsageFlags(PixelFormat format, TextureUsageMask usage, StorageMode mode, bool supports_memoryless_textures)
Definition: allocator_vk.cc:166
impeller::PixelFormat::kA8UNormInt
@ kA8UNormInt
impeller::ToVmaAllocationCreateFlags
static VmaAllocationCreateFlags ToVmaAllocationCreateFlags(StorageMode mode)
Definition: allocator_vk.cc:247