summaryrefslogtreecommitdiff
path: root/tests/conform/test-wrap-modes.c
blob: e5a2a1a61864779c1de71a0069fd536b79730bb7 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#define COGL_VERSION_MIN_REQUIRED COGL_VERSION_1_0

#include <cogl/cogl.h>
#include <string.h>

#include "test-utils.h"

#define TEX_SIZE 4

typedef struct _TestState
{
  int width;
  int height;
  CoglTexture *texture;
} TestState;

static CoglTexture *
create_texture (TestUtilsTextureFlags flags)
{
  uint8_t *data = g_malloc (TEX_SIZE * TEX_SIZE * 4), *p = data;
  CoglTexture *tex;
  int x, y;

  for (y = 0; y < TEX_SIZE; y++)
    for (x = 0; x < TEX_SIZE; x++)
      {
        *(p++) = 0;
        *(p++) = (x & 1) * 255;
        *(p++) = (y & 1) * 255;
        *(p++) = 255;
      }

  tex = test_utils_texture_new_from_data (test_ctx,
                                          TEX_SIZE, TEX_SIZE, flags,
                                          COGL_PIXEL_FORMAT_RGBA_8888_PRE,
                                          TEX_SIZE * 4,
                                          data);
  g_free (data);

  return tex;
}

static CoglPipeline *
create_pipeline (TestState *state,
                 CoglPipelineWrapMode wrap_mode_s,
                 CoglPipelineWrapMode wrap_mode_t)
{
  CoglPipeline *pipeline;

  pipeline = cogl_pipeline_new (test_ctx);
  cogl_pipeline_set_layer_texture (pipeline, 0, state->texture);
  cogl_pipeline_set_layer_filters (pipeline, 0,
                                   COGL_PIPELINE_FILTER_NEAREST,
                                   COGL_PIPELINE_FILTER_NEAREST);
  cogl_pipeline_set_layer_wrap_mode_s (pipeline, 0, wrap_mode_s);
  cogl_pipeline_set_layer_wrap_mode_t (pipeline, 0, wrap_mode_t);

  return pipeline;
}

static CoglPipelineWrapMode
wrap_modes[] =
  {
    COGL_PIPELINE_WRAP_MODE_REPEAT,
    COGL_PIPELINE_WRAP_MODE_REPEAT,

    COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE,
    COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE,

    COGL_PIPELINE_WRAP_MODE_REPEAT,
    COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE,

    COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE,
    COGL_PIPELINE_WRAP_MODE_REPEAT,

    COGL_PIPELINE_WRAP_MODE_AUTOMATIC,
    COGL_PIPELINE_WRAP_MODE_AUTOMATIC,

    COGL_PIPELINE_WRAP_MODE_AUTOMATIC,
    COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE
  };

static void
draw_tests (TestState *state)
{
  int i;

  for (i = 0; i < G_N_ELEMENTS (wrap_modes); i += 2)
    {
      CoglPipelineWrapMode wrap_mode_s, wrap_mode_t;
      CoglPipeline *pipeline;

      /* Create a separate pipeline for each pair of wrap modes so
         that we can verify whether the batch splitting works */
      wrap_mode_s = wrap_modes[i];
      wrap_mode_t = wrap_modes[i + 1];
      pipeline = create_pipeline (state, wrap_mode_s, wrap_mode_t);
      /* Render the pipeline at four times the size of the texture */
      cogl_framebuffer_draw_textured_rectangle (test_fb,
                                                pipeline,
                                                i * TEX_SIZE,
                                                0,
                                                (i + 2) * TEX_SIZE,
                                                TEX_SIZE * 2,
                                                0, 0, 2, 2);
      cogl_object_unref (pipeline);
    }
}

static const CoglTextureVertex vertices[4] =
  {
    { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f },
    { 0.0f, TEX_SIZE * 2, 0.0f, 0.0f, 2.0f },
    { TEX_SIZE * 2, TEX_SIZE * 2, 0.0f, 2.0f, 2.0f },
    { TEX_SIZE * 2, 0.0f, 0.0f, 2.0f, 0.0f }
  };

static void
draw_tests_polygon (TestState *state)
{
  int i;

  for (i = 0; i < G_N_ELEMENTS (wrap_modes); i += 2)
    {
      CoglPipelineWrapMode wrap_mode_s, wrap_mode_t;
      CoglPipeline *pipeline;

      wrap_mode_s = wrap_modes[i];
      wrap_mode_t = wrap_modes[i + 1];
      pipeline = create_pipeline (state, wrap_mode_s, wrap_mode_t);
      cogl_set_source (pipeline);
      cogl_object_unref (pipeline);
      cogl_push_matrix ();
      cogl_translate (TEX_SIZE * i, 0.0f, 0.0f);
      /* Render the pipeline at four times the size of the texture */
      cogl_polygon (vertices, G_N_ELEMENTS (vertices), FALSE);
      cogl_pop_matrix ();
    }
}

