[Impeller] Fix mipmap generation for Vulkan render target textures. (flutter/engine#49848)

Potentially fixes https://github.com/flutter/flutter/issues/141495

The texture used as a render target attachment can have miplevels, but
the image view cannot. Unconditionally create two image views per
texture (this could be optimized in some cases), where the specific
"render target texture view" always sets a mipcount of 1.

In theory this should allow us to generate mipmaps for textures that are
used as render target attachments, including toImage and toImageSync
textures which are currently missing this functionality.

---------

Co-authored-by: Aaron Clarke <aaclarke@google.com>
Co-authored-by: gaaclarke <30870216+gaaclarke@users.noreply.github.com>
This commit is contained in:
Jonah Williams
2024-01-25 12:28:26 -08:00
committed by GitHub
parent 2bc62b42f1
commit c80a0580f4
15 changed files with 118 additions and 43 deletions

View File

@@ -3827,7 +3827,7 @@ TEST_P(AiksTest, GaussianBlurSetsMipCountOnPass) {
TEST_P(AiksTest, GaussianBlurAllocatesCorrectMipCountRenderTarget) {
size_t blur_required_mip_count =
GetParam() == PlaygroundBackend::kMetal ? 4 : 1;
GetParam() == PlaygroundBackend::kOpenGLES ? 1 : 4;
Canvas canvas;
canvas.DrawCircle({100, 100}, 50, {.color = Color::CornflowerBlue()});
@@ -3855,7 +3855,7 @@ TEST_P(AiksTest, GaussianBlurAllocatesCorrectMipCountRenderTarget) {
TEST_P(AiksTest, GaussianBlurMipMapNestedLayer) {
fml::testing::LogCapture log_capture;
size_t blur_required_mip_count =
GetParam() == PlaygroundBackend::kMetal ? 4 : 1;
GetParam() == PlaygroundBackend::kOpenGLES ? 1 : 4;
Canvas canvas;
canvas.DrawPaint({.color = Color::Wheat()});
@@ -3882,7 +3882,7 @@ TEST_P(AiksTest, GaussianBlurMipMapNestedLayer) {
EXPECT_EQ(max_mip_count, blur_required_mip_count);
// The log is FML_DLOG, so only check in debug builds.
#ifndef NDEBUG
if (GetParam() == PlaygroundBackend::kMetal) {
if (GetParam() != PlaygroundBackend::kOpenGLES) {
EXPECT_EQ(log_capture.str().find(GaussianBlurFilterContents::kNoMipsError),
std::string::npos);
} else {
@@ -3894,7 +3894,7 @@ TEST_P(AiksTest, GaussianBlurMipMapNestedLayer) {
TEST_P(AiksTest, GaussianBlurMipMapImageFilter) {
size_t blur_required_mip_count =
GetParam() == PlaygroundBackend::kMetal ? 4 : 1;
GetParam() == PlaygroundBackend::kOpenGLES ? 1 : 4;
fml::testing::LogCapture log_capture;
Canvas canvas;
canvas.SaveLayer(
@@ -3918,7 +3918,7 @@ TEST_P(AiksTest, GaussianBlurMipMapImageFilter) {
EXPECT_EQ(max_mip_count, blur_required_mip_count);
// The log is FML_DLOG, so only check in debug builds.
#ifndef NDEBUG
if (GetParam() == PlaygroundBackend::kMetal) {
if (GetParam() != PlaygroundBackend::kOpenGLES) {
EXPECT_EQ(log_capture.str().find(GaussianBlurFilterContents::kNoMipsError),
std::string::npos);
} else {

View File

@@ -256,9 +256,7 @@ static EntityPassTarget CreateRenderTarget(ContentContext& renderer,
/// What's important is the `StorageMode` of the textures, which cannot be
/// changed for the lifetime of the textures.
if (context->GetBackendType() != Context::BackendType::kMetal) {
// TODO(https://github.com/flutter/flutter/issues/141495): Implement mip map
// generation on vulkan.
if (context->GetBackendType() == Context::BackendType::kOpenGLES) {
// TODO(https://github.com/flutter/flutter/issues/141732): Implement mip map
// generation on opengles.
mip_count = 1;
@@ -389,7 +387,6 @@ bool EntityPass::Render(ContentContext& renderer,
blit_pass->AddCopy(
offscreen_target.GetRenderTarget().GetRenderTargetTexture(),
root_render_target.GetRenderTargetTexture());
if (!command_buffer->EncodeAndSubmit(
blit_pass, renderer.GetContext()->GetResourceAllocator())) {
VALIDATION_LOG << "Failed to encode root pass blit command.";

View File

@@ -56,24 +56,26 @@ bool InlinePassContext::EndPass() {
if (!IsActive()) {
return true;
}
FML_DCHECK(command_buffer_);
if (command_buffer_) {
if (!command_buffer_->EncodeAndSubmit(pass_)) {
VALIDATION_LOG
<< "Failed to encode and submit command buffer while ending "
"render pass.";
return false;
}
if (!pass_->EncodeCommands()) {
VALIDATION_LOG << "Failed to encode and submit command buffer while ending "
"render pass.";
return false;
}
std::shared_ptr<Texture> target_texture =
const std::shared_ptr<Texture>& target_texture =
GetPassTarget().GetRenderTarget().GetRenderTargetTexture();
if (target_texture->GetMipCount() > 1) {
fml::Status mip_status = AddMipmapGeneration(context_, target_texture);
fml::Status mip_status =
AddMipmapGeneration(command_buffer_, context_, target_texture);
if (!mip_status.ok()) {
return false;
}
}
if (!command_buffer_->SubmitCommands()) {
return false;
}
pass_ = nullptr;
command_buffer_ = nullptr;

View File

@@ -353,8 +353,18 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
<< vk::to_string(result);
return;
}
// Create a specialized view for render target attachments.
view_info.subresourceRange.levelCount = 1u;
auto [rt_result, rt_image_view] = device.createImageViewUnique(view_info);
if (rt_result != vk::Result::eSuccess) {
VALIDATION_LOG << "Unable to create an image view for allocation: "
<< vk::to_string(rt_result);
return;
}
resource_.Swap(ImageResource(ImageVMA{allocator, allocation, image},
std::move(image_view)));
std::move(image_view),
std::move(rt_image_view)));
is_valid_ = true;
}
@@ -368,20 +378,28 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
return resource_->image_view.get();
}
vk::ImageView GetRenderTargetView() const override {
return resource_->rt_image_view.get();
}
bool IsSwapchainImage() const override { return false; }
private:
struct ImageResource {
UniqueImageVMA image;
vk::UniqueImageView image_view;
vk::UniqueImageView rt_image_view;
ImageResource() = default;
ImageResource(ImageVMA p_image, vk::UniqueImageView p_image_view)
: image(p_image), image_view(std::move(p_image_view)) {}
ImageResource(ImageVMA p_image,
vk::UniqueImageView p_image_view,
vk::UniqueImageView p_rt_image_view)
: image(p_image),
image_view(std::move(p_image_view)),
rt_image_view(std::move(p_rt_image_view)) {}
ImageResource(ImageResource&& o) {
std::swap(image, o.image);
std::swap(image_view, o.image_view);
}
ImageResource(ImageResource&& o) = default;
ImageResource(const ImageResource&) = delete;

View File

@@ -201,6 +201,13 @@ vk::ImageView AndroidHardwareBufferTextureSourceVK::GetImageView() const {
return image_view_.get();
}
// |TextureSourceVK|
vk::ImageView AndroidHardwareBufferTextureSourceVK::GetRenderTargetView()
const {
FML_CHECK(IsValid());
return image_view_.get();
}
} // namespace impeller
#endif

View File

@@ -38,8 +38,13 @@ class AndroidHardwareBufferTextureSourceVK final : public TextureSourceVK {
// |TextureSourceVK|
vk::ImageView GetImageView() const override;
// |TextureSourceVK|
vk::ImageView GetRenderTargetView() const override;
bool IsValid() const;
bool IsSwapchainImage() const override { return false; }
private:
const vk::Device& device_;
vk::Image image_ = VK_NULL_HANDLE;

View File

@@ -85,7 +85,21 @@ bool BlitCopyTextureToTextureCommandVK::Encode(
image_copy //
);
return true;
// If this is an onscreen texture, do not transition the layout
// back to shader read.
if (dst.IsSwapchainImage()) {
return true;
}
BarrierVK barrier;
barrier.cmd_buffer = cmd_buffer;
barrier.new_layout = vk::ImageLayout::eShaderReadOnlyOptimal;
barrier.src_access = {};
barrier.src_stage = vk::PipelineStageFlagBits::eTopOfPipe;
barrier.dst_access = vk::AccessFlagBits::eShaderRead;
barrier.dst_stage = vk::PipelineStageFlagBits::eFragmentShader;
return dst.SetLayout(barrier);
}
//------------------------------------------------------------------------------
@@ -352,6 +366,7 @@ bool BlitGenerateMipmapCommandVK::Encode(CommandEncoderVK& encoder) const {
// We modified the layouts of this image from underneath it. Tell it its new
// state so it doesn't try to perform redundant transitions under the hood.
src.SetLayoutWithoutEncoding(vk::ImageLayout::eShaderReadOnlyOptimal);
src.SetMipMapGenerated();
return true;
}

View File

@@ -105,8 +105,7 @@ static vk::AttachmentDescription CreateAttachmentDescription(
}
// Always insert a barrier to transition to color attachment optimal.
if (current_layout != vk::ImageLayout::ePresentSrcKHR &&
current_layout != vk::ImageLayout::eUndefined) {
if (current_layout != vk::ImageLayout::ePresentSrcKHR) {
// Note: This should incur a barrier.
current_layout = vk::ImageLayout::eGeneral;
}
@@ -360,18 +359,21 @@ SharedHandleVK<vk::Framebuffer> RenderPassVK::CreateVKFramebuffer(
for (const auto& [_, color] : render_target_.GetColorAttachments()) {
// The bind point doesn't matter here since that information is present in
// the render pass.
attachments.emplace_back(TextureVK::Cast(*color.texture).GetImageView());
attachments.emplace_back(
TextureVK::Cast(*color.texture).GetRenderTargetView());
if (color.resolve_texture) {
attachments.emplace_back(
TextureVK::Cast(*color.resolve_texture).GetImageView());
TextureVK::Cast(*color.resolve_texture).GetRenderTargetView());
}
}
if (auto depth = render_target_.GetDepthAttachment(); depth.has_value()) {
attachments.emplace_back(TextureVK::Cast(*depth->texture).GetImageView());
attachments.emplace_back(
TextureVK::Cast(*depth->texture).GetRenderTargetView());
}
if (auto stencil = render_target_.GetStencilAttachment();
stencil.has_value()) {
attachments.emplace_back(TextureVK::Cast(*stencil->texture).GetImageView());
attachments.emplace_back(
TextureVK::Cast(*stencil->texture).GetRenderTargetView());
}
fb_info.setAttachments(attachments);

View File

@@ -65,4 +65,9 @@ vk::ImageView SwapchainImageVK::GetImageView() const {
return image_view_.get();
}
// |TextureSourceVK|
vk::ImageView SwapchainImageVK::GetRenderTargetView() const {
return image_view_.get();
}
} // namespace impeller

View File

@@ -38,8 +38,12 @@ class SwapchainImageVK final : public TextureSourceVK {
// |TextureSourceVK|
vk::ImageView GetImageView() const override;
vk::ImageView GetRenderTargetView() const override;
void SetMSAATexture(std::shared_ptr<Texture> msaa_tex);
bool IsSwapchainImage() const override { return true; }
private:
vk::Image image_ = VK_NULL_HANDLE;
vk::UniqueImageView image_view_ = {};

View File

@@ -5,7 +5,6 @@
#ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_TEXTURE_SOURCE_VK_H_
#define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_TEXTURE_SOURCE_VK_H_
#include "flutter/fml/macros.h"
#include "flutter/fml/status.h"
#include "impeller/base/thread.h"
#include "impeller/core/texture_descriptor.h"
@@ -27,8 +26,18 @@ class TextureSourceVK {
virtual vk::Image GetImage() const = 0;
/// @brief Retrieve the image view used for sampling/blitting/compute with
/// this texture source.
virtual vk::ImageView GetImageView() const = 0;
/// @brief 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.
virtual vk::ImageView GetRenderTargetView() const = 0;
/// Encodes the layout transition `barrier` to `barrier.cmd_buffer` for the
/// image.
///
@@ -51,6 +60,9 @@ class TextureSourceVK {
/// reflect the actual layout.
vk::ImageLayout GetLayout() const;
/// Whether or not this is a swapchain image.
virtual bool IsSwapchainImage() const = 0;
protected:
const TextureDescriptor desc_;

View File

@@ -169,4 +169,8 @@ vk::ImageLayout TextureVK::GetLayout() const {
return source_ ? source_->GetLayout() : vk::ImageLayout::eUndefined;
}
vk::ImageView TextureVK::GetRenderTargetView() const {
return source_->GetRenderTargetView();
}
} // namespace impeller

View File

@@ -27,6 +27,8 @@ class TextureVK final : public Texture, public BackendCast<TextureVK, Texture> {
vk::ImageView GetImageView() const;
vk::ImageView GetRenderTargetView() const;
bool SetLayout(const BarrierVK& barrier) const;
vk::ImageLayout SetLayoutWithoutEncoding(vk::ImageLayout layout) const;
@@ -38,6 +40,10 @@ class TextureVK final : public Texture, public BackendCast<TextureVK, Texture> {
// |Texture|
ISize GetSize() const override;
void SetMipMapGenerated() { mipmap_generated_ = true; }
bool IsSwapchainImage() const { return source_->IsSwapchainImage(); }
private:
std::weak_ptr<Context> context_;
std::shared_ptr<TextureSourceVK> source_;

View File

@@ -8,11 +8,11 @@
namespace impeller {
fml::Status AddMipmapGeneration(const std::shared_ptr<Context>& context,
const std::shared_ptr<Texture>& texture) {
std::shared_ptr<CommandBuffer> mip_cmd_buffer =
context->CreateCommandBuffer();
std::shared_ptr<BlitPass> blit_pass = mip_cmd_buffer->CreateBlitPass();
fml::Status AddMipmapGeneration(
const std::shared_ptr<CommandBuffer>& command_buffer,
const std::shared_ptr<Context>& context,
const std::shared_ptr<Texture>& texture) {
std::shared_ptr<BlitPass> blit_pass = command_buffer->CreateBlitPass();
bool success = blit_pass->GenerateMipmap(texture);
if (!success) {
return fml::Status(fml::StatusCode::kUnknown, "");
@@ -21,11 +21,7 @@ fml::Status AddMipmapGeneration(const std::shared_ptr<Context>& context,
if (!success) {
return fml::Status(fml::StatusCode::kUnknown, "");
}
success = mip_cmd_buffer->SubmitCommands(/*callback=*/nullptr);
if (!success) {
return fml::Status(fml::StatusCode::kUnknown, "");
}
return {};
return fml::Status();
}
} // namespace impeller

View File

@@ -7,12 +7,14 @@
#include "flutter/fml/status.h"
#include "impeller/core/texture.h"
#include "impeller/renderer/command_buffer.h"
#include "impeller/renderer/context.h"
namespace impeller {
/// Adds a blit command to the render pass.
[[nodiscard]] fml::Status AddMipmapGeneration(
const std::shared_ptr<CommandBuffer>& command_buffer,
const std::shared_ptr<Context>& context,
const std::shared_ptr<Texture>& texture);