4 #include "test-utils.h"
8 typedef struct _TestState
16 create_texture (CoglTextureFlags flags)
18 guint8 *data = g_malloc (TEX_SIZE * TEX_SIZE * 4), *p = data;
22 for (y = 0; y < TEX_SIZE; y++)
23 for (x = 0; x < TEX_SIZE; x++)
26 *(p++) = (x & 1) * 255;
27 *(p++) = (y & 1) * 255;
31 tex = cogl_texture_new_from_data (TEX_SIZE, TEX_SIZE, flags,
32 COGL_PIXEL_FORMAT_RGBA_8888_PRE,
33 COGL_PIXEL_FORMAT_ANY,
42 create_pipeline (TestState *state,
43 CoglPipelineWrapMode wrap_mode_s,
44 CoglPipelineWrapMode wrap_mode_t)
46 CoglPipeline *pipeline;
48 pipeline = cogl_pipeline_new (ctx);
49 cogl_pipeline_set_layer_texture (pipeline, 0, state->texture);
50 cogl_pipeline_set_layer_filters (pipeline, 0,
51 COGL_PIPELINE_FILTER_NEAREST,
52 COGL_PIPELINE_FILTER_NEAREST);
53 cogl_pipeline_set_layer_wrap_mode_s (pipeline, 0, wrap_mode_s);
54 cogl_pipeline_set_layer_wrap_mode_t (pipeline, 0, wrap_mode_t);
59 static CoglPipelineWrapMode
62 COGL_PIPELINE_WRAP_MODE_REPEAT,
63 COGL_PIPELINE_WRAP_MODE_REPEAT,
65 COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE,
66 COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE,
68 COGL_PIPELINE_WRAP_MODE_REPEAT,
69 COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE,
71 COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE,
72 COGL_PIPELINE_WRAP_MODE_REPEAT,
74 COGL_PIPELINE_WRAP_MODE_AUTOMATIC,
75 COGL_PIPELINE_WRAP_MODE_AUTOMATIC,
77 COGL_PIPELINE_WRAP_MODE_AUTOMATIC,
78 COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE
82 draw_tests (TestState *state)
86 for (i = 0; i < G_N_ELEMENTS (wrap_modes); i += 2)
88 CoglPipelineWrapMode wrap_mode_s, wrap_mode_t;
89 CoglPipeline *pipeline;
91 /* Create a separate pipeline for each pair of wrap modes so
92 that we can verify whether the batch splitting works */
93 wrap_mode_s = wrap_modes[i];
94 wrap_mode_t = wrap_modes[i + 1];
95 pipeline = create_pipeline (state, wrap_mode_s, wrap_mode_t);
96 /* Render the pipeline at four times the size of the texture */
97 cogl_framebuffer_draw_textured_rectangle (fb,
104 cogl_object_unref (pipeline);
108 static const CoglTextureVertex vertices[4] =
110 { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f },
111 { 0.0f, TEX_SIZE * 2, 0.0f, 0.0f, 2.0f },
112 { TEX_SIZE * 2, TEX_SIZE * 2, 0.0f, 2.0f, 2.0f },
113 { TEX_SIZE * 2, 0.0f, 0.0f, 2.0f, 0.0f }
117 draw_tests_polygon (TestState *state)
121 for (i = 0; i < G_N_ELEMENTS (wrap_modes); i += 2)
123 CoglPipelineWrapMode wrap_mode_s, wrap_mode_t;
124 CoglPipeline *pipeline;
126 wrap_mode_s = wrap_modes[i];
127 wrap_mode_t = wrap_modes[i + 1];
128 pipeline = create_pipeline (state, wrap_mode_s, wrap_mode_t);
129 cogl_set_source (pipeline);
130 cogl_object_unref (pipeline);
132 cogl_translate (TEX_SIZE * i, 0.0f, 0.0f);
133 /* Render the pipeline at four times the size of the texture */
134 cogl_polygon (vertices, G_N_ELEMENTS (vertices), FALSE);
140 draw_tests_vbo (TestState *state)
145 vbo = cogl_vertex_buffer_new (4);
146 cogl_vertex_buffer_add (vbo, "gl_Vertex", 3,
147 COGL_ATTRIBUTE_TYPE_FLOAT, FALSE,
148 sizeof (vertices[0]),
150 cogl_vertex_buffer_add (vbo, "gl_MultiTexCoord0", 2,
151 COGL_ATTRIBUTE_TYPE_FLOAT, FALSE,
152 sizeof (vertices[0]),
154 cogl_vertex_buffer_submit (vbo);
156 for (i = 0; i < G_N_ELEMENTS (wrap_modes); i += 2)
158 CoglPipelineWrapMode wrap_mode_s, wrap_mode_t;
159 CoglPipeline *pipeline;
161 wrap_mode_s = wrap_modes[i];
162 wrap_mode_t = wrap_modes[i + 1];
163 pipeline = create_pipeline (state, wrap_mode_s, wrap_mode_t);
164 cogl_set_source (pipeline);
165 cogl_object_unref (pipeline);
167 cogl_translate (TEX_SIZE * i, 0.0f, 0.0f);
168 /* Render the pipeline at four times the size of the texture */
169 cogl_vertex_buffer_draw (vbo, COGL_VERTICES_MODE_TRIANGLE_FAN, 0, 4);
173 cogl_handle_unref (vbo);
177 validate_set (TestState *state, int offset)
179 guint8 data[TEX_SIZE * 2 * TEX_SIZE * 2 * 4], *p;
182 for (i = 0; i < G_N_ELEMENTS (wrap_modes); i += 2)
184 CoglPipelineWrapMode wrap_mode_s, wrap_mode_t;
186 wrap_mode_s = wrap_modes[i];
187 wrap_mode_t = wrap_modes[i + 1];
189 cogl_framebuffer_read_pixels (fb, i * TEX_SIZE, offset * TEX_SIZE * 2,
190 TEX_SIZE * 2, TEX_SIZE * 2,
191 COGL_PIXEL_FORMAT_RGBA_8888,
196 for (y = 0; y < TEX_SIZE * 2; y++)
197 for (x = 0; x < TEX_SIZE * 2; x++)
202 wrap_mode_s == COGL_PIPELINE_WRAP_MODE_REPEAT ||
203 wrap_mode_s == COGL_PIPELINE_WRAP_MODE_AUTOMATIC)
204 green = (x & 1) * 255;
206 green = ((TEX_SIZE - 1) & 1) * 255;
209 wrap_mode_t == COGL_PIPELINE_WRAP_MODE_REPEAT ||
210 wrap_mode_t == COGL_PIPELINE_WRAP_MODE_AUTOMATIC)
211 blue = (y & 1) * 255;
213 blue = ((TEX_SIZE - 1) & 1) * 255;
215 g_assert_cmpint (p[0], ==, 0);
216 g_assert_cmpint (p[1], ==, green);
217 g_assert_cmpint (p[2], ==, blue);
225 validate_result (TestState *state)
227 validate_set (state, 0); /* non-atlased rectangle */
228 #if 0 /* this doesn't currently work */
229 validate_set (state, 1); /* atlased rectangle */
231 validate_set (state, 2); /* cogl_polygon */
232 validate_set (state, 3); /* vertex buffer */
236 paint (TestState *state)
238 /* Draw the tests first with a non atlased texture */
239 state->texture = create_texture (COGL_TEXTURE_NO_ATLAS);
241 cogl_object_unref (state->texture);
243 /* Draw the tests again with a possible atlased texture. This should
244 end up testing software repeats */
245 state->texture = create_texture (COGL_TEXTURE_NONE);
247 cogl_translate (0.0f, TEX_SIZE * 2.0f, 0.0f);
250 cogl_object_unref (state->texture);
252 /* Draw the tests using cogl_polygon */
253 state->texture = create_texture (COGL_TEXTURE_NO_ATLAS);
255 cogl_translate (0.0f, TEX_SIZE * 4.0f, 0.0f);
256 draw_tests_polygon (state);
258 cogl_object_unref (state->texture);
260 /* Draw the tests using a vertex buffer */
261 state->texture = create_texture (COGL_TEXTURE_NO_ATLAS);
263 cogl_translate (0.0f, TEX_SIZE * 6.0f, 0.0f);
264 draw_tests_vbo (state);
266 cogl_object_unref (state->texture);
268 validate_result (state);
272 test_wrap_modes (void)
276 state.width = cogl_framebuffer_get_width (fb);
277 state.height = cogl_framebuffer_get_height (fb);
279 cogl_framebuffer_orthographic (fb,
286 /* XXX: we have to push/pop a framebuffer since this test currently
287 * uses the legacy cogl_vertex_buffer_draw() api. */
288 cogl_push_framebuffer (fb);
290 cogl_pop_framebuffer ();
292 if (cogl_test_verbose ())