@@ -444,6 +444,27 @@ static inline void sw_vec4_transform(float dst[4], const float v[4], const sw_ma
444
444
}
445
445
}
446
446
447
+ static inline float sw_saturate (float x )
448
+ {
449
+ // After several comparisons, this saturation method
450
+ // seems to be the most optimized by GCC and Clang,
451
+ // and it does not produce any conditional branching.
452
+
453
+ // However, it is possible that a clamp could be
454
+ // more efficient on certain platforms.
455
+ // Comparisons will need to be made.
456
+
457
+ // SEE: https://godbolt.org/z/5qYznK5zj
458
+
459
+ // Saturation from below: max(0, x)
460
+ float y = 0.5f * (x + fabsf (x ));
461
+
462
+ // Saturation from above: min(1, y)
463
+ return y - 0.5f * ((y - 1.0f ) + fabsf (y - 1.0f ));
464
+
465
+ // return (x < 0.0f) ? 0.0f : ((x > 1.0f) ? 1.0f : x);
466
+ }
467
+
447
468
static inline float sw_lerp (float a , float b , float t )
448
469
{
449
470
return a + t * (b - a );
@@ -700,8 +721,7 @@ static inline void sw_map_repeat(int* out, float in, int max)
700
721
701
722
static inline void sw_map_clamp_to_edge (int * out , float in , int max )
702
723
{
703
- in = (in > 1.0f ) ? 1.0f : ((in < 0.0f ) ? 0.0f : in );
704
- * out = (int )(in * (max - 1 ) + 0.5f );
724
+ * out = (int )(sw_saturate (in ) * (max - 1 ) + 0.5f );
705
725
}
706
726
707
727
static inline void sw_map_mirrored_repeat (int * out , float in , int max )
@@ -991,23 +1011,17 @@ void FUNC_NAME(const sw_texture_t* tex, const sw_vertex_t* start,
991
1011
\
992
1012
/* Interpolate the color and modulate by the texture color */ \
993
1013
for (int i = 0 ; i < 4 ; i ++ ) { \
994
- float lerp = start -> color [i ] + t * dcol [i ]; \
995
- float finalColor = texColor [i ] * lerp ; \
996
- /* Inline clamp to keep the value between 0 and 1 */ \
997
- /* NOTE: The need for clamp the colors could be a sign of problem during interpolation (?) */ \
998
- finalColor = (finalColor < 0.0f ) ? 0.0f : (finalColor > 1.0f ? 1.0f : finalColor ); \
999
- dst [i ] = (uint8_t )(finalColor * 255.0f ); \
1014
+ float finalColor = texColor [i ]; \
1015
+ finalColor *= start -> color [i ] + t * dcol [i ]; \
1016
+ dst [i ] = (uint8_t )(sw_saturate (finalColor ) * 255.0f ); \
1000
1017
} \
1001
1018
} \
1002
1019
else \
1003
1020
{ \
1004
1021
/* Interpolate the color */ \
1005
1022
for (int i = 0 ; i < 4 ; i ++ ) { \
1006
1023
float finalColor = start -> color [i ] + t * dcol [i ]; \
1007
- /* Inline clamp to keep the value between 0 and 1 */ \
1008
- /* NOTE: The need for clamp the colors could be a sign of problem during interpolation (?) */ \
1009
- finalColor = (finalColor < 0.0f ) ? 0.0f : (finalColor > 1.0f ? 1.0f : finalColor ); \
1010
- dst [i ] = (uint8_t )(finalColor * 255.0f ); \
1024
+ dst [i ] = (uint8_t )(sw_saturate (finalColor ) * 255.0f ); \
1011
1025
} \
1012
1026
} \
1013
1027
\
0 commit comments