1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 #include "piglit-test-pattern.h"
24 #include "piglit-fbo.h"
25 using namespace piglit_util_fbo;
26 using namespace piglit_util_test_pattern;
27 
28 /**
29  * \file unaligned-blit.cpp
30  *
31  * Verify the accuracy of blits involving MSAA buffers when the blit
32  * coordinates are not aligned to simple powers of two.
33  *
34  * This test operates through the use of a sequence of blits that
35  * might be called a "scrambling blit": a source image (whose size is
36  * not a power of two) is divided up into tiles (whose size is also
37  * not a power of two), and these tiles are blitted one at a time from
38  * the source to the destination buffer, permuting the order of the
39  * tiles in a deterministic way.  The scrambling ensures that we test
40  * a wide variety of different offsets and coordinate misalignments.
41  *
42  * The test performs the following operations: First an unscrambled
43  * test image is created in a source buffer, which may or may not be
44  * multisampled.  Then a scrambling blit is used to copy it to a
45  * destination buffer, which also may or may not be multisampled.
46  * Finally, the destination buffer is blitted to the window system
47  * framebuffer, using the inverse permutation.  This should result in
48  * an unscrambled test image.
49  *
50  * To verify that the test image is correct, we produce a reference
51  * image by repeating the same operation using ordinary unscrambled
52  * blits.
53  */
54 const int pattern_size = 245;
55 const int tile_size = 49;
56 const int tiles_across = 5;
57 const int num_tiles = tiles_across * tiles_across;
58 
59 PIGLIT_GL_TEST_CONFIG_BEGIN
60 
61 	config.supports_gl_compat_version = 10;
62 
63 	config.window_width = 2*pattern_size;
64 	config.window_height = pattern_size;
65 	config.window_visual = PIGLIT_GL_VISUAL_DOUBLE | PIGLIT_GL_VISUAL_RGBA | PIGLIT_GL_VISUAL_DEPTH | PIGLIT_GL_VISUAL_STENCIL;
66 	config.khr_no_error_support = PIGLIT_NO_ERRORS;
67 
68 PIGLIT_GL_TEST_CONFIG_END
69 
70 const int permutation[num_tiles] = {
71 	10, 5, 6, 17, 3, 11, 16, 21, 14, 24, 23, 8, 15, 18, 0, 12, 9,
72 	4, 22, 19, 20, 2, 7, 13, 1
73 };
74 
75 const int inverse_permutation[num_tiles] = {
76 	14, 24, 21, 4, 17, 1, 2, 22, 11, 16, 0, 5, 15, 23, 8, 12, 6,
77 	3, 13, 19, 20, 7, 18, 10, 9
78 };
79 
80 Fbo src_fbo;
81 Fbo dst_fbo;
82 TestPattern *test_pattern = NULL;
83 ManifestProgram *manifest_program = NULL;
84 GLbitfield buffer_to_test;
85 
86 void
scrambling_blit(const int * permutation)87 scrambling_blit(const int *permutation)
88 {
89 	for (int i = 0; i < num_tiles; ++i) {
90 		int src_x = (i % tiles_across) * tile_size;
91 		int src_y = (i / tiles_across) * tile_size;
92 		int dst_x = (permutation[i] % tiles_across) * tile_size;
93 		int dst_y = (permutation[i] / tiles_across) * tile_size;
94 		glBlitFramebuffer(src_x, src_y,
95 				  src_x + tile_size, src_y + tile_size,
96 				  dst_x, dst_y,
97 				  dst_x + tile_size, dst_y + tile_size,
98 				  buffer_to_test, GL_NEAREST);
99 	}
100 }
101 
102 void
print_usage_and_exit(char * prog_name)103 NORETURN print_usage_and_exit(char *prog_name)
104 {
105 	printf("Usage: %s <num_samples> <buffer_type> <blit_type>\n"
106 	       "  where <buffer_type> is one of:\n"
107 	       "    color\n"
108 	       "    stencil\n"
109 	       "    depth\n"
110 	       "  and <blit_type> is one of:\n"
111 	       "    msaa\n"
112 	       "    upsample\n"
113 	       "    downsample\n",
114 	       prog_name);
115 	piglit_report_result(PIGLIT_FAIL);
116 }
117 
118 void
piglit_init(int argc,char ** argv)119 piglit_init(int argc, char **argv)
120 {
121 	int num_samples;
122 	int src_samples;
123 	int dst_samples;
124 	if (argc < 4)
125 		print_usage_and_exit(argv[0]);
126 
127 	char *endptr = NULL;
128 	num_samples = strtol(argv[1], &endptr, 0);
129 	if (endptr != argv[1] + strlen(argv[1]))
130 		print_usage_and_exit(argv[0]);
131 
132 	piglit_require_gl_version(21);
133 	piglit_require_extension("GL_ARB_framebuffer_object");
134 	piglit_require_extension("GL_ARB_vertex_array_object");
135 
136 	/* Skip the test if num_samples > GL_MAX_SAMPLES */
137 	GLint max_samples;
138 	glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
139 	if (num_samples > max_samples)
140 		piglit_report_result(PIGLIT_SKIP);
141 
142 	if (strcmp(argv[2], "color") == 0) {
143 		test_pattern = new Triangles();
144 		buffer_to_test = GL_COLOR_BUFFER_BIT;
145 	} else if (strcmp(argv[2], "depth") == 0) {
146 		test_pattern = new DepthSunburst();
147 		manifest_program = new ManifestDepth();
148 		buffer_to_test = GL_DEPTH_BUFFER_BIT;
149 	} else if (strcmp(argv[2], "stencil") == 0) {
150 		test_pattern = new StencilSunburst();
151 		manifest_program = new ManifestStencil();
152 		buffer_to_test = GL_STENCIL_BUFFER_BIT;
153 	} else {
154 		print_usage_and_exit(argv[0]);
155 	}
156 
157 	if (strcmp(argv[3], "msaa") == 0) {
158 		src_samples = dst_samples = num_samples;
159 	} else if (strcmp(argv[3], "upsample") == 0) {
160 		src_samples = 0;
161 		dst_samples = num_samples;
162 	} else if (strcmp(argv[3], "downsample") == 0) {
163 		src_samples = num_samples;
164 		dst_samples = 0;
165 	} else {
166 		print_usage_and_exit(argv[0]);
167 	}
168 
169 	test_pattern->compile();
170 	if (manifest_program)
171 		manifest_program->compile();
172 	src_fbo.setup(FboConfig(src_samples, pattern_size, pattern_size));
173 	dst_fbo.setup(FboConfig(dst_samples, pattern_size, pattern_size));
174 }
175 
176 enum piglit_result
piglit_display()177 piglit_display()
178 {
179 	bool pass = true;
180 
181 	/* Draw the test pattern in src_fbo. */
182 	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, src_fbo.handle);
183 	src_fbo.set_viewport();
184 	test_pattern->draw(TestPattern::no_projection);
185 
186 	/* Blit from src_fbo to dst_fbo, scrambling the pattern as we go. */
187 	glBindFramebuffer(GL_READ_FRAMEBUFFER, src_fbo.handle);
188 	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst_fbo.handle);
189 	scrambling_blit(permutation);
190 
191 	/* Blit from dst_fbo to the left half of the window system
192 	 * framebuffer, unscrambling as we go.
193 	 */
194 	glBindFramebuffer(GL_READ_FRAMEBUFFER, dst_fbo.handle);
195 	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
196 	scrambling_blit(inverse_permutation);
197 
198 	/* Blit from src_fbo to dst_fbo with no scrambling. */
199 	glBindFramebuffer(GL_READ_FRAMEBUFFER, src_fbo.handle);
200 	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst_fbo.handle);
201 	glBlitFramebuffer(0, 0, pattern_size, pattern_size,
202 			  0, 0, pattern_size, pattern_size,
203 			  buffer_to_test, GL_NEAREST);
204 
205 	/* Blit from dst_fbo to the right half of the window system
206 	 * framebuffer, with no scrambling.
207 	 */
208 	glBindFramebuffer(GL_READ_FRAMEBUFFER, dst_fbo.handle);
209 	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, piglit_winsys_fbo);
210 	glBlitFramebuffer(0, 0, pattern_size, pattern_size,
211 			  pattern_size, 0, pattern_size*2, pattern_size,
212 			  buffer_to_test, GL_NEAREST);
213 
214 	/* If we were testing depth or stencil, manifest the image so
215 	 * that we can see it.
216 	 */
217 	glViewport(0, 0, piglit_width, piglit_height);
218 	if (manifest_program)
219 		manifest_program->run();
220 
221 	/* Check that the left and right halves of the screen match. */
222 	glBindFramebuffer(GL_READ_FRAMEBUFFER, piglit_winsys_fbo);
223 	pass = piglit_probe_rect_halves_equal_rgba(0, 0, piglit_width,
224 						   piglit_height) && pass;
225 
226 	pass = piglit_check_gl_error(GL_NO_ERROR) && pass;
227 
228 	piglit_present_results();
229 
230 	return pass ? PIGLIT_PASS : PIGLIT_FAIL;
231 }
232