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

Public Member Functions

 AllocatedTextureSourceVK (const ContextVK &context, const TextureDescriptor &desc, VmaAllocator allocator, vk::Device device, bool supports_memoryless_textures)
 
 ~AllocatedTextureSourceVK ()=default
 
bool IsValid () const
 
vk::Image GetImage () const override
 Get the image handle for this texture source. More...
 
vk::ImageView GetImageView () const override
 Retrieve the image view used for sampling/blitting/compute with this texture source. More...
 
vk::ImageView GetRenderTargetView () const override
 Retrieve the image view used for render target attachments with this texture source. More...
 
bool IsSwapchainImage () const override
 Determines if swapchain image. That is, an image used as the root render target. More...
 
- Public Member Functions inherited from impeller::TextureSourceVK
virtual ~TextureSourceVK ()
 
const TextureDescriptorGetTextureDescriptor () const
 Gets the texture descriptor for this image source. More...
 
fml::Status SetLayout (const BarrierVK &barrier) const
 Encodes the layout transition barrier to barrier.cmd_buffer for the image. More...
 
vk::ImageLayout SetLayoutWithoutEncoding (vk::ImageLayout layout) const
 Store the layout of the image. More...
 
vk::ImageLayout GetLayout () const
 Get the last layout assigned to the TextureSourceVK. More...
 
virtual std::shared_ptr< YUVConversionVKGetYUVConversion () const
 When sampling from textures whose formats are not known to Vulkan, a custom conversion is necessary to setup custom samplers. This accessor provides this conversion if one is present. Most texture source have none. More...
 
void SetCachedFramebuffer (const SharedHandleVK< vk::Framebuffer > &framebuffer)
 
void SetCachedRenderPass (const SharedHandleVK< vk::RenderPass > &render_pass)
 
SharedHandleVK< vk::Framebuffer > GetCachedFramebuffer () const
 
SharedHandleVK< vk::RenderPass > GetCachedRenderPass () 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 282 of file allocator_vk.cc.

Constructor & Destructor Documentation

◆ AllocatedTextureSourceVK()

impeller::AllocatedTextureSourceVK::AllocatedTextureSourceVK ( const ContextVK context,
const TextureDescriptor desc,
VmaAllocator  allocator,
vk::Device  device,
bool  supports_memoryless_textures 
)
inline

Definition at line 284 of file allocator_vk.cc.

