On Fri, 13 Nov 2026 14:52:18 GMT, Christopher Schnick <[email protected]> 
wrote:

>> It seems like long ago there were tiny rendering differences encountered 
>> between the sw and hardware renderer when rendering rounded edges. Instead 
>> of fixing the root cause, a workaround was added to just render the affected 
>> edges using a manual sw renderer approach. It seems like everyong forgot, 
>> and this workaround is still here. As a result, any region with uniform 
>> corner radius 0 < r <= 4 is rendered using a software renderering approach 
>> right now.
>> 
>> Maybe the issue has been fixed somehow, the linked issue is very unspecific. 
>> Even if not, and there are differences between the renderers, they don't 
>> warrant this performance slowdown. I can't see a difference so far with this 
>> workaround disabled.
>> 
>> You can see this case being logged when being run with 
>> `-Djavafx.pulseLogger=true`. According to the counters at the end of a 
>> render pass, around 10% of nodes were rendered using this ineffecient 
>> approach in our application.
>> 
>> This case still happens for non-uniform corner radius values, but that is 
>> somewhat expected.
>> 
>> ---------
>> - [x] I confirm that I make this contribution in accordance with the 
>> [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai).
>
> Christopher Schnick has updated the pull request incrementally with one 
> additional commit since the last revision:
> 
>   Adjust round rect shader formula

Here is my understanding so far:

The round rect shader treats the straight edges as part of the corner ellipse 
and uses the ellipse coverage formula there.
I think that the general pixel size calculation (which is needed for the 
coverage formula and antialiasing) is wrong in multiple shaders related to 
ellipses, not just this case.

That formula works in coordinates scaled by the corner radius, so the edge is 
always at distance `1`. To antialias, it needs to know how big one screen pixel 
is in those scaled coordinates, which is `1/r`. Because elliptical corners can 
scale x and y differently, the shader estimates this pixel size per pixel 
instead of using a constant. (Otherwise we could just use the fixed `1/r` 
constant)
I guess this is a necessary for the few cases where people use 
`-fx-background-radius: 10 / 5` to set horizontal and vertical radii 
independently or something like `-fx-background-radius: 50%` on a non-square 
region.

On the straight edges that pixel size estimate should be `1/r`. The shader's 
estimate is only correct for pixels right on the edge, and for pixels inside 
the edge it comes out larger than `1/r`. With a large radius the difference is 
tiny but with small radius it isn't. E.g. at `r = 2` the pixel just inside the 
edge is treated larger than its real size.
Its coverage is then not 100% which gives the behaviour at the edge.

Let `p = (x, y)` be the pixel's scaled position relative to the nearest point 
of the inner rectangle (the rectangle inset by the corner radius), and `|p|` 
its length.
Let `r` be the radius in that direction.
On a straight edge `p` only has an `x` or `y` component, not both (The shader 
technically clamps the other component to `0.001` to not cause issues with `0`).
The formula in the shader is `pix = (|p| / |p|²) * (1/r) = 1 / (|p| * r)` 
instead of `1/r`, so the extra division by `|p|` makes the pixel size too large 
wherever `|p| < 1`, e.g. `1 / (0.75 * 2) = 0.67` instead of `0.5` for the inner 
edge pixel at `r = 2`.
This was likely meant to normalize `p` to its direction `p / |p|` for the 
general corner rendering, but it divides by the squared length `|p|²` that was 
already computed, presumably to avoid a `sqrt`.
The difference is 0 only right on the edge (`|p| = 1`), which is probably why 
it went unnoticed.
But it breaks for pixels inside the edge.

The fix would be to compute the pixel size as a weighted sum of `1/rx` and 
`1/ry` weighted by direction:


pix = (x² * 1/rx + y² * 1/ry) / (x² + y²)


On a straight edge only one axis counts, so this is exactly `1/r` for every 
pixel, and the edge is rendered like a normal rectangle.
The corners will also be rendered normally.
I also replaced the squared distance approximation in the coverage formula with 
the actual distance (`sqrt`), so edges that don't sit on pixel boundaries get 
exact coverage too.
There was also a mismatch where most shaders used a `0.25*pix²` correction term 
that `MaskDrawRoundRect` and `MaskDrawEllipse` didn't have.

The changes I pushed should fix the issue from what I tested

-------------

PR Comment: https://git.openjdk.org/jfx/pull/2329#issuecomment-6856566544

Reply via email to