cogl/tests: Fix float->int, int->float conversions

To obtain a float between 0 and 1 we have to devide the integer by the
highest possible value instead of the number of values.

Fixes off by one errors in the tests on some hardware/driver
combinations.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3065>
This commit is contained in:
Sebastian Wick 2023-07-12 18:03:08 +02:00 committed by Marge Bot
parent 4097cbbb53
commit d79e4c182e

View File

@ -36,8 +36,8 @@ rgb10_to_rgb8 (int rgb10)
{ {
float r; float r;
r = rgb10 / (float) (1 << 10); r = rgb10 / (float) ((1 << 10) - 1);
return (int) (r * (float) (1 << 8)); return (int) (r * (float) ((1 << 8) - 1));
} }
static int static int
@ -45,8 +45,8 @@ rgb8_to_rgb10 (int rgb8)
{ {
float r; float r;
r = rgb8 / (float) (1 << 8); r = rgb8 / (float) ((1 << 8) - 1);
return (int) (r * (float) (1 << 10)); return (int) (r * (float) ((1 << 10) - 1));
} }
static void static void
@ -73,10 +73,10 @@ test_offscreen_texture_formats_store_rgb10 (void)
/* The extra fraction is there to avoid rounding inconsistencies in OpenGL /* The extra fraction is there to avoid rounding inconsistencies in OpenGL
* implementations. */ * implementations. */
red = (rgb10_red / (float) (1 << 10)) + 0.00001; red = (rgb10_red / (float) ((1 << 10) - 1)) + 0.00001;
green = (rgb10_green / (float) (1 << 10)) + 0.00001; green = (rgb10_green / (float) ((1 << 10) - 1)) + 0.00001;
blue = (rgb10_blue / (float) (1 << 10)) + 0.00001; blue = (rgb10_blue / (float) ((1 << 10) - 1)) + 0.00001;
alpha = (rgb10_alpha / (float) (1 << 2)) + 0.00001; alpha = (rgb10_alpha / (float) ((1 << 2) - 1)) + 0.00001;
/* Make sure that that the color value can't be represented using rgb8. */ /* Make sure that that the color value can't be represented using rgb8. */
g_assert_cmpint (rgb8_to_rgb10 (rgb10_to_rgb8 (rgb10_red)), !=, rgb10_red); g_assert_cmpint (rgb8_to_rgb10 (rgb10_to_rgb8 (rgb10_red)), !=, rgb10_red);
@ -304,10 +304,10 @@ test_offscreen_texture_formats_paint_rgb10 (void)
/* The extra fraction is there to avoid rounding inconsistencies in OpenGL /* The extra fraction is there to avoid rounding inconsistencies in OpenGL
* implementations. */ * implementations. */
red = (rgb10_red / (float) (1 << 10)) + 0.00001; red = (rgb10_red / (float) ((1 << 10 ) - 1)) + 0.00001;
green = (rgb10_green / (float) (1 << 10)) + 0.00001; green = (rgb10_green / (float) ((1 << 10) - 1)) + 0.00001;
blue = (rgb10_blue / (float) (1 << 10)) + 0.00001; blue = (rgb10_blue / (float) ((1 << 10) - 1)) + 0.00001;
alpha = (rgb10_alpha / (float) (1 << 2)) + 0.00001; alpha = (rgb10_alpha / (float) ((1 << 2) - 1)) + 0.00001;
/* Make sure that that the color value can't be represented using rgb8. */ /* Make sure that that the color value can't be represented using rgb8. */
g_assert_cmpint (rgb8_to_rgb10 (rgb10_to_rgb8 (rgb10_red)), !=, rgb10_red); g_assert_cmpint (rgb8_to_rgb10 (rgb10_to_rgb8 (rgb10_red)), !=, rgb10_red);