summaryrefslogtreecommitdiff
path: root/src/tests/cogl/conform/test-offscreen-texture-formats.c
blob: 7b6e61ec7000e6a3a563f809a6c789155120cc0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (C) 2022 Intel Corporation.
 * Copyright (C) 2022 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <cogl/cogl.h>

#include "tests/cogl-test-utils.h"

static int
get_bits (uint32_t in,
          int      end,
          int      begin)
{
  int mask = (1 << (end - begin + 1)) - 1;

  return (in >> begin) & mask;
}

static int
rgb10_to_rgb8 (int rgb10)
{
  float r;

  r = rgb10 / (float) (1 << 10);
  return (int) (r * (float) (1 << 8));
}

static int
rgb8_to_rgb10 (int rgb8)
{
  float r;

  r = rgb8 / (float) (1 << 8);
  return (int) (r * (float) (1 << 10));
}

static void
test_offscreen_texture_formats_store_rgb10 (void)
{
  const int rgb10_red = 514;
  const int rgb10_green = 258;
  const int rgb10_blue = 18;
  float red;
  float green;
  float blue;
  GError *error = NULL;
  CoglPixelFormat formats[] = {
    COGL_PIXEL_FORMAT_ABGR_2101010,
    COGL_PIXEL_FORMAT_ARGB_2101010,
    COGL_PIXEL_FORMAT_XRGB_2101010,
    COGL_PIXEL_FORMAT_RGBA_1010102,
    COGL_PIXEL_FORMAT_BGRA_1010102,
    COGL_PIXEL_FORMAT_XBGR_2101010,
  };
  int i;

  /* The extra fraction is there to avoid rounding inconsistencies in OpenGL
   * implementations. */
  red = (rgb10_red / (float) (1 << 10)) + 0.00001;
  green = (rgb10_green / (float) (1 << 10)) + 0.00001;
  blue = (rgb10_blue / (float) (1 << 10)) + 0.00001;

  /* 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_green)), !=, rgb10_green);
  g_assert_cmpint (rgb8_to_rgb10 (rgb10_to_rgb8 (rgb10_blue)), !=, rgb10_blue);

  for (i = 0; i < G_N_ELEMENTS (formats); i++)
    {
      CoglTexture2D *tex;
      CoglOffscreen *offscreen;
      uint32_t rgb8_readback[4];
      uint8_t *rgb8_buf;
      int j;

      /* Allocate 2x2 to ensure we avoid any fast paths. */
      tex = cogl_texture_2d_new_with_format (test_ctx, 2, 2, formats[i]);

      offscreen = cogl_offscreen_new_with_texture (COGL_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, 1.0);

      for (j = 0; j < G_N_ELEMENTS (formats); j++)
        {
          uint32_t rgb10_readback[4];
          int channels[3];
          int alpha;

          cogl_framebuffer_read_pixels (COGL_FRAMEBUFFER (offscreen), 0, 0, 2, 2,
                                        formats[j],
                                        (uint8_t *) &rgb10_readback);

          switch (formats[j])
            {
            case COGL_PIXEL_FORMAT_RGBA_1010102:
            case COGL_PIXEL_FORMAT_BGRA_1010102:
              channels[0] = get_bits (rgb10_readback[0], 31, 22);
              channels[1] = get_bits (rgb10_readback[0], 21, 12);
              channels[2] = get_bits (rgb10_readback[0], 11, 2);
              alpha = get_bits (rgb10_readback[0], 1, 0);
              break;
            case COGL_PIXEL_FORMAT_XRGB_2101010:
            case COGL_PIXEL_FORMAT_ARGB_2101010:
            case COGL_PIXEL_FORMAT_XBGR_2101010:
            case COGL_PIXEL_FORMAT_ABGR_2101010:
              alpha = get_bits (rgb10_readback[0], 31, 30);
              channels[0] = get_bits (rgb10_readback[0], 29, 20);
              channels[1] = get_bits (rgb10_readback[0], 19, 10);
              channels[2] = get_bits (rgb10_readback[0], 9, 0);
              break;
            default:
              g_assert_not_reached ();
            }

          g_assert_cmpint (alpha, ==, 0x3);

          switch (formats[j])
            {
            case COGL_PIXEL_FORMAT_RGBA_1010102:
            case COGL_PIXEL_FORMAT_XRGB_2101010:
            case COGL_PIXEL_FORMAT_ARGB_2101010:
              g_assert_cmpint (channels[0], ==, rgb10_red);
              g_assert_cmpint (channels[1], ==, rgb10_green);
              g_assert_cmpint (channels[2], ==, rgb10_blue);
              break;
            case COGL_PIXEL_FORMAT_BGRA_1010102:
            case COGL_PIXEL_FORMAT_XBGR_2101010:
            case COGL_PIXEL_FORMAT_ABGR_2101010:
              g_assert_cmpint (channels[0], ==, rgb10_blue);
              g_assert_cmpint (channels[1], ==, rgb10_green);
              g_assert_cmpint (channels[2], ==, rgb10_red);
              break;
            default:
              g_assert_not_reached ();
            }
        }

      cogl_framebuffer_read_pixels (COGL_FRAMEBUFFER (offscreen), 0, 0, 2, 2,
                                    COGL_PIXEL_FORMAT_RGBA_8888,
                                    (uint8_t *) &rgb8_readback);
      rgb8_buf = (uint8_t *) &rgb8_readback[0];

      g_assert_cmpint (rgb8_buf[0], ==, rgb10_to_rgb8 (rgb10_red));
      g_assert_cmpint (rgb8_buf[1], ==, rgb10_to_rgb8 (rgb10_green));
      g_assert_cmpint (rgb8_buf[2], ==, rgb10_to_rgb8 (rgb10_blue));
      g_assert_cmpint (rgb8_buf[3], ==, 0xff);

      g_object_unref (offscreen);
      cogl_object_unref (tex);
    }
}

