5 #include "test-utils.h"
7 typedef struct _TestState
13 #define PRIM_COLOR 0xff00ffff
14 #define TEX_COLOR 0x0000ffff
18 typedef CoglPrimitive * (* TestPrimFunc) (CoglContext *ctx, guint32 *expected_color);
20 static CoglPrimitive *
21 test_prim_p2 (CoglContext *ctx, guint32 *expected_color)
23 static const CoglVertexP2 verts[] =
24 { { 0, 0 }, { 0, 10 }, { 10, 0 } };
26 return cogl_primitive_new_p2 (ctx,
27 COGL_VERTICES_MODE_TRIANGLES,
32 static CoglPrimitive *
33 test_prim_p3 (CoglContext *ctx, guint32 *expected_color)
35 static const CoglVertexP3 verts[] =
36 { { 0, 0, 0 }, { 0, 10, 0 }, { 10, 0, 0 } };
38 return cogl_primitive_new_p3 (ctx,
39 COGL_VERTICES_MODE_TRIANGLES,
44 static CoglPrimitive *
45 test_prim_p2c4 (CoglContext *ctx, guint32 *expected_color)
47 static const CoglVertexP2C4 verts[] =
48 { { 0, 0, 255, 255, 0, 255 },
49 { 0, 10, 255, 255, 0, 255 },
50 { 10, 0, 255, 255, 0, 255 } };
52 *expected_color = 0xffff00ff;
54 return cogl_primitive_new_p2c4 (ctx,
55 COGL_VERTICES_MODE_TRIANGLES,
60 static CoglPrimitive *
61 test_prim_p3c4 (CoglContext *ctx, guint32 *expected_color)
63 static const CoglVertexP3C4 verts[] =
64 { { 0, 0, 0, 255, 255, 0, 255 },
65 { 0, 10, 0, 255, 255, 0, 255 },
66 { 10, 0, 0, 255, 255, 0, 255 } };
68 *expected_color = 0xffff00ff;
70 return cogl_primitive_new_p3c4 (ctx,
71 COGL_VERTICES_MODE_TRIANGLES,
76 static CoglPrimitive *
77 test_prim_p2t2 (CoglContext *ctx, guint32 *expected_color)
79 static const CoglVertexP2T2 verts[] =
84 *expected_color = TEX_COLOR;
86 return cogl_primitive_new_p2t2 (ctx,
87 COGL_VERTICES_MODE_TRIANGLES,
92 static CoglPrimitive *
93 test_prim_p3t2 (CoglContext *ctx, guint32 *expected_color)
95 static const CoglVertexP3T2 verts[] =
100 *expected_color = TEX_COLOR;
102 return cogl_primitive_new_p3t2 (ctx,
103 COGL_VERTICES_MODE_TRIANGLES,
108 static CoglPrimitive *
109 test_prim_p2t2c4 (CoglContext *ctx, guint32 *expected_color)
111 static const CoglVertexP2T2C4 verts[] =
112 { { 0, 0, 1, 0, 0xff, 0xff, 0xf0, 0xff },
113 { 0, 10, 1, 0, 0xff, 0xff, 0xf0, 0xff },
114 { 10, 0, 1, 0, 0xff, 0xff, 0xf0, 0xff } };
116 /* The blue component of the texture color should be replaced with 0xf0 */
117 *expected_color = (TEX_COLOR & 0xffff00ff) | 0x0000f000;
119 return cogl_primitive_new_p2t2c4 (ctx,
120 COGL_VERTICES_MODE_TRIANGLES,
125 static CoglPrimitive *
126 test_prim_p3t2c4 (CoglContext *ctx, guint32 *expected_color)
128 static const CoglVertexP3T2C4 verts[] =
129 { { 0, 0, 0, 1, 0, 0xff, 0xff, 0xf0, 0xff },
130 { 0, 10, 0, 1, 0, 0xff, 0xff, 0xf0, 0xff },
131 { 10, 0, 0, 1, 0, 0xff, 0xff, 0xf0, 0xff } };
133 /* The blue component of the texture color should be replaced with 0xf0 */
134 *expected_color = (TEX_COLOR & 0xffff00ff) | 0x0000f000;
136 return cogl_primitive_new_p3t2c4 (ctx,
137 COGL_VERTICES_MODE_TRIANGLES,
142 static const TestPrimFunc
156 test_paint (TestState *state)
158 CoglPipeline *pipeline;
163 /* Create a two pixel texture. The first pixel is white and the
164 second pixel is tex_color. The assumption is that if no texture
165 coordinates are specified then it will default to 0,0 and get
170 tex_data[3] = (TEX_COLOR >> 24) & 0xff;
171 tex_data[4] = (TEX_COLOR >> 16) & 0xff;
172 tex_data[5] = (TEX_COLOR >> 8) & 0xff;
173 tex = cogl_texture_new_from_data (2, 1, /* size */
174 COGL_TEXTURE_NO_ATLAS,
175 COGL_PIXEL_FORMAT_RGB_888,
176 COGL_PIXEL_FORMAT_ANY,
179 pipeline = cogl_pipeline_new (ctx);
180 cogl_pipeline_set_color4ub (pipeline,
181 (PRIM_COLOR >> 24) & 0xff,
182 (PRIM_COLOR >> 16) & 0xff,
183 (PRIM_COLOR >> 8) & 0xff,
184 (PRIM_COLOR >> 0) & 0xff);
185 cogl_pipeline_set_layer_texture (pipeline, 0, tex);
186 cogl_object_unref (tex);
188 for (i = 0; i < G_N_ELEMENTS (test_prim_funcs); i++)
191 guint32 expected_color = PRIM_COLOR;
193 prim = test_prim_funcs[i] (ctx, &expected_color);
195 cogl_framebuffer_push_matrix (fb);
196 cogl_framebuffer_translate (fb, i * 10, 0, 0);
197 cogl_framebuffer_draw_primitive (fb, pipeline, prim);
198 cogl_framebuffer_pop_matrix (fb);
200 test_utils_check_pixel (fb, i * 10 + 2, 2, expected_color);
202 cogl_object_unref (prim);
205 cogl_object_unref (pipeline);
209 get_attributes_cb (CoglPrimitive *prim,
210 CoglAttribute *attrib,
213 CoglAttribute ***p = user_data;
219 compare_pointers (const void *a, const void *b)
221 CoglAttribute *pa = *(CoglAttribute **) a;
222 CoglAttribute *pb = *(CoglAttribute **) b;
233 test_copy (TestState *state)
235 static const guint16 indices_data[2] = { 1, 2 };
236 CoglAttributeBuffer *buffer =
237 cogl_attribute_buffer_new (ctx, 100, NULL);
238 CoglAttribute *attributes[N_ATTRIBS];
239 CoglAttribute *attributes_a[N_ATTRIBS], *attributes_b[N_ATTRIBS];
241 CoglPrimitive *prim_a, *prim_b;
242 CoglIndices *indices;
245 for (i = 0; i < N_ATTRIBS; i++)
247 char *name = g_strdup_printf ("foo_%i", i);
248 attributes[i] = cogl_attribute_new (buffer,
253 COGL_ATTRIBUTE_TYPE_FLOAT);
257 prim_a = cogl_primitive_new_with_attributes (COGL_VERTICES_MODE_TRIANGLES,
262 indices = cogl_indices_new (ctx,
263 COGL_INDICES_TYPE_UNSIGNED_SHORT,
267 cogl_primitive_set_first_vertex (prim_a, 12);
268 cogl_primitive_set_indices (prim_a, indices, 2);
270 prim_b = cogl_primitive_copy (prim_a);
273 cogl_primitive_foreach_attribute (prim_a,
276 g_assert_cmpint (p - attributes_a, ==, N_ATTRIBS);
279 cogl_primitive_foreach_attribute (prim_b,
282 g_assert_cmpint (p - attributes_b, ==, N_ATTRIBS);
284 qsort (attributes_a, N_ATTRIBS, sizeof (CoglAttribute *), compare_pointers);
285 qsort (attributes_b, N_ATTRIBS, sizeof (CoglAttribute *), compare_pointers);
287 g_assert (memcmp (attributes_a, attributes_b, sizeof (attributes_a)) == 0);
289 g_assert_cmpint (cogl_primitive_get_first_vertex (prim_a),
291 cogl_primitive_get_first_vertex (prim_b));
293 g_assert_cmpint (cogl_primitive_get_n_vertices (prim_a),
295 cogl_primitive_get_n_vertices (prim_b));
297 g_assert_cmpint (cogl_primitive_get_mode (prim_a),
299 cogl_primitive_get_mode (prim_b));
301 g_assert (cogl_primitive_get_indices (prim_a) ==
302 cogl_primitive_get_indices (prim_b));
304 cogl_object_unref (prim_a);
305 cogl_object_unref (prim_b);
306 cogl_object_unref (indices);
308 for (i = 0; i < N_ATTRIBS; i++)
309 cogl_object_unref (attributes[i]);
311 cogl_object_unref (buffer);
315 test_primitive (void)
319 state.fb_width = cogl_framebuffer_get_width (fb);
320 state.fb_height = cogl_framebuffer_get_height (fb);
322 cogl_framebuffer_orthographic (fb,
332 if (cogl_test_verbose ())