289  : TextureSourceVK(desc), resource_(context.GetResourceManager()) {
290  FML_DCHECK(desc.format != PixelFormat::kUnknown);
291  vk::StructureChain<vk::ImageCreateInfo, vk::ImageCompressionControlEXT>
292  image_info_chain;
293  auto& image_info = image_info_chain.get();
294  image_info.flags = ToVKImageCreateFlags(desc.type);
295  image_info.imageType = vk::ImageType::e2D;
296  image_info.format = ToVKImageFormat(desc.format);
297  image_info.extent = VkExtent3D{
298  static_cast<uint32_t>(desc.size.width), // width
299  static_cast<uint32_t>(desc.size.height), // height
300  1u // depth
301  };
302  image_info.samples = ToVKSampleCount(desc.sample_count);
303  image_info.mipLevels = desc.mip_count;
304  image_info.arrayLayers = ToArrayLayerCount(desc.type);
305  image_info.tiling = vk::ImageTiling::eOptimal;
306  image_info.initialLayout = vk::ImageLayout::eUndefined;
307  image_info.usage = AllocatorVK::ToVKImageUsageFlags(
308  desc.format, desc.usage, desc.storage_mode,
309  supports_memoryless_textures);
310  image_info.sharingMode = vk::SharingMode::eExclusive;
311 
312  vk::ImageCompressionFixedRateFlagsEXT frc_rates[1] = {
313  vk::ImageCompressionFixedRateFlagBitsEXT::eNone};
314 
315  const auto frc_rate =
316  CapabilitiesVK::Cast(*context.GetCapabilities())
317  .GetSupportedFRCRate(desc.compression_type,
318  FRCFormatDescriptor{image_info});
319  if (frc_rate.has_value()) {
320  // This array must not be in a temporary scope.
321  frc_rates[0] = frc_rate.value();
322 
323  auto& compression_info =
324  image_info_chain.get<vk::ImageCompressionControlEXT>();
325  compression_info.pFixedRateFlags = frc_rates;
326  compression_info.compressionControlPlaneCount = 1u;
327  compression_info.flags =
328  vk::ImageCompressionFlagBitsEXT::eFixedRateExplicit;
329  } else {
330  image_info_chain.unlink<vk::ImageCompressionControlEXT>();
331  }
332 
333  VmaAllocationCreateInfo alloc_nfo = {};
334 
335  alloc_nfo.usage = ToVMAMemoryUsage();
336  alloc_nfo.preferredFlags =
337  static_cast<VkMemoryPropertyFlags>(ToVKTextureMemoryPropertyFlags(
338  desc.storage_mode, supports_memoryless_textures));
339  alloc_nfo.flags = ToVmaAllocationCreateFlags(desc.storage_mode);
340 
341  auto create_info_native =
342  static_cast<vk::ImageCreateInfo::NativeType>(image_info);
343 
344  VkImage vk_image = VK_NULL_HANDLE;
345  VmaAllocation allocation = {};
346  VmaAllocationInfo allocation_info = {};
347  {
348  auto result = vk::Result{::vmaCreateImage(allocator, //
349  &create_info_native, //
350  &alloc_nfo, //
351  &vk_image, //
352  &allocation, //
353  &allocation_info //
354  )};
355  if (result != vk::Result::eSuccess) {
356  VALIDATION_LOG << "Unable to allocate Vulkan Image: "
357  << vk::to_string(result)
358  << " Type: " << TextureTypeToString(desc.type)
359  << " Mode: " << StorageModeToString(desc.storage_mode)
360  << " Usage: " << TextureUsageMaskToString(desc.usage)
361  << " [VK]Flags: " << vk::to_string(image_info.flags)
362  << " [VK]Format: " << vk::to_string(image_info.format)
363  << " [VK]Usage: " << vk::to_string(image_info.usage)
364  << " [VK]Mem. Flags: "
365  << vk::to_string(vk::MemoryPropertyFlags(
366  alloc_nfo.preferredFlags));
367  return;
368  }
369  }
370 
371  auto image = vk::Image{vk_image};
372 
373  vk::ImageViewCreateInfo view_info = {};
374  view_info.image = image;
375  view_info.viewType = ToVKImageViewType(desc.type);
376  view_info.format = image_info.format;
377  view_info.subresourceRange.aspectMask = ToVKImageAspectFlags(desc.format);
378  view_info.subresourceRange.levelCount = image_info.mipLevels;
379  view_info.subresourceRange.layerCount = ToArrayLayerCount(desc.type);
380 
381  // Vulkan does not have an image format that is equivalent to
382  // `MTLPixelFormatA8Unorm`, so we use `R8Unorm` instead. Given that the
383  // shaders expect that alpha channel to be set in the cases, we swizzle.
384  // See: https://github.com/flutter/flutter/issues/115461 for more details.
385  if (desc.format == PixelFormat::kA8UNormInt) {
386  view_info.components.a = vk::ComponentSwizzle::eR;
387  view_info.components.r = vk::ComponentSwizzle::eA;
388  }
389 
390  auto [result, image_view] = device.createImageViewUnique(view_info);
391  if (result != vk::Result::eSuccess) {
392  VALIDATION_LOG << "Unable to create an image view for allocation: "
393  << vk::to_string(result);
394  return;
395  }
396  // Create a specialized view for render target attachments.
397  view_info.subresourceRange.levelCount = 1u;
398  auto [rt_result, rt_image_view] = device.createImageViewUnique(view_info);
399  if (rt_result != vk::Result::eSuccess) {
400  VALIDATION_LOG << "Unable to create an image view for allocation: "
401  << vk::to_string(rt_result);
402  return;
403  }
404 
405  resource_.Swap(ImageResource(ImageVMA{allocator, allocation, image},
406  std::move(image_view),
407  std::move(rt_image_view)));
408  is_valid_ = true;
409  }

References impeller::TextureDescriptor::compression_type, impeller::TextureDescriptor::format, impeller::ContextVK::GetCapabilities(), 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::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

Get the image handle for this texture source.

Returns
The image.

Implements impeller::TextureSourceVK.

