Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.

Commit ffff4e1

Browse files
author
Alexey Ushakov
committed
8259853: Lanai: nonAA Gradient painting is not precise for VI
1 parent 61b650c commit ffff4e1

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/java.desktop/macosx/native/libawt_lwawt/awt/shaders.metal

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ struct TxtShaderInOut_XOR {
9292
float2 tpCoords;
9393
};
9494

95+
inline float fromLinear(float c)
96+
{
97+
if (isnan(c)) c = 0.0;
98+
if (c > 1.0)
99+
c = 1.0;
100+
else if (c < 0.0)
101+
c = 0.0;
102+
else if (c < 0.0031308)
103+
c = 12.92 * c;
104+
else
105+
c = 1.055 * powr(c, 1.0/2.4) - 0.055;
106+
return c;
107+
}
108+
109+
inline float3 fromLinear3(float3 c) {
110+
//c.r = fromLinear(c.r);
111+
//c.g = fromLinear(c.g);
112+
//c.b = fromLinear(c.b);
113+
// Use approximated calculations to match software rendering
114+
c.rgb = 1.055 * pow(c.rgb, float3(0.416667)) - 0.055;
115+
return c;
116+
}
117+
95118
template <typename Uniforms> inline
96119
float4 frag_single_grad(float a, Uniforms uniforms)
97120
{
@@ -130,7 +153,7 @@ float4 frag_multi_grad(float a, Uniforms uniforms)
130153

131154
float4 c = mix(uniforms.color[n], uniforms.color[n + 1], a);
132155
if (uniforms.isLinear) {
133-
c.rgb = 1.055 * pow(c.rgb, float3(0.416667)) - 0.055;
156+
c.rgb = fromLinear3(c.rgb);
134157
}
135158
return c;
136159
}
@@ -328,7 +351,7 @@ fragment half4 frag_txt_grad(GradShaderInOut in [[stage_in]],
328351

329352
float4 renderColor = renderTexture.sample(textureSampler, in.texCoords);
330353

331-
float3 v = float3(in.position.x, in.position.y, 1);
354+
float3 v = float3(in.position.x-0.5, in.position.y-0.5, 1);
332355
float a = (dot(v,uniforms.params)-0.25)*2.0;
333356
return half4(frag_single_grad(a, uniforms)*renderColor.a)*uniforms.extraAlpha;
334357
}
@@ -355,7 +378,7 @@ fragment half4 frag_txt_rad_grad(GradShaderInOut in [[stage_in]],
355378

356379
float4 renderColor = renderTexture.sample(textureSampler, in.texCoords);
357380

358-
float3 fragCoord = float3(in.position.x+0.5, in.position.y-0.5, 1);
381+
float3 fragCoord = float3(in.position.x-0.5, in.position.y-0.5, 1);
359382
float x = dot(fragCoord, uniforms.m0);
360383
float y = dot(fragCoord, uniforms.m1);
361384
float xfx = x - uniforms.precalc.x;
@@ -501,22 +524,22 @@ fragment half4 frag_txt_op_lookup(
501524

502525
fragment half4 frag_grad(GradShaderInOut in [[stage_in]],
503526
constant GradFrameUniforms& uniforms [[buffer(0)]]) {
504-
float3 v = float3(in.position.x, in.position.y, 1);
527+
float3 v = float3(in.position.x-0.5, in.position.y-0.5, 1);
505528
float a = (dot(v,uniforms.params)-0.25)*2.0;
506529
return half4(frag_single_grad(a, uniforms)) * uniforms.extraAlpha;
507530
}
508531

509532
// LinGradFrameUniforms
510533
fragment half4 frag_lin_grad(GradShaderInOut in [[stage_in]],
511534
constant LinGradFrameUniforms& uniforms [[buffer(0)]]) {
512-
float3 v = float3(in.position.x, in.position.y, 1);
535+
float3 v = float3(in.position.x-0.5, in.position.y-0.5, 1);
513536
float a = dot(v, uniforms.params);
514537
return half4(frag_multi_grad(a, uniforms))*uniforms.extraAlpha;
515538
}
516539

517540
fragment half4 frag_rad_grad(GradShaderInOut in [[stage_in]],
518541
constant RadGradFrameUniforms& uniforms [[buffer(0)]]) {
519-
float3 fragCoord = float3(in.position.x+0.5, in.position.y-0.5, 1);
542+
float3 fragCoord = float3(in.position.x-0.5, in.position.y-0.5, 1);
520543
float x = dot(fragCoord, uniforms.m0);
521544
float y = dot(fragCoord, uniforms.m1);
522545
float xfx = x - uniforms.precalc.x;

0 commit comments

Comments
 (0)