1 #include <cogl/cogl.h>
2 
3 #include <string.h>
4 
5 #include "test-utils.h"
6 
7 static uint8_t
8 tex_data[2 * 2 * 4] =
9   {
10     0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
11     0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff
12   };
13 
14 /* Vertex data for a quad with all of the texture coordinates set to
15  * the top left (red) pixel */
16 static CoglVertexP2T2
17 vertex_data[4] =
18   {
19     { -1, -1, 0, 0 },
20     { 1, -1, 0, 0 },
21     { -1, 1, 0, 0 },
22     { 1, 1, 0, 0 }
23   };
24 
25 void
test_map_buffer_range(void)26 test_map_buffer_range (void)
27 {
28   CoglTexture2D *tex;
29   CoglPipeline *pipeline;
30   int fb_width, fb_height;
31   CoglAttributeBuffer *buffer;
32   CoglVertexP2T2 *data;
33   CoglAttribute *pos_attribute;
34   CoglAttribute *tex_coord_attribute;
35   CoglPrimitive *primitive;
36 
37   tex = cogl_texture_2d_new_from_data (test_ctx,
38                                        2, 2, /* width/height */
39                                        COGL_PIXEL_FORMAT_RGBA_8888_PRE,
40                                        2 * 4, /* rowstride */
41                                        tex_data,
42                                        NULL /* error */);
43 
44   pipeline = cogl_pipeline_new (test_ctx);
45 
46   cogl_pipeline_set_layer_texture (pipeline, 0, tex);
47   cogl_pipeline_set_layer_filters (pipeline,
48                                    0, /* layer */
49                                    COGL_PIPELINE_FILTER_NEAREST,
50                                    COGL_PIPELINE_FILTER_NEAREST);
51   cogl_pipeline_set_layer_wrap_mode (pipeline,
52                                      0, /* layer */
53                                      COGL_PIPELINE_WRAP_MODE_CLAMP_TO_EDGE);
54 
55   fb_width = cogl_framebuffer_get_width (test_fb);
56   fb_height = cogl_framebuffer_get_height (test_fb);
57 
58   buffer = cogl_attribute_buffer_new (test_ctx,
59                                       sizeof (vertex_data),
60                                       vertex_data);
61 
62   /* Replace the texture coordinates of the third vertex with the
63    * coordinates for a green texel */
64   data = cogl_buffer_map_range (buffer,
65                                 sizeof (vertex_data[0]) * 2,
66                                 sizeof (vertex_data[0]),
67                                 COGL_BUFFER_ACCESS_WRITE,
68                                 COGL_BUFFER_MAP_HINT_DISCARD_RANGE,
69                                 NULL); /* don't catch errors */
70   g_assert (data != NULL);
71 
72   data->x = vertex_data[2].x;
73   data->y = vertex_data[2].y;
74   data->s = 1.0f;
75   data->t = 0.0f;
76 
77   cogl_buffer_unmap (buffer);
78 
79   pos_attribute =
80     cogl_attribute_new (buffer,
81                         "cogl_position_in",
82                         sizeof (vertex_data[0]),
83                         offsetof (CoglVertexP2T2, x),
84                         2, /* n_components */
85                         COGL_ATTRIBUTE_TYPE_FLOAT);
86   tex_coord_attribute =
87     cogl_attribute_new (buffer,
88                         "cogl_tex_coord_in",
89                         sizeof (vertex_data[0]),
90                         offsetof (CoglVertexP2T2, s),
91                         2, /* n_components */
92                         COGL_ATTRIBUTE_TYPE_FLOAT);
93 
94   cogl_framebuffer_clear4f (test_fb,
95                             COGL_BUFFER_BIT_COLOR,
96                             0, 0, 0, 1);
97 
98   primitive =
99     cogl_primitive_new (COGL_VERTICES_MODE_TRIANGLE_STRIP,
100                         4, /* n_vertices */
101                         pos_attribute,
102                         tex_coord_attribute,
103                         NULL);
104   cogl_primitive_draw (primitive, test_fb, pipeline);
105   cogl_object_unref (primitive);
106 
107   /* Top left pixel should be the one that is replaced to be green */
108   test_utils_check_pixel (test_fb, 1, 1, 0x00ff00ff);
109   /* The other three corners should be left as red */
110   test_utils_check_pixel (test_fb, fb_width - 2, 1, 0xff0000ff);
111   test_utils_check_pixel (test_fb, 1, fb_height - 2, 0xff0000ff);
112   test_utils_check_pixel (test_fb, fb_width - 2, fb_height - 2, 0xff0000ff);
113 
114   cogl_object_unref (buffer);
115   cogl_object_unref (pos_attribute);
116   cogl_object_unref (tex_coord_attribute);
117 
118   cogl_object_unref (pipeline);
119   cogl_object_unref (tex);
120 
121   if (cogl_test_verbose ())
122     g_print ("OK\n");
123 }
124