Definition at line 415 of file allocator_vk.cc.

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

◆ GetImageView()

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

Retrieve the image view used for sampling/blitting/compute with this texture source.

Returns
The image view.

Implements impeller::TextureSourceVK.

Definition at line 417 of file allocator_vk.cc.

417  {
418  return resource_->image_view.get();
419  }

◆ GetRenderTargetView()

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

Retrieve the image view used for render target attachments with this texture source.

ImageViews used as render target attachments cannot have any mip levels. In cases where we want to generate mipmaps with the result of this texture, we need to create multiple image views.

Returns
The render target view.

Implements impeller::TextureSourceVK.

Definition at line 421 of file allocator_vk.cc.

421  {
422  return resource_->rt_image_view.get();
423  }

◆ IsSwapchainImage()

bool impeller::AllocatedTextureSourceVK::IsSwapchainImage ( ) const
inlineoverridevirtual

Determines if swapchain image. That is, an image used as the root render target.

Returns
Whether or not this is a swapchain image.

Implements impeller::TextureSourceVK.

Definition at line 425 of file allocator_vk.cc.

425 { return false; }

◆ IsValid()

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

Definition at line 413 of file allocator_vk.cc.

413 { 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:201
impeller::TextureSourceVK::TextureSourceVK
TextureSourceVK(TextureDescriptor desc)
Definition: texture_source_vk.cc:9
impeller::PixelFormat::kA8UNormInt
@ kA8UNormInt
impeller::ToVKTextureMemoryPropertyFlags
static constexpr vk::Flags< vk::MemoryPropertyFlagBits > ToVKTextureMemoryPropertyFlags(StorageMode mode, bool supports_memoryless_textures)
Definition: allocator_vk.cc:251
impeller::ToArrayLayerCount
constexpr uint32_t ToArrayLayerCount(TextureType type)
Definition: formats_vk.h:518
impeller::UniqueResourceVKT::Swap
void Swap(ResourceType &&other)
Reclaims the existing resource, if any, and replaces it.
Definition: resource_manager_vk.h:181
impeller::ToVKImageCreateFlags
constexpr vk::ImageCreateFlags ToVKImageCreateFlags(TextureType type)
Definition: formats_vk.h:546
impeller::AllocatorVK::ToVKImageUsageFlags
static vk::ImageUsageFlags ToVKImageUsageFlags(PixelFormat format, TextureUsageMask usage, StorageMode mode, bool supports_memoryless_textures)
Definition: allocator_vk.cc:201
impeller::ToVKImageFormat
constexpr vk::Format ToVKImageFormat(PixelFormat format)
Definition: formats_vk.h:133
impeller::StorageModeToString
constexpr const char * StorageModeToString(StorageMode mode)
Definition: formats.h:60
impeller::CapabilitiesVK::GetSupportedFRCRate
std::optional< vk::ImageCompressionFixedRateFlagBitsEXT > GetSupportedFRCRate(CompressionType compression_type, const FRCFormatDescriptor &desc) const
Get the fixed compression rate supported by the context for the given format and usage.
Definition: capabilities_vk.cc:663
impeller::ToVKImageViewType
constexpr vk::ImageViewType ToVKImageViewType(TextureType type)
Definition: formats_vk.h:532
impeller::ToVKImageAspectFlags
constexpr vk::ImageAspectFlags ToVKImageAspectFlags(PixelFormat format)
Definition: formats_vk.h:492
impeller::TextureUsageMaskToString
std::string TextureUsageMaskToString(TextureUsageMask mask)
Definition: formats.cc:81
impeller::PixelFormat::kUnknown
@ kUnknown
impeller::ToVMAMemoryUsage
static constexpr VmaMemoryUsage ToVMAMemoryUsage()
Definition: allocator_vk.cc:246
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
impeller::TextureTypeToString
constexpr const char * TextureTypeToString(TextureType type)
Definition: formats.h:269
impeller::BackendCast< CapabilitiesVK, Capabilities >::Cast
static CapabilitiesVK & Cast(Capabilities &base)
Definition: backend_cast.h:13
impeller::ToVmaAllocationCreateFlags
static VmaAllocationCreateFlags ToVmaAllocationCreateFlags(StorageMode mode)
Definition: allocator_vk.cc:269