static void
test_offscreen_texture_formats_paint_rgb10 (void)
{
  const int rgb10_red = 514;
  const int rgb10_green = 258;
  const int rgb10_blue = 18;
  float red;
  float green;
  float blue;
  CoglTexture2D *tex_src;
  CoglOffscreen *offscreen_src;
  CoglTexture2D *tex_dst;
  CoglOffscreen *offscreen_dst;
  CoglPipeline *pipeline;
  uint32_t rgb10_readback[4];
  GError *error = NULL;

  /* The extra fraction is there to avoid rounding inconsistencies in OpenGL
   * implementations. */
  red = (rgb10_red / (float) (1 << 10)) + 0.00001;
  green = (rgb10_green / (float) (1 << 10)) + 0.00001;
  blue = (rgb10_blue / (float) (1 << 10)) + 0.00001;

  /* 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_green)), !=, rgb10_green);
  g_assert_cmpint (rgb8_to_rgb10 (rgb10_to_rgb8 (rgb10_blue)), !=, rgb10_blue);

  tex_src = cogl_texture_2d_new_with_format (test_ctx, 2, 2,
                                             COGL_PIXEL_FORMAT_RGBA_1010102);
  offscreen_src = cogl_offscreen_new_with_texture (COGL_TEXTURE (tex_src));
  cogl_framebuffer_allocate (COGL_FRAMEBUFFER (offscreen_src), &error);
  g_assert_no_error (error);

  tex_dst = cogl_texture_2d_new_with_format (test_ctx, 2, 2,
                                             COGL_PIXEL_FORMAT_ABGR_2101010);
  offscreen_dst = cogl_offscreen_new_with_texture (COGL_TEXTURE (tex_dst));
  cogl_framebuffer_allocate (COGL_FRAMEBUFFER (offscreen_dst), &error);
  g_assert_no_error (error);

  cogl_framebuffer_clear4f (COGL_FRAMEBUFFER (offscreen_src),
                            COGL_BUFFER_BIT_COLOR,
                            red, green, blue, 1.0);

  pipeline = cogl_pipeline_new (test_ctx);
  cogl_pipeline_set_layer_texture (pipeline, 0, tex_src);
  cogl_framebuffer_draw_rectangle (COGL_FRAMEBUFFER (offscreen_dst),
                                   pipeline,
                                   -1.0, -1.0, 1.0, 1.0);
  cogl_object_unref (pipeline);

  cogl_framebuffer_read_pixels (COGL_FRAMEBUFFER (offscreen_dst), 0, 0, 2, 2,
                                COGL_PIXEL_FORMAT_ABGR_2101010,
                                (uint8_t *) &rgb10_readback);
  g_assert_cmpint (get_bits (rgb10_readback[0], 31, 30), ==, 0x3);
  g_assert_cmpint (get_bits (rgb10_readback[0], 29, 20), ==, rgb10_blue);
  g_assert_cmpint (get_bits (rgb10_readback[0], 19, 10), ==, rgb10_green);
  g_assert_cmpint (get_bits (rgb10_readback[0], 9, 0), ==, rgb10_red);
}

COGL_TEST_SUITE (
  g_test_add_func ("/offscreen/texture-formats/store-rgb10",
                   test_offscreen_texture_formats_store_rgb10);
  g_test_add_func ("/offscreen/texture-formats/paint-rgb10",
                   test_offscreen_texture_formats_paint_rgb10);
)