5 #include "test-utils.h"
7 typedef struct _TestState
9 CoglPipeline *pipeline;
30 test_float_verts (TestState *state, int offset_x, int offset_y)
32 CoglAttribute *attributes[2];
33 CoglAttributeBuffer *buffer;
35 static const FloatVert float_verts[] =
37 { 0, 10, /**/ 1, 0, 0, 1 },
38 { 10, 10, /**/ 1, 0, 0, 1 },
39 { 5, 0, /**/ 1, 0, 0, 1 },
41 { 10, 10, /**/ 0, 1, 0, 1 },
42 { 20, 10, /**/ 0, 1, 0, 1 },
43 { 15, 0, /**/ 0, 1, 0, 1 }
46 buffer = cogl_attribute_buffer_new (ctx,
47 sizeof (float_verts), float_verts);
48 attributes[0] = cogl_attribute_new (buffer,
51 G_STRUCT_OFFSET (FloatVert, x),
53 COGL_ATTRIBUTE_TYPE_SHORT);
54 attributes[1] = cogl_attribute_new (buffer,
57 G_STRUCT_OFFSET (FloatVert, r),
59 COGL_ATTRIBUTE_TYPE_FLOAT);
61 cogl_framebuffer_push_matrix (fb);
62 cogl_framebuffer_translate (fb, offset_x, offset_y, 0.0f);
64 cogl_framebuffer_draw_attributes (fb,
66 COGL_VERTICES_MODE_TRIANGLES,
70 2 /* n_attributes */);
72 cogl_framebuffer_pop_matrix (fb);
74 cogl_object_unref (attributes[1]);
75 cogl_object_unref (attributes[0]);
76 cogl_object_unref (buffer);
78 test_utils_check_pixel (fb, offset_x + 5, offset_y + 5, 0xff0000ff);
79 test_utils_check_pixel (fb, offset_x + 15, offset_y + 5, 0x00ff00ff);
83 test_byte_verts (TestState *state, int offset_x, int offset_y)
85 CoglAttribute *attributes[2];
86 CoglAttributeBuffer *buffer, *unnorm_buffer;
88 static const ByteVert norm_verts[] =
90 { 0, 10, /**/ 255, 0, 0, 255 },
91 { 10, 10, /**/ 255, 0, 0, 255 },
92 { 5, 0, /**/ 255, 0, 0, 255 },
94 { 10, 10, /**/ 0, 255, 0, 255 },
95 { 20, 10, /**/ 0, 255, 0, 255 },
96 { 15, 0, /**/ 0, 255, 0, 255 }
99 static const ByteVert unnorm_verts[] =
101 { 0, 0, /**/ 0, 0, 1, 1 },
102 { 0, 0, /**/ 0, 0, 1, 1 },
103 { 0, 0, /**/ 0, 0, 1, 1 },
106 buffer = cogl_attribute_buffer_new (ctx,
107 sizeof (norm_verts), norm_verts);
108 attributes[0] = cogl_attribute_new (buffer,
111 G_STRUCT_OFFSET (ByteVert, x),
112 2, /* n_components */
113 COGL_ATTRIBUTE_TYPE_SHORT);
114 attributes[1] = cogl_attribute_new (buffer,
117 G_STRUCT_OFFSET (ByteVert, r),
118 4, /* n_components */
119 COGL_ATTRIBUTE_TYPE_UNSIGNED_BYTE);
120 cogl_attribute_set_normalized (attributes[1], TRUE);
122 cogl_framebuffer_push_matrix (fb);
123 cogl_framebuffer_translate (fb, offset_x, offset_y, 0.0f);
125 cogl_framebuffer_draw_attributes (fb,
127 COGL_VERTICES_MODE_TRIANGLES,
128 0, /* first_vertex */
131 2 /* n_attributes */);
133 cogl_object_unref (attributes[1]);
135 /* Test again with unnormalized attributes */
136 unnorm_buffer = cogl_attribute_buffer_new (ctx,
137 sizeof (unnorm_verts),
139 attributes[1] = cogl_attribute_new (unnorm_buffer,
142 G_STRUCT_OFFSET (ByteVert, r),
143 4, /* n_components */
144 COGL_ATTRIBUTE_TYPE_BYTE);
146 cogl_framebuffer_translate (fb, 20, 0, 0);
148 cogl_framebuffer_draw_attributes (fb,
150 COGL_VERTICES_MODE_TRIANGLES,
151 0, /* first_vertex */
154 2 /* n_attributes */);
156 cogl_framebuffer_pop_matrix (fb);
158 cogl_object_unref (attributes[0]);
159 cogl_object_unref (attributes[1]);
160 cogl_object_unref (buffer);
161 cogl_object_unref (unnorm_buffer);
163 test_utils_check_pixel (fb, offset_x + 5, offset_y + 5, 0xff0000ff);
164 test_utils_check_pixel (fb, offset_x + 15, offset_y + 5, 0x00ff00ff);
165 test_utils_check_pixel (fb, offset_x + 25, offset_y + 5, 0x0000ffff);
169 test_short_verts (TestState *state, int offset_x, int offset_y)
171 CoglAttribute *attributes[1];
172 CoglAttributeBuffer *buffer;
173 CoglPipeline *pipeline, *pipeline2;
174 CoglSnippet *snippet;
176 static const ShortVert short_verts[] =
183 pipeline = cogl_pipeline_new (ctx);
184 snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_VERTEX_TRANSFORM,
185 "attribute vec2 pos;",
187 cogl_snippet_set_replace (snippet,
188 "cogl_position_out = "
189 "cogl_modelview_projection_matrix * "
190 "vec4 (pos, 0.0, 1.0);");
191 cogl_pipeline_add_snippet (pipeline, snippet);
192 cogl_object_unref (snippet);
194 cogl_pipeline_set_color4ub (pipeline, 255, 0, 0, 255);
196 buffer = cogl_attribute_buffer_new (ctx,
197 sizeof (short_verts), short_verts);
198 attributes[0] = cogl_attribute_new (buffer,
201 G_STRUCT_OFFSET (ShortVert, x),
202 2, /* n_components */
203 COGL_ATTRIBUTE_TYPE_SHORT);
205 cogl_framebuffer_push_matrix (fb);
206 cogl_framebuffer_translate (fb,
211 cogl_framebuffer_draw_attributes (fb,
213 COGL_VERTICES_MODE_TRIANGLES,
214 0, /* first_vertex */
217 1 /* n_attributes */);
219 cogl_framebuffer_pop_matrix (fb);
221 cogl_object_unref (attributes[0]);
223 /* Test again treating the attribute as unsigned */
224 attributes[0] = cogl_attribute_new (buffer,
227 G_STRUCT_OFFSET (ShortVert, x),
228 2, /* n_components */
229 COGL_ATTRIBUTE_TYPE_UNSIGNED_SHORT);
231 pipeline2 = cogl_pipeline_copy (pipeline);
232 cogl_pipeline_set_color4ub (pipeline2, 0, 255, 0, 255);
234 cogl_framebuffer_push_matrix (fb);
235 cogl_framebuffer_translate (fb,
236 offset_x + 10.0f - 65525.0f,
240 cogl_framebuffer_draw_attributes (fb,
242 COGL_VERTICES_MODE_TRIANGLES,
243 0, /* first_vertex */
246 1 /* n_attributes */);
248 cogl_framebuffer_pop_matrix (fb);
250 cogl_object_unref (attributes[0]);
252 cogl_object_unref (pipeline2);
253 cogl_object_unref (pipeline);
254 cogl_object_unref (buffer);
256 test_utils_check_pixel (fb, offset_x + 5, offset_y + 5, 0xff0000ff);
257 test_utils_check_pixel (fb, offset_x + 15, offset_y + 5, 0x00ff00ff);
261 paint (TestState *state)
263 cogl_framebuffer_clear4f (fb, COGL_BUFFER_BIT_COLOR, 0, 0, 0, 1);
265 test_float_verts (state, 0, 0);
266 test_byte_verts (state, 0, 10);
267 test_short_verts (state, 0, 20);
271 test_custom_attributes (void)
273 /* If shaders aren't supported then we can't run the test */
274 if (cogl_features_available (COGL_FEATURE_SHADERS_GLSL))
276 CoglSnippet *snippet;
279 cogl_framebuffer_orthographic (fb,
281 cogl_framebuffer_get_width (fb),
282 cogl_framebuffer_get_height (fb),
286 state.pipeline = cogl_pipeline_new (ctx);
287 snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_VERTEX,
288 "attribute vec4 color;",
289 "cogl_color_out = color;");
290 cogl_pipeline_add_snippet (state.pipeline, snippet);
294 cogl_object_unref (state.pipeline);
295 cogl_object_unref (snippet);
297 if (cogl_test_verbose ())
300 else if (cogl_test_verbose ())
301 g_print ("Skipping\n");