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
uint8_t *data = g_malloc (TEX_IMAGE_STRIDE * TEX_DEPTH);
27
CoglError *error = NULL;
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,
62
g_assert (error != NULL);
63
g_warning ("Failed to create 3D texture: %s", error->message);
64
g_assert_not_reached ();
73
draw_frame (TestState *state)
75
CoglTexture *tex = create_texture_3d (test_ctx);
76
CoglPipeline *pipeline = cogl_pipeline_new (test_ctx);
77
typedef struct { float x, y, s, t, r; } Vert;
78
CoglPrimitive *primitive;
79
CoglAttributeBuffer *attribute_buffer;
80
CoglAttribute *attributes[2];
84
cogl_pipeline_set_layer_texture (pipeline, 0, tex);
85
cogl_object_unref (tex);
86
cogl_pipeline_set_layer_filters (pipeline, 0,
87
COGL_PIPELINE_FILTER_NEAREST,
88
COGL_PIPELINE_FILTER_NEAREST);
90
/* Render the texture repeated horizontally twice using a regular
91
cogl rectangle. This should end up with the r texture coordinates
93
cogl_framebuffer_draw_textured_rectangle (test_fb, pipeline,
94
0.0f, 0.0f, TEX_WIDTH * 2, TEX_HEIGHT,
95
0.0f, 0.0f, 2.0f, 1.0f);
97
/* Render all of the images in the texture using coordinates from a
99
v = verts = g_new (Vert, 4 * TEX_DEPTH);
100
for (i = 0; i < TEX_DEPTH; i++)
102
float r = (i + 0.5f) / TEX_DEPTH;
104
v->x = i * TEX_WIDTH;
111
v->x = i * TEX_WIDTH;
112
v->y = TEX_HEIGHT * 2;
118
v->x = i * TEX_WIDTH + TEX_WIDTH;
119
v->y = TEX_HEIGHT * 2;
125
v->x = i * TEX_WIDTH + TEX_WIDTH;
133
attribute_buffer = cogl_attribute_buffer_new (test_ctx,
134
4 * TEX_DEPTH * sizeof (Vert),
136
attributes[0] = cogl_attribute_new (attribute_buffer,
139
G_STRUCT_OFFSET (Vert, x),
140
2, /* n_components */
141
COGL_ATTRIBUTE_TYPE_FLOAT);
142
attributes[1] = cogl_attribute_new (attribute_buffer,
145
G_STRUCT_OFFSET (Vert, s),
146
3, /* n_components */
147
COGL_ATTRIBUTE_TYPE_FLOAT);
148
primitive = cogl_primitive_new_with_attributes (COGL_VERTICES_MODE_TRIANGLES,
151
2 /* n_attributes */);
153
cogl_primitive_set_indices (primitive,
154
cogl_get_rectangle_indices (test_ctx,
158
cogl_primitive_draw (primitive, test_fb, pipeline);
162
cogl_object_unref (primitive);
163
cogl_object_unref (attributes[0]);
164
cogl_object_unref (attributes[1]);
165
cogl_object_unref (attribute_buffer);
166
cogl_object_unref (pipeline);
170
validate_block (int block_x, int block_y, int z)
174
for (y = 0; y < TEX_HEIGHT; y++)
175
for (x = 0; x < TEX_WIDTH; x++)
176
test_utils_check_pixel_rgb (test_fb,
177
block_x * TEX_WIDTH + x,
178
block_y * TEX_HEIGHT + y,
185
validate_result (void)
189
validate_block (0, 0, 0);
191
for (i = 0; i < TEX_DEPTH; i++)
192
validate_block (i, 1, i);
196
test_multi_texture (TestState *state)
198
CoglPipeline *pipeline;
199
CoglTexture3D *tex_3d;
200
CoglTexture2D *tex_2d;
203
cogl_framebuffer_clear4f (test_fb, COGL_BUFFER_BIT_COLOR, 0, 0, 0, 1);
205
/* Tests a pipeline that is using multi-texturing to combine a 3D
206
texture with a 2D texture. The texture from another layer is
207
sampled with TEXTURE_? just to pick up a specific bug that was
208
happening with the ARBfp fragend */
210
pipeline = cogl_pipeline_new (test_ctx);
216
tex_2d = cogl_texture_2d_new_from_data (test_ctx,
217
1, 1, /* width/height */
218
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
222
cogl_pipeline_set_layer_texture (pipeline, 0, tex_2d);
228
tex_3d = cogl_texture_3d_new_from_data (test_ctx,
229
1, 1, 1, /* width/height/depth */
230
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
232
4, /* image_stride */
235
cogl_pipeline_set_layer_texture (pipeline, 1, tex_3d);
237
cogl_pipeline_set_layer_combine (pipeline, 0,
238
"RGBA = REPLACE(PREVIOUS)",
240
cogl_pipeline_set_layer_combine (pipeline, 1,
241
"RGBA = ADD(TEXTURE_0, TEXTURE_1)",
244
cogl_framebuffer_draw_rectangle (test_fb, pipeline, 0, 0, 10, 10);
246
test_utils_check_pixel (test_fb, 5, 5, 0xffff00ff);
248
cogl_object_unref (tex_2d);
249
cogl_object_unref (tex_3d);
250
cogl_object_unref (pipeline);
254
test_texture_3d (void)
258
state.fb_width = cogl_framebuffer_get_width (test_fb);
259
state.fb_height = cogl_framebuffer_get_height (test_fb);
261
cogl_framebuffer_orthographic (test_fb,
263
state.fb_width, /* x_2 */
264
state.fb_height /* y_2 */,
265
-1, 100 /* near/far */);
270
test_multi_texture (&state);
272
if (cogl_test_verbose ())