Skip to content

Commit 83b8fb7

Browse files
committed
fix(impeller): preserve SDF gradient coordinate precision
Keep gradient coordinates, inverse lengths, and shared linear/radial calculations at high precision. Mediump dot products can overflow on ordinary desktop-sized gradients and clamp abruptly to the final color.
1 parent 0d708a8 commit 83b8fb7

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

‎engine/src/flutter/impeller/compiler/shader_lib/impeller/gradient.glsl‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,19 @@ vec3 IPComputeFixedGradientValues(float t, float colors_length) {
149149
return vec3(lower_index, upper_index, scale);
150150
}
151151

152-
float IPComputeLinearGradientT(vec2 positon,
153-
vec2 start,
154-
vec2 delta,
155-
float inv_gradient_length) {
152+
// Gradient dot products can overflow mediump even when the normalized
153+
// result is between zero and one. Preserve coordinate precision in both
154+
// helpers.
155+
highp float IPComputeLinearGradientT(highp vec2 positon,
156+
highp vec2 start,
157+
highp vec2 delta,
158+
highp float inv_gradient_length) {
156159
return dot(positon - start, delta) * inv_gradient_length;
157160
}
158161

159-
float IPComputeRadialGradientT(vec2 position,
160-
vec2 center,
161-
float inv_gradient_length) {
162+
highp float IPComputeRadialGradientT(highp vec2 position,
163+
highp vec2 center,
164+
highp float inv_gradient_length) {
162165
return length(position - center) * inv_gradient_length;
163166
}
164167

‎engine/src/flutter/impeller/entity/shaders/uber_sdf_common.glsl‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ uniform FragInfo {
3838
/// - Radial gradient:
3939
/// xy: Center point in local coordinates.
4040
/// zw: Unused (0.0).
41-
vec4 gradient_coords;
41+
highp vec4 gradient_coords;
4242

4343
// ===========================================================================
4444
// vec2 fields
@@ -103,10 +103,11 @@ uniform FragInfo {
103103
/// 2: Mirror
104104
/// 3: Decal
105105
float tile_mode;
106+
/// Retain small reciprocals for large gradients with high precision.
106107
/// Inverse gradient length:
107108
/// - Linear gradient: 1.0 / dot(delta, delta)
108109
/// - Radial gradient: 1.0 / radius
109-
float inv_gradient_length;
110+
highp float inv_gradient_length;
110111

111112
// --- Variant-Specific Gradient Parameters ---
112113
// Exactly one of these is used by any given entrypoint; the other is unset.
@@ -141,7 +142,7 @@ vec4 getColor() {
141142
// Solid color
142143
color = frag_info.color;
143144
} else {
144-
float t;
145+
highp float t;
145146
if (frag_info.color_source_type < 1.5) {
146147
// Linear gradient
147148
t = IPComputeLinearGradientT(v_position, frag_info.gradient_coords.xy,

0 commit comments

Comments
 (0)