static void
draw_tests_vbo (TestState *state)
{
  CoglHandle vbo;
  int i;

  vbo = cogl_vertex_buffer_new (4);
  cogl_vertex_buffer_add (vbo, "gl_Vertex", 3,
                          COGL_ATTRIBUTE_TYPE_FLOAT, FALSE,
                          sizeof (vertices[0]),
                          &vertices[0].x);
  cogl_vertex_buffer_add (vbo, "gl_MultiTexCoord0", 2,
                          COGL_ATTRIBUTE_TYPE_FLOAT, FALSE,
                          sizeof (vertices[0]),
                          &vertices[0].tx);
  cogl_vertex_buffer_submit (vbo);

  for (i = 0; i < G_N_ELEMENTS (wrap_modes); i += 2)
    {
      CoglPipelineWrapMode wrap_mode_s, wrap_mode_t;
      CoglPipeline *pipeline;

      wrap_mode_s = wrap_modes[i];
      wrap_mode_t = wrap_modes[i + 1];
      pipeline = create_pipeline (state, wrap_mode_s, wrap_mode_t);
      cogl_set_source (pipeline);
      cogl_object_unref (pipeline);
      cogl_push_matrix ();
      cogl_translate (TEX_SIZE * i, 0.0f, 0.0f);
      /* Render the pipeline at four times the size of the texture */
      cogl_vertex_buffer_draw (vbo, COGL_VERTICES_MODE_TRIANGLE_FAN, 0, 4);
      cogl_pop_matrix ();
    }

  cogl_handle_unref (vbo);
}

static void
validate_set (TestState *state, int offset)
{
  uint8_t data[TEX_SIZE * 2 * TEX_SIZE * 2 * 4], *p;
  int x, y, i;

  for (i = 0; i < G_N_ELEMENTS (wrap_modes); i += 2)
    {
      CoglPipelineWrapMode wrap_mode_s, wrap_mode_t;

      wrap_mode_s = wrap_modes[i];
      wrap_mode_t = wrap_modes[i + 1];

      cogl_framebuffer_read_pixels (test_fb, i * TEX_SIZE, offset * TEX_SIZE * 2,
                                    TEX_SIZE * 2, TEX_SIZE * 2,
                                    COGL_PIXEL_FORMAT_RGBA_8888,
                                    data);

      p = data;

      for (y = 0; y < TEX_SIZE * 2; y++)
        for (x = 0; x < TEX_SIZE * 2; x++)
          {
            uint8_t green, blue;

            if (x < TEX_SIZE ||
                wrap_mode_s == COGL_PIPELINE_WRAP_MODE_REPEAT ||
                wrap_mode_s == COGL_PIPELINE_WRAP_MODE_AUTOMATIC)
              green = (x & 1) * 255;
            else
              green = ((TEX_SIZE - 1) & 1) * 255;

            if (y < TEX_SIZE ||
                wrap_mode_t == COGL_PIPELINE_WRAP_MODE_REPEAT ||
                wrap_mode_t == COGL_PIPELINE_WRAP_MODE_AUTOMATIC)
              blue = (y & 1) * 255;
            else
              blue = ((TEX_SIZE - 1) & 1) * 255;

            g_assert_cmpint (p[0], ==, 0);
            g_assert_cmpint (p[1], ==, green);
            g_assert_cmpint (p[2], ==, blue);

            p += 4;
          }
    }
}

static void
validate_result (TestState *state)
{
  validate_set (state, 0); /* non-atlased rectangle */
#if 0 /* this doesn't currently work */
  validate_set (state, 1); /* atlased rectangle */
#endif
  validate_set (state, 2); /* cogl_polygon */
  validate_set (state, 3); /* vertex buffer */
}

static void
paint (TestState *state)
{
  /* Draw the tests first with a non atlased texture */
  state->texture = create_texture (TEST_UTILS_TEXTURE_NO_ATLAS);
  draw_tests (state);
  cogl_object_unref (state->texture);

  /* Draw the tests again with a possible atlased texture. This should
     end up testing software repeats */
  state->texture = create_texture (TEST_UTILS_TEXTURE_NONE);
  cogl_framebuffer_push_matrix (test_fb);
  cogl_framebuffer_translate (test_fb, 0.0f, TEX_SIZE * 2.0f, 0.0f);
  draw_tests (state);
  cogl_pop_matrix ();
  cogl_object_unref (state->texture);

  /* Draw the tests using cogl_polygon */
  state->texture = create_texture (COGL_TEXTURE_NO_ATLAS);
  cogl_push_matrix ();
  cogl_translate (0.0f, TEX_SIZE * 4.0f, 0.0f);
  draw_tests_polygon (state);
  cogl_pop_matrix ();
  cogl_object_unref (state->texture);

  /* Draw the tests using a vertex buffer */
  state->texture = create_texture (COGL_TEXTURE_NO_ATLAS);
  cogl_push_matrix ();
  cogl_translate (0.0f, TEX_SIZE * 6.0f, 0.0f);
  draw_tests_vbo (state);
  cogl_pop_matrix ();
  cogl_object_unref (state->texture);

  validate_result (state);
}

void
test_wrap_modes (void)
{
  TestState state;

  state.width = cogl_framebuffer_get_width (test_fb);
  state.height = cogl_framebuffer_get_height (test_fb);

  cogl_framebuffer_orthographic (test_fb,
                                 0, 0,
                                 state.width,
                                 state.height,
                                 -1,
                                 100);

  /* XXX: we have to push/pop a framebuffer since this test currently
   * uses the legacy cogl_vertex_buffer_draw() api. */
  cogl_push_framebuffer (test_fb);
  paint (&state);
  cogl_pop_framebuffer ();

  if (cogl_test_verbose ())
    g_print ("OK\n");
}