[Impeller] added mipmaps to subpasses directly on a draw call (flutter/engine#50157)

fixes https://github.com/flutter/flutter/issues/142001

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide] and the [C++,
Objective-C, Java style guides].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I added new tests to check the change I am making or feature I am
adding, or the PR is [test-exempt]. See [testing the engine] for
instructions on writing and running engine tests.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I signed the [CLA].
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#overview
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[test-exempt]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
[C++, Objective-C, Java style guides]:
https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
[testing the engine]:
https://github.com/flutter/flutter/wiki/Testing-the-engine
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
This commit is contained in:
gaaclarke
2024-01-30 10:48:19 -08:00
committed by GitHub
parent 72d65e3139
commit fca1b2166a
26 changed files with 141 additions and 46 deletions

View File

@@ -2750,7 +2750,7 @@ TEST_P(AiksTest, CanRenderClippedBlur) {
.color = Color::Green(),
.image_filter = ImageFilter::MakeBlur(
Sigma(20.0), Sigma(20.0), FilterContents::BlurStyle::kNormal,
Entity::TileMode::kClamp),
Entity::TileMode::kDecal),
});
canvas.Restore();
@@ -3960,6 +3960,48 @@ TEST_P(AiksTest, GaussianBlurMipMapImageFilter) {
#endif
}
TEST_P(AiksTest, GaussianBlurMipMapSolidColor) {
size_t blur_required_mip_count =
GetParam() == PlaygroundBackend::kOpenGLES ? 1 : 4;
fml::testing::LogCapture log_capture;
Canvas canvas;
canvas.DrawPath(PathBuilder{}
.MoveTo({100, 100})
.LineTo({200, 100})
.LineTo({150, 200})
.LineTo({50, 200})
.Close()
.TakePath(),
{.color = Color::Chartreuse(),
.image_filter = ImageFilter::MakeBlur(
Sigma(30), Sigma(30), FilterContents::BlurStyle::kNormal,
Entity::TileMode::kClamp)});
Picture picture = canvas.EndRecordingAsPicture();
std::shared_ptr<RenderTargetCache> cache =
std::make_shared<RenderTargetCache>(GetContext()->GetResourceAllocator());
AiksContext aiks_context(GetContext(), nullptr, cache);
picture.ToImage(aiks_context, {1024, 768});
size_t max_mip_count = 0;
for (auto it = cache->GetTextureDataBegin(); it != cache->GetTextureDataEnd();
++it) {
max_mip_count =
std::max(it->texture->GetTextureDescriptor().mip_count, max_mip_count);
}
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::kOpenGLES) {
EXPECT_EQ(log_capture.str().find(GaussianBlurFilterContents::kNoMipsError),
std::string::npos);
} else {
EXPECT_NE(log_capture.str().find(GaussianBlurFilterContents::kNoMipsError),
std::string::npos);
}
#endif
}
TEST_P(AiksTest, ImageColorSourceEffectTransform) {
// Compare with https://fiddle.skia.org/c/6cdc5aefb291fda3833b806ca347a885

View File

@@ -36,7 +36,8 @@ sk_sp<DlImageImpeller> DlImageImpeller::MakeFromYUVTextures(
std::nullopt, // coverage_limit
std::nullopt, // sampler_descriptor
true, // msaa_enabled
"MakeYUVToRGBFilter Snapshot"); // label
/*mip_count=*/1,
"MakeYUVToRGBFilter Snapshot"); // label
if (!snapshot.has_value()) {
return nullptr;
}

View File

