aboutsummaryrefslogtreecommitdiff
path: root/tests/spec/ext_framebuffer_multisample/clip-and-scissor-blit.cpp
blob: d7c733ab80a6a4b0d3ac6b72dc5605377cc77311 (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
/*
 * Copyright © 2012 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.
 */

/**
 * \file clip-and-scissor-blit.cpp
 *
 * Verify the accuracy of blits incolving MSAA buffers when the blit
 * coordinates are clipped to the edges of the source or destination
 * surface, or scissored.
 *
 * The test starts by creating a source framebuffer and populating it
 * with a simple image.  It also creates a destination framebuffer.
 *
 * Then, it executes the following sequence of steps several times in
 * a loop:
 *
 * 1. Clear the destination framebuffer to gray.
 *
 * 2. Blit from the source framebuffer to the destination framebuffer,
 *    using clipping or scissoring to limit the amount of data that is
 *    blitted.
 *
 * 3. Do a simple (unclipped, unscissored) blit from the destination
 *    framebuffer to the screen.  This produces a test image.
 *
 * 4. Clear the destination framebuffer to gray.
 *
 * 5. Blit from the source framebuffer to the destination framebuffer,
 *    this time adjusting the coordinates to limit the amount of data
 *    that is blitted.
 *
 * 6. Do a simple (unclipped, unscissored) blit from the destination
 *    framebuffer to the screen.  This produces a reference image.
 *
 * 7. Verify that the test and reference images match.
 */
#include "common.h"

PIGLIT_GL_TEST_MAIN(
    600 /*window_width*/,
    320 /*window_height*/,
    PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_ALPHA)

namespace {

const int src_size[2] = { 30, 20 };
const int dst_size[2] = { 50, 40 };
const int cut_amount[2] = { 10, 7 };

enum test_type_enum
{
	TEST_TYPE_SRC,
	TEST_TYPE_DST,
	TEST_TYPE_SCISSOR,
	NUM_TEST_TYPES
};

Fbo src_fbo;
Fbo dst_fbo;

/**
 * From the GL 3.3 spec (section 4.3.2 Copying Pixels):
 *
 * If SAMPLE_BUFFERS for either the read framebuffer or draw
 * framebuffer is greater than zero, no copy is performed and an
 * INVALID_OPERATION error is generated if the dimensions of the
 * source and destination rectangles provided to BlitFramebuffer are
 * not identical, if the formats of the read and draw framebuffers are
 * not identical, or if the values of SAMPLES for the read and draw
 * buffers are not identical.
 *
 * It is not 100% clear whether "the dimensions of the source and
 * destination rectangles" are meant in a signed sense or an unsigned
 * sense--in other words, if SAMPLE_BUFFERS is greater than zero for
 * either the read or draw framebuffer, and abs(srcX0 - srcX1) ==
 * abs(dstX0 - dstX1), but (srcX0 - srcX1) and (dstX0 - dstX1) have
 * opposite signs (so that the image is being mirrored in the X
 * direction), should an INVALID_OPERATION error be generated?
 *
 * Some implementations have interpreted the answer to be yes, so we
 * only test clipping of mirrored blits when SAMPLE_BUFFERS is 0 for
 * both the read and draw framebuffers.
 *
 * This boolean is true if we should test clipping of mirrored blits.
 */
bool test_mirrored_blits = false;

void
draw_simple_src_image()
{
	glColor4f(1.0, 0.0, 0.0, 0.0);
	piglit_draw_rect(-1, -1, 1, 1);

	glColor4f(0.0, 1.0, 0.0, 0.25);
	piglit_draw_rect(0, -1, 1, 1);

	glColor4f(0.0, 0.0, 1.0, 0.5);
	piglit_draw_rect(-1, 0, 1, 1);

	glColor4f(1.0, 1.0, 1.0, 1.0);
	piglit_draw_rect(0, 0, 1, 1);
}

bool
do_test(int coord, bool clip_low, test_type_enum test_type,
	bool flip_src, bool flip_dst)
{
	/* If this test flips src but not dst (or vice versa), then it
	 * is unclear from the spec whether it should be allowed for
	 * multisampled blits, so skip it unless test_mirrored_blits
	 * is true.
	 */
	if (flip_src != flip_dst && !test_mirrored_blits)
		return true;

	/* Figure out where to draw the images */
	int display_x = (6 * coord + 2 * test_type) * dst_size[0];
	int display_y = ((clip_low ? 4 : 0) + (flip_src ? 2 : 0) +
			 (flip_dst ? 1 : 0)) * dst_size[1];

	static const char * const test_type_strings[] = {
		"clip src",
		"clip dst",
		"scissor"
	};
	printf("Testing %s %s%s%s%s at (%d, %d)\n",
	       test_type_strings[test_type],
	       clip_low ? "-" : "+",
	       coord ? "y" : "x",
	       flip_src ? " (flip src)" : "",
	       flip_dst ? " (flip dst)" : "",
	       display_x, display_y);

	/* Number of pixels we'll try to cut out of the blit by
	 * clipping or scissoring.
	 */
	int cut = cut_amount[coord];

	/* Amount by which the blits must be offset to produce an
	 * image in the center of the destination fbo.
	 */
	int dx = (dst_size[0] - src_size[0]) / 2;
	int dy = (dst_size[1] - src_size[1]) / 2;

	/* Set up blit and scissor parameters for both the test and
	 * reference blits
	 */
	int test_src[2][2] = { /* E.g. test_src[1][0] == srcY0 */
		{ 0, src_size[0] }, { 0, src_size[1] } };
	int test_dst[2][2] = { /* E.g. test_dst[1][0] == dstY0 */
		{ dx, src_size[0] + dx }, { dy, src_size[1] + dy } };
	int ref_src[2][2] = { /* E.g. test_src[1][0] == srcY0 */
		{ 0, src_size[0] }, { 0, src_size[1] } };
	int ref_dst[2][2] = { /* E.g. test_dst[1][0] == dstY0 */
		{ dx, src_size[0] + dx }, { dy, src_size[1] + dy } };
	int scissor[2][2] = { /* E.g. scissor[0] = { left, right } */
		{ 0, 0 }, { 0, 0 } };
	switch (test_type) {
	case TEST_TYPE_SRC:
		if (clip_low) {
			test_src[coord][0] += cut;
			test_src[coord][1] += cut;
			ref_src[coord][0] += cut;
			ref_dst[coord][1] -= cut;
		} else {
			test_src[coord][0] -= cut;
			test_src[coord][1] -= cut;
			ref_src[coord][1] -= cut;
			ref_dst[coord][0] += cut;
		}
		break;
	case TEST_TYPE_DST:
		if (clip_low) {
			test_dst[coord][0] = -cut;
			test_dst[coord][1] =
				test_dst[coord][0] + src_size[coord];
			ref_src[coord][0] = cut;
			ref_dst[coord][0] = 0;
			ref_dst[coord][1] = test_dst[coord][1];
		} else {
			test_dst[coord][1] = dst_size[coord] + cut;
			test_dst[coord][0] =
				test_dst[coord][1] - src_size[coord];
			ref_src[coord][1] = src_size[coord] - cut;
			ref_dst[coord][0] = test_dst[coord][0];
			ref_dst[coord][1] = dst_size[coord];
		}
		break;
	case TEST_TYPE_SCISSOR:
		if (clip_low) {
			scissor[coord][0] = test_dst[coord][0] + cut;
			scissor[coord][1] = dst_size[coord];
			ref_src[coord][0] += cut;
			ref_dst[coord][0] += cut;
		} else {
			scissor[coord][0] = 0;
			scissor[coord][1] = test_dst[coord][1] - cut;
			ref_src[coord][1] -= cut;
			ref_dst[coord][1] -= cut;
		}
		scissor[1-coord][0] = 0;
		scissor[1-coord][1] = dst_size[1-coord];
		break;
	default:
		printf("Unexpected test type\n");
		piglit_report_result(PIGLIT_FAIL);
		break;
	}

	/* Flip coordinates if requested */
	if (flip_src) {
		test_src[coord][0] = src_size[coord] - test_src[coord][0];
		test_src[coord][1] = src_size[coord] - test_src[coord][1];
		ref_src[coord][0] = src_size[coord] - ref_src[coord][0];
		ref_src[coord][1] = src_size[coord] - ref_src[coord][1];
	}
	if (flip_dst) {
		test_dst[coord][0] = dst_size[coord] - test_dst[coord][0];
		test_dst[coord][1] = dst_size[coord] - test_dst[coord][1];
		ref_dst[coord][0] = dst_size[coord] - ref_dst[coord][0];
		ref_dst[coord][1] = dst_size[coord] - ref_dst[coord][1];
		int tmp = scissor[coord][0];
		scissor[coord][0] = dst_size[coord] - scissor[coord][1];
		scissor[coord][1] = dst_size[coord] - tmp;
	}

	/* Clear the destination framebuffer to gray */
	glBindFramebuffer(GL_READ_FRAMEBUFFER, src_fbo.handle);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst_fbo.handle);
	glClearColor(0.5, 0.5, 0.5, 0.5);
	glClear(GL_COLOR_BUFFER_BIT);

	/* Set up scissor */
	glScissor(scissor[0][0],
		  scissor[1][0],
		  scissor[0][1] - scissor[0][0],
		  scissor[1][1] - scissor[1][0]);
	if (test_type == TEST_TYPE_SCISSOR)
		glEnable(GL_SCISSOR_TEST);
	else
		glDisable(GL_SCISSOR_TEST);

	/* Do the test blit */
	glBlitFramebuffer(test_src[0][0], test_src[1][0],
			  test_src[0][1], test_src[1][1],
			  test_dst[0][0], test_dst[1][0],
			  test_dst[0][1], test_dst[1][1],
			  GL_COLOR_BUFFER_BIT, GL_NEAREST);

	/* Disable scissoring */
	glDisable(GL_SCISSOR_TEST);

	/* Transfer the test image to the screen */
	glBindFramebuffer(GL_READ_FRAMEBUFFER, dst_fbo.handle);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
	glBlitFramebuffer(0, 0, dst_size[0], dst_size[1],
			  display_x, display_y,
			  display_x + dst_size[0], display_y + dst_size[1],
			  GL_COLOR_BUFFER_BIT, GL_NEAREST);

	/* Clear the destination framebuffer to gray */
	glBindFramebuffer(GL_READ_FRAMEBUFFER, src_fbo.handle);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst_fbo.handle);
	glClearColor(0.5, 0.5, 0.5, 0.5);
	glClear(GL_COLOR_BUFFER_BIT);

	/* Do the reference blit */
	glBlitFramebuffer(ref_src[0][0], ref_src[1][0],
			  ref_src[0][1], ref_src[1][1],
			  ref_dst[0][0], ref_dst[1][0],
			  ref_dst[0][1], ref_dst[1][1],
			  GL_COLOR_BUFFER_BIT, GL_NEAREST);

	/* Transfer the reference image to the screen */
	glBindFramebuffer(GL_READ_FRAMEBUFFER, dst_fbo.handle);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
	glBlitFramebuffer(0, 0, dst_size[0], dst_size[1],
			  display_x + dst_size[0], display_y,
			  display_x + 2 * dst_size[0], display_y + dst_size[1],
			  GL_COLOR_BUFFER_BIT, GL_NEAREST);

	/* Compare the test and reference images */
	glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
	return piglit_probe_rect_halves_equal_rgba(display_x, display_y,
						   2 * dst_size[0],
						   dst_size[1]);
}

