Skip to content

Commit 1c5559c

Browse files
committed
fixed float rounding problem
1 parent bcc5a3b commit 1c5559c

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,14 @@ SamplerBinding SetupGradientParameters(
7979
if (gradient.type == UberSDFParameters::GradientParameters::Type::kLinear) {
8080
Point delta = gradient.end - gradient.start;
8181
Scalar length_sq = delta.x * delta.x + delta.y * delta.y;
82-
Point direction = length_sq > 0.0f ? delta / length_sq : Point(0.0f, 0.0f);
8382
frag_info.gradient_coords =
84-
Vector4(gradient.start.x, gradient.start.y, direction.x, direction.y);
83+
Vector4(gradient.start.x, gradient.start.y, delta.x, delta.y);
84+
frag_info.inv_gradient_length = length_sq > 0.0f ? 1.0f / length_sq : 0.0f;
8585
} else {
86-
Scalar inv_radius = gradient.end.x > 0.0f ? 1.0f / gradient.end.x : 0.0f;
8786
frag_info.gradient_coords =
88-
Vector4(gradient.start.x, gradient.start.y, inv_radius, 0.0f);
87+
Vector4(gradient.start.x, gradient.start.y, 0.0f, 0.0f);
88+
frag_info.inv_gradient_length =
89+
gradient.end.x > 0.0f ? 1.0f / gradient.end.x : 0.0f;
8990
}
9091

9192
SamplerDescriptor sampler_desc;
@@ -142,6 +143,7 @@ bool UberSDFContents::Render(const ContentContext& renderer,
142143
frag_info.gradient_coords = Vector4();
143144
frag_info.half_texel = 0.0f;
144145
frag_info.tile_mode = 0.0f;
146+
frag_info.inv_gradient_length = 0.0f;
145147

146148
SamplerBinding sampler_binding;
147149
if (params_.gradient) {

‎engine/src/flutter/impeller/entity/shaders/uber_sdf.frag‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,13 @@ uniform FragInfo {
3030
/// bottom-right), or the circular cap radii for rounded superellipses in
3131
/// radii.xy (top octant in x, right octant in y).
3232
vec4 radii;
33-
/// Gradient parameters packed to avoid shader-side uniform division:
33+
/// Gradient parameters:
3434
/// - Linear gradient:
3535
/// xy: Start point in local coordinates.
36-
/// zw: Pre-scaled direction vector (delta / dot(delta, delta)), such
37-
/// that dot(v_position - xy, zw) yields the [0, 1] parameter t.
36+
/// zw: Delta vector (end - start) in local coordinates.
3837
/// - Radial gradient:
3938
/// xy: Center point in local coordinates.
40-
/// z: Inverse radius (1.0 / radius), such that
41-
/// length(v_position - xy) * z yields the [0, 1] parameter t.
42-
/// w: Unused (0.0).
39+
/// zw: Unused (0.0).
4340
vec4 gradient_coords;
4441

4542
// ===========================================================================
@@ -108,6 +105,10 @@ uniform FragInfo {
108105
/// Half the size of a single gradient texel in normalized texture
109106
/// coordinates along the gradient ramp (x axis).
110107
float half_texel;
108+
/// Inverse gradient length:
109+
/// - Linear gradient: 1.0 / dot(delta, delta)
110+
/// - Radial gradient: 1.0 / radius
111+
float inv_gradient_length;
111112
}
112113
frag_info;
113114

@@ -126,11 +127,12 @@ vec4 getColor() {
126127
if (frag_info.color_source_type < 1.5) {
127128
// Linear gradient
128129
t = dot(v_position - frag_info.gradient_coords.xy,
129-
frag_info.gradient_coords.zw);
130+
frag_info.gradient_coords.zw) *
131+
frag_info.inv_gradient_length;
130132
} else {
131133
// Radial gradient
132134
t = length(v_position - frag_info.gradient_coords.xy) *
133-
frag_info.gradient_coords.z;
135+
frag_info.inv_gradient_length;
134136
}
135137
vec4 gradient_color = IPSampleLinearWithTileMode(
136138
color_source_sampler, vec2(t, 0.5), vec2(frag_info.half_texel, 0.5),

0 commit comments

Comments
 (0)