@@ -304,6 +304,7 @@ bool AtlasContents::Render(const ContentContext& renderer,
std::nullopt, // coverage_limit
std::nullopt, // sampler_descriptor
true, // msaa_enabled
/*mip_count=*/1,
"AtlasContents Snapshot"); // label
if (!snapshot.has_value()) {
return false;

View File

@@ -15,6 +15,7 @@
#include "impeller/renderer/pipeline_descriptor.h"
#include "impeller/renderer/pipeline_library.h"
#include "impeller/renderer/render_target.h"
#include "impeller/renderer/texture_mipmap.h"
#include "impeller/tessellator/tessellator.h"
#include "impeller/typographer/typographer_context.h"
@@ -429,20 +430,21 @@ fml::StatusOr<RenderTarget> ContentContext::MakeSubpass(
const std::string& label,
ISize texture_size,
const SubpassCallback& subpass_callback,
bool msaa_enabled) const {
bool msaa_enabled,
int32_t mip_count) const {
const std::shared_ptr<Context>& context = GetContext();
RenderTarget subpass_target;
if (context->GetCapabilities()->SupportsOffscreenMSAA() && msaa_enabled) {
subpass_target = RenderTarget::CreateOffscreenMSAA(
*context, *GetRenderTargetCache(), texture_size, /*mip_count=*/1,
SPrintF("%s Offscreen", label.c_str()),
*context, *GetRenderTargetCache(), texture_size,
/*mip_count=*/mip_count, SPrintF("%s Offscreen", label.c_str()),
RenderTarget::kDefaultColorAttachmentConfigMSAA,
std::nullopt // stencil_attachment_config
);
} else {
subpass_target = RenderTarget::CreateOffscreen(
*context, *GetRenderTargetCache(), texture_size, /*mip_count=*/1,
SPrintF("%s Offscreen", label.c_str()),
*context, *GetRenderTargetCache(), texture_size,
/*mip_count=*/mip_count, SPrintF("%s Offscreen", label.c_str()),
RenderTarget::kDefaultColorAttachmentConfig, //
std::nullopt // stencil_attachment_config
);
@@ -477,7 +479,21 @@ fml::StatusOr<RenderTarget> ContentContext::MakeSubpass(
return fml::Status(fml::StatusCode::kUnknown, "");
}
if (!sub_command_buffer->EncodeAndSubmit(sub_renderpass)) {
if (!sub_renderpass->EncodeCommands()) {
return fml::Status(fml::StatusCode::kUnknown, "");
}
const std::shared_ptr<Texture>& target_texture =
subpass_target.GetRenderTargetTexture();
if (target_texture->GetMipCount() > 1) {
fml::Status mipmap_status =
AddMipmapGeneration(sub_command_buffer, context, target_texture);
if (!mipmap_status.ok()) {
return mipmap_status;
}
}
if (!sub_command_buffer->SubmitCommands()) {
return fml::Status(fml::StatusCode::kUnknown, "");
}

View File

@@ -724,7 +724,8 @@ class ContentContext {
const std::string& label,
ISize texture_size,
const SubpassCallback& subpass_callback,
bool msaa_enabled = true) const;
bool msaa_enabled = true,
int32_t mip_count = 1) const;
/// Makes a subpass that will render to `subpass_target`.
fml::StatusOr<RenderTarget> MakeSubpass(

View File

@@ -60,6 +60,7 @@ std::optional<Snapshot> Contents::RenderToSnapshot(
std::optional<Rect> coverage_limit,
const std::optional<SamplerDescriptor>& sampler_descriptor,
bool msaa_enabled,
int32_t mip_count,
const std::string& label) const {
auto coverage = GetCoverage(entity);
if (!coverage.has_value()) {
@@ -90,7 +91,7 @@ std::optional<Snapshot> Contents::RenderToSnapshot(
entity.GetTransform());
return contents.Render(renderer, sub_entity, pass);
},
msaa_enabled);
msaa_enabled, mip_count);
if (!render_target.ok()) {
return std::nullopt;

View File

@@ -121,6 +121,7 @@ class Contents {
std::optional<Rect> coverage_limit = std::nullopt,
const std::optional<SamplerDescriptor>& sampler_descriptor = std::nullopt,
bool msaa_enabled = true,
int32_t mip_count = 1,
const std::string& label = "Snapshot") const;
virtual bool ShouldRender(const Entity& entity,

View File

@@ -50,7 +50,8 @@ std::shared_ptr<FilterContents> FilterContents::MakeDirectionalGaussianBlur(
return blur;
}
const int32_t FilterContents::kBlurFilterRequiredMipCount = 4;
const int32_t FilterContents::kBlurFilterRequiredMipCount =
GaussianBlurFilterContents::kBlurFilterRequiredMipCount;
std::shared_ptr<FilterContents> FilterContents::MakeGaussianBlur(
const FilterInput::Ref& input,
@@ -262,6 +263,7 @@ std::optional<Snapshot> FilterContents::RenderToSnapshot(
std::optional<Rect> coverage_limit,
const std::optional<SamplerDescriptor>& sampler_descriptor,
bool msaa_enabled,
int32_t mip_count,
const std::string& label) const {
// Resolve the render instruction (entity) from the filter and render it to a
// snapshot.
@@ -274,7 +276,8 @@ std::optional<Snapshot> FilterContents::RenderToSnapshot(
coverage_limit, // coverage_limit
std::nullopt, // sampler_descriptor
true, // msaa_enabled
label); // label
/*mip_count=*/mip_count,
label); // label
}
return std::nullopt;

View File

@@ -131,6 +131,7 @@ class FilterContents : public Contents {
std::optional<Rect> coverage_limit = std::nullopt,
const std::optional<SamplerDescriptor>& sampler_descriptor = std::nullopt,
bool msaa_enabled = true,
int32_t mip_count = 1,
const std::string& label = "Filter Snapshot") const override;
// |Contents|

View File

@@ -19,6 +19,8 @@ namespace impeller {
using GaussianBlurVertexShader = KernelPipeline::VertexShader;
using GaussianBlurFragmentShader = KernelPipeline::FragmentShader;
const int32_t GaussianBlurFilterContents::kBlurFilterRequiredMipCount = 4;
namespace {
SamplerDescriptor MakeSamplerDescriptor(MinMagFilter filter,
@@ -269,9 +271,18 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
expanded_coverage_hint = coverage_hint->Expand(local_padding);
}
int32_t mip_count = kBlurFilterRequiredMipCount;
if (renderer.GetContext()->GetBackendType() ==
Context::BackendType::kOpenGLES) {
// TODO(https://github.com/flutter/flutter/issues/141732): Implement mip map
// generation on opengles.
mip_count = 1;
}
std::optional<Snapshot> input_snapshot =
inputs[0]->GetSnapshot("GaussianBlur", renderer, entity,
/*coverage_limit=*/expanded_coverage_hint);
/*coverage_limit=*/expanded_coverage_hint,
/*mip_count=*/mip_count);
if (!input_snapshot.has_value()) {
return std::nullopt;
}

View File

@@ -28,6 +28,7 @@ KernelPipeline::FragmentShader::KernelSamples GenerateBlurInfo(
class GaussianBlurFilterContents final : public FilterContents {
public:
static std::string_view kNoMipsError;
static const int32_t kBlurFilterRequiredMipCount;
explicit GaussianBlurFilterContents(Scalar sigma_x,
Scalar sigma_y,

View File

@@ -25,7 +25,8 @@ std::optional<Snapshot> ContentsFilterInput::GetSnapshot(
const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit) const {
std::optional<Rect> coverage_limit,
int32_t mip_count) const {
if (!coverage_limit.has_value() && entity.GetContents()) {
coverage_limit = entity.GetContents()->GetCoverageHint();
}
@@ -36,6 +37,7 @@ std::optional<Snapshot> ContentsFilterInput::GetSnapshot(
coverage_limit, // coverage_limit
std::nullopt, // sampler_descriptor
msaa_enabled_, // msaa_enabled
/*mip_count=*/mip_count,
SPrintF("Contents to %s Filter Snapshot", label.c_str())); // label
}
return snapshot_;

View File

@@ -17,11 +17,11 @@ class ContentsFilterInput final : public FilterInput {
Variant GetInput() const override;
// |FilterInput|
std::optional<Snapshot> GetSnapshot(
const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit) const override;
std::optional<Snapshot> GetSnapshot(const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit,
int32_t mip_count) const override;
// |FilterInput|
std::optional<Rect> GetCoverage(const Entity& entity) const override;

View File

@@ -25,7 +25,8 @@ std::optional<Snapshot> FilterContentsFilterInput::GetSnapshot(
const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit) const {
std::optional<Rect> coverage_limit,
int32_t mip_count) const {
if (!snapshot_.has_value()) {
snapshot_ = filter_->RenderToSnapshot(
renderer, // renderer
@@ -33,6 +34,7 @@ std::optional<Snapshot> FilterContentsFilterInput::GetSnapshot(
coverage_limit, // coverage_limit
std::nullopt, // sampler_descriptor
true, // msaa_enabled
/*mip_count=*/mip_count,
SPrintF("Filter to %s Filter Snapshot", label.c_str())); // label
}
return snapshot_;

View File

@@ -17,11 +17,11 @@ class FilterContentsFilterInput final : public FilterInput {
Variant GetInput() const override;
// |FilterInput|
std::optional<Snapshot> GetSnapshot(
const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit) const override;
std::optional<Snapshot> GetSnapshot(const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit,
int32_t mip_count) const override;
// |FilterInput|
std::optional<Rect> GetCoverage(const Entity& entity) const override;

View File

@@ -51,7 +51,8 @@ class FilterInput {
const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit = std::nullopt) const = 0;
std::optional<Rect> coverage_limit = std::nullopt,
int32_t mip_count = 1) const = 0;
std::optional<Rect> GetLocalCoverage(const Entity& entity) const;

View File

@@ -24,7 +24,8 @@ std::optional<Snapshot> PlaceholderFilterInput::GetSnapshot(
const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit) const {
std::optional<Rect> coverage_limit,
int32_t mip_count) const {
return std::nullopt;
}

View File

@@ -19,11 +19,11 @@ class PlaceholderFilterInput final : public FilterInput {
Variant GetInput() const override;
// |FilterInput|
std::optional<Snapshot> GetSnapshot(
const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit) const override;
std::optional<Snapshot> GetSnapshot(const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit,
int32_t mip_count = 1) const override;
// |FilterInput|
std::optional<Rect> GetCoverage(const Entity& entity) const override;

View File

@@ -24,7 +24,8 @@ std::optional<Snapshot> TextureFilterInput::GetSnapshot(
const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit) const {
std::optional<Rect> coverage_limit,
int32_t mip_count) const {
auto snapshot =
Snapshot{.texture = texture_, .transform = GetTransform(entity)};
if (texture_->GetMipCount() > 1) {

View File

@@ -19,11 +19,11 @@ class TextureFilterInput final : public FilterInput {
Variant GetInput() const override;
// |FilterInput|
std::optional<Snapshot> GetSnapshot(
const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit) const override;
std::optional<Snapshot> GetSnapshot(const std::string& label,
const ContentContext& renderer,
const Entity& entity,
std::optional<Rect> coverage_limit,
int32_t mip_count) const override;
// |FilterInput|
std::optional<Rect> GetCoverage(const Entity& entity) const override;

View File

@@ -46,7 +46,8 @@ bool FramebufferBlendContents::Render(const ContentContext& renderer,
Rect::MakeSize(pass.GetRenderTargetSize()), // coverage_limit
std::nullopt, // sampler_descriptor
true, // msaa_enabled
"FramebufferBlendContents Snapshot"); // label
/*mip_count=*/1,
"FramebufferBlendContents Snapshot"); // label
if (!src_snapshot.has_value()) {
return true;

View File

@@ -79,6 +79,7 @@ std::optional<Snapshot> TextureContents::RenderToSnapshot(
std::optional<Rect> coverage_limit,
const std::optional<SamplerDescriptor>& sampler_descriptor,
bool msaa_enabled,
int32_t mip_count,
const std::string& label) const {
// Passthrough textures that have simple rectangle paths and complete source
// rects.
@@ -101,7 +102,8 @@ std::optional<Snapshot> TextureContents::RenderToSnapshot(
std::nullopt, // coverage_limit
sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
true, // msaa_enabled
label); // label
/*mip_count=*/mip_count,
label); // label
}
bool TextureContents::Render(const ContentContext& renderer,

View File

@@ -65,6 +65,7 @@ class TextureContents final : public Contents {
std::optional<Rect> coverage_limit = std::nullopt,
const std::optional<SamplerDescriptor>& sampler_descriptor = std::nullopt,
bool msaa_enabled = true,
int32_t mip_count = 1,
const std::string& label = "Texture Snapshot") const override;
// |Contents|

View File

@@ -65,11 +65,12 @@ std::shared_ptr<Texture> TiledTextureContents::CreateFilterTexture(
}
auto color_filter_contents = color_filter_(FilterInput::Make(texture_));
auto snapshot = color_filter_contents->RenderToSnapshot(
renderer, // renderer
Entity(), // entity
std::nullopt, // coverage_limit
std::nullopt, // sampler_descriptor
true, // msaa_enabled
renderer, // renderer
Entity(), // entity
std::nullopt, // coverage_limit
std::nullopt, // sampler_descriptor
true, // msaa_enabled
/*mip_count=*/1,
"TiledTextureContents Snapshot"); // label
if (snapshot.has_value()) {
return snapshot.value().texture;
@@ -236,6 +237,7 @@ std::optional<Snapshot> TiledTextureContents::RenderToSnapshot(
std::optional<Rect> coverage_limit,
const std::optional<SamplerDescriptor>& sampler_descriptor,
bool msaa_enabled,
int32_t mip_count,
const std::string& label) const {
if (GetInverseEffectTransform().IsIdentity() &&
GetGeometry()->IsAxisAlignedRect()) {
@@ -260,7 +262,8 @@ std::optional<Snapshot> TiledTextureContents::RenderToSnapshot(
std::nullopt, // coverage_limit
sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
true, // msaa_enabled
label); // label
/*mip_count=*/1,
label); // label
}
} // namespace impeller

View File

@@ -61,6 +61,7 @@ class TiledTextureContents final : public ColorSourceContents {
std::optional<Rect> coverage_limit = std::nullopt,
const std::optional<SamplerDescriptor>& sampler_descriptor = std::nullopt,
bool msaa_enabled = true,
int32_t mip_count = 1,
const std::string& label = "Tiled Texture Snapshot") const override;
private:

View File

@@ -116,6 +116,7 @@ bool VerticesUVContents::Render(const ContentContext& renderer,
GetCoverageHint(), // coverage_limit
std::nullopt, // sampler_descriptor
true, // msaa_enabled
/*mip_count=*/1,
"VerticesUVContents Snapshot"); // label
if (!snapshot.has_value()) {
return false;