test-texture-3d: Port to Cogl

This ports the texture-3d test to Cogl instead of depending on
Clutter.

Reviewed-by: Robert Bragg <robert@linux.intel.com>
This commit is contained in:
Neil Roberts 2012-02-09 17:54:07 +00:00
parent fdc07c865f
commit 7cd2f25eb0
3 changed files with 79 additions and 78 deletions

View File

@ -20,7 +20,6 @@ unported_test_sources = \
test-pixel-buffer.c \ test-pixel-buffer.c \
test-premult.c \ test-premult.c \
test-readpixels.c \ test-readpixels.c \
test-texture-3d.c \
test-texture-get-set-data.c \ test-texture-get-set-data.c \
test-texture-mipmaps.c \ test-texture-mipmaps.c \
test-texture-pixmap-x11.c \ test-texture-pixmap-x11.c \
@ -47,6 +46,7 @@ test_sources = \
test-custom-attributes.c \ test-custom-attributes.c \
test-offscreen.c \ test-offscreen.c \
test-primitive.c \ test-primitive.c \
test-texture-3d.c \
$(NULL) $(NULL)
test_conformance_SOURCES = $(common_sources) $(test_sources) test_conformance_SOURCES = $(common_sources) $(test_sources)

View File

@ -148,7 +148,7 @@ main (int argc, char **argv)
ADD_TEST ("/cogl/texture", test_cogl_sub_texture); ADD_TEST ("/cogl/texture", test_cogl_sub_texture);
UNPORTED_TEST ("/cogl/texture", test_cogl_pixel_array); UNPORTED_TEST ("/cogl/texture", test_cogl_pixel_array);
UNPORTED_TEST ("/cogl/texture", test_cogl_texture_rectangle); UNPORTED_TEST ("/cogl/texture", test_cogl_texture_rectangle);
UNPORTED_TEST ("/cogl/texture", test_cogl_texture_3d); ADD_TEST ("/cogl/texture", test_cogl_texture_3d);
ADD_TEST ("/cogl/texture", test_cogl_wrap_modes); ADD_TEST ("/cogl/texture", test_cogl_wrap_modes);
UNPORTED_TEST ("/cogl/texture", test_cogl_texture_pixmap_x11); UNPORTED_TEST ("/cogl/texture", test_cogl_texture_pixmap_x11);
UNPORTED_TEST ("/cogl/texture", test_cogl_texture_get_set_data); UNPORTED_TEST ("/cogl/texture", test_cogl_texture_get_set_data);

View File

