1 #include <cogl/cogl2-experimental.h>
4 #include "test-utils.h"
9 /* Leave four bytes of padding between each row */
10 #define TEX_ROWSTRIDE (TEX_WIDTH * 4 + 4)
11 /* Leave four rows of padding between each image */
12 #define TEX_IMAGE_STRIDE ((TEX_HEIGHT + 4) * TEX_ROWSTRIDE)
14 typedef struct _TestState
20 static CoglTexture3D *
21 create_texture_3d (CoglContext *context)
24 guint8 *data = g_malloc (TEX_IMAGE_STRIDE * TEX_DEPTH);
29 for (z = 0; z < TEX_DEPTH; z++)
31 for (y = 0; y < TEX_HEIGHT; y++)
33 for (x = 0; x < TEX_WIDTH; x++)
35 /* Set red, green, blue to values based on x, y, z */
43 /* Set the padding between rows to 0xde */
44 memset (p, 0xde, TEX_ROWSTRIDE - (TEX_WIDTH * 4));
45 p += TEX_ROWSTRIDE - (TEX_WIDTH * 4);
47 /* Set the padding between images to 0xad */
48 memset (p, 0xba, TEX_IMAGE_STRIDE - (TEX_HEIGHT * TEX_ROWSTRIDE));
49 p += TEX_IMAGE_STRIDE - (TEX_HEIGHT * TEX_ROWSTRIDE);
52 tex = cogl_texture_3d_new_from_data (context,
53 TEX_WIDTH, TEX_HEIGHT, TEX_DEPTH,
54 COGL_PIXEL_FORMAT_RGBA_8888,
55 COGL_PIXEL_FORMAT_ANY,
63 g_assert (error != NULL);
64 g_warning ("Failed to create 3D texture: %s", error->message);
65 g_assert_not_reached ();
74 draw_frame (TestState *state)
76 CoglTexture *tex = COGL_TEXTURE (create_texture_3d (ctx));
77 CoglPipeline *pipeline = cogl_pipeline_new (ctx);
78 typedef struct { float x, y, s, t, r; } Vert;
79 CoglPrimitive *primitive;
80 CoglAttributeBuffer *attribute_buffer;
81 CoglAttribute *attributes[2];
85 cogl_pipeline_set_layer_texture (pipeline, 0, tex);
86 cogl_object_unref (tex);
87 cogl_pipeline_set_layer_filters (pipeline, 0,
88 COGL_PIPELINE_FILTER_NEAREST,
89 COGL_PIPELINE_FILTER_NEAREST);
91 /* Render the texture repeated horizontally twice using a regular
92 cogl rectangle. This should end up with the r texture coordinates
94 cogl_framebuffer_draw_textured_rectangle (fb, pipeline,
95 0.0f, 0.0f, TEX_WIDTH * 2, TEX_HEIGHT,
96 0.0f, 0.0f, 2.0f, 1.0f);
98 /* Render all of the images in the texture using coordinates from a
100 v = verts = g_new (Vert, 4 * TEX_DEPTH);
101 for (i = 0; i < TEX_DEPTH; i++)
103 float r = (i + 0.5f) / TEX_DEPTH;
105 v->x = i * TEX_WIDTH;
112 v->x = i * TEX_WIDTH;
113 v->y = TEX_HEIGHT * 2;
119 v->x = i * TEX_WIDTH + TEX_WIDTH;
120 v->y = TEX_HEIGHT * 2;
126 v->x = i * TEX_WIDTH + TEX_WIDTH;
134 attribute_buffer = cogl_attribute_buffer_new (ctx,
135 4 * TEX_DEPTH * sizeof (Vert),
137 attributes[0] = cogl_attribute_new (attribute_buffer,
140 G_STRUCT_OFFSET (Vert, x),
141 2, /* n_components */
142 COGL_ATTRIBUTE_TYPE_FLOAT);
143 attributes[1] = cogl_attribute_new (attribute_buffer,
146 G_STRUCT_OFFSET (Vert, s),
147 3, /* n_components */
148 COGL_ATTRIBUTE_TYPE_FLOAT);
149 primitive = cogl_primitive_new_with_attributes (COGL_VERTICES_MODE_TRIANGLES,
152 2 /* n_attributes */);
154 cogl_primitive_set_indices (primitive,
155 cogl_get_rectangle_indices (ctx,
159 cogl_framebuffer_draw_primitive (fb, pipeline, primitive);
163 cogl_object_unref (primitive);
164 cogl_object_unref (attributes[0]);
165 cogl_object_unref (attributes[1]);
166 cogl_object_unref (attribute_buffer);
167 cogl_object_unref (pipeline);
171 validate_block (int block_x, int block_y, int z)
175 for (y = 0; y < TEX_HEIGHT; y++)
176 for (x = 0; x < TEX_WIDTH; x++)
177 test_utils_check_pixel_rgb (fb,
178 block_x * TEX_WIDTH + x,
179 block_y * TEX_HEIGHT + y,
186 validate_result (void)
190 validate_block (0, 0, 0);
192 for (i = 0; i < TEX_DEPTH; i++)
193 validate_block (i, 1, i);
197 test_multi_texture (TestState *state)
199 CoglPipeline *pipeline;
200 CoglTexture3D *tex_3d;
201 CoglTexture2D *tex_2d;
204 cogl_framebuffer_clear4f (fb, COGL_BUFFER_BIT_COLOR, 0, 0, 0, 1);
206 /* Tests a pipeline that is using multi-texturing to combine a 3D
207 texture with a 2D texture. The texture from another layer is
208 sampled with TEXTURE_? just to pick up a specific bug that was
209 happening with the ARBfp fragend */
211 pipeline = cogl_pipeline_new (ctx);
217 tex_2d = cogl_texture_2d_new_from_data (ctx,
218 1, 1, /* width/height */
219 COGL_PIXEL_FORMAT_RGBA_8888_PRE,
220 COGL_PIXEL_FORMAT_RGBA_8888_PRE,
224 cogl_pipeline_set_layer_texture (pipeline, 0, COGL_TEXTURE (tex_2d));
230 tex_3d = cogl_texture_3d_new_from_data (ctx,
231 1, 1, 1, /* width/height/depth */
232 COGL_PIXEL_FORMAT_RGBA_8888_PRE,
233 COGL_PIXEL_FORMAT_RGBA_8888_PRE,
235 4, /* image_stride */
238 cogl_pipeline_set_layer_texture (pipeline, 1, COGL_TEXTURE (tex_3d));
240 cogl_pipeline_set_layer_combine (pipeline, 0,
241 "RGBA = REPLACE(PREVIOUS)",
243 cogl_pipeline_set_layer_combine (pipeline, 1,
244 "RGBA = ADD(TEXTURE_0, TEXTURE_1)",
247 cogl_framebuffer_draw_rectangle (fb, pipeline, 0, 0, 10, 10);
249 test_utils_check_pixel (fb, 5, 5, 0xffff00ff);
251 cogl_object_unref (tex_2d);
252 cogl_object_unref (tex_3d);
253 cogl_object_unref (pipeline);
257 test_texture_3d (void)
259 /* Check whether GL supports the rectangle extension. If not we'll
260 just assume the test passes */
261 if (cogl_has_feature (ctx, COGL_FEATURE_ID_TEXTURE_3D))
265 state.fb_width = cogl_framebuffer_get_width (fb);
266 state.fb_height = cogl_framebuffer_get_height (fb);
268 cogl_framebuffer_orthographic (fb,
270 state.fb_width, /* x_2 */
271 state.fb_height /* y_2 */,
272 -1, 100 /* near/far */);
277 test_multi_texture (&state);
279 if (cogl_test_verbose ())
282 else if (cogl_test_verbose ())
283 g_print ("Skipping\n");