Skip to content

Commit 95feccb

Browse files
gaaclarkekrll-kov
andauthored
Removes feedback loop from advanced filters without offscreen msaa and framebufferfetch (flutter#193306)
My fork of flutter#193178 to avoid OP's open PR limit. Coauthored with @krll-kov Fixes flutter#192918 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Kyrylo K. <[email protected]>
1 parent 4e99e09 commit 95feccb

6 files changed

Lines changed: 457 additions & 17 deletions

File tree

‎engine/src/flutter/impeller/display_list/BUILD.gn‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,12 @@ template("aiks_unittests_component") {
190190
defines += [ "_USE_MATH_DEFINES" ]
191191

192192
sources = predefined_sources + additional_sources
193-
deps = [
193+
if (defined(invoker.deps)) {
194+
deps = invoker.deps
195+
} else {
196+
deps = []
197+
}
198+
deps += [
194199
":aiks_playground",
195200
":display_list",
196201
"//flutter/impeller/geometry:geometry_asserts",
@@ -207,6 +212,10 @@ template("aiks_unittests_component") {
207212

208213
aiks_unittests_component("aiks_unittests") {
209214
sources = [ "canvas_unittests.cc" ]
215+
if (impeller_enable_opengles) {
216+
sources += [ "canvas_gles_unittests.cc" ]
217+
deps = [ "//flutter/impeller/renderer/backend/gles:mock_gles" ]
218+
}
210219
}
211220

212221
aiks_unittests_component("aiks_unittests_golden") {

‎engine/src/flutter/impeller/display_list/canvas.cc‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,27 +2610,40 @@ std::shared_ptr<Texture> Canvas::FlipBackdrop(Point global_pass_position,
26102610
RenderPass& current_render_pass =
26112611
*render_passes_.back().GetInlinePassContext()->GetRenderPass();
26122612

2613+
// If the current pass already contains the backdrop, which only happens when
2614+
// MSAA is not available, the eager restore below is unnecessary and would
2615+
// draw the texture into itself, which is undefined behavior.
2616+
const ColorAttachment color0 =
2617+
current_render_pass.GetRenderTarget().GetColorAttachment(0);
2618+
const bool contents_already_present = color0.texture == input_texture;
2619+
FML_DCHECK(!contents_already_present ||
2620+
color0.load_action == LoadAction::kLoad)
2621+
<< "A pass writing to the backdrop texture must load it, otherwise the "
2622+
"backdrop is dropped from the frame.";
2623+
26132624
// Eagerly restore the BDF contents.
26142625

26152626
// If the pass context returns a backdrop texture, we need to draw it to the
26162627
// current pass. We do this because it's faster and takes significantly less
26172628
// memory than storing/loading large MSAA textures. Also, it's not possible
26182629
// to blit the non-MSAA resolve texture of the previous pass to MSAA
26192630
// textures (let alone a transient one).
2620-
Rect size_rect = Rect::MakeSize(input_texture->GetSize());
2621-
auto msaa_backdrop_contents = TextureContents::MakeRect(size_rect);
2622-
msaa_backdrop_contents->SetStencilEnabled(false);
2623-
msaa_backdrop_contents->SetLabel("MSAA backdrop");
2624-
msaa_backdrop_contents->SetSourceRect(size_rect);
2625-
msaa_backdrop_contents->SetTexture(input_texture);
2626-
2627-
Entity msaa_backdrop_entity;
2628-
msaa_backdrop_entity.SetContents(std::move(msaa_backdrop_contents));
2629-
msaa_backdrop_entity.SetBlendMode(BlendMode::kSrc);
2630-
msaa_backdrop_entity.SetClipDepth(std::numeric_limits<uint32_t>::max());
2631-
if (!msaa_backdrop_entity.Render(renderer_, current_render_pass)) {
2632-
VALIDATION_LOG << "Failed to render MSAA backdrop entity.";
2633-
return nullptr;
2631+
if (!contents_already_present) {
2632+
Rect size_rect = Rect::MakeSize(input_texture->GetSize());
2633+
auto msaa_backdrop_contents = TextureContents::MakeRect(size_rect);
2634+
msaa_backdrop_contents->SetStencilEnabled(false);
2635+
msaa_backdrop_contents->SetLabel("MSAA backdrop");
2636+
msaa_backdrop_contents->SetSourceRect(size_rect);
2637+
msaa_backdrop_contents->SetTexture(input_texture);
2638+
2639+
Entity msaa_backdrop_entity;
2640+
msaa_backdrop_entity.SetContents(std::move(msaa_backdrop_contents));
2641+
msaa_backdrop_entity.SetBlendMode(BlendMode::kSrc);
2642+
msaa_backdrop_entity.SetClipDepth(std::numeric_limits<uint32_t>::max());
2643+
if (!msaa_backdrop_entity.Render(renderer_, current_render_pass)) {
2644+
VALIDATION_LOG << "Failed to render MSAA backdrop entity.";
2645+
return nullptr;
2646+
}
26342647
}
26352648

26362649
// Restore any clips that were recorded before the backdrop filter was
Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include <array>
6+
#include <cstring>
7+
#include <map>
8+
#include <memory>
9+
#include <mutex>
10+
#include <set>
11+
#include <string_view>
12+
#include <vector>
13+
14+
#include "flutter/fml/mapping.h"
15+
#include "flutter/testing/testing.h" // IWYU pragma: keep
16+
#include "gmock/gmock.h"
17+
#include "gtest/gtest.h"
18+
#include "impeller/display_list/canvas.h"
19+
#include "impeller/entity/contents/content_context.h"
20+
#include "impeller/entity/gles/entity_shaders_gles.h"
21+
#include "impeller/entity/gles/framebuffer_blend_shaders_gles.h"
22+
#include "impeller/entity/gles/modern_shaders_gles.h"
23+
#include "impeller/renderer/backend/gles/context_gles.h"
24+
#include "impeller/renderer/backend/gles/test/mock_gles.h"
25+
#include "impeller/renderer/render_target.h"
26+
#include "impeller/typographer/backends/skia/typographer_context_skia.h"
27+
28+
namespace impeller {
29+
namespace testing {
30+
31+
namespace {
32+
33+
using ::testing::_;
34+
using ::testing::IsEmpty;
35+
using ::testing::NiceMock;
36+
using ::testing::Return;
37+
using ::testing::SetArgPointee;
38+
39+
class MockReactorWorker final : public ReactorGLES::Worker {
40+
public:
41+
// |ReactorGLES::Worker|
42+
bool CanReactorReactOnCurrentThreadNow(
43+
const ReactorGLES& reactor) const override {
44+
return true;
45+
}
46+
};
47+
48+
std::vector<std::shared_ptr<fml::Mapping>> ShaderLibraryMappings() {
49+
return {
50+
std::make_shared<fml::NonOwnedMapping>(
51+
impeller_entity_shaders_gles_data,
52+
impeller_entity_shaders_gles_length),
53+
std::make_shared<fml::NonOwnedMapping>(
54+
impeller_modern_shaders_gles_data,
55+
impeller_modern_shaders_gles_length),
56+
std::make_shared<fml::NonOwnedMapping>(
57+
impeller_framebuffer_blend_shaders_gles_data,
58+
impeller_framebuffer_blend_shaders_gles_length),
59+
};
60+
}
61+
62+
/// The sampler uniforms every mocked program reports as active, so the backend
63+
/// binds the textures the entity shaders sample from.
64+
constexpr std::array<std::string_view, 4> kSamplerUniforms = {
65+
"texture_sampler",
66+
"texture_sampler_dst",
67+
"texture_sampler_src",
68+
"color_source_sampler",
69+
};
70+
71+
/// Drives a |MockGLESImpl| with just enough OpenGL ES state tracking to tell
72+
/// which textures each draw samples and which texture it renders into.
73+
///
74+
/// A draw that samples the color attachment of the framebuffer it renders to
75+
/// is a feedback loop, which is undefined behavior. ANGLE on Direct3D 11, for
76+
/// example, samples zeros.
77+
class FeedbackLoopDetector {
78+
public:
79+
explicit FeedbackLoopDetector(NiceMock<MockGLESImpl>& gl) {
80+
// Pipelines compile, link, and report the sampler uniforms as active.
81+
ON_CALL(gl, CreateShader(_)).WillByDefault(Return(1));
82+
ON_CALL(gl, CreateProgram()).WillByDefault(Return(1));
83+
ON_CALL(gl, IsProgram(_)).WillByDefault(Return(GL_TRUE));
84+
ON_CALL(gl, GetShaderiv(_, GL_COMPILE_STATUS, _))
85+
.WillByDefault(SetArgPointee<2>(GL_TRUE));
86+
ON_CALL(gl, GetProgramiv(_, _, _))
87+
.WillByDefault([](GLuint program, GLenum pname, GLint* params) {
88+
switch (pname) {
89+
case GL_LINK_STATUS:
90+
*params = GL_TRUE;
91+
break;
92+
case GL_ACTIVE_UNIFORMS:
93+
*params = kSamplerUniforms.size();
94+
break;
95+
case GL_ACTIVE_UNIFORM_MAX_LENGTH:
96+
*params = 64;
97+
break;
98+
}
99+
});
100+
ON_CALL(gl, GetActiveUniform(_, _, _, _, _, _, _))
101+
.WillByDefault([](GLuint program, GLuint index, GLsizei buf_size,
102+
GLsizei* length, GLint* size, GLenum* type,
103+
GLchar* name) {
104+
std::string_view uniform = kSamplerUniforms[index];
105+
std::memcpy(name, uniform.data(), uniform.size());
106+
name[uniform.size()] = '\0';
107+
*length = uniform.size();
108+
*size = 1;
109+
*type = GL_SAMPLER_2D;
110+
});
111+
ON_CALL(gl, GetUniformLocation(_, _))
112+
.WillByDefault([](GLuint program, const GLchar* name) -> GLint {
113+
for (size_t i = 0; i < kSamplerUniforms.size(); i++) {
114+
if (kSamplerUniforms[i] == name) {
115+
return i;
116+
}
117+
}
118+
return -1;
119+
});
120+
ON_CALL(gl, GetIntegerv(_, _)).WillByDefault([](GLenum name, GLint* value) {
121+
if (name == GL_MAX_TEXTURE_IMAGE_UNITS ||
122+
name == GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS) {
123+
*value = 8;
124+
}
125+
});
126+
127+
// Every texture and framebuffer gets a distinct name.
128+
ON_CALL(gl, GenTextures(_, _))
129+
.WillByDefault(
130+
[this](GLsizei n, GLuint* names) { GenNames(n, names); });
131+
ON_CALL(gl, GenFramebuffers(_, _))
132+
.WillByDefault(
133+
[this](GLsizei n, GLuint* names) { GenNames(n, names); });
134+
ON_CALL(gl, IsTexture(_)).WillByDefault(Return(GL_TRUE));
135+
ON_CALL(gl, CheckFramebufferStatus(_))
136+
.WillByDefault(Return(GL_FRAMEBUFFER_COMPLETE));
137+
138+
// Track the framebuffer being rendered to and its color attachment.
139+
ON_CALL(gl, BindFramebuffer(_, _))
140+
.WillByDefault([this](GLenum target, GLuint framebuffer) {
141+
if (target == GL_FRAMEBUFFER || target == GL_DRAW_FRAMEBUFFER) {
142+
std::scoped_lock lock(mutex_);
143+
draw_framebuffer_ = framebuffer;
144+
}
145+
});
146+
ON_CALL(gl, FramebufferTexture2D(_, GL_COLOR_ATTACHMENT0, _, _, _))
147+
.WillByDefault([this](GLenum target, GLenum attachment,
148+
GLenum textarget, GLuint texture, GLint level) {
149+
std::scoped_lock lock(mutex_);
150+
color_attachments_[draw_framebuffer_] = texture;
151+
});
152+
153+
// Track the textures bound to sampler uniforms.
154+
ON_CALL(gl, ActiveTexture(_)).WillByDefault([this](GLenum texture) {
155+
std::scoped_lock lock(mutex_);
156+
active_unit_ = texture - GL_TEXTURE0;
157+
});
158+
ON_CALL(gl, BindTexture(_, _))
159+
.WillByDefault([this](GLenum target, GLuint texture) {
160+
std::scoped_lock lock(mutex_);
161+
texture_units_[active_unit_] = texture;
162+
});
163+
ON_CALL(gl, Uniform1i(_, _))
164+
.WillByDefault([this](GLint location, GLint v0) {
165+
std::scoped_lock lock(mutex_);
166+
sampled_textures_.insert(texture_units_[v0]);
167+
});
168+
169+
ON_CALL(gl, DrawArrays(_, _, _)).WillByDefault([this](auto...) { Draw(); });
170+
ON_CALL(gl, DrawElements(_, _, _, _)).WillByDefault([this](auto...) {
171+
Draw();
172+
});
173+
}
174+
175+
/// The number of draws that sampled at least one texture.
176+
size_t GetSamplingDrawCount() const {
177+
std::scoped_lock lock(mutex_);
178+
return sampling_draw_count_;
179+
}
180+
181+
/// The textures that were sampled by a draw rendering into them.
182+
std::vector<GLuint> GetFeedbackLoopTextures() const {
183+
std::scoped_lock lock(mutex_);
184+
return feedback_loop_textures_;
185+
}
186+
187+
private:
188+
mutable std::mutex mutex_;
189+
GLuint next_name_ = 1;
190+
GLuint draw_framebuffer_ = 0;
191+
GLenum active_unit_ = 0;
192+
std::map<GLuint, GLuint> color_attachments_;
193+
std::map<GLenum, GLuint> texture_units_;
194+
std::set<GLuint> sampled_textures_;
195+
size_t sampling_draw_count_ = 0;
196+
std::vector<GLuint> feedback_loop_textures_;
197+
198+
void GenNames(GLsizei n, GLuint* names) {
199+
std::scoped_lock lock(mutex_);
200+
for (GLsizei i = 0; i < n; i++) {
201+
names[i] = next_name_++;
202+
}
203+
}
204+
205+
void Draw() {
206+
std::scoped_lock lock(mutex_);
207+
if (!sampled_textures_.empty()) {
208+
sampling_draw_count_++;
209+
}
210+
auto target = color_attachments_.find(draw_framebuffer_);
211+
if (target != color_attachments_.end() &&
212+
sampled_textures_.contains(target->second)) {
213+
feedback_loop_textures_.push_back(target->second);
214+
}
215+
sampled_textures_.clear();
216+
}
217+
};
218+
219+
} // namespace
220+
221+
// Emulates a device without offscreen MSAA and without framebuffer fetch, for
222+
// example ANGLE on a Direct3D 11 feature level 10_0 device. The mock driver
223+
// advertises neither GL_EXT_multisampled_render_to_texture nor
224+
// GL_EXT_shader_framebuffer_fetch, so the capabilities come from the real
225+
// OpenGL ES backend instead of being overridden.
226+
TEST(CanvasGLESTest, AdvancedBlendWithoutOffscreenMSAAHasNoFeedbackLoop) {
227+
auto mock_gles_impl = std::make_unique<NiceMock<MockGLESImpl>>();
228+
FeedbackLoopDetector detector(*mock_gles_impl);
229+
std::shared_ptr<MockGLES> mock_gles = MockGLES::Init(
230+
std::move(mock_gles_impl), /*extensions=*/std::nullopt, "OpenGL ES 2.0");
231+
232+
std::shared_ptr<ContextGLES> context_gles = ContextGLES::Create(
233+
Flags{}, std::make_unique<ProcTableGLES>(kMockResolverGLES),
234+
ShaderLibraryMappings(),
235+
/*enable_gpu_tracing=*/false);
236+
ASSERT_TRUE(context_gles);
237+
auto worker = std::make_shared<MockReactorWorker>();
238+
context_gles->AddReactorWorker(worker);
239+
std::shared_ptr<Context> context = context_gles;
240+
ASSERT_TRUE(context->IsValid());
241+
242+
const std::shared_ptr<const Capabilities>& capabilities =
243+
context->GetCapabilities();
244+
ASSERT_FALSE(capabilities->SupportsOffscreenMSAA());
245+
ASSERT_FALSE(capabilities->SupportsFramebufferFetch());
246+
247+
ContentContext content_context(context, TypographerContextSkia::Make());
248+
ASSERT_TRUE(content_context.IsValid());
249+
250+
TextureDescriptor onscreen_desc;
251+
onscreen_desc.size = {100, 100};
252+
onscreen_desc.format = capabilities->GetDefaultColorFormat();
253+
onscreen_desc.usage = TextureUsage::kRenderTarget | TextureUsage::kShaderRead;
254+
onscreen_desc.storage_mode = StorageMode::kDevicePrivate;
255+
std::shared_ptr<Texture> onscreen =
256+
context->GetResourceAllocator()->CreateTexture(onscreen_desc);
257+
ASSERT_TRUE(onscreen);
258+
259+
ColorAttachment color0;
260+
color0.texture = onscreen;
261+
color0.load_action = LoadAction::kClear;
262+
color0.store_action = StoreAction::kStore;
263+
RenderTarget render_target;
264+
render_target.SetColorAttachment(color0, 0);
265+
266+
Canvas canvas(content_context, render_target, /*is_onscreen=*/true,
267+
/*requires_readback=*/true, Rect::MakeLTRB(0, 0, 100, 100));
268+
canvas.Save(/*total_content_depth=*/2);
269+
canvas.DrawRect(Rect::MakeXYWH(10, 10, 40, 40),
270+
Paint{.color = Color::Blue()});
271+
// An advanced blend without framebuffer fetch flips the backdrop. Without
272+
// offscreen MSAA the pass after the flip renders into the same texture the
273+
// backdrop is read from.
274+
canvas.DrawRect(
275+
Rect::MakeXYWH(20, 20, 40, 40),
276+
Paint{.color = Color::Orange(), .blend_mode = BlendMode::kScreen});
277+
canvas.Restore();
278+
canvas.EndReplay();
279+
280+
// The blend samples the backdrop, so the detector must have seen it.
281+
EXPECT_GT(detector.GetSamplingDrawCount(), 0u);
282+
EXPECT_THAT(detector.GetFeedbackLoopTextures(), IsEmpty());
283+
}
284+
285+
} // namespace testing
286+
} // namespace impeller

0 commit comments

Comments
 (0)