@@ -92,6 +92,29 @@ struct TxtShaderInOut_XOR {
92
92
float2 tpCoords;
93
93
};
94
94
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
+
95
118
template <typename Uniforms> inline
96
119
float4 frag_single_grad (float a, Uniforms uniforms)
97
120
{
@@ -130,7 +153,7 @@ float4 frag_multi_grad(float a, Uniforms uniforms)
130
153
131
154
float4 c = mix (uniforms.color [n], uniforms.color [n + 1 ], a);
132
155
if (uniforms.isLinear ) {
133
- c.rgb = 1.055 * pow (c.rgb , float3 ( 0.416667 )) - 0.055 ;
156
+ c.rgb = fromLinear3 (c.rgb ) ;
134
157
}
135
158
return c;
136
159
}
@@ -328,7 +351,7 @@ fragment half4 frag_txt_grad(GradShaderInOut in [[stage_in]],
328
351
329
352
float4 renderColor = renderTexture.sample (textureSampler, in.texCoords );
330
353
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 );
332
355
float a = (dot (v,uniforms.params )-0.25 )*2.0 ;
333
356
return half4 (frag_single_grad (a, uniforms)*renderColor.a )*uniforms.extraAlpha ;
334
357
}
@@ -355,7 +378,7 @@ fragment half4 frag_txt_rad_grad(GradShaderInOut in [[stage_in]],
355
378
356
379
float4 renderColor = renderTexture.sample (textureSampler, in.texCoords );
357
380
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 );
359
382
float x = dot (fragCoord, uniforms.m0 );
360
383
float y = dot (fragCoord, uniforms.m1 );
361
384
float xfx = x - uniforms.precalc .x ;
@@ -501,22 +524,22 @@ fragment half4 frag_txt_op_lookup(
501
524
502
525
fragment half4 frag_grad (GradShaderInOut in [[stage_in]],
503
526
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 );
505
528
float a = (dot (v,uniforms.params )-0.25 )*2.0 ;
506
529
return half4 (frag_single_grad (a, uniforms)) * uniforms.extraAlpha ;
507
530
}
508
531
509
532
// LinGradFrameUniforms
510
533
fragment half4 frag_lin_grad (GradShaderInOut in [[stage_in]],
511
534
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 );
513
536
float a = dot (v, uniforms.params );
514
537
return half4 (frag_multi_grad (a, uniforms))*uniforms.extraAlpha ;
515
538
}
516
539
517
540
fragment half4 frag_rad_grad (GradShaderInOut in [[stage_in]],
518
541
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 );
520
543
float x = dot (fragCoord, uniforms.m0 );
521
544
float y = dot (fragCoord, uniforms.m1 );
522
545
float xfx = x - uniforms.precalc .x ;
0 commit comments