@ -1,9 +1,7 @@
#include <clutter/clutter.h> #include <cogl/cogl2-experimental.h>
#include <string.h> #include <string.h>
#include "test-conform-common.h" #include "test-utils.h"
static const ClutterColor stage_color = { 0x0, 0xff, 0x0, 0xff };
#define TEX_WIDTH 4 #define TEX_WIDTH 4
#define TEX_HEIGHT 8 #define TEX_HEIGHT 8
@ -13,7 +11,15 @@ static const ClutterColor stage_color = { 0x0, 0xff, 0x0, 0xff };
/* Leave four rows of padding between each image */ /* Leave four rows of padding between each image */
#define TEX_IMAGE_STRIDE ((TEX_HEIGHT + 4) * TEX_ROWSTRIDE) #define TEX_IMAGE_STRIDE ((TEX_HEIGHT + 4) * TEX_ROWSTRIDE)
static CoglHandle typedef struct _TestState
{
CoglContext *context;
int fb_width;
int fb_height;
CoglFramebuffer *fb;
} TestState;
static CoglTexture *
create_texture_3d (void) create_texture_3d (void)
{ {
int x, y, z; int x, y, z;
@ -67,22 +73,23 @@ create_texture_3d (void)
} }
static void static void
draw_frame (void) draw_frame (TestState *state)
{ {
CoglHandle tex = create_texture_3d (); CoglTexture *tex = create_texture_3d ();
CoglHandle material = cogl_material_new (); CoglPipeline *pipeline = cogl_pipeline_new ();
typedef struct { float x, y, s, t, r; } Vert; typedef struct { float x, y, s, t, r; } Vert;
CoglHandle vbo, indices; CoglPrimitive *primitive;
CoglAttributeBuffer *attribute_buffer;
CoglAttribute *attributes[2];
Vert *verts, *v; Vert *verts, *v;
int i; int i;
cogl_material_set_layer (material, 0, tex); cogl_pipeline_set_layer_texture (pipeline, 0, tex);
cogl_handle_unref (tex); cogl_object_unref (tex);
cogl_material_set_layer_filters (material, 0, cogl_pipeline_set_layer_filters (pipeline, 0,
COGL_MATERIAL_FILTER_NEAREST, COGL_PIPELINE_FILTER_NEAREST,
COGL_MATERIAL_FILTER_NEAREST); COGL_PIPELINE_FILTER_NEAREST);
cogl_set_source (material); cogl_push_source (pipeline);
cogl_handle_unref (material);
/* Render the texture repeated horizontally twice using a regular /* Render the texture repeated horizontally twice using a regular
cogl rectangle. This should end up with the r texture coordinates cogl rectangle. This should end up with the r texture coordinates
@ -90,7 +97,10 @@ draw_frame (void)
cogl_rectangle_with_texture_coords (0.0f, 0.0f, TEX_WIDTH * 2, TEX_HEIGHT, cogl_rectangle_with_texture_coords (0.0f, 0.0f, TEX_WIDTH * 2, TEX_HEIGHT,
0.0f, 0.0f, 2.0f, 1.0f); 0.0f, 0.0f, 2.0f, 1.0f);
/* Render all of the images in the texture using coordinates from a VBO */ cogl_pop_source ();
/* Render all of the images in the texture using coordinates from a
CoglPrimitive */
v = verts = g_new (Vert, 4 * TEX_DEPTH); v = verts = g_new (Vert, 4 * TEX_DEPTH);
for (i = 0; i < TEX_DEPTH; i++) for (i = 0; i < TEX_DEPTH; i++)
{ {
@ -125,54 +135,54 @@ draw_frame (void)
v++; v++;
} }
vbo = cogl_vertex_buffer_new (4 * TEX_DEPTH); attribute_buffer = cogl_attribute_buffer_new (state->context,
cogl_vertex_buffer_add (vbo, "gl_Vertex", 4 * TEX_DEPTH * sizeof (Vert),
2, COGL_ATTRIBUTE_TYPE_FLOAT, FALSE, verts);
sizeof (Vert), attributes[0] = cogl_attribute_new (attribute_buffer,
&verts->x); "cogl_position_in",
cogl_vertex_buffer_add (vbo, "gl_MultiTexCoord0", sizeof (Vert),
3, COGL_ATTRIBUTE_TYPE_FLOAT, FALSE, G_STRUCT_OFFSET (Vert, x),
sizeof (Vert), 2, /* n_components */
&verts->s); COGL_ATTRIBUTE_TYPE_FLOAT);
cogl_vertex_buffer_submit (vbo); attributes[1] = cogl_attribute_new (attribute_buffer,
"cogl_tex_coord_in",
sizeof (Vert),
G_STRUCT_OFFSET (Vert, s),
3, /* n_components */
COGL_ATTRIBUTE_TYPE_FLOAT);
primitive = cogl_primitive_new_with_attributes (COGL_VERTICES_MODE_TRIANGLES,
6 * TEX_DEPTH,
attributes,
2 /* n_attributes */);
cogl_primitive_set_indices (primitive,
cogl_get_rectangle_indices (state->context,
TEX_DEPTH),
6 * TEX_DEPTH);
cogl_framebuffer_draw_primitive (state->fb, pipeline, primitive);
g_free (verts); g_free (verts);
indices = cogl_vertex_buffer_indices_get_for_quads (6 * TEX_DEPTH); cogl_object_unref (primitive);
cogl_object_unref (attributes[0]);
cogl_vertex_buffer_draw_elements (vbo, cogl_object_unref (attributes[1]);
COGL_VERTICES_MODE_TRIANGLES, cogl_object_unref (attribute_buffer);
indices, cogl_object_unref (pipeline);
0, TEX_DEPTH * 4 - 1,
0, TEX_DEPTH * 6);
cogl_handle_unref (vbo);
} }
static void static void
validate_block (int block_x, int block_y, int z) validate_block (int block_x, int block_y, int z)
{ {
guint8 *data, *p;
int x, y; int x, y;
p = data = g_malloc (TEX_WIDTH * TEX_HEIGHT * 4);
cogl_read_pixels (block_x * TEX_WIDTH, block_y * TEX_HEIGHT,
TEX_WIDTH, TEX_HEIGHT,
COGL_READ_PIXELS_COLOR_BUFFER,
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
data);
for (y = 0; y < TEX_HEIGHT; y++) for (y = 0; y < TEX_HEIGHT; y++)
for (x = 0; x < TEX_WIDTH; x++) for (x = 0; x < TEX_WIDTH; x++)
{ test_utils_check_pixel_rgb (block_x * TEX_WIDTH + x,
g_assert_cmpint (p[0], ==, 255 - x * 8); block_y * TEX_HEIGHT + y,
g_assert_cmpint (p[1], ==, y * 8); 255 - x * 8,
g_assert_cmpint (p[2], ==, 255 - z * 8); y * 8,
p += 4; 255 - z * 8);
}
g_free (data);
} }
static void static void
@ -186,40 +196,31 @@ validate_result (void)
validate_block (i, 1, i); validate_block (i, 1, i);
} }
static void
on_paint (void)
{
draw_frame ();
validate_result ();
/* Comment this out to see what the test paints */
clutter_main_quit ();
}
void void
test_cogl_texture_3d (TestUtilsGTestFixture *fixture, test_cogl_texture_3d (TestUtilsGTestFixture *fixture,
void *data) void *data)
{ {
ClutterActor *stage; TestUtilsSharedState *shared_state = data;
unsigned int paint_handler;
stage = clutter_stage_get_default ();
/* Check whether GL supports the rectangle extension. If not we'll /* Check whether GL supports the rectangle extension. If not we'll
just assume the test passes */ just assume the test passes */
if (cogl_features_available (COGL_FEATURE_TEXTURE_3D)) if (cogl_has_feature (shared_state->ctx, COGL_FEATURE_ID_TEXTURE_3D))
{ {
clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color); TestState state;
paint_handler = g_signal_connect_after (stage, "paint", state.context = shared_state->ctx;
G_CALLBACK (on_paint), NULL); state.fb_width = cogl_framebuffer_get_width (shared_state->fb);
state.fb_height = cogl_framebuffer_get_height (shared_state->fb);
state.fb = shared_state->fb;
clutter_actor_show (stage); cogl_framebuffer_orthographic (shared_state->fb,
0, 0, /* x_1, y_1 */
state.fb_width, /* x_2 */
state.fb_height /* y_2 */,
-1, 100 /* near/far */);
clutter_main (); draw_frame (&state);
validate_result ();
g_signal_handler_disconnect (stage, paint_handler);
if (g_test_verbose ()) if (g_test_verbose ())
g_print ("OK\n"); g_print ("OK\n");