summaryrefslogtreecommitdiff
path: root/tests/conform/test-blend-strings.c
blob: 05f853526d7bb49b6873eb0c275b2839b36ca88e (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include <cogl/cogl.h>

#include <string.h>

#include "test-utils.h"

#define QUAD_WIDTH 20

#define RED 0
#define GREEN 1
#define BLUE 2
#define ALPHA 3

#define MASK_RED(COLOR)   ((COLOR & 0xff000000) >> 24)
#define MASK_GREEN(COLOR) ((COLOR & 0xff0000) >> 16)
#define MASK_BLUE(COLOR)  ((COLOR & 0xff00) >> 8)
#define MASK_ALPHA(COLOR) (COLOR & 0xff)

#define BLEND_CONSTANT_UNUSED 0xDEADBEEF
#define TEX_CONSTANT_UNUSED   0xDEADBEEF

typedef struct _TestState
{
  CoglContext *ctx;
} TestState;


static void
test_blend (TestState *state,
            int x,
            int y,
            guint32 src_color,
            guint32 dst_color,
            const char *blend_string,
            guint32 blend_constant,
            guint32 expected_result)
{
  /* src color */
  guint8 Sr = MASK_RED (src_color);
  guint8 Sg = MASK_GREEN (src_color);
  guint8 Sb = MASK_BLUE (src_color);
  guint8 Sa = MASK_ALPHA (src_color);
  /* dest color */
  guint8 Dr = MASK_RED (dst_color);
  guint8 Dg = MASK_GREEN (dst_color);
  guint8 Db = MASK_BLUE (dst_color);
  guint8 Da = MASK_ALPHA (dst_color);
  /* blend constant - when applicable */
  guint8 Br = MASK_RED (blend_constant);
  guint8 Bg = MASK_GREEN (blend_constant);
  guint8 Bb = MASK_BLUE (blend_constant);
  guint8 Ba = MASK_ALPHA (blend_constant);
  CoglColor blend_const_color;

  CoglHandle material;
  CoglPipeline *pipeline;
  gboolean status;
  GError *error = NULL;
  int y_off;
  int x_off;

  /* First write out the destination color without any blending... */
  pipeline = cogl_pipeline_new (state->ctx);
  cogl_pipeline_set_color4ub (pipeline, Dr, Dg, Db, Da);
  cogl_pipeline_set_blend (pipeline, "RGBA = ADD (SRC_COLOR, 0)", NULL);
  cogl_set_source (pipeline);
  cogl_rectangle (x * QUAD_WIDTH,
                  y * QUAD_WIDTH,
                  x * QUAD_WIDTH + QUAD_WIDTH,
                  y * QUAD_WIDTH + QUAD_WIDTH);
  cogl_object_unref (pipeline);

  /*
   * Now blend a rectangle over our well defined destination:
   */

  pipeline = cogl_pipeline_new (state->ctx);
  cogl_pipeline_set_color4ub (pipeline, Sr, Sg, Sb, Sa);

  status = cogl_pipeline_set_blend (pipeline, blend_string, &error);
  if (!status)
    {
      /* It's not strictly a test failure; you need a more capable GPU or
       * driver to test this blend string. */
      if (cogl_test_verbose ())
	{
	  g_debug ("Failed to test blend string %s: %s",
		   blend_string, error->message);
	  g_print ("Skipping\n");
	}
      return;
    }

  cogl_color_init_from_4ub (&blend_const_color, Br, Bg, Bb, Ba);
  cogl_pipeline_set_blend_constant (pipeline, &blend_const_color);

  cogl_set_source (pipeline);
  cogl_rectangle (x * QUAD_WIDTH,
                  y * QUAD_WIDTH,
                  x * QUAD_WIDTH + QUAD_WIDTH,
                  y * QUAD_WIDTH + QUAD_WIDTH);
  cogl_object_unref (pipeline);

  /* See what we got... */

  y_off = y * QUAD_WIDTH + (QUAD_WIDTH / 2);
  x_off = x * QUAD_WIDTH + (QUAD_WIDTH / 2);

  if (cogl_test_verbose ())
    {
      g_print ("test_blend (%d, %d):\n%s\n", x, y, blend_string);
      g_print ("  src color = %02x, %02x, %02x, %02x\n", Sr, Sg, Sb, Sa);
      g_print ("  dst color = %02x, %02x, %02x, %02x\n", Dr, Dg, Db, Da);
      if (blend_constant != BLEND_CONSTANT_UNUSED)
        g_print ("  blend constant = %02x, %02x, %02x, %02x\n",
                 Br, Bg, Bb, Ba);
      else
        g_print ("  blend constant = UNUSED\n");
    }

  test_utils_check_pixel (x_off, y_off, expected_result);


  /*
   * Test with legacy API
   */

  /* Clear previous work */
  cogl_set_source_color4ub (0, 0, 0, 0xff);
  cogl_rectangle (x * QUAD_WIDTH,
                  y * QUAD_WIDTH,
                  x * QUAD_WIDTH + QUAD_WIDTH,
                  y * QUAD_WIDTH + QUAD_WIDTH);

  /* First write out the destination color without any blending... */
  material = cogl_material_new ();
  cogl_material_set_color4ub (material, Dr, Dg, Db, Da);
  cogl_material_set_blend (material, "RGBA = ADD (SRC_COLOR, 0)", NULL);
  cogl_set_source (material);
  cogl_rectangle (x * QUAD_WIDTH,
                  y * QUAD_WIDTH,
                  x * QUAD_WIDTH + QUAD_WIDTH,
                  y * QUAD_WIDTH + QUAD_WIDTH);
  cogl_handle_unref (material);

  /*
   * Now blend a rectangle over our well defined destination:
   */

  material = cogl_material_new ();
  cogl_material_set_color4ub (material, Sr, Sg, Sb, Sa);

  status = cogl_material_set_blend (material, blend_string, &error);
  if (!status)
    {
      /* This is a failure as it must be equivalent to the new API */
      g_warning ("Error setting blend string %s: %s",
		 blend_string, error->message);
      g_assert_not_reached ();
    }

  cogl_color_init_from_4ub (&blend_const_color, Br, Bg, Bb, Ba);
  cogl_material_set_blend_constant (material, &blend_const_color);

  cogl_set_source (material);
  cogl_rectangle (x * QUAD_WIDTH,
                  y * QUAD_WIDTH,
                  x * QUAD_WIDTH + QUAD_WIDTH,
                  y * QUAD_WIDTH + QUAD_WIDTH);
  cogl_handle_unref (material);

  /* See what we got... */

  test_utils_check_pixel (x_off, y_off, expected_result);
}

static CoglHandle
make_texture (guint32 color)
{
  guchar *tex_data, *p;
  guint8 r = MASK_RED (color);
  guint8 g = MASK_GREEN (color);
  guint8 b = MASK_BLUE (color);
  guint8 a = MASK_ALPHA (color);
  CoglHandle tex;

  tex_data = g_malloc (QUAD_WIDTH * QUAD_WIDTH * 4);

  for (p = tex_data + QUAD_WIDTH * QUAD_WIDTH * 4; p > tex_data;)
    {
      *(--p) = a;
      *(--p) = b;
      *(--p) = g;
      *(--p) = r;
    }

  /* Note: we don't use COGL_PIXEL_FORMAT_ANY for the internal format here
   * since we don't want to allow Cogl to premultiply our data. */
  tex = cogl_texture_new_from_data (QUAD_WIDTH,
                                    QUAD_WIDTH,
                                    COGL_TEXTURE_NONE,
                                    COGL_PIXEL_FORMAT_RGBA_8888,
                                    COGL_PIXEL_FORMAT_RGBA_8888,
                                    QUAD_WIDTH * 4,
                                    tex_data);

  g_free (tex_data);

  return tex;
}

static void
test_tex_combine (TestState *state,
                  int x,
                  int y,
                  guint32 tex0_color,
                  guint32 tex1_color,
                  guint32 combine_constant,
                  const char *combine_string,
                  guint32 expected_result)
{
  CoglHandle tex0, tex1;

  /* combine constant - when applicable */
  guint8 Cr = MASK_RED (combine_constant);
  guint8 Cg = MASK_GREEN (combine_constant);
  guint8 Cb = MASK_BLUE (combine_constant);
  guint8 Ca = MASK_ALPHA (combine_constant);
  CoglColor combine_const_color;

  CoglHandle material;
  gboolean status;
  GError *error = NULL;
  int y_off;
  int x_off;


  tex0 = make_texture (tex0_color);
  tex1 = make_texture (tex1_color);

  material = cogl_material_new ();

  cogl_material_set_color4ub (material, 0x80, 0x80, 0x80, 0x80);
  cogl_material_set_blend (material, "RGBA = ADD (SRC_COLOR, 0)", NULL);

  cogl_material_set_layer (material, 0, tex0);
  cogl_material_set_layer_combine (material, 0,
                                   "RGBA = REPLACE (TEXTURE)", NULL);

  cogl_material_set_layer (material, 1, tex1);
  status = cogl_material_set_layer_combine (material, 1,
                                            combine_string, &error);
  if (!status)
    {
      /* It's not strictly a test failure; you need a more capable GPU or
       * driver to test this texture combine string. */
      g_debug ("Failed to test texture combine string %s: %s",
               combine_string, error->message);
    }

  cogl_color_init_from_4ub (&combine_const_color, Cr, Cg, Cb, Ca);
  cogl_material_set_layer_combine_constant (material, 1, &combine_const_color);

  cogl_set_source (material);
  cogl_rectangle (x * QUAD_WIDTH,
                  y * QUAD_WIDTH,
                  x * QUAD_WIDTH + QUAD_WIDTH,
                  y * QUAD_WIDTH + QUAD_WIDTH);

  cogl_handle_unref (material);
  cogl_handle_unref (tex0);
  cogl_handle_unref (tex1);

  /* See what we got... */

  y_off = y * QUAD_WIDTH + (QUAD_WIDTH / 2);
  x_off = x * QUAD_WIDTH + (QUAD_WIDTH / 2);

  if (cogl_test_verbose ())
    {
      g_print ("test_tex_combine (%d, %d):\n%s\n", x, y, combine_string);
      g_print ("  texture 0 color = 0x%08lX\n", (unsigned long)tex0_color);
      g_print ("  texture 1 color = 0x%08lX\n", (unsigned long)tex1_color);
      if (combine_constant != TEX_CONSTANT_UNUSED)
        g_print ("  combine constant = %02x, %02x, %02x, %02x\n",
                 Cr, Cg, Cb, Ca);
      else
        g_print ("  combine constant = UNUSED\n");
    }

  test_utils_check_pixel (x_off, y_off, expected_result);
}

static void
paint (TestState *state)
{
  test_blend (state, 0, 0, /* position */
              0xff0000ff, /* src */
              0xffffffff, /* dst */
              "RGBA = ADD (SRC_COLOR, 0)",
              BLEND_CONSTANT_UNUSED,
              0xff0000ff); /* expected */

  test_blend (state, 1, 0, /* position */
              0x11223344, /* src */
              0x11223344, /* dst */
              "RGBA = ADD (SRC_COLOR, DST_COLOR)",
              BLEND_CONSTANT_UNUSED,
              0x22446688); /* expected */

  test_blend (state, 2, 0, /* position */
              0x80808080, /* src */
              0xffffffff, /* dst */
              "RGBA = ADD (SRC_COLOR * (CONSTANT), 0)",
              0x80808080, /* constant (RGBA all = 0.5 when normalized) */
              0x40404040); /* expected */

  test_blend (state, 3, 0, /* position */
              0x80000080, /* src (alpha = 0.5 when normalized) */
              0x40000000, /* dst */
              "RGBA = ADD (SRC_COLOR * (SRC_COLOR[A]),"
              "            DST_COLOR * (1-SRC_COLOR[A]))",
              BLEND_CONSTANT_UNUSED,
              0x60000040); /* expected */

  /* XXX:
   * For all texture combine tests tex0 will use a combine mode of
   * "RGBA = REPLACE (TEXTURE)"
   */

  test_tex_combine (state, 4, 0, /* position */
                    0x11111111, /* texture 0 color */
                    0x22222222, /* texture 1 color */
                    TEX_CONSTANT_UNUSED,
                    "RGBA = ADD (PREVIOUS, TEXTURE)", /* tex combine */
                    0x33333333); /* expected */

  test_tex_combine (state, 5, 0, /* position */
                    0x40404040, /* texture 0 color */
                    0x80808080, /* texture 1 color (RGBA all = 0.5) */
                    TEX_CONSTANT_UNUSED,
                    "RGBA = MODULATE (PREVIOUS, TEXTURE)", /* tex combine */
                    0x20202020); /* expected */

  test_tex_combine (state, 6, 0, /* position */
                    0xffffff80, /* texture 0 color (alpha = 0.5) */
                    0xDEADBE40, /* texture 1 color */
                    TEX_CONSTANT_UNUSED,
                    "RGB = REPLACE (PREVIOUS)"
                    "A = MODULATE (PREVIOUS, TEXTURE)", /* tex combine */
                    0xffffff20); /* expected */

  /* XXX: we are assuming test_tex_combine creates a material with
   * a color of 0x80808080 (i.e. the "PRIMARY" color) */
  test_tex_combine (state, 7, 0, /* position */
                    0xffffff80, /* texture 0 color (alpha = 0.5) */
                    0xDEADBE20, /* texture 1 color */
                    TEX_CONSTANT_UNUSED,
                    "RGB = REPLACE (PREVIOUS)"
                    "A = MODULATE (PRIMARY, TEXTURE)", /* tex combine */
                    0xffffff10); /* expected */

  test_tex_combine (state, 8, 0, /* position */
                    0x11111111, /* texture 0 color */
                    0x22222222, /* texture 1 color */
                    TEX_CONSTANT_UNUSED,
                    "RGBA = ADD (PREVIOUS, 1-TEXTURE)", /* tex combine */
                    0xeeeeeeee); /* expected */

  /* this is again assuming a primary color of 0x80808080 */
  test_tex_combine (state, 9, 0, /* position */
                    0x10101010, /* texture 0 color */
                    0x20202020, /* texture 1 color */
                    TEX_CONSTANT_UNUSED,
                    "RGBA = INTERPOLATE (PREVIOUS, TEXTURE, PRIMARY)",
                    0x18181818); /* expected */

#if 0 /* using TEXTURE_N appears to be broken in cogl-blend-string.c */
  test_tex_combine (state, 0, 1, /* position */
                    0xDEADBEEF, /* texture 0 color (not used) */
                    0x11223344, /* texture 1 color */
                    TEX_CONSTANT_UNUSED,
                    "RGBA = ADD (TEXTURE_1, TEXTURE)", /* tex combine */
                    0x22446688); /* expected */
#endif

  test_tex_combine (state, 1, 1, /* position */
                    0x21314151, /* texture 0 color */
                    0x99999999, /* texture 1 color */
                    TEX_CONSTANT_UNUSED,
                    "RGBA = ADD_SIGNED (PREVIOUS, TEXTURE)", /* tex combine */
                    0x3a4a5a6a); /* expected */

  test_tex_combine (state, 2, 1, /* position */
                    0xfedcba98, /* texture 0 color */
                    0x11111111, /* texture 1 color */
                    TEX_CONSTANT_UNUSED,
                    "RGBA = SUBTRACT (PREVIOUS, TEXTURE)", /* tex combine */
                    0xedcba987); /* expected */

  test_tex_combine (state, 3, 1, /* position */
                    0x8899aabb, /* texture 0 color */
                    0xbbaa9988, /* texture 1 color */
                    TEX_CONSTANT_UNUSED,
                    "RGB = DOT3_RGBA (PREVIOUS, TEXTURE)"
                    "A = REPLACE (PREVIOUS)",
                    0x2a2a2abb); /* expected */
}

void
test_cogl_blend_strings (TestUtilsGTestFixture *fixture,
                         void *data)
{
  TestUtilsSharedState *shared_state = data;
  TestState state;

  state.ctx = shared_state->ctx;

  cogl_ortho (0, cogl_framebuffer_get_width (shared_state->fb), /* left, right */
              cogl_framebuffer_get_height (shared_state->fb), 0, /* bottom, top */
              -1, 100 /* z near, far */);

  paint (&state);

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