Visitar URL original
Fixes for texture-backed gradients by b-luk · Pull Request #191925 · flutter/flutter · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,12 @@ vec4 IPSampleLinearWithTileMode(sampler2D tex,
float x_tile_mode,
float y_tile_mode,
vec4 decal_border_color) {
if (x_tile_mode == kTileModeDecal && (coords.x < 0 || coords.x >= 1) ||
y_tile_mode == kTileModeDecal && (coords.y < 0 || coords.y >= 1)) {
// Unlike 2D image sampling which uses a half-open interval [0, 1) where 1.0
// is outside the texture boundary, linear sampling uses a closed interval
// [0, 1]. Therefore, decal mode checks the ending coordinate using > 1 rather
// than >= 1.
if (x_tile_mode == kTileModeDecal && (coords.x < 0.0 || coords.x > 1.0) ||
y_tile_mode == kTileModeDecal && (coords.y < 0.0 || coords.y > 1.0)) {
return decal_border_color;
}

Expand Down
19 changes: 17 additions & 2 deletions engine/src/flutter/impeller/geometry/geometry_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1834,13 +1834,28 @@ TEST(GeometryTest, Gradient) {
}

{
// Gradient with duplicate stops does not create an empty texture.
// Gradient with duplicate stops creates a 1024 texture.
std::vector<Color> colors = {Color::Red(), Color::Yellow(), Color::Black(),
Color::Blue()};
std::vector<Scalar> stops = {0.0, 0.25, 0.25, 1.0};

auto gradient = CreateGradientBuffer(colors, stops);
ASSERT_EQ(gradient.texture_size, 5u);
ASSERT_EQ(gradient.texture_size, 1024u);

std::vector<Color> expected_colors(1024);
for (size_t i = 0; i < 1024; i++) {
double t = i / 1023.0;
if (i < 256) {
// Interval 1: texels 0 to 255 (stop interval 0 to 0.25)
expected_colors[i] =
Color::Lerp(Color::Red(), Color::Yellow(), t / 0.25);
} else {
// Interval 2: texels 256 to 1023 (stop interval 0.25 to 1.0)
expected_colors[i] =
Color::Lerp(Color::Black(), Color::Blue(), (t - 0.25) / 0.75);
}
}
ASSERT_COLOR_BUFFER_NEAR(gradient.color_bytes, expected_colors);
}

{
Expand Down
36 changes: 18 additions & 18 deletions engine/src/flutter/impeller/geometry/gradient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

namespace impeller {

// TODO(b-luk): this should use a platform specific max texture size.
// https://github.com/flutter/flutter/issues/191820
static constexpr uint32_t kMaxGradientTextureSize = 1024;

static void AppendColor(const Color& color, GradientData* data) {
auto converted = color.ToR8G8B8A8();
data->color_bytes.push_back(converted[0]);
Expand All @@ -26,30 +30,29 @@ GradientData CreateGradientBuffer(const std::vector<Color>& colors,
texture_size = colors.size();
} else {
auto minimum_delta = 1.0;
// Avoid creating textures that are absurdly large due to stops that are
// very close together.
auto minimum_allowable_delta = 1.0 / (kMaxGradientTextureSize - 1.0);
for (size_t i = 1; i < stops.size(); i++) {
auto value = stops[i] - stops[i - 1];
// Smaller than kEhCloseEnough
if (value < 0.0001) {
continue;
if (value <= minimum_allowable_delta) {
minimum_delta = minimum_allowable_delta;
break;
}
if (value < minimum_delta) {
minimum_delta = value;
}
}
// Avoid creating textures that are absurdly large due to stops that are
// very close together.
// TODO(jonahwilliams): this should use a platform specific max texture
// size.
texture_size = std::min(
static_cast<uint32_t>(std::round(1.0 / minimum_delta)) + 1, 1024u);
texture_size = static_cast<uint32_t>(std::round(1.0 / minimum_delta)) + 1;
}
GradientData data = {
.color_bytes = {},
.texture_size = texture_size,
};
data.color_bytes.reserve(texture_size * 4);

if (texture_size == colors.size() && colors.size() <= 1024) {
if (texture_size == colors.size() &&
colors.size() <= kMaxGradientTextureSize) {
for (auto i = 0u; i < colors.size(); i++) {
AppendColor(colors[i], &data);
}
Expand Down Expand Up @@ -79,17 +82,14 @@ GradientData CreateGradientBuffer(const std::vector<Color>& colors,

AppendColor(mixed_color, &data);
} else {
// We've slightly overshot the previous stop.
// We've passed the next stop. Advance to the next stop interval.
// Decrement `i` to re-evaluate the current texel against the new
// interval. When there are overlapping or zero-delta stops, this branch
// evaluates repeatedly until the overlap is passed.
previous_color = next_color;
previous_stop = next_stop;
previous_color_index += 1;
next_color = colors[previous_color_index + 1];
auto next_stop = stops[previous_color_index + 1];

auto t = (scaled_i - previous_stop) / (next_stop - previous_stop);
auto mixed_color = Color::Lerp(previous_color, next_color, t);

AppendColor(mixed_color, &data);
i--;
}
Comment thread
b-luk marked this conversation as resolved.
Resolved
}
// The last index is always equal to the last color, exactly.
Expand Down
85 changes: 44 additions & 41 deletions engine/src/flutter/impeller/tools/malioc.json
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@
],
"longest_path_cycles": [
0.675000011920929,
0.46875,
0.484375,
0.675000011920929,
0.1875,
0.0,
Expand Down Expand Up @@ -778,9 +778,9 @@
"arith_cvt"
],
"total_cycles": [
0.862500011920929,
0.84375,
0.59375,
0.862500011920929,
0.84375,
0.3125,
0.0,
0.25,
Expand Down Expand Up @@ -816,8 +816,8 @@
"arith_fma"
],
"longest_path_cycles": [
0.328125,
0.328125,
0.34375,
0.34375,
0.296875,
0.125,
0.0,
Expand All @@ -834,12 +834,14 @@
"texture"
],
"shortest_path_bound_pipelines": [
"arith_total",
"arith_fma",
"varying"
],
"shortest_path_cycles": [
0.234375,
0.234375,
0.203125,
0.25,
0.25,
0.1875,
0.125,
0.0,
0.25,
Expand All @@ -850,9 +852,9 @@
"arith_cvt"
],
"total_cycles": [
0.421875,
0.40625,
0.359375,
0.421875,
0.40625,
0.125,
0.0,
0.25,
Expand Down Expand Up @@ -889,7 +891,7 @@
],
"longest_path_cycles": [
0.421875,
0.328125,
0.34375,
0.421875,
0.125,
0.0,
Expand Down Expand Up @@ -922,9 +924,9 @@
"arith_cvt"
],
"total_cycles": [
0.546875,
0.53125,
0.359375,
0.546875,
0.53125,
0.125,
0.0,
0.25,
Expand Down Expand Up @@ -9365,13 +9367,15 @@
"has_stack_spilling": false,
"performance": {
"longest_path_bound_pipelines": [
"arith_total",
"arith_fma",
"varying",
"texture"
],
"longest_path_cycles": [
0.234375,
0.234375,
0.234375,
0.25,
0.25,
0.21875,
0.0625,
0.0,
0.25,
Expand All @@ -9390,9 +9394,9 @@
"varying"
],
"shortest_path_cycles": [
0.171875,
0.140625,
0.171875,
0.15625,
0.15625,
0.15625,
0.0625,
0.0,
0.25,
Expand All @@ -9403,9 +9407,9 @@
"arith_cvt"
],
"total_cycles": [
0.328125,
0.265625,
0.328125,
0.3125,
0.28125,
0.3125,
0.0625,
0.0,
0.25,
Expand Down Expand Up @@ -9906,12 +9910,12 @@
"performance": {
"longest_path_bound_pipelines": [
"arith_total",
"arith_cvt"
"arith_fma"
],
"longest_path_cycles": [
0.265625,
0.25,
0.265625,
0.25,
0.125,
0.0,
0.25,
Expand All @@ -9930,9 +9934,9 @@
"varying"
],
"shortest_path_cycles": [
0.203125,
0.15625,
0.203125,
0.1875,
0.171875,
0.1875,
0.125,
0.0,
0.25,
Expand All @@ -9943,9 +9947,9 @@
"arith_cvt"
],
"total_cycles": [
0.359375,
0.28125,
0.359375,
0.34375,
0.296875,
0.34375,
0.125,
0.0,
0.25,
Expand Down Expand Up @@ -10884,9 +10888,9 @@
"arith_fma"
],
"longest_path_cycles": [
0.40625,
0.40625,
0.296875,
0.421875,
0.421875,
0.28125,
0.3125,
0.0,
0.25,
Expand All @@ -10903,13 +10907,12 @@
],
"shortest_path_bound_pipelines": [
"arith_total",
"arith_fma",
"arith_sfu"
"arith_fma"
],
"shortest_path_cycles": [
0.3125,
0.3125,
0.234375,
0.328125,
0.328125,
0.21875,
0.3125,
0.0,
0.25,
Expand All @@ -10920,9 +10923,9 @@
"arith_fma"
],
"total_cycles": [
0.4375,
0.4375,
0.390625,
0.453125,
0.453125,
0.375,
0.3125,
0.0,
0.25,
Expand Down