void
print_usage_and_exit(char *prog_name)
{
	printf("Usage: %s <num_samples> <blit_type>\n"
	       "  where <blit_type> is one of:\n"
	       "    msaa\n"
	       "    upsample\n"
	       "    downsample\n"
	       "    normal\n",
	       prog_name);
	piglit_report_result(PIGLIT_FAIL);
}

extern "C" void
piglit_init(int argc, char **argv)
{
	piglit_require_gl_version(30);
	piglit_require_GLSL_version(130);

	if (argc < 3)
		print_usage_and_exit(argv[0]);

	int num_samples;
	{
		char *endptr = NULL;
		num_samples = strtol(argv[1], &endptr, 0);
		if (endptr != argv[1] + strlen(argv[1]))
			print_usage_and_exit(argv[0]);
	}

	/* Skip the test if num_samples > GL_MAX_SAMPLES */
	GLint max_samples;
	glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
	if (num_samples > max_samples)
		piglit_report_result(PIGLIT_SKIP);

	int src_samples;
	int dst_samples;
	if (strcmp(argv[2], "msaa") == 0) {
		src_samples = dst_samples = num_samples;
	} else if (strcmp(argv[2], "upsample") == 0) {
		src_samples = 0;
		dst_samples = num_samples;
	} else if (strcmp(argv[2], "downsample") == 0) {
		src_samples = num_samples;
		dst_samples = 0;
	} else if (strcmp(argv[2], "normal") == 0) {
		src_samples = dst_samples = 0;
		test_mirrored_blits = true;
	} else {
		print_usage_and_exit(argv[0]);
	}

	src_fbo.setup(FboConfig(src_samples, src_size[0], src_size[1]));
	dst_fbo.setup(FboConfig(dst_samples, dst_size[0], dst_size[1]));
}

extern "C" enum piglit_result
piglit_display()
{
	/* Draw a simple image in the source buffer */
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, src_fbo.handle);
	src_fbo.set_viewport();
	draw_simple_src_image();

	bool pass = true;
	for (int coord = 0; coord < 2; ++coord) {
		for (int clip_low = 0; clip_low < 2; ++clip_low) {
			for (int test_type = 0; test_type < NUM_TEST_TYPES; ++test_type) {
				for (int flip_src = 0; flip_src < 2; ++flip_src) {
					for (int flip_dst = 0; flip_dst < 2; ++flip_dst) {
						pass = do_test(coord, clip_low, test_type_enum(test_type),
							       flip_src, flip_dst) && pass;
					}
				}
			}
		}
	}

	piglit_present_results();

	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
}

} /* anonymous namespace */