summaryrefslogtreecommitdiff
path: root/tests/fbo/fbo-blit-stretch.cpp
blob: f9909546fa553c78058b89d0298774d63a2b0b9e (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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
 * Copyright (C) 2012 VMware, Inc.
 * Copyright (C) 2010 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Jose Fonseca
 *    Eric Anholt <eric@anholt.net>
 *    Brian Paul
 */

/** @file fbo-blit-stretch.c
 *
 * Tests EXT_framebuffer_blit with various combinations of window system and
 * FBO objects.  Because FBOs are generally stored inverted relative to
 * window system frambuffers, this could catch flipping failures in blit paths.
 *
 * See also fbo-blit.c
 */

#include <algorithm>

#include "piglit-util-gl-common.h"


/*
 * XXX: Checkerboard is not a good test pattern, because OpenGL spec allows the
 * implementation to clamp against source rectangle edge, as oposed to clamp
 * against the source edges, causing different results along the edge.
 */
#define CHECKERBOARD 0

#define DSTW 200
#define DSTH 150

PIGLIT_GL_TEST_MAIN(
    DSTW /*window_width*/,
    DSTH /*window_height*/,
    GLUT_RGBA | GLUT_DOUBLE)

struct TestCase
{
	GLint srcW, srcH;
	GLint srcX0; GLint srcY0; GLint srcX1; GLint srcY1;
	GLint dstX0; GLint dstY0; GLint dstX1; GLint dstY1;
	GLenum filter;
};

static void
describe(const TestCase &test)
{
	GLint dstW = piglit_width;
	GLint dstH = piglit_height;

	printf("%ix%i (%i, %i)-(%i, %i) => %ix%i (%i, %i)-(%i, %i)",
	       test.srcW, test.srcH,
	       test.srcX0, test.srcY0, test.srcX1, test.srcY1,
	       dstW, dstH,
	       test.dstX0, test.dstY0, test.dstX1, test.dstY1);

	GLint srcDX = test.srcX1 - test.srcX0;
	GLint srcDY = test.srcY1 - test.srcY0;
	GLint dstDX = test.dstX1 - test.dstX0;
	GLint dstDY = test.dstY1 - test.dstY0;

	if (srcDX < 0) {
		printf(" flip_src_x");
		srcDX = -srcDX;
	}
	if (srcDY < 0) {
		printf(" flip_src_y");
		srcDY = -srcDY;
	}
	if (dstDX < 0) {
		printf(" flip_dst_x");
		dstDX = -dstDX;
	}
	if (dstDY < 0) {
		printf(" flip_dst_y");
		dstDY = -dstDY;
	}

	if (dstDX > srcDX)
		printf(" stretch_x");
	if (dstDX < srcDX)
		printf(" shrink_x");

	if (dstDY > srcDY)
		printf(" stretch_y");
	if (dstDY < srcDY)
		printf(" shrink_y");

	if (test.srcX0 < 0 || test.srcX0 > test.srcW ||
	    test.srcX1 < 0 || test.srcX1 > test.srcW)
		printf(" clamp_src_x");
	if (test.srcY0 < 0 || test.srcY0 > test.srcH ||
	    test.srcY1 < 0 || test.srcY1 > test.srcH)
		printf(" clamp_src_y");

	if (test.dstX0 < 0 || test.dstX0 > dstW ||
	    test.dstX1 < 0 || test.dstX1 > dstW)
		printf(" clamp_dst_x");
	if (test.dstY0 < 0 || test.dstY0 > dstH ||
	    test.dstY1 < 0 || test.dstY1 > dstH)
		printf(" clamp_dst_y");

	switch (test.filter) {
	case GL_NEAREST:
		printf(" nearest");
		break;
	case GL_LINEAR:
		printf(" linear");
		break;
	default:
		assert(0);
	}

	printf("\n");
}

static void
filter(const TestCase &test, float coord, GLint &coord0, GLint &coord1, float &weight)
{
	switch (test.filter) {
	case GL_NEAREST:
		coord0 = roundf(coord);
		// ambigious
		assert(fabsf(coord0 - coord) != 0.5f);
		weight = 0.0f;
		break;
	case GL_LINEAR:
		coord0 = floorf(coord);
		weight = coord - (float)coord0;
		break;
	default:
		assert(0);
		coord0 = 0;
		weight = 0.0f;
	}

	assert(weight >= 0.0f);
	assert(weight  < 1.0f);

	coord1 = coord0 + 1;
}

static void
clamp(GLint &x, GLint xmin, GLint xmax)
{
	if (x < xmin) {
		x = xmin;
	}
	if (x > xmax) {
		x = xmax;
	}
}

static float
lerp(float x0, float x1, float w)
{
	return x0 + (x1 - x0) * w;
}

static float
lerp2d(float xy00, float xy01, float xy10, float xy11, float wx, float wy)
{
	float y0 = lerp(xy00, xy01, wx);
	float y1 = lerp(xy10, xy11, wx);
	return lerp(y0, y1, wy);
}

static float clearColor[4] = {
#if CHECKERBOARD
	0.0, 0.0, 1.0, 1.0
#else
	0.5, 0.5, 0.5, 0.5
#endif
};

static GLboolean
verify(const TestCase &test, GLuint srcFBO, GLuint dstFBO, GLuint numChannels)
{
	GLint srcX0 = test.srcX0;
	GLint srcY0 = test.srcY0;
	GLint srcX1 = test.srcX1;
	GLint srcY1 = test.srcY1;
	GLint dstX0 = test.dstX0;
	GLint dstY0 = test.dstY0;
	GLint dstX1 = test.dstX1;
	GLint dstY1 = test.dstY1;

	if (dstX1 < dstX0) {
		std::swap(srcX0, srcX1);
		std::swap(dstX0, dstX1);
	}
	if (dstY1 < dstY0) {
		std::swap(srcY0, srcY1);
		std::swap(dstY0, dstY1);
	}

	GLint srcDX = srcX1 - srcX0;
	GLint srcDY = srcY1 - srcY0;
	GLint dstDX = dstX1 - dstX0;
	GLint dstDY = dstY1 - dstY0;

	GLint dstW = piglit_width;
	GLint dstH = piglit_height;

	float *srcPixels = new float[test.srcH * test.srcW * numChannels];

	glBindFramebuffer(GL_READ_FRAMEBUFFER, srcFBO);
	glReadPixels(0, 0, test.srcW, test.srcH, GL_RGB, GL_FLOAT, srcPixels);

	float *expectedDstPixels = new float[dstH * dstW * numChannels];

	for (GLint dstY = 0; dstY < dstH; ++dstY) {
		for (GLint dstX = 0; dstX < dstW; ++dstX) {
			float *dstPixel = expectedDstPixels + (dstY * dstW + dstX) * numChannels;
			for (GLuint c = 0; c < numChannels; ++c) {
				dstPixel[c] = clearColor[c];
			}
		}
	}

	GLint dstX0clamped = std::max(dstX0, 0);
	GLint dstY0clamped = std::max(dstY0, 0);
	GLint dstX1clamped = std::min(dstX1, dstW);
	GLint dstY1clamped = std::min(dstY1, dstH);

	for (GLint dstY = dstY0clamped; dstY < dstY1clamped; ++dstY) {
		float srcY = srcY0 + (dstY - dstY0 + 0.5) * srcDY / dstDY;
		if (srcY < 0 || srcY >= test.srcH) {
			continue;
		}

		srcY -= 0.5f;

		GLint srcPixelY0, srcPixelY1;
		float weightY;
		filter(test, srcY, srcPixelY0, srcPixelY1, weightY);
		clamp(srcPixelY0, 0, test.srcH - 1);
		clamp(srcPixelY1, 0, test.srcH - 1);

		for (GLint dstX = dstX0clamped; dstX < dstX1clamped; ++dstX) {
			float srcX = srcX0 + (dstX - dstX0 + 0.5) * srcDX / dstDX;
			if (srcX < 0 || srcX >= test.srcW) {
				continue;
			}

			srcX -= 0.5f;

			GLint srcPixelX0, srcPixelX1;
			float weightX;
			filter(test, srcX, srcPixelX0, srcPixelX1, weightX);
			clamp(srcPixelX0, 0, test.srcW - 1);
			clamp(srcPixelX1, 0, test.srcW - 1);

			float *srcPixel00 = srcPixels + (srcPixelY0 * test.srcW + srcPixelX0) * numChannels;
			float *srcPixel01 = srcPixels + (srcPixelY0 * test.srcW + srcPixelX1) * numChannels;
			float *srcPixel10 = srcPixels + (srcPixelY1 * test.srcW + srcPixelX0) * numChannels;
			float *srcPixel11 = srcPixels + (srcPixelY1 * test.srcW + srcPixelX1) * numChannels;

			float *dstPixel = expectedDstPixels
				        + (dstY * dstW + dstX) * numChannels;

			for (GLuint c = 0; c < numChannels; ++c) {
				dstPixel[c] = lerp2d(srcPixel00[c],
						     srcPixel01[c],
						     srcPixel10[c],
						     srcPixel11[c],
						     weightX, weightY);
			}
		}
	}

	delete [] srcPixels;

	float *observedDstPixels = new float[dstH * dstW * numChannels];
	glBindFramebuffer(GL_READ_FRAMEBUFFER, dstFBO);
	glReadPixels(0, 0, dstW, dstH, GL_RGB, GL_FLOAT, observedDstPixels);

	GLboolean pass;
	pass = piglit_compare_images_color(0, 0, dstW, dstH, numChannels,
			                   piglit_tolerance,
					   expectedDstPixels,
					   observedDstPixels);

	delete [] observedDstPixels;
	delete [] expectedDstPixels;

	return pass;
}

static void
blit(const TestCase &test)
{
	glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
	glClear(GL_COLOR_BUFFER_BIT);
	glBlitFramebuffer(test.srcX0, test.srcY0, test.srcX1, test.srcY1,
			  test.dstX0, test.dstY0, test.dstX1, test.dstY1,
			  GL_COLOR_BUFFER_BIT, test.filter);
}


static GLboolean
run_test(const TestCase &test)
{
	describe(test);

	GLboolean pass;

	GLuint tex;
	GLuint fbo;
	GLenum status;

	glGenFramebuffers(1, &fbo);
	glBindFramebuffer(GL_FRAMEBUFFER, fbo);

#if CHECKERBOARD
	const float color1[4] = {1.0f, 0.0f, 0.0f, 1.0f};
	const float color2[4] = {0.0f, 1.0f, 0.0f, 1.0f};
	tex = piglit_checkerboard_texture(0, 0, test.srcW, test.srcH, 1, 1,  color1, color2);
#else
	tex = piglit_rgbw_texture(GL_RGBA, test.srcW, test.srcH, GL_FALSE, GL_TRUE, GL_UNSIGNED_NORMALIZED);
#endif
	glBindTexture(GL_TEXTURE_2D, tex);

	glFramebufferTexture2D(GL_FRAMEBUFFER,
			       GL_COLOR_ATTACHMENT0,
			       GL_TEXTURE_2D,
			       tex,
			       0);
	assert(glGetError() == 0);

	status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
	if (status != GL_FRAMEBUFFER_COMPLETE) {
		pass = GL_TRUE;
	} else {
		glViewport(0, 0, piglit_width, piglit_height);
		piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

		glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

		blit(test);

		pass = verify(test, fbo, 0, 3);

		if (!piglit_automatic) {
			piglit_present_results();
			if (!pass) {
				//getchar();
			}
		}
	}

	glDeleteFramebuffers(1, &fbo);
	glDeleteTextures(1, &tex);

	return pass;
}

/*
 * Constants to help define several test cases.
 */

#define SRCW 45
#define SRCH 79
#define DX 17
#define DY 11
#define SRCXMIN 13
#define SRCYMIN 33
#define SRCXMAX (SRCXMIN + DX)
#define SRCYMAX (SRCYMIN + DY)

#define DSTXMIN 19
#define DSTYMIN 23
#define DSTXMAX (DSTXMIN + DX)
#define DSTYMAX (DSTYMIN + DY)

const TestCase
tests[] = {
	/*
	 * Basic 1:1 copy
	 */

	{
		SRCW, SRCH,
		SRCXMIN, SRCYMIN, SRCXMAX, SRCYMAX,
		DSTXMIN, DSTYMIN, DSTXMAX, DSTYMAX,
		GL_NEAREST,
	},

	/*
	 * Flip tests
	 */

	{
		SRCW, SRCH,
		SRCXMAX, SRCYMAX, SRCXMIN, SRCYMIN, // flip xy
		DSTXMAX, DSTYMAX, DSTXMIN, DSTYMIN, // flip xy
		GL_NEAREST,
	},
	{
		SRCW, SRCH,
		SRCXMAX, SRCYMIN, SRCXMIN, SRCYMAX, // fliped x
		DSTXMIN, DSTYMAX, DSTXMAX, DSTYMIN, // fliped y
		GL_NEAREST,
	},
	{
		SRCW, SRCH,
		SRCXMIN, SRCYMAX, SRCXMAX, SRCYMIN, // fliped y
		DSTXMAX, DSTYMIN, DSTXMIN, DSTYMAX, // fliped x
		GL_NEAREST,
	},

	/*
	 * Stretch.
	 */

	{
		SRCW, SRCH,
		SRCXMIN, SRCYMIN, SRCXMAX, SRCYMAX,
		DSTXMIN, DSTYMIN, DSTXMAX + 3*DX, DSTYMAX + 3*DY, // strech x y
		GL_NEAREST,
	},
	{
		SRCW, SRCH,
		SRCXMIN, SRCYMIN, SRCXMAX, SRCYMAX,
		DSTXMIN, DSTYMIN, DSTXMAX + 3*DX, DSTYMAX + 3*DY, // strech x y
		GL_NEAREST,
	},

	/*
	 * Clip
	 */

	{
		SRCW, SRCH,
		SRCXMIN, SRCYMIN, SRCXMAX, SRCYMAX,
		-DX/2, -DY/2, -DX/2 + DX, -DY/2 + DY, // clip dst left bottom
		GL_NEAREST,
	},
	{
		SRCW, SRCH,
		SRCXMIN, SRCYMIN, SRCXMAX, SRCYMAX,
		DSTW - DX/2, DSTH - DY/2, DSTW - DX/2 + DX, DSTH - DY/2 + DY, // clip dst top right
		GL_NEAREST,
	},
	{
		SRCW, SRCH,
		-DX/2, -DY/2, -DX/2 + DX, -DY/2 + DY, // clip src left bottom
		DSTXMIN, DSTYMIN, DSTXMAX, DSTYMAX,
		GL_NEAREST,
	},
	{
		SRCW, SRCH,
		SRCW - DX/2, SRCH - DY/2, SRCW - DX/2 + DX, SRCH - DY/2 + DY, // clip src top right
		DSTXMIN, DSTYMIN, DSTXMAX, DSTYMAX,
		GL_NEAREST,
	},

	/*
	 * Clip & stretch.
	 *
	 * XXX: These tests are disabled for now, because Mesa clips in integer
	 * coordinates, instead of floats, which ends up affecting how the
	 * whole surface is interpolated, which goes against the spec.
	 */
#if 0
	{
		SRCW, SRCH,
		SRCXMIN, SRCYMIN, SRCXMAX, SRCYMAX,
		-DSTXMIN, -DSTYMIN, DSTXMAX, DSTYMAX, // clip dst left bottom
		GL_NEAREST,
	},
	{
		SRCW, SRCH,
		SRCXMIN, SRCYMIN, SRCXMAX, SRCYMAX,
		DSTXMIN, DSTYMIN, DSTW + DSTXMIN, DSTH + DSTYMIN, // clip dst top right
		GL_NEAREST,
	},
	{
		SRCW, SRCH,
		-SRCXMIN, -SRCYMIN, SRCXMAX, SRCYMAX, // clip src left bottom
		DSTXMIN, DSTYMIN, DSTXMAX, DSTYMAX,
		GL_NEAREST,
	},
	{
		SRCW, SRCH,
		SRCXMIN, SRCYMIN, SRCW + SRCXMIN, SRCH + SRCYMIN, // clip src top right
		DSTXMIN, DSTYMIN, DSTXMAX, DSTYMAX,
		GL_NEAREST,
	},
	{
		SRCW, SRCH,
		SRCXMAX, SRCYMIN, SRCXMIN, SRCYMAX, // fliped x
		-DSTXMIN, DSTH + DSTYMIN, DSTW + DSTXMIN, -DSTYMIN, // fliped y, cliped x y
		GL_NEAREST,
	},
#endif

	/*
	 * Full stretch
	 */

	{
		SRCW, SRCH,
		0, 0, SRCW, SRCH,
		0, 0, DSTW, DSTH,
		GL_NEAREST,
	},
};

enum piglit_result
piglit_display(void)
{
	GLboolean pass = GL_TRUE;
	for (unsigned i = 0; i < ARRAY_SIZE(tests); i++) {
		TestCase test = tests[i];

		test.filter = GL_NEAREST;
		pass = run_test(test) && pass;

		test.filter = GL_LINEAR;
		pass = run_test(test) && pass;
	}
	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}


void
piglit_init(int argc, char **argv)
{
	piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);

	piglit_require_extension("GL_ARB_framebuffer_object");
}