Skip to content

Commit b3cef5e

Browse files
b-lukgaaclarke
authored andcommitted
SSBO-based gradients in UberSDF (flutter#192962)
Moves most of the logic of uber_sdf.frag to uber_sdf_common.glsl. New uber_sdf.frag and uber_sdf_ssbo.frag shaders import uber_sdf_common.glsl and implement `getGradientColor()` with a texture-based or SSBO-based gradient implementation. Gradient SSBO logic based off the existing SSBO gradient shaders like https://github.com/flutter/flutter/blob/b138043fc75c1270e1bd9f1fa0a5c1299e86e638/engine/src/flutter/impeller/entity/shaders/gradients/linear_gradient_ssbo_fill.frag Note that for GLES, SSBO is currently not supported. https://github.com/flutter/flutter/blob/b138043fc75c1270e1bd9f1fa0a5c1299e86e638/engine/src/flutter/impeller/renderer/backend/gles/capabilities_gles.cc#L277-L279 This means that for Windows and Linux, which currently always have a GLES backend, this change does not do anything. This is a prerequisite for addressing flutter#190401. This improves UberSDF gradient rendering when SSBO is supported, but not on Windows. The next step to address flutter#190401 is to make Windows support SSBO: flutter#193162 Tested this with the "Scrolling basic LinearGradient" benchmark app from flutter#190401. I ran it 3 times, on my Mac. The raster time goes down significantly, so that it is on par with the solid color time. ## On Mac (my local dev machine, M3 MacBook Pro): ### Before: | | Avg raster p50 | |--|--| | Solid | 1.250ms | | Gradient | 2.596ms | <details> <summary> Full results </summary> ``` Note: This says "Windows" because that's hard-coded in the benchmark app. But this was run on my Mac. flutter: === Windows linear-gradient comparison === flutter: mode=profile speed=1400px/s flutter: Solid color | frames=240 fps=59.75 | ui p50/p95=1.085/1.661ms | raster p50/p90/p95/p99/max=1.278/1.701/1.764/1.857/1.999ms | raster_over_budget=0/240 (0.0%) flutter: Linear gradient | frames=240 fps=59.75 | ui p50/p95=1.460/2.101ms | raster p50/p90/p95/p99/max=2.639/3.182/3.325/3.727/4.102ms | raster_over_budget=0/240 (0.0%) flutter: ================================================ flutter: === Windows linear-gradient comparison === flutter: mode=profile speed=1400px/s flutter: Solid color | frames=240 fps=59.75 | ui p50/p95=0.904/1.400ms | raster p50/p90/p95/p99/max=1.232/1.635/1.752/1.918/2.125ms | raster_over_budget=0/240 (0.0%) flutter: Linear gradient | frames=240 fps=59.75 | ui p50/p95=1.490/2.262ms | raster p50/p90/p95/p99/max=2.686/3.368/3.615/3.863/4.256ms | raster_over_budget=0/240 (0.0%) flutter: ================================================ flutter: === Windows linear-gradient comparison === flutter: mode=profile speed=1400px/s flutter: Solid color | frames=240 fps=59.75 | ui p50/p95=0.903/1.426ms | raster p50/p90/p95/p99/max=1.240/1.655/1.727/1.879/2.097ms | raster_over_budget=0/240 (0.0%) flutter: Linear gradient | frames=240 fps=59.75 | ui p50/p95=1.176/2.259ms | raster p50/p90/p95/p99/max=2.463/3.069/3.270/3.706/3.934ms | raster_over_budget=0/240 (0.0%) flutter: ================================================ ``` </details> ### After: | | Avg raster p50 | |--|--| | Solid | 1.271ms | | Gradient | 1.286ms | <details> <summary> Full results </summary> ``` Note: This says "Windows" because that's hard-coded in the benchmark app. But this was run on my Mac. flutter: === Windows linear-gradient comparison === flutter: mode=profile speed=1400px/s flutter: Solid color | frames=240 fps=59.75 | ui p50/p95=1.290/1.816ms | raster p50/p90/p95/p99/max=1.365/1.748/1.823/1.953/1.990ms | raster_over_budget=0/240 (0.0%) flutter: Linear gradient | frames=240 fps=59.75 | ui p50/p95=1.523/2.281ms | raster p50/p90/p95/p99/max=1.286/1.457/1.528/1.722/1.812ms | raster_over_budget=0/240 (0.0%) flutter: ================================================ flutter: === Windows linear-gradient comparison === flutter: mode=profile speed=1400px/s flutter: Solid color | frames=240 fps=59.75 | ui p50/p95=0.957/1.108ms | raster p50/p90/p95/p99/max=1.192/1.354/1.385/1.462/1.854ms | raster_over_budget=0/240 (0.0%) flutter: Linear gradient | frames=240 fps=59.75 | ui p50/p95=1.572/3.112ms | raster p50/p90/p95/p99/max=1.263/1.455/1.542/1.702/1.859ms | raster_over_budget=0/240 (0.0%) flutter: ================================================ flutter: === Windows linear-gradient comparison === flutter: mode=profile speed=1400px/s flutter: Solid color | frames=240 fps=59.75 | ui p50/p95=1.044/1.657ms | raster p50/p90/p95/p99/max=1.256/1.721/1.770/1.892/1.919ms | raster_over_budget=0/240 (0.0%) flutter: Linear gradient | frames=240 fps=59.75 | ui p50/p95=1.925/3.021ms | raster p50/p90/p95/p99/max=1.308/1.536/1.589/1.722/1.905ms | raster_over_budget=0/240 (0.0%) flutter: ================================================ ``` </details> ## 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: gaaclarke <[email protected]>
1 parent 0d2859d commit b3cef5e

13 files changed

Lines changed: 753 additions & 412 deletions

File tree

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "flutter/display_list/dl_paint.h"
1717
#include "flutter/testing/testing.h"
1818
#include "impeller/playground/widgets.h"
19+
#include "impeller/renderer/testing/mocks.h"
1920

2021
using namespace flutter;
2122
////////////////////////////////////////////////////////////////////////////////
@@ -52,6 +53,66 @@ void CanRenderLinearGradient(AiksTest* aiks_test, DlTileMode tile_mode) {
5253

5354
} // namespace
5455

56+
/// Runs tests with `SupportsSSBO()` forced to false. All other capabilities are
57+
/// forwarded to the real device.
58+
class AiksTestNoSSBO : public AiksTest {
59+
protected:
60+
void SetUp() override {
61+
AiksTest::SetUp();
62+
if (::testing::Test::IsSkipped()) {
63+
return;
64+
}
65+
old_capabilities_ = GetContext()->GetCapabilities();
66+
auto capabilities =
67+
std::make_shared<::testing::NiceMock<MockCapabilities>>();
68+
EXPECT_CALL(*capabilities, SupportsSSBO())
69+
.Times(::testing::AtLeast(1))
70+
.WillRepeatedly(::testing::Return(false));
71+
FLT_FORWARD(capabilities, old_capabilities_, GetDefaultColorFormat);
72+
FLT_FORWARD(capabilities, old_capabilities_, GetDefaultStencilFormat);
73+
FLT_FORWARD(capabilities, old_capabilities_, GetDefaultDepthStencilFormat);
74+
FLT_FORWARD(capabilities, old_capabilities_, GetDefaultGlyphAtlasFormat);
75+
FLT_FORWARD(capabilities, old_capabilities_, SupportsOffscreenMSAA);
76+
FLT_FORWARD(capabilities, old_capabilities_, SupportsImplicitResolvingMSAA);
77+
FLT_FORWARD(capabilities, old_capabilities_, SupportsReadFromResolve);
78+
FLT_FORWARD(capabilities, old_capabilities_, SupportsCompute);
79+
FLT_FORWARD(capabilities, old_capabilities_, SupportsTextureToTextureBlits);
80+
FLT_FORWARD(capabilities, old_capabilities_, SupportsTriangleFan);
81+
FLT_FORWARD(capabilities, old_capabilities_,
82+
SupportsDecalSamplerAddressMode);
83+
FLT_FORWARD(capabilities, old_capabilities_, SupportsPrimitiveRestart);
84+
FLT_FORWARD(capabilities, old_capabilities_, Supports32BitPrimitiveIndices);
85+
FLT_FORWARD(capabilities, old_capabilities_, NeedsPartitionedHostBuffer);
86+
FLT_FORWARD(capabilities, old_capabilities_, GetMinimumUniformAlignment);
87+
FLT_FORWARD(capabilities, old_capabilities_,
88+
GetMaximumRenderPassAttachmentSize);
89+
FLT_FORWARD(capabilities, old_capabilities_, GetMaxSamplerAnisotropy);
90+
EXPECT_TRUE(SetCapabilities(capabilities).ok());
91+
}
92+
93+
void TearDown() override {
94+
if (old_capabilities_) {
95+
std::ignore = SetCapabilities(
96+
std::const_pointer_cast<Capabilities>(old_capabilities_));
97+
}
98+
AiksTest::TearDown();
99+
}
100+
101+
private:
102+
std::shared_ptr<const Capabilities> old_capabilities_;
103+
};
104+
105+
INSTANTIATE_TEST_SUITE_P(
106+
Play,
107+
AiksTestNoSSBO,
108+
// Only run on MetalSDF: Metal is the only backend that implements
109+
// `SetCapabilities`, and these tests are specifically to test UberSDF
110+
// rendering without SSBO.
111+
::testing::Values(PlaygroundBackend::kMetalSDF),
112+
[](const ::testing::TestParamInfo<AiksTestNoSSBO::ParamType>& info) {
113+
return PlaygroundBackendToString(info.param);
114+
});
115+
55116
TEST_P(AiksTest, CanRenderLinearGradientClamp) {
56117
CanRenderLinearGradient(this, DlTileMode::kClamp);
57118
}
@@ -65,6 +126,12 @@ TEST_P(AiksTest, CanRenderLinearGradientDecal) {
65126
CanRenderLinearGradient(this, DlTileMode::kDecal);
66127
}
67128

129+
// Test non-SSBO for a two-stop gradient resulting in a two texel gradient
130+
// texture.
131+
TEST_P(AiksTestNoSSBO, CanRenderLinearGradientClamp) {
132+
CanRenderLinearGradient(this, DlTileMode::kClamp);
133+
}
134+
68135
TEST_P(AiksTest, CanRenderLinearGradientDecalWithColorFilter) {
69136
DisplayListBuilder builder;
70137
Point scale = GetContentScale();
@@ -224,6 +291,12 @@ TEST_P(AiksTest, CanRenderLinearGradientWithOverlappingStopsClamp) {
224291
CanRenderLinearGradientWithOverlappingStops(this, DlTileMode::kClamp);
225292
}
226293

294+
// Test non-SSBO for overlapping stops, which results in a maximum size gradient
295+
// texture.
296+
TEST_P(AiksTestNoSSBO, CanRenderLinearGradientWithOverlappingStopsClamp) {
297+
CanRenderLinearGradientWithOverlappingStops(this, DlTileMode::kClamp);
298+
}
299+
227300
namespace {
228301
void CanRenderGradientWithIncompleteStops(AiksTest* aiks_test,
229302
DlColorSourceType type) {
@@ -333,6 +406,18 @@ TEST_P(AiksTest, CanRenderRadialGradientWithIncompleteStops) {
333406
CanRenderGradientWithIncompleteStops(this,
334407
DlColorSourceType::kRadialGradient);
335408
}
409+
410+
// Test non-SSBO for UberSDF-supported gradients (linear and radial). Each of
411+
// these tests covers all four tile modes in one image.
412+
TEST_P(AiksTestNoSSBO, CanRenderLinearGradientWithIncompleteStops) {
413+
CanRenderGradientWithIncompleteStops(this,
414+
DlColorSourceType::kLinearGradient);
415+
}
416+
TEST_P(AiksTestNoSSBO, CanRenderRadialGradientWithIncompleteStops) {
417+
CanRenderGradientWithIncompleteStops(this,
418+
DlColorSourceType::kRadialGradient);
419+
}
420+
336421
TEST_P(AiksTest, CanRenderConicalGradientWithIncompleteStops) {
337422
CanRenderGradientWithIncompleteStops(this,
338423
DlColorSourceType::kConicalGradient);

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -333,14 +333,19 @@ CreateUberSDFGradientParameters(const ContentContext& renderer,
333333
return std::nullopt;
334334
}
335335

336-
GradientData gradient_data = CreateGradientBuffer(colors, stops);
337-
std::shared_ptr<Texture> texture =
338-
CreateGradientTexture(gradient_data, renderer.GetContext());
339-
if (!texture) {
340-
return std::nullopt;
336+
if (renderer.GetDeviceCapabilities().SupportsSSBO()) {
337+
gradient.colors = std::move(colors);
338+
gradient.stops = std::move(stops);
339+
} else {
340+
GradientData gradient_data = CreateGradientBuffer(colors, stops);
341+
std::shared_ptr<Texture> texture =
342+
CreateGradientTexture(gradient_data, renderer.GetContext());
343+
if (!texture) {
344+
return std::nullopt;
345+
}
346+
gradient.texture = std::move(texture);
341347
}
342348

343-
gradient.texture = std::move(texture);
344349
return gradient;
345350
}
346351

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ impeller_shaders("modern_entity_shaders") {
9191
"shaders/gradients/linear_gradient_ssbo_fill.frag",
9292
"shaders/gradients/radial_gradient_ssbo_fill.frag",
9393
"shaders/gradients/sweep_gradient_ssbo_fill.frag",
94+
"shaders/uber_sdf_ssbo.frag",
9495
]
9596
}
9697

‎engine/src/flutter/impeller/entity/contents/content_context.cc‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ struct ContentContext::Pipelines {
304304
Variants<VerticesUber1Shader> vertices_uber_1_;
305305
Variants<VerticesUber2Shader> vertices_uber_2_;
306306
Variants<UberSDFPipeline> uber_sdf;
307+
Variants<UberSDFSSBOPipeline> uber_sdf_ssbo;
307308
Variants<ComplexRSEPipeline> complex_rse;
308309
Variants<YUVToRGBFilterPipeline> yuv_to_rgb_filter;
309310

@@ -638,7 +639,11 @@ ContentContext::ContentContext(
638639
pipelines_->fast_gradient.CreateDefault(*context_, options);
639640
pipelines_->circle.CreateDefault(*context_, options);
640641
if (context_->GetFlags().use_sdfs) {
641-
pipelines_->uber_sdf.CreateDefault(*context_, options);
642+
if (context_->GetCapabilities()->SupportsSSBO()) {
643+
pipelines_->uber_sdf_ssbo.CreateDefault(*context_, options);
644+
} else {
645+
pipelines_->uber_sdf.CreateDefault(*context_, options);
646+
}
642647
pipelines_->complex_rse.CreateDefault(*context_, options);
643648
}
644649

@@ -1209,6 +1214,11 @@ PipelineRef ContentContext::GetUberSDFPipeline(
12091214
return GetPipeline(this, pipelines_->uber_sdf, opts);
12101215
}
12111216

1217+
PipelineRef ContentContext::GetUberSDFSSBOPipeline(
1218+
ContentContextOptions opts) const {
1219+
return GetPipeline(this, pipelines_->uber_sdf_ssbo, opts);
1220+
}
1221+
12121222
PipelineRef ContentContext::GetComplexRSEPipeline(
12131223
ContentContextOptions opts) const {
12141224
return GetPipeline(this, pipelines_->complex_rse, opts);

‎engine/src/flutter/impeller/entity/contents/content_context.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ class ContentContext {
214214
PipelineRef GetXorBlendPipeline(ContentContextOptions opts) const;
215215
PipelineRef GetYUVToRGBFilterPipeline(ContentContextOptions opts) const;
216216
PipelineRef GetUberSDFPipeline(ContentContextOptions opts) const;
217+
PipelineRef GetUberSDFSSBOPipeline(ContentContextOptions opts) const;
217218
PipelineRef GetComplexRSEPipeline(ContentContextOptions opts) const;
218219
#ifdef IMPELLER_ENABLE_OPENGLES
219220
#if !defined(FML_OS_EMSCRIPTEN)

‎engine/src/flutter/impeller/entity/contents/pipelines.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include "impeller/entity/texture_uv_fill.vert.h"
6464
#include "impeller/entity/tiled_texture_fill.frag.h"
6565
#include "impeller/entity/uber_sdf.frag.h"
66+
#include "impeller/entity/uber_sdf_ssbo.frag.h"
6667
#include "impeller/entity/vertices_uber_1.frag.h"
6768
#include "impeller/entity/vertices_uber_2.frag.h"
6869
#include "impeller/entity/yuv_to_rgb_filter.frag.h"
@@ -160,6 +161,7 @@ using TiledTexturePipeline = RenderPipelineHandle<TextureUvFillVertexShader, Til
160161
using VerticesUber1Shader = RenderPipelineHandle<PorterDuffBlendVertexShader, VerticesUber1FragmentShader>;
161162
using VerticesUber2Shader = RenderPipelineHandle<PorterDuffBlendVertexShader, VerticesUber2FragmentShader>;
162163
using UberSDFPipeline = RenderPipelineHandle<CircleVertexShader, UberSdfFragmentShader>;
164+
using UberSDFSSBOPipeline = RenderPipelineHandle<CircleVertexShader, UberSdfSsboFragmentShader>;
163165
using ComplexRSEPipeline = RenderPipelineHandle<CircleVertexShader, ComplexRseFragmentShader>;
164166
using YUVToRGBFilterPipeline = RenderPipelineHandle<FilterPositionVertexShader, YuvToRgbFilterFragmentShader>;
165167
// clang-format on

0 commit comments

Comments
 (0)