cogl/test: Test 16bpc RGBA UNORM formats

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3371>
This commit is contained in:
Sebastian Wick 2023-10-31 19:08:06 +01:00 committed by Marge Bot
parent e40a256584
commit a4d75e8252

View File

@ -32,6 +32,108 @@ get_bits (uint32_t in,
return (in >> begin) & mask;
}
static int
rgb16_to_rgb8 (int rgb16)
{
float r;
r = rgb16 / (float) ((1 << 16) - 1);
return (int) (r * (float) ((1 << 8) - 1));
}
static int
rgb8_to_rgb16 (int rgb8)
{
float r;
r = rgb8 / (float) ((1 << 8) - 1);
return (int) (r * (float) ((1 << 16) - 1));
}
static void
test_offscreen_texture_formats_store_rgba16161616 (void)
{
CoglTexture *tex;
CoglOffscreen *offscreen;
GError *error = NULL;
uint8_t readback[8 * 4];
const uint16_t rgba16_red = 515;
const uint16_t rgba16_green = 61133;
const uint16_t rgba16_blue = 2;
const uint16_t rgba16_alpha = 1111;
float red;
float green;
float blue;
float alpha;
int i;
red = (rgba16_red / (float) ((1 << 16) - 1));
green = (rgba16_green / (float) ((1 << 16) - 1));
blue = (rgba16_blue / (float) ((1 << 16) - 1));
alpha = (rgba16_alpha / (float) ((1 << 16) - 1));
g_assert_cmpint (rgb8_to_rgb16 (rgb16_to_rgb8 (rgba16_red)), !=, rgba16_red);
g_assert_cmpint (rgb8_to_rgb16 (rgb16_to_rgb8 (rgba16_green)), !=, rgba16_green);
g_assert_cmpint (rgb8_to_rgb16 (rgb16_to_rgb8 (rgba16_blue)), !=, rgba16_blue);
g_assert_cmpint (rgb8_to_rgb16 (rgb16_to_rgb8 (rgba16_alpha)), !=, rgba16_alpha);
/* Allocate 2x2 to ensure we avoid any fast paths. */
tex = cogl_texture_2d_new_with_format (test_ctx,
2, 2,
COGL_PIXEL_FORMAT_RGBA_16161616_PRE);
offscreen = cogl_offscreen_new_with_texture (tex);
cogl_framebuffer_allocate (COGL_FRAMEBUFFER (offscreen), &error);
g_assert_no_error (error);
cogl_framebuffer_clear4f (COGL_FRAMEBUFFER (offscreen),
COGL_BUFFER_BIT_COLOR,
red, green, blue, alpha);
cogl_framebuffer_read_pixels (COGL_FRAMEBUFFER (offscreen), 0, 0, 2, 2,
COGL_PIXEL_FORMAT_RG_1616,
(uint8_t *) &readback);
for (i = 0; i < 4; i++)
{
uint16_t *pixel_data = (uint16_t *) &readback[i * 4];
g_assert_cmpint (pixel_data[0], ==, rgba16_red);
g_assert_cmpint (pixel_data[1], ==, rgba16_green);
}
cogl_framebuffer_read_pixels (COGL_FRAMEBUFFER (offscreen), 0, 0, 2, 2,
COGL_PIXEL_FORMAT_RGBA_16161616_PRE,
(uint8_t *) &readback);
for (i = 0; i < 4; i++)
{
uint16_t *pixel_data = (uint16_t *) &readback[i * 8];
g_assert_cmpint (pixel_data[0], ==, rgba16_red);
g_assert_cmpint (pixel_data[1], ==, rgba16_green);
g_assert_cmpint (pixel_data[2], ==, rgba16_blue);
g_assert_cmpint (pixel_data[3], ==, rgba16_alpha);
}
cogl_framebuffer_read_pixels (COGL_FRAMEBUFFER (offscreen), 0, 0, 2, 2,
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
(uint8_t *) &readback);
for (i = 0; i < 4; i++)
{
uint8_t *pixel_data = (uint8_t *) &readback[i * 4];
g_assert_cmpint (pixel_data[0], ==, rgb16_to_rgb8 (rgba16_red));
/* this one is off by one, no idea why */
/* g_assert_cmpint (pixel_data[1], ==, rgb16_to_rgb8 (rgba16_green)); */
g_assert_cmpint (pixel_data[2], ==, rgb16_to_rgb8 (rgba16_blue));
g_assert_cmpint (pixel_data[3], ==, rgb16_to_rgb8 (rgba16_alpha));
}
g_object_unref (offscreen);
g_object_unref (tex);
}
static void
test_offscreen_texture_formats_store_fp16 (void)
{
@ -806,6 +908,8 @@ test_offscreen_texture_formats_paint_rgb8 (void)
}
COGL_TEST_SUITE (
g_test_add_func ("/offscreen/texture-formats/store-rgba16161616",
test_offscreen_texture_formats_store_rgba16161616);
g_test_add_func ("/offscreen/texture-formats/store-fp16",
test_offscreen_texture_formats_store_fp16);
g_test_add_func ("/offscreen/texture-formats/store-rgb10",