summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorNeil Roberts <neil@linux.intel.com>2012-11-08 16:56:02 +0000
committerNeil Roberts <neil@linux.intel.com>2013-06-07 16:53:29 +0100
commit6d51a18e7cec5cbbb833e4f014c9596a418585a6 (patch)
tree900dd1959da04b88800530aa60368a7167cd72ee /tests
parent2d24248a3c3365987a08d3b15a2fbacbf1b8bc70 (diff)
downloadcogl-6d51a18e7cec5cbbb833e4f014c9596a418585a6.tar.gz
Add support for per-vertex point sizes
This adds a new function to enable per-vertex point size on a pipeline. This can be set with cogl_pipeline_set_per_vertex_point_size(). Once enabled the point size can be set either by drawing with an attribute named 'cogl_point_size_in' or by writing to the 'cogl_point_size_out' builtin from a snippet. There is a feature flag which must be checked for before using per-vertex point sizes. This will only be set on GL >= 2.0 or on GLES 2.0. GL will only let you set a per-vertex point size from GLSL by writing to gl_PointSize. This is only available in GL2 and not in the older GLSL extensions. The per-vertex point size has its own pipeline state flag so that it can be part of the state that affects vertex shader generation. Having to enable the per vertex point size with a separate function is a bit awkward. Ideally it would work like the color attribute where you can just set it for every vertex in your primitive with cogl_pipeline_set_color or set it per-vertex by just using the attribute. This is harder to get working with the point size because we need to generate a different vertex shader depending on what attributes are bound. I think if we wanted to make this work transparently we would still want to internally have a pipeline property describing whether the shader was generated with per-vertex support so that it would work with the shader cache correctly. Potentially we could make the per-vertex property internal and automatically make a weak pipeline whenever the attribute is bound. However we would then also need to automatically detect when an application is writing to cogl_point_size_out from a snippet. Reviewed-by: Robert Bragg <robert@linux.intel.com> (cherry picked from commit 8495d9c1c15ce389885a9356d965eabd97758115) Conflicts: cogl/cogl-context.c cogl/cogl-pipeline-private.h cogl/cogl-pipeline.c cogl/cogl-private.h cogl/driver/gl/cogl-pipeline-progend-fixed.c cogl/driver/gl/gl/cogl-pipeline-progend-fixed-arbfp.c
Diffstat (limited to 'tests')
-rw-r--r--tests/conform/Makefile.am1
-rw-r--r--tests/conform/test-conform-main.c5
-rw-r--r--tests/conform/test-point-size-attribute.c166
3 files changed, 172 insertions, 0 deletions
diff --git a/tests/conform/Makefile.am b/tests/conform/Makefile.am
index 2f0291ad..4277aa17 100644
--- a/tests/conform/Makefile.am
+++ b/tests/conform/Makefile.am
@@ -48,6 +48,7 @@ test_sources = \
test-read-texture-formats.c \
test-write-texture-formats.c \
test-point-size.c \
+ test-point-size-attribute.c \
test-point-sprite.c \
test-no-gl-header.c \
test-version.c \
diff --git a/tests/conform/test-conform-main.c b/tests/conform/test-conform-main.c
index 2c776601..f8124ed3 100644
--- a/tests/conform/test-conform-main.c
+++ b/tests/conform/test-conform-main.c
@@ -102,6 +102,11 @@ main (int argc, char **argv)
0);
ADD_TEST (test_point_size, 0, 0);
+ ADD_TEST (test_point_size_attribute,
+ TEST_REQUIREMENT_PER_VERTEX_POINT_SIZE, 0);
+ ADD_TEST (test_point_size_attribute_snippet,
+ TEST_REQUIREMENT_PER_VERTEX_POINT_SIZE |
+ TEST_REQUIREMENT_GLSL, 0);
ADD_TEST (test_point_sprite,
TEST_REQUIREMENT_POINT_SPRITE,
0);
diff --git a/tests/conform/test-point-size-attribute.c b/tests/conform/test-point-size-attribute.c
new file mode 100644
index 00000000..81cae95b
--- /dev/null
+++ b/tests/conform/test-point-size-attribute.c
@@ -0,0 +1,166 @@
+#include <cogl/cogl.h>
+
+#include "test-utils.h"
+
+/* This test assumes the GL driver supports point sizes up to 16
+ pixels. Cogl should probably have some way of querying the size so
+ we start from that instead */
+#define MAX_POINT_SIZE 16
+#define MIN_POINT_SIZE 4
+#define N_POINTS (MAX_POINT_SIZE - MIN_POINT_SIZE + 1)
+/* The size of the area that we'll paint each point in */
+#define POINT_BOX_SIZE (MAX_POINT_SIZE * 2)
+
+typedef struct
+{
+ float x, y;
+ float point_size;
+} PointVertex;
+
+static int
+calc_coord_offset (int pos, int pos_index, int point_size)
+{
+ switch (pos_index)
+ {
+ case 0: return pos - point_size / 2 - 2;
+ case 1: return pos - point_size / 2 + 2;
+ case 2: return pos + point_size / 2 - 2;
+ case 3: return pos + point_size / 2 + 2;
+ }
+
+ g_assert_not_reached ();
+}
+
+static void
+verify_point_size (CoglFramebuffer *test_fb,
+ int x_pos,
+ int y_pos,
+ int point_size)
+{
+ int y, x;
+
+ for (y = 0; y < 4; y++)
+ for (x = 0; x < 4; x++)
+ {
+ CoglBool in_point = x >= 1 && x <= 2 && y >= 1 && y <= 2;
+ uint32_t expected_pixel = in_point ? 0x00ff00ff : 0xff0000ff;
+
+ test_utils_check_pixel (test_fb,
+ calc_coord_offset (x_pos, x, point_size),
+ calc_coord_offset (y_pos, y, point_size),
+ expected_pixel);
+ }
+}
+
+static CoglPrimitive *
+create_primitive (const char *attribute_name)
+{
+ PointVertex vertices[N_POINTS];
+ CoglAttributeBuffer *buffer;
+ CoglAttribute *attributes[2];
+ CoglPrimitive *prim;
+ int i;
+
+ for (i = 0; i < N_POINTS; i++)
+ {
+ vertices[i].x = i * POINT_BOX_SIZE + POINT_BOX_SIZE / 2;
+ vertices[i].y = POINT_BOX_SIZE / 2;
+ vertices[i].point_size = MAX_POINT_SIZE - i;
+ }
+
+ buffer = cogl_attribute_buffer_new (test_ctx,
+ sizeof (vertices),
+ vertices);
+
+ attributes[0] = cogl_attribute_new (buffer,
+ "cogl_position_in",
+ sizeof (PointVertex),
+ G_STRUCT_OFFSET (PointVertex, x),
+ 2, /* n_components */
+ COGL_ATTRIBUTE_TYPE_FLOAT);
+ attributes[1] = cogl_attribute_new (buffer,
+ attribute_name,
+ sizeof (PointVertex),
+ G_STRUCT_OFFSET (PointVertex, point_size),
+ 1, /* n_components */
+ COGL_ATTRIBUTE_TYPE_FLOAT);
+
+ prim = cogl_primitive_new_with_attributes (COGL_VERTICES_MODE_POINTS,
+ N_POINTS,
+ attributes,
+ 2 /* n_attributes */);
+
+ for (i = 0; i < 2; i++)
+ cogl_object_unref (attributes[i]);
+
+ return prim;
+}
+
+static void
+do_test (const char *attribute_name,
+ void (* pipeline_setup_func) (CoglPipeline *pipeline))
+{
+ int fb_width = cogl_framebuffer_get_width (test_fb);
+ int fb_height = cogl_framebuffer_get_height (test_fb);
+ CoglPrimitive *primitive;
+ CoglPipeline *pipeline;
+ int i;
+
+ cogl_framebuffer_orthographic (test_fb,
+ 0, 0, /* x_1, y_1 */
+ fb_width, /* x_2 */
+ fb_height /* y_2 */,
+ -1, 100 /* near/far */);
+
+ cogl_framebuffer_clear4f (test_fb,
+ COGL_BUFFER_BIT_COLOR,
+ 1.0f, 0.0f, 0.0f, 1.0f);
+
+ primitive = create_primitive (attribute_name);
+ pipeline = cogl_pipeline_new (test_ctx);
+ cogl_pipeline_set_color4ub (pipeline, 0x00, 0xff, 0x00, 0xff);
+ cogl_pipeline_set_per_vertex_point_size (pipeline, TRUE, NULL);
+ if (pipeline_setup_func)
+ pipeline_setup_func (pipeline);
+ cogl_framebuffer_draw_primitive (test_fb, pipeline, primitive);
+ cogl_object_unref (pipeline);
+ cogl_object_unref (primitive);
+
+ /* Verify all of the points where drawn at the right size */
+ for (i = 0; i < N_POINTS; i++)
+ verify_point_size (test_fb,
+ i * POINT_BOX_SIZE + POINT_BOX_SIZE / 2, /* x */
+ POINT_BOX_SIZE / 2, /* y */
+ MAX_POINT_SIZE - i /* point size */);
+
+ if (cogl_test_verbose ())
+ g_print ("OK\n");
+}
+
+void
+test_point_size_attribute (void)
+{
+ do_test ("cogl_point_size_in", NULL);
+}
+
+static void
+setup_snippet (CoglPipeline *pipeline)
+{
+ CoglSnippet *snippet;
+
+ snippet = cogl_snippet_new (COGL_SNIPPET_HOOK_POINT_SIZE,
+ "attribute float "
+ "my_super_duper_point_size_attrib;\n",
+ NULL);
+ cogl_snippet_set_replace (snippet,
+ "cogl_point_size_out = "
+ "my_super_duper_point_size_attrib;\n");
+ cogl_pipeline_add_snippet (pipeline, snippet);
+ cogl_object_unref (snippet);
+}
+
+void
+test_point_size_attribute_snippet (void)
+{
+ do_test ("my_super_duper_point_size_attrib", setup_snippet);
+}