summaryrefslogtreecommitdiff
path: root/tests/conform/test-npot-texture.c
blob: 85c16c96003600d618f1da1be93117d4937ef441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <cogl/cogl.h>

#include <string.h>

#include "test-utils.h"

/* Non-power-of-two sized texture that should cause slicing */
#define TEXTURE_SIZE        384
/* Number of times to split the texture up on each axis */
#define PARTS               2
/* The texture is split into four parts, each with a different colour */
#define PART_SIZE           (TEXTURE_SIZE / PARTS)

/* Amount of pixels to skip off the top, bottom, left and right of the
   texture when reading back the stage */
#define TEST_INSET          4

/* Size to actually render the texture at */
#define TEXTURE_RENDER_SIZE TEXTURE_SIZE
/* The size of a part once rendered */
#define PART_RENDER_SIZE    (TEXTURE_RENDER_SIZE / PARTS)

static const uint32_t corner_colors[PARTS * PARTS] =
  {
    /* Top left     - red */    0xff0000ff,
    /* Top right    - green */  0x00ff00ff,
    /* Bottom left  - blue */   0x0000ffff,
    /* Bottom right - yellow */ 0xffff00ff
  };

static void
validate_part (int xnum,
               int ynum,
               uint32_t color)
{
  test_utils_check_region (test_fb,
                           xnum * PART_RENDER_SIZE + TEST_INSET,
                           ynum * PART_RENDER_SIZE + TEST_INSET,
                           PART_RENDER_SIZE - TEST_INSET * 2,
                           PART_RENDER_SIZE - TEST_INSET * 2,
                           color);
}

static void
validate_result (void)
{
  /* Validate that all four corners of the texture are drawn in the
     right color */
  validate_part (0, 0, corner_colors[0]);
  validate_part (1, 0, corner_colors[1]);
  validate_part (0, 1, corner_colors[2]);
  validate_part (1, 1, corner_colors[3]);
}

static CoglTexture *
make_texture (void)
{
  void *tex_data;
  uint32_t *p;
  CoglTexture *tex;
  int partx, party, width, height;

  p = tex_data = g_malloc (TEXTURE_SIZE * TEXTURE_SIZE * 4);

  /* Make a texture with a different color for each part */
  for (party = 0; party < PARTS; party++)
    {
      height = (party < PARTS - 1
                ? PART_SIZE
                : TEXTURE_SIZE - PART_SIZE * (PARTS - 1));

      for (partx = 0; partx < PARTS; partx++)
        {
          uint32_t color = corner_colors[party * PARTS + partx];
          width = (partx < PARTS - 1
                   ? PART_SIZE
                   : TEXTURE_SIZE - PART_SIZE * (PARTS - 1));

          while (width-- > 0)
            *(p++) = GUINT32_TO_BE (color);
        }

      while (--height > 0)
        {
          memcpy (p, p - TEXTURE_SIZE, TEXTURE_SIZE * 4);
          p += TEXTURE_SIZE;
        }
    }

  tex = test_utils_texture_new_from_data (test_ctx,
                                          TEXTURE_SIZE,
                                          TEXTURE_SIZE,
                                          TEST_UTILS_TEXTURE_NO_ATLAS,
                                          COGL_PIXEL_FORMAT_RGBA_8888_PRE,
                                          TEXTURE_SIZE * 4,
                                          tex_data);

  g_free (tex_data);

  if (cogl_test_verbose ())
    {
      if (cogl_texture_is_sliced (tex))
        g_print ("Texture is sliced\n");
      else
        g_print ("Texture is not sliced\n");
    }

  /* The texture should be sliced unless NPOTs are supported */
  g_assert (cogl_has_feature (test_ctx, COGL_FEATURE_ID_TEXTURE_NPOT)
            ? !cogl_texture_is_sliced (tex)
            : cogl_texture_is_sliced (tex));

  return tex;
}

static void
paint (void)
{
  CoglPipeline *pipeline = cogl_pipeline_new (test_ctx);
  CoglTexture *texture = make_texture ();
  int y, x;

  cogl_pipeline_set_layer_texture (pipeline, 0, texture);

  /* Just render the texture in the top left corner */
  /* Render the texture using four separate rectangles */
  for (y = 0; y < 2; y++)
    for (x = 0; x < 2; x++)
      cogl_framebuffer_draw_textured_rectangle (test_fb,
                                                pipeline,
                                                x * TEXTURE_RENDER_SIZE / 2,
                                                y * TEXTURE_RENDER_SIZE / 2,
                                                (x + 1) *
                                                TEXTURE_RENDER_SIZE / 2,
                                                (y + 1) *
                                                TEXTURE_RENDER_SIZE / 2,
                                                x / 2.0f,
                                                y / 2.0f,
                                                (x + 1) / 2.0f,
                                                (y + 1) / 2.0f);

  cogl_object_unref (pipeline);
  cogl_object_unref (texture);
}

void
test_npot_texture (void)
{
  if (cogl_test_verbose ())
    {
      if (cogl_has_feature (test_ctx, COGL_FEATURE_ID_TEXTURE_NPOT))
        g_print ("NPOT textures are supported\n");
      else
        g_print ("NPOT textures are not supported\n");
    }

  cogl_framebuffer_orthographic (test_fb,
                                 0, 0,
                                 cogl_framebuffer_get_width (test_fb),
                                 cogl_framebuffer_get_height (test_fb),
                                 -1,
                                 100);

  paint ();
  validate_result ();

  if (cogl_test_verbose ())
    g_print ("OK\n");
}